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