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)-1, PLUGIN_DESCRIPTION, VLC_Version() );
97 psz_desc[sizeof(psz_desc)-1] = 0;
98 *((char **)value) = psz_desc;
99 return NPERR_NO_ERROR;
102 /* move on to instance variables ... */
106 if( instance == NULL )
108 return NPERR_INVALID_INSTANCE_ERROR;
111 /* plugin instance variables */
113 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
114 if( NULL == p_plugin )
116 // plugin has not been initialized yet !
117 return NPERR_INVALID_INSTANCE_ERROR;
122 case NPPVpluginScriptableNPObject:
123 /* create an instance and return it */
124 *(NPObject**)value = NPN_CreateObject(instance, p_plugin->getScriptClass());
125 if( NULL == *(NPObject**)value )
127 return NPERR_OUT_OF_MEMORY_ERROR;
132 return NPERR_GENERIC_ERROR;
134 return NPERR_NO_ERROR;
137 /******************************************************************************
139 *****************************************************************************/
141 int16 NPP_HandleEvent( NPP instance, void * event )
143 if( instance == NULL )
148 VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
149 EventRecord *myEvent = (EventRecord*)event;
151 switch( myEvent->what )
164 int needsDisplay = TRUE;
165 libvlc_instance_t *p_vlc = p_plugin->getVLC();
169 if( libvlc_playlist_isplaying(p_vlc, NULL) )
171 libvlc_input_t *p_input = libvlc_playlist_get_input(p_vlc, NULL);
174 needsDisplay = ! libvlc_input_has_vout(p_input, NULL);
175 libvlc_input_free(p_input);
180 const NPWindow *npwindow = p_plugin->getWindow();
182 if( needsDisplay && npwindow->window )
184 /* draw the beautiful "No Picture" */
186 ForeColor(blackColor);
189 /* seems that firefox forgets to set the following on occasion (reload) */
190 SetOrigin(((NP_Port *)npwindow->window)->portx, ((NP_Port *)npwindow->window)->porty);
195 rect.right = npwindow->width;
196 rect.bottom = npwindow->height;
199 ForeColor(whiteColor);
200 MoveTo( (npwindow->width-80)/ 2 , npwindow->height / 2 );
201 DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
207 case NPEventType_GetFocusEvent:
208 case NPEventType_LoseFocusEvent:
210 case NPEventType_AdjustCursorEvent:
212 case NPEventType_MenuCommandEvent:
214 case NPEventType_ClippingChangedEvent:
216 case NPEventType_ScrollingBeginsEvent:
217 case NPEventType_ScrollingEndsEvent:
224 #endif /* XP_MACOSX */
226 /******************************************************************************
227 * General Plug-in Calls
228 *****************************************************************************/
229 NPError NPP_Initialize( void )
231 return NPERR_NO_ERROR;
234 jref NPP_GetJavaClass( void )
239 void NPP_Shutdown( void )
244 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
245 char* argn[], char* argv[], NPSavedData* saved )
249 if( instance == NULL )
251 return NPERR_INVALID_INSTANCE_ERROR;
254 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
255 if( NULL == p_plugin )
257 return NPERR_OUT_OF_MEMORY_ERROR;
260 status = p_plugin->init(argc, argn, argv);
261 if( NPERR_NO_ERROR == status ) {
262 instance->pdata = reinterpret_cast<void*>(p_plugin);
270 NPError NPP_Destroy( NPP instance, NPSavedData** save )
272 if( instance == NULL )
274 return NPERR_INVALID_INSTANCE_ERROR;
277 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
280 HWND win = (HWND)p_plugin->getWindow()->window;
281 WNDPROC winproc = p_plugin->getWindowProc();
285 SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
292 instance->pdata = NULL;
294 return NPERR_NO_ERROR;
297 NPError NPP_SetWindow( NPP instance, NPWindow* window )
301 return NPERR_INVALID_INSTANCE_ERROR;
304 /* NPP_SetWindow may be called before NPP_New (Opera) */
305 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
308 /* we should probably show a splash screen here */
309 return NPERR_NO_ERROR;
312 libvlc_instance_t *p_vlc = p_plugin->getVLC();
316 * Before setting window to point to the
317 * new window, you may wish to compare the new window
318 * info to the previous window (if any) to note window
322 const NPWindow *curwin = p_plugin->getWindow();
325 if( window && window->window )
327 /* check if plugin has a new parent window */
328 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
329 if( !curwin->window || drawable != (((NP_Port*) (curwin->window))->port) )
331 /* set/change parent window */
332 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
335 /* as MacOS X video output is windowless, set viewport */
336 libvlc_rectangle_t view, clip;
339 ** browser sets port origin to top-left location of plugin relative to GrafPort
340 ** window origin is set relative to document, which of little use for drawing
342 view.top = ((NP_Port*) (window->window))->porty;
343 view.left = ((NP_Port*) (window->window))->portx;
344 view.bottom = window->height+view.top;
345 view.right = window->width+view.left;
346 /* clipRect coordinates are also relative to GrafPort */
347 clip.top = window->clipRect.top;
348 clip.left = window->clipRect.left;
349 clip.bottom = window->clipRect.bottom;
350 clip.right = window->clipRect.right;
352 libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
354 /* remember window details */
355 p_plugin->setWindow(window);
357 #endif /* XP_MACOSX */
360 if( window && window->window )
362 /* check if plugin has a new parent window */
363 HWND drawable = (HWND) (window->window);
364 if( !curwin->window || drawable != curwin->window )
366 /* reset previous window settings */
367 HWND oldwin = (HWND)p_plugin->getWindow()->window;
368 WNDPROC oldproc = p_plugin->getWindowProc();
372 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
374 /* attach our plugin object */
375 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(p_plugin));
377 /* install our WNDPROC */
378 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
379 GWL_WNDPROC, (LONG)Manage ) );
381 /* change window style to our liking */
382 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
383 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
384 SetWindowLong((HWND)drawable, GWL_STYLE, style);
386 /* change/set parent */
387 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
390 /* remember window details */
391 p_plugin->setWindow(window);
394 InvalidateRect( (HWND)drawable, NULL, TRUE );
395 UpdateWindow( (HWND)drawable );
400 HWND oldwin = (HWND)curwin->window;
401 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
402 p_plugin->setWindowProc(NULL);
403 /* change/set parent */
404 libvlc_video_set_parent(p_vlc, 0, NULL);
409 if( window && window->window )
411 Window drawable = (Window) window->window;
412 if( !curwin->window || drawable != (Window)curwin->window )
414 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
416 XResizeWindow( p_display, drawable, window->width, window->height );
417 Widget w = XtWindowToWidget( p_display, drawable );
419 XtAddEventHandler( w, ExposureMask, FALSE, (XtEventHandler)Redraw, p_plugin );
420 XtAddEventHandler( w, StructureNotifyMask, FALSE, (XtEventHandler)Resize, p_plugin );
422 /* set/change parent window */
423 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
425 /* remember window */
426 p_plugin->setWindow(window);
428 Redraw( w, (XtPointer)p_plugin, NULL );
433 if( !p_plugin->b_stream )
435 if( p_plugin->psz_target )
437 if( VLC_SUCCESS == libvlc_playlist_add( p_vlc, p_plugin->psz_target, NULL, NULL ) )
439 if( p_plugin->b_autoplay )
441 libvlc_playlist_play(p_vlc, 0, 0, NULL, NULL);
443 p_plugin->b_stream = VLC_TRUE;
447 return NPERR_NO_ERROR;
450 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
451 NPBool seekable, uint16 *stype )
453 if( NULL == instance )
455 return NPERR_INVALID_INSTANCE_ERROR;
458 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
461 ** Firefox/Mozilla may decide to open a stream from the URL specified
462 ** in the SRC parameter of the EMBED tag and pass it to us
464 ** since VLC will open the SRC URL as well, we're not interested in
465 ** that stream. Otherwise, we'll take it and queue it up in the playlist
467 if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
469 /* TODO: use pipes !!!! */
470 *stype = NP_ASFILEONLY;
471 return NPERR_NO_ERROR;
473 return NPERR_GENERIC_ERROR;
476 int32 NPP_WriteReady( NPP instance, NPStream *stream )
483 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
484 int32 len, void *buffer )
491 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
493 if( instance == NULL )
495 return NPERR_INVALID_INSTANCE_ERROR;
497 return NPERR_NO_ERROR;
501 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
503 if( instance == NULL )
508 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
510 if( VLC_SUCCESS == libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL ) )
512 if( p_plugin->b_autoplay )
514 libvlc_playlist_play( p_plugin->getVLC(), 0, 0, NULL, NULL);
520 void NPP_URLNotify( NPP instance, const char* url,
521 NPReason reason, void* notifyData )
523 /***** Insert NPP_URLNotify code here *****\
524 PluginInstance* p_plugin;
525 if (instance != NULL)
526 p_plugin = (PluginInstance*) instance->pdata;
527 \*********************************************/
531 void NPP_Print( NPP instance, NPPrint* printInfo )
533 if( printInfo == NULL )
538 if( instance != NULL )
540 /***** Insert NPP_Print code here *****\
541 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
542 \**************************************/
544 if( printInfo->mode == NP_FULL )
548 * If your plugin would like to take over
549 * printing completely when it is in full-screen mode,
550 * set printInfo->pluginPrinted to TRUE and print your
551 * plugin as you see fit. If your plugin wants Netscape
552 * to handle printing in this case, set
553 * printInfo->pluginPrinted to FALSE (the default) and
554 * do nothing. If you do want to handle printing
555 * yourself, printOne is true if the print button
556 * (as opposed to the print menu) was clicked.
557 * On the Macintosh, platformPrint is a THPrint; on
558 * Windows, platformPrint is a structure
559 * (defined in npapi.h) containing the printer name, port,
563 /***** Insert NPP_Print code here *****\
564 void* platformPrint =
565 printInfo->print.fullPrint.platformPrint;
567 printInfo->print.fullPrint.printOne;
568 \**************************************/
571 printInfo->print.fullPrint.pluginPrinted = FALSE;
575 /* If not fullscreen, we must be embedded */
578 * If your plugin is embedded, or is full-screen
579 * but you returned false in pluginPrinted above, NPP_Print
580 * will be called with mode == NP_EMBED. The NPWindow
581 * in the printInfo gives the location and dimensions of
582 * the embedded plugin on the printed page. On the
583 * Macintosh, platformPrint is the printer port; on
584 * Windows, platformPrint is the handle to the printing
588 /***** Insert NPP_Print code here *****\
589 NPWindow* printWindow =
590 &(printInfo->print.embedPrint.window);
591 void* platformPrint =
592 printInfo->print.embedPrint.platformPrint;
593 \**************************************/
598 /******************************************************************************
599 * Windows-only methods
600 *****************************************************************************/
602 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
604 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
613 PAINTSTRUCT paintstruct;
617 hdc = BeginPaint( p_hwnd, &paintstruct );
619 GetClientRect( p_hwnd, &rect );
621 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
622 SetTextColor(hdc, RGB(255, 255, 255));
623 SetBkColor(hdc, RGB(0, 0, 0));
624 DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
626 EndPaint( p_hwnd, &paintstruct );
630 /* delegate to default handler */
631 return p_plugin->getWindowProc()( p_hwnd, i_msg, wpar, lpar );
636 /******************************************************************************
638 *****************************************************************************/
640 static void Redraw( Widget w, XtPointer closure, XEvent *event )
642 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
643 const NPWindow *window = p_plugin->getWindow();
647 Window drawable = (Window) window->window;
648 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
650 gcv.foreground = BlackPixel( p_display, 0 );
651 gc = XCreateGC( p_display, drawable, GCForeground, &gcv );
653 XFillRectangle( p_display, drawable, gc,
654 0, 0, window->width, window->height );
656 gcv.foreground = WhitePixel( p_display, 0 );
657 XChangeGC( p_display, gc, GCForeground, &gcv );
659 XDrawString( p_display, drawable, gc,
660 window->width / 2 - 40, window->height / 2,
661 WINDOW_TEXT, strlen(WINDOW_TEXT) );
663 XFreeGC( p_display, gc );
666 static void Resize ( Widget w, XtPointer closure, XEvent *event )
668 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
669 const NPWindow *window = p_plugin->getWindow();
670 Window drawable = (Window) window->window;
671 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
674 Window root_return, parent_return, * children_return;
676 unsigned int i_nchildren;
678 #ifdef X11_RESIZE_DEBUG
679 XWindowAttributes attr;
681 if( event && event->type == ConfigureNotify )
683 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
684 "send_event ? %s\n", event->xconfigure.width,
685 event->xconfigure.height,
686 event->xconfigure.send_event ? "TRUE" : "FALSE" );
688 #endif /* X11_RESIZE_DEBUG */
690 if( ! p_plugin->setSize(window->width, window->height) )
692 /* size already set */
697 i_ret = XResizeWindow( p_display, drawable, window->width, window->height );
699 #ifdef X11_RESIZE_DEBUG
701 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
703 XGetWindowAttributes ( p_display, drawable, &attr );
705 /* X is asynchronous, so the current size reported here is not
706 necessarily the requested size as the Resize request may not
707 yet have been handled by the plugin host */
708 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
709 attr.width, attr.height );
710 #endif /* X11_RESIZE_DEBUG */
712 XQueryTree( p_display, drawable,
713 &root_return, &parent_return, &children_return,
716 if( i_nchildren > 0 )
718 /* XXX: Make assumptions related to the window parenting structure in
719 vlc/modules/video_output/x11/xcommon.c */
720 base_window = children_return[i_nchildren - 1];
722 #ifdef X11_RESIZE_DEBUG
723 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
724 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
726 #endif /* X11_RESIZE_DEBUG */
728 i_ret = XResizeWindow( p_display, base_window,
729 window->width, window->height );
731 #ifdef X11_RESIZE_DEBUG
733 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
736 XGetWindowAttributes( p_display, base_window, &attr );
738 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
739 attr.width, attr.height );
740 #endif /* X11_RESIZE_DEBUG */