1 /*****************************************************************************
2 * vlcplugin.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002-2008 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";
122 const char *progid = NULL;
124 /* parse plugin arguments */
125 for( int i = 0; i < argc ; i++ )
127 fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
129 if( !strcmp( argn[i], "target" )
130 || !strcmp( argn[i], "mrl")
131 || !strcmp( argn[i], "filename")
132 || !strcmp( argn[i], "src") )
134 psz_target = argv[i];
136 else if( !strcmp( argn[i], "autoplay")
137 || !strcmp( argn[i], "autostart") )
139 b_autoplay = boolValue(argv[i]);
141 else if( !strcmp( argn[i], "fullscreen" ) )
143 if( boolValue(argv[i]) )
145 ppsz_argv[ppsz_argc++] = "--fullscreen";
149 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
152 else if( !strcmp( argn[i], "mute" ) )
154 if( boolValue(argv[i]) )
156 ppsz_argv[ppsz_argc++] = "--volume";
157 ppsz_argv[ppsz_argc++] = "0";
160 else if( !strcmp( argn[i], "loop")
161 || !strcmp( argn[i], "autoloop") )
163 if( boolValue(argv[i]) )
165 ppsz_argv[ppsz_argc++] = "--loop";
169 ppsz_argv[ppsz_argc++] = "--no-loop";
172 else if( !strcmp( argn[i], "version")
173 || !strcmp( argn[i], "progid") )
177 else if( !strcmp( argn[i], "toolbar" ) )
179 b_toolbar = boolValue(argv[i]);
183 libvlc_exception_t ex;
184 libvlc_exception_init(&ex);
186 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
187 if( libvlc_exception_raised(&ex) )
189 libvlc_exception_clear(&ex);
190 return NPERR_GENERIC_ERROR;
192 libvlc_exception_clear(&ex);
195 ** fetch plugin base URL, which is the URL of the page containing the plugin
196 ** this URL is used for making absolute URL from relative URL that may be
197 ** passed as an MRL argument
201 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
204 ** is there a better way to get that info ?
206 static const char docLocHref[] = "document.location.href";
210 script.utf8characters = docLocHref;
211 script.utf8length = sizeof(docLocHref)-1;
213 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
215 if( NPVARIANT_IS_STRING(result) )
217 NPString &location = NPVARIANT_TO_STRING(result);
219 psz_baseURL = static_cast<char*>(malloc(location.utf8length+1));
222 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
223 psz_baseURL[location.utf8length] = '\0';
226 NPN_ReleaseVariantValue(&result);
228 NPN_ReleaseObject(plugin);
233 // get absolute URL from src
234 char *psz_absurl = getAbsoluteURL(psz_target);
235 psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
238 /* assign plugin script root class */
240 p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
242 return NPERR_NO_ERROR;
245 VlcPlugin::~VlcPlugin()
250 libvlc_log_close(libvlc_log, NULL);
251 if( libvlc_instance )
252 libvlc_release(libvlc_instance);
255 /*****************************************************************************
257 *****************************************************************************/
259 char *VlcPlugin::getAbsoluteURL(const char *url)
263 // check whether URL is already absolute
264 const char *end=strchr(url, ':');
265 if( (NULL != end) && (end != url) )
267 // validate protocol header
268 const char *start = url;
273 while( start != end )
280 || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
281 // not valid protocol header, assume relative URL
285 /* we have a protocol header, therefore URL is absolute */
288 // not a valid protocol header, assume relative URL
295 size_t baseLen = strlen(psz_baseURL);
296 char *href = static_cast<char*>(malloc(baseLen+strlen(url)+1));
299 /* prepend base URL */
300 strcpy(href, psz_baseURL);
303 ** relative url could be empty,
304 ** in which case return base URL
310 ** locate pathname part of base URL
313 /* skip over protocol part */
314 char *pathstart = strchr(href, ':');
318 if( '/' == *(++pathstart) )
320 if( '/' == *(++pathstart) )
325 /* skip over host part */
326 pathstart = strchr(pathstart, '/');
327 pathend = href+baseLen;
330 // no path, add a / past end of url (over '\0')
337 /* baseURL is just a UNIX path */
340 /* baseURL is not an absolute path */
345 pathend = href+baseLen;
348 /* relative URL made of an absolute path ? */
351 /* replace path completely */
352 strcpy(pathstart, url);
356 /* find last path component and replace it */
357 while( '/' != *pathend)
361 ** if relative url path starts with one or more '../',
362 ** factor them out of href so that we return a
365 while( pathend != pathstart )
373 /* relative url is just '.' */
379 /* relative url starts with './' */
388 /* relative url is '..' */
394 /* relative url starts with '../' */
402 while( '/' != *pathend );
404 /* skip over '/' separator */
406 /* concatenate remaining base URL and relative URL */
407 strcpy(pathend, url);
416 int VlcPlugin::setSize(unsigned width, unsigned height)
418 int diff = (width != i_width) || (height != i_height);
427 #define BTN_SPACE ((unsigned int)4)
428 void VlcPlugin::showToolbar()
430 const NPWindow& window = getWindow();
431 Window control = getControlWindow();
432 Window video = getVideoWindow();
433 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
434 unsigned int i_height = 0, i_width = BTN_SPACE;
438 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
439 &p_btnPlay, NULL, NULL);
442 i_height = __MAX( i_height, p_btnPlay->height );
445 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
446 &p_btnPause, NULL, NULL);
449 i_height = __MAX( i_height, p_btnPause->height );
451 i_width += __MAX( p_btnPause->width, p_btnPlay->width );
454 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
455 &p_btnStop, NULL, NULL );
458 i_height = __MAX( i_height, p_btnStop->height );
459 i_width += BTN_SPACE + p_btnStop->width;
462 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
463 &p_timeline, NULL, NULL);
466 i_height = __MAX( i_height, p_timeline->height );
467 i_width += BTN_SPACE + p_timeline->width;
470 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
471 &p_btnTime, NULL, NULL);
474 i_height = __MAX( i_height, p_btnTime->height );
475 i_width += BTN_SPACE + p_btnTime->width;
477 if( !p_btnFullscreen )
478 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
479 &p_btnFullscreen, NULL, NULL);
480 if( p_btnFullscreen )
482 i_height = __MAX( i_height, p_btnFullscreen->height );
483 i_width += BTN_SPACE + p_btnFullscreen->width;
486 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
487 &p_btnMute, NULL, NULL);
490 i_height = __MAX( i_height, p_btnMute->height );
493 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
494 &p_btnUnmute, NULL, NULL);
497 i_height = __MAX( i_height, p_btnUnmute->height );
499 i_width += BTN_SPACE + __MAX( p_btnUnmute->width, p_btnMute->width );
501 setToolbarSize( i_width, i_height );
503 if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
504 !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
505 fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
507 /* reset panels position and size */
508 /* XXX use i_width */
509 XResizeWindow( p_display, video, window.width, window.height - i_height);
510 XMoveWindow( p_display, control, 0, window.height - i_height );
511 XResizeWindow( p_display, control, window.width, i_height -1);
513 b_toolbar = 1; /* says toolbar is now shown */
517 void VlcPlugin::hideToolbar()
519 const NPWindow& window = getWindow();
520 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
521 Window control = getControlWindow();
522 Window video = getVideoWindow();
524 i_tb_width = i_tb_height = 0;
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 );
540 p_btnFullscreen = NULL;
544 /* reset panels position and size */
545 /* XXX use i_width */
546 XResizeWindow( p_display, video, window.width, window.height );
547 XMoveWindow( p_display, control, 0, window.height-1 );
548 XResizeWindow( p_display, control, window.width, 1 );
550 b_toolbar = 0; /* says toolbar is now hidden */
554 void VlcPlugin::redrawToolbar()
556 libvlc_media_player_t *p_md = NULL;
557 libvlc_exception_t ex;
558 float f_position = 0.0;
561 unsigned int dst_x, dst_y;
564 unsigned int i_tb_width, i_tb_height;
566 /* This method does nothing if toolbar is hidden. */
570 const NPWindow& window = getWindow();
571 Window control = getControlWindow();
572 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
574 getToolbarSize( &i_tb_width, &i_tb_height );
576 /* get media instance */
577 libvlc_exception_init( &ex );
578 p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
579 libvlc_exception_clear( &ex );
582 i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
583 libvlc_exception_clear( &ex );
586 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
587 libvlc_exception_clear( &ex );
589 /* get movie position in % */
592 f_position = libvlc_media_player_get_position( p_md, &ex ) * 100;
593 libvlc_exception_clear( &ex );
595 libvlc_media_player_release( p_md );
597 gcv.foreground = BlackPixel( p_display, 0 );
598 gc = XCreateGC( p_display, control, GCForeground, &gcv );
600 XFillRectangle( p_display, control, gc,
601 0, 0, window.width, i_tb_height );
602 gcv.foreground = WhitePixel( p_display, 0 );
603 XChangeGC( p_display, gc, GCForeground, &gcv );
607 dst_y = i_tb_height >> 1; /* baseline = vertical middle */
609 if( p_btnPause && (i_playing == 1) )
611 XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
612 dst_y - (p_btnPause->height >> 1),
613 p_btnPause->width, p_btnPause->height );
614 dst_x += BTN_SPACE + p_btnPause->width;
618 XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
619 dst_y - (p_btnPlay->height >> 1),
620 p_btnPlay->width, p_btnPlay->height );
621 dst_x += BTN_SPACE + p_btnPlay->width;
625 XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
626 dst_y - (p_btnStop->height >> 1),
627 p_btnStop->width, p_btnStop->height );
629 dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
631 if( p_btnFullscreen )
632 XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
633 dst_y - (p_btnFullscreen->height >> 1),
634 p_btnFullscreen->width, p_btnFullscreen->height );
636 dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
638 if( p_btnUnmute && b_mute )
640 XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
641 dst_y - (p_btnUnmute->height >> 1),
642 p_btnUnmute->width, p_btnUnmute->height );
644 dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
648 XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
649 dst_y - (p_btnMute->height >> 1),
650 p_btnMute->width, p_btnMute->height );
652 dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
656 XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x,
657 dst_y - (p_timeline->height >> 1),
658 (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
661 i_last_position = (int)( f_position *
662 ( ((float)(window.width-(dst_x+BTN_SPACE))) / 100.0 ));
665 XPutImage( p_display, control, gc, p_btnTime,
666 0, 0, (dst_x+i_last_position),
667 dst_y - (p_btnTime->height >> 1),
668 p_btnTime->width, p_btnTime->height );
670 XFreeGC( p_display, gc );
673 vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos )
675 unsigned int i_dest = BTN_SPACE;//(i_tb_height >> 1);
678 libvlc_exception_t ex;
680 fprintf( stderr, "ToolbarButtonClicked:: "
681 "trying to match (%d,%d) (%d,%d)\n",
682 i_xpos, i_ypos, i_tb_height, i_tb_width );
684 if( i_ypos >= i_tb_width )
685 return clicked_Unknown;
687 /* Note: the order of testing is dependend on the original
688 * drawing positions of the icon buttons. Buttons are tested
693 libvlc_exception_init( &ex );
694 i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
695 libvlc_exception_clear( &ex );
698 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
699 libvlc_exception_clear( &ex );
701 /* is Pause of Play button clicked */
702 if( (i_playing != 1) &&
703 (i_xpos >= (BTN_SPACE>>1)) &&
704 (i_xpos <= i_dest + p_btnPlay->width + (BTN_SPACE>>1)) )
706 else if( (i_xpos >= (BTN_SPACE>>1)) &&
707 (i_xpos <= i_dest + p_btnPause->width) )
708 return clicked_Pause;
710 /* is Stop button clicked */
712 i_dest += (p_btnPlay->width + (BTN_SPACE>>1));
714 i_dest += (p_btnPause->width + (BTN_SPACE>>1));
716 if( (i_xpos >= i_dest) &&
717 (i_xpos <= i_dest + p_btnStop->width + (BTN_SPACE>>1)) )
720 /* is Fullscreen button clicked */
721 i_dest += (p_btnStop->width + (BTN_SPACE>>1));
722 if( (i_xpos >= i_dest) &&
723 (i_xpos <= i_dest + p_btnFullscreen->width + (BTN_SPACE>>1)) )
724 return clicked_Fullscreen;
726 /* is Mute or Unmute button clicked */
727 i_dest += (p_btnFullscreen->width + (BTN_SPACE>>1));
728 if( !b_mute && (i_xpos >= i_dest) &&
729 (i_xpos <= i_dest + p_btnMute->width + (BTN_SPACE>>1)) )
731 else if( (i_xpos >= i_dest) &&
732 (i_xpos <= i_dest + p_btnUnmute->width + (BTN_SPACE>>1)) )
733 return clicked_Unmute;
735 /* is timeline clicked */
737 i_dest += (p_btnMute->width + (BTN_SPACE>>1));
739 i_dest += (p_btnUnmute->width + (BTN_SPACE>>1));
740 if( (i_xpos >= i_dest) &&
741 (i_xpos <= i_dest + p_timeline->width + (BTN_SPACE>>1)) )
742 return clicked_timeline;
744 /* is time button clicked */
745 i_dest += (p_timeline->width + (BTN_SPACE>>1));
746 if( (i_xpos >= i_dest) &&
747 (i_xpos <= i_dest + p_btnTime->width + (BTN_SPACE>>1)) )
750 return clicked_Unknown;