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