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