]> git.sesse.net Git - vlc/blob - modules/video_output/x11/xcommon.c
GLX: remove some useless stuff (that should never have been in GLX)
[vlc] / modules / video_output / x11 / xcommon.c
1 /*****************************************************************************
2  * xcommon.c: Functions common to the X11 and XVideo plugins
3  *****************************************************************************
4  * Copyright (C) 1998-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Sam Hocevar <sam@zoy.org>
9  *          David Kennedy <dkennedy@tinytoad.com>
10  *          Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
37 #include <vlc_vout.h>
38 #include <vlc_vout_window.h>
39 #include <vlc_keys.h>
40
41 #ifdef HAVE_MACHINE_PARAM_H
42     /* BSD */
43 #   include <machine/param.h>
44 #   include <sys/types.h>                                  /* typedef ushort */
45 #   include <sys/ipc.h>
46 #endif
47
48 #ifdef HAVE_XSP
49 #include <X11/extensions/Xsp.h>
50 #endif
51
52 #ifdef HAVE_SYS_SHM_H
53 #   include <sys/shm.h>                                /* shmget(), shmctl() */
54 #endif
55
56 #include <X11/Xlib.h>
57 #include <X11/Xproto.h>
58 #include <X11/Xmd.h>
59 #include <X11/Xutil.h>
60 #include <X11/keysym.h>
61 #include <X11/XF86keysym.h>
62 #ifdef HAVE_SYS_SHM_H
63 #   include <X11/extensions/XShm.h>
64 #endif
65 #ifdef DPMSINFO_IN_DPMS_H
66 #   include <X11/extensions/dpms.h>
67 #endif
68
69 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
70 #   include <X11/extensions/Xv.h>
71 #   include <X11/extensions/Xvlib.h>
72 #endif
73
74 #ifdef MODULE_NAME_IS_glx
75 #   include <GL/glx.h>
76 #endif
77
78 #ifdef MODULE_NAME_IS_xvmc
79 #   include <X11/extensions/vldXvMC.h>
80 #   include "../../codec/xvmc/accel_xvmc.h"
81 #endif
82
83 #include "xcommon.h"
84
85 /*****************************************************************************
86  * Local prototypes
87  *****************************************************************************/
88 int  Activate   ( vlc_object_t * );
89 void Deactivate ( vlc_object_t * );
90
91 #ifndef MODULE_NAME_IS_glx
92 static int  InitVideo      ( vout_thread_t * );
93 static void EndVideo       ( vout_thread_t * );
94 static void DisplayVideo   ( vout_thread_t *, picture_t * );
95 #endif
96 static int  ManageVideo    ( vout_thread_t * );
97 static int  Control        ( vout_thread_t *, int, va_list );
98
99 static int  InitDisplay    ( vout_thread_t * );
100
101 static int  CreateWindow   ( vout_thread_t *, x11_window_t * );
102 static void DestroyWindow  ( vout_thread_t *, x11_window_t * );
103
104 #ifndef MODULE_NAME_IS_glx
105 static int  NewPicture     ( vout_thread_t *, picture_t * );
106 static void FreePicture    ( vout_thread_t *, picture_t * );
107 #endif
108
109 #ifdef HAVE_SYS_SHM_H
110 static int i_shm_major = 0;
111 #endif
112
113 static void ToggleFullScreen      ( vout_thread_t * );
114
115 static void EnableXScreenSaver    ( vout_thread_t * );
116 static void DisableXScreenSaver   ( vout_thread_t * );
117
118 static void CreateCursor   ( vout_thread_t * );
119 static void DestroyCursor  ( vout_thread_t * );
120 static void ToggleCursor   ( vout_thread_t * );
121
122 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
123 static int  XVideoGetPort    ( vout_thread_t *, vlc_fourcc_t, picture_heap_t * );
124 static void XVideoReleasePort( vout_thread_t *, int );
125 #endif
126
127 #ifdef MODULE_NAME_IS_x11
128 static void SetPalette     ( vout_thread_t *,
129                              uint16_t *, uint16_t *, uint16_t * );
130 #endif
131
132 #ifdef MODULE_NAME_IS_xvmc
133 static void RenderVideo    ( vout_thread_t *, picture_t * );
134 static int  xvmc_check_yv12( Display *display, XvPortID port );
135 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
136 #endif
137
138 static int X11ErrorHandler( Display *, XErrorEvent * );
139
140 #ifdef HAVE_XSP
141 static void EnablePixelDoubling( vout_thread_t *p_vout );
142 static void DisablePixelDoubling( vout_thread_t *p_vout );
143 #endif
144
145 #ifdef HAVE_OSSO
146 static const int i_backlight_on_interval = 300;
147 #endif
148
149
150
151 /*****************************************************************************
152  * Activate: allocate X11 video thread output method
153  *****************************************************************************
154  * This function allocate and initialize a X11 vout method. It uses some of the
155  * vout properties to choose the window size, and change them according to the
156  * actual properties of the display.
157  *****************************************************************************/
158 int Activate ( vlc_object_t *p_this )
159 {
160     vout_thread_t *p_vout = (vout_thread_t *)p_this;
161     char *        psz_display;
162 #if defined(MODULE_NAME_IS_xvmc)
163     char *psz_value;
164 #endif
165 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
166     char *       psz_chroma;
167     vlc_fourcc_t i_chroma = 0;
168     bool   b_chroma = 0;
169 #endif
170
171 #ifndef MODULE_NAME_IS_glx
172     p_vout->pf_init = InitVideo;
173     p_vout->pf_end = EndVideo;
174     p_vout->pf_display = DisplayVideo;
175 #endif
176 #ifdef MODULE_NAME_IS_xvmc
177     p_vout->pf_render = RenderVideo;
178 #else
179     p_vout->pf_render = NULL;
180 #endif
181     p_vout->pf_manage = ManageVideo;
182     p_vout->pf_control = Control;
183
184     /* Allocate structure */
185     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
186     if( p_vout->p_sys == NULL )
187         return VLC_ENOMEM;
188
189     /* key and mouse event handling */
190     p_vout->p_sys->i_vout_event = var_CreateGetInteger( p_vout, "vout-event" );
191
192     /* Open display, using the "display" config variable or the DISPLAY
193      * environment variable */
194     psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
195
196     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
197
198     if( p_vout->p_sys->p_display == NULL )                         /* error */
199     {
200         msg_Err( p_vout, "cannot open display %s",
201                          XDisplayName( psz_display ) );
202         free( p_vout->p_sys );
203         free( psz_display );
204         return VLC_EGENERIC;
205     }
206     free( psz_display );
207
208     /* Replace error handler so we can intercept some non-fatal errors */
209     XSetErrorHandler( X11ErrorHandler );
210
211     /* Get a screen ID matching the XOpenDisplay return value */
212     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
213
214 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
215     psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
216     if( psz_chroma )
217     {
218         if( strlen( psz_chroma ) >= 4 )
219         {
220             /* Do not use direct assignment because we are not sure of the
221              * alignment. */
222             memcpy(&i_chroma, psz_chroma, 4);
223             b_chroma = 1;
224         }
225
226         free( psz_chroma );
227     }
228
229     if( b_chroma )
230     {
231         msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
232                  i_chroma, (char*)&i_chroma );
233     }
234     else
235     {
236         i_chroma = p_vout->render.i_chroma;
237     }
238
239     /* Check that we have access to an XVideo port providing this chroma */
240     p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
241                                              &p_vout->output );
242     if( p_vout->p_sys->i_xvport < 0 )
243     {
244         /* If a specific chroma format was requested, then we don't try to
245          * be cleverer than the user. He knew pretty well what he wanted. */
246         if( b_chroma )
247         {
248             XCloseDisplay( p_vout->p_sys->p_display );
249             free( p_vout->p_sys );
250             return VLC_EGENERIC;
251         }
252
253         /* It failed, but it's not completely lost ! We try to open an
254          * XVideo port for an YUY2 picture. We'll need to do an YUV
255          * conversion, but at least it has got scaling. */
256         p_vout->p_sys->i_xvport =
257                         XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
258                                                &p_vout->output );
259         if( p_vout->p_sys->i_xvport < 0 )
260         {
261             /* It failed, but it's not completely lost ! We try to open an
262              * XVideo port for a simple 16bpp RGB picture. We'll need to do
263              * an YUV conversion, but at least it has got scaling. */
264             p_vout->p_sys->i_xvport =
265                             XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
266                                                    &p_vout->output );
267             if( p_vout->p_sys->i_xvport < 0 )
268             {
269                 XCloseDisplay( p_vout->p_sys->p_display );
270                 free( p_vout->p_sys );
271                 return VLC_EGENERIC;
272             }
273         }
274     }
275     p_vout->output.i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, X112VLC_FOURCC(p_vout->output.i_chroma) );
276 #elif defined(MODULE_NAME_IS_glx)
277     {
278         int i_opcode, i_evt, i_err = 0;
279         int i_maj, i_min = 0;
280
281         /* Check for GLX extension */
282         if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
283                               &i_opcode, &i_evt, &i_err ) )
284         {
285             msg_Err( p_this, "GLX extension not supported" );
286             XCloseDisplay( p_vout->p_sys->p_display );
287             free( p_vout->p_sys );
288             return VLC_EGENERIC;
289         }
290         if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
291         {
292             msg_Err( p_this, "glXQueryExtension failed" );
293             XCloseDisplay( p_vout->p_sys->p_display );
294             free( p_vout->p_sys );
295             return VLC_EGENERIC;
296         }
297
298         /* Check GLX version */
299         if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
300         {
301             msg_Err( p_this, "glXQueryVersion failed" );
302             XCloseDisplay( p_vout->p_sys->p_display );
303             free( p_vout->p_sys );
304             return VLC_EGENERIC;
305         }
306         if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
307         {
308             p_vout->p_sys->b_glx13 = false;
309             msg_Dbg( p_this, "using GLX 1.2 API" );
310         }
311         else
312         {
313             p_vout->p_sys->b_glx13 = true;
314             msg_Dbg( p_this, "using GLX 1.3 API" );
315         }
316     }
317 #endif
318
319     /* Create blank cursor (for mouse cursor autohiding) */
320     p_vout->p_sys->i_time_mouse_last_moved = mdate();
321     p_vout->p_sys->i_mouse_hide_timeout =
322         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
323     p_vout->p_sys->b_mouse_pointer_visible = 1;
324     CreateCursor( p_vout );
325
326     /* Set main window's size */
327     p_vout->p_sys->window.i_x      = 0;
328     p_vout->p_sys->window.i_y      = 0;
329     p_vout->p_sys->window.i_width  = p_vout->i_window_width;
330     p_vout->p_sys->window.i_height = p_vout->i_window_height;
331     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
332     /* Spawn base window - this window will include the video output window,
333      * but also command buttons, subtitles and other indicators */
334     if( CreateWindow( p_vout, &p_vout->p_sys->window ) )
335     {
336         msg_Err( p_vout, "cannot create X11 window" );
337         DestroyCursor( p_vout );
338         XCloseDisplay( p_vout->p_sys->p_display );
339         free( p_vout->p_sys );
340         return VLC_EGENERIC;
341     }
342
343     /* Open and initialize device. */
344     if( InitDisplay( p_vout ) )
345     {
346         msg_Err( p_vout, "cannot initialize X11 display" );
347         DestroyCursor( p_vout );
348         DestroyWindow( p_vout, &p_vout->p_sys->window );
349         XCloseDisplay( p_vout->p_sys->p_display );
350         free( p_vout->p_sys );
351         return VLC_EGENERIC;
352     }
353
354     /* Disable screen saver */
355     DisableXScreenSaver( p_vout );
356
357     /* Misc init */
358     p_vout->p_sys->i_time_button_last_pressed = 0;
359
360 #ifdef MODULE_NAME_IS_xvmc
361     p_vout->p_sys->p_last_subtitle_save = NULL;
362     psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
363
364     /* Look what method was requested */
365     //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
366     //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
367     if( psz_value )
368     {
369         if( (strcmp(psz_value, "bob") == 0) ||
370             (strcmp(psz_value, "blend") == 0) )
371            p_vout->p_sys->xvmc_deinterlace_method = 2;
372         else if (strcmp(psz_value, "discard") == 0)
373            p_vout->p_sys->xvmc_deinterlace_method = 1;
374         else
375            p_vout->p_sys->xvmc_deinterlace_method = 0;
376         free(psz_value );
377     }
378     else
379         p_vout->p_sys->xvmc_deinterlace_method = 0;
380
381     /* Look what method was requested */
382     //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
383     //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
384     psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
385
386     if( psz_value )
387     {
388         if( strncmp( psz_value, "eq", 2 ) == 0 )
389            p_vout->p_sys->xvmc_crop_style = 1;
390         else if( strncmp( psz_value, "4-16", 4 ) == 0)
391            p_vout->p_sys->xvmc_crop_style = 2;
392         else if( strncmp( psz_value, "16-4", 4 ) == 0)
393            p_vout->p_sys->xvmc_crop_style = 3;
394         else
395            p_vout->p_sys->xvmc_crop_style = 0;
396         free( psz_value );
397     }
398     else
399         p_vout->p_sys->xvmc_crop_style = 0;
400
401     msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
402     msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
403
404     if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
405     {
406         msg_Err( p_vout, "no XVMC capability found" );
407         Deactivate( p_this );
408         return VLC_EGENERIC;
409     }
410     subpicture_t sub_pic;
411     sub_pic.p_sys = NULL;
412     p_vout->p_sys->last_date = 0;
413 #endif
414
415 #ifdef HAVE_XSP
416     p_vout->p_sys->i_hw_scale = 1;
417 #endif
418
419 #ifdef HAVE_OSSO
420     p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
421     p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
422     if ( p_vout->p_sys->p_octx == NULL ) {
423         msg_Err( p_vout, "Could not get osso context" );
424     } else {
425         msg_Dbg( p_vout, "Initialized osso context" );
426     }
427 #endif
428
429     /* Variable to indicate if the window should be on top of others */
430     /* Trigger a callback right now */
431     var_TriggerCallback( p_vout, "video-on-top" );
432
433     return VLC_SUCCESS;
434 }
435
436 /*****************************************************************************
437  * Deactivate: destroy X11 video thread output method
438  *****************************************************************************
439  * Terminate an output method created by Open
440  *****************************************************************************/
441 void Deactivate ( vlc_object_t *p_this )
442 {
443     vout_thread_t *p_vout = (vout_thread_t *)p_this;
444
445     /* Restore cursor if it was blanked */
446     if( !p_vout->p_sys->b_mouse_pointer_visible )
447     {
448         ToggleCursor( p_vout );
449     }
450
451 #ifdef MODULE_NAME_IS_x11
452     /* Destroy colormap */
453     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
454     {
455         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
456     }
457 #elif defined(MODULE_NAME_IS_xvideo)
458     XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
459 #elif defined(MODULE_NAME_IS_xvmc)
460     if( p_vout->p_sys->xvmc_cap )
461     {
462         xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
463         xxmc_dispose_context( p_vout );
464         if( p_vout->p_sys->old_subpic )
465         {
466             xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
467             p_vout->p_sys->old_subpic = NULL;
468         }
469         if( p_vout->p_sys->new_subpic )
470         {
471             xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
472             p_vout->p_sys->new_subpic = NULL;
473         }
474         free( p_vout->p_sys->xvmc_cap );
475         xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
476     }
477 #endif
478
479 #ifdef HAVE_XSP
480     DisablePixelDoubling(p_vout);
481 #endif
482
483     DestroyCursor( p_vout );
484     EnableXScreenSaver( p_vout );
485     DestroyWindow( p_vout, &p_vout->p_sys->window );
486     XCloseDisplay( p_vout->p_sys->p_display );
487
488     /* Destroy structure */
489 #ifdef MODULE_NAME_IS_xvmc
490     free_context_lock( &p_vout->p_sys->xvmc_lock );
491 #endif
492
493 #ifdef HAVE_OSSO
494     if ( p_vout->p_sys->p_octx != NULL ) {
495         msg_Dbg( p_vout, "Deinitializing osso context" );
496         osso_deinitialize( p_vout->p_sys->p_octx );
497     }
498 #endif
499
500     free( p_vout->p_sys );
501 }
502
503 #ifdef MODULE_NAME_IS_xvmc
504
505 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
506
507 /* called xlocked */
508 static int xvmc_check_yv12( Display *display, XvPortID port )
509 {
510     XvImageFormatValues *formatValues;
511     int                  formats;
512     int                  i;
513
514     formatValues = XvListImageFormats( display, port, &formats );
515
516     for( i = 0; i < formats; i++ )
517     {
518         if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
519             ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
520         {
521             XFree (formatValues);
522             return 0;
523         }
524     }
525
526     XFree (formatValues);
527     return 1;
528 }
529
530 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
531 {
532     XvMCSyncSurface( p_vout->p_sys->p_display, srf );
533 }
534
535 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
536 {
537     Atom         atom;
538     int          xv_double_buffer;
539
540     xv_double_buffer = 1;
541
542     XLockDisplay( p_vout->p_sys->p_display );
543     atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
544 #if 0
545     XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
546 #endif
547     XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
548     XUnlockDisplay( p_vout->p_sys->p_display );
549
550     //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
551     //    "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
552 }
553
554 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
555 {
556     vlc_xxmc_t *xxmc = NULL;
557
558     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
559
560     xxmc = &p_pic->p_sys->xxmc_data;
561     if( (!xxmc->decoded ||
562         !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
563     {
564         xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
565         return;
566     }
567
568 #if 0
569     vlc_mutex_lock( &p_vout->lastsubtitle_lock );
570     if (p_vout->p_sys->p_last_subtitle != NULL)
571     {
572         if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
573         {
574             p_vout->p_sys->new_subpic =
575                 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
576                     p_vout->p_sys->xvmc_width,
577                     p_vout->p_sys->xvmc_height,
578                     p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
579
580             if (p_vout->p_sys->new_subpic)
581             {
582                 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
583                 XvMCClearSubpicture( p_vout->p_sys->p_display,
584                         p_vout->p_sys->new_subpic,
585                         0,
586                         0,
587                         p_vout->p_sys->xvmc_width,
588                         p_vout->p_sys->xvmc_height,
589                         0x00 );
590                 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
591                 clear_xx44_palette( &p_vout->p_sys->palette );
592
593                 if( sub_pic.p_sys == NULL )
594                 {
595                     sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
596                     if( sub_pic.p_sys != NULL )
597                     {
598                         sub_pic.p_sys->p_vout = p_vout;
599                         sub_pic.p_sys->xvmc_surf = NULL;
600                         sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
601                     }
602                 }
603                 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
604                 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
605                 sub_pic.p->i_pitch = p_vout->output.i_width;
606
607                 memset( p_vout->p_sys->subImage->data, 0,
608                         (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
609
610                 if (p_vout->p_last_subtitle != NULL)
611                 {
612                     blend_xx44( p_vout->p_sys->subImage->data,
613                                 p_vout->p_last_subtitle,
614                                 p_vout->p_sys->subImage->width,
615                                 p_vout->p_sys->subImage->height,
616                                 p_vout->p_sys->subImage->width,
617                                 &p_vout->p_sys->palette,
618                                 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
619                 }
620
621                 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
622                 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
623                                          p_vout->p_sys->new_subpic,
624                                          p_vout->p_sys->subImage,
625                                          0, /* overlay->x */
626                                          0, /* overlay->y */
627                                          p_vout->output.i_width, /* overlay->width, */
628                                          p_vout->output.i_height, /* overlay->height */
629                                          0, /* overlay->x */
630                                          0 ); /*overlay->y */
631                 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
632                 if (p_vout->p_sys->old_subpic)
633                 {
634                     xxmc_xvmc_free_subpicture( p_vout,
635                                                p_vout->p_sys->old_subpic);
636                     p_vout->p_sys->old_subpic = NULL;
637                 }
638                 if (p_vout->p_sys->new_subpic)
639                 {
640                     p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
641                     p_vout->p_sys->new_subpic = NULL;
642                     xx44_to_xvmc_palette( &p_vout->p_sys->palette,
643                             p_vout->p_sys->xvmc_palette,
644                             0,
645                             p_vout->p_sys->old_subpic->num_palette_entries,
646                             p_vout->p_sys->old_subpic->entry_bytes,
647                             p_vout->p_sys->old_subpic->component_order );
648                     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
649                     XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
650                                               p_vout->p_sys->old_subpic,
651                                               p_vout->p_sys->xvmc_palette );
652                     XvMCFlushSubpicture( p_vout->p_sys->p_display,
653                                          p_vout->p_sys->old_subpic);
654                     XvMCSyncSubpicture( p_vout->p_sys->p_display,
655                                         p_vout->p_sys->old_subpic );
656                     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
657                 }
658
659                 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
660                 if (p_vout->p_sys->xvmc_backend_subpic )
661                 {
662                     XvMCBlendSubpicture( p_vout->p_sys->p_display,
663                                          p_pic->p_sys->xvmc_surf,
664                                          p_vout->p_sys->old_subpic,
665                                          0,
666                                          0,
667                                          p_vout->p_sys->xvmc_width,
668                                          p_vout->p_sys->xvmc_height,
669                                          0,
670                                          0,
671                                          p_vout->p_sys->xvmc_width,
672                                          p_vout->p_sys->xvmc_height );
673                 }
674                 else
675                 {
676                     XvMCBlendSubpicture2( p_vout->p_sys->p_display,
677                                           p_pic->p_sys->xvmc_surf,
678                                           p_pic->p_sys->xvmc_surf,
679                                           p_vout->p_sys->old_subpic,
680                                           0,
681                                           0,
682                                           p_vout->p_sys->xvmc_width,
683                                           p_vout->p_sys->xvmc_height,
684                                           0,
685                                           0,
686                                           p_vout->p_sys->xvmc_width,
687                                           p_vout->p_sys->xvmc_height );
688                }
689                XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
690             }
691         }
692         else
693         {
694             XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
695             if( p_vout->p_sys->xvmc_backend_subpic )
696             {
697                 XvMCBlendSubpicture( p_vout->p_sys->p_display,
698                                      p_pic->p_sys->xvmc_surf,
699                                      p_vout->p_sys->old_subpic,
700                                      0, 0,
701                                      p_vout->p_sys->xvmc_width,
702                                      p_vout->p_sys->xvmc_height,
703                                      0, 0,
704                                      p_vout->p_sys->xvmc_width,
705                                      p_vout->p_sys->xvmc_height );
706             }
707             else
708             {
709                 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
710                                       p_pic->p_sys->xvmc_surf,
711                                       p_pic->p_sys->xvmc_surf,
712                                       p_vout->p_sys->old_subpic,
713                                       0, 0,
714                                       p_vout->p_sys->xvmc_width,
715                                       p_vout->p_sys->xvmc_height,
716                                       0, 0,
717                                       p_vout->p_sys->xvmc_width,
718                                       p_vout->p_sys->xvmc_height );
719             }
720             XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
721         }
722     }
723     p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
724
725     vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
726 #endif
727     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
728 }
729 #endif
730
731 #ifdef HAVE_XSP
732 /*****************************************************************************
733  * EnablePixelDoubling: Enables pixel doubling
734  *****************************************************************************
735  * Checks if the double size image fits in current window, and enables pixel
736  * doubling accordingly. The i_hw_scale is the integer scaling factor.
737  *****************************************************************************/
738 static void EnablePixelDoubling( vout_thread_t *p_vout )
739 {
740     int i_hor_scale = ( p_vout->p_sys->window.i_width ) / p_vout->render.i_width;
741     int i_vert_scale =  ( p_vout->p_sys->window.i_height ) / p_vout->render.i_height;
742     if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
743         p_vout->p_sys->i_hw_scale = 2;
744         msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
745         XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
746     }
747 }
748
749 /*****************************************************************************
750  * DisablePixelDoubling: Disables pixel doubling
751  *****************************************************************************
752  * The scaling factor i_hw_scale is reset to the no-scaling value 1.
753  *****************************************************************************/
754 static void DisablePixelDoubling( vout_thread_t *p_vout )
755 {
756     if ( p_vout->p_sys->i_hw_scale > 1 ) {
757         msg_Dbg( p_vout, "Disabling pixel doubling" );
758         XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
759         p_vout->p_sys->i_hw_scale = 1;
760     }
761 }
762 #endif
763
764
765 #if !defined(MODULE_NAME_IS_glx)
766 /*****************************************************************************
767  * InitVideo: initialize X11 video thread output method
768  *****************************************************************************
769  * This function create the XImages needed by the output thread. It is called
770  * at the beginning of the thread, but also each time the window is resized.
771  *****************************************************************************/
772 static int InitVideo( vout_thread_t *p_vout )
773 {
774     unsigned int i_index = 0;
775     picture_t *p_pic;
776
777     I_OUTPUTPICTURES = 0;
778
779 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
780     /* Initialize the output structure; we already found an XVideo port,
781      * and the corresponding chroma we will be using. Since we can
782      * arbitrary scale, stick to the coordinates and aspect. */
783     p_vout->output.i_width  = p_vout->render.i_width;
784     p_vout->output.i_height = p_vout->render.i_height;
785     p_vout->output.i_aspect = p_vout->render.i_aspect;
786
787     p_vout->fmt_out = p_vout->fmt_in;
788     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
789
790 #if XvVersion < 2 || ( XvVersion == 2 && XvRevision < 2 )
791     switch( p_vout->output.i_chroma )
792     {
793         case VLC_CODEC_RGB16:
794 #if defined( WORDS_BIGENDIAN )
795             p_vout->output.i_rmask = 0xf800;
796             p_vout->output.i_gmask = 0x07e0;
797             p_vout->output.i_bmask = 0x001f;
798 #else
799             p_vout->output.i_rmask = 0x001f;
800             p_vout->output.i_gmask = 0x07e0;
801             p_vout->output.i_bmask = 0xf800;
802 #endif
803             break;
804         case VLC_CODEC_RGB15:
805 #if defined( WORDS_BIGENDIAN )
806             p_vout->output.i_rmask = 0x7c00;
807             p_vout->output.i_gmask = 0x03e0;
808             p_vout->output.i_bmask = 0x001f;
809 #else
810             p_vout->output.i_rmask = 0x001f;
811             p_vout->output.i_gmask = 0x03e0;
812             p_vout->output.i_bmask = 0x7c00;
813 #endif
814             break;
815     }
816 #endif
817
818 #elif defined(MODULE_NAME_IS_x11)
819     /* Initialize the output structure: RGB with square pixels, whatever
820      * the input format is, since it's the only format we know */
821     switch( p_vout->p_sys->i_screen_depth )
822     {
823         case 8: /* FIXME: set the palette */
824             p_vout->output.i_chroma = VLC_CODEC_RGB8; break;
825         case 15:
826             p_vout->output.i_chroma = VLC_CODEC_RGB15; break;
827         case 16:
828             p_vout->output.i_chroma = VLC_CODEC_RGB16; break;
829         case 24:
830         case 32:
831             p_vout->output.i_chroma = VLC_CODEC_RGB32; break;
832         default:
833             msg_Err( p_vout, "unknown screen depth %i",
834                      p_vout->p_sys->i_screen_depth );
835             return VLC_SUCCESS;
836     }
837
838 #ifdef HAVE_XSP
839     vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width  / p_vout->p_sys->i_hw_scale,
840                        p_vout->p_sys->window.i_height  / p_vout->p_sys->i_hw_scale,
841                        &i_index, &i_index,
842                        &p_vout->fmt_out.i_visible_width,
843                        &p_vout->fmt_out.i_visible_height );
844 #else
845     vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
846                        p_vout->p_sys->window.i_height,
847                        &i_index, &i_index,
848                        &p_vout->fmt_out.i_visible_width,
849                        &p_vout->fmt_out.i_visible_height );
850 #endif
851
852     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
853
854     p_vout->output.i_width = p_vout->fmt_out.i_width =
855         p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
856         p_vout->fmt_in.i_visible_width;
857     p_vout->output.i_height = p_vout->fmt_out.i_height =
858         p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
859         p_vout->fmt_in.i_visible_height;
860     p_vout->fmt_out.i_x_offset =
861         p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
862         p_vout->fmt_in.i_visible_width;
863     p_vout->fmt_out.i_y_offset =
864         p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
865         p_vout->fmt_in.i_visible_height;
866
867     p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
868     p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
869         p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
870
871     msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
872              p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
873              p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
874              p_vout->fmt_out.i_visible_width,
875              p_vout->fmt_out.i_visible_height );
876 #endif
877
878     /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
879     while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
880     {
881         p_pic = NULL;
882
883         /* Find an empty picture slot */
884         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
885         {
886           if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
887             {
888                 p_pic = p_vout->p_picture + i_index;
889                 break;
890             }
891         }
892
893         /* Allocate the picture */
894         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
895         {
896             break;
897         }
898
899         p_pic->i_status = DESTROYED_PICTURE;
900         p_pic->i_type   = DIRECT_PICTURE;
901
902         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
903
904         I_OUTPUTPICTURES++;
905     }
906
907     if( p_vout->output.i_chroma == VLC_CODEC_YV12 )
908     {
909         /* U and V inverted compared to I420
910          * Fixme: this should be handled by the vout core */
911         p_vout->output.i_chroma = VLC_CODEC_I420;
912         p_vout->fmt_out.i_chroma = VLC_CODEC_I420;
913     }
914
915     return VLC_SUCCESS;
916 }
917
918 /*****************************************************************************
919  * DisplayVideo: displays previously rendered output
920  *****************************************************************************
921  * This function sends the currently rendered image to X11 server.
922  * (The Xv extension takes care of "double-buffering".)
923  *****************************************************************************/
924 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
925 {
926     unsigned int i_width, i_height, i_x, i_y;
927
928     vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
929                        p_vout->p_sys->window.i_height,
930                        &i_x, &i_y, &i_width, &i_height );
931
932 #ifdef MODULE_NAME_IS_xvmc
933     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
934
935     vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
936     if( !xxmc->decoded ||
937         !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
938     {
939       msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
940                xxmc->decoded,
941                xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
942       xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
943       return;
944     }
945
946     int src_width = p_vout->output.i_width;
947     int src_height = p_vout->output.i_height;
948     int src_x, src_y;
949
950     if( p_vout->p_sys->xvmc_crop_style == 1 )
951     {
952         src_x = 20;
953         src_y = 20;
954         src_width -= 40;
955         src_height -= 40;
956     }
957     else if( p_vout->p_sys->xvmc_crop_style == 2 )
958     {
959         src_x = 20;
960         src_y = 40;
961         src_width -= 40;
962         src_height -= 80;
963     }
964     else if( p_vout->p_sys->xvmc_crop_style == 3 )
965     {
966         src_x = 40;
967         src_y = 20;
968         src_width -= 80;
969         src_height -= 40;
970     }
971     else
972     {
973         src_x = 0;
974         src_y = 0;
975     }
976
977     int first_field;
978     if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
979     {   /* BOB DEINTERLACE */
980         if( (p_pic->p_sys->nb_display == 0) ||
981             (p_vout->p_sys->xvmc_deinterlace_method == 1) )
982         {
983             first_field = (p_pic->b_top_field_first) ?
984                                 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
985         }
986         else
987         {
988             first_field = (p_pic->b_top_field_first) ?
989                                 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
990         }
991     }
992     else
993     {
994         first_field = XVMC_FRAME_PICTURE;
995      }
996
997     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
998     XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
999     /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1000     XvMCPutSurface( p_vout->p_sys->p_display,
1001                     p_pic->p_sys->xvmc_surf,
1002                     p_vout->p_sys->window.video_window,
1003                     src_x,
1004                     src_y,
1005                     src_width,
1006                     src_height,
1007                     0 /*dest_x*/,
1008                     0 /*dest_y*/,
1009                     i_width,
1010                     i_height,
1011                     first_field);
1012
1013     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1014     if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1015     {   /* BOB DEINTERLACE */
1016         if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1017         {
1018             mtime_t last_date = p_pic->date;
1019
1020             vlc_mutex_lock( &p_vout->picture_lock );
1021             if( !p_vout->p_sys->last_date )
1022             {
1023                 p_pic->date += 20000;
1024             }
1025             else
1026             {
1027                 p_pic->date = ((3 * p_pic->date -
1028                                     p_vout->p_sys->last_date) / 2 );
1029             }
1030             p_vout->p_sys->last_date = last_date;
1031             p_pic->b_force = 1;
1032             p_pic->p_sys->nb_display = 1;
1033             vlc_mutex_unlock( &p_vout->picture_lock );
1034         }
1035         else
1036         {
1037             p_pic->p_sys->nb_display = 0;
1038             p_pic->b_force = 0;
1039         }
1040     }
1041     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1042 #endif
1043
1044 #ifdef HAVE_SYS_SHM_H
1045     if( p_vout->p_sys->i_shm_opcode )
1046     {
1047         /* Display rendered image using shared memory extension */
1048 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1049         XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1050                        p_vout->p_sys->window.video_window,
1051                        p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1052                        p_vout->fmt_out.i_x_offset,
1053                        p_vout->fmt_out.i_y_offset,
1054                        p_vout->fmt_out.i_visible_width,
1055                        p_vout->fmt_out.i_visible_height,
1056                        0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1057                        False /* Don't put True here or you'll waste your CPU */ );
1058 #else
1059         XShmPutImage( p_vout->p_sys->p_display,
1060                       p_vout->p_sys->window.video_window,
1061                       p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1062                       p_vout->fmt_out.i_x_offset,
1063                       p_vout->fmt_out.i_y_offset,
1064                       0 /*dest_x*/, 0 /*dest_y*/,
1065                       p_vout->fmt_out.i_visible_width,
1066                       p_vout->fmt_out.i_visible_height,
1067                       False /* Don't put True here ! */ );
1068 #endif
1069     }
1070     else
1071 #endif /* HAVE_SYS_SHM_H */
1072     {
1073         /* Use standard XPutImage -- this is gonna be slow ! */
1074 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1075         XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1076                     p_vout->p_sys->window.video_window,
1077                     p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1078                     p_vout->fmt_out.i_x_offset,
1079                     p_vout->fmt_out.i_y_offset,
1080                     p_vout->fmt_out.i_visible_width,
1081                     p_vout->fmt_out.i_visible_height,
1082                     0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1083 #else
1084         XPutImage( p_vout->p_sys->p_display,
1085                    p_vout->p_sys->window.video_window,
1086                    p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1087                    p_vout->fmt_out.i_x_offset,
1088                    p_vout->fmt_out.i_y_offset,
1089                    0 /*dest_x*/, 0 /*dest_y*/,
1090                    p_vout->fmt_out.i_visible_width,
1091                    p_vout->fmt_out.i_visible_height );
1092 #endif
1093     }
1094
1095     /* Make sure the command is sent now - do NOT use XFlush !*/
1096     XSync( p_vout->p_sys->p_display, False );
1097 }
1098 #endif
1099
1100 /*****************************************************************************
1101  * ManageVideo: handle X11 events
1102  *****************************************************************************
1103  * This function should be called regularly by video output thread. It manages
1104  * X11 events and allows window resizing. It returns a non null value on
1105  * error.
1106  *****************************************************************************/
1107 static int ManageVideo( vout_thread_t *p_vout )
1108 {
1109     XEvent      xevent;                                         /* X11 event */
1110     vlc_value_t val;
1111
1112 #ifdef MODULE_NAME_IS_xvmc
1113     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1114 #endif
1115
1116     /* Handle events from the owner window */
1117     while( XCheckWindowEvent( p_vout->p_sys->p_display,
1118                               p_vout->p_sys->window.owner_window->handle.xid,
1119                               StructureNotifyMask, &xevent ) == True )
1120     {
1121         /* ConfigureNotify event: prepare  */
1122         if( xevent.type == ConfigureNotify )
1123             /* Update dimensions */
1124             XResizeWindow( p_vout->p_sys->p_display,
1125                            p_vout->p_sys->window.base_window,
1126                            xevent.xconfigure.width,
1127                            xevent.xconfigure.height );
1128     }
1129
1130     /* Handle X11 events: ConfigureNotify events are parsed to know if the
1131      * output window's size changed, MapNotify and UnmapNotify to know if the
1132      * window is mapped (and if the display is useful), and ClientMessages
1133      * to intercept window destruction requests */
1134
1135     while( XCheckWindowEvent( p_vout->p_sys->p_display,
1136                               p_vout->p_sys->window.base_window,
1137                               StructureNotifyMask |
1138                               ButtonPressMask | ButtonReleaseMask |
1139                               PointerMotionMask | Button1MotionMask , &xevent )
1140            == True )
1141     {
1142         /* ConfigureNotify event: prepare  */
1143         if( xevent.type == ConfigureNotify )
1144         {
1145             if( (unsigned int)xevent.xconfigure.width
1146                    != p_vout->p_sys->window.i_width
1147               || (unsigned int)xevent.xconfigure.height
1148                     != p_vout->p_sys->window.i_height )
1149             {
1150                 /* Update dimensions */
1151                 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1152                 p_vout->p_sys->window.i_width = xevent.xconfigure.width;
1153                 p_vout->p_sys->window.i_height = xevent.xconfigure.height;
1154             }
1155         }
1156         /* Mouse click */
1157         else if( xevent.type == ButtonPress )
1158         {
1159             switch( ((XButtonEvent *)&xevent)->button )
1160             {
1161                 case Button1:
1162                     var_Get( p_vout, "mouse-button-down", &val );
1163                     val.i_int |= 1;
1164                     var_Set( p_vout, "mouse-button-down", val );
1165
1166                     var_SetBool( p_vout->p_libvlc, "intf-popupmenu", false );
1167
1168                     /* detect double-clicks */
1169                     if( ( ((XButtonEvent *)&xevent)->time -
1170                           p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1171                     {
1172                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1173                     }
1174
1175                     p_vout->p_sys->i_time_button_last_pressed =
1176                         ((XButtonEvent *)&xevent)->time;
1177                     break;
1178                 case Button2:
1179                     var_Get( p_vout, "mouse-button-down", &val );
1180                     val.i_int |= 2;
1181                     var_Set( p_vout, "mouse-button-down", val );
1182                     break;
1183
1184                 case Button3:
1185                     var_Get( p_vout, "mouse-button-down", &val );
1186                     val.i_int |= 4;
1187                     var_Set( p_vout, "mouse-button-down", val );
1188                     var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
1189                     break;
1190
1191                 case Button4:
1192                     var_Get( p_vout, "mouse-button-down", &val );
1193                     val.i_int |= 8;
1194                     var_Set( p_vout, "mouse-button-down", val );
1195                     break;
1196
1197                 case Button5:
1198                     var_Get( p_vout, "mouse-button-down", &val );
1199                     val.i_int |= 16;
1200                     var_Set( p_vout, "mouse-button-down", val );
1201                     break;
1202             }
1203         }
1204         /* Mouse release */
1205         else if( xevent.type == ButtonRelease )
1206         {
1207             switch( ((XButtonEvent *)&xevent)->button )
1208             {
1209                 case Button1:
1210                     {
1211                         var_Get( p_vout, "mouse-button-down", &val );
1212                         val.i_int &= ~1;
1213                         var_Set( p_vout, "mouse-button-down", val );
1214
1215                         var_SetBool( p_vout, "mouse-clicked", true );
1216                     }
1217                     break;
1218
1219                 case Button2:
1220                     {
1221                         var_Get( p_vout, "mouse-button-down", &val );
1222                         val.i_int &= ~2;
1223                         var_Set( p_vout, "mouse-button-down", val );
1224
1225                         var_Get( p_vout->p_libvlc, "intf-show", &val );
1226                         val.b_bool = !val.b_bool;
1227                         var_Set( p_vout->p_libvlc, "intf-show", val );
1228                     }
1229                     break;
1230
1231                 case Button3:
1232                     {
1233                         var_Get( p_vout, "mouse-button-down", &val );
1234                         val.i_int &= ~4;
1235                         var_Set( p_vout, "mouse-button-down", val );
1236
1237                     }
1238                     break;
1239
1240                 case Button4:
1241                     var_Get( p_vout, "mouse-button-down", &val );
1242                     val.i_int &= ~8;
1243                     var_Set( p_vout, "mouse-button-down", val );
1244                     break;
1245
1246                 case Button5:
1247                     var_Get( p_vout, "mouse-button-down", &val );
1248                     val.i_int &= ~16;
1249                     var_Set( p_vout, "mouse-button-down", val );
1250                     break;
1251
1252             }
1253         }
1254         /* Mouse move */
1255         else if( xevent.type == MotionNotify )
1256         {
1257             unsigned int i_width, i_height, i_x, i_y;
1258
1259             /* somewhat different use for vout_PlacePicture:
1260              * here the values are needed to give to mouse coordinates
1261              * in the original picture space */
1262             vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
1263                                p_vout->p_sys->window.i_height,
1264                                &i_x, &i_y, &i_width, &i_height );
1265
1266             /* Compute the x coordinate and check if the value is
1267                in [0,p_vout->fmt_in.i_visible_width] */
1268             val.i_int = ( xevent.xmotion.x - i_x ) *
1269                 p_vout->fmt_in.i_visible_width / i_width +
1270                 p_vout->fmt_in.i_x_offset;
1271
1272             if( (int)(xevent.xmotion.x - i_x) < 0 )
1273                 val.i_int = 0;
1274             else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1275                 val.i_int = p_vout->fmt_in.i_visible_width;
1276
1277             var_Set( p_vout, "mouse-x", val );
1278
1279             /* compute the y coordinate and check if the value is
1280                in [0,p_vout->fmt_in.i_visible_height] */
1281             val.i_int = ( xevent.xmotion.y - i_y ) *
1282                 p_vout->fmt_in.i_visible_height / i_height +
1283                 p_vout->fmt_in.i_y_offset;
1284
1285             if( (int)(xevent.xmotion.y - i_y) < 0 )
1286                 val.i_int = 0;
1287             else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1288                 val.i_int = p_vout->fmt_in.i_visible_height;
1289
1290             var_Set( p_vout, "mouse-y", val );
1291
1292             var_SetBool( p_vout, "mouse-moved", true );
1293
1294             p_vout->p_sys->i_time_mouse_last_moved = mdate();
1295             if( ! p_vout->p_sys->b_mouse_pointer_visible )
1296             {
1297                 ToggleCursor( p_vout );
1298             }
1299         }
1300         else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1301                   || xevent.type == MapNotify
1302                   || xevent.type == UnmapNotify )
1303         {
1304             /* Ignore these events */
1305         }
1306         else /* Other events */
1307         {
1308             msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1309         }
1310     }
1311
1312     /* Handle events for video output sub-window */
1313     while( XCheckWindowEvent( p_vout->p_sys->p_display,
1314                               p_vout->p_sys->window.video_window,
1315                               ExposureMask, &xevent ) == True )
1316     {
1317         /* Window exposed (only handled if stream playback is paused) */
1318         if( xevent.type == Expose )
1319         {
1320             if( ((XExposeEvent *)&xevent)->count == 0 )
1321             {
1322                 /* (if this is the last a collection of expose events...) */
1323
1324 #if defined(MODULE_NAME_IS_xvideo)
1325                 x11_window_t *p_win = &p_vout->p_sys->window;
1326
1327                 /* Paint the colour key if needed */
1328                 if( p_vout->p_sys->b_paint_colourkey &&
1329                     xevent.xexpose.window == p_win->video_window )
1330                 {
1331                     XSetForeground( p_vout->p_sys->p_display,
1332                                     p_win->gc, p_vout->p_sys->i_colourkey );
1333                     XFillRectangle( p_vout->p_sys->p_display,
1334                                     p_win->video_window, p_win->gc, 0, 0,
1335                                     p_win->i_width, p_win->i_height );
1336                 }
1337 #endif
1338
1339 #if 0
1340                 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1341                 {
1342                     if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1343                                                    ->stream.control.i_status )
1344                     {
1345                         /* XVideoDisplay( p_vout )*/;
1346                     }
1347                 }
1348 #endif
1349             }
1350         }
1351     }
1352
1353     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1354      * are handled - according to the man pages, the format is always 32
1355      * in this case */
1356     while( XCheckTypedEvent( p_vout->p_sys->p_display,
1357                              ClientMessage, &xevent ) )
1358     {
1359         if( (xevent.xclient.message_type == p_vout->p_sys->window.wm_protocols)
1360                && ((Atom)xevent.xclient.data.l[0]
1361                      == p_vout->p_sys->window.wm_delete_window ) )
1362         {
1363             /* the user wants to close the window */
1364             playlist_t * p_playlist = pl_Hold( p_vout );
1365             if( p_playlist != NULL )
1366             {
1367                 playlist_Stop( p_playlist );
1368                 pl_Release( p_vout );
1369             }
1370         }
1371     }
1372
1373     /*
1374      * Fullscreen Change
1375      */
1376     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1377     {
1378         /* Update the object variable and trigger callback */
1379         var_SetBool( p_vout, "fullscreen", !p_vout->b_fullscreen );
1380
1381         ToggleFullScreen( p_vout );
1382         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1383     }
1384
1385     /* autoscale toggle */
1386     if( p_vout->i_changes & VOUT_SCALE_CHANGE )
1387     {
1388         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
1389
1390         p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
1391         p_vout->i_zoom = ZOOM_FP_FACTOR;
1392
1393         p_vout->i_changes |= VOUT_SIZE_CHANGE;
1394     }
1395
1396     /* scaling factor */
1397     if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
1398     {
1399         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
1400
1401         p_vout->b_autoscale = false;
1402         p_vout->i_zoom =
1403             (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
1404
1405         p_vout->i_changes |= VOUT_SIZE_CHANGE;
1406     }
1407
1408     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1409         p_vout->i_changes & VOUT_ASPECT_CHANGE )
1410     {
1411         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1412         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1413
1414         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1415         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1416         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1417         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1418         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1419         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1420         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1421         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1422
1423         p_vout->i_changes |= VOUT_SIZE_CHANGE;
1424     }
1425
1426     /*
1427      * Size change
1428      *
1429      * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1430      *  the size flag inside the fullscreen routine)
1431      */
1432     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1433     {
1434         unsigned int i_width, i_height, i_x, i_y;
1435
1436 #ifdef MODULE_NAME_IS_x11
1437         /* We need to signal the vout thread about the size change because it
1438          * is doing the rescaling */
1439 #else
1440         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1441 #endif
1442
1443         vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
1444                            p_vout->p_sys->window.i_height,
1445                            &i_x, &i_y, &i_width, &i_height );
1446
1447         XMoveResizeWindow( p_vout->p_sys->p_display,
1448                            p_vout->p_sys->window.video_window,
1449                            i_x, i_y, i_width, i_height );
1450     }
1451
1452     /* cursor hiding depending on --vout-event option
1453      *      activated if:
1454      *            value = 1 (Fullsupport) (default value)
1455      *         or value = 2 (Fullscreen-Only) and condition met
1456      */
1457     bool b_vout_event = (   ( p_vout->p_sys->i_vout_event == 1 )
1458                          || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1459                         );
1460
1461     /* Autohide Cursour */
1462     if( mdate() - p_vout->p_sys->i_time_mouse_last_moved >
1463         p_vout->p_sys->i_mouse_hide_timeout )
1464     {
1465         /* Hide the mouse automatically */
1466         if( b_vout_event && p_vout->p_sys->b_mouse_pointer_visible )
1467         {
1468             ToggleCursor( p_vout );
1469         }
1470     }
1471
1472 #ifdef MODULE_NAME_IS_xvmc
1473     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1474 #endif
1475
1476 #ifdef HAVE_OSSO
1477     if ( p_vout->p_sys->p_octx != NULL ) {
1478         if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1479             if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1480                 msg_Err( p_vout, "Could not disable backlight blanking" );
1481         } else {
1482                 msg_Dbg( p_vout, "Backlight blanking disabled" );
1483             }
1484             p_vout->p_sys->i_backlight_on_counter = 0;
1485         } else {
1486             p_vout->p_sys->i_backlight_on_counter ++;
1487         }
1488     }
1489 #endif
1490     return 0;
1491 }
1492
1493 #if !defined( MODULE_NAME_IS_glx )
1494 /*****************************************************************************
1495  * EndVideo: terminate X11 video thread output method
1496  *****************************************************************************
1497  * Destroy the X11 XImages created by Init. It is called at the end of
1498  * the thread, but also each time the window is resized.
1499  *****************************************************************************/
1500 static void EndVideo( vout_thread_t *p_vout )
1501 {
1502     int i_index;
1503
1504     /* Free the direct buffers we allocated */
1505     for( i_index = I_OUTPUTPICTURES ; i_index ; )
1506     {
1507         i_index--;
1508         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1509     }
1510 }
1511 #endif
1512
1513 /* following functions are local */
1514
1515 /*****************************************************************************
1516  * CreateWindow: open and set-up X11 main window
1517  *****************************************************************************/
1518 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1519 {
1520     XSizeHints              xsize_hints;
1521     XSetWindowAttributes    xwindow_attributes;
1522     XGCValues               xgcvalues;
1523     XEvent                  xevent;
1524
1525     bool              b_map_notify = false;
1526
1527     /* Prepare window manager hints and properties */
1528     p_win->wm_protocols =
1529              XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1530     p_win->wm_delete_window =
1531              XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1532
1533     /* Never have a 0-pixel-wide window */
1534     xsize_hints.min_width = 2;
1535     xsize_hints.min_height = 1;
1536
1537     /* Prepare window attributes */
1538     xwindow_attributes.backing_store = Always;       /* save the hidden part */
1539     xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1540                                                      p_vout->p_sys->i_screen);
1541     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1542
1543     {
1544         vout_window_cfg_t wnd_cfg;
1545         memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
1546         wnd_cfg.type   = VOUT_WINDOW_TYPE_XID;
1547         wnd_cfg.x      = p_win->i_x;
1548         wnd_cfg.y      = p_win->i_y;
1549         wnd_cfg.width  = p_win->i_width;
1550         wnd_cfg.height = p_win->i_height;
1551
1552         p_win->owner_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
1553         if( !p_win->owner_window )
1554             return VLC_EGENERIC;
1555         xsize_hints.base_width  = xsize_hints.width = p_win->i_width;
1556         xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1557         xsize_hints.flags       = PSize | PMinSize;
1558
1559         if( p_win->i_x >=0 || p_win->i_y >= 0 )
1560         {
1561             xsize_hints.x = p_win->i_x;
1562             xsize_hints.y = p_win->i_y;
1563             xsize_hints.flags |= PPosition;
1564         }
1565
1566         /* Select events we are interested in. */
1567         XSelectInput( p_vout->p_sys->p_display,
1568                       p_win->owner_window->handle.xid, StructureNotifyMask );
1569
1570         /* Get the parent window's geometry information */
1571         XGetGeometry( p_vout->p_sys->p_display,
1572                       p_win->owner_window->handle.xid,
1573                       &(Window){ 0 }, &(int){ 0 }, &(int){ 0 },
1574                       &p_win->i_width,
1575                       &p_win->i_height,
1576                       &(unsigned){ 0 }, &(unsigned){ 0 } );
1577
1578         /* From man XSelectInput: only one client at a time can select a
1579          * ButtonPress event, so we need to open a new window anyway. */
1580         p_win->base_window =
1581             XCreateWindow( p_vout->p_sys->p_display,
1582                            p_win->owner_window->handle.xid,
1583                            0, 0,
1584                            p_win->i_width, p_win->i_height,
1585                            0,
1586                            0, CopyFromParent, 0,
1587                            CWBackingStore | CWBackPixel | CWEventMask,
1588                            &xwindow_attributes );
1589     }
1590
1591     if( (p_win->wm_protocols == None)        /* use WM_DELETE_WINDOW */
1592         || (p_win->wm_delete_window == None)
1593         || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1594                              &p_win->wm_delete_window, 1 ) )
1595     {
1596         /* WM_DELETE_WINDOW is not supported by window manager */
1597         msg_Warn( p_vout, "missing or bad window manager" );
1598     }
1599
1600     /* Creation of a graphic context that doesn't generate a GraphicsExpose
1601      * event when using functions like XCopyArea */
1602     xgcvalues.graphics_exposures = False;
1603     p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1604                            p_win->base_window,
1605                            GCGraphicsExposures, &xgcvalues );
1606
1607     /* Wait till the window is mapped */
1608     XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1609     do
1610     {
1611         XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1612                       SubstructureNotifyMask | StructureNotifyMask, &xevent);
1613         if( (xevent.type == MapNotify)
1614                  && (xevent.xmap.window == p_win->base_window) )
1615         {
1616             b_map_notify = true;
1617         }
1618         else if( (xevent.type == ConfigureNotify)
1619                  && (xevent.xconfigure.window == p_win->base_window) )
1620         {
1621             p_win->i_width = xevent.xconfigure.width;
1622             p_win->i_height = xevent.xconfigure.height;
1623         }
1624     } while( !b_map_notify );
1625
1626     /* key and mouse events handling depending on --vout-event option
1627      *      activated if:
1628      *            value = 1 (Fullsupport) (default value)
1629      *         or value = 2 (Fullscreen-Only) and condition met
1630      */
1631     bool b_vout_event = (   ( p_vout->p_sys->i_vout_event == 1 )
1632                          || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1633                         );
1634     if ( b_vout_event )
1635         XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1636                       StructureNotifyMask |
1637                       ButtonPressMask | ButtonReleaseMask |
1638                       PointerMotionMask );
1639
1640 #ifdef MODULE_NAME_IS_x11
1641     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1642     {
1643         /* Allocate a new palette */
1644         p_vout->p_sys->colormap =
1645             XCreateColormap( p_vout->p_sys->p_display,
1646                              DefaultRootWindow( p_vout->p_sys->p_display ),
1647                              DefaultVisual( p_vout->p_sys->p_display,
1648                                             p_vout->p_sys->i_screen ),
1649                              AllocAll );
1650
1651         xwindow_attributes.colormap = p_vout->p_sys->colormap;
1652         XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1653                                  CWColormap, &xwindow_attributes );
1654     }
1655 #endif
1656
1657     /* Create video output sub-window. */
1658     p_win->video_window =  XCreateSimpleWindow(
1659                                       p_vout->p_sys->p_display,
1660                                       p_win->base_window, 0, 0,
1661                                       p_win->i_width, p_win->i_height,
1662                                       0,
1663                                       BlackPixel( p_vout->p_sys->p_display,
1664                                                   p_vout->p_sys->i_screen ),
1665                                       WhitePixel( p_vout->p_sys->p_display,
1666                                                   p_vout->p_sys->i_screen ) );
1667
1668     XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1669                           BlackPixel( p_vout->p_sys->p_display,
1670                                       p_vout->p_sys->i_screen ) );
1671
1672     XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1673     XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1674                   ExposureMask );
1675
1676     /* make sure the video window will be centered in the next ManageVideo() */
1677     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1678
1679     /* If the cursor was formerly blank than blank it again */
1680     if( !p_vout->p_sys->b_mouse_pointer_visible )
1681     {
1682         ToggleCursor( p_vout );
1683         ToggleCursor( p_vout );
1684     }
1685
1686     /* Do NOT use XFlush here ! */
1687     XSync( p_vout->p_sys->p_display, False );
1688
1689     return VLC_SUCCESS;
1690 }
1691
1692 /*****************************************************************************
1693  * DestroyWindow: destroy the window
1694  *****************************************************************************
1695  *
1696  *****************************************************************************/
1697 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1698 {
1699     /* Do NOT use XFlush here ! */
1700     XSync( p_vout->p_sys->p_display, False );
1701
1702     if( p_win->video_window != None )
1703         XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1704
1705     XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1706
1707     XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1708     XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1709
1710     /* make sure base window is destroyed before proceeding further */
1711     bool b_destroy_notify = false;
1712     do
1713     {
1714         XEvent      xevent;
1715         XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1716                       SubstructureNotifyMask | StructureNotifyMask, &xevent);
1717         if( (xevent.type == DestroyNotify)
1718                  && (xevent.xmap.window == p_win->base_window) )
1719         {
1720             b_destroy_notify = true;
1721         }
1722     } while( !b_destroy_notify );
1723
1724     vout_window_Delete( p_win->owner_window );
1725 }
1726
1727 /*****************************************************************************
1728  * NewPicture: allocate a picture
1729  *****************************************************************************
1730  * Returns 0 on success, -1 otherwise
1731  *****************************************************************************/
1732 #if !defined(MODULE_NAME_IS_glx)
1733 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1734 {
1735 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1736     int i_plane;
1737 #endif
1738
1739     /* We know the chroma, allocate a buffer which will be used
1740      * directly by the decoder */
1741     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1742
1743     if( p_pic->p_sys == NULL )
1744     {
1745         return -1;
1746     }
1747
1748 #ifdef MODULE_NAME_IS_xvmc
1749     p_pic->p_sys->p_vout = p_vout;
1750     p_pic->p_sys->xvmc_surf = NULL;
1751     p_pic->p_sys->xxmc_data.decoded = 0;
1752     p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1753     //    p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1754     p_pic->p_sys->nb_display = 0;
1755 #endif
1756
1757     /* Fill in picture_t fields */
1758     if( picture_Setup( p_pic, p_vout->output.i_chroma,
1759                        p_vout->output.i_width, p_vout->output.i_height,
1760                        p_vout->output.i_aspect ) )
1761         return -1;
1762
1763 #ifdef HAVE_SYS_SHM_H
1764     if( p_vout->p_sys->i_shm_opcode )
1765     {
1766         /* Create image using XShm extension */
1767         p_pic->p_sys->p_image =
1768             CreateShmImage( p_vout, p_vout->p_sys->p_display,
1769 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1770                             p_vout->p_sys->i_xvport,
1771                             VLC2X11_FOURCC(p_vout->output.i_chroma),
1772 #else
1773                             p_vout->p_sys->p_visual,
1774                             p_vout->p_sys->i_screen_depth,
1775 #endif
1776                             &p_pic->p_sys->shminfo,
1777                             p_vout->output.i_width, p_vout->output.i_height );
1778     }
1779
1780     if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1781 #endif /* HAVE_SYS_SHM_H */
1782     {
1783         /* Create image without XShm extension */
1784         p_pic->p_sys->p_image =
1785             CreateImage( p_vout, p_vout->p_sys->p_display,
1786 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1787                          p_vout->p_sys->i_xvport,
1788                          VLC2X11_FOURCC(p_vout->output.i_chroma),
1789                          p_pic->format.i_bits_per_pixel,
1790 #else
1791                          p_vout->p_sys->p_visual,
1792                          p_vout->p_sys->i_screen_depth,
1793                          p_vout->p_sys->i_bytes_per_pixel,
1794 #endif
1795                          p_vout->output.i_width, p_vout->output.i_height );
1796
1797 #ifdef HAVE_SYS_SHM_H
1798         if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1799         {
1800             msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1801             p_vout->p_sys->i_shm_opcode = 0;
1802         }
1803 #endif /* HAVE_SYS_SHM_H */
1804     }
1805
1806     if( p_pic->p_sys->p_image == NULL )
1807     {
1808         free( p_pic->p_sys );
1809         return -1;
1810     }
1811
1812     switch( p_vout->output.i_chroma )
1813     {
1814 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1815         case VLC_CODEC_I420:
1816         case VLC_CODEC_YV12:
1817         case VLC_CODEC_Y211:
1818         case VLC_CODEC_YUYV:
1819         case VLC_CODEC_UYVY:
1820         case VLC_CODEC_RGB15:
1821         case VLC_CODEC_RGB16:
1822         case VLC_CODEC_RGB24: /* Fixme: pixel pitch == 4 ? */
1823         case VLC_CODEC_RGB32:
1824
1825             for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1826                  i_plane++ )
1827             {
1828                 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1829                     + p_pic->p_sys->p_image->offsets[i_plane];
1830                 p_pic->p[i_plane].i_pitch =
1831                     p_pic->p_sys->p_image->pitches[i_plane];
1832             }
1833             if( p_vout->output.i_chroma == VLC_CODEC_YV12 )
1834             {
1835                 /* U and V inverted compared to I420
1836                  * Fixme: this should be handled by the vout core */
1837                 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1838                     + p_pic->p_sys->p_image->offsets[2];
1839                 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1840                     + p_pic->p_sys->p_image->offsets[1];
1841             }
1842
1843             break;
1844
1845 #else
1846         case VLC_CODEC_RGB8:
1847         case VLC_CODEC_RGB16:
1848         case VLC_CODEC_RGB15:
1849         case VLC_CODEC_RGB24:
1850         case VLC_CODEC_RGB32:
1851
1852             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1853             p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1854             p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1855                                   + p_pic->p_sys->p_image->xoffset;
1856             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1857
1858             /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1859              * properly by picture_Setup() */
1860             p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1861                                          * p_pic->p_sys->p_image->width;
1862             break;
1863 #endif
1864
1865         default:
1866             /* Unknown chroma, tell the guy to get lost */
1867             IMAGE_FREE( p_pic->p_sys->p_image );
1868             free( p_pic->p_sys );
1869             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1870                      p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1871             p_pic->i_planes = 0;
1872             return -1;
1873     }
1874     return 0;
1875 }
1876
1877 /*****************************************************************************
1878  * FreePicture: destroy a picture allocated with NewPicture
1879  *****************************************************************************
1880  * Destroy XImage AND associated data. If using Shm, detach shared memory
1881  * segment from server and process, then free it. The XDestroyImage manpage
1882  * says that both the image structure _and_ the data pointed to by the
1883  * image structure are freed, so no need to free p_image->data.
1884  *****************************************************************************/
1885 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1886 {
1887     /* The order of operations is correct */
1888 #ifdef HAVE_SYS_SHM_H
1889     if( p_vout->p_sys->i_shm_opcode )
1890     {
1891         XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1892         IMAGE_FREE( p_pic->p_sys->p_image );
1893
1894         shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1895         if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1896         {
1897             msg_Err( p_vout, "cannot detach shared memory (%m)" );
1898         }
1899     }
1900     else
1901 #endif
1902     {
1903         IMAGE_FREE( p_pic->p_sys->p_image );
1904     }
1905
1906 #ifdef MODULE_NAME_IS_xvmc
1907     if( p_pic->p_sys->xvmc_surf != NULL )
1908     {
1909         xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
1910         p_pic->p_sys->xvmc_surf = NULL;
1911     }
1912 #endif
1913
1914     /* Do NOT use XFlush here ! */
1915     XSync( p_vout->p_sys->p_display, False );
1916
1917     free( p_pic->p_sys );
1918 }
1919 #endif /* !MODULE_NAME_IS_glx */
1920
1921 /*****************************************************************************
1922  * ToggleFullScreen: Enable or disable full screen mode
1923  *****************************************************************************
1924  * This function will switch between fullscreen and window mode.
1925  *****************************************************************************/
1926 static void ToggleFullScreen ( vout_thread_t *p_vout )
1927 {
1928     p_vout->b_fullscreen = !p_vout->b_fullscreen;
1929     vout_window_SetFullScreen( p_vout->p_sys->window.owner_window,
1930                                p_vout->b_fullscreen );
1931
1932 #ifdef HAVE_XSP
1933     if( p_vout->b_fullscreen )
1934         EnablePixelDoubling( p_vout );
1935     else
1936         DisablePixelDoubling( p_vout );
1937 #endif
1938 }
1939
1940 /*****************************************************************************
1941  * EnableXScreenSaver: enable screen saver
1942  *****************************************************************************
1943  * This function enables the screen saver on a display after it has been
1944  * disabled by XDisableScreenSaver.
1945  * FIXME: what happens if multiple vlc sessions are running at the same
1946  *        time ???
1947  *****************************************************************************/
1948 static void EnableXScreenSaver( vout_thread_t *p_vout )
1949 {
1950 #ifdef DPMSINFO_IN_DPMS_H
1951     int dummy;
1952 #endif
1953
1954     if( p_vout->p_sys->i_ss_timeout )
1955     {
1956         XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1957                          p_vout->p_sys->i_ss_interval,
1958                          p_vout->p_sys->i_ss_blanking,
1959                          p_vout->p_sys->i_ss_exposure );
1960     }
1961
1962     /* Restore DPMS settings */
1963 #ifdef DPMSINFO_IN_DPMS_H
1964     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1965     {
1966         if( p_vout->p_sys->b_ss_dpms )
1967         {
1968             DPMSEnable( p_vout->p_sys->p_display );
1969         }
1970     }
1971 #endif
1972 }
1973
1974 /*****************************************************************************
1975  * DisableXScreenSaver: disable screen saver
1976  *****************************************************************************
1977  * See XEnableXScreenSaver
1978  *****************************************************************************/
1979 static void DisableXScreenSaver( vout_thread_t *p_vout )
1980 {
1981 #ifdef DPMSINFO_IN_DPMS_H
1982     int dummy;
1983 #endif
1984
1985     /* Save screen saver information */
1986     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1987                      &p_vout->p_sys->i_ss_interval,
1988                      &p_vout->p_sys->i_ss_blanking,
1989                      &p_vout->p_sys->i_ss_exposure );
1990
1991     /* Disable screen saver */
1992     if( p_vout->p_sys->i_ss_timeout )
1993     {
1994         XSetScreenSaver( p_vout->p_sys->p_display, 0,
1995                          p_vout->p_sys->i_ss_interval,
1996                          p_vout->p_sys->i_ss_blanking,
1997                          p_vout->p_sys->i_ss_exposure );
1998     }
1999
2000     /* Disable DPMS */
2001 #ifdef DPMSINFO_IN_DPMS_H
2002     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2003     {
2004         CARD16 unused;
2005         /* Save DPMS current state */
2006         DPMSInfo( p_vout->p_sys->p_display, &unused,
2007                   &p_vout->p_sys->b_ss_dpms );
2008         DPMSDisable( p_vout->p_sys->p_display );
2009    }
2010 #endif
2011 }
2012
2013 /*****************************************************************************
2014  * CreateCursor: create a blank mouse pointer
2015  *****************************************************************************/
2016 static void CreateCursor( vout_thread_t *p_vout )
2017 {
2018     XColor cursor_color;
2019
2020     p_vout->p_sys->cursor_pixmap =
2021         XCreatePixmap( p_vout->p_sys->p_display,
2022                        DefaultRootWindow( p_vout->p_sys->p_display ),
2023                        1, 1, 1 );
2024
2025     XParseColor( p_vout->p_sys->p_display,
2026                  XCreateColormap( p_vout->p_sys->p_display,
2027                                   DefaultRootWindow(
2028                                                     p_vout->p_sys->p_display ),
2029                                   DefaultVisual(
2030                                                 p_vout->p_sys->p_display,
2031                                                 p_vout->p_sys->i_screen ),
2032                                   AllocNone ),
2033                  "black", &cursor_color );
2034
2035     p_vout->p_sys->blank_cursor =
2036         XCreatePixmapCursor( p_vout->p_sys->p_display,
2037                              p_vout->p_sys->cursor_pixmap,
2038                              p_vout->p_sys->cursor_pixmap,
2039                              &cursor_color, &cursor_color, 1, 1 );
2040 }
2041
2042 /*****************************************************************************
2043  * DestroyCursor: destroy the blank mouse pointer
2044  *****************************************************************************/
2045 static void DestroyCursor( vout_thread_t *p_vout )
2046 {
2047     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2048 }
2049
2050 /*****************************************************************************
2051  * ToggleCursor: hide or show the mouse pointer
2052  *****************************************************************************
2053  * This function hides the X pointer if it is visible by setting the pointer
2054  * sprite to a blank one. To show it again, we disable the sprite.
2055  *****************************************************************************/
2056 static void ToggleCursor( vout_thread_t *p_vout )
2057 {
2058     if( p_vout->p_sys->b_mouse_pointer_visible )
2059     {
2060         XDefineCursor( p_vout->p_sys->p_display,
2061                        p_vout->p_sys->window.base_window,
2062                        p_vout->p_sys->blank_cursor );
2063         p_vout->p_sys->b_mouse_pointer_visible = 0;
2064     }
2065     else
2066     {
2067         XUndefineCursor( p_vout->p_sys->p_display,
2068                          p_vout->p_sys->window.base_window );
2069         p_vout->p_sys->b_mouse_pointer_visible = 1;
2070     }
2071 }
2072
2073 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2074 /*****************************************************************************
2075  * XVideoGetPort: get YUV12 port
2076  *****************************************************************************/
2077 static int XVideoGetPort( vout_thread_t *p_vout,
2078                           vlc_fourcc_t i_chroma, picture_heap_t *p_heap )
2079 {
2080     XvAdaptorInfo *p_adaptor;
2081     unsigned int i;
2082     unsigned int i_adaptor, i_num_adaptors;
2083     int i_requested_adaptor;
2084     int i_selected_port;
2085
2086     switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2087     {
2088         case Success:
2089             break;
2090
2091         case XvBadExtension:
2092             msg_Warn( p_vout, "XvBadExtension" );
2093             return -1;
2094
2095         case XvBadAlloc:
2096             msg_Warn( p_vout, "XvBadAlloc" );
2097             return -1;
2098
2099         default:
2100             msg_Warn( p_vout, "XvQueryExtension failed" );
2101             return -1;
2102     }
2103
2104     switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2105                              DefaultRootWindow( p_vout->p_sys->p_display ),
2106                              &i_num_adaptors, &p_adaptor ) )
2107     {
2108         case Success:
2109             break;
2110
2111         case XvBadExtension:
2112             msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2113             return -1;
2114
2115         case XvBadAlloc:
2116             msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2117             return -1;
2118
2119         default:
2120             msg_Warn( p_vout, "XvQueryAdaptors failed" );
2121             return -1;
2122     }
2123
2124     i_selected_port = -1;
2125 #ifdef MODULE_NAME_IS_xvmc
2126     i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2127 #else
2128     i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2129 #endif
2130     for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2131     {
2132         XvImageFormatValues *p_formats;
2133         int i_format, i_num_formats;
2134         int i_port;
2135
2136         /* If we requested an adaptor and it's not this one, we aren't
2137          * interested */
2138         if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2139         {
2140             continue;
2141         }
2142
2143         /* If the adaptor doesn't have the required properties, skip it */
2144         if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2145             !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2146         {
2147             continue;
2148         }
2149
2150         /* Check that adaptor supports our requested format... */
2151         p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2152                                         p_adaptor[i_adaptor].base_id,
2153                                         &i_num_formats );
2154
2155         for( i_format = 0;
2156              i_format < i_num_formats && ( i_selected_port == -1 );
2157              i_format++ )
2158         {
2159             XvAttribute     *p_attr;
2160             int             i_attr, i_num_attributes;
2161             Atom            autopaint = None, colorkey = None;
2162
2163             /* If this is not the format we want, or at least a
2164              * similar one, forget it */
2165             if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2166             {
2167                 continue;
2168             }
2169
2170             /* Look for the first available port supporting this format */
2171             for( i_port = p_adaptor[i_adaptor].base_id;
2172                  ( i_port < (int)(p_adaptor[i_adaptor].base_id
2173                                    + p_adaptor[i_adaptor].num_ports) )
2174                    && ( i_selected_port == -1 );
2175                  i_port++ )
2176             {
2177                 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2178                      == Success )
2179                 {
2180                     i_selected_port = i_port;
2181                     p_heap->i_chroma = p_formats[ i_format ].id;
2182 #if XvVersion > 2 || ( XvVersion == 2 && XvRevision >= 2 )
2183                     p_heap->i_rmask = p_formats[ i_format ].red_mask;
2184                     p_heap->i_gmask = p_formats[ i_format ].green_mask;
2185                     p_heap->i_bmask = p_formats[ i_format ].blue_mask;
2186 #endif
2187                 }
2188             }
2189
2190             /* If no free port was found, forget it */
2191             if( i_selected_port == -1 )
2192             {
2193                 continue;
2194             }
2195
2196             /* If we found a port, print information about it */
2197             msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2198                      i_adaptor, i_selected_port, p_formats[ i_format ].id,
2199                      (char *)&p_formats[ i_format ].id,
2200                      ( p_formats[ i_format ].format == XvPacked ) ?
2201                          "packed" : "planar" );
2202
2203             /* Use XV_AUTOPAINT_COLORKEY if supported, otherwise we will
2204              * manually paint the colour key */
2205             p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2206                                             i_selected_port,
2207                                             &i_num_attributes );
2208
2209             for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2210             {
2211                 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2212                 {
2213                     autopaint = XInternAtom( p_vout->p_sys->p_display,
2214                                              "XV_AUTOPAINT_COLORKEY", False );
2215                     XvSetPortAttribute( p_vout->p_sys->p_display,
2216                                         i_selected_port, autopaint, 1 );
2217                 }
2218                 if( !strcmp( p_attr[i_attr].name, "XV_COLORKEY" ) )
2219                 {
2220                     /* Find out the default colour key */
2221                     colorkey = XInternAtom( p_vout->p_sys->p_display,
2222                                             "XV_COLORKEY", False );
2223                     XvGetPortAttribute( p_vout->p_sys->p_display,
2224                                         i_selected_port, colorkey,
2225                                         &p_vout->p_sys->i_colourkey );
2226                 }
2227             }
2228             p_vout->p_sys->b_paint_colourkey =
2229                 autopaint == None && colorkey != None;
2230
2231             if( p_attr != NULL )
2232             {
2233                 XFree( p_attr );
2234             }
2235         }
2236
2237         if( p_formats != NULL )
2238         {
2239             XFree( p_formats );
2240         }
2241
2242     }
2243
2244     if( i_num_adaptors > 0 )
2245     {
2246         XvFreeAdaptorInfo( p_adaptor );
2247     }
2248
2249     if( i_selected_port == -1 )
2250     {
2251         int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2252         if( i_requested_adaptor == -1 )
2253         {
2254             msg_Warn( p_vout, "no free XVideo port found for format "
2255                       "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2256         }
2257         else
2258         {
2259             msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2260                       "XVideo port for format 0x%.8x (%4.4s)",
2261                       i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2262         }
2263     }
2264
2265     return i_selected_port;
2266 }
2267
2268 /*****************************************************************************
2269  * XVideoReleasePort: release YUV12 port
2270  *****************************************************************************/
2271 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2272 {
2273     XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2274 }
2275 #endif
2276
2277 /*****************************************************************************
2278  * InitDisplay: open and initialize X11 device
2279  *****************************************************************************
2280  * Create a window according to video output given size, and set other
2281  * properties according to the display properties.
2282  *****************************************************************************/
2283 static int InitDisplay( vout_thread_t *p_vout )
2284 {
2285 #ifdef MODULE_NAME_IS_x11
2286     XPixmapFormatValues *       p_formats;                 /* pixmap formats */
2287     XVisualInfo *               p_xvisual;            /* visuals information */
2288     XVisualInfo                 xvisual_template;         /* visual template */
2289     int                         i_count, i;                    /* array size */
2290 #endif
2291
2292 #ifdef HAVE_SYS_SHM_H
2293     p_vout->p_sys->i_shm_opcode = 0;
2294
2295     if( config_GetInt( p_vout, MODULE_STRING "-shm" ) > 0 )
2296     {
2297         int major, evt, err;
2298
2299         if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2300                              &evt, &err )
2301          && XShmQueryExtension( p_vout->p_sys->p_display ) )
2302             p_vout->p_sys->i_shm_opcode = major;
2303
2304         if( p_vout->p_sys->i_shm_opcode )
2305         {
2306             int minor;
2307             Bool pixmaps;
2308
2309             XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2310                               &pixmaps );
2311             msg_Dbg( p_vout, "XShm video extension v%d.%d "
2312                      "(with%s pixmaps, opcode: %d)",
2313                      major, minor, pixmaps ? "" : "out",
2314                      p_vout->p_sys->i_shm_opcode );
2315         }
2316         else
2317             msg_Warn( p_vout, "XShm video extension not available" );
2318     }
2319     else
2320         msg_Dbg( p_vout, "XShm video extension disabled" );
2321 #endif
2322
2323 #ifdef MODULE_NAME_IS_xvideo
2324     /* XXX The brightness and contrast values should be read from environment
2325      * XXX variables... */
2326 #if 0
2327     XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2328     XVideoSetAttribute( p_vout, "XV_CONTRAST",   0.5 );
2329 #endif
2330 #endif
2331
2332 #ifdef MODULE_NAME_IS_x11
2333     /* Initialize structure */
2334     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2335
2336     /* Get screen depth */
2337     p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2338                                                    p_vout->p_sys->i_screen );
2339     switch( p_vout->p_sys->i_screen_depth )
2340     {
2341     case 8:
2342         /*
2343          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2344          */
2345         xvisual_template.screen =   p_vout->p_sys->i_screen;
2346         xvisual_template.class =    DirectColor;
2347         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2348                                     VisualScreenMask | VisualClassMask,
2349                                     &xvisual_template, &i_count );
2350         if( p_xvisual == NULL )
2351         {
2352             msg_Err( p_vout, "no PseudoColor visual available" );
2353             return VLC_EGENERIC;
2354         }
2355         p_vout->p_sys->i_bytes_per_pixel = 1;
2356         p_vout->output.pf_setpalette = SetPalette;
2357         break;
2358     case 15:
2359     case 16:
2360     case 24:
2361     default:
2362         /*
2363          * Screen depth is higher than 8bpp. TrueColor visual is used.
2364          */
2365         xvisual_template.screen =   p_vout->p_sys->i_screen;
2366         xvisual_template.class =    TrueColor;
2367 /* In some cases, we get a truecolor class adaptor that has a different
2368    color depth. So try to get a real true color one first */
2369         xvisual_template.depth =    p_vout->p_sys->i_screen_depth;
2370
2371         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2372                                     VisualScreenMask | VisualClassMask |
2373                                     VisualDepthMask,
2374                                     &xvisual_template, &i_count );
2375         if( p_xvisual == NULL )
2376         {
2377             msg_Warn( p_vout, "No screen matching the required color depth" );
2378             p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2379                                     VisualScreenMask | VisualClassMask,
2380                                     &xvisual_template, &i_count );
2381             if( p_xvisual == NULL )
2382             {
2383
2384                 msg_Err( p_vout, "no TrueColor visual available" );
2385                 return VLC_EGENERIC;
2386             }
2387         }
2388
2389         p_vout->output.i_rmask = p_xvisual->red_mask;
2390         p_vout->output.i_gmask = p_xvisual->green_mask;
2391         p_vout->output.i_bmask = p_xvisual->blue_mask;
2392
2393         /* There is no difference yet between 3 and 4 Bpp. The only way
2394          * to find the actual number of bytes per pixel is to list supported
2395          * pixmap formats. */
2396         p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2397         p_vout->p_sys->i_bytes_per_pixel = 0;
2398
2399         for( i = 0; i < i_count; i++ )
2400         {
2401             /* Under XFree4.0, the list contains pixmap formats available
2402              * through all video depths ; so we have to check against current
2403              * depth. */
2404             if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2405             {
2406                 if( p_formats[i].bits_per_pixel / 8
2407                         > (int)p_vout->p_sys->i_bytes_per_pixel )
2408                 {
2409                     p_vout->p_sys->i_bytes_per_pixel =
2410                         p_formats[i].bits_per_pixel / 8;
2411                 }
2412             }
2413         }
2414         if( p_formats ) XFree( p_formats );
2415
2416         break;
2417     }
2418     p_vout->p_sys->p_visual = p_xvisual->visual;
2419     XFree( p_xvisual );
2420 #endif
2421
2422     return VLC_SUCCESS;
2423 }
2424
2425 #ifndef MODULE_NAME_IS_glx
2426
2427 #ifdef HAVE_SYS_SHM_H
2428 /*****************************************************************************
2429  * CreateShmImage: create an XImage or XvImage using shared memory extension
2430  *****************************************************************************
2431  * Prepare an XImage or XvImage for display function.
2432  * The order of the operations respects the recommandations of the mit-shm
2433  * document by J.Corbet and K.Packard. Most of the parameters were copied from
2434  * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2435  *****************************************************************************/
2436 IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2437                                     Display* p_display, EXTRA_ARGS_SHM,
2438                                     int i_width, int i_height )
2439 {
2440     IMAGE_TYPE *p_image;
2441     Status result;
2442
2443     /* Create XImage / XvImage */
2444 #ifdef MODULE_NAME_IS_xvideo
2445     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2446                                 i_width, i_height, p_shm );
2447 #elif defined(MODULE_NAME_IS_xvmc)
2448     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2449                                 i_width, i_height, p_shm );
2450 #else
2451     p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2452                                p_shm, i_width, i_height );
2453 #endif
2454     if( p_image == NULL )
2455     {
2456         msg_Err( p_vout, "image creation failed" );
2457         return NULL;
2458     }
2459
2460     /* For too big image, the buffer returned is sometimes too small, prevent
2461      * VLC to segfault because of it
2462      * FIXME is it normal ? Is there a way to detect it
2463      * before (XvQueryBestSize did not) ? */
2464     if( p_image->width < i_width || p_image->height < i_height )
2465     {
2466         msg_Err( p_vout, "cannot allocate shared image data with the right size "
2467                          "(%dx%d instead of %dx%d)",
2468                          p_image->width, p_image->height,
2469                          i_width, i_height );
2470         IMAGE_FREE( p_image );
2471         return NULL;
2472     }
2473
2474     /* Allocate shared memory segment. */
2475     p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0600 );
2476     if( p_shm->shmid < 0 )
2477     {
2478         msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2479         IMAGE_FREE( p_image );
2480         return NULL;
2481     }
2482
2483     /* Attach shared memory segment to process (read/write) */
2484     p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2485     if(! p_shm->shmaddr )
2486     {
2487         msg_Err( p_vout, "cannot attach shared memory (%m)" );
2488         IMAGE_FREE( p_image );
2489         shmctl( p_shm->shmid, IPC_RMID, 0 );
2490         return NULL;
2491     }
2492
2493     /* Read-only data. We won't be using XShmGetImage */
2494     p_shm->readOnly = True;
2495
2496     /* Attach shared memory segment to X server */
2497     XSynchronize( p_display, True );
2498     i_shm_major = p_vout->p_sys->i_shm_opcode;
2499     result = XShmAttach( p_display, p_shm );
2500     if( result == False || !i_shm_major )
2501     {
2502         msg_Err( p_vout, "cannot attach shared memory to X server" );
2503         IMAGE_FREE( p_image );
2504         shmctl( p_shm->shmid, IPC_RMID, 0 );
2505         shmdt( p_shm->shmaddr );
2506         return NULL;
2507     }
2508     XSynchronize( p_display, False );
2509
2510     /* Send image to X server. This instruction is required, since having
2511      * built a Shm XImage and not using it causes an error on XCloseDisplay,
2512      * and remember NOT to use XFlush ! */
2513     XSync( p_display, False );
2514
2515 #if 0
2516     /* Mark the shm segment to be removed when there are no more
2517      * attachements, so it is automatic on process exit or after shmdt */
2518     shmctl( p_shm->shmid, IPC_RMID, 0 );
2519 #endif
2520
2521     return p_image;
2522 }
2523 #endif
2524
2525 /*****************************************************************************
2526  * CreateImage: create an XImage or XvImage
2527  *****************************************************************************
2528  * Create a simple image used as a buffer.
2529  *****************************************************************************/
2530 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2531                                  Display *p_display, EXTRA_ARGS,
2532                                  int i_width, int i_height )
2533 {
2534     uint8_t *    p_data;                          /* image data storage zone */
2535     IMAGE_TYPE *p_image;
2536 #ifdef MODULE_NAME_IS_x11
2537     int         i_quantum;                     /* XImage quantum (see below) */
2538     int         i_bytes_per_line;
2539 #endif
2540
2541     /* Allocate memory for image */
2542 #ifdef MODULE_NAME_IS_xvideo
2543     p_data = malloc( i_width * i_height * i_bits_per_pixel / 8 );
2544 #elif defined(MODULE_NAME_IS_x11)
2545     i_bytes_per_line = i_width * i_bytes_per_pixel;
2546     p_data = malloc( i_bytes_per_line * i_height );
2547 #endif
2548     if( !p_data )
2549         return NULL;
2550
2551 #ifdef MODULE_NAME_IS_x11
2552     /* Optimize the quantum of a scanline regarding its size - the quantum is
2553        a diviser of the number of bits between the start of two scanlines. */
2554     if( i_bytes_per_line & 0xf )
2555     {
2556         i_quantum = 0x8;
2557     }
2558     else if( i_bytes_per_line & 0x10 )
2559     {
2560         i_quantum = 0x10;
2561     }
2562     else
2563     {
2564         i_quantum = 0x20;
2565     }
2566 #endif
2567
2568     /* Create XImage. p_data will be automatically freed */
2569 #ifdef MODULE_NAME_IS_xvideo
2570     p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2571                              (char *)p_data, i_width, i_height );
2572 #elif defined(MODULE_NAME_IS_x11)
2573     p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2574                             (char *)p_data, i_width, i_height, i_quantum, 0 );
2575 #endif
2576     if( p_image == NULL )
2577     {
2578         msg_Err( p_vout, "XCreateImage() failed" );
2579         free( p_data );
2580         return NULL;
2581     }
2582
2583     return p_image;
2584 }
2585
2586 #endif
2587 /*****************************************************************************
2588  * X11ErrorHandler: replace error handler so we can intercept some of them
2589  *****************************************************************************/
2590 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2591 {
2592     char txt[1024];
2593
2594     XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
2595     fprintf( stderr,
2596              "[????????] x11 video output error: X11 request %u.%u failed "
2597               "with error code %u:\n %s\n",
2598              event->request_code, event->minor_code, event->error_code, txt );
2599
2600     switch( event->request_code )
2601     {
2602     case X_SetInputFocus:
2603         /* Ignore errors on XSetInputFocus()
2604          * (they happen when a window is not yet mapped) */
2605         return 0;
2606     }
2607
2608 #ifdef HAVE_SYS_SHM_H
2609     if( event->request_code == i_shm_major ) /* MIT-SHM */
2610     {
2611         fprintf( stderr,
2612                  "[????????] x11 video output notice:"
2613                  " buggy X11 server claims shared memory\n"
2614                  "[????????] x11 video output notice:"
2615                  " support though it does not work (OpenSSH?)\n" );
2616         return i_shm_major = 0;
2617     }
2618 #endif
2619
2620 #ifndef HAVE_OSSO
2621     XSetErrorHandler(NULL);
2622     return (XSetErrorHandler(X11ErrorHandler))( display, event );
2623 #else
2624     /* Work-around Maemo Xvideo bug */
2625     return 0;
2626 #endif
2627 }
2628
2629 #ifdef MODULE_NAME_IS_x11
2630 /*****************************************************************************
2631  * SetPalette: sets an 8 bpp palette
2632  *****************************************************************************
2633  * This function sets the palette given as an argument. It does not return
2634  * anything, but could later send information on which colors it was unable
2635  * to set.
2636  *****************************************************************************/
2637 static void SetPalette( vout_thread_t *p_vout,
2638                         uint16_t *red, uint16_t *green, uint16_t *blue )
2639 {
2640     int i;
2641     XColor p_colors[255];
2642
2643     /* allocate palette */
2644     for( i = 0; i < 255; i++ )
2645     {
2646         /* kludge: colors are indexed reversely because color 255 seems
2647          * to be reserved for black even if we try to set it to white */
2648         p_colors[ i ].pixel = 255 - i;
2649         p_colors[ i ].pad   = 0;
2650         p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2651         p_colors[ i ].red   = red[ 255 - i ];
2652         p_colors[ i ].blue  = blue[ 255 - i ];
2653         p_colors[ i ].green = green[ 255 - i ];
2654     }
2655
2656     XStoreColors( p_vout->p_sys->p_display,
2657                   p_vout->p_sys->colormap, p_colors, 255 );
2658 }
2659 #endif
2660
2661 /*****************************************************************************
2662  * Control: control facility for the vout
2663  *****************************************************************************/
2664 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2665 {
2666     unsigned int i_width, i_height;
2667
2668     switch( i_query )
2669     {
2670         case VOUT_SET_SIZE:
2671             i_width  = va_arg( args, unsigned int );
2672             i_height = va_arg( args, unsigned int );
2673             if( !i_width ) i_width = p_vout->i_window_width;
2674             if( !i_height ) i_height = p_vout->i_window_height;
2675
2676             return vout_window_SetSize( p_vout->p_sys->window.owner_window,
2677                                         i_width, i_height);
2678
2679         case VOUT_SET_STAY_ON_TOP:
2680             return vout_window_SetOnTop( p_vout->p_sys->window.owner_window,
2681                                          va_arg( args, int ) );
2682
2683        default:
2684             return VLC_EGENERIC;
2685     }
2686 }