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