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