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