]> git.sesse.net Git - vlc/blob - mozilla/vlcplugin.cpp
Use a separate message bank for each libvlc instance
[vlc] / 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 /*****************************************************************************
39  * VlcPlugin constructor and destructor
40  *****************************************************************************/
41 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
42     i_npmode(mode),
43     b_stream(0),
44     b_autoplay(0),
45     psz_target(NULL),
46     libvlc_instance(NULL),
47     scriptClass(NULL),
48     p_browser(instance),
49     psz_baseURL(NULL)
50 #if XP_WIN
51     ,pf_wndproc(NULL)
52 #endif
53 #if XP_UNIX
54     ,i_width((unsigned)-1)
55     ,i_height((unsigned)-1)
56 #endif
57 {
58     memset(&npwindow, 0, sizeof(NPWindow));
59 }
60
61 static int boolValue(const char *value) {
62     return ( !strcmp(value, "1") || 
63              !strcasecmp(value, "true") ||
64              !strcasecmp(value, "yes") );
65 }
66
67 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
68 {
69     /* prepare VLC command line */
70     char *ppsz_argv[32] =
71     {
72         "vlc",
73         "-vv",
74         "--no-stats",
75         "--no-media-library",
76         "--intf", "dummy",
77     };
78     int ppsz_argc = 6;
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, "\\vlc" );
98                  ppsz_argv[0] = p_data;
99              }
100          }
101          RegCloseKey( h_key );
102     }
103     ppsz_argv[ppsz_argc++] = "--no-one-instance";
104 #if 0
105     ppsz_argv[ppsz_argc++] = "--fast-mutex";
106     ppsz_argv[ppsz_argc++] = "--win9x-cv-method=1";
107 #endif
108
109 #endif /* XP_MACOSX */
110
111     const char *version = NULL;
112
113     /* parse plugin arguments */
114     for( int i = 0; i < argc ; i++ )
115     {
116         fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
117
118         if( !strcmp( argn[i], "target" )
119          || !strcmp( argn[i], "mrl")
120          || !strcmp( argn[i], "filename")
121          || !strcmp( argn[i], "src") )
122         {
123             psz_target = argv[i];
124         }
125         else if( !strcmp( argn[i], "autoplay")
126               || !strcmp( argn[i], "autostart") )
127         {
128             b_autoplay = boolValue(argv[i]);
129         }
130         else if( !strcmp( argn[i], "fullscreen" ) )
131         {
132             if( boolValue(argv[i]) )
133             {
134                 ppsz_argv[ppsz_argc++] = "--fullscreen";
135             }
136             else
137             {
138                 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
139             }
140         }
141         else if( !strcmp( argn[i], "mute" ) )
142         {
143             if( boolValue(argv[i]) )
144             {
145                 ppsz_argv[ppsz_argc++] = "--volume";
146                 ppsz_argv[ppsz_argc++] = "0";
147             }
148         }
149         else if( !strcmp( argn[i], "loop")
150               || !strcmp( argn[i], "autoloop") )
151         {
152             if( boolValue(argv[i]) )
153             {
154                 ppsz_argv[ppsz_argc++] = "--loop";
155             }
156             else {
157                 ppsz_argv[ppsz_argc++] = "--no-loop";
158             }
159         }
160         else if( !strcmp( argn[i], "version") )
161         {
162             version = argv[i];
163         }
164     }
165
166     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
167     if( ! libvlc_instance )
168     {
169         return NPERR_GENERIC_ERROR;
170     }
171
172     /*
173     ** fetch plugin base URL, which is the URL of the page containing the plugin
174     ** this URL is used for making absolute URL from relative URL that may be
175     ** passed as an MRL argument
176     */
177     NPObject *plugin;
178
179     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
180     {
181         /*
182         ** is there a better way to get that info ?
183         */
184         static const char docLocHref[] = "document.location.href";
185         NPString script;
186         NPVariant result;
187
188         script.utf8characters = docLocHref;
189         script.utf8length = sizeof(docLocHref)-1;
190
191         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
192         {
193             if( NPVARIANT_IS_STRING(result) )
194             {
195                 NPString &location = NPVARIANT_TO_STRING(result);
196
197                 psz_baseURL = new char[location.utf8length+1];
198                 if( psz_baseURL )
199                 {
200                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
201                     psz_baseURL[location.utf8length] = '\0';
202                 }
203             }
204             NPN_ReleaseVariantValue(&result);
205         }
206         NPN_ReleaseObject(plugin);
207     }
208
209     if( psz_target )
210     {
211         // get absolute URL from src
212         psz_target = getAbsoluteURL(psz_target);
213     }
214
215     /* assign plugin script root class */
216     if( (NULL != version) && (!strcmp(version, "VideoLAN.VLCPlugin.2")) )
217     {
218         /* new APIs */
219         scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
220     }
221     else
222     {
223         /* legacy APIs */
224         scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
225     }
226
227     return NPERR_NO_ERROR;
228 }
229
230 #if 0
231 #ifdef XP_WIN
232 /* This is really ugly but there is a deadlock when stopping a stream
233  * (in VLC_CleanUp()) because the video output is a child of the drawable but
234  * is in a different thread. */
235 static void HackStopVout( VlcPlugin* p_plugin )
236 {
237     MSG msg;
238     HWND hwnd;
239     vlc_value_t value;
240
241     int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
242     VLC_VariableGet( i_vlc, "drawable", &value );
243
244     hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
245     if( !hwnd ) return;
246
247     PostMessage( hwnd, WM_CLOSE, 0, 0 );
248
249     do
250     {
251         while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
252         {
253             TranslateMessage(&msg);
254             DispatchMessage(&msg);
255         }
256         if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
257     }
258     while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
259 }
260 #endif /* XP_WIN */
261 #endif
262
263 VlcPlugin::~VlcPlugin()
264 {
265     delete psz_baseURL;
266     delete psz_target;
267     if( libvlc_instance )
268         libvlc_destroy(libvlc_instance, NULL );
269 }
270
271 /*****************************************************************************
272  * VlcPlugin methods
273  *****************************************************************************/
274
275 char *VlcPlugin::getAbsoluteURL(const char *url)
276 {
277     if( NULL != url )
278     {
279         // check whether URL is already absolute
280         const char *end=strchr(url, ':');
281         if( (NULL != end) && (end != url) )
282         {
283             // validate protocol header
284             const char *start = url;
285             while( start != end ) {
286                 char c = *start | 0x20;
287                 if( (c < 'a') || (c > 'z') )
288                     // not valid protocol header, assume relative URL
289                     break;
290                 ++start;
291             }
292             /* we have a protocol header, therefore URL is absolute */
293             return strdup(url);
294         }
295
296         if( psz_baseURL )
297         {
298             size_t baseLen = strlen(psz_baseURL);
299             char *href = new char[baseLen+strlen(url)];
300             if( href )
301             {
302                 /* prepend base URL */
303                 strcpy(href, psz_baseURL);
304
305                 /*
306                 ** relative url could be empty,
307                 ** in which case return base URL
308                 */
309                 if( '\0' == *url )
310                     return href;
311
312                 /*
313                 ** locate pathname part of base URL
314                 */
315
316                 /* skip over protocol part  */
317                 char *pathstart = strchr(href, ':');
318                 char *pathend;
319                 if( '/' == *(++pathstart) )
320                 {
321                     if( '/' == *(++pathstart) )
322                     {
323                         ++pathstart;
324                     }
325                 }
326                 /* skip over host part */
327                 pathstart = strchr(pathstart, '/');
328                 pathend = href+baseLen;
329                 if( ! pathstart )
330                 {
331                     // no path, add a / past end of url (over '\0')
332                     pathstart = pathend;
333                     *pathstart = '/';
334                 }
335
336                 /* relative URL made of an absolute path ? */
337                 if( '/' == *url )
338                 {
339                     /* replace path completely */
340                     strcpy(pathstart, url);
341                     return href;
342                 }
343
344                 /* find last path component and replace it */ 
345                 while( '/' != *pathend) --pathend;
346
347                 /*
348                 ** if relative url path starts with one or more '../',
349                 ** factor them out of href so that we return a
350                 ** normalized URL
351                 */
352                 while( pathend != pathstart )
353                 {
354                     const char *p = url;
355                     if( '.' != *p )
356                         break;
357                     ++p;
358                     if( '.' != *p ) 
359                         break;
360                     ++p;
361                     if( '/' != *p ) 
362                         break;
363                     ++p;
364                     url = p;
365                     while( '/' != *pathend ) --pathend;
366                 }
367                 /* concatenate remaining base URL and relative URL */
368                 strcpy(pathend+1, url);
369             }
370             return href;
371         }
372     }
373     return NULL;
374 }
375
376 #if XP_UNIX
377 int  VlcPlugin::setSize(unsigned width, unsigned height)
378 {
379     int diff = (width != i_width) || (height != i_height);
380
381     i_width = width;
382     i_height = height;
383
384     /* return size */
385     return diff;
386 }
387 #endif
388