Add "toolbar:bool" RW property for the JS API, fix toolbar visibility
[vlc.git] / projects / mozilla / vlcplugin.cpp
1 /*****************************************************************************
2  * vlcplugin.cpp: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf.fouilleul@laposte.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "config.h"
29
30 #ifdef HAVE_MOZILLA_CONFIG_H
31 #   include <mozilla-config.h>
32 #endif
33
34 #include "vlcplugin.h"
35 #include "control/npolibvlc.h"
36
37 #include <ctype.h>
38
39 /*****************************************************************************
40  * VlcPlugin constructor and destructor
41  *****************************************************************************/
42 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
43     i_npmode(mode),
44     b_stream(0),
45     b_autoplay(1),
46     b_toolbar(0),
47     psz_target(NULL),
48     libvlc_instance(NULL),
49     libvlc_log(NULL),
50     p_scriptClass(NULL),
51     p_browser(instance),
52     psz_baseURL(NULL)
53 #if XP_WIN
54     ,pf_wndproc(NULL)
55 #endif
56 #if XP_UNIX
57     ,i_width((unsigned)-1)
58     ,i_height((unsigned)-1)
59     ,i_tb_width(0)
60     ,i_tb_height(0)
61     ,i_last_position(0)
62     ,p_btnPlay(NULL)
63     ,p_btnPause(NULL)
64     ,p_btnStop(NULL)
65     ,p_btnMute(NULL)
66     ,p_btnUnmute(NULL)
67     ,p_btnFullscreen(NULL)
68     ,p_btnTime(NULL)
69     ,p_timeline(NULL)
70 #endif
71 {
72     memset(&npwindow, 0, sizeof(NPWindow));
73 }
74
75 static bool boolValue(const char *value) {
76     return ( !strcmp(value, "1") ||
77              !strcasecmp(value, "true") ||
78              !strcasecmp(value, "yes") );
79 }
80
81 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
82 {
83     /* prepare VLC command line */
84     char *ppsz_argv[32];
85     int ppsz_argc = 0;
86
87     /* locate VLC module path */
88 #ifdef XP_MACOSX
89     ppsz_argv[ppsz_argc++] = "--plugin-path";
90     ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
91                              "Contents/MacOS/modules";
92 #elif defined(XP_WIN)
93     HKEY h_key;
94     DWORD i_type, i_data = MAX_PATH + 1;
95     char p_data[MAX_PATH + 1];
96     if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
97                       0, KEY_READ, &h_key ) == ERROR_SUCCESS )
98     {
99          if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
100                               (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
101          {
102              if( i_type == REG_SZ )
103              {
104                  strcat( p_data, "\\plugins" );
105                  ppsz_argv[ppsz_argc++] = "--plugin-path";
106                  ppsz_argv[ppsz_argc++] = p_data;
107              }
108          }
109          RegCloseKey( h_key );
110     }
111     ppsz_argv[ppsz_argc++] = "--no-one-instance";
112
113 #endif /* XP_MACOSX */
114
115     /* common settings */
116     ppsz_argv[ppsz_argc++] = "-vv";
117     ppsz_argv[ppsz_argc++] = "--no-stats";
118     ppsz_argv[ppsz_argc++] = "--no-media-library";
119     ppsz_argv[ppsz_argc++] = "--ignore-config";
120     ppsz_argv[ppsz_argc++] = "--intf";
121     ppsz_argv[ppsz_argc++] = "dummy";
122
123     const char *progid = NULL;
124
125     /* parse plugin arguments */
126     for( int i = 0; i < argc ; i++ )
127     {
128         fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
129
130         if( !strcmp( argn[i], "target" )
131          || !strcmp( argn[i], "mrl")
132          || !strcmp( argn[i], "filename")
133          || !strcmp( argn[i], "src") )
134         {
135             psz_target = argv[i];
136         }
137         else if( !strcmp( argn[i], "autoplay")
138               || !strcmp( argn[i], "autostart") )
139         {
140             b_autoplay = boolValue(argv[i]);
141         }
142         else if( !strcmp( argn[i], "fullscreen" ) )
143         {
144             if( boolValue(argv[i]) )
145             {
146                 ppsz_argv[ppsz_argc++] = "--fullscreen";
147             }
148             else
149             {
150                 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
151             }
152         }
153         else if( !strcmp( argn[i], "mute" ) )
154         {
155             if( boolValue(argv[i]) )
156             {
157                 ppsz_argv[ppsz_argc++] = "--volume";
158                 ppsz_argv[ppsz_argc++] = "0";
159             }
160         }
161         else if( !strcmp( argn[i], "loop")
162               || !strcmp( argn[i], "autoloop") )
163         {
164             if( boolValue(argv[i]) )
165             {
166                 ppsz_argv[ppsz_argc++] = "--loop";
167             }
168             else
169             {
170                 ppsz_argv[ppsz_argc++] = "--no-loop";
171             }
172         }
173         else if( !strcmp( argn[i], "version")
174               || !strcmp( argn[i], "progid") )
175         {
176             progid = argv[i];
177         }
178         else if( !strcmp( argn[i], "toolbar" ) )
179         {
180             b_toolbar = boolValue(argv[i]);
181         }
182     }
183
184     libvlc_exception_t ex;
185     libvlc_exception_init(&ex);
186
187     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
188     if( libvlc_exception_raised(&ex) )
189     {
190         libvlc_exception_clear(&ex);
191         return NPERR_GENERIC_ERROR;
192     }
193
194     /*
195     ** fetch plugin base URL, which is the URL of the page containing the plugin
196     ** this URL is used for making absolute URL from relative URL that may be
197     ** passed as an MRL argument
198     */
199     NPObject *plugin;
200
201     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
202     {
203         /*
204         ** is there a better way to get that info ?
205         */
206         static const char docLocHref[] = "document.location.href";
207         NPString script;
208         NPVariant result;
209
210         script.utf8characters = docLocHref;
211         script.utf8length = sizeof(docLocHref)-1;
212
213         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
214         {
215             if( NPVARIANT_IS_STRING(result) )
216             {
217                 NPString &location = NPVARIANT_TO_STRING(result);
218
219                 psz_baseURL = new char[location.utf8length+1];
220                 if( psz_baseURL )
221                 {
222                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
223                     psz_baseURL[location.utf8length] = '\0';
224                 }
225             }
226             NPN_ReleaseVariantValue(&result);
227         }
228         NPN_ReleaseObject(plugin);
229     }
230
231     if( psz_target )
232     {
233         // get absolute URL from src
234         char *psz_absurl = getAbsoluteURL(psz_target);
235         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
236     }
237
238     /* assign plugin script root class */
239     /* new APIs */
240     p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
241
242     return NPERR_NO_ERROR;
243 }
244
245 VlcPlugin::~VlcPlugin()
246 {
247     delete psz_baseURL;
248     delete psz_target;
249     if( libvlc_log )
250         libvlc_log_close(libvlc_log, NULL);
251     if( libvlc_instance )
252         libvlc_release(libvlc_instance);
253 }
254
255 /*****************************************************************************
256  * VlcPlugin methods
257  *****************************************************************************/
258
259 char *VlcPlugin::getAbsoluteURL(const char *url)
260 {
261     if( NULL != url )
262     {
263         // check whether URL is already absolute
264         const char *end=strchr(url, ':');
265         if( (NULL != end) && (end != url) )
266         {
267             // validate protocol header
268             const char *start = url;
269             char c = *start;
270             if( isalpha(c) )
271             {
272                 ++start;
273                 while( start != end )
274                 {
275                     c  = *start;
276                     if( ! (isalnum(c)
277                        || ('-' == c)
278                        || ('+' == c)
279                        || ('.' == c)
280                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
281                         // not valid protocol header, assume relative URL
282                         goto relativeurl;
283                     ++start;
284                 }
285                 /* we have a protocol header, therefore URL is absolute */
286                 return strdup(url);
287             }
288             // not a valid protocol header, assume relative URL
289         }
290
291 relativeurl:
292
293         if( psz_baseURL )
294         {
295             size_t baseLen = strlen(psz_baseURL);
296             char *href = new char[baseLen+strlen(url)+1];
297             if( href )
298             {
299                 /* prepend base URL */
300                 strcpy(href, psz_baseURL);
301
302                 /*
303                 ** relative url could be empty,
304                 ** in which case return base URL
305                 */
306                 if( '\0' == *url )
307                     return href;
308
309                 /*
310                 ** locate pathname part of base URL
311                 */
312
313                 /* skip over protocol part  */
314                 char *pathstart = strchr(href, ':');
315                 char *pathend;
316                 if( pathstart )
317                 {
318                     if( '/' == *(++pathstart) )
319                     {
320                         if( '/' == *(++pathstart) )
321                         {
322                             ++pathstart;
323                         }
324                     }
325                     /* skip over host part */
326                     pathstart = strchr(pathstart, '/');
327                     pathend = href+baseLen;
328                     if( ! pathstart )
329                     {
330                         // no path, add a / past end of url (over '\0')
331                         pathstart = pathend;
332                         *pathstart = '/';
333                     }
334                 }
335                 else
336                 {
337                     /* baseURL is just a UNIX path */
338                     if( '/' != *href )
339                     {
340                         /* baseURL is not an absolute path */
341                         return NULL;
342                     }
343                     pathstart = href;
344                     pathend = href+baseLen;
345                 }
346
347                 /* relative URL made of an absolute path ? */
348                 if( '/' == *url )
349                 {
350                     /* replace path completely */
351                     strcpy(pathstart, url);
352                     return href;
353                 }
354
355                 /* find last path component and replace it */
356                 while( '/' != *pathend)
357                     --pathend;
358
359                 /*
360                 ** if relative url path starts with one or more '../',
361                 ** factor them out of href so that we return a
362                 ** normalized URL
363                 */
364                 while( pathend != pathstart )
365                 {
366                     const char *p = url;
367                     if( '.' != *p )
368                         break;
369                     ++p;
370                     if( '\0' == *p  )
371                     {
372                         /* relative url is just '.' */
373                         url = p;
374                         break;
375                     }
376                     if( '/' == *p  )
377                     {
378                         /* relative url starts with './' */
379                         url = ++p;
380                         continue;
381                     }
382                     if( '.' != *p )
383                         break;
384                     ++p;
385                     if( '\0' == *p )
386                     {
387                         /* relative url is '..' */
388                     }
389                     else
390                     {
391                         if( '/' != *p )
392                             break;
393                         /* relative url starts with '../' */
394                         ++p;
395                     }
396                     url = p;
397                     do
398                     {
399                         --pathend;
400                     }
401                     while( '/' != *pathend );
402                 }
403                 /* skip over '/' separator */
404                 ++pathend;
405                 /* concatenate remaining base URL and relative URL */
406                 strcpy(pathend, url);
407             }
408             return href;
409         }
410     }
411     return NULL;
412 }
413
414 #if XP_UNIX
415 int  VlcPlugin::setSize(unsigned width, unsigned height)
416 {
417     int diff = (width != i_width) || (height != i_height);
418
419     i_width = width;
420     i_height = height;
421
422     /* return size */
423     return diff;
424 }
425
426 void VlcPlugin::showToolbar()
427 {
428     const NPWindow& window = getWindow();
429     Window control = getControlWindow();
430     Window video = getVideoWindow();
431     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
432     unsigned int i_height = 0, i_width = 0;
433
434     /* load icons */
435     if( !p_btnPlay )
436         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
437                             &p_btnPlay, NULL, NULL);
438     if( p_btnPlay )
439     {
440         i_height = __MAX( i_height, p_btnPlay->height );
441         i_width += p_btnPlay->width;
442     }
443     if( !p_btnPause )
444         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
445                             &p_btnPause, NULL, NULL);
446     if( p_btnPause )
447     {
448         i_height = __MAX( i_height, p_btnPause->height );
449         i_width += p_btnPause->width;
450     }
451     if( !p_btnStop )
452         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
453                             &p_btnStop, NULL, NULL );
454     if( p_btnStop )
455     {
456         i_height = __MAX( i_height, p_btnStop->height );
457         i_width += p_btnStop->width;
458     }
459     if( !p_timeline )
460         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
461                             &p_timeline, NULL, NULL);
462     if( p_timeline )
463     {
464         i_height = __MAX( i_height, p_timeline->height );
465         i_width += p_timeline->width;
466     }
467     if( !p_btnTime )
468         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
469                             &p_btnTime, NULL, NULL);
470     if( p_btnTime )
471     {
472         i_height = __MAX( i_height, p_btnTime->height );
473         i_width += p_btnTime->width;
474     }
475     if( !p_btnFullscreen )
476         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
477                             &p_btnFullscreen, NULL, NULL);
478     if( p_btnFullscreen )
479     {
480         i_height = __MAX( i_height, p_btnFullscreen->height );
481         i_width += p_btnFullscreen->width;
482     }
483     if( !p_btnMute )
484         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
485                             &p_btnMute, NULL, NULL);
486     if( p_btnMute )
487     {
488         i_height = __MAX( i_height, p_btnMute->height );
489         i_width += p_btnMute->width;
490     }
491     if( !p_btnUnmute )
492         XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
493                             &p_btnUnmute, NULL, NULL);
494     if( p_btnUnmute )
495     {
496         i_height = __MAX( i_height, p_btnUnmute->height );
497         i_width += p_btnUnmute->width;
498     }
499     setToolbarSize( i_width, i_height );
500
501     if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
502         !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
503         fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
504
505     /* reset panels position and size */
506     /* XXX  use i_width */
507     XResizeWindow( p_display, video, window.width, window.height - i_height);
508     XMoveWindow( p_display, control, 0, window.height - i_height );
509     XResizeWindow( p_display, control, window.width, i_height -1);
510
511     b_toolbar = 1; /* says toolbar is now shown */
512     redrawToolbar();
513 }
514
515 void VlcPlugin::hideToolbar()
516 {
517     const NPWindow& window = getWindow();
518     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
519     Window control = getControlWindow();
520     Window video = getVideoWindow();
521
522     i_tb_width = i_tb_height = 0;
523
524     if( p_btnPlay )  XDestroyImage( p_btnPlay );
525     if( p_btnPause ) XDestroyImage( p_btnPause );
526     if( p_btnStop )  XDestroyImage( p_btnStop );
527     if( p_timeline ) XDestroyImage( p_timeline );
528     if( p_btnTime )  XDestroyImage( p_btnTime );
529     if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
530     if( p_btnMute )  XDestroyImage( p_btnMute );
531     if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
532
533     p_btnPlay = NULL;
534     p_btnPause = NULL;
535     p_btnStop = NULL;
536     p_timeline = NULL;
537     p_btnTime = NULL;
538     p_btnFullscreen = NULL;
539     p_btnMute = NULL;
540     p_btnUnmute = NULL;
541
542     /* reset panels position and size */
543     /* XXX  use i_width */
544     XResizeWindow( p_display, video, window.width, window.height );
545     XMoveWindow( p_display, control, 0, window.height-1 );
546     XResizeWindow( p_display, control, window.width, 1 );
547
548     b_toolbar = 0; /* says toolbar is now hidden */
549     redrawToolbar();
550 }
551
552 void VlcPlugin::redrawToolbar()
553 {
554     libvlc_media_player_t *p_md = NULL;
555     libvlc_exception_t ex;
556     float f_position = 0.0;
557     int i_playing = 0;
558     bool b_mute = false;
559     unsigned int dst_x, dst_y;
560     GC gc;
561     XGCValues gcv;
562     unsigned int i_tb_width, i_tb_height;
563 #define BTN_SPACE ((unsigned int)4)
564
565     /* This method does nothing if toolbar is hidden. */
566     if( !b_toolbar )
567         return;
568
569     const NPWindow& window = getWindow();
570     Window control = getControlWindow();
571     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
572
573     getToolbarSize( &i_tb_width, &i_tb_height );
574
575     /* get media instance */
576     libvlc_exception_init( &ex );
577     p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
578     libvlc_exception_clear( &ex );
579
580     /* get isplaying */
581     libvlc_exception_init( &ex );
582     i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
583     libvlc_exception_clear( &ex );
584
585     /* get mute info */
586     libvlc_exception_init(&ex);
587     b_mute = libvlc_audio_get_mute( getVLC(), &ex );
588     libvlc_exception_clear( &ex );
589
590     /* get movie position in % */
591     if( i_playing == 1 )
592     {
593         libvlc_exception_init( &ex );
594         f_position = libvlc_media_player_get_position( p_md, &ex ) * 100;
595         libvlc_exception_clear( &ex );
596     }
597     libvlc_media_player_release( p_md );
598
599     gcv.foreground = BlackPixel( p_display, 0 );
600     gc = XCreateGC( p_display, control, GCForeground, &gcv );
601
602     XFillRectangle( p_display, control, gc,
603                     0, 0, window.width, i_tb_height );
604     gcv.foreground = WhitePixel( p_display, 0 );
605     XChangeGC( p_display, gc, GCForeground, &gcv );
606
607     /* position icons */
608     dst_x = BTN_SPACE;
609     dst_y = i_tb_height >> 1; /* baseline = vertical middle */
610
611     fprintf( stderr, ">>>>>> is playing = %d\n", i_playing );
612     if( p_btnPause && (i_playing == 1) )
613     {
614         XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
615                    dst_y - (p_btnPause->height >> 1),
616                    p_btnPause->width, p_btnPause->height );
617         dst_x += BTN_SPACE + p_btnPause->width;
618     }
619     else if( p_btnPlay )
620     {
621         XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
622                    dst_y - (p_btnPlay->height >> 1),
623                    p_btnPlay->width, p_btnPlay->height );
624         dst_x += BTN_SPACE + p_btnPlay->width;
625     }
626
627     if( p_btnStop )
628         XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
629                    dst_y - (p_btnStop->height >> 1),
630                    p_btnStop->width, p_btnStop->height );
631
632     dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
633
634     if( p_btnFullscreen )
635         XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
636                    dst_y - (p_btnFullscreen->height >> 1),
637                    p_btnFullscreen->width, p_btnFullscreen->height );
638
639     dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
640
641     if( p_btnUnmute && b_mute )
642     {
643         XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
644                    dst_y - (p_btnUnmute->height >> 1),
645                    p_btnUnmute->width, p_btnUnmute->height );
646
647         dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
648     }
649     else if( p_btnMute )
650     {
651         XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
652                    dst_y - (p_btnMute->height >> 1),
653                    p_btnMute->width, p_btnMute->height );
654
655         dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
656     }
657
658     if( p_timeline )
659         XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x,
660                    dst_y - (p_timeline->height >> 1),
661                    (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
662
663     if( f_position > 0 )
664         i_last_position = (int)( f_position *
665                         ( ((float)(window.width-(dst_x+BTN_SPACE))) / 100.0 ));
666
667     if( p_btnTime )
668         XPutImage( p_display, control, gc, p_btnTime,
669                    0, 0, (dst_x+i_last_position),
670                    dst_y - (p_btnTime->height >> 1),
671                    p_btnTime->width, p_btnTime->height );
672
673     XFreeGC( p_display, gc );
674 }
675 #endif