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