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