]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcshell.cpp
mozilla: rewrite and license npunix.c/npwin.cpp under LGPLv2.1 (or later)
[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 = (VlcPlugin*)instance->pdata;
171
172     if( p_plugin == NULL )
173     {
174         return false;
175     }
176
177     EventRecord *myEvent = (EventRecord*)event;
178
179     switch( myEvent->what )
180     {
181         case nullEvent:
182             return true;
183         case mouseDown:
184         {
185             if( (myEvent->when - lastMouseUp) < GetDblTime() )
186             {
187                 /* double click */
188                 p_plugin->toggle_fullscreen(&ex);
189                 libvlc_exception_clear(&ex);
190             }
191             return true;
192         }
193         case mouseUp:
194             lastMouseUp = myEvent->when;
195             return true;
196         case keyUp:
197         case keyDown:
198         case autoKey:
199             return true;
200         case updateEvt:
201         {
202             const NPWindow& npwindow = p_plugin->getWindow();
203             if( npwindow.window )
204             {
205                 int hasVout = FALSE;
206
207                 if( p_plugin->playlist_isplaying(&ex) )
208                 {
209                     hasVout = p_plugin->player_has_vout(NULL);
210                     if( hasVout )
211                     {
212                         libvlc_rectangle_t area;
213                         area.left = 0;
214                         area.top = 0;
215                         area.right = npwindow.width;
216                         area.bottom = npwindow.height;
217                         libvlc_video_redraw_rectangle(p_plugin->getMD(&ex), &area, NULL);
218                     }
219                 }
220                 libvlc_exception_clear(&ex);
221
222                 if( ! hasVout )
223                 {
224                     /* draw the beautiful "No Picture" */
225
226                     ForeColor(blackColor);
227                     PenMode( patCopy );
228
229                     /* seems that firefox forgets to set the following
230                      * on occasion (reload) */
231                     SetOrigin(((NP_Port *)npwindow.window)->portx,
232                               ((NP_Port *)npwindow.window)->porty);
233
234                     Rect rect;
235                     rect.left = 0;
236                     rect.top = 0;
237                     rect.right = npwindow.width;
238                     rect.bottom = npwindow.height;
239                     PaintRect( &rect );
240
241                     ForeColor(whiteColor);
242                     MoveTo( (npwindow.width-80)/ 2  , npwindow.height / 2 );
243                     DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
244                 }
245             }
246             return true;
247         }
248         case activateEvt:
249             return false;
250         case NPEventType_GetFocusEvent:
251         case NPEventType_LoseFocusEvent:
252             return true;
253         case NPEventType_AdjustCursorEvent:
254             return false;
255         case NPEventType_MenuCommandEvent:
256             return false;
257         case NPEventType_ClippingChangedEvent:
258             return false;
259         case NPEventType_ScrollingBeginsEvent:
260             return true;
261         case NPEventType_ScrollingEndsEvent:
262             return true;
263         default:
264             ;
265     }
266     return false;
267 }
268 #endif /* XP_MACOSX */
269
270 /******************************************************************************
271  * General Plug-in Calls
272  *****************************************************************************/
273 NPError NPP_Initialize( void )
274 {
275     return NPERR_NO_ERROR;
276 }
277
278 jref NPP_GetJavaClass( void )
279 {
280     return NULL;
281 }
282
283 void NPP_Shutdown( void )
284 {
285     ;
286 }
287
288 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
289                  char* argn[], char* argv[], NPSavedData* saved )
290 {
291     NPError status;
292
293     if( instance == NULL )
294     {
295         return NPERR_INVALID_INSTANCE_ERROR;
296     }
297
298     VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
299     if( NULL == p_plugin )
300     {
301         return NPERR_OUT_OF_MEMORY_ERROR;
302     }
303
304     status = p_plugin->init(argc, argn, argv);
305     if( NPERR_NO_ERROR == status )
306     {
307         instance->pdata = reinterpret_cast<void*>(p_plugin);
308 #if 0
309         NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
310         NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
311 #endif
312     }
313     else
314     {
315         delete p_plugin;
316     }
317     return status;
318 }
319
320 NPError NPP_Destroy( NPP instance, NPSavedData** save )
321 {
322     if( NULL == instance )
323         return NPERR_INVALID_INSTANCE_ERROR;
324
325     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
326     if( NULL == p_plugin )
327         return NPERR_NO_ERROR;
328
329     instance->pdata = NULL;
330
331 #if XP_WIN
332     HWND win = (HWND)p_plugin->getWindow().window;
333     WNDPROC winproc = p_plugin->getWindowProc();
334     if( winproc )
335     {
336         /* reset WNDPROC */
337         SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
338     }
339 #endif
340
341     delete p_plugin;
342
343     return NPERR_NO_ERROR;
344 }
345
346 NPError NPP_SetWindow( NPP instance, NPWindow* window )
347 {
348 #if defined(XP_UNIX) && !defined(__APPLE__)
349     Window control;
350     unsigned int i_control_height = 0, i_control_width = 0;
351 #endif
352
353     if( ! instance )
354     {
355         return NPERR_INVALID_INSTANCE_ERROR;
356     }
357
358     /* NPP_SetWindow may be called before NPP_New (Opera) */
359     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
360     if( NULL == p_plugin )
361     {
362         /* we should probably show a splash screen here */
363         return NPERR_NO_ERROR;
364     }
365 #if defined(XP_UNIX) && !defined(__APPLE__)
366     control = p_plugin->getControlWindow();
367 #endif
368     libvlc_instance_t *p_vlc = p_plugin->getVLC();
369
370     /*
371      * PLUGIN DEVELOPERS:
372      *  Before setting window to point to the
373      *  new window, you may wish to compare the new window
374      *  info to the previous window (if any) to note window
375      *  size changes, etc.
376      */
377
378     /* retrieve current window */
379     NPWindow& curwin = p_plugin->getWindow();
380
381 #ifdef XP_MACOSX
382     if( window && window->window )
383     {
384         /* check if plugin has a new parent window */
385         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
386         if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
387         {
388             /* set/change parent window */
389             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
390         }
391
392         /* as MacOS X video output is windowless, set viewport */
393         libvlc_rectangle_t view, clip;
394
395         /*
396         ** browser sets port origin to top-left location of plugin
397         ** relative to GrafPort window origin is set relative to document,
398         ** which of little use for drawing
399         */
400         view.top     = ((NP_Port*) (window->window))->porty;
401         view.left    = ((NP_Port*) (window->window))->portx;
402         view.bottom  = window->height+view.top;
403         view.right   = window->width+view.left;
404         /* clipRect coordinates are also relative to GrafPort */
405         clip.top     = window->clipRect.top;
406         clip.left    = window->clipRect.left;
407         clip.bottom  = window->clipRect.bottom;
408         clip.right   = window->clipRect.right;
409
410         libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
411
412         /* remember new window */
413         p_plugin->setWindow(*window);
414     }
415     else if( curwin.window )
416     {
417         /* change/set parent */
418         libvlc_video_set_parent(p_vlc, 0, NULL);
419         curwin.window = NULL;
420     }
421 #endif /* XP_MACOSX */
422
423 #ifdef XP_WIN
424     if( window && window->window )
425     {
426         /* check if plugin has a new parent window */
427         HWND drawable = (HWND) (window->window);
428         if( !curwin.window || drawable != curwin.window )
429         {
430             /* reset previous window settings */
431             HWND oldwin = (HWND)p_plugin->getWindow().window;
432             WNDPROC oldproc = p_plugin->getWindowProc();
433             if( oldproc )
434             {
435                 /* reset WNDPROC */
436                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
437             }
438             /* attach our plugin object */
439             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
440                              reinterpret_cast<LONG_PTR>(p_plugin));
441
442             /* install our WNDPROC */
443             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
444                                              GWL_WNDPROC, (LONG)Manage ) );
445
446             /* change window style to our liking */
447             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
448             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
449             SetWindowLong((HWND)drawable, GWL_STYLE, style);
450
451             /* change/set parent */
452             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
453
454             /* remember new window */
455             p_plugin->setWindow(*window);
456
457             /* Redraw window */
458             InvalidateRect( (HWND)drawable, NULL, TRUE );
459             UpdateWindow( (HWND)drawable );
460         }
461     }
462     else if ( curwin.window )
463     {
464         /* reset WNDPROC */
465         HWND oldwin = (HWND)curwin.window;
466         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
467         p_plugin->setWindowProc(NULL);
468         /* change/set parent */
469         libvlc_video_set_parent(p_vlc, 0, NULL);
470         curwin.window = NULL;
471     }
472 #endif /* XP_WIN */
473
474 #ifdef XP_UNIX
475     /* default to hidden toolbar, shown at the end of this method if asked *
476      * developers note : getToolbarSize need to wait the end of this method
477      */
478     i_control_height = 0;
479     i_control_width = window->width;
480
481     if( window && window->window )
482     {
483         Window  parent  = (Window) window->window;
484         if( !curwin.window || (parent != (Window)curwin.window) )
485         {
486             Display *p_display = ( (NPSetWindowCallbackStruct *)
487                                    window->ws_info )->display;
488
489             XResizeWindow( p_display, parent, window->width, window->height );
490
491             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
492
493             /* create windows */
494             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
495                            window->width, window->height - i_control_height,
496                            0, i_blackColor, i_blackColor );
497             Window controls = (Window) NULL;
498             controls = XCreateSimpleWindow( p_display, parent,
499                             0, window->height - i_control_height-1,
500                             window->width, i_control_height-1,
501                             0, i_blackColor, i_blackColor );
502
503             XMapWindow( p_display, parent );
504             XMapWindow( p_display, video );
505             if( controls ) { XMapWindow( p_display, controls ); }
506
507             XFlush(p_display);
508
509             /* bind events */
510             Widget w = XtWindowToWidget( p_display, parent );
511
512             XtAddEventHandler( w, ExposureMask, FALSE,
513                                (XtEventHandler)Redraw, p_plugin );
514             XtAddEventHandler( w, StructureNotifyMask, FALSE,
515                                (XtEventHandler)Resize, p_plugin );
516             XtAddEventHandler( w, ButtonReleaseMask, FALSE,
517                                (XtEventHandler)ControlHandler, p_plugin );
518
519             /* callback */
520 /*
521             libvlc_media_player_t *p_md;
522
523             libvlc_exception_t ex;
524             libvlc_exception_init(& ex );
525             p_md = p_plugin->getMD( &ex );
526             libvlc_exception_clear( &ex );
527             libvlc_event_attach( libvlc_media_player_event_manager( p_md, &ex ),
528                                  libvlc_MediaPlayerPositionChanged, Redraw, NULL, &ex );
529 */
530
531             /* set/change parent window */
532             libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, NULL );
533
534             /* remember window */
535             p_plugin->setWindow( *window );
536             p_plugin->setVideoWindow( video );
537             if( controls ) { p_plugin->setControlWindow( controls ); }
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         /* change/set parent */
551         libvlc_video_set_parent(p_vlc, 0, NULL);
552         curwin.window = NULL;
553     }
554 #endif /* XP_UNIX */
555
556     if( !p_plugin->b_stream )
557     {
558         if( p_plugin->psz_target )
559         {
560             if( p_plugin->playlist_add( p_plugin->psz_target, NULL ) != -1 )
561             {
562                 if( p_plugin->b_autoplay )
563                 {
564                     p_plugin->playlist_play(NULL);
565                 }
566             }
567             p_plugin->b_stream = true;
568         }
569     }
570     return NPERR_NO_ERROR;
571 }
572
573 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
574                        NPBool seekable, uint16 *stype )
575 {
576     if( NULL == instance  )
577     {
578         return NPERR_INVALID_INSTANCE_ERROR;
579     }
580
581     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
582     if( NULL == p_plugin )
583     {
584         return NPERR_INVALID_INSTANCE_ERROR;
585     }
586
587    /*
588    ** Firefox/Mozilla may decide to open a stream from the URL specified
589    ** in the SRC parameter of the EMBED tag and pass it to us
590    **
591    ** since VLC will open the SRC URL as well, we're not interested in
592    ** that stream. Otherwise, we'll take it and queue it up in the playlist
593    */
594     if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
595     {
596         /* TODO: use pipes !!!! */
597         *stype = NP_ASFILEONLY;
598         return NPERR_NO_ERROR;
599     }
600     return NPERR_GENERIC_ERROR;
601 }
602
603 int32 NPP_WriteReady( NPP instance, NPStream *stream )
604 {
605     /* TODO */
606     return 8*1024;
607 }
608
609 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
610                  int32 len, void *buffer )
611 {
612     /* TODO */
613     return len;
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 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
626 {
627     if( instance == NULL )
628     {
629         return;
630     }
631
632     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
633     if( NULL == p_plugin )
634     {
635         return;
636     }
637
638     if( p_plugin->playlist_add( stream->url, NULL ) != -1 )
639     {
640         if( p_plugin->b_autoplay )
641         {
642             p_plugin->playlist_play(NULL);
643         }
644     }
645 }
646
647 void NPP_URLNotify( NPP instance, const char* url,
648                     NPReason reason, void* notifyData )
649 {
650     /***** Insert NPP_URLNotify code here *****\
651     PluginInstance* p_plugin;
652     if (instance != NULL)
653         p_plugin = (PluginInstance*) instance->pdata;
654     \*********************************************/
655 }
656
657 void NPP_Print( NPP instance, NPPrint* printInfo )
658 {
659     if( printInfo == NULL )
660     {
661         return;
662     }
663
664     if( instance != NULL )
665     {
666         /***** Insert NPP_Print code here *****\
667         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
668         \**************************************/
669
670         if( printInfo->mode == NP_FULL )
671         {
672             /*
673              * PLUGIN DEVELOPERS:
674              *  If your plugin would like to take over
675              *  printing completely when it is in full-screen mode,
676              *  set printInfo->pluginPrinted to TRUE and print your
677              *  plugin as you see fit.  If your plugin wants Netscape
678              *  to handle printing in this case, set
679              *  printInfo->pluginPrinted to FALSE (the default) and
680              *  do nothing.  If you do want to handle printing
681              *  yourself, printOne is true if the print button
682              *  (as opposed to the print menu) was clicked.
683              *  On the Macintosh, platformPrint is a THPrint; on
684              *  Windows, platformPrint is a structure
685              *  (defined in npapi.h) containing the printer name, port,
686              *  etc.
687              */
688
689             /***** Insert NPP_Print code here *****\
690             void* platformPrint =
691                 printInfo->print.fullPrint.platformPrint;
692             NPBool printOne =
693                 printInfo->print.fullPrint.printOne;
694             \**************************************/
695
696             /* Do the default*/
697             printInfo->print.fullPrint.pluginPrinted = FALSE;
698         }
699         else
700         {
701             /* If not fullscreen, we must be embedded */
702             /*
703              * PLUGIN DEVELOPERS:
704              *  If your plugin is embedded, or is full-screen
705              *  but you returned false in pluginPrinted above, NPP_Print
706              *  will be called with mode == NP_EMBED.  The NPWindow
707              *  in the printInfo gives the location and dimensions of
708              *  the embedded plugin on the printed page.  On the
709              *  Macintosh, platformPrint is the printer port; on
710              *  Windows, platformPrint is the handle to the printing
711              *  device context.
712              */
713
714             /***** Insert NPP_Print code here *****\
715             NPWindow* printWindow =
716                 &(printInfo->print.embedPrint.window);
717             void* platformPrint =
718                 printInfo->print.embedPrint.platformPrint;
719             \**************************************/
720         }
721     }
722 }
723
724 /******************************************************************************
725  * Windows-only methods
726  *****************************************************************************/
727 #if XP_WIN
728 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
729 {
730     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
731
732     switch( i_msg )
733     {
734         case WM_ERASEBKGND:
735             return 1L;
736
737         case WM_PAINT:
738         {
739             PAINTSTRUCT paintstruct;
740             HDC hdc;
741             RECT rect;
742
743             hdc = BeginPaint( p_hwnd, &paintstruct );
744
745             GetClientRect( p_hwnd, &rect );
746
747             FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
748             SetTextColor(hdc, RGB(255, 255, 255));
749             SetBkColor(hdc, RGB(0, 0, 0));
750             DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect,
751                       DT_CENTER|DT_VCENTER|DT_SINGLELINE);
752
753             EndPaint( p_hwnd, &paintstruct );
754             return 0L;
755         }
756         default:
757             /* delegate to default handler */
758             return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
759                                    i_msg, wpar, lpar );
760     }
761 }
762 #endif /* XP_WIN */
763
764 /******************************************************************************
765  * UNIX-only methods
766  *****************************************************************************/
767 #ifdef XP_UNIX
768 static void Redraw( Widget w, XtPointer closure, XEvent *event )
769 {
770     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
771     Window control = p_plugin->getControlWindow();
772     const NPWindow& window = p_plugin->getWindow();
773     GC gc;
774     XGCValues gcv;
775     unsigned int i_control_height, i_control_width;
776
777     if( p_plugin->b_toolbar )
778         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
779     else
780         i_control_height = i_control_width = 0;
781
782     Window video = p_plugin->getVideoWindow();
783     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
784
785     gcv.foreground = BlackPixel( p_display, 0 );
786     gc = XCreateGC( p_display, video, GCForeground, &gcv );
787
788     XFillRectangle( p_display, video, gc,
789                     0, 0, window.width, window.height - i_control_height);
790
791     gcv.foreground = WhitePixel( p_display, 0 );
792     XChangeGC( p_display, gc, GCForeground, &gcv );
793
794     XDrawString( p_display, video, gc,
795                  window.width / 2 - 40, (window.height - i_control_height) / 2,
796                  WINDOW_TEXT, strlen(WINDOW_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         if( libvlc_exception_raised(&ex) )
820             fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
821         libvlc_exception_clear( &ex );
822
823         i_playing = p_plugin->playlist_isplaying( &ex );
824         if( libvlc_exception_raised(&ex) )
825             fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
826         libvlc_exception_clear( &ex );
827
828         vlc_toolbar_clicked_t clicked;
829         clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
830         switch( clicked )
831         {
832             case clicked_Play:
833             case clicked_Pause:
834             {
835                 if( i_playing == 1 )
836                     p_plugin->playlist_pause( &ex );
837                 else
838                     p_plugin->playlist_play( &ex );
839
840                 if( libvlc_exception_raised(&ex) )
841                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
842                 libvlc_exception_clear( &ex );
843             }
844             break;
845
846             case clicked_Stop:
847             {
848                 p_plugin->playlist_stop(&ex);
849                 if( libvlc_exception_raised(&ex) )
850                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
851                 libvlc_exception_clear( &ex );
852             }
853             break;
854
855             case clicked_Fullscreen:
856             {
857                 p_plugin->set_fullscreen( 1, &ex );
858                 if( libvlc_exception_raised(&ex) )
859                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
860                 libvlc_exception_clear( &ex );
861             }
862             break;
863
864             case clicked_Mute:
865             case clicked_Unmute:
866             {
867                 libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
868                 if( libvlc_exception_raised(&ex) )
869                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
870                 libvlc_exception_clear( &ex );
871             }
872             break;
873
874             case clicked_timeline:
875             {
876                 /* if a movie is loaded */
877                 if( p_md )
878                 {
879                     int64_t f_length;
880                     f_length = libvlc_media_player_get_length( p_md, &ex ) / 100;
881                     libvlc_exception_clear( &ex );
882
883                     f_length = (float)f_length *
884                             ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
885
886                     libvlc_media_player_set_time( p_md, f_length, &ex );
887                     if( libvlc_exception_raised(&ex) )
888                         fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
889                     libvlc_exception_clear( &ex );
890                 }
891             }
892             break;
893
894             case clicked_Time:
895             {
896                 /* Not implemented yet*/
897             }
898             break;
899
900             default: /* button_Unknown */
901             break;
902         }
903     }
904     Redraw( w, closure, event );
905 }
906
907 static void Resize ( Widget w, XtPointer closure, XEvent *event )
908 {
909     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
910     Window control = p_plugin->getControlWindow();
911     const NPWindow& window = p_plugin->getWindow();
912     Window  drawable   = p_plugin->getVideoWindow();
913     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
914
915     int i_ret;
916     Window root_return, parent_return, * children_return;
917     Window base_window;
918     unsigned int i_nchildren;
919     unsigned int i_control_height, i_control_width;
920
921     if( p_plugin->b_toolbar )
922     {
923         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
924     }
925     else
926     {
927         i_control_height = i_control_width = 0;
928     }
929
930 #ifdef X11_RESIZE_DEBUG
931     XWindowAttributes attr;
932
933     if( event && event->type == ConfigureNotify )
934     {
935         fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
936                  "send_event ? %s\n", event->xconfigure.width,
937                  event->xconfigure.height,
938                  event->xconfigure.send_event ? "TRUE" : "FALSE" );
939     }
940 #endif /* X11_RESIZE_DEBUG */
941
942     if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
943     {
944         /* size already set */
945         return;
946     }
947
948     i_ret = XResizeWindow( p_display, drawable,
949                            window.width, (window.height - i_control_height) );
950
951 #ifdef X11_RESIZE_DEBUG
952     fprintf( stderr,
953              "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
954
955     XGetWindowAttributes ( p_display, drawable, &attr );
956
957     /* X is asynchronous, so the current size reported here is not
958        necessarily the requested size as the Resize request may not
959        yet have been handled by the plugin host */
960     fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
961              attr.width, attr.height );
962 #endif /* X11_RESIZE_DEBUG */
963
964     XQueryTree( p_display, drawable,
965                 &root_return, &parent_return, &children_return,
966                 &i_nchildren );
967
968     if( i_nchildren > 0 )
969     {
970         /* XXX: Make assumptions related to the window parenting structure in
971            vlc/modules/video_output/x11/xcommon.c */
972         base_window = children_return[i_nchildren - 1];
973
974 #ifdef X11_RESIZE_DEBUG
975         fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
976         fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
977                  base_window );
978 #endif /* X11_RESIZE_DEBUG */
979
980         i_ret = XResizeWindow( p_display, base_window,
981                 window.width, ( window.height - i_control_height ) );
982
983 #ifdef X11_RESIZE_DEBUG
984         fprintf( stderr,
985                  "vlcshell::Resize() XResizeWindow(base) returned %d\n",
986                  i_ret );
987
988         XGetWindowAttributes( p_display, base_window, &attr );
989
990         fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
991                  attr.width, attr.height );
992 #endif /* X11_RESIZE_DEBUG */
993     }
994 }
995
996 #endif /* XP_UNIX */