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