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