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