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:
122 /* create an instance and return it */
123 *(NPObject**)value = p_plugin->getScriptObject();
124 if( NULL == *(NPObject**)value )
126 return NPERR_OUT_OF_MEMORY_ERROR;
131 return NPERR_GENERIC_ERROR;
133 return NPERR_NO_ERROR;
136 /******************************************************************************
138 *****************************************************************************/
140 int16 NPP_HandleEvent( NPP instance, void * event )
142 static UInt32 lastMouseUp = 0;
144 if( instance == NULL )
149 VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
150 EventRecord *myEvent = (EventRecord*)event;
152 switch( myEvent->what )
158 if( (myEvent->when - lastMouseUp) < GetDblTime() )
161 libvlc_instance_t *p_vlc = p_plugin->getVLC();
165 if( libvlc_playlist_isplaying(p_vlc, NULL) )
167 libvlc_input_t *p_input = libvlc_playlist_get_input(p_vlc, NULL);
170 libvlc_toggle_fullscreen(p_input, NULL);
171 libvlc_input_free(p_input);
179 lastMouseUp = myEvent->when;
187 int needsDisplay = TRUE;
188 libvlc_instance_t *p_vlc = p_plugin->getVLC();
192 if( libvlc_playlist_isplaying(p_vlc, NULL) )
194 libvlc_input_t *p_input = libvlc_playlist_get_input(p_vlc, NULL);
197 needsDisplay = ! libvlc_input_has_vout(p_input, NULL);
198 libvlc_input_free(p_input);
203 const NPWindow *npwindow = p_plugin->getWindow();
205 if( needsDisplay && npwindow->window )
207 /* draw the beautiful "No Picture" */
209 ForeColor(blackColor);
212 /* seems that firefox forgets to set the following on occasion (reload) */
213 SetOrigin(((NP_Port *)npwindow->window)->portx, ((NP_Port *)npwindow->window)->porty);
218 rect.right = npwindow->width;
219 rect.bottom = npwindow->height;
222 ForeColor(whiteColor);
223 MoveTo( (npwindow->width-80)/ 2 , npwindow->height / 2 );
224 DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
230 case NPEventType_GetFocusEvent:
231 case NPEventType_LoseFocusEvent:
233 case NPEventType_AdjustCursorEvent:
235 case NPEventType_MenuCommandEvent:
237 case NPEventType_ClippingChangedEvent:
239 case NPEventType_ScrollingBeginsEvent:
240 case NPEventType_ScrollingEndsEvent:
247 #endif /* XP_MACOSX */
249 /******************************************************************************
250 * General Plug-in Calls
251 *****************************************************************************/
252 NPError NPP_Initialize( void )
254 return NPERR_NO_ERROR;
257 jref NPP_GetJavaClass( void )
262 void NPP_Shutdown( void )
267 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
268 char* argn[], char* argv[], NPSavedData* saved )
272 if( instance == NULL )
274 return NPERR_INVALID_INSTANCE_ERROR;
277 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
278 if( NULL == p_plugin )
280 return NPERR_OUT_OF_MEMORY_ERROR;
283 status = p_plugin->init(argc, argn, argv);
284 if( NPERR_NO_ERROR == status ) {
285 instance->pdata = reinterpret_cast<void*>(p_plugin);
293 NPError NPP_Destroy( NPP instance, NPSavedData** save )
295 if( NULL == instance )
296 return NPERR_INVALID_INSTANCE_ERROR;
298 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
299 if( NULL == p_plugin )
300 return NPERR_NO_ERROR;
302 instance->pdata = NULL;
305 HWND win = (HWND)p_plugin->getWindow()->window;
306 WNDPROC winproc = p_plugin->getWindowProc();
310 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
317 return NPERR_NO_ERROR;
320 NPError NPP_SetWindow( NPP instance, NPWindow* window )
324 return NPERR_INVALID_INSTANCE_ERROR;
327 /* NPP_SetWindow may be called before NPP_New (Opera) */
328 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
331 /* we should probably show a splash screen here */
332 return NPERR_NO_ERROR;
335 libvlc_instance_t *p_vlc = p_plugin->getVLC();
339 * Before setting window to point to the
340 * new window, you may wish to compare the new window
341 * info to the previous window (if any) to note window
345 const NPWindow *curwin = p_plugin->getWindow();
348 if( window && window->window )
350 /* check if plugin has a new parent window */
351 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
352 if( !curwin->window || drawable != (((NP_Port*) (curwin->window))->port) )
354 /* set/change parent window */
355 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
358 /* as MacOS X video output is windowless, set viewport */
359 libvlc_rectangle_t view, clip;
362 ** browser sets port origin to top-left location of plugin relative to GrafPort
363 ** window origin is set relative to document, which of little use for drawing
365 view.top = ((NP_Port*) (window->window))->porty;
366 view.left = ((NP_Port*) (window->window))->portx;
367 view.bottom = window->height+view.top;
368 view.right = window->width+view.left;
369 /* clipRect coordinates are also relative to GrafPort */
370 clip.top = window->clipRect.top;
371 clip.left = window->clipRect.left;
372 clip.bottom = window->clipRect.bottom;
373 clip.right = window->clipRect.right;
375 libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
377 /* remember window details */
378 p_plugin->setWindow(window);
380 #endif /* XP_MACOSX */
383 if( window && window->window )
385 /* check if plugin has a new parent window */
386 HWND drawable = (HWND) (window->window);
387 if( !curwin->window || drawable != curwin->window )
389 /* reset previous window settings */
390 HWND oldwin = (HWND)p_plugin->getWindow()->window;
391 WNDPROC oldproc = p_plugin->getWindowProc();
395 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
397 /* attach our plugin object */
398 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(p_plugin));
400 /* install our WNDPROC */
401 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
402 GWL_WNDPROC, (LONG)Manage ) );
404 /* change window style to our liking */
405 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
406 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
407 SetWindowLong((HWND)drawable, GWL_STYLE, style);
409 /* change/set parent */
410 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
413 /* remember window details */
414 p_plugin->setWindow(window);
417 InvalidateRect( (HWND)drawable, NULL, TRUE );
418 UpdateWindow( (HWND)drawable );
423 HWND oldwin = (HWND)curwin->window;
424 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
425 p_plugin->setWindowProc(NULL);
426 /* change/set parent */
427 libvlc_video_set_parent(p_vlc, 0, NULL);
432 if( window && window->window )
434 Window drawable = (Window) window->window;
435 if( !curwin->window || drawable != (Window)curwin->window )
437 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
439 XResizeWindow( p_display, drawable, window->width, window->height );
440 Widget w = XtWindowToWidget( p_display, drawable );
442 XtAddEventHandler( w, ExposureMask, FALSE, (XtEventHandler)Redraw, p_plugin );
443 XtAddEventHandler( w, StructureNotifyMask, FALSE, (XtEventHandler)Resize, p_plugin );
445 /* set/change parent window */
446 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
448 /* remember window */
449 p_plugin->setWindow(window);
451 Redraw( w, (XtPointer)p_plugin, NULL );
456 if( !p_plugin->b_stream )
458 if( p_plugin->psz_target )
460 if( libvlc_playlist_add( p_vlc, p_plugin->psz_target, NULL, NULL ) != -1 )
462 if( p_plugin->b_autoplay )
464 libvlc_playlist_play(p_vlc, 0, 0, NULL, NULL);
467 p_plugin->b_stream = VLC_TRUE;
470 return NPERR_NO_ERROR;
473 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
474 NPBool seekable, uint16 *stype )
476 if( NULL == instance )
478 return NPERR_INVALID_INSTANCE_ERROR;
481 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
484 ** Firefox/Mozilla may decide to open a stream from the URL specified
485 ** in the SRC parameter of the EMBED tag and pass it to us
487 ** since VLC will open the SRC URL as well, we're not interested in
488 ** that stream. Otherwise, we'll take it and queue it up in the playlist
490 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
492 /* TODO: use pipes !!!! */
493 *stype = NP_ASFILEONLY;
494 return NPERR_NO_ERROR;
496 return NPERR_GENERIC_ERROR;
499 int32 NPP_WriteReady( NPP instance, NPStream *stream )
506 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
507 int32 len, void *buffer )
514 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
516 if( instance == NULL )
518 return NPERR_INVALID_INSTANCE_ERROR;
520 return NPERR_NO_ERROR;
524 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
526 if( instance == NULL )
531 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
533 if( libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL ) != -1 )
535 if( p_plugin->b_autoplay )
537 libvlc_playlist_play( p_plugin->getVLC(), 0, 0, NULL, NULL);
543 void NPP_URLNotify( NPP instance, const char* url,
544 NPReason reason, void* notifyData )
546 /***** Insert NPP_URLNotify code here *****\
547 PluginInstance* p_plugin;
548 if (instance != NULL)
549 p_plugin = (PluginInstance*) instance->pdata;
550 \*********************************************/
554 void NPP_Print( NPP instance, NPPrint* printInfo )
556 if( printInfo == NULL )
561 if( instance != NULL )
563 /***** Insert NPP_Print code here *****\
564 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
565 \**************************************/
567 if( printInfo->mode == NP_FULL )
571 * If your plugin would like to take over
572 * printing completely when it is in full-screen mode,
573 * set printInfo->pluginPrinted to TRUE and print your
574 * plugin as you see fit. If your plugin wants Netscape
575 * to handle printing in this case, set
576 * printInfo->pluginPrinted to FALSE (the default) and
577 * do nothing. If you do want to handle printing
578 * yourself, printOne is true if the print button
579 * (as opposed to the print menu) was clicked.
580 * On the Macintosh, platformPrint is a THPrint; on
581 * Windows, platformPrint is a structure
582 * (defined in npapi.h) containing the printer name, port,
586 /***** Insert NPP_Print code here *****\
587 void* platformPrint =
588 printInfo->print.fullPrint.platformPrint;
590 printInfo->print.fullPrint.printOne;
591 \**************************************/
594 printInfo->print.fullPrint.pluginPrinted = FALSE;
598 /* If not fullscreen, we must be embedded */
601 * If your plugin is embedded, or is full-screen
602 * but you returned false in pluginPrinted above, NPP_Print
603 * will be called with mode == NP_EMBED. The NPWindow
604 * in the printInfo gives the location and dimensions of
605 * the embedded plugin on the printed page. On the
606 * Macintosh, platformPrint is the printer port; on
607 * Windows, platformPrint is the handle to the printing
611 /***** Insert NPP_Print code here *****\
612 NPWindow* printWindow =
613 &(printInfo->print.embedPrint.window);
614 void* platformPrint =
615 printInfo->print.embedPrint.platformPrint;
616 \**************************************/
621 /******************************************************************************
622 * Windows-only methods
623 *****************************************************************************/
625 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
627 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
636 PAINTSTRUCT paintstruct;
640 hdc = BeginPaint( p_hwnd, &paintstruct );
642 GetClientRect( p_hwnd, &rect );
644 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
645 SetTextColor(hdc, RGB(255, 255, 255));
646 SetBkColor(hdc, RGB(0, 0, 0));
647 DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
649 EndPaint( p_hwnd, &paintstruct );
653 /* delegate to default handler */
654 return p_plugin->getWindowProc()( p_hwnd, i_msg, wpar, lpar );
659 /******************************************************************************
661 *****************************************************************************/
663 static void Redraw( Widget w, XtPointer closure, XEvent *event )
665 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
666 const NPWindow *window = p_plugin->getWindow();
670 Window drawable = (Window) window->window;
671 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
673 gcv.foreground = BlackPixel( p_display, 0 );
674 gc = XCreateGC( p_display, drawable, GCForeground, &gcv );
676 XFillRectangle( p_display, drawable, gc,
677 0, 0, window->width, window->height );
679 gcv.foreground = WhitePixel( p_display, 0 );
680 XChangeGC( p_display, gc, GCForeground, &gcv );
682 XDrawString( p_display, drawable, gc,
683 window->width / 2 - 40, window->height / 2,
684 WINDOW_TEXT, strlen(WINDOW_TEXT) );
686 XFreeGC( p_display, gc );
689 static void Resize ( Widget w, XtPointer closure, XEvent *event )
691 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
692 const NPWindow *window = p_plugin->getWindow();
693 Window drawable = (Window) window->window;
694 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
697 Window root_return, parent_return, * children_return;
699 unsigned int i_nchildren;
701 #ifdef X11_RESIZE_DEBUG
702 XWindowAttributes attr;
704 if( event && event->type == ConfigureNotify )
706 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
707 "send_event ? %s\n", event->xconfigure.width,
708 event->xconfigure.height,
709 event->xconfigure.send_event ? "TRUE" : "FALSE" );
711 #endif /* X11_RESIZE_DEBUG */
713 if( ! p_plugin->setSize(window->width, window->height) )
715 /* size already set */
720 i_ret = XResizeWindow( p_display, drawable, window->width, window->height );
722 #ifdef X11_RESIZE_DEBUG
724 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
726 XGetWindowAttributes ( p_display, drawable, &attr );
728 /* X is asynchronous, so the current size reported here is not
729 necessarily the requested size as the Resize request may not
730 yet have been handled by the plugin host */
731 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
732 attr.width, attr.height );
733 #endif /* X11_RESIZE_DEBUG */
735 XQueryTree( p_display, drawable,
736 &root_return, &parent_return, &children_return,
739 if( i_nchildren > 0 )
741 /* XXX: Make assumptions related to the window parenting structure in
742 vlc/modules/video_output/x11/xcommon.c */
743 base_window = children_return[i_nchildren - 1];
745 #ifdef X11_RESIZE_DEBUG
746 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
747 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
749 #endif /* X11_RESIZE_DEBUG */
751 i_ret = XResizeWindow( p_display, base_window,
752 window->width, window->height );
754 #ifdef X11_RESIZE_DEBUG
756 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
759 XGetWindowAttributes( p_display, base_window, &attr );
761 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
762 attr.width, attr.height );
763 #endif /* X11_RESIZE_DEBUG */