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