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