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