]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcshell.cpp
Remove references to mozilla-config.h, remove linkage with libnspr and other xpcom...
[vlc] / projects / mozilla / vlcshell.cpp
1 /*****************************************************************************
2  * vlcshell.cpp: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Jean-Paul Saman <jpsaman@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "config.h"
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 /* This is from mozilla java, do we really need it? */
35 #if 0
36 #include <jri.h>
37 #endif
38
39 #include "vlcplugin.h"
40 #include "vlcshell.h"
41
42 /* Enable/disable debugging printf's for X11 resizing */
43 #undef X11_RESIZE_DEBUG
44
45 /*****************************************************************************
46  * Unix-only declarations
47 ******************************************************************************/
48 #if defined(XP_UNIX)
49
50 static void Redraw( Widget w, XtPointer closure, XEvent *event );
51 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
52 static void Resize( Widget w, XtPointer closure, XEvent *event );
53
54 #endif
55
56 /*****************************************************************************
57  * MacOS-only declarations
58 ******************************************************************************/
59 #ifdef XP_MACOSX
60 #endif
61
62 /*****************************************************************************
63  * Windows-only declarations
64  *****************************************************************************/
65 #ifdef XP_WIN
66
67 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
68
69 #endif
70
71 /******************************************************************************
72  * UNIX-only API calls
73  *****************************************************************************/
74 char * NPP_GetMIMEDescription( void )
75 {
76     static char mimetype[] = PLUGIN_MIMETYPES;
77     return mimetype;
78 }
79
80 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
81 {
82     static char psz_name[] = PLUGIN_NAME;
83     static char psz_desc[1000];
84
85     /* plugin class variables */
86     switch( variable )
87     {
88         case NPPVpluginNameString:
89             *((char **)value) = psz_name;
90             return NPERR_NO_ERROR;
91
92         case NPPVpluginDescriptionString:
93             snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
94                       libvlc_get_version() );
95             *((char **)value) = psz_desc;
96             return NPERR_NO_ERROR;
97
98         default:
99             /* move on to instance variables ... */
100             ;
101     }
102
103     if( instance == NULL )
104     {
105         return NPERR_INVALID_INSTANCE_ERROR;
106     }
107
108     /* plugin instance variables */
109
110     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
111     if( NULL == p_plugin )
112     {
113         // plugin has not been initialized yet !
114         return NPERR_INVALID_INSTANCE_ERROR;
115     }
116
117     switch( variable )
118     {
119         case NPPVpluginScriptableNPObject:
120         {
121             /* retrieve plugin root class */
122             NPClass *scriptClass = p_plugin->getScriptClass();
123             if( scriptClass )
124             {
125                 /* create an instance and return it */
126                 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
127                 return NPERR_NO_ERROR;
128             }
129             break;
130         }
131
132         default:
133             ;
134     }
135     return NPERR_GENERIC_ERROR;
136 }
137
138 /*
139  * there is some confusion in gecko headers regarding definition of this API
140  * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
141  */
142
143 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
144 {
145     return NPERR_GENERIC_ERROR;
146 }
147
148 /******************************************************************************
149  * Mac-only API calls
150  *****************************************************************************/
151 #ifdef XP_MACOSX
152 int16_t NPP_HandleEvent( NPP instance, void * event )
153 {
154     static UInt32 lastMouseUp = 0;
155     if( instance == NULL )
156     {
157         return false;
158     }
159
160     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
161     if( p_plugin == NULL )
162     {
163         return false;
164     }
165
166     EventRecord *myEvent = (EventRecord*)event;
167
168     switch( myEvent->what )
169     {
170         case nullEvent:
171             return true;
172         case mouseDown:
173         {
174             if( (myEvent->when - lastMouseUp) < GetDblTime() )
175             {
176                 /* double click */
177                 p_plugin->toggle_fullscreen();
178             }
179             return true;
180         }
181         case mouseUp:
182             lastMouseUp = myEvent->when;
183             return true;
184         case keyUp:
185         case keyDown:
186         case autoKey:
187             return true;
188         case updateEvt:
189         {
190             const NPWindow& npwindow = p_plugin->getWindow();
191             if( npwindow.window )
192             {
193                 bool hasVout = false;
194
195                 if( p_plugin->playlist_isplaying() )
196                 {
197                     hasVout = p_plugin->player_has_vout();
198 #if 0
199                     if( hasVout )
200                     {
201                         libvlc_rectangle_t area;
202                         area.left = 0;
203                         area.top = 0;
204                         area.right = npwindow.width;
205                         area.bottom = npwindow.height;
206                         libvlc_video_redraw_rectangle(p_plugin->getMD(), &area, NULL);
207                     }
208 #else
209 #warning disabled code
210 #endif
211                 }
212
213                 if( ! hasVout )
214                 {
215                     /* draw the beautiful "No Picture" */
216
217                     ForeColor(blackColor);
218                     PenMode( patCopy );
219
220                     /* seems that firefox forgets to set the following
221                      * on occasion (reload) */
222                     SetOrigin(((NP_Port *)npwindow.window)->portx,
223                               ((NP_Port *)npwindow.window)->porty);
224
225                     Rect rect;
226                     rect.left = 0;
227                     rect.top = 0;
228                     rect.right = npwindow.width;
229                     rect.bottom = npwindow.height;
230                     PaintRect( &rect );
231
232                     ForeColor(whiteColor);
233                     MoveTo( (npwindow.width-80)/ 2  , npwindow.height / 2 );
234                     if( p_plugin->psz_text )
235                         DrawText( p_plugin->psz_text, 0, strlen(p_plugin->psz_text) );
236                 }
237             }
238             return true;
239         }
240         case activateEvt:
241             return false;
242         case NPEventType_GetFocusEvent:
243         case NPEventType_LoseFocusEvent:
244             return true;
245         case NPEventType_AdjustCursorEvent:
246             return false;
247         case NPEventType_MenuCommandEvent:
248             return false;
249         case NPEventType_ClippingChangedEvent:
250             return false;
251         case NPEventType_ScrollingBeginsEvent:
252             return true;
253         case NPEventType_ScrollingEndsEvent:
254             return true;
255         default:
256             ;
257     }
258     return false;
259 }
260 #endif /* XP_MACOSX */
261
262 /******************************************************************************
263  * General Plug-in Calls
264  *****************************************************************************/
265 NPError NPP_Initialize( void )
266 {
267     return NPERR_NO_ERROR;
268 }
269
270 #ifdef OJI
271 jref NPP_GetJavaClass( void )
272 {
273     return NULL;
274 }
275 #endif
276
277 void NPP_Shutdown( void )
278 {
279     ;
280 }
281
282 NPError NPP_New( NPMIMEType pluginType, NPP instance,
283 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
284                  uint16 mode, int16 argc,
285 #else
286                  uint16_t mode, int16_t argc,
287 #endif
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 defined(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     if( p_plugin->playlist_isplaying() )
341         p_plugin->playlist_stop();
342
343     delete p_plugin;
344
345     return NPERR_NO_ERROR;
346 }
347
348 NPError NPP_SetWindow( NPP instance, NPWindow* window )
349 {
350 #if defined(XP_UNIX)
351     Window control;
352     unsigned int i_control_height = 0, i_control_width = 0;
353 #endif
354
355     if( ! instance )
356     {
357         return NPERR_INVALID_INSTANCE_ERROR;
358     }
359
360     /* NPP_SetWindow may be called before NPP_New (Opera) */
361     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
362     if( NULL == p_plugin )
363     {
364         /* we should probably show a splash screen here */
365         return NPERR_NO_ERROR;
366     }
367
368 #if defined(XP_UNIX)
369     control = p_plugin->getControlWindow();
370 #endif
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 #ifdef NOT_WORKING
410         libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
411 #else
412 #warning disabled code
413 #endif
414         /* remember new window */
415         p_plugin->setWindow(*window);
416     }
417     else if( curwin.window )
418     {
419         /* change/set parent */
420         curwin.window = NULL;
421     }
422 #endif /* XP_MACOSX */
423
424 #ifdef XP_WIN
425     if( window && window->window )
426     {
427         /* check if plugin has a new parent window */
428         HWND drawable = (HWND) (window->window);
429         if( !curwin.window || drawable != curwin.window )
430         {
431             /* reset previous window settings */
432             HWND oldwin = (HWND)p_plugin->getWindow().window;
433             WNDPROC oldproc = p_plugin->getWindowProc();
434             if( oldproc )
435             {
436                 /* reset WNDPROC */
437                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
438             }
439             /* attach our plugin object */
440             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
441                              reinterpret_cast<LONG_PTR>(p_plugin));
442
443             /* install our WNDPROC */
444             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
445                                              GWL_WNDPROC, (LONG)Manage ) );
446
447             /* change window style to our liking */
448             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
449             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
450             SetWindowLong((HWND)drawable, GWL_STYLE, style);
451
452             /* remember new window */
453             p_plugin->setWindow(*window);
454
455             /* Redraw window */
456             InvalidateRect( (HWND)drawable, NULL, TRUE );
457             UpdateWindow( (HWND)drawable );
458         }
459     }
460     else if( curwin.window )
461     {
462         /* reset WNDPROC */
463         HWND oldwin = (HWND)curwin.window;
464         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
465         p_plugin->setWindowProc(NULL);
466
467         curwin.window = NULL;
468     }
469 #endif /* XP_WIN */
470
471 #if defined(XP_UNIX)
472     /* default to hidden toolbar, shown at the end of this method if asked *
473      * developers note : getToolbarSize need to wait the end of this method
474      */
475     i_control_height = 0;
476     i_control_width = window->width;
477
478     if( window && window->window )
479     {
480         Window  parent  = (Window) window->window;
481         if( !curwin.window || (parent != (Window)curwin.window) )
482         {
483             Display *p_display = ( (NPSetWindowCallbackStruct *)
484                                    window->ws_info )->display;
485
486             XResizeWindow( p_display, parent, window->width, window->height );
487
488             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
489
490             /* create windows */
491             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
492                            window->width, window->height - i_control_height,
493                            0, i_blackColor, i_blackColor );
494             Window controls = (Window) NULL;
495             controls = XCreateSimpleWindow( p_display, parent,
496                             0, window->height - i_control_height-1,
497                             window->width, i_control_height-1,
498                             0, i_blackColor, i_blackColor );
499
500             XMapWindow( p_display, parent );
501             XMapWindow( p_display, video );
502             if( controls ) { XMapWindow( p_display, controls ); }
503
504             XFlush(p_display);
505
506             /* bind events */
507             Widget w = XtWindowToWidget( p_display, parent );
508
509             XtAddEventHandler( w, ExposureMask, FALSE,
510                                (XtEventHandler)Redraw, p_plugin );
511             XtAddEventHandler( w, StructureNotifyMask, FALSE,
512                                (XtEventHandler)Resize, p_plugin );
513             XtAddEventHandler( w, ButtonReleaseMask, FALSE,
514                                (XtEventHandler)ControlHandler, p_plugin );
515
516             /* remember window */
517             p_plugin->setWindow( *window );
518             p_plugin->setVideoWindow( video );
519
520             if( controls )
521             {
522                 p_plugin->setControlWindow( controls );
523             }
524
525             Redraw( w, (XtPointer)p_plugin, NULL );
526
527             /* now display toolbar if asked through parameters */
528             if( p_plugin->b_toolbar )
529             {
530                 p_plugin->showToolbar();
531             }
532         }
533     }
534     else if( curwin.window )
535     {
536         curwin.window = NULL;
537     }
538 #endif /* XP_UNIX */
539
540     if( !p_plugin->b_stream )
541     {
542         if( p_plugin->psz_target )
543         {
544             if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
545             {
546                 if( p_plugin->b_autoplay )
547                 {
548                     p_plugin->playlist_play();
549                 }
550             }
551             p_plugin->b_stream = true;
552         }
553     }
554     return NPERR_NO_ERROR;
555 }
556
557 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
558 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
559                        NPBool seekable, uint16 *stype )
560 #else
561                        NPBool seekable, uint16_t *stype )
562 #endif
563 {
564     if( NULL == instance  )
565     {
566         return NPERR_INVALID_INSTANCE_ERROR;
567     }
568
569     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
570     if( NULL == p_plugin )
571     {
572         return NPERR_INVALID_INSTANCE_ERROR;
573     }
574
575    /*
576    ** Firefox/Mozilla may decide to open a stream from the URL specified
577    ** in the SRC parameter of the EMBED tag and pass it to us
578    **
579    ** since VLC will open the SRC URL as well, we're not interested in
580    ** that stream. Otherwise, we'll take it and queue it up in the playlist
581    */
582     if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
583     {
584         /* TODO: use pipes !!!! */
585         *stype = NP_ASFILEONLY;
586         return NPERR_NO_ERROR;
587     }
588     return NPERR_GENERIC_ERROR;
589 }
590
591 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
592 int32 NPP_WriteReady( NPP instance, NPStream *stream )
593 #else
594 int32_t NPP_WriteReady( NPP instance, NPStream *stream )
595 #endif
596 {
597     /* TODO */
598     return 8*1024;
599 }
600
601 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
602 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
603                  int32 len, void *buffer )
604 #else
605 int32_t NPP_Write( NPP instance, NPStream *stream, int32_t offset,
606                  int32_t len, void *buffer )
607 #endif
608 {
609     /* TODO */
610     return len;
611 }
612
613 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
614 {
615     if( instance == NULL )
616     {
617         return NPERR_INVALID_INSTANCE_ERROR;
618     }
619     return NPERR_NO_ERROR;
620 }
621
622 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
623 {
624     if( instance == NULL )
625     {
626         return;
627     }
628
629     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
630     if( NULL == p_plugin )
631     {
632         return;
633     }
634
635     if( p_plugin->playlist_add( stream->url ) != -1 )
636     {
637         if( p_plugin->b_autoplay )
638         {
639             p_plugin->playlist_play();
640         }
641     }
642 }
643
644 void NPP_URLNotify( NPP instance, const char* url,
645                     NPReason reason, void* notifyData )
646 {
647     /***** Insert NPP_URLNotify code here *****\
648     PluginInstance* p_plugin;
649     if (instance != NULL)
650         p_plugin = (PluginInstance*) instance->pdata;
651     \*********************************************/
652 }
653
654 void NPP_Print( NPP instance, NPPrint* printInfo )
655 {
656     if( printInfo == NULL )
657     {
658         return;
659     }
660
661     if( instance != NULL )
662     {
663         /***** Insert NPP_Print code here *****\
664         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
665         \**************************************/
666
667         if( printInfo->mode == NP_FULL )
668         {
669             /*
670              * PLUGIN DEVELOPERS:
671              *  If your plugin would like to take over
672              *  printing completely when it is in full-screen mode,
673              *  set printInfo->pluginPrinted to TRUE and print your
674              *  plugin as you see fit.  If your plugin wants Netscape
675              *  to handle printing in this case, set
676              *  printInfo->pluginPrinted to FALSE (the default) and
677              *  do nothing.  If you do want to handle printing
678              *  yourself, printOne is true if the print button
679              *  (as opposed to the print menu) was clicked.
680              *  On the Macintosh, platformPrint is a THPrint; on
681              *  Windows, platformPrint is a structure
682              *  (defined in npapi.h) containing the printer name, port,
683              *  etc.
684              */
685
686             /***** Insert NPP_Print code here *****\
687             void* platformPrint =
688                 printInfo->print.fullPrint.platformPrint;
689             NPBool printOne =
690                 printInfo->print.fullPrint.printOne;
691             \**************************************/
692
693             /* Do the default*/
694             printInfo->print.fullPrint.pluginPrinted = FALSE;
695         }
696         else
697         {
698             /* If not fullscreen, we must be embedded */
699             /*
700              * PLUGIN DEVELOPERS:
701              *  If your plugin is embedded, or is full-screen
702              *  but you returned false in pluginPrinted above, NPP_Print
703              *  will be called with mode == NP_EMBED.  The NPWindow
704              *  in the printInfo gives the location and dimensions of
705              *  the embedded plugin on the printed page.  On the
706              *  Macintosh, platformPrint is the printer port; on
707              *  Windows, platformPrint is the handle to the printing
708              *  device context.
709              */
710
711             /***** Insert NPP_Print code here *****\
712             NPWindow* printWindow =
713                 &(printInfo->print.embedPrint.window);
714             void* platformPrint =
715                 printInfo->print.embedPrint.platformPrint;
716             \**************************************/
717         }
718     }
719 }
720
721 /******************************************************************************
722  * Windows-only methods
723  *****************************************************************************/
724 #if defined(XP_WIN)
725 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
726 {
727     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
728
729     switch( i_msg )
730     {
731         case WM_ERASEBKGND:
732             return 1L;
733
734         case WM_PAINT:
735         {
736             PAINTSTRUCT paintstruct;
737             HDC hdc;
738             RECT rect;
739
740             hdc = BeginPaint( p_hwnd, &paintstruct );
741
742             GetClientRect( p_hwnd, &rect );
743
744             FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
745             SetTextColor(hdc, RGB(255, 255, 255));
746             SetBkColor(hdc, RGB(0, 0, 0));
747             if( p_plugin->psz_text )
748                 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_text), &rect,
749                           DT_CENTER|DT_VCENTER|DT_SINGLELINE);
750
751             EndPaint( p_hwnd, &paintstruct );
752             return 0L;
753         }
754         default:
755             /* delegate to default handler */
756             return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
757                                    i_msg, wpar, lpar );
758     }
759 }
760 #endif /* XP_WIN */
761
762 /******************************************************************************
763  * UNIX-only methods
764  *****************************************************************************/
765 #if defined(XP_UNIX)
766 static void Redraw( Widget w, XtPointer closure, XEvent *event )
767 {
768     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
769     Window control = p_plugin->getControlWindow();
770     const NPWindow& window = p_plugin->getWindow();
771     GC gc;
772     XGCValues gcv;
773     unsigned int i_control_height, i_control_width;
774
775     if( p_plugin->b_toolbar )
776         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
777     else
778         i_control_height = i_control_width = 0;
779
780     Window video = p_plugin->getVideoWindow();
781     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
782
783     gcv.foreground = BlackPixel( p_display, 0 );
784     gc = XCreateGC( p_display, video, GCForeground, &gcv );
785
786     XFillRectangle( p_display, video, gc,
787                     0, 0, window.width, window.height - i_control_height);
788
789     gcv.foreground = WhitePixel( p_display, 0 );
790     XChangeGC( p_display, gc, GCForeground, &gcv );
791
792     if( p_plugin->psz_text )
793         XDrawString( p_display, video, gc,
794                      window.width / 2 - 40, (window.height - i_control_height) / 2,
795                      p_plugin->psz_text, strlen(p_plugin->psz_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
815         libvlc_media_player_t *p_md = p_plugin->getMD();
816
817         i_playing = p_plugin->playlist_isplaying();
818
819         vlc_toolbar_clicked_t clicked;
820         clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
821         switch( clicked )
822         {
823             case clicked_Play:
824             case clicked_Pause:
825             {
826                 if( i_playing == 1 )
827                     p_plugin->playlist_pause();
828                 else
829                     p_plugin->playlist_play();
830             }
831             break;
832
833             case clicked_Stop:
834             {
835                 p_plugin->playlist_stop();
836             }
837             break;
838
839             case clicked_Fullscreen:
840             {
841                 p_plugin->set_fullscreen( 1 );
842             }
843             break;
844
845             case clicked_Mute:
846             case clicked_Unmute:
847             {
848                 if( p_md )
849                     libvlc_audio_toggle_mute( p_md );
850             }
851             break;
852
853             case clicked_timeline:
854             {
855                 /* if a movie is loaded */
856                 if( p_md )
857                 {
858                     int64_t f_length;
859                     f_length = libvlc_media_player_get_length( p_md ) / 100;
860
861                     f_length = (float)f_length *
862                             ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
863
864                     libvlc_media_player_set_time( p_md, f_length );
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 */