b59c42d1651d92c5f7105510ccd1aab0a60740e6
[vlc.git] / 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
366 #if defined(XP_UNIX) && !defined(__APPLE__)
367     control = p_plugin->getControlWindow();
368 #endif
369
370     libvlc_exception_t ex;
371     libvlc_exception_init(&ex);
372
373     libvlc_instance_t *p_vlc = p_plugin->getVLC();
374
375     /*
376      * PLUGIN DEVELOPERS:
377      *  Before setting window to point to the
378      *  new window, you may wish to compare the new window
379      *  info to the previous window (if any) to note window
380      *  size changes, etc.
381      */
382
383     /* retrieve current window */
384     NPWindow& curwin = p_plugin->getWindow();
385
386 #ifdef XP_MACOSX
387     if( window && window->window )
388     {
389         /* check if plugin has a new parent window */
390         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
391         if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
392         {
393             /* set/change parent window */
394             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, &ex);
395             if( libvlc_exception_raised(&ex) )
396                 fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
397             libvlc_exception_clear(&ex);
398         }
399
400         /* as MacOS X video output is windowless, set viewport */
401         libvlc_rectangle_t view, clip;
402
403         /*
404         ** browser sets port origin to top-left location of plugin
405         ** relative to GrafPort window origin is set relative to document,
406         ** which of little use for drawing
407         */
408         view.top     = ((NP_Port*) (window->window))->porty;
409         view.left    = ((NP_Port*) (window->window))->portx;
410         view.bottom  = window->height+view.top;
411         view.right   = window->width+view.left;
412
413         /* clipRect coordinates are also relative to GrafPort */
414         clip.top     = window->clipRect.top;
415         clip.left    = window->clipRect.left;
416         clip.bottom  = window->clipRect.bottom;
417         clip.right   = window->clipRect.right;
418
419         libvlc_video_set_viewport(p_vlc, p_plugin->getMD(&ex), &view, &clip, &ex);
420         if( libvlc_exception_raised(&ex) )
421             fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
422         libvlc_exception_clear(&ex);
423
424         /* remember new window */
425         p_plugin->setWindow(*window);
426     }
427     else if( curwin.window )
428     {
429         /* change/set parent */
430         libvlc_video_set_parent(p_vlc, 0, &ex);
431         if( libvlc_exception_raised(&ex) )
432             fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
433         libvlc_exception_clear(&ex);
434
435         curwin.window = NULL;
436     }
437 #endif /* XP_MACOSX */
438
439 #ifdef XP_WIN
440     if( window && window->window )
441     {
442         /* check if plugin has a new parent window */
443         HWND drawable = (HWND) (window->window);
444         if( !curwin.window || drawable != curwin.window )
445         {
446             /* reset previous window settings */
447             HWND oldwin = (HWND)p_plugin->getWindow().window;
448             WNDPROC oldproc = p_plugin->getWindowProc();
449             if( oldproc )
450             {
451                 /* reset WNDPROC */
452                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
453             }
454             /* attach our plugin object */
455             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
456                              reinterpret_cast<LONG_PTR>(p_plugin));
457
458             /* install our WNDPROC */
459             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
460                                              GWL_WNDPROC, (LONG)Manage ) );
461
462             /* change window style to our liking */
463             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
464             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
465             SetWindowLong((HWND)drawable, GWL_STYLE, style);
466
467             /* change/set parent */
468             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, &ex);
469             if( libvlc_exception_raised(&ex) )
470                 fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
471             libvlc_exception_clear(&ex);
472
473             /* remember new window */
474             p_plugin->setWindow(*window);
475
476             /* Redraw window */
477             InvalidateRect( (HWND)drawable, NULL, TRUE );
478             UpdateWindow( (HWND)drawable );
479         }
480     }
481     else if( curwin.window )
482     {
483         /* reset WNDPROC */
484         HWND oldwin = (HWND)curwin.window;
485         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
486         p_plugin->setWindowProc(NULL);
487
488         /* change/set parent */
489         libvlc_video_set_parent(p_vlc, 0, &ex);
490         if( libvlc_exception_raised(&ex) )
491             fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
492         libvlc_exception_clear(&ex);
493
494         curwin.window = NULL;
495     }
496 #endif /* XP_WIN */
497
498 #ifdef XP_UNIX
499     /* default to hidden toolbar, shown at the end of this method if asked *
500      * developers note : getToolbarSize need to wait the end of this method
501      */
502     i_control_height = 0;
503     i_control_width = window->width;
504
505     if( window && window->window )
506     {
507         Window  parent  = (Window) window->window;
508         if( !curwin.window || (parent != (Window)curwin.window) )
509         {
510             Display *p_display = ( (NPSetWindowCallbackStruct *)
511                                    window->ws_info )->display;
512
513             XResizeWindow( p_display, parent, window->width, window->height );
514
515             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
516
517             /* create windows */
518             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
519                            window->width, window->height - i_control_height,
520                            0, i_blackColor, i_blackColor );
521             Window controls = (Window) NULL;
522             controls = XCreateSimpleWindow( p_display, parent,
523                             0, window->height - i_control_height-1,
524                             window->width, i_control_height-1,
525                             0, i_blackColor, i_blackColor );
526
527             XMapWindow( p_display, parent );
528             XMapWindow( p_display, video );
529             if( controls ) { XMapWindow( p_display, controls ); }
530
531             XFlush(p_display);
532
533             /* bind events */
534             Widget w = XtWindowToWidget( p_display, parent );
535
536             XtAddEventHandler( w, ExposureMask, FALSE,
537                                (XtEventHandler)Redraw, p_plugin );
538             XtAddEventHandler( w, StructureNotifyMask, FALSE,
539                                (XtEventHandler)Resize, p_plugin );
540             XtAddEventHandler( w, ButtonReleaseMask, FALSE,
541                                (XtEventHandler)ControlHandler, p_plugin );
542
543             /* set/change parent window */
544             libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, &ex );
545             if( libvlc_exception_raised(&ex) )
546                 fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
547             libvlc_exception_clear(&ex);
548
549             /* remember window */
550             p_plugin->setWindow( *window );
551             p_plugin->setVideoWindow( video );
552
553             if( controls )
554             {
555                 p_plugin->setControlWindow( controls );
556             }
557
558             Redraw( w, (XtPointer)p_plugin, NULL );
559
560             /* now display toolbar if asked through parameters */
561             if( p_plugin->b_toolbar )
562             {
563                 p_plugin->showToolbar();
564             }
565         }
566     }
567     else if( curwin.window )
568     {
569         /* change/set parent */
570         libvlc_video_set_parent(p_vlc, 0, &ex);
571         if( libvlc_exception_raised(&ex) )
572             fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
573         libvlc_exception_clear(&ex);
574         curwin.window = NULL;
575     }
576 #endif /* XP_UNIX */
577
578     if( !p_plugin->b_stream )
579     {
580         if( p_plugin->psz_target )
581         {
582             if( p_plugin->playlist_add( p_plugin->psz_target, NULL ) != -1 )
583             {
584                 if( p_plugin->b_autoplay )
585                 {
586                     p_plugin->playlist_play(NULL);
587                 }
588             }
589             p_plugin->b_stream = true;
590         }
591     }
592     return NPERR_NO_ERROR;
593 }
594
595 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
596                        NPBool seekable, uint16 *stype )
597 {
598     if( NULL == instance  )
599     {
600         return NPERR_INVALID_INSTANCE_ERROR;
601     }
602
603     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
604     if( NULL == p_plugin )
605     {
606         return NPERR_INVALID_INSTANCE_ERROR;
607     }
608
609    /*
610    ** Firefox/Mozilla may decide to open a stream from the URL specified
611    ** in the SRC parameter of the EMBED tag and pass it to us
612    **
613    ** since VLC will open the SRC URL as well, we're not interested in
614    ** that stream. Otherwise, we'll take it and queue it up in the playlist
615    */
616     if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
617     {
618         /* TODO: use pipes !!!! */
619         *stype = NP_ASFILEONLY;
620         return NPERR_NO_ERROR;
621     }
622     return NPERR_GENERIC_ERROR;
623 }
624
625 int32 NPP_WriteReady( NPP instance, NPStream *stream )
626 {
627     /* TODO */
628     return 8*1024;
629 }
630
631 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
632                  int32 len, void *buffer )
633 {
634     /* TODO */
635     return len;
636 }
637
638 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
639 {
640     if( instance == NULL )
641     {
642         return NPERR_INVALID_INSTANCE_ERROR;
643     }
644     return NPERR_NO_ERROR;
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( p_plugin->playlist_add( stream->url, NULL ) != -1 )
661     {
662         if( p_plugin->b_autoplay )
663         {
664             p_plugin->playlist_play(NULL);
665         }
666     }
667 }
668
669 void NPP_URLNotify( NPP instance, const char* url,
670                     NPReason reason, void* notifyData )
671 {
672     /***** Insert NPP_URLNotify code here *****\
673     PluginInstance* p_plugin;
674     if (instance != NULL)
675         p_plugin = (PluginInstance*) instance->pdata;
676     \*********************************************/
677 }
678
679 void NPP_Print( NPP instance, NPPrint* printInfo )
680 {
681     if( printInfo == NULL )
682     {
683         return;
684     }
685
686     if( instance != NULL )
687     {
688         /***** Insert NPP_Print code here *****\
689         PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
690         \**************************************/
691
692         if( printInfo->mode == NP_FULL )
693         {
694             /*
695              * PLUGIN DEVELOPERS:
696              *  If your plugin would like to take over
697              *  printing completely when it is in full-screen mode,
698              *  set printInfo->pluginPrinted to TRUE and print your
699              *  plugin as you see fit.  If your plugin wants Netscape
700              *  to handle printing in this case, set
701              *  printInfo->pluginPrinted to FALSE (the default) and
702              *  do nothing.  If you do want to handle printing
703              *  yourself, printOne is true if the print button
704              *  (as opposed to the print menu) was clicked.
705              *  On the Macintosh, platformPrint is a THPrint; on
706              *  Windows, platformPrint is a structure
707              *  (defined in npapi.h) containing the printer name, port,
708              *  etc.
709              */
710
711             /***** Insert NPP_Print code here *****\
712             void* platformPrint =
713                 printInfo->print.fullPrint.platformPrint;
714             NPBool printOne =
715                 printInfo->print.fullPrint.printOne;
716             \**************************************/
717
718             /* Do the default*/
719             printInfo->print.fullPrint.pluginPrinted = FALSE;
720         }
721         else
722         {
723             /* If not fullscreen, we must be embedded */
724             /*
725              * PLUGIN DEVELOPERS:
726              *  If your plugin is embedded, or is full-screen
727              *  but you returned false in pluginPrinted above, NPP_Print
728              *  will be called with mode == NP_EMBED.  The NPWindow
729              *  in the printInfo gives the location and dimensions of
730              *  the embedded plugin on the printed page.  On the
731              *  Macintosh, platformPrint is the printer port; on
732              *  Windows, platformPrint is the handle to the printing
733              *  device context.
734              */
735
736             /***** Insert NPP_Print code here *****\
737             NPWindow* printWindow =
738                 &(printInfo->print.embedPrint.window);
739             void* platformPrint =
740                 printInfo->print.embedPrint.platformPrint;
741             \**************************************/
742         }
743     }
744 }
745
746 /******************************************************************************
747  * Windows-only methods
748  *****************************************************************************/
749 #if XP_WIN
750 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
751 {
752     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
753
754     switch( i_msg )
755     {
756         case WM_ERASEBKGND:
757             return 1L;
758
759         case WM_PAINT:
760         {
761             PAINTSTRUCT paintstruct;
762             HDC hdc;
763             RECT rect;
764
765             hdc = BeginPaint( p_hwnd, &paintstruct );
766
767             GetClientRect( p_hwnd, &rect );
768
769             FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
770             SetTextColor(hdc, RGB(255, 255, 255));
771             SetBkColor(hdc, RGB(0, 0, 0));
772             DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect,
773                       DT_CENTER|DT_VCENTER|DT_SINGLELINE);
774
775             EndPaint( p_hwnd, &paintstruct );
776             return 0L;
777         }
778         default:
779             /* delegate to default handler */
780             return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
781                                    i_msg, wpar, lpar );
782     }
783 }
784 #endif /* XP_WIN */
785
786 /******************************************************************************
787  * UNIX-only methods
788  *****************************************************************************/
789 #ifdef XP_UNIX
790 static void Redraw( Widget w, XtPointer closure, XEvent *event )
791 {
792     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
793     Window control = p_plugin->getControlWindow();
794     const NPWindow& window = p_plugin->getWindow();
795     GC gc;
796     XGCValues gcv;
797     unsigned int i_control_height, i_control_width;
798
799     if( p_plugin->b_toolbar )
800         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
801     else
802         i_control_height = i_control_width = 0;
803
804     Window video = p_plugin->getVideoWindow();
805     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
806
807     gcv.foreground = BlackPixel( p_display, 0 );
808     gc = XCreateGC( p_display, video, GCForeground, &gcv );
809
810     XFillRectangle( p_display, video, gc,
811                     0, 0, window.width, window.height - i_control_height);
812
813     gcv.foreground = WhitePixel( p_display, 0 );
814     XChangeGC( p_display, gc, GCForeground, &gcv );
815
816     XDrawString( p_display, video, gc,
817                  window.width / 2 - 40, (window.height - i_control_height) / 2,
818                  WINDOW_TEXT, strlen(WINDOW_TEXT) );
819     XFreeGC( p_display, gc );
820
821     p_plugin->redrawToolbar();
822 }
823
824 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
825 {
826     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
827     const NPWindow& window = p_plugin->getWindow();
828
829     int i_height = window.height;
830     int i_width = window.width;
831     int i_xPos = event->xbutton.x;
832     int i_yPos = event->xbutton.y;
833
834     if( p_plugin && p_plugin->b_toolbar )
835     {
836         int i_playing;
837         libvlc_exception_t ex;
838
839         libvlc_exception_init( &ex );
840         libvlc_media_player_t *p_md = p_plugin->getMD(&ex);
841         if( libvlc_exception_raised(&ex) )
842             fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
843         libvlc_exception_clear( &ex );
844
845         i_playing = p_plugin->playlist_isplaying( &ex );
846         if( libvlc_exception_raised(&ex) )
847             fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
848         libvlc_exception_clear( &ex );
849
850         vlc_toolbar_clicked_t clicked;
851         clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
852         switch( clicked )
853         {
854             case clicked_Play:
855             case clicked_Pause:
856             {
857                 if( i_playing == 1 )
858                     p_plugin->playlist_pause( &ex );
859                 else
860                     p_plugin->playlist_play( &ex );
861
862                 if( libvlc_exception_raised(&ex) )
863                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
864                 libvlc_exception_clear( &ex );
865             }
866             break;
867
868             case clicked_Stop:
869             {
870                 p_plugin->playlist_stop(&ex);
871                 if( libvlc_exception_raised(&ex) )
872                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
873                 libvlc_exception_clear( &ex );
874             }
875             break;
876
877             case clicked_Fullscreen:
878             {
879                 p_plugin->set_fullscreen( 1, &ex );
880                 if( libvlc_exception_raised(&ex) )
881                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
882                 libvlc_exception_clear( &ex );
883             }
884             break;
885
886             case clicked_Mute:
887             case clicked_Unmute:
888             {
889                 libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
890                 if( libvlc_exception_raised(&ex) )
891                     fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
892                 libvlc_exception_clear( &ex );
893             }
894             break;
895
896             case clicked_timeline:
897             {
898                 /* if a movie is loaded */
899                 if( p_md )
900                 {
901                     int64_t f_length;
902                     f_length = libvlc_media_player_get_length( p_md, &ex ) / 100;
903                     libvlc_exception_clear( &ex );
904
905                     f_length = (float)f_length *
906                             ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
907
908                     libvlc_media_player_set_time( p_md, f_length, &ex );
909                     if( libvlc_exception_raised(&ex) )
910                         fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
911                     libvlc_exception_clear( &ex );
912                 }
913             }
914             break;
915
916             case clicked_Time:
917             {
918                 /* Not implemented yet*/
919             }
920             break;
921
922             default: /* button_Unknown */
923             break;
924         }
925     }
926     Redraw( w, closure, event );
927 }
928
929 static void Resize ( Widget w, XtPointer closure, XEvent *event )
930 {
931     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
932     Window control = p_plugin->getControlWindow();
933     const NPWindow& window = p_plugin->getWindow();
934     Window  drawable   = p_plugin->getVideoWindow();
935     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
936
937     int i_ret;
938     Window root_return, parent_return, * children_return;
939     Window base_window;
940     unsigned int i_nchildren;
941     unsigned int i_control_height, i_control_width;
942
943     if( p_plugin->b_toolbar )
944     {
945         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
946     }
947     else
948     {
949         i_control_height = i_control_width = 0;
950     }
951
952 #ifdef X11_RESIZE_DEBUG
953     XWindowAttributes attr;
954
955     if( event && event->type == ConfigureNotify )
956     {
957         fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
958                  "send_event ? %s\n", event->xconfigure.width,
959                  event->xconfigure.height,
960                  event->xconfigure.send_event ? "TRUE" : "FALSE" );
961     }
962 #endif /* X11_RESIZE_DEBUG */
963
964     if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
965     {
966         /* size already set */
967         return;
968     }
969
970     i_ret = XResizeWindow( p_display, drawable,
971                            window.width, (window.height - i_control_height) );
972
973 #ifdef X11_RESIZE_DEBUG
974     fprintf( stderr,
975              "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
976
977     XGetWindowAttributes ( p_display, drawable, &attr );
978
979     /* X is asynchronous, so the current size reported here is not
980        necessarily the requested size as the Resize request may not
981        yet have been handled by the plugin host */
982     fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
983              attr.width, attr.height );
984 #endif /* X11_RESIZE_DEBUG */
985
986     XQueryTree( p_display, drawable,
987                 &root_return, &parent_return, &children_return,
988                 &i_nchildren );
989
990     if( i_nchildren > 0 )
991     {
992         /* XXX: Make assumptions related to the window parenting structure in
993            vlc/modules/video_output/x11/xcommon.c */
994         base_window = children_return[i_nchildren - 1];
995
996 #ifdef X11_RESIZE_DEBUG
997         fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
998         fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
999                  base_window );
1000 #endif /* X11_RESIZE_DEBUG */
1001
1002         i_ret = XResizeWindow( p_display, base_window,
1003                 window.width, ( window.height - i_control_height ) );
1004
1005 #ifdef X11_RESIZE_DEBUG
1006         fprintf( stderr,
1007                  "vlcshell::Resize() XResizeWindow(base) returned %d\n",
1008                  i_ret );
1009
1010         XGetWindowAttributes( p_display, base_window, &attr );
1011
1012         fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
1013                  attr.width, attr.height );
1014 #endif /* X11_RESIZE_DEBUG */
1015     }
1016 }
1017
1018 #endif /* XP_UNIX */