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