1 /*****************************************************************************
2 * vlcshell.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: vlcshell.cpp,v 1.15 2003/07/23 01:13:48 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /* XXX: disable VLC here */
27 /*****************************************************************************
29 *****************************************************************************/
40 #include <nsISupports.h>
50 # include <X11/Xlib.h>
51 # include <X11/Intrinsic.h>
52 # include <X11/StringDefs.h>
57 # include <QuickDraw.h>
61 #include "vlcplugin.h"
64 # define WINDOW_TEXT "(no picture)"
66 # define WINDOW_TEXT "(no libvlc)"
69 /*****************************************************************************
70 * Unix-only declarations
71 ******************************************************************************/
73 # define VOUT_PLUGINS "xvideo,x11,dummy"
74 # define AOUT_PLUGINS "oss,dummy"
76 static void Redraw( Widget w, XtPointer closure, XEvent *event );
79 /*****************************************************************************
80 * Windows-only declarations
81 *****************************************************************************/
83 # define VOUT_PLUGINS "directx,dummy"
84 # define AOUT_PLUGINS "none" /* "directx,waveout,dummy" */
86 HINSTANCE g_hDllInstance = NULL;
89 DllMain( HINSTANCE hinstDLL, // handle of DLL module
90 DWORD fdwReason, // reason for calling function
95 case DLL_PROCESS_ATTACH:
96 g_hDllInstance = hinstDLL;
98 case DLL_THREAD_ATTACH:
99 case DLL_PROCESS_DETACH:
100 case DLL_THREAD_DETACH:
106 LRESULT CALLBACK Manage( HWND, UINT, WPARAM, LPARAM );
109 /******************************************************************************
110 * UNIX-only API calls
111 *****************************************************************************/
112 char * NPP_GetMIMEDescription( void )
114 return PLUGIN_MIMETYPES;
117 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
119 static nsIID nsid = VLCINTF_IID;
120 static char psz_desc[1000];
124 case NPPVpluginNameString:
125 *((char **)value) = PLUGIN_NAME;
126 return NPERR_NO_ERROR;
128 case NPPVpluginDescriptionString:
130 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, VLC_Version() );
132 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, "(disabled)" );
134 psz_desc[1000-1] = 0;
135 *((char **)value) = psz_desc;
136 return NPERR_NO_ERROR;
143 if( instance == NULL )
145 return NPERR_INVALID_INSTANCE_ERROR;
148 VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;
152 case NPPVpluginScriptableInstance:
153 *(nsISupports**)value = p_plugin->GetPeer();
154 if( *(nsISupports**)value == NULL )
156 return NPERR_OUT_OF_MEMORY_ERROR;
160 case NPPVpluginScriptableIID:
161 *(nsIID**)value = (nsIID*)NPN_MemAlloc( sizeof(nsIID) );
162 if( *(nsIID**)value == NULL )
164 return NPERR_OUT_OF_MEMORY_ERROR;
166 **(nsIID**)value = nsid;
170 return NPERR_GENERIC_ERROR;
173 return NPERR_NO_ERROR;
176 /******************************************************************************
178 *****************************************************************************/
180 int16 NPP_HandleEvent( NPP instance, void * event )
182 if( instance == NULL )
187 Boolean eventHandled = false;
190 TPlugin *pPlugin = (TPlugin*)instance->pdata;
191 if( pPlugin != NULL && event != NULL )
193 eventHandled = pPlugin->HandleEvent( *(EventRecord*)event );
201 /******************************************************************************
202 * General Plug-in Calls
203 *****************************************************************************/
204 NPError NPP_Initialize( void )
206 return NPERR_NO_ERROR;
209 jref NPP_GetJavaClass( void )
214 void NPP_Shutdown( void )
219 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
220 char* argn[], char* argv[], NPSavedData* saved )
230 /*, "--plugin-path", "/home/sam/videolan/vlc_MAIN/plugins"*/
234 if( instance == NULL )
236 return NPERR_INVALID_INSTANCE_ERROR;
239 VlcPlugin * p_plugin = new VlcPlugin( instance );
241 if( p_plugin == NULL )
243 return NPERR_OUT_OF_MEMORY_ERROR;
246 instance->pdata = p_plugin;
249 p_plugin->p_hwnd = NULL;
250 p_plugin->pf_wndproc = NULL;
254 p_plugin->window = 0;
255 p_plugin->p_display = NULL;
258 p_plugin->p_npwin = NULL;
259 p_plugin->i_npmode = mode;
260 p_plugin->i_width = 0;
261 p_plugin->i_height = 0;
264 p_plugin->i_vlc = VLC_Create();
265 if( p_plugin->i_vlc < 0 )
270 return NPERR_GENERIC_ERROR;
273 i_ret = VLC_Init( p_plugin->i_vlc, sizeof(ppsz_foo)/sizeof(char*), ppsz_foo );
276 VLC_Destroy( p_plugin->i_vlc );
280 return NPERR_GENERIC_ERROR;
283 value.psz_string = "dummy";
284 VLC_Set( p_plugin->i_vlc, "conf::intf", value );
285 value.psz_string = VOUT_PLUGINS;
286 VLC_Set( p_plugin->i_vlc, "conf::vout", value );
287 value.psz_string = AOUT_PLUGINS;
288 VLC_Set( p_plugin->i_vlc, "conf::aout", value );
293 #endif /* USE_LIBVLC */
295 p_plugin->b_stream = VLC_FALSE;
296 p_plugin->b_autoplay = VLC_FALSE;
297 p_plugin->psz_target = NULL;
299 for( i = 0; i < argc ; i++ )
301 if( !strcmp( argn[i], "target" ) )
303 p_plugin->psz_target = argv[i];
305 else if( !strcmp( argn[i], "autoplay" ) )
307 if( !strcmp( argv[i], "yes" ) )
309 p_plugin->b_autoplay = 1;
312 else if( !strcmp( argn[i], "autostart" ) )
314 if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "true" ) )
316 p_plugin->b_autoplay = 1;
319 else if( !strcmp( argn[i], "filename" ) )
321 p_plugin->psz_target = argv[i];
323 else if( !strcmp( argn[i], "src" ) )
325 p_plugin->psz_target = argv[i];
329 else if( !strcmp( argn[i], "loop" ) )
331 if( !strcmp( argv[i], "yes" ) )
333 value.b_bool = VLC_TRUE;
334 VLC_Set( p_plugin->i_vlc, "conf::loop", value );
340 if( p_plugin->psz_target )
342 p_plugin->psz_target = strdup( p_plugin->psz_target );
345 return NPERR_NO_ERROR;
348 NPError NPP_Destroy( NPP instance, NPSavedData** save )
350 if( instance == NULL )
352 return NPERR_INVALID_INSTANCE_ERROR;
355 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
357 if( p_plugin != NULL )
359 if( p_plugin->i_vlc )
362 VLC_Stop( p_plugin->i_vlc );
363 VLC_Destroy( p_plugin->i_vlc );
368 if( p_plugin->psz_target )
370 free( p_plugin->psz_target );
371 p_plugin->psz_target = NULL;
377 instance->pdata = NULL;
379 return NPERR_NO_ERROR;
382 NPError NPP_SetWindow( NPP instance, NPWindow* window )
384 if( instance == NULL )
386 return NPERR_INVALID_INSTANCE_ERROR;
389 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
391 /* Write the window ID for vlc */
395 /* FIXME: this cast sucks */
396 value.i_int = (int) (ptrdiff_t) (void *) window->window;
397 VLC_Set( p_plugin->i_vlc, "drawable", value );
402 * Before setting window to point to the
403 * new window, you may wish to compare the new window
404 * info to the previous window (if any) to note window
409 if( !window || !window->window )
411 /* Window was destroyed. Invalidate everything. */
412 if( p_plugin->p_npwin )
414 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
415 (LONG)p_plugin->pf_wndproc );
416 p_plugin->pf_wndproc = NULL;
417 p_plugin->p_hwnd = NULL;
420 p_plugin->p_npwin = window;
421 return NPERR_NO_ERROR;
424 if( p_plugin->p_npwin )
426 if( p_plugin->p_hwnd == (HWND)window->window )
428 /* Same window, but something may have changed. First we
429 * update the plugin structure, then we redraw the window */
430 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
431 p_plugin->i_width = window->width;
432 p_plugin->i_height = window->height;
433 p_plugin->p_npwin = window;
434 UpdateWindow( p_plugin->p_hwnd );
435 return NPERR_NO_ERROR;
438 /* Window has changed. Destroy the one we have, and go
439 * on as if it was a real initialization. */
440 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
441 (LONG)p_plugin->pf_wndproc );
442 p_plugin->pf_wndproc = NULL;
443 p_plugin->p_hwnd = NULL;
446 p_plugin->pf_wndproc = (WNDPROC)SetWindowLong( (HWND)window->window,
447 GWL_WNDPROC, (LONG)Manage );
448 p_plugin->p_hwnd = (HWND)window->window;
449 SetProp( p_plugin->p_hwnd, "w00t", (HANDLE)p_plugin );
450 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
451 UpdateWindow( p_plugin->p_hwnd );
455 p_plugin->window = (Window) window->window;
456 p_plugin->p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
458 Widget w = XtWindowToWidget( p_plugin->p_display, p_plugin->window );
459 XtAddEventHandler( w, ExposureMask, FALSE,
460 (XtEventHandler)Redraw, p_plugin );
461 Redraw( w, (XtPointer)p_plugin, NULL );
464 p_plugin->p_npwin = window;
466 p_plugin->i_width = window->width;
467 p_plugin->i_height = window->height;
469 if( !p_plugin->b_stream )
471 int i_mode = PLAYLIST_APPEND;
473 if( p_plugin->b_autoplay )
475 i_mode |= PLAYLIST_GO;
478 if( p_plugin->psz_target )
481 VLC_AddTarget( p_plugin->i_vlc, p_plugin->psz_target,
482 0, 0, i_mode, PLAYLIST_END );
484 p_plugin->b_stream = VLC_TRUE;
488 return NPERR_NO_ERROR;
491 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
492 NPBool seekable, uint16 *stype )
494 if( instance == NULL )
496 return NPERR_INVALID_INSTANCE_ERROR;
500 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
503 /* fprintf(stderr, "NPP_NewStream - FILE mode !!\n"); */
505 /* We want a *filename* ! */
509 if( !p_plugin->b_stream )
511 p_plugin->psz_target = strdup( stream->url );
512 p_plugin->b_stream = VLC_TRUE;
516 return NPERR_NO_ERROR;
519 int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
520 * mode so we can take any size stream in our
521 * write call (since we ignore it) */
523 #define SARASS_SIZE (1024*1024)
525 int32 NPP_WriteReady( NPP instance, NPStream *stream )
529 /* fprintf(stderr, "NPP_WriteReady\n"); */
531 if (instance != NULL)
533 p_plugin = (VlcPlugin*) instance->pdata;
534 /* Muahahahahahahaha */
535 return STREAMBUFSIZE;
536 /*return SARASS_SIZE;*/
539 /* Number of bytes ready to accept in NPP_Write() */
540 return STREAMBUFSIZE;
545 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
546 int32 len, void *buffer )
548 /* fprintf(stderr, "NPP_Write %i\n", (int)len); */
550 if( instance != NULL )
552 /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
555 return len; /* The number of bytes accepted */
559 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
561 if( instance == NULL )
563 return NPERR_INVALID_INSTANCE_ERROR;
566 return NPERR_NO_ERROR;
570 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
572 if( instance == NULL )
577 /* fprintf(stderr, "NPP_StreamAsFile %s\n", fname); */
580 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
582 VLC_AddTarget( p_plugin->i_vlc, fname, 0, 0,
583 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
588 void NPP_URLNotify( NPP instance, const char* url,
589 NPReason reason, void* notifyData )
591 /***** Insert NPP_URLNotify code here *****\
592 PluginInstance* p_plugin;
593 if (instance != NULL)
594 p_plugin = (PluginInstance*) instance->pdata;
595 \*********************************************/
599 void NPP_Print( NPP instance, NPPrint* printInfo )
601 if( printInfo == NULL )
606 if( instance != NULL )
608 /***** Insert NPP_Print code here *****\
609 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
610 \**************************************/
612 if( printInfo->mode == NP_FULL )
616 * If your plugin would like to take over
617 * printing completely when it is in full-screen mode,
618 * set printInfo->pluginPrinted to TRUE and print your
619 * plugin as you see fit. If your plugin wants Netscape
620 * to handle printing in this case, set
621 * printInfo->pluginPrinted to FALSE (the default) and
622 * do nothing. If you do want to handle printing
623 * yourself, printOne is true if the print button
624 * (as opposed to the print menu) was clicked.
625 * On the Macintosh, platformPrint is a THPrint; on
626 * Windows, platformPrint is a structure
627 * (defined in npapi.h) containing the printer name, port,
631 /***** Insert NPP_Print code here *****\
632 void* platformPrint =
633 printInfo->print.fullPrint.platformPrint;
635 printInfo->print.fullPrint.printOne;
636 \**************************************/
639 printInfo->print.fullPrint.pluginPrinted = FALSE;
643 /* If not fullscreen, we must be embedded */
646 * If your plugin is embedded, or is full-screen
647 * but you returned false in pluginPrinted above, NPP_Print
648 * will be called with mode == NP_EMBED. The NPWindow
649 * in the printInfo gives the location and dimensions of
650 * the embedded plugin on the printed page. On the
651 * Macintosh, platformPrint is the printer port; on
652 * Windows, platformPrint is the handle to the printing
656 /***** Insert NPP_Print code here *****\
657 NPWindow* printWindow =
658 &(printInfo->print.embedPrint.window);
659 void* platformPrint =
660 printInfo->print.embedPrint.platformPrint;
661 \**************************************/
666 /******************************************************************************
667 * Windows-only methods
668 *****************************************************************************/
670 LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
672 VlcPlugin* p_plugin = (VlcPlugin*) GetProp( p_hwnd, "w00t" );
679 PAINTSTRUCT paintstruct;
683 hdc = BeginPaint( p_hwnd, &paintstruct );
685 GetClientRect( p_hwnd, &rect );
686 FillRect( hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH) );
687 TextOut( hdc, p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
688 WINDOW_TEXT, strlen(WINDOW_TEXT) );
690 EndPaint( p_hwnd, &paintstruct );
695 p_plugin->pf_wndproc( p_hwnd, i_msg, wpar, lpar );
702 /******************************************************************************
704 *****************************************************************************/
706 static void Redraw( Widget w, XtPointer closure, XEvent *event )
708 VlcPlugin* p_plugin = (VlcPlugin*)closure;
712 gcv.foreground = BlackPixel( p_plugin->p_display, 0 );
713 gc = XCreateGC( p_plugin->p_display, p_plugin->window, GCForeground, &gcv );
715 XFillRectangle( p_plugin->p_display, p_plugin->window, gc,
716 0, 0, p_plugin->i_width, p_plugin->i_height );
718 gcv.foreground = WhitePixel( p_plugin->p_display, 0 );
719 XChangeGC( p_plugin->p_display, gc, GCForeground, &gcv );
721 XDrawString( p_plugin->p_display, p_plugin->window, gc,
722 p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
723 WINDOW_TEXT, strlen(WINDOW_TEXT) );
725 XFreeGC( p_plugin->p_display, gc );