]> git.sesse.net Git - vlc/blob - modules/video_output/x11/xcommon.c
* modules/video_output/x11/xcommon.c: support changing aspect-ratio on-the-fly.
[vlc] / modules / video_output / x11 / xcommon.c
1 /*****************************************************************************
2  * xcommon.c: Functions common to the X11 and XVideo plugins
3  *****************************************************************************
4  * Copyright (C) 1998-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Sam Hocevar <sam@zoy.org>
9  *          David Kennedy <dkennedy@tinytoad.com>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <string.h>                                            /* strerror() */
33
34 #include <vlc/vlc.h>
35 #include <vlc/intf.h>
36 #include <vlc/vout.h>
37 #include <vlc_keys.h>
38
39 #ifdef HAVE_MACHINE_PARAM_H
40     /* BSD */
41 #   include <machine/param.h>
42 #   include <sys/types.h>                                  /* typedef ushort */
43 #   include <sys/ipc.h>
44 #endif
45
46 #ifndef WIN32
47 #   include <netinet/in.h>                            /* BSD: struct in_addr */
48 #endif
49
50 #ifdef HAVE_SYS_SHM_H
51 #   include <sys/shm.h>                                /* shmget(), shmctl() */
52 #endif
53
54 #include <X11/Xlib.h>
55 #include <X11/Xproto.h>
56 #include <X11/Xmd.h>
57 #include <X11/Xutil.h>
58 #include <X11/keysym.h>
59 #ifdef HAVE_SYS_SHM_H
60 #   include <X11/extensions/XShm.h>
61 #endif
62 #ifdef DPMSINFO_IN_DPMS_H
63 #   include <X11/extensions/dpms.h>
64 #endif
65
66 #ifdef MODULE_NAME_IS_xvideo
67 #   include <X11/extensions/Xv.h>
68 #   include <X11/extensions/Xvlib.h>
69 #endif
70
71 #ifdef MODULE_NAME_IS_glx
72 #   include <GL/glx.h>
73 #endif
74
75 #ifdef HAVE_XINERAMA
76 #   include <X11/extensions/Xinerama.h>
77 #endif
78
79 #include "xcommon.h"
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 int  E_(Activate)   ( vlc_object_t * );
85 void E_(Deactivate) ( vlc_object_t * );
86
87 static int  InitVideo      ( vout_thread_t * );
88 static void EndVideo       ( vout_thread_t * );
89 static void DisplayVideo   ( vout_thread_t *, picture_t * );
90 static int  ManageVideo    ( vout_thread_t * );
91 static int  Control        ( vout_thread_t *, int, va_list );
92
93 static int  InitDisplay    ( vout_thread_t * );
94
95 static int  CreateWindow   ( vout_thread_t *, x11_window_t * );
96 static void DestroyWindow  ( vout_thread_t *, x11_window_t * );
97
98 static int  NewPicture     ( vout_thread_t *, picture_t * );
99 static void FreePicture    ( vout_thread_t *, picture_t * );
100
101 static IMAGE_TYPE *CreateImage    ( vout_thread_t *,
102                                     Display *, EXTRA_ARGS, int, int );
103 #ifdef HAVE_SYS_SHM_H
104 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
105                                     Display *, EXTRA_ARGS_SHM, int, int );
106 static vlc_bool_t b_shm = VLC_TRUE;
107 #endif
108
109 static void ToggleFullScreen      ( vout_thread_t * );
110
111 static void EnableXScreenSaver    ( vout_thread_t * );
112 static void DisableXScreenSaver   ( vout_thread_t * );
113
114 static void CreateCursor   ( vout_thread_t * );
115 static void DestroyCursor  ( vout_thread_t * );
116 static void ToggleCursor   ( vout_thread_t * );
117
118 #ifdef MODULE_NAME_IS_xvideo
119 static int  XVideoGetPort    ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
120 static void XVideoReleasePort( vout_thread_t *, int );
121 #endif
122
123 #ifdef MODULE_NAME_IS_x11
124 static void SetPalette     ( vout_thread_t *,
125                              uint16_t *, uint16_t *, uint16_t * );
126 #endif
127
128 static void TestNetWMSupport( vout_thread_t * );
129 static int ConvertKey( int );
130
131 static int WindowOnTop( vout_thread_t *, vlc_bool_t );
132
133 static int X11ErrorHandler( Display *, XErrorEvent * );
134
135 /*****************************************************************************
136  * Activate: allocate X11 video thread output method
137  *****************************************************************************
138  * This function allocate and initialize a X11 vout method. It uses some of the
139  * vout properties to choose the window size, and change them according to the
140  * actual properties of the display.
141  *****************************************************************************/
142 int E_(Activate) ( vlc_object_t *p_this )
143 {
144     vout_thread_t *p_vout = (vout_thread_t *)p_this;
145     char *        psz_display;
146     vlc_value_t   val;
147
148 #ifdef MODULE_NAME_IS_xvideo
149     char *       psz_chroma;
150     vlc_fourcc_t i_chroma = 0;
151     vlc_bool_t   b_chroma = 0;
152 #endif
153
154     p_vout->pf_init = InitVideo;
155     p_vout->pf_end = EndVideo;
156     p_vout->pf_manage = ManageVideo;
157     p_vout->pf_render = NULL;
158     p_vout->pf_display = DisplayVideo;
159     p_vout->pf_control = Control;
160
161     /* Allocate structure */
162     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
163     if( p_vout->p_sys == NULL )
164     {
165         msg_Err( p_vout, "out of memory" );
166         return VLC_ENOMEM;
167     }
168
169     vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
170
171     /* Open display, using the "display" config variable or the DISPLAY
172      * environment variable */
173     psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
174
175     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
176
177     if( p_vout->p_sys->p_display == NULL )                          /* error */
178     {
179         msg_Err( p_vout, "cannot open display %s",
180                          XDisplayName( psz_display ) );
181         free( p_vout->p_sys );
182         if( psz_display ) free( psz_display );
183         return VLC_EGENERIC;
184     }
185     if( psz_display ) free( psz_display );
186
187     /* Replace error handler so we can intercept some non-fatal errors */
188     XSetErrorHandler( X11ErrorHandler );
189
190     /* Get a screen ID matching the XOpenDisplay return value */
191     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
192
193 #ifdef MODULE_NAME_IS_xvideo
194     psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
195     if( psz_chroma )
196     {
197         if( strlen( psz_chroma ) >= 4 )
198         {
199             /* Do not use direct assignment because we are not sure of the
200              * alignment. */
201             memcpy(&i_chroma, psz_chroma, 4);
202             b_chroma = 1;
203         }
204
205         free( psz_chroma );
206     }
207
208     if( b_chroma )
209     {
210         msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
211                  i_chroma, (char*)&i_chroma );
212     }
213     else
214     {
215         i_chroma = p_vout->render.i_chroma;
216     }
217
218     /* Check that we have access to an XVideo port providing this chroma */
219     p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
220                                              &p_vout->output.i_chroma );
221     if( p_vout->p_sys->i_xvport < 0 )
222     {
223         /* If a specific chroma format was requested, then we don't try to
224          * be cleverer than the user. He knew pretty well what he wanted. */
225         if( b_chroma )
226         {
227             XCloseDisplay( p_vout->p_sys->p_display );
228             free( p_vout->p_sys );
229             return VLC_EGENERIC;
230         }
231
232         /* It failed, but it's not completely lost ! We try to open an
233          * XVideo port for an YUY2 picture. We'll need to do an YUV
234          * conversion, but at least it has got scaling. */
235         p_vout->p_sys->i_xvport =
236                         XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
237                                                &p_vout->output.i_chroma );
238         if( p_vout->p_sys->i_xvport < 0 )
239         {
240             /* It failed, but it's not completely lost ! We try to open an
241              * XVideo port for a simple 16bpp RGB picture. We'll need to do
242              * an YUV conversion, but at least it has got scaling. */
243             p_vout->p_sys->i_xvport =
244                             XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
245                                                    &p_vout->output.i_chroma );
246             if( p_vout->p_sys->i_xvport < 0 )
247             {
248                 XCloseDisplay( p_vout->p_sys->p_display );
249                 free( p_vout->p_sys );
250                 return VLC_EGENERIC;
251             }
252         }
253     }
254     p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
255 #endif
256
257     /* Create blank cursor (for mouse cursor autohiding) */
258     p_vout->p_sys->i_time_mouse_last_moved = mdate();
259     p_vout->p_sys->b_mouse_pointer_visible = 1;
260     CreateCursor( p_vout );
261
262     /* Set main window's size */
263     p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
264     p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
265     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
266     /* Spawn base window - this window will include the video output window,
267      * but also command buttons, subtitles and other indicators */
268     if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
269     {
270         msg_Err( p_vout, "cannot create X11 window" );
271         DestroyCursor( p_vout );
272         XCloseDisplay( p_vout->p_sys->p_display );
273         free( p_vout->p_sys );
274         return VLC_EGENERIC;
275     }
276
277     /* Open and initialize device. */
278     if( InitDisplay( p_vout ) )
279     {
280         msg_Err( p_vout, "cannot initialize X11 display" );
281         DestroyCursor( p_vout );
282         DestroyWindow( p_vout, &p_vout->p_sys->original_window );
283         XCloseDisplay( p_vout->p_sys->p_display );
284         free( p_vout->p_sys );
285         return VLC_EGENERIC;
286     }
287
288     /* Disable screen saver */
289     DisableXScreenSaver( p_vout );
290
291     /* Misc init */
292     p_vout->p_sys->b_altfullscreen = 0;
293     p_vout->p_sys->i_time_button_last_pressed = 0;
294
295     TestNetWMSupport( p_vout );
296
297     /* Variable to indicate if the window should be on top of others */
298     /* Trigger a callback right now */
299     var_Get( p_vout, "video-on-top", &val );
300     var_Set( p_vout, "video-on-top", val );
301
302     return VLC_SUCCESS;
303 }
304
305 /*****************************************************************************
306  * Deactivate: destroy X11 video thread output method
307  *****************************************************************************
308  * Terminate an output method created by Open
309  *****************************************************************************/
310 void E_(Deactivate) ( vlc_object_t *p_this )
311 {
312     vout_thread_t *p_vout = (vout_thread_t *)p_this;
313
314     /* If the fullscreen window is still open, close it */
315     if( p_vout->b_fullscreen )
316     {
317         ToggleFullScreen( p_vout );
318     }
319
320     /* Restore cursor if it was blanked */
321     if( !p_vout->p_sys->b_mouse_pointer_visible )
322     {
323         ToggleCursor( p_vout );
324     }
325
326 #ifdef MODULE_NAME_IS_x11
327     /* Destroy colormap */
328     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
329     {
330         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
331     }
332 #elif defined(MODULE_NAME_IS_xvideo)
333     XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
334 #endif
335
336     DestroyCursor( p_vout );
337     EnableXScreenSaver( p_vout );
338     DestroyWindow( p_vout, &p_vout->p_sys->original_window );
339
340     XCloseDisplay( p_vout->p_sys->p_display );
341
342     /* Destroy structure */
343     vlc_mutex_destroy( &p_vout->p_sys->lock );
344     free( p_vout->p_sys );
345 }
346
347 /*****************************************************************************
348  * InitVideo: initialize X11 video thread output method
349  *****************************************************************************
350  * This function create the XImages needed by the output thread. It is called
351  * at the beginning of the thread, but also each time the window is resized.
352  *****************************************************************************/
353 static int InitVideo( vout_thread_t *p_vout )
354 {
355     int i_index;
356     picture_t *p_pic;
357
358     I_OUTPUTPICTURES = 0;
359
360 #ifdef MODULE_NAME_IS_xvideo
361     /* Initialize the output structure; we already found an XVideo port,
362      * and the corresponding chroma we will be using. Since we can
363      * arbitrary scale, stick to the coordinates and aspect. */
364     p_vout->output.i_width  = p_vout->render.i_width;
365     p_vout->output.i_height = p_vout->render.i_height;
366     p_vout->output.i_aspect = p_vout->render.i_aspect;
367
368     p_vout->fmt_out = p_vout->fmt_in;
369     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
370
371     switch( p_vout->output.i_chroma )
372     {
373         case VLC_FOURCC('R','V','1','5'):
374             p_vout->output.i_rmask = 0x001f;
375             p_vout->output.i_gmask = 0x07e0;
376             p_vout->output.i_bmask = 0xf800;
377             break;
378         case VLC_FOURCC('R','V','1','6'):
379             p_vout->output.i_rmask = 0x001f;
380             p_vout->output.i_gmask = 0x03e0;
381             p_vout->output.i_bmask = 0x7c00;
382             break;
383     }
384
385 #elif defined(MODULE_NAME_IS_x11)
386     /* Initialize the output structure: RGB with square pixels, whatever
387      * the input format is, since it's the only format we know */
388     switch( p_vout->p_sys->i_screen_depth )
389     {
390         case 8: /* FIXME: set the palette */
391             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
392         case 15:
393             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
394         case 16:
395             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
396         case 24:
397         case 32:
398             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
399         default:
400             msg_Err( p_vout, "unknown screen depth %i",
401                      p_vout->p_sys->i_screen_depth );
402             return VLC_SUCCESS;
403     }
404
405     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
406                        p_vout->p_sys->p_win->i_height,
407                        &i_index, &i_index,
408                        &p_vout->output.i_width, &p_vout->output.i_height );
409
410     /* Assume we have square pixels */
411     p_vout->output.i_aspect = p_vout->output.i_width
412                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
413 #endif
414
415     /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
416     while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
417     {
418         p_pic = NULL;
419
420         /* Find an empty picture slot */
421         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
422         {
423           if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
424             {
425                 p_pic = p_vout->p_picture + i_index;
426                 break;
427             }
428         }
429
430         /* Allocate the picture */
431         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
432         {
433             break;
434         }
435
436         p_pic->i_status = DESTROYED_PICTURE;
437         p_pic->i_type   = DIRECT_PICTURE;
438
439         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
440
441         I_OUTPUTPICTURES++;
442     }
443
444     if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
445     {
446         /* U and V inverted compared to I420
447          * Fixme: this should be handled by the vout core */
448         p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
449         p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
450     }
451
452     return VLC_SUCCESS;
453 }
454
455 /*****************************************************************************
456  * DisplayVideo: displays previously rendered output
457  *****************************************************************************
458  * This function sends the currently rendered image to X11 server.
459  * (The Xv extension takes care of "double-buffering".)
460  *****************************************************************************/
461 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
462 {
463     int i_width, i_height, i_x, i_y;
464
465     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
466                        p_vout->p_sys->p_win->i_height,
467                        &i_x, &i_y, &i_width, &i_height );
468
469     vlc_mutex_lock( &p_vout->p_sys->lock );
470
471 #ifdef HAVE_SYS_SHM_H
472     if( p_vout->p_sys->b_shm )
473     {
474         /* Display rendered image using shared memory extension */
475 #   ifdef MODULE_NAME_IS_xvideo
476         XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
477                        p_vout->p_sys->p_win->video_window,
478                        p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
479                        p_vout->fmt_out.i_x_offset,
480                        p_vout->fmt_out.i_y_offset,
481                        p_vout->fmt_out.i_visible_width,
482                        p_vout->fmt_out.i_visible_height,
483                        0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
484                        False /* Don't put True here or you'll waste your CPU */ );
485 #   else
486         XShmPutImage( p_vout->p_sys->p_display,
487                       p_vout->p_sys->p_win->video_window,
488                       p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
489                       0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
490                       p_vout->output.i_width, p_vout->output.i_height,
491                       False /* Don't put True here ! */ );
492 #   endif
493     }
494     else
495 #endif /* HAVE_SYS_SHM_H */
496     {
497         /* Use standard XPutImage -- this is gonna be slow ! */
498 #ifdef MODULE_NAME_IS_xvideo
499         XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
500                     p_vout->p_sys->p_win->video_window,
501                     p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
502                     p_vout->fmt_out.i_x_offset,
503                     p_vout->fmt_out.i_y_offset,
504                     p_vout->fmt_out.i_visible_width,
505                     p_vout->fmt_out.i_visible_height,
506                     0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
507 #else
508         XPutImage( p_vout->p_sys->p_display,
509                    p_vout->p_sys->p_win->video_window,
510                    p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
511                    0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
512                    p_vout->output.i_width, p_vout->output.i_height );
513 #endif
514     }
515
516     /* Make sure the command is sent now - do NOT use XFlush !*/
517     XSync( p_vout->p_sys->p_display, False );
518
519     vlc_mutex_unlock( &p_vout->p_sys->lock );
520 }
521
522 /*****************************************************************************
523  * ManageVideo: handle X11 events
524  *****************************************************************************
525  * This function should be called regularly by video output thread. It manages
526  * X11 events and allows window resizing. It returns a non null value on
527  * error.
528  *****************************************************************************/
529 static int ManageVideo( vout_thread_t *p_vout )
530 {
531     XEvent      xevent;                                         /* X11 event */
532     vlc_value_t val;
533
534     vlc_mutex_lock( &p_vout->p_sys->lock );
535
536     /* Handle events from the owner window */
537     if( p_vout->p_sys->p_win->owner_window )
538     {
539         while( XCheckWindowEvent( p_vout->p_sys->p_display,
540                                   p_vout->p_sys->p_win->owner_window,
541                                   StructureNotifyMask, &xevent ) == True )
542         {
543             /* ConfigureNotify event: prepare  */
544             if( xevent.type == ConfigureNotify )
545             {
546                 /* Update dimensions */
547                 XResizeWindow( p_vout->p_sys->p_display,
548                                p_vout->p_sys->p_win->base_window,
549                                xevent.xconfigure.width,
550                                xevent.xconfigure.height );
551             }
552         }
553     }
554
555     /* Handle X11 events: ConfigureNotify events are parsed to know if the
556      * output window's size changed, MapNotify and UnmapNotify to know if the
557      * window is mapped (and if the display is useful), and ClientMessages
558      * to intercept window destruction requests */
559
560     while( XCheckWindowEvent( p_vout->p_sys->p_display,
561                               p_vout->p_sys->p_win->base_window,
562                               StructureNotifyMask | KeyPressMask |
563                               ButtonPressMask | ButtonReleaseMask |
564                               PointerMotionMask | Button1MotionMask , &xevent )
565            == True )
566     {
567         /* ConfigureNotify event: prepare  */
568         if( xevent.type == ConfigureNotify )
569         {
570             if( (unsigned int)xevent.xconfigure.width
571                    != p_vout->p_sys->p_win->i_width
572               || (unsigned int)xevent.xconfigure.height
573                     != p_vout->p_sys->p_win->i_height )
574             {
575                 /* Update dimensions */
576                 p_vout->i_changes |= VOUT_SIZE_CHANGE;
577                 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
578                 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
579             }
580         }
581         /* Keyboard event */
582         else if( xevent.type == KeyPress )
583         {
584             unsigned int state = xevent.xkey.state;
585             KeySym x_key_symbol;
586             char i_key;                                   /* ISO Latin-1 key */
587
588             /* We may have keys like F1 trough F12, ESC ... */
589             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
590                                              xevent.xkey.keycode, 0 );
591             val.i_int = ConvertKey( (int)x_key_symbol );
592
593             xevent.xkey.state &= ~ShiftMask;
594             xevent.xkey.state &= ~ControlMask;
595             xevent.xkey.state &= ~Mod1Mask;
596
597             if( !val.i_int &&
598                 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
599             {
600                 /* "Normal Keys"
601                  * The reason why I use this instead of XK_0 is that
602                  * with XLookupString, we don't have to care about
603                  * keymaps. */
604                 val.i_int = i_key;
605             }
606
607             if( val.i_int )
608             {
609                 if( state & ShiftMask )
610                 {
611                     val.i_int |= KEY_MODIFIER_SHIFT;
612                 }
613                 if( state & ControlMask )
614                 {
615                     val.i_int |= KEY_MODIFIER_CTRL;
616                 }
617                 if( state & Mod1Mask )
618                 {
619                     val.i_int |= KEY_MODIFIER_ALT;
620                 }
621                 var_Set( p_vout->p_vlc, "key-pressed", val );
622             }
623         }
624         /* Mouse click */
625         else if( xevent.type == ButtonPress )
626         {
627             switch( ((XButtonEvent *)&xevent)->button )
628             {
629                 case Button1:
630                     var_Get( p_vout, "mouse-button-down", &val );
631                     val.i_int |= 1;
632                     var_Set( p_vout, "mouse-button-down", val );
633
634                     /* detect double-clicks */
635                     if( ( ((XButtonEvent *)&xevent)->time -
636                           p_vout->p_sys->i_time_button_last_pressed ) < 300 )
637                     {
638                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
639                     }
640
641                     p_vout->p_sys->i_time_button_last_pressed =
642                         ((XButtonEvent *)&xevent)->time;
643                     break;
644                 case Button2:
645                     var_Get( p_vout, "mouse-button-down", &val );
646                     val.i_int |= 2;
647                     var_Set( p_vout, "mouse-button-down", val );
648                     break;
649
650                 case Button3:
651                     var_Get( p_vout, "mouse-button-down", &val );
652                     val.i_int |= 4;
653                     var_Set( p_vout, "mouse-button-down", val );
654                     break;
655
656                 case Button4:
657                     var_Get( p_vout, "mouse-button-down", &val );
658                     val.i_int |= 8;
659                     var_Set( p_vout, "mouse-button-down", val );
660                     break;
661
662                 case Button5:
663                     var_Get( p_vout, "mouse-button-down", &val );
664                     val.i_int |= 16;
665                     var_Set( p_vout, "mouse-button-down", val );
666                     break;
667             }
668         }
669         /* Mouse release */
670         else if( xevent.type == ButtonRelease )
671         {
672             switch( ((XButtonEvent *)&xevent)->button )
673             {
674                 case Button1:
675                     var_Get( p_vout, "mouse-button-down", &val );
676                     val.i_int &= ~1;
677                     var_Set( p_vout, "mouse-button-down", val );
678
679                     val.b_bool = VLC_TRUE;
680                     var_Set( p_vout, "mouse-clicked", val );
681                     break;
682
683                 case Button2:
684                     {
685                         playlist_t *p_playlist;
686
687                         var_Get( p_vout, "mouse-button-down", &val );
688                         val.i_int &= ~2;
689                         var_Set( p_vout, "mouse-button-down", val );
690
691                         p_playlist = vlc_object_find( p_vout,
692                                                       VLC_OBJECT_PLAYLIST,
693                                                       FIND_ANYWHERE );
694                         if( p_playlist != NULL )
695                         {
696                             vlc_value_t val;
697                             var_Get( p_playlist, "intf-show", &val );
698                             val.b_bool = !val.b_bool;
699                             var_Set( p_playlist, "intf-show", val );
700                             vlc_object_release( p_playlist );
701                         }
702                     }
703                     break;
704
705                 case Button3:
706                     {
707                         intf_thread_t *p_intf;
708                         playlist_t *p_playlist;
709
710                         var_Get( p_vout, "mouse-button-down", &val );
711                         val.i_int &= ~4;
712                         var_Set( p_vout, "mouse-button-down", val );
713                         p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
714                                                           FIND_ANYWHERE );
715                         if( p_intf )
716                         {
717                             p_intf->b_menu_change = 1;
718                             vlc_object_release( p_intf );
719                         }
720
721                         p_playlist = vlc_object_find( p_vout,
722                                                       VLC_OBJECT_PLAYLIST,
723                                                       FIND_ANYWHERE );
724                         if( p_playlist != NULL )
725                         {
726                             vlc_value_t val; val.b_bool = VLC_TRUE;
727                             var_Set( p_playlist, "intf-popupmenu", val );
728                             vlc_object_release( p_playlist );
729                         }
730                     }
731                     break;
732
733                 case Button4:
734                     var_Get( p_vout, "mouse-button-down", &val );
735                     val.i_int &= ~8;
736                     var_Set( p_vout, "mouse-button-down", val );
737                     break;
738
739                 case Button5:
740                     var_Get( p_vout, "mouse-button-down", &val );
741                     val.i_int &= ~16;
742                     var_Set( p_vout, "mouse-button-down", val );
743                     break;
744
745             }
746         }
747         /* Mouse move */
748         else if( xevent.type == MotionNotify )
749         {
750             int i_width, i_height, i_x, i_y;
751             vlc_value_t val;
752
753             /* somewhat different use for vout_PlacePicture:
754              * here the values are needed to give to mouse coordinates
755              * in the original picture space */
756             vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
757                                p_vout->p_sys->p_win->i_height,
758                                &i_x, &i_y, &i_width, &i_height );
759
760             val.i_int = ( xevent.xmotion.x - i_x )
761                          * p_vout->render.i_width / i_width;
762             var_Set( p_vout, "mouse-x", val );
763             val.i_int = ( xevent.xmotion.y - i_y )
764                          * p_vout->render.i_height / i_height;
765             var_Set( p_vout, "mouse-y", val );
766
767             val.b_bool = VLC_TRUE;
768             var_Set( p_vout, "mouse-moved", val );
769
770             p_vout->p_sys->i_time_mouse_last_moved = mdate();
771             if( ! p_vout->p_sys->b_mouse_pointer_visible )
772             {
773                 ToggleCursor( p_vout );
774             }
775         }
776         else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
777                   || xevent.type == MapNotify
778                   || xevent.type == UnmapNotify )
779         {
780             /* Ignore these events */
781         }
782         else /* Other events */
783         {
784             msg_Warn( p_vout, "unhandled event %d received", xevent.type );
785         }
786     }
787
788     /* Handle events for video output sub-window */
789     while( XCheckWindowEvent( p_vout->p_sys->p_display,
790                               p_vout->p_sys->p_win->video_window,
791                               ExposureMask, &xevent ) == True )
792     {
793         /* Window exposed (only handled if stream playback is paused) */
794         if( xevent.type == Expose )
795         {
796             if( ((XExposeEvent *)&xevent)->count == 0 )
797             {
798                 /* (if this is the last a collection of expose events...) */
799 #if 0
800                 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
801                 {
802                     if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
803                                                    ->stream.control.i_status )
804                     {
805                         /* XVideoDisplay( p_vout )*/;
806                     }
807                 }
808 #endif
809             }
810         }
811     }
812
813     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
814      * are handled - according to the man pages, the format is always 32
815      * in this case */
816     while( XCheckTypedEvent( p_vout->p_sys->p_display,
817                              ClientMessage, &xevent ) )
818     {
819         if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
820                && ((Atom)xevent.xclient.data.l[0]
821                      == p_vout->p_sys->p_win->wm_delete_window ) )
822         {
823             /* the user wants to close the window */
824             playlist_t * p_playlist =
825                 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
826                                                FIND_ANYWHERE );
827             if( p_playlist != NULL )
828             {
829                 playlist_Stop( p_playlist );
830                 vlc_object_release( p_playlist );
831             }
832         }
833     }
834
835     /*
836      * Fullscreen Change
837      */
838     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
839     {
840         vlc_value_t val;
841
842         /* Update the object variable and trigger callback */
843         val.b_bool = !p_vout->b_fullscreen;
844         var_Set( p_vout, "fullscreen", val );
845
846         ToggleFullScreen( p_vout );
847         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
848     }
849
850 #ifndef MODULE_NAME_IS_x11
851     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
852         p_vout->i_changes & VOUT_ASPECT_CHANGE )
853     {
854         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
855         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
856
857         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
858         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
859         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
860         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
861         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
862         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
863         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
864         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
865         p_vout->i_changes |= VOUT_SIZE_CHANGE;
866     }
867 #endif
868
869     /*
870      * Size change
871      *
872      * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
873      *  the size flag inside the fullscreen routine)
874      */
875     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
876     {
877         int i_width, i_height, i_x, i_y;
878
879         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
880
881 #ifdef MODULE_NAME_IS_x11
882         /* We need to signal the vout thread about the size change because it
883          * is doing the rescaling */
884         p_vout->i_changes |= VOUT_SIZE_CHANGE;
885 #endif
886
887         vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
888                            p_vout->p_sys->p_win->i_height,
889                            &i_x, &i_y, &i_width, &i_height );
890
891         XMoveResizeWindow( p_vout->p_sys->p_display,
892                            p_vout->p_sys->p_win->video_window,
893                            i_x, i_y, i_width, i_height );
894     }
895
896     /* Autohide Cursour */
897     if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
898     {
899         /* Hide the mouse automatically */
900         if( p_vout->p_sys->b_mouse_pointer_visible )
901         {
902             ToggleCursor( p_vout );
903         }
904     }
905
906     vlc_mutex_unlock( &p_vout->p_sys->lock );
907
908     return 0;
909 }
910
911 /*****************************************************************************
912  * EndVideo: terminate X11 video thread output method
913  *****************************************************************************
914  * Destroy the X11 XImages created by Init. It is called at the end of
915  * the thread, but also each time the window is resized.
916  *****************************************************************************/
917 static void EndVideo( vout_thread_t *p_vout )
918 {
919     int i_index;
920
921     /* Free the direct buffers we allocated */
922     for( i_index = I_OUTPUTPICTURES ; i_index ; )
923     {
924         i_index--;
925         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
926     }
927 }
928
929 /* following functions are local */
930
931 /*****************************************************************************
932  * CreateWindow: open and set-up X11 main window
933  *****************************************************************************/
934 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
935 {
936     XSizeHints              xsize_hints;
937     XSetWindowAttributes    xwindow_attributes;
938     XGCValues               xgcvalues;
939     XEvent                  xevent;
940
941     vlc_bool_t              b_expose = VLC_FALSE;
942     vlc_bool_t              b_configure_notify = VLC_FALSE;
943     vlc_bool_t              b_map_notify = VLC_FALSE;
944     vlc_value_t             val;
945
946     /* Prepare window manager hints and properties */
947     p_win->wm_protocols =
948              XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
949     p_win->wm_delete_window =
950              XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
951
952     /* Never have a 0-pixel-wide window */
953     xsize_hints.min_width = 2;
954     xsize_hints.min_height = 1;
955
956     /* Prepare window attributes */
957     xwindow_attributes.backing_store = Always;       /* save the hidden part */
958     xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
959                                                      p_vout->p_sys->i_screen);
960     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
961
962     if( !p_vout->b_fullscreen )
963     {
964         p_win->owner_window =
965             (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
966                                         &p_win->i_width, &p_win->i_height );
967
968         xsize_hints.base_width  = xsize_hints.width = p_win->i_width;
969         xsize_hints.base_height = xsize_hints.height = p_win->i_height;
970         xsize_hints.flags       = PSize | PMinSize;
971
972         if( p_win->i_x >=0 || p_win->i_y >= 0 )
973         {
974             xsize_hints.x = p_win->i_x;
975             xsize_hints.y = p_win->i_y;
976             xsize_hints.flags |= PPosition;
977         }
978     }
979     else
980     {
981         /* Fullscreen window size and position */
982         p_win->owner_window = 0;
983         p_win->i_x = p_win->i_y = 0;
984         p_win->i_width =
985             DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
986         p_win->i_height =
987             DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
988     }
989
990     if( !p_win->owner_window )
991     {
992         /* Create the window and set hints - the window must receive
993          * ConfigureNotify events, and until it is displayed, Expose and
994          * MapNotify events. */
995
996         p_win->base_window =
997             XCreateWindow( p_vout->p_sys->p_display,
998                            DefaultRootWindow( p_vout->p_sys->p_display ),
999                            p_win->i_x, p_win->i_y,
1000                            p_win->i_width, p_win->i_height,
1001                            0,
1002                            0, InputOutput, 0,
1003                            CWBackingStore | CWBackPixel | CWEventMask,
1004                            &xwindow_attributes );
1005
1006         if( !p_vout->b_fullscreen )
1007         {
1008             /* Set window manager hints and properties: size hints, command,
1009              * window's name, and accepted protocols */
1010             XSetWMNormalHints( p_vout->p_sys->p_display,
1011                                p_win->base_window, &xsize_hints );
1012             XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1013                          p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
1014
1015             if( !var_GetBool( p_vout, "video-deco") )
1016             {
1017                 Atom prop;
1018                 mwmhints_t mwmhints;
1019
1020                 mwmhints.flags = MWM_HINTS_DECORATIONS;
1021                 mwmhints.decorations = False;
1022
1023                 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1024                                     False );
1025
1026                 XChangeProperty( p_vout->p_sys->p_display,
1027                                  p_win->base_window,
1028                                  prop, prop, 32, PropModeReplace,
1029                                  (unsigned char *)&mwmhints,
1030                                  PROP_MWM_HINTS_ELEMENTS );
1031             }
1032             else
1033             {
1034                  var_Get( p_vout, "video-title", &val );
1035                  if( !val.psz_string || !*val.psz_string )
1036                  {
1037                     XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1038 #ifdef MODULE_NAME_IS_x11
1039                                 VOUT_TITLE " (X11 output)"
1040 #elif defined(MODULE_NAME_IS_glx)
1041                                 VOUT_TITLE " (GLX output)"
1042 #else
1043                                 VOUT_TITLE " (XVideo output)"
1044 #endif
1045                       );
1046                 }
1047                 else
1048                 {
1049                     XStoreName( p_vout->p_sys->p_display,
1050                                p_win->base_window, val.psz_string );
1051                 }
1052             }
1053         }
1054     }
1055     else
1056     {
1057         Window dummy1;
1058         unsigned int dummy2, dummy3;
1059
1060         /* Select events we are interested in. */
1061         XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1062                       StructureNotifyMask );
1063
1064         /* Get the parent window's geometry information */
1065         XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1066                       &dummy1, &dummy2, &dummy3,
1067                       &p_win->i_width,
1068                       &p_win->i_height,
1069                       &dummy2, &dummy3 );
1070
1071         /* We are already configured */
1072         b_configure_notify = VLC_TRUE;
1073
1074         /* From man XSelectInput: only one client at a time can select a
1075          * ButtonPress event, so we need to open a new window anyway. */
1076         p_win->base_window =
1077             XCreateWindow( p_vout->p_sys->p_display,
1078                            p_win->owner_window,
1079                            0, 0,
1080                            p_win->i_width, p_win->i_height,
1081                            0,
1082                            0, CopyFromParent, 0,
1083                            CWBackingStore | CWBackPixel | CWEventMask,
1084                            &xwindow_attributes );
1085     }
1086
1087     if( (p_win->wm_protocols == None)        /* use WM_DELETE_WINDOW */
1088         || (p_win->wm_delete_window == None)
1089         || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1090                              &p_win->wm_delete_window, 1 ) )
1091     {
1092         /* WM_DELETE_WINDOW is not supported by window manager */
1093         msg_Warn( p_vout, "missing or bad window manager" );
1094     }
1095
1096     /* Creation of a graphic context that doesn't generate a GraphicsExpose
1097      * event when using functions like XCopyArea */
1098     xgcvalues.graphics_exposures = False;
1099     p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1100                            p_win->base_window,
1101                            GCGraphicsExposures, &xgcvalues );
1102
1103     /* Send orders to server, and wait until window is displayed - three
1104      * events must be received: a MapNotify event, an Expose event allowing
1105      * drawing in the window, and a ConfigureNotify to get the window
1106      * dimensions. Once those events have been received, only
1107      * ConfigureNotify events need to be received. */
1108     XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1109     do
1110     {
1111         XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1112                       SubstructureNotifyMask | StructureNotifyMask |
1113                       ExposureMask, &xevent);
1114         if( (xevent.type == Expose)
1115             && (xevent.xexpose.window == p_win->base_window) )
1116         {
1117             b_expose = VLC_TRUE;
1118             /* ConfigureNotify isn't sent if there isn't a window manager.
1119              * Expose should be the last event to be received so it should
1120              * be fine to assume we won't receive it anymore. */
1121             b_configure_notify = VLC_TRUE;
1122         }
1123         else if( (xevent.type == MapNotify)
1124                  && (xevent.xmap.window == p_win->base_window) )
1125         {
1126             b_map_notify = VLC_TRUE;
1127         }
1128         else if( (xevent.type == ConfigureNotify)
1129                  && (xevent.xconfigure.window == p_win->base_window) )
1130         {
1131             b_configure_notify = VLC_TRUE;
1132             p_win->i_width = xevent.xconfigure.width;
1133             p_win->i_height = xevent.xconfigure.height;
1134         }
1135     } while( !( b_expose && b_configure_notify && b_map_notify ) );
1136
1137     XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1138                   StructureNotifyMask | KeyPressMask |
1139                   ButtonPressMask | ButtonReleaseMask |
1140                   PointerMotionMask );
1141
1142 #ifdef MODULE_NAME_IS_x11
1143     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1144     {
1145         /* Allocate a new palette */
1146         p_vout->p_sys->colormap =
1147             XCreateColormap( p_vout->p_sys->p_display,
1148                              DefaultRootWindow( p_vout->p_sys->p_display ),
1149                              DefaultVisual( p_vout->p_sys->p_display,
1150                                             p_vout->p_sys->i_screen ),
1151                              AllocAll );
1152
1153         xwindow_attributes.colormap = p_vout->p_sys->colormap;
1154         XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1155                                  CWColormap, &xwindow_attributes );
1156     }
1157 #endif
1158
1159     /* Create video output sub-window. */
1160     p_win->video_window =  XCreateSimpleWindow(
1161                                       p_vout->p_sys->p_display,
1162                                       p_win->base_window, 0, 0,
1163                                       p_win->i_width, p_win->i_height,
1164                                       0,
1165                                       BlackPixel( p_vout->p_sys->p_display,
1166                                                   p_vout->p_sys->i_screen ),
1167                                       WhitePixel( p_vout->p_sys->p_display,
1168                                                   p_vout->p_sys->i_screen ) );
1169
1170     XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1171                           BlackPixel( p_vout->p_sys->p_display,
1172                                       p_vout->p_sys->i_screen ) );
1173
1174     XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1175     XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1176                   ExposureMask );
1177
1178     /* make sure the video window will be centered in the next ManageVideo() */
1179     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1180
1181     /* If the cursor was formerly blank than blank it again */
1182     if( !p_vout->p_sys->b_mouse_pointer_visible )
1183     {
1184         ToggleCursor( p_vout );
1185         ToggleCursor( p_vout );
1186     }
1187
1188     /* Do NOT use XFlush here ! */
1189     XSync( p_vout->p_sys->p_display, False );
1190
1191     /* At this stage, the window is open, displayed, and ready to
1192      * receive data */
1193     p_vout->p_sys->p_win = p_win;
1194
1195     return VLC_SUCCESS;
1196 }
1197
1198 /*****************************************************************************
1199  * DestroyWindow: destroy the window
1200  *****************************************************************************
1201  *
1202  *****************************************************************************/
1203 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1204 {
1205     /* Do NOT use XFlush here ! */
1206     XSync( p_vout->p_sys->p_display, False );
1207
1208     if( p_win->video_window != None )
1209         XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1210
1211     XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1212
1213     XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1214     XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1215
1216     if( p_win->owner_window )
1217         vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1218 }
1219
1220 /*****************************************************************************
1221  * NewPicture: allocate a picture
1222  *****************************************************************************
1223  * Returns 0 on success, -1 otherwise
1224  *****************************************************************************/
1225 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1226 {
1227 #ifndef MODULE_NAME_IS_glx
1228
1229 #ifdef MODULE_NAME_IS_xvideo
1230     int i_plane;
1231 #endif
1232
1233     /* We know the chroma, allocate a buffer which will be used
1234      * directly by the decoder */
1235     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1236
1237     if( p_pic->p_sys == NULL )
1238     {
1239         return -1;
1240     }
1241
1242     /* Fill in picture_t fields */
1243     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1244                       p_vout->output.i_width, p_vout->output.i_height,
1245                       p_vout->output.i_aspect );
1246
1247 #ifdef HAVE_SYS_SHM_H
1248     if( p_vout->p_sys->b_shm )
1249     {
1250         /* Create image using XShm extension */
1251         p_pic->p_sys->p_image =
1252             CreateShmImage( p_vout, p_vout->p_sys->p_display,
1253 #   ifdef MODULE_NAME_IS_xvideo
1254                             p_vout->p_sys->i_xvport, 
1255                             VLC2X11_FOURCC(p_vout->output.i_chroma),
1256 #   else
1257                             p_vout->p_sys->p_visual,
1258                             p_vout->p_sys->i_screen_depth,
1259 #   endif
1260                             &p_pic->p_sys->shminfo,
1261                             p_vout->output.i_width, p_vout->output.i_height );
1262     }
1263
1264     if( !p_vout->p_sys->b_shm || !p_pic->p_sys->p_image )
1265 #endif /* HAVE_SYS_SHM_H */
1266     {
1267         /* Create image without XShm extension */
1268         p_pic->p_sys->p_image =
1269             CreateImage( p_vout, p_vout->p_sys->p_display,
1270 #ifdef MODULE_NAME_IS_xvideo
1271                          p_vout->p_sys->i_xvport, 
1272                          VLC2X11_FOURCC(p_vout->output.i_chroma),
1273                          p_pic->format.i_bits_per_pixel,
1274 #else
1275                          p_vout->p_sys->p_visual,
1276                          p_vout->p_sys->i_screen_depth,
1277                          p_vout->p_sys->i_bytes_per_pixel,
1278 #endif
1279                          p_vout->output.i_width, p_vout->output.i_height );
1280
1281 #ifdef HAVE_SYS_SHM_H
1282         if( p_pic->p_sys->p_image && p_vout->p_sys->b_shm )
1283         {
1284             msg_Warn( p_vout, "couldn't create SHM image, disabling SHM." );
1285             p_vout->p_sys->b_shm = VLC_FALSE;
1286         }
1287 #endif /* HAVE_SYS_SHM_H */
1288     }
1289
1290     if( p_pic->p_sys->p_image == NULL )
1291     {
1292         free( p_pic->p_sys );
1293         return -1;
1294     }
1295
1296     switch( p_vout->output.i_chroma )
1297     {
1298 #ifdef MODULE_NAME_IS_xvideo
1299         case VLC_FOURCC('I','4','2','0'):
1300         case VLC_FOURCC('Y','V','1','2'):
1301         case VLC_FOURCC('Y','2','1','1'):
1302         case VLC_FOURCC('Y','U','Y','2'):
1303         case VLC_FOURCC('U','Y','V','Y'):
1304         case VLC_FOURCC('R','V','1','5'):
1305         case VLC_FOURCC('R','V','1','6'):
1306         case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1307         case VLC_FOURCC('R','V','3','2'):
1308
1309             for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1310                  i_plane++ )
1311             {
1312                 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1313                     + p_pic->p_sys->p_image->offsets[i_plane];
1314                 p_pic->p[i_plane].i_pitch =
1315                     p_pic->p_sys->p_image->pitches[i_plane];
1316             }
1317             if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1318             {
1319                 /* U and V inverted compared to I420
1320                  * Fixme: this should be handled by the vout core */
1321                 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1322                     + p_pic->p_sys->p_image->offsets[2];
1323                 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1324                     + p_pic->p_sys->p_image->offsets[1];
1325             }
1326             break;
1327
1328 #else
1329         case VLC_FOURCC('R','G','B','2'):
1330         case VLC_FOURCC('R','V','1','6'):
1331         case VLC_FOURCC('R','V','1','5'):
1332         case VLC_FOURCC('R','V','2','4'):
1333         case VLC_FOURCC('R','V','3','2'):
1334
1335             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1336             p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1337             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1338                                   + p_pic->p_sys->p_image->xoffset;
1339             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1340
1341             /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1342              * properly by vout_InitPicture() */
1343             p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1344                                          * p_pic->p_sys->p_image->width;
1345             break;
1346 #endif
1347
1348         default:
1349             /* Unknown chroma, tell the guy to get lost */
1350             IMAGE_FREE( p_pic->p_sys->p_image );
1351             free( p_pic->p_sys );
1352             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1353                      p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1354             p_pic->i_planes = 0;
1355             return -1;
1356     }
1357
1358 #endif /* !MODULE_NAME_IS_glx */
1359
1360     return 0;
1361 }
1362
1363 /*****************************************************************************
1364  * FreePicture: destroy a picture allocated with NewPicture
1365  *****************************************************************************
1366  * Destroy XImage AND associated data. If using Shm, detach shared memory
1367  * segment from server and process, then free it. The XDestroyImage manpage
1368  * says that both the image structure _and_ the data pointed to by the
1369  * image structure are freed, so no need to free p_image->data.
1370  *****************************************************************************/
1371 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1372 {
1373     /* The order of operations is correct */
1374 #ifdef HAVE_SYS_SHM_H
1375     if( p_vout->p_sys->b_shm )
1376     {
1377         XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1378         IMAGE_FREE( p_pic->p_sys->p_image );
1379
1380         shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1381         if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1382         {
1383             msg_Err( p_vout, "cannot detach shared memory (%s)",
1384                              strerror(errno) );
1385         }
1386     }
1387     else
1388 #endif
1389     {
1390         IMAGE_FREE( p_pic->p_sys->p_image );
1391     }
1392
1393     /* Do NOT use XFlush here ! */
1394     XSync( p_vout->p_sys->p_display, False );
1395
1396     free( p_pic->p_sys );
1397 }
1398
1399 /*****************************************************************************
1400  * ToggleFullScreen: Enable or disable full screen mode
1401  *****************************************************************************
1402  * This function will switch between fullscreen and window mode.
1403  *****************************************************************************/
1404 static void ToggleFullScreen ( vout_thread_t *p_vout )
1405 {
1406     Atom prop;
1407     XEvent xevent;
1408     mwmhints_t mwmhints;
1409     XSetWindowAttributes attributes;
1410
1411 #ifdef HAVE_XINERAMA
1412     int i_d1, i_d2;
1413 #endif
1414
1415     p_vout->b_fullscreen = !p_vout->b_fullscreen;
1416
1417     if( p_vout->b_fullscreen )
1418     {
1419         msg_Dbg( p_vout, "entering fullscreen mode" );
1420
1421         p_vout->p_sys->b_altfullscreen =
1422             config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1423
1424         XUnmapWindow( p_vout->p_sys->p_display,
1425                       p_vout->p_sys->p_win->base_window );
1426
1427         p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1428
1429         CreateWindow( p_vout, p_vout->p_sys->p_win );
1430         XDestroyWindow( p_vout->p_sys->p_display,
1431                         p_vout->p_sys->fullscreen_window.video_window );
1432         XReparentWindow( p_vout->p_sys->p_display,
1433                          p_vout->p_sys->original_window.video_window,
1434                          p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
1435         p_vout->p_sys->fullscreen_window.video_window =
1436             p_vout->p_sys->original_window.video_window;
1437
1438         /* To my knowledge there are two ways to create a borderless window.
1439          * There's the generic way which is to tell x to bypass the window
1440          * manager, but this creates problems with the focus of other
1441          * applications.
1442          * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1443          * luckily seems to be supported by most window managers. */
1444         if( !p_vout->p_sys->b_altfullscreen )
1445         {
1446             mwmhints.flags = MWM_HINTS_DECORATIONS;
1447             mwmhints.decorations = False;
1448
1449             prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1450                                 False );
1451             XChangeProperty( p_vout->p_sys->p_display,
1452                              p_vout->p_sys->p_win->base_window,
1453                              prop, prop, 32, PropModeReplace,
1454                              (unsigned char *)&mwmhints,
1455                              PROP_MWM_HINTS_ELEMENTS );
1456         }
1457         else
1458         {
1459             /* brute force way to remove decorations */
1460             attributes.override_redirect = True;
1461             XChangeWindowAttributes( p_vout->p_sys->p_display,
1462                                      p_vout->p_sys->p_win->base_window,
1463                                      CWOverrideRedirect,
1464                                      &attributes);
1465
1466             /* Make sure the change is effective */
1467             XReparentWindow( p_vout->p_sys->p_display,
1468                              p_vout->p_sys->p_win->base_window,
1469                              DefaultRootWindow( p_vout->p_sys->p_display ),
1470                              0, 0 );
1471         }
1472
1473         if( p_vout->p_sys->b_net_wm_state_fullscreen )
1474         {
1475             XClientMessageEvent event;
1476
1477             memset( &event, 0, sizeof( XClientMessageEvent ) );
1478
1479             event.type = ClientMessage;
1480             event.message_type = p_vout->p_sys->net_wm_state;
1481             event.display = p_vout->p_sys->p_display;
1482             event.window = p_vout->p_sys->p_win->base_window;
1483             event.format = 32;
1484             event.data.l[ 0 ] = 1; /* set property */
1485             event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
1486
1487             XSendEvent( p_vout->p_sys->p_display,
1488                         DefaultRootWindow( p_vout->p_sys->p_display ),
1489                         False, SubstructureRedirectMask,
1490                         (XEvent*)&event );
1491         }
1492
1493         /* Make sure the change is effective */
1494         XReparentWindow( p_vout->p_sys->p_display,
1495                          p_vout->p_sys->p_win->base_window,
1496                          DefaultRootWindow( p_vout->p_sys->p_display ),
1497                          0, 0 );
1498
1499 #ifdef HAVE_XINERAMA
1500         if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
1501             XineramaIsActive( p_vout->p_sys->p_display ) )
1502         {
1503             XineramaScreenInfo *screens;   /* infos for xinerama */
1504             int i_num_screens;
1505
1506             msg_Dbg( p_vout, "using XFree Xinerama extension");
1507
1508 #define SCREEN p_vout->p_sys->p_win->i_screen
1509
1510             /* Get Information about Xinerama (num of screens) */
1511             screens = XineramaQueryScreens( p_vout->p_sys->p_display,
1512                                             &i_num_screens );
1513
1514             if( !SCREEN )
1515                 SCREEN = config_GetInt( p_vout,
1516                                         MODULE_STRING "-xineramascreen" );
1517
1518             /* just check that user has entered a good value */
1519             if( SCREEN >= i_num_screens || SCREEN < 0 )
1520             {
1521                 msg_Dbg( p_vout, "requested screen number invalid" );
1522                 SCREEN = 0;
1523             }
1524
1525             /* Get the X/Y upper left corner coordinate of the above screen */
1526             p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
1527             p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
1528
1529             /* Set the Height/width to the screen resolution */
1530             p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
1531             p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
1532
1533             XFree(screens);
1534
1535 #undef SCREEN
1536
1537         }
1538         else
1539 #endif
1540         {
1541             /* The window wasn't necessarily created at the requested size */
1542             p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
1543             p_vout->p_sys->p_win->i_width =
1544                 DisplayWidth( p_vout->p_sys->p_display,
1545                               p_vout->p_sys->i_screen );
1546             p_vout->p_sys->p_win->i_height =
1547                 DisplayHeight( p_vout->p_sys->p_display,
1548                                p_vout->p_sys->i_screen );
1549         }
1550
1551         XMoveResizeWindow( p_vout->p_sys->p_display,
1552                            p_vout->p_sys->p_win->base_window,
1553                            p_vout->p_sys->p_win->i_x,
1554                            p_vout->p_sys->p_win->i_y,
1555                            p_vout->p_sys->p_win->i_width,
1556                            p_vout->p_sys->p_win->i_height );
1557     }
1558     else
1559     {
1560         msg_Dbg( p_vout, "leaving fullscreen mode" );
1561
1562         XReparentWindow( p_vout->p_sys->p_display,
1563                          p_vout->p_sys->original_window.video_window,
1564                          p_vout->p_sys->original_window.base_window, 0, 0 );
1565
1566         p_vout->p_sys->fullscreen_window.video_window = None;
1567         DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1568         p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1569
1570         XMapWindow( p_vout->p_sys->p_display,
1571                     p_vout->p_sys->p_win->base_window );
1572     }
1573
1574     /* Unfortunately, using XSync() here is not enough to ensure the
1575      * window has already been mapped because the XMapWindow() request
1576      * has not necessarily been sent directly to our window (remember,
1577      * the call is first redirected to the window manager) */
1578     do
1579     {
1580         XWindowEvent( p_vout->p_sys->p_display,
1581                       p_vout->p_sys->p_win->base_window,
1582                       StructureNotifyMask, &xevent );
1583     } while( xevent.type != MapNotify );
1584
1585     /* Be careful, this can generate a BadMatch error if the window is not
1586      * already mapped by the server (see above) */
1587     XSetInputFocus(p_vout->p_sys->p_display,
1588                    p_vout->p_sys->p_win->base_window,
1589                    RevertToParent,
1590                    CurrentTime);
1591
1592     /* signal that the size needs to be updated */
1593     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1594 }
1595
1596 /*****************************************************************************
1597  * EnableXScreenSaver: enable screen saver
1598  *****************************************************************************
1599  * This function enables the screen saver on a display after it has been
1600  * disabled by XDisableScreenSaver.
1601  * FIXME: what happens if multiple vlc sessions are running at the same
1602  *        time ???
1603  *****************************************************************************/
1604 static void EnableXScreenSaver( vout_thread_t *p_vout )
1605 {
1606 #ifdef DPMSINFO_IN_DPMS_H
1607     int dummy;
1608 #endif
1609
1610     if( p_vout->p_sys->i_ss_timeout )
1611     {
1612         XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1613                          p_vout->p_sys->i_ss_interval,
1614                          p_vout->p_sys->i_ss_blanking,
1615                          p_vout->p_sys->i_ss_exposure );
1616     }
1617
1618     /* Restore DPMS settings */
1619 #ifdef DPMSINFO_IN_DPMS_H
1620     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1621     {
1622         if( p_vout->p_sys->b_ss_dpms )
1623         {
1624             DPMSEnable( p_vout->p_sys->p_display );
1625         }
1626     }
1627 #endif
1628 }
1629
1630 /*****************************************************************************
1631  * DisableXScreenSaver: disable screen saver
1632  *****************************************************************************
1633  * See XEnableXScreenSaver
1634  *****************************************************************************/
1635 static void DisableXScreenSaver( vout_thread_t *p_vout )
1636 {
1637 #ifdef DPMSINFO_IN_DPMS_H
1638     int dummy;
1639 #endif
1640
1641     /* Save screen saver information */
1642     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1643                      &p_vout->p_sys->i_ss_interval,
1644                      &p_vout->p_sys->i_ss_blanking,
1645                      &p_vout->p_sys->i_ss_exposure );
1646
1647     /* Disable screen saver */
1648     if( p_vout->p_sys->i_ss_timeout )
1649     {
1650         XSetScreenSaver( p_vout->p_sys->p_display, 0,
1651                          p_vout->p_sys->i_ss_interval,
1652                          p_vout->p_sys->i_ss_blanking,
1653                          p_vout->p_sys->i_ss_exposure );
1654     }
1655
1656     /* Disable DPMS */
1657 #ifdef DPMSINFO_IN_DPMS_H
1658     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1659     {
1660         CARD16 unused;
1661         /* Save DPMS current state */
1662         DPMSInfo( p_vout->p_sys->p_display, &unused,
1663                   &p_vout->p_sys->b_ss_dpms );
1664         DPMSDisable( p_vout->p_sys->p_display );
1665    }
1666 #endif
1667 }
1668
1669 /*****************************************************************************
1670  * CreateCursor: create a blank mouse pointer
1671  *****************************************************************************/
1672 static void CreateCursor( vout_thread_t *p_vout )
1673 {
1674     XColor cursor_color;
1675
1676     p_vout->p_sys->cursor_pixmap =
1677         XCreatePixmap( p_vout->p_sys->p_display,
1678                        DefaultRootWindow( p_vout->p_sys->p_display ),
1679                        1, 1, 1 );
1680
1681     XParseColor( p_vout->p_sys->p_display,
1682                  XCreateColormap( p_vout->p_sys->p_display,
1683                                   DefaultRootWindow(
1684                                                     p_vout->p_sys->p_display ),
1685                                   DefaultVisual(
1686                                                 p_vout->p_sys->p_display,
1687                                                 p_vout->p_sys->i_screen ),
1688                                   AllocNone ),
1689                  "black", &cursor_color );
1690
1691     p_vout->p_sys->blank_cursor =
1692         XCreatePixmapCursor( p_vout->p_sys->p_display,
1693                              p_vout->p_sys->cursor_pixmap,
1694                              p_vout->p_sys->cursor_pixmap,
1695                              &cursor_color, &cursor_color, 1, 1 );
1696 }
1697
1698 /*****************************************************************************
1699  * DestroyCursor: destroy the blank mouse pointer
1700  *****************************************************************************/
1701 static void DestroyCursor( vout_thread_t *p_vout )
1702 {
1703     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1704 }
1705
1706 /*****************************************************************************
1707  * ToggleCursor: hide or show the mouse pointer
1708  *****************************************************************************
1709  * This function hides the X pointer if it is visible by setting the pointer
1710  * sprite to a blank one. To show it again, we disable the sprite.
1711  *****************************************************************************/
1712 static void ToggleCursor( vout_thread_t *p_vout )
1713 {
1714     if( p_vout->p_sys->b_mouse_pointer_visible )
1715     {
1716         XDefineCursor( p_vout->p_sys->p_display,
1717                        p_vout->p_sys->p_win->base_window,
1718                        p_vout->p_sys->blank_cursor );
1719         p_vout->p_sys->b_mouse_pointer_visible = 0;
1720     }
1721     else
1722     {
1723         XUndefineCursor( p_vout->p_sys->p_display,
1724                          p_vout->p_sys->p_win->base_window );
1725         p_vout->p_sys->b_mouse_pointer_visible = 1;
1726     }
1727 }
1728
1729 #ifdef MODULE_NAME_IS_xvideo
1730 /*****************************************************************************
1731  * XVideoGetPort: get YUV12 port
1732  *****************************************************************************/
1733 static int XVideoGetPort( vout_thread_t *p_vout,
1734                           vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1735 {
1736     XvAdaptorInfo *p_adaptor;
1737     unsigned int i;
1738     int i_adaptor, i_num_adaptors, i_requested_adaptor;
1739     int i_selected_port;
1740
1741     switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1742     {
1743         case Success:
1744             break;
1745
1746         case XvBadExtension:
1747             msg_Warn( p_vout, "XvBadExtension" );
1748             return -1;
1749
1750         case XvBadAlloc:
1751             msg_Warn( p_vout, "XvBadAlloc" );
1752             return -1;
1753
1754         default:
1755             msg_Warn( p_vout, "XvQueryExtension failed" );
1756             return -1;
1757     }
1758
1759     switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1760                              DefaultRootWindow( p_vout->p_sys->p_display ),
1761                              &i_num_adaptors, &p_adaptor ) )
1762     {
1763         case Success:
1764             break;
1765
1766         case XvBadExtension:
1767             msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1768             return -1;
1769
1770         case XvBadAlloc:
1771             msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1772             return -1;
1773
1774         default:
1775             msg_Warn( p_vout, "XvQueryAdaptors failed" );
1776             return -1;
1777     }
1778
1779     i_selected_port = -1;
1780     i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1781
1782     for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1783     {
1784         XvImageFormatValues *p_formats;
1785         int i_format, i_num_formats;
1786         int i_port;
1787
1788         /* If we requested an adaptor and it's not this one, we aren't
1789          * interested */
1790         if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1791         {
1792             continue;
1793         }
1794
1795         /* If the adaptor doesn't have the required properties, skip it */
1796         if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1797             !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1798         {
1799             continue;
1800         }
1801
1802         /* Check that adaptor supports our requested format... */
1803         p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1804                                         p_adaptor[i_adaptor].base_id,
1805                                         &i_num_formats );
1806
1807         for( i_format = 0;
1808              i_format < i_num_formats && ( i_selected_port == -1 );
1809              i_format++ )
1810         {
1811             XvAttribute     *p_attr;
1812             int             i_attr, i_num_attributes;
1813
1814             /* If this is not the format we want, or at least a
1815              * similar one, forget it */
1816             if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1817             {
1818                 continue;
1819             }
1820
1821             /* Look for the first available port supporting this format */
1822             for( i_port = p_adaptor[i_adaptor].base_id;
1823                  ( i_port < (int)(p_adaptor[i_adaptor].base_id
1824                                    + p_adaptor[i_adaptor].num_ports) )
1825                    && ( i_selected_port == -1 );
1826                  i_port++ )
1827             {
1828                 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1829                      == Success )
1830                 {
1831                     i_selected_port = i_port;
1832                     *pi_newchroma = p_formats[ i_format ].id;
1833                 }
1834             }
1835
1836             /* If no free port was found, forget it */
1837             if( i_selected_port == -1 )
1838             {
1839                 continue;
1840             }
1841
1842             /* If we found a port, print information about it */
1843             msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1844                      i_adaptor, i_selected_port, p_formats[ i_format ].id,
1845                      (char *)&p_formats[ i_format ].id,
1846                      ( p_formats[ i_format ].format == XvPacked ) ?
1847                          "packed" : "planar" );
1848
1849             /* Make sure XV_AUTOPAINT_COLORKEY is set */
1850             p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1851                                             i_selected_port,
1852                                             &i_num_attributes );
1853
1854             for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1855             {
1856                 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
1857                 {
1858                     const Atom autopaint =
1859                         XInternAtom( p_vout->p_sys->p_display,
1860                                      "XV_AUTOPAINT_COLORKEY", False );
1861                     XvSetPortAttribute( p_vout->p_sys->p_display,
1862                                         i_selected_port, autopaint, 1 );
1863                     break;
1864                 }
1865             }
1866
1867             if( p_attr != NULL )
1868             {
1869                 XFree( p_attr );
1870             }
1871         }
1872
1873         if( p_formats != NULL )
1874         {
1875             XFree( p_formats );
1876         }
1877
1878     }
1879
1880     if( i_num_adaptors > 0 )
1881     {
1882         XvFreeAdaptorInfo( p_adaptor );
1883     }
1884
1885     if( i_selected_port == -1 )
1886     {
1887         int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
1888         if( i_requested_adaptor == -1 )
1889         {
1890             msg_Warn( p_vout, "no free XVideo port found for format "
1891                       "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
1892         }
1893         else
1894         {
1895             msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1896                       "XVideo port for format 0x%.8x (%4.4s)",
1897                       i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
1898         }
1899     }
1900
1901     return i_selected_port;
1902 }
1903
1904 /*****************************************************************************
1905  * XVideoReleasePort: release YUV12 port
1906  *****************************************************************************/
1907 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1908 {
1909     XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1910 }
1911 #endif
1912
1913 /*****************************************************************************
1914  * InitDisplay: open and initialize X11 device
1915  *****************************************************************************
1916  * Create a window according to video output given size, and set other
1917  * properties according to the display properties.
1918  *****************************************************************************/
1919 static int InitDisplay( vout_thread_t *p_vout )
1920 {
1921 #ifdef MODULE_NAME_IS_x11
1922     XPixmapFormatValues *       p_formats;                 /* pixmap formats */
1923     XVisualInfo *               p_xvisual;            /* visuals information */
1924     XVisualInfo                 xvisual_template;         /* visual template */
1925     int                         i_count;                       /* array size */
1926 #endif
1927
1928 #ifdef HAVE_SYS_SHM_H
1929     p_vout->p_sys->b_shm = 0;
1930
1931     if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1932     {
1933 #   ifdef SYS_DARWIN
1934         /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1935 #   else
1936         p_vout->p_sys->b_shm =
1937                   ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1938 #   endif
1939
1940         if( !p_vout->p_sys->b_shm )
1941         {
1942             msg_Warn( p_vout, "XShm video extension is unavailable" );
1943         }
1944     }
1945     else
1946     {
1947         msg_Dbg( p_vout, "disabling XShm video extension" );
1948     }
1949
1950 #else
1951     msg_Warn( p_vout, "XShm video extension is unavailable" );
1952
1953 #endif
1954
1955 #ifdef MODULE_NAME_IS_xvideo
1956     /* XXX The brightness and contrast values should be read from environment
1957      * XXX variables... */
1958 #if 0
1959     XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1960     XVideoSetAttribute( p_vout, "XV_CONTRAST",   0.5 );
1961 #endif
1962 #endif
1963
1964 #ifdef MODULE_NAME_IS_x11
1965     /* Initialize structure */
1966     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1967
1968     /* Get screen depth */
1969     p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1970                                                    p_vout->p_sys->i_screen );
1971     switch( p_vout->p_sys->i_screen_depth )
1972     {
1973     case 8:
1974         /*
1975          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1976          */
1977         xvisual_template.screen =   p_vout->p_sys->i_screen;
1978         xvisual_template.class =    DirectColor;
1979         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1980                                     VisualScreenMask | VisualClassMask,
1981                                     &xvisual_template, &i_count );
1982         if( p_xvisual == NULL )
1983         {
1984             msg_Err( p_vout, "no PseudoColor visual available" );
1985             return VLC_EGENERIC;
1986         }
1987         p_vout->p_sys->i_bytes_per_pixel = 1;
1988         p_vout->output.pf_setpalette = SetPalette;
1989         break;
1990     case 15:
1991     case 16:
1992     case 24:
1993     default:
1994         /*
1995          * Screen depth is higher than 8bpp. TrueColor visual is used.
1996          */
1997         xvisual_template.screen =   p_vout->p_sys->i_screen;
1998         xvisual_template.class =    TrueColor;
1999         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2000                                     VisualScreenMask | VisualClassMask,
2001                                     &xvisual_template, &i_count );
2002         if( p_xvisual == NULL )
2003         {
2004             msg_Err( p_vout, "no TrueColor visual available" );
2005             return VLC_EGENERIC;
2006         }
2007
2008         p_vout->output.i_rmask = p_xvisual->red_mask;
2009         p_vout->output.i_gmask = p_xvisual->green_mask;
2010         p_vout->output.i_bmask = p_xvisual->blue_mask;
2011
2012         /* There is no difference yet between 3 and 4 Bpp. The only way
2013          * to find the actual number of bytes per pixel is to list supported
2014          * pixmap formats. */
2015         p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2016         p_vout->p_sys->i_bytes_per_pixel = 0;
2017
2018         for( ; i_count-- ; p_formats++ )
2019         {
2020             /* Under XFree4.0, the list contains pixmap formats available
2021              * through all video depths ; so we have to check against current
2022              * depth. */
2023             if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
2024             {
2025                 if( p_formats->bits_per_pixel / 8
2026                         > (int)p_vout->p_sys->i_bytes_per_pixel )
2027                 {
2028                     p_vout->p_sys->i_bytes_per_pixel =
2029                                                p_formats->bits_per_pixel / 8;
2030                 }
2031             }
2032         }
2033         break;
2034     }
2035     p_vout->p_sys->p_visual = p_xvisual->visual;
2036     XFree( p_xvisual );
2037 #endif
2038
2039     return VLC_SUCCESS;
2040 }
2041
2042 #ifdef HAVE_SYS_SHM_H
2043 /*****************************************************************************
2044  * CreateShmImage: create an XImage or XvImage using shared memory extension
2045  *****************************************************************************
2046  * Prepare an XImage or XvImage for display function.
2047  * The order of the operations respects the recommandations of the mit-shm
2048  * document by J.Corbet and K.Packard. Most of the parameters were copied from
2049  * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2050  *****************************************************************************/
2051 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2052                                     Display* p_display, EXTRA_ARGS_SHM,
2053                                     int i_width, int i_height )
2054 {
2055     IMAGE_TYPE *p_image;
2056     Status result;
2057
2058     /* Create XImage / XvImage */
2059 #ifdef MODULE_NAME_IS_xvideo
2060
2061     /* Make sure the buffer is aligned to multiple of 16 */
2062     i_height = ( i_height + 15 ) >> 4 << 4;
2063     i_width = ( i_width + 15 ) >> 4 << 4;
2064
2065     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2066                                 i_width, i_height, p_shm );
2067 #else
2068     p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2069                                p_shm, i_width, i_height );
2070 #endif
2071     if( p_image == NULL )
2072     {
2073         msg_Err( p_vout, "image creation failed" );
2074         return NULL;
2075     }
2076
2077     /* Allocate shared memory segment - 0776 set the access permission
2078      * rights (like umask), they are not yet supported by all X servers */
2079     p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2080     if( p_shm->shmid < 0 )
2081     {
2082         msg_Err( p_vout, "cannot allocate shared image data (%s)",
2083                          strerror( errno ) );
2084         IMAGE_FREE( p_image );
2085         return NULL;
2086     }
2087
2088     /* Attach shared memory segment to process (read/write) */
2089     p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2090     if(! p_shm->shmaddr )
2091     {
2092         msg_Err( p_vout, "cannot attach shared memory (%s)",
2093                          strerror(errno));
2094         IMAGE_FREE( p_image );
2095         shmctl( p_shm->shmid, IPC_RMID, 0 );
2096         return NULL;
2097     }
2098
2099     /* Read-only data. We won't be using XShmGetImage */
2100     p_shm->readOnly = True;
2101
2102     /* Attach shared memory segment to X server */
2103     XSynchronize( p_display, True );
2104     b_shm = VLC_TRUE;
2105     result = XShmAttach( p_display, p_shm );
2106     if( result == False || !b_shm )
2107     {
2108         msg_Err( p_vout, "cannot attach shared memory to X server" );
2109         IMAGE_FREE( p_image );
2110         shmctl( p_shm->shmid, IPC_RMID, 0 );
2111         shmdt( p_shm->shmaddr );
2112         return NULL;
2113     }
2114     XSynchronize( p_display, False );
2115
2116     /* Send image to X server. This instruction is required, since having
2117      * built a Shm XImage and not using it causes an error on XCloseDisplay,
2118      * and remember NOT to use XFlush ! */
2119     XSync( p_display, False );
2120
2121 #if 0
2122     /* Mark the shm segment to be removed when there are no more
2123      * attachements, so it is automatic on process exit or after shmdt */
2124     shmctl( p_shm->shmid, IPC_RMID, 0 );
2125 #endif
2126
2127     return p_image;
2128 }
2129 #endif
2130
2131 /*****************************************************************************
2132  * CreateImage: create an XImage or XvImage
2133  *****************************************************************************
2134  * Create a simple image used as a buffer.
2135  *****************************************************************************/
2136 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2137                                  Display *p_display, EXTRA_ARGS,
2138                                  int i_width, int i_height )
2139 {
2140     byte_t *    p_data;                           /* image data storage zone */
2141     IMAGE_TYPE *p_image;
2142 #ifdef MODULE_NAME_IS_x11
2143     int         i_quantum;                     /* XImage quantum (see below) */
2144     int         i_bytes_per_line;
2145 #endif
2146
2147     /* Allocate memory for image */
2148 #ifdef MODULE_NAME_IS_xvideo
2149
2150     /* Make sure the buffer is aligned to multiple of 16 */
2151     i_height = ( i_height + 15 ) >> 4 << 4;
2152     i_width = ( i_width + 15 ) >> 4 << 4;
2153
2154     p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2155 #elif defined(MODULE_NAME_IS_x11)
2156     i_bytes_per_line = i_width * i_bytes_per_pixel;
2157     p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2158 #endif
2159     if( !p_data )
2160     {
2161         msg_Err( p_vout, "out of memory" );
2162         return NULL;
2163     }
2164
2165 #ifdef MODULE_NAME_IS_x11
2166     /* Optimize the quantum of a scanline regarding its size - the quantum is
2167        a diviser of the number of bits between the start of two scanlines. */
2168     if( i_bytes_per_line & 0xf )
2169     {
2170         i_quantum = 0x8;
2171     }
2172     else if( i_bytes_per_line & 0x10 )
2173     {
2174         i_quantum = 0x10;
2175     }
2176     else
2177     {
2178         i_quantum = 0x20;
2179     }
2180 #endif
2181
2182     /* Create XImage. p_data will be automatically freed */
2183 #ifdef MODULE_NAME_IS_xvideo
2184     p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2185                              p_data, i_width, i_height );
2186 #elif defined(MODULE_NAME_IS_x11)
2187     p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2188                             p_data, i_width, i_height, i_quantum, 0 );
2189 #endif
2190     if( p_image == NULL )
2191     {
2192         msg_Err( p_vout, "XCreateImage() failed" );
2193         free( p_data );
2194         return NULL;
2195     }
2196
2197     return p_image;
2198 }
2199
2200 /*****************************************************************************
2201  * X11ErrorHandler: replace error handler so we can intercept some of them
2202  *****************************************************************************/
2203 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2204 {
2205     /* Ingnore errors on XSetInputFocus()
2206      * (they happen when a window is not yet mapped) */
2207     if( event->request_code == X_SetInputFocus )
2208     {
2209         fprintf(stderr, "XSetInputFocus failed\n");
2210         return 0;
2211     }
2212
2213     if( event->request_code == 150 /* MIT-SHM */ &&
2214         event->minor_code == X_ShmAttach )
2215     {
2216         fprintf(stderr, "XShmAttach failed\n");
2217         b_shm = VLC_FALSE;
2218         return 0;
2219     }
2220
2221     XSetErrorHandler(NULL);
2222     return (XSetErrorHandler(X11ErrorHandler))( display, event );
2223 }
2224
2225 #ifdef MODULE_NAME_IS_x11
2226 /*****************************************************************************
2227  * SetPalette: sets an 8 bpp palette
2228  *****************************************************************************
2229  * This function sets the palette given as an argument. It does not return
2230  * anything, but could later send information on which colors it was unable
2231  * to set.
2232  *****************************************************************************/
2233 static void SetPalette( vout_thread_t *p_vout,
2234                         uint16_t *red, uint16_t *green, uint16_t *blue )
2235 {
2236     int i;
2237     XColor p_colors[255];
2238
2239     /* allocate palette */
2240     for( i = 0; i < 255; i++ )
2241     {
2242         /* kludge: colors are indexed reversely because color 255 seems
2243          * to be reserved for black even if we try to set it to white */
2244         p_colors[ i ].pixel = 255 - i;
2245         p_colors[ i ].pad   = 0;
2246         p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2247         p_colors[ i ].red   = red[ 255 - i ];
2248         p_colors[ i ].blue  = blue[ 255 - i ];
2249         p_colors[ i ].green = green[ 255 - i ];
2250     }
2251
2252     XStoreColors( p_vout->p_sys->p_display,
2253                   p_vout->p_sys->colormap, p_colors, 255 );
2254 }
2255 #endif
2256
2257 /*****************************************************************************
2258  * Control: control facility for the vout
2259  *****************************************************************************/
2260 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2261 {
2262     double f_arg;
2263     vlc_bool_t b_arg;
2264
2265     switch( i_query )
2266     {
2267         case VOUT_SET_ZOOM:
2268             if( p_vout->p_sys->p_win->owner_window )
2269                 return vout_ControlWindow( p_vout,
2270                     (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2271
2272             f_arg = va_arg( args, double );
2273
2274             vlc_mutex_lock( &p_vout->p_sys->lock );
2275
2276             /* Update dimensions */
2277             /* FIXME: export InitWindowSize() from vout core */
2278             XResizeWindow( p_vout->p_sys->p_display,
2279                            p_vout->p_sys->p_win->base_window,
2280                            p_vout->i_window_width * f_arg,
2281                            p_vout->i_window_height * f_arg );
2282
2283             vlc_mutex_unlock( &p_vout->p_sys->lock );
2284             return VLC_SUCCESS;
2285
2286        case VOUT_CLOSE:
2287             vlc_mutex_lock( &p_vout->p_sys->lock );
2288             XUnmapWindow( p_vout->p_sys->p_display,
2289                           p_vout->p_sys->original_window.base_window );
2290             vlc_mutex_unlock( &p_vout->p_sys->lock );
2291             /* Fall through */
2292
2293        case VOUT_REPARENT:
2294             vlc_mutex_lock( &p_vout->p_sys->lock );
2295             XReparentWindow( p_vout->p_sys->p_display,
2296                              p_vout->p_sys->original_window.base_window,
2297                              DefaultRootWindow( p_vout->p_sys->p_display ),
2298                              0, 0 );
2299             XSync( p_vout->p_sys->p_display, False );
2300             p_vout->p_sys->original_window.owner_window = 0;
2301             vlc_mutex_unlock( &p_vout->p_sys->lock );
2302             return vout_vaControlDefault( p_vout, i_query, args );
2303
2304         case VOUT_SET_STAY_ON_TOP:
2305             if( p_vout->p_sys->p_win->owner_window )
2306                 return vout_ControlWindow( p_vout,
2307                     (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2308
2309             b_arg = va_arg( args, vlc_bool_t );
2310             vlc_mutex_lock( &p_vout->p_sys->lock );
2311             WindowOnTop( p_vout, b_arg );
2312             vlc_mutex_unlock( &p_vout->p_sys->lock );
2313             return VLC_SUCCESS;
2314
2315        default:
2316             return vout_vaControlDefault( p_vout, i_query, args );
2317     }
2318 }
2319
2320 /*****************************************************************************
2321  * TestNetWMSupport: tests for Extended Window Manager Hints support
2322  *****************************************************************************/
2323 static void TestNetWMSupport( vout_thread_t *p_vout )
2324 {
2325     int i_ret, i_format;
2326     unsigned long i, i_items, i_bytesafter;
2327     Atom net_wm_supported;
2328     union { Atom *p_atom; unsigned char *p_char; } p_args;
2329
2330     p_args.p_atom = NULL;
2331
2332     p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
2333     p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
2334     p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
2335     p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
2336
2337     net_wm_supported =
2338         XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
2339
2340     i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
2341                                 DefaultRootWindow( p_vout->p_sys->p_display ),
2342                                 net_wm_supported,
2343                                 0, 16384, False, AnyPropertyType,
2344                                 &net_wm_supported,
2345                                 &i_format, &i_items, &i_bytesafter,
2346                                 (unsigned char **)&p_args );
2347
2348     if( i_ret != Success || i_items == 0 ) return;
2349
2350     msg_Dbg( p_vout, "Window manager supports NetWM" );
2351
2352     p_vout->p_sys->net_wm_state =
2353         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
2354     p_vout->p_sys->net_wm_state_fullscreen =
2355         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
2356                      False );
2357     p_vout->p_sys->net_wm_state_above =
2358         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
2359     p_vout->p_sys->net_wm_state_below =
2360         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
2361     p_vout->p_sys->net_wm_state_stays_on_top =
2362         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
2363                      False );
2364
2365     for( i = 0; i < i_items; i++ )
2366     {
2367         if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
2368         {
2369             msg_Dbg( p_vout,
2370                      "Window manager supports _NET_WM_STATE_FULLSCREEN" );
2371             p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
2372         }
2373         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
2374         {
2375             msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
2376             p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
2377         }
2378         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
2379         {
2380             msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
2381             p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
2382         }
2383         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
2384         {
2385             msg_Dbg( p_vout,
2386                      "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
2387             p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
2388         }
2389     }
2390
2391     XFree( p_args.p_atom );
2392 }
2393
2394 /*****************************************************************************
2395  * Key events handling
2396  *****************************************************************************/
2397 static struct
2398 {
2399     int i_x11key;
2400     int i_vlckey;
2401
2402 } x11keys_to_vlckeys[] =
2403 {
2404     { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
2405     { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
2406     { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
2407     { XK_F12, KEY_F12 },
2408
2409     { XK_Return, KEY_ENTER },
2410     { XK_KP_Enter, KEY_ENTER },
2411     { XK_space, KEY_SPACE },
2412     { XK_Escape, KEY_ESC },
2413
2414     { XK_Menu, KEY_MENU },
2415     { XK_Left, KEY_LEFT },
2416     { XK_Right, KEY_RIGHT },
2417     { XK_Up, KEY_UP },
2418     { XK_Down, KEY_DOWN },
2419
2420     { XK_Home, KEY_HOME },
2421     { XK_End, KEY_END },
2422     { XK_Page_Up, KEY_PAGEUP },
2423     { XK_Page_Down, KEY_PAGEDOWN },
2424
2425     { XK_Insert, KEY_INSERT },
2426     { XK_Delete, KEY_DELETE },
2427
2428     { 0, 0 }
2429 };
2430
2431 static int ConvertKey( int i_key )
2432 {
2433     int i;
2434
2435     for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
2436     {
2437         if( x11keys_to_vlckeys[i].i_x11key == i_key )
2438         {
2439             return x11keys_to_vlckeys[i].i_vlckey;
2440         }
2441     }
2442
2443     return 0;
2444 }
2445
2446 /*****************************************************************************
2447  * WindowOnTop: Switches the "always on top" state of the video window.
2448  *****************************************************************************/
2449 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
2450 {
2451     if( p_vout->p_sys->b_net_wm_state_stays_on_top )
2452     {
2453         XClientMessageEvent event;
2454
2455         memset( &event, 0, sizeof( XClientMessageEvent ) );
2456
2457         event.type = ClientMessage;
2458         event.message_type = p_vout->p_sys->net_wm_state;
2459         event.display = p_vout->p_sys->p_display;
2460         event.window = p_vout->p_sys->p_win->base_window;
2461         event.format = 32;
2462         event.data.l[ 0 ] = b_on_top; /* set property */
2463         event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
2464
2465         XSendEvent( p_vout->p_sys->p_display,
2466                     DefaultRootWindow( p_vout->p_sys->p_display ),
2467                     False, SubstructureRedirectMask,
2468                     (XEvent*)&event );
2469     }
2470
2471     return VLC_SUCCESS;
2472 }