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