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 /* FIXME: Remove this when toolbar functionality has been implemented on\
180 * MacOS X and Win32 for Firefox/Mozilla/Safari. */
182 b_toolbar = boolValue(argv[i]);
187 libvlc_exception_t ex;
188 libvlc_exception_init(&ex);
190 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
191 if( libvlc_exception_raised(&ex) )
193 libvlc_exception_clear(&ex);
194 return NPERR_GENERIC_ERROR;
196 libvlc_exception_clear(&ex);
199 ** fetch plugin base URL, which is the URL of the page containing the plugin
200 ** this URL is used for making absolute URL from relative URL that may be
201 ** passed as an MRL argument
205 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
208 ** is there a better way to get that info ?
210 static const char docLocHref[] = "document.location.href";
214 script.utf8characters = docLocHref;
215 script.utf8length = sizeof(docLocHref)-1;
217 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
219 if( NPVARIANT_IS_STRING(result) )
221 NPString &location = NPVARIANT_TO_STRING(result);
223 psz_baseURL = static_cast<char*>(malloc(location.utf8length+1));
226 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
227 psz_baseURL[location.utf8length] = '\0';
230 NPN_ReleaseVariantValue(&result);
232 NPN_ReleaseObject(plugin);
237 // get absolute URL from src
238 char *psz_absurl = getAbsoluteURL(psz_target);
239 psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
242 /* assign plugin script root class */
244 p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
246 return NPERR_NO_ERROR;
249 VlcPlugin::~VlcPlugin()
254 libvlc_log_close(libvlc_log, NULL);
255 if( libvlc_instance )
256 libvlc_release(libvlc_instance);
259 /*****************************************************************************
261 *****************************************************************************/
263 char *VlcPlugin::getAbsoluteURL(const char *url)
267 // check whether URL is already absolute
268 const char *end=strchr(url, ':');
269 if( (NULL != end) && (end != url) )
271 // validate protocol header
272 const char *start = url;
277 while( start != end )
284 || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
285 // not valid protocol header, assume relative URL
289 /* we have a protocol header, therefore URL is absolute */
292 // not a valid protocol header, assume relative URL
299 size_t baseLen = strlen(psz_baseURL);
300 char *href = static_cast<char*>(malloc(baseLen+strlen(url)+1));
303 /* prepend base URL */
304 strcpy(href, psz_baseURL);
307 ** relative url could be empty,
308 ** in which case return base URL
314 ** locate pathname part of base URL
317 /* skip over protocol part */
318 char *pathstart = strchr(href, ':');
322 if( '/' == *(++pathstart) )
324 if( '/' == *(++pathstart) )
329 /* skip over host part */
330 pathstart = strchr(pathstart, '/');
331 pathend = href+baseLen;
334 // no path, add a / past end of url (over '\0')
341 /* baseURL is just a UNIX path */
344 /* baseURL is not an absolute path */
349 pathend = href+baseLen;
352 /* relative URL made of an absolute path ? */
355 /* replace path completely */
356 strcpy(pathstart, url);
360 /* find last path component and replace it */
361 while( '/' != *pathend)
365 ** if relative url path starts with one or more '../',
366 ** factor them out of href so that we return a
369 while( pathend != pathstart )
377 /* relative url is just '.' */
383 /* relative url starts with './' */
392 /* relative url is '..' */
398 /* relative url starts with '../' */
406 while( '/' != *pathend );
408 /* skip over '/' separator */
410 /* concatenate remaining base URL and relative URL */
411 strcpy(pathend, url);
420 int VlcPlugin::setSize(unsigned width, unsigned height)
422 int diff = (width != i_width) || (height != i_height);
431 #define BTN_SPACE ((unsigned int)4)
432 void VlcPlugin::showToolbar()
434 const NPWindow& window = getWindow();
435 Window control = getControlWindow();
436 Window video = getVideoWindow();
437 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
438 unsigned int i_height = 0, i_width = BTN_SPACE;
442 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
443 &p_btnPlay, NULL, NULL);
446 i_height = __MAX( i_height, p_btnPlay->height );
449 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
450 &p_btnPause, NULL, NULL);
453 i_height = __MAX( i_height, p_btnPause->height );
455 i_width += __MAX( p_btnPause->width, p_btnPlay->width );
458 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
459 &p_btnStop, NULL, NULL );
462 i_height = __MAX( i_height, p_btnStop->height );
463 i_width += BTN_SPACE + p_btnStop->width;
466 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
467 &p_timeline, NULL, NULL);
470 i_height = __MAX( i_height, p_timeline->height );
471 i_width += BTN_SPACE + p_timeline->width;
474 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
475 &p_btnTime, NULL, NULL);
478 i_height = __MAX( i_height, p_btnTime->height );
479 i_width += BTN_SPACE + p_btnTime->width;
481 if( !p_btnFullscreen )
482 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
483 &p_btnFullscreen, NULL, NULL);
484 if( p_btnFullscreen )
486 i_height = __MAX( i_height, p_btnFullscreen->height );
487 i_width += BTN_SPACE + p_btnFullscreen->width;
490 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
491 &p_btnMute, NULL, NULL);
494 i_height = __MAX( i_height, p_btnMute->height );
497 XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
498 &p_btnUnmute, NULL, NULL);
501 i_height = __MAX( i_height, p_btnUnmute->height );
503 i_width += BTN_SPACE + __MAX( p_btnUnmute->width, p_btnMute->width );
505 setToolbarSize( i_width, i_height );
507 if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
508 !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
509 fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
511 /* reset panels position and size */
512 /* XXX use i_width */
513 XResizeWindow( p_display, video, window.width, window.height - i_height);
514 XMoveWindow( p_display, control, 0, window.height - i_height );
515 XResizeWindow( p_display, control, window.width, i_height -1);
517 b_toolbar = 1; /* says toolbar is now shown */
521 void VlcPlugin::hideToolbar()
523 const NPWindow& window = getWindow();
524 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
525 Window control = getControlWindow();
526 Window video = getVideoWindow();
528 i_tb_width = i_tb_height = 0;
530 if( p_btnPlay ) XDestroyImage( p_btnPlay );
531 if( p_btnPause ) XDestroyImage( p_btnPause );
532 if( p_btnStop ) XDestroyImage( p_btnStop );
533 if( p_timeline ) XDestroyImage( p_timeline );
534 if( p_btnTime ) XDestroyImage( p_btnTime );
535 if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
536 if( p_btnMute ) XDestroyImage( p_btnMute );
537 if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
544 p_btnFullscreen = NULL;
548 /* reset panels position and size */
549 /* XXX use i_width */
550 XResizeWindow( p_display, video, window.width, window.height );
551 XMoveWindow( p_display, control, 0, window.height-1 );
552 XResizeWindow( p_display, control, window.width, 1 );
554 b_toolbar = 0; /* says toolbar is now hidden */
558 void VlcPlugin::redrawToolbar()
560 libvlc_media_player_t *p_md = NULL;
561 libvlc_exception_t ex;
562 float f_position = 0.0;
565 unsigned int dst_x, dst_y;
568 unsigned int i_tb_width, i_tb_height;
570 /* This method does nothing if toolbar is hidden. */
574 const NPWindow& window = getWindow();
575 Window control = getControlWindow();
576 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
578 getToolbarSize( &i_tb_width, &i_tb_height );
580 /* get media instance */
581 libvlc_exception_init( &ex );
582 p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
583 libvlc_exception_clear( &ex );
586 i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
587 libvlc_exception_clear( &ex );
590 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
591 libvlc_exception_clear( &ex );
593 /* get movie position in % */
596 f_position = libvlc_media_player_get_position( p_md, &ex ) * 100;
597 libvlc_exception_clear( &ex );
599 libvlc_media_player_release( p_md );
601 gcv.foreground = BlackPixel( p_display, 0 );
602 gc = XCreateGC( p_display, control, GCForeground, &gcv );
604 XFillRectangle( p_display, control, gc,
605 0, 0, window.width, i_tb_height );
606 gcv.foreground = WhitePixel( p_display, 0 );
607 XChangeGC( p_display, gc, GCForeground, &gcv );
611 dst_y = i_tb_height >> 1; /* baseline = vertical middle */
613 if( p_btnPause && (i_playing == 1) )
615 XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
616 dst_y - (p_btnPause->height >> 1),
617 p_btnPause->width, p_btnPause->height );
618 dst_x += BTN_SPACE + p_btnPause->width;
622 XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
623 dst_y - (p_btnPlay->height >> 1),
624 p_btnPlay->width, p_btnPlay->height );
625 dst_x += BTN_SPACE + p_btnPlay->width;
629 XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
630 dst_y - (p_btnStop->height >> 1),
631 p_btnStop->width, p_btnStop->height );
633 dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
635 if( p_btnFullscreen )
636 XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
637 dst_y - (p_btnFullscreen->height >> 1),
638 p_btnFullscreen->width, p_btnFullscreen->height );
640 dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
642 if( p_btnUnmute && b_mute )
644 XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
645 dst_y - (p_btnUnmute->height >> 1),
646 p_btnUnmute->width, p_btnUnmute->height );
648 dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
652 XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
653 dst_y - (p_btnMute->height >> 1),
654 p_btnMute->width, p_btnMute->height );
656 dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
660 XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x,
661 dst_y - (p_timeline->height >> 1),
662 (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
665 i_last_position = (int)( f_position *
666 ( ((float)(window.width-(dst_x+BTN_SPACE))) / 100.0 ));
669 XPutImage( p_display, control, gc, p_btnTime,
670 0, 0, (dst_x+i_last_position),
671 dst_y - (p_btnTime->height >> 1),
672 p_btnTime->width, p_btnTime->height );
674 XFreeGC( p_display, gc );
677 vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos )
679 unsigned int i_dest = BTN_SPACE;//(i_tb_height >> 1);
682 libvlc_exception_t ex;
684 fprintf( stderr, "ToolbarButtonClicked:: "
685 "trying to match (%d,%d) (%d,%d)\n",
686 i_xpos, i_ypos, i_tb_height, i_tb_width );
688 if( i_ypos >= i_tb_width )
689 return clicked_Unknown;
691 /* Note: the order of testing is dependend on the original
692 * drawing positions of the icon buttons. Buttons are tested
697 libvlc_exception_init( &ex );
698 i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
699 libvlc_exception_clear( &ex );
702 b_mute = libvlc_audio_get_mute( getVLC(), &ex );
703 libvlc_exception_clear( &ex );
705 /* is Pause of Play button clicked */
706 if( (i_playing != 1) &&
707 (i_xpos >= (BTN_SPACE>>1)) &&
708 (i_xpos <= i_dest + p_btnPlay->width + (BTN_SPACE>>1)) )
710 else if( (i_xpos >= (BTN_SPACE>>1)) &&
711 (i_xpos <= i_dest + p_btnPause->width) )
712 return clicked_Pause;
714 /* is Stop button clicked */
716 i_dest += (p_btnPlay->width + (BTN_SPACE>>1));
718 i_dest += (p_btnPause->width + (BTN_SPACE>>1));
720 if( (i_xpos >= i_dest) &&
721 (i_xpos <= i_dest + p_btnStop->width + (BTN_SPACE>>1)) )
724 /* is Fullscreen button clicked */
725 i_dest += (p_btnStop->width + (BTN_SPACE>>1));
726 if( (i_xpos >= i_dest) &&
727 (i_xpos <= i_dest + p_btnFullscreen->width + (BTN_SPACE>>1)) )
728 return clicked_Fullscreen;
730 /* is Mute or Unmute button clicked */
731 i_dest += (p_btnFullscreen->width + (BTN_SPACE>>1));
732 if( !b_mute && (i_xpos >= i_dest) &&
733 (i_xpos <= i_dest + p_btnMute->width + (BTN_SPACE>>1)) )
735 else if( (i_xpos >= i_dest) &&
736 (i_xpos <= i_dest + p_btnUnmute->width + (BTN_SPACE>>1)) )
737 return clicked_Unmute;
739 /* is timeline clicked */
741 i_dest += (p_btnMute->width + (BTN_SPACE>>1));
743 i_dest += (p_btnUnmute->width + (BTN_SPACE>>1));
744 if( (i_xpos >= i_dest) &&
745 (i_xpos <= i_dest + p_timeline->width + (BTN_SPACE>>1)) )
746 return clicked_timeline;
748 /* is time button clicked */
749 i_dest += (p_timeline->width + (BTN_SPACE>>1));
750 if( (i_xpos >= i_dest) &&
751 (i_xpos <= i_dest + p_btnTime->width + (BTN_SPACE>>1)) )
754 return clicked_Unknown;