[PATCH] by Anthony Loiseau <anthony.loiseau at gmail dot com>: Avoid browser crashing...
[vlc.git] / projects / mozilla / vlcshell.cpp
1 /*****************************************************************************
2  * vlcshell.cpp: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "config.h"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 /* Mozilla stuff */
34 #ifdef HAVE_MOZILLA_CONFIG_H
35 #   include <mozilla-config.h>
36 #endif
37
38 /* This is from mozilla java, do we really need it? */
39 #if 0
40 #include <jri.h>
41 #endif
42
43 #include "vlcplugin.h"
44
45 /* Enable/disable debugging printf's for X11 resizing */
46 #undef X11_RESIZE_DEBUG
47
48 #define WINDOW_TEXT "Video is loading..."
49
50 /*****************************************************************************
51  * Unix-only declarations
52 ******************************************************************************/
53 #ifdef XP_UNIX
54
55 static void Redraw( Widget w, XtPointer closure, XEvent *event );
56 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
57 static void Resize( Widget w, XtPointer closure, XEvent *event );
58
59 #endif
60
61 /*****************************************************************************
62  * MacOS-only declarations
63 ******************************************************************************/
64 #ifdef XP_MACOSX
65 #endif
66
67 /*****************************************************************************
68  * Windows-only declarations
69  *****************************************************************************/
70 #ifdef XP_WIN
71
72 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
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
87     static char psz_desc[1000];
88
89     /* plugin class variables */
90     switch( variable )
91     {
92         case NPPVpluginNameString:
93             *((char **)value) = PLUGIN_NAME;
94             return NPERR_NO_ERROR;
95
96         case NPPVpluginDescriptionString:
97             snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION, VLC_Version() );
98             *((char **)value) = psz_desc;
99             return NPERR_NO_ERROR;
100
101         default:
102             /* move on to instance variables ... */
103             ;
104     }
105
106     if( instance == NULL )
107     {
108         return NPERR_INVALID_INSTANCE_ERROR;
109     }
110
111     /* plugin instance variables */
112
113     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
114     if( NULL == p_plugin )
115     {
116         // plugin has not been initialized yet !
117         return NPERR_INVALID_INSTANCE_ERROR;
118     }
119
120     switch( variable )
121     {
122         case NPPVpluginScriptableNPObject:
123         {
124             /* retrieve plugin root class */
125             NPClass *scriptClass = p_plugin->getScriptClass();
126             if( scriptClass )
127             {
128                 /* create an instance and return it */
129                 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
130                 return NPERR_NO_ERROR;
131             }
132             break;
133         }
134
135         default:
136             ;
137     }
138     return NPERR_GENERIC_ERROR;
139 }
140
141 /*
142  * there is some confusion in gecko headers regarding definition of this API
143  * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
144  */
145
146 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
147 {
148     return NPERR_GENERIC_ERROR;
149 }
150
151 /******************************************************************************
152  * Mac-only API calls
153  *****************************************************************************/
154 #ifdef XP_MACOSX
155 int16 NPP_HandleEvent( NPP instance, void * event )
156 {
157     static UInt32 lastMouseUp = 0;
158
159     if( instance == NULL )
160     {
161         return false;
162     }
163
164     VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
165
166     if( p_plugin == NULL )
167     {
168         return false;
169     }
170
171     EventRecord *myEvent = (EventRecord*)event;
172
173     switch( myEvent->what )
174     {
175         case nullEvent:
176             return true;
177         case mouseDown:
178         {
179             if( (myEvent->when - lastMouseUp) < GetDblTime() )
180             {
181                 /* double click */
182                 libvlc_instance_t *p_vlc = p_plugin->getVLC();
183
184                 if( p_vlc )
185                 {
186                     if( libvlc_playlist_isplaying(p_vlc, NULL) )
187                     {
188                         libvlc_media_instance_t *p_md =
189                             libvlc_playlist_get_media_instance(p_vlc, NULL);
190                         if( p_md )
191                         {
192                             libvlc_toggle_fullscreen(p_md, NULL);
193                             libvlc_media_instance_release(p_md);
194                         }
195                     }
196                 }
197             }
198             return true;
199         }
200         case mouseUp:
201             lastMouseUp = myEvent->when;
202             return true;
203         case keyUp:
204         case keyDown:
205         case autoKey:
206             return true;
207         case updateEvt:
208         {
209             const NPWindow& npwindow = p_plugin->getWindow();
210             if( npwindow.window )
211             {
212                 int hasVout = FALSE;
213                 libvlc_instance_t *p_vlc = p_plugin->getVLC();
214
215                 if( p_vlc )
216                 {
217                     if( libvlc_playlist_isplaying(p_vlc, NULL) )
218                     {
219                         libvlc_media_instance_t *p_md =
220                             libvlc_playlist_get_media_instance(p_vlc, NULL);
221                         if( p_md )
222                         {
223                             hasVout = libvlc_media_instance_has_vout(p_md, NULL);
224                             if( hasVout )
225                             {
226                                 libvlc_rectangle_t area;
227                                 area.left = 0;
228                                 area.top = 0;
229                                 area.right = npwindow.width;
230                                 area.bottom = npwindow.height;
231                                 libvlc_video_redraw_rectangle(p_md, &area, NULL);
232                             }
233                             libvlc_media_instance_release(p_md);
234                         }
235                     }
236                 }
237
238                 if( ! hasVout )
239                 {
240                     /* draw the beautiful "No Picture" */
241
242                     ForeColor(blackColor);
243                     PenMode( patCopy );
244
245                     /* seems that firefox forgets to set the following on occasion (reload) */
246                     SetOrigin(((NP_Port *)npwindow.window)->portx,
247                               ((NP_Port *)npwindow.window)->porty);
248
249                     Rect rect;
250                     rect.left = 0;
251                     rect.top = 0;
252                     rect.right = npwindow.width;
253                     rect.bottom = npwindow.height;
254                     PaintRect( &rect );
255
256                     ForeColor(whiteColor);
257                     MoveTo( (npwindow.width-80)/ 2  , npwindow.height / 2 );
258                     DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
259                 }
260             }
261             return true;
262         }
263         case activateEvt:
264             return false;
265         case NPEventType_GetFocusEvent:
266         case NPEventType_LoseFocusEvent:
267             return true;
268         case NPEventType_AdjustCursorEvent:
269             return false;
270         case NPEventType_MenuCommandEvent:
271             return false;
272         case NPEventType_ClippingChangedEvent:
273             return false;
274         case NPEventType_ScrollingBeginsEvent:
275             return true;
276         case NPEventType_ScrollingEndsEvent:
277             return true;
278         default:
279             ;
280     }
281     return false;
282 }
283 #endif /* XP_MACOSX */
284
285 /******************************************************************************
286  * General Plug-in Calls
287  *****************************************************************************/
288 NPError NPP_Initialize( void )
289 {
290     return NPERR_NO_ERROR;
291 }
292
293 jref NPP_GetJavaClass( void )
294 {
295     return NULL;
296 }
297
298 void NPP_Shutdown( void )
299 {
300     ;
301 }
302
303 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
304                  char* argn[], char* argv[], NPSavedData* saved )
305 {
306     NPError status;
307
308     if( instance == NULL )
309     {
310         return NPERR_INVALID_INSTANCE_ERROR;
311     }
312
313     VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
314     if( NULL == p_plugin )
315     {
316         return NPERR_OUT_OF_MEMORY_ERROR;
317     }
318
319     status = p_plugin->init(argc, argn, argv);
320     if( NPERR_NO_ERROR == status )
321     {
322         instance->pdata = reinterpret_cast<void*>(p_plugin);
323 #if 0
324         NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
325         NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
326 #endif
327     }
328     else
329     {
330         delete p_plugin;
331     }
332     return status;
333 }
334
335 NPError NPP_Destroy( NPP instance, NPSavedData** save )
336 {
337     if( NULL == instance )
338         return NPERR_INVALID_INSTANCE_ERROR;
339
340     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
341     if( NULL == p_plugin )
342         return NPERR_NO_ERROR;
343
344     instance->pdata = NULL;
345
346 #if XP_WIN
347     HWND win = (HWND)p_plugin->getWindow().window;
348     WNDPROC winproc = p_plugin->getWindowProc();
349     if( winproc )
350     {
351         /* reset WNDPROC */
352         SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
353     }
354 #endif
355
356     delete p_plugin;
357
358     return NPERR_NO_ERROR;
359 }
360
361 NPError NPP_SetWindow( NPP instance, NPWindow* window )
362 {
363     if( ! instance )
364     {
365         return NPERR_INVALID_INSTANCE_ERROR;
366     }
367
368     /* NPP_SetWindow may be called before NPP_New (Opera) */
369     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
370     if( ! p_plugin )
371     {
372         /* we should probably show a splash screen here */
373         return NPERR_NO_ERROR;
374     }
375
376     libvlc_instance_t *p_vlc = p_plugin->getVLC();
377
378     /*
379      * PLUGIN DEVELOPERS:
380      *  Before setting window to point to the
381      *  new window, you may wish to compare the new window
382      *  info to the previous window (if any) to note window
383      *  size changes, etc.
384      */
385
386     /* retrieve current window */
387     NPWindow& curwin = p_plugin->getWindow();
388
389 #ifdef XP_MACOSX
390     if( window && window->window )
391     {
392         /* check if plugin has a new parent window */
393         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
394         if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
395         {
396             /* set/change parent window */
397             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
398         }
399
400         /* as MacOS X video output is windowless, set viewport */
401         libvlc_rectangle_t view, clip;
402
403         /*
404         ** browser sets port origin to top-left location of plugin relative to GrafPort
405         ** window origin is set relative to document, which of little use for drawing
406         */
407         view.top     = ((NP_Port*) (window->window))->porty;
408         view.left    = ((NP_Port*) (window->window))->portx;
409         view.bottom  = window->height+view.top;
410         view.right   = window->width+view.left;
411         /* clipRect coordinates are also relative to GrafPort */
412         clip.top     = window->clipRect.top;
413         clip.left    = window->clipRect.left;
414         clip.bottom  = window->clipRect.bottom;
415         clip.right   = window->clipRect.right;
416
417         libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
418
419         /* remember new window */
420         p_plugin->setWindow(*window);
421     }
422     else if( curwin.window ) {
423         /* change/set parent */
424         libvlc_video_set_parent(p_vlc, 0, NULL);
425         curwin.window = NULL;
426     }
427 #endif /* XP_MACOSX */
428
429 #ifdef XP_WIN
430     if( window && window->window )
431     {
432         /* check if plugin has a new parent window */
433         HWND drawable = (HWND) (window->window);
434         if( !curwin.window || drawable != curwin.window )
435         {
436             /* reset previous window settings */
437             HWND oldwin = (HWND)p_plugin->getWindow().window;
438             WNDPROC oldproc = p_plugin->getWindowProc();
439             if( oldproc )
440             {
441                 /* reset WNDPROC */
442                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
443             }
444             /* attach our plugin object */
445             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
446                              reinterpret_cast<LONG_PTR>(p_plugin));
447
448             /* install our WNDPROC */
449             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
450                                              GWL_WNDPROC, (LONG)Manage ) );
451
452             /* change window style to our liking */
453             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
454             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
455             SetWindowLong((HWND)drawable, GWL_STYLE, style);
456
457             /* change/set parent */
458             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
459
460             /* remember new window */
461             p_plugin->setWindow(*window);
462
463             /* Redraw window */
464             InvalidateRect( (HWND)drawable, NULL, TRUE );
465             UpdateWindow( (HWND)drawable );
466         }
467     }
468     else if ( curwin.window )
469     {
470         /* reset WNDPROC */
471         HWND oldwin = (HWND)curwin.window;
472         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
473         p_plugin->setWindowProc(NULL);
474         /* change/set parent */
475         libvlc_video_set_parent(p_vlc, 0, NULL);
476         curwin.window = NULL;
477     }
478 #endif /* XP_WIN */
479
480 #ifdef XP_UNIX
481     if( window && window->window )
482     {
483         Window  parent   = (Window) window->window;
484         if( !curwin.window || (parent != (Window)curwin.window) )
485         {
486             Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
487
488             XResizeWindow( p_display, parent, window->width, window->height );
489
490             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
491
492             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
493                            window->width, window->height - p_plugin->i_control_height, 0,
494                            i_blackColor, i_blackColor );
495             Window controls = XCreateSimpleWindow( p_display, parent, 0,
496                             window->height - p_plugin->i_control_height-1, window->width,
497                             p_plugin->i_control_height-1, 0, i_blackColor, i_blackColor );
498
499             XMapWindow( p_display, parent );
500             XMapWindow( p_display, video );
501             XMapWindow( p_display, controls );
502
503             XFlush(p_display);
504
505             Widget w = XtWindowToWidget( p_display, parent );
506
507             XtAddEventHandler( w, ExposureMask, FALSE,
508                                (XtEventHandler)Redraw, p_plugin );
509             XtAddEventHandler( w, StructureNotifyMask, FALSE,
510                                (XtEventHandler)Resize, p_plugin );
511             XtAddEventHandler( w, ButtonReleaseMask, FALSE,
512                                (XtEventHandler)ControlHandler, p_plugin );
513
514             /* callback */
515 /*
516             libvlc_media_instance_t *p_md;
517
518             libvlc_exception_t ex;
519             libvlc_exception_init(& ex );
520             p_md = libvlc_playlist_get_media_instance( p_plugin->getVLC(), &ex );
521             libvlc_exception_init( &ex );
522             libvlc_event_attach( libvlc_media_instance_event_manager( p_md, &ex ),
523                                  libvlc_MediaInstancePositionChanged, Redraw, NULL, &ex );
524 */
525
526             /* set/change parent window */
527             libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, NULL );
528
529             /* remember window */
530             p_plugin->setWindow( *window );
531             p_plugin->setVideoWindow( video );
532             p_plugin->setControlWindow( controls );
533
534             Redraw( w, (XtPointer)p_plugin, NULL );
535         }
536     }
537     else if ( curwin.window )
538     {
539         /* change/set parent */
540         libvlc_video_set_parent(p_vlc, 0, NULL);
541         curwin.window = NULL;
542     }
543 #endif /* XP_UNIX */
544
545     if( !p_plugin->b_stream )
546     {
547         if( p_plugin->psz_target )
548         {
549             if( libvlc_playlist_add( p_vlc, p_plugin->psz_target,
550                                      NULL, NULL ) != -1 )
551             {
552                 if( p_plugin->b_autoplay )
553                 {
554                     libvlc_playlist_play(p_vlc, 0, 0, NULL, NULL);
555                 }
556             }
557             p_plugin->b_stream = VLC_TRUE;
558         }
559     }
560     return NPERR_NO_ERROR;
561 }
562
563 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
564                        NPBool seekable, uint16 *stype )
565 {
566     if( NULL == instance  )
567     {
568         return NPERR_INVALID_INSTANCE_ERROR;
569     }
570
571     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
572     if( NULL == p_plugin )
573     {
574         return NPERR_INVALID_INSTANCE_ERROR;
575     }
576
577    /*
578    ** Firefox/Mozilla may decide to open a stream from the URL specified
579    ** in the SRC parameter of the EMBED tag and pass it to us
580    **
581    ** since VLC will open the SRC URL as well, we're not interested in
582    ** that stream. Otherwise, we'll take it and queue it up in the playlist
583    */
584     if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
585     {
586         /* TODO: use pipes !!!! */
587         *stype = NP_ASFILEONLY;
588         return NPERR_NO_ERROR;
589     }
590     return NPERR_GENERIC_ERROR;
591 }
592
593 int32 NPP_WriteReady( NPP instance, NPStream *stream )
594 {
595     /* TODO */
596     return 8*1024;
597 }
598
599
600 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
601                  int32 len, void *buffer )
602 {
603     /* TODO */
604     return len;
605 }
606
607
608 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
609 {
610     if( instance == NULL )
611     {
612         return NPERR_INVALID_INSTANCE_ERROR;
613     }
614     return NPERR_NO_ERROR;
615 }
616
617
618 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
619 {
620     if( instance == NULL )
621     {
622         return;
623     }
624
625     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
626     if( NULL == p_plugin )
627     {
628         return;
629     }
630
631     if( libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL ) != -1 )
632     {
633         if( p_plugin->b_autoplay )
634         {
635             libvlc_playlist_play( p_plugin->getVLC(), 0, 0, NULL, NULL);
636         }
637     }
638 }
639
640
641 void NPP_URLNotify( NPP instance, const char* url,
642                     NPReason reason, void* notifyData )
643 {
644     /***** Insert NPP_URLNotify code here *****\
645     PluginInstance* p_plugin;
646     if (instance != NULL)
647         p_plugin = (PluginInstance*) instance->pdata;
648     \*********************************************/
649 }
650
651
652 void NPP_Print( NPP instance, NPPrint* printInfo )
653 {
654     if( printInfo == NULL )
655     {
656         return;
657     }
658
659     if( instance != NULL )
660     {
661         /***** Insert NPP_Print code here *****\
662         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
663         \**************************************/
664
665         if( printInfo->mode == NP_FULL )
666         {
667             /*
668              * PLUGIN DEVELOPERS:
669              *  If your plugin would like to take over
670              *  printing completely when it is in full-screen mode,
671              *  set printInfo->pluginPrinted to TRUE and print your
672              *  plugin as you see fit.  If your plugin wants Netscape
673              *  to handle printing in this case, set
674              *  printInfo->pluginPrinted to FALSE (the default) and
675              *  do nothing.  If you do want to handle printing
676              *  yourself, printOne is true if the print button
677              *  (as opposed to the print menu) was clicked.
678              *  On the Macintosh, platformPrint is a THPrint; on
679              *  Windows, platformPrint is a structure
680              *  (defined in npapi.h) containing the printer name, port,
681              *  etc.
682              */
683
684             /***** Insert NPP_Print code here *****\
685             void* platformPrint =
686                 printInfo->print.fullPrint.platformPrint;
687             NPBool printOne =
688                 printInfo->print.fullPrint.printOne;
689             \**************************************/
690
691             /* Do the default*/
692             printInfo->print.fullPrint.pluginPrinted = FALSE;
693         }
694         else
695         {
696             /* If not fullscreen, we must be embedded */
697             /*
698              * PLUGIN DEVELOPERS:
699              *  If your plugin is embedded, or is full-screen
700              *  but you returned false in pluginPrinted above, NPP_Print
701              *  will be called with mode == NP_EMBED.  The NPWindow
702              *  in the printInfo gives the location and dimensions of
703              *  the embedded plugin on the printed page.  On the
704              *  Macintosh, platformPrint is the printer port; on
705              *  Windows, platformPrint is the handle to the printing
706              *  device context.
707              */
708
709             /***** Insert NPP_Print code here *****\
710             NPWindow* printWindow =
711                 &(printInfo->print.embedPrint.window);
712             void* platformPrint =
713                 printInfo->print.embedPrint.platformPrint;
714             \**************************************/
715         }
716     }
717 }
718
719 /******************************************************************************
720  * Windows-only methods
721  *****************************************************************************/
722 #if XP_WIN
723 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
724 {
725     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
726
727     switch( i_msg )
728     {
729         case WM_ERASEBKGND:
730             return 1L;
731
732         case WM_PAINT:
733         {
734             PAINTSTRUCT paintstruct;
735             HDC hdc;
736             RECT rect;
737
738             hdc = BeginPaint( p_hwnd, &paintstruct );
739
740             GetClientRect( p_hwnd, &rect );
741
742             FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
743             SetTextColor(hdc, RGB(255, 255, 255));
744             SetBkColor(hdc, RGB(0, 0, 0));
745             DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect,
746                       DT_CENTER|DT_VCENTER|DT_SINGLELINE);
747
748             EndPaint( p_hwnd, &paintstruct );
749             return 0L;
750         }
751         default:
752             /* delegate to default handler */
753             return CallWindowProc(p_plugin->getWindowProc(), p_hwnd, i_msg, wpar, lpar );
754     }
755 }
756 #endif /* XP_WIN */
757
758 /******************************************************************************
759  * UNIX-only methods
760  *****************************************************************************/
761 #ifdef XP_UNIX
762 static void Redraw( Widget w, XtPointer closure, XEvent *event )
763 {
764     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
765     const NPWindow& window = p_plugin->getWindow();
766     GC gc;
767     XGCValues gcv;
768
769     Window video = p_plugin->getVideoWindow();
770     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
771
772     p_plugin->showToolbar();
773
774     gcv.foreground = BlackPixel( p_display, 0 );
775     gc = XCreateGC( p_display, video, GCForeground, &gcv );
776
777     XFillRectangle( p_display, video, gc,
778                     0, 0, window.width, window.height - p_plugin->i_control_height );
779
780     gcv.foreground = WhitePixel( p_display, 0 );
781     XChangeGC( p_display, gc, GCForeground, &gcv );
782
783     XDrawString( p_display, video, gc,
784                  window.width / 2 - 40, (window.height - p_plugin->i_control_height) / 2,
785                  WINDOW_TEXT, strlen(WINDOW_TEXT) );
786     XFreeGC( p_display, gc );
787
788     p_plugin->redrawToolbar();
789
790     p_plugin->hideToolbar();
791 }
792
793 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
794 {
795     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
796     const NPWindow& window = p_plugin->getWindow();
797
798     int i_height = window.height;
799     int i_width = window.width;
800     int i_xPos = event->xbutton.x;
801     int i_yPos = event->xbutton.y;
802
803     if( p_plugin )
804     {
805         libvlc_exception_t ex;
806         libvlc_exception_init( &ex );
807         libvlc_media_instance_t *p_md =
808                 libvlc_playlist_get_media_instance(p_plugin->getVLC(), &ex);
809         libvlc_exception_clear( &ex );
810
811         /* jump in the movie */
812         if( i_yPos <= (i_height-30) )
813         {
814             /* if a movie is loaded */
815             if( p_md )
816             {
817                 vlc_int64_t f_length;
818                 libvlc_exception_init( &ex );
819                 f_length = libvlc_media_instance_get_length( p_md, &ex ) / 100;
820                 libvlc_exception_clear( &ex );
821
822                 f_length = (float)f_length *
823                         ( ((float)i_xPos-4 ) / ( ((float)i_width-8)/100) );
824
825                 libvlc_exception_init( &ex );
826                 libvlc_media_instance_set_time( p_md, f_length, &ex );
827                 libvlc_exception_clear( &ex );
828             }
829         }
830
831         /* play/pause toggle */
832         if( (i_yPos > (i_height-30)) && (i_xPos > 4) && (i_xPos <= 39) )
833         {
834             int i_playing;
835             libvlc_exception_init( &ex );
836             i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
837             libvlc_exception_clear( &ex );
838
839             libvlc_exception_init( &ex );
840             if( i_playing == 1 )
841                 libvlc_playlist_pause( p_plugin->getVLC(), &ex );
842             else
843                 libvlc_playlist_play( p_plugin->getVLC(), -1, 0, NULL, &ex );
844             libvlc_exception_clear( &ex );
845         }
846
847         /* stop */
848         if( (i_yPos > (i_height-30)) && (i_xPos > 39) && (i_xPos < 67) )
849         {
850             libvlc_exception_init( &ex );
851             libvlc_playlist_stop( p_plugin->getVLC(), &ex );
852             libvlc_exception_clear( &ex );
853         }
854
855         /* fullscreen */
856         if( (i_yPos > (i_height-30)) && (i_xPos >= 67) && (i_xPos < 94) )
857         {
858             int i_playing;
859             libvlc_exception_init( &ex );
860             i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
861             libvlc_exception_clear( &ex );
862
863             if( i_playing == 1 )
864             {
865                 libvlc_exception_init( &ex );
866                 libvlc_set_fullscreen( p_md, 1, &ex );
867                 libvlc_exception_clear( &ex );
868             }
869         }
870
871         /* mute toggle */
872         if( (i_yPos > (i_height-30)) && (i_xPos >= 94) && (i_xPos < 109))
873         {
874             libvlc_exception_init( &ex );
875             libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
876             libvlc_exception_clear( &ex );
877         }
878         libvlc_media_instance_release( p_md );
879     }
880     Redraw( w, closure, event );
881 }
882
883 static void Resize ( Widget w, XtPointer closure, XEvent *event )
884 {
885     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
886     const NPWindow& window = p_plugin->getWindow();
887     Window  drawable   = p_plugin->getVideoWindow();
888     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
889
890     int i_ret;
891     Window root_return, parent_return, * children_return;
892     Window base_window;
893     unsigned int i_nchildren;
894
895 #ifdef X11_RESIZE_DEBUG
896     XWindowAttributes attr;
897
898     if( event && event->type == ConfigureNotify )
899     {
900         fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
901                  "send_event ? %s\n", event->xconfigure.width,
902                  event->xconfigure.height,
903                  event->xconfigure.send_event ? "TRUE" : "FALSE" );
904     }
905 #endif /* X11_RESIZE_DEBUG */
906
907     if( ! p_plugin->setSize(window.width, (window.height - p_plugin->i_control_height)) )
908     {
909         /* size already set */
910         return;
911     }
912
913     i_ret = XResizeWindow( p_display, drawable, window.width, (window.height - p_plugin->i_control_height) );
914
915 #ifdef X11_RESIZE_DEBUG
916     fprintf( stderr,
917              "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
918
919     XGetWindowAttributes ( p_display, drawable, &attr );
920
921     /* X is asynchronous, so the current size reported here is not
922        necessarily the requested size as the Resize request may not
923        yet have been handled by the plugin host */
924     fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
925              attr.width, attr.height );
926 #endif /* X11_RESIZE_DEBUG */
927
928     XQueryTree( p_display, drawable,
929                 &root_return, &parent_return, &children_return,
930                 &i_nchildren );
931
932     if( i_nchildren > 0 )
933     {
934         /* XXX: Make assumptions related to the window parenting structure in
935            vlc/modules/video_output/x11/xcommon.c */
936         base_window = children_return[i_nchildren - 1];
937
938 #ifdef X11_RESIZE_DEBUG
939         fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
940         fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
941                  base_window );
942 #endif /* X11_RESIZE_DEBUG */
943
944         i_ret = XResizeWindow( p_display, base_window,
945                 window.width, ( window.height - p_plugin->i_control_height ) );
946
947 #ifdef X11_RESIZE_DEBUG
948         fprintf( stderr,
949                  "vlcshell::Resize() XResizeWindow(base) returned %d\n",
950                  i_ret );
951
952         XGetWindowAttributes( p_display, base_window, &attr );
953
954         fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
955                  attr.width, attr.height );
956 #endif /* X11_RESIZE_DEBUG */
957     }
958 }
959
960 #endif /* XP_UNIX */