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 #include "vlcplugin.h"
37 /* Enable/disable debugging printf's for X11 resizing */
38 #undef X11_RESIZE_DEBUG
40 /*****************************************************************************
41 * Unix-only declarations
42 ******************************************************************************/
45 static void Redraw( Widget w, XtPointer closure, XEvent *event );
46 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
47 static void Resize( Widget w, XtPointer closure, XEvent *event );
51 /*****************************************************************************
52 * MacOS-only declarations
53 ******************************************************************************/
57 /*****************************************************************************
58 * Windows-only declarations
59 *****************************************************************************/
62 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
66 /******************************************************************************
68 *****************************************************************************/
69 char * NPP_GetMIMEDescription( void )
71 static char mimetype[] = PLUGIN_MIMETYPES;
75 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
77 static char psz_name[] = PLUGIN_NAME;
78 static char psz_desc[1000];
80 /* plugin class variables */
83 case NPPVpluginNameString:
84 *((char **)value) = psz_name;
85 return NPERR_NO_ERROR;
87 case NPPVpluginDescriptionString:
88 snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
89 libvlc_get_version() );
90 *((char **)value) = psz_desc;
91 return NPERR_NO_ERROR;
94 /* move on to instance variables ... */
98 if( instance == NULL )
100 return NPERR_INVALID_INSTANCE_ERROR;
103 /* plugin instance variables */
105 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
106 if( NULL == p_plugin )
108 /* plugin has not been initialized yet ! */
109 return NPERR_INVALID_INSTANCE_ERROR;
114 case NPPVpluginScriptableNPObject:
116 /* retrieve plugin root class */
117 NPClass *scriptClass = p_plugin->getScriptClass();
120 /* create an instance and return it */
121 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
122 return NPERR_NO_ERROR;
130 return NPERR_GENERIC_ERROR;
134 * there is some confusion in gecko headers regarding definition of this API
135 * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
138 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
140 return NPERR_GENERIC_ERROR;
143 /******************************************************************************
145 *****************************************************************************/
147 int16_t NPP_HandleEvent( NPP instance, void * event )
149 static UInt32 lastMouseUp = 0;
150 if( instance == NULL )
155 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
156 if( p_plugin == NULL )
162 EventRecord *myEvent = (EventRecord*)event;
164 switch( myEvent->what )
170 if( (myEvent->when - lastMouseUp) < GetDblTime() )
173 p_plugin->toggle_fullscreen();
178 lastMouseUp = myEvent->when;
186 const NPWindow& npwindow = p_plugin->getWindow();
187 if( npwindow.window )
189 bool hasVout = false;
191 if( p_plugin->playlist_isplaying() )
193 hasVout = p_plugin->player_has_vout();
197 libvlc_rectangle_t area;
200 area.right = npwindow.width;
201 area.bottom = npwindow.height;
202 libvlc_video_redraw_rectangle(p_plugin->getMD(), &area, NULL);
205 #warning disabled code
211 /* draw the text from p_plugin->psz_text */
212 ForeColor(blackColor);
215 /* seems that firefox forgets to set the following
216 * on occasion (reload) */
217 SetOrigin(((NP_Port *)npwindow.window)->portx,
218 ((NP_Port *)npwindow.window)->porty);
223 rect.right = npwindow.width;
224 rect.bottom = npwindow.height;
227 ForeColor(whiteColor);
228 MoveTo( (npwindow.width-80)/ 2 , npwindow.height / 2 );
229 if( p_plugin->psz_text )
230 DrawText( p_plugin->psz_text, 0, strlen(p_plugin->psz_text) );
237 case NPEventType_GetFocusEvent:
238 case NPEventType_LoseFocusEvent:
240 case NPEventType_AdjustCursorEvent:
242 case NPEventType_MenuCommandEvent:
244 case NPEventType_ClippingChangedEvent:
246 case NPEventType_ScrollingBeginsEvent:
248 case NPEventType_ScrollingEndsEvent:
256 #endif /* XP_MACOSX */
258 /******************************************************************************
259 * General Plug-in Calls
260 *****************************************************************************/
261 NPError NPP_Initialize( void )
263 return NPERR_NO_ERROR;
267 jref NPP_GetJavaClass( void )
273 void NPP_Shutdown( void )
278 NPError NPP_New( NPMIMEType pluginType, NPP instance,
279 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
280 uint16 mode, int16 argc,
282 uint16_t mode, int16_t argc,
284 char* argn[], char* argv[], NPSavedData* saved )
288 if( instance == NULL )
290 return NPERR_INVALID_INSTANCE_ERROR;
293 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
294 if( NULL == p_plugin )
296 return NPERR_OUT_OF_MEMORY_ERROR;
299 status = p_plugin->init(argc, argn, argv);
300 if( NPERR_NO_ERROR == status )
302 instance->pdata = reinterpret_cast<void*>(p_plugin);
304 NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
305 NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
315 NPError NPP_Destroy( NPP instance, NPSavedData** save )
317 if( NULL == instance )
318 return NPERR_INVALID_INSTANCE_ERROR;
320 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
321 if( NULL == p_plugin )
322 return NPERR_NO_ERROR;
324 instance->pdata = NULL;
327 HWND win = (HWND)p_plugin->getWindow().window;
328 WNDPROC winproc = p_plugin->getWindowProc();
332 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
336 if( p_plugin->playlist_isplaying() )
337 p_plugin->playlist_stop();
341 return NPERR_NO_ERROR;
344 NPError NPP_SetWindow( NPP instance, NPWindow* window )
348 unsigned int i_control_height = 0, i_control_width = 0;
353 return NPERR_INVALID_INSTANCE_ERROR;
356 /* NPP_SetWindow may be called before NPP_New (Opera) */
357 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
358 if( NULL == p_plugin )
360 /* we should probably show a splash screen here */
361 return NPERR_NO_ERROR;
365 control = p_plugin->getControlWindow();
368 libvlc_instance_t *p_vlc = p_plugin->getVLC();
372 * Before setting window to point to the
373 * new window, you may wish to compare the new window
374 * info to the previous window (if any) to note window
378 /* retrieve current window */
379 NPWindow& curwin = p_plugin->getWindow();
382 if( window && window->window )
384 /* check if plugin has a new parent window */
385 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
387 /* as MacOS X video output is windowless, set viewport */
388 libvlc_rectangle_t view, clip;
391 ** browser sets port origin to top-left location of plugin
392 ** relative to GrafPort window origin is set relative to document,
393 ** which of little use for drawing
395 view.top = ((NP_Port*) (window->window))->porty;
396 view.left = ((NP_Port*) (window->window))->portx;
397 view.bottom = window->height+view.top;
398 view.right = window->width+view.left;
400 /* clipRect coordinates are also relative to GrafPort */
401 clip.top = window->clipRect.top;
402 clip.left = window->clipRect.left;
403 clip.bottom = window->clipRect.bottom;
404 clip.right = window->clipRect.right;
406 libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
408 #warning disabled code
410 /* remember new window */
411 p_plugin->setWindow(*window);
413 else if( curwin.window )
415 /* change/set parent */
416 curwin.window = NULL;
418 #endif /* XP_MACOSX */
421 if( window && window->window )
423 /* check if plugin has a new parent window */
424 HWND drawable = (HWND) (window->window);
425 if( !curwin.window || drawable != curwin.window )
427 /* reset previous window settings */
428 HWND oldwin = (HWND)p_plugin->getWindow().window;
429 WNDPROC oldproc = p_plugin->getWindowProc();
433 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
435 /* attach our plugin object */
436 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
437 reinterpret_cast<LONG_PTR>(p_plugin));
439 /* install our WNDPROC */
440 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
441 GWL_WNDPROC, (LONG)Manage ) );
443 /* change window style to our liking */
444 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
445 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
446 SetWindowLong((HWND)drawable, GWL_STYLE, style);
448 /* remember new window */
449 p_plugin->setWindow(*window);
452 InvalidateRect( (HWND)drawable, NULL, TRUE );
453 UpdateWindow( (HWND)drawable );
456 else if( curwin.window )
459 HWND oldwin = (HWND)curwin.window;
460 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
461 p_plugin->setWindowProc(NULL);
463 curwin.window = NULL;
468 /* default to hidden toolbar, shown at the end of this method if asked *
469 * developers note : getToolbarSize need to wait the end of this method
471 i_control_height = 0;
472 i_control_width = window->width;
474 if( window && window->window )
476 Window parent = (Window) window->window;
477 if( !curwin.window || (parent != (Window)curwin.window) )
479 Display *p_display = ( (NPSetWindowCallbackStruct *)
480 window->ws_info )->display;
482 XResizeWindow( p_display, parent, window->width, window->height );
484 int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
487 Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
488 window->width, window->height - i_control_height,
489 0, i_blackColor, i_blackColor );
490 Window controls = (Window) NULL;
491 controls = XCreateSimpleWindow( p_display, parent,
492 0, window->height - i_control_height-1,
493 window->width, i_control_height-1,
494 0, i_blackColor, i_blackColor );
496 XMapWindow( p_display, parent );
497 XMapWindow( p_display, video );
498 if( controls ) { XMapWindow( p_display, controls ); }
503 Widget w = XtWindowToWidget( p_display, parent );
505 XtAddEventHandler( w, ExposureMask, FALSE,
506 (XtEventHandler)Redraw, p_plugin );
507 XtAddEventHandler( w, StructureNotifyMask, FALSE,
508 (XtEventHandler)Resize, p_plugin );
509 XtAddEventHandler( w, ButtonReleaseMask, FALSE,
510 (XtEventHandler)ControlHandler, p_plugin );
512 /* remember window */
513 p_plugin->setWindow( *window );
514 p_plugin->setVideoWindow( video );
518 p_plugin->setControlWindow( controls );
521 Redraw( w, (XtPointer)p_plugin, NULL );
523 /* now display toolbar if asked through parameters */
524 if( p_plugin->b_toolbar )
526 p_plugin->showToolbar();
530 else if( curwin.window )
532 curwin.window = NULL;
536 if( !p_plugin->b_stream )
538 if( p_plugin->psz_target )
540 if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
542 if( p_plugin->b_autoplay )
544 p_plugin->playlist_play();
547 p_plugin->b_stream = true;
550 return NPERR_NO_ERROR;
553 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
554 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
555 NPBool seekable, uint16 *stype )
557 NPBool seekable, uint16_t *stype )
560 if( NULL == instance )
562 return NPERR_INVALID_INSTANCE_ERROR;
565 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
566 if( NULL == p_plugin )
568 return NPERR_INVALID_INSTANCE_ERROR;
572 ** Firefox/Mozilla may decide to open a stream from the URL specified
573 ** in the SRC parameter of the EMBED tag and pass it to us
575 ** since VLC will open the SRC URL as well, we're not interested in
576 ** that stream. Otherwise, we'll take it and queue it up in the playlist
578 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
580 /* TODO: use pipes !!!! */
581 *stype = NP_ASFILEONLY;
582 return NPERR_NO_ERROR;
584 return NPERR_GENERIC_ERROR;
587 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
588 int32 NPP_WriteReady( NPP instance, NPStream *stream )
590 int32_t NPP_WriteReady( NPP instance, NPStream *stream )
597 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
598 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
599 int32 len, void *buffer )
601 int32_t NPP_Write( NPP instance, NPStream *stream, int32_t offset,
602 int32_t len, void *buffer )
609 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
611 if( instance == NULL )
613 return NPERR_INVALID_INSTANCE_ERROR;
615 return NPERR_NO_ERROR;
618 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
620 if( instance == NULL )
625 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
626 if( NULL == p_plugin )
631 if( p_plugin->playlist_add( stream->url ) != -1 )
633 if( p_plugin->b_autoplay )
635 p_plugin->playlist_play();
640 void NPP_URLNotify( NPP instance, const char* url,
641 NPReason reason, void* notifyData )
643 /***** Insert NPP_URLNotify code here *****\
644 PluginInstance* p_plugin;
645 if (instance != NULL)
646 p_plugin = (PluginInstance*) instance->pdata;
647 \*********************************************/
650 void NPP_Print( NPP instance, NPPrint* printInfo )
652 if( printInfo == NULL )
657 if( instance != NULL )
659 /***** Insert NPP_Print code here *****\
660 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
661 \**************************************/
663 if( printInfo->mode == NP_FULL )
667 * If your plugin would like to take over
668 * printing completely when it is in full-screen mode,
669 * set printInfo->pluginPrinted to TRUE and print your
670 * plugin as you see fit. If your plugin wants Netscape
671 * to handle printing in this case, set
672 * printInfo->pluginPrinted to FALSE (the default) and
673 * do nothing. If you do want to handle printing
674 * yourself, printOne is true if the print button
675 * (as opposed to the print menu) was clicked.
676 * On the Macintosh, platformPrint is a THPrint; on
677 * Windows, platformPrint is a structure
678 * (defined in npapi.h) containing the printer name, port,
682 /***** Insert NPP_Print code here *****\
683 void* platformPrint =
684 printInfo->print.fullPrint.platformPrint;
686 printInfo->print.fullPrint.printOne;
687 \**************************************/
690 printInfo->print.fullPrint.pluginPrinted = FALSE;
694 /* If not fullscreen, we must be embedded */
697 * If your plugin is embedded, or is full-screen
698 * but you returned false in pluginPrinted above, NPP_Print
699 * will be called with mode == NP_EMBED. The NPWindow
700 * in the printInfo gives the location and dimensions of
701 * the embedded plugin on the printed page. On the
702 * Macintosh, platformPrint is the printer port; on
703 * Windows, platformPrint is the handle to the printing
707 /***** Insert NPP_Print code here *****\
708 NPWindow* printWindow =
709 &(printInfo->print.embedPrint.window);
710 void* platformPrint =
711 printInfo->print.embedPrint.platformPrint;
712 \**************************************/
717 /******************************************************************************
718 * Windows-only methods
719 *****************************************************************************/
721 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
723 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
732 PAINTSTRUCT paintstruct;
736 hdc = BeginPaint( p_hwnd, &paintstruct );
738 GetClientRect( p_hwnd, &rect );
740 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
741 SetTextColor(hdc, RGB(255, 255, 255));
742 SetBkColor(hdc, RGB(0, 0, 0));
743 if( p_plugin->psz_text )
744 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
745 DT_CENTER|DT_VCENTER|DT_SINGLELINE);
747 EndPaint( p_hwnd, &paintstruct );
751 /* delegate to default handler */
752 return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
758 /******************************************************************************
760 *****************************************************************************/
762 static void Redraw( Widget w, XtPointer closure, XEvent *event )
764 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
765 Window control = p_plugin->getControlWindow();
766 const NPWindow& window = p_plugin->getWindow();
769 unsigned int i_control_height, i_control_width;
771 if( p_plugin->b_toolbar )
772 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
774 i_control_height = i_control_width = 0;
776 Window video = p_plugin->getVideoWindow();
777 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
779 gcv.foreground = BlackPixel( p_display, 0 );
780 gc = XCreateGC( p_display, video, GCForeground, &gcv );
782 XFillRectangle( p_display, video, gc,
783 0, 0, window.width, window.height - i_control_height);
785 gcv.foreground = WhitePixel( p_display, 0 );
786 XChangeGC( p_display, gc, GCForeground, &gcv );
788 if( p_plugin->psz_text )
789 XDrawString( p_display, video, gc,
790 window.width / 2 - 40, (window.height - i_control_height) / 2,
791 p_plugin->psz_text, strlen(p_plugin->psz_text) );
792 XFreeGC( p_display, gc );
794 p_plugin->redrawToolbar();
797 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
799 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
800 const NPWindow& window = p_plugin->getWindow();
802 int i_height = window.height;
803 int i_width = window.width;
804 int i_xPos = event->xbutton.x;
805 int i_yPos = event->xbutton.y;
807 if( p_plugin && p_plugin->b_toolbar )
811 libvlc_media_player_t *p_md = p_plugin->getMD();
813 i_playing = p_plugin->playlist_isplaying();
815 vlc_toolbar_clicked_t clicked;
816 clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
823 p_plugin->playlist_pause();
825 p_plugin->playlist_play();
831 p_plugin->playlist_stop();
835 case clicked_Fullscreen:
837 p_plugin->set_fullscreen( 1 );
845 libvlc_audio_toggle_mute( p_md );
849 case clicked_timeline:
851 /* if a movie is loaded */
855 f_length = libvlc_media_player_get_length( p_md ) / 100;
857 f_length = (float)f_length *
858 ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
860 libvlc_media_player_set_time( p_md, f_length );
867 /* Not implemented yet*/
871 default: /* button_Unknown */
875 Redraw( w, closure, event );
878 static void Resize ( Widget w, XtPointer closure, XEvent *event )
880 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
881 Window control = p_plugin->getControlWindow();
882 const NPWindow& window = p_plugin->getWindow();
883 Window drawable = p_plugin->getVideoWindow();
884 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
887 Window root_return, parent_return, * children_return;
889 unsigned int i_nchildren;
890 unsigned int i_control_height, i_control_width;
892 if( p_plugin->b_toolbar )
894 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
898 i_control_height = i_control_width = 0;
901 #ifdef X11_RESIZE_DEBUG
902 XWindowAttributes attr;
904 if( event && event->type == ConfigureNotify )
906 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
907 "send_event ? %s\n", event->xconfigure.width,
908 event->xconfigure.height,
909 event->xconfigure.send_event ? "TRUE" : "FALSE" );
911 #endif /* X11_RESIZE_DEBUG */
913 if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
915 /* size already set */
919 i_ret = XResizeWindow( p_display, drawable,
920 window.width, (window.height - i_control_height) );
922 #ifdef X11_RESIZE_DEBUG
924 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
926 XGetWindowAttributes ( p_display, drawable, &attr );
928 /* X is asynchronous, so the current size reported here is not
929 necessarily the requested size as the Resize request may not
930 yet have been handled by the plugin host */
931 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
932 attr.width, attr.height );
933 #endif /* X11_RESIZE_DEBUG */
935 XQueryTree( p_display, drawable,
936 &root_return, &parent_return, &children_return,
939 if( i_nchildren > 0 )
941 /* XXX: Make assumptions related to the window parenting structure in
942 vlc/modules/video_output/x11/xcommon.c */
943 base_window = children_return[i_nchildren - 1];
945 #ifdef X11_RESIZE_DEBUG
946 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
947 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
949 #endif /* X11_RESIZE_DEBUG */
951 i_ret = XResizeWindow( p_display, base_window,
952 window.width, ( window.height - i_control_height ) );
954 #ifdef X11_RESIZE_DEBUG
956 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
959 XGetWindowAttributes( p_display, base_window, &attr );
961 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
962 attr.width, attr.height );
963 #endif /* X11_RESIZE_DEBUG */