]> git.sesse.net Git - vlc/blob - modules/video_output/x11/xcommon.c
a1c3f671eeea1ca65f802c39b2bca9a0bfe500b6
[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->fmt_out.i_x_offset != p_vout->fmt_in.i_x_offset ||
852         p_vout->fmt_out.i_y_offset != p_vout->fmt_in.i_y_offset ||
853         p_vout->fmt_out.i_visible_width != p_vout->fmt_in.i_visible_width ||
854         p_vout->fmt_out.i_visible_height != p_vout->fmt_in.i_visible_height )
855     {
856         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
857         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
858         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
859         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
860         p_vout->i_changes |= VOUT_SIZE_CHANGE;
861     }
862 #endif
863
864     /*
865      * Size change
866      *
867      * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
868      *  the size flag inside the fullscreen routine)
869      */
870     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
871     {
872         int i_width, i_height, i_x, i_y;
873
874         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
875
876 #ifdef MODULE_NAME_IS_x11
877         /* We need to signal the vout thread about the size change because it
878          * is doing the rescaling */
879         p_vout->i_changes |= VOUT_SIZE_CHANGE;
880 #endif
881
882         vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
883                            p_vout->p_sys->p_win->i_height,
884                            &i_x, &i_y, &i_width, &i_height );
885
886         XMoveResizeWindow( p_vout->p_sys->p_display,
887                            p_vout->p_sys->p_win->video_window,
888                            i_x, i_y, i_width, i_height );
889     }
890
891     /* Autohide Cursour */
892     if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
893     {
894         /* Hide the mouse automatically */
895         if( p_vout->p_sys->b_mouse_pointer_visible )
896         {
897             ToggleCursor( p_vout );
898         }
899     }
900
901     vlc_mutex_unlock( &p_vout->p_sys->lock );
902
903     return 0;
904 }
905
906 /*****************************************************************************
907  * EndVideo: terminate X11 video thread output method
908  *****************************************************************************
909  * Destroy the X11 XImages created by Init. It is called at the end of
910  * the thread, but also each time the window is resized.
911  *****************************************************************************/
912 static void EndVideo( vout_thread_t *p_vout )
913 {
914     int i_index;
915
916     /* Free the direct buffers we allocated */
917     for( i_index = I_OUTPUTPICTURES ; i_index ; )
918     {
919         i_index--;
920         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
921     }
922 }
923
924 /* following functions are local */
925
926 /*****************************************************************************
927  * CreateWindow: open and set-up X11 main window
928  *****************************************************************************/
929 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
930 {
931     XSizeHints              xsize_hints;
932     XSetWindowAttributes    xwindow_attributes;
933     XGCValues               xgcvalues;
934     XEvent                  xevent;
935
936     vlc_bool_t              b_expose = VLC_FALSE;
937     vlc_bool_t              b_configure_notify = VLC_FALSE;
938     vlc_bool_t              b_map_notify = VLC_FALSE;
939     vlc_value_t             val;
940
941     /* Prepare window manager hints and properties */
942     p_win->wm_protocols =
943              XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
944     p_win->wm_delete_window =
945              XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
946
947     /* Never have a 0-pixel-wide window */
948     xsize_hints.min_width = 2;
949     xsize_hints.min_height = 1;
950
951     /* Prepare window attributes */
952     xwindow_attributes.backing_store = Always;       /* save the hidden part */
953     xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
954                                                      p_vout->p_sys->i_screen);
955     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
956
957     if( !p_vout->b_fullscreen )
958     {
959         p_win->owner_window =
960             (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
961                                         &p_win->i_width, &p_win->i_height );
962
963         xsize_hints.base_width  = xsize_hints.width = p_win->i_width;
964         xsize_hints.base_height = xsize_hints.height = p_win->i_height;
965         xsize_hints.flags       = PSize | PMinSize;
966
967         if( p_win->i_x >=0 || p_win->i_y >= 0 )
968         {
969             xsize_hints.x = p_win->i_x;
970             xsize_hints.y = p_win->i_y;
971             xsize_hints.flags |= PPosition;
972         }
973     }
974     else
975     {
976         /* Fullscreen window size and position */
977         p_win->owner_window = 0;
978         p_win->i_x = p_win->i_y = 0;
979         p_win->i_width =
980             DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
981         p_win->i_height =
982             DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
983     }
984
985     if( !p_win->owner_window )
986     {
987         /* Create the window and set hints - the window must receive
988          * ConfigureNotify events, and until it is displayed, Expose and
989          * MapNotify events. */
990
991         p_win->base_window =
992             XCreateWindow( p_vout->p_sys->p_display,
993                            DefaultRootWindow( p_vout->p_sys->p_display ),
994                            p_win->i_x, p_win->i_y,
995                            p_win->i_width, p_win->i_height,
996                            0,
997                            0, InputOutput, 0,
998                            CWBackingStore | CWBackPixel | CWEventMask,
999                            &xwindow_attributes );
1000
1001         if( !p_vout->b_fullscreen )
1002         {
1003             /* Set window manager hints and properties: size hints, command,
1004              * window's name, and accepted protocols */
1005             XSetWMNormalHints( p_vout->p_sys->p_display,
1006                                p_win->base_window, &xsize_hints );
1007             XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1008                          p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
1009
1010             if( !var_GetBool( p_vout, "video-deco") )
1011             {
1012                 Atom prop;
1013                 mwmhints_t mwmhints;
1014
1015                 mwmhints.flags = MWM_HINTS_DECORATIONS;
1016                 mwmhints.decorations = False;
1017
1018                 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1019                                     False );
1020
1021                 XChangeProperty( p_vout->p_sys->p_display,
1022                                  p_win->base_window,
1023                                  prop, prop, 32, PropModeReplace,
1024                                  (unsigned char *)&mwmhints,
1025                                  PROP_MWM_HINTS_ELEMENTS );
1026             }
1027             else
1028             {
1029                  var_Get( p_vout, "video-title", &val );
1030                  if( !val.psz_string || !*val.psz_string )
1031                  {
1032                     XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1033 #ifdef MODULE_NAME_IS_x11
1034                                 VOUT_TITLE " (X11 output)"
1035 #elif defined(MODULE_NAME_IS_glx)
1036                                 VOUT_TITLE " (GLX output)"
1037 #else
1038                                 VOUT_TITLE " (XVideo output)"
1039 #endif
1040                       );
1041                 }
1042                 else
1043                 {
1044                     XStoreName( p_vout->p_sys->p_display,
1045                                p_win->base_window, val.psz_string );
1046                 }
1047             }
1048         }
1049     }
1050     else
1051     {
1052         Window dummy1;
1053         unsigned int dummy2, dummy3;
1054
1055         /* Select events we are interested in. */
1056         XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1057                       StructureNotifyMask );
1058
1059         /* Get the parent window's geometry information */
1060         XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1061                       &dummy1, &dummy2, &dummy3,
1062                       &p_win->i_width,
1063                       &p_win->i_height,
1064                       &dummy2, &dummy3 );
1065
1066         /* We are already configured */
1067         b_configure_notify = VLC_TRUE;
1068
1069         /* From man XSelectInput: only one client at a time can select a
1070          * ButtonPress event, so we need to open a new window anyway. */
1071         p_win->base_window =
1072             XCreateWindow( p_vout->p_sys->p_display,
1073                            p_win->owner_window,
1074                            0, 0,
1075                            p_win->i_width, p_win->i_height,
1076                            0,
1077                            0, CopyFromParent, 0,
1078                            CWBackingStore | CWBackPixel | CWEventMask,
1079                            &xwindow_attributes );
1080     }
1081
1082     if( (p_win->wm_protocols == None)        /* use WM_DELETE_WINDOW */
1083         || (p_win->wm_delete_window == None)
1084         || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1085                              &p_win->wm_delete_window, 1 ) )
1086     {
1087         /* WM_DELETE_WINDOW is not supported by window manager */
1088         msg_Warn( p_vout, "missing or bad window manager" );
1089     }
1090
1091     /* Creation of a graphic context that doesn't generate a GraphicsExpose
1092      * event when using functions like XCopyArea */
1093     xgcvalues.graphics_exposures = False;
1094     p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1095                            p_win->base_window,
1096                            GCGraphicsExposures, &xgcvalues );
1097
1098     /* Send orders to server, and wait until window is displayed - three
1099      * events must be received: a MapNotify event, an Expose event allowing
1100      * drawing in the window, and a ConfigureNotify to get the window
1101      * dimensions. Once those events have been received, only
1102      * ConfigureNotify events need to be received. */
1103     XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1104     do
1105     {
1106         XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1107                       SubstructureNotifyMask | StructureNotifyMask |
1108                       ExposureMask, &xevent);
1109         if( (xevent.type == Expose)
1110             && (xevent.xexpose.window == p_win->base_window) )
1111         {
1112             b_expose = VLC_TRUE;
1113             /* ConfigureNotify isn't sent if there isn't a window manager.
1114              * Expose should be the last event to be received so it should
1115              * be fine to assume we won't receive it anymore. */
1116             b_configure_notify = VLC_TRUE;
1117         }
1118         else if( (xevent.type == MapNotify)
1119                  && (xevent.xmap.window == p_win->base_window) )
1120         {
1121             b_map_notify = VLC_TRUE;
1122         }
1123         else if( (xevent.type == ConfigureNotify)
1124                  && (xevent.xconfigure.window == p_win->base_window) )
1125         {
1126             b_configure_notify = VLC_TRUE;
1127             p_win->i_width = xevent.xconfigure.width;
1128             p_win->i_height = xevent.xconfigure.height;
1129         }
1130     } while( !( b_expose && b_configure_notify && b_map_notify ) );
1131
1132     XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1133                   StructureNotifyMask | KeyPressMask |
1134                   ButtonPressMask | ButtonReleaseMask |
1135                   PointerMotionMask );
1136
1137 #ifdef MODULE_NAME_IS_x11
1138     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1139     {
1140         /* Allocate a new palette */
1141         p_vout->p_sys->colormap =
1142             XCreateColormap( p_vout->p_sys->p_display,
1143                              DefaultRootWindow( p_vout->p_sys->p_display ),
1144                              DefaultVisual( p_vout->p_sys->p_display,
1145                                             p_vout->p_sys->i_screen ),
1146                              AllocAll );
1147
1148         xwindow_attributes.colormap = p_vout->p_sys->colormap;
1149         XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1150                                  CWColormap, &xwindow_attributes );
1151     }
1152 #endif
1153
1154     /* Create video output sub-window. */
1155     p_win->video_window =  XCreateSimpleWindow(
1156                                       p_vout->p_sys->p_display,
1157                                       p_win->base_window, 0, 0,
1158                                       p_win->i_width, p_win->i_height,
1159                                       0,
1160                                       BlackPixel( p_vout->p_sys->p_display,
1161                                                   p_vout->p_sys->i_screen ),
1162                                       WhitePixel( p_vout->p_sys->p_display,
1163                                                   p_vout->p_sys->i_screen ) );
1164
1165     XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1166                           BlackPixel( p_vout->p_sys->p_display,
1167                                       p_vout->p_sys->i_screen ) );
1168
1169     XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1170     XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1171                   ExposureMask );
1172
1173     /* make sure the video window will be centered in the next ManageVideo() */
1174     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1175
1176     /* If the cursor was formerly blank than blank it again */
1177     if( !p_vout->p_sys->b_mouse_pointer_visible )
1178     {
1179         ToggleCursor( p_vout );
1180         ToggleCursor( p_vout );
1181     }
1182
1183     /* Do NOT use XFlush here ! */
1184     XSync( p_vout->p_sys->p_display, False );
1185
1186     /* At this stage, the window is open, displayed, and ready to
1187      * receive data */
1188     p_vout->p_sys->p_win = p_win;
1189
1190     return VLC_SUCCESS;
1191 }
1192
1193 /*****************************************************************************
1194  * DestroyWindow: destroy the window
1195  *****************************************************************************
1196  *
1197  *****************************************************************************/
1198 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1199 {
1200     /* Do NOT use XFlush here ! */
1201     XSync( p_vout->p_sys->p_display, False );
1202
1203     if( p_win->video_window != None )
1204         XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1205
1206     XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1207
1208     XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1209     XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1210
1211     if( p_win->owner_window )
1212         vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1213 }
1214
1215 /*****************************************************************************
1216  * NewPicture: allocate a picture
1217  *****************************************************************************
1218  * Returns 0 on success, -1 otherwise
1219  *****************************************************************************/
1220 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1221 {
1222 #ifndef MODULE_NAME_IS_glx
1223
1224 #ifdef MODULE_NAME_IS_xvideo
1225     int i_plane;
1226 #endif
1227
1228     /* We know the chroma, allocate a buffer which will be used
1229      * directly by the decoder */
1230     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1231
1232     if( p_pic->p_sys == NULL )
1233     {
1234         return -1;
1235     }
1236
1237     /* Fill in picture_t fields */
1238     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1239                       p_vout->output.i_width, p_vout->output.i_height,
1240                       p_vout->output.i_aspect );
1241
1242 #ifdef HAVE_SYS_SHM_H
1243     if( p_vout->p_sys->b_shm )
1244     {
1245         /* Create image using XShm extension */
1246         p_pic->p_sys->p_image =
1247             CreateShmImage( p_vout, p_vout->p_sys->p_display,
1248 #   ifdef MODULE_NAME_IS_xvideo
1249                             p_vout->p_sys->i_xvport, 
1250                             VLC2X11_FOURCC(p_vout->output.i_chroma),
1251 #   else
1252                             p_vout->p_sys->p_visual,
1253                             p_vout->p_sys->i_screen_depth,
1254 #   endif
1255                             &p_pic->p_sys->shminfo,
1256                             p_vout->output.i_width, p_vout->output.i_height );
1257     }
1258
1259     if( !p_vout->p_sys->b_shm || !p_pic->p_sys->p_image )
1260 #endif /* HAVE_SYS_SHM_H */
1261     {
1262         /* Create image without XShm extension */
1263         p_pic->p_sys->p_image =
1264             CreateImage( p_vout, p_vout->p_sys->p_display,
1265 #ifdef MODULE_NAME_IS_xvideo
1266                          p_vout->p_sys->i_xvport, 
1267                          VLC2X11_FOURCC(p_vout->output.i_chroma),
1268                          p_pic->format.i_bits_per_pixel,
1269 #else
1270                          p_vout->p_sys->p_visual,
1271                          p_vout->p_sys->i_screen_depth,
1272                          p_vout->p_sys->i_bytes_per_pixel,
1273 #endif
1274                          p_vout->output.i_width, p_vout->output.i_height );
1275
1276 #ifdef HAVE_SYS_SHM_H
1277         if( p_pic->p_sys->p_image && p_vout->p_sys->b_shm )
1278         {
1279             msg_Warn( p_vout, "couldn't create SHM image, disabling SHM." );
1280             p_vout->p_sys->b_shm = VLC_FALSE;
1281         }
1282 #endif /* HAVE_SYS_SHM_H */
1283     }
1284
1285     if( p_pic->p_sys->p_image == NULL )
1286     {
1287         free( p_pic->p_sys );
1288         return -1;
1289     }
1290
1291     switch( p_vout->output.i_chroma )
1292     {
1293 #ifdef MODULE_NAME_IS_xvideo
1294         case VLC_FOURCC('I','4','2','0'):
1295         case VLC_FOURCC('Y','V','1','2'):
1296         case VLC_FOURCC('Y','2','1','1'):
1297         case VLC_FOURCC('Y','U','Y','2'):
1298         case VLC_FOURCC('U','Y','V','Y'):
1299         case VLC_FOURCC('R','V','1','5'):
1300         case VLC_FOURCC('R','V','1','6'):
1301         case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1302         case VLC_FOURCC('R','V','3','2'):
1303
1304             for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1305                  i_plane++ )
1306             {
1307                 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1308                     + p_pic->p_sys->p_image->offsets[i_plane];
1309                 p_pic->p[i_plane].i_pitch =
1310                     p_pic->p_sys->p_image->pitches[i_plane];
1311             }
1312             if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1313             {
1314                 /* U and V inverted compared to I420
1315                  * Fixme: this should be handled by the vout core */
1316                 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1317                     + p_pic->p_sys->p_image->offsets[2];
1318                 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1319                     + p_pic->p_sys->p_image->offsets[1];
1320             }
1321             break;
1322
1323 #else
1324         case VLC_FOURCC('R','G','B','2'):
1325         case VLC_FOURCC('R','V','1','6'):
1326         case VLC_FOURCC('R','V','1','5'):
1327         case VLC_FOURCC('R','V','2','4'):
1328         case VLC_FOURCC('R','V','3','2'):
1329
1330             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1331             p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1332             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1333                                   + p_pic->p_sys->p_image->xoffset;
1334             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1335
1336             /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1337              * properly by vout_InitPicture() */
1338             p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1339                                          * p_pic->p_sys->p_image->width;
1340             break;
1341 #endif
1342
1343         default:
1344             /* Unknown chroma, tell the guy to get lost */
1345             IMAGE_FREE( p_pic->p_sys->p_image );
1346             free( p_pic->p_sys );
1347             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1348                      p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1349             p_pic->i_planes = 0;
1350             return -1;
1351     }
1352
1353 #endif /* !MODULE_NAME_IS_glx */
1354
1355     return 0;
1356 }
1357
1358 /*****************************************************************************
1359  * FreePicture: destroy a picture allocated with NewPicture
1360  *****************************************************************************
1361  * Destroy XImage AND associated data. If using Shm, detach shared memory
1362  * segment from server and process, then free it. The XDestroyImage manpage
1363  * says that both the image structure _and_ the data pointed to by the
1364  * image structure are freed, so no need to free p_image->data.
1365  *****************************************************************************/
1366 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1367 {
1368     /* The order of operations is correct */
1369 #ifdef HAVE_SYS_SHM_H
1370     if( p_vout->p_sys->b_shm )
1371     {
1372         XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1373         IMAGE_FREE( p_pic->p_sys->p_image );
1374
1375         shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1376         if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1377         {
1378             msg_Err( p_vout, "cannot detach shared memory (%s)",
1379                              strerror(errno) );
1380         }
1381     }
1382     else
1383 #endif
1384     {
1385         IMAGE_FREE( p_pic->p_sys->p_image );
1386     }
1387
1388     /* Do NOT use XFlush here ! */
1389     XSync( p_vout->p_sys->p_display, False );
1390
1391     free( p_pic->p_sys );
1392 }
1393
1394 /*****************************************************************************
1395  * ToggleFullScreen: Enable or disable full screen mode
1396  *****************************************************************************
1397  * This function will switch between fullscreen and window mode.
1398  *****************************************************************************/
1399 static void ToggleFullScreen ( vout_thread_t *p_vout )
1400 {
1401     Atom prop;
1402     XEvent xevent;
1403     mwmhints_t mwmhints;
1404     XSetWindowAttributes attributes;
1405
1406 #ifdef HAVE_XINERAMA
1407     int i_d1, i_d2;
1408 #endif
1409
1410     p_vout->b_fullscreen = !p_vout->b_fullscreen;
1411
1412     if( p_vout->b_fullscreen )
1413     {
1414         msg_Dbg( p_vout, "entering fullscreen mode" );
1415
1416         p_vout->p_sys->b_altfullscreen =
1417             config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1418
1419         XUnmapWindow( p_vout->p_sys->p_display,
1420                       p_vout->p_sys->p_win->base_window );
1421
1422         p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1423
1424         CreateWindow( p_vout, p_vout->p_sys->p_win );
1425         XDestroyWindow( p_vout->p_sys->p_display,
1426                         p_vout->p_sys->fullscreen_window.video_window );
1427         XReparentWindow( p_vout->p_sys->p_display,
1428                          p_vout->p_sys->original_window.video_window,
1429                          p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
1430         p_vout->p_sys->fullscreen_window.video_window =
1431             p_vout->p_sys->original_window.video_window;
1432
1433         /* To my knowledge there are two ways to create a borderless window.
1434          * There's the generic way which is to tell x to bypass the window
1435          * manager, but this creates problems with the focus of other
1436          * applications.
1437          * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1438          * luckily seems to be supported by most window managers. */
1439         if( !p_vout->p_sys->b_altfullscreen )
1440         {
1441             mwmhints.flags = MWM_HINTS_DECORATIONS;
1442             mwmhints.decorations = False;
1443
1444             prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1445                                 False );
1446             XChangeProperty( p_vout->p_sys->p_display,
1447                              p_vout->p_sys->p_win->base_window,
1448                              prop, prop, 32, PropModeReplace,
1449                              (unsigned char *)&mwmhints,
1450                              PROP_MWM_HINTS_ELEMENTS );
1451         }
1452         else
1453         {
1454             /* brute force way to remove decorations */
1455             attributes.override_redirect = True;
1456             XChangeWindowAttributes( p_vout->p_sys->p_display,
1457                                      p_vout->p_sys->p_win->base_window,
1458                                      CWOverrideRedirect,
1459                                      &attributes);
1460
1461             /* Make sure the change is effective */
1462             XReparentWindow( p_vout->p_sys->p_display,
1463                              p_vout->p_sys->p_win->base_window,
1464                              DefaultRootWindow( p_vout->p_sys->p_display ),
1465                              0, 0 );
1466         }
1467
1468         if( p_vout->p_sys->b_net_wm_state_fullscreen )
1469         {
1470             XClientMessageEvent event;
1471
1472             memset( &event, 0, sizeof( XClientMessageEvent ) );
1473
1474             event.type = ClientMessage;
1475             event.message_type = p_vout->p_sys->net_wm_state;
1476             event.display = p_vout->p_sys->p_display;
1477             event.window = p_vout->p_sys->p_win->base_window;
1478             event.format = 32;
1479             event.data.l[ 0 ] = 1; /* set property */
1480             event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
1481
1482             XSendEvent( p_vout->p_sys->p_display,
1483                         DefaultRootWindow( p_vout->p_sys->p_display ),
1484                         False, SubstructureRedirectMask,
1485                         (XEvent*)&event );
1486         }
1487
1488         /* Make sure the change is effective */
1489         XReparentWindow( p_vout->p_sys->p_display,
1490                          p_vout->p_sys->p_win->base_window,
1491                          DefaultRootWindow( p_vout->p_sys->p_display ),
1492                          0, 0 );
1493
1494 #ifdef HAVE_XINERAMA
1495         if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
1496             XineramaIsActive( p_vout->p_sys->p_display ) )
1497         {
1498             XineramaScreenInfo *screens;   /* infos for xinerama */
1499             int i_num_screens;
1500
1501             msg_Dbg( p_vout, "using XFree Xinerama extension");
1502
1503 #define SCREEN p_vout->p_sys->p_win->i_screen
1504
1505             /* Get Information about Xinerama (num of screens) */
1506             screens = XineramaQueryScreens( p_vout->p_sys->p_display,
1507                                             &i_num_screens );
1508
1509             if( !SCREEN )
1510                 SCREEN = config_GetInt( p_vout,
1511                                         MODULE_STRING "-xineramascreen" );
1512
1513             /* just check that user has entered a good value */
1514             if( SCREEN >= i_num_screens || SCREEN < 0 )
1515             {
1516                 msg_Dbg( p_vout, "requested screen number invalid" );
1517                 SCREEN = 0;
1518             }
1519
1520             /* Get the X/Y upper left corner coordinate of the above screen */
1521             p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
1522             p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
1523
1524             /* Set the Height/width to the screen resolution */
1525             p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
1526             p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
1527
1528             XFree(screens);
1529
1530 #undef SCREEN
1531
1532         }
1533         else
1534 #endif
1535         {
1536             /* The window wasn't necessarily created at the requested size */
1537             p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
1538             p_vout->p_sys->p_win->i_width =
1539                 DisplayWidth( p_vout->p_sys->p_display,
1540                               p_vout->p_sys->i_screen );
1541             p_vout->p_sys->p_win->i_height =
1542                 DisplayHeight( p_vout->p_sys->p_display,
1543                                p_vout->p_sys->i_screen );
1544         }
1545
1546         XMoveResizeWindow( p_vout->p_sys->p_display,
1547                            p_vout->p_sys->p_win->base_window,
1548                            p_vout->p_sys->p_win->i_x,
1549                            p_vout->p_sys->p_win->i_y,
1550                            p_vout->p_sys->p_win->i_width,
1551                            p_vout->p_sys->p_win->i_height );
1552     }
1553     else
1554     {
1555         msg_Dbg( p_vout, "leaving fullscreen mode" );
1556
1557         XReparentWindow( p_vout->p_sys->p_display,
1558                          p_vout->p_sys->original_window.video_window,
1559                          p_vout->p_sys->original_window.base_window, 0, 0 );
1560
1561         p_vout->p_sys->fullscreen_window.video_window = None;
1562         DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1563         p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1564
1565         XMapWindow( p_vout->p_sys->p_display,
1566                     p_vout->p_sys->p_win->base_window );
1567     }
1568
1569     /* Unfortunately, using XSync() here is not enough to ensure the
1570      * window has already been mapped because the XMapWindow() request
1571      * has not necessarily been sent directly to our window (remember,
1572      * the call is first redirected to the window manager) */
1573     do
1574     {
1575         XWindowEvent( p_vout->p_sys->p_display,
1576                       p_vout->p_sys->p_win->base_window,
1577                       StructureNotifyMask, &xevent );
1578     } while( xevent.type != MapNotify );
1579
1580     /* Be careful, this can generate a BadMatch error if the window is not
1581      * already mapped by the server (see above) */
1582     XSetInputFocus(p_vout->p_sys->p_display,
1583                    p_vout->p_sys->p_win->base_window,
1584                    RevertToParent,
1585                    CurrentTime);
1586
1587     /* signal that the size needs to be updated */
1588     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1589 }
1590
1591 /*****************************************************************************
1592  * EnableXScreenSaver: enable screen saver
1593  *****************************************************************************
1594  * This function enables the screen saver on a display after it has been
1595  * disabled by XDisableScreenSaver.
1596  * FIXME: what happens if multiple vlc sessions are running at the same
1597  *        time ???
1598  *****************************************************************************/
1599 static void EnableXScreenSaver( vout_thread_t *p_vout )
1600 {
1601 #ifdef DPMSINFO_IN_DPMS_H
1602     int dummy;
1603 #endif
1604
1605     if( p_vout->p_sys->i_ss_timeout )
1606     {
1607         XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1608                          p_vout->p_sys->i_ss_interval,
1609                          p_vout->p_sys->i_ss_blanking,
1610                          p_vout->p_sys->i_ss_exposure );
1611     }
1612
1613     /* Restore DPMS settings */
1614 #ifdef DPMSINFO_IN_DPMS_H
1615     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1616     {
1617         if( p_vout->p_sys->b_ss_dpms )
1618         {
1619             DPMSEnable( p_vout->p_sys->p_display );
1620         }
1621     }
1622 #endif
1623 }
1624
1625 /*****************************************************************************
1626  * DisableXScreenSaver: disable screen saver
1627  *****************************************************************************
1628  * See XEnableXScreenSaver
1629  *****************************************************************************/
1630 static void DisableXScreenSaver( vout_thread_t *p_vout )
1631 {
1632 #ifdef DPMSINFO_IN_DPMS_H
1633     int dummy;
1634 #endif
1635
1636     /* Save screen saver information */
1637     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1638                      &p_vout->p_sys->i_ss_interval,
1639                      &p_vout->p_sys->i_ss_blanking,
1640                      &p_vout->p_sys->i_ss_exposure );
1641
1642     /* Disable screen saver */
1643     if( p_vout->p_sys->i_ss_timeout )
1644     {
1645         XSetScreenSaver( p_vout->p_sys->p_display, 0,
1646                          p_vout->p_sys->i_ss_interval,
1647                          p_vout->p_sys->i_ss_blanking,
1648                          p_vout->p_sys->i_ss_exposure );
1649     }
1650
1651     /* Disable DPMS */
1652 #ifdef DPMSINFO_IN_DPMS_H
1653     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1654     {
1655         CARD16 unused;
1656         /* Save DPMS current state */
1657         DPMSInfo( p_vout->p_sys->p_display, &unused,
1658                   &p_vout->p_sys->b_ss_dpms );
1659         DPMSDisable( p_vout->p_sys->p_display );
1660    }
1661 #endif
1662 }
1663
1664 /*****************************************************************************
1665  * CreateCursor: create a blank mouse pointer
1666  *****************************************************************************/
1667 static void CreateCursor( vout_thread_t *p_vout )
1668 {
1669     XColor cursor_color;
1670
1671     p_vout->p_sys->cursor_pixmap =
1672         XCreatePixmap( p_vout->p_sys->p_display,
1673                        DefaultRootWindow( p_vout->p_sys->p_display ),
1674                        1, 1, 1 );
1675
1676     XParseColor( p_vout->p_sys->p_display,
1677                  XCreateColormap( p_vout->p_sys->p_display,
1678                                   DefaultRootWindow(
1679                                                     p_vout->p_sys->p_display ),
1680                                   DefaultVisual(
1681                                                 p_vout->p_sys->p_display,
1682                                                 p_vout->p_sys->i_screen ),
1683                                   AllocNone ),
1684                  "black", &cursor_color );
1685
1686     p_vout->p_sys->blank_cursor =
1687         XCreatePixmapCursor( p_vout->p_sys->p_display,
1688                              p_vout->p_sys->cursor_pixmap,
1689                              p_vout->p_sys->cursor_pixmap,
1690                              &cursor_color, &cursor_color, 1, 1 );
1691 }
1692
1693 /*****************************************************************************
1694  * DestroyCursor: destroy the blank mouse pointer
1695  *****************************************************************************/
1696 static void DestroyCursor( vout_thread_t *p_vout )
1697 {
1698     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1699 }
1700
1701 /*****************************************************************************
1702  * ToggleCursor: hide or show the mouse pointer
1703  *****************************************************************************
1704  * This function hides the X pointer if it is visible by setting the pointer
1705  * sprite to a blank one. To show it again, we disable the sprite.
1706  *****************************************************************************/
1707 static void ToggleCursor( vout_thread_t *p_vout )
1708 {
1709     if( p_vout->p_sys->b_mouse_pointer_visible )
1710     {
1711         XDefineCursor( p_vout->p_sys->p_display,
1712                        p_vout->p_sys->p_win->base_window,
1713                        p_vout->p_sys->blank_cursor );
1714         p_vout->p_sys->b_mouse_pointer_visible = 0;
1715     }
1716     else
1717     {
1718         XUndefineCursor( p_vout->p_sys->p_display,
1719                          p_vout->p_sys->p_win->base_window );
1720         p_vout->p_sys->b_mouse_pointer_visible = 1;
1721     }
1722 }
1723
1724 #ifdef MODULE_NAME_IS_xvideo
1725 /*****************************************************************************
1726  * XVideoGetPort: get YUV12 port
1727  *****************************************************************************/
1728 static int XVideoGetPort( vout_thread_t *p_vout,
1729                           vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1730 {
1731     XvAdaptorInfo *p_adaptor;
1732     unsigned int i;
1733     int i_adaptor, i_num_adaptors, i_requested_adaptor;
1734     int i_selected_port;
1735
1736     switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1737     {
1738         case Success:
1739             break;
1740
1741         case XvBadExtension:
1742             msg_Warn( p_vout, "XvBadExtension" );
1743             return -1;
1744
1745         case XvBadAlloc:
1746             msg_Warn( p_vout, "XvBadAlloc" );
1747             return -1;
1748
1749         default:
1750             msg_Warn( p_vout, "XvQueryExtension failed" );
1751             return -1;
1752     }
1753
1754     switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1755                              DefaultRootWindow( p_vout->p_sys->p_display ),
1756                              &i_num_adaptors, &p_adaptor ) )
1757     {
1758         case Success:
1759             break;
1760
1761         case XvBadExtension:
1762             msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1763             return -1;
1764
1765         case XvBadAlloc:
1766             msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1767             return -1;
1768
1769         default:
1770             msg_Warn( p_vout, "XvQueryAdaptors failed" );
1771             return -1;
1772     }
1773
1774     i_selected_port = -1;
1775     i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1776
1777     for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1778     {
1779         XvImageFormatValues *p_formats;
1780         int i_format, i_num_formats;
1781         int i_port;
1782
1783         /* If we requested an adaptor and it's not this one, we aren't
1784          * interested */
1785         if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1786         {
1787             continue;
1788         }
1789
1790         /* If the adaptor doesn't have the required properties, skip it */
1791         if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1792             !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1793         {
1794             continue;
1795         }
1796
1797         /* Check that adaptor supports our requested format... */
1798         p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1799                                         p_adaptor[i_adaptor].base_id,
1800                                         &i_num_formats );
1801
1802         for( i_format = 0;
1803              i_format < i_num_formats && ( i_selected_port == -1 );
1804              i_format++ )
1805         {
1806             XvAttribute     *p_attr;
1807             int             i_attr, i_num_attributes;
1808
1809             /* If this is not the format we want, or at least a
1810              * similar one, forget it */
1811             if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1812             {
1813                 continue;
1814             }
1815
1816             /* Look for the first available port supporting this format */
1817             for( i_port = p_adaptor[i_adaptor].base_id;
1818                  ( i_port < (int)(p_adaptor[i_adaptor].base_id
1819                                    + p_adaptor[i_adaptor].num_ports) )
1820                    && ( i_selected_port == -1 );
1821                  i_port++ )
1822             {
1823                 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1824                      == Success )
1825                 {
1826                     i_selected_port = i_port;
1827                     *pi_newchroma = p_formats[ i_format ].id;
1828                 }
1829             }
1830
1831             /* If no free port was found, forget it */
1832             if( i_selected_port == -1 )
1833             {
1834                 continue;
1835             }
1836
1837             /* If we found a port, print information about it */
1838             msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1839                      i_adaptor, i_selected_port, p_formats[ i_format ].id,
1840                      (char *)&p_formats[ i_format ].id,
1841                      ( p_formats[ i_format ].format == XvPacked ) ?
1842                          "packed" : "planar" );
1843
1844             /* Make sure XV_AUTOPAINT_COLORKEY is set */
1845             p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1846                                             i_selected_port,
1847                                             &i_num_attributes );
1848
1849             for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1850             {
1851                 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
1852                 {
1853                     const Atom autopaint =
1854                         XInternAtom( p_vout->p_sys->p_display,
1855                                      "XV_AUTOPAINT_COLORKEY", False );
1856                     XvSetPortAttribute( p_vout->p_sys->p_display,
1857                                         i_selected_port, autopaint, 1 );
1858                     break;
1859                 }
1860             }
1861
1862             if( p_attr != NULL )
1863             {
1864                 XFree( p_attr );
1865             }
1866         }
1867
1868         if( p_formats != NULL )
1869         {
1870             XFree( p_formats );
1871         }
1872
1873     }
1874
1875     if( i_num_adaptors > 0 )
1876     {
1877         XvFreeAdaptorInfo( p_adaptor );
1878     }
1879
1880     if( i_selected_port == -1 )
1881     {
1882         int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
1883         if( i_requested_adaptor == -1 )
1884         {
1885             msg_Warn( p_vout, "no free XVideo port found for format "
1886                       "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
1887         }
1888         else
1889         {
1890             msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1891                       "XVideo port for format 0x%.8x (%4.4s)",
1892                       i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
1893         }
1894     }
1895
1896     return i_selected_port;
1897 }
1898
1899 /*****************************************************************************
1900  * XVideoReleasePort: release YUV12 port
1901  *****************************************************************************/
1902 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1903 {
1904     XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1905 }
1906 #endif
1907
1908 /*****************************************************************************
1909  * InitDisplay: open and initialize X11 device
1910  *****************************************************************************
1911  * Create a window according to video output given size, and set other
1912  * properties according to the display properties.
1913  *****************************************************************************/
1914 static int InitDisplay( vout_thread_t *p_vout )
1915 {
1916 #ifdef MODULE_NAME_IS_x11
1917     XPixmapFormatValues *       p_formats;                 /* pixmap formats */
1918     XVisualInfo *               p_xvisual;            /* visuals information */
1919     XVisualInfo                 xvisual_template;         /* visual template */
1920     int                         i_count;                       /* array size */
1921 #endif
1922
1923 #ifdef HAVE_SYS_SHM_H
1924     p_vout->p_sys->b_shm = 0;
1925
1926     if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1927     {
1928 #   ifdef SYS_DARWIN
1929         /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1930 #   else
1931         p_vout->p_sys->b_shm =
1932                   ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1933 #   endif
1934
1935         if( !p_vout->p_sys->b_shm )
1936         {
1937             msg_Warn( p_vout, "XShm video extension is unavailable" );
1938         }
1939     }
1940     else
1941     {
1942         msg_Dbg( p_vout, "disabling XShm video extension" );
1943     }
1944
1945 #else
1946     msg_Warn( p_vout, "XShm video extension is unavailable" );
1947
1948 #endif
1949
1950 #ifdef MODULE_NAME_IS_xvideo
1951     /* XXX The brightness and contrast values should be read from environment
1952      * XXX variables... */
1953 #if 0
1954     XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1955     XVideoSetAttribute( p_vout, "XV_CONTRAST",   0.5 );
1956 #endif
1957 #endif
1958
1959 #ifdef MODULE_NAME_IS_x11
1960     /* Initialize structure */
1961     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1962
1963     /* Get screen depth */
1964     p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1965                                                    p_vout->p_sys->i_screen );
1966     switch( p_vout->p_sys->i_screen_depth )
1967     {
1968     case 8:
1969         /*
1970          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1971          */
1972         xvisual_template.screen =   p_vout->p_sys->i_screen;
1973         xvisual_template.class =    DirectColor;
1974         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1975                                     VisualScreenMask | VisualClassMask,
1976                                     &xvisual_template, &i_count );
1977         if( p_xvisual == NULL )
1978         {
1979             msg_Err( p_vout, "no PseudoColor visual available" );
1980             return VLC_EGENERIC;
1981         }
1982         p_vout->p_sys->i_bytes_per_pixel = 1;
1983         p_vout->output.pf_setpalette = SetPalette;
1984         break;
1985     case 15:
1986     case 16:
1987     case 24:
1988     default:
1989         /*
1990          * Screen depth is higher than 8bpp. TrueColor visual is used.
1991          */
1992         xvisual_template.screen =   p_vout->p_sys->i_screen;
1993         xvisual_template.class =    TrueColor;
1994         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1995                                     VisualScreenMask | VisualClassMask,
1996                                     &xvisual_template, &i_count );
1997         if( p_xvisual == NULL )
1998         {
1999             msg_Err( p_vout, "no TrueColor visual available" );
2000             return VLC_EGENERIC;
2001         }
2002
2003         p_vout->output.i_rmask = p_xvisual->red_mask;
2004         p_vout->output.i_gmask = p_xvisual->green_mask;
2005         p_vout->output.i_bmask = p_xvisual->blue_mask;
2006
2007         /* There is no difference yet between 3 and 4 Bpp. The only way
2008          * to find the actual number of bytes per pixel is to list supported
2009          * pixmap formats. */
2010         p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2011         p_vout->p_sys->i_bytes_per_pixel = 0;
2012
2013         for( ; i_count-- ; p_formats++ )
2014         {
2015             /* Under XFree4.0, the list contains pixmap formats available
2016              * through all video depths ; so we have to check against current
2017              * depth. */
2018             if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
2019             {
2020                 if( p_formats->bits_per_pixel / 8
2021                         > (int)p_vout->p_sys->i_bytes_per_pixel )
2022                 {
2023                     p_vout->p_sys->i_bytes_per_pixel =
2024                                                p_formats->bits_per_pixel / 8;
2025                 }
2026             }
2027         }
2028         break;
2029     }
2030     p_vout->p_sys->p_visual = p_xvisual->visual;
2031     XFree( p_xvisual );
2032 #endif
2033
2034     return VLC_SUCCESS;
2035 }
2036
2037 #ifdef HAVE_SYS_SHM_H
2038 /*****************************************************************************
2039  * CreateShmImage: create an XImage or XvImage using shared memory extension
2040  *****************************************************************************
2041  * Prepare an XImage or XvImage for display function.
2042  * The order of the operations respects the recommandations of the mit-shm
2043  * document by J.Corbet and K.Packard. Most of the parameters were copied from
2044  * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2045  *****************************************************************************/
2046 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2047                                     Display* p_display, EXTRA_ARGS_SHM,
2048                                     int i_width, int i_height )
2049 {
2050     IMAGE_TYPE *p_image;
2051     Status result;
2052
2053     /* Create XImage / XvImage */
2054 #ifdef MODULE_NAME_IS_xvideo
2055
2056     /* Make sure the buffer is aligned to multiple of 16 */
2057     i_height = ( i_height + 15 ) >> 4 << 4;
2058     i_width = ( i_width + 15 ) >> 4 << 4;
2059
2060     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2061                                 i_width, i_height, p_shm );
2062 #else
2063     p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2064                                p_shm, i_width, i_height );
2065 #endif
2066     if( p_image == NULL )
2067     {
2068         msg_Err( p_vout, "image creation failed" );
2069         return NULL;
2070     }
2071
2072     /* Allocate shared memory segment - 0776 set the access permission
2073      * rights (like umask), they are not yet supported by all X servers */
2074     p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2075     if( p_shm->shmid < 0 )
2076     {
2077         msg_Err( p_vout, "cannot allocate shared image data (%s)",
2078                          strerror( errno ) );
2079         IMAGE_FREE( p_image );
2080         return NULL;
2081     }
2082
2083     /* Attach shared memory segment to process (read/write) */
2084     p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2085     if(! p_shm->shmaddr )
2086     {
2087         msg_Err( p_vout, "cannot attach shared memory (%s)",
2088                          strerror(errno));
2089         IMAGE_FREE( p_image );
2090         shmctl( p_shm->shmid, IPC_RMID, 0 );
2091         return NULL;
2092     }
2093
2094     /* Read-only data. We won't be using XShmGetImage */
2095     p_shm->readOnly = True;
2096
2097     /* Attach shared memory segment to X server */
2098     XSynchronize( p_display, True );
2099     b_shm = VLC_TRUE;
2100     result = XShmAttach( p_display, p_shm );
2101     if( result == False || !b_shm )
2102     {
2103         msg_Err( p_vout, "cannot attach shared memory to X server" );
2104         IMAGE_FREE( p_image );
2105         shmctl( p_shm->shmid, IPC_RMID, 0 );
2106         shmdt( p_shm->shmaddr );
2107         return NULL;
2108     }
2109     XSynchronize( p_display, False );
2110
2111     /* Send image to X server. This instruction is required, since having
2112      * built a Shm XImage and not using it causes an error on XCloseDisplay,
2113      * and remember NOT to use XFlush ! */
2114     XSync( p_display, False );
2115
2116 #if 0
2117     /* Mark the shm segment to be removed when there are no more
2118      * attachements, so it is automatic on process exit or after shmdt */
2119     shmctl( p_shm->shmid, IPC_RMID, 0 );
2120 #endif
2121
2122     return p_image;
2123 }
2124 #endif
2125
2126 /*****************************************************************************
2127  * CreateImage: create an XImage or XvImage
2128  *****************************************************************************
2129  * Create a simple image used as a buffer.
2130  *****************************************************************************/
2131 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2132                                  Display *p_display, EXTRA_ARGS,
2133                                  int i_width, int i_height )
2134 {
2135     byte_t *    p_data;                           /* image data storage zone */
2136     IMAGE_TYPE *p_image;
2137 #ifdef MODULE_NAME_IS_x11
2138     int         i_quantum;                     /* XImage quantum (see below) */
2139     int         i_bytes_per_line;
2140 #endif
2141
2142     /* Allocate memory for image */
2143 #ifdef MODULE_NAME_IS_xvideo
2144
2145     /* Make sure the buffer is aligned to multiple of 16 */
2146     i_height = ( i_height + 15 ) >> 4 << 4;
2147     i_width = ( i_width + 15 ) >> 4 << 4;
2148
2149     p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2150 #elif defined(MODULE_NAME_IS_x11)
2151     i_bytes_per_line = i_width * i_bytes_per_pixel;
2152     p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2153 #endif
2154     if( !p_data )
2155     {
2156         msg_Err( p_vout, "out of memory" );
2157         return NULL;
2158     }
2159
2160 #ifdef MODULE_NAME_IS_x11
2161     /* Optimize the quantum of a scanline regarding its size - the quantum is
2162        a diviser of the number of bits between the start of two scanlines. */
2163     if( i_bytes_per_line & 0xf )
2164     {
2165         i_quantum = 0x8;
2166     }
2167     else if( i_bytes_per_line & 0x10 )
2168     {
2169         i_quantum = 0x10;
2170     }
2171     else
2172     {
2173         i_quantum = 0x20;
2174     }
2175 #endif
2176
2177     /* Create XImage. p_data will be automatically freed */
2178 #ifdef MODULE_NAME_IS_xvideo
2179     p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2180                              p_data, i_width, i_height );
2181 #elif defined(MODULE_NAME_IS_x11)
2182     p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2183                             p_data, i_width, i_height, i_quantum, 0 );
2184 #endif
2185     if( p_image == NULL )
2186     {
2187         msg_Err( p_vout, "XCreateImage() failed" );
2188         free( p_data );
2189         return NULL;
2190     }
2191
2192     return p_image;
2193 }
2194
2195 /*****************************************************************************
2196  * X11ErrorHandler: replace error handler so we can intercept some of them
2197  *****************************************************************************/
2198 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2199 {
2200     /* Ingnore errors on XSetInputFocus()
2201      * (they happen when a window is not yet mapped) */
2202     if( event->request_code == X_SetInputFocus )
2203     {
2204         fprintf(stderr, "XSetInputFocus failed\n");
2205         return 0;
2206     }
2207
2208     if( event->request_code == 150 /* MIT-SHM */ &&
2209         event->minor_code == X_ShmAttach )
2210     {
2211         fprintf(stderr, "XShmAttach failed\n");
2212         b_shm = VLC_FALSE;
2213         return 0;
2214     }
2215
2216     XSetErrorHandler(NULL);
2217     return (XSetErrorHandler(X11ErrorHandler))( display, event );
2218 }
2219
2220 #ifdef MODULE_NAME_IS_x11
2221 /*****************************************************************************
2222  * SetPalette: sets an 8 bpp palette
2223  *****************************************************************************
2224  * This function sets the palette given as an argument. It does not return
2225  * anything, but could later send information on which colors it was unable
2226  * to set.
2227  *****************************************************************************/
2228 static void SetPalette( vout_thread_t *p_vout,
2229                         uint16_t *red, uint16_t *green, uint16_t *blue )
2230 {
2231     int i;
2232     XColor p_colors[255];
2233
2234     /* allocate palette */
2235     for( i = 0; i < 255; i++ )
2236     {
2237         /* kludge: colors are indexed reversely because color 255 seems
2238          * to be reserved for black even if we try to set it to white */
2239         p_colors[ i ].pixel = 255 - i;
2240         p_colors[ i ].pad   = 0;
2241         p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2242         p_colors[ i ].red   = red[ 255 - i ];
2243         p_colors[ i ].blue  = blue[ 255 - i ];
2244         p_colors[ i ].green = green[ 255 - i ];
2245     }
2246
2247     XStoreColors( p_vout->p_sys->p_display,
2248                   p_vout->p_sys->colormap, p_colors, 255 );
2249 }
2250 #endif
2251
2252 /*****************************************************************************
2253  * Control: control facility for the vout
2254  *****************************************************************************/
2255 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2256 {
2257     double f_arg;
2258     vlc_bool_t b_arg;
2259
2260     switch( i_query )
2261     {
2262         case VOUT_SET_ZOOM:
2263             if( p_vout->p_sys->p_win->owner_window )
2264                 return vout_ControlWindow( p_vout,
2265                     (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2266
2267             f_arg = va_arg( args, double );
2268
2269             vlc_mutex_lock( &p_vout->p_sys->lock );
2270
2271             /* Update dimensions */
2272             /* FIXME: export InitWindowSize() from vout core */
2273             XResizeWindow( p_vout->p_sys->p_display,
2274                            p_vout->p_sys->p_win->base_window,
2275                            p_vout->i_window_width * f_arg,
2276                            p_vout->i_window_height * f_arg );
2277
2278             vlc_mutex_unlock( &p_vout->p_sys->lock );
2279             return VLC_SUCCESS;
2280
2281        case VOUT_CLOSE:
2282             vlc_mutex_lock( &p_vout->p_sys->lock );
2283             XUnmapWindow( p_vout->p_sys->p_display,
2284                           p_vout->p_sys->original_window.base_window );
2285             vlc_mutex_unlock( &p_vout->p_sys->lock );
2286             /* Fall through */
2287
2288        case VOUT_REPARENT:
2289             vlc_mutex_lock( &p_vout->p_sys->lock );
2290             XReparentWindow( p_vout->p_sys->p_display,
2291                              p_vout->p_sys->original_window.base_window,
2292                              DefaultRootWindow( p_vout->p_sys->p_display ),
2293                              0, 0 );
2294             XSync( p_vout->p_sys->p_display, False );
2295             p_vout->p_sys->original_window.owner_window = 0;
2296             vlc_mutex_unlock( &p_vout->p_sys->lock );
2297             return vout_vaControlDefault( p_vout, i_query, args );
2298
2299         case VOUT_SET_STAY_ON_TOP:
2300             if( p_vout->p_sys->p_win->owner_window )
2301                 return vout_ControlWindow( p_vout,
2302                     (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2303
2304             b_arg = va_arg( args, vlc_bool_t );
2305             vlc_mutex_lock( &p_vout->p_sys->lock );
2306             WindowOnTop( p_vout, b_arg );
2307             vlc_mutex_unlock( &p_vout->p_sys->lock );
2308             return VLC_SUCCESS;
2309
2310        default:
2311             return vout_vaControlDefault( p_vout, i_query, args );
2312     }
2313 }
2314
2315 /*****************************************************************************
2316  * TestNetWMSupport: tests for Extended Window Manager Hints support
2317  *****************************************************************************/
2318 static void TestNetWMSupport( vout_thread_t *p_vout )
2319 {
2320     int i_ret, i_format;
2321     unsigned long i, i_items, i_bytesafter;
2322     Atom net_wm_supported;
2323     union { Atom *p_atom; unsigned char *p_char; } p_args;
2324
2325     p_args.p_atom = NULL;
2326
2327     p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
2328     p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
2329     p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
2330     p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
2331
2332     net_wm_supported =
2333         XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
2334
2335     i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
2336                                 DefaultRootWindow( p_vout->p_sys->p_display ),
2337                                 net_wm_supported,
2338                                 0, 16384, False, AnyPropertyType,
2339                                 &net_wm_supported,
2340                                 &i_format, &i_items, &i_bytesafter,
2341                                 (unsigned char **)&p_args );
2342
2343     if( i_ret != Success || i_items == 0 ) return;
2344
2345     msg_Dbg( p_vout, "Window manager supports NetWM" );
2346
2347     p_vout->p_sys->net_wm_state =
2348         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
2349     p_vout->p_sys->net_wm_state_fullscreen =
2350         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
2351                      False );
2352     p_vout->p_sys->net_wm_state_above =
2353         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
2354     p_vout->p_sys->net_wm_state_below =
2355         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
2356     p_vout->p_sys->net_wm_state_stays_on_top =
2357         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
2358                      False );
2359
2360     for( i = 0; i < i_items; i++ )
2361     {
2362         if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
2363         {
2364             msg_Dbg( p_vout,
2365                      "Window manager supports _NET_WM_STATE_FULLSCREEN" );
2366             p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
2367         }
2368         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
2369         {
2370             msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
2371             p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
2372         }
2373         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
2374         {
2375             msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
2376             p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
2377         }
2378         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
2379         {
2380             msg_Dbg( p_vout,
2381                      "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
2382             p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
2383         }
2384     }
2385
2386     XFree( p_args.p_atom );
2387 }
2388
2389 /*****************************************************************************
2390  * Key events handling
2391  *****************************************************************************/
2392 static struct
2393 {
2394     int i_x11key;
2395     int i_vlckey;
2396
2397 } x11keys_to_vlckeys[] =
2398 {
2399     { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
2400     { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
2401     { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
2402     { XK_F12, KEY_F12 },
2403
2404     { XK_Return, KEY_ENTER },
2405     { XK_KP_Enter, KEY_ENTER },
2406     { XK_space, KEY_SPACE },
2407     { XK_Escape, KEY_ESC },
2408
2409     { XK_Menu, KEY_MENU },
2410     { XK_Left, KEY_LEFT },
2411     { XK_Right, KEY_RIGHT },
2412     { XK_Up, KEY_UP },
2413     { XK_Down, KEY_DOWN },
2414
2415     { XK_Home, KEY_HOME },
2416     { XK_End, KEY_END },
2417     { XK_Page_Up, KEY_PAGEUP },
2418     { XK_Page_Down, KEY_PAGEDOWN },
2419
2420     { XK_Insert, KEY_INSERT },
2421     { XK_Delete, KEY_DELETE },
2422
2423     { 0, 0 }
2424 };
2425
2426 static int ConvertKey( int i_key )
2427 {
2428     int i;
2429
2430     for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
2431     {
2432         if( x11keys_to_vlckeys[i].i_x11key == i_key )
2433         {
2434             return x11keys_to_vlckeys[i].i_vlckey;
2435         }
2436     }
2437
2438     return 0;
2439 }
2440
2441 /*****************************************************************************
2442  * WindowOnTop: Switches the "always on top" state of the video window.
2443  *****************************************************************************/
2444 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
2445 {
2446     if( p_vout->p_sys->b_net_wm_state_stays_on_top )
2447     {
2448         XClientMessageEvent event;
2449
2450         memset( &event, 0, sizeof( XClientMessageEvent ) );
2451
2452         event.type = ClientMessage;
2453         event.message_type = p_vout->p_sys->net_wm_state;
2454         event.display = p_vout->p_sys->p_display;
2455         event.window = p_vout->p_sys->p_win->base_window;
2456         event.format = 32;
2457         event.data.l[ 0 ] = b_on_top; /* set property */
2458         event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
2459
2460         XSendEvent( p_vout->p_sys->p_display,
2461                     DefaultRootWindow( p_vout->p_sys->p_display ),
2462                     False, SubstructureRedirectMask,
2463                     (XEvent*)&event );
2464     }
2465
2466     return VLC_SUCCESS;
2467 }