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