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