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