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