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