]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcshell.cpp
mozilla: correct comment
[vlc] / projects / mozilla / vlcshell.cpp
1 /*****************************************************************************
2  * vlcshell.cpp: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Jean-Paul Saman <jpsaman@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "config.h"
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 /* This is from mozilla java, do we really need it? */
35 #if 0
36 #include <jri.h>
37 #endif
38
39 #include "vlcplugin.h"
40 #include "vlcshell.h"
41
42 /* Enable/disable debugging printf's for X11 resizing */
43 #undef X11_RESIZE_DEBUG
44
45 /*****************************************************************************
46  * Unix-only declarations
47 ******************************************************************************/
48 #if defined(XP_UNIX)
49
50 static void Redraw( Widget w, XtPointer closure, XEvent *event );
51 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
52 static void Resize( Widget w, XtPointer closure, XEvent *event );
53
54 #endif
55
56 /*****************************************************************************
57  * MacOS-only declarations
58 ******************************************************************************/
59 #ifdef XP_MACOSX
60 #endif
61
62 /*****************************************************************************
63  * Windows-only declarations
64  *****************************************************************************/
65 #ifdef XP_WIN
66
67 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
68
69 #endif
70
71 /******************************************************************************
72  * UNIX-only API calls
73  *****************************************************************************/
74 char * NPP_GetMIMEDescription( void )
75 {
76     static char mimetype[] = PLUGIN_MIMETYPES;
77     return mimetype;
78 }
79
80 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
81 {
82     static char psz_name[] = PLUGIN_NAME;
83     static char psz_desc[1000];
84
85     /* plugin class variables */
86     switch( variable )
87     {
88         case NPPVpluginNameString:
89             *((char **)value) = psz_name;
90             return NPERR_NO_ERROR;
91
92         case NPPVpluginDescriptionString:
93             snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
94                       libvlc_get_version() );
95             *((char **)value) = psz_desc;
96             return NPERR_NO_ERROR;
97
98         default:
99             /* move on to instance variables ... */
100             ;
101     }
102
103     if( instance == NULL )
104     {
105         return NPERR_INVALID_INSTANCE_ERROR;
106     }
107
108     /* plugin instance variables */
109
110     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
111     if( NULL == p_plugin )
112     {
113         /* plugin has not been initialized yet ! */
114         return NPERR_INVALID_INSTANCE_ERROR;
115     }
116
117     switch( variable )
118     {
119         case NPPVpluginScriptableNPObject:
120         {
121             /* retrieve plugin root class */
122             NPClass *scriptClass = p_plugin->getScriptClass();
123             if( scriptClass )
124             {
125                 /* create an instance and return it */
126                 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
127                 return NPERR_NO_ERROR;
128             }
129             break;
130         }
131
132         default:
133             ;
134     }
135     return NPERR_GENERIC_ERROR;
136 }
137
138 /*
139  * there is some confusion in gecko headers regarding definition of this API
140  * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
141  */
142
143 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
144 {
145     return NPERR_GENERIC_ERROR;
146 }
147
148 /******************************************************************************
149  * Mac-only API calls
150  *****************************************************************************/
151 #ifdef XP_MACOSX
152 int16_t NPP_HandleEvent( NPP instance, void * event )
153 {
154     static UInt32 lastMouseUp = 0;
155     if( instance == NULL )
156     {
157         return false;
158     }
159
160     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
161     if( p_plugin == NULL )
162     {
163         return false;
164     }
165
166 #ifndef __x86_64__  
167     EventRecord *myEvent = (EventRecord*)event;
168
169     switch( myEvent->what )
170     {
171         case nullEvent:
172             return true;
173         case mouseDown:
174         {
175             if( (myEvent->when - lastMouseUp) < GetDblTime() )
176             {
177                 /* double click */
178                 p_plugin->toggle_fullscreen();
179             }
180             return true;
181         }
182         case mouseUp:
183             lastMouseUp = myEvent->when;
184             return true;
185         case keyUp:
186         case keyDown:
187         case autoKey:
188             return true;
189         case updateEvt:
190         {
191             const NPWindow& npwindow = p_plugin->getWindow();
192             if( npwindow.window )
193             {
194                 bool hasVout = false;
195
196                 if( p_plugin->playlist_isplaying() )
197                 {
198                     hasVout = p_plugin->player_has_vout();
199 #if 0
200                     if( hasVout )
201                     {
202                         libvlc_rectangle_t area;
203                         area.left = 0;
204                         area.top = 0;
205                         area.right = npwindow.width;
206                         area.bottom = npwindow.height;
207                         libvlc_video_redraw_rectangle(p_plugin->getMD(), &area, NULL);
208                     }
209 #else
210 #warning disabled code
211 #endif
212                 }
213
214                 if( ! hasVout )
215                 {
216                     /* draw the text from p_plugin->psz_text */
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 #endif // __x86_64__
259     return false;
260 }
261 #endif /* XP_MACOSX */
262
263 /******************************************************************************
264  * General Plug-in Calls
265  *****************************************************************************/
266 NPError NPP_Initialize( void )
267 {
268     return NPERR_NO_ERROR;
269 }
270
271 #ifdef OJI
272 jref NPP_GetJavaClass( void )
273 {
274     return NULL;
275 }
276 #endif
277
278 void NPP_Shutdown( void )
279 {
280     ;
281 }
282
283 NPError NPP_New( NPMIMEType pluginType, NPP instance,
284 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
285                  uint16 mode, int16 argc,
286 #else
287                  uint16_t mode, int16_t argc,
288 #endif
289                  char* argn[], char* argv[], NPSavedData* saved )
290 {
291     NPError status;
292
293     if( instance == NULL )
294     {
295         return NPERR_INVALID_INSTANCE_ERROR;
296     }
297
298     VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
299     if( NULL == p_plugin )
300     {
301         return NPERR_OUT_OF_MEMORY_ERROR;
302     }
303
304     status = p_plugin->init(argc, argn, argv);
305     if( NPERR_NO_ERROR == status )
306     {
307         instance->pdata = reinterpret_cast<void*>(p_plugin);
308 #if 0
309         NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
310         NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
311 #endif
312     }
313     else
314     {
315         delete p_plugin;
316     }
317     return status;
318 }
319
320 NPError NPP_Destroy( NPP instance, NPSavedData** save )
321 {
322     if( NULL == instance )
323         return NPERR_INVALID_INSTANCE_ERROR;
324
325     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
326     if( NULL == p_plugin )
327         return NPERR_NO_ERROR;
328
329     instance->pdata = NULL;
330
331 #if defined(XP_WIN)
332     HWND win = (HWND)p_plugin->getWindow().window;
333     WNDPROC winproc = p_plugin->getWindowProc();
334     if( winproc )
335     {
336         /* reset WNDPROC */
337         SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
338     }
339 #endif
340
341     if( p_plugin->playlist_isplaying() )
342         p_plugin->playlist_stop();
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)
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)
370     control = p_plugin->getControlWindow();
371 #endif
372
373     libvlc_instance_t *p_vlc = p_plugin->getVLC();
374
375     /*
376      * PLUGIN DEVELOPERS:
377      *  Before setting window to point to the
378      *  new window, you may wish to compare the new window
379      *  info to the previous window (if any) to note window
380      *  size changes, etc.
381      */
382
383     /* retrieve current window */
384     NPWindow& curwin = p_plugin->getWindow();
385
386 #ifdef XP_MACOSX
387     if( window && window->window )
388     {
389         /* check if plugin has a new parent window */
390         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
391
392         /* as MacOS X video output is windowless, set viewport */
393         libvlc_rectangle_t view, clip;
394
395         /*
396         ** browser sets port origin to top-left location of plugin
397         ** relative to GrafPort window origin is set relative to document,
398         ** which of little use for drawing
399         */
400         view.top     = ((NP_Port*) (window->window))->porty;
401         view.left    = ((NP_Port*) (window->window))->portx;
402         view.bottom  = window->height+view.top;
403         view.right   = window->width+view.left;
404
405         /* clipRect coordinates are also relative to GrafPort */
406         clip.top     = window->clipRect.top;
407         clip.left    = window->clipRect.left;
408         clip.bottom  = window->clipRect.bottom;
409         clip.right   = window->clipRect.right;
410 #ifdef NOT_WORKING
411         libvlc_video_set_viewport(p_vlc, p_plugin->getMD(), &view, &clip);
412 #else
413 #warning disabled code
414 #endif
415         /* remember new window */
416         p_plugin->setWindow(*window);
417     }
418     else if( curwin.window )
419     {
420         /* change/set parent */
421         curwin.window = NULL;
422     }
423 #endif /* XP_MACOSX */
424
425 #ifdef XP_WIN
426     if( window && window->window )
427     {
428         /* check if plugin has a new parent window */
429         HWND drawable = (HWND) (window->window);
430         if( !curwin.window || drawable != curwin.window )
431         {
432             /* reset previous window settings */
433             HWND oldwin = (HWND)p_plugin->getWindow().window;
434             WNDPROC oldproc = p_plugin->getWindowProc();
435             if( oldproc )
436             {
437                 /* reset WNDPROC */
438                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
439             }
440             /* attach our plugin object */
441             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
442                              reinterpret_cast<LONG_PTR>(p_plugin));
443
444             /* install our WNDPROC */
445             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
446                                              GWL_WNDPROC, (LONG)Manage ) );
447
448             /* change window style to our liking */
449             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
450             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
451             SetWindowLong((HWND)drawable, GWL_STYLE, style);
452
453             /* remember new window */
454             p_plugin->setWindow(*window);
455
456             /* Redraw window */
457             InvalidateRect( (HWND)drawable, NULL, TRUE );
458             UpdateWindow( (HWND)drawable );
459         }
460     }
461     else if( curwin.window )
462     {
463         /* reset WNDPROC */
464         HWND oldwin = (HWND)curwin.window;
465         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
466         p_plugin->setWindowProc(NULL);
467
468         curwin.window = NULL;
469     }
470 #endif /* XP_WIN */
471
472 #if defined(XP_UNIX)
473     /* default to hidden toolbar, shown at the end of this method if asked *
474      * developers note : getToolbarSize need to wait the end of this method
475      */
476     i_control_height = 0;
477     i_control_width = window->width;
478
479     if( window && window->window )
480     {
481         Window  parent  = (Window) window->window;
482         if( !curwin.window || (parent != (Window)curwin.window) )
483         {
484             Display *p_display = ( (NPSetWindowCallbackStruct *)
485                                    window->ws_info )->display;
486
487             XResizeWindow( p_display, parent, window->width, window->height );
488
489             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
490
491             /* create windows */
492             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
493                            window->width, window->height - i_control_height,
494                            0, i_blackColor, i_blackColor );
495             Window controls = (Window) NULL;
496             controls = XCreateSimpleWindow( p_display, parent,
497                             0, window->height - i_control_height-1,
498                             window->width, i_control_height-1,
499                             0, i_blackColor, i_blackColor );
500
501             XMapWindow( p_display, parent );
502             XMapWindow( p_display, video );
503             if( controls ) { XMapWindow( p_display, controls ); }
504
505             XFlush(p_display);
506
507             /* bind events */
508             Widget w = XtWindowToWidget( p_display, parent );
509
510             XtAddEventHandler( w, ExposureMask, FALSE,
511                                (XtEventHandler)Redraw, p_plugin );
512             XtAddEventHandler( w, StructureNotifyMask, FALSE,
513                                (XtEventHandler)Resize, p_plugin );
514             XtAddEventHandler( w, ButtonReleaseMask, FALSE,
515                                (XtEventHandler)ControlHandler, p_plugin );
516
517             /* remember window */
518             p_plugin->setWindow( *window );
519             p_plugin->setVideoWindow( video );
520
521             if( controls )
522             {
523                 p_plugin->setControlWindow( controls );
524             }
525
526             Redraw( w, (XtPointer)p_plugin, NULL );
527
528             /* now display toolbar if asked through parameters */
529             if( p_plugin->b_toolbar )
530             {
531                 p_plugin->showToolbar();
532             }
533         }
534     }
535     else if( curwin.window )
536     {
537         curwin.window = NULL;
538     }
539 #endif /* XP_UNIX */
540
541     if( !p_plugin->b_stream )
542     {
543         if( p_plugin->psz_target )
544         {
545             if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
546             {
547                 if( p_plugin->b_autoplay )
548                 {
549                     p_plugin->playlist_play();
550                 }
551             }
552             p_plugin->b_stream = true;
553         }
554     }
555     return NPERR_NO_ERROR;
556 }
557
558 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
559 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
560                        NPBool seekable, uint16 *stype )
561 #else
562                        NPBool seekable, uint16_t *stype )
563 #endif
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 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
593 int32 NPP_WriteReady( NPP instance, NPStream *stream )
594 #else
595 int32_t NPP_WriteReady( NPP instance, NPStream *stream )
596 #endif
597 {
598     /* TODO */
599     return 8*1024;
600 }
601
602 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
603 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
604                  int32 len, void *buffer )
605 #else
606 int32_t NPP_Write( NPP instance, NPStream *stream, int32_t offset,
607                  int32_t len, void *buffer )
608 #endif
609 {
610     /* TODO */
611     return len;
612 }
613
614 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
615 {
616     if( instance == NULL )
617     {
618         return NPERR_INVALID_INSTANCE_ERROR;
619     }
620     return NPERR_NO_ERROR;
621 }
622
623 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
624 {
625     if( instance == NULL )
626     {
627         return;
628     }
629
630     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
631     if( NULL == p_plugin )
632     {
633         return;
634     }
635
636     if( p_plugin->playlist_add( stream->url ) != -1 )
637     {
638         if( p_plugin->b_autoplay )
639         {
640             p_plugin->playlist_play();
641         }
642     }
643 }
644
645 void NPP_URLNotify( NPP instance, const char* url,
646                     NPReason reason, void* notifyData )
647 {
648     /***** Insert NPP_URLNotify code here *****\
649     PluginInstance* p_plugin;
650     if (instance != NULL)
651         p_plugin = (PluginInstance*) instance->pdata;
652     \*********************************************/
653 }
654
655 void NPP_Print( NPP instance, NPPrint* printInfo )
656 {
657     if( printInfo == NULL )
658     {
659         return;
660     }
661
662     if( instance != NULL )
663     {
664         /***** Insert NPP_Print code here *****\
665         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
666         \**************************************/
667
668         if( printInfo->mode == NP_FULL )
669         {
670             /*
671              * PLUGIN DEVELOPERS:
672              *  If your plugin would like to take over
673              *  printing completely when it is in full-screen mode,
674              *  set printInfo->pluginPrinted to TRUE and print your
675              *  plugin as you see fit.  If your plugin wants Netscape
676              *  to handle printing in this case, set
677              *  printInfo->pluginPrinted to FALSE (the default) and
678              *  do nothing.  If you do want to handle printing
679              *  yourself, printOne is true if the print button
680              *  (as opposed to the print menu) was clicked.
681              *  On the Macintosh, platformPrint is a THPrint; on
682              *  Windows, platformPrint is a structure
683              *  (defined in npapi.h) containing the printer name, port,
684              *  etc.
685              */
686
687             /***** Insert NPP_Print code here *****\
688             void* platformPrint =
689                 printInfo->print.fullPrint.platformPrint;
690             NPBool printOne =
691                 printInfo->print.fullPrint.printOne;
692             \**************************************/
693
694             /* Do the default*/
695             printInfo->print.fullPrint.pluginPrinted = FALSE;
696         }
697         else
698         {
699             /* If not fullscreen, we must be embedded */
700             /*
701              * PLUGIN DEVELOPERS:
702              *  If your plugin is embedded, or is full-screen
703              *  but you returned false in pluginPrinted above, NPP_Print
704              *  will be called with mode == NP_EMBED.  The NPWindow
705              *  in the printInfo gives the location and dimensions of
706              *  the embedded plugin on the printed page.  On the
707              *  Macintosh, platformPrint is the printer port; on
708              *  Windows, platformPrint is the handle to the printing
709              *  device context.
710              */
711
712             /***** Insert NPP_Print code here *****\
713             NPWindow* printWindow =
714                 &(printInfo->print.embedPrint.window);
715             void* platformPrint =
716                 printInfo->print.embedPrint.platformPrint;
717             \**************************************/
718         }
719     }
720 }
721
722 /******************************************************************************
723  * Windows-only methods
724  *****************************************************************************/
725 #if defined(XP_WIN)
726 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
727 {
728     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
729
730     switch( i_msg )
731     {
732         case WM_ERASEBKGND:
733             return 1L;
734
735         case WM_PAINT:
736         {
737             PAINTSTRUCT paintstruct;
738             HDC hdc;
739             RECT rect;
740
741             hdc = BeginPaint( p_hwnd, &paintstruct );
742
743             GetClientRect( p_hwnd, &rect );
744
745             FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
746             SetTextColor(hdc, RGB(255, 255, 255));
747             SetBkColor(hdc, RGB(0, 0, 0));
748             if( p_plugin->psz_text )
749                 DrawText( hdc, p_plugin->psz_text, strlen(p_plugin->psz_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 #if defined(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     if( p_plugin->psz_text )
794         XDrawString( p_display, video, gc,
795                      window.width / 2 - 40, (window.height - i_control_height) / 2,
796                      p_plugin->psz_text, strlen(p_plugin->psz_text) );
797     XFreeGC( p_display, gc );
798
799     p_plugin->redrawToolbar();
800 }
801
802 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
803 {
804     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
805     const NPWindow& window = p_plugin->getWindow();
806
807     int i_height = window.height;
808     int i_width = window.width;
809     int i_xPos = event->xbutton.x;
810     int i_yPos = event->xbutton.y;
811
812     if( p_plugin && p_plugin->b_toolbar )
813     {
814         int i_playing;
815
816         libvlc_media_player_t *p_md = p_plugin->getMD();
817
818         i_playing = p_plugin->playlist_isplaying();
819
820         vlc_toolbar_clicked_t clicked;
821         clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
822         switch( clicked )
823         {
824             case clicked_Play:
825             case clicked_Pause:
826             {
827                 if( i_playing == 1 )
828                     p_plugin->playlist_pause();
829                 else
830                     p_plugin->playlist_play();
831             }
832             break;
833
834             case clicked_Stop:
835             {
836                 p_plugin->playlist_stop();
837             }
838             break;
839
840             case clicked_Fullscreen:
841             {
842                 p_plugin->set_fullscreen( 1 );
843             }
844             break;
845
846             case clicked_Mute:
847             case clicked_Unmute:
848             {
849                 if( p_md )
850                     libvlc_audio_toggle_mute( p_md );
851             }
852             break;
853
854             case clicked_timeline:
855             {
856                 /* if a movie is loaded */
857                 if( p_md )
858                 {
859                     int64_t f_length;
860                     f_length = libvlc_media_player_get_length( p_md ) / 100;
861
862                     f_length = (float)f_length *
863                             ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
864
865                     libvlc_media_player_set_time( p_md, f_length );
866                 }
867             }
868             break;
869
870             case clicked_Time:
871             {
872                 /* Not implemented yet*/
873             }
874             break;
875
876             default: /* button_Unknown */
877             break;
878         }
879     }
880     Redraw( w, closure, event );
881 }
882
883 static void Resize ( Widget w, XtPointer closure, XEvent *event )
884 {
885     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
886     Window control = p_plugin->getControlWindow();
887     const NPWindow& window = p_plugin->getWindow();
888     Window  drawable   = p_plugin->getVideoWindow();
889     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
890
891     int i_ret;
892     Window root_return, parent_return, * children_return;
893     Window base_window;
894     unsigned int i_nchildren;
895     unsigned int i_control_height, i_control_width;
896
897     if( p_plugin->b_toolbar )
898     {
899         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
900     }
901     else
902     {
903         i_control_height = i_control_width = 0;
904     }
905
906 #ifdef X11_RESIZE_DEBUG
907     XWindowAttributes attr;
908
909     if( event && event->type == ConfigureNotify )
910     {
911         fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
912                  "send_event ? %s\n", event->xconfigure.width,
913                  event->xconfigure.height,
914                  event->xconfigure.send_event ? "TRUE" : "FALSE" );
915     }
916 #endif /* X11_RESIZE_DEBUG */
917
918     if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
919     {
920         /* size already set */
921         return;
922     }
923
924     i_ret = XResizeWindow( p_display, drawable,
925                            window.width, (window.height - i_control_height) );
926
927 #ifdef X11_RESIZE_DEBUG
928     fprintf( stderr,
929              "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
930
931     XGetWindowAttributes ( p_display, drawable, &attr );
932
933     /* X is asynchronous, so the current size reported here is not
934        necessarily the requested size as the Resize request may not
935        yet have been handled by the plugin host */
936     fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
937              attr.width, attr.height );
938 #endif /* X11_RESIZE_DEBUG */
939
940     XQueryTree( p_display, drawable,
941                 &root_return, &parent_return, &children_return,
942                 &i_nchildren );
943
944     if( i_nchildren > 0 )
945     {
946         /* XXX: Make assumptions related to the window parenting structure in
947            vlc/modules/video_output/x11/xcommon.c */
948         base_window = children_return[i_nchildren - 1];
949
950 #ifdef X11_RESIZE_DEBUG
951         fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
952         fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
953                  base_window );
954 #endif /* X11_RESIZE_DEBUG */
955
956         i_ret = XResizeWindow( p_display, base_window,
957                 window.width, ( window.height - i_control_height ) );
958
959 #ifdef X11_RESIZE_DEBUG
960         fprintf( stderr,
961                  "vlcshell::Resize() XResizeWindow(base) returned %d\n",
962                  i_ret );
963
964         XGetWindowAttributes( p_display, base_window, &attr );
965
966         fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
967                  attr.width, attr.height );
968 #endif /* X11_RESIZE_DEBUG */
969     }
970 }
971
972 #endif /* XP_UNIX */