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 NPP_HandleEvent( NPP instance, void * event )
159 static UInt32 lastMouseUp = 0;
160 libvlc_exception_t ex;
161 libvlc_exception_init(&ex);
163 if( instance == NULL )
168 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
169 if( p_plugin == NULL )
174 EventRecord *myEvent = (EventRecord*)event;
176 switch( myEvent->what )
182 if( (myEvent->when - lastMouseUp) < GetDblTime() )
185 p_plugin->toggle_fullscreen(&ex);
186 libvlc_exception_clear(&ex);
191 lastMouseUp = myEvent->when;
199 const NPWindow& npwindow = p_plugin->getWindow();
200 if( npwindow.window )
202 bool hasVout = false;
204 if( p_plugin->playlist_isplaying() )
206 hasVout = p_plugin->player_has_vout(NULL);
210 libvlc_rectangle_t area;
213 area.right = npwindow.width;
214 area.bottom = npwindow.height;
215 libvlc_video_redraw_rectangle(p_plugin->getMD(&ex), &area, NULL);
217 #warning disabled code
221 libvlc_exception_clear(&ex);
225 /* draw the beautiful "No Picture" */
227 ForeColor(blackColor);
230 /* seems that firefox forgets to set the following
231 * on occasion (reload) */
232 SetOrigin(((NP_Port *)npwindow.window)->portx,
233 ((NP_Port *)npwindow.window)->porty);
238 rect.right = npwindow.width;
239 rect.bottom = npwindow.height;
242 ForeColor(whiteColor);
243 MoveTo( (npwindow.width-80)/ 2 , npwindow.height / 2 );
244 if( p_plugin->psz_text )
245 DrawText( p_plugin->psz_text, 0, strlen(p_plugin->psz_text) );
252 case NPEventType_GetFocusEvent:
253 case NPEventType_LoseFocusEvent:
255 case NPEventType_AdjustCursorEvent:
257 case NPEventType_MenuCommandEvent:
259 case NPEventType_ClippingChangedEvent:
261 case NPEventType_ScrollingBeginsEvent:
263 case NPEventType_ScrollingEndsEvent:
270 #endif /* XP_MACOSX */
272 /******************************************************************************
273 * General Plug-in Calls
274 *****************************************************************************/
275 NPError NPP_Initialize( void )
277 return NPERR_NO_ERROR;
280 jref NPP_GetJavaClass( void )
285 void NPP_Shutdown( void )
290 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
291 char* argn[], char* argv[], NPSavedData* saved )
295 if( instance == NULL )
297 return NPERR_INVALID_INSTANCE_ERROR;
300 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
301 if( NULL == p_plugin )
303 return NPERR_OUT_OF_MEMORY_ERROR;
306 status = p_plugin->init(argc, argn, argv);
307 if( NPERR_NO_ERROR == status )
309 instance->pdata = reinterpret_cast<void*>(p_plugin);
311 NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
312 NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
322 NPError NPP_Destroy( NPP instance, NPSavedData** save )
324 if( NULL == instance )
325 return NPERR_INVALID_INSTANCE_ERROR;
327 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
328 if( NULL == p_plugin )
329 return NPERR_NO_ERROR;
331 instance->pdata = NULL;
334 HWND win = (HWND)p_plugin->getWindow().window;
335 WNDPROC winproc = p_plugin->getWindowProc();
339 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
343 if( p_plugin->playlist_isplaying() )
344 p_plugin->playlist_stop();
348 return NPERR_NO_ERROR;
351 NPError NPP_SetWindow( NPP instance, NPWindow* window )
355 unsigned int i_control_height = 0, i_control_width = 0;
360 return NPERR_INVALID_INSTANCE_ERROR;
363 /* NPP_SetWindow may be called before NPP_New (Opera) */
364 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
365 if( NULL == p_plugin )
367 /* we should probably show a splash screen here */
368 return NPERR_NO_ERROR;
372 control = p_plugin->getControlWindow();
375 libvlc_exception_t ex;
376 libvlc_exception_init(&ex);
378 libvlc_instance_t *p_vlc = p_plugin->getVLC();
382 * Before setting window to point to the
383 * new window, you may wish to compare the new window
384 * info to the previous window (if any) to note window
388 /* retrieve current window */
389 NPWindow& curwin = p_plugin->getWindow();
392 if( window && window->window )
394 /* check if plugin has a new parent window */
395 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
397 /* as MacOS X video output is windowless, set viewport */
398 libvlc_rectangle_t view, clip;
401 ** browser sets port origin to top-left location of plugin
402 ** relative to GrafPort window origin is set relative to document,
403 ** which of little use for drawing
405 view.top = ((NP_Port*) (window->window))->porty;
406 view.left = ((NP_Port*) (window->window))->portx;
407 view.bottom = window->height+view.top;
408 view.right = window->width+view.left;
410 /* clipRect coordinates are also relative to GrafPort */
411 clip.top = window->clipRect.top;
412 clip.left = window->clipRect.left;
413 clip.bottom = window->clipRect.bottom;
414 clip.right = window->clipRect.right;
416 libvlc_video_set_viewport(p_vlc, p_plugin->getMD(&ex), &view, &clip, &ex);
417 libvlc_exception_clear(&ex);
419 #warning disabled code
421 /* remember new window */
422 p_plugin->setWindow(*window);
424 else if( curwin.window )
426 /* change/set parent */
427 curwin.window = NULL;
429 #endif /* XP_MACOSX */
432 if( window && window->window )
434 /* check if plugin has a new parent window */
435 HWND drawable = (HWND) (window->window);
436 if( !curwin.window || drawable != curwin.window )
438 /* reset previous window settings */
439 HWND oldwin = (HWND)p_plugin->getWindow().window;
440 WNDPROC oldproc = p_plugin->getWindowProc();
444 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
446 /* attach our plugin object */
447 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
448 reinterpret_cast<LONG_PTR>(p_plugin));
450 /* install our WNDPROC */
451 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
452 GWL_WNDPROC, (LONG)Manage ) );
454 /* change window style to our liking */
455 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
456 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
457 SetWindowLong((HWND)drawable, GWL_STYLE, style);
459 /* remember new window */
460 p_plugin->setWindow(*window);
463 InvalidateRect( (HWND)drawable, NULL, TRUE );
464 UpdateWindow( (HWND)drawable );
467 else if( curwin.window )
470 HWND oldwin = (HWND)curwin.window;
471 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
472 p_plugin->setWindowProc(NULL);
474 curwin.window = NULL;
479 /* default to hidden toolbar, shown at the end of this method if asked *
480 * developers note : getToolbarSize need to wait the end of this method
482 i_control_height = 0;
483 i_control_width = window->width;
485 if( window && window->window )
487 Window parent = (Window) window->window;
488 if( !curwin.window || (parent != (Window)curwin.window) )
490 Display *p_display = ( (NPSetWindowCallbackStruct *)
491 window->ws_info )->display;
493 XResizeWindow( p_display, parent, window->width, window->height );
495 int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
498 Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
499 window->width, window->height - i_control_height,
500 0, i_blackColor, i_blackColor );
501 Window controls = (Window) NULL;
502 controls = XCreateSimpleWindow( p_display, parent,
503 0, window->height - i_control_height-1,
504 window->width, i_control_height-1,
505 0, i_blackColor, i_blackColor );
507 XMapWindow( p_display, parent );
508 XMapWindow( p_display, video );
509 if( controls ) { XMapWindow( p_display, controls ); }
514 Widget w = XtWindowToWidget( p_display, parent );
516 XtAddEventHandler( w, ExposureMask, FALSE,
517 (XtEventHandler)Redraw, p_plugin );
518 XtAddEventHandler( w, StructureNotifyMask, FALSE,
519 (XtEventHandler)Resize, p_plugin );
520 XtAddEventHandler( w, ButtonReleaseMask, FALSE,
521 (XtEventHandler)ControlHandler, p_plugin );
523 /* remember window */
524 p_plugin->setWindow( *window );
525 p_plugin->setVideoWindow( video );
529 p_plugin->setControlWindow( controls );
532 Redraw( w, (XtPointer)p_plugin, NULL );
534 /* now display toolbar if asked through parameters */
535 if( p_plugin->b_toolbar )
537 p_plugin->showToolbar();
541 else if( curwin.window )
543 curwin.window = NULL;
547 if( !p_plugin->b_stream )
549 if( p_plugin->psz_target )
551 if( p_plugin->playlist_add( p_plugin->psz_target, NULL ) != -1 )
553 if( p_plugin->b_autoplay )
555 p_plugin->playlist_play(&ex);
556 libvlc_exception_clear(&ex);
559 p_plugin->b_stream = true;
562 return NPERR_NO_ERROR;
565 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
566 NPBool seekable, uint16 *stype )
568 if( NULL == instance )
570 return NPERR_INVALID_INSTANCE_ERROR;
573 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
574 if( NULL == p_plugin )
576 return NPERR_INVALID_INSTANCE_ERROR;
580 ** Firefox/Mozilla may decide to open a stream from the URL specified
581 ** in the SRC parameter of the EMBED tag and pass it to us
583 ** since VLC will open the SRC URL as well, we're not interested in
584 ** that stream. Otherwise, we'll take it and queue it up in the playlist
586 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
588 /* TODO: use pipes !!!! */
589 *stype = NP_ASFILEONLY;
590 return NPERR_NO_ERROR;
592 return NPERR_GENERIC_ERROR;
595 int32 NPP_WriteReady( NPP instance, NPStream *stream )
601 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
602 int32 len, void *buffer )
608 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
610 if( instance == NULL )
612 return NPERR_INVALID_INSTANCE_ERROR;
614 return NPERR_NO_ERROR;
617 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
619 if( instance == NULL )
624 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
625 if( NULL == p_plugin )
630 if( p_plugin->playlist_add( stream->url, NULL ) != -1 )
632 if( p_plugin->b_autoplay )
634 p_plugin->playlist_play(NULL);
639 void NPP_URLNotify( NPP instance, const char* url,
640 NPReason reason, void* notifyData )
642 /***** Insert NPP_URLNotify code here *****\
643 PluginInstance* p_plugin;
644 if (instance != NULL)
645 p_plugin = (PluginInstance*) instance->pdata;
646 \*********************************************/
649 void NPP_Print( NPP instance, NPPrint* printInfo )
651 if( printInfo == NULL )
656 if( instance != NULL )
658 /***** Insert NPP_Print code here *****\
659 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
660 \**************************************/
662 if( printInfo->mode == NP_FULL )
666 * If your plugin would like to take over
667 * printing completely when it is in full-screen mode,
668 * set printInfo->pluginPrinted to TRUE and print your
669 * plugin as you see fit. If your plugin wants Netscape
670 * to handle printing in this case, set
671 * printInfo->pluginPrinted to FALSE (the default) and
672 * do nothing. If you do want to handle printing
673 * yourself, printOne is true if the print button
674 * (as opposed to the print menu) was clicked.
675 * On the Macintosh, platformPrint is a THPrint; on
676 * Windows, platformPrint is a structure
677 * (defined in npapi.h) containing the printer name, port,
681 /***** Insert NPP_Print code here *****\
682 void* platformPrint =
683 printInfo->print.fullPrint.platformPrint;
685 printInfo->print.fullPrint.printOne;
686 \**************************************/
689 printInfo->print.fullPrint.pluginPrinted = FALSE;
693 /* If not fullscreen, we must be embedded */
696 * If your plugin is embedded, or is full-screen
697 * but you returned false in pluginPrinted above, NPP_Print
698 * will be called with mode == NP_EMBED. The NPWindow
699 * in the printInfo gives the location and dimensions of
700 * the embedded plugin on the printed page. On the
701 * Macintosh, platformPrint is the printer port; on
702 * Windows, platformPrint is the handle to the printing
706 /***** Insert NPP_Print code here *****\
707 NPWindow* printWindow =
708 &(printInfo->print.embedPrint.window);
709 void* platformPrint =
710 printInfo->print.embedPrint.platformPrint;
711 \**************************************/
716 /******************************************************************************
717 * Windows-only methods
718 *****************************************************************************/
720 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
722 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
731 PAINTSTRUCT paintstruct;
735 hdc = BeginPaint( p_hwnd, &paintstruct );
737 GetClientRect( p_hwnd, &rect );
739 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
740 SetTextColor(hdc, RGB(255, 255, 255));
741 SetBkColor(hdc, RGB(0, 0, 0));
742 if( p_plugin->psz_text )
743 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
744 DT_CENTER|DT_VCENTER|DT_SINGLELINE);
746 EndPaint( p_hwnd, &paintstruct );
750 /* delegate to default handler */
751 return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
757 /******************************************************************************
759 *****************************************************************************/
761 static void Redraw( Widget w, XtPointer closure, XEvent *event )
763 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
764 Window control = p_plugin->getControlWindow();
765 const NPWindow& window = p_plugin->getWindow();
768 unsigned int i_control_height, i_control_width;
770 if( p_plugin->b_toolbar )
771 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
773 i_control_height = i_control_width = 0;
775 Window video = p_plugin->getVideoWindow();
776 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
778 gcv.foreground = BlackPixel( p_display, 0 );
779 gc = XCreateGC( p_display, video, GCForeground, &gcv );
781 XFillRectangle( p_display, video, gc,
782 0, 0, window.width, window.height - i_control_height);
784 gcv.foreground = WhitePixel( p_display, 0 );
785 XChangeGC( p_display, gc, GCForeground, &gcv );
787 if( p_plugin->psz_text )
788 XDrawString( p_display, video, gc,
789 window.width / 2 - 40, (window.height - i_control_height) / 2,
790 p_plugin->psz_text, strlen(p_plugin->psz_text) );
791 XFreeGC( p_display, gc );
793 p_plugin->redrawToolbar();
796 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
798 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
799 const NPWindow& window = p_plugin->getWindow();
801 int i_height = window.height;
802 int i_width = window.width;
803 int i_xPos = event->xbutton.x;
804 int i_yPos = event->xbutton.y;
806 if( p_plugin && p_plugin->b_toolbar )
809 libvlc_exception_t ex;
811 libvlc_exception_init( &ex );
812 libvlc_media_player_t *p_md = p_plugin->getMD(&ex);
813 libvlc_exception_clear( &ex );
815 i_playing = p_plugin->playlist_isplaying();
817 vlc_toolbar_clicked_t clicked;
818 clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
825 p_plugin->playlist_pause( &ex );
827 p_plugin->playlist_play( &ex );
829 libvlc_exception_clear( &ex );
835 p_plugin->playlist_stop();
839 case clicked_Fullscreen:
841 p_plugin->set_fullscreen( 1, &ex );
842 libvlc_exception_clear( &ex );
849 libvlc_audio_toggle_mute( p_plugin->getVLC() );
853 case clicked_timeline:
855 /* if a movie is loaded */
859 f_length = libvlc_media_player_get_length( p_md, &ex ) / 100;
860 libvlc_exception_clear( &ex );
862 f_length = (float)f_length *
863 ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
865 libvlc_media_player_set_time( p_md, f_length, &ex );
866 libvlc_exception_clear( &ex );
873 /* Not implemented yet*/
877 default: /* button_Unknown */
881 Redraw( w, closure, event );
884 static void Resize ( Widget w, XtPointer closure, XEvent *event )
886 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
887 Window control = p_plugin->getControlWindow();
888 const NPWindow& window = p_plugin->getWindow();
889 Window drawable = p_plugin->getVideoWindow();
890 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
893 Window root_return, parent_return, * children_return;
895 unsigned int i_nchildren;
896 unsigned int i_control_height, i_control_width;
898 if( p_plugin->b_toolbar )
900 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
904 i_control_height = i_control_width = 0;
907 #ifdef X11_RESIZE_DEBUG
908 XWindowAttributes attr;
910 if( event && event->type == ConfigureNotify )
912 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
913 "send_event ? %s\n", event->xconfigure.width,
914 event->xconfigure.height,
915 event->xconfigure.send_event ? "TRUE" : "FALSE" );
917 #endif /* X11_RESIZE_DEBUG */
919 if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
921 /* size already set */
925 i_ret = XResizeWindow( p_display, drawable,
926 window.width, (window.height - i_control_height) );
928 #ifdef X11_RESIZE_DEBUG
930 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
932 XGetWindowAttributes ( p_display, drawable, &attr );
934 /* X is asynchronous, so the current size reported here is not
935 necessarily the requested size as the Resize request may not
936 yet have been handled by the plugin host */
937 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
938 attr.width, attr.height );
939 #endif /* X11_RESIZE_DEBUG */
941 XQueryTree( p_display, drawable,
942 &root_return, &parent_return, &children_return,
945 if( i_nchildren > 0 )
947 /* XXX: Make assumptions related to the window parenting structure in
948 vlc/modules/video_output/x11/xcommon.c */
949 base_window = children_return[i_nchildren - 1];
951 #ifdef X11_RESIZE_DEBUG
952 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
953 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
955 #endif /* X11_RESIZE_DEBUG */
957 i_ret = XResizeWindow( p_display, base_window,
958 window.width, ( window.height - i_control_height ) );
960 #ifdef X11_RESIZE_DEBUG
962 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
965 XGetWindowAttributes( p_display, base_window, &attr );
967 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
968 attr.width, attr.height );
969 #endif /* X11_RESIZE_DEBUG */