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