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 ) :
48 libvlc_instance(NULL),
57 ,i_width((unsigned)-1)
58 ,i_height((unsigned)-1)
61 memset(&npwindow, 0, sizeof(NPWindow));
64 static bool boolValue(const char *value) {
65 return ( !strcmp(value, "1") ||
66 !strcasecmp(value, "true") ||
67 !strcasecmp(value, "yes") );
70 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
72 /* prepare VLC command line */
73 char *ppsz_argv[32] = { "vlc" };
76 /* locate VLC module path */
78 ppsz_argv[ppsz_argc++] = "--plugin-path";
79 ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
80 "Contents/MacOS/modules";
83 DWORD i_type, i_data = MAX_PATH + 1;
84 char p_data[MAX_PATH + 1];
85 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
86 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
88 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
89 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
91 if( i_type == REG_SZ )
93 strcat( p_data, "\\plugins000" );
94 ppsz_argv[ppsz_argc++] = "--plugin-path";
95 ppsz_argv[ppsz_argc++] = p_data;
100 ppsz_argv[ppsz_argc++] = "--no-one-instance";
103 ppsz_argv[0] = "C:\\Cygwin\\home\\damienf\\vlc-trunk\\vlc";
106 #endif /* XP_MACOSX */
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++] = "--intf";
113 ppsz_argv[ppsz_argc++] = "dummy";
115 const char *progid = NULL;
117 /* parse plugin arguments */
118 for( int i = 0; i < argc ; i++ )
120 fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
122 if( !strcmp( argn[i], "target" )
123 || !strcmp( argn[i], "mrl")
124 || !strcmp( argn[i], "filename")
125 || !strcmp( argn[i], "src") )
127 psz_target = argv[i];
129 else if( !strcmp( argn[i], "autoplay")
130 || !strcmp( argn[i], "autostart") )
132 b_autoplay = boolValue(argv[i]);
134 else if( !strcmp( argn[i], "fullscreen" ) )
136 if( boolValue(argv[i]) )
138 ppsz_argv[ppsz_argc++] = "--fullscreen";
142 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
145 else if( !strcmp( argn[i], "mute" ) )
147 if( boolValue(argv[i]) )
149 ppsz_argv[ppsz_argc++] = "--volume";
150 ppsz_argv[ppsz_argc++] = "0";
153 else if( !strcmp( argn[i], "loop")
154 || !strcmp( argn[i], "autoloop") )
156 if( boolValue(argv[i]) )
158 ppsz_argv[ppsz_argc++] = "--loop";
161 ppsz_argv[ppsz_argc++] = "--no-loop";
164 else if( !strcmp( argn[i], "version")
165 || !strcmp( argn[i], "progid") )
171 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
172 if( ! libvlc_instance )
174 return NPERR_GENERIC_ERROR;
178 ** fetch plugin base URL, which is the URL of the page containing the plugin
179 ** this URL is used for making absolute URL from relative URL that may be
180 ** passed as an MRL argument
184 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
187 ** is there a better way to get that info ?
189 static const char docLocHref[] = "document.location.href";
193 script.utf8characters = docLocHref;
194 script.utf8length = sizeof(docLocHref)-1;
196 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
198 if( NPVARIANT_IS_STRING(result) )
200 NPString &location = NPVARIANT_TO_STRING(result);
202 psz_baseURL = new char[location.utf8length+1];
205 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
206 psz_baseURL[location.utf8length] = '\0';
209 NPN_ReleaseVariantValue(&result);
211 NPN_ReleaseObject(plugin);
216 // get absolute URL from src
217 char *psz_absurl = getAbsoluteURL(psz_target);
218 psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
221 /* assign plugin script root class */
222 if( (NULL != progid) && (!strcmp(progid, "VideoLAN.VLCPlugin.2")) )
225 p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
230 p_scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
233 return NPERR_NO_ERROR;
238 /* This is really ugly but there is a deadlock when stopping a stream
239 * (in VLC_CleanUp()) because the video output is a child of the drawable but
240 * is in a different thread. */
241 static void HackStopVout( VlcPlugin* p_plugin )
247 int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
248 VLC_VariableGet( i_vlc, "drawable", &value );
250 hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
253 PostMessage( hwnd, WM_CLOSE, 0, 0 );
257 while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
259 TranslateMessage(&msg);
260 DispatchMessage(&msg);
262 if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
264 while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
269 VlcPlugin::~VlcPlugin()
274 libvlc_log_close(libvlc_log, NULL);
275 if( libvlc_instance )
276 libvlc_destroy(libvlc_instance, NULL );
279 /*****************************************************************************
281 *****************************************************************************/
283 char *VlcPlugin::getAbsoluteURL(const char *url)
287 // check whether URL is already absolute
288 const char *end=strchr(url, ':');
289 if( (NULL != end) && (end != url) )
291 // validate protocol header
292 const char *start = url;
297 while( start != end )
304 || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
305 // not valid protocol header, assume relative URL
309 /* we have a protocol header, therefore URL is absolute */
312 // not a valid protocol header, assume relative URL
319 size_t baseLen = strlen(psz_baseURL);
320 char *href = new char[baseLen+strlen(url)+1];
323 /* prepend base URL */
324 strcpy(href, psz_baseURL);
327 ** relative url could be empty,
328 ** in which case return base URL
334 ** locate pathname part of base URL
337 /* skip over protocol part */
338 char *pathstart = strchr(href, ':');
342 if( '/' == *(++pathstart) )
344 if( '/' == *(++pathstart) )
349 /* skip over host part */
350 pathstart = strchr(pathstart, '/');
351 pathend = href+baseLen;
354 // no path, add a / past end of url (over '\0')
361 /* baseURL is just a UNIX path */
364 /* baseURL is not an absolute path */
368 pathend = href+baseLen;
371 /* relative URL made of an absolute path ? */
374 /* replace path completely */
375 strcpy(pathstart, url);
379 /* find last path component and replace it */
380 while( '/' != *pathend)
384 ** if relative url path starts with one or more '../',
385 ** factor them out of href so that we return a
388 while( pathend != pathstart )
396 /* relative url is just '.' */
402 /* relative url starts with './' */
411 /* relative url is '..' */
417 /* relative url starts with '../' */
425 while( '/' != *pathend );
427 /* skip over '/' separator */
429 /* concatenate remaining base URL and relative URL */
430 strcpy(pathend, url);
439 int VlcPlugin::setSize(unsigned width, unsigned height)
441 int diff = (width != i_width) || (height != i_height);