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