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