1 /*****************************************************************************
2 * vlcshell.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: vlcshell.cpp,v 1.12 2003/04/09 17:27:51 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 *****************************************************************************/
47 # include <X11/Xlib.h>
48 # include <X11/Intrinsic.h>
49 # include <X11/StringDefs.h>
54 # include <QuickDraw.h>
58 #include "vlcplugin.h"
61 # define WINDOW_TEXT "(no picture)"
63 # define WINDOW_TEXT "(no libvlc)"
66 /*****************************************************************************
67 * Unix-only declarations
68 ******************************************************************************/
70 # define VOUT_PLUGINS "xvideo,x11,dummy"
71 # define AOUT_PLUGINS "oss,dummy"
73 static void Redraw( Widget w, XtPointer closure, XEvent *event );
76 /*****************************************************************************
77 * Windows-only declarations
78 *****************************************************************************/
80 # define VOUT_PLUGINS "directx,dummy"
81 # define AOUT_PLUGINS "none" /* "directx,waveout,dummy" */
83 HINSTANCE g_hDllInstance = NULL;
86 DllMain( HINSTANCE hinstDLL, // handle of DLL module
87 DWORD fdwReason, // reason for calling function
92 case DLL_PROCESS_ATTACH:
93 g_hDllInstance = hinstDLL;
95 case DLL_THREAD_ATTACH:
96 case DLL_PROCESS_DETACH:
97 case DLL_THREAD_DETACH:
103 LRESULT CALLBACK Manage( HWND, UINT, WPARAM, LPARAM );
106 /******************************************************************************
107 * UNIX-only API calls
108 *****************************************************************************/
109 char * NPP_GetMIMEDescription( void )
111 return PLUGIN_MIMETYPES;
114 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
116 static nsIID nsid = VLCINTF_IID;
117 static char psz_desc[1000];
121 case NPPVpluginNameString:
122 *((char **)value) = PLUGIN_NAME;
123 return NPERR_NO_ERROR;
125 case NPPVpluginDescriptionString:
127 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, VLC_Version() );
129 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, "(disabled)" );
131 psz_desc[1000-1] = 0;
132 *((char **)value) = psz_desc;
133 return NPERR_NO_ERROR;
140 if( instance == NULL )
142 return NPERR_INVALID_INSTANCE_ERROR;
145 VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;
149 case NPPVpluginScriptableInstance:
150 *(nsISupports**)value = p_plugin->GetPeer();
151 if( *(nsISupports**)value == NULL )
153 return NPERR_OUT_OF_MEMORY_ERROR;
157 case NPPVpluginScriptableIID:
158 *(nsIID**)value = (nsIID*)NPN_MemAlloc( sizeof(nsIID) );
159 if( *(nsIID**)value == NULL )
161 return NPERR_OUT_OF_MEMORY_ERROR;
163 **(nsIID**)value = nsid;
167 return NPERR_GENERIC_ERROR;
170 return NPERR_NO_ERROR;
173 /******************************************************************************
175 *****************************************************************************/
177 int16 NPP_HandleEvent( NPP instance, void * event )
179 if( instance == NULL )
184 Boolean eventHandled = false;
187 TPlugin *pPlugin = (TPlugin*)instance->pdata;
188 if( pPlugin != NULL && event != NULL )
190 eventHandled = pPlugin->HandleEvent( *(EventRecord*)event );
198 /******************************************************************************
199 * General Plug-in Calls
200 *****************************************************************************/
201 NPError NPP_Initialize( void )
203 return NPERR_NO_ERROR;
206 jref NPP_GetJavaClass( void )
211 void NPP_Shutdown( void )
216 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
217 char* argn[], char* argv[], NPSavedData* saved )
227 /*, "--plugin-path", "/home/sam/videolan/vlc_MAIN/plugins"*/
231 if( instance == NULL )
233 return NPERR_INVALID_INSTANCE_ERROR;
236 VlcPlugin * p_plugin = new VlcPlugin( instance );
238 if( p_plugin == NULL )
240 return NPERR_OUT_OF_MEMORY_ERROR;
243 instance->pdata = p_plugin;
246 p_plugin->p_hwnd = NULL;
247 p_plugin->pf_wndproc = NULL;
251 p_plugin->window = 0;
252 p_plugin->p_display = NULL;
255 p_plugin->p_npwin = NULL;
256 p_plugin->i_npmode = mode;
257 p_plugin->i_width = 0;
258 p_plugin->i_height = 0;
261 p_plugin->i_vlc = VLC_Create();
262 if( p_plugin->i_vlc < 0 )
267 return NPERR_GENERIC_ERROR;
270 i_ret = VLC_Init( p_plugin->i_vlc, sizeof(ppsz_foo)/sizeof(char*), ppsz_foo );
273 VLC_Destroy( p_plugin->i_vlc );
277 return NPERR_GENERIC_ERROR;
280 value.psz_string = "dummy";
281 VLC_Set( p_plugin->i_vlc, "conf::intf", value );
282 value.psz_string = VOUT_PLUGINS;
283 VLC_Set( p_plugin->i_vlc, "conf::vout", value );
284 value.psz_string = AOUT_PLUGINS;
285 VLC_Set( p_plugin->i_vlc, "conf::aout", value );
290 #endif /* USE_LIBVLC */
292 p_plugin->b_stream = VLC_FALSE;
293 p_plugin->b_autoplay = VLC_FALSE;
294 p_plugin->psz_target = NULL;
296 for( i = 0; i < argc ; i++ )
298 if( !strcmp( argn[i], "target" ) )
300 p_plugin->psz_target = argv[i];
302 else if( !strcmp( argn[i], "autoplay" ) )
304 if( !strcmp( argv[i], "yes" ) )
306 p_plugin->b_autoplay = 1;
309 else if( !strcmp( argn[i], "autostart" ) )
311 if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "true" ) )
313 p_plugin->b_autoplay = 1;
316 else if( !strcmp( argn[i], "filename" ) )
318 p_plugin->psz_target = argv[i];
320 else if( !strcmp( argn[i], "src" ) )
322 p_plugin->psz_target = argv[i];
326 else if( !strcmp( argn[i], "loop" ) )
328 if( !strcmp( argv[i], "yes" ) )
330 value.b_bool = VLC_TRUE;
331 VLC_Set( p_plugin->i_vlc, "conf::loop", value );
337 if( p_plugin->psz_target )
339 p_plugin->psz_target = strdup( p_plugin->psz_target );
342 return NPERR_NO_ERROR;
345 NPError NPP_Destroy( NPP instance, NPSavedData** save )
347 if( instance == NULL )
349 return NPERR_INVALID_INSTANCE_ERROR;
352 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
354 if( p_plugin != NULL )
356 if( p_plugin->i_vlc )
359 VLC_Stop( p_plugin->i_vlc );
360 VLC_Destroy( p_plugin->i_vlc );
365 if( p_plugin->psz_target )
367 free( p_plugin->psz_target );
368 p_plugin->psz_target = NULL;
374 instance->pdata = NULL;
376 return NPERR_NO_ERROR;
379 NPError NPP_SetWindow( NPP instance, NPWindow* window )
381 if( instance == NULL )
383 return NPERR_INVALID_INSTANCE_ERROR;
386 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
388 /* Write the window ID for vlc */
392 /* FIXME: this cast sucks */
393 value.i_int = (int) (ptrdiff_t) (void *) window->window;
394 VLC_Set( p_plugin->i_vlc, "drawable", value );
399 * Before setting window to point to the
400 * new window, you may wish to compare the new window
401 * info to the previous window (if any) to note window
406 if( !window || !window->window )
408 /* Window was destroyed. Invalidate everything. */
409 if( p_plugin->p_npwin )
411 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
412 (LONG)p_plugin->pf_wndproc );
413 p_plugin->pf_wndproc = NULL;
414 p_plugin->p_hwnd = NULL;
417 p_plugin->p_npwin = window;
418 return NPERR_NO_ERROR;
421 if( p_plugin->p_npwin )
423 if( p_plugin->p_hwnd == (HWND)window->window )
425 /* Same window, but something may have changed. First we
426 * update the plugin structure, then we redraw the window */
427 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
428 p_plugin->i_width = window->width;
429 p_plugin->i_height = window->height;
430 p_plugin->p_npwin = window;
431 UpdateWindow( p_plugin->p_hwnd );
432 return NPERR_NO_ERROR;
435 /* Window has changed. Destroy the one we have, and go
436 * on as if it was a real initialization. */
437 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
438 (LONG)p_plugin->pf_wndproc );
439 p_plugin->pf_wndproc = NULL;
440 p_plugin->p_hwnd = NULL;
443 p_plugin->pf_wndproc = (WNDPROC)SetWindowLong( (HWND)window->window,
444 GWL_WNDPROC, (LONG)Manage );
445 p_plugin->p_hwnd = (HWND)window->window;
446 SetProp( p_plugin->p_hwnd, "w00t", (HANDLE)p_plugin );
447 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
448 UpdateWindow( p_plugin->p_hwnd );
452 p_plugin->window = (Window) window->window;
453 p_plugin->p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
455 Widget w = XtWindowToWidget( p_plugin->p_display, p_plugin->window );
456 XtAddEventHandler( w, ExposureMask, FALSE,
457 (XtEventHandler)Redraw, p_plugin );
458 Redraw( w, (XtPointer)p_plugin, NULL );
461 p_plugin->p_npwin = window;
463 p_plugin->i_width = window->width;
464 p_plugin->i_height = window->height;
466 if( !p_plugin->b_stream )
468 int i_mode = PLAYLIST_APPEND;
470 if( p_plugin->b_autoplay )
472 i_mode |= PLAYLIST_GO;
475 if( p_plugin->psz_target )
478 VLC_AddTarget( p_plugin->i_vlc, p_plugin->psz_target,
479 i_mode, PLAYLIST_END );
481 p_plugin->b_stream = VLC_TRUE;
485 return NPERR_NO_ERROR;
488 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
489 NPBool seekable, uint16 *stype )
491 if( instance == NULL )
493 return NPERR_INVALID_INSTANCE_ERROR;
497 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
500 /* fprintf(stderr, "NPP_NewStream - FILE mode !!\n"); */
502 /* We want a *filename* ! */
506 if( !p_plugin->b_stream )
508 p_plugin->psz_target = strdup( stream->url );
509 p_plugin->b_stream = VLC_TRUE;
513 return NPERR_NO_ERROR;
516 int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
517 * mode so we can take any size stream in our
518 * write call (since we ignore it) */
520 #define SARASS_SIZE (1024*1024)
522 int32 NPP_WriteReady( NPP instance, NPStream *stream )
526 /* fprintf(stderr, "NPP_WriteReady\n"); */
528 if (instance != NULL)
530 p_plugin = (VlcPlugin*) instance->pdata;
531 /* Muahahahahahahaha */
532 return STREAMBUFSIZE;
533 /*return SARASS_SIZE;*/
536 /* Number of bytes ready to accept in NPP_Write() */
537 return STREAMBUFSIZE;
542 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
543 int32 len, void *buffer )
545 /* fprintf(stderr, "NPP_Write %i\n", (int)len); */
547 if( instance != NULL )
549 /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
552 return len; /* The number of bytes accepted */
556 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
558 if( instance == NULL )
560 return NPERR_INVALID_INSTANCE_ERROR;
563 return NPERR_NO_ERROR;
567 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
569 if( instance == NULL )
574 /* fprintf(stderr, "NPP_StreamAsFile %s\n", fname); */
577 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
579 VLC_AddTarget( p_plugin->i_vlc, fname,
580 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
585 void NPP_URLNotify( NPP instance, const char* url,
586 NPReason reason, void* notifyData )
588 /***** Insert NPP_URLNotify code here *****\
589 PluginInstance* p_plugin;
590 if (instance != NULL)
591 p_plugin = (PluginInstance*) instance->pdata;
592 \*********************************************/
596 void NPP_Print( NPP instance, NPPrint* printInfo )
598 if( printInfo == NULL )
603 if( instance != NULL )
605 /***** Insert NPP_Print code here *****\
606 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
607 \**************************************/
609 if( printInfo->mode == NP_FULL )
613 * If your plugin would like to take over
614 * printing completely when it is in full-screen mode,
615 * set printInfo->pluginPrinted to TRUE and print your
616 * plugin as you see fit. If your plugin wants Netscape
617 * to handle printing in this case, set
618 * printInfo->pluginPrinted to FALSE (the default) and
619 * do nothing. If you do want to handle printing
620 * yourself, printOne is true if the print button
621 * (as opposed to the print menu) was clicked.
622 * On the Macintosh, platformPrint is a THPrint; on
623 * Windows, platformPrint is a structure
624 * (defined in npapi.h) containing the printer name, port,
628 /***** Insert NPP_Print code here *****\
629 void* platformPrint =
630 printInfo->print.fullPrint.platformPrint;
632 printInfo->print.fullPrint.printOne;
633 \**************************************/
636 printInfo->print.fullPrint.pluginPrinted = FALSE;
640 /* If not fullscreen, we must be embedded */
643 * If your plugin is embedded, or is full-screen
644 * but you returned false in pluginPrinted above, NPP_Print
645 * will be called with mode == NP_EMBED. The NPWindow
646 * in the printInfo gives the location and dimensions of
647 * the embedded plugin on the printed page. On the
648 * Macintosh, platformPrint is the printer port; on
649 * Windows, platformPrint is the handle to the printing
653 /***** Insert NPP_Print code here *****\
654 NPWindow* printWindow =
655 &(printInfo->print.embedPrint.window);
656 void* platformPrint =
657 printInfo->print.embedPrint.platformPrint;
658 \**************************************/
663 /******************************************************************************
664 * Windows-only methods
665 *****************************************************************************/
667 LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
669 VlcPlugin* p_plugin = (VlcPlugin*) GetProp( p_hwnd, "w00t" );
676 PAINTSTRUCT paintstruct;
680 hdc = BeginPaint( p_hwnd, &paintstruct );
682 GetClientRect( p_hwnd, &rect );
683 FillRect( hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH) );
684 TextOut( hdc, p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
685 WINDOW_TEXT, strlen(WINDOW_TEXT) );
687 EndPaint( p_hwnd, &paintstruct );
692 p_plugin->pf_wndproc( p_hwnd, i_msg, wpar, lpar );
699 /******************************************************************************
701 *****************************************************************************/
703 static void Redraw( Widget w, XtPointer closure, XEvent *event )
705 VlcPlugin* p_plugin = (VlcPlugin*)closure;
709 gcv.foreground = BlackPixel( p_plugin->p_display, 0 );
710 gc = XCreateGC( p_plugin->p_display, p_plugin->window, GCForeground, &gcv );
712 XFillRectangle( p_plugin->p_display, p_plugin->window, gc,
713 0, 0, p_plugin->i_width, p_plugin->i_height );
715 gcv.foreground = WhitePixel( p_plugin->p_display, 0 );
716 XChangeGC( p_plugin->p_display, gc, GCForeground, &gcv );
718 XDrawString( p_plugin->p_display, p_plugin->window, gc,
719 p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
720 WINDOW_TEXT, strlen(WINDOW_TEXT) );
722 XFreeGC( p_plugin->p_display, gc );