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