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