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];
88 /* locate VLC module path */
90 ppsz_argv[ppsz_argc++] = "--plugin-path=/Library/Internet\\ Plug-Ins/VLC\\ Plugin.plugin/Contents/MacOS/modules";
91 ppsz_argv[ppsz_argc++] = "--vout=macosx";
94 DWORD i_type, i_data = MAX_PATH + 1;
95 char p_data[MAX_PATH + 1];
96 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
97 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
99 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
100 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
102 if( i_type == REG_SZ )
104 strcat( p_data, "\\plugins" );
105 ppsz_argv[ppsz_argc++] = "--plugin-path";
106 ppsz_argv[ppsz_argc++] = p_data;
109 RegCloseKey( h_key );
111 ppsz_argv[ppsz_argc++] = "--no-one-instance";
113 #endif /* XP_MACOSX */
115 /* common settings */
116 ppsz_argv[ppsz_argc++] = "-vv";
117 ppsz_argv[ppsz_argc++] = "--no-stats";
118 ppsz_argv[ppsz_argc++] = "--no-media-library";
119 ppsz_argv[ppsz_argc++] = "--ignore-config";
120 ppsz_argv[ppsz_argc++] = "--intf=dummy";
121 const char *progid = NULL;
123 /* parse plugin arguments */
124 for( int i = 0; i < argc ; i++ )
126 fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
128 if( !strcmp( argn[i], "target" )
129 || !strcmp( argn[i], "mrl")
130 || !strcmp( argn[i], "filename")
131 || !strcmp( argn[i], "src") )
133 psz_target = argv[i];
135 else if( !strcmp( argn[i], "autoplay")
136 || !strcmp( argn[i], "autostart") )
138 b_autoplay = boolValue(argv[i]);
140 else if( !strcmp( argn[i], "fullscreen" ) )
142 if( boolValue(argv[i]) )
144 ppsz_argv[ppsz_argc++] = "--fullscreen";
148 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
151 else if( !strcmp( argn[i], "mute" ) )
153 if( boolValue(argv[i]) )
155 ppsz_argv[ppsz_argc++] = "--volume";
156 ppsz_argv[ppsz_argc++] = "0";
159 else if( !strcmp( argn[i], "loop")
160 || !strcmp( argn[i], "autoloop") )
162 if( boolValue(argv[i]) )
164 ppsz_argv[ppsz_argc++] = "--loop";
168 ppsz_argv[ppsz_argc++] = "--no-loop";
171 else if( !strcmp( argn[i], "version")
172 || !strcmp( argn[i], "progid") )
176 else if( !strcmp( argn[i], "toolbar" ) )
178 /* FIXME: Remove this when toolbar functionality has been implemented on\
179 * MacOS X and Win32 for Firefox/Mozilla/Safari. */
181 b_toolbar = boolValue(argv[i]);
186 libvlc_exception_t ex;
187 libvlc_exception_init(&ex);
189 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
190 if( libvlc_exception_raised(&ex) )
192 libvlc_exception_clear(&ex);
193 return NPERR_GENERIC_ERROR;
195 libvlc_exception_clear(&ex);
198 ** fetch plugin base URL, which is the URL of the page containing the plugin
199 ** this URL is used for making absolute URL from relative URL that may be
200 ** passed as an MRL argument
204 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
207 ** is there a better way to get that info ?
209 static const char docLocHref[] = "document.location.href";
213 script.utf8characters = docLocHref;
214 script.utf8length = sizeof(docLocHref)-1;
216 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
218 if( NPVARIANT_IS_STRING(result) )
220 NPString &location = NPVARIANT_TO_STRING(result);
222 psz_baseURL = static_cast<char*>(malloc(location.utf8length+1));
225 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
226 psz_baseURL[location.utf8length] = '\0';
229 NPN_ReleaseVariantValue(&result);
231 NPN_ReleaseObject(plugin);
236 // get absolute URL from src
237 char *psz_absurl = getAbsoluteURL(psz_target);
238 psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
241 /* assign plugin script root class */
243 p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
245 return NPERR_NO_ERROR;
248 VlcPlugin::~VlcPlugin()
253 libvlc_log_close(libvlc_log, NULL);
254 if( libvlc_instance )
255 libvlc_release(libvlc_instance);
258 /*****************************************************************************
260 *****************************************************************************/
262 char *VlcPlugin::getAbsoluteURL(const char *url)
266 // check whether URL is already absolute
267 const char *end=strchr(url, ':');
268 if( (NULL != end) && (end != url) )
270 // validate protocol header
271 const char *start = url;
276 while( start != end )
283 || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
284 // not valid protocol header, assume relative URL
288 /* we have a protocol header, therefore URL is absolute */
291 // not a valid protocol header, assume relative URL
298 size_t baseLen = strlen(psz_baseURL);
299 char *href = static_cast<char*>(malloc(baseLen+strlen(url)+1));
302 /* prepend base URL */
303 strcpy(href, psz_baseURL);
306 ** relative url could be empty,
307 ** in which case return base URL
313 ** locate pathname part of base URL
316 /* skip over protocol part */
317 char *pathstart = strchr(href, ':');
321 if( '/' == *(++pathstart) )
323 if( '/' == *(++pathstart) )
328 /* skip over host part */
329 pathstart = strchr(pathstart, '/');
330 pathend = href+baseLen;
333 // no path, add a / past end of url (over '\0')
340 /* baseURL is just a UNIX path */
343 /* baseURL is not an absolute path */
348 pathend = href+baseLen;
351 /* relative URL made of an absolute path ? */
354 /* replace path completely */
355 strcpy(pathstart, url);
359 /* find last path component and replace it */
360 while( '/' != *pathend)
364 ** if relative url path starts with one or more '../',
365 ** factor them out of href so that we return a
368 while( pathend != pathstart )
376 /* relative url is just '.' */
382 /* relative url starts with './' */
391 /* relative url is '..' */
397 /* relative url starts with '../' */
405 while( '/' != *pathend );
407 /* skip over '/' separator */
409 /* concatenate remaining base URL and relative URL */
410 strcpy(pathend, url);
419 int VlcPlugin::setSize(unsigned width, unsigned height)
421 int diff = (width != i_width) || (height != i_height);
430 #define BTN_SPACE ((unsigned int)4)
431 void VlcPlugin::showToolbar()
433 const NPWindow& window = getWindow();
434 Window control = getControlWindow();
435 Window video = getVideoWindow();
436 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
437 unsigned int i_height = 0, i_width = BTN_SPACE;
441 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
442 &p_btnPlay, NULL, NULL);
445 i_height = __MAX( i_height, p_btnPlay->height );
448 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
449 &p_btnPause, NULL, NULL);
452 i_height = __MAX( i_height, p_btnPause->height );
454 i_width += __MAX( p_btnPause->width, p_btnPlay->width );
457 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
458 &p_btnStop, NULL, NULL );
461 i_height = __MAX( i_height, p_btnStop->height );
462 i_width += BTN_SPACE + p_btnStop->width;
465 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
466 &p_timeline, NULL, NULL);
469 i_height = __MAX( i_height, p_timeline->height );
470 i_width += BTN_SPACE + p_timeline->width;
473 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
474 &p_btnTime, NULL, NULL);
477 i_height = __MAX( i_height, p_btnTime->height );
478 i_width += BTN_SPACE + p_btnTime->width;
480 if( !p_btnFullscreen )
481 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
482 &p_btnFullscreen, NULL, NULL);
483 if( p_btnFullscreen )
485 i_height = __MAX( i_height, p_btnFullscreen->height );
486 i_width += BTN_SPACE + p_btnFullscreen->width;
489 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
490 &p_btnMute, NULL, NULL);
493 i_height = __MAX( i_height, p_btnMute->height );
496 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
497 &p_btnUnmute, NULL, NULL);
500 i_height = __MAX( i_height, p_btnUnmute->height );
502 i_width += BTN_SPACE + __MAX( p_btnUnmute->width, p_btnMute->width );
504 setToolbarSize( i_width, i_height );
506 if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
507 !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
508 fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
510 /* reset panels position and size */
511 /* XXX use i_width */
512 XResizeWindow( p_display, video, window.width, window.height - i_height);
513 XMoveWindow( p_display, control, 0, window.height - i_height );
514 XResizeWindow( p_display, control, window.width, i_height -1);
516 b_toolbar = 1; /* says toolbar is now shown */
520 void VlcPlugin::hideToolbar()
522 const NPWindow& window = getWindow();
523 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
524 Window control = getControlWindow();
525 Window video = getVideoWindow();
527 i_tb_width = i_tb_height = 0;
529 if( p_btnPlay ) XDestroyImage( p_btnPlay );
530 if( p_btnPause ) XDestroyImage( p_btnPause );
531 if( p_btnStop ) XDestroyImage( p_btnStop );
532 if( p_timeline ) XDestroyImage( p_timeline );
533 if( p_btnTime ) XDestroyImage( p_btnTime );
534 if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
535 if( p_btnMute ) XDestroyImage( p_btnMute );
536 if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
543 p_btnFullscreen = NULL;
547 /* reset panels position and size */
548 /* XXX use i_width */
549 XResizeWindow( p_display, video, window.width, window.height );
550 XMoveWindow( p_display, control, 0, window.height-1 );
551 XResizeWindow( p_display, control, window.width, 1 );
553 b_toolbar = 0; /* says toolbar is now hidden */
557 void VlcPlugin::redrawToolbar()
559 libvlc_media_player_t *p_md = NULL;
560 libvlc_exception_t ex;
561 float f_position = 0.0;
564 unsigned int dst_x, dst_y;
567 unsigned int i_tb_width, i_tb_height;
569 /* This method does nothing if toolbar is hidden. */
573 const NPWindow& window = getWindow();
574 Window control = getControlWindow();
575 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
577 getToolbarSize( &i_tb_width, &i_tb_height );
579 /* get media instance */
580 libvlc_exception_init( &ex );
581 p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
582 libvlc_exception_clear( &ex );
586 libvlc_playlist_lock( getVLC() );
587 is_playing = libvlc_playlist_isplaying( getVLC(), &ex );
588 libvlc_playlist_unlock( getVLC() );
589 libvlc_exception_clear( &ex );
591 /* get movie position in % */
592 if( is_playing == 1 )
594 f_position = libvlc_media_player_get_position( p_md, &ex ) * 100.0;
595 libvlc_exception_clear( &ex );
597 libvlc_media_player_release( p_md );
601 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
602 libvlc_exception_clear( &ex );
604 gcv.foreground = BlackPixel( p_display, 0 );
605 gc = XCreateGC( p_display, control, GCForeground, &gcv );
607 XFillRectangle( p_display, control, gc,
608 0, 0, window.width, i_tb_height );
609 gcv.foreground = WhitePixel( p_display, 0 );
610 XChangeGC( p_display, gc, GCForeground, &gcv );
614 dst_y = i_tb_height >> 1; /* baseline = vertical middle */
616 if( p_btnPause && (is_playing == 1) )
618 XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
619 dst_y - (p_btnPause->height >> 1),
620 p_btnPause->width, p_btnPause->height );
621 dst_x += BTN_SPACE + p_btnPause->width;
625 XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
626 dst_y - (p_btnPlay->height >> 1),
627 p_btnPlay->width, p_btnPlay->height );
628 dst_x += BTN_SPACE + p_btnPlay->width;
632 XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
633 dst_y - (p_btnStop->height >> 1),
634 p_btnStop->width, p_btnStop->height );
636 dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
638 if( p_btnFullscreen )
639 XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
640 dst_y - (p_btnFullscreen->height >> 1),
641 p_btnFullscreen->width, p_btnFullscreen->height );
643 dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
645 if( p_btnUnmute && b_mute )
647 XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
648 dst_y - (p_btnUnmute->height >> 1),
649 p_btnUnmute->width, p_btnUnmute->height );
651 dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
655 XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
656 dst_y - (p_btnMute->height >> 1),
657 p_btnMute->width, p_btnMute->height );
659 dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
663 XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x,
664 dst_y - (p_timeline->height >> 1),
665 (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
668 i_last_position = (int)( f_position *
669 ( ((float)(window.width-(dst_x+BTN_SPACE))) / 100.0 ));
672 XPutImage( p_display, control, gc, p_btnTime,
673 0, 0, (dst_x+i_last_position),
674 dst_y - (p_btnTime->height >> 1),
675 p_btnTime->width, p_btnTime->height );
677 XFreeGC( p_display, gc );
680 vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos )
682 unsigned int i_dest = BTN_SPACE;//(i_tb_height >> 1);
685 libvlc_exception_t ex;
687 fprintf( stderr, "ToolbarButtonClicked:: "
688 "trying to match (%d,%d) (%d,%d)\n",
689 i_xpos, i_ypos, i_tb_height, i_tb_width );
691 if( i_ypos >= i_tb_width )
692 return clicked_Unknown;
694 /* Note: the order of testing is dependend on the original
695 * drawing positions of the icon buttons. Buttons are tested
700 libvlc_exception_init( &ex );
701 libvlc_playlist_lock( getVLC() );
702 is_playing = libvlc_playlist_isplaying( getVLC(), &ex );
703 libvlc_playlist_unlock( getVLC() );
704 libvlc_exception_clear( &ex );
707 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
708 libvlc_exception_clear( &ex );
710 /* is Pause of Play button clicked */
711 if( (is_playing != 1) &&
712 (i_xpos >= (BTN_SPACE>>1)) &&
713 (i_xpos <= i_dest + p_btnPlay->width + (BTN_SPACE>>1)) )
715 else if( (i_xpos >= (BTN_SPACE>>1)) &&
716 (i_xpos <= i_dest + p_btnPause->width) )
717 return clicked_Pause;
719 /* is Stop button clicked */
720 if( is_playing != 1 )
721 i_dest += (p_btnPlay->width + (BTN_SPACE>>1));
723 i_dest += (p_btnPause->width + (BTN_SPACE>>1));
725 if( (i_xpos >= i_dest) &&
726 (i_xpos <= i_dest + p_btnStop->width + (BTN_SPACE>>1)) )
729 /* is Fullscreen button clicked */
730 i_dest += (p_btnStop->width + (BTN_SPACE>>1));
731 if( (i_xpos >= i_dest) &&
732 (i_xpos <= i_dest + p_btnFullscreen->width + (BTN_SPACE>>1)) )
733 return clicked_Fullscreen;
735 /* is Mute or Unmute button clicked */
736 i_dest += (p_btnFullscreen->width + (BTN_SPACE>>1));
737 if( !b_mute && (i_xpos >= i_dest) &&
738 (i_xpos <= i_dest + p_btnMute->width + (BTN_SPACE>>1)) )
740 else if( (i_xpos >= i_dest) &&
741 (i_xpos <= i_dest + p_btnUnmute->width + (BTN_SPACE>>1)) )
742 return clicked_Unmute;
744 /* is timeline clicked */
746 i_dest += (p_btnMute->width + (BTN_SPACE>>1));
748 i_dest += (p_btnUnmute->width + (BTN_SPACE>>1));
749 if( (i_xpos >= i_dest) &&
750 (i_xpos <= i_dest + p_timeline->width + (BTN_SPACE>>1)) )
751 return clicked_timeline;
753 /* is time button clicked */
754 i_dest += (p_timeline->width + (BTN_SPACE>>1));
755 if( (i_xpos >= i_dest) &&
756 (i_xpos <= i_dest + p_btnTime->width + (BTN_SPACE>>1)) )
759 return clicked_Unknown;