1 /*****************************************************************************
2 * vlcplugin.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Damien Fouilleul <damienf.fouilleul@laposte.net>
9 * Jean-Paul Saman <jpsaman@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #ifdef HAVE_MOZILLA_CONFIG_H
32 # include <mozilla-config.h>
35 #include "vlcplugin.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)
68 ,p_btnFullscreen(NULL)
73 memset(&npwindow, 0, sizeof(NPWindow));
76 static bool boolValue(const char *value) {
77 return ( !strcmp(value, "1") ||
78 !strcasecmp(value, "true") ||
79 !strcasecmp(value, "yes") );
82 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
84 /* prepare VLC command line */
85 const char *ppsz_argv[32];
89 ppsz_argv[ppsz_argc++] = "--no-plugins-cache";
92 /* locate VLC module path */
94 ppsz_argv[ppsz_argc++] = "--plugin-path=/Library/Internet\\ Plug-Ins/VLC\\ Plugin.plugin/Contents/MacOS/modules";
95 ppsz_argv[ppsz_argc++] = "--vout=macosx";
98 DWORD i_type, i_data = MAX_PATH + 1;
99 char p_data[MAX_PATH + 1];
100 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
101 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
103 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
104 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
106 if( i_type == REG_SZ )
108 strcat( p_data, "\\plugins" );
109 ppsz_argv[ppsz_argc++] = "--plugin-path";
110 ppsz_argv[ppsz_argc++] = p_data;
113 RegCloseKey( h_key );
115 ppsz_argv[ppsz_argc++] = "--no-one-instance";
117 #endif /* XP_MACOSX */
119 /* common settings */
120 ppsz_argv[ppsz_argc++] = "-vv";
121 ppsz_argv[ppsz_argc++] = "--no-stats";
122 ppsz_argv[ppsz_argc++] = "--no-media-library";
123 ppsz_argv[ppsz_argc++] = "--ignore-config";
124 ppsz_argv[ppsz_argc++] = "--intf=dummy";
125 const char *progid = NULL;
127 /* parse plugin arguments */
128 for( int i = 0; i < argc ; i++ )
130 fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
132 if( !strcmp( argn[i], "target" )
133 || !strcmp( argn[i], "mrl")
134 || !strcmp( argn[i], "filename")
135 || !strcmp( argn[i], "src") )
137 psz_target = argv[i];
139 else if( !strcmp( argn[i], "autoplay")
140 || !strcmp( argn[i], "autostart") )
142 b_autoplay = boolValue(argv[i]);
144 else if( !strcmp( argn[i], "fullscreen" ) )
146 if( boolValue(argv[i]) )
148 ppsz_argv[ppsz_argc++] = "--fullscreen";
152 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
155 else if( !strcmp( argn[i], "mute" ) )
157 if( boolValue(argv[i]) )
159 ppsz_argv[ppsz_argc++] = "--volume=0";
162 else if( !strcmp( argn[i], "loop")
163 || !strcmp( argn[i], "autoloop") )
165 if( boolValue(argv[i]) )
167 ppsz_argv[ppsz_argc++] = "--loop";
171 ppsz_argv[ppsz_argc++] = "--no-loop";
174 else if( !strcmp( argn[i], "version")
175 || !strcmp( argn[i], "progid") )
179 else if( !strcmp( argn[i], "toolbar" ) )
181 /* FIXME: Remove this when toolbar functionality has been implemented on\
182 * MacOS X and Win32 for Firefox/Mozilla/Safari. */
184 b_toolbar = boolValue(argv[i]);
189 libvlc_exception_t ex;
190 libvlc_exception_init(&ex);
192 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
193 if( libvlc_exception_raised(&ex) )
195 libvlc_exception_clear(&ex);
196 return NPERR_GENERIC_ERROR;
198 libvlc_exception_clear(&ex);
201 ** fetch plugin base URL, which is the URL of the page containing the plugin
202 ** this URL is used for making absolute URL from relative URL that may be
203 ** passed as an MRL argument
207 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
210 ** is there a better way to get that info ?
212 static const char docLocHref[] = "document.location.href";
216 script.utf8characters = docLocHref;
217 script.utf8length = sizeof(docLocHref)-1;
219 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
221 if( NPVARIANT_IS_STRING(result) )
223 NPString &location = NPVARIANT_TO_STRING(result);
225 psz_baseURL = static_cast<char*>(malloc(location.utf8length+1));
228 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
229 psz_baseURL[location.utf8length] = '\0';
232 NPN_ReleaseVariantValue(&result);
234 NPN_ReleaseObject(plugin);
239 // get absolute URL from src
240 char *psz_absurl = getAbsoluteURL(psz_target);
241 psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
244 /* assign plugin script root class */
246 p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
248 return NPERR_NO_ERROR;
251 VlcPlugin::~VlcPlugin()
256 libvlc_log_close(libvlc_log, NULL);
257 if( libvlc_instance )
258 libvlc_release(libvlc_instance);
261 /*****************************************************************************
263 *****************************************************************************/
265 char *VlcPlugin::getAbsoluteURL(const char *url)
269 // check whether URL is already absolute
270 const char *end=strchr(url, ':');
271 if( (NULL != end) && (end != url) )
273 // validate protocol header
274 const char *start = url;
279 while( start != end )
286 || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
287 // not valid protocol header, assume relative URL
291 /* we have a protocol header, therefore URL is absolute */
294 // not a valid protocol header, assume relative URL
301 size_t baseLen = strlen(psz_baseURL);
302 char *href = static_cast<char*>(malloc(baseLen+strlen(url)+1));
305 /* prepend base URL */
306 strcpy(href, psz_baseURL);
309 ** relative url could be empty,
310 ** in which case return base URL
316 ** locate pathname part of base URL
319 /* skip over protocol part */
320 char *pathstart = strchr(href, ':');
324 if( '/' == *(++pathstart) )
326 if( '/' == *(++pathstart) )
331 /* skip over host part */
332 pathstart = strchr(pathstart, '/');
333 pathend = href+baseLen;
336 // no path, add a / past end of url (over '\0')
343 /* baseURL is just a UNIX path */
346 /* baseURL is not an absolute path */
351 pathend = href+baseLen;
354 /* relative URL made of an absolute path ? */
357 /* replace path completely */
358 strcpy(pathstart, url);
362 /* find last path component and replace it */
363 while( '/' != *pathend)
367 ** if relative url path starts with one or more '../',
368 ** factor them out of href so that we return a
371 while( pathend != pathstart )
379 /* relative url is just '.' */
385 /* relative url starts with './' */
394 /* relative url is '..' */
400 /* relative url starts with '../' */
408 while( '/' != *pathend );
410 /* skip over '/' separator */
412 /* concatenate remaining base URL and relative URL */
413 strcpy(pathend, url);
422 int VlcPlugin::setSize(unsigned width, unsigned height)
424 int diff = (width != i_width) || (height != i_height);
433 #define BTN_SPACE ((unsigned int)4)
434 void VlcPlugin::showToolbar()
436 const NPWindow& window = getWindow();
437 Window control = getControlWindow();
438 Window video = getVideoWindow();
439 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
440 unsigned int i_height = 0, i_width = BTN_SPACE;
444 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
445 &p_btnPlay, NULL, NULL);
448 i_height = __MAX( i_height, p_btnPlay->height );
451 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
452 &p_btnPause, NULL, NULL);
455 i_height = __MAX( i_height, p_btnPause->height );
457 i_width += __MAX( p_btnPause->width, p_btnPlay->width );
460 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
461 &p_btnStop, NULL, NULL );
464 i_height = __MAX( i_height, p_btnStop->height );
465 i_width += BTN_SPACE + p_btnStop->width;
468 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
469 &p_timeline, NULL, NULL);
472 i_height = __MAX( i_height, p_timeline->height );
473 i_width += BTN_SPACE + p_timeline->width;
476 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
477 &p_btnTime, NULL, NULL);
480 i_height = __MAX( i_height, p_btnTime->height );
481 i_width += BTN_SPACE + p_btnTime->width;
483 if( !p_btnFullscreen )
484 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
485 &p_btnFullscreen, NULL, NULL);
486 if( p_btnFullscreen )
488 i_height = __MAX( i_height, p_btnFullscreen->height );
489 i_width += BTN_SPACE + p_btnFullscreen->width;
492 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
493 &p_btnMute, NULL, NULL);
496 i_height = __MAX( i_height, p_btnMute->height );
499 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
500 &p_btnUnmute, NULL, NULL);
503 i_height = __MAX( i_height, p_btnUnmute->height );
505 i_width += BTN_SPACE + __MAX( p_btnUnmute->width, p_btnMute->width );
507 setToolbarSize( i_width, i_height );
509 if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
510 !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
511 fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
513 /* reset panels position and size */
514 /* XXX use i_width */
515 XResizeWindow( p_display, video, window.width, window.height - i_height);
516 XMoveWindow( p_display, control, 0, window.height - i_height );
517 XResizeWindow( p_display, control, window.width, i_height -1);
519 b_toolbar = 1; /* says toolbar is now shown */
523 void VlcPlugin::hideToolbar()
525 const NPWindow& window = getWindow();
526 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
527 Window control = getControlWindow();
528 Window video = getVideoWindow();
530 i_tb_width = i_tb_height = 0;
532 if( p_btnPlay ) XDestroyImage( p_btnPlay );
533 if( p_btnPause ) XDestroyImage( p_btnPause );
534 if( p_btnStop ) XDestroyImage( p_btnStop );
535 if( p_timeline ) XDestroyImage( p_timeline );
536 if( p_btnTime ) XDestroyImage( p_btnTime );
537 if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
538 if( p_btnMute ) XDestroyImage( p_btnMute );
539 if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
546 p_btnFullscreen = NULL;
550 /* reset panels position and size */
551 /* XXX use i_width */
552 XResizeWindow( p_display, video, window.width, window.height );
553 XMoveWindow( p_display, control, 0, window.height-1 );
554 XResizeWindow( p_display, control, window.width, 1 );
556 b_toolbar = 0; /* says toolbar is now hidden */
560 void VlcPlugin::redrawToolbar()
562 libvlc_media_player_t *p_md = NULL;
563 libvlc_exception_t ex;
564 float f_position = 0.0;
567 unsigned int dst_x, dst_y;
570 unsigned int i_tb_width, i_tb_height;
572 /* This method does nothing if toolbar is hidden. */
576 const NPWindow& window = getWindow();
577 Window control = getControlWindow();
578 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
580 getToolbarSize( &i_tb_width, &i_tb_height );
582 /* get media instance */
583 libvlc_exception_init( &ex );
584 p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
585 libvlc_exception_clear( &ex );
589 libvlc_playlist_lock( getVLC() );
590 is_playing = libvlc_playlist_isplaying( getVLC(), &ex );
591 libvlc_playlist_unlock( getVLC() );
592 libvlc_exception_clear( &ex );
594 /* get movie position in % */
595 if( is_playing == 1 )
597 f_position = libvlc_media_player_get_position( p_md, &ex ) * 100.0;
598 libvlc_exception_clear( &ex );
600 libvlc_media_player_release( p_md );
604 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
605 libvlc_exception_clear( &ex );
607 gcv.foreground = BlackPixel( p_display, 0 );
608 gc = XCreateGC( p_display, control, GCForeground, &gcv );
610 XFillRectangle( p_display, control, gc,
611 0, 0, window.width, i_tb_height );
612 gcv.foreground = WhitePixel( p_display, 0 );
613 XChangeGC( p_display, gc, GCForeground, &gcv );
617 dst_y = i_tb_height >> 1; /* baseline = vertical middle */
619 if( p_btnPause && (is_playing == 1) )
621 XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
622 dst_y - (p_btnPause->height >> 1),
623 p_btnPause->width, p_btnPause->height );
624 dst_x += BTN_SPACE + p_btnPause->width;
628 XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
629 dst_y - (p_btnPlay->height >> 1),
630 p_btnPlay->width, p_btnPlay->height );
631 dst_x += BTN_SPACE + p_btnPlay->width;
635 XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
636 dst_y - (p_btnStop->height >> 1),
637 p_btnStop->width, p_btnStop->height );
639 dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
641 if( p_btnFullscreen )
642 XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
643 dst_y - (p_btnFullscreen->height >> 1),
644 p_btnFullscreen->width, p_btnFullscreen->height );
646 dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
648 if( p_btnUnmute && b_mute )
650 XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
651 dst_y - (p_btnUnmute->height >> 1),
652 p_btnUnmute->width, p_btnUnmute->height );
654 dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
658 XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
659 dst_y - (p_btnMute->height >> 1),
660 p_btnMute->width, p_btnMute->height );
662 dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
666 XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x,
667 dst_y - (p_timeline->height >> 1),
668 (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
671 i_last_position = (int)( f_position *
672 ( ((float)(window.width-(dst_x+BTN_SPACE))) / 100.0 ));
675 XPutImage( p_display, control, gc, p_btnTime,
676 0, 0, (dst_x+i_last_position),
677 dst_y - (p_btnTime->height >> 1),
678 p_btnTime->width, p_btnTime->height );
680 XFreeGC( p_display, gc );
683 vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos )
685 unsigned int i_dest = BTN_SPACE;//(i_tb_height >> 1);
688 libvlc_exception_t ex;
690 fprintf( stderr, "ToolbarButtonClicked:: "
691 "trying to match (%d,%d) (%d,%d)\n",
692 i_xpos, i_ypos, i_tb_height, i_tb_width );
694 if( i_ypos >= i_tb_width )
695 return clicked_Unknown;
697 /* Note: the order of testing is dependend on the original
698 * drawing positions of the icon buttons. Buttons are tested
703 libvlc_exception_init( &ex );
704 libvlc_playlist_lock( getVLC() );
705 is_playing = libvlc_playlist_isplaying( getVLC(), &ex );
706 libvlc_playlist_unlock( getVLC() );
707 libvlc_exception_clear( &ex );
710 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
711 libvlc_exception_clear( &ex );
713 /* is Pause of Play button clicked */
714 if( (is_playing != 1) &&
715 (i_xpos >= (BTN_SPACE>>1)) &&
716 (i_xpos <= i_dest + p_btnPlay->width + (BTN_SPACE>>1)) )
718 else if( (i_xpos >= (BTN_SPACE>>1)) &&
719 (i_xpos <= i_dest + p_btnPause->width) )
720 return clicked_Pause;
722 /* is Stop button clicked */
723 if( is_playing != 1 )
724 i_dest += (p_btnPlay->width + (BTN_SPACE>>1));
726 i_dest += (p_btnPause->width + (BTN_SPACE>>1));
728 if( (i_xpos >= i_dest) &&
729 (i_xpos <= i_dest + p_btnStop->width + (BTN_SPACE>>1)) )
732 /* is Fullscreen button clicked */
733 i_dest += (p_btnStop->width + (BTN_SPACE>>1));
734 if( (i_xpos >= i_dest) &&
735 (i_xpos <= i_dest + p_btnFullscreen->width + (BTN_SPACE>>1)) )
736 return clicked_Fullscreen;
738 /* is Mute or Unmute button clicked */
739 i_dest += (p_btnFullscreen->width + (BTN_SPACE>>1));
740 if( !b_mute && (i_xpos >= i_dest) &&
741 (i_xpos <= i_dest + p_btnMute->width + (BTN_SPACE>>1)) )
743 else if( (i_xpos >= i_dest) &&
744 (i_xpos <= i_dest + p_btnUnmute->width + (BTN_SPACE>>1)) )
745 return clicked_Unmute;
747 /* is timeline clicked */
749 i_dest += (p_btnMute->width + (BTN_SPACE>>1));
751 i_dest += (p_btnUnmute->width + (BTN_SPACE>>1));
752 if( (i_xpos >= i_dest) &&
753 (i_xpos <= i_dest + p_timeline->width + (BTN_SPACE>>1)) )
754 return clicked_timeline;
756 /* is time button clicked */
757 i_dest += (p_timeline->width + (BTN_SPACE>>1));
758 if( (i_xpos >= i_dest) &&
759 (i_xpos <= i_dest + p_btnTime->width + (BTN_SPACE>>1)) )
762 return clicked_Unknown;