]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcshell.cpp
Fix mozilla compilation on OSX
[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 /* 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() )
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 defined(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     if( p_plugin->playlist_isplaying() )
344         p_plugin->playlist_stop();
345
346     delete p_plugin;
347
348     return NPERR_NO_ERROR;
349 }
350
351 NPError NPP_SetWindow( NPP instance, NPWindow* window )
352 {
353 #if defined(XP_UNIX)
354     Window control;
355     unsigned int i_control_height = 0, i_control_width = 0;
356 #endif
357
358     if( ! instance )
359     {
360         return NPERR_INVALID_INSTANCE_ERROR;
361     }
362
363     /* NPP_SetWindow may be called before NPP_New (Opera) */
364     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
365     if( NULL == p_plugin )
366     {
367         /* we should probably show a splash screen here */
368         return NPERR_NO_ERROR;
369     }
370
371 #if defined(XP_UNIX)
372     control = p_plugin->getControlWindow();
373 #endif
374
375     libvlc_exception_t ex;
376     libvlc_exception_init(&ex);
377
378     libvlc_instance_t *p_vlc = p_plugin->getVLC();
379
380     /*
381      * PLUGIN DEVELOPERS:
382      *  Before setting window to point to the
383      *  new window, you may wish to compare the new window
384      *  info to the previous window (if any) to note window
385      *  size changes, etc.
386      */
387
388     /* retrieve current window */
389     NPWindow& curwin = p_plugin->getWindow();
390
391 #ifdef XP_MACOSX
392     if( window && window->window )
393     {
394         /* check if plugin has a new parent window */
395         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
396
397         /* as MacOS X video output is windowless, set viewport */
398         libvlc_rectangle_t view, clip;
399
400         /*
401         ** browser sets port origin to top-left location of plugin
402         ** relative to GrafPort window origin is set relative to document,
403         ** which of little use for drawing
404         */
405         view.top     = ((NP_Port*) (window->window))->porty;
406         view.left    = ((NP_Port*) (window->window))->portx;
407         view.bottom  = window->height+view.top;
408         view.right   = window->width+view.left;
409
410         /* clipRect coordinates are also relative to GrafPort */
411         clip.top     = window->clipRect.top;
412         clip.left    = window->clipRect.left;
413         clip.bottom  = window->clipRect.bottom;
414         clip.right   = window->clipRect.right;
415 #ifdef NOT_WORKING
416         libvlc_video_set_viewport(p_vlc, p_plugin->getMD(&ex), &view, &clip, &ex);
417         libvlc_exception_clear(&ex);
418 #else
419 #warning disabled code
420 #endif        
421         /* remember new window */
422         p_plugin->setWindow(*window);
423     }
424     else if( curwin.window )
425     {
426         /* change/set parent */
427         curwin.window = NULL;
428     }
429 #endif /* XP_MACOSX */
430
431 #ifdef XP_WIN
432     if( window && window->window )
433     {
434         /* check if plugin has a new parent window */
435         HWND drawable = (HWND) (window->window);
436         if( !curwin.window || drawable != curwin.window )
437         {
438             /* reset previous window settings */
439             HWND oldwin = (HWND)p_plugin->getWindow().window;
440             WNDPROC oldproc = p_plugin->getWindowProc();
441             if( oldproc )
442             {
443                 /* reset WNDPROC */
444                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
445             }
446             /* attach our plugin object */
447             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
448                              reinterpret_cast<LONG_PTR>(p_plugin));
449
450             /* install our WNDPROC */
451             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
452                                              GWL_WNDPROC, (LONG)Manage ) );
453
454             /* change window style to our liking */
455             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
456             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
457             SetWindowLong((HWND)drawable, GWL_STYLE, style);
458
459             /* remember new window */
460             p_plugin->setWindow(*window);
461
462             /* Redraw window */
463             InvalidateRect( (HWND)drawable, NULL, TRUE );
464             UpdateWindow( (HWND)drawable );
465         }
466     }
467     else if( curwin.window )
468     {
469         /* reset WNDPROC */
470         HWND oldwin = (HWND)curwin.window;
471         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
472         p_plugin->setWindowProc(NULL);
473
474         curwin.window = NULL;
475     }
476 #endif /* XP_WIN */
477
478 #if defined(XP_UNIX)
479     /* default to hidden toolbar, shown at the end of this method if asked *
480      * developers note : getToolbarSize need to wait the end of this method
481      */
482     i_control_height = 0;
483     i_control_width = window->width;
484
485     if( window && window->window )
486     {
487         Window  parent  = (Window) window->window;
488         if( !curwin.window || (parent != (Window)curwin.window) )
489         {
490             Display *p_display = ( (NPSetWindowCallbackStruct *)
491                                    window->ws_info )->display;
492
493             XResizeWindow( p_display, parent, window->width, window->height );
494
495             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
496
497             /* create windows */
498             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
499                            window->width, window->height - i_control_height,
500                            0, i_blackColor, i_blackColor );
501             Window controls = (Window) NULL;
502             controls = XCreateSimpleWindow( p_display, parent,
503                             0, window->height - i_control_height-1,
504                             window->width, i_control_height-1,
505                             0, i_blackColor, i_blackColor );
506
507             XMapWindow( p_display, parent );
508             XMapWindow( p_display, video );
509             if( controls ) { XMapWindow( p_display, controls ); }
510
511             XFlush(p_display);
512
513             /* bind events */
514             Widget w = XtWindowToWidget( p_display, parent );
515
516             XtAddEventHandler( w, ExposureMask, FALSE,
517                                (XtEventHandler)Redraw, p_plugin );
518             XtAddEventHandler( w, StructureNotifyMask, FALSE,
519                                (XtEventHandler)Resize, p_plugin );
520             XtAddEventHandler( w, ButtonReleaseMask, FALSE,
521                                (XtEventHandler)ControlHandler, p_plugin );
522
523             /* remember window */
524             p_plugin->setWindow( *window );
525             p_plugin->setVideoWindow( video );
526
527             if( controls )
528             {
529                 p_plugin->setControlWindow( controls );
530             }
531
532             Redraw( w, (XtPointer)p_plugin, NULL );
533
534             /* now display toolbar if asked through parameters */
535             if( p_plugin->b_toolbar )
536             {
537                 p_plugin->showToolbar();
538             }
539         }
540     }
541     else if( curwin.window )
542     {
543         curwin.window = NULL;
544     }
545 #endif /* XP_UNIX */
546
547     if( !p_plugin->b_stream )
548     {
549         if( p_plugin->psz_target )
550         {
551             if( p_plugin->playlist_add( p_plugin->psz_target, NULL ) != -1 )
552             {
553                 if( p_plugin->b_autoplay )
554                 {
555                     p_plugin->playlist_play(&ex);
556                     libvlc_exception_clear(&ex);
557                 }
558             }
559             p_plugin->b_stream = true;
560         }
561     }
562     return NPERR_NO_ERROR;
563 }
564
565 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
566                        NPBool seekable, uint16 *stype )
567 {
568     if( NULL == instance  )
569     {
570         return NPERR_INVALID_INSTANCE_ERROR;
571     }
572
573     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
574     if( NULL == p_plugin )
575     {
576         return NPERR_INVALID_INSTANCE_ERROR;
577     }
578
579    /*
580    ** Firefox/Mozilla may decide to open a stream from the URL specified
581    ** in the SRC parameter of the EMBED tag and pass it to us
582    **
583    ** since VLC will open the SRC URL as well, we're not interested in
584    ** that stream. Otherwise, we'll take it and queue it up in the playlist
585    */
586     if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
587     {
588         /* TODO: use pipes !!!! */
589         *stype = NP_ASFILEONLY;
590         return NPERR_NO_ERROR;
591     }
592     return NPERR_GENERIC_ERROR;
593 }
594
595 int32 NPP_WriteReady( NPP instance, NPStream *stream )
596 {
597     /* TODO */
598     return 8*1024;
599 }
600
601 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
602                  int32 len, void *buffer )
603 {
604     /* TODO */
605     return len;
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 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
618 {
619     if( instance == NULL )
620     {
621         return;
622     }
623
624     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
625     if( NULL == p_plugin )
626     {
627         return;
628     }
629
630     if( p_plugin->playlist_add( stream->url, NULL ) != -1 )
631     {
632         if( p_plugin->b_autoplay )
633         {
634             p_plugin->playlist_play(NULL);
635         }
636     }
637 }
638
639 void NPP_URLNotify( NPP instance, const char* url,
640                     NPReason reason, void* notifyData )
641 {
642     /***** Insert NPP_URLNotify code here *****\
643     PluginInstance* p_plugin;
644     if (instance != NULL)
645         p_plugin = (PluginInstance*) instance->pdata;
646     \*********************************************/
647 }
648
649 void NPP_Print( NPP instance, NPPrint* printInfo )
650 {
651     if( printInfo == NULL )
652     {
653         return;
654     }
655
656     if( instance != NULL )
657     {
658         /***** Insert NPP_Print code here *****\
659         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
660         \**************************************/
661
662         if( printInfo->mode == NP_FULL )
663         {
664             /*
665              * PLUGIN DEVELOPERS:
666              *  If your plugin would like to take over
667              *  printing completely when it is in full-screen mode,
668              *  set printInfo->pluginPrinted to TRUE and print your
669              *  plugin as you see fit.  If your plugin wants Netscape
670              *  to handle printing in this case, set
671              *  printInfo->pluginPrinted to FALSE (the default) and
672              *  do nothing.  If you do want to handle printing
673              *  yourself, printOne is true if the print button
674              *  (as opposed to the print menu) was clicked.
675              *  On the Macintosh, platformPrint is a THPrint; on
676              *  Windows, platformPrint is a structure
677              *  (defined in npapi.h) containing the printer name, port,
678              *  etc.
679              */
680
681             /***** Insert NPP_Print code here *****\
682             void* platformPrint =
683                 printInfo->print.fullPrint.platformPrint;
684             NPBool printOne =
685                 printInfo->print.fullPrint.printOne;
686             \**************************************/
687
688             /* Do the default*/
689             printInfo->print.fullPrint.pluginPrinted = FALSE;
690         }
691         else
692         {
693             /* If not fullscreen, we must be embedded */
694             /*
695              * PLUGIN DEVELOPERS:
696              *  If your plugin is embedded, or is full-screen
697              *  but you returned false in pluginPrinted above, NPP_Print
698              *  will be called with mode == NP_EMBED.  The NPWindow
699              *  in the printInfo gives the location and dimensions of
700              *  the embedded plugin on the printed page.  On the
701              *  Macintosh, platformPrint is the printer port; on
702              *  Windows, platformPrint is the handle to the printing
703              *  device context.
704              */
705
706             /***** Insert NPP_Print code here *****\
707             NPWindow* printWindow =
708                 &(printInfo->print.embedPrint.window);
709             void* platformPrint =
710                 printInfo->print.embedPrint.platformPrint;
711             \**************************************/
712         }
713     }
714 }
715
716 /******************************************************************************
717  * Windows-only methods
718  *****************************************************************************/
719 #if defined(XP_WIN)
720 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
721 {
722     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
723
724     switch( i_msg )
725     {
726         case WM_ERASEBKGND:
727             return 1L;
728
729         case WM_PAINT:
730         {
731             PAINTSTRUCT paintstruct;
732             HDC hdc;
733             RECT rect;
734
735             hdc = BeginPaint( p_hwnd, &paintstruct );
736
737             GetClientRect( p_hwnd, &rect );
738
739             FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
740             SetTextColor(hdc, RGB(255, 255, 255));
741             SetBkColor(hdc, RGB(0, 0, 0));
742             if( p_plugin->psz_text )
743                 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
744                           DT_CENTER|DT_VCENTER|DT_SINGLELINE);
745
746             EndPaint( p_hwnd, &paintstruct );
747             return 0L;
748         }
749         default:
750             /* delegate to default handler */
751             return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
752                                    i_msg, wpar, lpar );
753     }
754 }
755 #endif /* XP_WIN */
756
757 /******************************************************************************
758  * UNIX-only methods
759  *****************************************************************************/
760 #if defined(XP_UNIX)
761 static void Redraw( Widget w, XtPointer closure, XEvent *event )
762 {
763     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
764     Window control = p_plugin->getControlWindow();
765     const NPWindow& window = p_plugin->getWindow();
766     GC gc;
767     XGCValues gcv;
768     unsigned int i_control_height, i_control_width;
769
770     if( p_plugin->b_toolbar )
771         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
772     else
773         i_control_height = i_control_width = 0;
774
775     Window video = p_plugin->getVideoWindow();
776     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
777
778     gcv.foreground = BlackPixel( p_display, 0 );
779     gc = XCreateGC( p_display, video, GCForeground, &gcv );
780
781     XFillRectangle( p_display, video, gc,
782                     0, 0, window.width, window.height - i_control_height);
783
784     gcv.foreground = WhitePixel( p_display, 0 );
785     XChangeGC( p_display, gc, GCForeground, &gcv );
786
787     if( p_plugin->psz_text )
788         XDrawString( p_display, video, gc,
789                      window.width / 2 - 40, (window.height - i_control_height) / 2,
790                      p_plugin->psz_text, strlen(p_plugin->psz_text) );
791     XFreeGC( p_display, gc );
792
793     p_plugin->redrawToolbar();
794 }
795
796 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
797 {
798     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
799     const NPWindow& window = p_plugin->getWindow();
800
801     int i_height = window.height;
802     int i_width = window.width;
803     int i_xPos = event->xbutton.x;
804     int i_yPos = event->xbutton.y;
805
806     if( p_plugin && p_plugin->b_toolbar )
807     {
808         int i_playing;
809         libvlc_exception_t ex;
810
811         libvlc_exception_init( &ex );
812         libvlc_media_player_t *p_md = p_plugin->getMD(&ex);
813         libvlc_exception_clear( &ex );
814
815         i_playing = p_plugin->playlist_isplaying();
816
817         vlc_toolbar_clicked_t clicked;
818         clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
819         switch( clicked )
820         {
821             case clicked_Play:
822             case clicked_Pause:
823             {
824                 if( i_playing == 1 )
825                     p_plugin->playlist_pause( &ex );
826                 else
827                     p_plugin->playlist_play( &ex );
828
829                 libvlc_exception_clear( &ex );
830             }
831             break;
832
833             case clicked_Stop:
834             {
835                 p_plugin->playlist_stop();
836             }
837             break;
838
839             case clicked_Fullscreen:
840             {
841                 p_plugin->set_fullscreen( 1, &ex );
842                 libvlc_exception_clear( &ex );
843             }
844             break;
845
846             case clicked_Mute:
847             case clicked_Unmute:
848             {
849                 libvlc_audio_toggle_mute( p_plugin->getVLC() );
850             }
851             break;
852
853             case clicked_timeline:
854             {
855                 /* if a movie is loaded */
856                 if( p_md )
857                 {
858                     int64_t f_length;
859                     f_length = libvlc_media_player_get_length( p_md, &ex ) / 100;
860                     libvlc_exception_clear( &ex );
861
862                     f_length = (float)f_length *
863                             ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
864
865                     libvlc_media_player_set_time( p_md, f_length, &ex );
866                     libvlc_exception_clear( &ex );
867                 }
868             }
869             break;
870
871             case clicked_Time:
872             {
873                 /* Not implemented yet*/
874             }
875             break;
876
877             default: /* button_Unknown */
878             break;
879         }
880     }
881     Redraw( w, closure, event );
882 }
883
884 static void Resize ( Widget w, XtPointer closure, XEvent *event )
885 {
886     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
887     Window control = p_plugin->getControlWindow();
888     const NPWindow& window = p_plugin->getWindow();
889     Window  drawable   = p_plugin->getVideoWindow();
890     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
891
892     int i_ret;
893     Window root_return, parent_return, * children_return;
894     Window base_window;
895     unsigned int i_nchildren;
896     unsigned int i_control_height, i_control_width;
897
898     if( p_plugin->b_toolbar )
899     {
900         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
901     }
902     else
903     {
904         i_control_height = i_control_width = 0;
905     }
906
907 #ifdef X11_RESIZE_DEBUG
908     XWindowAttributes attr;
909
910     if( event && event->type == ConfigureNotify )
911     {
912         fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
913                  "send_event ? %s\n", event->xconfigure.width,
914                  event->xconfigure.height,
915                  event->xconfigure.send_event ? "TRUE" : "FALSE" );
916     }
917 #endif /* X11_RESIZE_DEBUG */
918
919     if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
920     {
921         /* size already set */
922         return;
923     }
924
925     i_ret = XResizeWindow( p_display, drawable,
926                            window.width, (window.height - i_control_height) );
927
928 #ifdef X11_RESIZE_DEBUG
929     fprintf( stderr,
930              "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
931
932     XGetWindowAttributes ( p_display, drawable, &attr );
933
934     /* X is asynchronous, so the current size reported here is not
935        necessarily the requested size as the Resize request may not
936        yet have been handled by the plugin host */
937     fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
938              attr.width, attr.height );
939 #endif /* X11_RESIZE_DEBUG */
940
941     XQueryTree( p_display, drawable,
942                 &root_return, &parent_return, &children_return,
943                 &i_nchildren );
944
945     if( i_nchildren > 0 )
946     {
947         /* XXX: Make assumptions related to the window parenting structure in
948            vlc/modules/video_output/x11/xcommon.c */
949         base_window = children_return[i_nchildren - 1];
950
951 #ifdef X11_RESIZE_DEBUG
952         fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
953         fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
954                  base_window );
955 #endif /* X11_RESIZE_DEBUG */
956
957         i_ret = XResizeWindow( p_display, base_window,
958                 window.width, ( window.height - i_control_height ) );
959
960 #ifdef X11_RESIZE_DEBUG
961         fprintf( stderr,
962                  "vlcshell::Resize() XResizeWindow(base) returned %d\n",
963                  i_ret );
964
965         XGetWindowAttributes( p_display, base_window, &attr );
966
967         fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
968                  attr.width, attr.height );
969 #endif /* X11_RESIZE_DEBUG */
970     }
971 }
972
973 #endif /* XP_UNIX */