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 *****************************************************************************/
34 /* This is from mozilla java, do we really need it? */
39 #include "vlcplugin.h"
42 /* Enable/disable debugging printf's for X11 resizing */
43 #undef X11_RESIZE_DEBUG
45 /*****************************************************************************
46 * Unix-only declarations
47 ******************************************************************************/
50 static void Redraw( Widget w, XtPointer closure, XEvent *event );
51 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
52 static void Resize( Widget w, XtPointer closure, XEvent *event );
56 /*****************************************************************************
57 * MacOS-only declarations
58 ******************************************************************************/
62 /*****************************************************************************
63 * Windows-only declarations
64 *****************************************************************************/
67 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
71 /******************************************************************************
73 *****************************************************************************/
74 char * NPP_GetMIMEDescription( void )
76 static char mimetype[] = PLUGIN_MIMETYPES;
80 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
82 static char psz_name[] = PLUGIN_NAME;
83 static char psz_desc[1000];
85 /* plugin class variables */
88 case NPPVpluginNameString:
89 *((char **)value) = psz_name;
90 return NPERR_NO_ERROR;
92 case NPPVpluginDescriptionString:
93 snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
94 libvlc_get_version() );
95 *((char **)value) = psz_desc;
96 return NPERR_NO_ERROR;
99 /* move on to instance variables ... */
103 if( instance == NULL )
105 return NPERR_INVALID_INSTANCE_ERROR;
108 /* plugin instance variables */
110 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
111 if( NULL == p_plugin )
113 // plugin has not been initialized yet !
114 return NPERR_INVALID_INSTANCE_ERROR;
119 case NPPVpluginScriptableNPObject:
121 /* retrieve plugin root class */
122 NPClass *scriptClass = p_plugin->getScriptClass();
125 /* create an instance and return it */
126 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
127 return NPERR_NO_ERROR;
135 return NPERR_GENERIC_ERROR;
139 * there is some confusion in gecko headers regarding definition of this API
140 * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
143 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
145 return NPERR_GENERIC_ERROR;
148 /******************************************************************************
150 *****************************************************************************/
152 int16_t NPP_HandleEvent( NPP instance, void * event )
154 static UInt32 lastMouseUp = 0;
155 if( instance == NULL )
160 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
161 if( p_plugin == NULL )
166 EventRecord *myEvent = (EventRecord*)event;
168 switch( myEvent->what )
174 if( (myEvent->when - lastMouseUp) < GetDblTime() )
177 p_plugin->toggle_fullscreen();
182 lastMouseUp = myEvent->when;
190 const NPWindow& npwindow = p_plugin->getWindow();
191 if( npwindow.window )
193 bool hasVout = false;
195 if( p_plugin->playlist_isplaying() )
197 hasVout = p_plugin->player_has_vout();
201 libvlc_rectangle_t area;
204 area.right = npwindow.width;
205 area.bottom = npwindow.height;
206 libvlc_video_redraw_rectangle(p_plugin->getMD(), &area, NULL);
209 #warning disabled code
215 /* draw the beautiful "No Picture" */
217 ForeColor(blackColor);
220 /* seems that firefox forgets to set the following
221 * on occasion (reload) */
222 SetOrigin(((NP_Port *)npwindow.window)->portx,
223 ((NP_Port *)npwindow.window)->porty);
228 rect.right = npwindow.width;
229 rect.bottom = npwindow.height;
232 ForeColor(whiteColor);
233 MoveTo( (npwindow.width-80)/ 2 , npwindow.height / 2 );
234 if( p_plugin->psz_text )
235 DrawText( p_plugin->psz_text, 0, strlen(p_plugin->psz_text) );
242 case NPEventType_GetFocusEvent:
243 case NPEventType_LoseFocusEvent:
245 case NPEventType_AdjustCursorEvent:
247 case NPEventType_MenuCommandEvent:
249 case NPEventType_ClippingChangedEvent:
251 case NPEventType_ScrollingBeginsEvent:
253 case NPEventType_ScrollingEndsEvent:
260 #endif /* XP_MACOSX */
262 /******************************************************************************
263 * General Plug-in Calls
264 *****************************************************************************/
265 NPError NPP_Initialize( void )
267 return NPERR_NO_ERROR;
271 jref NPP_GetJavaClass( void )
277 void NPP_Shutdown( void )
282 NPError NPP_New( NPMIMEType pluginType, NPP instance,
283 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
284 uint16 mode, int16 argc,
286 uint16_t mode, int16_t argc,
288 char* argn[], char* argv[], NPSavedData* saved )
292 if( instance == NULL )
294 return NPERR_INVALID_INSTANCE_ERROR;
297 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
298 if( NULL == p_plugin )
300 return NPERR_OUT_OF_MEMORY_ERROR;
303 status = p_plugin->init(argc, argn, argv);
304 if( NPERR_NO_ERROR == status )
306 instance->pdata = reinterpret_cast<void*>(p_plugin);
308 NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
309 NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
319 NPError NPP_Destroy( NPP instance, NPSavedData** save )
321 if( NULL == instance )
322 return NPERR_INVALID_INSTANCE_ERROR;
324 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
325 if( NULL == p_plugin )
326 return NPERR_NO_ERROR;
328 instance->pdata = NULL;
331 HWND win = (HWND)p_plugin->getWindow().window;
332 WNDPROC winproc = p_plugin->getWindowProc();
336 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
340 if( p_plugin->playlist_isplaying() )
341 p_plugin->playlist_stop();
345 return NPERR_NO_ERROR;
348 NPError NPP_SetWindow( NPP instance, NPWindow* window )
352 unsigned int i_control_height = 0, i_control_width = 0;
357 return NPERR_INVALID_INSTANCE_ERROR;
360 /* NPP_SetWindow may be called before NPP_New (Opera) */
361 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
362 if( NULL == p_plugin )
364 /* we should probably show a splash screen here */
365 return NPERR_NO_ERROR;
369 control = p_plugin->getControlWindow();
372 libvlc_instance_t *p_vlc = p_plugin->getVLC();
376 * Before setting window to point to the
377 * new window, you may wish to compare the new window
378 * info to the previous window (if any) to note window
382 /* retrieve current window */
383 NPWindow& curwin = p_plugin->getWindow();
386 if( window && window->window )
388 /* check if plugin has a new parent window */
389 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
391 /* as MacOS X video output is windowless, set viewport */
392 libvlc_rectangle_t view, clip;
395 ** browser sets port origin to top-left location of plugin
396 ** relative to GrafPort window origin is set relative to document,
397 ** which of little use for drawing
399 view.top = ((NP_Port*) (window->window))->porty;
400 view.left = ((NP_Port*) (window->window))->portx;
401 view.bottom = window->height+view.top;
402 view.right = window->width+view.left;
404 /* clipRect coordinates are also relative to GrafPort */
405 clip.top = window->clipRect.top;
406 clip.left = window->clipRect.left;
407 clip.bottom = window->clipRect.bottom;
408 clip.right = window->clipRect.right;
410 libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
412 #warning disabled code
414 /* remember new window */
415 p_plugin->setWindow(*window);
417 else if( curwin.window )
419 /* change/set parent */
420 curwin.window = NULL;
422 #endif /* XP_MACOSX */
425 if( window && window->window )
427 /* check if plugin has a new parent window */
428 HWND drawable = (HWND) (window->window);
429 if( !curwin.window || drawable != curwin.window )
431 /* reset previous window settings */
432 HWND oldwin = (HWND)p_plugin->getWindow().window;
433 WNDPROC oldproc = p_plugin->getWindowProc();
437 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
439 /* attach our plugin object */
440 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
441 reinterpret_cast<LONG_PTR>(p_plugin));
443 /* install our WNDPROC */
444 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
445 GWL_WNDPROC, (LONG)Manage ) );
447 /* change window style to our liking */
448 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
449 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
450 SetWindowLong((HWND)drawable, GWL_STYLE, style);
452 /* remember new window */
453 p_plugin->setWindow(*window);
456 InvalidateRect( (HWND)drawable, NULL, TRUE );
457 UpdateWindow( (HWND)drawable );
460 else if( curwin.window )
463 HWND oldwin = (HWND)curwin.window;
464 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
465 p_plugin->setWindowProc(NULL);
467 curwin.window = NULL;
472 /* default to hidden toolbar, shown at the end of this method if asked *
473 * developers note : getToolbarSize need to wait the end of this method
475 i_control_height = 0;
476 i_control_width = window->width;
478 if( window && window->window )
480 Window parent = (Window) window->window;
481 if( !curwin.window || (parent != (Window)curwin.window) )
483 Display *p_display = ( (NPSetWindowCallbackStruct *)
484 window->ws_info )->display;
486 XResizeWindow( p_display, parent, window->width, window->height );
488 int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
491 Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
492 window->width, window->height - i_control_height,
493 0, i_blackColor, i_blackColor );
494 Window controls = (Window) NULL;
495 controls = XCreateSimpleWindow( p_display, parent,
496 0, window->height - i_control_height-1,
497 window->width, i_control_height-1,
498 0, i_blackColor, i_blackColor );
500 XMapWindow( p_display, parent );
501 XMapWindow( p_display, video );
502 if( controls ) { XMapWindow( p_display, controls ); }
507 Widget w = XtWindowToWidget( p_display, parent );
509 XtAddEventHandler( w, ExposureMask, FALSE,
510 (XtEventHandler)Redraw, p_plugin );
511 XtAddEventHandler( w, StructureNotifyMask, FALSE,
512 (XtEventHandler)Resize, p_plugin );
513 XtAddEventHandler( w, ButtonReleaseMask, FALSE,
514 (XtEventHandler)ControlHandler, p_plugin );
516 /* remember window */
517 p_plugin->setWindow( *window );
518 p_plugin->setVideoWindow( video );
522 p_plugin->setControlWindow( controls );
525 Redraw( w, (XtPointer)p_plugin, NULL );
527 /* now display toolbar if asked through parameters */
528 if( p_plugin->b_toolbar )
530 p_plugin->showToolbar();
534 else if( curwin.window )
536 curwin.window = NULL;
540 if( !p_plugin->b_stream )
542 if( p_plugin->psz_target )
544 if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
546 if( p_plugin->b_autoplay )
548 p_plugin->playlist_play();
551 p_plugin->b_stream = true;
554 return NPERR_NO_ERROR;
557 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
558 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
559 NPBool seekable, uint16 *stype )
561 NPBool seekable, uint16_t *stype )
564 if( NULL == instance )
566 return NPERR_INVALID_INSTANCE_ERROR;
569 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
570 if( NULL == p_plugin )
572 return NPERR_INVALID_INSTANCE_ERROR;
576 ** Firefox/Mozilla may decide to open a stream from the URL specified
577 ** in the SRC parameter of the EMBED tag and pass it to us
579 ** since VLC will open the SRC URL as well, we're not interested in
580 ** that stream. Otherwise, we'll take it and queue it up in the playlist
582 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
584 /* TODO: use pipes !!!! */
585 *stype = NP_ASFILEONLY;
586 return NPERR_NO_ERROR;
588 return NPERR_GENERIC_ERROR;
591 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
592 int32 NPP_WriteReady( NPP instance, NPStream *stream )
594 int32_t NPP_WriteReady( NPP instance, NPStream *stream )
601 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
602 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
603 int32 len, void *buffer )
605 int32_t NPP_Write( NPP instance, NPStream *stream, int32_t offset,
606 int32_t len, void *buffer )
613 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
615 if( instance == NULL )
617 return NPERR_INVALID_INSTANCE_ERROR;
619 return NPERR_NO_ERROR;
622 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
624 if( instance == NULL )
629 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
630 if( NULL == p_plugin )
635 if( p_plugin->playlist_add( stream->url ) != -1 )
637 if( p_plugin->b_autoplay )
639 p_plugin->playlist_play();
644 void NPP_URLNotify( NPP instance, const char* url,
645 NPReason reason, void* notifyData )
647 /***** Insert NPP_URLNotify code here *****\
648 PluginInstance* p_plugin;
649 if (instance != NULL)
650 p_plugin = (PluginInstance*) instance->pdata;
651 \*********************************************/
654 void NPP_Print( NPP instance, NPPrint* printInfo )
656 if( printInfo == NULL )
661 if( instance != NULL )
663 /***** Insert NPP_Print code here *****\
664 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
665 \**************************************/
667 if( printInfo->mode == NP_FULL )
671 * If your plugin would like to take over
672 * printing completely when it is in full-screen mode,
673 * set printInfo->pluginPrinted to TRUE and print your
674 * plugin as you see fit. If your plugin wants Netscape
675 * to handle printing in this case, set
676 * printInfo->pluginPrinted to FALSE (the default) and
677 * do nothing. If you do want to handle printing
678 * yourself, printOne is true if the print button
679 * (as opposed to the print menu) was clicked.
680 * On the Macintosh, platformPrint is a THPrint; on
681 * Windows, platformPrint is a structure
682 * (defined in npapi.h) containing the printer name, port,
686 /***** Insert NPP_Print code here *****\
687 void* platformPrint =
688 printInfo->print.fullPrint.platformPrint;
690 printInfo->print.fullPrint.printOne;
691 \**************************************/
694 printInfo->print.fullPrint.pluginPrinted = FALSE;
698 /* If not fullscreen, we must be embedded */
701 * If your plugin is embedded, or is full-screen
702 * but you returned false in pluginPrinted above, NPP_Print
703 * will be called with mode == NP_EMBED. The NPWindow
704 * in the printInfo gives the location and dimensions of
705 * the embedded plugin on the printed page. On the
706 * Macintosh, platformPrint is the printer port; on
707 * Windows, platformPrint is the handle to the printing
711 /***** Insert NPP_Print code here *****\
712 NPWindow* printWindow =
713 &(printInfo->print.embedPrint.window);
714 void* platformPrint =
715 printInfo->print.embedPrint.platformPrint;
716 \**************************************/
721 /******************************************************************************
722 * Windows-only methods
723 *****************************************************************************/
725 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
727 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
736 PAINTSTRUCT paintstruct;
740 hdc = BeginPaint( p_hwnd, &paintstruct );
742 GetClientRect( p_hwnd, &rect );
744 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
745 SetTextColor(hdc, RGB(255, 255, 255));
746 SetBkColor(hdc, RGB(0, 0, 0));
747 if( p_plugin->psz_text )
748 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
749 DT_CENTER|DT_VCENTER|DT_SINGLELINE);
751 EndPaint( p_hwnd, &paintstruct );
755 /* delegate to default handler */
756 return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
762 /******************************************************************************
764 *****************************************************************************/
766 static void Redraw( Widget w, XtPointer closure, XEvent *event )
768 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
769 Window control = p_plugin->getControlWindow();
770 const NPWindow& window = p_plugin->getWindow();
773 unsigned int i_control_height, i_control_width;
775 if( p_plugin->b_toolbar )
776 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
778 i_control_height = i_control_width = 0;
780 Window video = p_plugin->getVideoWindow();
781 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
783 gcv.foreground = BlackPixel( p_display, 0 );
784 gc = XCreateGC( p_display, video, GCForeground, &gcv );
786 XFillRectangle( p_display, video, gc,
787 0, 0, window.width, window.height - i_control_height);
789 gcv.foreground = WhitePixel( p_display, 0 );
790 XChangeGC( p_display, gc, GCForeground, &gcv );
792 if( p_plugin->psz_text )
793 XDrawString( p_display, video, gc,
794 window.width / 2 - 40, (window.height - i_control_height) / 2,
795 p_plugin->psz_text, strlen(p_plugin->psz_text) );
796 XFreeGC( p_display, gc );
798 p_plugin->redrawToolbar();
801 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
803 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
804 const NPWindow& window = p_plugin->getWindow();
806 int i_height = window.height;
807 int i_width = window.width;
808 int i_xPos = event->xbutton.x;
809 int i_yPos = event->xbutton.y;
811 if( p_plugin && p_plugin->b_toolbar )
815 libvlc_media_player_t *p_md = p_plugin->getMD();
817 i_playing = p_plugin->playlist_isplaying();
819 vlc_toolbar_clicked_t clicked;
820 clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
827 p_plugin->playlist_pause();
829 p_plugin->playlist_play();
835 p_plugin->playlist_stop();
839 case clicked_Fullscreen:
841 p_plugin->set_fullscreen( 1 );
849 libvlc_audio_toggle_mute( p_md );
853 case clicked_timeline:
855 /* if a movie is loaded */
859 f_length = libvlc_media_player_get_length( p_md ) / 100;
861 f_length = (float)f_length *
862 ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
864 libvlc_media_player_set_time( p_md, f_length );
871 /* Not implemented yet*/
875 default: /* button_Unknown */
879 Redraw( w, closure, event );
882 static void Resize ( Widget w, XtPointer closure, XEvent *event )
884 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
885 Window control = p_plugin->getControlWindow();
886 const NPWindow& window = p_plugin->getWindow();
887 Window drawable = p_plugin->getVideoWindow();
888 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
891 Window root_return, parent_return, * children_return;
893 unsigned int i_nchildren;
894 unsigned int i_control_height, i_control_width;
896 if( p_plugin->b_toolbar )
898 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
902 i_control_height = i_control_width = 0;
905 #ifdef X11_RESIZE_DEBUG
906 XWindowAttributes attr;
908 if( event && event->type == ConfigureNotify )
910 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
911 "send_event ? %s\n", event->xconfigure.width,
912 event->xconfigure.height,
913 event->xconfigure.send_event ? "TRUE" : "FALSE" );
915 #endif /* X11_RESIZE_DEBUG */
917 if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
919 /* size already set */
923 i_ret = XResizeWindow( p_display, drawable,
924 window.width, (window.height - i_control_height) );
926 #ifdef X11_RESIZE_DEBUG
928 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
930 XGetWindowAttributes ( p_display, drawable, &attr );
932 /* X is asynchronous, so the current size reported here is not
933 necessarily the requested size as the Resize request may not
934 yet have been handled by the plugin host */
935 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
936 attr.width, attr.height );
937 #endif /* X11_RESIZE_DEBUG */
939 XQueryTree( p_display, drawable,
940 &root_return, &parent_return, &children_return,
943 if( i_nchildren > 0 )
945 /* XXX: Make assumptions related to the window parenting structure in
946 vlc/modules/video_output/x11/xcommon.c */
947 base_window = children_return[i_nchildren - 1];
949 #ifdef X11_RESIZE_DEBUG
950 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
951 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
953 #endif /* X11_RESIZE_DEBUG */
955 i_ret = XResizeWindow( p_display, base_window,
956 window.width, ( window.height - i_control_height ) );
958 #ifdef X11_RESIZE_DEBUG
960 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
963 XGetWindowAttributes( p_display, base_window, &attr );
965 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
966 attr.width, attr.height );
967 #endif /* X11_RESIZE_DEBUG */