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