]> git.sesse.net Git - vlc/blob - mozilla/vlcplugin.cpp
- bye, bye "fast-mutex" and "win9x-cv-method" variables
[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 #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     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 #endif
60 {
61     memset(&npwindow, 0, sizeof(NPWindow));
62 }
63
64 static bool boolValue(const char *value) {
65     return ( !strcmp(value, "1") || 
66              !strcasecmp(value, "true") ||
67              !strcasecmp(value, "yes") );
68 }
69
70 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
71 {
72     /* prepare VLC command line */
73     char *ppsz_argv[32] = { "vlc" };
74     int ppsz_argc = 1;
75
76     /* locate VLC module path */
77 #ifdef XP_MACOSX
78     ppsz_argv[ppsz_argc++] = "--plugin-path";
79     ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
80                              "Contents/MacOS/modules";
81 #elif defined(XP_WIN)
82     HKEY h_key;
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 )
87     {
88          if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
89                               (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
90          {
91              if( i_type == REG_SZ )
92              {
93                  strcat( p_data, "\\plugins" );
94                  ppsz_argv[ppsz_argc++] = "--plugin-path";
95                  ppsz_argv[ppsz_argc++] = p_data;
96              }
97          }
98          RegCloseKey( h_key );
99     }
100     ppsz_argv[ppsz_argc++] = "--no-one-instance";
101
102 #if 0
103     ppsz_argv[0] = "C:\\Cygwin\\home\\damienf\\vlc-trunk\\vlc";
104 #endif
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++] = "--intf";
113     ppsz_argv[ppsz_argc++] = "dummy";
114
115     const char *progid = NULL;
116
117     /* parse plugin arguments */
118     for( int i = 0; i < argc ; i++ )
119     {
120         fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
121
122         if( !strcmp( argn[i], "target" )
123          || !strcmp( argn[i], "mrl")
124          || !strcmp( argn[i], "filename")
125          || !strcmp( argn[i], "src") )
126         {
127             psz_target = argv[i];
128         }
129         else if( !strcmp( argn[i], "autoplay")
130               || !strcmp( argn[i], "autostart") )
131         {
132             b_autoplay = boolValue(argv[i]);
133         }
134         else if( !strcmp( argn[i], "fullscreen" ) )
135         {
136             if( boolValue(argv[i]) )
137             {
138                 ppsz_argv[ppsz_argc++] = "--fullscreen";
139             }
140             else
141             {
142                 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
143             }
144         }
145         else if( !strcmp( argn[i], "mute" ) )
146         {
147             if( boolValue(argv[i]) )
148             {
149                 ppsz_argv[ppsz_argc++] = "--volume";
150                 ppsz_argv[ppsz_argc++] = "0";
151             }
152         }
153         else if( !strcmp( argn[i], "loop")
154               || !strcmp( argn[i], "autoloop") )
155         {
156             if( boolValue(argv[i]) )
157             {
158                 ppsz_argv[ppsz_argc++] = "--loop";
159             }
160             else {
161                 ppsz_argv[ppsz_argc++] = "--no-loop";
162             }
163         }
164         else if( !strcmp( argn[i], "version")
165               || !strcmp( argn[i], "progid") )
166         {
167             progid = argv[i];
168         }
169     }
170
171     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
172     if( ! libvlc_instance )
173     {
174         return NPERR_GENERIC_ERROR;
175     }
176
177     /*
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
181     */
182     NPObject *plugin;
183
184     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
185     {
186         /*
187         ** is there a better way to get that info ?
188         */
189         static const char docLocHref[] = "document.location.href";
190         NPString script;
191         NPVariant result;
192
193         script.utf8characters = docLocHref;
194         script.utf8length = sizeof(docLocHref)-1;
195
196         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
197         {
198             if( NPVARIANT_IS_STRING(result) )
199             {
200                 NPString &location = NPVARIANT_TO_STRING(result);
201
202                 psz_baseURL = new char[location.utf8length+1];
203                 if( psz_baseURL )
204                 {
205                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
206                     psz_baseURL[location.utf8length] = '\0';
207                 }
208             }
209             NPN_ReleaseVariantValue(&result);
210         }
211         NPN_ReleaseObject(plugin);
212     }
213
214     if( psz_target )
215     {
216         // get absolute URL from src
217         char *psz_absurl = getAbsoluteURL(psz_target);
218         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
219     }
220
221     /* assign plugin script root class */
222     if( (NULL != progid) && (!strcmp(progid, "VideoLAN.VLCPlugin.2")) )
223     {
224         /* new APIs */
225         p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
226     }
227     else
228     {
229         /* legacy APIs */
230         p_scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
231     }
232
233     return NPERR_NO_ERROR;
234 }
235
236 #if 0
237 #ifdef XP_WIN
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 )
242 {
243     MSG msg;
244     HWND hwnd;
245     vlc_value_t value;
246
247     int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
248     VLC_VariableGet( i_vlc, "drawable", &value );
249
250     hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
251     if( !hwnd ) return;
252
253     PostMessage( hwnd, WM_CLOSE, 0, 0 );
254
255     do
256     {
257         while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
258         {
259             TranslateMessage(&msg);
260             DispatchMessage(&msg);
261         }
262         if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
263     }
264     while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
265 }
266 #endif /* XP_WIN */
267 #endif
268
269 VlcPlugin::~VlcPlugin()
270 {
271     delete psz_baseURL;
272     delete psz_target;
273     if( libvlc_log )
274         libvlc_log_close(libvlc_log, NULL);
275     if( libvlc_instance )
276         libvlc_destroy(libvlc_instance, NULL );
277 }
278
279 /*****************************************************************************
280  * VlcPlugin methods
281  *****************************************************************************/
282
283 char *VlcPlugin::getAbsoluteURL(const char *url)
284 {
285     if( NULL != url )
286     {
287         // check whether URL is already absolute
288         const char *end=strchr(url, ':');
289         if( (NULL != end) && (end != url) )
290         {
291             // validate protocol header
292             const char *start = url;
293             while( start != end ) {
294                 char c = tolower(*start);
295                 if( (c < 'a') || (c > 'z') )
296                     // not valid protocol header, assume relative URL
297                     goto relativeurl;
298                 ++start;
299             }
300             /* we have a protocol header, therefore URL is absolute */
301             return strdup(url);
302         }
303
304 relativeurl:
305
306         if( psz_baseURL )
307         {
308             size_t baseLen = strlen(psz_baseURL);
309             char *href = new char[baseLen+strlen(url)+1];
310             if( href )
311             {
312                 /* prepend base URL */
313                 strcpy(href, psz_baseURL);
314
315                 /*
316                 ** relative url could be empty,
317                 ** in which case return base URL
318                 */
319                 if( '\0' == *url )
320                     return href;
321
322                 /*
323                 ** locate pathname part of base URL
324                 */
325
326                 /* skip over protocol part  */
327                 char *pathstart = strchr(href, ':');
328                 char *pathend;
329                 if( pathstart )
330                 {
331                     if( '/' == *(++pathstart) )
332                     {
333                         if( '/' == *(++pathstart) )
334                         {
335                             ++pathstart;
336                         }
337                     }
338                     /* skip over host part */
339                     pathstart = strchr(pathstart, '/');
340                     pathend = href+baseLen;
341                     if( ! pathstart )
342                     {
343                         // no path, add a / past end of url (over '\0')
344                         pathstart = pathend;
345                         *pathstart = '/';
346                     }
347                 }
348                 else
349                 {
350                     /* baseURL is just a UNIX path */
351                     if( '/' != *href )
352                     {
353                         /* baseURL is not an absolute path */
354                         return NULL;
355                     }
356                     pathstart = href;
357                     pathend = href+baseLen;
358                 }
359
360                 /* relative URL made of an absolute path ? */
361                 if( '/' == *url )
362                 {
363                     /* replace path completely */
364                     strcpy(pathstart, url);
365                     return href;
366                 }
367
368                 /* find last path component and replace it */ 
369                 while( '/' != *pathend)
370                     --pathend;
371
372                 /*
373                 ** if relative url path starts with one or more '../',
374                 ** factor them out of href so that we return a
375                 ** normalized URL
376                 */
377                 while( pathend != pathstart )
378                 {
379                     const char *p = url;
380                     if( '.' != *p )
381                         break;
382                     ++p;
383                     if( '\0' == *p  )
384                     {
385                         /* relative url is just '.' */
386                         url = p;
387                         break;
388                     }
389                     if( '/' == *p  )
390                     {
391                         /* relative url starts with './' */
392                         url = ++p;
393                         continue;
394                     }
395                     if( '.' != *p ) 
396                         break;
397                     ++p;
398                     if( '\0' == *p )
399                     {
400                         /* relative url is '..' */
401                     }
402                     else
403                     {
404                         if( '/' != *p ) 
405                             break;
406                         /* relative url starts with '../' */
407                         ++p;
408                     }
409                     url = p;
410                     do
411                     {
412                         --pathend;
413                     }
414                     while( '/' != *pathend );
415                 }
416                 /* skip over '/' separator */
417                 ++pathend;
418                 /* concatenate remaining base URL and relative URL */
419                 strcpy(pathend, url);
420             }
421             return href;
422         }
423     }
424     return NULL;
425 }
426
427 #if XP_UNIX
428 int  VlcPlugin::setSize(unsigned width, unsigned height)
429 {
430     int diff = (width != i_width) || (height != i_height);
431
432     i_width = width;
433     i_height = height;
434
435     /* return size */
436     return diff;
437 }
438 #endif
439