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