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