1 /*****************************************************************************
2 * vlcshell.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Jean-Paul Saman <jpsaman@videolan.org>
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 *****************************************************************************/
35 #ifdef HAVE_MOZILLA_CONFIG_H
36 # include <mozilla-config.h>
39 /* This is from mozilla java, do we really need it? */
44 #include "vlcplugin.h"
47 /* Enable/disable debugging printf's for X11 resizing */
48 #undef X11_RESIZE_DEBUG
50 /*****************************************************************************
51 * Unix-only declarations
52 ******************************************************************************/
55 static void Redraw( Widget w, XtPointer closure, XEvent *event );
56 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
57 static void Resize( Widget w, XtPointer closure, XEvent *event );
61 /*****************************************************************************
62 * MacOS-only declarations
63 ******************************************************************************/
67 /*****************************************************************************
68 * Windows-only declarations
69 *****************************************************************************/
72 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
76 /******************************************************************************
78 *****************************************************************************/
79 char * NPP_GetMIMEDescription( void )
81 static char mimetype[] = PLUGIN_MIMETYPES;
85 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
87 static char psz_name[] = PLUGIN_NAME;
88 static char psz_desc[1000];
90 /* plugin class variables */
93 case NPPVpluginNameString:
94 *((char **)value) = psz_name;
95 return NPERR_NO_ERROR;
97 case NPPVpluginDescriptionString:
98 snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
99 libvlc_get_version() );
100 *((char **)value) = psz_desc;
101 return NPERR_NO_ERROR;
104 /* move on to instance variables ... */
108 if( instance == NULL )
110 return NPERR_INVALID_INSTANCE_ERROR;
113 /* plugin instance variables */
115 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
116 if( NULL == p_plugin )
118 // plugin has not been initialized yet !
119 return NPERR_INVALID_INSTANCE_ERROR;
124 case NPPVpluginScriptableNPObject:
126 /* retrieve plugin root class */
127 NPClass *scriptClass = p_plugin->getScriptClass();
130 /* create an instance and return it */
131 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
132 return NPERR_NO_ERROR;
140 return NPERR_GENERIC_ERROR;
144 * there is some confusion in gecko headers regarding definition of this API
145 * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
148 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
150 return NPERR_GENERIC_ERROR;
153 /******************************************************************************
155 *****************************************************************************/
157 int16_t NPP_HandleEvent( NPP instance, void * event )
159 static UInt32 lastMouseUp = 0;
160 if( instance == NULL )
165 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
166 if( p_plugin == NULL )
171 EventRecord *myEvent = (EventRecord*)event;
173 switch( myEvent->what )
179 if( (myEvent->when - lastMouseUp) < GetDblTime() )
182 p_plugin->toggle_fullscreen();
187 lastMouseUp = myEvent->when;
195 const NPWindow& npwindow = p_plugin->getWindow();
196 if( npwindow.window )
198 bool hasVout = false;
200 if( p_plugin->playlist_isplaying() )
202 hasVout = p_plugin->player_has_vout();
206 libvlc_rectangle_t area;
209 area.right = npwindow.width;
210 area.bottom = npwindow.height;
211 libvlc_video_redraw_rectangle(p_plugin->getMD(), &area, NULL);
214 #warning disabled code
220 /* draw the beautiful "No Picture" */
222 ForeColor(blackColor);
225 /* seems that firefox forgets to set the following
226 * on occasion (reload) */
227 SetOrigin(((NP_Port *)npwindow.window)->portx,
228 ((NP_Port *)npwindow.window)->porty);
233 rect.right = npwindow.width;
234 rect.bottom = npwindow.height;
237 ForeColor(whiteColor);
238 MoveTo( (npwindow.width-80)/ 2 , npwindow.height / 2 );
239 if( p_plugin->psz_text )
240 DrawText( p_plugin->psz_text, 0, strlen(p_plugin->psz_text) );
247 case NPEventType_GetFocusEvent:
248 case NPEventType_LoseFocusEvent:
250 case NPEventType_AdjustCursorEvent:
252 case NPEventType_MenuCommandEvent:
254 case NPEventType_ClippingChangedEvent:
256 case NPEventType_ScrollingBeginsEvent:
258 case NPEventType_ScrollingEndsEvent:
265 #endif /* XP_MACOSX */
267 /******************************************************************************
268 * General Plug-in Calls
269 *****************************************************************************/
270 NPError NPP_Initialize( void )
272 return NPERR_NO_ERROR;
276 jref NPP_GetJavaClass( void )
282 void NPP_Shutdown( void )
287 NPError NPP_New( NPMIMEType pluginType, NPP instance,
288 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
289 uint16 mode, int16 argc,
291 uint16_t mode, int16_t argc,
293 char* argn[], char* argv[], NPSavedData* saved )
297 if( instance == NULL )
299 return NPERR_INVALID_INSTANCE_ERROR;
302 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
303 if( NULL == p_plugin )
305 return NPERR_OUT_OF_MEMORY_ERROR;
308 status = p_plugin->init(argc, argn, argv);
309 if( NPERR_NO_ERROR == status )
311 instance->pdata = reinterpret_cast<void*>(p_plugin);
313 NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
314 NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
324 NPError NPP_Destroy( NPP instance, NPSavedData** save )
326 if( NULL == instance )
327 return NPERR_INVALID_INSTANCE_ERROR;
329 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
330 if( NULL == p_plugin )
331 return NPERR_NO_ERROR;
333 instance->pdata = NULL;
336 HWND win = (HWND)p_plugin->getWindow().window;
337 WNDPROC winproc = p_plugin->getWindowProc();
341 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
345 if( p_plugin->playlist_isplaying() )
346 p_plugin->playlist_stop();
350 return NPERR_NO_ERROR;
353 NPError NPP_SetWindow( NPP instance, NPWindow* window )
357 unsigned int i_control_height = 0, i_control_width = 0;
362 return NPERR_INVALID_INSTANCE_ERROR;
365 /* NPP_SetWindow may be called before NPP_New (Opera) */
366 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
367 if( NULL == p_plugin )
369 /* we should probably show a splash screen here */
370 return NPERR_NO_ERROR;
374 control = p_plugin->getControlWindow();
377 libvlc_instance_t *p_vlc = p_plugin->getVLC();
381 * Before setting window to point to the
382 * new window, you may wish to compare the new window
383 * info to the previous window (if any) to note window
387 /* retrieve current window */
388 NPWindow& curwin = p_plugin->getWindow();
391 if( window && window->window )
393 /* check if plugin has a new parent window */
394 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
396 /* as MacOS X video output is windowless, set viewport */
397 libvlc_rectangle_t view, clip;
400 ** browser sets port origin to top-left location of plugin
401 ** relative to GrafPort window origin is set relative to document,
402 ** which of little use for drawing
404 view.top = ((NP_Port*) (window->window))->porty;
405 view.left = ((NP_Port*) (window->window))->portx;
406 view.bottom = window->height+view.top;
407 view.right = window->width+view.left;
409 /* clipRect coordinates are also relative to GrafPort */
410 clip.top = window->clipRect.top;
411 clip.left = window->clipRect.left;
412 clip.bottom = window->clipRect.bottom;
413 clip.right = window->clipRect.right;
415 libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
417 #warning disabled code
419 /* remember new window */
420 p_plugin->setWindow(*window);
422 else if( curwin.window )
424 /* change/set parent */
425 curwin.window = NULL;
427 #endif /* XP_MACOSX */
430 if( window && window->window )
432 /* check if plugin has a new parent window */
433 HWND drawable = (HWND) (window->window);
434 if( !curwin.window || drawable != curwin.window )
436 /* reset previous window settings */
437 HWND oldwin = (HWND)p_plugin->getWindow().window;
438 WNDPROC oldproc = p_plugin->getWindowProc();
442 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
444 /* attach our plugin object */
445 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
446 reinterpret_cast<LONG_PTR>(p_plugin));
448 /* install our WNDPROC */
449 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
450 GWL_WNDPROC, (LONG)Manage ) );
452 /* change window style to our liking */
453 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
454 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
455 SetWindowLong((HWND)drawable, GWL_STYLE, style);
457 /* remember new window */
458 p_plugin->setWindow(*window);
461 InvalidateRect( (HWND)drawable, NULL, TRUE );
462 UpdateWindow( (HWND)drawable );
465 else if( curwin.window )
468 HWND oldwin = (HWND)curwin.window;
469 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
470 p_plugin->setWindowProc(NULL);
472 curwin.window = NULL;
477 /* default to hidden toolbar, shown at the end of this method if asked *
478 * developers note : getToolbarSize need to wait the end of this method
480 i_control_height = 0;
481 i_control_width = window->width;
483 if( window && window->window )
485 Window parent = (Window) window->window;
486 if( !curwin.window || (parent != (Window)curwin.window) )
488 Display *p_display = ( (NPSetWindowCallbackStruct *)
489 window->ws_info )->display;
491 XResizeWindow( p_display, parent, window->width, window->height );
493 int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
496 Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
497 window->width, window->height - i_control_height,
498 0, i_blackColor, i_blackColor );
499 Window controls = (Window) NULL;
500 controls = XCreateSimpleWindow( p_display, parent,
501 0, window->height - i_control_height-1,
502 window->width, i_control_height-1,
503 0, i_blackColor, i_blackColor );
505 XMapWindow( p_display, parent );
506 XMapWindow( p_display, video );
507 if( controls ) { XMapWindow( p_display, controls ); }
512 Widget w = XtWindowToWidget( p_display, parent );
514 XtAddEventHandler( w, ExposureMask, FALSE,
515 (XtEventHandler)Redraw, p_plugin );
516 XtAddEventHandler( w, StructureNotifyMask, FALSE,
517 (XtEventHandler)Resize, p_plugin );
518 XtAddEventHandler( w, ButtonReleaseMask, FALSE,
519 (XtEventHandler)ControlHandler, p_plugin );
521 /* remember window */
522 p_plugin->setWindow( *window );
523 p_plugin->setVideoWindow( video );
527 p_plugin->setControlWindow( controls );
530 Redraw( w, (XtPointer)p_plugin, NULL );
532 /* now display toolbar if asked through parameters */
533 if( p_plugin->b_toolbar )
535 p_plugin->showToolbar();
539 else if( curwin.window )
541 curwin.window = NULL;
545 if( !p_plugin->b_stream )
547 if( p_plugin->psz_target )
549 if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
551 if( p_plugin->b_autoplay )
553 p_plugin->playlist_play();
556 p_plugin->b_stream = true;
559 return NPERR_NO_ERROR;
562 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
563 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
564 NPBool seekable, uint16 *stype )
566 NPBool seekable, uint16_t *stype )
569 if( NULL == instance )
571 return NPERR_INVALID_INSTANCE_ERROR;
574 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
575 if( NULL == p_plugin )
577 return NPERR_INVALID_INSTANCE_ERROR;
581 ** Firefox/Mozilla may decide to open a stream from the URL specified
582 ** in the SRC parameter of the EMBED tag and pass it to us
584 ** since VLC will open the SRC URL as well, we're not interested in
585 ** that stream. Otherwise, we'll take it and queue it up in the playlist
587 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
589 /* TODO: use pipes !!!! */
590 *stype = NP_ASFILEONLY;
591 return NPERR_NO_ERROR;
593 return NPERR_GENERIC_ERROR;
596 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
597 int32 NPP_WriteReady( NPP instance, NPStream *stream )
599 int32_t NPP_WriteReady( NPP instance, NPStream *stream )
606 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
607 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
608 int32 len, void *buffer )
610 int32_t NPP_Write( NPP instance, NPStream *stream, int32_t offset,
611 int32_t len, void *buffer )
618 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
620 if( instance == NULL )
622 return NPERR_INVALID_INSTANCE_ERROR;
624 return NPERR_NO_ERROR;
627 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
629 if( instance == NULL )
634 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
635 if( NULL == p_plugin )
640 if( p_plugin->playlist_add( stream->url ) != -1 )
642 if( p_plugin->b_autoplay )
644 p_plugin->playlist_play();
649 void NPP_URLNotify( NPP instance, const char* url,
650 NPReason reason, void* notifyData )
652 /***** Insert NPP_URLNotify code here *****\
653 PluginInstance* p_plugin;
654 if (instance != NULL)
655 p_plugin = (PluginInstance*) instance->pdata;
656 \*********************************************/
659 void NPP_Print( NPP instance, NPPrint* printInfo )
661 if( printInfo == NULL )
666 if( instance != NULL )
668 /***** Insert NPP_Print code here *****\
669 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
670 \**************************************/
672 if( printInfo->mode == NP_FULL )
676 * If your plugin would like to take over
677 * printing completely when it is in full-screen mode,
678 * set printInfo->pluginPrinted to TRUE and print your
679 * plugin as you see fit. If your plugin wants Netscape
680 * to handle printing in this case, set
681 * printInfo->pluginPrinted to FALSE (the default) and
682 * do nothing. If you do want to handle printing
683 * yourself, printOne is true if the print button
684 * (as opposed to the print menu) was clicked.
685 * On the Macintosh, platformPrint is a THPrint; on
686 * Windows, platformPrint is a structure
687 * (defined in npapi.h) containing the printer name, port,
691 /***** Insert NPP_Print code here *****\
692 void* platformPrint =
693 printInfo->print.fullPrint.platformPrint;
695 printInfo->print.fullPrint.printOne;
696 \**************************************/
699 printInfo->print.fullPrint.pluginPrinted = FALSE;
703 /* If not fullscreen, we must be embedded */
706 * If your plugin is embedded, or is full-screen
707 * but you returned false in pluginPrinted above, NPP_Print
708 * will be called with mode == NP_EMBED. The NPWindow
709 * in the printInfo gives the location and dimensions of
710 * the embedded plugin on the printed page. On the
711 * Macintosh, platformPrint is the printer port; on
712 * Windows, platformPrint is the handle to the printing
716 /***** Insert NPP_Print code here *****\
717 NPWindow* printWindow =
718 &(printInfo->print.embedPrint.window);
719 void* platformPrint =
720 printInfo->print.embedPrint.platformPrint;
721 \**************************************/
726 /******************************************************************************
727 * Windows-only methods
728 *****************************************************************************/
730 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
732 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
741 PAINTSTRUCT paintstruct;
745 hdc = BeginPaint( p_hwnd, &paintstruct );
747 GetClientRect( p_hwnd, &rect );
749 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
750 SetTextColor(hdc, RGB(255, 255, 255));
751 SetBkColor(hdc, RGB(0, 0, 0));
752 if( p_plugin->psz_text )
753 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
754 DT_CENTER|DT_VCENTER|DT_SINGLELINE);
756 EndPaint( p_hwnd, &paintstruct );
760 /* delegate to default handler */
761 return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
767 /******************************************************************************
769 *****************************************************************************/
771 static void Redraw( Widget w, XtPointer closure, XEvent *event )
773 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
774 Window control = p_plugin->getControlWindow();
775 const NPWindow& window = p_plugin->getWindow();
778 unsigned int i_control_height, i_control_width;
780 if( p_plugin->b_toolbar )
781 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
783 i_control_height = i_control_width = 0;
785 Window video = p_plugin->getVideoWindow();
786 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
788 gcv.foreground = BlackPixel( p_display, 0 );
789 gc = XCreateGC( p_display, video, GCForeground, &gcv );
791 XFillRectangle( p_display, video, gc,
792 0, 0, window.width, window.height - i_control_height);
794 gcv.foreground = WhitePixel( p_display, 0 );
795 XChangeGC( p_display, gc, GCForeground, &gcv );
797 if( p_plugin->psz_text )
798 XDrawString( p_display, video, gc,
799 window.width / 2 - 40, (window.height - i_control_height) / 2,
800 p_plugin->psz_text, strlen(p_plugin->psz_text) );
801 XFreeGC( p_display, gc );
803 p_plugin->redrawToolbar();
806 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
808 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
809 const NPWindow& window = p_plugin->getWindow();
811 int i_height = window.height;
812 int i_width = window.width;
813 int i_xPos = event->xbutton.x;
814 int i_yPos = event->xbutton.y;
816 if( p_plugin && p_plugin->b_toolbar )
820 libvlc_media_player_t *p_md = p_plugin->getMD();
822 i_playing = p_plugin->playlist_isplaying();
824 vlc_toolbar_clicked_t clicked;
825 clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
832 p_plugin->playlist_pause();
834 p_plugin->playlist_play();
840 p_plugin->playlist_stop();
844 case clicked_Fullscreen:
846 p_plugin->set_fullscreen( 1 );
854 libvlc_audio_toggle_mute( p_md );
858 case clicked_timeline:
860 /* if a movie is loaded */
864 f_length = libvlc_media_player_get_length( p_md ) / 100;
866 f_length = (float)f_length *
867 ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
869 libvlc_media_player_set_time( p_md, f_length );
876 /* Not implemented yet*/
880 default: /* button_Unknown */
884 Redraw( w, closure, event );
887 static void Resize ( Widget w, XtPointer closure, XEvent *event )
889 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
890 Window control = p_plugin->getControlWindow();
891 const NPWindow& window = p_plugin->getWindow();
892 Window drawable = p_plugin->getVideoWindow();
893 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
896 Window root_return, parent_return, * children_return;
898 unsigned int i_nchildren;
899 unsigned int i_control_height, i_control_width;
901 if( p_plugin->b_toolbar )
903 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
907 i_control_height = i_control_width = 0;
910 #ifdef X11_RESIZE_DEBUG
911 XWindowAttributes attr;
913 if( event && event->type == ConfigureNotify )
915 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
916 "send_event ? %s\n", event->xconfigure.width,
917 event->xconfigure.height,
918 event->xconfigure.send_event ? "TRUE" : "FALSE" );
920 #endif /* X11_RESIZE_DEBUG */
922 if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
924 /* size already set */
928 i_ret = XResizeWindow( p_display, drawable,
929 window.width, (window.height - i_control_height) );
931 #ifdef X11_RESIZE_DEBUG
933 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
935 XGetWindowAttributes ( p_display, drawable, &attr );
937 /* X is asynchronous, so the current size reported here is not
938 necessarily the requested size as the Resize request may not
939 yet have been handled by the plugin host */
940 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
941 attr.width, attr.height );
942 #endif /* X11_RESIZE_DEBUG */
944 XQueryTree( p_display, drawable,
945 &root_return, &parent_return, &children_return,
948 if( i_nchildren > 0 )
950 /* XXX: Make assumptions related to the window parenting structure in
951 vlc/modules/video_output/x11/xcommon.c */
952 base_window = children_return[i_nchildren - 1];
954 #ifdef X11_RESIZE_DEBUG
955 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
956 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
958 #endif /* X11_RESIZE_DEBUG */
960 i_ret = XResizeWindow( p_display, base_window,
961 window.width, ( window.height - i_control_height ) );
963 #ifdef X11_RESIZE_DEBUG
965 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
968 XGetWindowAttributes( p_display, base_window, &attr );
970 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
971 attr.width, attr.height );
972 #endif /* X11_RESIZE_DEBUG */