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 if( instance == NULL )
165 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
166 if( p_plugin == NULL )
171 EventRecord *myEvent = (EventRecord*)event;
173 switch( myEvent->what )
179 if( (myEvent->when - lastMouseUp) < GetDblTime() )
182 p_plugin->toggle_fullscreen();
187 lastMouseUp = myEvent->when;
195 const NPWindow& npwindow = p_plugin->getWindow();
196 if( npwindow.window )
198 bool hasVout = false;
200 if( p_plugin->playlist_isplaying() )
202 hasVout = p_plugin->player_has_vout();
206 libvlc_rectangle_t area;
209 area.right = npwindow.width;
210 area.bottom = npwindow.height;
211 libvlc_video_redraw_rectangle(p_plugin->getMD(), &area, NULL);
214 #warning disabled code
220 /* draw the beautiful "No Picture" */
222 ForeColor(blackColor);
225 /* seems that firefox forgets to set the following
226 * on occasion (reload) */
227 SetOrigin(((NP_Port *)npwindow.window)->portx,
228 ((NP_Port *)npwindow.window)->porty);
233 rect.right = npwindow.width;
234 rect.bottom = npwindow.height;
237 ForeColor(whiteColor);
238 MoveTo( (npwindow.width-80)/ 2 , npwindow.height / 2 );
239 if( p_plugin->psz_text )
240 DrawText( p_plugin->psz_text, 0, strlen(p_plugin->psz_text) );
247 case NPEventType_GetFocusEvent:
248 case NPEventType_LoseFocusEvent:
250 case NPEventType_AdjustCursorEvent:
252 case NPEventType_MenuCommandEvent:
254 case NPEventType_ClippingChangedEvent:
256 case NPEventType_ScrollingBeginsEvent:
258 case NPEventType_ScrollingEndsEvent:
265 #endif /* XP_MACOSX */
267 /******************************************************************************
268 * General Plug-in Calls
269 *****************************************************************************/
270 NPError NPP_Initialize( void )
272 return NPERR_NO_ERROR;
275 jref NPP_GetJavaClass( void )
280 void NPP_Shutdown( void )
285 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
286 char* argn[], char* argv[], NPSavedData* saved )
290 if( instance == NULL )
292 return NPERR_INVALID_INSTANCE_ERROR;
295 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
296 if( NULL == p_plugin )
298 return NPERR_OUT_OF_MEMORY_ERROR;
301 status = p_plugin->init(argc, argn, argv);
302 if( NPERR_NO_ERROR == status )
304 instance->pdata = reinterpret_cast<void*>(p_plugin);
306 NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
307 NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
317 NPError NPP_Destroy( NPP instance, NPSavedData** save )
319 if( NULL == instance )
320 return NPERR_INVALID_INSTANCE_ERROR;
322 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
323 if( NULL == p_plugin )
324 return NPERR_NO_ERROR;
326 instance->pdata = NULL;
329 HWND win = (HWND)p_plugin->getWindow().window;
330 WNDPROC winproc = p_plugin->getWindowProc();
334 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
338 if( p_plugin->playlist_isplaying() )
339 p_plugin->playlist_stop();
343 return NPERR_NO_ERROR;
346 NPError NPP_SetWindow( NPP instance, NPWindow* window )
350 unsigned int i_control_height = 0, i_control_width = 0;
355 return NPERR_INVALID_INSTANCE_ERROR;
358 /* NPP_SetWindow may be called before NPP_New (Opera) */
359 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
360 if( NULL == p_plugin )
362 /* we should probably show a splash screen here */
363 return NPERR_NO_ERROR;
367 control = p_plugin->getControlWindow();
370 libvlc_instance_t *p_vlc = p_plugin->getVLC();
374 * Before setting window to point to the
375 * new window, you may wish to compare the new window
376 * info to the previous window (if any) to note window
380 /* retrieve current window */
381 NPWindow& curwin = p_plugin->getWindow();
384 if( window && window->window )
386 /* check if plugin has a new parent window */
387 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
389 /* as MacOS X video output is windowless, set viewport */
390 libvlc_rectangle_t view, clip;
393 ** browser sets port origin to top-left location of plugin
394 ** relative to GrafPort window origin is set relative to document,
395 ** which of little use for drawing
397 view.top = ((NP_Port*) (window->window))->porty;
398 view.left = ((NP_Port*) (window->window))->portx;
399 view.bottom = window->height+view.top;
400 view.right = window->width+view.left;
402 /* clipRect coordinates are also relative to GrafPort */
403 clip.top = window->clipRect.top;
404 clip.left = window->clipRect.left;
405 clip.bottom = window->clipRect.bottom;
406 clip.right = window->clipRect.right;
408 libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
410 #warning disabled code
412 /* remember new window */
413 p_plugin->setWindow(*window);
415 else if( curwin.window )
417 /* change/set parent */
418 curwin.window = NULL;
420 #endif /* XP_MACOSX */
423 if( window && window->window )
425 /* check if plugin has a new parent window */
426 HWND drawable = (HWND) (window->window);
427 if( !curwin.window || drawable != curwin.window )
429 /* reset previous window settings */
430 HWND oldwin = (HWND)p_plugin->getWindow().window;
431 WNDPROC oldproc = p_plugin->getWindowProc();
435 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
437 /* attach our plugin object */
438 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
439 reinterpret_cast<LONG_PTR>(p_plugin));
441 /* install our WNDPROC */
442 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
443 GWL_WNDPROC, (LONG)Manage ) );
445 /* change window style to our liking */
446 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
447 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
448 SetWindowLong((HWND)drawable, GWL_STYLE, style);
450 /* remember new window */
451 p_plugin->setWindow(*window);
454 InvalidateRect( (HWND)drawable, NULL, TRUE );
455 UpdateWindow( (HWND)drawable );
458 else if( curwin.window )
461 HWND oldwin = (HWND)curwin.window;
462 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
463 p_plugin->setWindowProc(NULL);
465 curwin.window = NULL;
470 /* default to hidden toolbar, shown at the end of this method if asked *
471 * developers note : getToolbarSize need to wait the end of this method
473 i_control_height = 0;
474 i_control_width = window->width;
476 if( window && window->window )
478 Window parent = (Window) window->window;
479 if( !curwin.window || (parent != (Window)curwin.window) )
481 Display *p_display = ( (NPSetWindowCallbackStruct *)
482 window->ws_info )->display;
484 XResizeWindow( p_display, parent, window->width, window->height );
486 int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
489 Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
490 window->width, window->height - i_control_height,
491 0, i_blackColor, i_blackColor );
492 Window controls = (Window) NULL;
493 controls = XCreateSimpleWindow( p_display, parent,
494 0, window->height - i_control_height-1,
495 window->width, i_control_height-1,
496 0, i_blackColor, i_blackColor );
498 XMapWindow( p_display, parent );
499 XMapWindow( p_display, video );
500 if( controls ) { XMapWindow( p_display, controls ); }
505 Widget w = XtWindowToWidget( p_display, parent );
507 XtAddEventHandler( w, ExposureMask, FALSE,
508 (XtEventHandler)Redraw, p_plugin );
509 XtAddEventHandler( w, StructureNotifyMask, FALSE,
510 (XtEventHandler)Resize, p_plugin );
511 XtAddEventHandler( w, ButtonReleaseMask, FALSE,
512 (XtEventHandler)ControlHandler, p_plugin );
514 /* remember window */
515 p_plugin->setWindow( *window );
516 p_plugin->setVideoWindow( video );
520 p_plugin->setControlWindow( controls );
523 Redraw( w, (XtPointer)p_plugin, NULL );
525 /* now display toolbar if asked through parameters */
526 if( p_plugin->b_toolbar )
528 p_plugin->showToolbar();
532 else if( curwin.window )
534 curwin.window = NULL;
538 if( !p_plugin->b_stream )
540 if( p_plugin->psz_target )
542 if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
544 if( p_plugin->b_autoplay )
546 p_plugin->playlist_play();
549 p_plugin->b_stream = true;
552 return NPERR_NO_ERROR;
555 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
556 NPBool seekable, uint16 *stype )
558 if( NULL == instance )
560 return NPERR_INVALID_INSTANCE_ERROR;
563 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
564 if( NULL == p_plugin )
566 return NPERR_INVALID_INSTANCE_ERROR;
570 ** Firefox/Mozilla may decide to open a stream from the URL specified
571 ** in the SRC parameter of the EMBED tag and pass it to us
573 ** since VLC will open the SRC URL as well, we're not interested in
574 ** that stream. Otherwise, we'll take it and queue it up in the playlist
576 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
578 /* TODO: use pipes !!!! */
579 *stype = NP_ASFILEONLY;
580 return NPERR_NO_ERROR;
582 return NPERR_GENERIC_ERROR;
585 int32 NPP_WriteReady( NPP instance, NPStream *stream )
591 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
592 int32 len, void *buffer )
598 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
600 if( instance == NULL )
602 return NPERR_INVALID_INSTANCE_ERROR;
604 return NPERR_NO_ERROR;
607 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
609 if( instance == NULL )
614 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
615 if( NULL == p_plugin )
620 if( p_plugin->playlist_add( stream->url ) != -1 )
622 if( p_plugin->b_autoplay )
624 p_plugin->playlist_play();
629 void NPP_URLNotify( NPP instance, const char* url,
630 NPReason reason, void* notifyData )
632 /***** Insert NPP_URLNotify code here *****\
633 PluginInstance* p_plugin;
634 if (instance != NULL)
635 p_plugin = (PluginInstance*) instance->pdata;
636 \*********************************************/
639 void NPP_Print( NPP instance, NPPrint* printInfo )
641 if( printInfo == NULL )
646 if( instance != NULL )
648 /***** Insert NPP_Print code here *****\
649 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
650 \**************************************/
652 if( printInfo->mode == NP_FULL )
656 * If your plugin would like to take over
657 * printing completely when it is in full-screen mode,
658 * set printInfo->pluginPrinted to TRUE and print your
659 * plugin as you see fit. If your plugin wants Netscape
660 * to handle printing in this case, set
661 * printInfo->pluginPrinted to FALSE (the default) and
662 * do nothing. If you do want to handle printing
663 * yourself, printOne is true if the print button
664 * (as opposed to the print menu) was clicked.
665 * On the Macintosh, platformPrint is a THPrint; on
666 * Windows, platformPrint is a structure
667 * (defined in npapi.h) containing the printer name, port,
671 /***** Insert NPP_Print code here *****\
672 void* platformPrint =
673 printInfo->print.fullPrint.platformPrint;
675 printInfo->print.fullPrint.printOne;
676 \**************************************/
679 printInfo->print.fullPrint.pluginPrinted = FALSE;
683 /* If not fullscreen, we must be embedded */
686 * If your plugin is embedded, or is full-screen
687 * but you returned false in pluginPrinted above, NPP_Print
688 * will be called with mode == NP_EMBED. The NPWindow
689 * in the printInfo gives the location and dimensions of
690 * the embedded plugin on the printed page. On the
691 * Macintosh, platformPrint is the printer port; on
692 * Windows, platformPrint is the handle to the printing
696 /***** Insert NPP_Print code here *****\
697 NPWindow* printWindow =
698 &(printInfo->print.embedPrint.window);
699 void* platformPrint =
700 printInfo->print.embedPrint.platformPrint;
701 \**************************************/
706 /******************************************************************************
707 * Windows-only methods
708 *****************************************************************************/
710 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
712 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
721 PAINTSTRUCT paintstruct;
725 hdc = BeginPaint( p_hwnd, &paintstruct );
727 GetClientRect( p_hwnd, &rect );
729 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
730 SetTextColor(hdc, RGB(255, 255, 255));
731 SetBkColor(hdc, RGB(0, 0, 0));
732 if( p_plugin->psz_text )
733 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
734 DT_CENTER|DT_VCENTER|DT_SINGLELINE);
736 EndPaint( p_hwnd, &paintstruct );
740 /* delegate to default handler */
741 return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
747 /******************************************************************************
749 *****************************************************************************/
751 static void Redraw( Widget w, XtPointer closure, XEvent *event )
753 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
754 Window control = p_plugin->getControlWindow();
755 const NPWindow& window = p_plugin->getWindow();
758 unsigned int i_control_height, i_control_width;
760 if( p_plugin->b_toolbar )
761 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
763 i_control_height = i_control_width = 0;
765 Window video = p_plugin->getVideoWindow();
766 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
768 gcv.foreground = BlackPixel( p_display, 0 );
769 gc = XCreateGC( p_display, video, GCForeground, &gcv );
771 XFillRectangle( p_display, video, gc,
772 0, 0, window.width, window.height - i_control_height);
774 gcv.foreground = WhitePixel( p_display, 0 );
775 XChangeGC( p_display, gc, GCForeground, &gcv );
777 if( p_plugin->psz_text )
778 XDrawString( p_display, video, gc,
779 window.width / 2 - 40, (window.height - i_control_height) / 2,
780 p_plugin->psz_text, strlen(p_plugin->psz_text) );
781 XFreeGC( p_display, gc );
783 p_plugin->redrawToolbar();
786 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
788 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
789 const NPWindow& window = p_plugin->getWindow();
791 int i_height = window.height;
792 int i_width = window.width;
793 int i_xPos = event->xbutton.x;
794 int i_yPos = event->xbutton.y;
796 if( p_plugin && p_plugin->b_toolbar )
800 libvlc_media_player_t *p_md = p_plugin->getMD();
802 i_playing = p_plugin->playlist_isplaying();
804 vlc_toolbar_clicked_t clicked;
805 clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
812 p_plugin->playlist_pause();
814 p_plugin->playlist_play();
820 p_plugin->playlist_stop();
824 case clicked_Fullscreen:
826 p_plugin->set_fullscreen( 1 );
834 libvlc_audio_toggle_mute( p_md );
838 case clicked_timeline:
840 /* if a movie is loaded */
844 f_length = libvlc_media_player_get_length( p_md ) / 100;
846 f_length = (float)f_length *
847 ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
849 libvlc_media_player_set_time( p_md, f_length );
856 /* Not implemented yet*/
860 default: /* button_Unknown */
864 Redraw( w, closure, event );
867 static void Resize ( Widget w, XtPointer closure, XEvent *event )
869 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
870 Window control = p_plugin->getControlWindow();
871 const NPWindow& window = p_plugin->getWindow();
872 Window drawable = p_plugin->getVideoWindow();
873 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
876 Window root_return, parent_return, * children_return;
878 unsigned int i_nchildren;
879 unsigned int i_control_height, i_control_width;
881 if( p_plugin->b_toolbar )
883 p_plugin->getToolbarSize( &i_control_width, &i_control_height );
887 i_control_height = i_control_width = 0;
890 #ifdef X11_RESIZE_DEBUG
891 XWindowAttributes attr;
893 if( event && event->type == ConfigureNotify )
895 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
896 "send_event ? %s\n", event->xconfigure.width,
897 event->xconfigure.height,
898 event->xconfigure.send_event ? "TRUE" : "FALSE" );
900 #endif /* X11_RESIZE_DEBUG */
902 if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
904 /* size already set */
908 i_ret = XResizeWindow( p_display, drawable,
909 window.width, (window.height - i_control_height) );
911 #ifdef X11_RESIZE_DEBUG
913 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
915 XGetWindowAttributes ( p_display, drawable, &attr );
917 /* X is asynchronous, so the current size reported here is not
918 necessarily the requested size as the Resize request may not
919 yet have been handled by the plugin host */
920 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
921 attr.width, attr.height );
922 #endif /* X11_RESIZE_DEBUG */
924 XQueryTree( p_display, drawable,
925 &root_return, &parent_return, &children_return,
928 if( i_nchildren > 0 )
930 /* XXX: Make assumptions related to the window parenting structure in
931 vlc/modules/video_output/x11/xcommon.c */
932 base_window = children_return[i_nchildren - 1];
934 #ifdef X11_RESIZE_DEBUG
935 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
936 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
938 #endif /* X11_RESIZE_DEBUG */
940 i_ret = XResizeWindow( p_display, base_window,
941 window.width, ( window.height - i_control_height ) );
943 #ifdef X11_RESIZE_DEBUG
945 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
948 XGetWindowAttributes( p_display, base_window, &attr );
950 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
951 attr.width, attr.height );
952 #endif /* X11_RESIZE_DEBUG */