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 )
167 EventRecord *myEvent = (EventRecord*)event;
169 switch( myEvent->what )
175 if( (myEvent->when - lastMouseUp) < GetDblTime() )
178 p_plugin->toggle_fullscreen();
183 lastMouseUp = myEvent->when;
191 const NPWindow& npwindow = p_plugin->getWindow();
192 if( npwindow.window )
194 bool hasVout = false;
196 if( p_plugin->playlist_isplaying() )
198 hasVout = p_plugin->player_has_vout();
202 libvlc_rectangle_t area;
205 area.right = npwindow.width;
206 area.bottom = npwindow.height;
207 libvlc_video_redraw_rectangle(p_plugin->getMD(), &area, NULL);
210 #warning disabled code
216 /* draw the beautiful "No Picture" */
218 ForeColor(blackColor);
221 /* seems that firefox forgets to set the following
222 * on occasion (reload) */
223 SetOrigin(((NP_Port *)npwindow.window)->portx,
224 ((NP_Port *)npwindow.window)->porty);
229 rect.right = npwindow.width;
230 rect.bottom = npwindow.height;
233 ForeColor(whiteColor);
234 MoveTo( (npwindow.width-80)/ 2 , npwindow.height / 2 );
235 if( p_plugin->psz_text )
236 DrawText( p_plugin->psz_text, 0, strlen(p_plugin->psz_text) );
243 case NPEventType_GetFocusEvent:
244 case NPEventType_LoseFocusEvent:
246 case NPEventType_AdjustCursorEvent:
248 case NPEventType_MenuCommandEvent:
250 case NPEventType_ClippingChangedEvent:
252 case NPEventType_ScrollingBeginsEvent:
254 case NPEventType_ScrollingEndsEvent:
262 #endif /* XP_MACOSX */
264 /******************************************************************************
265 * General Plug-in Calls
266 *****************************************************************************/
267 NPError NPP_Initialize( void )
269 return NPERR_NO_ERROR;
273 jref NPP_GetJavaClass( void )
279 void NPP_Shutdown( void )
284 NPError NPP_New( NPMIMEType pluginType, NPP instance,
285 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
286 uint16 mode, int16 argc,
288 uint16_t mode, int16_t argc,
290 char* argn[], char* argv[], NPSavedData* saved )
294 if( instance == NULL )
296 return NPERR_INVALID_INSTANCE_ERROR;
299 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
300 if( NULL == p_plugin )
302 return NPERR_OUT_OF_MEMORY_ERROR;
305 status = p_plugin->init(argc, argn, argv);
306 if( NPERR_NO_ERROR == status )
308 instance->pdata = reinterpret_cast<void*>(p_plugin);
310 NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
311 NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
321 NPError NPP_Destroy( NPP instance, NPSavedData** save )
323 if( NULL == instance )
324 return NPERR_INVALID_INSTANCE_ERROR;
326 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
327 if( NULL == p_plugin )
328 return NPERR_NO_ERROR;
330 instance->pdata = NULL;
333 HWND win = (HWND)p_plugin->getWindow().window;
334 WNDPROC winproc = p_plugin->getWindowProc();
338 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
342 if( p_plugin->playlist_isplaying() )
343 p_plugin->playlist_stop();
347 return NPERR_NO_ERROR;
350 NPError NPP_SetWindow( NPP instance, NPWindow* window )
354 unsigned int i_control_height = 0, i_control_width = 0;
359 return NPERR_INVALID_INSTANCE_ERROR;
362 /* NPP_SetWindow may be called before NPP_New (Opera) */
363 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
364 if( NULL == p_plugin )
366 /* we should probably show a splash screen here */
367 return NPERR_NO_ERROR;
371 control = p_plugin->getControlWindow();
374 libvlc_instance_t *p_vlc = p_plugin->getVLC();
378 * Before setting window to point to the
379 * new window, you may wish to compare the new window
380 * info to the previous window (if any) to note window
384 /* retrieve current window */
385 NPWindow& curwin = p_plugin->getWindow();
388 if( window && window->window )
390 /* check if plugin has a new parent window */
391 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
393 /* as MacOS X video output is windowless, set viewport */
394 libvlc_rectangle_t view, clip;
397 ** browser sets port origin to top-left location of plugin
398 ** relative to GrafPort window origin is set relative to document,
399 ** which of little use for drawing
401 view.top = ((NP_Port*) (window->window))->porty;
402 view.left = ((NP_Port*) (window->window))->portx;
403 view.bottom = window->height+view.top;
404 view.right = window->width+view.left;
406 /* clipRect coordinates are also relative to GrafPort */
407 clip.top = window->clipRect.top;
408 clip.left = window->clipRect.left;
409 clip.bottom = window->clipRect.bottom;
410 clip.right = window->clipRect.right;
412 libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
414 #warning disabled code
416 /* remember new window */
417 p_plugin->setWindow(*window);
419 else if( curwin.window )
421 /* change/set parent */
422 curwin.window = NULL;
424 #endif /* XP_MACOSX */
427 if( window && window->window )
429 /* check if plugin has a new parent window */
430 HWND drawable = (HWND) (window->window);
431 if( !curwin.window || drawable != curwin.window )
433 /* reset previous window settings */
434 HWND oldwin = (HWND)p_plugin->getWindow().window;
435 WNDPROC oldproc = p_plugin->getWindowProc();
439 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
441 /* attach our plugin object */
442 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
443 reinterpret_cast<LONG_PTR>(p_plugin));
445 /* install our WNDPROC */
446 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
447 GWL_WNDPROC, (LONG)Manage ) );
449 /* change window style to our liking */
450 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
451 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
452 SetWindowLong((HWND)drawable, GWL_STYLE, style);
454 /* remember new window */
455 p_plugin->setWindow(*window);
458 InvalidateRect( (HWND)drawable, NULL, TRUE );
459 UpdateWindow( (HWND)drawable );
462 else if( curwin.window )
465 HWND oldwin = (HWND)curwin.window;
466 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
467 p_plugin->setWindowProc(NULL);
469 curwin.window = NULL;
474 /* default to hidden toolbar, shown at the end of this method if asked *
475 * developers note : getToolbarSize need to wait the end of this method
477 i_control_height = 0;
478 i_control_width = window->width;
480 if( window && window->window )
482 Window parent = (Window) window->window;
483 if( !curwin.window || (parent != (Window)curwin.window) )
485 Display *p_display = ( (NPSetWindowCallbackStruct *)
486 window->ws_info )->display;
488 XResizeWindow( p_display, parent, window->width, window->height );
490 int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
493 Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
494 window->width, window->height - i_control_height,
495 0, i_blackColor, i_blackColor );
496 Window controls = (Window) NULL;
497 controls = XCreateSimpleWindow( p_display, parent,
498 0, window->height - i_control_height-1,
499 window->width, i_control_height-1,
500 0, i_blackColor, i_blackColor );
502 XMapWindow( p_display, parent );
503 XMapWindow( p_display, video );
504 if( controls ) { XMapWindow( p_display, controls ); }
509 Widget w = XtWindowToWidget( p_display, parent );
511 XtAddEventHandler( w, ExposureMask, FALSE,
512 (XtEventHandler)Redraw, p_plugin );
513 XtAddEventHandler( w, StructureNotifyMask, FALSE,
514 (XtEventHandler)Resize, p_plugin );
515 XtAddEventHandler( w, ButtonReleaseMask, FALSE,
516 (XtEventHandler)ControlHandler, p_plugin );
518 /* remember window */
519 p_plugin->setWindow( *window );
520 p_plugin->setVideoWindow( video );
524 p_plugin->setControlWindow( controls );
527 Redraw( w, (XtPointer)p_plugin, NULL );
529 /* now display toolbar if asked through parameters */
530 if( p_plugin->b_toolbar )
532 p_plugin->showToolbar();
536 else if( curwin.window )
538 curwin.window = NULL;
542 if( !p_plugin->b_stream )
544 if( p_plugin->psz_target )
546 if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
548 if( p_plugin->b_autoplay )
550 p_plugin->playlist_play();
553 p_plugin->b_stream = true;
556 return NPERR_NO_ERROR;
559 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
560 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
561 NPBool seekable, uint16 *stype )
563 NPBool seekable, uint16_t *stype )
566 if( NULL == instance )
568 return NPERR_INVALID_INSTANCE_ERROR;
571 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
572 if( NULL == p_plugin )
574 return NPERR_INVALID_INSTANCE_ERROR;
578 ** Firefox/Mozilla may decide to open a stream from the URL specified
579 ** in the SRC parameter of the EMBED tag and pass it to us
581 ** since VLC will open the SRC URL as well, we're not interested in
582 ** that stream. Otherwise, we'll take it and queue it up in the playlist
584 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
586 /* TODO: use pipes !!!! */
587 *stype = NP_ASFILEONLY;
588 return NPERR_NO_ERROR;
590 return NPERR_GENERIC_ERROR;
593 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
594 int32 NPP_WriteReady( NPP instance, NPStream *stream )
596 int32_t NPP_WriteReady( NPP instance, NPStream *stream )
603 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
604 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
605 int32 len, void *buffer )
607 int32_t NPP_Write( NPP instance, NPStream *stream, int32_t offset,
608 int32_t len, void *buffer )
615 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
617 if( instance == NULL )
619 return NPERR_INVALID_INSTANCE_ERROR;
621 return NPERR_NO_ERROR;
624 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
626 if( instance == NULL )
631 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
632 if( NULL == p_plugin )
637 if( p_plugin->playlist_add( stream->url ) != -1 )
639 if( p_plugin->b_autoplay )
641 p_plugin->playlist_play();
646 void NPP_URLNotify( NPP instance, const char* url,
647 NPReason reason, void* notifyData )
649 /***** Insert NPP_URLNotify code here *****\
650 PluginInstance* p_plugin;
651 if (instance != NULL)
652 p_plugin = (PluginInstance*) instance->pdata;
653 \*********************************************/
656 void NPP_Print( NPP instance, NPPrint* printInfo )
658 if( printInfo == NULL )
663 if( instance != NULL )
665 /***** Insert NPP_Print code here *****\
666 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
667 \**************************************/
669 if( printInfo->mode == NP_FULL )
673 * If your plugin would like to take over
674 * printing completely when it is in full-screen mode,
675 * set printInfo->pluginPrinted to TRUE and print your
676 * plugin as you see fit. If your plugin wants Netscape
677 * to handle printing in this case, set
678 * printInfo->pluginPrinted to FALSE (the default) and
679 * do nothing. If you do want to handle printing
680 * yourself, printOne is true if the print button
681 * (as opposed to the print menu) was clicked.
682 * On the Macintosh, platformPrint is a THPrint; on
683 * Windows, platformPrint is a structure
684 * (defined in npapi.h) containing the printer name, port,
688 /***** Insert NPP_Print code here *****\
689 void* platformPrint =
690 printInfo->print.fullPrint.platformPrint;
692 printInfo->print.fullPrint.printOne;
693 \**************************************/
696 printInfo->print.fullPrint.pluginPrinted = FALSE;
700 /* If not fullscreen, we must be embedded */
703 * If your plugin is embedded, or is full-screen
704 * but you returned false in pluginPrinted above, NPP_Print
705 * will be called with mode == NP_EMBED. The NPWindow
706 * in the printInfo gives the location and dimensions of
707 * the embedded plugin on the printed page. On the
708 * Macintosh, platformPrint is the printer port; on
709 * Windows, platformPrint is the handle to the printing
713 /***** Insert NPP_Print code here *****\
714 NPWindow* printWindow =
715 &(printInfo->print.embedPrint.window);
716 void* platformPrint =
717 printInfo->print.embedPrint.platformPrint;
718 \**************************************/
723 /******************************************************************************
724 * Windows-only methods
725 *****************************************************************************/
727 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
729 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
738 PAINTSTRUCT paintstruct;
742 hdc = BeginPaint( p_hwnd, &paintstruct );
744 GetClientRect( p_hwnd, &rect );
746 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
747 SetTextColor(hdc, RGB(255, 255, 255));
748 SetBkColor(hdc, RGB(0, 0, 0));
749 if( p_plugin->psz_text )
750 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
751 DT_CENTER|DT_VCENTER|DT_SINGLELINE);
753 EndPaint( p_hwnd, &paintstruct );
757 /* delegate to default handler */
758 return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
764 /******************************************************************************
766 *****************************************************************************/
768 static void Redraw( Widget w, XtPointer closure, XEvent *event )
770 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
771 Window control = p_plugin->getControlWindow();
772 const NPWindow& window = p_plugin->getWindow();
775 unsigned int i_control_height, i_control_width;
777 if( p_plugin->b_toolbar )
778 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
780 i_control_height = i_control_width = 0;
782 Window video = p_plugin->getVideoWindow();
783 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
785 gcv.foreground = BlackPixel( p_display, 0 );
786 gc = XCreateGC( p_display, video, GCForeground, &gcv );
788 XFillRectangle( p_display, video, gc,
789 0, 0, window.width, window.height - i_control_height);
791 gcv.foreground = WhitePixel( p_display, 0 );
792 XChangeGC( p_display, gc, GCForeground, &gcv );
794 if( p_plugin->psz_text )
795 XDrawString( p_display, video, gc,
796 window.width / 2 - 40, (window.height - i_control_height) / 2,
797 p_plugin->psz_text, strlen(p_plugin->psz_text) );
798 XFreeGC( p_display, gc );
800 p_plugin->redrawToolbar();
803 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
805 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
806 const NPWindow& window = p_plugin->getWindow();
808 int i_height = window.height;
809 int i_width = window.width;
810 int i_xPos = event->xbutton.x;
811 int i_yPos = event->xbutton.y;
813 if( p_plugin && p_plugin->b_toolbar )
817 libvlc_media_player_t *p_md = p_plugin->getMD();
819 i_playing = p_plugin->playlist_isplaying();
821 vlc_toolbar_clicked_t clicked;
822 clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
829 p_plugin->playlist_pause();
831 p_plugin->playlist_play();
837 p_plugin->playlist_stop();
841 case clicked_Fullscreen:
843 p_plugin->set_fullscreen( 1 );
851 libvlc_audio_toggle_mute( p_md );
855 case clicked_timeline:
857 /* if a movie is loaded */
861 f_length = libvlc_media_player_get_length( p_md ) / 100;
863 f_length = (float)f_length *
864 ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
866 libvlc_media_player_set_time( p_md, f_length );
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 */