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