1 /*****************************************************************************
2 * vlcshell.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: vlcshell.cpp,v 1.13 2003/07/09 13:52:22 sam 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 *****************************************************************************/
48 # include <X11/Xlib.h>
49 # include <X11/Intrinsic.h>
50 # include <X11/StringDefs.h>
55 # include <QuickDraw.h>
59 #include "vlcplugin.h"
62 # define WINDOW_TEXT "(no picture)"
64 # define WINDOW_TEXT "(no libvlc)"
67 /*****************************************************************************
68 * Unix-only declarations
69 ******************************************************************************/
71 # define VOUT_PLUGINS "xvideo,x11,dummy"
72 # define AOUT_PLUGINS "oss,dummy"
74 static void Redraw( Widget w, XtPointer closure, XEvent *event );
77 /*****************************************************************************
78 * Windows-only declarations
79 *****************************************************************************/
81 # define VOUT_PLUGINS "directx,dummy"
82 # define AOUT_PLUGINS "none" /* "directx,waveout,dummy" */
84 HINSTANCE g_hDllInstance = NULL;
87 DllMain( HINSTANCE hinstDLL, // handle of DLL module
88 DWORD fdwReason, // reason for calling function
93 case DLL_PROCESS_ATTACH:
94 g_hDllInstance = hinstDLL;
96 case DLL_THREAD_ATTACH:
97 case DLL_PROCESS_DETACH:
98 case DLL_THREAD_DETACH:
104 LRESULT CALLBACK Manage( HWND, UINT, WPARAM, LPARAM );
107 /******************************************************************************
108 * UNIX-only API calls
109 *****************************************************************************/
110 char * NPP_GetMIMEDescription( void )
112 return PLUGIN_MIMETYPES;
115 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
117 static nsIID nsid = VLCINTF_IID;
118 static char psz_desc[1000];
122 case NPPVpluginNameString:
123 *((char **)value) = PLUGIN_NAME;
124 return NPERR_NO_ERROR;
126 case NPPVpluginDescriptionString:
128 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, VLC_Version() );
130 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, "(disabled)" );
132 psz_desc[1000-1] = 0;
133 *((char **)value) = psz_desc;
134 return NPERR_NO_ERROR;
141 if( instance == NULL )
143 return NPERR_INVALID_INSTANCE_ERROR;
146 VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;
150 case NPPVpluginScriptableInstance:
151 *(nsISupports**)value = p_plugin->GetPeer();
152 if( *(nsISupports**)value == NULL )
154 return NPERR_OUT_OF_MEMORY_ERROR;
158 case NPPVpluginScriptableIID:
159 *(nsIID**)value = (nsIID*)NPN_MemAlloc( sizeof(nsIID) );
160 if( *(nsIID**)value == NULL )
162 return NPERR_OUT_OF_MEMORY_ERROR;
164 **(nsIID**)value = nsid;
168 return NPERR_GENERIC_ERROR;
171 return NPERR_NO_ERROR;
174 /******************************************************************************
176 *****************************************************************************/
178 int16 NPP_HandleEvent( NPP instance, void * event )
180 if( instance == NULL )
185 Boolean eventHandled = false;
188 TPlugin *pPlugin = (TPlugin*)instance->pdata;
189 if( pPlugin != NULL && event != NULL )
191 eventHandled = pPlugin->HandleEvent( *(EventRecord*)event );
199 /******************************************************************************
200 * General Plug-in Calls
201 *****************************************************************************/
202 NPError NPP_Initialize( void )
204 return NPERR_NO_ERROR;
207 jref NPP_GetJavaClass( void )
212 void NPP_Shutdown( void )
217 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
218 char* argn[], char* argv[], NPSavedData* saved )
228 /*, "--plugin-path", "/home/sam/videolan/vlc_MAIN/plugins"*/
232 if( instance == NULL )
234 return NPERR_INVALID_INSTANCE_ERROR;
237 VlcPlugin * p_plugin = new VlcPlugin( instance );
239 if( p_plugin == NULL )
241 return NPERR_OUT_OF_MEMORY_ERROR;
244 instance->pdata = p_plugin;
247 p_plugin->p_hwnd = NULL;
248 p_plugin->pf_wndproc = NULL;
252 p_plugin->window = 0;
253 p_plugin->p_display = NULL;
256 p_plugin->p_npwin = NULL;
257 p_plugin->i_npmode = mode;
258 p_plugin->i_width = 0;
259 p_plugin->i_height = 0;
262 p_plugin->i_vlc = VLC_Create();
263 if( p_plugin->i_vlc < 0 )
268 return NPERR_GENERIC_ERROR;
271 i_ret = VLC_Init( p_plugin->i_vlc, sizeof(ppsz_foo)/sizeof(char*), ppsz_foo );
274 VLC_Destroy( p_plugin->i_vlc );
278 return NPERR_GENERIC_ERROR;
281 value.psz_string = "dummy";
282 VLC_Set( p_plugin->i_vlc, "conf::intf", value );
283 value.psz_string = VOUT_PLUGINS;
284 VLC_Set( p_plugin->i_vlc, "conf::vout", value );
285 value.psz_string = AOUT_PLUGINS;
286 VLC_Set( p_plugin->i_vlc, "conf::aout", value );
291 #endif /* USE_LIBVLC */
293 p_plugin->b_stream = VLC_FALSE;
294 p_plugin->b_autoplay = VLC_FALSE;
295 p_plugin->psz_target = NULL;
297 for( i = 0; i < argc ; i++ )
299 if( !strcmp( argn[i], "target" ) )
301 p_plugin->psz_target = argv[i];
303 else if( !strcmp( argn[i], "autoplay" ) )
305 if( !strcmp( argv[i], "yes" ) )
307 p_plugin->b_autoplay = 1;
310 else if( !strcmp( argn[i], "autostart" ) )
312 if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "true" ) )
314 p_plugin->b_autoplay = 1;
317 else if( !strcmp( argn[i], "filename" ) )
319 p_plugin->psz_target = argv[i];
321 else if( !strcmp( argn[i], "src" ) )
323 p_plugin->psz_target = argv[i];
327 else if( !strcmp( argn[i], "loop" ) )
329 if( !strcmp( argv[i], "yes" ) )
331 value.b_bool = VLC_TRUE;
332 VLC_Set( p_plugin->i_vlc, "conf::loop", value );
338 if( p_plugin->psz_target )
340 p_plugin->psz_target = strdup( p_plugin->psz_target );
343 return NPERR_NO_ERROR;
346 NPError NPP_Destroy( NPP instance, NPSavedData** save )
348 if( instance == NULL )
350 return NPERR_INVALID_INSTANCE_ERROR;
353 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
355 if( p_plugin != NULL )
357 if( p_plugin->i_vlc )
360 VLC_Stop( p_plugin->i_vlc );
361 VLC_Destroy( p_plugin->i_vlc );
366 if( p_plugin->psz_target )
368 free( p_plugin->psz_target );
369 p_plugin->psz_target = NULL;
375 instance->pdata = NULL;
377 return NPERR_NO_ERROR;
380 NPError NPP_SetWindow( NPP instance, NPWindow* window )
382 if( instance == NULL )
384 return NPERR_INVALID_INSTANCE_ERROR;
387 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
389 /* Write the window ID for vlc */
393 /* FIXME: this cast sucks */
394 value.i_int = (int) (ptrdiff_t) (void *) window->window;
395 VLC_Set( p_plugin->i_vlc, "drawable", value );
400 * Before setting window to point to the
401 * new window, you may wish to compare the new window
402 * info to the previous window (if any) to note window
407 if( !window || !window->window )
409 /* Window was destroyed. Invalidate everything. */
410 if( p_plugin->p_npwin )
412 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
413 (LONG)p_plugin->pf_wndproc );
414 p_plugin->pf_wndproc = NULL;
415 p_plugin->p_hwnd = NULL;
418 p_plugin->p_npwin = window;
419 return NPERR_NO_ERROR;
422 if( p_plugin->p_npwin )
424 if( p_plugin->p_hwnd == (HWND)window->window )
426 /* Same window, but something may have changed. First we
427 * update the plugin structure, then we redraw the window */
428 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
429 p_plugin->i_width = window->width;
430 p_plugin->i_height = window->height;
431 p_plugin->p_npwin = window;
432 UpdateWindow( p_plugin->p_hwnd );
433 return NPERR_NO_ERROR;
436 /* Window has changed. Destroy the one we have, and go
437 * on as if it was a real initialization. */
438 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
439 (LONG)p_plugin->pf_wndproc );
440 p_plugin->pf_wndproc = NULL;
441 p_plugin->p_hwnd = NULL;
444 p_plugin->pf_wndproc = (WNDPROC)SetWindowLong( (HWND)window->window,
445 GWL_WNDPROC, (LONG)Manage );
446 p_plugin->p_hwnd = (HWND)window->window;
447 SetProp( p_plugin->p_hwnd, "w00t", (HANDLE)p_plugin );
448 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
449 UpdateWindow( p_plugin->p_hwnd );
453 p_plugin->window = (Window) window->window;
454 p_plugin->p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
456 Widget w = XtWindowToWidget( p_plugin->p_display, p_plugin->window );
457 XtAddEventHandler( w, ExposureMask, FALSE,
458 (XtEventHandler)Redraw, p_plugin );
459 Redraw( w, (XtPointer)p_plugin, NULL );
462 p_plugin->p_npwin = window;
464 p_plugin->i_width = window->width;
465 p_plugin->i_height = window->height;
467 if( !p_plugin->b_stream )
469 int i_mode = PLAYLIST_APPEND;
471 if( p_plugin->b_autoplay )
473 i_mode |= PLAYLIST_GO;
476 if( p_plugin->psz_target )
479 VLC_AddTarget( p_plugin->i_vlc, p_plugin->psz_target,
480 i_mode, PLAYLIST_END );
482 p_plugin->b_stream = VLC_TRUE;
486 return NPERR_NO_ERROR;
489 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
490 NPBool seekable, uint16 *stype )
492 if( instance == NULL )
494 return NPERR_INVALID_INSTANCE_ERROR;
498 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
501 /* fprintf(stderr, "NPP_NewStream - FILE mode !!\n"); */
503 /* We want a *filename* ! */
507 if( !p_plugin->b_stream )
509 p_plugin->psz_target = strdup( stream->url );
510 p_plugin->b_stream = VLC_TRUE;
514 return NPERR_NO_ERROR;
517 int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
518 * mode so we can take any size stream in our
519 * write call (since we ignore it) */
521 #define SARASS_SIZE (1024*1024)
523 int32 NPP_WriteReady( NPP instance, NPStream *stream )
527 /* fprintf(stderr, "NPP_WriteReady\n"); */
529 if (instance != NULL)
531 p_plugin = (VlcPlugin*) instance->pdata;
532 /* Muahahahahahahaha */
533 return STREAMBUFSIZE;
534 /*return SARASS_SIZE;*/
537 /* Number of bytes ready to accept in NPP_Write() */
538 return STREAMBUFSIZE;
543 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
544 int32 len, void *buffer )
546 /* fprintf(stderr, "NPP_Write %i\n", (int)len); */
548 if( instance != NULL )
550 /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
553 return len; /* The number of bytes accepted */
557 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
559 if( instance == NULL )
561 return NPERR_INVALID_INSTANCE_ERROR;
564 return NPERR_NO_ERROR;
568 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
570 if( instance == NULL )
575 /* fprintf(stderr, "NPP_StreamAsFile %s\n", fname); */
578 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
580 VLC_AddTarget( p_plugin->i_vlc, fname,
581 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
586 void NPP_URLNotify( NPP instance, const char* url,
587 NPReason reason, void* notifyData )
589 /***** Insert NPP_URLNotify code here *****\
590 PluginInstance* p_plugin;
591 if (instance != NULL)
592 p_plugin = (PluginInstance*) instance->pdata;
593 \*********************************************/
597 void NPP_Print( NPP instance, NPPrint* printInfo )
599 if( printInfo == NULL )
604 if( instance != NULL )
606 /***** Insert NPP_Print code here *****\
607 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
608 \**************************************/
610 if( printInfo->mode == NP_FULL )
614 * If your plugin would like to take over
615 * printing completely when it is in full-screen mode,
616 * set printInfo->pluginPrinted to TRUE and print your
617 * plugin as you see fit. If your plugin wants Netscape
618 * to handle printing in this case, set
619 * printInfo->pluginPrinted to FALSE (the default) and
620 * do nothing. If you do want to handle printing
621 * yourself, printOne is true if the print button
622 * (as opposed to the print menu) was clicked.
623 * On the Macintosh, platformPrint is a THPrint; on
624 * Windows, platformPrint is a structure
625 * (defined in npapi.h) containing the printer name, port,
629 /***** Insert NPP_Print code here *****\
630 void* platformPrint =
631 printInfo->print.fullPrint.platformPrint;
633 printInfo->print.fullPrint.printOne;
634 \**************************************/
637 printInfo->print.fullPrint.pluginPrinted = FALSE;
641 /* If not fullscreen, we must be embedded */
644 * If your plugin is embedded, or is full-screen
645 * but you returned false in pluginPrinted above, NPP_Print
646 * will be called with mode == NP_EMBED. The NPWindow
647 * in the printInfo gives the location and dimensions of
648 * the embedded plugin on the printed page. On the
649 * Macintosh, platformPrint is the printer port; on
650 * Windows, platformPrint is the handle to the printing
654 /***** Insert NPP_Print code here *****\
655 NPWindow* printWindow =
656 &(printInfo->print.embedPrint.window);
657 void* platformPrint =
658 printInfo->print.embedPrint.platformPrint;
659 \**************************************/
664 /******************************************************************************
665 * Windows-only methods
666 *****************************************************************************/
668 LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
670 VlcPlugin* p_plugin = (VlcPlugin*) GetProp( p_hwnd, "w00t" );
677 PAINTSTRUCT paintstruct;
681 hdc = BeginPaint( p_hwnd, &paintstruct );
683 GetClientRect( p_hwnd, &rect );
684 FillRect( hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH) );
685 TextOut( hdc, p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
686 WINDOW_TEXT, strlen(WINDOW_TEXT) );
688 EndPaint( p_hwnd, &paintstruct );
693 p_plugin->pf_wndproc( p_hwnd, i_msg, wpar, lpar );
700 /******************************************************************************
702 *****************************************************************************/
704 static void Redraw( Widget w, XtPointer closure, XEvent *event )
706 VlcPlugin* p_plugin = (VlcPlugin*)closure;
710 gcv.foreground = BlackPixel( p_plugin->p_display, 0 );
711 gc = XCreateGC( p_plugin->p_display, p_plugin->window, GCForeground, &gcv );
713 XFillRectangle( p_plugin->p_display, p_plugin->window, gc,
714 0, 0, p_plugin->i_width, p_plugin->i_height );
716 gcv.foreground = WhitePixel( p_plugin->p_display, 0 );
717 XChangeGC( p_plugin->p_display, gc, GCForeground, &gcv );
719 XDrawString( p_plugin->p_display, p_plugin->window, gc,
720 p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
721 WINDOW_TEXT, strlen(WINDOW_TEXT) );
723 XFreeGC( p_plugin->p_display, gc );