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