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);
181 const NPWindow *npwindow = p_plugin->getWindow();
183 /* draw the beautiful "No Picture" */
185 ForeColor(blackColor);
191 rect.right = npwindow->width;
192 rect.bottom = npwindow->height;
195 ForeColor(whiteColor);
196 MoveTo( (npwindow->width-80)/ 2 , npwindow->height / 2 );
197 DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
203 case NPEventType_GetFocusEvent:
204 case NPEventType_LoseFocusEvent:
206 case NPEventType_AdjustCursorEvent:
208 case NPEventType_MenuCommandEvent:
210 case NPEventType_ClippingChangedEvent:
212 case NPEventType_ScrollingBeginsEvent:
213 case NPEventType_ScrollingEndsEvent:
220 #endif /* XP_MACOSX */
222 /******************************************************************************
223 * General Plug-in Calls
224 *****************************************************************************/
225 NPError NPP_Initialize( void )
227 return NPERR_NO_ERROR;
230 jref NPP_GetJavaClass( void )
235 void NPP_Shutdown( void )
240 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
241 char* argn[], char* argv[], NPSavedData* saved )
245 if( instance == NULL )
247 return NPERR_INVALID_INSTANCE_ERROR;
250 VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
251 if( NULL == p_plugin )
253 return NPERR_OUT_OF_MEMORY_ERROR;
256 status = p_plugin->init(argc, argn, argv);
257 if( NPERR_NO_ERROR == status ) {
258 instance->pdata = reinterpret_cast<void*>(p_plugin);
266 NPError NPP_Destroy( NPP instance, NPSavedData** save )
268 if( instance == NULL )
270 return NPERR_INVALID_INSTANCE_ERROR;
273 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
278 instance->pdata = NULL;
280 return NPERR_NO_ERROR;
283 NPError NPP_SetWindow( NPP instance, NPWindow* window )
287 return NPERR_INVALID_INSTANCE_ERROR;
290 /* NPP_SetWindow may be called before NPP_New (Opera) */
291 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
294 /* we should probably show a splash screen here */
295 return NPERR_NO_ERROR;
298 libvlc_instance_t *p_vlc = p_plugin->getVLC();
302 * Before setting window to point to the
303 * new window, you may wish to compare the new window
304 * info to the previous window (if any) to note window
308 const NPWindow *curwin = p_plugin->getWindow();
311 if( window && window->window )
313 /* check if plugin has a new parent window */
314 CGrafPtr drawable = (((NP_Port*) (window->window))->port);
315 if( !curwin->window || drawable != (((NP_Port*) (curwin->window))->port) )
317 /* set/change parent window */
318 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
321 /* as MacOS X video output is windowless, set viewport */
322 libvlc_rectangle_t view, clip;
325 ** browser sets port origin to top-left location of plugin relative to GrafPort
326 ** window origin is set relative to document, which of little use for drawing
328 view.top = ((NP_Port*) (window->window))->porty;
329 view.left = ((NP_Port*) (window->window))->portx;
330 view.bottom = window->height+view.top;
331 view.right = window->width+view.left;
333 /* clipRect coordinates are also relative to GrafPort */
334 clip.top = window->clipRect.top;
335 clip.left = window->clipRect.left;
336 clip.bottom = window->clipRect.bottom;
337 clip.right = window->clipRect.right;
339 libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
341 /* remember window details */
342 p_plugin->setWindow(window);
344 #endif /* XP_MACOSX */
347 if( window && window->window )
349 /* check if plugin has a new parent window */
350 HWND drawable = (HWND) (window->window);
351 if( !curwin->window || drawable != curwin->window )
353 /* reset previous window settings */
354 HWND oldwin = (HWND)p_plugin->getWindow()->window;
355 WNDPROC oldproc = p_plugin->getWindowProc();
359 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
361 /* install our WNDPROC */
362 p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
363 GWL_WNDPROC, (LONG)Manage ) );
365 /* attach our plugin object */
366 SetWindowLongPtr((HWND)drawable, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(p_plugin));
368 /* change window style to our liking */
369 LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
370 style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
371 SetWindowLong((HWND)drawable, GWL_STYLE, style);
373 /* change/set parent */
374 libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
377 /* remember window details */
378 p_plugin->setWindow(window);
381 InvalidateRect( (HWND)drawable, NULL, TRUE );
382 UpdateWindow( (HWND)drawable );
387 HWND oldwin = (HWND)curwin->window;
388 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
389 p_plugin->setWindowProc(NULL);
390 /* change/set parent */
391 libvlc_video_set_parent(p_vlc, 0, NULL);
396 if( window && window->window )
398 Window drawable = (Window) window->window;
399 if( !curwin->window || drawable != (Window)curwin->window )
401 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
403 XResizeWindow( p_display, drawable, window->width, window->height );
404 Widget w = XtWindowToWidget( p_display, drawable );
406 XtAddEventHandler( w, ExposureMask, FALSE, (XtEventHandler)Redraw, p_plugin );
407 XtAddEventHandler( w, StructureNotifyMask, FALSE, (XtEventHandler)Resize, p_plugin );
409 /* remember window */
410 p_plugin->setWindow(window);
412 Redraw( w, (XtPointer)p_plugin, NULL );
417 if( !p_plugin->b_stream )
419 if( p_plugin->psz_target )
421 if( VLC_SUCCESS == libvlc_playlist_add( p_vlc, p_plugin->psz_target, NULL, NULL ) )
423 if( p_plugin->b_autoplay )
425 libvlc_playlist_play(p_vlc, 0, 0, NULL, NULL);
427 p_plugin->b_stream = VLC_TRUE;
431 return NPERR_NO_ERROR;
434 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
435 NPBool seekable, uint16 *stype )
437 if( instance == NULL )
439 return NPERR_INVALID_INSTANCE_ERROR;
443 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
446 /* fprintf(stderr, "NPP_NewStream - FILE mode !!\n"); */
448 /* We want a *filename* ! */
452 if( !p_plugin->b_stream )
454 p_plugin->psz_target = strdup( stream->url );
455 p_plugin->b_stream = VLC_TRUE;
459 return NPERR_NO_ERROR;
462 int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
463 * mode so we can take any size stream in our
464 * write call (since we ignore it) */
466 #define SARASS_SIZE (1024*1024)
468 int32 NPP_WriteReady( NPP instance, NPStream *stream )
472 /* fprintf(stderr, "NPP_WriteReady\n"); */
474 if (instance != NULL)
476 p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
477 /* Muahahahahahahaha */
478 return STREAMBUFSIZE;
479 /*return SARASS_SIZE;*/
482 /* Number of bytes ready to accept in NPP_Write() */
483 return STREAMBUFSIZE;
488 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
489 int32 len, void *buffer )
491 /* fprintf(stderr, "NPP_Write %i\n", (int)len); */
493 if( instance != NULL )
495 /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
498 return len; /* The number of bytes accepted */
502 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
504 if( instance == NULL )
506 return NPERR_INVALID_INSTANCE_ERROR;
509 return NPERR_NO_ERROR;
513 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
515 if( instance == NULL )
520 /* fprintf(stderr, "NPP_StreamAsFile %s\n", fname); */
523 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
525 VLC_AddTarget( p_plugin->i_vlc, fname, 0, 0,
526 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
527 #endif /* USE_LIBVLC */
531 void NPP_URLNotify( NPP instance, const char* url,
532 NPReason reason, void* notifyData )
534 /***** Insert NPP_URLNotify code here *****\
535 PluginInstance* p_plugin;
536 if (instance != NULL)
537 p_plugin = (PluginInstance*) instance->pdata;
538 \*********************************************/
542 void NPP_Print( NPP instance, NPPrint* printInfo )
544 if( printInfo == NULL )
549 if( instance != NULL )
551 /***** Insert NPP_Print code here *****\
552 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
553 \**************************************/
555 if( printInfo->mode == NP_FULL )
559 * If your plugin would like to take over
560 * printing completely when it is in full-screen mode,
561 * set printInfo->pluginPrinted to TRUE and print your
562 * plugin as you see fit. If your plugin wants Netscape
563 * to handle printing in this case, set
564 * printInfo->pluginPrinted to FALSE (the default) and
565 * do nothing. If you do want to handle printing
566 * yourself, printOne is true if the print button
567 * (as opposed to the print menu) was clicked.
568 * On the Macintosh, platformPrint is a THPrint; on
569 * Windows, platformPrint is a structure
570 * (defined in npapi.h) containing the printer name, port,
574 /***** Insert NPP_Print code here *****\
575 void* platformPrint =
576 printInfo->print.fullPrint.platformPrint;
578 printInfo->print.fullPrint.printOne;
579 \**************************************/
582 printInfo->print.fullPrint.pluginPrinted = FALSE;
586 /* If not fullscreen, we must be embedded */
589 * If your plugin is embedded, or is full-screen
590 * but you returned false in pluginPrinted above, NPP_Print
591 * will be called with mode == NP_EMBED. The NPWindow
592 * in the printInfo gives the location and dimensions of
593 * the embedded plugin on the printed page. On the
594 * Macintosh, platformPrint is the printer port; on
595 * Windows, platformPrint is the handle to the printing
599 /***** Insert NPP_Print code here *****\
600 NPWindow* printWindow =
601 &(printInfo->print.embedPrint.window);
602 void* platformPrint =
603 printInfo->print.embedPrint.platformPrint;
604 \**************************************/
609 /******************************************************************************
610 * Windows-only methods
611 *****************************************************************************/
613 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
615 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
624 PAINTSTRUCT paintstruct;
628 hdc = BeginPaint( p_hwnd, &paintstruct );
630 GetClientRect( p_hwnd, &rect );
632 FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
633 SetTextColor(hdc, RGB(255, 255, 255));
634 SetBkColor(hdc, RGB(0, 0, 0));
635 TextOut( hdc, (rect.right-rect.left)/ 2 - 40,
636 (rect.bottom-rect.top)/ 2,
637 WINDOW_TEXT, strlen(WINDOW_TEXT) );
639 EndPaint( p_hwnd, &paintstruct );
643 /* delegate to default handler */
644 return p_plugin->getWindowProc()( p_hwnd, i_msg, wpar, lpar );
649 /******************************************************************************
651 *****************************************************************************/
653 static void Redraw( Widget w, XtPointer closure, XEvent *event )
655 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
656 const NPWindow *window = p_plugin->getWindow();
660 Window drawable = (Window) window->window;
661 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
663 gcv.foreground = BlackPixel( p_display, 0 );
664 gc = XCreateGC( p_display, drawable, GCForeground, &gcv );
666 XFillRectangle( p_display, drawable, gc,
667 0, 0, window->width, window->height );
669 gcv.foreground = WhitePixel( p_display, 0 );
670 XChangeGC( p_display, gc, GCForeground, &gcv );
672 XDrawString( p_display, drawable, gc,
673 window->width / 2 - 40, window->height / 2,
674 WINDOW_TEXT, strlen(WINDOW_TEXT) );
676 XFreeGC( p_display, gc );
679 static void Resize ( Widget w, XtPointer closure, XEvent *event )
681 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
682 const NPWindow *window = p_plugin->getWindow();
683 Window drawable = (Window) window->window;
684 Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
687 Window root_return, parent_return, * children_return;
689 unsigned int i_nchildren;
691 #ifdef X11_RESIZE_DEBUG
692 XWindowAttributes attr;
694 if( event && event->type == ConfigureNotify )
696 fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
697 "send_event ? %s\n", event->xconfigure.width,
698 event->xconfigure.height,
699 event->xconfigure.send_event ? "TRUE" : "FALSE" );
701 #endif /* X11_RESIZE_DEBUG */
703 if( ! p_plugin->setSize(window->width, window->height) )
705 /* size already set */
710 i_ret = XResizeWindow( p_display, drawable, window->width, window->height );
712 #ifdef X11_RESIZE_DEBUG
714 "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
716 XGetWindowAttributes ( p_display, drawable, &attr );
718 /* X is asynchronous, so the current size reported here is not
719 necessarily the requested size as the Resize request may not
720 yet have been handled by the plugin host */
721 fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
722 attr.width, attr.height );
723 #endif /* X11_RESIZE_DEBUG */
725 XQueryTree( p_display, drawable,
726 &root_return, &parent_return, &children_return,
729 if( i_nchildren > 0 )
731 /* XXX: Make assumptions related to the window parenting structure in
732 vlc/modules/video_output/x11/xcommon.c */
733 base_window = children_return[i_nchildren - 1];
735 #ifdef X11_RESIZE_DEBUG
736 fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
737 fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
739 #endif /* X11_RESIZE_DEBUG */
741 i_ret = XResizeWindow( p_display, base_window,
742 window->width, window->height );
744 #ifdef X11_RESIZE_DEBUG
746 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
749 XGetWindowAttributes( p_display, base_window, &attr );
751 fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
752 attr.width, attr.height );
753 #endif /* X11_RESIZE_DEBUG */