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