]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcshell.cpp
Mozilla-plugin linux toolbar can be hidden by adding show_toolbar="no" to the
[vlc] / projects / mozilla / vlcshell.cpp
1 /*****************************************************************************
2  * vlcshell.cpp: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "config.h"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 /* Mozilla stuff */
34 #ifdef HAVE_MOZILLA_CONFIG_H
35 #   include <mozilla-config.h>
36 #endif
37
38 /* This is from mozilla java, do we really need it? */
39 #if 0
40 #include <jri.h>
41 #endif
42
43 #include "vlcplugin.h"
44
45 /* Enable/disable debugging printf's for X11 resizing */
46 #undef X11_RESIZE_DEBUG
47
48 #define WINDOW_TEXT "Video is loading..."
49
50 /*****************************************************************************
51  * Unix-only declarations
52 ******************************************************************************/
53 #ifdef XP_UNIX
54
55 static void Redraw( Widget w, XtPointer closure, XEvent *event );
56 static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
57 static void Resize( Widget w, XtPointer closure, XEvent *event );
58
59 #endif
60
61 /*****************************************************************************
62  * MacOS-only declarations
63 ******************************************************************************/
64 #ifdef XP_MACOSX
65 #endif
66
67 /*****************************************************************************
68  * Windows-only declarations
69  *****************************************************************************/
70 #ifdef XP_WIN
71
72 static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );
73
74 #endif
75
76 /******************************************************************************
77  * UNIX-only API calls
78  *****************************************************************************/
79 char * NPP_GetMIMEDescription( void )
80 {
81     return PLUGIN_MIMETYPES;
82 }
83
84 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
85 {
86
87     static char psz_desc[1000];
88
89     /* plugin class variables */
90     switch( variable )
91     {
92         case NPPVpluginNameString:
93             *((char **)value) = PLUGIN_NAME;
94             return NPERR_NO_ERROR;
95
96         case NPPVpluginDescriptionString:
97             snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
98                       VLC_Version() );
99             *((char **)value) = psz_desc;
100             return NPERR_NO_ERROR;
101
102         default:
103             /* move on to instance variables ... */
104             ;
105     }
106
107     if( instance == NULL )
108     {
109         return NPERR_INVALID_INSTANCE_ERROR;
110     }
111
112     /* plugin instance variables */
113
114     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
115     if( NULL == p_plugin )
116     {
117         // plugin has not been initialized yet !
118         return NPERR_INVALID_INSTANCE_ERROR;
119     }
120
121     switch( variable )
122     {
123         case NPPVpluginScriptableNPObject:
124         {
125             /* retrieve plugin root class */
126             NPClass *scriptClass = p_plugin->getScriptClass();
127             if( scriptClass )
128             {
129                 /* create an instance and return it */
130                 *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
131                 return NPERR_NO_ERROR;
132             }
133             break;
134         }
135
136         default:
137             ;
138     }
139     return NPERR_GENERIC_ERROR;
140 }
141
142 /*
143  * there is some confusion in gecko headers regarding definition of this API
144  * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
145  */
146
147 NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
148 {
149     return NPERR_GENERIC_ERROR;
150 }
151
152 /******************************************************************************
153  * Mac-only API calls
154  *****************************************************************************/
155 #ifdef XP_MACOSX
156 int16 NPP_HandleEvent( NPP instance, void * event )
157 {
158     static UInt32 lastMouseUp = 0;
159
160     if( instance == NULL )
161     {
162         return false;
163     }
164
165     VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
166
167     if( p_plugin == NULL )
168     {
169         return false;
170     }
171
172     EventRecord *myEvent = (EventRecord*)event;
173
174     switch( myEvent->what )
175     {
176         case nullEvent:
177             return true;
178         case mouseDown:
179         {
180             if( (myEvent->when - lastMouseUp) < GetDblTime() )
181             {
182                 /* double click */
183                 libvlc_instance_t *p_vlc = p_plugin->getVLC();
184
185                 if( p_vlc )
186                 {
187                     if( libvlc_playlist_isplaying(p_vlc, NULL) )
188                     {
189                         libvlc_media_instance_t *p_md =
190                             libvlc_playlist_get_media_instance(p_vlc, NULL);
191                         if( p_md )
192                         {
193                             libvlc_toggle_fullscreen(p_md, NULL);
194                             libvlc_media_instance_release(p_md);
195                         }
196                     }
197                 }
198             }
199             return true;
200         }
201         case mouseUp:
202             lastMouseUp = myEvent->when;
203             return true;
204         case keyUp:
205         case keyDown:
206         case autoKey:
207             return true;
208         case updateEvt:
209         {
210             const NPWindow& npwindow = p_plugin->getWindow();
211             if( npwindow.window )
212             {
213                 int hasVout = FALSE;
214                 libvlc_instance_t *p_vlc = p_plugin->getVLC();
215
216                 if( p_vlc )
217                 {
218                     if( libvlc_playlist_isplaying(p_vlc, NULL) )
219                     {
220                         libvlc_media_instance_t *p_md =
221                             libvlc_playlist_get_media_instance(p_vlc, NULL);
222                         if( p_md )
223                         {
224                             hasVout = libvlc_media_instance_has_vout(p_md,
225                                                                      NULL);
226                             if( hasVout )
227                             {
228                                 libvlc_rectangle_t area;
229                                 area.left = 0;
230                                 area.top = 0;
231                                 area.right = npwindow.width;
232                                 area.bottom = npwindow.height;
233                                 libvlc_video_redraw_rectangle(p_md, &area,
234                                                               NULL);
235                             }
236                             libvlc_media_instance_release(p_md);
237                         }
238                     }
239                 }
240
241                 if( ! hasVout )
242                 {
243                     /* draw the beautiful "No Picture" */
244
245                     ForeColor(blackColor);
246                     PenMode( patCopy );
247
248                     /* seems that firefox forgets to set the following
249                      * on occasion (reload) */
250                     SetOrigin(((NP_Port *)npwindow.window)->portx,
251                               ((NP_Port *)npwindow.window)->porty);
252
253                     Rect rect;
254                     rect.left = 0;
255                     rect.top = 0;
256                     rect.right = npwindow.width;
257                     rect.bottom = npwindow.height;
258                     PaintRect( &rect );
259
260                     ForeColor(whiteColor);
261                     MoveTo( (npwindow.width-80)/ 2  , npwindow.height / 2 );
262                     DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
263                 }
264             }
265             return true;
266         }
267         case activateEvt:
268             return false;
269         case NPEventType_GetFocusEvent:
270         case NPEventType_LoseFocusEvent:
271             return true;
272         case NPEventType_AdjustCursorEvent:
273             return false;
274         case NPEventType_MenuCommandEvent:
275             return false;
276         case NPEventType_ClippingChangedEvent:
277             return false;
278         case NPEventType_ScrollingBeginsEvent:
279             return true;
280         case NPEventType_ScrollingEndsEvent:
281             return true;
282         default:
283             ;
284     }
285     return false;
286 }
287 #endif /* XP_MACOSX */
288
289 /******************************************************************************
290  * General Plug-in Calls
291  *****************************************************************************/
292 NPError NPP_Initialize( void )
293 {
294     return NPERR_NO_ERROR;
295 }
296
297 jref NPP_GetJavaClass( void )
298 {
299     return NULL;
300 }
301
302 void NPP_Shutdown( void )
303 {
304     ;
305 }
306
307 NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
308                  char* argn[], char* argv[], NPSavedData* saved )
309 {
310     NPError status;
311
312     if( instance == NULL )
313     {
314         return NPERR_INVALID_INSTANCE_ERROR;
315     }
316
317     VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
318     if( NULL == p_plugin )
319     {
320         return NPERR_OUT_OF_MEMORY_ERROR;
321     }
322
323     status = p_plugin->init(argc, argn, argv);
324     if( NPERR_NO_ERROR == status )
325     {
326         instance->pdata = reinterpret_cast<void*>(p_plugin);
327 #if 0
328         NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
329         NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
330 #endif
331     }
332     else
333     {
334         delete p_plugin;
335     }
336     return status;
337 }
338
339 NPError NPP_Destroy( NPP instance, NPSavedData** save )
340 {
341     if( NULL == instance )
342         return NPERR_INVALID_INSTANCE_ERROR;
343
344     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
345     if( NULL == p_plugin )
346         return NPERR_NO_ERROR;
347
348     instance->pdata = NULL;
349
350 #if XP_WIN
351     HWND win = (HWND)p_plugin->getWindow().window;
352     WNDPROC winproc = p_plugin->getWindowProc();
353     if( winproc )
354     {
355         /* reset WNDPROC */
356         SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
357     }
358 #endif
359
360     delete p_plugin;
361
362     return NPERR_NO_ERROR;
363 }
364
365 NPError NPP_SetWindow( NPP instance, NPWindow* window )
366 {
367     /* height used by bottom toolbar (pixels). 0 if hidden */
368     unsigned int i_toolbar_height;
369
370     if( ! instance )
371     {
372         return NPERR_INVALID_INSTANCE_ERROR;
373     }
374
375     /* NPP_SetWindow may be called before NPP_New (Opera) */
376     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
377     if( NULL == p_plugin )
378     {
379         /* we should probably show a splash screen here */
380         return NPERR_NO_ERROR;
381     }
382
383     libvlc_instance_t *p_vlc = p_plugin->getVLC();
384
385
386     if( p_plugin->b_show_toolbar )
387     {
388         i_toolbar_height = p_plugin->i_control_height;
389     }
390     else
391     {
392         i_toolbar_height = 0;
393     }
394
395     /*
396      * PLUGIN DEVELOPERS:
397      *  Before setting window to point to the
398      *  new window, you may wish to compare the new window
399      *  info to the previous window (if any) to note window
400      *  size changes, etc.
401      */
402
403     /* retrieve current window */
404     NPWindow& curwin = p_plugin->getWindow();
405
406 #ifdef XP_MACOSX
407     if( window && window->window )
408     {
409         /* check if plugin has a new parent window */
410         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
411         if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
412         {
413             /* set/change parent window */
414             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
415         }
416
417         /* as MacOS X video output is windowless, set viewport */
418         libvlc_rectangle_t view, clip;
419
420         /*
421         ** browser sets port origin to top-left location of plugin
422         ** relative to GrafPort window origin is set relative to document,
423         ** which of little use for drawing
424         */
425         view.top     = ((NP_Port*) (window->window))->porty;
426         view.left    = ((NP_Port*) (window->window))->portx;
427         view.bottom  = window->height+view.top;
428         view.right   = window->width+view.left;
429         /* clipRect coordinates are also relative to GrafPort */
430         clip.top     = window->clipRect.top;
431         clip.left    = window->clipRect.left;
432         clip.bottom  = window->clipRect.bottom;
433         clip.right   = window->clipRect.right;
434
435         libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
436
437         /* remember new window */
438         p_plugin->setWindow(*window);
439     }
440     else if( curwin.window ) {
441         /* change/set parent */
442         libvlc_video_set_parent(p_vlc, 0, NULL);
443         curwin.window = NULL;
444     }
445 #endif /* XP_MACOSX */
446
447 #ifdef XP_WIN
448     if( window && window->window )
449     {
450         /* check if plugin has a new parent window */
451         HWND drawable = (HWND) (window->window);
452         if( !curwin.window || drawable != curwin.window )
453         {
454             /* reset previous window settings */
455             HWND oldwin = (HWND)p_plugin->getWindow().window;
456             WNDPROC oldproc = p_plugin->getWindowProc();
457             if( oldproc )
458             {
459                 /* reset WNDPROC */
460                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
461             }
462             /* attach our plugin object */
463             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
464                              reinterpret_cast<LONG_PTR>(p_plugin));
465
466             /* install our WNDPROC */
467             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
468                                              GWL_WNDPROC, (LONG)Manage ) );
469
470             /* change window style to our liking */
471             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
472             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
473             SetWindowLong((HWND)drawable, GWL_STYLE, style);
474
475             /* change/set parent */
476             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
477
478             /* remember new window */
479             p_plugin->setWindow(*window);
480
481             /* Redraw window */
482             InvalidateRect( (HWND)drawable, NULL, TRUE );
483             UpdateWindow( (HWND)drawable );
484         }
485     }
486     else if ( curwin.window )
487     {
488         /* reset WNDPROC */
489         HWND oldwin = (HWND)curwin.window;
490         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
491         p_plugin->setWindowProc(NULL);
492         /* change/set parent */
493         libvlc_video_set_parent(p_vlc, 0, NULL);
494         curwin.window = NULL;
495     }
496 #endif /* XP_WIN */
497
498 #ifdef XP_UNIX
499     if( window && window->window )
500     {
501         Window  parent   = (Window) window->window;
502         if( !curwin.window || (parent != (Window)curwin.window) )
503         {
504             Display *p_display = ( (NPSetWindowCallbackStruct *)
505                                    window->ws_info )->display;
506
507             XResizeWindow( p_display, parent, window->width, window->height );
508
509             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
510
511             /* create windows */
512             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
513                            window->width, window->height - i_toolbar_height,
514                            0, i_blackColor, i_blackColor );
515             Window controls = (Window) NULL;
516             if( p_plugin->b_show_toolbar )
517             {
518                 controls = XCreateSimpleWindow( p_display, parent,
519                                 0, window->height - i_toolbar_height-1,
520                                 window->width, i_toolbar_height-1,
521                                 0, i_blackColor, i_blackColor );
522             }
523
524             XMapWindow( p_display, parent );
525             XMapWindow( p_display, video );
526             if( controls ) { XMapWindow( p_display, controls ); }
527
528             XFlush(p_display);
529
530             /* bind events */
531             Widget w = XtWindowToWidget( p_display, parent );
532
533             XtAddEventHandler( w, ExposureMask, FALSE,
534                                (XtEventHandler)Redraw, p_plugin );
535             XtAddEventHandler( w, StructureNotifyMask, FALSE,
536                                (XtEventHandler)Resize, p_plugin );
537             XtAddEventHandler( w, ButtonReleaseMask, FALSE,
538                                (XtEventHandler)ControlHandler, p_plugin );
539
540             /* callback */
541 /*
542             libvlc_media_instance_t *p_md;
543
544             libvlc_exception_t ex;
545             libvlc_exception_init(& ex );
546             p_md = libvlc_playlist_get_media_instance( p_plugin->getVLC(), &ex );
547             libvlc_exception_init( &ex );
548             libvlc_event_attach( libvlc_media_instance_event_manager( p_md, &ex ),
549                                  libvlc_MediaInstancePositionChanged, Redraw, NULL, &ex );
550 */
551
552             /* set/change parent window */
553             libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, NULL );
554
555             /* remember window */
556             p_plugin->setWindow( *window );
557             p_plugin->setVideoWindow( video );
558             if( controls ) { p_plugin->setControlWindow( controls ); }
559
560             Redraw( w, (XtPointer)p_plugin, NULL );
561         }
562     }
563     else if ( curwin.window )
564     {
565         /* change/set parent */
566         libvlc_video_set_parent(p_vlc, 0, NULL);
567         curwin.window = NULL;
568     }
569 #endif /* XP_UNIX */
570
571     if( !p_plugin->b_stream )
572     {
573         if( p_plugin->psz_target )
574         {
575             if( libvlc_playlist_add( p_vlc, p_plugin->psz_target,
576                                      NULL, NULL ) != -1 )
577             {
578                 if( p_plugin->b_autoplay )
579                 {
580                     libvlc_playlist_play(p_vlc, 0, 0, NULL, NULL);
581                 }
582             }
583             p_plugin->b_stream = VLC_TRUE;
584         }
585     }
586     return NPERR_NO_ERROR;
587 }
588
589 NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
590                        NPBool seekable, uint16 *stype )
591 {
592     if( NULL == instance  )
593     {
594         return NPERR_INVALID_INSTANCE_ERROR;
595     }
596
597     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
598     if( NULL == p_plugin )
599     {
600         return NPERR_INVALID_INSTANCE_ERROR;
601     }
602
603    /*
604    ** Firefox/Mozilla may decide to open a stream from the URL specified
605    ** in the SRC parameter of the EMBED tag and pass it to us
606    **
607    ** since VLC will open the SRC URL as well, we're not interested in
608    ** that stream. Otherwise, we'll take it and queue it up in the playlist
609    */
610     if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
611     {
612         /* TODO: use pipes !!!! */
613         *stype = NP_ASFILEONLY;
614         return NPERR_NO_ERROR;
615     }
616     return NPERR_GENERIC_ERROR;
617 }
618
619 int32 NPP_WriteReady( NPP instance, NPStream *stream )
620 {
621     /* TODO */
622     return 8*1024;
623 }
624
625
626 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
627                  int32 len, void *buffer )
628 {
629     /* TODO */
630     return len;
631 }
632
633
634 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
635 {
636     if( instance == NULL )
637     {
638         return NPERR_INVALID_INSTANCE_ERROR;
639     }
640     return NPERR_NO_ERROR;
641 }
642
643
644 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
645 {
646     if( instance == NULL )
647     {
648         return;
649     }
650
651     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
652     if( NULL == p_plugin )
653     {
654         return;
655     }
656
657     if( libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL )
658         != -1 )
659     {
660         if( p_plugin->b_autoplay )
661         {
662             libvlc_playlist_play( p_plugin->getVLC(), 0, 0, NULL, NULL);
663         }
664     }
665 }
666
667
668 void NPP_URLNotify( NPP instance, const char* url,
669                     NPReason reason, void* notifyData )
670 {
671     /***** Insert NPP_URLNotify code here *****\
672     PluginInstance* p_plugin;
673     if (instance != NULL)
674         p_plugin = (PluginInstance*) instance->pdata;
675     \*********************************************/
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     const NPWindow& window = p_plugin->getWindow();
794     GC gc;
795     XGCValues gcv;
796     /* height used to show the bottom toolbar in non-fullscreen mode */
797     unsigned int i_toolbar_height;
798
799     if( p_plugin->b_show_toolbar )
800     {
801         p_plugin->showToolbar();
802         i_toolbar_height = p_plugin->i_control_height;
803     }
804     else
805     {
806         i_toolbar_height = 0;
807     }
808
809     Window video = p_plugin->getVideoWindow();
810     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
811
812     gcv.foreground = BlackPixel( p_display, 0 );
813     gc = XCreateGC( p_display, video, GCForeground, &gcv );
814
815     XFillRectangle( p_display, video, gc,
816                     0, 0, window.width, window.height - i_toolbar_height);
817
818     gcv.foreground = WhitePixel( p_display, 0 );
819     XChangeGC( p_display, gc, GCForeground, &gcv );
820
821     XDrawString( p_display, video, gc,
822                  window.width / 2 - 40, (window.height - i_toolbar_height) / 2,
823                  WINDOW_TEXT, strlen(WINDOW_TEXT) );
824     XFreeGC( p_display, gc );
825
826     if( p_plugin->b_show_toolbar )
827     {
828         p_plugin->redrawToolbar();
829         p_plugin->hideToolbar();
830     }
831 }
832
833 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
834 {
835     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
836     const NPWindow& window = p_plugin->getWindow();
837
838     int i_height = window.height;
839     int i_width = window.width;
840     int i_xPos = event->xbutton.x;
841     int i_yPos = event->xbutton.y;
842
843     if( p_plugin && p_plugin->b_show_toolbar )
844     {
845         libvlc_exception_t ex;
846         libvlc_exception_init( &ex );
847         libvlc_media_instance_t *p_md =
848                 libvlc_playlist_get_media_instance(p_plugin->getVLC(), &ex);
849         libvlc_exception_clear( &ex );
850
851         /* jump in the movie */
852         if( i_yPos <= (i_height-30) )
853         {
854             /* if a movie is loaded */
855             if( p_md )
856             {
857                 vlc_int64_t f_length;
858                 libvlc_exception_init( &ex );
859                 f_length = libvlc_media_instance_get_length( p_md, &ex ) / 100;
860                 libvlc_exception_clear( &ex );
861
862                 f_length = (float)f_length *
863                            ( ((float)i_xPos-4 ) / ( ((float)i_width-8)/100) );
864
865                 libvlc_exception_init( &ex );
866                 libvlc_media_instance_set_time( p_md, f_length, &ex );
867                 libvlc_exception_clear( &ex );
868             }
869         }
870
871         /* play/pause toggle */
872         if( (i_yPos > (i_height-30)) && (i_xPos > 4) && (i_xPos <= 39) )
873         {
874             int i_playing;
875             libvlc_exception_init( &ex );
876             i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
877             libvlc_exception_clear( &ex );
878
879             libvlc_exception_init( &ex );
880             if( i_playing == 1 )
881                 libvlc_playlist_pause( p_plugin->getVLC(), &ex );
882             else
883                 libvlc_playlist_play( p_plugin->getVLC(), -1, 0, NULL, &ex );
884             libvlc_exception_clear( &ex );
885         }
886
887         /* stop */
888         if( (i_yPos > (i_height-30)) && (i_xPos > 39) && (i_xPos < 67) )
889         {
890             libvlc_exception_init( &ex );
891             libvlc_playlist_stop( p_plugin->getVLC(), &ex );
892             libvlc_exception_clear( &ex );
893         }
894
895         /* fullscreen */
896         if( (i_yPos > (i_height-30)) && (i_xPos >= 67) && (i_xPos < 94) )
897         {
898             int i_playing;
899             libvlc_exception_init( &ex );
900             i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
901             libvlc_exception_clear( &ex );
902
903             if( (i_playing == 1) && p_md )
904             {
905                 libvlc_exception_init( &ex );
906                 libvlc_set_fullscreen( p_md, 1, &ex );
907                 libvlc_exception_clear( &ex );
908             }
909         }
910
911         /* mute toggle */
912         if( (i_yPos > (i_height-30)) && (i_xPos >= 94) && (i_xPos < 109))
913         {
914             libvlc_exception_init( &ex );
915             libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
916             libvlc_exception_clear( &ex );
917         }
918
919         if( p_md ) libvlc_media_instance_release( p_md );
920     }
921     Redraw( w, closure, event );
922 }
923
924 static void Resize ( Widget w, XtPointer closure, XEvent *event )
925 {
926     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
927     const NPWindow& window = p_plugin->getWindow();
928     Window  drawable   = p_plugin->getVideoWindow();
929     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
930
931     int i_ret;
932     Window root_return, parent_return, * children_return;
933     Window base_window;
934     unsigned int i_nchildren;
935     /* height used for the bottom control bar, 0 if hidden */
936     unsigned int i_toolbar_height;
937
938     if( p_plugin->b_show_toolbar )
939     {
940         i_toolbar_height = p_plugin->i_control_height;
941     }
942     else
943     {
944         i_toolbar_height = 0;
945     }
946
947 #ifdef X11_RESIZE_DEBUG
948     XWindowAttributes attr;
949
950     if( event && event->type == ConfigureNotify )
951     {
952         fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
953                  "send_event ? %s\n", event->xconfigure.width,
954                  event->xconfigure.height,
955                  event->xconfigure.send_event ? "TRUE" : "FALSE" );
956     }
957 #endif /* X11_RESIZE_DEBUG */
958
959     if( ! p_plugin->setSize(window.width, (window.height - i_toolbar_height)) )
960     {
961         /* size already set */
962         return;
963     }
964
965     i_ret = XResizeWindow( p_display, drawable,
966                            window.width, (window.height - i_toolbar_height) );
967
968 #ifdef X11_RESIZE_DEBUG
969     fprintf( stderr,
970              "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
971
972     XGetWindowAttributes ( p_display, drawable, &attr );
973
974     /* X is asynchronous, so the current size reported here is not
975        necessarily the requested size as the Resize request may not
976        yet have been handled by the plugin host */
977     fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
978              attr.width, attr.height );
979 #endif /* X11_RESIZE_DEBUG */
980
981     XQueryTree( p_display, drawable,
982                 &root_return, &parent_return, &children_return,
983                 &i_nchildren );
984
985     if( i_nchildren > 0 )
986     {
987         /* XXX: Make assumptions related to the window parenting structure in
988            vlc/modules/video_output/x11/xcommon.c */
989         base_window = children_return[i_nchildren - 1];
990
991 #ifdef X11_RESIZE_DEBUG
992         fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
993         fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
994                  base_window );
995 #endif /* X11_RESIZE_DEBUG */
996
997         i_ret = XResizeWindow( p_display, base_window,
998                 window.width, ( window.height - i_toolbar_height ) );
999
1000 #ifdef X11_RESIZE_DEBUG
1001         fprintf( stderr,
1002                  "vlcshell::Resize() XResizeWindow(base) returned %d\n",
1003                  i_ret );
1004
1005         XGetWindowAttributes( p_display, base_window, &attr );
1006
1007         fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
1008                  attr.width, attr.height );
1009 #endif /* X11_RESIZE_DEBUG */
1010     }
1011 }
1012
1013 #endif /* XP_UNIX */