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 text from p_plugin->psz_text */
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:
261 #endif /* XP_MACOSX */
263 /******************************************************************************
264 * General Plug-in Calls
265 *****************************************************************************/
266 NPError NPP_Initialize( void )
268 return NPERR_NO_ERROR;
272 jref NPP_GetJavaClass( void )
278 void NPP_Shutdown( void )
283 NPError NPP_New( NPMIMEType pluginType, NPP instance,
284 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
285 uint16 mode, int16 argc,
287 uint16_t mode, int16_t argc,
289 char* argn[], char* argv[], NPSavedData* saved )
293 if( instance == NULL )
295 return NPERR_INVALID_INSTANCE_ERROR;
298 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
299 if( NULL == p_plugin )
301 return NPERR_OUT_OF_MEMORY_ERROR;
304 status = p_plugin->init(argc, argn, argv);
305 if( NPERR_NO_ERROR == status )
307 instance->pdata = reinterpret_cast<void*>(p_plugin);
309 NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
310 NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
320 NPError NPP_Destroy( NPP instance, NPSavedData** save )
322 if( NULL == instance )
323 return NPERR_INVALID_INSTANCE_ERROR;
325 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
326 if( NULL == p_plugin )
327 return NPERR_NO_ERROR;
329 instance->pdata = NULL;
332 HWND win = (HWND)p_plugin->getWindow().window;
333 WNDPROC winproc = p_plugin->getWindowProc();
337 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
341 if( p_plugin->playlist_isplaying() )
342 p_plugin->playlist_stop();
346 return NPERR_NO_ERROR;
349 NPError NPP_SetWindow( NPP instance, NPWindow* window )
353 unsigned int i_control_height = 0, i_control_width = 0;
358 return NPERR_INVALID_INSTANCE_ERROR;
361 /* NPP_SetWindow may be called before NPP_New (Opera) */
362 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
363 if( NULL == p_plugin )
365 /* we should probably show a splash screen here */
366 return NPERR_NO_ERROR;
370 control = p_plugin->getControlWindow();
373 libvlc_instance_t *p_vlc = p_plugin->getVLC();
377 * Before setting window to point to the
378 * new window, you may wish to compare the new window
379 * info to the previous window (if any) to note window
383 /* retrieve current window */
384 NPWindow& curwin = p_plugin->getWindow();
387 if( window && window->window )
389 /* check if plugin has a new parent window */
390 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
392 /* as MacOS X video output is windowless, set viewport */
393 libvlc_rectangle_t view, clip;
396 ** browser sets port origin to top-left location of plugin
397 ** relative to GrafPort window origin is set relative to document,
398 ** which of little use for drawing
400 view.top = ((NP_Port*) (window->window))->porty;
401 view.left = ((NP_Port*) (window->window))->portx;
402 view.bottom = window->height+view.top;
403 view.right = window->width+view.left;
405 /* clipRect coordinates are also relative to GrafPort */
406 clip.top = window->clipRect.top;
407 clip.left = window->clipRect.left;
408 clip.bottom = window->clipRect.bottom;
409 clip.right = window->clipRect.right;
411 libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
413 #warning disabled code
415 /* remember new window */
416 p_plugin->setWindow(*window);
418 else if( curwin.window )
420 /* change/set parent */
421 curwin.window = NULL;
423 #endif /* XP_MACOSX */
426 if( window && window->window )
428 /* check if plugin has a new parent window */
429 HWND drawable = (HWND) (window->window);
430 if( !curwin.window || drawable != curwin.window )
432 /* reset previous window settings */
433 HWND oldwin = (HWND)p_plugin->getWindow().window;
434 WNDPROC oldproc = p_plugin->getWindowProc();
438 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
440 /* attach our plugin object */
441 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
442 reinterpret_cast<LONG_PTR>(p_plugin));
444 /* install our WNDPROC */
445 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
446 GWL_WNDPROC, (LONG)Manage ) );
448 /* change window style to our liking */
449 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
450 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
451 SetWindowLong((HWND)drawable, GWL_STYLE, style);
453 /* remember new window */
454 p_plugin->setWindow(*window);
457 InvalidateRect( (HWND)drawable, NULL, TRUE );
458 UpdateWindow( (HWND)drawable );
461 else if( curwin.window )
464 HWND oldwin = (HWND)curwin.window;
465 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
466 p_plugin->setWindowProc(NULL);
468 curwin.window = NULL;
473 /* default to hidden toolbar, shown at the end of this method if asked *
474 * developers note : getToolbarSize need to wait the end of this method
476 i_control_height = 0;
477 i_control_width = window->width;
479 if( window && window->window )
481 Window parent = (Window) window->window;
482 if( !curwin.window || (parent != (Window)curwin.window) )
484 Display *p_display = ( (NPSetWindowCallbackStruct *)
485 window->ws_info )->display;
487 XResizeWindow( p_display, parent, window->width, window->height );
489 int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
492 Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
493 window->width, window->height - i_control_height,
494 0, i_blackColor, i_blackColor );
495 Window controls = (Window) NULL;
496 controls = XCreateSimpleWindow( p_display, parent,
497 0, window->height - i_control_height-1,
498 window->width, i_control_height-1,
499 0, i_blackColor, i_blackColor );
501 XMapWindow( p_display, parent );
502 XMapWindow( p_display, video );
503 if( controls ) { XMapWindow( p_display, controls ); }
508 Widget w = XtWindowToWidget( p_display, parent );
510 XtAddEventHandler( w, ExposureMask, FALSE,
511 (XtEventHandler)Redraw, p_plugin );
512 XtAddEventHandler( w, StructureNotifyMask, FALSE,
513 (XtEventHandler)Resize, p_plugin );
514 XtAddEventHandler( w, ButtonReleaseMask, FALSE,
515 (XtEventHandler)ControlHandler, p_plugin );
517 /* remember window */
518 p_plugin->setWindow( *window );
519 p_plugin->setVideoWindow( video );
523 p_plugin->setControlWindow( controls );
526 Redraw( w, (XtPointer)p_plugin, NULL );
528 /* now display toolbar if asked through parameters */
529 if( p_plugin->b_toolbar )
531 p_plugin->showToolbar();
535 else if( curwin.window )
537 curwin.window = NULL;
541 if( !p_plugin->b_stream )
543 if( p_plugin->psz_target )
545 if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
547 if( p_plugin->b_autoplay )
549 p_plugin->playlist_play();
552 p_plugin->b_stream = true;
555 return NPERR_NO_ERROR;
558 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
559 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
560 NPBool seekable, uint16 *stype )
562 NPBool seekable, uint16_t *stype )
565 if( NULL == instance )
567 return NPERR_INVALID_INSTANCE_ERROR;
570 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
571 if( NULL == p_plugin )
573 return NPERR_INVALID_INSTANCE_ERROR;
577 ** Firefox/Mozilla may decide to open a stream from the URL specified
578 ** in the SRC parameter of the EMBED tag and pass it to us
580 ** since VLC will open the SRC URL as well, we're not interested in
581 ** that stream. Otherwise, we'll take it and queue it up in the playlist
583 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
585 /* TODO: use pipes !!!! */
586 *stype = NP_ASFILEONLY;
587 return NPERR_NO_ERROR;
589 return NPERR_GENERIC_ERROR;
592 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
593 int32 NPP_WriteReady( NPP instance, NPStream *stream )
595 int32_t NPP_WriteReady( NPP instance, NPStream *stream )
602 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
603 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
604 int32 len, void *buffer )
606 int32_t NPP_Write( NPP instance, NPStream *stream, int32_t offset,
607 int32_t len, void *buffer )
614 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
616 if( instance == NULL )
618 return NPERR_INVALID_INSTANCE_ERROR;
620 return NPERR_NO_ERROR;
623 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
625 if( instance == NULL )
630 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
631 if( NULL == p_plugin )
636 if( p_plugin->playlist_add( stream->url ) != -1 )
638 if( p_plugin->b_autoplay )
640 p_plugin->playlist_play();
645 void NPP_URLNotify( NPP instance, const char* url,
646 NPReason reason, void* notifyData )
648 /***** Insert NPP_URLNotify code here *****\
649 PluginInstance* p_plugin;
650 if (instance != NULL)
651 p_plugin = (PluginInstance*) instance->pdata;
652 \*********************************************/
655 void NPP_Print( NPP instance, NPPrint* printInfo )
657 if( printInfo == NULL )
662 if( instance != NULL )
664 /***** Insert NPP_Print code here *****\
665 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
666 \**************************************/
668 if( printInfo->mode == NP_FULL )
672 * If your plugin would like to take over
673 * printing completely when it is in full-screen mode,
674 * set printInfo->pluginPrinted to TRUE and print your
675 * plugin as you see fit. If your plugin wants Netscape
676 * to handle printing in this case, set
677 * printInfo->pluginPrinted to FALSE (the default) and
678 * do nothing. If you do want to handle printing
679 * yourself, printOne is true if the print button
680 * (as opposed to the print menu) was clicked.
681 * On the Macintosh, platformPrint is a THPrint; on
682 * Windows, platformPrint is a structure
683 * (defined in npapi.h) containing the printer name, port,
687 /***** Insert NPP_Print code here *****\
688 void* platformPrint =
689 printInfo->print.fullPrint.platformPrint;
691 printInfo->print.fullPrint.printOne;
692 \**************************************/
695 printInfo->print.fullPrint.pluginPrinted = FALSE;
699 /* If not fullscreen, we must be embedded */
702 * If your plugin is embedded, or is full-screen
703 * but you returned false in pluginPrinted above, NPP_Print
704 * will be called with mode == NP_EMBED. The NPWindow
705 * in the printInfo gives the location and dimensions of
706 * the embedded plugin on the printed page. On the
707 * Macintosh, platformPrint is the printer port; on
708 * Windows, platformPrint is the handle to the printing
712 /***** Insert NPP_Print code here *****\
713 NPWindow* printWindow =
714 &(printInfo->print.embedPrint.window);
715 void* platformPrint =
716 printInfo->print.embedPrint.platformPrint;
717 \**************************************/
722 /******************************************************************************
723 * Windows-only methods
724 *****************************************************************************/
726 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
728 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
737 PAINTSTRUCT paintstruct;
741 hdc = BeginPaint( p_hwnd, &paintstruct );
743 GetClientRect( p_hwnd, &rect );
745 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
746 SetTextColor(hdc, RGB(255, 255, 255));
747 SetBkColor(hdc, RGB(0, 0, 0));
748 if( p_plugin->psz_text )
749 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
750 DT_CENTER|DT_VCENTER|DT_SINGLELINE);
752 EndPaint( p_hwnd, &paintstruct );
756 /* delegate to default handler */
757 return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
763 /******************************************************************************
765 *****************************************************************************/
767 static void Redraw( Widget w, XtPointer closure, XEvent *event )
769 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
770 Window control = p_plugin->getControlWindow();
771 const NPWindow& window = p_plugin->getWindow();
774 unsigned int i_control_height, i_control_width;
776 if( p_plugin->b_toolbar )
777 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
779 i_control_height = i_control_width = 0;
781 Window video = p_plugin->getVideoWindow();
782 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
784 gcv.foreground = BlackPixel( p_display, 0 );
785 gc = XCreateGC( p_display, video, GCForeground, &gcv );
787 XFillRectangle( p_display, video, gc,
788 0, 0, window.width, window.height - i_control_height);
790 gcv.foreground = WhitePixel( p_display, 0 );
791 XChangeGC( p_display, gc, GCForeground, &gcv );
793 if( p_plugin->psz_text )
794 XDrawString( p_display, video, gc,
795 window.width / 2 - 40, (window.height - i_control_height) / 2,
796 p_plugin->psz_text, strlen(p_plugin->psz_text) );
797 XFreeGC( p_display, gc );
799 p_plugin->redrawToolbar();
802 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
804 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
805 const NPWindow& window = p_plugin->getWindow();
807 int i_height = window.height;
808 int i_width = window.width;
809 int i_xPos = event->xbutton.x;
810 int i_yPos = event->xbutton.y;
812 if( p_plugin && p_plugin->b_toolbar )
816 libvlc_media_player_t *p_md = p_plugin->getMD();
818 i_playing = p_plugin->playlist_isplaying();
820 vlc_toolbar_clicked_t clicked;
821 clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
828 p_plugin->playlist_pause();
830 p_plugin->playlist_play();
836 p_plugin->playlist_stop();
840 case clicked_Fullscreen:
842 p_plugin->set_fullscreen( 1 );
850 libvlc_audio_toggle_mute( p_md );
854 case clicked_timeline:
856 /* if a movie is loaded */
860 f_length = libvlc_media_player_get_length( p_md ) / 100;
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 );
872 /* Not implemented yet*/
876 default: /* button_Unknown */
880 Redraw( w, closure, event );
883 static void Resize ( Widget w, XtPointer closure, XEvent *event )
885 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
886 Window control = p_plugin->getControlWindow();
887 const NPWindow& window = p_plugin->getWindow();
888 Window drawable = p_plugin->getVideoWindow();
889 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
892 Window root_return, parent_return, * children_return;
894 unsigned int i_nchildren;
895 unsigned int i_control_height, i_control_width;
897 if( p_plugin->b_toolbar )
899 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
903 i_control_height = i_control_width = 0;
906 #ifdef X11_RESIZE_DEBUG
907 XWindowAttributes attr;
909 if( event && event->type == ConfigureNotify )
911 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
912 "send_event ? %s\n", event->xconfigure.width,
913 event->xconfigure.height,
914 event->xconfigure.send_event ? "TRUE" : "FALSE" );
916 #endif /* X11_RESIZE_DEBUG */
918 if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
920 /* size already set */
924 i_ret = XResizeWindow( p_display, drawable,
925 window.width, (window.height - i_control_height) );
927 #ifdef X11_RESIZE_DEBUG
929 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
931 XGetWindowAttributes ( p_display, drawable, &attr );
933 /* X is asynchronous, so the current size reported here is not
934 necessarily the requested size as the Resize request may not
935 yet have been handled by the plugin host */
936 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
937 attr.width, attr.height );
938 #endif /* X11_RESIZE_DEBUG */
940 XQueryTree( p_display, drawable,
941 &root_return, &parent_return, &children_return,
944 if( i_nchildren > 0 )
946 /* XXX: Make assumptions related to the window parenting structure in
947 vlc/modules/video_output/x11/xcommon.c */
948 base_window = children_return[i_nchildren - 1];
950 #ifdef X11_RESIZE_DEBUG
951 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
952 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
954 #endif /* X11_RESIZE_DEBUG */
956 i_ret = XResizeWindow( p_display, base_window,
957 window.width, ( window.height - i_control_height ) );
959 #ifdef X11_RESIZE_DEBUG
961 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
964 XGetWindowAttributes( p_display, base_window, &attr );
966 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
967 attr.width, attr.height );
968 #endif /* X11_RESIZE_DEBUG */