1 /*****************************************************************************
2 * vlcplugin.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002-2005 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Damien Fouilleul <damienf.fouilleul@laposte.net>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #ifdef HAVE_MOZILLA_CONFIG_H
31 # include <mozilla-config.h>
34 #include "vlcplugin.h"
35 #include "control/npovlc.h"
36 #include "control/npolibvlc.h"
40 /*****************************************************************************
41 * VlcPlugin constructor and destructor
42 *****************************************************************************/
43 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
49 libvlc_instance(NULL),
58 ,i_width((unsigned)-1)
59 ,i_height((unsigned)-1)
63 memset(&npwindow, 0, sizeof(NPWindow));
66 static bool boolValue(const char *value) {
67 return ( !strcmp(value, "1") ||
68 !strcasecmp(value, "true") ||
69 !strcasecmp(value, "yes") );
72 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
74 /* prepare VLC command line */
78 /* locate VLC module path */
80 ppsz_argv[ppsz_argc++] = "--plugin-path";
81 ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
82 "Contents/MacOS/modules";
85 DWORD i_type, i_data = MAX_PATH + 1;
86 char p_data[MAX_PATH + 1];
87 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
88 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
90 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
91 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
93 if( i_type == REG_SZ )
95 strcat( p_data, "\\plugins" );
96 ppsz_argv[ppsz_argc++] = "--plugin-path";
97 ppsz_argv[ppsz_argc++] = p_data;
100 RegCloseKey( h_key );
102 ppsz_argv[ppsz_argc++] = "--no-one-instance";
104 #endif /* XP_MACOSX */
106 /* common settings */
107 ppsz_argv[ppsz_argc++] = "-vv";
108 ppsz_argv[ppsz_argc++] = "--no-stats";
109 ppsz_argv[ppsz_argc++] = "--no-media-library";
110 ppsz_argv[ppsz_argc++] = "--intf";
111 ppsz_argv[ppsz_argc++] = "dummy";
113 const char *progid = NULL;
115 /* parse plugin arguments */
116 for( int i = 0; i < argc ; i++ )
118 fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
120 if( !strcmp( argn[i], "target" )
121 || !strcmp( argn[i], "mrl")
122 || !strcmp( argn[i], "filename")
123 || !strcmp( argn[i], "src") )
125 psz_target = argv[i];
127 else if( !strcmp( argn[i], "autoplay")
128 || !strcmp( argn[i], "autostart") )
130 b_autoplay = boolValue(argv[i]);
132 else if( !strcmp( argn[i], "fullscreen" ) )
134 if( boolValue(argv[i]) )
136 ppsz_argv[ppsz_argc++] = "--fullscreen";
140 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
143 else if( !strcmp( argn[i], "mute" ) )
145 if( boolValue(argv[i]) )
147 ppsz_argv[ppsz_argc++] = "--volume";
148 ppsz_argv[ppsz_argc++] = "0";
151 else if( !strcmp( argn[i], "loop")
152 || !strcmp( argn[i], "autoloop") )
154 if( boolValue(argv[i]) )
156 ppsz_argv[ppsz_argc++] = "--loop";
159 ppsz_argv[ppsz_argc++] = "--no-loop";
162 else if( !strcmp( argn[i], "version")
163 || !strcmp( argn[i], "progid") )
169 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
170 if( ! libvlc_instance )
172 return NPERR_GENERIC_ERROR;
176 ** fetch plugin base URL, which is the URL of the page containing the plugin
177 ** this URL is used for making absolute URL from relative URL that may be
178 ** passed as an MRL argument
182 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
185 ** is there a better way to get that info ?
187 static const char docLocHref[] = "document.location.href";
191 script.utf8characters = docLocHref;
192 script.utf8length = sizeof(docLocHref)-1;
194 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
196 if( NPVARIANT_IS_STRING(result) )
198 NPString &location = NPVARIANT_TO_STRING(result);
200 psz_baseURL = new char[location.utf8length+1];
203 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
204 psz_baseURL[location.utf8length] = '\0';
207 NPN_ReleaseVariantValue(&result);
209 NPN_ReleaseObject(plugin);
214 // get absolute URL from src
215 char *psz_absurl = getAbsoluteURL(psz_target);
216 psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
219 /* assign plugin script root class */
220 if( (NULL != progid) && (!strcmp(progid, "VideoLAN.VLCPlugin.2")) )
223 p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
228 p_scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
231 return NPERR_NO_ERROR;
236 /* This is really ugly but there is a deadlock when stopping a stream
237 * (in VLC_CleanUp()) because the video output is a child of the drawable but
238 * is in a different thread. */
239 static void HackStopVout( VlcPlugin* p_plugin )
245 int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
246 VLC_VariableGet( i_vlc, "drawable", &value );
248 hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
251 PostMessage( hwnd, WM_CLOSE, 0, 0 );
255 while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
257 TranslateMessage(&msg);
258 DispatchMessage(&msg);
260 if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
262 while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
267 VlcPlugin::~VlcPlugin()
272 libvlc_log_close(libvlc_log, NULL);
273 if( libvlc_instance )
274 libvlc_release(libvlc_instance);
277 /*****************************************************************************
279 *****************************************************************************/
281 char *VlcPlugin::getAbsoluteURL(const char *url)
285 // check whether URL is already absolute
286 const char *end=strchr(url, ':');
287 if( (NULL != end) && (end != url) )
289 // validate protocol header
290 const char *start = url;
295 while( start != end )
302 || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
303 // not valid protocol header, assume relative URL
307 /* we have a protocol header, therefore URL is absolute */
310 // not a valid protocol header, assume relative URL
317 size_t baseLen = strlen(psz_baseURL);
318 char *href = new char[baseLen+strlen(url)+1];
321 /* prepend base URL */
322 strcpy(href, psz_baseURL);
325 ** relative url could be empty,
326 ** in which case return base URL
332 ** locate pathname part of base URL
335 /* skip over protocol part */
336 char *pathstart = strchr(href, ':');
340 if( '/' == *(++pathstart) )
342 if( '/' == *(++pathstart) )
347 /* skip over host part */
348 pathstart = strchr(pathstart, '/');
349 pathend = href+baseLen;
352 // no path, add a / past end of url (over '\0')
359 /* baseURL is just a UNIX path */
362 /* baseURL is not an absolute path */
366 pathend = href+baseLen;
369 /* relative URL made of an absolute path ? */
372 /* replace path completely */
373 strcpy(pathstart, url);
377 /* find last path component and replace it */
378 while( '/' != *pathend)
382 ** if relative url path starts with one or more '../',
383 ** factor them out of href so that we return a
386 while( pathend != pathstart )
394 /* relative url is just '.' */
400 /* relative url starts with './' */
409 /* relative url is '..' */
415 /* relative url starts with '../' */
423 while( '/' != *pathend );
425 /* skip over '/' separator */
427 /* concatenate remaining base URL and relative URL */
428 strcpy(pathend, url);
437 int VlcPlugin::setSize(unsigned width, unsigned height)
439 int diff = (width != i_width) || (height != i_height);
448 void VlcPlugin::showToolbar()
450 const NPWindow& window = getWindow();
451 Window control = getControlWindow();
452 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
455 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
456 &p_btnPlay, NULL, NULL);
458 i_control_height = __MAX( i_control_height, p_btnPlay->height );
460 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
461 &p_btnPause, NULL, NULL);
463 i_control_height = __MAX( i_control_height, p_btnPause->height );
465 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
466 &p_btnStop, NULL, NULL );
468 i_control_height = __MAX( i_control_height, p_btnStop->height );
470 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
471 &p_timeline, NULL, NULL);
473 i_control_height = __MAX( i_control_height, p_timeline->height );
475 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
476 &p_btnTime, NULL, NULL);
478 i_control_height = __MAX( i_control_height, p_btnTime->height );
480 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
481 &p_btnFullscreen, NULL, NULL);
482 if( p_btnFullscreen )
483 i_control_height = __MAX( i_control_height, p_btnFullscreen->height);
485 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
486 &p_btnMute, NULL, NULL);
488 i_control_height = __MAX( i_control_height, p_btnMute->height);
490 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
491 &p_btnUnmute, NULL, NULL);
493 i_control_height = __MAX( i_control_height, p_btnUnmute->height);
495 if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
496 !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
497 fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
500 void VlcPlugin::hideToolbar()
502 if( p_btnPlay ) XDestroyImage( p_btnPlay );
503 if( p_btnPause ) XDestroyImage( p_btnPause );
504 if( p_btnStop ) XDestroyImage( p_btnStop );
505 if( p_timeline ) XDestroyImage( p_timeline );
506 if( p_btnTime ) XDestroyImage( p_btnTime );
507 if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
508 if( p_btnMute ) XDestroyImage( p_btnMute );
509 if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
516 p_btnFullscreen = NULL;
521 void VlcPlugin::redrawToolbar()
523 libvlc_media_instance_t *p_md = NULL;
524 libvlc_exception_t ex;
525 float f_position = 0.0;
532 const NPWindow& window = getWindow();
533 Window control = getControlWindow();
534 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
536 /* get media instance */
537 libvlc_exception_init( &ex );
538 p_md = libvlc_playlist_get_media_instance( getVLC(), &ex );
539 libvlc_exception_clear( &ex );
542 libvlc_exception_init( &ex );
543 i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
544 libvlc_exception_clear( &ex );
547 libvlc_exception_init(&ex);
548 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
549 libvlc_exception_clear( &ex );
551 /* get movie position in % */
554 libvlc_exception_init( &ex );
555 f_position = libvlc_media_instance_get_position( p_md, &ex ) * 100;
556 libvlc_exception_clear( &ex );
558 libvlc_media_instance_release( p_md );
560 gcv.foreground = BlackPixel( p_display, 0 );
561 gc = XCreateGC( p_display, control, GCForeground, &gcv );
563 XFillRectangle( p_display, control, gc,
564 0, 0, window.width, i_control_height );
565 gcv.foreground = WhitePixel( p_display, 0 );
566 XChangeGC( p_display, gc, GCForeground, &gcv );
569 fprintf( stderr, ">>>>>> is playing = %d\n", i_playing );
570 if( p_btnPause && (i_playing == 1) )
572 XPutImage( p_display, control, gc, p_btnPause, 0, 0, 4, 14,
573 p_btnPause->width, p_btnPause->height );
577 XPutImage( p_display, control, gc, p_btnPlay, 0, 0, 4, 14,
578 p_btnPlay->width, p_btnPlay->height );
582 XPutImage( p_display, control, gc, p_btnStop, 0, 0, 39, 14,
583 p_btnStop->width, p_btnStop->height );
584 if( p_btnFullscreen )
585 XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, 67, 21,
586 p_btnFullscreen->width, p_btnFullscreen->height );
588 if( p_btnUnmute && b_mute )
590 XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, 94, 30,
591 p_btnUnmute->width, p_btnUnmute->height );
595 XPutImage( p_display, control, gc, p_btnMute, 0, 0, 94, 30,
596 p_btnMute->width, p_btnMute->height );
600 XPutImage( p_display, control, gc, p_timeline, 0, 0, 4, 4,
601 (window.width-8), p_timeline->height );
604 i_last_position = (((float)window.width-8.0)/100.0)*f_position;
606 XPutImage( p_display, control, gc, p_btnTime,
607 0, 0, (4+i_last_position), 2,
608 p_btnTime->width, p_btnTime->height );
610 XFreeGC( p_display, gc );