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