]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.cpp
Source Tree Layout Cleanup: (As decided at videolan-meeting #2)
[vlc] / projects / 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, "\\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 #endif /* XP_MACOSX */
103
104     /* common settings */
105     ppsz_argv[ppsz_argc++] = "-vv";
106     ppsz_argv[ppsz_argc++] = "--no-stats";
107     ppsz_argv[ppsz_argc++] = "--no-media-library";
108     ppsz_argv[ppsz_argc++] = "--intf";
109     ppsz_argv[ppsz_argc++] = "dummy";
110
111     const char *progid = 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               || !strcmp( argn[i], "progid") )
162         {
163             progid = argv[i];
164         }
165     }
166
167     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
168     if( ! libvlc_instance )
169     {
170         return NPERR_GENERIC_ERROR;
171     }
172
173     /*
174     ** fetch plugin base URL, which is the URL of the page containing the plugin
175     ** this URL is used for making absolute URL from relative URL that may be
176     ** passed as an MRL argument
177     */
178     NPObject *plugin;
179
180     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
181     {
182         /*
183         ** is there a better way to get that info ?
184         */
185         static const char docLocHref[] = "document.location.href";
186         NPString script;
187         NPVariant result;
188
189         script.utf8characters = docLocHref;
190         script.utf8length = sizeof(docLocHref)-1;
191
192         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
193         {
194             if( NPVARIANT_IS_STRING(result) )
195             {
196                 NPString &location = NPVARIANT_TO_STRING(result);
197
198                 psz_baseURL = new char[location.utf8length+1];
199                 if( psz_baseURL )
200                 {
201                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
202                     psz_baseURL[location.utf8length] = '\0';
203                 }
204             }
205             NPN_ReleaseVariantValue(&result);
206         }
207         NPN_ReleaseObject(plugin);
208     }
209
210     if( psz_target )
211     {
212         // get absolute URL from src
213         char *psz_absurl = getAbsoluteURL(psz_target);
214         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
215     }
216
217     /* assign plugin script root class */
218     if( (NULL != progid) && (!strcmp(progid, "VideoLAN.VLCPlugin.2")) )
219     {
220         /* new APIs */
221         p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
222     }
223     else
224     {
225         /* legacy APIs */
226         p_scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
227     }
228
229     return NPERR_NO_ERROR;
230 }
231
232 #if 0
233 #ifdef XP_WIN
234 /* This is really ugly but there is a deadlock when stopping a stream
235  * (in VLC_CleanUp()) because the video output is a child of the drawable but
236  * is in a different thread. */
237 static void HackStopVout( VlcPlugin* p_plugin )
238 {
239     MSG msg;
240     HWND hwnd;
241     vlc_value_t value;
242
243     int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
244     VLC_VariableGet( i_vlc, "drawable", &value );
245
246     hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
247     if( !hwnd ) return;
248
249     PostMessage( hwnd, WM_CLOSE, 0, 0 );
250
251     do
252     {
253         while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
254         {
255             TranslateMessage(&msg);
256             DispatchMessage(&msg);
257         }
258         if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
259     }
260     while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
261 }
262 #endif /* XP_WIN */
263 #endif
264
265 VlcPlugin::~VlcPlugin()
266 {
267     delete psz_baseURL;
268     delete psz_target;
269     if( libvlc_log )
270         libvlc_log_close(libvlc_log, NULL);
271     if( libvlc_instance )
272         libvlc_release(libvlc_instance);
273 }
274
275 /*****************************************************************************
276  * VlcPlugin methods
277  *****************************************************************************/
278
279 char *VlcPlugin::getAbsoluteURL(const char *url)
280 {
281     if( NULL != url )
282     {
283         // check whether URL is already absolute
284         const char *end=strchr(url, ':');
285         if( (NULL != end) && (end != url) )
286         {
287             // validate protocol header
288             const char *start = url;
289             char c = *start;
290             if( isalpha(c) )
291             {
292                 ++start;
293                 while( start != end )
294                 {
295                     c  = *start;
296                     if( ! (isalnum(c)
297                        || ('-' == c)
298                        || ('+' == c)
299                        || ('.' == c)
300                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
301                         // not valid protocol header, assume relative URL
302                         goto relativeurl;
303                     ++start;
304                 }
305                 /* we have a protocol header, therefore URL is absolute */
306                 return strdup(url);
307             }
308             // not a valid protocol header, assume relative URL
309         }
310
311 relativeurl:
312
313         if( psz_baseURL )
314         {
315             size_t baseLen = strlen(psz_baseURL);
316             char *href = new char[baseLen+strlen(url)+1];
317             if( href )
318             {
319                 /* prepend base URL */
320                 strcpy(href, psz_baseURL);
321
322                 /*
323                 ** relative url could be empty,
324                 ** in which case return base URL
325                 */
326                 if( '\0' == *url )
327                     return href;
328
329                 /*
330                 ** locate pathname part of base URL
331                 */
332
333                 /* skip over protocol part  */
334                 char *pathstart = strchr(href, ':');
335                 char *pathend;
336                 if( pathstart )
337                 {
338                     if( '/' == *(++pathstart) )
339                     {
340                         if( '/' == *(++pathstart) )
341                         {
342                             ++pathstart;
343                         }
344                     }
345                     /* skip over host part */
346                     pathstart = strchr(pathstart, '/');
347                     pathend = href+baseLen;
348                     if( ! pathstart )
349                     {
350                         // no path, add a / past end of url (over '\0')
351                         pathstart = pathend;
352                         *pathstart = '/';
353                     }
354                 }
355                 else
356                 {
357                     /* baseURL is just a UNIX path */
358                     if( '/' != *href )
359                     {
360                         /* baseURL is not an absolute path */
361                         return NULL;
362                     }
363                     pathstart = href;
364                     pathend = href+baseLen;
365                 }
366
367                 /* relative URL made of an absolute path ? */
368                 if( '/' == *url )
369                 {
370                     /* replace path completely */
371                     strcpy(pathstart, url);
372                     return href;
373                 }
374
375                 /* find last path component and replace it */
376                 while( '/' != *pathend)
377                     --pathend;
378
379                 /*
380                 ** if relative url path starts with one or more '../',
381                 ** factor them out of href so that we return a
382                 ** normalized URL
383                 */
384                 while( pathend != pathstart )
385                 {
386                     const char *p = url;
387                     if( '.' != *p )
388                         break;
389                     ++p;
390                     if( '\0' == *p  )
391                     {
392                         /* relative url is just '.' */
393                         url = p;
394                         break;
395                     }
396                     if( '/' == *p  )
397                     {
398                         /* relative url starts with './' */
399                         url = ++p;
400                         continue;
401                     }
402                     if( '.' != *p )
403                         break;
404                     ++p;
405                     if( '\0' == *p )
406                     {
407                         /* relative url is '..' */
408                     }
409                     else
410                     {
411                         if( '/' != *p )
412                             break;
413                         /* relative url starts with '../' */
414                         ++p;
415                     }
416                     url = p;
417                     do
418                     {
419                         --pathend;
420                     }
421                     while( '/' != *pathend );
422                 }
423                 /* skip over '/' separator */
424                 ++pathend;
425                 /* concatenate remaining base URL and relative URL */
426                 strcpy(pathend, url);
427             }
428             return href;
429         }
430     }
431     return NULL;
432 }
433
434 #if XP_UNIX
435 int  VlcPlugin::setSize(unsigned width, unsigned height)
436 {
437     int diff = (width != i_width) || (height != i_height);
438
439     i_width = width;
440     i_height = height;
441
442     /* return size */
443     return diff;
444 }
445 #endif
446