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