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