1 /*****************************************************************************
2 * vlcshell.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002-2005 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #ifdef HAVE_MOZILLA_CONFIG_H
35 # include <mozilla-config.h>
38 /* This is from mozilla java, do we really need it? */
43 #include "vlcplugin.h"
45 /* Enable/disable debugging printf's for X11 resizing */
46 #undef X11_RESIZE_DEBUG
48 #define WINDOW_TEXT "(no video)"
50 /*****************************************************************************
51 * Unix-only declarations
52 ******************************************************************************/
55 static void Redraw( Widget w, XtPointer closure, XEvent *event );
56 static void Resize( Widget w, XtPointer closure, XEvent *event );
60 /*****************************************************************************
61 * MacOS-only declarations
62 ******************************************************************************/
66 /*****************************************************************************
67 * Windows-only declarations
68 *****************************************************************************/
71 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
75 /******************************************************************************
77 *****************************************************************************/
78 char * NPP_GetMIMEDescription( void )
80 return PLUGIN_MIMETYPES;
83 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
86 static char psz_desc[1000];
88 /* plugin class variables */
91 case NPPVpluginNameString:
92 *((char **)value) = PLUGIN_NAME;
93 return NPERR_NO_ERROR;
95 case NPPVpluginDescriptionString:
96 snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION, VLC_Version() );
97 *((char **)value) = psz_desc;
98 return NPERR_NO_ERROR;
101 /* move on to instance variables ... */
105 if( instance == NULL )
107 return NPERR_INVALID_INSTANCE_ERROR;
110 /* plugin instance variables */
112 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
113 if( NULL == p_plugin )
115 // plugin has not been initialized yet !
116 return NPERR_INVALID_INSTANCE_ERROR;
121 case NPPVpluginScriptableNPObject:
123 /* retrieve plugin root class */
124 NPClass *scriptClass = p_plugin->getScriptClass();
127 /* create an instance and return it */
128 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
129 return NPERR_NO_ERROR;
137 return NPERR_GENERIC_ERROR;
141 * there is some confusion in gecko headers regarding definition of this API
142 * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
145 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
147 return NPERR_GENERIC_ERROR;
150 /******************************************************************************
152 *****************************************************************************/
154 int16 NPP_HandleEvent( NPP instance, void * event )
156 static UInt32 lastMouseUp = 0;
158 if( instance == NULL )
163 VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
165 if( p_plugin == NULL )
170 EventRecord *myEvent = (EventRecord*)event;
172 switch( myEvent->what )
178 if( (myEvent->when - lastMouseUp) < GetDblTime() )
181 libvlc_instance_t *p_vlc = p_plugin->getVLC();
185 if( libvlc_playlist_isplaying(p_vlc, NULL) )
187 libvlc_media_instance_t *p_md = libvlc_playlist_get_media_instance(p_vlc, NULL);
190 libvlc_toggle_fullscreen(p_md, NULL);
191 libvlc_media_instance_release(p_md);
199 lastMouseUp = myEvent->when;
207 const NPWindow& npwindow = p_plugin->getWindow();
208 if( npwindow.window )
211 libvlc_instance_t *p_vlc = p_plugin->getVLC();
215 if( libvlc_playlist_isplaying(p_vlc, NULL) )
217 libvlc_media_instance_t *p_md = libvlc_playlist_get_media_instance(p_vlc, NULL);
220 hasVout = libvlc_media_instance_has_vout(p_md, NULL);
223 libvlc_rectangle_t area;
226 area.right = npwindow.width;
227 area.bottom = npwindow.height;
228 libvlc_video_redraw_rectangle(p_md, &area, NULL);
230 libvlc_media_instance_release(p_md);
237 /* draw the beautiful "No Picture" */
239 ForeColor(blackColor);
242 /* seems that firefox forgets to set the following on occasion (reload) */
243 SetOrigin(((NP_Port *)npwindow.window)->portx, ((NP_Port *)npwindow.window)->porty);
248 rect.right = npwindow.width;
249 rect.bottom = npwindow.height;
252 ForeColor(whiteColor);
253 MoveTo( (npwindow.width-80)/ 2 , npwindow.height / 2 );
254 DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
261 case NPEventType_GetFocusEvent:
262 case NPEventType_LoseFocusEvent:
264 case NPEventType_AdjustCursorEvent:
266 case NPEventType_MenuCommandEvent:
268 case NPEventType_ClippingChangedEvent:
270 case NPEventType_ScrollingBeginsEvent:
272 case NPEventType_ScrollingEndsEvent:
279 #endif /* XP_MACOSX */
281 /******************************************************************************
282 * General Plug-in Calls
283 *****************************************************************************/
284 NPError NPP_Initialize( void )
286 return NPERR_NO_ERROR;
289 jref NPP_GetJavaClass( void )
294 void NPP_Shutdown( void )
299 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
300 char* argn[], char* argv[], NPSavedData* saved )
304 if( instance == NULL )
306 return NPERR_INVALID_INSTANCE_ERROR;
309 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
310 if( NULL == p_plugin )
312 return NPERR_OUT_OF_MEMORY_ERROR;
315 status = p_plugin->init(argc, argn, argv);
316 if( NPERR_NO_ERROR == status )
318 instance->pdata = reinterpret_cast<void*>(p_plugin);
320 NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
321 NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
331 NPError NPP_Destroy( NPP instance, NPSavedData** save )
333 if( NULL == instance )
334 return NPERR_INVALID_INSTANCE_ERROR;
336 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
337 if( NULL == p_plugin )
338 return NPERR_NO_ERROR;
340 instance->pdata = NULL;
343 HWND win = (HWND)p_plugin->getWindow().window;
344 WNDPROC winproc = p_plugin->getWindowProc();
348 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
354 return NPERR_NO_ERROR;
357 NPError NPP_SetWindow( NPP instance, NPWindow* window )
361 return NPERR_INVALID_INSTANCE_ERROR;
364 /* NPP_SetWindow may be called before NPP_New (Opera) */
365 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
368 /* we should probably show a splash screen here */
369 return NPERR_NO_ERROR;
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);
390 if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
392 /* set/change parent window */
393 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
396 /* as MacOS X video output is windowless, set viewport */
397 libvlc_rectangle_t view, clip;
400 ** browser sets port origin to top-left location of plugin relative to GrafPort
401 ** window origin is set relative to document, which of little use for drawing
403 view.top = ((NP_Port*) (window->window))->porty;
404 view.left = ((NP_Port*) (window->window))->portx;
405 view.bottom = window->height+view.top;
406 view.right = window->width+view.left;
407 /* clipRect coordinates are also relative to GrafPort */
408 clip.top = window->clipRect.top;
409 clip.left = window->clipRect.left;
410 clip.bottom = window->clipRect.bottom;
411 clip.right = window->clipRect.right;
413 libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
415 /* remember new window */
416 p_plugin->setWindow(*window);
418 else if( curwin.window ) {
419 /* change/set parent */
420 libvlc_video_set_parent(p_vlc, 0, NULL);
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, 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 /* change/set parent */
453 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
455 /* remember new window */
456 p_plugin->setWindow(*window);
459 InvalidateRect( (HWND)drawable, NULL, TRUE );
460 UpdateWindow( (HWND)drawable );
463 else if ( curwin.window )
466 HWND oldwin = (HWND)curwin.window;
467 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
468 p_plugin->setWindowProc(NULL);
469 /* change/set parent */
470 libvlc_video_set_parent(p_vlc, 0, NULL);
471 curwin.window = NULL;
476 if( window && window->window )
478 Window drawable = (Window) window->window;
479 if( !curwin.window || drawable != (Window)curwin.window )
481 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
483 XResizeWindow( p_display, drawable, window->width, window->height );
484 Widget w = XtWindowToWidget( p_display, drawable );
486 XtAddEventHandler( w, ExposureMask, FALSE, (XtEventHandler)Redraw, p_plugin );
487 XtAddEventHandler( w, StructureNotifyMask, FALSE, (XtEventHandler)Resize, p_plugin );
489 /* set/change parent window */
490 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
492 /* remember window */
493 p_plugin->setWindow(*window);
495 Redraw( w, (XtPointer)p_plugin, NULL );
498 else if ( curwin.window )
500 /* change/set parent */
501 libvlc_video_set_parent(p_vlc, 0, NULL);
502 curwin.window = NULL;
506 if( !p_plugin->b_stream )
508 if( p_plugin->psz_target )
510 if( libvlc_playlist_add( p_vlc, p_plugin->psz_target, NULL, NULL ) != -1 )
512 if( p_plugin->b_autoplay )
514 libvlc_playlist_play(p_vlc, 0, 0, NULL, NULL);
517 p_plugin->b_stream = VLC_TRUE;
520 return NPERR_NO_ERROR;
523 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
524 NPBool seekable, uint16 *stype )
526 if( NULL == instance )
528 return NPERR_INVALID_INSTANCE_ERROR;
531 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
532 if( NULL == p_plugin )
534 return NPERR_INVALID_INSTANCE_ERROR;
538 ** Firefox/Mozilla may decide to open a stream from the URL specified
539 ** in the SRC parameter of the EMBED tag and pass it to us
541 ** since VLC will open the SRC URL as well, we're not interested in
542 ** that stream. Otherwise, we'll take it and queue it up in the playlist
544 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
546 /* TODO: use pipes !!!! */
547 *stype = NP_ASFILEONLY;
548 return NPERR_NO_ERROR;
550 return NPERR_GENERIC_ERROR;
553 int32 NPP_WriteReady( NPP instance, NPStream *stream )
560 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
561 int32 len, void *buffer )
568 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
570 if( instance == NULL )
572 return NPERR_INVALID_INSTANCE_ERROR;
574 return NPERR_NO_ERROR;
578 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
580 if( instance == NULL )
585 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
586 if( NULL == p_plugin )
591 if( libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL ) != -1 )
593 if( p_plugin->b_autoplay )
595 libvlc_playlist_play( p_plugin->getVLC(), 0, 0, NULL, NULL);
601 void NPP_URLNotify( NPP instance, const char* url,
602 NPReason reason, void* notifyData )
604 /***** Insert NPP_URLNotify code here *****\
605 PluginInstance* p_plugin;
606 if (instance != NULL)
607 p_plugin = (PluginInstance*) instance->pdata;
608 \*********************************************/
612 void NPP_Print( NPP instance, NPPrint* printInfo )
614 if( printInfo == NULL )
619 if( instance != NULL )
621 /***** Insert NPP_Print code here *****\
622 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
623 \**************************************/
625 if( printInfo->mode == NP_FULL )
629 * If your plugin would like to take over
630 * printing completely when it is in full-screen mode,
631 * set printInfo->pluginPrinted to TRUE and print your
632 * plugin as you see fit. If your plugin wants Netscape
633 * to handle printing in this case, set
634 * printInfo->pluginPrinted to FALSE (the default) and
635 * do nothing. If you do want to handle printing
636 * yourself, printOne is true if the print button
637 * (as opposed to the print menu) was clicked.
638 * On the Macintosh, platformPrint is a THPrint; on
639 * Windows, platformPrint is a structure
640 * (defined in npapi.h) containing the printer name, port,
644 /***** Insert NPP_Print code here *****\
645 void* platformPrint =
646 printInfo->print.fullPrint.platformPrint;
648 printInfo->print.fullPrint.printOne;
649 \**************************************/
652 printInfo->print.fullPrint.pluginPrinted = FALSE;
656 /* If not fullscreen, we must be embedded */
659 * If your plugin is embedded, or is full-screen
660 * but you returned false in pluginPrinted above, NPP_Print
661 * will be called with mode == NP_EMBED. The NPWindow
662 * in the printInfo gives the location and dimensions of
663 * the embedded plugin on the printed page. On the
664 * Macintosh, platformPrint is the printer port; on
665 * Windows, platformPrint is the handle to the printing
669 /***** Insert NPP_Print code here *****\
670 NPWindow* printWindow =
671 &(printInfo->print.embedPrint.window);
672 void* platformPrint =
673 printInfo->print.embedPrint.platformPrint;
674 \**************************************/
679 /******************************************************************************
680 * Windows-only methods
681 *****************************************************************************/
683 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
685 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
694 PAINTSTRUCT paintstruct;
698 hdc = BeginPaint( p_hwnd, &paintstruct );
700 GetClientRect( p_hwnd, &rect );
702 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
703 SetTextColor(hdc, RGB(255, 255, 255));
704 SetBkColor(hdc, RGB(0, 0, 0));
705 DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
707 EndPaint( p_hwnd, &paintstruct );
711 /* delegate to default handler */
712 return CallWindowProc(p_plugin->getWindowProc(), p_hwnd, i_msg, wpar, lpar );
717 /******************************************************************************
719 *****************************************************************************/
721 static void Redraw( Widget w, XtPointer closure, XEvent *event )
723 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
724 const NPWindow& window = p_plugin->getWindow();
728 Window drawable = (Window) window.window;
729 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
731 gcv.foreground = BlackPixel( p_display, 0 );
732 gc = XCreateGC( p_display, drawable, GCForeground, &gcv );
734 XFillRectangle( p_display, drawable, gc,
735 0, 0, window.width, window.height );
737 gcv.foreground = WhitePixel( p_display, 0 );
738 XChangeGC( p_display, gc, GCForeground, &gcv );
740 XDrawString( p_display, drawable, gc,
741 window.width / 2 - 40, window.height / 2,
742 WINDOW_TEXT, strlen(WINDOW_TEXT) );
744 XFreeGC( p_display, gc );
747 static void Resize ( Widget w, XtPointer closure, XEvent *event )
749 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
750 const NPWindow& window = p_plugin->getWindow();
751 Window drawable = (Window) window.window;
752 Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
755 Window root_return, parent_return, * children_return;
757 unsigned int i_nchildren;
759 #ifdef X11_RESIZE_DEBUG
760 XWindowAttributes attr;
762 if( event && event->type == ConfigureNotify )
764 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
765 "send_event ? %s\n", event->xconfigure.width,
766 event->xconfigure.height,
767 event->xconfigure.send_event ? "TRUE" : "FALSE" );
769 #endif /* X11_RESIZE_DEBUG */
771 if( ! p_plugin->setSize(window.width, window.height) )
773 /* size already set */
778 i_ret = XResizeWindow( p_display, drawable, window.width, window.height );
780 #ifdef X11_RESIZE_DEBUG
782 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
784 XGetWindowAttributes ( p_display, drawable, &attr );
786 /* X is asynchronous, so the current size reported here is not
787 necessarily the requested size as the Resize request may not
788 yet have been handled by the plugin host */
789 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
790 attr.width, attr.height );
791 #endif /* X11_RESIZE_DEBUG */
793 XQueryTree( p_display, drawable,
794 &root_return, &parent_return, &children_return,
797 if( i_nchildren > 0 )
799 /* XXX: Make assumptions related to the window parenting structure in
800 vlc/modules/video_output/x11/xcommon.c */
801 base_window = children_return[i_nchildren - 1];
803 #ifdef X11_RESIZE_DEBUG
804 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
805 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
807 #endif /* X11_RESIZE_DEBUG */
809 i_ret = XResizeWindow( p_display, base_window,
810 window.width, window.height );
812 #ifdef X11_RESIZE_DEBUG
814 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
817 XGetWindowAttributes( p_display, base_window, &attr );
819 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
820 attr.width, attr.height );
821 #endif /* X11_RESIZE_DEBUG */