* ALL: the build mechanism now uses automake. See HACKING for more details.
[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.2 2002/09/30 11:05:41 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         /*, "-q"*/
169         , "-v"
170     };
171
172     if( instance == NULL )
173     {
174         return NPERR_INVALID_INSTANCE_ERROR;
175     }
176
177     VlcPlugin * p_plugin = new VlcPlugin( instance );
178
179     if( p_plugin == NULL )
180     {
181         return NPERR_OUT_OF_MEMORY_ERROR;
182     }
183
184     instance->pdata = p_plugin;
185
186     p_plugin->fMode = mode;
187     p_plugin->fWindow = NULL;
188     p_plugin->window = 0;
189
190     p_plugin->p_vlc = vlc_create_r();
191     if( p_plugin->p_vlc == NULL )
192     {
193         delete p_plugin;
194         p_plugin = NULL;
195         return NPERR_GENERIC_ERROR;
196     }
197
198     i_ret = vlc_init_r( p_plugin->p_vlc, sizeof(ppsz_foo)/sizeof(char*), ppsz_foo );
199     if( i_ret )
200     {
201         vlc_destroy_r( p_plugin->p_vlc );
202         p_plugin->p_vlc = NULL;
203         delete p_plugin;
204         p_plugin = NULL;
205         return NPERR_GENERIC_ERROR;
206     }
207
208     vlc_set_r( p_plugin->p_vlc, "vout", "xvideo,x11,dummy" );
209     vlc_set_r( p_plugin->p_vlc, "intf", "dummy" );
210
211     p_plugin->b_stream = 0;
212     p_plugin->b_autoplay = 0;
213     p_plugin->psz_target = NULL;
214
215     for( i = 0; i < argc ; i++ )
216     {
217         if( !strcmp( argn[i], "target" ) )
218         {
219             p_plugin->psz_target = argv[i];
220         }
221         else if( !strcmp( argn[i], "autoplay" ) )
222         {
223             if( !strcmp( argv[i], "yes" ) )
224             {
225                 p_plugin->b_autoplay = 1;
226             }
227         }
228         else if( !strcmp( argn[i], "loop" ) )
229         {
230             if( !strcmp( argv[i], "yes" ) )
231             {
232                 vlc_set_r( p_plugin->p_vlc, "loop", "1" );
233             }
234         }
235     }
236
237     if( p_plugin->psz_target )
238     {
239         p_plugin->psz_target = strdup( p_plugin->psz_target );
240     }
241
242     return NPERR_NO_ERROR;
243 }
244
245 NPError NPP_Destroy( NPP instance, NPSavedData** save )
246 {
247     if( instance == NULL )
248     {
249         return NPERR_INVALID_INSTANCE_ERROR;
250     }
251
252     VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
253
254     if( p_plugin != NULL )
255     {
256         if( p_plugin->p_vlc != NULL )
257         {
258             vlc_stop_r( p_plugin->p_vlc );
259             vlc_destroy_r( p_plugin->p_vlc );
260             p_plugin->p_vlc = NULL;
261         }
262
263         if( p_plugin->psz_target )
264         {
265             free( p_plugin->psz_target );
266             p_plugin->psz_target = NULL;
267         }
268
269         delete p_plugin;
270     }
271
272     instance->pdata = NULL;
273
274     return NPERR_NO_ERROR;
275 }
276
277 NPError NPP_SetWindow( NPP instance, NPWindow* window )
278 {
279     char psz_window[32];
280
281     if( instance == NULL )
282     {
283         return NPERR_INVALID_INSTANCE_ERROR;
284     }
285
286     VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
287
288     /* Write the window ID for vlc */
289     sprintf( psz_window, "%li", (long int)window->window );
290     vlc_set_r( p_plugin->p_vlc, "x11-drawable", psz_window );
291     vlc_set_r( p_plugin->p_vlc, "xvideo-drawable", psz_window );
292
293     /*
294      * PLUGIN DEVELOPERS:
295      *  Before setting window to point to the
296      *  new window, you may wish to compare the new window
297      *  info to the previous window (if any) to note window
298      *  size changes, etc.
299      */
300
301     Widget netscape_widget;
302
303     p_plugin->window = (Window) window->window;
304     p_plugin->x = window->x;
305     p_plugin->y = window->y;
306     p_plugin->width = window->width;
307     p_plugin->height = window->height;
308     p_plugin->display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
309
310     netscape_widget = XtWindowToWidget(p_plugin->display, p_plugin->window);
311     XtAddEventHandler(netscape_widget, ExposureMask, FALSE, (XtEventHandler)Redraw, p_plugin);
312     Redraw(netscape_widget, (XtPointer)p_plugin, NULL);
313
314     p_plugin->fWindow = window;
315
316 #if 1
317     if( !p_plugin->b_stream )
318     {
319         int i_mode = PLAYLIST_APPEND;
320
321         if( p_plugin->b_autoplay )
322         {
323             i_mode |= PLAYLIST_GO;
324         }
325
326         if( p_plugin->psz_target )
327         {
328             vlc_add_target_r( p_plugin->p_vlc, p_plugin->psz_target,
329                               i_mode, PLAYLIST_END );
330             p_plugin->b_stream = 1;
331         }
332     }
333 #endif
334
335     return NPERR_NO_ERROR;
336 }
337
338 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
339                        NPBool seekable, uint16 *stype )
340 {
341     if( instance == NULL )
342     {
343         return NPERR_INVALID_INSTANCE_ERROR;
344     }
345
346 #if 0
347     VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
348 #endif
349
350     fprintf(stderr, "NPP_NewStream - FILE mode !!\n");
351
352     /* We want a *filename* ! */
353     *stype = NP_ASFILE;
354
355 #if 0
356     if( p_plugin->b_stream == 0 )
357     {
358         p_plugin->psz_target = strdup( stream->url );
359         p_plugin->b_stream = 1;
360     }
361 #endif
362
363     return NPERR_NO_ERROR;
364 }
365
366 int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
367                    * mode so we can take any size stream in our
368                    * write call (since we ignore it) */
369
370 #define SARASS_SIZE (1024*1024)
371
372 int32 NPP_WriteReady( NPP instance, NPStream *stream )
373 {
374     VlcPlugin* p_plugin;
375
376     fprintf(stderr, "NPP_WriteReady\n");
377
378     if (instance != NULL)
379     {
380         p_plugin = (VlcPlugin*) instance->pdata;
381         /* Muahahahahahahaha */
382         return STREAMBUFSIZE;
383         /*return SARASS_SIZE;*/
384     }
385
386     /* Number of bytes ready to accept in NPP_Write() */
387     return STREAMBUFSIZE;
388     /*return 0;*/
389 }
390
391
392 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
393                  int32 len, void *buffer )
394 {
395     fprintf(stderr, "NPP_Write %i\n", len);
396
397     if( instance != NULL )
398     {
399         /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
400     }
401
402     return len;         /* The number of bytes accepted */
403 }
404
405
406 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
407 {
408     if( instance == NULL )
409     {
410         return NPERR_INVALID_INSTANCE_ERROR;
411     }
412
413     return NPERR_NO_ERROR;
414 }
415
416
417 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
418 {
419     if( instance == NULL )
420     {
421         return;
422     }
423
424     VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
425
426     fprintf(stderr, "NPP_StreamAsFile\n");
427     vlc_add_target_r( p_plugin->p_vlc, fname,
428                       PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
429 }
430
431 #if 0
432 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
433 {
434     fprintf(stderr,"filename : %s\n", fname);
435     ((VlcPlugin*) instance->pdata)->SetFileName(fname);
436
437     fprintf(stderr,"SetFileNeme ok. \n");
438 }
439 #endif
440
441
442 void NPP_URLNotify( NPP instance, const char* url,
443                     NPReason reason, void* notifyData )
444 {
445     /***** Insert NPP_URLNotify code here *****\
446     PluginInstance* p_plugin;
447     if (instance != NULL)
448         p_plugin = (PluginInstance*) instance->pdata;
449     \*********************************************/
450 }
451
452
453 void NPP_Print( NPP instance, NPPrint* printInfo )
454 {
455     if( printInfo == NULL )
456     {
457         return;
458     }
459
460     if( instance != NULL )
461     {
462         /***** Insert NPP_Print code here *****\
463         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
464         \**************************************/
465
466         if( printInfo->mode == NP_FULL )
467         {
468             /*
469              * PLUGIN DEVELOPERS:
470              *  If your plugin would like to take over
471              *  printing completely when it is in full-screen mode,
472              *  set printInfo->pluginPrinted to TRUE and print your
473              *  plugin as you see fit.  If your plugin wants Netscape
474              *  to handle printing in this case, set
475              *  printInfo->pluginPrinted to FALSE (the default) and
476              *  do nothing.  If you do want to handle printing
477              *  yourself, printOne is true if the print button
478              *  (as opposed to the print menu) was clicked.
479              *  On the Macintosh, platformPrint is a THPrint; on
480              *  Windows, platformPrint is a structure
481              *  (defined in npapi.h) containing the printer name, port,
482              *  etc.
483              */
484
485             /***** Insert NPP_Print code here *****\
486             void* platformPrint =
487                 printInfo->print.fullPrint.platformPrint;
488             NPBool printOne =
489                 printInfo->print.fullPrint.printOne;
490             \**************************************/
491
492             /* Do the default*/
493             printInfo->print.fullPrint.pluginPrinted = FALSE;
494         }
495         else
496         {
497             /* If not fullscreen, we must be embedded */
498             /*
499              * PLUGIN DEVELOPERS:
500              *  If your plugin is embedded, or is full-screen
501              *  but you returned false in pluginPrinted above, NPP_Print
502              *  will be called with mode == NP_EMBED.  The NPWindow
503              *  in the printInfo gives the location and dimensions of
504              *  the embedded plugin on the printed page.  On the
505              *  Macintosh, platformPrint is the printer port; on
506              *  Windows, platformPrint is the handle to the printing
507              *  device context.
508              */
509
510             /***** Insert NPP_Print code here *****\
511             NPWindow* printWindow =
512                 &(printInfo->print.embedPrint.window);
513             void* platformPrint =
514                 printInfo->print.embedPrint.platformPrint;
515             \**************************************/
516         }
517     }
518 }
519
520 /******************************************************************************
521  * UNIX-only methods
522  *****************************************************************************/
523 #ifndef WIN32
524 static void Redraw( Widget w, XtPointer closure, XEvent *event )
525 {
526     VlcPlugin* p_plugin = (VlcPlugin*)closure;
527     GC gc;
528     XGCValues gcv;
529     const char * psz_text = "(no picture)";
530
531     gcv.foreground = BlackPixel( p_plugin->display, 0 );
532     gc = XCreateGC( p_plugin->display, p_plugin->window, GCForeground, &gcv );
533
534     XFillRectangle( p_plugin->display, p_plugin->window, gc,
535                     0, 0, p_plugin->width, p_plugin->height );
536
537     gcv.foreground = WhitePixel( p_plugin->display, 0 );
538     XChangeGC( p_plugin->display, gc, GCForeground, &gcv );
539
540     XDrawString( p_plugin->display, p_plugin->window, gc,
541                  p_plugin->width / 2 - 40, p_plugin->height / 2,
542                  psz_text, strlen(psz_text) );
543
544     XFreeGC( p_plugin->display, gc );
545 }
546 #endif
547