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