]> git.sesse.net Git - vlc/blob - modules/video_output/x11/xcommon.c
e063e10fc65e695969998b4b5475621552927698
[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.16 2003/05/25 11:31:54 gbazin Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          David Kennedy <dkennedy@tinytoad.com>
10  *          Gildas Bazin <gbazin@netcourrier.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <string.h>                                            /* strerror() */
33
34 #include <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 "xcommon.h"
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74 int  E_(Activate)   ( vlc_object_t * );
75 void E_(Deactivate) ( vlc_object_t * );
76
77 static int  InitVideo      ( vout_thread_t * );
78 static void EndVideo       ( vout_thread_t * );
79 static void DisplayVideo   ( vout_thread_t *, picture_t * );
80 static int  ManageVideo    ( vout_thread_t * );
81
82 static int  InitDisplay    ( vout_thread_t * );
83
84 static int  CreateWindow   ( vout_thread_t *, x11_window_t * );
85 static void DestroyWindow  ( vout_thread_t *, x11_window_t * );
86
87 static int  NewPicture     ( vout_thread_t *, picture_t * );
88 static void FreePicture    ( vout_thread_t *, picture_t * );
89
90 static IMAGE_TYPE *CreateImage    ( vout_thread_t *,
91                                     Display *, EXTRA_ARGS, int, int );
92 #ifdef HAVE_SYS_SHM_H
93 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
94                                     Display *, EXTRA_ARGS_SHM, int, int );
95 #endif
96
97 static void ToggleFullScreen      ( vout_thread_t * );
98
99 static void EnableXScreenSaver    ( vout_thread_t * );
100 static void DisableXScreenSaver   ( vout_thread_t * );
101
102 static void CreateCursor   ( vout_thread_t * );
103 static void DestroyCursor  ( vout_thread_t * );
104 static void ToggleCursor   ( vout_thread_t * );
105
106 #ifdef MODULE_NAME_IS_xvideo
107 static int  XVideoGetPort    ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
108 static void XVideoReleasePort( vout_thread_t *, int );
109 #endif
110
111 #ifdef MODULE_NAME_IS_x11
112 static void SetPalette     ( vout_thread_t *,
113                              uint16_t *, uint16_t *, uint16_t * );
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 VLC_ENOMEM;
145     }
146
147     /* Open display, using 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 VLC_EGENERIC;
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 knew pretty well what he wanted. */
200         if( b_chroma )
201         {
202             XCloseDisplay( p_vout->p_sys->p_display );
203             free( p_vout->p_sys );
204             return VLC_EGENERIC;
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 VLC_EGENERIC;
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 VLC_EGENERIC;
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 VLC_EGENERIC;
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 VLC_SUCCESS;
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 VLC_SUCCESS;
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 VLC_SUCCESS;
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     vlc_value_t val;
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( (unsigned int)xevent.xconfigure.width
500                    != p_vout->p_sys->p_win->i_width
501               || (unsigned int)xevent.xconfigure.height
502                     != p_vout->p_sys->p_win->i_height )
503             {
504                 /* Update dimensions */
505                 p_vout->i_changes |= VOUT_SIZE_CHANGE;
506                 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
507                 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
508             }
509         }
510         /* Keyboard event */
511         else if( xevent.type == KeyPress )
512         {
513             /* We may have keys like F1 trough F12, ESC ... */
514             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
515                                              xevent.xkey.keycode, 0 );
516             switch( (int)x_key_symbol )
517             {
518             case XK_Escape:
519                 if( p_vout->b_fullscreen )
520                 {
521                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
522                 }
523                 else
524                 {
525                     p_vout->p_vlc->b_die = 1;
526                 }
527                 break;
528             case XK_Menu:
529                 {
530                     intf_thread_t *p_intf;
531                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
532                                                       FIND_ANYWHERE );
533                     if( p_intf )
534                     {
535                         p_intf->b_menu_change = 1;
536                         vlc_object_release( p_intf );
537                     }
538                 }
539                 break;
540             case XK_Left:
541                 input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
542                 break;
543             case XK_Right:
544                 input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
545                 break;
546             case XK_Up:
547                 input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
548                 break;
549             case XK_Down:
550                 input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
551                 break;
552             case XK_Home:
553                 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
554                 break;
555             case XK_End:
556                 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
557                 break;
558             case XK_Page_Up:
559                 input_Seek( p_vout, 900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
560                 break;
561             case XK_Page_Down:
562                 input_Seek( p_vout, -900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
563                 break;
564             case XK_space:
565                 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
566                 break;
567
568             default:
569                 /* "Normal Keys"
570                  * The reason why I use this instead of XK_0 is that
571                  * with XLookupString, we don't have to care about
572                  * keymaps. */
573
574                 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
575                 {
576                     /* FIXME: handle stuff here */
577                     switch( i_key )
578                     {
579                     case 'q':
580                     case 'Q':
581                         p_vout->p_vlc->b_die = 1;
582                         break;
583                     case 'f':
584                     case 'F':
585                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
586                         break;
587
588                     default:
589                         break;
590                     }
591                 }
592                 break;
593             }
594         }
595         /* Mouse click */
596         else if( xevent.type == ButtonPress )
597         {
598             switch( ((XButtonEvent *)&xevent)->button )
599             {
600                 case Button1:
601                     var_Get( p_vout, "mouse-button-down", &val );
602                     val.i_int |= 1;
603                     var_Set( p_vout, "mouse-button-down", val );
604                     
605                     /* detect double-clicks */
606                     if( ( ((XButtonEvent *)&xevent)->time -
607                           p_vout->p_sys->i_time_button_last_pressed ) < 300 )
608                     {
609                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
610                     }
611
612                     p_vout->p_sys->i_time_button_last_pressed =
613                         ((XButtonEvent *)&xevent)->time;
614                     break;
615                 case Button2:
616                     var_Get( p_vout, "mouse-button-down", &val );
617                     val.i_int |= 2;
618                     var_Set( p_vout, "mouse-button-down", val );
619                     break;
620                 
621                 case Button3:
622                     var_Get( p_vout, "mouse-button-down", &val );
623                     val.i_int |= 4;
624                     var_Set( p_vout, "mouse-button-down", val );
625                     break;
626                 
627                 case Button4:
628                     var_Get( p_vout, "mouse-button-down", &val );
629                     val.i_int |= 8;
630                     var_Set( p_vout, "mouse-button-down", val );
631                     input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
632                     break;
633
634                 case Button5:
635                     var_Get( p_vout, "mouse-button-down", &val );
636                     val.i_int |= 16;
637                     var_Set( p_vout, "mouse-button-down", val );
638                     input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
639                     break;
640             }
641         }
642         /* Mouse release */
643         else if( xevent.type == ButtonRelease )
644         {
645             switch( ((XButtonEvent *)&xevent)->button )
646             {
647                 case Button1:
648                     var_Get( p_vout, "mouse-button-down", &val );
649                     val.i_int &= ~1;
650                     var_Set( p_vout, "mouse-button-down", val );
651
652                     val.b_bool = VLC_TRUE;
653                     var_Set( p_vout, "mouse-clicked", val );
654                     break;
655                     
656                 case Button2:
657                     var_Get( p_vout, "mouse-button-down", &val );
658                     val.i_int &= ~2;
659                     var_Set( p_vout, "mouse-button-down", val );
660                     break;
661                     
662                 case Button3:
663                     {
664                         intf_thread_t *p_intf;
665                         var_Get( p_vout, "mouse-button-down", &val );
666                         val.i_int &= ~4;
667                         var_Set( p_vout, "mouse-button-down", val );
668                         p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
669                                                           FIND_ANYWHERE );
670                         if( p_intf )
671                         {
672                             p_intf->b_menu_change = 1;
673                             vlc_object_release( p_intf );
674                         }
675                     }
676                     break;
677
678                 case Button4:
679                     var_Get( p_vout, "mouse-button-down", &val );
680                     val.i_int &= ~8;
681                     var_Set( p_vout, "mouse-button-down", val );
682                     break;
683
684                 case Button5:
685                     var_Get( p_vout, "mouse-button-down", &val );
686                     val.i_int &= ~16;
687                     var_Set( p_vout, "mouse-button-down", val );
688                     break;
689                     
690             }
691         }
692         /* Mouse move */
693         else if( xevent.type == MotionNotify )
694         {
695             int i_width, i_height, i_x, i_y;
696             vlc_value_t val;
697
698             /* somewhat different use for vout_PlacePicture:
699              * here the values are needed to give to mouse coordinates
700              * in the original picture space */
701             vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
702                                p_vout->p_sys->p_win->i_height,
703                                &i_x, &i_y, &i_width, &i_height );
704
705             val.i_int = ( xevent.xmotion.x - i_x )
706                          * p_vout->render.i_width / i_width;
707             var_Set( p_vout, "mouse-x", val );
708             val.i_int = ( xevent.xmotion.y - i_y )
709                          * p_vout->render.i_height / i_height;
710             var_Set( p_vout, "mouse-y", val );
711
712             val.b_bool = VLC_TRUE;
713             var_Set( p_vout, "mouse-moved", val );
714
715             p_vout->p_sys->i_time_mouse_last_moved = mdate();
716             if( ! p_vout->p_sys->b_mouse_pointer_visible )
717             {
718                 ToggleCursor( p_vout );
719             }
720         }
721         else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
722                   || xevent.type == MapNotify
723                   || xevent.type == UnmapNotify )
724         {
725             /* Ignore these events */
726         }
727         else /* Other events */
728         {
729             msg_Warn( p_vout, "unhandled event %d received", xevent.type );
730         }
731     }
732
733     /* Handle events for video output sub-window */
734     while( XCheckWindowEvent( p_vout->p_sys->p_display,
735                               p_vout->p_sys->p_win->video_window,
736                               ExposureMask, &xevent ) == True )
737     {
738         /* Window exposed (only handled if stream playback is paused) */
739         if( xevent.type == Expose )
740         {
741             if( ((XExposeEvent *)&xevent)->count == 0 )
742             {
743                 /* (if this is the last a collection of expose events...) */
744 #if 0
745                 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
746                 {
747                     if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
748                                                    ->stream.control.i_status )
749                     {
750                         /* XVideoDisplay( p_vout )*/;
751                     }
752                 }
753 #endif
754             }
755         }
756     }
757
758     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
759      * are handled - according to the man pages, the format is always 32
760      * in this case */
761     while( XCheckTypedEvent( p_vout->p_sys->p_display,
762                              ClientMessage, &xevent ) )
763     {
764         if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
765                && ((Atom)xevent.xclient.data.l[0]
766                      == p_vout->p_sys->p_win->wm_delete_window ) )
767         {
768             p_vout->p_vlc->b_die = 1;
769         }
770     }
771
772     /*
773      * Fullscreen Change
774      */
775     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
776     {
777         vlc_value_t val;
778
779         /* Update the object variable and trigger callback */
780         val.b_bool = !p_vout->b_fullscreen;
781         var_Set( p_vout, "fullscreen", val );
782
783         ToggleFullScreen( p_vout );
784         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
785     }
786
787     /*
788      * Size change
789      *
790      * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
791      *  the size flag inside the fullscreen routine)
792      */
793     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
794     {
795         int i_width, i_height, i_x, i_y;
796
797         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
798
799 #ifdef MODULE_NAME_IS_x11
800         /* We need to signal the vout thread about the size change because it
801          * is doing the rescaling */
802         p_vout->i_changes |= VOUT_SIZE_CHANGE;
803 #endif
804
805         vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
806                            p_vout->p_sys->p_win->i_height,
807                            &i_x, &i_y, &i_width, &i_height );
808
809         XResizeWindow( p_vout->p_sys->p_display,
810                        p_vout->p_sys->p_win->video_window, i_width, i_height );
811
812         XMoveWindow( p_vout->p_sys->p_display,
813                      p_vout->p_sys->p_win->video_window, i_x, i_y );
814     }
815
816     /* Autohide Cursour */
817     if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
818     {
819         /* Hide the mouse automatically */
820         if( p_vout->p_sys->b_mouse_pointer_visible )
821         {
822             ToggleCursor( p_vout );
823         }
824     }
825
826     return 0;
827 }
828
829 /*****************************************************************************
830  * EndVideo: terminate X11 video thread output method
831  *****************************************************************************
832  * Destroy the X11 XImages created by Init. It is called at the end of
833  * the thread, but also each time the window is resized.
834  *****************************************************************************/
835 static void EndVideo( vout_thread_t *p_vout )
836 {
837     int i_index;
838
839     /* Free the direct buffers we allocated */
840     for( i_index = I_OUTPUTPICTURES ; i_index ; )
841     {
842         i_index--;
843         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
844     }
845 }
846
847 /* following functions are local */
848
849 /*****************************************************************************
850  * CreateWindow: open and set-up X11 main window
851  *****************************************************************************/
852 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
853 {
854     XSizeHints              xsize_hints;
855     XSetWindowAttributes    xwindow_attributes;
856     XGCValues               xgcvalues;
857     XEvent                  xevent;
858
859     vlc_bool_t              b_expose;
860     vlc_bool_t              b_configure_notify;
861     vlc_bool_t              b_map_notify;
862
863     vlc_value_t             val;
864     long long int           i_drawable;
865
866     /* Prepare window manager hints and properties */
867     xsize_hints.base_width          = p_win->i_width;
868     xsize_hints.base_height         = p_win->i_height;
869     xsize_hints.flags               = PSize;
870     p_win->wm_protocols =
871              XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
872     p_win->wm_delete_window =
873              XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
874
875     /* Prepare window attributes */
876     xwindow_attributes.backing_store = Always;       /* save the hidden part */
877     xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
878                                                      p_vout->p_sys->i_screen);
879     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
880
881     /* Check whether someone provided us with a window ID */
882     var_Get( p_vout->p_vlc, "drawable", &val );
883     i_drawable = p_vout->b_fullscreen ? 0 : val.i_int;
884
885     if( !i_drawable )
886     {
887         p_win->b_owned = VLC_TRUE;
888
889         /* Create the window and set hints - the window must receive
890          * ConfigureNotify events, and until it is displayed, Expose and
891          * MapNotify events. */
892
893         p_win->base_window =
894             XCreateWindow( p_vout->p_sys->p_display,
895                            DefaultRootWindow( p_vout->p_sys->p_display ),
896                            0, 0,
897                            p_win->i_width, p_win->i_height,
898                            0,
899                            0, InputOutput, 0,
900                            CWBackingStore | CWBackPixel | CWEventMask,
901                            &xwindow_attributes );
902
903         if( !p_vout->b_fullscreen )
904         {
905             /* Set window manager hints and properties: size hints, command,
906              * window's name, and accepted protocols */
907             XSetWMNormalHints( p_vout->p_sys->p_display,
908                                p_win->base_window, &xsize_hints );
909             XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
910                          p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
911
912             XStoreName( p_vout->p_sys->p_display, p_win->base_window,
913 #ifdef MODULE_NAME_IS_x11
914                         VOUT_TITLE " (X11 output)"
915 #else
916                         VOUT_TITLE " (XVideo output)"
917 #endif
918                       );
919         }
920     }
921     else
922     {
923         p_win->b_owned = VLC_FALSE;
924         p_win->base_window = i_drawable;
925
926         XChangeWindowAttributes( p_vout->p_sys->p_display,
927                                  p_win->base_window,
928                                  CWBackingStore | CWBackPixel | CWEventMask,
929                                  &xwindow_attributes );
930     }
931
932     if( (p_win->wm_protocols == None)        /* use WM_DELETE_WINDOW */
933         || (p_win->wm_delete_window == None)
934         || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
935                              &p_win->wm_delete_window, 1 ) )
936     {
937         /* WM_DELETE_WINDOW is not supported by window manager */
938         msg_Warn( p_vout, "missing or bad window manager" );
939     }
940
941     /* Creation of a graphic context that doesn't generate a GraphicsExpose
942      * event when using functions like XCopyArea */
943     xgcvalues.graphics_exposures = False;
944     p_win->gc = XCreateGC( p_vout->p_sys->p_display,
945                            p_win->base_window,
946                            GCGraphicsExposures, &xgcvalues );
947
948     if( p_win->b_owned )
949     {
950         /* Send orders to server, and wait until window is displayed - three
951          * events must be received: a MapNotify event, an Expose event allowing
952          * drawing in the window, and a ConfigureNotify to get the window
953          * dimensions. Once those events have been received, only
954          * ConfigureNotify events need to be received. */
955         b_expose = 0;
956         b_configure_notify = 0;
957         b_map_notify = 0;
958         XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
959         do
960         {
961             XNextEvent( p_vout->p_sys->p_display, &xevent);
962             if( (xevent.type == Expose)
963                 && (xevent.xexpose.window == p_win->base_window) )
964             {
965                 b_expose = 1;
966             }
967             else if( (xevent.type == MapNotify)
968                      && (xevent.xmap.window == p_win->base_window) )
969             {
970                 b_map_notify = 1;
971             }
972             else if( (xevent.type == ConfigureNotify)
973                      && (xevent.xconfigure.window == p_win->base_window) )
974             {
975                 b_configure_notify = 1;
976                 p_win->i_width = xevent.xconfigure.width;
977                 p_win->i_height = xevent.xconfigure.height;
978             }
979         } while( !( b_expose && b_configure_notify && b_map_notify ) );
980     }
981     else
982     {
983         /* Get the window's geometry information */
984         Window dummy1;
985         unsigned int dummy2, dummy3;
986         XGetGeometry( p_vout->p_sys->p_display, p_win->base_window,
987                       &dummy1, &dummy2, &dummy3,
988                       &p_win->i_width,
989                       &p_win->i_height,
990                       &dummy2, &dummy3 );
991     }
992
993     XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
994                   StructureNotifyMask | KeyPressMask |
995                   ButtonPressMask | ButtonReleaseMask |
996                   PointerMotionMask );
997
998 #ifdef MODULE_NAME_IS_x11
999     if( p_win->b_owned &&
1000          XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1001     {
1002         /* Allocate a new palette */
1003         p_vout->p_sys->colormap =
1004             XCreateColormap( p_vout->p_sys->p_display,
1005                              DefaultRootWindow( p_vout->p_sys->p_display ),
1006                              DefaultVisual( p_vout->p_sys->p_display,
1007                                             p_vout->p_sys->i_screen ),
1008                              AllocAll );
1009
1010         xwindow_attributes.colormap = p_vout->p_sys->colormap;
1011         XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1012                                  CWColormap, &xwindow_attributes );
1013     }
1014 #endif
1015
1016     /* Create video output sub-window. */
1017     p_win->video_window =  XCreateSimpleWindow(
1018                                       p_vout->p_sys->p_display,
1019                                       p_win->base_window, 0, 0,
1020                                       p_win->i_width, p_win->i_height,
1021                                       0,
1022                                       BlackPixel( p_vout->p_sys->p_display,
1023                                                   p_vout->p_sys->i_screen ),
1024                                       WhitePixel( p_vout->p_sys->p_display,
1025                                                   p_vout->p_sys->i_screen ) );
1026
1027     XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1028                           BlackPixel( p_vout->p_sys->p_display,
1029                                       p_vout->p_sys->i_screen ) );
1030
1031     XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1032     XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1033                   ExposureMask );
1034
1035     /* make sure the video window will be centered in the next ManageVideo() */
1036     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1037
1038     /* If the cursor was formerly blank than blank it again */
1039     if( !p_vout->p_sys->b_mouse_pointer_visible )
1040     {
1041         ToggleCursor( p_vout );
1042         ToggleCursor( p_vout );
1043     }
1044
1045     /* Do NOT use XFlush here ! */
1046     XSync( p_vout->p_sys->p_display, False );
1047
1048     /* At this stage, the window is open, displayed, and ready to
1049      * receive data */
1050     p_vout->p_sys->p_win = p_win;
1051
1052     return VLC_SUCCESS;
1053 }
1054
1055 /*****************************************************************************
1056  * DestroyWindow: destroy the window
1057  *****************************************************************************
1058  *
1059  *****************************************************************************/
1060 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1061 {
1062     /* Do NOT use XFlush here ! */
1063     XSync( p_vout->p_sys->p_display, False );
1064
1065     XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1066     XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1067
1068     if( p_win->b_owned )
1069     {
1070         XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1071         XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1072     }
1073 }
1074
1075 /*****************************************************************************
1076  * NewPicture: allocate a picture
1077  *****************************************************************************
1078  * Returns 0 on success, -1 otherwise
1079  *****************************************************************************/
1080 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1081 {
1082     /* We know the chroma, allocate a buffer which will be used
1083      * directly by the decoder */
1084     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1085
1086     if( p_pic->p_sys == NULL )
1087     {
1088         return -1;
1089     }
1090
1091 #ifdef HAVE_SYS_SHM_H
1092     if( p_vout->p_sys->b_shm )
1093     {
1094         /* Create image using XShm extension */
1095         p_pic->p_sys->p_image =
1096             CreateShmImage( p_vout, p_vout->p_sys->p_display,
1097 #   ifdef MODULE_NAME_IS_xvideo
1098                             p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1099 #   else
1100                             p_vout->p_sys->p_visual,
1101                             p_vout->p_sys->i_screen_depth,
1102 #   endif
1103                             &p_pic->p_sys->shminfo,
1104                             p_vout->output.i_width, p_vout->output.i_height );
1105     }
1106     else
1107 #endif /* HAVE_SYS_SHM_H */
1108     {
1109         /* Create image without XShm extension */
1110         p_pic->p_sys->p_image =
1111             CreateImage( p_vout, p_vout->p_sys->p_display,
1112 #ifdef MODULE_NAME_IS_xvideo
1113                          p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1114 #else
1115                          p_vout->p_sys->p_visual,
1116                          p_vout->p_sys->i_screen_depth,
1117                          p_vout->p_sys->i_bytes_per_pixel,
1118 #endif
1119                          p_vout->output.i_width, p_vout->output.i_height );
1120     }
1121
1122     if( p_pic->p_sys->p_image == NULL )
1123     {
1124         free( p_pic->p_sys );
1125         return -1;
1126     }
1127
1128     switch( p_vout->output.i_chroma )
1129     {
1130 #ifdef MODULE_NAME_IS_xvideo
1131         case VLC_FOURCC('I','4','2','0'):
1132
1133             p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1134                                + p_pic->p_sys->p_image->offsets[0];
1135             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1136             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1137             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1138             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1139
1140             p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1141                                + p_pic->p_sys->p_image->offsets[1];
1142             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1143             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1144             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1145             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1146
1147             p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1148                                + p_pic->p_sys->p_image->offsets[2];
1149             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1150             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1151             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1152             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1153
1154             p_pic->i_planes = 3;
1155             break;
1156
1157         case VLC_FOURCC('Y','V','1','2'):
1158
1159             p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1160                                + p_pic->p_sys->p_image->offsets[0];
1161             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1162             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1163             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1164             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
1165             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1166
1167             p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1168                                + p_pic->p_sys->p_image->offsets[2];
1169             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1170             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1171             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1172             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1173
1174             p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1175                                + p_pic->p_sys->p_image->offsets[1];
1176             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1177             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1178             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1179             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1180
1181             p_pic->i_planes = 3;
1182             break;
1183
1184         case VLC_FOURCC('Y','2','1','1'):
1185
1186             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1187                                   + p_pic->p_sys->p_image->offsets[0];
1188             p_pic->p->i_lines = p_vout->output.i_height;
1189             /* XXX: this just looks so plain wrong... check it out ! */
1190             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1191             p_pic->p->i_pixel_pitch = 4;
1192             p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1193
1194             p_pic->i_planes = 1;
1195             break;
1196
1197         case VLC_FOURCC('Y','U','Y','2'):
1198         case VLC_FOURCC('U','Y','V','Y'):
1199
1200             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1201                                   + p_pic->p_sys->p_image->offsets[0];
1202             p_pic->p->i_lines = p_vout->output.i_height;
1203             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1204             p_pic->p->i_pixel_pitch = 4;
1205             p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1206
1207             p_pic->i_planes = 1;
1208             break;
1209
1210         case VLC_FOURCC('R','V','1','5'):
1211
1212             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1213                                   + p_pic->p_sys->p_image->offsets[0];
1214             p_pic->p->i_lines = p_vout->output.i_height;
1215             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1216             p_pic->p->i_pixel_pitch = 2;
1217             p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1218
1219             p_pic->i_planes = 1;
1220             break;
1221
1222         case VLC_FOURCC('R','V','1','6'):
1223
1224             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1225                                   + p_pic->p_sys->p_image->offsets[0];
1226             p_pic->p->i_lines = p_vout->output.i_height;
1227             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1228             p_pic->p->i_pixel_pitch = 2;
1229             p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1230
1231             p_pic->i_planes = 1;
1232             break;
1233
1234 #else
1235         case VLC_FOURCC('R','G','B','2'):
1236
1237             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1238                                   + p_pic->p_sys->p_image->xoffset;
1239             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1240             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1241             p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1242             p_pic->p->i_visible_pitch = p_pic->p_sys->p_image->width;
1243
1244             p_pic->i_planes = 1;
1245
1246             break;
1247
1248         case VLC_FOURCC('R','V','1','6'):
1249         case VLC_FOURCC('R','V','1','5'):
1250
1251             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1252                                   + p_pic->p_sys->p_image->xoffset;
1253             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1254             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1255             p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1256             p_pic->p->i_visible_pitch = 2 * p_pic->p_sys->p_image->width;
1257
1258             p_pic->i_planes = 1;
1259
1260             break;
1261
1262         case VLC_FOURCC('R','V','3','2'):
1263         case VLC_FOURCC('R','V','2','4'):
1264
1265             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1266                                   + p_pic->p_sys->p_image->xoffset;
1267             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1268             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1269             p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1270             p_pic->p->i_visible_pitch = 4 * p_pic->p_sys->p_image->width;
1271
1272             p_pic->i_planes = 1;
1273
1274             break;
1275 #endif
1276
1277         default:
1278             /* Unknown chroma, tell the guy to get lost */
1279             IMAGE_FREE( p_pic->p_sys->p_image );
1280             free( p_pic->p_sys );
1281             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1282                      p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1283             p_pic->i_planes = 0;
1284             return -1;
1285     }
1286
1287     return 0;
1288 }
1289
1290 /*****************************************************************************
1291  * FreePicture: destroy a picture allocated with NewPicture
1292  *****************************************************************************
1293  * Destroy XImage AND associated data. If using Shm, detach shared memory
1294  * segment from server and process, then free it. The XDestroyImage manpage
1295  * says that both the image structure _and_ the data pointed to by the
1296  * image structure are freed, so no need to free p_image->data.
1297  *****************************************************************************/
1298 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1299 {
1300     /* The order of operations is correct */
1301 #ifdef HAVE_SYS_SHM_H
1302     if( p_vout->p_sys->b_shm )
1303     {
1304         XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1305         IMAGE_FREE( p_pic->p_sys->p_image );
1306
1307         shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1308         if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1309         {
1310             msg_Err( p_vout, "cannot detach shared memory (%s)",
1311                              strerror(errno) );
1312         }
1313     }
1314     else
1315 #endif
1316     {
1317         IMAGE_FREE( p_pic->p_sys->p_image );
1318     }
1319
1320     /* Do NOT use XFlush here ! */
1321     XSync( p_vout->p_sys->p_display, False );
1322
1323     free( p_pic->p_sys );
1324 }
1325
1326 /*****************************************************************************
1327  * ToggleFullScreen: Enable or disable full screen mode
1328  *****************************************************************************
1329  * This function will switch between fullscreen and window mode.
1330  *****************************************************************************/
1331 static void ToggleFullScreen ( vout_thread_t *p_vout )
1332 {
1333     Atom prop;
1334     XEvent xevent;
1335     mwmhints_t mwmhints;
1336     XSetWindowAttributes attributes;
1337
1338     p_vout->b_fullscreen = !p_vout->b_fullscreen;
1339
1340     if( p_vout->b_fullscreen )
1341     {
1342         msg_Dbg( p_vout, "entering fullscreen mode" );
1343
1344         p_vout->p_sys->b_altfullscreen =
1345             config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1346
1347         if( p_vout->p_sys->p_win->b_owned )
1348         {
1349             XUnmapWindow( p_vout->p_sys->p_display,
1350                           p_vout->p_sys->p_win->base_window);
1351         }
1352
1353         p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1354
1355         /* fullscreen window size and position */
1356         p_vout->p_sys->p_win->i_width =
1357             DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1358         p_vout->p_sys->p_win->i_height =
1359             DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1360
1361         CreateWindow( p_vout, p_vout->p_sys->p_win );
1362
1363         /* To my knowledge there are two ways to create a borderless window.
1364          * There's the generic way which is to tell x to bypass the window
1365          * manager, but this creates problems with the focus of other
1366          * applications.
1367          * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1368          * luckily seems to be supported by most window managers. */
1369         if( !p_vout->p_sys->b_altfullscreen )
1370         {
1371             mwmhints.flags = MWM_HINTS_DECORATIONS;
1372             mwmhints.decorations = False;
1373
1374             prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1375                                 False );
1376             XChangeProperty( p_vout->p_sys->p_display,
1377                              p_vout->p_sys->p_win->base_window,
1378                              prop, prop, 32, PropModeReplace,
1379                              (unsigned char *)&mwmhints,
1380                              PROP_MWM_HINTS_ELEMENTS );
1381         }
1382         else
1383         {
1384             /* brute force way to remove decorations */
1385             attributes.override_redirect = True;
1386             XChangeWindowAttributes( p_vout->p_sys->p_display,
1387                                      p_vout->p_sys->p_win->base_window,
1388                                      CWOverrideRedirect,
1389                                      &attributes);
1390         }
1391
1392         /* Make sure the change is effective */
1393         XReparentWindow( p_vout->p_sys->p_display,
1394                          p_vout->p_sys->p_win->base_window,
1395                          DefaultRootWindow( p_vout->p_sys->p_display ),
1396                          0, 0 );
1397
1398         /* fullscreen window size and position */
1399         p_vout->p_sys->p_win->i_width =
1400             DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1401         p_vout->p_sys->p_win->i_height =
1402             DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1403
1404         XMoveResizeWindow( p_vout->p_sys->p_display,
1405                            p_vout->p_sys->p_win->base_window,
1406                            0, 0,
1407                            p_vout->p_sys->p_win->i_width,
1408                            p_vout->p_sys->p_win->i_height );
1409     }
1410     else
1411     {
1412         msg_Dbg( p_vout, "leaving fullscreen mode" );
1413         DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1414         p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1415
1416         if( p_vout->p_sys->p_win->b_owned )
1417         {
1418             XMapWindow( p_vout->p_sys->p_display,
1419                         p_vout->p_sys->p_win->base_window);
1420         }
1421     }
1422
1423     /* Unfortunately, using XSync() here is not enough to ensure the
1424      * window has already been mapped because the XMapWindow() request
1425      * has not necessarily been sent directly to our window (remember,
1426      * the call is first redirected to the window manager) */
1427     if( p_vout->p_sys->p_win->b_owned )
1428     {
1429         do
1430         {
1431             XWindowEvent( p_vout->p_sys->p_display,
1432                           p_vout->p_sys->p_win->base_window,
1433                           StructureNotifyMask, &xevent );
1434         } while( xevent.type != MapNotify );
1435     }
1436     else
1437     {
1438         XSync( p_vout->p_sys->p_display, False );
1439     }
1440
1441     /* Becareful, this can generate a BadMatch error if the window is not
1442      * already mapped by the server (see above) */
1443     XSetInputFocus(p_vout->p_sys->p_display,
1444                    p_vout->p_sys->p_win->base_window,
1445                    RevertToParent,
1446                    CurrentTime);
1447
1448     /* signal that the size needs to be updated */
1449     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1450 }
1451
1452 /*****************************************************************************
1453  * EnableXScreenSaver: enable screen saver
1454  *****************************************************************************
1455  * This function enables the screen saver on a display after it has been
1456  * disabled by XDisableScreenSaver.
1457  * FIXME: what happens if multiple vlc sessions are running at the same
1458  *        time ???
1459  *****************************************************************************/
1460 static void EnableXScreenSaver( vout_thread_t *p_vout )
1461 {
1462 #ifdef DPMSINFO_IN_DPMS_H
1463     int dummy;
1464 #endif
1465
1466     if( p_vout->p_sys->i_ss_timeout )
1467     {
1468         XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1469                          p_vout->p_sys->i_ss_interval,
1470                          p_vout->p_sys->i_ss_blanking,
1471                          p_vout->p_sys->i_ss_exposure );
1472     }
1473
1474     /* Restore DPMS settings */
1475 #ifdef DPMSINFO_IN_DPMS_H
1476     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1477     {
1478         if( p_vout->p_sys->b_ss_dpms )
1479         {
1480             DPMSEnable( p_vout->p_sys->p_display );
1481         }
1482     }
1483 #endif
1484 }
1485
1486 /*****************************************************************************
1487  * DisableXScreenSaver: disable screen saver
1488  *****************************************************************************
1489  * See XEnableXScreenSaver
1490  *****************************************************************************/
1491 static void DisableXScreenSaver( vout_thread_t *p_vout )
1492 {
1493 #ifdef DPMSINFO_IN_DPMS_H
1494     int dummy;
1495 #endif
1496
1497     /* Save screen saver informations */
1498     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1499                      &p_vout->p_sys->i_ss_interval,
1500                      &p_vout->p_sys->i_ss_blanking,
1501                      &p_vout->p_sys->i_ss_exposure );
1502
1503     /* Disable screen saver */
1504     if( p_vout->p_sys->i_ss_timeout )
1505     {
1506         XSetScreenSaver( p_vout->p_sys->p_display, 0,
1507                          p_vout->p_sys->i_ss_interval,
1508                          p_vout->p_sys->i_ss_blanking,
1509                          p_vout->p_sys->i_ss_exposure );
1510     }
1511
1512     /* Disable DPMS */
1513 #ifdef DPMSINFO_IN_DPMS_H
1514     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1515     {
1516         CARD16 unused;
1517         /* Save DPMS current state */
1518         DPMSInfo( p_vout->p_sys->p_display, &unused,
1519                   &p_vout->p_sys->b_ss_dpms );
1520         DPMSDisable( p_vout->p_sys->p_display );
1521    }
1522 #endif
1523 }
1524
1525 /*****************************************************************************
1526  * CreateCursor: create a blank mouse pointer
1527  *****************************************************************************/
1528 static void CreateCursor( vout_thread_t *p_vout )
1529 {
1530     XColor cursor_color;
1531
1532     p_vout->p_sys->cursor_pixmap =
1533         XCreatePixmap( p_vout->p_sys->p_display,
1534                        DefaultRootWindow( p_vout->p_sys->p_display ),
1535                        1, 1, 1 );
1536
1537     XParseColor( p_vout->p_sys->p_display,
1538                  XCreateColormap( p_vout->p_sys->p_display,
1539                                   DefaultRootWindow(
1540                                                     p_vout->p_sys->p_display ),
1541                                   DefaultVisual(
1542                                                 p_vout->p_sys->p_display,
1543                                                 p_vout->p_sys->i_screen ),
1544                                   AllocNone ),
1545                  "black", &cursor_color );
1546
1547     p_vout->p_sys->blank_cursor =
1548         XCreatePixmapCursor( p_vout->p_sys->p_display,
1549                              p_vout->p_sys->cursor_pixmap,
1550                              p_vout->p_sys->cursor_pixmap,
1551                              &cursor_color, &cursor_color, 1, 1 );
1552 }
1553
1554 /*****************************************************************************
1555  * DestroyCursor: destroy the blank mouse pointer
1556  *****************************************************************************/
1557 static void DestroyCursor( vout_thread_t *p_vout )
1558 {
1559     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1560 }
1561
1562 /*****************************************************************************
1563  * ToggleCursor: hide or show the mouse pointer
1564  *****************************************************************************
1565  * This function hides the X pointer if it is visible by setting the pointer
1566  * sprite to a blank one. To show it again, we disable the sprite.
1567  *****************************************************************************/
1568 static void ToggleCursor( vout_thread_t *p_vout )
1569 {
1570     if( p_vout->p_sys->b_mouse_pointer_visible )
1571     {
1572         XDefineCursor( p_vout->p_sys->p_display,
1573                        p_vout->p_sys->p_win->base_window,
1574                        p_vout->p_sys->blank_cursor );
1575         p_vout->p_sys->b_mouse_pointer_visible = 0;
1576     }
1577     else
1578     {
1579         XUndefineCursor( p_vout->p_sys->p_display,
1580                          p_vout->p_sys->p_win->base_window );
1581         p_vout->p_sys->b_mouse_pointer_visible = 1;
1582     }
1583 }
1584
1585 #ifdef MODULE_NAME_IS_xvideo
1586 /*****************************************************************************
1587  * XVideoGetPort: get YUV12 port
1588  *****************************************************************************/
1589 static int XVideoGetPort( vout_thread_t *p_vout,
1590                           vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1591 {
1592     XvAdaptorInfo *p_adaptor;
1593     unsigned int i;
1594     int i_adaptor, i_num_adaptors, i_requested_adaptor;
1595     int i_selected_port;
1596
1597     switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1598     {
1599         case Success:
1600             break;
1601
1602         case XvBadExtension:
1603             msg_Warn( p_vout, "XvBadExtension" );
1604             return -1;
1605
1606         case XvBadAlloc:
1607             msg_Warn( p_vout, "XvBadAlloc" );
1608             return -1;
1609
1610         default:
1611             msg_Warn( p_vout, "XvQueryExtension failed" );
1612             return -1;
1613     }
1614
1615     switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1616                              DefaultRootWindow( p_vout->p_sys->p_display ),
1617                              &i_num_adaptors, &p_adaptor ) )
1618     {
1619         case Success:
1620             break;
1621
1622         case XvBadExtension:
1623             msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1624             return -1;
1625
1626         case XvBadAlloc:
1627             msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1628             return -1;
1629
1630         default:
1631             msg_Warn( p_vout, "XvQueryAdaptors failed" );
1632             return -1;
1633     }
1634
1635     i_selected_port = -1;
1636     i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1637
1638     for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1639     {
1640         XvImageFormatValues *p_formats;
1641         int i_format, i_num_formats;
1642         int i_port;
1643
1644         /* If we requested an adaptor and it's not this one, we aren't
1645          * interested */
1646         if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1647         {
1648             continue;
1649         }
1650
1651         /* If the adaptor doesn't have the required properties, skip it */
1652         if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1653             !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1654         {
1655             continue;
1656         }
1657
1658         /* Check that adaptor supports our requested format... */
1659         p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1660                                         p_adaptor[i_adaptor].base_id,
1661                                         &i_num_formats );
1662
1663         for( i_format = 0;
1664              i_format < i_num_formats && ( i_selected_port == -1 );
1665              i_format++ )
1666         {
1667             /* Code removed, we can get this through xvinfo anyway */
1668 #if 0
1669             XvEncodingInfo  *p_enc;
1670             int             i_enc, i_num_encodings;
1671             XvAttribute     *p_attr;
1672             int             i_attr, i_num_attributes;
1673 #endif
1674
1675             /* If this is not the format we want, or at least a
1676              * similar one, forget it */
1677             if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1678             {
1679                 continue;
1680             }
1681
1682             /* Look for the first available port supporting this format */
1683             for( i_port = p_adaptor[i_adaptor].base_id;
1684                  ( i_port < (int)(p_adaptor[i_adaptor].base_id
1685                                    + p_adaptor[i_adaptor].num_ports) )
1686                    && ( i_selected_port == -1 );
1687                  i_port++ )
1688             {
1689                 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1690                      == Success )
1691                 {
1692                     i_selected_port = i_port;
1693                     *pi_newchroma = p_formats[ i_format ].id;
1694                 }
1695             }
1696
1697             /* If no free port was found, forget it */
1698             if( i_selected_port == -1 )
1699             {
1700                 continue;
1701             }
1702
1703             /* If we found a port, print information about it */
1704             msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1705                      i_adaptor, i_selected_port, p_formats[ i_format ].id,
1706                      (char *)&p_formats[ i_format ].id,
1707                      ( p_formats[ i_format ].format == XvPacked ) ?
1708                          "packed" : "planar" );
1709
1710 #if 0
1711             msg_Dbg( p_vout, " encoding list:" );
1712
1713             if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1714                                   &i_num_encodings, &p_enc )
1715                  != Success )
1716             {
1717                 msg_Dbg( p_vout, "  XvQueryEncodings failed" );
1718                 continue;
1719             }
1720
1721             for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1722             {
1723                 msg_Dbg( p_vout, "  id=%ld, name=%s, size=%ldx%ld,"
1724                                       " numerator=%d, denominator=%d",
1725                              p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1726                              p_enc[i_enc].width, p_enc[i_enc].height,
1727                              p_enc[i_enc].rate.numerator,
1728                              p_enc[i_enc].rate.denominator );
1729             }
1730
1731             if( p_enc != NULL )
1732             {
1733                 XvFreeEncodingInfo( p_enc );
1734             }
1735
1736             msg_Dbg( p_vout, " attribute list:" );
1737             p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1738                                             i_selected_port,
1739                                             &i_num_attributes );
1740             for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1741             {
1742                 msg_Dbg( p_vout, "  name=%s, flags=[%s%s ], min=%i, max=%i",
1743                       p_attr[i_attr].name,
1744                       (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1745                       (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1746                       p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1747             }
1748
1749             if( p_attr != NULL )
1750             {
1751                 XFree( p_attr );
1752             }
1753 #endif
1754         }
1755
1756         if( p_formats != NULL )
1757         {
1758             XFree( p_formats );
1759         }
1760
1761     }
1762
1763     if( i_num_adaptors > 0 )
1764     {
1765         XvFreeAdaptorInfo( p_adaptor );
1766     }
1767
1768     if( i_selected_port == -1 )
1769     {
1770         if( i_requested_adaptor == -1 )
1771         {
1772             msg_Warn( p_vout, "no free XVideo port found for format "
1773                        "0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
1774         }
1775         else
1776         {
1777             msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1778                        "XVideo port for format 0x%.8x (%4.4s)",
1779                        i_requested_adaptor, i_chroma, (char*)&i_chroma );
1780         }
1781     }
1782
1783     return i_selected_port;
1784 }
1785
1786 /*****************************************************************************
1787  * XVideoReleasePort: release YUV12 port
1788  *****************************************************************************/
1789 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1790 {
1791     XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1792 }
1793 #endif
1794
1795 /*****************************************************************************
1796  * InitDisplay: open and initialize X11 device
1797  *****************************************************************************
1798  * Create a window according to video output given size, and set other
1799  * properties according to the display properties.
1800  *****************************************************************************/
1801 static int InitDisplay( vout_thread_t *p_vout )
1802 {
1803 #ifdef MODULE_NAME_IS_x11
1804     XPixmapFormatValues *       p_formats;                 /* pixmap formats */
1805     XVisualInfo *               p_xvisual;           /* visuals informations */
1806     XVisualInfo                 xvisual_template;         /* visual template */
1807     int                         i_count;                       /* array size */
1808 #endif
1809
1810 #ifdef HAVE_SYS_SHM_H
1811     p_vout->p_sys->b_shm = 0;
1812
1813     if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1814     {
1815 #   ifdef SYS_DARWIN
1816         /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1817 #   else
1818         p_vout->p_sys->b_shm =
1819                   ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1820 #   endif
1821
1822         if( !p_vout->p_sys->b_shm )
1823         {
1824             msg_Warn( p_vout, "XShm video extension is unavailable" );
1825         }
1826     }
1827     else
1828     {
1829         msg_Dbg( p_vout, "disabling XShm video extension" );
1830     }
1831
1832 #else
1833     msg_Warn( p_vout, "XShm video extension is unavailable" );
1834
1835 #endif
1836
1837 #ifdef MODULE_NAME_IS_xvideo
1838     /* XXX The brightness and contrast values should be read from environment
1839      * XXX variables... */
1840 #if 0
1841     XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1842     XVideoSetAttribute( p_vout, "XV_CONTRAST",   0.5 );
1843 #endif
1844 #endif
1845
1846 #ifdef MODULE_NAME_IS_x11
1847     /* Initialize structure */
1848     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1849
1850     /* Get screen depth */
1851     p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1852                                                    p_vout->p_sys->i_screen );
1853     switch( p_vout->p_sys->i_screen_depth )
1854     {
1855     case 8:
1856         /*
1857          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1858          */
1859         xvisual_template.screen =   p_vout->p_sys->i_screen;
1860         xvisual_template.class =    DirectColor;
1861         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1862                                     VisualScreenMask | VisualClassMask,
1863                                     &xvisual_template, &i_count );
1864         if( p_xvisual == NULL )
1865         {
1866             msg_Err( p_vout, "no PseudoColor visual available" );
1867             return VLC_EGENERIC;
1868         }
1869         p_vout->p_sys->i_bytes_per_pixel = 1;
1870         p_vout->output.pf_setpalette = SetPalette;
1871         break;
1872     case 15:
1873     case 16:
1874     case 24:
1875     default:
1876         /*
1877          * Screen depth is higher than 8bpp. TrueColor visual is used.
1878          */
1879         xvisual_template.screen =   p_vout->p_sys->i_screen;
1880         xvisual_template.class =    TrueColor;
1881         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1882                                     VisualScreenMask | VisualClassMask,
1883                                     &xvisual_template, &i_count );
1884         if( p_xvisual == NULL )
1885         {
1886             msg_Err( p_vout, "no TrueColor visual available" );
1887             return VLC_EGENERIC;
1888         }
1889
1890         p_vout->output.i_rmask = p_xvisual->red_mask;
1891         p_vout->output.i_gmask = p_xvisual->green_mask;
1892         p_vout->output.i_bmask = p_xvisual->blue_mask;
1893
1894         /* There is no difference yet between 3 and 4 Bpp. The only way
1895          * to find the actual number of bytes per pixel is to list supported
1896          * pixmap formats. */
1897         p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1898         p_vout->p_sys->i_bytes_per_pixel = 0;
1899
1900         for( ; i_count-- ; p_formats++ )
1901         {
1902             /* Under XFree4.0, the list contains pixmap formats available
1903              * through all video depths ; so we have to check against current
1904              * depth. */
1905             if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
1906             {
1907                 if( p_formats->bits_per_pixel / 8
1908                         > (int)p_vout->p_sys->i_bytes_per_pixel )
1909                 {
1910                     p_vout->p_sys->i_bytes_per_pixel =
1911                                                p_formats->bits_per_pixel / 8;
1912                 }
1913             }
1914         }
1915         break;
1916     }
1917     p_vout->p_sys->p_visual = p_xvisual->visual;
1918     XFree( p_xvisual );
1919 #endif
1920
1921     return VLC_SUCCESS;
1922 }
1923
1924 #ifdef HAVE_SYS_SHM_H
1925 /*****************************************************************************
1926  * CreateShmImage: create an XImage or XvImage using shared memory extension
1927  *****************************************************************************
1928  * Prepare an XImage or XvImage for display function.
1929  * The order of the operations respects the recommandations of the mit-shm
1930  * document by J.Corbet and K.Packard. Most of the parameters were copied from
1931  * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1932  *****************************************************************************/
1933 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
1934                                     Display* p_display, EXTRA_ARGS_SHM,
1935                                     int i_width, int i_height )
1936 {
1937     IMAGE_TYPE *p_image;
1938
1939     /* Create XImage / XvImage */
1940 #ifdef MODULE_NAME_IS_xvideo
1941     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1942                                 i_width, i_height, p_shm );
1943 #else
1944     p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1945                                p_shm, i_width, i_height );
1946 #endif
1947     if( p_image == NULL )
1948     {
1949         msg_Err( p_vout, "image creation failed" );
1950         return NULL;
1951     }
1952
1953     /* Allocate shared memory segment - 0776 set the access permission
1954      * rights (like umask), they are not yet supported by all X servers */
1955     p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
1956     if( p_shm->shmid < 0 )
1957     {
1958         msg_Err( p_vout, "cannot allocate shared image data (%s)",
1959                          strerror( errno ) );
1960         IMAGE_FREE( p_image );
1961         return NULL;
1962     }
1963
1964     /* Attach shared memory segment to process (read/write) */
1965     p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
1966     if(! p_shm->shmaddr )
1967     {
1968         msg_Err( p_vout, "cannot attach shared memory (%s)",
1969                          strerror(errno));
1970         IMAGE_FREE( p_image );
1971         shmctl( p_shm->shmid, IPC_RMID, 0 );
1972         return NULL;
1973     }
1974
1975     /* Read-only data. We won't be using XShmGetImage */
1976     p_shm->readOnly = True;
1977
1978     /* Attach shared memory segment to X server */
1979     if( XShmAttach( p_display, p_shm ) == False )
1980     {
1981         msg_Err( p_vout, "cannot attach shared memory to X server" );
1982         IMAGE_FREE( p_image );
1983         shmctl( p_shm->shmid, IPC_RMID, 0 );
1984         shmdt( p_shm->shmaddr );
1985         return NULL;
1986     }
1987
1988     /* Send image to X server. This instruction is required, since having
1989      * built a Shm XImage and not using it causes an error on XCloseDisplay,
1990      * and remember NOT to use XFlush ! */
1991     XSync( p_display, False );
1992
1993 #if 0
1994     /* Mark the shm segment to be removed when there are no more
1995      * attachements, so it is automatic on process exit or after shmdt */
1996     shmctl( p_shm->shmid, IPC_RMID, 0 );
1997 #endif
1998
1999     return p_image;
2000 }
2001 #endif
2002
2003 /*****************************************************************************
2004  * CreateImage: create an XImage or XvImage
2005  *****************************************************************************
2006  * Create a simple image used as a buffer.
2007  *****************************************************************************/
2008 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2009                                  Display *p_display, EXTRA_ARGS,
2010                                  int i_width, int i_height )
2011 {
2012     byte_t *    p_data;                           /* image data storage zone */
2013     IMAGE_TYPE *p_image;
2014 #ifdef MODULE_NAME_IS_x11
2015     int         i_quantum;                     /* XImage quantum (see below) */
2016     int         i_bytes_per_line;
2017 #endif
2018
2019     /* Allocate memory for image */
2020 #ifdef MODULE_NAME_IS_xvideo
2021     p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2022 #else
2023     i_bytes_per_line = i_width * i_bytes_per_pixel;
2024     p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2025 #endif
2026     if( !p_data )
2027     {
2028         msg_Err( p_vout, "out of memory" );
2029         return NULL;
2030     }
2031
2032 #ifdef MODULE_NAME_IS_x11
2033     /* Optimize the quantum of a scanline regarding its size - the quantum is
2034        a diviser of the number of bits between the start of two scanlines. */
2035     if( i_bytes_per_line & 0xf )
2036     {
2037         i_quantum = 0x8;
2038     }
2039     else if( i_bytes_per_line & 0x10 )
2040     {
2041         i_quantum = 0x10;
2042     }
2043     else
2044     {
2045         i_quantum = 0x20;
2046     }
2047 #endif
2048
2049     /* Create XImage. p_data will be automatically freed */
2050 #ifdef MODULE_NAME_IS_xvideo
2051     p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2052                              p_data, i_width, i_height );
2053 #else
2054     p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2055                             p_data, i_width, i_height, i_quantum, 0 );
2056 #endif
2057     if( p_image == NULL )
2058     {
2059         msg_Err( p_vout, "XCreateImage() failed" );
2060         free( p_data );
2061         return NULL;
2062     }
2063
2064     return p_image;
2065 }
2066
2067 #ifdef MODULE_NAME_IS_x11
2068 /*****************************************************************************
2069  * SetPalette: sets an 8 bpp palette
2070  *****************************************************************************
2071  * This function sets the palette given as an argument. It does not return
2072  * anything, but could later send information on which colors it was unable
2073  * to set.
2074  *****************************************************************************/
2075 static void SetPalette( vout_thread_t *p_vout,
2076                         uint16_t *red, uint16_t *green, uint16_t *blue )
2077 {
2078     int i;
2079     XColor p_colors[255];
2080
2081     /* allocate palette */
2082     for( i = 0; i < 255; i++ )
2083     {
2084         /* kludge: colors are indexed reversely because color 255 seems
2085          * to be reserved for black even if we try to set it to white */
2086         p_colors[ i ].pixel = 255 - i;
2087         p_colors[ i ].pad   = 0;
2088         p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2089         p_colors[ i ].red   = red[ 255 - i ];
2090         p_colors[ i ].blue  = blue[ 255 - i ];
2091         p_colors[ i ].green = green[ 255 - i ];
2092     }
2093
2094     XStoreColors( p_vout->p_sys->p_display,
2095                   p_vout->p_sys->colormap, p_colors, 255 );
2096 }
2097 #endif