]> git.sesse.net Git - vlc/blob - modules/video_output/x11/xcommon.c
x11: remove unused variables in Control.
[vlc] / modules / video_output / x11 / xcommon.c
1 /*****************************************************************************
2  * xcommon.c: Functions common to the X11 and XVideo plugins
3  *****************************************************************************
4  * Copyright (C) 1998-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Sam Hocevar <sam@zoy.org>
9  *          David Kennedy <dkennedy@tinytoad.com>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
37 #include <vlc_vout.h>
38 #include <vlc_window.h>
39 #include <vlc_keys.h>
40
41 #include <errno.h>                                                 /* ENOMEM */
42
43 #ifdef HAVE_MACHINE_PARAM_H
44     /* BSD */
45 #   include <machine/param.h>
46 #   include <sys/types.h>                                  /* typedef ushort */
47 #   include <sys/ipc.h>
48 #endif
49
50 #ifndef WIN32
51 #   include <netinet/in.h>                            /* BSD: struct in_addr */
52 #endif
53
54 #ifdef HAVE_XSP
55 #include <X11/extensions/Xsp.h>
56 #endif
57
58 #ifdef HAVE_SYS_SHM_H
59 #   include <sys/shm.h>                                /* shmget(), shmctl() */
60 #endif
61
62 #include <X11/Xlib.h>
63 #include <X11/Xproto.h>
64 #include <X11/Xmd.h>
65 #include <X11/Xutil.h>
66 #include <X11/keysym.h>
67 #include <X11/XF86keysym.h>
68 #ifdef HAVE_SYS_SHM_H
69 #   include <X11/extensions/XShm.h>
70 #endif
71 #ifdef DPMSINFO_IN_DPMS_H
72 #   include <X11/extensions/dpms.h>
73 #endif
74
75 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
76 #   include <X11/extensions/Xv.h>
77 #   include <X11/extensions/Xvlib.h>
78 #endif
79
80 #ifdef MODULE_NAME_IS_glx
81 #   include <GL/glx.h>
82 #endif
83
84 #ifdef HAVE_XINERAMA
85 #   include <X11/extensions/Xinerama.h>
86 #endif
87
88 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
89 #   include <X11/extensions/xf86vmode.h>
90 #endif
91
92 #ifdef MODULE_NAME_IS_xvmc
93 #   include <X11/extensions/vldXvMC.h>
94 #   include "../../codec/xvmc/accel_xvmc.h"
95 #endif
96
97 #include "xcommon.h"
98
99 /*****************************************************************************
100  * Local prototypes
101  *****************************************************************************/
102 int  Activate   ( vlc_object_t * );
103 void Deactivate ( vlc_object_t * );
104
105 static int  InitVideo      ( vout_thread_t * );
106 static void EndVideo       ( vout_thread_t * );
107 static void DisplayVideo   ( vout_thread_t *, picture_t * );
108 static int  ManageVideo    ( vout_thread_t * );
109 static int  Control        ( vout_thread_t *, int, va_list );
110
111 static int  InitDisplay    ( vout_thread_t * );
112
113 static int  CreateWindow   ( vout_thread_t *, x11_window_t * );
114 static void DestroyWindow  ( vout_thread_t *, x11_window_t * );
115
116 static int  NewPicture     ( vout_thread_t *, picture_t * );
117 static void FreePicture    ( vout_thread_t *, picture_t * );
118
119 #ifndef MODULE_NAME_IS_glx
120 static IMAGE_TYPE *CreateImage    ( vout_thread_t *,
121                                     Display *, EXTRA_ARGS, int, int );
122 #endif
123
124 #ifdef HAVE_SYS_SHM_H
125 #ifndef MODULE_NAME_IS_glx
126 IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
127                                     Display *, EXTRA_ARGS_SHM, int, int );
128 #endif
129 static int i_shm_major = 0;
130 #endif
131
132 static void ToggleFullScreen      ( vout_thread_t * );
133
134 static void EnableXScreenSaver    ( vout_thread_t * );
135 static void DisableXScreenSaver   ( vout_thread_t * );
136
137 static void CreateCursor   ( vout_thread_t * );
138 static void DestroyCursor  ( vout_thread_t * );
139 static void ToggleCursor   ( vout_thread_t * );
140
141 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
142 static int  XVideoGetPort    ( vout_thread_t *, vlc_fourcc_t, picture_heap_t * );
143 static void XVideoReleasePort( vout_thread_t *, int );
144 #endif
145
146 #ifdef MODULE_NAME_IS_x11
147 static void SetPalette     ( vout_thread_t *,
148                              uint16_t *, uint16_t *, uint16_t * );
149 #endif
150
151 #ifdef MODULE_NAME_IS_xvmc
152 static void RenderVideo    ( vout_thread_t *, picture_t * );
153 static int  xvmc_check_yv12( Display *display, XvPortID port );
154 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
155 #endif
156
157 static void TestNetWMSupport( vout_thread_t * );
158 static int ConvertKey( int );
159
160 static int WindowOnTop( vout_thread_t *, bool );
161
162 static int X11ErrorHandler( Display *, XErrorEvent * );
163
164 #ifdef HAVE_XSP
165 static void EnablePixelDoubling( vout_thread_t *p_vout );
166 static void DisablePixelDoubling( vout_thread_t *p_vout );
167 #endif
168
169 #ifdef HAVE_OSSO
170 static const int i_backlight_on_interval = 300;
171 #endif
172
173
174
175 /*****************************************************************************
176  * Activate: allocate X11 video thread output method
177  *****************************************************************************
178  * This function allocate and initialize a X11 vout method. It uses some of the
179  * vout properties to choose the window size, and change them according to the
180  * actual properties of the display.
181  *****************************************************************************/
182 int Activate ( vlc_object_t *p_this )
183 {
184     vout_thread_t *p_vout = (vout_thread_t *)p_this;
185     char *        psz_display;
186     vlc_value_t   val;
187 #if defined(MODULE_NAME_IS_xvmc)
188     char *psz_value;
189 #endif
190 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
191     char *       psz_chroma;
192     vlc_fourcc_t i_chroma = 0;
193     bool   b_chroma = 0;
194 #endif
195
196     p_vout->pf_init = InitVideo;
197     p_vout->pf_end = EndVideo;
198     p_vout->pf_manage = ManageVideo;
199 #ifdef MODULE_NAME_IS_xvmc
200     p_vout->pf_render = RenderVideo;
201 #else
202     p_vout->pf_render = NULL;
203 #endif
204     p_vout->pf_display = DisplayVideo;
205     p_vout->pf_control = Control;
206
207     /* Allocate structure */
208     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
209     if( p_vout->p_sys == NULL )
210         return VLC_ENOMEM;
211
212     /* key and mouse event handling */
213     p_vout->p_sys->i_vout_event = var_CreateGetInteger( p_vout, "vout-event" );
214
215     /* Open display, using the "display" config variable or the DISPLAY
216      * environment variable */
217     psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
218
219     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
220
221     if( p_vout->p_sys->p_display == NULL )                         /* error */
222     {
223         msg_Err( p_vout, "cannot open display %s",
224                          XDisplayName( psz_display ) );
225         free( p_vout->p_sys );
226         free( psz_display );
227         return VLC_EGENERIC;
228     }
229     free( psz_display );
230
231     /* Replace error handler so we can intercept some non-fatal errors */
232     XSetErrorHandler( X11ErrorHandler );
233
234     /* Get a screen ID matching the XOpenDisplay return value */
235     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
236
237 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
238     psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
239     if( psz_chroma )
240     {
241         if( strlen( psz_chroma ) >= 4 )
242         {
243             /* Do not use direct assignment because we are not sure of the
244              * alignment. */
245             memcpy(&i_chroma, psz_chroma, 4);
246             b_chroma = 1;
247         }
248
249         free( psz_chroma );
250     }
251
252     if( b_chroma )
253     {
254         msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
255                  i_chroma, (char*)&i_chroma );
256     }
257     else
258     {
259         i_chroma = p_vout->render.i_chroma;
260     }
261
262     /* Check that we have access to an XVideo port providing this chroma */
263     p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
264                                              &p_vout->output );
265     if( p_vout->p_sys->i_xvport < 0 )
266     {
267         /* If a specific chroma format was requested, then we don't try to
268          * be cleverer than the user. He knew pretty well what he wanted. */
269         if( b_chroma )
270         {
271             XCloseDisplay( p_vout->p_sys->p_display );
272             free( p_vout->p_sys );
273             return VLC_EGENERIC;
274         }
275
276         /* It failed, but it's not completely lost ! We try to open an
277          * XVideo port for an YUY2 picture. We'll need to do an YUV
278          * conversion, but at least it has got scaling. */
279         p_vout->p_sys->i_xvport =
280                         XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
281                                                &p_vout->output );
282         if( p_vout->p_sys->i_xvport < 0 )
283         {
284             /* It failed, but it's not completely lost ! We try to open an
285              * XVideo port for a simple 16bpp RGB picture. We'll need to do
286              * an YUV conversion, but at least it has got scaling. */
287             p_vout->p_sys->i_xvport =
288                             XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
289                                                    &p_vout->output );
290             if( p_vout->p_sys->i_xvport < 0 )
291             {
292                 XCloseDisplay( p_vout->p_sys->p_display );
293                 free( p_vout->p_sys );
294                 return VLC_EGENERIC;
295             }
296         }
297     }
298     p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
299 #elif defined(MODULE_NAME_IS_glx)
300     {
301         int i_opcode, i_evt, i_err = 0;
302         int i_maj, i_min = 0;
303
304         /* Check for GLX extension */
305         if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
306                               &i_opcode, &i_evt, &i_err ) )
307         {
308             msg_Err( p_this, "GLX extension not supported" );
309             XCloseDisplay( p_vout->p_sys->p_display );
310             free( p_vout->p_sys );
311             return VLC_EGENERIC;
312         }
313         if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
314         {
315             msg_Err( p_this, "glXQueryExtension failed" );
316             XCloseDisplay( p_vout->p_sys->p_display );
317             free( p_vout->p_sys );
318             return VLC_EGENERIC;
319         }
320
321         /* Check GLX version */
322         if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
323         {
324             msg_Err( p_this, "glXQueryVersion failed" );
325             XCloseDisplay( p_vout->p_sys->p_display );
326             free( p_vout->p_sys );
327             return VLC_EGENERIC;
328         }
329         if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
330         {
331             p_vout->p_sys->b_glx13 = false;
332             msg_Dbg( p_this, "using GLX 1.2 API" );
333         }
334         else
335         {
336             p_vout->p_sys->b_glx13 = true;
337             msg_Dbg( p_this, "using GLX 1.3 API" );
338         }
339     }
340 #endif
341
342     /* Create blank cursor (for mouse cursor autohiding) */
343     p_vout->p_sys->i_time_mouse_last_moved = mdate();
344     p_vout->p_sys->i_mouse_hide_timeout =
345         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
346     p_vout->p_sys->b_mouse_pointer_visible = 1;
347     CreateCursor( p_vout );
348
349     /* Set main window's size */
350     p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
351     p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
352     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
353     /* Spawn base window - this window will include the video output window,
354      * but also command buttons, subtitles and other indicators */
355     if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
356     {
357         msg_Err( p_vout, "cannot create X11 window" );
358         DestroyCursor( p_vout );
359         XCloseDisplay( p_vout->p_sys->p_display );
360         free( p_vout->p_sys );
361         return VLC_EGENERIC;
362     }
363
364     /* Open and initialize device. */
365     if( InitDisplay( p_vout ) )
366     {
367         msg_Err( p_vout, "cannot initialize X11 display" );
368         DestroyCursor( p_vout );
369         DestroyWindow( p_vout, &p_vout->p_sys->original_window );
370         XCloseDisplay( p_vout->p_sys->p_display );
371         free( p_vout->p_sys );
372         return VLC_EGENERIC;
373     }
374
375     /* Disable screen saver */
376     DisableXScreenSaver( p_vout );
377
378     /* Misc init */
379     p_vout->p_sys->b_altfullscreen = 0;
380     p_vout->p_sys->i_time_button_last_pressed = 0;
381
382     TestNetWMSupport( p_vout );
383
384 #ifdef MODULE_NAME_IS_xvmc
385     p_vout->p_sys->p_last_subtitle_save = NULL;
386     psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
387
388     /* Look what method was requested */
389     //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
390     //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
391     if( psz_value )
392     {
393         if( (strcmp(psz_value, "bob") == 0) ||
394             (strcmp(psz_value, "blend") == 0) )
395            p_vout->p_sys->xvmc_deinterlace_method = 2;
396         else if (strcmp(psz_value, "discard") == 0)
397            p_vout->p_sys->xvmc_deinterlace_method = 1;
398         else
399            p_vout->p_sys->xvmc_deinterlace_method = 0;
400         free(psz_value );
401     }
402     else
403         p_vout->p_sys->xvmc_deinterlace_method = 0;
404
405     /* Look what method was requested */
406     //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
407     //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
408     psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
409
410     if( psz_value )
411     {
412         if( strncmp( psz_value, "eq", 2 ) == 0 )
413            p_vout->p_sys->xvmc_crop_style = 1;
414         else if( strncmp( psz_value, "4-16", 4 ) == 0)
415            p_vout->p_sys->xvmc_crop_style = 2;
416         else if( strncmp( psz_value, "16-4", 4 ) == 0)
417            p_vout->p_sys->xvmc_crop_style = 3;
418         else
419            p_vout->p_sys->xvmc_crop_style = 0;
420         free( psz_value );
421     }
422     else
423         p_vout->p_sys->xvmc_crop_style = 0;
424
425     msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
426     msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
427
428     if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
429     {
430         msg_Err( p_vout, "no XVMC capability found" );
431         Deactivate( p_vout );
432         return VLC_EGENERIC;
433     }
434     subpicture_t sub_pic;
435     sub_pic.p_sys = NULL;
436     p_vout->p_sys->last_date = 0;
437 #endif
438
439 #ifdef HAVE_XSP
440     p_vout->p_sys->i_hw_scale = 1;
441 #endif
442
443 #ifdef HAVE_OSSO
444     p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
445     p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
446     if ( p_vout->p_sys->p_octx == NULL ) {
447         msg_Err( p_vout, "Could not get osso context" );
448     } else {
449         msg_Dbg( p_vout, "Initialized osso context" );
450     }
451 #endif
452
453     /* Variable to indicate if the window should be on top of others */
454     /* Trigger a callback right now */
455     var_Get( p_vout, "video-on-top", &val );
456     var_Set( p_vout, "video-on-top", val );
457
458     return VLC_SUCCESS;
459 }
460
461 /*****************************************************************************
462  * Deactivate: destroy X11 video thread output method
463  *****************************************************************************
464  * Terminate an output method created by Open
465  *****************************************************************************/
466 void Deactivate ( vlc_object_t *p_this )
467 {
468     vout_thread_t *p_vout = (vout_thread_t *)p_this;
469
470     /* If the fullscreen window is still open, close it */
471     if( p_vout->b_fullscreen )
472     {
473         ToggleFullScreen( p_vout );
474     }
475
476     /* Restore cursor if it was blanked */
477     if( !p_vout->p_sys->b_mouse_pointer_visible )
478     {
479         ToggleCursor( p_vout );
480     }
481
482 #ifdef MODULE_NAME_IS_x11
483     /* Destroy colormap */
484     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
485     {
486         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
487     }
488 #elif defined(MODULE_NAME_IS_xvideo)
489     XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
490 #elif defined(MODULE_NAME_IS_xvmc)
491     if( p_vout->p_sys->xvmc_cap )
492     {
493         xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
494         xxmc_dispose_context( p_vout );
495         if( p_vout->p_sys->old_subpic )
496         {
497             xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
498             p_vout->p_sys->old_subpic = NULL;
499         }
500         if( p_vout->p_sys->new_subpic )
501         {
502             xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
503             p_vout->p_sys->new_subpic = NULL;
504         }
505         free( p_vout->p_sys->xvmc_cap );
506         xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
507     }
508 #endif
509
510 #ifdef HAVE_XSP
511     DisablePixelDoubling(p_vout);
512 #endif
513
514     DestroyCursor( p_vout );
515     EnableXScreenSaver( p_vout );
516     DestroyWindow( p_vout, &p_vout->p_sys->original_window );
517     XCloseDisplay( p_vout->p_sys->p_display );
518
519     /* Destroy structure */
520 #ifdef MODULE_NAME_IS_xvmc
521     free_context_lock( &p_vout->p_sys->xvmc_lock );
522 #endif
523
524 #ifdef HAVE_OSSO
525     if ( p_vout->p_sys->p_octx != NULL ) {
526         msg_Dbg( p_vout, "Deinitializing osso context" );
527         osso_deinitialize( p_vout->p_sys->p_octx );
528     }
529 #endif
530
531     free( p_vout->p_sys );
532 }
533
534 #ifdef MODULE_NAME_IS_xvmc
535
536 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
537
538 /* called xlocked */
539 static int xvmc_check_yv12( Display *display, XvPortID port )
540 {
541     XvImageFormatValues *formatValues;
542     int                  formats;
543     int                  i;
544
545     formatValues = XvListImageFormats( display, port, &formats );
546
547     for( i = 0; i < formats; i++ )
548     {
549         if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
550             ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
551         {
552             XFree (formatValues);
553             return 0;
554         }
555     }
556
557     XFree (formatValues);
558     return 1;
559 }
560
561 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
562 {
563     XvMCSyncSurface( p_vout->p_sys->p_display, srf );
564 }
565
566 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
567 {
568     Atom         atom;
569     int          xv_double_buffer;
570
571     xv_double_buffer = 1;
572
573     XLockDisplay( p_vout->p_sys->p_display );
574     atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
575 #if 0
576     XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
577 #endif
578     XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
579     XUnlockDisplay( p_vout->p_sys->p_display );
580
581     //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
582     //    "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
583 }
584
585 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
586 {
587     vlc_xxmc_t *xxmc = NULL;
588
589     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
590
591     xxmc = &p_pic->p_sys->xxmc_data;
592     if( (!xxmc->decoded ||
593         !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
594     {
595         xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
596         return;
597     }
598
599 #if 0
600     vlc_mutex_lock( &p_vout->lastsubtitle_lock );
601     if (p_vout->p_sys->p_last_subtitle != NULL)
602     {
603         if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
604         {
605             p_vout->p_sys->new_subpic =
606                 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
607                     p_vout->p_sys->xvmc_width,
608                     p_vout->p_sys->xvmc_height,
609                     p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
610
611             if (p_vout->p_sys->new_subpic)
612             {
613                 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
614                 XvMCClearSubpicture( p_vout->p_sys->p_display,
615                         p_vout->p_sys->new_subpic,
616                         0,
617                         0,
618                         p_vout->p_sys->xvmc_width,
619                         p_vout->p_sys->xvmc_height,
620                         0x00 );
621                 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
622                 clear_xx44_palette( &p_vout->p_sys->palette );
623
624                 if( sub_pic.p_sys == NULL )
625                 {
626                     sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
627                     if( sub_pic.p_sys != NULL )
628                     {
629                         sub_pic.p_sys->p_vout = p_vout;
630                         sub_pic.p_sys->xvmc_surf = NULL;
631                         sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
632                     }
633                 }
634                 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
635                 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
636                 sub_pic.p->i_pitch = p_vout->output.i_width;
637
638                 memset( p_vout->p_sys->subImage->data, 0,
639                         (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
640
641                 if (p_vout->p_last_subtitle != NULL)
642                 {
643                     blend_xx44( p_vout->p_sys->subImage->data,
644                                 p_vout->p_last_subtitle,
645                                 p_vout->p_sys->subImage->width,
646                                 p_vout->p_sys->subImage->height,
647                                 p_vout->p_sys->subImage->width,
648                                 &p_vout->p_sys->palette,
649                                 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
650                 }
651
652                 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
653                 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
654                                          p_vout->p_sys->new_subpic,
655                                          p_vout->p_sys->subImage,
656                                          0, /* overlay->x */
657                                          0, /* overlay->y */
658                                          p_vout->output.i_width, /* overlay->width, */
659                                          p_vout->output.i_height, /* overlay->height */
660                                          0, /* overlay->x */
661                                          0 ); /*overlay->y */
662                 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
663                 if (p_vout->p_sys->old_subpic)
664                 {
665                     xxmc_xvmc_free_subpicture( p_vout,
666                                                p_vout->p_sys->old_subpic);
667                     p_vout->p_sys->old_subpic = NULL;
668                 }
669                 if (p_vout->p_sys->new_subpic)
670                 {
671                     p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
672                     p_vout->p_sys->new_subpic = NULL;
673                     xx44_to_xvmc_palette( &p_vout->p_sys->palette,
674                             p_vout->p_sys->xvmc_palette,
675                             0,
676                             p_vout->p_sys->old_subpic->num_palette_entries,
677                             p_vout->p_sys->old_subpic->entry_bytes,
678                             p_vout->p_sys->old_subpic->component_order );
679                     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
680                     XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
681                                               p_vout->p_sys->old_subpic,
682                                               p_vout->p_sys->xvmc_palette );
683                     XvMCFlushSubpicture( p_vout->p_sys->p_display,
684                                          p_vout->p_sys->old_subpic);
685                     XvMCSyncSubpicture( p_vout->p_sys->p_display,
686                                         p_vout->p_sys->old_subpic );
687                     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
688                 }
689
690                 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
691                 if (p_vout->p_sys->xvmc_backend_subpic )
692                 {
693                     XvMCBlendSubpicture( p_vout->p_sys->p_display,
694                                          p_pic->p_sys->xvmc_surf,
695                                          p_vout->p_sys->old_subpic,
696                                          0,
697                                          0,
698                                          p_vout->p_sys->xvmc_width,
699                                          p_vout->p_sys->xvmc_height,
700                                          0,
701                                          0,
702                                          p_vout->p_sys->xvmc_width,
703                                          p_vout->p_sys->xvmc_height );
704                 }
705                 else
706                 {
707                     XvMCBlendSubpicture2( p_vout->p_sys->p_display,
708                                           p_pic->p_sys->xvmc_surf,
709                                           p_pic->p_sys->xvmc_surf,
710                                           p_vout->p_sys->old_subpic,
711                                           0,
712                                           0,
713                                           p_vout->p_sys->xvmc_width,
714                                           p_vout->p_sys->xvmc_height,
715                                           0,
716                                           0,
717                                           p_vout->p_sys->xvmc_width,
718                                           p_vout->p_sys->xvmc_height );
719                }
720                XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
721             }
722         }
723         else
724         {
725             XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
726             if( p_vout->p_sys->xvmc_backend_subpic )
727             {
728                 XvMCBlendSubpicture( p_vout->p_sys->p_display,
729                                      p_pic->p_sys->xvmc_surf,
730                                      p_vout->p_sys->old_subpic,
731                                      0, 0,
732                                      p_vout->p_sys->xvmc_width,
733                                      p_vout->p_sys->xvmc_height,
734                                      0, 0,
735                                      p_vout->p_sys->xvmc_width,
736                                      p_vout->p_sys->xvmc_height );
737             }
738             else
739             {
740                 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
741                                       p_pic->p_sys->xvmc_surf,
742                                       p_pic->p_sys->xvmc_surf,
743                                       p_vout->p_sys->old_subpic,
744                                       0, 0,
745                                       p_vout->p_sys->xvmc_width,
746                                       p_vout->p_sys->xvmc_height,
747                                       0, 0,
748                                       p_vout->p_sys->xvmc_width,
749                                       p_vout->p_sys->xvmc_height );
750             }
751             XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
752         }
753     }
754     p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
755
756     vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
757 #endif
758     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
759 }
760 #endif
761
762 #ifdef HAVE_XSP
763 /*****************************************************************************
764  * EnablePixelDoubling: Enables pixel doubling
765  *****************************************************************************
766  * Checks if the double size image fits in current window, and enables pixel
767  * doubling accordingly. The i_hw_scale is the integer scaling factor.
768  *****************************************************************************/
769 static void EnablePixelDoubling( vout_thread_t *p_vout )
770 {
771     int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
772     int i_vert_scale =  ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
773     if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
774         p_vout->p_sys->i_hw_scale = 2;
775         msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
776         XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
777     }
778 }
779
780 /*****************************************************************************
781  * DisablePixelDoubling: Disables pixel doubling
782  *****************************************************************************
783  * The scaling factor i_hw_scale is reset to the no-scaling value 1.
784  *****************************************************************************/
785 static void DisablePixelDoubling( vout_thread_t *p_vout )
786 {
787     if ( p_vout->p_sys->i_hw_scale > 1 ) {
788         msg_Dbg( p_vout, "Disabling pixel doubling" );
789         XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
790         p_vout->p_sys->i_hw_scale = 1;
791     }
792 }
793 #endif
794
795
796
797 /*****************************************************************************
798  * InitVideo: initialize X11 video thread output method
799  *****************************************************************************
800  * This function create the XImages needed by the output thread. It is called
801  * at the beginning of the thread, but also each time the window is resized.
802  *****************************************************************************/
803 static int InitVideo( vout_thread_t *p_vout )
804 {
805     unsigned int i_index = 0;
806     picture_t *p_pic;
807
808     I_OUTPUTPICTURES = 0;
809
810 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
811     /* Initialize the output structure; we already found an XVideo port,
812      * and the corresponding chroma we will be using. Since we can
813      * arbitrary scale, stick to the coordinates and aspect. */
814     p_vout->output.i_width  = p_vout->render.i_width;
815     p_vout->output.i_height = p_vout->render.i_height;
816     p_vout->output.i_aspect = p_vout->render.i_aspect;
817
818     p_vout->fmt_out = p_vout->fmt_in;
819     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
820
821 #if XvVersion < 2 || ( XvVersion == 2 && XvRevision < 2 )
822     switch( p_vout->output.i_chroma )
823     {
824         case VLC_FOURCC('R','V','1','6'):
825 #if defined( WORDS_BIGENDIAN )
826             p_vout->output.i_rmask = 0xf800;
827             p_vout->output.i_gmask = 0x07e0;
828             p_vout->output.i_bmask = 0x001f;
829 #else
830             p_vout->output.i_rmask = 0x001f;
831             p_vout->output.i_gmask = 0x07e0;
832             p_vout->output.i_bmask = 0xf800;
833 #endif
834             break;
835         case VLC_FOURCC('R','V','1','5'):
836 #if defined( WORDS_BIGENDIAN )
837             p_vout->output.i_rmask = 0x7c00;
838             p_vout->output.i_gmask = 0x03e0;
839             p_vout->output.i_bmask = 0x001f;
840 #else
841             p_vout->output.i_rmask = 0x001f;
842             p_vout->output.i_gmask = 0x03e0;
843             p_vout->output.i_bmask = 0x7c00;
844 #endif
845             break;
846     }
847 #endif
848
849 #elif defined(MODULE_NAME_IS_x11)
850     /* Initialize the output structure: RGB with square pixels, whatever
851      * the input format is, since it's the only format we know */
852     switch( p_vout->p_sys->i_screen_depth )
853     {
854         case 8: /* FIXME: set the palette */
855             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
856         case 15:
857             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
858         case 16:
859             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
860         case 24:
861         case 32:
862             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
863         default:
864             msg_Err( p_vout, "unknown screen depth %i",
865                      p_vout->p_sys->i_screen_depth );
866             return VLC_SUCCESS;
867     }
868
869 #ifdef HAVE_XSP
870     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width  / p_vout->p_sys->i_hw_scale,
871                        p_vout->p_sys->p_win->i_height  / p_vout->p_sys->i_hw_scale,
872                        &i_index, &i_index,
873                        &p_vout->fmt_out.i_visible_width,
874                        &p_vout->fmt_out.i_visible_height );
875 #else
876     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
877                        p_vout->p_sys->p_win->i_height,
878                        &i_index, &i_index,
879                        &p_vout->fmt_out.i_visible_width,
880                        &p_vout->fmt_out.i_visible_height );
881 #endif
882
883     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
884
885     p_vout->output.i_width = p_vout->fmt_out.i_width =
886         p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
887         p_vout->fmt_in.i_visible_width;
888     p_vout->output.i_height = p_vout->fmt_out.i_height =
889         p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
890         p_vout->fmt_in.i_visible_height;
891     p_vout->fmt_out.i_x_offset =
892         p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
893         p_vout->fmt_in.i_visible_width;
894     p_vout->fmt_out.i_y_offset =
895         p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
896         p_vout->fmt_in.i_visible_height;
897
898     p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
899     p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
900         p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
901
902     msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
903              p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
904              p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
905              p_vout->fmt_out.i_visible_width,
906              p_vout->fmt_out.i_visible_height );
907 #endif
908
909     /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
910     while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
911     {
912         p_pic = NULL;
913
914         /* Find an empty picture slot */
915         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
916         {
917           if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
918             {
919                 p_pic = p_vout->p_picture + i_index;
920                 break;
921             }
922         }
923
924         /* Allocate the picture */
925         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
926         {
927             break;
928         }
929
930         p_pic->i_status = DESTROYED_PICTURE;
931         p_pic->i_type   = DIRECT_PICTURE;
932
933         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
934
935         I_OUTPUTPICTURES++;
936     }
937
938     if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
939     {
940         /* U and V inverted compared to I420
941          * Fixme: this should be handled by the vout core */
942         p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
943         p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
944     }
945
946     return VLC_SUCCESS;
947 }
948
949 /*****************************************************************************
950  * DisplayVideo: displays previously rendered output
951  *****************************************************************************
952  * This function sends the currently rendered image to X11 server.
953  * (The Xv extension takes care of "double-buffering".)
954  *****************************************************************************/
955 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
956 {
957     unsigned int i_width, i_height, i_x, i_y;
958
959     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
960                        p_vout->p_sys->p_win->i_height,
961                        &i_x, &i_y, &i_width, &i_height );
962
963 #ifdef MODULE_NAME_IS_xvmc
964     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
965
966     vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
967     if( !xxmc->decoded ||
968         !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
969     {
970       msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
971                xxmc->decoded,
972                xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
973       xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
974       return;
975     }
976
977     int src_width = p_vout->output.i_width;
978     int src_height = p_vout->output.i_height;
979     int src_x, src_y;
980
981     if( p_vout->p_sys->xvmc_crop_style == 1 )
982     {
983         src_x = 20;
984         src_y = 20;
985         src_width -= 40;
986         src_height -= 40;
987     }
988     else if( p_vout->p_sys->xvmc_crop_style == 2 )
989     {
990         src_x = 20;
991         src_y = 40;
992         src_width -= 40;
993         src_height -= 80;
994     }
995     else if( p_vout->p_sys->xvmc_crop_style == 3 )
996     {
997         src_x = 40;
998         src_y = 20;
999         src_width -= 80;
1000         src_height -= 40;
1001     }
1002     else
1003     {
1004         src_x = 0;
1005         src_y = 0;
1006     }
1007
1008     int first_field;
1009     if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1010     {   /* BOB DEINTERLACE */
1011         if( (p_pic->p_sys->nb_display == 0) ||
1012             (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1013         {
1014             first_field = (p_pic->b_top_field_first) ?
1015                                 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1016         }
1017         else
1018         {
1019             first_field = (p_pic->b_top_field_first) ?
1020                                 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1021         }
1022     }
1023     else
1024     {
1025         first_field = XVMC_FRAME_PICTURE;
1026      }
1027
1028     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1029     XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
1030     /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1031     XvMCPutSurface( p_vout->p_sys->p_display,
1032                     p_pic->p_sys->xvmc_surf,
1033                     p_vout->p_sys->p_win->video_window,
1034                     src_x,
1035                     src_y,
1036                     src_width,
1037                     src_height,
1038                     0 /*dest_x*/,
1039                     0 /*dest_y*/,
1040                     i_width,
1041                     i_height,
1042                     first_field);
1043
1044     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1045     if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1046     {   /* BOB DEINTERLACE */
1047         if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1048         {
1049             mtime_t last_date = p_pic->date;
1050
1051             vlc_mutex_lock( &p_vout->picture_lock );
1052             if( !p_vout->p_sys->last_date )
1053             {
1054                 p_pic->date += 20000;
1055             }
1056             else
1057             {
1058                 p_pic->date = ((3 * p_pic->date -
1059                                     p_vout->p_sys->last_date) / 2 );
1060             }
1061             p_vout->p_sys->last_date = last_date;
1062             p_pic->b_force = 1;
1063             p_pic->p_sys->nb_display = 1;
1064             vlc_mutex_unlock( &p_vout->picture_lock );
1065         }
1066         else
1067         {
1068             p_pic->p_sys->nb_display = 0;
1069             p_pic->b_force = 0;
1070         }
1071     }
1072     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1073 #endif
1074
1075 #ifdef HAVE_SYS_SHM_H
1076     if( p_vout->p_sys->i_shm_opcode )
1077     {
1078         /* Display rendered image using shared memory extension */
1079 #   if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1080         XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1081                        p_vout->p_sys->p_win->video_window,
1082                        p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1083                        p_vout->fmt_out.i_x_offset,
1084                        p_vout->fmt_out.i_y_offset,
1085                        p_vout->fmt_out.i_visible_width,
1086                        p_vout->fmt_out.i_visible_height,
1087                        0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1088                        False /* Don't put True here or you'll waste your CPU */ );
1089 #   else
1090         XShmPutImage( p_vout->p_sys->p_display,
1091                       p_vout->p_sys->p_win->video_window,
1092                       p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1093                       p_vout->fmt_out.i_x_offset,
1094                       p_vout->fmt_out.i_y_offset,
1095                       0 /*dest_x*/, 0 /*dest_y*/,
1096                       p_vout->fmt_out.i_visible_width,
1097                       p_vout->fmt_out.i_visible_height,
1098                       False /* Don't put True here ! */ );
1099 #   endif
1100     }
1101     else
1102 #endif /* HAVE_SYS_SHM_H */
1103     {
1104         /* Use standard XPutImage -- this is gonna be slow ! */
1105 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1106         XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1107                     p_vout->p_sys->p_win->video_window,
1108                     p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1109                     p_vout->fmt_out.i_x_offset,
1110                     p_vout->fmt_out.i_y_offset,
1111                     p_vout->fmt_out.i_visible_width,
1112                     p_vout->fmt_out.i_visible_height,
1113                     0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1114 #else
1115         XPutImage( p_vout->p_sys->p_display,
1116                    p_vout->p_sys->p_win->video_window,
1117                    p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1118                    p_vout->fmt_out.i_x_offset,
1119                    p_vout->fmt_out.i_y_offset,
1120                    0 /*dest_x*/, 0 /*dest_y*/,
1121                    p_vout->fmt_out.i_visible_width,
1122                    p_vout->fmt_out.i_visible_height );
1123 #endif
1124     }
1125
1126     /* Make sure the command is sent now - do NOT use XFlush !*/
1127     XSync( p_vout->p_sys->p_display, False );
1128 }
1129
1130 /*****************************************************************************
1131  * ManageVideo: handle X11 events
1132  *****************************************************************************
1133  * This function should be called regularly by video output thread. It manages
1134  * X11 events and allows window resizing. It returns a non null value on
1135  * error.
1136  *****************************************************************************/
1137 static int ManageVideo( vout_thread_t *p_vout )
1138 {
1139     XEvent      xevent;                                         /* X11 event */
1140     vlc_value_t val;
1141
1142 #ifdef MODULE_NAME_IS_xvmc
1143     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1144 #endif
1145
1146     /* Handle events from the owner window */
1147     if( p_vout->p_sys->p_win->owner_window )
1148     {
1149         while( XCheckWindowEvent( p_vout->p_sys->p_display,
1150                                 p_vout->p_sys->p_win->owner_window->handle.xid,
1151                                   StructureNotifyMask, &xevent ) == True )
1152         {
1153             /* ConfigureNotify event: prepare  */
1154             if( xevent.type == ConfigureNotify )
1155             {
1156                 /* Update dimensions */
1157                 XResizeWindow( p_vout->p_sys->p_display,
1158                                p_vout->p_sys->p_win->base_window,
1159                                xevent.xconfigure.width,
1160                                xevent.xconfigure.height );
1161             }
1162         }
1163     }
1164
1165     /* Handle X11 events: ConfigureNotify events are parsed to know if the
1166      * output window's size changed, MapNotify and UnmapNotify to know if the
1167      * window is mapped (and if the display is useful), and ClientMessages
1168      * to intercept window destruction requests */
1169
1170     while( XCheckWindowEvent( p_vout->p_sys->p_display,
1171                               p_vout->p_sys->p_win->base_window,
1172                               StructureNotifyMask | KeyPressMask |
1173                               ButtonPressMask | ButtonReleaseMask |
1174                               PointerMotionMask | Button1MotionMask , &xevent )
1175            == True )
1176     {
1177         /* ConfigureNotify event: prepare  */
1178         if( xevent.type == ConfigureNotify )
1179         {
1180             if( (unsigned int)xevent.xconfigure.width
1181                    != p_vout->p_sys->p_win->i_width
1182               || (unsigned int)xevent.xconfigure.height
1183                     != p_vout->p_sys->p_win->i_height )
1184             {
1185                 /* Update dimensions */
1186                 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1187                 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1188                 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1189             }
1190         }
1191         /* Keyboard event */
1192         else if( xevent.type == KeyPress )
1193         {
1194             unsigned int state = xevent.xkey.state;
1195             KeySym x_key_symbol;
1196             char i_key;                                   /* ISO Latin-1 key */
1197
1198             /* We may have keys like F1 trough F12, ESC ... */
1199             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1200                                              xevent.xkey.keycode, 0 );
1201             val.i_int = ConvertKey( (int)x_key_symbol );
1202
1203             xevent.xkey.state &= ~ShiftMask;
1204             xevent.xkey.state &= ~ControlMask;
1205             xevent.xkey.state &= ~Mod1Mask;
1206
1207             if( !val.i_int &&
1208                 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1209             {
1210                 /* "Normal Keys"
1211                  * The reason why I use this instead of XK_0 is that
1212                  * with XLookupString, we don't have to care about
1213                  * keymaps. */
1214                 val.i_int = i_key;
1215             }
1216
1217             if( val.i_int )
1218             {
1219                 if( state & ShiftMask )
1220                 {
1221                     val.i_int |= KEY_MODIFIER_SHIFT;
1222                 }
1223                 if( state & ControlMask )
1224                 {
1225                     val.i_int |= KEY_MODIFIER_CTRL;
1226                 }
1227                 if( state & Mod1Mask )
1228                 {
1229                     val.i_int |= KEY_MODIFIER_ALT;
1230                 }
1231                 var_Set( p_vout->p_libvlc, "key-pressed", val );
1232             }
1233         }
1234         /* Mouse click */
1235         else if( xevent.type == ButtonPress )
1236         {
1237             switch( ((XButtonEvent *)&xevent)->button )
1238             {
1239                 case Button1:
1240                     var_Get( p_vout, "mouse-button-down", &val );
1241                     val.i_int |= 1;
1242                     var_Set( p_vout, "mouse-button-down", val );
1243
1244                     /* detect double-clicks */
1245                     if( ( ((XButtonEvent *)&xevent)->time -
1246                           p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1247                     {
1248                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1249                     }
1250
1251                     p_vout->p_sys->i_time_button_last_pressed =
1252                         ((XButtonEvent *)&xevent)->time;
1253                     break;
1254                 case Button2:
1255                     var_Get( p_vout, "mouse-button-down", &val );
1256                     val.i_int |= 2;
1257                     var_Set( p_vout, "mouse-button-down", val );
1258                     break;
1259
1260                 case Button3:
1261                     var_Get( p_vout, "mouse-button-down", &val );
1262                     val.i_int |= 4;
1263                     var_Set( p_vout, "mouse-button-down", val );
1264                     break;
1265
1266                 case Button4:
1267                     var_Get( p_vout, "mouse-button-down", &val );
1268                     val.i_int |= 8;
1269                     var_Set( p_vout, "mouse-button-down", val );
1270                     break;
1271
1272                 case Button5:
1273                     var_Get( p_vout, "mouse-button-down", &val );
1274                     val.i_int |= 16;
1275                     var_Set( p_vout, "mouse-button-down", val );
1276                     break;
1277             }
1278         }
1279         /* Mouse release */
1280         else if( xevent.type == ButtonRelease )
1281         {
1282             switch( ((XButtonEvent *)&xevent)->button )
1283             {
1284                 case Button1:
1285                     {
1286                         var_Get( p_vout, "mouse-button-down", &val );
1287                         val.i_int &= ~1;
1288                         var_Set( p_vout, "mouse-button-down", val );
1289
1290                         var_SetBool( p_vout, "mouse-clicked", true );
1291                         var_SetBool( p_vout->p_libvlc, "intf-popupmenu", false );
1292                     }
1293                     break;
1294
1295                 case Button2:
1296                     {
1297                         var_Get( p_vout, "mouse-button-down", &val );
1298                         val.i_int &= ~2;
1299                         var_Set( p_vout, "mouse-button-down", val );
1300
1301                         var_Get( p_vout->p_libvlc, "intf-show", &val );
1302                         val.b_bool = !val.b_bool;
1303                         var_Set( p_vout->p_libvlc, "intf-show", val );
1304                     }
1305                     break;
1306
1307                 case Button3:
1308                     {
1309                         var_Get( p_vout, "mouse-button-down", &val );
1310                         val.i_int &= ~4;
1311                         var_Set( p_vout, "mouse-button-down", val );
1312
1313                         var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
1314                     }
1315                     break;
1316
1317                 case Button4:
1318                     var_Get( p_vout, "mouse-button-down", &val );
1319                     val.i_int &= ~8;
1320                     var_Set( p_vout, "mouse-button-down", val );
1321                     break;
1322
1323                 case Button5:
1324                     var_Get( p_vout, "mouse-button-down", &val );
1325                     val.i_int &= ~16;
1326                     var_Set( p_vout, "mouse-button-down", val );
1327                     break;
1328
1329             }
1330         }
1331         /* Mouse move */
1332         else if( xevent.type == MotionNotify )
1333         {
1334             unsigned int i_width, i_height, i_x, i_y;
1335
1336             /* somewhat different use for vout_PlacePicture:
1337              * here the values are needed to give to mouse coordinates
1338              * in the original picture space */
1339             vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1340                                p_vout->p_sys->p_win->i_height,
1341                                &i_x, &i_y, &i_width, &i_height );
1342
1343             /* Compute the x coordinate and check if the value is
1344                in [0,p_vout->fmt_in.i_visible_width] */
1345             val.i_int = ( xevent.xmotion.x - i_x ) *
1346                 p_vout->fmt_in.i_visible_width / i_width +
1347                 p_vout->fmt_in.i_x_offset;
1348
1349             if( (int)(xevent.xmotion.x - i_x) < 0 )
1350                 val.i_int = 0;
1351             else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1352                 val.i_int = p_vout->fmt_in.i_visible_width;
1353
1354             var_Set( p_vout, "mouse-x", val );
1355
1356             /* compute the y coordinate and check if the value is
1357                in [0,p_vout->fmt_in.i_visible_height] */
1358             val.i_int = ( xevent.xmotion.y - i_y ) *
1359                 p_vout->fmt_in.i_visible_height / i_height +
1360                 p_vout->fmt_in.i_y_offset;
1361
1362             if( (int)(xevent.xmotion.y - i_y) < 0 )
1363                 val.i_int = 0;
1364             else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1365                 val.i_int = p_vout->fmt_in.i_visible_height;
1366
1367             var_Set( p_vout, "mouse-y", val );
1368
1369             var_SetBool( p_vout, "mouse-moved", true );
1370
1371             p_vout->p_sys->i_time_mouse_last_moved = mdate();
1372             if( ! p_vout->p_sys->b_mouse_pointer_visible )
1373             {
1374                 ToggleCursor( p_vout );
1375             }
1376         }
1377         else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1378                   || xevent.type == MapNotify
1379                   || xevent.type == UnmapNotify )
1380         {
1381             /* Ignore these events */
1382         }
1383         else /* Other events */
1384         {
1385             msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1386         }
1387     }
1388
1389     /* Handle events for video output sub-window */
1390     while( XCheckWindowEvent( p_vout->p_sys->p_display,
1391                               p_vout->p_sys->p_win->video_window,
1392                               ExposureMask, &xevent ) == True )
1393     {
1394         /* Window exposed (only handled if stream playback is paused) */
1395         if( xevent.type == Expose )
1396         {
1397             if( ((XExposeEvent *)&xevent)->count == 0 )
1398             {
1399                 /* (if this is the last a collection of expose events...) */
1400
1401 #if defined(MODULE_NAME_IS_xvideo)
1402                 x11_window_t *p_win = p_vout->p_sys->p_win;
1403
1404                 /* Paint the colour key if needed */
1405                 if( p_vout->p_sys->b_paint_colourkey &&
1406                     xevent.xexpose.window == p_win->video_window )
1407                 {
1408                     XSetForeground( p_vout->p_sys->p_display,
1409                                     p_win->gc, p_vout->p_sys->i_colourkey );
1410                     XFillRectangle( p_vout->p_sys->p_display,
1411                                     p_win->video_window, p_win->gc, 0, 0,
1412                                     p_win->i_width, p_win->i_height );
1413                 }
1414 #endif
1415
1416 #if 0
1417                 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1418                 {
1419                     if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1420                                                    ->stream.control.i_status )
1421                     {
1422                         /* XVideoDisplay( p_vout )*/;
1423                     }
1424                 }
1425 #endif
1426             }
1427         }
1428     }
1429
1430     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1431      * are handled - according to the man pages, the format is always 32
1432      * in this case */
1433     while( XCheckTypedEvent( p_vout->p_sys->p_display,
1434                              ClientMessage, &xevent ) )
1435     {
1436         if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1437                && ((Atom)xevent.xclient.data.l[0]
1438                      == p_vout->p_sys->p_win->wm_delete_window ) )
1439         {
1440             /* the user wants to close the window */
1441             playlist_t * p_playlist = pl_Hold( p_vout );
1442             if( p_playlist != NULL )
1443             {
1444                 playlist_Stop( p_playlist );
1445                 pl_Release( p_vout );
1446             }
1447         }
1448     }
1449
1450     /*
1451      * Fullscreen Change
1452      */
1453     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1454     {
1455         /* Update the object variable and trigger callback */
1456         var_SetBool( p_vout, "fullscreen", !p_vout->b_fullscreen );
1457
1458         ToggleFullScreen( p_vout );
1459         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1460     }
1461
1462     /* autoscale toggle */
1463     if( p_vout->i_changes & VOUT_SCALE_CHANGE )
1464     {
1465         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
1466
1467         p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
1468         p_vout->i_zoom = ZOOM_FP_FACTOR;
1469
1470         p_vout->i_changes |= VOUT_SIZE_CHANGE;
1471     }
1472
1473     /* scaling factor */
1474     if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
1475     {
1476         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
1477
1478         p_vout->b_autoscale = false;
1479         p_vout->i_zoom =
1480             (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
1481
1482         p_vout->i_changes |= VOUT_SIZE_CHANGE;
1483     }
1484
1485     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1486         p_vout->i_changes & VOUT_ASPECT_CHANGE )
1487     {
1488         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1489         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1490
1491         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1492         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1493         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1494         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1495         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1496         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1497         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1498         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1499
1500         p_vout->i_changes |= VOUT_SIZE_CHANGE;
1501     }
1502
1503     /*
1504      * Size change
1505      *
1506      * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1507      *  the size flag inside the fullscreen routine)
1508      */
1509     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1510     {
1511         unsigned int i_width, i_height, i_x, i_y;
1512
1513         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1514
1515 #ifdef MODULE_NAME_IS_x11
1516         /* We need to signal the vout thread about the size change because it
1517          * is doing the rescaling */
1518         p_vout->i_changes |= VOUT_SIZE_CHANGE;
1519 #endif
1520
1521         vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1522                            p_vout->p_sys->p_win->i_height,
1523                            &i_x, &i_y, &i_width, &i_height );
1524
1525         XMoveResizeWindow( p_vout->p_sys->p_display,
1526                            p_vout->p_sys->p_win->video_window,
1527                            i_x, i_y, i_width, i_height );
1528     }
1529
1530     /* cursor hiding depending on --vout-event option
1531      *      activated if:
1532      *            value = 1 (Fullsupport) (default value)
1533      *         or value = 2 (Fullscreen-Only) and condition met
1534      */
1535     bool b_vout_event = (   ( p_vout->p_sys->i_vout_event == 1 )
1536                          || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1537                         );
1538
1539     /* Autohide Cursour */
1540     if( mdate() - p_vout->p_sys->i_time_mouse_last_moved >
1541         p_vout->p_sys->i_mouse_hide_timeout )
1542     {
1543         /* Hide the mouse automatically */
1544         if( b_vout_event && p_vout->p_sys->b_mouse_pointer_visible )
1545         {
1546             ToggleCursor( p_vout );
1547         }
1548     }
1549
1550 #ifdef MODULE_NAME_IS_xvmc
1551     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1552 #endif
1553
1554 #ifdef HAVE_OSSO
1555     if ( p_vout->p_sys->p_octx != NULL ) {
1556         if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1557             if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1558                 msg_Err( p_vout, "Could not disable backlight blanking" );
1559         } else {
1560                 msg_Dbg( p_vout, "Backlight blanking disabled" );
1561             }
1562             p_vout->p_sys->i_backlight_on_counter = 0;
1563         } else {
1564             p_vout->p_sys->i_backlight_on_counter ++;
1565         }
1566     }
1567 #endif
1568     return 0;
1569 }
1570
1571 /*****************************************************************************
1572  * EndVideo: terminate X11 video thread output method
1573  *****************************************************************************
1574  * Destroy the X11 XImages created by Init. It is called at the end of
1575  * the thread, but also each time the window is resized.
1576  *****************************************************************************/
1577 static void EndVideo( vout_thread_t *p_vout )
1578 {
1579     int i_index;
1580
1581     /* Free the direct buffers we allocated */
1582     for( i_index = I_OUTPUTPICTURES ; i_index ; )
1583     {
1584         i_index--;
1585         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1586     }
1587 }
1588
1589 /* following functions are local */
1590
1591 /*****************************************************************************
1592  * CreateWindow: open and set-up X11 main window
1593  *****************************************************************************/
1594 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1595 {
1596     XSizeHints              xsize_hints;
1597     XSetWindowAttributes    xwindow_attributes;
1598     XGCValues               xgcvalues;
1599     XEvent                  xevent;
1600
1601     bool              b_map_notify = false;
1602     vlc_value_t             val;
1603
1604     /* Prepare window manager hints and properties */
1605     p_win->wm_protocols =
1606              XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1607     p_win->wm_delete_window =
1608              XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1609
1610     /* Never have a 0-pixel-wide window */
1611     xsize_hints.min_width = 2;
1612     xsize_hints.min_height = 1;
1613
1614     /* Prepare window attributes */
1615     xwindow_attributes.backing_store = Always;       /* save the hidden part */
1616     xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1617                                                      p_vout->p_sys->i_screen);
1618     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1619
1620     if( !p_vout->b_fullscreen )
1621     {
1622         p_win->owner_window = vout_RequestXWindow( p_vout, &p_win->i_x,
1623                               &p_win->i_y, &p_win->i_width, &p_win->i_height );
1624         xsize_hints.base_width  = xsize_hints.width = p_win->i_width;
1625         xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1626         xsize_hints.flags       = PSize | PMinSize;
1627
1628         if( p_win->i_x >=0 || p_win->i_y >= 0 )
1629         {
1630             xsize_hints.x = p_win->i_x;
1631             xsize_hints.y = p_win->i_y;
1632             xsize_hints.flags |= PPosition;
1633         }
1634     }
1635     else
1636     {
1637         /* Fullscreen window size and position */
1638         p_win->owner_window = NULL;
1639         p_win->i_x = p_win->i_y = 0;
1640         p_win->i_width =
1641             DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1642         p_win->i_height =
1643             DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1644     }
1645
1646     if( !p_win->owner_window )
1647     {
1648         /* Create the window and set hints - the window must receive
1649          * ConfigureNotify events, and until it is displayed, Expose and
1650          * MapNotify events. */
1651
1652         p_win->base_window =
1653             XCreateWindow( p_vout->p_sys->p_display,
1654                            DefaultRootWindow( p_vout->p_sys->p_display ),
1655                            p_win->i_x, p_win->i_y,
1656                            p_win->i_width, p_win->i_height,
1657                            0,
1658                            0, InputOutput, 0,
1659                            CWBackingStore | CWBackPixel | CWEventMask,
1660                            &xwindow_attributes );
1661
1662         var_Get( p_vout, "video-title", &val );
1663         if( !val.psz_string || !*val.psz_string )
1664         {
1665             XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1666 #ifdef MODULE_NAME_IS_x11
1667                         VOUT_TITLE " (X11 output)"
1668 #elif defined(MODULE_NAME_IS_glx)
1669                         VOUT_TITLE " (GLX output)"
1670 #else
1671                         VOUT_TITLE " (XVideo output)"
1672 #endif
1673               );
1674         }
1675         else
1676         {
1677             XStoreName( p_vout->p_sys->p_display,
1678                         p_win->base_window, val.psz_string );
1679         }
1680         free( val.psz_string );
1681
1682         if( !p_vout->b_fullscreen )
1683         {
1684             const char *argv[] = { "vlc", NULL };
1685
1686             /* Set window manager hints and properties: size hints, command,
1687              * window's name, and accepted protocols */
1688             XSetWMNormalHints( p_vout->p_sys->p_display,
1689                                p_win->base_window, &xsize_hints );
1690             XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1691                          (char**)argv, 1 );
1692
1693             if( !var_GetBool( p_vout, "video-deco") )
1694             {
1695                 Atom prop;
1696                 mwmhints_t mwmhints;
1697
1698                 mwmhints.flags = MWM_HINTS_DECORATIONS;
1699                 mwmhints.decorations = False;
1700
1701                 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1702                                     False );
1703
1704                 XChangeProperty( p_vout->p_sys->p_display,
1705                                  p_win->base_window,
1706                                  prop, prop, 32, PropModeReplace,
1707                                  (unsigned char *)&mwmhints,
1708                                  PROP_MWM_HINTS_ELEMENTS );
1709             }
1710         }
1711     }
1712     else
1713     {
1714         Window dummy1;
1715         int dummy2, dummy3;
1716         unsigned int dummy4, dummy5;
1717
1718         /* Select events we are interested in. */
1719         XSelectInput( p_vout->p_sys->p_display,
1720                       p_win->owner_window->handle.xid, StructureNotifyMask );
1721
1722         /* Get the parent window's geometry information */
1723         XGetGeometry( p_vout->p_sys->p_display,
1724                       p_win->owner_window->handle.xid,
1725                       &dummy1, &dummy2, &dummy3,
1726                       &p_win->i_width,
1727                       &p_win->i_height,
1728                       &dummy4, &dummy5 );
1729
1730         /* From man XSelectInput: only one client at a time can select a
1731          * ButtonPress event, so we need to open a new window anyway. */
1732         p_win->base_window =
1733             XCreateWindow( p_vout->p_sys->p_display,
1734                            p_win->owner_window->handle.xid,
1735                            0, 0,
1736                            p_win->i_width, p_win->i_height,
1737                            0,
1738                            0, CopyFromParent, 0,
1739                            CWBackingStore | CWBackPixel | CWEventMask,
1740                            &xwindow_attributes );
1741     }
1742
1743     if( (p_win->wm_protocols == None)        /* use WM_DELETE_WINDOW */
1744         || (p_win->wm_delete_window == None)
1745         || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1746                              &p_win->wm_delete_window, 1 ) )
1747     {
1748         /* WM_DELETE_WINDOW is not supported by window manager */
1749         msg_Warn( p_vout, "missing or bad window manager" );
1750     }
1751
1752     /* Creation of a graphic context that doesn't generate a GraphicsExpose
1753      * event when using functions like XCopyArea */
1754     xgcvalues.graphics_exposures = False;
1755     p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1756                            p_win->base_window,
1757                            GCGraphicsExposures, &xgcvalues );
1758
1759     /* Wait till the window is mapped */
1760     XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1761     do
1762     {
1763         XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1764                       SubstructureNotifyMask | StructureNotifyMask, &xevent);
1765         if( (xevent.type == MapNotify)
1766                  && (xevent.xmap.window == p_win->base_window) )
1767         {
1768             b_map_notify = true;
1769         }
1770         else if( (xevent.type == ConfigureNotify)
1771                  && (xevent.xconfigure.window == p_win->base_window) )
1772         {
1773             p_win->i_width = xevent.xconfigure.width;
1774             p_win->i_height = xevent.xconfigure.height;
1775         }
1776     } while( !b_map_notify );
1777
1778     /* key and mouse events handling depending on --vout-event option
1779      *      activated if:
1780      *            value = 1 (Fullsupport) (default value)
1781      *         or value = 2 (Fullscreen-Only) and condition met
1782      */
1783     bool b_vout_event = (   ( p_vout->p_sys->i_vout_event == 1 )
1784                          || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1785                         );
1786     if ( b_vout_event )
1787         XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1788                       StructureNotifyMask | KeyPressMask |
1789                       ButtonPressMask | ButtonReleaseMask |
1790                       PointerMotionMask );
1791
1792 #ifdef MODULE_NAME_IS_x11
1793     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1794     {
1795         /* Allocate a new palette */
1796         p_vout->p_sys->colormap =
1797             XCreateColormap( p_vout->p_sys->p_display,
1798                              DefaultRootWindow( p_vout->p_sys->p_display ),
1799                              DefaultVisual( p_vout->p_sys->p_display,
1800                                             p_vout->p_sys->i_screen ),
1801                              AllocAll );
1802
1803         xwindow_attributes.colormap = p_vout->p_sys->colormap;
1804         XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1805                                  CWColormap, &xwindow_attributes );
1806     }
1807 #endif
1808
1809     /* Create video output sub-window. */
1810     p_win->video_window =  XCreateSimpleWindow(
1811                                       p_vout->p_sys->p_display,
1812                                       p_win->base_window, 0, 0,
1813                                       p_win->i_width, p_win->i_height,
1814                                       0,
1815                                       BlackPixel( p_vout->p_sys->p_display,
1816                                                   p_vout->p_sys->i_screen ),
1817                                       WhitePixel( p_vout->p_sys->p_display,
1818                                                   p_vout->p_sys->i_screen ) );
1819
1820     XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1821                           BlackPixel( p_vout->p_sys->p_display,
1822                                       p_vout->p_sys->i_screen ) );
1823
1824     XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1825     XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1826                   ExposureMask );
1827
1828     /* make sure the video window will be centered in the next ManageVideo() */
1829     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1830
1831     /* If the cursor was formerly blank than blank it again */
1832     if( !p_vout->p_sys->b_mouse_pointer_visible )
1833     {
1834         ToggleCursor( p_vout );
1835         ToggleCursor( p_vout );
1836     }
1837
1838     /* Do NOT use XFlush here ! */
1839     XSync( p_vout->p_sys->p_display, False );
1840
1841     /* At this stage, the window is open, displayed, and ready to
1842      * receive data */
1843     p_vout->p_sys->p_win = p_win;
1844
1845     return VLC_SUCCESS;
1846 }
1847
1848 /*****************************************************************************
1849  * DestroyWindow: destroy the window
1850  *****************************************************************************
1851  *
1852  *****************************************************************************/
1853 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1854 {
1855     /* Do NOT use XFlush here ! */
1856     XSync( p_vout->p_sys->p_display, False );
1857
1858     if( p_win->video_window != None )
1859         XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1860
1861     XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1862
1863     XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1864     XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1865
1866     /* make sure base window is destroyed before proceeding further */
1867     bool b_destroy_notify = false;
1868     do
1869     {
1870         XEvent      xevent;
1871         XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1872                       SubstructureNotifyMask | StructureNotifyMask, &xevent);
1873         if( (xevent.type == DestroyNotify)
1874                  && (xevent.xmap.window == p_win->base_window) )
1875         {
1876             b_destroy_notify = true;
1877         }
1878     } while( !b_destroy_notify );
1879
1880     vout_ReleaseWindow( p_win->owner_window );
1881 }
1882
1883 /*****************************************************************************
1884  * NewPicture: allocate a picture
1885  *****************************************************************************
1886  * Returns 0 on success, -1 otherwise
1887  *****************************************************************************/
1888 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1889 {
1890 #ifndef MODULE_NAME_IS_glx
1891
1892 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1893     int i_plane;
1894 #endif
1895
1896     /* We know the chroma, allocate a buffer which will be used
1897      * directly by the decoder */
1898     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1899
1900     if( p_pic->p_sys == NULL )
1901     {
1902         return -1;
1903     }
1904
1905 #ifdef MODULE_NAME_IS_xvmc
1906     p_pic->p_sys->p_vout = p_vout;
1907     p_pic->p_sys->xvmc_surf = NULL;
1908     p_pic->p_sys->xxmc_data.decoded = 0;
1909     p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1910     //    p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1911     p_pic->p_sys->nb_display = 0;
1912 #endif
1913
1914     /* Fill in picture_t fields */
1915     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1916                       p_vout->output.i_width, p_vout->output.i_height,
1917                       p_vout->output.i_aspect );
1918
1919 #ifdef HAVE_SYS_SHM_H
1920     if( p_vout->p_sys->i_shm_opcode )
1921     {
1922         /* Create image using XShm extension */
1923         p_pic->p_sys->p_image =
1924             CreateShmImage( p_vout, p_vout->p_sys->p_display,
1925 #   if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1926                             p_vout->p_sys->i_xvport,
1927                             VLC2X11_FOURCC(p_vout->output.i_chroma),
1928 #   else
1929                             p_vout->p_sys->p_visual,
1930                             p_vout->p_sys->i_screen_depth,
1931 #   endif
1932                             &p_pic->p_sys->shminfo,
1933                             p_vout->output.i_width, p_vout->output.i_height );
1934     }
1935
1936     if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1937 #endif /* HAVE_SYS_SHM_H */
1938     {
1939         /* Create image without XShm extension */
1940         p_pic->p_sys->p_image =
1941             CreateImage( p_vout, p_vout->p_sys->p_display,
1942 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1943                          p_vout->p_sys->i_xvport,
1944                          VLC2X11_FOURCC(p_vout->output.i_chroma),
1945                          p_pic->format.i_bits_per_pixel,
1946 #else
1947                          p_vout->p_sys->p_visual,
1948                          p_vout->p_sys->i_screen_depth,
1949                          p_vout->p_sys->i_bytes_per_pixel,
1950 #endif
1951                          p_vout->output.i_width, p_vout->output.i_height );
1952
1953 #ifdef HAVE_SYS_SHM_H
1954         if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1955         {
1956             msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1957             p_vout->p_sys->i_shm_opcode = 0;
1958         }
1959 #endif /* HAVE_SYS_SHM_H */
1960     }
1961
1962     if( p_pic->p_sys->p_image == NULL )
1963     {
1964         free( p_pic->p_sys );
1965         return -1;
1966     }
1967
1968     switch( p_vout->output.i_chroma )
1969     {
1970 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1971         case VLC_FOURCC('I','4','2','0'):
1972         case VLC_FOURCC('Y','V','1','2'):
1973         case VLC_FOURCC('Y','2','1','1'):
1974         case VLC_FOURCC('Y','U','Y','2'):
1975         case VLC_FOURCC('U','Y','V','Y'):
1976         case VLC_FOURCC('R','V','1','5'):
1977         case VLC_FOURCC('R','V','1','6'):
1978         case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1979         case VLC_FOURCC('R','V','3','2'):
1980
1981             for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1982                  i_plane++ )
1983             {
1984                 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1985                     + p_pic->p_sys->p_image->offsets[i_plane];
1986                 p_pic->p[i_plane].i_pitch =
1987                     p_pic->p_sys->p_image->pitches[i_plane];
1988             }
1989             if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1990             {
1991                 /* U and V inverted compared to I420
1992                  * Fixme: this should be handled by the vout core */
1993                 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1994                     + p_pic->p_sys->p_image->offsets[2];
1995                 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1996                     + p_pic->p_sys->p_image->offsets[1];
1997             }
1998
1999             break;
2000
2001 #else
2002         case VLC_FOURCC('R','G','B','2'):
2003         case VLC_FOURCC('R','V','1','6'):
2004         case VLC_FOURCC('R','V','1','5'):
2005         case VLC_FOURCC('R','V','2','4'):
2006         case VLC_FOURCC('R','V','3','2'):
2007
2008             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
2009             p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
2010             p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
2011                                   + p_pic->p_sys->p_image->xoffset;
2012             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
2013
2014             /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
2015              * properly by vout_InitPicture() */
2016             p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
2017                                          * p_pic->p_sys->p_image->width;
2018             break;
2019 #endif
2020
2021         default:
2022             /* Unknown chroma, tell the guy to get lost */
2023             IMAGE_FREE( p_pic->p_sys->p_image );
2024             free( p_pic->p_sys );
2025             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
2026                      p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
2027             p_pic->i_planes = 0;
2028             return -1;
2029     }
2030 #else
2031
2032     VLC_UNUSED(p_vout); VLC_UNUSED(p_pic);
2033
2034 #endif /* !MODULE_NAME_IS_glx */
2035
2036     return 0;
2037 }
2038
2039 /*****************************************************************************
2040  * FreePicture: destroy a picture allocated with NewPicture
2041  *****************************************************************************
2042  * Destroy XImage AND associated data. If using Shm, detach shared memory
2043  * segment from server and process, then free it. The XDestroyImage manpage
2044  * says that both the image structure _and_ the data pointed to by the
2045  * image structure are freed, so no need to free p_image->data.
2046  *****************************************************************************/
2047 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2048 {
2049     /* The order of operations is correct */
2050 #ifdef HAVE_SYS_SHM_H
2051     if( p_vout->p_sys->i_shm_opcode )
2052     {
2053         XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2054         IMAGE_FREE( p_pic->p_sys->p_image );
2055
2056         shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2057         if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2058         {
2059             msg_Err( p_vout, "cannot detach shared memory (%m)" );
2060         }
2061     }
2062     else
2063 #endif
2064     {
2065         IMAGE_FREE( p_pic->p_sys->p_image );
2066     }
2067
2068 #ifdef MODULE_NAME_IS_xvmc
2069     if( p_pic->p_sys->xvmc_surf != NULL )
2070     {
2071         xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2072         p_pic->p_sys->xvmc_surf = NULL;
2073     }
2074 #endif
2075
2076     /* Do NOT use XFlush here ! */
2077     XSync( p_vout->p_sys->p_display, False );
2078
2079     free( p_pic->p_sys );
2080 }
2081
2082 /*****************************************************************************
2083  * ToggleFullScreen: Enable or disable full screen mode
2084  *****************************************************************************
2085  * This function will switch between fullscreen and window mode.
2086  *****************************************************************************/
2087 static void ToggleFullScreen ( vout_thread_t *p_vout )
2088 {
2089     Atom prop;
2090     mwmhints_t mwmhints;
2091     XSetWindowAttributes attributes;
2092
2093 #ifdef HAVE_XINERAMA
2094     int i_d1, i_d2;
2095 #endif
2096
2097     p_vout->b_fullscreen = !p_vout->b_fullscreen;
2098
2099     if( p_vout->b_fullscreen )
2100     {
2101         msg_Dbg( p_vout, "entering fullscreen mode" );
2102
2103         /* Getting current window position */
2104         Window root_win;
2105         Window* child_windows;
2106         unsigned int num_child_windows;
2107         Window parent_win;
2108         Window child_win;
2109         XWindowAttributes win_attr;
2110         int screen_x,screen_y;
2111
2112         XGetWindowAttributes(
2113                 p_vout->p_sys->p_display,
2114                 p_vout->p_sys->p_win->video_window,
2115                 &win_attr);
2116
2117         XQueryTree(
2118                 p_vout->p_sys->p_display,
2119                 p_vout->p_sys->p_win->video_window,
2120                 &root_win,
2121                 &parent_win,
2122                 &child_windows,
2123                 &num_child_windows);
2124         XFree(child_windows);
2125
2126         XTranslateCoordinates(
2127                 p_vout->p_sys->p_display,
2128                 parent_win, win_attr.root,
2129                 win_attr.x,win_attr.y,
2130                 &screen_x,&screen_y,
2131                 &child_win);
2132
2133         msg_Dbg( p_vout, "X %d/%d Y %d/%d", win_attr.x,screen_x,win_attr.y,screen_y);
2134         /* screen_x and screen_y are current position */
2135
2136         p_vout->p_sys->b_altfullscreen =
2137             config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2138
2139         XUnmapWindow( p_vout->p_sys->p_display,
2140                       p_vout->p_sys->p_win->base_window );
2141
2142         p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2143
2144         CreateWindow( p_vout, p_vout->p_sys->p_win );
2145         XDestroyWindow( p_vout->p_sys->p_display,
2146                         p_vout->p_sys->fullscreen_window.video_window );
2147         XReparentWindow( p_vout->p_sys->p_display,
2148                          p_vout->p_sys->original_window.video_window,
2149                          p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2150         p_vout->p_sys->fullscreen_window.video_window =
2151             p_vout->p_sys->original_window.video_window;
2152
2153         /* To my knowledge there are two ways to create a borderless window.
2154          * There's the generic way which is to tell x to bypass the window
2155          * manager, but this creates problems with the focus of other
2156          * applications.
2157          * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2158          * luckily seems to be supported by most window managers. */
2159         if( !p_vout->p_sys->b_altfullscreen )
2160         {
2161             mwmhints.flags = MWM_HINTS_DECORATIONS;
2162             mwmhints.decorations = False;
2163
2164             prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2165                                 False );
2166             XChangeProperty( p_vout->p_sys->p_display,
2167                              p_vout->p_sys->p_win->base_window,
2168                              prop, prop, 32, PropModeReplace,
2169                              (unsigned char *)&mwmhints,
2170                              PROP_MWM_HINTS_ELEMENTS );
2171         }
2172         else
2173         {
2174             /* brute force way to remove decorations */
2175             attributes.override_redirect = True;
2176             XChangeWindowAttributes( p_vout->p_sys->p_display,
2177                                      p_vout->p_sys->p_win->base_window,
2178                                      CWOverrideRedirect,
2179                                      &attributes);
2180
2181             /* Make sure the change is effective */
2182             XReparentWindow( p_vout->p_sys->p_display,
2183                              p_vout->p_sys->p_win->base_window,
2184                              DefaultRootWindow( p_vout->p_sys->p_display ),
2185                              0, 0 );
2186         }
2187
2188         if( p_vout->p_sys->b_net_wm_state_fullscreen )
2189         {
2190             XClientMessageEvent event;
2191
2192             memset( &event, 0, sizeof( XClientMessageEvent ) );
2193
2194             event.type = ClientMessage;
2195             event.message_type = p_vout->p_sys->net_wm_state;
2196             event.display = p_vout->p_sys->p_display;
2197             event.window = p_vout->p_sys->p_win->base_window;
2198             event.format = 32;
2199             event.data.l[ 0 ] = 1; /* set property */
2200             event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2201
2202             XSendEvent( p_vout->p_sys->p_display,
2203                         DefaultRootWindow( p_vout->p_sys->p_display ),
2204                         False, SubstructureRedirectMask,
2205                         (XEvent*)&event );
2206         }
2207
2208 /* "bad fullscreen" - set this to 0. doing fullscreen this way is problematic
2209  * for many reasons and basically fights with the window manager as the wm
2210  * reparents AND vlc goes and reparents - multiple times. don't do it. it just
2211  * makes it more inefficient and less "nice" to the x11 citizenry. this turns
2212  * it off */
2213 #define BADFS 0
2214 /* explicitly asking for focus when you fullscreened is a little silly. the
2215  * window manager SHOULD be handling this itself based on its own focus
2216  * policies. if the user is "using" a given xinerama/xrandr screen or x11
2217  * multihead screen AND vlc wants to fullscreen the wm should also focus it
2218  * as its the only thing on the screen. if vlc fullscreens and its on
2219  * "another monitor" to the one the user is using - this may "steal" the focus
2220  * as really the wm should be deciding if, on fullscreening of a window
2221  * the focus should go there or not, so let the wm decided */
2222 #define APPFOCUS 0
2223         /* Make sure the change is effective */
2224         XReparentWindow( p_vout->p_sys->p_display,
2225                          p_vout->p_sys->p_win->base_window,
2226                          DefaultRootWindow( p_vout->p_sys->p_display ),
2227                          0, 0 );
2228
2229 #ifdef HAVE_XINERAMA
2230         if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2231             XineramaIsActive( p_vout->p_sys->p_display ) )
2232         {
2233             XineramaScreenInfo *screens;   /* infos for xinerama */
2234             int i_num_screens;
2235
2236             msg_Dbg( p_vout, "using XFree Xinerama extension");
2237
2238 #define SCREEN p_vout->p_sys->p_win->i_screen
2239
2240             /* Get Information about Xinerama (num of screens) */
2241             screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2242                                             &i_num_screens );
2243
2244             SCREEN = config_GetInt( p_vout,
2245                                         MODULE_STRING "-xineramascreen" );
2246
2247             /* just check that user has entered a good value,
2248              * otherwise use that screen where window is */
2249             if( SCREEN >= i_num_screens || SCREEN < 0 )
2250             {
2251                 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2252 #define left screens[SCREEN].x_org
2253 #define right left + screens[SCREEN].width
2254 #define top screens[SCREEN].y_org
2255 #define bottom top + screens[SCREEN].height
2256
2257                  for( SCREEN = i_num_screens-1; SCREEN > 0; SCREEN--)
2258                  {
2259                      if( left <= screen_x && screen_x <= right &&
2260                              top <= screen_y && screen_y <= bottom )
2261                          break;
2262                  }
2263 #undef bottom
2264 #undef top
2265 #undef right
2266 #undef left
2267             }
2268
2269             /* Get the X/Y upper left corner coordinate of the above screen */
2270             p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2271             p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2272
2273             /* Set the Height/width to the screen resolution */
2274             p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2275             p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2276
2277             XFree(screens);
2278
2279 #undef SCREEN
2280
2281         }
2282         else
2283 #endif
2284         {
2285             /* The window wasn't necessarily created at the requested size */
2286             p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2287
2288 #ifdef HAVE_XF86VIDMODE
2289             XF86VidModeModeLine mode;
2290             int i_dummy;
2291
2292             if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2293                                         p_vout->p_sys->i_screen, &i_dummy,
2294                                         &mode ) )
2295             {
2296                 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2297                 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2298
2299                 /* move cursor to the middle of the window to prevent
2300                  * unwanted display move if the display is smaller than the
2301                  * full desktop */
2302                 XWarpPointer( p_vout->p_sys->p_display, None,
2303                               p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2304                               mode.hdisplay / 2 , mode.vdisplay / 2 );
2305                 /* force desktop view to upper left corner */
2306                 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2307                                         p_vout->p_sys->i_screen, 0, 0 );
2308             }
2309             else
2310 #endif
2311             {
2312                 p_vout->p_sys->p_win->i_width =
2313                     DisplayWidth( p_vout->p_sys->p_display,
2314                                 p_vout->p_sys->i_screen );
2315                 p_vout->p_sys->p_win->i_height =
2316                     DisplayHeight( p_vout->p_sys->p_display,
2317                                 p_vout->p_sys->i_screen );
2318             }
2319
2320         }
2321
2322         XMoveResizeWindow( p_vout->p_sys->p_display,
2323                            p_vout->p_sys->p_win->base_window,
2324                            p_vout->p_sys->p_win->i_x,
2325                            p_vout->p_sys->p_win->i_y,
2326                            p_vout->p_sys->p_win->i_width,
2327                            p_vout->p_sys->p_win->i_height );
2328
2329 #ifdef HAVE_XSP
2330         EnablePixelDoubling( p_vout );
2331 #endif
2332
2333 #if APPFOCUS // RASTER: let the wm do focus policy
2334         /* Activate the window (give it the focus) */
2335         XClientMessageEvent event;
2336
2337         memset( &event, 0, sizeof( XClientMessageEvent ) );
2338
2339         event.type = ClientMessage;
2340         event.message_type =
2341            XInternAtom( p_vout->p_sys->p_display, "_NET_ACTIVE_WINDOW", False );
2342         event.display = p_vout->p_sys->p_display;
2343         event.window = p_vout->p_sys->p_win->base_window;
2344         event.format = 32;
2345         event.data.l[ 0 ] = 1; /* source indication (1 = from an application */
2346         event.data.l[ 1 ] = 0; /* timestamp */
2347         event.data.l[ 2 ] = 0; /* requestor's currently active window */
2348         /* XXX: window manager would be more likely to obey if we already have
2349          * an active window (and give it to the event), such as an interface */
2350
2351         XSendEvent( p_vout->p_sys->p_display,
2352                     DefaultRootWindow( p_vout->p_sys->p_display ),
2353                     False, SubstructureRedirectMask,
2354                     (XEvent*)&event );
2355 #endif
2356     }
2357     else
2358     {
2359         msg_Dbg( p_vout, "leaving fullscreen mode" );
2360
2361 #ifdef HAVE_XSP
2362         DisablePixelDoubling( p_vout );
2363 #endif
2364
2365         XReparentWindow( p_vout->p_sys->p_display,
2366                          p_vout->p_sys->original_window.video_window,
2367                          p_vout->p_sys->original_window.base_window, 0, 0 );
2368
2369         p_vout->p_sys->fullscreen_window.video_window = None;
2370         DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2371         p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2372
2373         XMapWindow( p_vout->p_sys->p_display,
2374                     p_vout->p_sys->p_win->base_window );
2375     }
2376
2377     /* Unfortunately, using XSync() here is not enough to ensure the
2378      * window has already been mapped because the XMapWindow() request
2379      * has not necessarily been sent directly to our window (remember,
2380      * the call is first redirected to the window manager) */
2381
2382 #if BADFS // RASTER: this is silly... if we have already mapped before
2383     XEvent xevent;
2384     do
2385     {
2386         XWindowEvent( p_vout->p_sys->p_display,
2387                       p_vout->p_sys->p_win->base_window,
2388                       StructureNotifyMask, &xevent );
2389     } while( xevent.type != MapNotify );
2390 #else
2391    XSync(p_vout->p_sys->p_display, False);
2392 #endif
2393
2394     /* Be careful, this can generate a BadMatch error if the window is not
2395      * already mapped by the server (see above) */
2396     XSetInputFocus(p_vout->p_sys->p_display,
2397                    p_vout->p_sys->p_win->base_window,
2398                    RevertToParent,
2399                    CurrentTime);
2400
2401     /* signal that the size needs to be updated */
2402     p_vout->i_changes |= VOUT_SIZE_CHANGE;
2403 }
2404
2405 /*****************************************************************************
2406  * EnableXScreenSaver: enable screen saver
2407  *****************************************************************************
2408  * This function enables the screen saver on a display after it has been
2409  * disabled by XDisableScreenSaver.
2410  * FIXME: what happens if multiple vlc sessions are running at the same
2411  *        time ???
2412  *****************************************************************************/
2413 static void EnableXScreenSaver( vout_thread_t *p_vout )
2414 {
2415 #ifdef DPMSINFO_IN_DPMS_H
2416     int dummy;
2417 #endif
2418
2419     if( p_vout->p_sys->i_ss_timeout )
2420     {
2421         XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2422                          p_vout->p_sys->i_ss_interval,
2423                          p_vout->p_sys->i_ss_blanking,
2424                          p_vout->p_sys->i_ss_exposure );
2425     }
2426
2427     /* Restore DPMS settings */
2428 #ifdef DPMSINFO_IN_DPMS_H
2429     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2430     {
2431         if( p_vout->p_sys->b_ss_dpms )
2432         {
2433             DPMSEnable( p_vout->p_sys->p_display );
2434         }
2435     }
2436 #endif
2437 }
2438
2439 /*****************************************************************************
2440  * DisableXScreenSaver: disable screen saver
2441  *****************************************************************************
2442  * See XEnableXScreenSaver
2443  *****************************************************************************/
2444 static void DisableXScreenSaver( vout_thread_t *p_vout )
2445 {
2446 #ifdef DPMSINFO_IN_DPMS_H
2447     int dummy;
2448 #endif
2449
2450     /* Save screen saver information */
2451     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2452                      &p_vout->p_sys->i_ss_interval,
2453                      &p_vout->p_sys->i_ss_blanking,
2454                      &p_vout->p_sys->i_ss_exposure );
2455
2456     /* Disable screen saver */
2457     if( p_vout->p_sys->i_ss_timeout )
2458     {
2459         XSetScreenSaver( p_vout->p_sys->p_display, 0,
2460                          p_vout->p_sys->i_ss_interval,
2461                          p_vout->p_sys->i_ss_blanking,
2462                          p_vout->p_sys->i_ss_exposure );
2463     }
2464
2465     /* Disable DPMS */
2466 #ifdef DPMSINFO_IN_DPMS_H
2467     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2468     {
2469         CARD16 unused;
2470         /* Save DPMS current state */
2471         DPMSInfo( p_vout->p_sys->p_display, &unused,
2472                   &p_vout->p_sys->b_ss_dpms );
2473         DPMSDisable( p_vout->p_sys->p_display );
2474    }
2475 #endif
2476 }
2477
2478 /*****************************************************************************
2479  * CreateCursor: create a blank mouse pointer
2480  *****************************************************************************/
2481 static void CreateCursor( vout_thread_t *p_vout )
2482 {
2483     XColor cursor_color;
2484
2485     p_vout->p_sys->cursor_pixmap =
2486         XCreatePixmap( p_vout->p_sys->p_display,
2487                        DefaultRootWindow( p_vout->p_sys->p_display ),
2488                        1, 1, 1 );
2489
2490     XParseColor( p_vout->p_sys->p_display,
2491                  XCreateColormap( p_vout->p_sys->p_display,
2492                                   DefaultRootWindow(
2493                                                     p_vout->p_sys->p_display ),
2494                                   DefaultVisual(
2495                                                 p_vout->p_sys->p_display,
2496                                                 p_vout->p_sys->i_screen ),
2497                                   AllocNone ),
2498                  "black", &cursor_color );
2499
2500     p_vout->p_sys->blank_cursor =
2501         XCreatePixmapCursor( p_vout->p_sys->p_display,
2502                              p_vout->p_sys->cursor_pixmap,
2503                              p_vout->p_sys->cursor_pixmap,
2504                              &cursor_color, &cursor_color, 1, 1 );
2505 }
2506
2507 /*****************************************************************************
2508  * DestroyCursor: destroy the blank mouse pointer
2509  *****************************************************************************/
2510 static void DestroyCursor( vout_thread_t *p_vout )
2511 {
2512     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2513 }
2514
2515 /*****************************************************************************
2516  * ToggleCursor: hide or show the mouse pointer
2517  *****************************************************************************
2518  * This function hides the X pointer if it is visible by setting the pointer
2519  * sprite to a blank one. To show it again, we disable the sprite.
2520  *****************************************************************************/
2521 static void ToggleCursor( vout_thread_t *p_vout )
2522 {
2523     if( p_vout->p_sys->b_mouse_pointer_visible )
2524     {
2525         XDefineCursor( p_vout->p_sys->p_display,
2526                        p_vout->p_sys->p_win->base_window,
2527                        p_vout->p_sys->blank_cursor );
2528         p_vout->p_sys->b_mouse_pointer_visible = 0;
2529     }
2530     else
2531     {
2532         XUndefineCursor( p_vout->p_sys->p_display,
2533                          p_vout->p_sys->p_win->base_window );
2534         p_vout->p_sys->b_mouse_pointer_visible = 1;
2535     }
2536 }
2537
2538 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2539 /*****************************************************************************
2540  * XVideoGetPort: get YUV12 port
2541  *****************************************************************************/
2542 static int XVideoGetPort( vout_thread_t *p_vout,
2543                           vlc_fourcc_t i_chroma, picture_heap_t *p_heap )
2544 {
2545     XvAdaptorInfo *p_adaptor;
2546     unsigned int i;
2547     unsigned int i_adaptor, i_num_adaptors;
2548     int i_requested_adaptor;
2549     int i_selected_port;
2550
2551     switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2552     {
2553         case Success:
2554             break;
2555
2556         case XvBadExtension:
2557             msg_Warn( p_vout, "XvBadExtension" );
2558             return -1;
2559
2560         case XvBadAlloc:
2561             msg_Warn( p_vout, "XvBadAlloc" );
2562             return -1;
2563
2564         default:
2565             msg_Warn( p_vout, "XvQueryExtension failed" );
2566             return -1;
2567     }
2568
2569     switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2570                              DefaultRootWindow( p_vout->p_sys->p_display ),
2571                              &i_num_adaptors, &p_adaptor ) )
2572     {
2573         case Success:
2574             break;
2575
2576         case XvBadExtension:
2577             msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2578             return -1;
2579
2580         case XvBadAlloc:
2581             msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2582             return -1;
2583
2584         default:
2585             msg_Warn( p_vout, "XvQueryAdaptors failed" );
2586             return -1;
2587     }
2588
2589     i_selected_port = -1;
2590 #ifdef MODULE_NAME_IS_xvmc
2591     i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2592 #else
2593     i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2594 #endif
2595     for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2596     {
2597         XvImageFormatValues *p_formats;
2598         int i_format, i_num_formats;
2599         int i_port;
2600
2601         /* If we requested an adaptor and it's not this one, we aren't
2602          * interested */
2603         if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2604         {
2605             continue;
2606         }
2607
2608         /* If the adaptor doesn't have the required properties, skip it */
2609         if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2610             !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2611         {
2612             continue;
2613         }
2614
2615         /* Check that adaptor supports our requested format... */
2616         p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2617                                         p_adaptor[i_adaptor].base_id,
2618                                         &i_num_formats );
2619
2620         for( i_format = 0;
2621              i_format < i_num_formats && ( i_selected_port == -1 );
2622              i_format++ )
2623         {
2624             XvAttribute     *p_attr;
2625             int             i_attr, i_num_attributes;
2626             Atom            autopaint = None, colorkey = None;
2627
2628             /* If this is not the format we want, or at least a
2629              * similar one, forget it */
2630             if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2631             {
2632                 continue;
2633             }
2634
2635             /* Look for the first available port supporting this format */
2636             for( i_port = p_adaptor[i_adaptor].base_id;
2637                  ( i_port < (int)(p_adaptor[i_adaptor].base_id
2638                                    + p_adaptor[i_adaptor].num_ports) )
2639                    && ( i_selected_port == -1 );
2640                  i_port++ )
2641             {
2642                 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2643                      == Success )
2644                 {
2645                     i_selected_port = i_port;
2646                     p_heap->i_chroma = p_formats[ i_format ].id;
2647 #if XvVersion > 2 || ( XvVersion == 2 && XvRevision >= 2 )
2648                     p_heap->i_rmask = p_formats[ i_format ].red_mask;
2649                     p_heap->i_gmask = p_formats[ i_format ].green_mask;
2650                     p_heap->i_bmask = p_formats[ i_format ].blue_mask;
2651 #endif
2652                 }
2653             }
2654
2655             /* If no free port was found, forget it */
2656             if( i_selected_port == -1 )
2657             {
2658                 continue;
2659             }
2660
2661             /* If we found a port, print information about it */
2662             msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2663                      i_adaptor, i_selected_port, p_formats[ i_format ].id,
2664                      (char *)&p_formats[ i_format ].id,
2665                      ( p_formats[ i_format ].format == XvPacked ) ?
2666                          "packed" : "planar" );
2667
2668             /* Use XV_AUTOPAINT_COLORKEY if supported, otherwise we will
2669              * manually paint the colour key */
2670             p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2671                                             i_selected_port,
2672                                             &i_num_attributes );
2673
2674             for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2675             {
2676                 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2677                 {
2678                     autopaint = XInternAtom( p_vout->p_sys->p_display,
2679                                              "XV_AUTOPAINT_COLORKEY", False );
2680                     XvSetPortAttribute( p_vout->p_sys->p_display,
2681                                         i_selected_port, autopaint, 1 );
2682                 }
2683                 if( !strcmp( p_attr[i_attr].name, "XV_COLORKEY" ) )
2684                 {
2685                     /* Find out the default colour key */
2686                     colorkey = XInternAtom( p_vout->p_sys->p_display,
2687                                             "XV_COLORKEY", False );
2688                     XvGetPortAttribute( p_vout->p_sys->p_display,
2689                                         i_selected_port, colorkey,
2690                                         &p_vout->p_sys->i_colourkey );
2691                 }
2692             }
2693             p_vout->p_sys->b_paint_colourkey =
2694                 autopaint == None && colorkey != None;
2695
2696             if( p_attr != NULL )
2697             {
2698                 XFree( p_attr );
2699             }
2700         }
2701
2702         if( p_formats != NULL )
2703         {
2704             XFree( p_formats );
2705         }
2706
2707     }
2708
2709     if( i_num_adaptors > 0 )
2710     {
2711         XvFreeAdaptorInfo( p_adaptor );
2712     }
2713
2714     if( i_selected_port == -1 )
2715     {
2716         int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2717         if( i_requested_adaptor == -1 )
2718         {
2719             msg_Warn( p_vout, "no free XVideo port found for format "
2720                       "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2721         }
2722         else
2723         {
2724             msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2725                       "XVideo port for format 0x%.8x (%4.4s)",
2726                       i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2727         }
2728     }
2729
2730     return i_selected_port;
2731 }
2732
2733 /*****************************************************************************
2734  * XVideoReleasePort: release YUV12 port
2735  *****************************************************************************/
2736 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2737 {
2738     XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2739 }
2740 #endif
2741
2742 /*****************************************************************************
2743  * InitDisplay: open and initialize X11 device
2744  *****************************************************************************
2745  * Create a window according to video output given size, and set other
2746  * properties according to the display properties.
2747  *****************************************************************************/
2748 static int InitDisplay( vout_thread_t *p_vout )
2749 {
2750 #ifdef MODULE_NAME_IS_x11
2751     XPixmapFormatValues *       p_formats;                 /* pixmap formats */
2752     XVisualInfo *               p_xvisual;            /* visuals information */
2753     XVisualInfo                 xvisual_template;         /* visual template */
2754     int                         i_count, i;                    /* array size */
2755 #endif
2756
2757 #ifdef HAVE_SYS_SHM_H
2758     p_vout->p_sys->i_shm_opcode = 0;
2759
2760     if( config_GetInt( p_vout, MODULE_STRING "-shm" ) > 0 )
2761     {
2762         int major, evt, err;
2763
2764         if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2765                              &evt, &err )
2766          && XShmQueryExtension( p_vout->p_sys->p_display ) )
2767             p_vout->p_sys->i_shm_opcode = major;
2768
2769         if( p_vout->p_sys->i_shm_opcode )
2770         {
2771             int minor;
2772             Bool pixmaps;
2773
2774             XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2775                               &pixmaps );
2776             msg_Dbg( p_vout, "XShm video extension v%d.%d "
2777                      "(with%s pixmaps, opcode: %d)",
2778                      major, minor, pixmaps ? "" : "out",
2779                      p_vout->p_sys->i_shm_opcode );
2780         }
2781         else
2782             msg_Warn( p_vout, "XShm video extension not available" );
2783     }
2784     else
2785         msg_Dbg( p_vout, "XShm video extension disabled" );
2786 #endif
2787
2788 #ifdef MODULE_NAME_IS_xvideo
2789     /* XXX The brightness and contrast values should be read from environment
2790      * XXX variables... */
2791 #if 0
2792     XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2793     XVideoSetAttribute( p_vout, "XV_CONTRAST",   0.5 );
2794 #endif
2795 #endif
2796
2797 #ifdef MODULE_NAME_IS_x11
2798     /* Initialize structure */
2799     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2800
2801     /* Get screen depth */
2802     p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2803                                                    p_vout->p_sys->i_screen );
2804     switch( p_vout->p_sys->i_screen_depth )
2805     {
2806     case 8:
2807         /*
2808          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2809          */
2810         xvisual_template.screen =   p_vout->p_sys->i_screen;
2811         xvisual_template.class =    DirectColor;
2812         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2813                                     VisualScreenMask | VisualClassMask,
2814                                     &xvisual_template, &i_count );
2815         if( p_xvisual == NULL )
2816         {
2817             msg_Err( p_vout, "no PseudoColor visual available" );
2818             return VLC_EGENERIC;
2819         }
2820         p_vout->p_sys->i_bytes_per_pixel = 1;
2821         p_vout->output.pf_setpalette = SetPalette;
2822         break;
2823     case 15:
2824     case 16:
2825     case 24:
2826     default:
2827         /*
2828          * Screen depth is higher than 8bpp. TrueColor visual is used.
2829          */
2830         xvisual_template.screen =   p_vout->p_sys->i_screen;
2831         xvisual_template.class =    TrueColor;
2832 /* In some cases, we get a truecolor class adaptor that has a different
2833    color depth. So try to get a real true color one first */
2834         xvisual_template.depth =    p_vout->p_sys->i_screen_depth;
2835
2836         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2837                                     VisualScreenMask | VisualClassMask |
2838                                     VisualDepthMask,
2839                                     &xvisual_template, &i_count );
2840         if( p_xvisual == NULL )
2841         {
2842             msg_Warn( p_vout, "No screen matching the required color depth" );
2843             p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2844                                     VisualScreenMask | VisualClassMask,
2845                                     &xvisual_template, &i_count );
2846             if( p_xvisual == NULL )
2847             {
2848
2849                 msg_Err( p_vout, "no TrueColor visual available" );
2850                 return VLC_EGENERIC;
2851             }
2852         }
2853
2854         p_vout->output.i_rmask = p_xvisual->red_mask;
2855         p_vout->output.i_gmask = p_xvisual->green_mask;
2856         p_vout->output.i_bmask = p_xvisual->blue_mask;
2857
2858         /* There is no difference yet between 3 and 4 Bpp. The only way
2859          * to find the actual number of bytes per pixel is to list supported
2860          * pixmap formats. */
2861         p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2862         p_vout->p_sys->i_bytes_per_pixel = 0;
2863
2864         for( i = 0; i < i_count; i++ )
2865         {
2866             /* Under XFree4.0, the list contains pixmap formats available
2867              * through all video depths ; so we have to check against current
2868              * depth. */
2869             if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2870             {
2871                 if( p_formats[i].bits_per_pixel / 8
2872                         > (int)p_vout->p_sys->i_bytes_per_pixel )
2873                 {
2874                     p_vout->p_sys->i_bytes_per_pixel =
2875                         p_formats[i].bits_per_pixel / 8;
2876                 }
2877             }
2878         }
2879         if( p_formats ) XFree( p_formats );
2880
2881         break;
2882     }
2883     p_vout->p_sys->p_visual = p_xvisual->visual;
2884     XFree( p_xvisual );
2885 #endif
2886
2887     return VLC_SUCCESS;
2888 }
2889
2890 #ifndef MODULE_NAME_IS_glx
2891
2892 #ifdef HAVE_SYS_SHM_H
2893 /*****************************************************************************
2894  * CreateShmImage: create an XImage or XvImage using shared memory extension
2895  *****************************************************************************
2896  * Prepare an XImage or XvImage for display function.
2897  * The order of the operations respects the recommandations of the mit-shm
2898  * document by J.Corbet and K.Packard. Most of the parameters were copied from
2899  * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2900  *****************************************************************************/
2901 IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2902                                     Display* p_display, EXTRA_ARGS_SHM,
2903                                     int i_width, int i_height )
2904 {
2905     IMAGE_TYPE *p_image;
2906     Status result;
2907
2908     /* Create XImage / XvImage */
2909 #ifdef MODULE_NAME_IS_xvideo
2910     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2911                                 i_width, i_height, p_shm );
2912 #elif defined(MODULE_NAME_IS_xvmc)
2913     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2914                                 i_width, i_height, p_shm );
2915 #else
2916     p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2917                                p_shm, i_width, i_height );
2918 #endif
2919     if( p_image == NULL )
2920     {
2921         msg_Err( p_vout, "image creation failed" );
2922         return NULL;
2923     }
2924
2925     /* For too bug image, the buffer returned is sometimes too low, prevent
2926      * VLC to segfault because of it
2927      * FIXME is it normal ? Is there a way to detect it
2928      * before (XvQueryBestSize did not) ? */
2929     if( p_image->width < i_width || p_image->height < i_height )
2930     {
2931         msg_Err( p_vout, "cannot allocate shared image data with the right size "
2932                          "(%dx%d instead of %dx%d)",
2933                          p_image->width, p_image->height,
2934                          i_width, i_height );
2935         IMAGE_FREE( p_image );
2936         return NULL;
2937     }
2938
2939     /* Allocate shared memory segment - 0776 set the access permission
2940      * rights (like umask), they are not yet supported by all X servers */
2941     p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2942     if( p_shm->shmid < 0 )
2943     {
2944         msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2945         IMAGE_FREE( p_image );
2946         return NULL;
2947     }
2948
2949     /* Attach shared memory segment to process (read/write) */
2950     p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2951     if(! p_shm->shmaddr )
2952     {
2953         msg_Err( p_vout, "cannot attach shared memory (%m)" );
2954         IMAGE_FREE( p_image );
2955         shmctl( p_shm->shmid, IPC_RMID, 0 );
2956         return NULL;
2957     }
2958
2959     /* Read-only data. We won't be using XShmGetImage */
2960     p_shm->readOnly = True;
2961
2962     /* Attach shared memory segment to X server */
2963     XSynchronize( p_display, True );
2964     i_shm_major = p_vout->p_sys->i_shm_opcode;
2965     result = XShmAttach( p_display, p_shm );
2966     if( result == False || !i_shm_major )
2967     {
2968         msg_Err( p_vout, "cannot attach shared memory to X server" );
2969         IMAGE_FREE( p_image );
2970         shmctl( p_shm->shmid, IPC_RMID, 0 );
2971         shmdt( p_shm->shmaddr );
2972         return NULL;
2973     }
2974     XSynchronize( p_display, False );
2975
2976     /* Send image to X server. This instruction is required, since having
2977      * built a Shm XImage and not using it causes an error on XCloseDisplay,
2978      * and remember NOT to use XFlush ! */
2979     XSync( p_display, False );
2980
2981 #if 0
2982     /* Mark the shm segment to be removed when there are no more
2983      * attachements, so it is automatic on process exit or after shmdt */
2984     shmctl( p_shm->shmid, IPC_RMID, 0 );
2985 #endif
2986
2987     return p_image;
2988 }
2989 #endif
2990
2991 /*****************************************************************************
2992  * CreateImage: create an XImage or XvImage
2993  *****************************************************************************
2994  * Create a simple image used as a buffer.
2995  *****************************************************************************/
2996 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2997                                  Display *p_display, EXTRA_ARGS,
2998                                  int i_width, int i_height )
2999 {
3000     uint8_t *    p_data;                          /* image data storage zone */
3001     IMAGE_TYPE *p_image;
3002 #ifdef MODULE_NAME_IS_x11
3003     int         i_quantum;                     /* XImage quantum (see below) */
3004     int         i_bytes_per_line;
3005 #endif
3006
3007     /* Allocate memory for image */
3008 #ifdef MODULE_NAME_IS_xvideo
3009     p_data = malloc( i_width * i_height * i_bits_per_pixel / 8 );
3010 #elif defined(MODULE_NAME_IS_x11)
3011     i_bytes_per_line = i_width * i_bytes_per_pixel;
3012     p_data = malloc( i_bytes_per_line * i_height );
3013 #endif
3014     if( !p_data )
3015         return NULL;
3016
3017 #ifdef MODULE_NAME_IS_x11
3018     /* Optimize the quantum of a scanline regarding its size - the quantum is
3019        a diviser of the number of bits between the start of two scanlines. */
3020     if( i_bytes_per_line & 0xf )
3021     {
3022         i_quantum = 0x8;
3023     }
3024     else if( i_bytes_per_line & 0x10 )
3025     {
3026         i_quantum = 0x10;
3027     }
3028     else
3029     {
3030         i_quantum = 0x20;
3031     }
3032 #endif
3033
3034     /* Create XImage. p_data will be automatically freed */
3035 #ifdef MODULE_NAME_IS_xvideo
3036     p_image = XvCreateImage( p_display, i_xvport, i_chroma,
3037                              (char *)p_data, i_width, i_height );
3038 #elif defined(MODULE_NAME_IS_x11)
3039     p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
3040                             (char *)p_data, i_width, i_height, i_quantum, 0 );
3041 #endif
3042     if( p_image == NULL )
3043     {
3044         msg_Err( p_vout, "XCreateImage() failed" );
3045         free( p_data );
3046         return NULL;
3047     }
3048
3049     return p_image;
3050 }
3051
3052 #endif
3053 /*****************************************************************************
3054  * X11ErrorHandler: replace error handler so we can intercept some of them
3055  *****************************************************************************/
3056 static int X11ErrorHandler( Display * display, XErrorEvent * event )
3057 {
3058     char txt[1024];
3059
3060     XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
3061     fprintf( stderr,
3062              "[????????] x11 video output error: X11 request %u.%u failed "
3063               "with error code %u:\n %s\n",
3064              event->request_code, event->minor_code, event->error_code, txt );
3065
3066     switch( event->request_code )
3067     {
3068     case X_SetInputFocus:
3069         /* Ignore errors on XSetInputFocus()
3070          * (they happen when a window is not yet mapped) */
3071         return 0;
3072     }
3073
3074 #ifdef HAVE_SYS_SHM_H
3075     if( event->request_code == i_shm_major ) /* MIT-SHM */
3076     {
3077         fprintf( stderr,
3078                  "[????????] x11 video output notice:"
3079                  " buggy X11 server claims shared memory\n"
3080                  "[????????] x11 video output notice:"
3081                  " support though it does not work (OpenSSH?)\n" );
3082         return i_shm_major = 0;
3083     }
3084 #endif
3085
3086 #ifndef HAVE_OSSO
3087     XSetErrorHandler(NULL);
3088     return (XSetErrorHandler(X11ErrorHandler))( display, event );
3089 #else
3090     /* Work-around Maemo Xvideo bug */
3091     return 0;
3092 #endif
3093 }
3094
3095 #ifdef MODULE_NAME_IS_x11
3096 /*****************************************************************************
3097  * SetPalette: sets an 8 bpp palette
3098  *****************************************************************************
3099  * This function sets the palette given as an argument. It does not return
3100  * anything, but could later send information on which colors it was unable
3101  * to set.
3102  *****************************************************************************/
3103 static void SetPalette( vout_thread_t *p_vout,
3104                         uint16_t *red, uint16_t *green, uint16_t *blue )
3105 {
3106     int i;
3107     XColor p_colors[255];
3108
3109     /* allocate palette */
3110     for( i = 0; i < 255; i++ )
3111     {
3112         /* kludge: colors are indexed reversely because color 255 seems
3113          * to be reserved for black even if we try to set it to white */
3114         p_colors[ i ].pixel = 255 - i;
3115         p_colors[ i ].pad   = 0;
3116         p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
3117         p_colors[ i ].red   = red[ 255 - i ];
3118         p_colors[ i ].blue  = blue[ 255 - i ];
3119         p_colors[ i ].green = green[ 255 - i ];
3120     }
3121
3122     XStoreColors( p_vout->p_sys->p_display,
3123                   p_vout->p_sys->colormap, p_colors, 255 );
3124 }
3125 #endif
3126
3127 /*****************************************************************************
3128  * Control: control facility for the vout
3129  *****************************************************************************/
3130 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
3131 {
3132     bool b_arg;
3133     unsigned int i_width, i_height;
3134
3135     switch( i_query )
3136     {
3137         case VOUT_SET_SIZE:
3138             if( p_vout->p_sys->p_win->owner_window )
3139                 return vout_ControlWindow( p_vout->p_sys->p_win->owner_window,
3140                                            i_query, args);
3141
3142             i_width  = va_arg( args, unsigned int );
3143             i_height = va_arg( args, unsigned int );
3144             if( !i_width ) i_width = p_vout->i_window_width;
3145             if( !i_height ) i_height = p_vout->i_window_height;
3146
3147 #ifdef MODULE_NAME_IS_xvmc
3148             xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3149 #endif
3150             /* Update dimensions */
3151             XResizeWindow( p_vout->p_sys->p_display,
3152                            p_vout->p_sys->p_win->base_window,
3153                            i_width, i_height );
3154 #ifdef MODULE_NAME_IS_xvmc
3155             xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3156 #endif
3157             return VLC_SUCCESS;
3158
3159         case VOUT_SET_STAY_ON_TOP:
3160             if( p_vout->p_sys->p_win->owner_window )
3161                 return vout_ControlWindow( p_vout->p_sys->p_win->owner_window,
3162                                            i_query, args);
3163
3164             b_arg = (bool) va_arg( args, int );
3165 #ifdef MODULE_NAME_IS_xvmc
3166             xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3167 #endif
3168             WindowOnTop( p_vout, b_arg );
3169 #ifdef MODULE_NAME_IS_xvmc
3170             xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3171 #endif
3172             return VLC_SUCCESS;
3173
3174        default:
3175             return VLC_EGENERIC;
3176     }
3177 }
3178
3179 /*****************************************************************************
3180  * TestNetWMSupport: tests for Extended Window Manager Hints support
3181  *****************************************************************************/
3182 static void TestNetWMSupport( vout_thread_t *p_vout )
3183 {
3184     int i_ret, i_format;
3185     unsigned long i, i_items, i_bytesafter;
3186     Atom net_wm_supported;
3187     union { Atom *p_atom; unsigned char *p_char; } p_args;
3188
3189     p_args.p_atom = NULL;
3190
3191     p_vout->p_sys->b_net_wm_state_fullscreen =
3192     p_vout->p_sys->b_net_wm_state_above =
3193     p_vout->p_sys->b_net_wm_state_below =
3194     p_vout->p_sys->b_net_wm_state_stays_on_top =
3195         false;
3196
3197     net_wm_supported =
3198         XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3199
3200     i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3201                                 DefaultRootWindow( p_vout->p_sys->p_display ),
3202                                 net_wm_supported,
3203                                 0, 16384, False, AnyPropertyType,
3204                                 &net_wm_supported,
3205                                 &i_format, &i_items, &i_bytesafter,
3206                                 (unsigned char **)&p_args );
3207
3208     if( i_ret != Success || i_items == 0 ) return;
3209
3210     msg_Dbg( p_vout, "Window manager supports NetWM" );
3211
3212     p_vout->p_sys->net_wm_state =
3213         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3214     p_vout->p_sys->net_wm_state_fullscreen =
3215         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3216                      False );
3217     p_vout->p_sys->net_wm_state_above =
3218         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3219     p_vout->p_sys->net_wm_state_below =
3220         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3221     p_vout->p_sys->net_wm_state_stays_on_top =
3222         XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3223                      False );
3224
3225     for( i = 0; i < i_items; i++ )
3226     {
3227         if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3228         {
3229             msg_Dbg( p_vout,
3230                      "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3231             p_vout->p_sys->b_net_wm_state_fullscreen = true;
3232         }
3233         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3234         {
3235             msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3236             p_vout->p_sys->b_net_wm_state_above = true;
3237         }
3238         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3239         {
3240             msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3241             p_vout->p_sys->b_net_wm_state_below = true;
3242         }
3243         else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3244         {
3245             msg_Dbg( p_vout,
3246                      "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3247             p_vout->p_sys->b_net_wm_state_stays_on_top = true;
3248         }
3249     }
3250
3251     XFree( p_args.p_atom );
3252 }
3253
3254 /*****************************************************************************
3255  * Key events handling
3256  *****************************************************************************/
3257 static const struct
3258 {
3259     int i_x11key;
3260     int i_vlckey;
3261
3262 } x11keys_to_vlckeys[] =
3263 {
3264     { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3265     { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3266     { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3267     { XK_F12, KEY_F12 },
3268
3269     { XK_Return, KEY_ENTER },
3270     { XK_KP_Enter, KEY_ENTER },
3271     { XK_space, KEY_SPACE },
3272     { XK_Escape, KEY_ESC },
3273
3274     { XK_Menu, KEY_MENU },
3275     { XK_Left, KEY_LEFT },
3276     { XK_Right, KEY_RIGHT },
3277     { XK_Up, KEY_UP },
3278     { XK_Down, KEY_DOWN },
3279
3280     { XK_Home, KEY_HOME },
3281     { XK_End, KEY_END },
3282     { XK_Page_Up, KEY_PAGEUP },
3283     { XK_Page_Down, KEY_PAGEDOWN },
3284
3285     { XK_Insert, KEY_INSERT },
3286     { XK_Delete, KEY_DELETE },
3287     { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3288     { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3289     { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3290     { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3291     { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3292     { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3293     { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3294
3295     { 0, 0 }
3296 };
3297
3298 static int ConvertKey( int i_key )
3299 {
3300     int i;
3301
3302     for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3303     {
3304         if( x11keys_to_vlckeys[i].i_x11key == i_key )
3305         {
3306             return x11keys_to_vlckeys[i].i_vlckey;
3307         }
3308     }
3309
3310     return 0;
3311 }
3312
3313 /*****************************************************************************
3314  * WindowOnTop: Switches the "always on top" state of the video window.
3315  *****************************************************************************/
3316 static int WindowOnTop( vout_thread_t *p_vout, bool b_on_top )
3317 {
3318     if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3319     {
3320         XClientMessageEvent event;
3321
3322         memset( &event, 0, sizeof( XClientMessageEvent ) );
3323
3324         event.type = ClientMessage;
3325         event.message_type = p_vout->p_sys->net_wm_state;
3326         event.display = p_vout->p_sys->p_display;
3327         event.window = p_vout->p_sys->p_win->base_window;
3328         event.format = 32;
3329         event.data.l[ 0 ] = b_on_top; /* set property */
3330         event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3331
3332         XSendEvent( p_vout->p_sys->p_display,
3333                     DefaultRootWindow( p_vout->p_sys->p_display ),
3334                     False, SubstructureRedirectMask,
3335                     (XEvent*)&event );
3336     }
3337
3338     /* use _NET_WM_STATE_ABOVE if window manager
3339      * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3340     else if( p_vout->p_sys->b_net_wm_state_above )
3341     {
3342         XClientMessageEvent event;
3343
3344         memset( &event, 0, sizeof( XClientMessageEvent ) );
3345
3346         event.type = ClientMessage;
3347         event.message_type = p_vout->p_sys->net_wm_state;
3348         event.display = p_vout->p_sys->p_display;
3349         event.window = p_vout->p_sys->p_win->base_window;
3350         event.format = 32;
3351         event.data.l[ 0 ] = b_on_top; /* set property */
3352         event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3353
3354         XSendEvent( p_vout->p_sys->p_display,
3355                     DefaultRootWindow( p_vout->p_sys->p_display ),
3356                     False, SubstructureRedirectMask,
3357                     (XEvent*)&event );
3358     }
3359
3360     return VLC_SUCCESS;
3361 }