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