]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.cpp
mozilla plugin: rework events listeners to dom level 2 type, sort of.
[vlc] / projects / mozilla / vlcplugin.cpp
1 /*****************************************************************************
2  * vlcplugin.cpp: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf.fouilleul@laposte.net>
9  *          Jean-Paul Saman <jpsaman@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "config.h"
30
31 #ifdef HAVE_MOZILLA_CONFIG_H
32 #   include <mozilla-config.h>
33 #endif
34
35 #include "vlcplugin.h"
36 #include "control/npolibvlc.h"
37
38 #include <ctype.h>
39 #include <pthread.h>
40
41 /*****************************************************************************
42  * VlcPlugin constructor and destructor
43  *****************************************************************************/
44 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
45     i_npmode(mode),
46     b_stream(0),
47     b_autoplay(1),
48     b_toolbar(0),
49     psz_text(NULL),
50     psz_target(NULL),
51     playlist_index(-1),
52     libvlc_instance(NULL),
53     libvlc_media_list(NULL),
54     libvlc_media_player(NULL),
55     p_scriptClass(NULL),
56     p_browser(instance),
57     psz_baseURL(NULL)
58 #if defined(XP_WIN)
59     ,pf_wndproc(NULL)
60 #endif
61 #if defined(XP_UNIX)
62     ,i_width((unsigned)-1)
63     ,i_height((unsigned)-1)
64     ,i_tb_width(0)
65     ,i_tb_height(0)
66     ,i_last_position(0)
67     ,p_btnPlay(NULL)
68     ,p_btnPause(NULL)
69     ,p_btnStop(NULL)
70     ,p_btnMute(NULL)
71     ,p_btnUnmute(NULL)
72     ,p_btnFullscreen(NULL)
73     ,p_btnTime(NULL)
74     ,p_timeline(NULL)
75 #endif
76 {
77     memset(&npwindow, 0, sizeof(NPWindow));
78 #if defined(XP_UNIX)
79     memset(&npvideo, 0, sizeof(Window));
80     memset(&npcontrol, 0, sizeof(Window));
81 #endif
82 }
83
84 static bool boolValue(const char *value) {
85     return ( !strcmp(value, "1") ||
86              !strcasecmp(value, "true") ||
87              !strcasecmp(value, "yes") );
88 }
89
90
91 void EventObj::deliver(NPP browser)
92 {
93     NPVariant result;
94     NPVariant params[1];
95
96     pthread_mutex_lock(&mutex);
97
98     for( ev_l::iterator i=_elist.begin();i!=_elist.end();++i )
99     {
100         libvlc_event_type_t event = *i;
101         STRINGZ_TO_NPVARIANT(libvlc_event_type_name(event), params[0]);
102
103         // Invalid events aren't supposed to be queued up.
104         // if( !have_event(event) ) continue;
105
106         for( lr_l::iterator j=_llist.begin();j!=_llist.end();++j )
107         {
108             if (j->get(event))
109             {
110                 NPN_InvokeDefault(browser, j->listener(), params, 1, &result);
111                 NPN_ReleaseVariantValue(&result);
112             }
113         }
114     }
115     _elist.clear();
116
117     pthread_mutex_unlock(&mutex);
118 }
119
120 void VlcPlugin::eventAsync(void *param)
121 {
122     VlcPlugin *plugin = (VlcPlugin*)param;
123     plugin->events.deliver(plugin->getBrowser());
124 }
125
126 void EventObj::callback(const libvlc_event_t* event)
127 {
128     pthread_mutex_lock(&mutex);
129
130     if( have_event(event->type) )
131         _elist.push_back(event->type);
132
133     pthread_mutex_unlock(&mutex);
134 }
135
136 void VlcPlugin::event_callback(const libvlc_event_t* event, void *param)
137 {
138     VlcPlugin *plugin = (VlcPlugin*)param;
139     plugin->events.callback(event);
140 #ifdef XP_UNIX
141     NPN_PluginThreadAsyncCall(plugin->getBrowser(), eventAsync, plugin);
142 #else
143     NPN_SetException(this, "NPN_PluginThreadAsyncCall not implemented yet.");
144 #endif
145 }
146
147 inline EventObj::event_t EventObj::find_event(const char *s) const
148 {
149     event_t i;
150     for(i=0;i<maxbit();++i)
151         if(!strcmp(s,libvlc_event_type_name(i)))
152             break;
153     return i;
154 }
155
156 bool EventObj::insert(const NPString &s, NPObject *l, bool b)
157 {
158     event_t e = find_event(s.utf8characters);
159     if( e>=maxbit() )
160         return false;
161
162     if( !have_event(e) && !ask_for_event(e) )
163         return false;
164
165     lr_l::iterator i;
166     for(i=_llist.begin();i!=_llist.end();++i)
167         if(i->listener()==l && i->bubble()==b)
168             break;
169
170     if( i == _llist.end() ) {
171         _llist.push_back(Listener(e,l,b));
172     } else {
173         if( i->get(e) )
174             return false;
175         i->get(e);
176     }
177 }
178
179
180 bool EventObj::remove(const NPString &s, NPObject *l, bool b)
181 {
182     event_t e = find_event(s.utf8characters);
183     if( e>=maxbit() || !get(e) )
184         return false;
185
186     bool any=false;
187     for(lr_l::iterator i=_llist.begin();i!=_llist.end();)
188     {
189         if(i->listener()!=l || i->bubble()!=b)
190             any|=i->get(e);
191         else
192         {
193             i->reset(e);
194             if(i->empty())
195             {
196                 i=_llist.erase(i);
197                 continue;
198             }
199         }
200         ++i;
201     }
202     if(!any)
203         unask_for_event(e);
204 }
205
206
207 void EventObj::hook_manager(libvlc_event_manager_t *em,
208                             libvlc_callback_t cb, void *udata)
209 {
210     _em = em; _cb = cb; _ud = udata;
211     if( !_em )
212         return;
213     for(size_t i=0;i<maxbit();++i)
214         if(get(i))
215             libvlc_event_attach(_em, i, _cb, _ud);
216 }
217
218 void EventObj::unhook_manager()
219 {
220     if( !_em )
221     return;
222     for(size_t i=0;i<maxbit();++i)
223         if(get(i))
224             libvlc_event_detach(_em, i, _cb, _ud);
225 }
226
227
228 bool EventObj::ask_for_event(event_t e)
229 {
230     return _em?0==libvlc_event_attach(_em, e, _cb, _ud):false;
231 }
232
233
234 void EventObj::unask_for_event(event_t e)
235 {
236     if(_em) libvlc_event_detach(_em, e, _cb, _ud);
237 }
238
239
240 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
241 {
242     /* prepare VLC command line */
243     const char *ppsz_argv[32];
244     int ppsz_argc = 0;
245
246 #ifndef NDEBUG
247     ppsz_argv[ppsz_argc++] = "--no-plugins-cache";
248 #endif
249
250     /* locate VLC module path */
251 #ifdef XP_MACOSX
252     ppsz_argv[ppsz_argc++] = "--plugin-path=/Library/Internet\\ Plug-Ins/VLC\\ Plugin.plugin/Contents/MacOS/modules";
253     ppsz_argv[ppsz_argc++] = "--vout=minimal_macosx";
254 #elif defined(XP_WIN)
255     HKEY h_key;
256     DWORD i_type, i_data = MAX_PATH + 1;
257     char p_data[MAX_PATH + 1];
258     if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
259                       0, KEY_READ, &h_key ) == ERROR_SUCCESS )
260     {
261          if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
262                               (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
263          {
264              if( i_type == REG_SZ )
265              {
266                  strcat( p_data, "\\plugins" );
267                  ppsz_argv[ppsz_argc++] = "--plugin-path";
268                  ppsz_argv[ppsz_argc++] = p_data;
269              }
270          }
271          RegCloseKey( h_key );
272     }
273     ppsz_argv[ppsz_argc++] = "--no-one-instance";
274
275 #endif /* XP_MACOSX */
276
277     /* common settings */
278     ppsz_argv[ppsz_argc++] = "-vv";
279     ppsz_argv[ppsz_argc++] = "--no-stats";
280     ppsz_argv[ppsz_argc++] = "--no-media-library";
281     ppsz_argv[ppsz_argc++] = "--intf=dummy";
282     ppsz_argv[ppsz_argc++] = "--no-video-title-show";
283
284     const char *progid = NULL;
285
286     /* parse plugin arguments */
287     for( int i = 0; (i < argc) && (ppsz_argc < 32); i++ )
288     {
289        /* fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]); */
290
291         if( !strcmp( argn[i], "target" )
292          || !strcmp( argn[i], "mrl")
293          || !strcmp( argn[i], "filename")
294          || !strcmp( argn[i], "src") )
295         {
296             psz_target = argv[i];
297         }
298         else if( !strcmp( argn[i], "text" ) )
299         {
300             free( psz_text );
301             psz_text = strdup( argv[i] );
302         }
303         else if( !strcmp( argn[i], "autoplay")
304               || !strcmp( argn[i], "autostart") )
305         {
306             b_autoplay = boolValue(argv[i]);
307         }
308         else if( !strcmp( argn[i], "fullscreen" ) )
309         {
310             if( boolValue(argv[i]) )
311             {
312                 ppsz_argv[ppsz_argc++] = "--fullscreen";
313             }
314             else
315             {
316                 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
317             }
318         }
319         else if( !strcmp( argn[i], "mute" ) )
320         {
321             if( boolValue(argv[i]) )
322             {
323                 ppsz_argv[ppsz_argc++] = "--volume=0";
324             }
325         }
326         else if( !strcmp( argn[i], "loop")
327               || !strcmp( argn[i], "autoloop") )
328         {
329             if( boolValue(argv[i]) )
330             {
331                 ppsz_argv[ppsz_argc++] = "--loop";
332             }
333             else
334             {
335                 ppsz_argv[ppsz_argc++] = "--no-loop";
336             }
337         }
338         else if( !strcmp( argn[i], "version")
339               || !strcmp( argn[i], "progid") )
340         {
341             progid = argv[i];
342         }
343         else if( !strcmp( argn[i], "toolbar" ) )
344         {
345 /* FIXME: Remove this when toolbar functionality has been implemented on
346  * MacOS X and Win32 for Firefox/Mozilla/Safari. */
347 #ifdef XP_UNIX
348             b_toolbar = boolValue(argv[i]);
349 #endif
350         }
351     }
352
353     libvlc_exception_t ex;
354     libvlc_exception_init(&ex);
355
356     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
357     if( libvlc_exception_raised(&ex) )
358     {
359         libvlc_exception_clear(&ex);
360         return NPERR_GENERIC_ERROR;
361     }
362
363     libvlc_media_list = libvlc_media_list_new(libvlc_instance);
364
365     /*
366     ** fetch plugin base URL, which is the URL of the page containing the plugin
367     ** this URL is used for making absolute URL from relative URL that may be
368     ** passed as an MRL argument
369     */
370     NPObject *plugin = NULL;
371
372     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
373     {
374         /*
375         ** is there a better way to get that info ?
376         */
377         static const char docLocHref[] = "document.location.href";
378         NPString script;
379         NPVariant result;
380
381         script.utf8characters = docLocHref;
382         script.utf8length = sizeof(docLocHref)-1;
383
384         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
385         {
386             if( NPVARIANT_IS_STRING(result) )
387             {
388                 NPString &location = NPVARIANT_TO_STRING(result);
389
390                 psz_baseURL = (char *) malloc(location.utf8length+1);
391                 if( psz_baseURL )
392                 {
393                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
394                     psz_baseURL[location.utf8length] = '\0';
395                 }
396             }
397             NPN_ReleaseVariantValue(&result);
398         }
399         NPN_ReleaseObject(plugin);
400     }
401
402     if( psz_target )
403     {
404         // get absolute URL from src
405         char *psz_absurl = getAbsoluteURL(psz_target);
406         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
407     }
408
409     /* assign plugin script root class */
410     /* new APIs */
411     p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
412
413     if( !events.init() )
414         return NPERR_GENERIC_ERROR;
415
416     return NPERR_NO_ERROR;
417 }
418
419 VlcPlugin::~VlcPlugin()
420 {
421     free(psz_baseURL);
422     free(psz_target);
423     free(psz_text);
424
425     if( libvlc_media_player )
426     {
427         events.unhook_manager();
428         libvlc_media_player_release( libvlc_media_player );
429     }
430     if( libvlc_media_list )
431         libvlc_media_list_release( libvlc_media_list );
432     if( libvlc_instance )
433         libvlc_release(libvlc_instance);
434 }
435
436 /*****************************************************************************
437  * VlcPlugin playlist replacement methods
438  *****************************************************************************/
439 void VlcPlugin::set_player_window()
440 {
441 #ifdef XP_UNIX
442     libvlc_media_player_set_xwindow(libvlc_media_player,
443                                     (libvlc_drawable_t)getVideoWindow());
444 #endif
445 #ifdef XP_MACOSX
446     // XXX FIXME insert appropriate call here
447 #endif
448 #ifdef XP_WIN
449     libvlc_media_player_set_hwnd(libvlc_media_player,
450                                  getWindow().window);
451 #endif
452 }
453
454 int VlcPlugin::playlist_add( const char *mrl, libvlc_exception_t *ex )
455 {
456     int item = -1;
457     libvlc_media_t *p_m = libvlc_media_new(libvlc_instance,mrl,ex);
458     if( libvlc_exception_raised(ex) )
459         return -1;
460
461     libvlc_media_list_lock(libvlc_media_list);
462     libvlc_media_list_add_media(libvlc_media_list,p_m,ex);
463     if( !libvlc_exception_raised(ex) )
464         item = libvlc_media_list_count(libvlc_media_list)-1;
465     libvlc_media_list_unlock(libvlc_media_list);
466
467     libvlc_media_release(p_m);
468
469     return item;
470 }
471
472 int VlcPlugin::playlist_add_extended_untrusted( const char *mrl, const char *name,
473                     int optc, const char **optv, libvlc_exception_t *ex )
474 {
475     libvlc_media_t *p_m = libvlc_media_new(libvlc_instance, mrl,ex);
476     int item = -1;
477     if( libvlc_exception_raised(ex) )
478         return -1;
479
480     for( int i = 0; i < optc; ++i )
481         libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique);
482
483     libvlc_media_list_lock(libvlc_media_list);
484     libvlc_media_list_add_media(libvlc_media_list,p_m,ex);
485     if( !libvlc_exception_raised(ex) )
486         item = libvlc_media_list_count(libvlc_media_list)-1;
487     libvlc_media_list_unlock(libvlc_media_list);
488     libvlc_media_release(p_m);
489
490     return item;
491 }
492
493 bool VlcPlugin::playlist_select( int idx, libvlc_exception_t *ex )
494 {
495     libvlc_media_t *p_m = NULL;
496
497     libvlc_media_list_lock(libvlc_media_list);
498
499     int count = libvlc_media_list_count(libvlc_media_list);
500
501     if( idx<0||idx>=count )
502         goto bad_unlock;
503
504     playlist_index = idx;
505
506     p_m = libvlc_media_list_item_at_index(libvlc_media_list,playlist_index,ex);
507     libvlc_media_list_unlock(libvlc_media_list);
508
509     if( libvlc_exception_raised(ex) )
510         return false;
511
512     if( libvlc_media_player )
513     {
514         events.unhook_manager();
515         libvlc_media_player_release( libvlc_media_player );
516         libvlc_media_player = NULL;
517     }
518
519     libvlc_media_player = libvlc_media_player_new_from_media(p_m,ex);
520     if( libvlc_media_player )
521     {
522         set_player_window();
523         events.hook_manager(
524                       libvlc_media_player_event_manager(libvlc_media_player),
525                       event_callback, this);
526     }
527
528     libvlc_media_release( p_m );
529     return !libvlc_exception_raised(ex);
530
531 bad_unlock:
532     libvlc_media_list_unlock(libvlc_media_list);
533     return false;
534 }
535
536 void VlcPlugin::playlist_delete_item( int idx, libvlc_exception_t *ex )
537 {
538     libvlc_media_list_lock(libvlc_media_list);
539     libvlc_media_list_remove_index(libvlc_media_list,idx,ex);
540     libvlc_media_list_unlock(libvlc_media_list);
541 }
542
543 void VlcPlugin::playlist_clear( libvlc_exception_t *ex )
544 {
545     if( libvlc_media_list )
546         libvlc_media_list_release(libvlc_media_list);
547     libvlc_media_list = libvlc_media_list_new(getVLC());
548 }
549
550 int VlcPlugin::playlist_count()
551 {
552     int items_count = 0;
553     libvlc_media_list_lock(libvlc_media_list);
554     items_count = libvlc_media_list_count(libvlc_media_list);
555     libvlc_media_list_unlock(libvlc_media_list);
556     return items_count;
557 }
558
559 void VlcPlugin::toggle_fullscreen( libvlc_exception_t *ex )
560 {
561     if( playlist_isplaying() )
562         libvlc_toggle_fullscreen(libvlc_media_player,ex);
563 }
564 void VlcPlugin::set_fullscreen( int yes, libvlc_exception_t *ex )
565 {
566     if( playlist_isplaying() )
567         libvlc_set_fullscreen(libvlc_media_player,yes,ex);
568 }
569 int  VlcPlugin::get_fullscreen( libvlc_exception_t *ex )
570 {
571     int r = 0;
572     if( playlist_isplaying() )
573         r = libvlc_get_fullscreen(libvlc_media_player,ex);
574     return r;
575 }
576
577 bool  VlcPlugin::player_has_vout( libvlc_exception_t *ex )
578 {
579     bool r = false;
580     if( playlist_isplaying() )
581         r = libvlc_media_player_has_vout(libvlc_media_player, ex);
582     return r;
583 }
584
585 /*****************************************************************************
586  * VlcPlugin methods
587  *****************************************************************************/
588
589 char *VlcPlugin::getAbsoluteURL(const char *url)
590 {
591     if( NULL != url )
592     {
593         // check whether URL is already absolute
594         const char *end=strchr(url, ':');
595         if( (NULL != end) && (end != url) )
596         {
597             // validate protocol header
598             const char *start = url;
599             char c = *start;
600             if( isalpha(c) )
601             {
602                 ++start;
603                 while( start != end )
604                 {
605                     c  = *start;
606                     if( ! (isalnum(c)
607                        || ('-' == c)
608                        || ('+' == c)
609                        || ('.' == c)
610                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
611                         // not valid protocol header, assume relative URL
612                         goto relativeurl;
613                     ++start;
614                 }
615                 /* we have a protocol header, therefore URL is absolute */
616                 return strdup(url);
617             }
618             // not a valid protocol header, assume relative URL
619         }
620
621 relativeurl:
622
623         if( psz_baseURL )
624         {
625             size_t baseLen = strlen(psz_baseURL);
626             char *href = (char *) malloc(baseLen+strlen(url)+1);
627             if( href )
628             {
629                 /* prepend base URL */
630                 memcpy(href, psz_baseURL, baseLen+1);
631
632                 /*
633                 ** relative url could be empty,
634                 ** in which case return base URL
635                 */
636                 if( '\0' == *url )
637                     return href;
638
639                 /*
640                 ** locate pathname part of base URL
641                 */
642
643                 /* skip over protocol part  */
644                 char *pathstart = strchr(href, ':');
645                 char *pathend = href+baseLen;
646                 if( pathstart )
647                 {
648                     if( '/' == *(++pathstart) )
649                     {
650                         if( '/' == *(++pathstart) )
651                         {
652                             ++pathstart;
653                         }
654                     }
655                     /* skip over host part */
656                     pathstart = strchr(pathstart, '/');
657                     if( ! pathstart )
658                     {
659                         // no path, add a / past end of url (over '\0')
660                         pathstart = pathend;
661                         *pathstart = '/';
662                     }
663                 }
664                 else
665                 {
666                     /* baseURL is just a UNIX path */
667                     if( '/' != *href )
668                     {
669                         /* baseURL is not an absolute path */
670                         free(href);
671                         return NULL;
672                     }
673                     pathstart = href;
674                 }
675
676                 /* relative URL made of an absolute path ? */
677                 if( '/' == *url )
678                 {
679                     /* replace path completely */
680                     strcpy(pathstart, url);
681                     return href;
682                 }
683
684                 /* find last path component and replace it */
685                 while( '/' != *pathend)
686                     --pathend;
687
688                 /*
689                 ** if relative url path starts with one or more '../',
690                 ** factor them out of href so that we return a
691                 ** normalized URL
692                 */
693                 while( pathend != pathstart )
694                 {
695                     const char *p = url;
696                     if( '.' != *p )
697                         break;
698                     ++p;
699                     if( '\0' == *p  )
700                     {
701                         /* relative url is just '.' */
702                         url = p;
703                         break;
704                     }
705                     if( '/' == *p  )
706                     {
707                         /* relative url starts with './' */
708                         url = ++p;
709                         continue;
710                     }
711                     if( '.' != *p )
712                         break;
713                     ++p;
714                     if( '\0' == *p )
715                     {
716                         /* relative url is '..' */
717                     }
718                     else
719                     {
720                         if( '/' != *p )
721                             break;
722                         /* relative url starts with '../' */
723                         ++p;
724                     }
725                     url = p;
726                     do
727                     {
728                         --pathend;
729                     }
730                     while( '/' != *pathend );
731                 }
732                 /* skip over '/' separator */
733                 ++pathend;
734                 /* concatenate remaining base URL and relative URL */
735                 strcpy(pathend, url);
736             }
737             return href;
738         }
739     }
740     return NULL;
741 }
742
743 #if defined(XP_UNIX)
744 int  VlcPlugin::setSize(unsigned width, unsigned height)
745 {
746     int diff = (width != i_width) || (height != i_height);
747
748     i_width = width;
749     i_height = height;
750
751     /* return size */
752     return diff;
753 }
754
755 #define BTN_SPACE ((unsigned int)4)
756 void VlcPlugin::showToolbar()
757 {
758     const NPWindow& window = getWindow();
759     Window control = getControlWindow();
760     Window video = getVideoWindow();
761     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
762     unsigned int i_height = 0, i_width = BTN_SPACE;
763
764     /* load icons */
765     if( !p_btnPlay )
766         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
767                             &p_btnPlay, NULL, NULL);
768     if( p_btnPlay )
769     {
770         i_height = __MAX( i_height, p_btnPlay->height );
771     }
772     if( !p_btnPause )
773         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
774                             &p_btnPause, NULL, NULL);
775     if( p_btnPause )
776     {
777         i_height = __MAX( i_height, p_btnPause->height );
778     }
779     i_width += __MAX( p_btnPause->width, p_btnPlay->width );
780
781     if( !p_btnStop )
782         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
783                             &p_btnStop, NULL, NULL );
784     if( p_btnStop )
785     {
786         i_height = __MAX( i_height, p_btnStop->height );
787         i_width += BTN_SPACE + p_btnStop->width;
788     }
789     if( !p_timeline )
790         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
791                             &p_timeline, NULL, NULL);
792     if( p_timeline )
793     {
794         i_height = __MAX( i_height, p_timeline->height );
795         i_width += BTN_SPACE + p_timeline->width;
796     }
797     if( !p_btnTime )
798         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
799                             &p_btnTime, NULL, NULL);
800     if( p_btnTime )
801     {
802         i_height = __MAX( i_height, p_btnTime->height );
803         i_width += BTN_SPACE + p_btnTime->width;
804     }
805     if( !p_btnFullscreen )
806         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
807                             &p_btnFullscreen, NULL, NULL);
808     if( p_btnFullscreen )
809     {
810         i_height = __MAX( i_height, p_btnFullscreen->height );
811         i_width += BTN_SPACE + p_btnFullscreen->width;
812     }
813     if( !p_btnMute )
814         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
815                             &p_btnMute, NULL, NULL);
816     if( p_btnMute )
817     {
818         i_height = __MAX( i_height, p_btnMute->height );
819     }
820     if( !p_btnUnmute )
821         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
822                             &p_btnUnmute, NULL, NULL);
823     if( p_btnUnmute )
824     {
825         i_height = __MAX( i_height, p_btnUnmute->height );
826     }
827     i_width += BTN_SPACE + __MAX( p_btnUnmute->width, p_btnMute->width );
828
829     setToolbarSize( i_width, i_height );
830
831     if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
832         !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
833         fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
834
835     /* reset panels position and size */
836     /* XXX  use i_width */
837     XResizeWindow( p_display, video, window.width, window.height - i_height);
838     XMoveWindow( p_display, control, 0, window.height - i_height );
839     XResizeWindow( p_display, control, window.width, i_height -1);
840
841     b_toolbar = 1; /* says toolbar is now shown */
842     redrawToolbar();
843 }
844
845 void VlcPlugin::hideToolbar()
846 {
847     const NPWindow& window = getWindow();
848     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
849     Window control = getControlWindow();
850     Window video = getVideoWindow();
851
852     i_tb_width = i_tb_height = 0;
853
854     if( p_btnPlay )  XDestroyImage( p_btnPlay );
855     if( p_btnPause ) XDestroyImage( p_btnPause );
856     if( p_btnStop )  XDestroyImage( p_btnStop );
857     if( p_timeline ) XDestroyImage( p_timeline );
858     if( p_btnTime )  XDestroyImage( p_btnTime );
859     if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
860     if( p_btnMute )  XDestroyImage( p_btnMute );
861     if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
862
863     p_btnPlay = NULL;
864     p_btnPause = NULL;
865     p_btnStop = NULL;
866     p_timeline = NULL;
867     p_btnTime = NULL;
868     p_btnFullscreen = NULL;
869     p_btnMute = NULL;
870     p_btnUnmute = NULL;
871
872     /* reset panels position and size */
873     /* XXX  use i_width */
874     XResizeWindow( p_display, video, window.width, window.height );
875     XMoveWindow( p_display, control, 0, window.height-1 );
876     XResizeWindow( p_display, control, window.width, 1 );
877
878     b_toolbar = 0; /* says toolbar is now hidden */
879     redrawToolbar();
880 }
881
882 void VlcPlugin::redrawToolbar()
883 {
884     int is_playing = 0;
885     bool b_mute = false;
886     unsigned int dst_x, dst_y;
887     GC gc;
888     XGCValues gcv;
889     unsigned int i_tb_width, i_tb_height;
890
891     /* This method does nothing if toolbar is hidden. */
892     if( !b_toolbar )
893         return;
894
895     const NPWindow& window = getWindow();
896     Window control = getControlWindow();
897     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
898
899     getToolbarSize( &i_tb_width, &i_tb_height );
900
901
902     /* get mute info */
903     b_mute = libvlc_audio_get_mute( getVLC() );
904
905     gcv.foreground = BlackPixel( p_display, 0 );
906     gc = XCreateGC( p_display, control, GCForeground, &gcv );
907
908     XFillRectangle( p_display, control, gc,
909                     0, 0, window.width, i_tb_height );
910     gcv.foreground = WhitePixel( p_display, 0 );
911     XChangeGC( p_display, gc, GCForeground, &gcv );
912
913     /* position icons */
914     dst_x = BTN_SPACE;
915     dst_y = i_tb_height >> 1; /* baseline = vertical middle */
916
917     if( p_btnPause && (is_playing == 1) )
918     {
919         XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
920                    dst_y - (p_btnPause->height >> 1),
921                    p_btnPause->width, p_btnPause->height );
922         dst_x += BTN_SPACE + p_btnPause->width;
923     }
924     else if( p_btnPlay )
925     {
926         XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
927                    dst_y - (p_btnPlay->height >> 1),
928                    p_btnPlay->width, p_btnPlay->height );
929         dst_x += BTN_SPACE + p_btnPlay->width;
930     }
931
932     if( p_btnStop )
933         XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
934                    dst_y - (p_btnStop->height >> 1),
935                    p_btnStop->width, p_btnStop->height );
936
937     dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
938
939     if( p_btnFullscreen )
940         XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
941                    dst_y - (p_btnFullscreen->height >> 1),
942                    p_btnFullscreen->width, p_btnFullscreen->height );
943
944     dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
945
946     if( p_btnUnmute && b_mute )
947     {
948         XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
949                    dst_y - (p_btnUnmute->height >> 1),
950                    p_btnUnmute->width, p_btnUnmute->height );
951
952         dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
953     }
954     else if( p_btnMute )
955     {
956         XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
957                    dst_y - (p_btnMute->height >> 1),
958                    p_btnMute->width, p_btnMute->height );
959
960         dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
961     }
962
963     if( p_timeline )
964         XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x,
965                    dst_y - (p_timeline->height >> 1),
966                    (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
967
968     /* get movie position in % */
969     if( playlist_isplaying() )
970     {
971         libvlc_exception_t ex;
972         libvlc_exception_init( &ex );
973         i_last_position = (int)((window.width-(dst_x+BTN_SPACE))*
974                    libvlc_media_player_get_position(libvlc_media_player,&ex));
975         libvlc_exception_clear( &ex );
976     }
977
978     if( p_btnTime )
979         XPutImage( p_display, control, gc, p_btnTime,
980                    0, 0, (dst_x+i_last_position),
981                    dst_y - (p_btnTime->height >> 1),
982                    p_btnTime->width, p_btnTime->height );
983
984     XFreeGC( p_display, gc );
985 }
986
987 vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos )
988 {
989     unsigned int i_dest = BTN_SPACE;
990     int is_playing = 0;
991     bool b_mute = false;
992
993 #ifndef NDEBUG
994     fprintf( stderr, "ToolbarButtonClicked:: "
995                      "trying to match (%d,%d) (%d,%d)\n",
996              i_xpos, i_ypos, i_tb_height, i_tb_width );
997 #endif
998     if( i_ypos >= i_tb_width )
999         return clicked_Unknown;
1000
1001     /* Note: the order of testing is dependend on the original
1002      * drawing positions of the icon buttons. Buttons are tested
1003      * left to right.
1004      */
1005
1006     /* get isplaying */
1007     is_playing = playlist_isplaying();
1008
1009     /* get mute info */
1010     b_mute = libvlc_audio_get_mute( getVLC() );
1011
1012     /* is Pause of Play button clicked */
1013     if( (is_playing != 1) &&
1014         (i_xpos >= (BTN_SPACE>>1)) &&
1015         (i_xpos <= i_dest + p_btnPlay->width + (BTN_SPACE>>1)) )
1016         return clicked_Play;
1017     else if( (i_xpos >= (BTN_SPACE>>1))  &&
1018              (i_xpos <= i_dest + p_btnPause->width) )
1019         return clicked_Pause;
1020
1021     /* is Stop button clicked */
1022     if( is_playing != 1 )
1023         i_dest += (p_btnPlay->width + (BTN_SPACE>>1));
1024     else
1025         i_dest += (p_btnPause->width + (BTN_SPACE>>1));
1026
1027     if( (i_xpos >= i_dest) &&
1028         (i_xpos <= i_dest + p_btnStop->width + (BTN_SPACE>>1)) )
1029         return clicked_Stop;
1030
1031     /* is Fullscreen button clicked */
1032     i_dest += (p_btnStop->width + (BTN_SPACE>>1));
1033     if( (i_xpos >= i_dest) &&
1034         (i_xpos <= i_dest + p_btnFullscreen->width + (BTN_SPACE>>1)) )
1035         return clicked_Fullscreen;
1036
1037     /* is Mute or Unmute button clicked */
1038     i_dest += (p_btnFullscreen->width + (BTN_SPACE>>1));
1039     if( !b_mute && (i_xpos >= i_dest) &&
1040         (i_xpos <= i_dest + p_btnMute->width + (BTN_SPACE>>1)) )
1041         return clicked_Mute;
1042     else if( (i_xpos >= i_dest) &&
1043              (i_xpos <= i_dest + p_btnUnmute->width + (BTN_SPACE>>1)) )
1044         return clicked_Unmute;
1045
1046     /* is timeline clicked */
1047     if( !b_mute )
1048         i_dest += (p_btnMute->width + (BTN_SPACE>>1));
1049     else
1050         i_dest += (p_btnUnmute->width + (BTN_SPACE>>1));
1051     if( (i_xpos >= i_dest) &&
1052         (i_xpos <= i_dest + p_timeline->width + (BTN_SPACE>>1)) )
1053         return clicked_timeline;
1054
1055     /* is time button clicked */
1056     i_dest += (p_timeline->width + (BTN_SPACE>>1));
1057     if( (i_xpos >= i_dest) &&
1058         (i_xpos <= i_dest + p_btnTime->width + (BTN_SPACE>>1)) )
1059         return clicked_Time;
1060
1061     return clicked_Unknown;
1062 }
1063 #undef BTN_SPACE
1064 #endif
1065
1066 // Verifies the version of the NPAPI.
1067 // The eventListeners use a NPAPI function available
1068 // since Gecko 1.9.
1069 bool VlcPlugin::canUseEventListener()
1070 {
1071     int plugin_major, plugin_minor;
1072     int browser_major, browser_minor;
1073
1074     NPN_Version(&plugin_major, &plugin_minor,
1075                 &browser_major, &browser_minor);
1076
1077     if (browser_minor >= 19 || browser_major > 0)
1078         return true;
1079     return false;
1080 }
1081