]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcshell.cpp
ifdef out 32-bit Carbon API calls on x86_64
[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 /* This is from mozilla java, do we really need it? */
35 #if 0
36 #include <jri.h>
37 #endif
38
39 #include "vlcplugin.h"
40 #include "vlcshell.h"
41
42 /* Enable/disable debugging printf's for X11 resizing */
43 #undef X11_RESIZE_DEBUG
44
45 /*****************************************************************************
46  * Unix-only declarations
47 ******************************************************************************/
48 #if defined(XP_UNIX)
49
50 static void Redraw( Widget w, XtPointer closure, XEvent *event );
51 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
52 static void Resize( Widget w, XtPointer closure, XEvent *event );
53
54 #endif
55
56 /*****************************************************************************
57  * MacOS-only declarations
58 ******************************************************************************/
59 #ifdef XP_MACOSX
60 #endif
61
62 /*****************************************************************************
63  * Windows-only declarations
64  *****************************************************************************/
65 #ifdef XP_WIN
66
67 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
68
69 #endif
70
71 /******************************************************************************
72  * UNIX-only API calls
73  *****************************************************************************/
74 char * NPP_GetMIMEDescription( void )
75 {
76     static char mimetype[] = PLUGIN_MIMETYPES;
77     return mimetype;
78 }
79
80 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
81 {
82     static char psz_name[] = PLUGIN_NAME;
83     static char psz_desc[1000];
84
85     /* plugin class variables */
86     switch( variable )
87     {
88         case NPPVpluginNameString:
89             *((char **)value) = psz_name;
90             return NPERR_NO_ERROR;
91
92         case NPPVpluginDescriptionString:
93             snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
94                       libvlc_get_version() );
95             *((char **)value) = psz_desc;
96             return NPERR_NO_ERROR;
97
98         default:
99             /* move on to instance variables ... */
100             ;
101     }
102
103     if( instance == NULL )
104     {
105         return NPERR_INVALID_INSTANCE_ERROR;
106     }
107
108     /* plugin instance variables */
109
110     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
111     if( NULL == p_plugin )
112     {
113         // plugin has not been initialized yet !
114         return NPERR_INVALID_INSTANCE_ERROR;
115     }
116
117     switch( variable )
118     {
119         case NPPVpluginScriptableNPObject:
120         {
121             /* retrieve plugin root class */
122             NPClass *scriptClass = p_plugin->getScriptClass();
123             if( scriptClass )
124             {
125                 /* create an instance and return it */
126                 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
127                 return NPERR_NO_ERROR;
128             }
129             break;
130         }
131
132         default:
133             ;
134     }
135     return NPERR_GENERIC_ERROR;
136 }
137
138 /*
139  * there is some confusion in gecko headers regarding definition of this API
140  * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
141  */
142
143 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
144 {
145     return NPERR_GENERIC_ERROR;
146 }
147
148 /******************************************************************************
149  * Mac-only API calls
150  *****************************************************************************/
151 #ifdef XP_MACOSX
152 int16_t NPP_HandleEvent( NPP instance, void * event )
153 {
154     static UInt32 lastMouseUp = 0;
155     if( instance == NULL )
156     {
157         return false;
158     }
159
160     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
161     if( p_plugin == NULL )
162     {
163         return false;
164     }
165
166 #ifndef __x86_64__  
167     EventRecord *myEvent = (EventRecord*)event;
168
169     switch( myEvent->what )
170     {
171         case nullEvent:
172             return true;
173         case mouseDown:
174         {
175             if( (myEvent->when - lastMouseUp) < GetDblTime() )
176             {
177                 /* double click */
178                 p_plugin->toggle_fullscreen();
179             }
180             return true;
181         }
182         case mouseUp:
183             lastMouseUp = myEvent->when;
184             return true;
185         case keyUp:
186         case keyDown:
187         case autoKey:
188             return true;
189         case updateEvt:
190         {
191             const NPWindow& npwindow = p_plugin->getWindow();
192             if( npwindow.window )
193             {
194                 bool hasVout = false;
195
196                 if( p_plugin->playlist_isplaying() )
197                 {
198                     hasVout = p_plugin->player_has_vout();
199 #if 0
200                     if( hasVout )
201                     {
202                         libvlc_rectangle_t area;
203                         area.left = 0;
204                         area.top = 0;
205                         area.right = npwindow.width;
206                         area.bottom = npwindow.height;
207                         libvlc_video_redraw_rectangle(p_plugin->getMD(), &area, NULL);
208                     }
209 #else
210 #warning disabled code
211 #endif
212                 }
213
214                 if( ! hasVout )
215                 {
216                     /* draw the beautiful "No Picture" */
217
218                     ForeColor(blackColor);
219                     PenMode( patCopy );
220
221                     /* seems that firefox forgets to set the following
222                      * on occasion (reload) */
223                     SetOrigin(((NP_Port *)npwindow.window)->portx,
224                               ((NP_Port *)npwindow.window)->porty);
225
226                     Rect rect;
227                     rect.left = 0;
228                     rect.top = 0;
229                     rect.right = npwindow.width;
230                     rect.bottom = npwindow.height;
231                     PaintRect( &rect );
232
233                     ForeColor(whiteColor);
234                     MoveTo( (npwindow.width-80)/ 2  , npwindow.height / 2 );
235                     if( p_plugin->psz_text )
236                         DrawText( p_plugin->psz_text, 0, strlen(p_plugin->psz_text) );
237                 }
238             }
239             return true;
240         }
241         case activateEvt:
242             return false;
243         case NPEventType_GetFocusEvent:
244         case NPEventType_LoseFocusEvent:
245             return true;
246         case NPEventType_AdjustCursorEvent:
247             return false;
248         case NPEventType_MenuCommandEvent:
249             return false;
250         case NPEventType_ClippingChangedEvent:
251             return false;
252         case NPEventType_ScrollingBeginsEvent:
253             return true;
254         case NPEventType_ScrollingEndsEvent:
255             return true;
256         default:
257             ;
258     }
259 #endif // __x86_64__
260     return false;
261 }
262 #endif /* XP_MACOSX */
263
264 /******************************************************************************
265  * General Plug-in Calls
266  *****************************************************************************/
267 NPError NPP_Initialize( void )
268 {
269     return NPERR_NO_ERROR;
270 }
271
272 #ifdef OJI
273 jref NPP_GetJavaClass( void )
274 {
275     return NULL;
276 }
277 #endif
278
279 void NPP_Shutdown( void )
280 {
281     ;
282 }
283
284 NPError NPP_New( NPMIMEType pluginType, NPP instance,
285 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
286                  uint16 mode, int16 argc,
287 #else
288                  uint16_t mode, int16_t argc,
289 #endif
290                  char* argn[], char* argv[], NPSavedData* saved )
291 {
292     NPError status;
293
294     if( instance == NULL )
295     {
296         return NPERR_INVALID_INSTANCE_ERROR;
297     }
298
299     VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
300     if( NULL == p_plugin )
301     {
302         return NPERR_OUT_OF_MEMORY_ERROR;
303     }
304
305     status = p_plugin->init(argc, argn, argv);
306     if( NPERR_NO_ERROR == status )
307     {
308         instance->pdata = reinterpret_cast<void*>(p_plugin);
309 #if 0
310         NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
311         NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
312 #endif
313     }
314     else
315     {
316         delete p_plugin;
317     }
318     return status;
319 }
320
321 NPError NPP_Destroy( NPP instance, NPSavedData** save )
322 {
323     if( NULL == instance )
324         return NPERR_INVALID_INSTANCE_ERROR;
325
326     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
327     if( NULL == p_plugin )
328         return NPERR_NO_ERROR;
329
330     instance->pdata = NULL;
331
332 #if defined(XP_WIN)
333     HWND win = (HWND)p_plugin->getWindow().window;
334     WNDPROC winproc = p_plugin->getWindowProc();
335     if( winproc )
336     {
337         /* reset WNDPROC */
338         SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
339     }
340 #endif
341
342     if( p_plugin->playlist_isplaying() )
343         p_plugin->playlist_stop();
344
345     delete p_plugin;
346
347     return NPERR_NO_ERROR;
348 }
349
350 NPError NPP_SetWindow( NPP instance, NPWindow* window )
351 {
352 #if defined(XP_UNIX)
353     Window control;
354     unsigned int i_control_height = 0, i_control_width = 0;
355 #endif
356
357     if( ! instance )
358     {
359         return NPERR_INVALID_INSTANCE_ERROR;
360     }
361
362     /* NPP_SetWindow may be called before NPP_New (Opera) */
363     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
364     if( NULL == p_plugin )
365     {
366         /* we should probably show a splash screen here */
367         return NPERR_NO_ERROR;
368     }
369
370 #if defined(XP_UNIX)
371     control = p_plugin->getControlWindow();
372 #endif
373
374     libvlc_instance_t *p_vlc = p_plugin->getVLC();
375
376     /*
377      * PLUGIN DEVELOPERS:
378      *  Before setting window to point to the
379      *  new window, you may wish to compare the new window
380      *  info to the previous window (if any) to note window
381      *  size changes, etc.
382      */
383
384     /* retrieve current window */
385     NPWindow& curwin = p_plugin->getWindow();
386
387 #ifdef XP_MACOSX
388     if( window && window->window )
389     {
390         /* check if plugin has a new parent window */
391         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
392
393         /* as MacOS X video output is windowless, set viewport */
394         libvlc_rectangle_t view, clip;
395
396         /*
397         ** browser sets port origin to top-left location of plugin
398         ** relative to GrafPort window origin is set relative to document,
399         ** which of little use for drawing
400         */
401         view.top     = ((NP_Port*) (window->window))->porty;
402         view.left    = ((NP_Port*) (window->window))->portx;
403         view.bottom  = window->height+view.top;
404         view.right   = window->width+view.left;
405
406         /* clipRect coordinates are also relative to GrafPort */
407         clip.top     = window->clipRect.top;
408         clip.left    = window->clipRect.left;
409         clip.bottom  = window->clipRect.bottom;
410         clip.right   = window->clipRect.right;
411 #ifdef NOT_WORKING
412         libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
413 #else
414 #warning disabled code
415 #endif
416         /* remember new window */
417         p_plugin->setWindow(*window);
418     }
419     else if( curwin.window )
420     {
421         /* change/set parent */
422         curwin.window = NULL;
423     }
424 #endif /* XP_MACOSX */
425
426 #ifdef XP_WIN
427     if( window && window->window )
428     {
429         /* check if plugin has a new parent window */
430         HWND drawable = (HWND) (window->window);
431         if( !curwin.window || drawable != curwin.window )
432         {
433             /* reset previous window settings */
434             HWND oldwin = (HWND)p_plugin->getWindow().window;
435             WNDPROC oldproc = p_plugin->getWindowProc();
436             if( oldproc )
437             {
438                 /* reset WNDPROC */
439                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
440             }
441             /* attach our plugin object */
442             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
443                              reinterpret_cast<LONG_PTR>(p_plugin));
444
445             /* install our WNDPROC */
446             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
447                                              GWL_WNDPROC, (LONG)Manage ) );
448
449             /* change window style to our liking */
450             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
451             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
452             SetWindowLong((HWND)drawable, GWL_STYLE, style);
453
454             /* remember new window */
455             p_plugin->setWindow(*window);
456
457             /* Redraw window */
458             InvalidateRect( (HWND)drawable, NULL, TRUE );
459             UpdateWindow( (HWND)drawable );
460         }
461     }
462     else if( curwin.window )
463     {
464         /* reset WNDPROC */
465         HWND oldwin = (HWND)curwin.window;
466         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
467         p_plugin->setWindowProc(NULL);
468
469         curwin.window = NULL;
470     }
471 #endif /* XP_WIN */
472
473 #if defined(XP_UNIX)
474     /* default to hidden toolbar, shown at the end of this method if asked *
475      * developers note : getToolbarSize need to wait the end of this method
476      */
477     i_control_height = 0;
478     i_control_width = window->width;
479
480     if( window && window->window )
481     {
482         Window  parent  = (Window) window->window;
483         if( !curwin.window || (parent != (Window)curwin.window) )
484         {
485             Display *p_display = ( (NPSetWindowCallbackStruct *)
486                                    window->ws_info )->display;
487
488             XResizeWindow( p_display, parent, window->width, window->height );
489
490             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
491
492             /* create windows */
493             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
494                            window->width, window->height - i_control_height,
495                            0, i_blackColor, i_blackColor );
496             Window controls = (Window) NULL;
497             controls = XCreateSimpleWindow( p_display, parent,
498                             0, window->height - i_control_height-1,
499                             window->width, i_control_height-1,
500                             0, i_blackColor, i_blackColor );
501
502             XMapWindow( p_display, parent );
503             XMapWindow( p_display, video );
504             if( controls ) { XMapWindow( p_display, controls ); }
505
506             XFlush(p_display);
507
508             /* bind events */
509             Widget w = XtWindowToWidget( p_display, parent );
510
511             XtAddEventHandler( w, ExposureMask, FALSE,
512                                (XtEventHandler)Redraw, p_plugin );
513             XtAddEventHandler( w, StructureNotifyMask, FALSE,
514                                (XtEventHandler)Resize, p_plugin );
515             XtAddEventHandler( w, ButtonReleaseMask, FALSE,
516                                (XtEventHandler)ControlHandler, p_plugin );
517
518             /* remember window */
519             p_plugin->setWindow( *window );
520             p_plugin->setVideoWindow( video );
521
522             if( controls )
523             {
524                 p_plugin->setControlWindow( controls );
525             }
526
527             Redraw( w, (XtPointer)p_plugin, NULL );
528
529             /* now display toolbar if asked through parameters */
530             if( p_plugin->b_toolbar )
531             {
532                 p_plugin->showToolbar();
533             }
534         }
535     }
536     else if( curwin.window )
537     {
538         curwin.window = NULL;
539     }
540 #endif /* XP_UNIX */
541
542     if( !p_plugin->b_stream )
543     {
544         if( p_plugin->psz_target )
545         {
546             if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
547             {
548                 if( p_plugin->b_autoplay )
549                 {
550                     p_plugin->playlist_play();
551                 }
552             }
553             p_plugin->b_stream = true;
554         }
555     }
556     return NPERR_NO_ERROR;
557 }
558
559 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
560 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
561                        NPBool seekable, uint16 *stype )
562 #else
563                        NPBool seekable, uint16_t *stype )
564 #endif
565 {
566     if( NULL == instance  )
567     {
568         return NPERR_INVALID_INSTANCE_ERROR;
569     }
570
571     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
572     if( NULL == p_plugin )
573     {
574         return NPERR_INVALID_INSTANCE_ERROR;
575     }
576
577    /*
578    ** Firefox/Mozilla may decide to open a stream from the URL specified
579    ** in the SRC parameter of the EMBED tag and pass it to us
580    **
581    ** since VLC will open the SRC URL as well, we're not interested in
582    ** that stream. Otherwise, we'll take it and queue it up in the playlist
583    */
584     if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
585     {
586         /* TODO: use pipes !!!! */
587         *stype = NP_ASFILEONLY;
588         return NPERR_NO_ERROR;
589     }
590     return NPERR_GENERIC_ERROR;
591 }
592
593 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
594 int32 NPP_WriteReady( NPP instance, NPStream *stream )
595 #else
596 int32_t NPP_WriteReady( NPP instance, NPStream *stream )
597 #endif
598 {
599     /* TODO */
600     return 8*1024;
601 }
602
603 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
604 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
605                  int32 len, void *buffer )
606 #else
607 int32_t NPP_Write( NPP instance, NPStream *stream, int32_t offset,
608                  int32_t len, void *buffer )
609 #endif
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 ) != -1 )
638     {
639         if( p_plugin->b_autoplay )
640         {
641             p_plugin->playlist_play();
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 defined(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
817         libvlc_media_player_t *p_md = p_plugin->getMD();
818
819         i_playing = p_plugin->playlist_isplaying();
820
821         vlc_toolbar_clicked_t clicked;
822         clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
823         switch( clicked )
824         {
825             case clicked_Play:
826             case clicked_Pause:
827             {
828                 if( i_playing == 1 )
829                     p_plugin->playlist_pause();
830                 else
831                     p_plugin->playlist_play();
832             }
833             break;
834
835             case clicked_Stop:
836             {
837                 p_plugin->playlist_stop();
838             }
839             break;
840
841             case clicked_Fullscreen:
842             {
843                 p_plugin->set_fullscreen( 1 );
844             }
845             break;
846
847             case clicked_Mute:
848             case clicked_Unmute:
849             {
850                 if( p_md )
851                     libvlc_audio_toggle_mute( p_md );
852             }
853             break;
854
855             case clicked_timeline:
856             {
857                 /* if a movie is loaded */
858                 if( p_md )
859                 {
860                     int64_t f_length;
861                     f_length = libvlc_media_player_get_length( p_md ) / 100;
862
863                     f_length = (float)f_length *
864                             ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
865
866                     libvlc_media_player_set_time( p_md, f_length );
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 */