1 /*****************************************************************************
2 * vlcshell.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: vlcshell.cpp,v 1.24 2003/09/23 16:07: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 <Quickdraw.h>
54 #include "vlcplugin.h"
57 # define WINDOW_TEXT "(no picture)"
59 # define WINDOW_TEXT "(no libvlc)"
62 /* No, I really don't want to use XP_UNIX stuff on MacOSX */
69 # include <X11/Xlib.h>
70 # include <X11/Intrinsic.h>
71 # include <X11/StringDefs.h>
74 /*****************************************************************************
75 * Unix-only declarations
76 ******************************************************************************/
78 # define VOUT_PLUGINS "xvideo,x11,dummy"
79 # define AOUT_PLUGINS "oss,dummy"
81 static void Redraw( Widget w, XtPointer closure, XEvent *event );
84 /*****************************************************************************
85 * MacOS-only declarations
86 ******************************************************************************/
88 # define VOUT_PLUGINS "macosx"
89 # define AOUT_PLUGINS "macosx"
93 /*****************************************************************************
94 * Windows-only declarations
95 *****************************************************************************/
97 # define VOUT_PLUGINS "directx,wingdi,dummy"
98 # define AOUT_PLUGINS "directx,waveout,dummy"
100 HINSTANCE g_hDllInstance = NULL;
102 BOOL WINAPI DllMain( HINSTANCE hinstDLL, /* handle of DLL module */
103 DWORD fdwReason, /* reason for calling the function */
108 case DLL_PROCESS_ATTACH:
109 g_hDllInstance = hinstDLL;
111 case DLL_THREAD_ATTACH:
112 case DLL_PROCESS_DETACH:
113 case DLL_THREAD_DETACH:
119 LRESULT CALLBACK Manage( HWND, UINT, WPARAM, LPARAM );
122 /******************************************************************************
123 * UNIX-only API calls
124 *****************************************************************************/
125 char * NPP_GetMIMEDescription( void )
127 return PLUGIN_MIMETYPES;
130 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
133 static nsIID nsid = VLCINTF_IID;
134 static char psz_desc[1000];
138 case NPPVpluginNameString:
139 *((char **)value) = PLUGIN_NAME;
140 return NPERR_NO_ERROR;
142 case NPPVpluginDescriptionString:
144 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, VLC_Version() );
146 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, "(disabled)" );
148 psz_desc[1000-1] = 0;
149 *((char **)value) = psz_desc;
150 return NPERR_NO_ERROR;
157 if( instance == NULL )
159 return NPERR_INVALID_INSTANCE_ERROR;
162 VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;
166 case NPPVpluginScriptableInstance:
167 *(nsISupports**)value = p_plugin->GetPeer();
168 if( *(nsISupports**)value == NULL )
170 return NPERR_OUT_OF_MEMORY_ERROR;
174 case NPPVpluginScriptableIID:
175 *(nsIID**)value = (nsIID*)NPN_MemAlloc( sizeof(nsIID) );
176 if( *(nsIID**)value == NULL )
178 return NPERR_OUT_OF_MEMORY_ERROR;
180 **(nsIID**)value = nsid;
184 return NPERR_GENERIC_ERROR;
187 return NPERR_NO_ERROR;
190 /******************************************************************************
192 *****************************************************************************/
194 int16 NPP_HandleEvent( NPP instance, void * event )
196 VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
199 if( instance == NULL )
204 EventRecord *pouetEvent = (EventRecord*)event;
206 if (pouetEvent->what == 6)
209 VLC_Set( p_plugin->i_vlc, "drawableredraw", value );
213 Boolean eventHandled = false;
219 /******************************************************************************
220 * General Plug-in Calls
221 *****************************************************************************/
222 NPError NPP_Initialize( void )
224 return NPERR_NO_ERROR;
227 jref NPP_GetJavaClass( void )
232 void NPP_Shutdown( void )
237 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
238 char* argn[], char* argv[], NPSavedData* saved )
248 if( instance == NULL )
250 return NPERR_INVALID_INSTANCE_ERROR;
253 VlcPlugin * p_plugin = new VlcPlugin( instance );
255 if( p_plugin == NULL )
257 return NPERR_OUT_OF_MEMORY_ERROR;
260 instance->pdata = p_plugin;
263 p_plugin->p_hwnd = NULL;
264 p_plugin->pf_wndproc = NULL;
268 p_plugin->window = 0;
269 p_plugin->p_display = NULL;
272 p_plugin->p_npwin = NULL;
273 p_plugin->i_npmode = mode;
274 p_plugin->i_width = 0;
275 p_plugin->i_height = 0;
278 p_plugin->i_vlc = VLC_Create();
279 if( p_plugin->i_vlc < 0 )
284 return NPERR_GENERIC_ERROR;
292 char *ppsz_argv[] = { "vlc", "--plugin-path", NULL };
294 home_user = strdup( getenv("HOME") );
295 directory = strdup( "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
296 "Contents/MacOS/modules" );
297 plugin_path = malloc( strlen( directory ) + strlen( home_user ) );
298 memcpy( plugin_path , home_user , strlen(home_user) );
299 memcpy( plugin_path + strlen( home_user ) , directory ,
300 strlen( directory ) );
302 ppsz_argv[2] = plugin_path;
304 #elif defined(XP_WIN)
305 char *ppsz_argv[] = { "vlc", "--plugin-path", NULL, "-vv" };
307 DWORD i_type, i_data;
308 char p_data[MAX_PATH + 1];
310 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
311 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
313 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
314 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
316 if( i_type == REG_SZ )
318 strcat( p_data, "\\plugins" );
319 ppsz_argv[2] = p_data;
322 RegCloseKey( h_key );
325 if( !ppsz_argv[2] ) ppsz_argv[2] = ".";
331 /*, "--plugin-path", "/home/sam/videolan/vlc_MAIN/plugins"*/
336 i_ret = VLC_Init( p_plugin->i_vlc, sizeof(ppsz_argv)/sizeof(char*),
348 VLC_Destroy( p_plugin->i_vlc );
352 return NPERR_GENERIC_ERROR;
355 value.psz_string = "dummy";
356 VLC_Set( p_plugin->i_vlc, "conf::intf", value );
357 value.psz_string = VOUT_PLUGINS;
358 VLC_Set( p_plugin->i_vlc, "conf::vout", value );
359 value.psz_string = AOUT_PLUGINS;
360 VLC_Set( p_plugin->i_vlc, "conf::aout", value );
365 #endif /* USE_LIBVLC */
367 p_plugin->b_stream = VLC_FALSE;
368 p_plugin->b_autoplay = VLC_FALSE;
369 p_plugin->psz_target = NULL;
371 for( i = 0; i < argc ; i++ )
373 if( !strcmp( argn[i], "target" ) )
375 p_plugin->psz_target = argv[i];
377 else if( !strcmp( argn[i], "autoplay" ) )
379 if( !strcmp( argv[i], "yes" ) )
381 p_plugin->b_autoplay = 1;
384 else if( !strcmp( argn[i], "autostart" ) )
386 if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "true" ) )
388 p_plugin->b_autoplay = 1;
391 else if( !strcmp( argn[i], "filename" ) )
393 p_plugin->psz_target = argv[i];
395 else if( !strcmp( argn[i], "src" ) )
397 p_plugin->psz_target = argv[i];
401 else if( !strcmp( argn[i], "loop" ) )
403 if( !strcmp( argv[i], "yes" ) )
405 value.b_bool = VLC_TRUE;
406 VLC_Set( p_plugin->i_vlc, "conf::loop", value );
412 if( p_plugin->psz_target )
414 p_plugin->psz_target = strdup( p_plugin->psz_target );
417 return NPERR_NO_ERROR;
420 NPError NPP_Destroy( NPP instance, NPSavedData** save )
422 if( instance == NULL )
424 return NPERR_INVALID_INSTANCE_ERROR;
427 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
429 if( p_plugin != NULL )
431 if( p_plugin->i_vlc )
434 VLC_Stop( p_plugin->i_vlc );
435 VLC_Destroy( p_plugin->i_vlc );
440 if( p_plugin->psz_target )
442 free( p_plugin->psz_target );
443 p_plugin->psz_target = NULL;
449 instance->pdata = NULL;
451 return NPERR_NO_ERROR;
454 NPError NPP_SetWindow( NPP instance, NPWindow* window )
466 vlc_value_t valueportx;
467 vlc_value_t valueporty;
472 if( instance == NULL )
474 return NPERR_INVALID_INSTANCE_ERROR;
477 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
479 /* Write the window ID for vlc */
483 value.i_int = ((NP_Port*) (window->window))->port;
484 VLC_Set( p_plugin->i_vlc, "drawable", value );
486 valueportx.i_int = ((NP_Port*) (window->window))->portx;
487 valueporty.i_int = ((NP_Port*) (window->window))->porty;
488 VLC_Set( p_plugin->i_vlc, "drawableportx", valueportx );
489 VLC_Set( p_plugin->i_vlc, "drawableporty", valueporty );
491 valuex.i_int = window->x;
492 valuey.i_int = window->y;
493 valuew.i_int = window->width;
494 valueh.i_int = window->height;
495 valuet.i_int = window->clipRect.top;
496 valuel.i_int = window->clipRect.left;
497 valueb.i_int = window->clipRect.bottom;
498 valuer.i_int = window->clipRect.right;
500 VLC_Set( p_plugin->i_vlc, "drawablet", valuet );
501 VLC_Set( p_plugin->i_vlc, "drawablel", valuel );
502 VLC_Set( p_plugin->i_vlc, "drawableb", valueb );
503 VLC_Set( p_plugin->i_vlc, "drawabler", valuer );
504 VLC_Set( p_plugin->i_vlc, "drawablex", valuex );
505 VLC_Set( p_plugin->i_vlc, "drawabley", valuey );
506 VLC_Set( p_plugin->i_vlc, "drawablew", valuew );
507 VLC_Set( p_plugin->i_vlc, "drawableh", valueh );
509 p_plugin->window = window;
511 /* draw the beautiful "No Picture" */
513 black_rect.top = valuet.i_int - valuey.i_int;
514 black_rect.left = valuel.i_int - valuex.i_int;
515 black_rect.bottom = valueb.i_int - valuey.i_int;
516 black_rect.right = valuer.i_int - valuex.i_int;
518 SetPort( value.i_int );
519 SetOrigin( valueportx.i_int , valueporty.i_int );
520 ForeColor(blackColor);
522 PaintRect( &black_rect );
524 ForeColor(whiteColor);
525 text = strdup( WINDOW_TEXT );
526 MoveTo( valuew.i_int / 2 - 40 , valueh.i_int / 2 );
527 DrawText( text , 0 , strlen(text) );
531 /* FIXME: this cast sucks */
532 value.i_int = (int) (ptrdiff_t) (void *) window->window;
533 VLC_Set( p_plugin->i_vlc, "drawable", value );
540 * Before setting window to point to the
541 * new window, you may wish to compare the new window
542 * info to the previous window (if any) to note window
547 if( !window || !window->window )
549 /* Window was destroyed. Invalidate everything. */
550 if( p_plugin->p_npwin )
552 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
553 (LONG)p_plugin->pf_wndproc );
554 p_plugin->pf_wndproc = NULL;
555 p_plugin->p_hwnd = NULL;
558 p_plugin->p_npwin = window;
559 return NPERR_NO_ERROR;
562 if( p_plugin->p_npwin )
564 if( p_plugin->p_hwnd == (HWND)window->window )
566 /* Same window, but something may have changed. First we
567 * update the plugin structure, then we redraw the window */
568 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
569 p_plugin->i_width = window->width;
570 p_plugin->i_height = window->height;
571 p_plugin->p_npwin = window;
572 UpdateWindow( p_plugin->p_hwnd );
573 return NPERR_NO_ERROR;
576 /* Window has changed. Destroy the one we have, and go
577 * on as if it was a real initialization. */
578 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
579 (LONG)p_plugin->pf_wndproc );
580 p_plugin->pf_wndproc = NULL;
581 p_plugin->p_hwnd = NULL;
584 p_plugin->pf_wndproc = (WNDPROC)SetWindowLong( (HWND)window->window,
585 GWL_WNDPROC, (LONG)Manage );
586 p_plugin->p_hwnd = (HWND)window->window;
587 SetProp( p_plugin->p_hwnd, "w00t", (HANDLE)p_plugin );
588 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
589 UpdateWindow( p_plugin->p_hwnd );
593 p_plugin->window = (Window) window->window;
594 p_plugin->p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
596 Widget w = XtWindowToWidget( p_plugin->p_display, p_plugin->window );
597 XtAddEventHandler( w, ExposureMask, FALSE,
598 (XtEventHandler)Redraw, p_plugin );
599 Redraw( w, (XtPointer)p_plugin, NULL );
602 p_plugin->p_npwin = window;
604 p_plugin->i_width = window->width;
605 p_plugin->i_height = window->height;
607 if( !p_plugin->b_stream )
609 int i_mode = PLAYLIST_APPEND;
611 if( p_plugin->b_autoplay )
613 i_mode |= PLAYLIST_GO;
616 if( p_plugin->psz_target )
619 VLC_AddTarget( p_plugin->i_vlc, p_plugin->psz_target,
620 0, 0, i_mode, PLAYLIST_END );
622 p_plugin->b_stream = VLC_TRUE;
626 return NPERR_NO_ERROR;
629 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
630 NPBool seekable, uint16 *stype )
632 if( instance == NULL )
634 return NPERR_INVALID_INSTANCE_ERROR;
638 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
641 /* fprintf(stderr, "NPP_NewStream - FILE mode !!\n"); */
643 /* We want a *filename* ! */
647 if( !p_plugin->b_stream )
649 p_plugin->psz_target = strdup( stream->url );
650 p_plugin->b_stream = VLC_TRUE;
654 return NPERR_NO_ERROR;
657 int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
658 * mode so we can take any size stream in our
659 * write call (since we ignore it) */
661 #define SARASS_SIZE (1024*1024)
663 int32 NPP_WriteReady( NPP instance, NPStream *stream )
667 /* fprintf(stderr, "NPP_WriteReady\n"); */
669 if (instance != NULL)
671 p_plugin = (VlcPlugin*) instance->pdata;
672 /* Muahahahahahahaha */
673 return STREAMBUFSIZE;
674 /*return SARASS_SIZE;*/
677 /* Number of bytes ready to accept in NPP_Write() */
678 return STREAMBUFSIZE;
683 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
684 int32 len, void *buffer )
686 /* fprintf(stderr, "NPP_Write %i\n", (int)len); */
688 if( instance != NULL )
690 /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
693 return len; /* The number of bytes accepted */
697 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
699 if( instance == NULL )
701 return NPERR_INVALID_INSTANCE_ERROR;
704 return NPERR_NO_ERROR;
708 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
710 if( instance == NULL )
715 /* fprintf(stderr, "NPP_StreamAsFile %s\n", fname); */
718 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
720 VLC_AddTarget( p_plugin->i_vlc, fname, 0, 0,
721 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
726 void NPP_URLNotify( NPP instance, const char* url,
727 NPReason reason, void* notifyData )
729 /***** Insert NPP_URLNotify code here *****\
730 PluginInstance* p_plugin;
731 if (instance != NULL)
732 p_plugin = (PluginInstance*) instance->pdata;
733 \*********************************************/
737 void NPP_Print( NPP instance, NPPrint* printInfo )
739 if( printInfo == NULL )
744 if( instance != NULL )
746 /***** Insert NPP_Print code here *****\
747 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
748 \**************************************/
750 if( printInfo->mode == NP_FULL )
754 * If your plugin would like to take over
755 * printing completely when it is in full-screen mode,
756 * set printInfo->pluginPrinted to TRUE and print your
757 * plugin as you see fit. If your plugin wants Netscape
758 * to handle printing in this case, set
759 * printInfo->pluginPrinted to FALSE (the default) and
760 * do nothing. If you do want to handle printing
761 * yourself, printOne is true if the print button
762 * (as opposed to the print menu) was clicked.
763 * On the Macintosh, platformPrint is a THPrint; on
764 * Windows, platformPrint is a structure
765 * (defined in npapi.h) containing the printer name, port,
769 /***** Insert NPP_Print code here *****\
770 void* platformPrint =
771 printInfo->print.fullPrint.platformPrint;
773 printInfo->print.fullPrint.printOne;
774 \**************************************/
777 printInfo->print.fullPrint.pluginPrinted = FALSE;
781 /* If not fullscreen, we must be embedded */
784 * If your plugin is embedded, or is full-screen
785 * but you returned false in pluginPrinted above, NPP_Print
786 * will be called with mode == NP_EMBED. The NPWindow
787 * in the printInfo gives the location and dimensions of
788 * the embedded plugin on the printed page. On the
789 * Macintosh, platformPrint is the printer port; on
790 * Windows, platformPrint is the handle to the printing
794 /***** Insert NPP_Print code here *****\
795 NPWindow* printWindow =
796 &(printInfo->print.embedPrint.window);
797 void* platformPrint =
798 printInfo->print.embedPrint.platformPrint;
799 \**************************************/
804 /******************************************************************************
805 * Windows-only methods
806 *****************************************************************************/
808 LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
810 VlcPlugin* p_plugin = (VlcPlugin*) GetProp( p_hwnd, "w00t" );
817 PAINTSTRUCT paintstruct;
821 hdc = BeginPaint( p_hwnd, &paintstruct );
823 GetClientRect( p_hwnd, &rect );
824 FillRect( hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH) );
825 TextOut( hdc, p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
826 WINDOW_TEXT, strlen(WINDOW_TEXT) );
828 EndPaint( p_hwnd, &paintstruct );
833 p_plugin->pf_wndproc( p_hwnd, i_msg, wpar, lpar );
840 /******************************************************************************
842 *****************************************************************************/
844 static void Redraw( Widget w, XtPointer closure, XEvent *event )
846 VlcPlugin* p_plugin = (VlcPlugin*)closure;
850 gcv.foreground = BlackPixel( p_plugin->p_display, 0 );
851 gc = XCreateGC( p_plugin->p_display, p_plugin->window, GCForeground, &gcv );
853 XFillRectangle( p_plugin->p_display, p_plugin->window, gc,
854 0, 0, p_plugin->i_width, p_plugin->i_height );
856 gcv.foreground = WhitePixel( p_plugin->p_display, 0 );
857 XChangeGC( p_plugin->p_display, gc, GCForeground, &gcv );
859 XDrawString( p_plugin->p_display, p_plugin->window, gc,
860 p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
861 WINDOW_TEXT, strlen(WINDOW_TEXT) );
863 XFreeGC( p_plugin->p_display, gc );