]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.cpp
Don't include deleted files.
[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/npolibvlc.h"
36
37 #include <ctype.h>
38
39 /*****************************************************************************
40  * VlcPlugin constructor and destructor
41  *****************************************************************************/
42 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
43     i_npmode(mode),
44     b_stream(0),
45     b_autoplay(1),
46     b_toolbar(0),
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     ,i_tb_width(0)
60     ,i_tb_height(0)
61     ,i_last_position(0)
62 #endif
63 {
64     memset(&npwindow, 0, sizeof(NPWindow));
65 }
66
67 static bool boolValue(const char *value) {
68     return ( !strcmp(value, "1") ||
69              !strcasecmp(value, "true") ||
70              !strcasecmp(value, "yes") );
71 }
72
73 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
74 {
75     /* prepare VLC command line */
76     char *ppsz_argv[32];
77     int ppsz_argc = 0;
78
79     /* locate VLC module path */
80 #ifdef XP_MACOSX
81     ppsz_argv[ppsz_argc++] = "--plugin-path";
82     ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
83                              "Contents/MacOS/modules";
84 #elif defined(XP_WIN)
85     HKEY h_key;
86     DWORD i_type, i_data = MAX_PATH + 1;
87     char p_data[MAX_PATH + 1];
88     if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
89                       0, KEY_READ, &h_key ) == ERROR_SUCCESS )
90     {
91          if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
92                               (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
93          {
94              if( i_type == REG_SZ )
95              {
96                  strcat( p_data, "\\plugins" );
97                  ppsz_argv[ppsz_argc++] = "--plugin-path";
98                  ppsz_argv[ppsz_argc++] = p_data;
99              }
100          }
101          RegCloseKey( h_key );
102     }
103     ppsz_argv[ppsz_argc++] = "--no-one-instance";
104
105 #endif /* XP_MACOSX */
106
107     /* common settings */
108     ppsz_argv[ppsz_argc++] = "-vv";
109     ppsz_argv[ppsz_argc++] = "--no-stats";
110     ppsz_argv[ppsz_argc++] = "--no-media-library";
111     ppsz_argv[ppsz_argc++] = "--ignore-config";
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         else if( !strcmp( argn[i], "toolbar" ) )
170         {
171             b_toolbar = boolValue(argv[i]);
172         }
173     }
174
175
176     libvlc_exception_t ex;
177     libvlc_exception_init(&ex);
178
179     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
180     if( libvlc_exception_raised(&ex) )
181     {
182         libvlc_exception_clear(&ex);
183         return NPERR_GENERIC_ERROR;
184     }
185
186     /*
187     ** fetch plugin base URL, which is the URL of the page containing the plugin
188     ** this URL is used for making absolute URL from relative URL that may be
189     ** passed as an MRL argument
190     */
191     NPObject *plugin;
192
193     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
194     {
195         /*
196         ** is there a better way to get that info ?
197         */
198         static const char docLocHref[] = "document.location.href";
199         NPString script;
200         NPVariant result;
201
202         script.utf8characters = docLocHref;
203         script.utf8length = sizeof(docLocHref)-1;
204
205         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
206         {
207             if( NPVARIANT_IS_STRING(result) )
208             {
209                 NPString &location = NPVARIANT_TO_STRING(result);
210
211                 psz_baseURL = new char[location.utf8length+1];
212                 if( psz_baseURL )
213                 {
214                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
215                     psz_baseURL[location.utf8length] = '\0';
216                 }
217             }
218             NPN_ReleaseVariantValue(&result);
219         }
220         NPN_ReleaseObject(plugin);
221     }
222
223     if( psz_target )
224     {
225         // get absolute URL from src
226         char *psz_absurl = getAbsoluteURL(psz_target);
227         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
228     }
229
230     /* assign plugin script root class */
231     /* new APIs */
232     p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
233
234     return NPERR_NO_ERROR;
235 }
236
237 #if 0
238 #ifdef XP_WIN
239 /* This is really ugly but there is a deadlock when stopping a stream
240  * (in VLC_CleanUp()) because the video output is a child of the drawable but
241  * is in a different thread. */
242 static void HackStopVout( VlcPlugin* p_plugin )
243 {
244     MSG msg;
245     HWND hwnd;
246     vlc_value_t value;
247
248     int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
249     VLC_VariableGet( i_vlc, "drawable", &value );
250
251     hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
252     if( !hwnd ) return;
253
254     PostMessage( hwnd, WM_CLOSE, 0, 0 );
255
256     do
257     {
258         while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
259         {
260             TranslateMessage(&msg);
261             DispatchMessage(&msg);
262         }
263         if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
264     }
265     while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
266 }
267 #endif /* XP_WIN */
268 #endif
269
270 VlcPlugin::~VlcPlugin()
271 {
272     delete psz_baseURL;
273     delete psz_target;
274     if( libvlc_log )
275         libvlc_log_close(libvlc_log, NULL);
276     if( libvlc_instance )
277         libvlc_release(libvlc_instance);
278 }
279
280 /*****************************************************************************
281  * VlcPlugin methods
282  *****************************************************************************/
283
284 char *VlcPlugin::getAbsoluteURL(const char *url)
285 {
286     if( NULL != url )
287     {
288         // check whether URL is already absolute
289         const char *end=strchr(url, ':');
290         if( (NULL != end) && (end != url) )
291         {
292             // validate protocol header
293             const char *start = url;
294             char c = *start;
295             if( isalpha(c) )
296             {
297                 ++start;
298                 while( start != end )
299                 {
300                     c  = *start;
301                     if( ! (isalnum(c)
302                        || ('-' == c)
303                        || ('+' == c)
304                        || ('.' == c)
305                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
306                         // not valid protocol header, assume relative URL
307                         goto relativeurl;
308                     ++start;
309                 }
310                 /* we have a protocol header, therefore URL is absolute */
311                 return strdup(url);
312             }
313             // not a valid protocol header, assume relative URL
314         }
315
316 relativeurl:
317
318         if( psz_baseURL )
319         {
320             size_t baseLen = strlen(psz_baseURL);
321             char *href = new char[baseLen+strlen(url)+1];
322             if( href )
323             {
324                 /* prepend base URL */
325                 strcpy(href, psz_baseURL);
326
327                 /*
328                 ** relative url could be empty,
329                 ** in which case return base URL
330                 */
331                 if( '\0' == *url )
332                     return href;
333
334                 /*
335                 ** locate pathname part of base URL
336                 */
337
338                 /* skip over protocol part  */
339                 char *pathstart = strchr(href, ':');
340                 char *pathend;
341                 if( pathstart )
342                 {
343                     if( '/' == *(++pathstart) )
344                     {
345                         if( '/' == *(++pathstart) )
346                         {
347                             ++pathstart;
348                         }
349                     }
350                     /* skip over host part */
351                     pathstart = strchr(pathstart, '/');
352                     pathend = href+baseLen;
353                     if( ! pathstart )
354                     {
355                         // no path, add a / past end of url (over '\0')
356                         pathstart = pathend;
357                         *pathstart = '/';
358                     }
359                 }
360                 else
361                 {
362                     /* baseURL is just a UNIX path */
363                     if( '/' != *href )
364                     {
365                         /* baseURL is not an absolute path */
366                         return NULL;
367                     }
368                     pathstart = href;
369                     pathend = href+baseLen;
370                 }
371
372                 /* relative URL made of an absolute path ? */
373                 if( '/' == *url )
374                 {
375                     /* replace path completely */
376                     strcpy(pathstart, url);
377                     return href;
378                 }
379
380                 /* find last path component and replace it */
381                 while( '/' != *pathend)
382                     --pathend;
383
384                 /*
385                 ** if relative url path starts with one or more '../',
386                 ** factor them out of href so that we return a
387                 ** normalized URL
388                 */
389                 while( pathend != pathstart )
390                 {
391                     const char *p = url;
392                     if( '.' != *p )
393                         break;
394                     ++p;
395                     if( '\0' == *p  )
396                     {
397                         /* relative url is just '.' */
398                         url = p;
399                         break;
400                     }
401                     if( '/' == *p  )
402                     {
403                         /* relative url starts with './' */
404                         url = ++p;
405                         continue;
406                     }
407                     if( '.' != *p )
408                         break;
409                     ++p;
410                     if( '\0' == *p )
411                     {
412                         /* relative url is '..' */
413                     }
414                     else
415                     {
416                         if( '/' != *p )
417                             break;
418                         /* relative url starts with '../' */
419                         ++p;
420                     }
421                     url = p;
422                     do
423                     {
424                         --pathend;
425                     }
426                     while( '/' != *pathend );
427                 }
428                 /* skip over '/' separator */
429                 ++pathend;
430                 /* concatenate remaining base URL and relative URL */
431                 strcpy(pathend, url);
432             }
433             return href;
434         }
435     }
436     return NULL;
437 }
438
439 #if XP_UNIX
440 int  VlcPlugin::setSize(unsigned width, unsigned height)
441 {
442     int diff = (width != i_width) || (height != i_height);
443
444     i_width = width;
445     i_height = height;
446
447     /* return size */
448     return diff;
449 }
450
451 void VlcPlugin::showToolbar()
452 {
453     const NPWindow& window = getWindow();
454     Window control = getControlWindow();
455     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
456     unsigned int i_height = 0, i_width = 0;
457
458     /* load icons */
459     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
460                         &p_btnPlay, NULL, NULL);
461     if( p_btnPlay )
462     {
463         i_height = __MAX( i_height, p_btnPlay->height );
464         i_width  = __MAX( i_width,  p_btnPlay->width );
465     }
466     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
467                         &p_btnPause, NULL, NULL);
468     if( p_btnPause )
469     {
470         i_height = __MAX( i_height, p_btnPause->height );
471         i_width  = __MAX( i_width,  p_btnPause->width );
472     }
473     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
474                         &p_btnStop, NULL, NULL );
475     if( p_btnStop )
476     {
477         i_height = __MAX( i_height, p_btnStop->height );
478         i_width  = __MAX( i_width,  p_btnStop->width );
479     }
480     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
481                         &p_timeline, NULL, NULL);
482     if( p_timeline )
483     {
484         i_height = __MAX( i_height, p_timeline->height );
485         i_width  = __MAX( i_width,  p_timeline->width );
486     }
487     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
488                         &p_btnTime, NULL, NULL);
489     if( p_btnTime )
490     {
491         i_height = __MAX( i_height, p_btnTime->height );
492         i_width  = __MAX( i_width,  p_btnTime->width );
493     }
494     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
495                         &p_btnFullscreen, NULL, NULL);
496     if( p_btnFullscreen )
497     {
498         i_height = __MAX( i_height, p_btnFullscreen->height );
499         i_width  = __MAX( i_width,  p_btnFullscreen->width );
500     }
501     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
502                         &p_btnMute, NULL, NULL);
503     if( p_btnMute )
504     {
505         i_height = __MAX( i_height, p_btnMute->height );
506         i_width  = __MAX( i_width,  p_btnMute->width );
507     }
508     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
509                         &p_btnUnmute, NULL, NULL);
510     if( p_btnUnmute )
511     {
512         i_height = __MAX( i_height, p_btnUnmute->height );
513         i_width  = __MAX( i_width,  p_btnUnmute->width );
514     }
515     setToolbarSize( i_width, i_height );
516
517     if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
518         !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
519         fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
520 }
521
522 void VlcPlugin::hideToolbar()
523 {
524     i_tb_width = i_tb_height = 0;
525
526     if( p_btnPlay )  XDestroyImage( p_btnPlay );
527     if( p_btnPause ) XDestroyImage( p_btnPause );
528     if( p_btnStop )  XDestroyImage( p_btnStop );
529     if( p_timeline ) XDestroyImage( p_timeline );
530     if( p_btnTime )  XDestroyImage( p_btnTime );
531     if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
532     if( p_btnMute )  XDestroyImage( p_btnMute );
533     if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
534
535     p_btnPlay = NULL;
536     p_btnPause = NULL;
537     p_btnStop = NULL;
538     p_timeline = NULL;
539     p_btnTime = NULL;
540     p_btnFullscreen = NULL;
541     p_btnMute = NULL;
542     p_btnUnmute = NULL;
543 }
544
545 void VlcPlugin::redrawToolbar()
546 {
547     libvlc_media_player_t *p_md = NULL;
548     libvlc_exception_t ex;
549     float f_position = 0.0;
550     int i_playing = 0;
551     bool b_mute = false;
552     unsigned int dst_x, dst_y;
553     GC gc;
554     XGCValues gcv;
555 #define BTN_SPACE ((unsigned int)4)
556
557     const NPWindow& window = getWindow();
558     Window control = getControlWindow();
559     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
560
561     /* get media instance */
562     libvlc_exception_init( &ex );
563     p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
564     libvlc_exception_clear( &ex );
565
566     /* get isplaying */
567     libvlc_exception_init( &ex );
568     i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
569     libvlc_exception_clear( &ex );
570
571     /* get mute info */
572     libvlc_exception_init(&ex);
573     b_mute = libvlc_audio_get_mute( getVLC(), &ex );
574     libvlc_exception_clear( &ex );
575
576     /* get movie position in % */
577     if( i_playing == 1 )
578     {
579         libvlc_exception_init( &ex );
580         f_position = libvlc_media_player_get_position( p_md, &ex ) * 100;
581         libvlc_exception_clear( &ex );
582     }
583     libvlc_media_player_release( p_md );
584
585     gcv.foreground = BlackPixel( p_display, 0 );
586     gc = XCreateGC( p_display, control, GCForeground, &gcv );
587
588     XFillRectangle( p_display, control, gc,
589                     0, 0, window.width, i_tb_height );
590     gcv.foreground = WhitePixel( p_display, 0 );
591     XChangeGC( p_display, gc, GCForeground, &gcv );
592
593     /* position icons */
594     dst_x = 4; dst_y = 4;
595
596     fprintf( stderr, ">>>>>> is playing = %d\n", i_playing );
597     if( p_btnPause && (i_playing == 1) )
598     {
599         XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x, dst_y,
600                    p_btnPause->width, p_btnPause->height );
601     }
602     else if( p_btnPlay )
603     {
604         XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x, dst_y,
605                    p_btnPlay->width, p_btnPlay->height );
606     }
607
608     dst_x += BTN_SPACE + ( p_btnPlay ? p_btnPlay->width : 0 );
609     dst_y = 4;
610
611     if( p_btnStop )
612         XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x, dst_y,
613                    p_btnStop->width, p_btnStop->height );
614
615     dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
616     dst_y = 4;
617
618     if( p_btnFullscreen )
619         XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x, dst_y,
620                    p_btnFullscreen->width, p_btnFullscreen->height );
621
622     dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
623     dst_y = 4;
624
625     if( p_btnUnmute && b_mute )
626     {
627         XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x, dst_y,
628                    p_btnUnmute->width, p_btnUnmute->height );
629
630         dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
631         dst_y = 4;
632     }
633     else if( p_btnMute )
634     {
635         XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x, dst_y,
636                    p_btnMute->width, p_btnMute->height );
637
638         dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
639         dst_y = 4;
640     }
641
642     if( p_timeline )
643         XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x, dst_y,
644                    (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
645
646     if( f_position > 0 )
647         i_last_position = (((float)window.width-8.0)/100.0)*f_position;
648
649     if( p_btnTime )
650         XPutImage( p_display, control, gc, p_btnTime,
651                    0, 0, (dst_x+i_last_position), dst_y,
652                    p_btnTime->width, p_btnTime->height );
653
654     XFreeGC( p_display, gc );
655 }
656 #endif