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