13696ac59505d4d0f0748de1f90b50c68b78c754
[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                       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     Window control;
368     unsigned int i_control_height = 0, i_control_width = 0;
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     control = p_plugin->getControlWindow();
383
384     libvlc_instance_t *p_vlc = p_plugin->getVLC();
385
386     /*
387      * PLUGIN DEVELOPERS:
388      *  Before setting window to point to the
389      *  new window, you may wish to compare the new window
390      *  info to the previous window (if any) to note window
391      *  size changes, etc.
392      */
393
394     /* retrieve current window */
395     NPWindow& curwin = p_plugin->getWindow();
396
397 #ifdef XP_MACOSX
398     if( window && window->window )
399     {
400         /* check if plugin has a new parent window */
401         CGrafPtr drawable = (((NP_Port*) (window->window))->port);
402         if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
403         {
404             /* set/change parent window */
405             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
406         }
407
408         /* as MacOS X video output is windowless, set viewport */
409         libvlc_rectangle_t view, clip;
410
411         /*
412         ** browser sets port origin to top-left location of plugin
413         ** relative to GrafPort window origin is set relative to document,
414         ** which of little use for drawing
415         */
416         view.top     = ((NP_Port*) (window->window))->porty;
417         view.left    = ((NP_Port*) (window->window))->portx;
418         view.bottom  = window->height+view.top;
419         view.right   = window->width+view.left;
420         /* clipRect coordinates are also relative to GrafPort */
421         clip.top     = window->clipRect.top;
422         clip.left    = window->clipRect.left;
423         clip.bottom  = window->clipRect.bottom;
424         clip.right   = window->clipRect.right;
425
426         libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
427
428         /* remember new window */
429         p_plugin->setWindow(*window);
430     }
431     else if( curwin.window ) {
432         /* change/set parent */
433         libvlc_video_set_parent(p_vlc, 0, NULL);
434         curwin.window = NULL;
435     }
436 #endif /* XP_MACOSX */
437
438 #ifdef XP_WIN
439     if( window && window->window )
440     {
441         /* check if plugin has a new parent window */
442         HWND drawable = (HWND) (window->window);
443         if( !curwin.window || drawable != curwin.window )
444         {
445             /* reset previous window settings */
446             HWND oldwin = (HWND)p_plugin->getWindow().window;
447             WNDPROC oldproc = p_plugin->getWindowProc();
448             if( oldproc )
449             {
450                 /* reset WNDPROC */
451                 SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
452             }
453             /* attach our plugin object */
454             SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
455                              reinterpret_cast<LONG_PTR>(p_plugin));
456
457             /* install our WNDPROC */
458             p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
459                                              GWL_WNDPROC, (LONG)Manage ) );
460
461             /* change window style to our liking */
462             LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
463             style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
464             SetWindowLong((HWND)drawable, GWL_STYLE, style);
465
466             /* change/set parent */
467             libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
468
469             /* remember new window */
470             p_plugin->setWindow(*window);
471
472             /* Redraw window */
473             InvalidateRect( (HWND)drawable, NULL, TRUE );
474             UpdateWindow( (HWND)drawable );
475         }
476     }
477     else if ( curwin.window )
478     {
479         /* reset WNDPROC */
480         HWND oldwin = (HWND)curwin.window;
481         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
482         p_plugin->setWindowProc(NULL);
483         /* change/set parent */
484         libvlc_video_set_parent(p_vlc, 0, NULL);
485         curwin.window = NULL;
486     }
487 #endif /* XP_WIN */
488
489 #ifdef XP_UNIX
490     if( p_plugin->b_toolbar )
491     {
492         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
493     }
494     else
495     {
496         i_control_height = i_control_width = 0;
497     }
498
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_control_height,
514                            0, i_blackColor, i_blackColor );
515             Window controls = (Window) NULL;
516             if( p_plugin->b_toolbar )
517             {
518                 controls = XCreateSimpleWindow( p_display, parent,
519                                 0, window->height - i_control_height-1,
520                                 window->width, i_control_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     Window control = p_plugin->getControlWindow();
794     const NPWindow& window = p_plugin->getWindow();
795     GC gc;
796     XGCValues gcv;
797     unsigned int i_control_height, i_control_width;
798
799     if( p_plugin->b_toolbar )
800     {
801         p_plugin->showToolbar();
802         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
803     }
804     else
805         i_control_height = i_control_width = 0;
806
807     Window video = p_plugin->getVideoWindow();
808     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
809
810     gcv.foreground = BlackPixel( p_display, 0 );
811     gc = XCreateGC( p_display, video, GCForeground, &gcv );
812
813     XFillRectangle( p_display, video, gc,
814                     0, 0, window.width, window.height - i_control_height);
815
816     gcv.foreground = WhitePixel( p_display, 0 );
817     XChangeGC( p_display, gc, GCForeground, &gcv );
818
819     XDrawString( p_display, video, gc,
820                  window.width / 2 - 40, (window.height - i_control_height) / 2,
821                  WINDOW_TEXT, strlen(WINDOW_TEXT) );
822     XFreeGC( p_display, gc );
823
824     if( p_plugin->b_toolbar )
825     {
826         p_plugin->redrawToolbar();
827         p_plugin->hideToolbar();
828     }
829 }
830
831 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
832 {
833     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
834     const NPWindow& window = p_plugin->getWindow();
835
836     int i_height = window.height;
837     int i_width = window.width;
838     int i_xPos = event->xbutton.x;
839     int i_yPos = event->xbutton.y;
840
841     if( p_plugin && p_plugin->b_toolbar )
842     {
843         libvlc_exception_t ex;
844         libvlc_exception_init( &ex );
845         libvlc_media_instance_t *p_md =
846                 libvlc_playlist_get_media_instance(p_plugin->getVLC(), &ex);
847         libvlc_exception_clear( &ex );
848
849         /* jump in the movie */
850         if( i_yPos <= (i_height-30) )
851         {
852             /* if a movie is loaded */
853             if( p_md )
854             {
855                 vlc_int64_t f_length;
856                 libvlc_exception_init( &ex );
857                 f_length = libvlc_media_instance_get_length( p_md, &ex ) / 100;
858                 libvlc_exception_clear( &ex );
859
860                 f_length = (float)f_length *
861                            ( ((float)i_xPos-4 ) / ( ((float)i_width-8)/100) );
862
863                 libvlc_exception_init( &ex );
864                 libvlc_media_instance_set_time( p_md, f_length, &ex );
865                 libvlc_exception_clear( &ex );
866             }
867         }
868
869         /* play/pause toggle */
870         if( (i_yPos > (i_height-30)) && (i_xPos > 4) && (i_xPos <= 39) )
871         {
872             int i_playing;
873             libvlc_exception_init( &ex );
874             i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
875             libvlc_exception_clear( &ex );
876
877             libvlc_exception_init( &ex );
878             if( i_playing == 1 )
879                 libvlc_playlist_pause( p_plugin->getVLC(), &ex );
880             else
881                 libvlc_playlist_play( p_plugin->getVLC(), -1, 0, NULL, &ex );
882             libvlc_exception_clear( &ex );
883         }
884
885         /* stop */
886         if( (i_yPos > (i_height-30)) && (i_xPos > 39) && (i_xPos < 67) )
887         {
888             libvlc_exception_init( &ex );
889             libvlc_playlist_stop( p_plugin->getVLC(), &ex );
890             libvlc_exception_clear( &ex );
891         }
892
893         /* fullscreen */
894         if( (i_yPos > (i_height-30)) && (i_xPos >= 67) && (i_xPos < 94) )
895         {
896             int i_playing;
897             libvlc_exception_init( &ex );
898             i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
899             libvlc_exception_clear( &ex );
900
901             if( (i_playing == 1) && p_md )
902             {
903                 libvlc_exception_init( &ex );
904                 libvlc_set_fullscreen( p_md, 1, &ex );
905                 libvlc_exception_clear( &ex );
906             }
907         }
908
909         /* mute toggle */
910         if( (i_yPos > (i_height-30)) && (i_xPos >= 94) && (i_xPos < 109))
911         {
912             libvlc_exception_init( &ex );
913             libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
914             libvlc_exception_clear( &ex );
915         }
916
917         if( p_md ) libvlc_media_instance_release( p_md );
918     }
919     Redraw( w, closure, event );
920 }
921
922 static void Resize ( Widget w, XtPointer closure, XEvent *event )
923 {
924     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
925     Window control = p_plugin->getControlWindow();
926     const NPWindow& window = p_plugin->getWindow();
927     Window  drawable   = p_plugin->getVideoWindow();
928     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
929
930     int i_ret;
931     Window root_return, parent_return, * children_return;
932     Window base_window;
933     unsigned int i_nchildren;
934     unsigned int i_control_height, i_control_width;
935
936     if( p_plugin->b_toolbar )
937     {
938         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
939     }
940     else
941     {
942         i_control_height = i_control_width = 0;
943     }
944
945 #ifdef X11_RESIZE_DEBUG
946     XWindowAttributes attr;
947
948     if( event && event->type == ConfigureNotify )
949     {
950         fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
951                  "send_event ? %s\n", event->xconfigure.width,
952                  event->xconfigure.height,
953                  event->xconfigure.send_event ? "TRUE" : "FALSE" );
954     }
955 #endif /* X11_RESIZE_DEBUG */
956
957     if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
958     {
959         /* size already set */
960         return;
961     }
962
963     i_ret = XResizeWindow( p_display, drawable,
964                            window.width, (window.height - i_control_height) );
965
966 #ifdef X11_RESIZE_DEBUG
967     fprintf( stderr,
968              "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );
969
970     XGetWindowAttributes ( p_display, drawable, &attr );
971
972     /* X is asynchronous, so the current size reported here is not
973        necessarily the requested size as the Resize request may not
974        yet have been handled by the plugin host */
975     fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
976              attr.width, attr.height );
977 #endif /* X11_RESIZE_DEBUG */
978
979     XQueryTree( p_display, drawable,
980                 &root_return, &parent_return, &children_return,
981                 &i_nchildren );
982
983     if( i_nchildren > 0 )
984     {
985         /* XXX: Make assumptions related to the window parenting structure in
986            vlc/modules/video_output/x11/xcommon.c */
987         base_window = children_return[i_nchildren - 1];
988
989 #ifdef X11_RESIZE_DEBUG
990         fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
991         fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
992                  base_window );
993 #endif /* X11_RESIZE_DEBUG */
994
995         i_ret = XResizeWindow( p_display, base_window,
996                 window.width, ( window.height - i_control_height ) );
997
998 #ifdef X11_RESIZE_DEBUG
999         fprintf( stderr,
1000                  "vlcshell::Resize() XResizeWindow(base) returned %d\n",
1001                  i_ret );
1002
1003         XGetWindowAttributes( p_display, base_window, &attr );
1004
1005         fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
1006                  attr.width, attr.height );
1007 #endif /* X11_RESIZE_DEBUG */
1008     }
1009 }
1010
1011 #endif /* XP_UNIX */