* ./mozilla/*: added hooks so that the vlc plugin is scriptable from
[vlc.git] / mozilla / vlcshell.cpp
1 /*****************************************************************************
2  * vlcshell.c: a VideoLAN Client plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: vlcshell.cpp,v 1.1 2002/09/17 08:18:24 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdio.h>
28 #include <string.h>
29
30 /* Mozilla stuff */
31 #include <plugin/npapi.h>
32
33 /* X11 stuff */
34 #include <X11/Xlib.h>
35 #include <X11/Intrinsic.h>
36 #include <X11/StringDefs.h>
37
38 /* vlc stuff */
39 #include <vlc/vlc.h>
40 #include "config.h"
41
42 #include "vlcpeer.h"
43 #include "vlcplugin.h"
44
45 /******************************************************************************
46  * Unix-only declarations
47  ******************************************************************************/
48 #ifndef WIN32
49 static void Redraw( Widget w, XtPointer closure, XEvent *event );
50 #endif
51
52 /******************************************************************************
53  * Windows-only declarations
54  *****************************************************************************/
55 #ifdef WIN32
56 HINSTANCE g_hDllInstance = NULL;
57
58 BOOL WINAPI
59 DllMain( HINSTANCE  hinstDLL,                   // handle of DLL module
60                     DWORD  fdwReason,       // reason for calling function
61                     LPVOID  lpvReserved)
62 {
63         switch (fdwReason) {
64                 case DLL_PROCESS_ATTACH:
65                   g_hDllInstance = hinstDLL;
66                   break;
67                 case DLL_THREAD_ATTACH:
68                 case DLL_PROCESS_DETACH:
69                 case DLL_THREAD_DETACH:
70                 break;
71         }
72         return TRUE;
73 }
74 #endif
75
76 /******************************************************************************
77  * UNIX-only API calls
78  *****************************************************************************/
79 char * NPP_GetMIMEDescription( void )
80 {
81     return PLUGIN_MIMETYPES;
82 }
83
84 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
85 {
86     static nsIID nsid = VLCINTF_IID;
87
88     switch( variable )
89     {
90         case NPPVpluginNameString:
91             *((char **)value) = PLUGIN_NAME;
92             return NPERR_NO_ERROR;
93
94         case NPPVpluginDescriptionString:
95             *((char **)value) = PLUGIN_DESCRIPTION;
96             return NPERR_NO_ERROR;
97     }
98
99     if( instance == NULL )
100     {
101         return NPERR_INVALID_INSTANCE_ERROR;
102     }
103
104     VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;
105
106     switch( variable )
107     {
108         case NPPVpluginScriptableInstance:
109             *(nsISupports**)value = p_plugin->getScriptable();
110             if( *(nsISupports**)value == NULL )
111             {
112                 return NPERR_OUT_OF_MEMORY_ERROR;
113             }
114             break;
115
116         case NPPVpluginScriptableIID:
117             *(nsIID**)value = (nsIID*)NPN_MemAlloc( sizeof(nsIID) );
118             if( *(nsIID**)value == NULL )
119             {
120                 return NPERR_OUT_OF_MEMORY_ERROR;
121             }
122             **(nsIID**)value = nsid;
123             break;
124
125         default:
126             return NPERR_GENERIC_ERROR;
127     }
128
129     return NPERR_NO_ERROR;
130 }
131
132 /******************************************************************************
133  * General Plug-in Calls
134  *****************************************************************************/
135 NPError NPP_Initialize( void )
136 {
137     return NPERR_NO_ERROR;
138 }
139
140 jref NPP_GetJavaClass( void )
141 {
142     return NULL;
143 }
144
145 void NPP_Shutdown( void )
146 {
147     ;
148 }
149
150 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
151                  char* argn[], char* argv[], NPSavedData* saved )
152 {
153     int i_ret;
154     int i;
155
156     char *ppsz_foo[] =
157     {
158         "vlc"
159         /*, "--plugin-path", "/home/sam/videolan/vlc_MAIN/plugins"*/
160         , "--vout", "xvideo,x11,dummy"
161         /*, "--aout", "none"*/
162         , "--intf", "dummy"
163         /*, "--noaudio"*/
164         , "-q"
165         /*, "-v"*/
166     };
167
168     if( instance == NULL )
169     {
170         return NPERR_INVALID_INSTANCE_ERROR;
171     }
172
173     VlcPlugin * p_plugin = new VlcPlugin( instance );
174
175     if( p_plugin == NULL )
176     {
177         return NPERR_OUT_OF_MEMORY_ERROR;
178     }
179
180     instance->pdata = p_plugin;
181
182     p_plugin->fMode = mode;
183     p_plugin->fWindow = NULL;
184     p_plugin->window = 0;
185
186     p_plugin->p_vlc = vlc_create_r();
187     if( p_plugin->p_vlc == NULL )
188     {
189         delete p_plugin;
190         p_plugin = NULL;
191         return NPERR_GENERIC_ERROR;
192     }
193
194     i_ret = vlc_init_r( p_plugin->p_vlc, sizeof(ppsz_foo)/sizeof(char*), ppsz_foo );
195     if( i_ret )
196     {
197         vlc_destroy_r( p_plugin->p_vlc );
198         p_plugin->p_vlc = NULL;
199         delete p_plugin;
200         p_plugin = NULL;
201         return NPERR_GENERIC_ERROR;
202     }
203
204     vlc_set_r( p_plugin->p_vlc, "vout", "xvideo,x11,dummy" );
205     vlc_set_r( p_plugin->p_vlc, "intf", "dummy" );
206     vlc_set_r( p_plugin->p_vlc, "audio", "0" );
207
208     i_ret = vlc_run_r( p_plugin->p_vlc );
209     if( i_ret )
210     {
211         vlc_destroy_r( p_plugin->p_vlc );
212         p_plugin->p_vlc = NULL;
213         delete p_plugin;
214         p_plugin = NULL;
215         return NPERR_GENERIC_ERROR;
216     }
217
218     p_plugin->b_stream = 0;
219     p_plugin->psz_target = NULL;
220
221     for( i = 0; i < argc ; i++ )
222     {
223         fprintf(stderr, "arg %i: '%s' = '%s'\n", i, argn[i], argv[i]);
224         if( !strcmp(argn[i],"target") )
225         {
226             fprintf(stderr, "target specified: %s\n", argv[i]);
227             p_plugin->psz_target = strdup( argv[i] );
228         }
229         else
230         {
231             /*vlc_set_r( p_plugin->psz_target, argn[i], argv[i] );*/
232         }
233     }
234
235     return NPERR_NO_ERROR;
236 }
237
238 NPError NPP_Destroy( NPP instance, NPSavedData** save )
239 {
240     if( instance == NULL )
241     {
242         return NPERR_INVALID_INSTANCE_ERROR;
243     }
244
245     VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
246
247     if( p_plugin != NULL )
248     {
249         if( p_plugin->p_vlc != NULL )
250         {
251             vlc_destroy_r( p_plugin->p_vlc );
252             p_plugin->p_vlc = NULL;
253         }
254
255         if( p_plugin->psz_target )
256         {
257             free( p_plugin->psz_target );
258             p_plugin->psz_target = NULL;
259         }
260
261         delete p_plugin;
262     }
263
264     instance->pdata = NULL;
265
266     return NPERR_NO_ERROR;
267 }
268
269 NPError NPP_SetWindow( NPP instance, NPWindow* window )
270 {
271     char psz_window[32];
272
273     if( instance == NULL )
274     {
275         return NPERR_INVALID_INSTANCE_ERROR;
276     }
277
278     VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
279
280     /* Write the window ID for vlc */
281     sprintf( psz_window, "%li", (long int)window->window );
282     vlc_set_r( p_plugin->p_vlc, "x11-drawable", psz_window );
283     vlc_set_r( p_plugin->p_vlc, "xvideo-drawable", psz_window );
284
285     /*
286      * PLUGIN DEVELOPERS:
287      *  Before setting window to point to the
288      *  new window, you may wish to compare the new window
289      *  info to the previous window (if any) to note window
290      *  size changes, etc.
291      */
292
293     Widget netscape_widget;
294
295     p_plugin->window = (Window) window->window;
296     p_plugin->x = window->x;
297     p_plugin->y = window->y;
298     p_plugin->width = window->width;
299     p_plugin->height = window->height;
300     p_plugin->display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
301
302     netscape_widget = XtWindowToWidget(p_plugin->display, p_plugin->window);
303     XtAddEventHandler(netscape_widget, ExposureMask, FALSE, (XtEventHandler)Redraw, p_plugin);
304     Redraw(netscape_widget, (XtPointer)p_plugin, NULL);
305
306     p_plugin->fWindow = window;
307
308 #if 1
309     if( !p_plugin->b_stream )
310     {
311         p_plugin->b_stream = 1;
312         if( p_plugin->psz_target )
313         {
314             vlc_add_target_r( p_plugin->p_vlc, p_plugin->psz_target,
315                               PLAYLIST_APPEND, PLAYLIST_END );
316             vlc_add_target_r( p_plugin->p_vlc, "vlc:loop",
317                               PLAYLIST_APPEND, PLAYLIST_END );
318         }
319     }
320 #endif
321
322     return NPERR_NO_ERROR;
323 }
324
325 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
326                        NPBool seekable, uint16 *stype )
327 {
328     if( instance == NULL )
329     {
330         return NPERR_INVALID_INSTANCE_ERROR;
331     }
332
333     VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
334
335     fprintf(stderr, "NPP_NewStream - FILE mode !!\n");
336
337     /* We want a *filename* ! */
338     *stype = NP_ASFILE;
339
340 #if 0
341     if( p_plugin->b_stream == 0 )
342     {
343         p_plugin->psz_target = strdup( stream->url );
344         p_plugin->b_stream = 1;
345     }
346 #endif
347
348     return NPERR_NO_ERROR;
349 }
350
351 int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
352                    * mode so we can take any size stream in our
353                    * write call (since we ignore it) */
354
355 #define SARASS_SIZE (1024*1024)
356
357 int32 NPP_WriteReady( NPP instance, NPStream *stream )
358 {
359     VlcPlugin* p_plugin;
360
361     fprintf(stderr, "NPP_WriteReady\n");
362
363     if (instance != NULL)
364     {
365         p_plugin = (VlcPlugin*) instance->pdata;
366         /* Muahahahahahahaha */
367         return STREAMBUFSIZE;
368         /*return SARASS_SIZE;*/
369     }
370
371     /* Number of bytes ready to accept in NPP_Write() */
372     return STREAMBUFSIZE;
373     /*return 0;*/
374 }
375
376
377 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
378                  int32 len, void *buffer )
379 {
380     fprintf(stderr, "NPP_Write %i\n", len);
381
382     if (instance != NULL)
383     {
384         /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
385     }
386
387     return len;         /* The number of bytes accepted */
388 }
389
390
391 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
392 {
393     if( instance == NULL )
394     {
395         return NPERR_INVALID_INSTANCE_ERROR;
396     }
397
398     VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;
399
400     return NPERR_NO_ERROR;
401 }
402
403
404 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
405 {
406     if( instance == NULL )
407     {
408         return;
409     }
410
411     VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
412
413     fprintf(stderr, "NPP_StreamAsFile\n");
414     vlc_add_target_r( p_plugin->p_vlc, fname, PLAYLIST_APPEND, PLAYLIST_END );
415     vlc_add_target_r( p_plugin->p_vlc, "vlc:loop", 
416                       PLAYLIST_APPEND, PLAYLIST_END );
417 }
418
419 #if 0
420 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
421 {
422     fprintf(stderr,"filename : %s\n", fname);
423     ((VlcPlugin*) instance->pdata)->SetFileName(fname);
424
425     fprintf(stderr,"SetFileNeme ok. \n");
426 }
427 #endif
428
429
430 void NPP_URLNotify( NPP instance, const char* url,
431                     NPReason reason, void* notifyData )
432 {
433     /***** Insert NPP_URLNotify code here *****\
434     PluginInstance* p_plugin;
435     if (instance != NULL)
436         p_plugin = (PluginInstance*) instance->pdata;
437     \*********************************************/
438 }
439
440
441 void NPP_Print( NPP instance, NPPrint* printInfo )
442 {
443     if(printInfo == NULL)
444         return;
445
446     if (instance != NULL) {
447     /***** Insert NPP_Print code here *****\
448         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
449     \**************************************/
450
451         if (printInfo->mode == NP_FULL) {
452             /*
453              * PLUGIN DEVELOPERS:
454              *  If your plugin would like to take over
455              *  printing completely when it is in full-screen mode,
456              *  set printInfo->pluginPrinted to TRUE and print your
457              *  plugin as you see fit.  If your plugin wants Netscape
458              *  to handle printing in this case, set
459              *  printInfo->pluginPrinted to FALSE (the default) and
460              *  do nothing.  If you do want to handle printing
461              *  yourself, printOne is true if the print button
462              *  (as opposed to the print menu) was clicked.
463              *  On the Macintosh, platformPrint is a THPrint; on
464              *  Windows, platformPrint is a structure
465              *  (defined in npapi.h) containing the printer name, port,
466              *  etc.
467              */
468
469     /***** Insert NPP_Print code here *****\
470             void* platformPrint =
471                 printInfo->print.fullPrint.platformPrint;
472             NPBool printOne =
473                 printInfo->print.fullPrint.printOne;
474     \**************************************/
475
476             /* Do the default*/
477             printInfo->print.fullPrint.pluginPrinted = FALSE;
478         }
479         else {  /* If not fullscreen, we must be embedded */
480             /*
481              * PLUGIN DEVELOPERS:
482              *  If your plugin is embedded, or is full-screen
483              *  but you returned false in pluginPrinted above, NPP_Print
484              *  will be called with mode == NP_EMBED.  The NPWindow
485              *  in the printInfo gives the location and dimensions of
486              *  the embedded plugin on the printed page.  On the
487              *  Macintosh, platformPrint is the printer port; on
488              *  Windows, platformPrint is the handle to the printing
489              *  device context.
490              */
491
492     /***** Insert NPP_Print code here *****\
493             NPWindow* printWindow =
494                 &(printInfo->print.embedPrint.window);
495             void* platformPrint =
496                 printInfo->print.embedPrint.platformPrint;
497     \**************************************/
498         }
499     }
500 }
501
502 /******************************************************************************
503  * UNIX-only methods
504  *****************************************************************************/
505 #ifndef WIN32
506 static void Redraw( Widget w, XtPointer closure, XEvent *event )
507 {
508     VlcPlugin* p_plugin = (VlcPlugin*)closure;
509     GC gc;
510     XGCValues gcv;
511     const char* text = "hello d00dZ, I'm in void Redraw()";
512
513     XtVaGetValues(w, XtNbackground, &gcv.background,
514                   XtNforeground, &gcv.foreground, 0);
515     gc = XCreateGC(p_plugin->display, p_plugin->window,
516                    GCForeground|GCBackground, &gcv);
517     XDrawRectangle(p_plugin->display, p_plugin->window, gc,
518                    0, 0, p_plugin->width-1, p_plugin->height-1);
519     XDrawString(p_plugin->display, p_plugin->window, gc,
520                 p_plugin->width/2 - 100, p_plugin->height/2,
521                 text, strlen(text));
522     return;
523 }
524 #endif
525