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