]> git.sesse.net Git - vlc/blob - plugins/x11/xcommon.c
* ./BUGS: added a list of known bugs. Please add your findings!
[vlc] / plugins / x11 / xcommon.c
1 /*****************************************************************************
2  * xcommon.c: Functions common to the X11 and XVideo plugins
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: xcommon.c,v 1.5 2002/01/04 14:01:34 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          David Kennedy <dkennedy@tinytoad.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                                /* free() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <videolan/vlc.h>
34
35 #ifdef HAVE_MACHINE_PARAM_H
36 /* BSD */
37 #include <machine/param.h>
38 #include <sys/types.h>                                     /* typedef ushort */
39 #include <sys/ipc.h>
40 #endif
41
42 #ifndef WIN32
43 #include <netinet/in.h>                               /* BSD: struct in_addr */
44 #endif
45
46 #include <sys/shm.h>                                   /* shmget(), shmctl() */
47 #include <X11/Xlib.h>
48 #include <X11/Xutil.h>
49 #include <X11/keysym.h>
50 #include <X11/extensions/XShm.h>
51 #include <X11/extensions/dpms.h>
52
53 #ifdef MODULE_NAME_IS_xvideo
54 #   include <X11/extensions/Xv.h>
55 #   include <X11/extensions/Xvlib.h>
56 #endif
57
58 #include "video.h"
59 #include "video_output.h"
60 #include "xcommon.h"
61
62 #include "interface.h"
63 #include "netutils.h"                                 /* network_ChannelJoin */
64
65 #include "stream_control.h"                 /* needed by input_ext-intf.h... */
66 #include "input_ext-intf.h"
67
68 #ifdef MODULE_NAME_IS_xvideo
69 #   define IMAGE_TYPE     XvImage
70 #   define EXTRA_ARGS     int i_xvport, int i_chroma
71 #   define EXTRA_ARGS_SHM int i_xvport, int i_chroma, XShmSegmentInfo *p_shm
72 #   define DATA_SIZE(p)   (p)->data_size
73 #   define IMAGE_FREE     XFree      /* There is nothing like XvDestroyImage */
74 #else
75 #   define IMAGE_TYPE     XImage
76 #   define EXTRA_ARGS     Visual *p_visual, int i_depth, int i_bytes_per_pixel
77 #   define EXTRA_ARGS_SHM Visual *p_visual, int i_depth, XShmSegmentInfo *p_shm
78 #   define DATA_SIZE(p)   ((p)->bytes_per_line * (p)->height)
79 #   define IMAGE_FREE     XDestroyImage
80 #endif
81
82 /*****************************************************************************
83  * Local prototypes
84  *****************************************************************************/
85 static int  vout_Probe     ( probedata_t * );
86 static int  vout_Create    ( vout_thread_t * );
87 static void vout_Destroy   ( vout_thread_t * );
88 static void vout_Render    ( vout_thread_t *, picture_t * );
89 static void vout_Display   ( vout_thread_t *, picture_t * );
90 static int  vout_Manage    ( vout_thread_t * );
91 static int  vout_Init      ( vout_thread_t * );
92 static void vout_End       ( vout_thread_t * );
93
94 static int  InitDisplay    ( vout_thread_t * );
95
96 static int  CreateWindow   ( vout_thread_t * );
97 static void DestroyWindow  ( vout_thread_t * );
98
99 static int  NewPicture     ( vout_thread_t *, picture_t * );
100 static void FreePicture    ( vout_thread_t *, picture_t * );
101
102 static IMAGE_TYPE *CreateImage    ( Display *, EXTRA_ARGS, int, int );
103 static IMAGE_TYPE *CreateShmImage ( Display *, EXTRA_ARGS_SHM, int, int );
104
105 static void ToggleFullScreen      ( vout_thread_t * );
106
107 static void EnableXScreenSaver    ( vout_thread_t * );
108 static void DisableXScreenSaver   ( vout_thread_t * );
109
110 static void CreateCursor   ( vout_thread_t * );
111 static void DestroyCursor  ( vout_thread_t * );
112 static void ToggleCursor   ( vout_thread_t * );
113
114 #ifdef MODULE_NAME_IS_xvideo
115 static int  XVideoGetPort         ( Display *, int );
116 static void XVideoReleasePort     ( Display *, int );
117 #endif
118
119 /*****************************************************************************
120  * vout_sys_t: video output method descriptor
121  *****************************************************************************
122  * This structure is part of the video output thread descriptor.
123  * It describes the X11 and XVideo specific properties of an output thread.
124  *****************************************************************************/
125 typedef struct vout_sys_s
126 {
127     /* Internal settings and properties */
128     Display *           p_display;                        /* display pointer */
129
130     Visual *            p_visual;                          /* visual pointer */
131     int                 i_screen;                           /* screen number */
132     Window              window;                               /* root window */
133     GC                  gc;              /* graphic context instance handler */
134
135     boolean_t           b_shm;               /* shared memory extension flag */
136
137 #ifdef MODULE_NAME_IS_xvideo
138     Window              yuv_window;   /* sub-window for displaying yuv video
139                                                                         data */
140     int                 i_xvport;
141     u32                 i_chroma;               /* the chroma we will select */
142 #else
143     Colormap            colormap;               /* colormap used (8bpp only) */
144
145     int                 i_screen_depth;
146     int                 i_bytes_per_pixel;
147     int                 i_bytes_per_line;
148     int                 i_red_mask;
149     int                 i_green_mask;
150     int                 i_blue_mask;
151 #endif
152
153     /* X11 generic properties */
154     Atom                wm_protocols;
155     Atom                wm_delete_window;
156
157     int                 i_width;                     /* width of main window */
158     int                 i_height;                   /* height of main window */
159
160     /* Backup of window position and size before fullscreen switch */
161     int                 i_width_backup;
162     int                 i_height_backup;
163     int                 i_xpos_backup;
164     int                 i_ypos_backup;
165
166     /* Screen saver properties */
167     int                 i_ss_timeout;                             /* timeout */
168     int                 i_ss_interval;           /* interval between changes */
169     int                 i_ss_blanking;                      /* blanking mode */
170     int                 i_ss_exposure;                      /* exposure mode */
171     BOOL                b_ss_dpms;                              /* DPMS mode */
172
173     /* Mouse pointer properties */
174     boolean_t           b_mouse_pointer_visible;
175     mtime_t             i_time_mouse_last_moved; /* used to auto-hide pointer*/
176     Cursor              blank_cursor;                   /* the hidden cursor */
177     Pixmap              cursor_pixmap;
178
179 } vout_sys_t;
180
181 /*****************************************************************************
182  * picture_sys_t: direct buffer method descriptor
183  *****************************************************************************
184  * This structure is part of the picture descriptor, it describes the
185  * XVideo specific properties of a direct buffer.
186  *****************************************************************************/
187 typedef struct picture_sys_s
188 {
189     IMAGE_TYPE *        p_image;
190
191     XShmSegmentInfo     shminfo;       /* shared memory zone information */
192
193 } picture_sys_t;
194
195 /*****************************************************************************
196  * mwmhints_t: window manager hints
197  *****************************************************************************
198  * Fullscreen needs to be able to hide the wm decorations so we provide
199  * this structure to make it easier.
200  *****************************************************************************/
201 #define MWM_HINTS_DECORATIONS   (1L << 1)
202 #define PROP_MWM_HINTS_ELEMENTS 5
203 typedef struct mwmhints_s
204 {
205     u32 flags;
206     u32 functions;
207     u32 decorations;
208     s32 input_mode;
209     u32 status;
210 } mwmhints_t;
211
212 /*****************************************************************************
213  * Chroma defines
214  *****************************************************************************/
215 #ifdef MODULE_NAME_IS_xvideo
216 #   define MAX_DIRECTBUFFERS 5
217 #else
218 #   define MAX_DIRECTBUFFERS 2
219 #endif
220
221 /*****************************************************************************
222  * Seeking function TODO: put this in a generic location !
223  *****************************************************************************/
224 static __inline__ void vout_Seek( off_t i_seek )
225 {
226 #define area p_main->p_intf->p_input->stream.p_selected_area
227     off_t i_tell = area->i_tell;
228
229     i_tell += i_seek * (off_t)50 * p_main->p_intf->p_input->stream.i_mux_rate;
230
231     i_tell = ( i_tell <= 0/*area->i_start*/ ) ? 0/*area->i_start*/
232            : ( i_tell >= area->i_size ) ? area->i_size
233            : i_tell;
234
235     input_Seek( p_main->p_intf->p_input, i_tell );
236 #undef area
237 }
238
239 /*****************************************************************************
240  * Return the best suited FourCC value for a given chroma. We use this
241  * because a decoder may output FOURCC_IYUV, which is exactly the same as
242  * FOURCC_I420, but X servers usually know FOURCC_I420 and not FOURCC_IYUV.
243  *****************************************************************************/
244 static __inline__ u32 BestChroma( u32 i_chroma )
245 {
246     /* XXX: don't forget to update vout_Init if you change this */
247     switch( i_chroma )
248     {
249         /* These ones are all the same */
250         case FOURCC_I420:
251         case FOURCC_IYUV:
252         case FOURCC_YV12:
253             return FOURCC_I420;
254
255         /* These ones are all the same */
256         case FOURCC_YUY2:
257         case FOURCC_YUNV:
258             return FOURCC_YUY2;
259
260         /* We know this one */
261         case FOURCC_Y211:
262             return FOURCC_Y211;
263
264         /* This is seldom supported, but we know how to convert */
265         case FOURCC_I422:
266             return FOURCC_YUY2;
267
268         default:
269             return i_chroma;
270             break;
271     }
272 }
273
274 /*****************************************************************************
275  * Functions exported as capabilities. They are declared as static so that
276  * we don't pollute the namespace too much.
277  *****************************************************************************/
278 void _M( vout_getfunctions )( function_list_t * p_function_list )
279 {
280     p_function_list->pf_probe = vout_Probe;
281     p_function_list->functions.vout.pf_create     = vout_Create;
282     p_function_list->functions.vout.pf_init       = vout_Init;
283     p_function_list->functions.vout.pf_end        = vout_End;
284     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
285     p_function_list->functions.vout.pf_manage     = vout_Manage;
286     p_function_list->functions.vout.pf_render     = vout_Render;
287     p_function_list->functions.vout.pf_display    = vout_Display;
288     p_function_list->functions.vout.pf_setpalette = NULL;
289 }
290
291 /*****************************************************************************
292  * vout_Probe: probe the video driver and return a score
293  *****************************************************************************
294  * This function tries to initialize SDL and returns a score to the
295  * plugin manager so that it can select the best plugin.
296  *****************************************************************************/
297 static int vout_Probe( probedata_t *p_data )
298 {
299     Display *p_display;                                   /* display pointer */
300     char    *psz_display;
301 #ifdef MODULE_NAME_IS_xvideo
302     int      i_xvport;
303 #endif
304
305     /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
306     psz_display = XDisplayName( main_GetPszVariable(VOUT_DISPLAY_VAR, NULL) );
307     p_display = XOpenDisplay( psz_display );
308     if( p_display == NULL )                                         /* error */
309     {
310         intf_WarnMsg( 3, "vout: cannot open display %s", psz_display );
311         return( 0 );
312     }
313
314 #ifdef MODULE_NAME_IS_xvideo
315     /* Check that there is an available XVideo port */
316     i_xvport = XVideoGetPort( p_display, BestChroma( p_data->vout.i_chroma ) );
317     if( i_xvport < 0 )
318     {
319         /* It failed, but it's not completely lost ! We try to open an
320          * XVideo port for a simple 16bpp RGB picture */
321         i_xvport = XVideoGetPort( p_display, FOURCC_RV16 );
322         if( i_xvport < 0 )
323         {
324             XCloseDisplay( p_display );
325             return( 0 );
326         }
327     }
328     XVideoReleasePort( p_display, i_xvport );
329 #endif
330
331     /* Clean-up everyting */
332     XCloseDisplay( p_display );
333
334 #ifdef MODULE_NAME_IS_xvideo 
335     return( 150 );
336 #else
337     return( 50 );
338 #endif
339 }
340
341 /*****************************************************************************
342  * vout_Create: allocate X11 video thread output method
343  *****************************************************************************
344  * This function allocate and initialize a X11 vout method. It uses some of the
345  * vout properties to choose the window size, and change them according to the
346  * actual properties of the display.
347  *****************************************************************************/
348 static int vout_Create( vout_thread_t *p_vout )
349 {
350     char *psz_display;
351
352     /* Allocate structure */
353     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
354     if( p_vout->p_sys == NULL )
355     {
356         intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
357         return( 1 );
358     }
359
360     /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
361     psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
362     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
363
364     if( p_vout->p_sys->p_display == NULL )                          /* error */
365     {
366         intf_ErrMsg( "vout error: cannot open display %s", psz_display );
367         free( p_vout->p_sys );
368         return( 1 );
369     }
370     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
371
372 #ifdef MODULE_NAME_IS_xvideo
373     /* Try to guess the chroma format we will be using */
374     p_vout->p_sys->i_chroma = BestChroma( p_vout->render.i_chroma );
375
376     /* Check that we have access to an XVideo port providing this chroma */
377     p_vout->p_sys->i_xvport = XVideoGetPort( p_vout->p_sys->p_display,
378                                              p_vout->p_sys->i_chroma );
379     if( p_vout->p_sys->i_xvport < 0 )
380     {
381         /* It failed, but it's not completely lost ! We try to open an
382          * XVideo port for a simple 16bpp RGB picture */
383         p_vout->p_sys->i_xvport = XVideoGetPort( p_vout->p_sys->p_display,
384                                                  FOURCC_RV16 );
385         if( p_vout->p_sys->i_xvport < 0 )
386         {
387             XCloseDisplay( p_vout->p_sys->p_display );
388             free( p_vout->p_sys );
389             return 1;
390         }
391
392         /* This one worked ! That's better than nothing, it has HW scaling. */
393         p_vout->p_sys->i_chroma = FOURCC_RV16;
394     }
395 #endif
396
397     /* Create blank cursor (for mouse cursor autohiding) */
398     p_vout->p_sys->b_mouse_pointer_visible = 1;
399     CreateCursor( p_vout );
400
401     /* Spawn base window - this window will include the video output window,
402      * but also command buttons, subtitles and other indicators */
403     if( CreateWindow( p_vout ) )
404     {
405         intf_ErrMsg( "vout error: cannot create X11 window" );
406         DestroyCursor( p_vout );
407         XCloseDisplay( p_vout->p_sys->p_display );
408         free( p_vout->p_sys );
409         return( 1 );
410     }
411
412     /* Open and initialize device. */
413     if( InitDisplay( p_vout ) )
414     {
415         intf_ErrMsg( "vout error: cannot initialize X11 display" );
416         DestroyCursor( p_vout );
417         DestroyWindow( p_vout );
418         XCloseDisplay( p_vout->p_sys->p_display );
419         free( p_vout->p_sys );
420         return( 1 );
421     }
422
423     /* Disable screen saver and return */
424     DisableXScreenSaver( p_vout );
425
426     return( 0 );
427 }
428
429 /*****************************************************************************
430  * vout_Destroy: destroy X11 video thread output method
431  *****************************************************************************
432  * Terminate an output method created by vout_CreateOutputMethod
433  *****************************************************************************/
434 static void vout_Destroy( vout_thread_t *p_vout )
435 {
436     /* Restore cursor if it was blanked */
437     if( !p_vout->p_sys->b_mouse_pointer_visible )
438     {
439         ToggleCursor( p_vout );
440     }
441
442 #ifdef MODULE_NAME_IS_xvideo   
443     XVideoReleasePort( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport );
444 #else
445 #if 0
446     /* Destroy colormap */
447     if( p_vout->p_sys->i_screen_depth == 8 )
448     {
449         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
450     }
451 #endif
452 #endif
453
454     DestroyCursor( p_vout );
455     EnableXScreenSaver( p_vout );
456     DestroyWindow( p_vout );
457
458     XCloseDisplay( p_vout->p_sys->p_display );
459
460     /* Destroy structure */
461     free( p_vout->p_sys );
462 }
463
464 /*****************************************************************************
465  * vout_Init: initialize X11 video thread output method
466  *****************************************************************************
467  * This function create the XImages needed by the output thread. It is called
468  * at the beginning of the thread, but also each time the window is resized.
469  *****************************************************************************/
470 static int vout_Init( vout_thread_t *p_vout )
471 {
472     int i_index;
473     picture_t *p_pic;
474
475     I_OUTPUTPICTURES = 0;
476
477 #ifdef MODULE_NAME_IS_xvideo
478     /* Initialize the output structure ; we already found an XVideo port,
479      * which either suits p_vout->render.i_chroma or p_vout->p_sys->i_chroma
480      * if nothing was found at the first attempt. */
481     switch( p_vout->render.i_chroma )
482     {
483         /* These ones are equivalent to what we chose instead, for instance
484          * we tell the video output we can do FOURCC_IYUV even though we
485          * actually do FOURCC_I420. */
486         case FOURCC_I420:
487         case FOURCC_IYUV:
488         case FOURCC_YV12:
489
490         case FOURCC_YUY2:
491         case FOURCC_YUNV:
492
493         case FOURCC_Y211:
494             p_vout->output.i_chroma = p_vout->render.i_chroma;
495             p_vout->output.i_width  = p_vout->render.i_width;
496             p_vout->output.i_height = p_vout->render.i_height;
497             p_vout->output.i_aspect = p_vout->render.i_aspect;
498             break;
499
500         /* These ones aren't equivalent to what we chose, so we need to
501          * warn the decoder that we chose another chroma and that it has
502          * conversion to do. */
503
504         /* At least for this one we can have the aspect ratio */
505         case FOURCC_I422:
506             p_vout->output.i_chroma = p_vout->p_sys->i_chroma;
507             p_vout->output.i_width  = p_vout->render.i_width;
508             p_vout->output.i_height = p_vout->render.i_height;
509             p_vout->output.i_aspect = p_vout->render.i_aspect;
510             break;
511
512         /* Here we don't even control the aspect ratio */
513         default:
514             p_vout->output.i_chroma = p_vout->p_sys->i_chroma;
515             p_vout->output.i_width  = p_vout->render.i_width;
516             p_vout->output.i_height = p_vout->render.i_height;
517             p_vout->output.i_aspect = p_vout->p_sys->i_width
518                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
519             break;
520     }
521
522 #else
523     /* Initialize the output structure: RGB with square pixels, whatever
524      * the input format is, since it's the only format we know */
525     switch( p_vout->p_sys->i_screen_depth )
526     {
527         case 8: /* FIXME: set the palette */
528             p_vout->output.i_chroma = FOURCC_BI_RGB; break;
529         case 15:
530             p_vout->output.i_chroma = FOURCC_RV15; break;
531         case 16:
532             p_vout->output.i_chroma = FOURCC_RV16; break;
533         case 24:
534             p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
535         case 32:
536             p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
537         default:
538             intf_ErrMsg( "vout error: unknown screen depth" );
539             return( 0 );
540     }
541
542     p_vout->output.i_width = p_vout->p_sys->i_width;
543     p_vout->output.i_height = p_vout->p_sys->i_height;
544
545     /* Assume we have square pixels */
546     p_vout->output.i_aspect = p_vout->p_sys->i_width
547                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
548 #endif
549
550     /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
551     while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
552     {
553         p_pic = NULL;
554
555         /* Find an empty picture slot */
556         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
557         {
558             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
559             {
560                 p_pic = p_vout->p_picture + i_index;
561                 break;
562             }
563         }
564
565         /* Allocate the picture */
566         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
567         {
568             break;
569         }
570
571         p_pic->i_status = DESTROYED_PICTURE;
572         p_pic->i_type   = DIRECT_PICTURE;
573
574         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
575
576         I_OUTPUTPICTURES++;
577     }
578
579     return( 0 );
580 }
581
582 /*****************************************************************************
583  * vout_Render: render previously calculated output
584  *****************************************************************************/
585 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
586 {
587     ;
588 }
589
590  /*****************************************************************************
591  * vout_Display: displays previously rendered output
592  *****************************************************************************
593  * This function sends the currently rendered image to X11 server.
594  * (The Xv extension takes care of "double-buffering".)
595  *****************************************************************************/
596 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
597 {
598     int i_width, i_height, i_x, i_y;
599
600     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
601                        &i_x, &i_y, &i_width, &i_height );
602
603     if( p_vout->p_sys->b_shm )
604     {
605         /* Display rendered image using shared memory extension */
606 #ifdef MODULE_NAME_IS_xvideo
607         XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
608                        p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
609                        p_pic->p_sys->p_image, 0 /*src_x*/, 0 /*src_y*/,
610                        p_vout->output.i_width, p_vout->output.i_height,
611                        0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
612                        False /* Don't put True here or you'll waste your CPU */ );
613 #else
614         XShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->window,
615                       p_vout->p_sys->gc, p_pic->p_sys->p_image,
616                       0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
617                       p_vout->output.i_width, p_vout->output.i_height,
618                       False /* Don't put True here ! */ );
619 #endif
620     }
621     else
622     {
623         /* Use standard XPutImage -- this is gonna be slow ! */
624 #ifdef MODULE_NAME_IS_xvideo
625         XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
626                     p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
627                     p_pic->p_sys->p_image, 0 /*src_x*/, 0 /*src_y*/,
628                     p_vout->output.i_width, p_vout->output.i_height,
629                     0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
630 #else
631         XPutImage( p_vout->p_sys->p_display, p_vout->p_sys->window,
632                    p_vout->p_sys->gc, p_pic->p_sys->p_image,
633                    0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
634                    p_vout->output.i_width, p_vout->output.i_height );
635 #endif
636     }
637
638     /* Make sure the command is sent now */
639     XFlush( p_vout->p_sys->p_display );
640 }
641
642 /*****************************************************************************
643  * vout_Manage: handle X11 events
644  *****************************************************************************
645  * This function should be called regularly by video output thread. It manages
646  * X11 events and allows window resizing. It returns a non null value on
647  * error.
648  *****************************************************************************/
649 static int vout_Manage( vout_thread_t *p_vout )
650 {
651     XEvent      xevent;                                         /* X11 event */
652     boolean_t   b_resized;                        /* window has been resized */
653     char        i_key;                                    /* ISO Latin-1 key */
654     KeySym      x_key_symbol;
655
656     /* Handle X11 events: ConfigureNotify events are parsed to know if the
657      * output window's size changed, MapNotify and UnmapNotify to know if the
658      * window is mapped (and if the display is useful), and ClientMessages
659      * to intercept window destruction requests */
660
661     b_resized = 0;
662     while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
663                               StructureNotifyMask | KeyPressMask |
664                               ButtonPressMask | ButtonReleaseMask | 
665                               PointerMotionMask | Button1MotionMask , &xevent )
666            == True )
667     {
668         /* ConfigureNotify event: prepare  */
669         if( (xevent.type == ConfigureNotify)
670           && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
671              || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
672         {
673             /* Update dimensions */
674             b_resized = 1;
675             p_vout->i_changes |= VOUT_SIZE_CHANGE;
676             p_vout->p_sys->i_width = xevent.xconfigure.width;
677             p_vout->p_sys->i_height = xevent.xconfigure.height;
678         }
679         /* MapNotify event: change window status and disable screen saver */
680         else if( xevent.type == MapNotify)
681         {
682             if( (p_vout != NULL) && !p_vout->b_active )
683             {
684                 DisableXScreenSaver( p_vout );
685                 p_vout->b_active = 1;
686             }
687         }
688         /* UnmapNotify event: change window status and enable screen saver */
689         else if( xevent.type == UnmapNotify )
690         {
691             if( (p_vout != NULL) && p_vout->b_active )
692             {
693                 EnableXScreenSaver( p_vout );
694                 p_vout->b_active = 0;
695             }
696         }
697         /* Keyboard event */
698         else if( xevent.type == KeyPress )
699         {
700             /* We may have keys like F1 trough F12, ESC ... */
701             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
702                                              xevent.xkey.keycode, 0 );
703             switch( x_key_symbol )
704             {
705                  case XK_Escape:
706                      p_main->p_intf->b_die = 1;
707                      break;
708                  case XK_Menu:
709                      p_main->p_intf->b_menu_change = 1;
710                      break;
711                  case XK_Left:
712                      vout_Seek( -5 );
713                      break;
714                  case XK_Right:
715                      vout_Seek( 5 );
716                      break;
717                  case XK_Up:
718                      vout_Seek( 60 );
719                      break;
720                  case XK_Down:
721                      vout_Seek( -60 );
722                      break;
723                  case XK_Home:
724                      input_Seek( p_main->p_intf->p_input,
725                      p_main->p_intf->p_input->stream.p_selected_area->i_start );
726                      break;
727                  case XK_End:
728                      input_Seek( p_main->p_intf->p_input,
729                      p_main->p_intf->p_input->stream.p_selected_area->i_size );
730                      break;
731                  case XK_Page_Up:
732                      vout_Seek( 900 );
733                      break;
734                  case XK_Page_Down:
735                      vout_Seek( -900 );
736                      break;
737                  case XK_space:
738                      input_SetStatus( p_main->p_intf->p_input,
739                                       INPUT_STATUS_PAUSE );
740                      break;
741
742                  default:
743                      /* "Normal Keys"
744                       * The reason why I use this instead of XK_0 is that 
745                       * with XLookupString, we don't have to care about
746                       * keymaps. */
747
748                     if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
749                     {
750                         /* FIXME: handle stuff here */
751                         switch( i_key )
752                         {
753                         case 'q':
754                         case 'Q':
755                             p_main->p_intf->b_die = 1;
756                             break;
757                         case 'f':
758                         case 'F':
759                             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
760                             break;
761
762                         case '0': network_ChannelJoin( 0 ); break;
763                         case '1': network_ChannelJoin( 1 ); break;
764                         case '2': network_ChannelJoin( 2 ); break;
765                         case '3': network_ChannelJoin( 3 ); break;
766                         case '4': network_ChannelJoin( 4 ); break;
767                         case '5': network_ChannelJoin( 5 ); break;
768                         case '6': network_ChannelJoin( 6 ); break;
769                         case '7': network_ChannelJoin( 7 ); break;
770                         case '8': network_ChannelJoin( 8 ); break;
771                         case '9': network_ChannelJoin( 9 ); break;
772
773                         default:
774                             intf_DbgMsg( "vout: unhandled key '%c' (%i)", 
775                                          (char)i_key, i_key );
776                             break;
777                         }
778                     }
779                 break;
780             }
781         }
782         /* Mouse click */
783         else if( xevent.type == ButtonPress )
784         {
785             switch( ((XButtonEvent *)&xevent)->button )
786             {
787                 case Button1:
788                     /* In this part we will eventually manage
789                      * clicks for DVD navigation for instance. For the
790                      * moment just pause the stream. */
791                     input_SetStatus( p_main->p_intf->p_input,
792                                      INPUT_STATUS_PAUSE );
793                     break;
794
795                 case Button4:
796                     vout_Seek( 15 );
797                     break;
798
799                 case Button5:
800                     vout_Seek( -15 );
801                     break;
802             }
803         }
804         /* Mouse release */
805         else if( xevent.type == ButtonRelease )
806         {
807             switch( ((XButtonEvent *)&xevent)->button )
808             {
809                 case Button3:
810                     /* FIXME: need locking ! */
811                     p_main->p_intf->b_menu_change = 1;
812                     break;
813             }
814         }
815         /* Mouse move */
816         else if( xevent.type == MotionNotify )
817         {
818             p_vout->p_sys->i_time_mouse_last_moved = mdate();
819             if( ! p_vout->p_sys->b_mouse_pointer_visible )
820             {
821                 ToggleCursor( p_vout ); 
822             }
823         }
824         /* Other event */
825         else
826         {
827             intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
828         }
829     }
830
831 #ifdef MODULE_NAME_IS_xvideo
832     /* Handle events for YUV video output sub-window */
833     while( XCheckWindowEvent( p_vout->p_sys->p_display,
834                               p_vout->p_sys->yuv_window,
835                               ExposureMask, &xevent ) == True )
836     {
837         /* Window exposed (only handled if stream playback is paused) */
838         if( xevent.type == Expose )
839         {
840             if( ((XExposeEvent *)&xevent)->count == 0 )
841             {
842                 /* (if this is the last a collection of expose events...) */
843                 if( p_main->p_intf->p_input != NULL )
844                 {
845                     if( PAUSE_S ==
846                             p_main->p_intf->p_input->stream.control.i_status )
847                     {
848 /*                        XVideoDisplay( p_vout )*/;
849                     }
850                 }
851             }
852         }
853     }
854 #endif
855
856     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
857      * are handled - according to the man pages, the format is always 32
858      * in this case */
859     while( XCheckTypedEvent( p_vout->p_sys->p_display,
860                              ClientMessage, &xevent ) )
861     {
862         if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
863             && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
864         {
865             p_main->p_intf->b_die = 1;
866         }
867         else
868         {
869             intf_DbgMsg( "vout: unhandled ClientMessage received" );
870         }
871     }
872
873     /*
874      * Fullscreen Change
875      */
876     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
877     {
878         ToggleFullScreen( p_vout );
879         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
880
881     }
882
883
884 #ifdef MODULE_NAME_IS_x11
885     /*
886      * Handle vout window resizing
887      */
888 #if 0
889     if( b_resized )
890     {
891         /* If interface window has been resized, change vout size */
892         intf_DbgMsg( "vout: resizing output window" );
893         p_vout->i_width =  p_vout->p_sys->i_width;
894         p_vout->i_height = p_vout->p_sys->i_height;
895         p_vout->i_changes |= VOUT_SIZE_CHANGE;
896     }
897     else if( (p_vout->i_width  != p_vout->p_sys->i_width) ||
898              (p_vout->i_height != p_vout->p_sys->i_height) )
899     {
900         /* If video output size has changed, change interface window size */
901         intf_DbgMsg( "vout: resizing output window" );
902         p_vout->p_sys->i_width =    p_vout->i_width;
903         p_vout->p_sys->i_height =   p_vout->i_height;
904         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
905                        p_vout->p_sys->i_width, p_vout->p_sys->i_height );
906     }
907     /*
908      * Color/Grayscale or gamma change: in 8bpp, just change the colormap
909      */
910     if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
911         && (p_vout->i_screen_depth == 8) )
912     {
913         /* FIXME: clear flags ?? */
914     }
915
916     /*
917      * Size change
918      */
919     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
920     {
921         intf_DbgMsg( "vout info: resizing window" );
922         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
923
924         /* Resize window */
925         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
926                        p_vout->i_width, p_vout->i_height );
927
928         /* Destroy XImages to change their size */
929         vout_End( p_vout );
930
931         /* Recreate XImages. If SysInit failed, the thread can't go on. */
932         if( vout_Init( p_vout ) )
933         {
934             intf_ErrMsg( "vout error: cannot resize display" );
935             return( 1 );
936        }
937
938         /* Tell the video output thread that it will need to rebuild YUV
939          * tables. This is needed since conversion buffer size may have
940          * changed */
941         p_vout->i_changes |= VOUT_YUV_CHANGE;
942         intf_Msg( "vout: video display resized (%dx%d)",
943                   p_vout->i_width, p_vout->i_height);
944     }
945 #endif /* #if 0 */
946 #else
947     /*
948      * Size change
949      */
950     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
951     {
952         int i_width, i_height, i_x, i_y;
953
954         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
955
956         intf_WarnMsg( 3, "vout: video display resized (%dx%d)",
957                       p_vout->p_sys->i_width,
958                       p_vout->p_sys->i_height );
959  
960         vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
961                            p_vout->p_sys->i_height,
962                            &i_x, &i_y, &i_width, &i_height );
963
964         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
965                        i_width, i_height );
966         
967         XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
968                      i_x, i_y );
969
970     }
971 #endif
972
973     /* Autohide Cursour */
974     if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
975     {
976         /* Hide the mouse automatically */
977         if( p_vout->p_sys->b_mouse_pointer_visible )
978         {
979             ToggleCursor( p_vout ); 
980         }
981     }
982
983     return 0;
984 }
985
986 /*****************************************************************************
987  * vout_End: terminate X11 video thread output method
988  *****************************************************************************
989  * Destroy the X11 XImages created by vout_Init. It is called at the end of
990  * the thread, but also each time the window is resized.
991  *****************************************************************************/
992 static void vout_End( vout_thread_t *p_vout )
993 {
994     int i_index;
995
996     /* Free the direct buffers we allocated */
997     for( i_index = I_OUTPUTPICTURES ; i_index ; )
998     {
999         i_index--;
1000         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1001     }
1002 }
1003
1004 /* following functions are local */
1005
1006 /*****************************************************************************
1007  * CreateWindow: open and set-up X11 main window
1008  *****************************************************************************/
1009 static int CreateWindow( vout_thread_t *p_vout )
1010 {
1011     XSizeHints              xsize_hints;
1012     XSetWindowAttributes    xwindow_attributes;
1013     XGCValues               xgcvalues;
1014     XEvent                  xevent;
1015
1016     boolean_t               b_expose;
1017     boolean_t               b_configure_notify;
1018     boolean_t               b_map_notify;
1019
1020     /* Set main window's size */
1021     if( p_vout->render.i_height * p_vout->render.i_aspect
1022         >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1023     {
1024         p_vout->p_sys->i_width = p_vout->render.i_height
1025           * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
1026         p_vout->p_sys->i_height = p_vout->render.i_height;
1027     }
1028     else
1029     {
1030         p_vout->p_sys->i_width = p_vout->render.i_width;
1031         p_vout->p_sys->i_height = p_vout->render.i_width
1032           * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
1033     }
1034
1035 #if 0
1036     if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 300 )
1037     {
1038         p_vout->p_sys->i_width <<= 1;
1039         p_vout->p_sys->i_height <<= 1;
1040     }
1041     else if( p_vout->p_sys->i_width <= 400
1042              && p_vout->p_sys->i_height <= 400 )
1043     {
1044         p_vout->p_sys->i_width += p_vout->p_sys->i_width >> 1;
1045         p_vout->p_sys->i_height += p_vout->p_sys->i_height >> 1;
1046     }
1047 #endif
1048
1049     /* Prepare window manager hints and properties */
1050     xsize_hints.base_width          = p_vout->p_sys->i_width;
1051     xsize_hints.base_height         = p_vout->p_sys->i_height;
1052     xsize_hints.flags               = PSize;
1053     p_vout->p_sys->wm_protocols     = XInternAtom( p_vout->p_sys->p_display,
1054                                                    "WM_PROTOCOLS", True );
1055     p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
1056                                                    "WM_DELETE_WINDOW", True );
1057
1058     /* Prepare window attributes */
1059     xwindow_attributes.backing_store = Always;       /* save the hidden part */
1060     xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1061                                                      p_vout->p_sys->i_screen);
1062     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1063     
1064
1065     /* Create the window and set hints - the window must receive
1066      * ConfigureNotify events, and until it is displayed, Expose and
1067      * MapNotify events. */
1068
1069     p_vout->p_sys->window =
1070         XCreateWindow( p_vout->p_sys->p_display,
1071                        DefaultRootWindow( p_vout->p_sys->p_display ),
1072                        0, 0,
1073                        p_vout->p_sys->i_width,
1074                        p_vout->p_sys->i_height,
1075                        0,
1076                        0, InputOutput, 0,
1077                        CWBackingStore | CWBackPixel | CWEventMask,
1078                        &xwindow_attributes );
1079
1080     /* Set window manager hints and properties: size hints, command,
1081      * window's name, and accepted protocols */
1082     XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
1083                        &xsize_hints );
1084     XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
1085                  p_main->ppsz_argv, p_main->i_argc );
1086     XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
1087 #ifdef MODULE_NAME_IS_x11
1088                 VOUT_TITLE " (X11 output)"
1089 #else
1090                 VOUT_TITLE " (XVideo output)"
1091 #endif
1092               );
1093
1094     if( (p_vout->p_sys->wm_protocols == None)        /* use WM_DELETE_WINDOW */
1095         || (p_vout->p_sys->wm_delete_window == None)
1096         || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
1097                              &p_vout->p_sys->wm_delete_window, 1 ) )
1098     {
1099         /* WM_DELETE_WINDOW is not supported by window manager */
1100         intf_Msg( "vout error: missing or bad window manager" );
1101     } 
1102
1103     /* Creation of a graphic context that doesn't generate a GraphicsExpose
1104      * event when using functions like XCopyArea */
1105     xgcvalues.graphics_exposures = False;
1106     p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
1107                                    p_vout->p_sys->window,
1108                                    GCGraphicsExposures, &xgcvalues);
1109
1110     /* Send orders to server, and wait until window is displayed - three
1111      * events must be received: a MapNotify event, an Expose event allowing
1112      * drawing in the window, and a ConfigureNotify to get the window
1113      * dimensions. Once those events have been received, only ConfigureNotify
1114      * events need to be received. */
1115     b_expose = 0;
1116     b_configure_notify = 0;
1117     b_map_notify = 0;
1118     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
1119     do
1120     {
1121         XNextEvent( p_vout->p_sys->p_display, &xevent);
1122         if( (xevent.type == Expose)
1123             && (xevent.xexpose.window == p_vout->p_sys->window) )
1124         {
1125             b_expose = 1;
1126         }
1127         else if( (xevent.type == MapNotify)
1128                  && (xevent.xmap.window == p_vout->p_sys->window) )
1129         {
1130             b_map_notify = 1;
1131         }
1132         else if( (xevent.type == ConfigureNotify)
1133                  && (xevent.xconfigure.window == p_vout->p_sys->window) )
1134         {
1135             b_configure_notify = 1;
1136             p_vout->p_sys->i_width = xevent.xconfigure.width;
1137             p_vout->p_sys->i_height = xevent.xconfigure.height;
1138         }
1139     } while( !( b_expose && b_configure_notify && b_map_notify ) );
1140
1141     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
1142                   StructureNotifyMask | KeyPressMask |
1143                   ButtonPressMask | ButtonReleaseMask | 
1144                   PointerMotionMask );
1145
1146 #ifdef MODULE_NAME_IS_x11
1147     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1148     {
1149         /* Allocate a new palette */
1150         p_vout->p_sys->colormap =
1151             XCreateColormap( p_vout->p_sys->p_display,
1152                              DefaultRootWindow( p_vout->p_sys->p_display ),
1153                              DefaultVisual( p_vout->p_sys->p_display,
1154                                             p_vout->p_sys->i_screen ),
1155                              AllocAll );
1156
1157         xwindow_attributes.colormap = p_vout->p_sys->colormap;
1158         XChangeWindowAttributes( p_vout->p_sys->p_display,
1159                                  p_vout->p_sys->window,
1160                                  CWColormap, &xwindow_attributes );
1161     }
1162
1163 #else
1164     /* Create YUV output sub-window. */
1165     p_vout->p_sys->yuv_window = XCreateSimpleWindow( p_vout->p_sys->p_display,
1166                          p_vout->p_sys->window, 0, 0,
1167                          p_vout->p_sys->i_width,
1168                          p_vout->p_sys->i_height,
1169                          0,
1170                          BlackPixel( p_vout->p_sys->p_display,
1171                                          p_vout->p_sys->i_screen ),
1172                          WhitePixel( p_vout->p_sys->p_display,
1173                                          p_vout->p_sys->i_screen ) );
1174     
1175     XSetWindowBackground( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1176              BlackPixel(p_vout->p_sys->p_display, p_vout->p_sys->i_screen ) );
1177     
1178     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
1179     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1180                   ExposureMask );
1181 #endif
1182
1183     /* If the cursor was formerly blank than blank it again */
1184     if( !p_vout->p_sys->b_mouse_pointer_visible )
1185     {
1186         ToggleCursor( p_vout );
1187         ToggleCursor( p_vout );
1188     }
1189
1190     XSync( p_vout->p_sys->p_display, False );
1191
1192     /* At this stage, the window is open, displayed, and ready to
1193      * receive data */
1194
1195     return( 0 );
1196 }
1197
1198 /*****************************************************************************
1199  * DestroyWindow: destroy the window
1200  *****************************************************************************
1201  *
1202  *****************************************************************************/
1203 static void DestroyWindow( vout_thread_t *p_vout )
1204 {
1205     XSync( p_vout->p_sys->p_display, False );
1206
1207 #ifdef MODULE_NAME_IS_xvideo
1208     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
1209 #endif
1210
1211     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
1212     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
1213     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
1214 }
1215
1216 /*****************************************************************************
1217  * NewPicture: allocate a picture
1218  *****************************************************************************
1219  * Returns 0 on success, -1 otherwise
1220  *****************************************************************************/
1221 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1222 {
1223     /* We know the chroma, allocate a buffer which will be used
1224      * directly by the decoder */
1225     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1226
1227     if( p_pic->p_sys == NULL )
1228     {
1229         return -1;
1230     }
1231
1232     if( p_vout->p_sys->b_shm )
1233     {
1234         /* Create image using XShm extension */
1235         p_pic->p_sys->p_image =
1236             CreateShmImage( p_vout->p_sys->p_display,
1237 #ifdef MODULE_NAME_IS_xvideo
1238                             p_vout->p_sys->i_xvport,
1239                             p_vout->p_sys->i_chroma,
1240 #else
1241                             p_vout->p_sys->p_visual,
1242                             p_vout->p_sys->i_screen_depth,
1243 #endif
1244                             &p_pic->p_sys->shminfo,
1245                             p_vout->output.i_width, p_vout->output.i_height );
1246     }
1247     else
1248     {
1249         /* Create image without XShm extension */
1250         p_pic->p_sys->p_image =
1251             CreateImage( p_vout->p_sys->p_display,
1252 #ifdef MODULE_NAME_IS_xvideo
1253                          p_vout->p_sys->i_xvport,
1254                          p_vout->p_sys->i_chroma,
1255 #else
1256                          p_vout->p_sys->p_visual,
1257                          p_vout->p_sys->i_screen_depth, 
1258                          p_vout->p_sys->i_bytes_per_pixel,
1259 #endif
1260                          p_vout->output.i_width, p_vout->output.i_height );
1261     }
1262
1263     if( p_pic->p_sys->p_image == NULL )
1264     {
1265         free( p_pic->p_sys );
1266         return -1;
1267     }
1268
1269 #ifdef MODULE_NAME_IS_xvideo
1270     switch( p_vout->p_sys->i_chroma )
1271     {
1272         case FOURCC_I420:
1273
1274             p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1275                                + p_pic->p_sys->p_image->offsets[0];
1276             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1277             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1278             p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1279             p_pic->p[Y_PLANE].b_margin = 0;
1280
1281             p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1282                                + p_pic->p_sys->p_image->offsets[1];
1283             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1284             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1285             p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1286             p_pic->p[Y_PLANE].b_margin = 0;
1287
1288             p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1289                                + p_pic->p_sys->p_image->offsets[2];
1290             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1291             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1292             p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1293             p_pic->p[Y_PLANE].b_margin = 0;
1294
1295             p_pic->i_planes = 3;
1296             break;
1297
1298         case FOURCC_Y211:
1299
1300             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1301                                   + p_pic->p_sys->p_image->offsets[0];
1302             p_pic->p->i_lines = p_vout->output.i_height;
1303             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1304             p_pic->p->i_pixel_bytes = 1;
1305             p_pic->p->b_margin = 0;
1306
1307             p_pic->i_planes = 1;
1308             break;
1309
1310         case FOURCC_YUY2:
1311
1312             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1313                                   + p_pic->p_sys->p_image->offsets[0];
1314             p_pic->p->i_lines = p_vout->output.i_height;
1315             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1316             p_pic->p->i_pixel_bytes = 2;
1317             p_pic->p->b_margin = 0;
1318
1319             p_pic->i_planes = 1;
1320             break;
1321
1322         case FOURCC_RV16:
1323
1324             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1325                                   + p_pic->p_sys->p_image->offsets[0];
1326             p_pic->p->i_lines = p_vout->output.i_height;
1327             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1328             p_pic->p->i_pixel_bytes = 2;
1329             p_pic->p->b_margin = 0;
1330
1331             p_pic->p->i_red_mask   = 0x001f;
1332             p_pic->p->i_green_mask = 0x03e0;
1333             p_pic->p->i_blue_mask  = 0x7c00;
1334
1335             p_pic->i_planes = 1;
1336             break;
1337
1338         default:
1339             /* Unknown chroma, tell the guy to get lost */
1340             IMAGE_FREE( p_pic->p_sys->p_image );
1341             free( p_pic->p_sys );
1342             intf_ErrMsg( "vout error: never heard of chroma 0x%.8x",
1343                          p_vout->p_sys->i_chroma );
1344             p_pic->i_planes = 0;
1345             return -1;
1346     }
1347 #else
1348     switch( p_vout->output.i_chroma )
1349     {
1350         case FOURCC_RV16:
1351
1352             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1353                                   + p_pic->p_sys->p_image->xoffset;
1354             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1355             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1356             p_pic->p->i_pixel_bytes = p_pic->p_sys->p_image->depth;
1357
1358             if( p_pic->p->i_pitch == 2 * p_pic->p_sys->p_image->width )
1359             {
1360                 p_pic->p->b_margin = 0;
1361             }
1362             else
1363             {
1364                 p_pic->p->b_margin = 1;
1365                 p_pic->p->b_hidden = 1;
1366                 p_pic->p->i_visible_bytes = 2 * p_pic->p_sys->p_image->width;
1367             }
1368
1369             p_pic->p->i_red_mask   = p_pic->p_sys->p_image->red_mask;
1370             p_pic->p->i_green_mask = p_pic->p_sys->p_image->green_mask;
1371             p_pic->p->i_blue_mask  = p_pic->p_sys->p_image->blue_mask;
1372
1373             p_pic->i_planes = 1;
1374
1375             break;
1376
1377         default:
1378             /* Unknown chroma, tell the guy to get lost */
1379             IMAGE_FREE( p_pic->p_sys->p_image );
1380             free( p_pic->p_sys );
1381             intf_ErrMsg( "vout error: never heard of chroma 0x%.8x",
1382                          p_vout->output.i_chroma );
1383             p_pic->i_planes = 0;
1384             return -1;
1385     }
1386 #endif
1387
1388     return 0;
1389 }
1390
1391 /*****************************************************************************
1392  * FreePicture: destroy a picture allocated with NewPicture
1393  *****************************************************************************
1394  * Destroy XImage AND associated data. If using Shm, detach shared memory
1395  * segment from server and process, then free it. The XDestroyImage manpage
1396  * says that both the image structure _and_ the data pointed to by the
1397  * image structure are freed, so no need to free p_image->data.
1398  *****************************************************************************/
1399 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1400 {
1401     /* The order of operations is correct */
1402     if( p_vout->p_sys->b_shm )
1403     {
1404         XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1405         IMAGE_FREE( p_pic->p_sys->p_image );
1406
1407         shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1408         if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1409         {
1410             intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1411                          strerror(errno) );
1412         }
1413     }
1414     else
1415     {
1416         IMAGE_FREE( p_pic->p_sys->p_image );
1417     }
1418
1419     XSync( p_vout->p_sys->p_display, False );
1420
1421     free( p_pic->p_sys );
1422 }
1423
1424 /*****************************************************************************
1425  * ToggleFullScreen: Enable or disable full screen mode
1426  *****************************************************************************
1427  * This function will switch between fullscreen and window mode.
1428  *
1429  *****************************************************************************/
1430 static void ToggleFullScreen ( vout_thread_t *p_vout )
1431 {
1432   Atom prop;
1433   mwmhints_t mwmhints;
1434   int i_xpos, i_ypos, i_width, i_height;
1435
1436   p_vout->b_fullscreen = !p_vout->b_fullscreen;
1437
1438   if( p_vout->b_fullscreen )
1439   {
1440       Window next_parent, parent, *p_dummy, dummy1;
1441       unsigned int dummy2, dummy3;
1442      
1443       intf_WarnMsg( 3, "vout: entering fullscreen mode" );
1444
1445       /* Save current window coordinates so they can be restored when
1446        * we exit from fullscreen mode */
1447
1448       /* find the real parent, which means the which is a direct child of
1449        * the root window */
1450       next_parent = parent = p_vout->p_sys->window;
1451       while( next_parent != DefaultRootWindow( p_vout->p_sys->p_display ) )
1452       {
1453           parent = next_parent;
1454           XQueryTree( p_vout->p_sys->p_display,
1455                       parent,
1456                       &dummy1,
1457                       &next_parent,
1458                       &p_dummy,
1459                       &dummy2 );
1460           XFree((void *)p_dummy);
1461       }
1462
1463       XGetGeometry( p_vout->p_sys->p_display,
1464                     p_vout->p_sys->window,
1465                     &dummy1,
1466                     &dummy2,
1467                     &dummy3,
1468                     &p_vout->p_sys->i_width_backup,
1469                     &p_vout->p_sys->i_height_backup,
1470                     &dummy2, &dummy3 );
1471
1472       XTranslateCoordinates( p_vout->p_sys->p_display,
1473                              parent,
1474                              DefaultRootWindow( p_vout->p_sys->p_display ),
1475                              0,
1476                              0,
1477                              &p_vout->p_sys->i_xpos_backup,
1478                              &p_vout->p_sys->i_ypos_backup,
1479                              &dummy1 );
1480
1481       mwmhints.flags = MWM_HINTS_DECORATIONS;
1482       mwmhints.decorations = 0;
1483
1484       i_xpos = 0;
1485       i_ypos = 0;
1486       i_width = DisplayWidth( p_vout->p_sys->p_display,
1487                               p_vout->p_sys->i_screen );
1488       i_height = DisplayHeight( p_vout->p_sys->p_display,
1489                                 p_vout->p_sys->i_screen );
1490
1491 #if 0
1492       /* Being a transient window allows us to really be fullscreen (display
1493        * over the taskbar for instance) but then we end-up with the same
1494        * result as with the brute force method */
1495       XSetTransientForHint( p_vout->p_sys->p_display,
1496                             p_vout->p_sys->window, None );
1497 #endif
1498   }
1499   else
1500   {
1501       intf_WarnMsg( 3, "vout: leaving fullscreen mode" );
1502       
1503       mwmhints.flags = MWM_HINTS_DECORATIONS;
1504       mwmhints.decorations = 1;
1505
1506       i_xpos = p_vout->p_sys->i_xpos_backup;
1507       i_ypos = p_vout->p_sys->i_ypos_backup;
1508       i_width = p_vout->p_sys->i_width_backup;
1509       i_height = p_vout->p_sys->i_height_backup;
1510   }
1511
1512   /* To my knowledge there are two ways to create a borderless window.
1513    * There's the generic way which is to tell x to bypass the window manager,
1514    * but this creates problems with the focus of other applications.
1515    * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1516    * luckily seems to be supported by most window managers.
1517    */
1518   prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1519                       False );
1520   XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
1521                    prop, prop, 32, PropModeReplace,
1522                    (unsigned char *)&mwmhints,
1523                    PROP_MWM_HINTS_ELEMENTS );
1524 #if 0 /* brute force way to remove decorations */
1525   XSetWindowAttributes attributes;
1526   attributes.override_redirect = True;
1527   XChangeWindowAttributes( p_vout->p_sys->p_display,
1528                            p_vout->p_sys->window,
1529                            CWOverrideRedirect,
1530                            &attributes);
1531 #endif
1532
1533   /* We need to unmap and remap the window if we want the window 
1534    * manager to take our changes into effect */
1535   XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
1536   XMapRaised( p_vout->p_sys->p_display, p_vout->p_sys->window);
1537   XMoveResizeWindow( p_vout->p_sys->p_display,
1538                      p_vout->p_sys->window,
1539                      i_xpos,
1540                      i_ypos,
1541                      i_width,
1542                      i_height );
1543   XFlush( p_vout->p_sys->p_display );
1544 }
1545
1546 /*****************************************************************************
1547  * EnableXScreenSaver: enable screen saver
1548  *****************************************************************************
1549  * This function enables the screen saver on a display after it has been
1550  * disabled by XDisableScreenSaver.
1551  * FIXME: what happens if multiple vlc sessions are running at the same
1552  *        time ???
1553  *****************************************************************************/
1554 static void EnableXScreenSaver( vout_thread_t *p_vout )
1555 {
1556     int dummy;
1557
1558     intf_DbgMsg( "vout: enabling screen saver" );
1559     XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1560                      p_vout->p_sys->i_ss_interval,
1561                      p_vout->p_sys->i_ss_blanking,
1562                      p_vout->p_sys->i_ss_exposure );
1563
1564     /* Restore DPMS settings */
1565     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1566     {
1567         if( p_vout->p_sys->b_ss_dpms )
1568             DPMSEnable( p_vout->p_sys->p_display );
1569     }
1570 }
1571
1572 /*****************************************************************************
1573  * DisableXScreenSaver: disable screen saver
1574  *****************************************************************************
1575  * See XEnableXScreenSaver
1576  *****************************************************************************/
1577 static void DisableXScreenSaver( vout_thread_t *p_vout )
1578 {
1579     int dummy;
1580
1581     /* Save screen saver informations */
1582     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1583                      &p_vout->p_sys->i_ss_interval,
1584                      &p_vout->p_sys->i_ss_blanking,
1585                      &p_vout->p_sys->i_ss_exposure );
1586
1587     /* Disable screen saver */
1588     intf_DbgMsg( "vout: disabling screen saver" );
1589     XSetScreenSaver( p_vout->p_sys->p_display, 0,
1590                      p_vout->p_sys->i_ss_interval,
1591                      p_vout->p_sys->i_ss_blanking,
1592                      p_vout->p_sys->i_ss_exposure );
1593
1594     /* Disable DPMS */
1595     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1596     {
1597         CARD16 dummy;
1598         /* Save DPMS current state */
1599         DPMSInfo( p_vout->p_sys->p_display, &dummy,
1600                   &p_vout->p_sys->b_ss_dpms );
1601         intf_DbgMsg( "vout: disabling DPMS" );
1602         DPMSDisable( p_vout->p_sys->p_display );
1603    }
1604 }
1605
1606 /*****************************************************************************
1607  * CreateCursor: create a blank mouse pointer
1608  *****************************************************************************/
1609 static void CreateCursor( vout_thread_t *p_vout )
1610 {
1611     XColor cursor_color;
1612
1613     p_vout->p_sys->cursor_pixmap =
1614         XCreatePixmap( p_vout->p_sys->p_display,
1615                        DefaultRootWindow( p_vout->p_sys->p_display ),
1616                        1, 1, 1 );
1617
1618     XParseColor( p_vout->p_sys->p_display,
1619                  XCreateColormap( p_vout->p_sys->p_display,
1620                                   DefaultRootWindow(
1621                                                     p_vout->p_sys->p_display ),
1622                                   DefaultVisual(
1623                                                 p_vout->p_sys->p_display,
1624                                                 p_vout->p_sys->i_screen ),
1625                                   AllocNone ),
1626                  "black", &cursor_color );
1627
1628     p_vout->p_sys->blank_cursor =
1629         XCreatePixmapCursor( p_vout->p_sys->p_display,
1630                              p_vout->p_sys->cursor_pixmap,
1631                              p_vout->p_sys->cursor_pixmap,
1632                              &cursor_color, &cursor_color, 1, 1 );
1633 }
1634
1635 /*****************************************************************************
1636  * DestroyCursor: destroy the blank mouse pointer
1637  *****************************************************************************/
1638 static void DestroyCursor( vout_thread_t *p_vout )
1639 {
1640     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1641 }
1642
1643 /*****************************************************************************
1644  * ToggleCursor: hide or show the mouse pointer
1645  *****************************************************************************
1646  * This function hides the X pointer if it is visible by setting the pointer
1647  * sprite to a blank one. To show it again, we disable the sprite.
1648  *****************************************************************************/
1649 static void ToggleCursor( vout_thread_t *p_vout )
1650 {
1651     if( p_vout->p_sys->b_mouse_pointer_visible )
1652     {
1653         XDefineCursor( p_vout->p_sys->p_display,
1654                        p_vout->p_sys->window,
1655                        p_vout->p_sys->blank_cursor );
1656         p_vout->p_sys->b_mouse_pointer_visible = 0;
1657     }
1658     else
1659     {
1660         XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );
1661         p_vout->p_sys->b_mouse_pointer_visible = 1;
1662     }
1663 }
1664
1665 #ifdef MODULE_NAME_IS_xvideo
1666 /*****************************************************************************
1667  * XVideoGetPort: get YUV12 port
1668  *****************************************************************************/
1669 static int XVideoGetPort( Display *dpy, int i_id )
1670 {
1671     XvAdaptorInfo *p_adaptor;
1672     unsigned int i;
1673     int i_adaptor, i_num_adaptors, i_requested_adaptor;
1674     int i_selected_port;
1675
1676     switch( XvQueryExtension( dpy, &i, &i, &i, &i, &i ) )
1677     {
1678         case Success:
1679             break;
1680
1681         case XvBadExtension:
1682             intf_WarnMsg( 3, "vout error: XvBadExtension" );
1683             return( -1 );
1684
1685         case XvBadAlloc:
1686             intf_WarnMsg( 3, "vout error: XvBadAlloc" );
1687             return( -1 );
1688
1689         default:
1690             intf_WarnMsg( 3, "vout error: XvQueryExtension failed" );
1691             return( -1 );
1692     }
1693
1694     switch( XvQueryAdaptors( dpy, DefaultRootWindow( dpy ),
1695                              &i_num_adaptors, &p_adaptor ) )
1696     {
1697         case Success:
1698             break;
1699
1700         case XvBadExtension:
1701             intf_WarnMsg( 3, "vout error: XvBadExtension for XvQueryAdaptors" );
1702             return( -1 );
1703
1704         case XvBadAlloc:
1705             intf_WarnMsg( 3, "vout error: XvBadAlloc for XvQueryAdaptors" );
1706             return( -1 );
1707
1708         default:
1709             intf_WarnMsg( 3, "vout error: XvQueryAdaptors failed" );
1710             return( -1 );
1711     }
1712
1713     i_selected_port = -1;
1714     i_requested_adaptor = main_GetIntVariable( VOUT_XVADAPTOR_VAR, -1 );
1715
1716     /* No special xv port has been requested so try all of them */
1717     for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1718     {
1719         XvImageFormatValues *p_formats;
1720         int i_format, i_num_formats;
1721         int i_port;
1722
1723         /* If we requested an adaptor and it's not this one, we aren't
1724          * interested */
1725         if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1726         {
1727             continue;
1728         }
1729
1730         /* If the adaptor doesn't have the required properties, skip it */
1731         if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1732             !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1733         {
1734             continue;
1735         }
1736
1737         /* Check that port supports YUV12 planar format... */
1738         p_formats = XvListImageFormats( dpy, p_adaptor[i_adaptor].base_id,
1739                                         &i_num_formats );
1740
1741         for( i_format = 0; i_format < i_num_formats; i_format++ )
1742         {
1743             XvEncodingInfo  *p_enc;
1744             int             i_enc, i_num_encodings;
1745             XvAttribute     *p_attr;
1746             int             i_attr, i_num_attributes;
1747
1748             /* If this is not the format we want, forget it */
1749             if( p_formats[ i_format ].id != i_id )
1750             {
1751                 continue;
1752             }
1753
1754             /* Look for the first available port supporting this format */
1755             for( i_port = p_adaptor[i_adaptor].base_id;
1756                  ( i_port < p_adaptor[i_adaptor].base_id
1757                              + p_adaptor[i_adaptor].num_ports )
1758                    && ( i_selected_port == -1 );
1759                  i_port++ )
1760             {
1761                 if( XvGrabPort( dpy, i_port, CurrentTime ) == Success )
1762                 {
1763                     i_selected_port = i_port;
1764                 }
1765             }
1766
1767             /* If no free port was found, forget it */
1768             if( i_selected_port == -1 )
1769             {
1770                 continue;
1771             }
1772
1773             /* If we found a port, print information about it */
1774             intf_WarnMsg( 3, "vout: found adaptor %i, port %i, "
1775                              "image format 0x%x (%4.4s) %s",
1776                              i_adaptor, i_selected_port,
1777                              p_formats[ i_format ].id,
1778                              (char *)&p_formats[ i_format ].id,
1779                              ( p_formats[ i_format ].format
1780                                 == XvPacked ) ? "packed" : "planar" );
1781
1782             intf_WarnMsg( 10, " encoding list:" );
1783
1784             if( XvQueryEncodings( dpy, i_selected_port,
1785                                   &i_num_encodings, &p_enc )
1786                  != Success )
1787             {
1788                 intf_WarnMsg( 10, "  XvQueryEncodings failed" );
1789                 continue;
1790             }
1791
1792             for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1793             {
1794                 intf_WarnMsg( 10, "  id=%ld, name=%s, size=%ldx%ld,"
1795                                   " numerator=%d, denominator=%d",
1796                               p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1797                               p_enc[i_enc].width, p_enc[i_enc].height,
1798                               p_enc[i_enc].rate.numerator,
1799                               p_enc[i_enc].rate.denominator );
1800             }
1801
1802             if( p_enc != NULL )
1803             {
1804                 XvFreeEncodingInfo( p_enc );
1805             }
1806
1807             intf_WarnMsg( 10, " attribute list:" );
1808             p_attr = XvQueryPortAttributes( dpy, i_selected_port,
1809                                             &i_num_attributes );
1810             for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1811             {
1812                 intf_WarnMsg( 10,
1813                       "  name=%s, flags=[%s%s ], min=%i, max=%i",
1814                       p_attr[i_attr].name,
1815                       (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1816                       (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1817                       p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1818             }
1819
1820             if( p_attr != NULL )
1821             {
1822                 XFree( p_attr );
1823             }
1824         }
1825
1826         if( p_formats != NULL )
1827         {
1828             XFree( p_formats );
1829         }
1830
1831     }
1832
1833     if( i_num_adaptors > 0 )
1834     {
1835         XvFreeAdaptorInfo( p_adaptor );
1836     }
1837
1838     if( i_selected_port == -1 )
1839     {
1840         if( i_requested_adaptor == -1 )
1841         {
1842             intf_WarnMsg( 3, "vout: no free XVideo port found for format "
1843                              "0x%.8x", i_id );
1844         }
1845         else
1846         {
1847             intf_WarnMsg( 3, "vout: XVideo adaptor %i does not have a free "
1848                              "XVideo port for format 0x%.8x",
1849                              i_requested_adaptor, i_id );
1850         }
1851     }
1852
1853     return( i_selected_port );
1854 }
1855
1856 /*****************************************************************************
1857  * XVideoReleasePort: release YUV12 port
1858  *****************************************************************************/
1859 static void XVideoReleasePort( Display *dpy, int i_port )
1860 {
1861     XvUngrabPort( dpy, i_port, CurrentTime );
1862 }
1863 #endif
1864
1865 /*****************************************************************************
1866  * InitDisplay: open and initialize X11 device
1867  *****************************************************************************
1868  * Create a window according to video output given size, and set other
1869  * properties according to the display properties.
1870  *****************************************************************************/
1871 static int InitDisplay( vout_thread_t *p_vout )
1872 {
1873 #ifdef MODULE_NAME_IS_x11
1874     XPixmapFormatValues *       p_formats;                 /* pixmap formats */
1875     XVisualInfo *               p_xvisual;           /* visuals informations */
1876     XVisualInfo                 xvisual_template;         /* visual template */
1877     int                         i_count;                       /* array size */
1878 #endif
1879
1880 #ifdef SYS_DARWIN
1881     /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
1882     p_vout->p_sys->b_shm = 0;
1883 #else
1884     p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
1885                               == True );
1886 #endif
1887     if( !p_vout->p_sys->b_shm )
1888     {
1889         intf_WarnMsg( 1, "vout warning: XShm video extension is unavailable" );
1890     }
1891
1892 #ifdef MODULE_NAME_IS_xvideo
1893     /* XXX The brightness and contrast values should be read from environment
1894      * XXX variables... */
1895 #if 0
1896     XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1897     XVideoSetAttribute( p_vout, "XV_CONTRAST",   0.5 );
1898 #endif
1899 #endif
1900
1901 #ifdef MODULE_NAME_IS_x11
1902     /* Initialize structure */
1903     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1904
1905     /* Get screen depth */
1906     p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1907                                                    p_vout->p_sys->i_screen );
1908     switch( p_vout->p_sys->i_screen_depth )
1909     {
1910     case 8:
1911         /*
1912          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1913          */
1914         xvisual_template.screen =   p_vout->p_sys->i_screen;
1915         xvisual_template.class =    DirectColor;
1916         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1917                                     VisualScreenMask | VisualClassMask,
1918                                     &xvisual_template, &i_count );
1919         if( p_xvisual == NULL )
1920         {
1921             intf_ErrMsg( "vout error: no PseudoColor visual available" );
1922             return( 1 );
1923         }
1924         p_vout->p_sys->i_bytes_per_pixel = 1;
1925         break;
1926     case 15:
1927     case 16:
1928     case 24:
1929     default:
1930         /*
1931          * Screen depth is higher than 8bpp. TrueColor visual is used.
1932          */
1933         xvisual_template.screen =   p_vout->p_sys->i_screen;
1934         xvisual_template.class =    TrueColor;
1935         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1936                                     VisualScreenMask | VisualClassMask,
1937                                     &xvisual_template, &i_count );
1938         if( p_xvisual == NULL )
1939         {
1940             intf_ErrMsg( "vout error: no TrueColor visual available" );
1941             return( 1 );
1942         }
1943         p_vout->p_sys->i_red_mask =        p_xvisual->red_mask;
1944         p_vout->p_sys->i_green_mask =      p_xvisual->green_mask;
1945         p_vout->p_sys->i_blue_mask =       p_xvisual->blue_mask;
1946
1947         /* There is no difference yet between 3 and 4 Bpp. The only way
1948          * to find the actual number of bytes per pixel is to list supported
1949          * pixmap formats. */
1950         p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1951         p_vout->p_sys->i_bytes_per_pixel = 0;
1952
1953         for( ; i_count-- ; p_formats++ )
1954         {
1955             /* Under XFree4.0, the list contains pixmap formats available
1956              * through all video depths ; so we have to check against current
1957              * depth. */
1958             if( p_formats->depth == p_vout->p_sys->i_screen_depth )
1959             {
1960                 if( p_formats->bits_per_pixel / 8
1961                         > p_vout->p_sys->i_bytes_per_pixel )
1962                 {
1963                     p_vout->p_sys->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
1964                 }
1965             }
1966         }
1967         break;
1968     }
1969     p_vout->p_sys->p_visual = p_xvisual->visual;
1970     XFree( p_xvisual );
1971 #endif
1972
1973     return( 0 );
1974 }
1975
1976 /*****************************************************************************
1977  * CreateShmImage: create an XImage or XvImage using shared memory extension
1978  *****************************************************************************
1979  * Prepare an XImage or XvImage for display function.
1980  * The order of the operations respects the recommandations of the mit-shm
1981  * document by J.Corbet and K.Packard. Most of the parameters were copied from
1982  * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1983  *****************************************************************************/
1984 static IMAGE_TYPE * CreateShmImage( Display* p_display, EXTRA_ARGS_SHM,
1985                                     int i_width, int i_height )
1986 {
1987     IMAGE_TYPE *p_image;
1988
1989     /* Create XImage / XvImage */
1990 #ifdef MODULE_NAME_IS_xvideo
1991     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1992                                 i_width, i_height, p_shm );
1993 #else
1994     p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1995                                p_shm, i_width, i_height );
1996 #endif
1997     if( p_image == NULL )
1998     {
1999         intf_ErrMsg( "vout error: image creation failed." );
2000         return( NULL );
2001     }
2002
2003     /* Allocate shared memory segment - 0776 set the access permission
2004      * rights (like umask), they are not yet supported by all X servers */
2005     p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2006     if( p_shm->shmid < 0 )
2007     {
2008         intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
2009                      strerror( errno ) );
2010         IMAGE_FREE( p_image );
2011         return( NULL );
2012     }
2013
2014     /* Attach shared memory segment to process (read/write) */
2015     p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2016     if(! p_shm->shmaddr )
2017     {
2018         intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
2019                     strerror(errno));
2020         IMAGE_FREE( p_image );
2021         shmctl( p_shm->shmid, IPC_RMID, 0 );
2022         return( NULL );
2023     }
2024
2025     /* Read-only data. We won't be using XShmGetImage */
2026     p_shm->readOnly = True;
2027
2028     /* Attach shared memory segment to X server */
2029     if( XShmAttach( p_display, p_shm ) == False )
2030     {
2031         intf_ErrMsg( "vout error: cannot attach shared memory to X server" );
2032         IMAGE_FREE( p_image );
2033         shmctl( p_shm->shmid, IPC_RMID, 0 );
2034         shmdt( p_shm->shmaddr );
2035         return( NULL );
2036     }
2037
2038     /* Send image to X server. This instruction is required, since having
2039      * built a Shm XImage and not using it causes an error on XCloseDisplay */
2040     XSync( p_display, False );
2041
2042 #if 0
2043     /* Mark the shm segment to be removed when there are no more
2044      * attachements, so it is automatic on process exit or after shmdt */
2045     shmctl( p_shm->shmid, IPC_RMID, 0 );
2046 #endif
2047
2048     return( p_image );
2049 }
2050
2051 /*****************************************************************************
2052  * CreateImage: create an XImage or XvImage
2053  *****************************************************************************
2054  * Create a simple image used as a buffer.
2055  *****************************************************************************/
2056 static IMAGE_TYPE * CreateImage( Display *p_display, EXTRA_ARGS,
2057                                  int i_width, int i_height )
2058 {
2059     byte_t *    p_data;                           /* image data storage zone */
2060     IMAGE_TYPE *p_image;
2061 #ifdef MODULE_NAME_IS_x11
2062     int         i_quantum;                     /* XImage quantum (see below) */
2063     int         i_bytes_per_line;
2064 #endif
2065
2066     /* Allocate memory for image */
2067 #ifdef MODULE_NAME_IS_xvideo
2068     p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2069 #else
2070     i_bytes_per_line = i_width * i_bytes_per_pixel;
2071     p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2072 #endif
2073     if( !p_data )
2074     {
2075         intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
2076         return( NULL );
2077     }
2078
2079 #ifdef MODULE_NAME_IS_x11
2080     /* Optimize the quantum of a scanline regarding its size - the quantum is
2081        a diviser of the number of bits between the start of two scanlines. */
2082     if( i_bytes_per_line & 0xf )
2083     {
2084         i_quantum = 0x8;
2085     }
2086     else if( i_bytes_per_line & 0x10 )
2087     {
2088         i_quantum = 0x10;
2089     }
2090     else
2091     {
2092         i_quantum = 0x20;
2093     }
2094 #endif
2095
2096     /* Create XImage. p_data will be automatically freed */
2097 #ifdef MODULE_NAME_IS_xvideo
2098     p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2099                              p_data, i_width, i_height );
2100 #else
2101     p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2102                             p_data, i_width, i_height, i_quantum, 0 );
2103 #endif
2104     if( p_image == NULL )
2105     {
2106         intf_ErrMsg( "vout error: XCreateImage() failed" );
2107         free( p_data );
2108         return( NULL );
2109     }
2110
2111     return p_image;
2112 }
2113