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