1 /*****************************************************************************
2 * vlcshell.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
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 *****************************************************************************/
42 #ifdef HAVE_MOZILLA_CONFIG_H
43 # include <mozilla-config.h>
45 #include <nsISupports.h>
50 #if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
52 #elif defined(XP_MACOSX)
62 # include <Quickdraw.h>
67 # include <X11/Xlib.h>
68 # include <X11/Intrinsic.h>
69 # include <X11/StringDefs.h>
73 #include "vlcplugin.h"
76 # define WINDOW_TEXT "(no picture)"
78 # define WINDOW_TEXT "(no libvlc)"
81 /*****************************************************************************
82 * Unix-only declarations
83 ******************************************************************************/
85 # define VOUT_PLUGINS "xvideo,x11,dummy"
86 # define AOUT_PLUGINS "oss,dummy"
88 static void Redraw( Widget w, XtPointer closure, XEvent *event );
91 /*****************************************************************************
92 * MacOS-only declarations
93 ******************************************************************************/
95 # define VOUT_PLUGINS "macosx"
96 # define AOUT_PLUGINS "macosx"
100 /*****************************************************************************
101 * Windows-only declarations
102 *****************************************************************************/
104 # define VOUT_PLUGINS "directx,wingdi,dummy"
105 # define AOUT_PLUGINS "directx,waveout,dummy"
107 #if defined(XP_WIN) && !USE_LIBVLC
108 LRESULT CALLBACK Manage( HWND, UINT, WPARAM, LPARAM );
112 /******************************************************************************
113 * UNIX-only API calls
114 *****************************************************************************/
115 char * NPP_GetMIMEDescription( void )
117 return PLUGIN_MIMETYPES;
120 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
123 static nsIID nsid = VLCINTF_IID;
124 static char psz_desc[1000];
128 case NPPVpluginNameString:
129 *((char **)value) = PLUGIN_NAME;
130 return NPERR_NO_ERROR;
132 case NPPVpluginDescriptionString:
134 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, VLC_Version() );
136 snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, "(disabled)" );
138 psz_desc[1000-1] = 0;
139 *((char **)value) = psz_desc;
140 return NPERR_NO_ERROR;
147 if( instance == NULL )
149 return NPERR_INVALID_INSTANCE_ERROR;
152 VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;
156 case NPPVpluginScriptableInstance:
157 *(nsISupports**)value = p_plugin->GetPeer();
158 if( *(nsISupports**)value == NULL )
160 return NPERR_OUT_OF_MEMORY_ERROR;
164 case NPPVpluginScriptableIID:
165 *(nsIID**)value = (nsIID*)NPN_MemAlloc( sizeof(nsIID) );
166 if( *(nsIID**)value == NULL )
168 return NPERR_OUT_OF_MEMORY_ERROR;
170 **(nsIID**)value = nsid;
174 return NPERR_GENERIC_ERROR;
177 return NPERR_NO_ERROR;
180 /******************************************************************************
182 *****************************************************************************/
184 int16 NPP_HandleEvent( NPP instance, void * event )
186 VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
189 if( instance == NULL )
194 EventRecord *pouetEvent = (EventRecord*)event;
196 if (pouetEvent->what == 6)
199 VLC_VariableSet( p_plugin->i_vlc, "drawableredraw", value );
203 Boolean eventHandled = false;
209 /******************************************************************************
210 * General Plug-in Calls
211 *****************************************************************************/
212 NPError NPP_Initialize( void )
214 return NPERR_NO_ERROR;
217 jref NPP_GetJavaClass( void )
222 void NPP_Shutdown( void )
227 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
228 char* argn[], char* argv[], NPSavedData* saved )
238 if( instance == NULL )
240 return NPERR_INVALID_INSTANCE_ERROR;
243 VlcPlugin * p_plugin = new VlcPlugin( instance );
245 if( p_plugin == NULL )
247 return NPERR_OUT_OF_MEMORY_ERROR;
250 instance->pdata = p_plugin;
253 p_plugin->p_hwnd = NULL;
254 p_plugin->pf_wndproc = NULL;
258 p_plugin->window = 0;
259 p_plugin->p_display = NULL;
262 p_plugin->p_npwin = NULL;
263 p_plugin->i_npmode = mode;
264 p_plugin->i_width = 0;
265 p_plugin->i_height = 0;
268 p_plugin->i_vlc = VLC_Create();
269 if( p_plugin->i_vlc < 0 )
274 return NPERR_GENERIC_ERROR;
282 char *ppsz_argv[] = { "vlc", "--plugin-path", NULL };
284 home_user = strdup( getenv("HOME") );
285 directory = strdup( "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
286 "Contents/MacOS/modules" );
287 plugin_path = malloc( strlen( directory ) + strlen( home_user ) );
288 memcpy( plugin_path , home_user , strlen(home_user) );
289 memcpy( plugin_path + strlen( home_user ) , directory ,
290 strlen( directory ) );
292 ppsz_argv[2] = plugin_path;
294 #elif defined(XP_WIN)
295 char *ppsz_argv[] = { NULL, "-vv" };
297 DWORD i_type, i_data = MAX_PATH + 1;
298 char p_data[MAX_PATH + 1];
299 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
300 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
302 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
303 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
305 if( i_type == REG_SZ )
307 strcat( p_data, "\\vlc" );
308 ppsz_argv[0] = p_data;
311 RegCloseKey( h_key );
314 if( !ppsz_argv[0] ) ppsz_argv[0] = "vlc";
320 /*, "--plugin-path", "/home/sam/videolan/vlc_MAIN/plugins"*/
325 i_ret = VLC_Init( p_plugin->i_vlc, sizeof(ppsz_argv)/sizeof(char*),
337 VLC_Destroy( p_plugin->i_vlc );
341 return NPERR_GENERIC_ERROR;
344 value.psz_string = "dummy";
345 VLC_VariableSet( p_plugin->i_vlc, "conf::intf", value );
346 value.psz_string = VOUT_PLUGINS;
347 VLC_VariableSet( p_plugin->i_vlc, "conf::vout", value );
348 value.psz_string = AOUT_PLUGINS;
349 VLC_VariableSet( p_plugin->i_vlc, "conf::aout", value );
354 #endif /* USE_LIBVLC */
356 p_plugin->b_stream = VLC_FALSE;
357 p_plugin->b_autoplay = VLC_FALSE;
358 p_plugin->psz_target = NULL;
360 for( i = 0; i < argc ; i++ )
362 if( !strcmp( argn[i], "target" ) )
364 p_plugin->psz_target = argv[i];
366 else if( !strcmp( argn[i], "autoplay" ) )
368 if( !strcmp( argv[i], "yes" ) )
370 p_plugin->b_autoplay = 1;
373 else if( !strcmp( argn[i], "autostart" ) )
375 if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "true" ) )
377 p_plugin->b_autoplay = 1;
380 else if( !strcmp( argn[i], "filename" ) )
382 p_plugin->psz_target = argv[i];
384 else if( !strcmp( argn[i], "src" ) )
386 p_plugin->psz_target = argv[i];
390 else if( !strcmp( argn[i], "loop" ) )
392 if( !strcmp( argv[i], "yes" ) )
394 value.b_bool = VLC_TRUE;
395 VLC_VariableSet( p_plugin->i_vlc, "conf::loop", value );
398 else if( !strcmp( argn[i], "fullscreen" ) )
400 if( !strcmp( argv[i], "yes" ) )
402 value.b_bool = VLC_TRUE;
403 VLC_VariableSet( p_plugin->i_vlc, "conf::fullscreen", value );
409 if( p_plugin->psz_target )
411 p_plugin->psz_target = strdup( p_plugin->psz_target );
414 return NPERR_NO_ERROR;
417 NPError NPP_Destroy( NPP instance, NPSavedData** save )
419 if( instance == NULL )
421 return NPERR_INVALID_INSTANCE_ERROR;
424 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
426 if( p_plugin != NULL )
428 if( p_plugin->i_vlc )
431 VLC_CleanUp( p_plugin->i_vlc );
432 VLC_Destroy( p_plugin->i_vlc );
437 if( p_plugin->psz_target )
439 free( p_plugin->psz_target );
440 p_plugin->psz_target = NULL;
446 instance->pdata = NULL;
448 return NPERR_NO_ERROR;
451 NPError NPP_SetWindow( NPP instance, NPWindow* window )
463 vlc_value_t valueportx;
464 vlc_value_t valueporty;
469 if( instance == NULL )
471 return NPERR_INVALID_INSTANCE_ERROR;
474 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
476 /* Write the window ID for vlc */
480 value.i_int = ((NP_Port*) (window->window))->port;
481 VLC_VariableSet( p_plugin->i_vlc, "drawable", value );
483 valueportx.i_int = ((NP_Port*) (window->window))->portx;
484 valueporty.i_int = ((NP_Port*) (window->window))->porty;
485 VLC_VariableSet( p_plugin->i_vlc, "drawableportx", valueportx );
486 VLC_VariableSet( p_plugin->i_vlc, "drawableporty", valueporty );
488 valuex.i_int = window->x;
489 valuey.i_int = window->y;
490 valuew.i_int = window->width;
491 valueh.i_int = window->height;
492 valuet.i_int = window->clipRect.top;
493 valuel.i_int = window->clipRect.left;
494 valueb.i_int = window->clipRect.bottom;
495 valuer.i_int = window->clipRect.right;
497 VLC_VariableSet( p_plugin->i_vlc, "drawablet", valuet );
498 VLC_VariableSet( p_plugin->i_vlc, "drawablel", valuel );
499 VLC_VariableSet( p_plugin->i_vlc, "drawableb", valueb );
500 VLC_VarialbeSet( p_plugin->i_vlc, "drawabler", valuer );
501 VLC_VariableSet( p_plugin->i_vlc, "drawablex", valuex );
502 VLC_VariableSet( p_plugin->i_vlc, "drawabley", valuey );
503 VLC_VariableSet( p_plugin->i_vlc, "drawablew", valuew );
504 VLC_VariableSet( p_plugin->i_vlc, "drawableh", valueh );
506 p_plugin->window = window;
508 /* draw the beautiful "No Picture" */
510 black_rect.top = valuet.i_int - valuey.i_int;
511 black_rect.left = valuel.i_int - valuex.i_int;
512 black_rect.bottom = valueb.i_int - valuey.i_int;
513 black_rect.right = valuer.i_int - valuex.i_int;
515 SetPort( value.i_int );
516 SetOrigin( valueportx.i_int , valueporty.i_int );
517 ForeColor(blackColor);
519 PaintRect( &black_rect );
521 ForeColor(whiteColor);
522 text = strdup( WINDOW_TEXT );
523 MoveTo( valuew.i_int / 2 - 40 , valueh.i_int / 2 );
524 DrawText( text , 0 , strlen(text) );
528 /* FIXME: this cast sucks */
529 value.i_int = (int) (ptrdiff_t) (void *) window->window;
530 VLC_VariableSet( p_plugin->i_vlc, "drawable", value );
537 * Before setting window to point to the
538 * new window, you may wish to compare the new window
539 * info to the previous window (if any) to note window
544 if( !window || !window->window )
546 /* Window was destroyed. Invalidate everything. */
547 if( p_plugin->p_npwin )
550 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
551 (LONG)p_plugin->pf_wndproc );
553 p_plugin->pf_wndproc = NULL;
554 p_plugin->p_hwnd = NULL;
557 p_plugin->p_npwin = window;
558 return NPERR_NO_ERROR;
561 if( p_plugin->p_npwin )
563 if( p_plugin->p_hwnd == (HWND)window->window )
565 /* Same window, but something may have changed. First we
566 * update the plugin structure, then we redraw the window */
567 p_plugin->i_width = window->width;
568 p_plugin->i_height = window->height;
569 p_plugin->p_npwin = window;
571 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
572 UpdateWindow( p_plugin->p_hwnd );
574 return NPERR_NO_ERROR;
577 /* Window has changed. Destroy the one we have, and go
578 * on as if it was a real initialization. */
580 SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
581 (LONG)p_plugin->pf_wndproc );
583 p_plugin->pf_wndproc = NULL;
584 p_plugin->p_hwnd = NULL;
588 p_plugin->pf_wndproc = (WNDPROC)SetWindowLong( (HWND)window->window,
589 GWL_WNDPROC, (LONG)Manage );
592 p_plugin->p_hwnd = (HWND)window->window;
593 SetProp( p_plugin->p_hwnd, "w00t", (HANDLE)p_plugin );
594 InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
595 UpdateWindow( p_plugin->p_hwnd );
599 p_plugin->window = (Window) window->window;
600 p_plugin->p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
602 Widget w = XtWindowToWidget( p_plugin->p_display, p_plugin->window );
603 XtAddEventHandler( w, ExposureMask, FALSE,
604 (XtEventHandler)Redraw, p_plugin );
605 Redraw( w, (XtPointer)p_plugin, NULL );
608 p_plugin->p_npwin = window;
610 p_plugin->i_width = window->width;
611 p_plugin->i_height = window->height;
613 if( !p_plugin->b_stream )
615 int i_mode = PLAYLIST_APPEND;
617 if( p_plugin->b_autoplay )
619 i_mode |= PLAYLIST_GO;
622 if( p_plugin->psz_target )
625 VLC_AddTarget( p_plugin->i_vlc, p_plugin->psz_target,
626 0, 0, i_mode, PLAYLIST_END );
628 p_plugin->b_stream = VLC_TRUE;
632 return NPERR_NO_ERROR;
635 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
636 NPBool seekable, uint16 *stype )
638 if( instance == NULL )
640 return NPERR_INVALID_INSTANCE_ERROR;
644 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
647 /* fprintf(stderr, "NPP_NewStream - FILE mode !!\n"); */
649 /* We want a *filename* ! */
653 if( !p_plugin->b_stream )
655 p_plugin->psz_target = strdup( stream->url );
656 p_plugin->b_stream = VLC_TRUE;
660 return NPERR_NO_ERROR;
663 int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
664 * mode so we can take any size stream in our
665 * write call (since we ignore it) */
667 #define SARASS_SIZE (1024*1024)
669 int32 NPP_WriteReady( NPP instance, NPStream *stream )
673 /* fprintf(stderr, "NPP_WriteReady\n"); */
675 if (instance != NULL)
677 p_plugin = (VlcPlugin*) instance->pdata;
678 /* Muahahahahahahaha */
679 return STREAMBUFSIZE;
680 /*return SARASS_SIZE;*/
683 /* Number of bytes ready to accept in NPP_Write() */
684 return STREAMBUFSIZE;
689 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
690 int32 len, void *buffer )
692 /* fprintf(stderr, "NPP_Write %i\n", (int)len); */
694 if( instance != NULL )
696 /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
699 return len; /* The number of bytes accepted */
703 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
705 if( instance == NULL )
707 return NPERR_INVALID_INSTANCE_ERROR;
710 return NPERR_NO_ERROR;
714 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
716 if( instance == NULL )
721 /* fprintf(stderr, "NPP_StreamAsFile %s\n", fname); */
724 VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
726 VLC_AddTarget( p_plugin->i_vlc, fname, 0, 0,
727 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
732 void NPP_URLNotify( NPP instance, const char* url,
733 NPReason reason, void* notifyData )
735 /***** Insert NPP_URLNotify code here *****\
736 PluginInstance* p_plugin;
737 if (instance != NULL)
738 p_plugin = (PluginInstance*) instance->pdata;
739 \*********************************************/
743 void NPP_Print( NPP instance, NPPrint* printInfo )
745 if( printInfo == NULL )
750 if( instance != NULL )
752 /***** Insert NPP_Print code here *****\
753 PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
754 \**************************************/
756 if( printInfo->mode == NP_FULL )
760 * If your plugin would like to take over
761 * printing completely when it is in full-screen mode,
762 * set printInfo->pluginPrinted to TRUE and print your
763 * plugin as you see fit. If your plugin wants Netscape
764 * to handle printing in this case, set
765 * printInfo->pluginPrinted to FALSE (the default) and
766 * do nothing. If you do want to handle printing
767 * yourself, printOne is true if the print button
768 * (as opposed to the print menu) was clicked.
769 * On the Macintosh, platformPrint is a THPrint; on
770 * Windows, platformPrint is a structure
771 * (defined in npapi.h) containing the printer name, port,
775 /***** Insert NPP_Print code here *****\
776 void* platformPrint =
777 printInfo->print.fullPrint.platformPrint;
779 printInfo->print.fullPrint.printOne;
780 \**************************************/
783 printInfo->print.fullPrint.pluginPrinted = FALSE;
787 /* If not fullscreen, we must be embedded */
790 * If your plugin is embedded, or is full-screen
791 * but you returned false in pluginPrinted above, NPP_Print
792 * will be called with mode == NP_EMBED. The NPWindow
793 * in the printInfo gives the location and dimensions of
794 * the embedded plugin on the printed page. On the
795 * Macintosh, platformPrint is the printer port; on
796 * Windows, platformPrint is the handle to the printing
800 /***** Insert NPP_Print code here *****\
801 NPWindow* printWindow =
802 &(printInfo->print.embedPrint.window);
803 void* platformPrint =
804 printInfo->print.embedPrint.platformPrint;
805 \**************************************/
810 /******************************************************************************
811 * Windows-only methods
812 *****************************************************************************/
813 #if defined(XP_WIN) && !USE_LIBVLC
814 LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
816 VlcPlugin* p_plugin = (VlcPlugin*) GetProp( p_hwnd, "w00t" );
822 PAINTSTRUCT paintstruct;
826 hdc = BeginPaint( p_hwnd, &paintstruct );
828 GetClientRect( p_hwnd, &rect );
829 FillRect( hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH) );
830 TextOut( hdc, p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
831 WINDOW_TEXT, strlen(WINDOW_TEXT) );
833 EndPaint( p_hwnd, &paintstruct );
837 p_plugin->pf_wndproc( p_hwnd, i_msg, wpar, lpar );
844 /******************************************************************************
846 *****************************************************************************/
848 static void Redraw( Widget w, XtPointer closure, XEvent *event )
850 VlcPlugin* p_plugin = (VlcPlugin*)closure;
854 gcv.foreground = BlackPixel( p_plugin->p_display, 0 );
855 gc = XCreateGC( p_plugin->p_display, p_plugin->window, GCForeground, &gcv );
857 XFillRectangle( p_plugin->p_display, p_plugin->window, gc,
858 0, 0, p_plugin->i_width, p_plugin->i_height );
860 gcv.foreground = WhitePixel( p_plugin->p_display, 0 );
861 XChangeGC( p_plugin->p_display, gc, GCForeground, &gcv );
863 XDrawString( p_plugin->p_display, p_plugin->window, gc,
864 p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
865 WINDOW_TEXT, strlen(WINDOW_TEXT) );
867 XFreeGC( p_plugin->p_display, gc );