]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcshell.cpp
mozilla: cosmetics
[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
46 /* Enable/disable debugging printf's for X11 resizing */
47 #undef X11_RESIZE_DEBUG
48
49 #define WINDOW_TEXT "Waiting for video"
50
51 /*****************************************************************************
52  * Unix-only declarations
53 ******************************************************************************/
54 #ifdef XP_UNIX
55
56 static void Redraw( Widget w, XtPointer closure, XEvent *event );
57 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
58 static void Resize( Widget w, XtPointer closure, XEvent *event );
59
60 #endif
61
62 /*****************************************************************************
63  * MacOS-only declarations
64 ******************************************************************************/
65 #ifdef XP_MACOSX
66 #endif
67
68 /*****************************************************************************
69  * Windows-only declarations
70  *****************************************************************************/
71 #ifdef XP_WIN
72
73 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
74
75 #endif
76
77 /******************************************************************************
78  * UNIX-only API calls
79  *****************************************************************************/
80 char * NPP_GetMIMEDescription( void )
81 {
82     return PLUGIN_MIMETYPES;
83 }
84
85 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
86 {
87
88     static char psz_desc[1000];
89
90     /* plugin class variables */
91     switch( variable )
92     {
93         case NPPVpluginNameString:
94             *((char **)value) = PLUGIN_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 = (VlcPlugin*)instance->pdata;
169
170     if( p_plugin == NULL )
171     {
172         return false;
173     }
174
175     EventRecord *myEvent = (EventRecord*)event;
176
177     switch( myEvent->what )
178     {
179         case nullEvent:
180             return true;
181         case mouseDown:
182         {
183             if( (myEvent->when - lastMouseUp) < GetDblTime() )
184             {
185                 /* double click */
186                 p_plugin->toggle_fullscreen(&ex);
187                 libvlc_exception_clear(&ex);
188             }
189             return true;
190         }
191         case mouseUp:
192             lastMouseUp = myEvent->when;
193             return true;
194         case keyUp:
195         case keyDown:
196         case autoKey:
197             return true;
198         case updateEvt:
199         {
200             const NPWindow& npwindow = p_plugin->getWindow();
201             if( npwindow.window )
202             {
203                 int hasVout = FALSE;
204
205                 if( p_plugin->playlist_isplaying(&ex) )
206                 {
207                     hasVout = p_plugin->player_has_vout(NULL);
208                     if( hasVout )
209                     {
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_md, &area, NULL);
216                     }
217                 }
218                 libvlc_exception_clear(&ex);
219
220                 if( ! hasVout )
221                 {
222                     /* draw the beautiful "No Picture" */
223
224                     ForeColor(blackColor);
225                     PenMode( patCopy );
226
227                     /* seems that firefox forgets to set the following
228                      * on occasion (reload) */
229                     SetOrigin(((NP_Port *)npwindow.window)->portx,
230                               ((NP_Port *)npwindow.window)->porty);
231
232                     Rect rect;
233                     rect.left = 0;
234                     rect.top = 0;
235                     rect.right = npwindow.width;
236                     rect.bottom = npwindow.height;
237                     PaintRect( &rect );
238
239                     ForeColor(whiteColor);
240                     MoveTo( (npwindow.width-80)/ 2  , npwindow.height / 2 );
241                     DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
242                 }
243             }
244             return true;
245         }
246         case activateEvt:
247             return false;
248         case NPEventType_GetFocusEvent:
249         case NPEventType_LoseFocusEvent:
250             return true;
251         case NPEventType_AdjustCursorEvent:
252             return false;
253         case NPEventType_MenuCommandEvent:
254             return false;
255         case NPEventType_ClippingChangedEvent:
256             return false;
257         case NPEventType_ScrollingBeginsEvent:
258             return true;
259         case NPEventType_ScrollingEndsEvent:
260             return true;
261         default:
262             ;
263     }
264     return false;
265 }
266 #endif /* XP_MACOSX */
267
268 /******************************************************************************
269  * General Plug-in Calls
270  *****************************************************************************/
271 NPError NPP_Initialize( void )
272 {
273     return NPERR_NO_ERROR;
274 }
275
276 jref NPP_GetJavaClass( void )
277 {
278     return NULL;
279 }
280
281 void NPP_Shutdown( void )
282 {
283     ;
284 }
285
286 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
287                  char* argn[], char* argv[], NPSavedData* saved )
288 {
289     NPError status;
290
291     if( instance == NULL )
292     {
293         return NPERR_INVALID_INSTANCE_ERROR;
294     }
295
296     VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
297     if( NULL == p_plugin )
298     {
299         return NPERR_OUT_OF_MEMORY_ERROR;
300     }
301
302     status = p_plugin->init(argc, argn, argv);
303     if( NPERR_NO_ERROR == status )
304     {
305         instance->pdata = reinterpret_cast<void*>(p_plugin);
306 #if 0
307         NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
308         NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
309 #endif
310     }
311     else
312     {
313         delete p_plugin;
314     }
315     return status;
316 }
317
318 NPError NPP_Destroy( NPP instance, NPSavedData** save )
319 {
320     if( NULL == instance )
321         return NPERR_INVALID_INSTANCE_ERROR;
322
323     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
324     if( NULL == p_plugin )
325         return NPERR_NO_ERROR;
326
327     instance->pdata = NULL;
328
329 #if XP_WIN
330     HWND win = (HWND)p_plugin->getWindow().window;
331     WNDPROC winproc = p_plugin->getWindowProc();
332     if( winproc )
333     {
334         /* reset WNDPROC */
335         SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
336     }
337 #endif
338
339     delete p_plugin;
340
341     return NPERR_NO_ERROR;
342 }
343
344 NPError NPP_SetWindow( NPP instance, NPWindow* window )
345 {
346 #if defined(XP_UNIX) && !defined(__APPLE__)
347     Window control;
348     unsigned int i_control_height = 0, i_control_width = 0;
349 #endif
350
351     if( ! instance )
352     {
353         return NPERR_INVALID_INSTANCE_ERROR;
354     }
355
356     /* NPP_SetWindow may be called before NPP_New (Opera) */
357     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
358     if( NULL == p_plugin )
359     {
360         /* we should probably show a splash screen here */
361         return NPERR_NO_ERROR;
362     }
363 #if defined(XP_UNIX) && !defined(__APPLE__)
364     control = p_plugin->getControlWindow();
365 #endif
366     libvlc_instance_t *p_vlc = p_plugin->getVLC();
367
368     /*
369      * PLUGIN DEVELOPERS:
370      *  Before setting window to point to the
371      *  new window, you may wish to compare the new window
372      *  info to the previous window (if any) to note window
373      *  size changes, etc.
374      */
375
376     /* retrieve current window */
377     NPWindow& curwin = p_plugin->getWindow();
378
379 #ifdef XP_MACOSX
380     if( window && window->window )
381     {
382         /* check if plugin has a new parent window */
383         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
384         if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
385         {
386             /* set/change parent window */
387             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
388         }
389
390         /* as MacOS X video output is windowless, set viewport */
391         libvlc_rectangle_t view, clip;
392
393         /*
394         ** browser sets port origin to top-left location of plugin
395         ** relative to GrafPort window origin is set relative to document,
396         ** which of little use for drawing
397         */
398         view.top     = ((NP_Port*) (window->window))->porty;
399         view.left    = ((NP_Port*) (window->window))->portx;
400         view.bottom  = window->height+view.top;
401         view.right   = window->width+view.left;
402         /* clipRect coordinates are also relative to GrafPort */
403         clip.top     = window->clipRect.top;
404         clip.left    = window->clipRect.left;
405         clip.bottom  = window->clipRect.bottom;
406         clip.right   = window->clipRect.right;
407
408         libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
409
410         /* remember new window */
411         p_plugin->setWindow(*window);
412     }
413     else if( curwin.window )
414     {
415         /* change/set parent */
416         libvlc_video_set_parent(p_vlc, 0, NULL);
417         curwin.window = NULL;
418     }
419 #endif /* XP_MACOSX */
420
421 #ifdef XP_WIN
422     if( window && window->window )
423     {
424         /* check if plugin has a new parent window */
425         HWND drawable = (HWND) (window->window);
426         if( !curwin.window || drawable != curwin.window )
427         {
428             /* reset previous window settings */
429             HWND oldwin = (HWND)p_plugin->getWindow().window;
430             WNDPROC oldproc = p_plugin->getWindowProc();
431             if( oldproc )
432             {
433                 /* reset WNDPROC */
434                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
435             }
436             /* attach our plugin object */
437             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
438                              reinterpret_cast<LONG_PTR>(p_plugin));
439
440             /* install our WNDPROC */
441             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
442                                              GWL_WNDPROC, (LONG)Manage ) );
443
444             /* change window style to our liking */
445             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
446             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
447             SetWindowLong((HWND)drawable, GWL_STYLE, style);
448
449             /* change/set parent */
450             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
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         /* change/set parent */
467         libvlc_video_set_parent(p_vlc, 0, NULL);
468         curwin.window = NULL;
469     }
470 #endif /* XP_WIN */
471
472 #ifdef 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             /* callback */
518 /*
519             libvlc_media_player_t *p_md;
520
521             libvlc_exception_t ex;
522             libvlc_exception_init(& ex );
523             p_md = p_plugin->getMD( &ex );
524             libvlc_exception_clear( &ex );
525             libvlc_event_attach( libvlc_media_player_event_manager( p_md, &ex ),
526                                  libvlc_MediaPlayerPositionChanged, Redraw, NULL, &ex );
527 */
528
529             /* set/change parent window */
530             libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, NULL );
531
532             /* remember window */
533             p_plugin->setWindow( *window );
534             p_plugin->setVideoWindow( video );
535             if( controls ) { p_plugin->setControlWindow( controls ); }
536
537             Redraw( w, (XtPointer)p_plugin, NULL );
538
539             /* now display toolbar if asked through parameters */
540             if( p_plugin->b_toolbar )
541             {
542                 p_plugin->showToolbar();
543             }
544         }
545     }
546     else if ( curwin.window )
547     {
548         /* change/set parent */
549         libvlc_video_set_parent(p_vlc, 0, NULL);
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
608 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
609                  int32 len, void *buffer )
610 {
611     /* TODO */
612     return len;
613 }
614
615
616 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
617 {
618     if( instance == NULL )
619     {
620         return NPERR_INVALID_INSTANCE_ERROR;
621     }
622     return NPERR_NO_ERROR;
623 }
624
625
626 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
627 {
628     if( instance == NULL )
629     {
630         return;
631     }
632
633     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
634     if( NULL == p_plugin )
635     {
636         return;
637     }
638
639     if( p_plugin->playlist_add( stream->url, NULL ) != -1 )
640     {
641         if( p_plugin->b_autoplay )
642         {
643             p_plugin->playlist_play(NULL);
644         }
645     }
646 }
647
648
649 void NPP_URLNotify( NPP instance, const char* url,
650                     NPReason reason, void* notifyData )
651 {
652     /***** Insert NPP_URLNotify code here *****\
653     PluginInstance* p_plugin;
654     if (instance != NULL)
655         p_plugin = (PluginInstance*) instance->pdata;
656     \*********************************************/
657 }
658
659 void NPP_Print( NPP instance, NPPrint* printInfo )
660 {
661     if( printInfo == NULL )
662     {
663         return;
664     }
665
666     if( instance != NULL )
667     {
668         /***** Insert NPP_Print code here *****\
669         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
670         \**************************************/
671
672         if( printInfo->mode == NP_FULL )
673         {
674             /*
675              * PLUGIN DEVELOPERS:
676              *  If your plugin would like to take over
677              *  printing completely when it is in full-screen mode,
678              *  set printInfo->pluginPrinted to TRUE and print your
679              *  plugin as you see fit.  If your plugin wants Netscape
680              *  to handle printing in this case, set
681              *  printInfo->pluginPrinted to FALSE (the default) and
682              *  do nothing.  If you do want to handle printing
683              *  yourself, printOne is true if the print button
684              *  (as opposed to the print menu) was clicked.
685              *  On the Macintosh, platformPrint is a THPrint; on
686              *  Windows, platformPrint is a structure
687              *  (defined in npapi.h) containing the printer name, port,
688              *  etc.
689              */
690
691             /***** Insert NPP_Print code here *****\
692             void* platformPrint =
693                 printInfo->print.fullPrint.platformPrint;
694             NPBool printOne =
695                 printInfo->print.fullPrint.printOne;
696             \**************************************/
697
698             /* Do the default*/
699             printInfo->print.fullPrint.pluginPrinted = FALSE;
700         }
701         else
702         {
703             /* If not fullscreen, we must be embedded */
704             /*
705              * PLUGIN DEVELOPERS:
706              *  If your plugin is embedded, or is full-screen
707              *  but you returned false in pluginPrinted above, NPP_Print
708              *  will be called with mode == NP_EMBED.  The NPWindow
709              *  in the printInfo gives the location and dimensions of
710              *  the embedded plugin on the printed page.  On the
711              *  Macintosh, platformPrint is the printer port; on
712              *  Windows, platformPrint is the handle to the printing
713              *  device context.
714              */
715
716             /***** Insert NPP_Print code here *****\
717             NPWindow* printWindow =
718                 &(printInfo->print.embedPrint.window);
719             void* platformPrint =
720                 printInfo->print.embedPrint.platformPrint;
721             \**************************************/
722         }
723     }
724 }
725
726 /******************************************************************************
727  * Windows-only methods
728  *****************************************************************************/
729 #if XP_WIN
730 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
731 {
732     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
733
734     switch( i_msg )
735     {
736         case WM_ERASEBKGND:
737             return 1L;
738
739         case WM_PAINT:
740         {
741             PAINTSTRUCT paintstruct;
742             HDC hdc;
743             RECT rect;
744
745             hdc = BeginPaint( p_hwnd, &paintstruct );
746
747             GetClientRect( p_hwnd, &rect );
748
749             FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
750             SetTextColor(hdc, RGB(255, 255, 255));
751             SetBkColor(hdc, RGB(0, 0, 0));
752             DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect,
753                       DT_CENTER|DT_VCENTER|DT_SINGLELINE);
754
755             EndPaint( p_hwnd, &paintstruct );
756             return 0L;
757         }
758         default:
759             /* delegate to default handler */
760             return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
761                                    i_msg, wpar, lpar );
762     }
763 }
764 #endif /* XP_WIN */
765
766 /******************************************************************************
767  * UNIX-only methods
768  *****************************************************************************/
769 #ifdef XP_UNIX
770 static void Redraw( Widget w, XtPointer closure, XEvent *event )
771 {
772     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
773     Window control = p_plugin->getControlWindow();
774     const NPWindow& window = p_plugin->getWindow();
775     GC gc;
776     XGCValues gcv;
777     unsigned int i_control_height, i_control_width;
778
779     if( p_plugin->b_toolbar )
780         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
781     else
782         i_control_height = i_control_width = 0;
783
784     Window video = p_plugin->getVideoWindow();
785     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
786
787     gcv.foreground = BlackPixel( p_display, 0 );
788     gc = XCreateGC( p_display, video, GCForeground, &gcv );
789
790     XFillRectangle( p_display, video, gc,
791                     0, 0, window.width, window.height - i_control_height);
792
793     gcv.foreground = WhitePixel( p_display, 0 );
794     XChangeGC( p_display, gc, GCForeground, &gcv );
795
796     XDrawString( p_display, video, gc,
797                  window.width / 2 - 40, (window.height - i_control_height) / 2,
798                  WINDOW_TEXT, strlen(WINDOW_TEXT) );
799     XFreeGC( p_display, gc );
800
801     p_plugin->redrawToolbar();
802 }
803
804 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
805 {
806     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
807     const NPWindow& window = p_plugin->getWindow();
808
809     int i_height = window.height;
810     int i_width = window.width;
811     int i_xPos = event->xbutton.x;
812     int i_yPos = event->xbutton.y;
813
814     if( p_plugin && p_plugin->b_toolbar )
815     {
816         int i_playing;
817         libvlc_exception_t ex;
818
819         libvlc_exception_init( &ex );
820         libvlc_media_player_t *p_md = p_plugin->getMD(&ex);
821         if( libvlc_exception_raised(&ex) )
822             fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
823         libvlc_exception_clear( &ex );
824
825         i_playing = p_plugin->playlist_isplaying( &ex );
826         if( libvlc_exception_raised(&ex) )
827             fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
828         libvlc_exception_clear( &ex );
829
830         vlc_toolbar_clicked_t clicked;
831         clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
832         switch( clicked )
833         {
834             case clicked_Play:
835             case clicked_Pause:
836             {
837                 if( i_playing == 1 )
838                     p_plugin->playlist_pause( &ex );
839                 else
840                     p_plugin->playlist_play( &ex );
841
842                 if( libvlc_exception_raised(&ex) )
843                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
844                 libvlc_exception_clear( &ex );
845             }
846             break;
847
848             case clicked_Stop:
849             {
850                 p_plugin->playlist_stop(&ex);
851                 if( libvlc_exception_raised(&ex) )
852                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
853                 libvlc_exception_clear( &ex );
854             }
855             break;
856
857             case clicked_Fullscreen:
858             {
859                 p_plugin->set_fullscreen( 1, &ex );
860                 if( libvlc_exception_raised(&ex) )
861                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
862                 libvlc_exception_clear( &ex );
863             }
864             break;
865
866             case clicked_Mute:
867             case clicked_Unmute:
868             {
869                 libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
870                 if( libvlc_exception_raised(&ex) )
871                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
872                 libvlc_exception_clear( &ex );
873             }
874             break;
875
876             case clicked_timeline:
877             {
878                 /* if a movie is loaded */
879                 if( p_md )
880                 {
881                     int64_t f_length;
882                     f_length = libvlc_media_player_get_length( p_md, &ex ) / 100;
883                     libvlc_exception_clear( &ex );
884
885                     f_length = (float)f_length *
886                             ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
887
888                     libvlc_media_player_set_time( p_md, f_length, &ex );
889                     if( libvlc_exception_raised(&ex) )
890                         fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
891                     libvlc_exception_clear( &ex );
892                 }
893             }
894             break;
895
896             case clicked_Time:
897             {
898                 /* Not implemented yet*/
899             }
900             break;
901
902             default: /* button_Unknown */
903             break;
904         }
905     }
906     Redraw( w, closure, event );
907 }
908
909 static void Resize ( Widget w, XtPointer closure, XEvent *event )
910 {
911     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
912     Window control = p_plugin->getControlWindow();
913     const NPWindow& window = p_plugin->getWindow();
914     Window  drawable   = p_plugin->getVideoWindow();
915     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
916
917     int i_ret;
918     Window root_return, parent_return, * children_return;
919     Window base_window;
920     unsigned int i_nchildren;
921     unsigned int i_control_height, i_control_width;
922
923     if( p_plugin->b_toolbar )
924     {
925         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
926     }
927     else
928     {
929         i_control_height = i_control_width = 0;
930     }
931
932 #ifdef X11_RESIZE_DEBUG
933     XWindowAttributes attr;
934
935     if( event && event->type == ConfigureNotify )
936     {
937         fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
938                  "send_event ? %s\n", event->xconfigure.width,
939                  event->xconfigure.height,
940                  event->xconfigure.send_event ? "TRUE" : "FALSE" );
941     }
942 #endif /* X11_RESIZE_DEBUG */
943
944     if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
945     {
946         /* size already set */
947         return;
948     }
949
950     i_ret = XResizeWindow( p_display, drawable,
951                            window.width, (window.height - i_control_height) );
952
953 #ifdef X11_RESIZE_DEBUG
954     fprintf( stderr,
955              "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
956
957     XGetWindowAttributes ( p_display, drawable, &attr );
958
959     /* X is asynchronous, so the current size reported here is not
960        necessarily the requested size as the Resize request may not
961        yet have been handled by the plugin host */
962     fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
963              attr.width, attr.height );
964 #endif /* X11_RESIZE_DEBUG */
965
966     XQueryTree( p_display, drawable,
967                 &root_return, &parent_return, &children_return,
968                 &i_nchildren );
969
970     if( i_nchildren > 0 )
971     {
972         /* XXX: Make assumptions related to the window parenting structure in
973            vlc/modules/video_output/x11/xcommon.c */
974         base_window = children_return[i_nchildren - 1];
975
976 #ifdef X11_RESIZE_DEBUG
977         fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
978         fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
979                  base_window );
980 #endif /* X11_RESIZE_DEBUG */
981
982         i_ret = XResizeWindow( p_display, base_window,
983                 window.width, ( window.height - i_control_height ) );
984
985 #ifdef X11_RESIZE_DEBUG
986         fprintf( stderr,
987                  "vlcshell::Resize() XResizeWindow(base) returned %d\n",
988                  i_ret );
989
990         XGetWindowAttributes( p_display, base_window, &attr );
991
992         fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
993                  attr.width, attr.height );
994 #endif /* X11_RESIZE_DEBUG */
995     }
996 }
997
998 #endif /* XP_UNIX */