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