1 /*****************************************************************************
2 * vlcplugin.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002-2005 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Damien Fouilleul <damienf.fouilleul@laposte.net>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #ifdef HAVE_MOZILLA_CONFIG_H
31 # include <mozilla-config.h>
34 #include "vlcplugin.h"
35 #include "control/npovlc.h"
36 #include "control/npolibvlc.h"
40 /*****************************************************************************
41 * VlcPlugin constructor and destructor
42 *****************************************************************************/
43 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
49 libvlc_instance(NULL),
58 ,i_width((unsigned)-1)
59 ,i_height((unsigned)-1)
65 memset(&npwindow, 0, sizeof(NPWindow));
68 static bool boolValue(const char *value) {
69 return ( !strcmp(value, "1") ||
70 !strcasecmp(value, "true") ||
71 !strcasecmp(value, "yes") );
74 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
76 /* prepare VLC command line */
80 /* locate VLC module path */
82 ppsz_argv[ppsz_argc++] = "--plugin-path";
83 ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
84 "Contents/MacOS/modules";
87 DWORD i_type, i_data = MAX_PATH + 1;
88 char p_data[MAX_PATH + 1];
89 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
90 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
92 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
93 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
95 if( i_type == REG_SZ )
97 strcat( p_data, "\\plugins" );
98 ppsz_argv[ppsz_argc++] = "--plugin-path";
99 ppsz_argv[ppsz_argc++] = p_data;
102 RegCloseKey( h_key );
104 ppsz_argv[ppsz_argc++] = "--no-one-instance";
106 #endif /* XP_MACOSX */
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";
115 const char *progid = NULL;
117 /* parse plugin arguments */
118 for( int i = 0; i < argc ; i++ )
120 fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
122 if( !strcmp( argn[i], "target" )
123 || !strcmp( argn[i], "mrl")
124 || !strcmp( argn[i], "filename")
125 || !strcmp( argn[i], "src") )
127 psz_target = argv[i];
129 else if( !strcmp( argn[i], "autoplay")
130 || !strcmp( argn[i], "autostart") )
132 b_autoplay = boolValue(argv[i]);
134 else if( !strcmp( argn[i], "fullscreen" ) )
136 if( boolValue(argv[i]) )
138 ppsz_argv[ppsz_argc++] = "--fullscreen";
142 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
145 else if( !strcmp( argn[i], "mute" ) )
147 if( boolValue(argv[i]) )
149 ppsz_argv[ppsz_argc++] = "--volume";
150 ppsz_argv[ppsz_argc++] = "0";
153 else if( !strcmp( argn[i], "loop")
154 || !strcmp( argn[i], "autoloop") )
156 if( boolValue(argv[i]) )
158 ppsz_argv[ppsz_argc++] = "--loop";
161 ppsz_argv[ppsz_argc++] = "--no-loop";
164 else if( !strcmp( argn[i], "version")
165 || !strcmp( argn[i], "progid") )
169 else if( !strcmp( argn[i], "toolbar" ) )
171 b_toolbar = boolValue(argv[i]);
177 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
178 if( ! libvlc_instance )
180 return NPERR_GENERIC_ERROR;
184 ** fetch plugin base URL, which is the URL of the page containing the plugin
185 ** this URL is used for making absolute URL from relative URL that may be
186 ** passed as an MRL argument
190 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
193 ** is there a better way to get that info ?
195 static const char docLocHref[] = "document.location.href";
199 script.utf8characters = docLocHref;
200 script.utf8length = sizeof(docLocHref)-1;
202 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
204 if( NPVARIANT_IS_STRING(result) )
206 NPString &location = NPVARIANT_TO_STRING(result);
208 psz_baseURL = new char[location.utf8length+1];
211 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
212 psz_baseURL[location.utf8length] = '\0';
215 NPN_ReleaseVariantValue(&result);
217 NPN_ReleaseObject(plugin);
222 // get absolute URL from src
223 char *psz_absurl = getAbsoluteURL(psz_target);
224 psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
227 /* assign plugin script root class */
228 if( (NULL != progid) && (!strcmp(progid, "VideoLAN.VLCPlugin.2")) )
231 p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
236 p_scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
239 return NPERR_NO_ERROR;
244 /* This is really ugly but there is a deadlock when stopping a stream
245 * (in VLC_CleanUp()) because the video output is a child of the drawable but
246 * is in a different thread. */
247 static void HackStopVout( VlcPlugin* p_plugin )
253 int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
254 VLC_VariableGet( i_vlc, "drawable", &value );
256 hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
259 PostMessage( hwnd, WM_CLOSE, 0, 0 );
263 while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
265 TranslateMessage(&msg);
266 DispatchMessage(&msg);
268 if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
270 while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
275 VlcPlugin::~VlcPlugin()
280 libvlc_log_close(libvlc_log, NULL);
281 if( libvlc_instance )
282 libvlc_release(libvlc_instance);
285 /*****************************************************************************
287 *****************************************************************************/
289 char *VlcPlugin::getAbsoluteURL(const char *url)
293 // check whether URL is already absolute
294 const char *end=strchr(url, ':');
295 if( (NULL != end) && (end != url) )
297 // validate protocol header
298 const char *start = url;
303 while( start != end )
310 || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
311 // not valid protocol header, assume relative URL
315 /* we have a protocol header, therefore URL is absolute */
318 // not a valid protocol header, assume relative URL
325 size_t baseLen = strlen(psz_baseURL);
326 char *href = new char[baseLen+strlen(url)+1];
329 /* prepend base URL */
330 strcpy(href, psz_baseURL);
333 ** relative url could be empty,
334 ** in which case return base URL
340 ** locate pathname part of base URL
343 /* skip over protocol part */
344 char *pathstart = strchr(href, ':');
348 if( '/' == *(++pathstart) )
350 if( '/' == *(++pathstart) )
355 /* skip over host part */
356 pathstart = strchr(pathstart, '/');
357 pathend = href+baseLen;
360 // no path, add a / past end of url (over '\0')
367 /* baseURL is just a UNIX path */
370 /* baseURL is not an absolute path */
374 pathend = href+baseLen;
377 /* relative URL made of an absolute path ? */
380 /* replace path completely */
381 strcpy(pathstart, url);
385 /* find last path component and replace it */
386 while( '/' != *pathend)
390 ** if relative url path starts with one or more '../',
391 ** factor them out of href so that we return a
394 while( pathend != pathstart )
402 /* relative url is just '.' */
408 /* relative url starts with './' */
417 /* relative url is '..' */
423 /* relative url starts with '../' */
431 while( '/' != *pathend );
433 /* skip over '/' separator */
435 /* concatenate remaining base URL and relative URL */
436 strcpy(pathend, url);
445 int VlcPlugin::setSize(unsigned width, unsigned height)
447 int diff = (width != i_width) || (height != i_height);
456 void VlcPlugin::showToolbar()
458 const NPWindow& window = getWindow();
459 Window control = getControlWindow();
460 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
461 unsigned int i_height = 0, i_width = 0;
464 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
465 &p_btnPlay, NULL, NULL);
468 i_height = __MAX( i_height, p_btnPlay->height );
469 i_width = __MAX( i_width, p_btnPlay->width );
471 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
472 &p_btnPause, NULL, NULL);
475 i_height = __MAX( i_height, p_btnPause->height );
476 i_width = __MAX( i_width, p_btnPause->width );
478 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
479 &p_btnStop, NULL, NULL );
482 i_height = __MAX( i_height, p_btnStop->height );
483 i_width = __MAX( i_width, p_btnStop->width );
485 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
486 &p_timeline, NULL, NULL);
489 i_height = __MAX( i_height, p_timeline->height );
490 i_width = __MAX( i_width, p_timeline->width );
492 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
493 &p_btnTime, NULL, NULL);
496 i_height = __MAX( i_height, p_btnTime->height );
497 i_width = __MAX( i_width, p_btnTime->width );
499 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
500 &p_btnFullscreen, NULL, NULL);
501 if( p_btnFullscreen )
503 i_height = __MAX( i_height, p_btnFullscreen->height );
504 i_width = __MAX( i_width, p_btnFullscreen->width );
506 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
507 &p_btnMute, NULL, NULL);
510 i_height = __MAX( i_height, p_btnMute->height );
511 i_width = __MAX( i_width, p_btnMute->width );
513 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
514 &p_btnUnmute, NULL, NULL);
517 i_height = __MAX( i_height, p_btnUnmute->height );
518 i_width = __MAX( i_width, p_btnUnmute->width );
520 setToolbarSize( i_width, i_height );
522 if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
523 !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
524 fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
527 void VlcPlugin::hideToolbar()
529 i_tb_width = i_tb_height = 0;
531 if( p_btnPlay ) XDestroyImage( p_btnPlay );
532 if( p_btnPause ) XDestroyImage( p_btnPause );
533 if( p_btnStop ) XDestroyImage( p_btnStop );
534 if( p_timeline ) XDestroyImage( p_timeline );
535 if( p_btnTime ) XDestroyImage( p_btnTime );
536 if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
537 if( p_btnMute ) XDestroyImage( p_btnMute );
538 if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
545 p_btnFullscreen = NULL;
550 void VlcPlugin::redrawToolbar()
552 libvlc_media_instance_t *p_md = NULL;
553 libvlc_exception_t ex;
554 float f_position = 0.0;
557 unsigned int dst_x, dst_y;
560 #define BTN_SPACE ((unsigned int)4)
562 const NPWindow& window = getWindow();
563 Window control = getControlWindow();
564 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
566 /* get media instance */
567 libvlc_exception_init( &ex );
568 p_md = libvlc_playlist_get_media_instance( getVLC(), &ex );
569 libvlc_exception_clear( &ex );
572 libvlc_exception_init( &ex );
573 i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
574 libvlc_exception_clear( &ex );
577 libvlc_exception_init(&ex);
578 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
579 libvlc_exception_clear( &ex );
581 /* get movie position in % */
584 libvlc_exception_init( &ex );
585 f_position = libvlc_media_instance_get_position( p_md, &ex ) * 100;
586 libvlc_exception_clear( &ex );
588 libvlc_media_instance_release( p_md );
590 gcv.foreground = BlackPixel( p_display, 0 );
591 gc = XCreateGC( p_display, control, GCForeground, &gcv );
593 XFillRectangle( p_display, control, gc,
594 0, 0, window.width, i_tb_height );
595 gcv.foreground = WhitePixel( p_display, 0 );
596 XChangeGC( p_display, gc, GCForeground, &gcv );
599 dst_x = 4; dst_y = 4;
601 fprintf( stderr, ">>>>>> is playing = %d\n", i_playing );
602 if( p_btnPause && (i_playing == 1) )
604 XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x, dst_y,
605 p_btnPause->width, p_btnPause->height );
609 XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x, dst_y,
610 p_btnPlay->width, p_btnPlay->height );
613 dst_x += BTN_SPACE + ( p_btnPlay ? p_btnPlay->width : 0 );
617 XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x, dst_y,
618 p_btnStop->width, p_btnStop->height );
620 dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
623 if( p_btnFullscreen )
624 XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x, dst_y,
625 p_btnFullscreen->width, p_btnFullscreen->height );
627 dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
630 if( p_btnUnmute && b_mute )
632 XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x, dst_y,
633 p_btnUnmute->width, p_btnUnmute->height );
635 dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
640 XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x, dst_y,
641 p_btnMute->width, p_btnMute->height );
643 dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
648 XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x, dst_y,
649 (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
652 i_last_position = (((float)window.width-8.0)/100.0)*f_position;
655 XPutImage( p_display, control, gc, p_btnTime,
656 0, 0, (dst_x+i_last_position), dst_y,
657 p_btnTime->width, p_btnTime->height );
659 XFreeGC( p_display, gc );