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