mozilla: use new libvlc api
[vlc.git] / 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];
74     int ppsz_argc = 0;
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, "\\plugins000" );
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 1
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_release(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             char c = *start;
294             if( isalpha(c) )
295             {
296                 ++start;
297                 while( start != end )
298                 {
299                     c  = *start;
300                     if( ! (isalnum(c)
301                        || ('-' == c)
302                        || ('+' == c)
303                        || ('.' == c)
304                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
305                         // not valid protocol header, assume relative URL
306                         goto relativeurl;
307                     ++start;
308                 }
309                 /* we have a protocol header, therefore URL is absolute */
310                 return strdup(url);
311             }
312             // not a valid protocol header, assume relative URL
313         }
314
315 relativeurl:
316
317         if( psz_baseURL )
318         {
319             size_t baseLen = strlen(psz_baseURL);
320             char *href = new char[baseLen+strlen(url)+1];
321             if( href )
322             {
323                 /* prepend base URL */
324                 strcpy(href, psz_baseURL);
325
326                 /*
327                 ** relative url could be empty,
328                 ** in which case return base URL
329                 */
330                 if( '\0' == *url )
331                     return href;
332
333                 /*
334                 ** locate pathname part of base URL
335                 */
336
337                 /* skip over protocol part  */
338                 char *pathstart = strchr(href, ':');
339                 char *pathend;
340                 if( pathstart )
341                 {
342                     if( '/' == *(++pathstart) )
343                     {
344                         if( '/' == *(++pathstart) )
345                         {
346                             ++pathstart;
347                         }
348                     }
349                     /* skip over host part */
350                     pathstart = strchr(pathstart, '/');
351                     pathend = href+baseLen;
352                     if( ! pathstart )
353                     {
354                         // no path, add a / past end of url (over '\0')
355                         pathstart = pathend;
356                         *pathstart = '/';
357                     }
358                 }
359                 else
360                 {
361                     /* baseURL is just a UNIX path */
362                     if( '/' != *href )
363                     {
364                         /* baseURL is not an absolute path */
365                         return NULL;
366                     }
367                     pathstart = href;
368                     pathend = href+baseLen;
369                 }
370
371                 /* relative URL made of an absolute path ? */
372                 if( '/' == *url )
373                 {
374                     /* replace path completely */
375                     strcpy(pathstart, url);
376                     return href;
377                 }
378
379                 /* find last path component and replace it */
380                 while( '/' != *pathend)
381                     --pathend;
382
383                 /*
384                 ** if relative url path starts with one or more '../',
385                 ** factor them out of href so that we return a
386                 ** normalized URL
387                 */
388                 while( pathend != pathstart )
389                 {
390                     const char *p = url;
391                     if( '.' != *p )
392                         break;
393                     ++p;
394                     if( '\0' == *p  )
395                     {
396                         /* relative url is just '.' */
397                         url = p;
398                         break;
399                     }
400                     if( '/' == *p  )
401                     {
402                         /* relative url starts with './' */
403                         url = ++p;
404                         continue;
405                     }
406                     if( '.' != *p )
407                         break;
408                     ++p;
409                     if( '\0' == *p )
410                     {
411                         /* relative url is '..' */
412                     }
413                     else
414                     {
415                         if( '/' != *p )
416                             break;
417                         /* relative url starts with '../' */
418                         ++p;
419                     }
420                     url = p;
421                     do
422                     {
423                         --pathend;
424                     }
425                     while( '/' != *pathend );
426                 }
427                 /* skip over '/' separator */
428                 ++pathend;
429                 /* concatenate remaining base URL and relative URL */
430                 strcpy(pathend, url);
431             }
432             return href;
433         }
434     }
435     return NULL;
436 }
437
438 #if XP_UNIX
439 int  VlcPlugin::setSize(unsigned width, unsigned height)
440 {
441     int diff = (width != i_width) || (height != i_height);
442
443     i_width = width;
444     i_height = height;
445
446     /* return size */
447     return diff;
448 }
449 #endif
450