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