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