1 /*****************************************************************************
2 * xcommon.c: Functions common to the X11 and XVideo plugins
3 *****************************************************************************
4 * Copyright (C) 1998-2006 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
40 #ifdef HAVE_MACHINE_PARAM_H
42 # include <machine/param.h>
43 # include <sys/types.h> /* typedef ushort */
48 # include <netinet/in.h> /* BSD: struct in_addr */
52 # include <sys/shm.h> /* shmget(), shmctl() */
56 #include <X11/Xproto.h>
58 #include <X11/Xutil.h>
59 #include <X11/keysym.h>
61 # include <X11/extensions/XShm.h>
63 #ifdef DPMSINFO_IN_DPMS_H
64 # include <X11/extensions/dpms.h>
67 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
68 # include <X11/extensions/Xv.h>
69 # include <X11/extensions/Xvlib.h>
72 #ifdef MODULE_NAME_IS_glx
77 # include <X11/extensions/Xinerama.h>
80 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
81 # include <X11/extensions/xf86vmode.h>
84 #ifdef MODULE_NAME_IS_xvmc
85 # include <X11/extensions/vldXvMC.h>
86 # include "../../codec/xvmc/accel_xvmc.h"
91 /*****************************************************************************
93 *****************************************************************************/
94 int E_(Activate) ( vlc_object_t * );
95 void E_(Deactivate) ( vlc_object_t * );
97 static int InitVideo ( vout_thread_t * );
98 static void EndVideo ( vout_thread_t * );
99 static void DisplayVideo ( vout_thread_t *, picture_t * );
100 static int ManageVideo ( vout_thread_t * );
101 static int Control ( vout_thread_t *, int, va_list );
103 static int InitDisplay ( vout_thread_t * );
105 static int CreateWindow ( vout_thread_t *, x11_window_t * );
106 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
108 static int NewPicture ( vout_thread_t *, picture_t * );
109 static void FreePicture ( vout_thread_t *, picture_t * );
111 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
112 Display *, EXTRA_ARGS, int, int );
113 #ifdef HAVE_SYS_SHM_H
114 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
115 Display *, EXTRA_ARGS_SHM, int, int );
116 static vlc_bool_t b_shm = VLC_TRUE;
119 static void ToggleFullScreen ( vout_thread_t * );
121 static void EnableXScreenSaver ( vout_thread_t * );
122 static void DisableXScreenSaver ( vout_thread_t * );
124 static void CreateCursor ( vout_thread_t * );
125 static void DestroyCursor ( vout_thread_t * );
126 static void ToggleCursor ( vout_thread_t * );
128 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
129 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
130 static void XVideoReleasePort( vout_thread_t *, int );
133 #ifdef MODULE_NAME_IS_x11
134 static void SetPalette ( vout_thread_t *,
135 uint16_t *, uint16_t *, uint16_t * );
138 #ifdef MODULE_NAME_IS_xvmc
139 static void RenderVideo ( vout_thread_t *, picture_t * );
140 static int xvmc_check_yv12( Display *display, XvPortID port );
141 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
144 static void TestNetWMSupport( vout_thread_t * );
145 static int ConvertKey( int );
147 static int WindowOnTop( vout_thread_t *, vlc_bool_t );
149 static int X11ErrorHandler( Display *, XErrorEvent * );
151 /*****************************************************************************
152 * Activate: allocate X11 video thread output method
153 *****************************************************************************
154 * This function allocate and initialize a X11 vout method. It uses some of the
155 * vout properties to choose the window size, and change them according to the
156 * actual properties of the display.
157 *****************************************************************************/
158 int E_(Activate) ( vlc_object_t *p_this )
160 vout_thread_t *p_vout = (vout_thread_t *)p_this;
163 #if defined(MODULE_NAME_IS_xvmc)
166 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
168 vlc_fourcc_t i_chroma = 0;
169 vlc_bool_t b_chroma = 0;
172 p_vout->pf_init = InitVideo;
173 p_vout->pf_end = EndVideo;
174 p_vout->pf_manage = ManageVideo;
175 #ifdef MODULE_NAME_IS_xvmc
176 p_vout->pf_render = RenderVideo;
178 p_vout->pf_render = NULL;
180 p_vout->pf_display = DisplayVideo;
181 p_vout->pf_control = Control;
183 /* Allocate structure */
184 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
185 if( p_vout->p_sys == NULL )
187 msg_Err( p_vout, "out of memory" );
191 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
193 /* Open display, using the "display" config variable or the DISPLAY
194 * environment variable */
195 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
197 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
199 if( p_vout->p_sys->p_display == NULL ) /* error */
201 msg_Err( p_vout, "cannot open display %s",
202 XDisplayName( psz_display ) );
203 free( p_vout->p_sys );
204 if( psz_display ) free( psz_display );
207 if( psz_display ) free( psz_display );
209 /* Replace error handler so we can intercept some non-fatal errors */
210 XSetErrorHandler( X11ErrorHandler );
212 /* Get a screen ID matching the XOpenDisplay return value */
213 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
215 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
216 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
219 if( strlen( psz_chroma ) >= 4 )
221 /* Do not use direct assignment because we are not sure of the
223 memcpy(&i_chroma, psz_chroma, 4);
232 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
233 i_chroma, (char*)&i_chroma );
237 i_chroma = p_vout->render.i_chroma;
240 /* Check that we have access to an XVideo port providing this chroma */
241 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
242 &p_vout->output.i_chroma );
243 if( p_vout->p_sys->i_xvport < 0 )
245 /* If a specific chroma format was requested, then we don't try to
246 * be cleverer than the user. He knew pretty well what he wanted. */
249 XCloseDisplay( p_vout->p_sys->p_display );
250 free( p_vout->p_sys );
254 /* It failed, but it's not completely lost ! We try to open an
255 * XVideo port for an YUY2 picture. We'll need to do an YUV
256 * conversion, but at least it has got scaling. */
257 p_vout->p_sys->i_xvport =
258 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
259 &p_vout->output.i_chroma );
260 if( p_vout->p_sys->i_xvport < 0 )
262 /* It failed, but it's not completely lost ! We try to open an
263 * XVideo port for a simple 16bpp RGB picture. We'll need to do
264 * an YUV conversion, but at least it has got scaling. */
265 p_vout->p_sys->i_xvport =
266 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
267 &p_vout->output.i_chroma );
268 if( p_vout->p_sys->i_xvport < 0 )
270 XCloseDisplay( p_vout->p_sys->p_display );
271 free( p_vout->p_sys );
276 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
279 /* Create blank cursor (for mouse cursor autohiding) */
280 p_vout->p_sys->i_time_mouse_last_moved = mdate();
281 p_vout->p_sys->b_mouse_pointer_visible = 1;
282 CreateCursor( p_vout );
284 /* Set main window's size */
285 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
286 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
287 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
288 /* Spawn base window - this window will include the video output window,
289 * but also command buttons, subtitles and other indicators */
290 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
292 msg_Err( p_vout, "cannot create X11 window" );
293 DestroyCursor( p_vout );
294 XCloseDisplay( p_vout->p_sys->p_display );
295 free( p_vout->p_sys );
299 /* Open and initialize device. */
300 if( InitDisplay( p_vout ) )
302 msg_Err( p_vout, "cannot initialize X11 display" );
303 DestroyCursor( p_vout );
304 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
305 XCloseDisplay( p_vout->p_sys->p_display );
306 free( p_vout->p_sys );
310 /* Disable screen saver */
311 DisableXScreenSaver( p_vout );
314 p_vout->p_sys->b_altfullscreen = 0;
315 p_vout->p_sys->i_time_button_last_pressed = 0;
317 TestNetWMSupport( p_vout );
319 #ifdef MODULE_NAME_IS_xvmc
320 p_vout->p_sys->p_last_subtitle_save = NULL;
321 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
323 /* Look what method was requested */
324 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
325 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
328 if( (strcmp(psz_value, "bob") == 0) ||
329 (strcmp(psz_value, "blend") == 0) )
330 p_vout->p_sys->xvmc_deinterlace_method = 2;
331 else if (strcmp(psz_value, "discard") == 0)
332 p_vout->p_sys->xvmc_deinterlace_method = 1;
334 p_vout->p_sys->xvmc_deinterlace_method = 0;
338 p_vout->p_sys->xvmc_deinterlace_method = 0;
340 /* Look what method was requested */
341 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
342 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
343 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
347 if( strncmp( psz_value, "eq", 2 ) == 0 )
348 p_vout->p_sys->xvmc_crop_style = 1;
349 else if( strncmp( psz_value, "4-16", 4 ) == 0)
350 p_vout->p_sys->xvmc_crop_style = 2;
351 else if( strncmp( psz_value, "16-4", 4 ) == 0)
352 p_vout->p_sys->xvmc_crop_style = 3;
354 p_vout->p_sys->xvmc_crop_style = 0;
358 p_vout->p_sys->xvmc_crop_style = 0;
360 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
361 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
363 if( !checkXvMCCap( p_vout ) )
365 msg_Err( p_vout, "no XVMC capability found" );
366 E_(Deactivate)( p_vout );
369 sub_pic.p_sys = NULL;
370 p_vout->p_sys->last_date = 0;
373 /* Variable to indicate if the window should be on top of others */
374 /* Trigger a callback right now */
375 var_Get( p_vout, "video-on-top", &val );
376 var_Set( p_vout, "video-on-top", val );
381 /*****************************************************************************
382 * Deactivate: destroy X11 video thread output method
383 *****************************************************************************
384 * Terminate an output method created by Open
385 *****************************************************************************/
386 void E_(Deactivate) ( vlc_object_t *p_this )
388 vout_thread_t *p_vout = (vout_thread_t *)p_this;
390 /* If the fullscreen window is still open, close it */
391 if( p_vout->b_fullscreen )
393 ToggleFullScreen( p_vout );
396 /* Restore cursor if it was blanked */
397 if( !p_vout->p_sys->b_mouse_pointer_visible )
399 ToggleCursor( p_vout );
402 #ifdef MODULE_NAME_IS_x11
403 /* Destroy colormap */
404 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
406 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
408 #elif defined(MODULE_NAME_IS_xvideo)
409 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
410 #elif defined(MODULE_NAME_IS_xvmc)
411 if( p_vout->p_sys->xvmc_cap )
413 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
414 xxmc_dispose_context( p_vout );
415 if( p_vout->p_sys->old_subpic )
417 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
418 p_vout->p_sys->old_subpic = NULL;
420 if( p_vout->p_sys->new_subpic )
422 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
423 p_vout->p_sys->new_subpic = NULL;
425 free( p_vout->p_sys->xvmc_cap );
426 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
430 DestroyCursor( p_vout );
431 EnableXScreenSaver( p_vout );
432 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
433 XCloseDisplay( p_vout->p_sys->p_display );
435 /* Destroy structure */
436 vlc_mutex_destroy( &p_vout->p_sys->lock );
437 #ifdef MODULE_NAME_IS_xvmc
438 free_context_lock( &p_vout->p_sys->xvmc_lock );
441 free( p_vout->p_sys );
444 #ifdef MODULE_NAME_IS_xvmc
446 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
449 static int xvmc_check_yv12( Display *display, XvPortID port )
451 XvImageFormatValues *formatValues;
455 formatValues = XvListImageFormats( display, port, &formats );
457 for( i = 0; i < formats; i++ )
459 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
460 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
462 XFree (formatValues);
467 XFree (formatValues);
471 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
473 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
476 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
479 int xv_double_buffer;
481 xv_double_buffer = 1;
483 XLockDisplay( p_vout->p_sys->p_display );
484 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
486 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
488 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
489 XUnlockDisplay( p_vout->p_sys->p_display );
491 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
492 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
495 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
497 vlc_xxmc_t *xxmc = NULL;
499 vlc_mutex_lock( &p_vout->p_sys->lock );
500 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
502 xxmc = &p_pic->p_sys->xxmc_data;
503 if( (!xxmc->decoded ||
504 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
506 vlc_mutex_unlock( &p_vout->p_sys->lock );
507 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
511 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
513 if (p_vout->p_last_subtitle != NULL)
515 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_last_subtitle )
517 p_vout->p_sys->new_subpic =
518 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
519 p_vout->p_sys->xvmc_width,
520 p_vout->p_sys->xvmc_height,
521 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
523 if (p_vout->p_sys->new_subpic)
525 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
526 XvMCClearSubpicture( p_vout->p_sys->p_display,
527 p_vout->p_sys->new_subpic,
530 p_vout->p_sys->xvmc_width,
531 p_vout->p_sys->xvmc_height,
533 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
534 clear_xx44_palette( &p_vout->p_sys->palette );
536 if( sub_pic.p_sys == NULL )
538 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
539 if( sub_pic.p_sys != NULL )
541 sub_pic.p_sys->p_vout = p_vout;
542 sub_pic.p_sys->xvmc_surf = NULL;
543 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
546 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
547 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
548 sub_pic.p->i_pitch = p_vout->output.i_width;
550 memset( p_vout->p_sys->subImage->data, 0,
551 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
553 if (p_vout->p_last_subtitle != NULL)
555 blend_xx44( p_vout->p_sys->subImage->data,
556 p_vout->p_last_subtitle,
557 p_vout->p_sys->subImage->width,
558 p_vout->p_sys->subImage->height,
559 p_vout->p_sys->subImage->width,
560 &p_vout->p_sys->palette,
561 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
564 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
565 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
566 p_vout->p_sys->new_subpic,
567 p_vout->p_sys->subImage,
570 p_vout->output.i_width, /* overlay->width, */
571 p_vout->output.i_height, /* overlay->height */
574 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
575 if (p_vout->p_sys->old_subpic)
577 xxmc_xvmc_free_subpicture( p_vout,
578 p_vout->p_sys->old_subpic);
579 p_vout->p_sys->old_subpic = NULL;
581 if (p_vout->p_sys->new_subpic)
583 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
584 p_vout->p_sys->new_subpic = NULL;
585 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
586 p_vout->p_sys->xvmc_palette,
588 p_vout->p_sys->old_subpic->num_palette_entries,
589 p_vout->p_sys->old_subpic->entry_bytes,
590 p_vout->p_sys->old_subpic->component_order );
591 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
592 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
593 p_vout->p_sys->old_subpic,
594 p_vout->p_sys->xvmc_palette );
595 XvMCFlushSubpicture( p_vout->p_sys->p_display,
596 p_vout->p_sys->old_subpic);
597 XvMCSyncSubpicture( p_vout->p_sys->p_display,
598 p_vout->p_sys->old_subpic );
599 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
602 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
603 if (p_vout->p_sys->xvmc_backend_subpic )
605 XvMCBlendSubpicture( p_vout->p_sys->p_display,
606 p_pic->p_sys->xvmc_surf,
607 p_vout->p_sys->old_subpic,
610 p_vout->p_sys->xvmc_width,
611 p_vout->p_sys->xvmc_height,
614 p_vout->p_sys->xvmc_width,
615 p_vout->p_sys->xvmc_height );
619 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
620 p_pic->p_sys->xvmc_surf,
621 p_pic->p_sys->xvmc_surf,
622 p_vout->p_sys->old_subpic,
625 p_vout->p_sys->xvmc_width,
626 p_vout->p_sys->xvmc_height,
629 p_vout->p_sys->xvmc_width,
630 p_vout->p_sys->xvmc_height );
632 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
637 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
638 if( p_vout->p_sys->xvmc_backend_subpic )
640 XvMCBlendSubpicture( p_vout->p_sys->p_display,
641 p_pic->p_sys->xvmc_surf,
642 p_vout->p_sys->old_subpic,
644 p_vout->p_sys->xvmc_width,
645 p_vout->p_sys->xvmc_height,
647 p_vout->p_sys->xvmc_width,
648 p_vout->p_sys->xvmc_height );
652 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
653 p_pic->p_sys->xvmc_surf,
654 p_pic->p_sys->xvmc_surf,
655 p_vout->p_sys->old_subpic,
657 p_vout->p_sys->xvmc_width,
658 p_vout->p_sys->xvmc_height,
660 p_vout->p_sys->xvmc_width,
661 p_vout->p_sys->xvmc_height );
663 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
666 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
668 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
669 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
671 vlc_mutex_unlock( &p_vout->p_sys->lock );
675 /*****************************************************************************
676 * InitVideo: initialize X11 video thread output method
677 *****************************************************************************
678 * This function create the XImages needed by the output thread. It is called
679 * at the beginning of the thread, but also each time the window is resized.
680 *****************************************************************************/
681 static int InitVideo( vout_thread_t *p_vout )
683 unsigned int i_index = 0;
686 I_OUTPUTPICTURES = 0;
688 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
689 /* Initialize the output structure; we already found an XVideo port,
690 * and the corresponding chroma we will be using. Since we can
691 * arbitrary scale, stick to the coordinates and aspect. */
692 p_vout->output.i_width = p_vout->render.i_width;
693 p_vout->output.i_height = p_vout->render.i_height;
694 p_vout->output.i_aspect = p_vout->render.i_aspect;
696 p_vout->fmt_out = p_vout->fmt_in;
697 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
699 switch( p_vout->output.i_chroma )
701 case VLC_FOURCC('R','V','1','6'):
702 #if defined( WORDS_BIGENDIAN )
703 p_vout->output.i_rmask = 0xf800;
704 p_vout->output.i_gmask = 0x07e0;
705 p_vout->output.i_bmask = 0x001f;
707 p_vout->output.i_rmask = 0x001f;
708 p_vout->output.i_gmask = 0x07e0;
709 p_vout->output.i_bmask = 0xf800;
712 case VLC_FOURCC('R','V','1','5'):
713 #if defined( WORDS_BIGENDIAN )
714 p_vout->output.i_rmask = 0x7c00;
715 p_vout->output.i_gmask = 0x03e0;
716 p_vout->output.i_bmask = 0x001f;
718 p_vout->output.i_rmask = 0x001f;
719 p_vout->output.i_gmask = 0x03e0;
720 p_vout->output.i_bmask = 0x7c00;
725 #elif defined(MODULE_NAME_IS_x11)
726 /* Initialize the output structure: RGB with square pixels, whatever
727 * the input format is, since it's the only format we know */
728 switch( p_vout->p_sys->i_screen_depth )
730 case 8: /* FIXME: set the palette */
731 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
733 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
735 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
738 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
740 msg_Err( p_vout, "unknown screen depth %i",
741 p_vout->p_sys->i_screen_depth );
745 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
746 p_vout->p_sys->p_win->i_height,
748 &p_vout->fmt_out.i_visible_width,
749 &p_vout->fmt_out.i_visible_height );
751 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
753 p_vout->output.i_width = p_vout->fmt_out.i_width =
754 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
755 p_vout->fmt_in.i_visible_width;
756 p_vout->output.i_height = p_vout->fmt_out.i_height =
757 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
758 p_vout->fmt_in.i_visible_height;
759 p_vout->fmt_out.i_x_offset =
760 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
761 p_vout->fmt_in.i_visible_width;
762 p_vout->fmt_out.i_y_offset =
763 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
764 p_vout->fmt_in.i_visible_height;
766 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
767 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
768 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
770 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
771 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
772 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
773 p_vout->fmt_out.i_visible_width,
774 p_vout->fmt_out.i_visible_height );
777 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
778 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
782 /* Find an empty picture slot */
783 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
785 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
787 p_pic = p_vout->p_picture + i_index;
792 /* Allocate the picture */
793 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
798 p_pic->i_status = DESTROYED_PICTURE;
799 p_pic->i_type = DIRECT_PICTURE;
801 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
806 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
808 /* U and V inverted compared to I420
809 * Fixme: this should be handled by the vout core */
810 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
811 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
817 /*****************************************************************************
818 * DisplayVideo: displays previously rendered output
819 *****************************************************************************
820 * This function sends the currently rendered image to X11 server.
821 * (The Xv extension takes care of "double-buffering".)
822 *****************************************************************************/
823 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
825 unsigned int i_width, i_height, i_x, i_y;
827 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
828 p_vout->p_sys->p_win->i_height,
829 &i_x, &i_y, &i_width, &i_height );
831 vlc_mutex_lock( &p_vout->p_sys->lock );
833 #ifdef MODULE_NAME_IS_xvmc
834 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
836 vlc_xxmc_t *xxmc = &p_picture->p_sys->xxmc_data;
837 if( !xxmc->decoded ||
838 !xxmc_xvmc_surface_valid( p_vout, p_picture->p_sys->xvmc_surf ) )
840 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
842 xxmc_xvmc_surface_valid( p_vout, p_picture->p_sys->xvmc_surf ) );
843 vlc_mutex_unlock( &p_vout->p_sys->lock );
844 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
848 src_width = p_vout->output.i_width;
849 src_height = p_vout->output.i_height;
851 if( p_vout->p_sys->xvmc_crop_style == 1 )
858 else if( p_vout->p_sys->xvmc_crop_style == 2 )
865 else if( p_vout->p_sys->xvmc_crop_style == 3 )
878 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
879 { /* BOB DEINTERLACE */
880 if( (p_picture->p_sys->nb_display == 0) ||
881 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
883 first_field = (p_picture->b_top_field_first) ?
884 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
888 first_field = (p_picture->b_top_field_first) ?
889 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
894 first_field = XVMC_FRAME_PICTURE;
897 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
898 XvMCFlushSurface( p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf );
899 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
900 XvMCPutSurface( p_vout->p_sys->p_display,
901 p_picture->p_sys->xvmc_surf,
902 p_vout->p_sys->p_win->video_window,
913 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
914 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
915 { /* BOB DEINTERLACE */
916 if( p_picture->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
918 mtime_t last_date = p_picture->date;
920 vlc_mutex_lock( &p_vout->picture_lock );
921 if( !p_vout->p_sys->last_date )
923 p_picture->date += 20000;
927 p_picture->date = ((3 * p_picture->date -
928 p_vout->p_sys->last_date) / 2 );
930 p_vout->p_sys->last_date = last_date;
931 p_picture->b_force = 1;
932 p_picture->p_sys->nb_display = 1;
933 vlc_mutex_unlock( &p_vout->picture_lock );
937 p_picture->p_sys->nb_display = 0;
938 p_picture->b_force = 0;
941 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
944 #ifdef HAVE_SYS_SHM_H
945 if( p_vout->p_sys->b_shm )
947 /* Display rendered image using shared memory extension */
948 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
949 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
950 p_vout->p_sys->p_win->video_window,
951 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
952 p_vout->fmt_out.i_x_offset,
953 p_vout->fmt_out.i_y_offset,
954 p_vout->fmt_out.i_visible_width,
955 p_vout->fmt_out.i_visible_height,
956 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
957 False /* Don't put True here or you'll waste your CPU */ );
959 XShmPutImage( p_vout->p_sys->p_display,
960 p_vout->p_sys->p_win->video_window,
961 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
962 p_vout->fmt_out.i_x_offset,
963 p_vout->fmt_out.i_y_offset,
964 0 /*dest_x*/, 0 /*dest_y*/,
965 p_vout->fmt_out.i_visible_width,
966 p_vout->fmt_out.i_visible_height,
967 False /* Don't put True here ! */ );
971 #endif /* HAVE_SYS_SHM_H */
973 /* Use standard XPutImage -- this is gonna be slow ! */
974 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
975 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
976 p_vout->p_sys->p_win->video_window,
977 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
978 p_vout->fmt_out.i_x_offset,
979 p_vout->fmt_out.i_y_offset,
980 p_vout->fmt_out.i_visible_width,
981 p_vout->fmt_out.i_visible_height,
982 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
984 XPutImage( p_vout->p_sys->p_display,
985 p_vout->p_sys->p_win->video_window,
986 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
987 p_vout->fmt_out.i_x_offset,
988 p_vout->fmt_out.i_y_offset,
989 0 /*dest_x*/, 0 /*dest_y*/,
990 p_vout->fmt_out.i_visible_width,
991 p_vout->fmt_out.i_visible_height );
995 /* Make sure the command is sent now - do NOT use XFlush !*/
996 XSync( p_vout->p_sys->p_display, False );
998 vlc_mutex_unlock( &p_vout->p_sys->lock );
1001 /*****************************************************************************
1002 * ManageVideo: handle X11 events
1003 *****************************************************************************
1004 * This function should be called regularly by video output thread. It manages
1005 * X11 events and allows window resizing. It returns a non null value on
1007 *****************************************************************************/
1008 static int ManageVideo( vout_thread_t *p_vout )
1010 XEvent xevent; /* X11 event */
1013 vlc_mutex_lock( &p_vout->p_sys->lock );
1015 #ifdef MODULE_NAME_IS_xvmc
1016 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1019 /* Handle events from the owner window */
1020 if( p_vout->p_sys->p_win->owner_window )
1022 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1023 p_vout->p_sys->p_win->owner_window,
1024 StructureNotifyMask, &xevent ) == True )
1026 /* ConfigureNotify event: prepare */
1027 if( xevent.type == ConfigureNotify )
1029 /* Update dimensions */
1030 XResizeWindow( p_vout->p_sys->p_display,
1031 p_vout->p_sys->p_win->base_window,
1032 xevent.xconfigure.width,
1033 xevent.xconfigure.height );
1038 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1039 * output window's size changed, MapNotify and UnmapNotify to know if the
1040 * window is mapped (and if the display is useful), and ClientMessages
1041 * to intercept window destruction requests */
1043 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1044 p_vout->p_sys->p_win->base_window,
1045 StructureNotifyMask | KeyPressMask |
1046 ButtonPressMask | ButtonReleaseMask |
1047 PointerMotionMask | Button1MotionMask , &xevent )
1050 /* ConfigureNotify event: prepare */
1051 if( xevent.type == ConfigureNotify )
1053 if( (unsigned int)xevent.xconfigure.width
1054 != p_vout->p_sys->p_win->i_width
1055 || (unsigned int)xevent.xconfigure.height
1056 != p_vout->p_sys->p_win->i_height )
1058 /* Update dimensions */
1059 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1060 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1061 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1064 /* Keyboard event */
1065 else if( xevent.type == KeyPress )
1067 unsigned int state = xevent.xkey.state;
1068 KeySym x_key_symbol;
1069 char i_key; /* ISO Latin-1 key */
1071 /* We may have keys like F1 trough F12, ESC ... */
1072 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1073 xevent.xkey.keycode, 0 );
1074 val.i_int = ConvertKey( (int)x_key_symbol );
1076 xevent.xkey.state &= ~ShiftMask;
1077 xevent.xkey.state &= ~ControlMask;
1078 xevent.xkey.state &= ~Mod1Mask;
1081 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1084 * The reason why I use this instead of XK_0 is that
1085 * with XLookupString, we don't have to care about
1092 if( state & ShiftMask )
1094 val.i_int |= KEY_MODIFIER_SHIFT;
1096 if( state & ControlMask )
1098 val.i_int |= KEY_MODIFIER_CTRL;
1100 if( state & Mod1Mask )
1102 val.i_int |= KEY_MODIFIER_ALT;
1104 var_Set( p_vout->p_libvlc, "key-pressed", val );
1108 else if( xevent.type == ButtonPress )
1110 switch( ((XButtonEvent *)&xevent)->button )
1113 var_Get( p_vout, "mouse-button-down", &val );
1115 var_Set( p_vout, "mouse-button-down", val );
1117 /* detect double-clicks */
1118 if( ( ((XButtonEvent *)&xevent)->time -
1119 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1121 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1124 p_vout->p_sys->i_time_button_last_pressed =
1125 ((XButtonEvent *)&xevent)->time;
1128 var_Get( p_vout, "mouse-button-down", &val );
1130 var_Set( p_vout, "mouse-button-down", val );
1134 var_Get( p_vout, "mouse-button-down", &val );
1136 var_Set( p_vout, "mouse-button-down", val );
1140 var_Get( p_vout, "mouse-button-down", &val );
1142 var_Set( p_vout, "mouse-button-down", val );
1146 var_Get( p_vout, "mouse-button-down", &val );
1148 var_Set( p_vout, "mouse-button-down", val );
1153 else if( xevent.type == ButtonRelease )
1155 switch( ((XButtonEvent *)&xevent)->button )
1158 var_Get( p_vout, "mouse-button-down", &val );
1160 var_Set( p_vout, "mouse-button-down", val );
1162 val.b_bool = VLC_TRUE;
1163 var_Set( p_vout, "mouse-clicked", val );
1168 playlist_t *p_playlist;
1170 var_Get( p_vout, "mouse-button-down", &val );
1172 var_Set( p_vout, "mouse-button-down", val );
1174 p_playlist = vlc_object_find( p_vout,
1175 VLC_OBJECT_PLAYLIST,
1177 if( p_playlist != NULL )
1180 var_Get( p_playlist, "intf-show", &val );
1181 val.b_bool = !val.b_bool;
1182 var_Set( p_playlist, "intf-show", val );
1183 vlc_object_release( p_playlist );
1190 intf_thread_t *p_intf;
1191 playlist_t *p_playlist;
1193 var_Get( p_vout, "mouse-button-down", &val );
1195 var_Set( p_vout, "mouse-button-down", val );
1196 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
1200 p_intf->b_menu_change = 1;
1201 vlc_object_release( p_intf );
1204 p_playlist = vlc_object_find( p_vout,
1205 VLC_OBJECT_PLAYLIST,
1207 if( p_playlist != NULL )
1209 vlc_value_t val; val.b_bool = VLC_TRUE;
1210 var_Set( p_playlist, "intf-popupmenu", val );
1211 vlc_object_release( p_playlist );
1217 var_Get( p_vout, "mouse-button-down", &val );
1219 var_Set( p_vout, "mouse-button-down", val );
1223 var_Get( p_vout, "mouse-button-down", &val );
1225 var_Set( p_vout, "mouse-button-down", val );
1231 else if( xevent.type == MotionNotify )
1233 unsigned int i_width, i_height, i_x, i_y;
1236 /* somewhat different use for vout_PlacePicture:
1237 * here the values are needed to give to mouse coordinates
1238 * in the original picture space */
1239 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1240 p_vout->p_sys->p_win->i_height,
1241 &i_x, &i_y, &i_width, &i_height );
1243 val.i_int = ( xevent.xmotion.x - i_x ) *
1244 p_vout->fmt_in.i_visible_width / i_width +
1245 p_vout->fmt_in.i_x_offset;
1246 var_Set( p_vout, "mouse-x", val );
1247 val.i_int = ( xevent.xmotion.y - i_y ) *
1248 p_vout->fmt_in.i_visible_height / i_height +
1249 p_vout->fmt_in.i_y_offset;
1250 var_Set( p_vout, "mouse-y", val );
1252 val.b_bool = VLC_TRUE;
1253 var_Set( p_vout, "mouse-moved", val );
1255 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1256 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1258 ToggleCursor( p_vout );
1261 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1262 || xevent.type == MapNotify
1263 || xevent.type == UnmapNotify )
1265 /* Ignore these events */
1267 else /* Other events */
1269 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1273 /* Handle events for video output sub-window */
1274 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1275 p_vout->p_sys->p_win->video_window,
1276 ExposureMask, &xevent ) == True )
1278 /* Window exposed (only handled if stream playback is paused) */
1279 if( xevent.type == Expose )
1281 if( ((XExposeEvent *)&xevent)->count == 0 )
1283 /* (if this is the last a collection of expose events...) */
1285 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1287 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1288 ->stream.control.i_status )
1290 /* XVideoDisplay( p_vout )*/;
1298 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1299 * are handled - according to the man pages, the format is always 32
1301 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1302 ClientMessage, &xevent ) )
1304 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1305 && ((Atom)xevent.xclient.data.l[0]
1306 == p_vout->p_sys->p_win->wm_delete_window ) )
1308 /* the user wants to close the window */
1309 playlist_t * p_playlist =
1310 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1312 if( p_playlist != NULL )
1314 playlist_Stop( p_playlist );
1315 vlc_object_release( p_playlist );
1323 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1327 /* Update the object variable and trigger callback */
1328 val.b_bool = !p_vout->b_fullscreen;
1331 * FIXME FIXME FIXME FIXME: EXPLICIT HACK.
1332 * On the one hand, we cannot hold the lock while triggering a
1333 * callback, as it causes a deadlock with video-on-top handling.
1334 * On the other hand, we have to lock while triggering the
1336 * 1/ make sure video-on-top remains in sync with fullscreen
1337 * (i.e. unlocking creates a race condition if fullscreen is
1338 * switched on and off VERY FAST).
1339 * 2/ avoid possible corruption bugs if another thread gets the
1340 * mutex and modifies our data in-between.
1342 * This is obviously contradictory. Correct solutions may include:
1343 * - putting the fullscreen NAND video-on-top logic out of libvlc,
1344 * back into the video output plugins (ugly code duplication...),
1345 * - serializing fullscreen and video-on-top handling properly
1346 * instead of doing it via the fullscreen callback. That's got to
1347 * be the correct one.
1349 #ifdef MODULE_NAME_IS_xvmc
1350 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1352 vlc_mutex_unlock( &p_vout->p_sys->lock );
1354 var_Set( p_vout, "fullscreen", val );
1356 vlc_mutex_lock( &p_vout->p_sys->lock );
1357 #ifdef MODULE_NAME_IS_xvmc
1358 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1361 ToggleFullScreen( p_vout );
1362 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1365 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1366 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1368 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1369 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1371 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1372 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1373 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1374 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1375 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1376 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1377 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1378 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1380 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1386 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1387 * the size flag inside the fullscreen routine)
1389 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1391 unsigned int i_width, i_height, i_x, i_y;
1393 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1395 #ifdef MODULE_NAME_IS_x11
1396 /* We need to signal the vout thread about the size change because it
1397 * is doing the rescaling */
1398 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1401 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1402 p_vout->p_sys->p_win->i_height,
1403 &i_x, &i_y, &i_width, &i_height );
1405 XMoveResizeWindow( p_vout->p_sys->p_display,
1406 p_vout->p_sys->p_win->video_window,
1407 i_x, i_y, i_width, i_height );
1410 /* Autohide Cursour */
1411 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
1413 /* Hide the mouse automatically */
1414 if( p_vout->p_sys->b_mouse_pointer_visible )
1416 ToggleCursor( p_vout );
1420 #ifdef MODULE_NAME_IS_xvmc
1421 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1424 vlc_mutex_unlock( &p_vout->p_sys->lock );
1428 /*****************************************************************************
1429 * EndVideo: terminate X11 video thread output method
1430 *****************************************************************************
1431 * Destroy the X11 XImages created by Init. It is called at the end of
1432 * the thread, but also each time the window is resized.
1433 *****************************************************************************/
1434 static void EndVideo( vout_thread_t *p_vout )
1438 /* Free the direct buffers we allocated */
1439 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1442 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1446 /* following functions are local */
1448 /*****************************************************************************
1449 * CreateWindow: open and set-up X11 main window
1450 *****************************************************************************/
1451 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1453 XSizeHints xsize_hints;
1454 XSetWindowAttributes xwindow_attributes;
1455 XGCValues xgcvalues;
1458 vlc_bool_t b_expose = VLC_FALSE;
1459 vlc_bool_t b_configure_notify = VLC_FALSE;
1460 vlc_bool_t b_map_notify = VLC_FALSE;
1463 /* Prepare window manager hints and properties */
1464 p_win->wm_protocols =
1465 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1466 p_win->wm_delete_window =
1467 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1469 /* Never have a 0-pixel-wide window */
1470 xsize_hints.min_width = 2;
1471 xsize_hints.min_height = 1;
1473 /* Prepare window attributes */
1474 xwindow_attributes.backing_store = Always; /* save the hidden part */
1475 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1476 p_vout->p_sys->i_screen);
1477 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1479 if( !p_vout->b_fullscreen )
1481 p_win->owner_window =
1482 (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
1483 &p_win->i_width, &p_win->i_height );
1485 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1486 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1487 xsize_hints.flags = PSize | PMinSize;
1489 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1491 xsize_hints.x = p_win->i_x;
1492 xsize_hints.y = p_win->i_y;
1493 xsize_hints.flags |= PPosition;
1498 /* Fullscreen window size and position */
1499 p_win->owner_window = 0;
1500 p_win->i_x = p_win->i_y = 0;
1502 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1504 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1507 if( !p_win->owner_window )
1509 /* Create the window and set hints - the window must receive
1510 * ConfigureNotify events, and until it is displayed, Expose and
1511 * MapNotify events. */
1513 p_win->base_window =
1514 XCreateWindow( p_vout->p_sys->p_display,
1515 DefaultRootWindow( p_vout->p_sys->p_display ),
1516 p_win->i_x, p_win->i_y,
1517 p_win->i_width, p_win->i_height,
1520 CWBackingStore | CWBackPixel | CWEventMask,
1521 &xwindow_attributes );
1523 if( !p_vout->b_fullscreen )
1525 /* Set window manager hints and properties: size hints, command,
1526 * window's name, and accepted protocols */
1527 XSetWMNormalHints( p_vout->p_sys->p_display,
1528 p_win->base_window, &xsize_hints );
1529 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1530 p_vout->p_libvlc->ppsz_argv, p_vout->p_libvlc->i_argc );
1532 if( !var_GetBool( p_vout, "video-deco") )
1535 mwmhints_t mwmhints;
1537 mwmhints.flags = MWM_HINTS_DECORATIONS;
1538 mwmhints.decorations = False;
1540 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1543 XChangeProperty( p_vout->p_sys->p_display,
1545 prop, prop, 32, PropModeReplace,
1546 (unsigned char *)&mwmhints,
1547 PROP_MWM_HINTS_ELEMENTS );
1551 var_Get( p_vout, "video-title", &val );
1552 if( !val.psz_string || !*val.psz_string )
1554 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1555 #ifdef MODULE_NAME_IS_x11
1556 VOUT_TITLE " (X11 output)"
1557 #elif defined(MODULE_NAME_IS_glx)
1558 VOUT_TITLE " (GLX output)"
1560 VOUT_TITLE " (XVideo output)"
1566 XStoreName( p_vout->p_sys->p_display,
1567 p_win->base_window, val.psz_string );
1569 if( val.psz_string ) free( val.psz_string );
1577 unsigned int dummy4, dummy5;
1579 /* Select events we are interested in. */
1580 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1581 StructureNotifyMask );
1583 /* Get the parent window's geometry information */
1584 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1585 &dummy1, &dummy2, &dummy3,
1590 /* We are already configured */
1591 b_configure_notify = VLC_TRUE;
1593 /* From man XSelectInput: only one client at a time can select a
1594 * ButtonPress event, so we need to open a new window anyway. */
1595 p_win->base_window =
1596 XCreateWindow( p_vout->p_sys->p_display,
1597 p_win->owner_window,
1599 p_win->i_width, p_win->i_height,
1601 0, CopyFromParent, 0,
1602 CWBackingStore | CWBackPixel | CWEventMask,
1603 &xwindow_attributes );
1606 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1607 || (p_win->wm_delete_window == None)
1608 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1609 &p_win->wm_delete_window, 1 ) )
1611 /* WM_DELETE_WINDOW is not supported by window manager */
1612 msg_Warn( p_vout, "missing or bad window manager" );
1615 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1616 * event when using functions like XCopyArea */
1617 xgcvalues.graphics_exposures = False;
1618 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1620 GCGraphicsExposures, &xgcvalues );
1622 /* Send orders to server, and wait until window is displayed - three
1623 * events must be received: a MapNotify event, an Expose event allowing
1624 * drawing in the window, and a ConfigureNotify to get the window
1625 * dimensions. Once those events have been received, only
1626 * ConfigureNotify events need to be received. */
1627 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1630 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1631 SubstructureNotifyMask | StructureNotifyMask |
1632 ExposureMask, &xevent);
1633 if( (xevent.type == Expose)
1634 && (xevent.xexpose.window == p_win->base_window) )
1636 b_expose = VLC_TRUE;
1637 /* ConfigureNotify isn't sent if there isn't a window manager.
1638 * Expose should be the last event to be received so it should
1639 * be fine to assume we won't receive it anymore. */
1640 b_configure_notify = VLC_TRUE;
1642 else if( (xevent.type == MapNotify)
1643 && (xevent.xmap.window == p_win->base_window) )
1645 b_map_notify = VLC_TRUE;
1647 else if( (xevent.type == ConfigureNotify)
1648 && (xevent.xconfigure.window == p_win->base_window) )
1650 b_configure_notify = VLC_TRUE;
1651 p_win->i_width = xevent.xconfigure.width;
1652 p_win->i_height = xevent.xconfigure.height;
1654 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1656 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1657 StructureNotifyMask | KeyPressMask |
1658 ButtonPressMask | ButtonReleaseMask |
1659 PointerMotionMask );
1661 #ifdef MODULE_NAME_IS_x11
1662 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1664 /* Allocate a new palette */
1665 p_vout->p_sys->colormap =
1666 XCreateColormap( p_vout->p_sys->p_display,
1667 DefaultRootWindow( p_vout->p_sys->p_display ),
1668 DefaultVisual( p_vout->p_sys->p_display,
1669 p_vout->p_sys->i_screen ),
1672 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1673 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1674 CWColormap, &xwindow_attributes );
1678 /* Create video output sub-window. */
1679 p_win->video_window = XCreateSimpleWindow(
1680 p_vout->p_sys->p_display,
1681 p_win->base_window, 0, 0,
1682 p_win->i_width, p_win->i_height,
1684 BlackPixel( p_vout->p_sys->p_display,
1685 p_vout->p_sys->i_screen ),
1686 WhitePixel( p_vout->p_sys->p_display,
1687 p_vout->p_sys->i_screen ) );
1689 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1690 BlackPixel( p_vout->p_sys->p_display,
1691 p_vout->p_sys->i_screen ) );
1693 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1694 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1697 /* make sure the video window will be centered in the next ManageVideo() */
1698 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1700 /* If the cursor was formerly blank than blank it again */
1701 if( !p_vout->p_sys->b_mouse_pointer_visible )
1703 ToggleCursor( p_vout );
1704 ToggleCursor( p_vout );
1707 /* Do NOT use XFlush here ! */
1708 XSync( p_vout->p_sys->p_display, False );
1710 /* At this stage, the window is open, displayed, and ready to
1712 p_vout->p_sys->p_win = p_win;
1717 /*****************************************************************************
1718 * DestroyWindow: destroy the window
1719 *****************************************************************************
1721 *****************************************************************************/
1722 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1724 /* Do NOT use XFlush here ! */
1725 XSync( p_vout->p_sys->p_display, False );
1727 if( p_win->video_window != None )
1728 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1730 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1732 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1733 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1735 if( p_win->owner_window )
1736 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1739 /*****************************************************************************
1740 * NewPicture: allocate a picture
1741 *****************************************************************************
1742 * Returns 0 on success, -1 otherwise
1743 *****************************************************************************/
1744 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1746 #ifndef MODULE_NAME_IS_glx
1748 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1752 /* We know the chroma, allocate a buffer which will be used
1753 * directly by the decoder */
1754 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1756 if( p_pic->p_sys == NULL )
1761 #ifdef MODULE_NAME_IS_xvmc
1762 p_pic->p_sys->p_vout = p_vout;
1763 p_pic->p_sys->xvmc_surf = NULL;
1764 p_pic->p_sys->xxmc_data.decoded = 0;
1765 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1766 p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1767 p_pic->p_sys->nb_display = 0;
1770 /* Fill in picture_t fields */
1771 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1772 p_vout->output.i_width, p_vout->output.i_height,
1773 p_vout->output.i_aspect );
1775 #ifdef HAVE_SYS_SHM_H
1776 if( p_vout->p_sys->b_shm )
1778 /* Create image using XShm extension */
1779 p_pic->p_sys->p_image =
1780 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1781 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1782 p_vout->p_sys->i_xvport,
1783 VLC2X11_FOURCC(p_vout->output.i_chroma),
1785 p_vout->p_sys->p_visual,
1786 p_vout->p_sys->i_screen_depth,
1788 &p_pic->p_sys->shminfo,
1789 p_vout->output.i_width, p_vout->output.i_height );
1792 if( !p_vout->p_sys->b_shm || !p_pic->p_sys->p_image )
1793 #endif /* HAVE_SYS_SHM_H */
1795 /* Create image without XShm extension */
1796 p_pic->p_sys->p_image =
1797 CreateImage( p_vout, p_vout->p_sys->p_display,
1798 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1799 p_vout->p_sys->i_xvport,
1800 VLC2X11_FOURCC(p_vout->output.i_chroma),
1801 p_pic->format.i_bits_per_pixel,
1803 p_vout->p_sys->p_visual,
1804 p_vout->p_sys->i_screen_depth,
1805 p_vout->p_sys->i_bytes_per_pixel,
1807 p_vout->output.i_width, p_vout->output.i_height );
1809 #ifdef HAVE_SYS_SHM_H
1810 if( p_pic->p_sys->p_image && p_vout->p_sys->b_shm )
1812 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1813 p_vout->p_sys->b_shm = VLC_FALSE;
1815 #endif /* HAVE_SYS_SHM_H */
1818 if( p_pic->p_sys->p_image == NULL )
1820 free( p_pic->p_sys );
1824 switch( p_vout->output.i_chroma )
1826 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1827 case VLC_FOURCC('I','4','2','0'):
1828 case VLC_FOURCC('Y','V','1','2'):
1829 case VLC_FOURCC('Y','2','1','1'):
1830 case VLC_FOURCC('Y','U','Y','2'):
1831 case VLC_FOURCC('U','Y','V','Y'):
1832 case VLC_FOURCC('R','V','1','5'):
1833 case VLC_FOURCC('R','V','1','6'):
1834 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1835 case VLC_FOURCC('R','V','3','2'):
1837 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1840 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1841 + p_pic->p_sys->p_image->offsets[i_plane];
1842 p_pic->p[i_plane].i_pitch =
1843 p_pic->p_sys->p_image->pitches[i_plane];
1845 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1847 /* U and V inverted compared to I420
1848 * Fixme: this should be handled by the vout core */
1849 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1850 + p_pic->p_sys->p_image->offsets[2];
1851 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1852 + p_pic->p_sys->p_image->offsets[1];
1857 case VLC_FOURCC('R','G','B','2'):
1858 case VLC_FOURCC('R','V','1','6'):
1859 case VLC_FOURCC('R','V','1','5'):
1860 case VLC_FOURCC('R','V','2','4'):
1861 case VLC_FOURCC('R','V','3','2'):
1863 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1864 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1865 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1866 + p_pic->p_sys->p_image->xoffset;
1867 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1869 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1870 * properly by vout_InitPicture() */
1871 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1872 * p_pic->p_sys->p_image->width;
1877 /* Unknown chroma, tell the guy to get lost */
1878 IMAGE_FREE( p_pic->p_sys->p_image );
1879 free( p_pic->p_sys );
1880 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1881 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1882 p_pic->i_planes = 0;
1886 #endif /* !MODULE_NAME_IS_glx */
1891 /*****************************************************************************
1892 * FreePicture: destroy a picture allocated with NewPicture
1893 *****************************************************************************
1894 * Destroy XImage AND associated data. If using Shm, detach shared memory
1895 * segment from server and process, then free it. The XDestroyImage manpage
1896 * says that both the image structure _and_ the data pointed to by the
1897 * image structure are freed, so no need to free p_image->data.
1898 *****************************************************************************/
1899 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1901 /* The order of operations is correct */
1902 #ifdef HAVE_SYS_SHM_H
1903 if( p_vout->p_sys->b_shm )
1905 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1906 IMAGE_FREE( p_pic->p_sys->p_image );
1908 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1909 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1911 msg_Err( p_vout, "cannot detach shared memory (%s)",
1918 IMAGE_FREE( p_pic->p_sys->p_image );
1921 #ifdef MODULE_NAME_IS_xvmc
1922 if( p_pic->p_sys->xvmc_surf != NULL )
1924 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
1925 p_pic->p_sys->xvmc_surf = NULL;
1929 /* Do NOT use XFlush here ! */
1930 XSync( p_vout->p_sys->p_display, False );
1932 free( p_pic->p_sys );
1935 /*****************************************************************************
1936 * ToggleFullScreen: Enable or disable full screen mode
1937 *****************************************************************************
1938 * This function will switch between fullscreen and window mode.
1939 *****************************************************************************/
1940 static void ToggleFullScreen ( vout_thread_t *p_vout )
1944 mwmhints_t mwmhints;
1945 XSetWindowAttributes attributes;
1947 #ifdef HAVE_XINERAMA
1951 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1953 if( p_vout->b_fullscreen )
1955 msg_Dbg( p_vout, "entering fullscreen mode" );
1957 p_vout->p_sys->b_altfullscreen =
1958 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1960 XUnmapWindow( p_vout->p_sys->p_display,
1961 p_vout->p_sys->p_win->base_window );
1963 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1965 CreateWindow( p_vout, p_vout->p_sys->p_win );
1966 XDestroyWindow( p_vout->p_sys->p_display,
1967 p_vout->p_sys->fullscreen_window.video_window );
1968 XReparentWindow( p_vout->p_sys->p_display,
1969 p_vout->p_sys->original_window.video_window,
1970 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
1971 p_vout->p_sys->fullscreen_window.video_window =
1972 p_vout->p_sys->original_window.video_window;
1974 /* To my knowledge there are two ways to create a borderless window.
1975 * There's the generic way which is to tell x to bypass the window
1976 * manager, but this creates problems with the focus of other
1978 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1979 * luckily seems to be supported by most window managers. */
1980 if( !p_vout->p_sys->b_altfullscreen )
1982 mwmhints.flags = MWM_HINTS_DECORATIONS;
1983 mwmhints.decorations = False;
1985 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1987 XChangeProperty( p_vout->p_sys->p_display,
1988 p_vout->p_sys->p_win->base_window,
1989 prop, prop, 32, PropModeReplace,
1990 (unsigned char *)&mwmhints,
1991 PROP_MWM_HINTS_ELEMENTS );
1995 /* brute force way to remove decorations */
1996 attributes.override_redirect = True;
1997 XChangeWindowAttributes( p_vout->p_sys->p_display,
1998 p_vout->p_sys->p_win->base_window,
2002 /* Make sure the change is effective */
2003 XReparentWindow( p_vout->p_sys->p_display,
2004 p_vout->p_sys->p_win->base_window,
2005 DefaultRootWindow( p_vout->p_sys->p_display ),
2009 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2011 XClientMessageEvent event;
2013 memset( &event, 0, sizeof( XClientMessageEvent ) );
2015 event.type = ClientMessage;
2016 event.message_type = p_vout->p_sys->net_wm_state;
2017 event.display = p_vout->p_sys->p_display;
2018 event.window = p_vout->p_sys->p_win->base_window;
2020 event.data.l[ 0 ] = 1; /* set property */
2021 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2023 XSendEvent( p_vout->p_sys->p_display,
2024 DefaultRootWindow( p_vout->p_sys->p_display ),
2025 False, SubstructureRedirectMask,
2029 /* Make sure the change is effective */
2030 XReparentWindow( p_vout->p_sys->p_display,
2031 p_vout->p_sys->p_win->base_window,
2032 DefaultRootWindow( p_vout->p_sys->p_display ),
2035 #ifdef HAVE_XINERAMA
2036 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2037 XineramaIsActive( p_vout->p_sys->p_display ) )
2039 XineramaScreenInfo *screens; /* infos for xinerama */
2042 msg_Dbg( p_vout, "using XFree Xinerama extension");
2044 #define SCREEN p_vout->p_sys->p_win->i_screen
2046 /* Get Information about Xinerama (num of screens) */
2047 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2050 SCREEN = config_GetInt( p_vout,
2051 MODULE_STRING "-xineramascreen" );
2053 /* just check that user has entered a good value */
2054 if( SCREEN >= i_num_screens || SCREEN < 0 )
2056 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2060 /* Get the X/Y upper left corner coordinate of the above screen */
2061 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2062 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2064 /* Set the Height/width to the screen resolution */
2065 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2066 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2076 /* The window wasn't necessarily created at the requested size */
2077 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2079 #ifdef HAVE_XF86VIDMODE
2080 XF86VidModeModeLine mode;
2083 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2084 p_vout->p_sys->i_screen, &i_dummy,
2087 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2088 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2090 /* move cursor to the middle of the window to prevent
2091 * unwanted display move if the display is smaller than the
2093 XWarpPointer( p_vout->p_sys->p_display, None,
2094 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2095 mode.hdisplay / 2 , mode.vdisplay / 2 );
2096 /* force desktop view to upper left corner */
2097 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2098 p_vout->p_sys->i_screen, 0, 0 );
2103 p_vout->p_sys->p_win->i_width =
2104 DisplayWidth( p_vout->p_sys->p_display,
2105 p_vout->p_sys->i_screen );
2106 p_vout->p_sys->p_win->i_height =
2107 DisplayHeight( p_vout->p_sys->p_display,
2108 p_vout->p_sys->i_screen );
2113 XMoveResizeWindow( p_vout->p_sys->p_display,
2114 p_vout->p_sys->p_win->base_window,
2115 p_vout->p_sys->p_win->i_x,
2116 p_vout->p_sys->p_win->i_y,
2117 p_vout->p_sys->p_win->i_width,
2118 p_vout->p_sys->p_win->i_height );
2122 msg_Dbg( p_vout, "leaving fullscreen mode" );
2124 XReparentWindow( p_vout->p_sys->p_display,
2125 p_vout->p_sys->original_window.video_window,
2126 p_vout->p_sys->original_window.base_window, 0, 0 );
2128 p_vout->p_sys->fullscreen_window.video_window = None;
2129 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2130 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2132 XMapWindow( p_vout->p_sys->p_display,
2133 p_vout->p_sys->p_win->base_window );
2136 /* Unfortunately, using XSync() here is not enough to ensure the
2137 * window has already been mapped because the XMapWindow() request
2138 * has not necessarily been sent directly to our window (remember,
2139 * the call is first redirected to the window manager) */
2142 XWindowEvent( p_vout->p_sys->p_display,
2143 p_vout->p_sys->p_win->base_window,
2144 StructureNotifyMask, &xevent );
2145 } while( xevent.type != MapNotify );
2147 /* Be careful, this can generate a BadMatch error if the window is not
2148 * already mapped by the server (see above) */
2149 XSetInputFocus(p_vout->p_sys->p_display,
2150 p_vout->p_sys->p_win->base_window,
2154 /* signal that the size needs to be updated */
2155 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2158 /*****************************************************************************
2159 * EnableXScreenSaver: enable screen saver
2160 *****************************************************************************
2161 * This function enables the screen saver on a display after it has been
2162 * disabled by XDisableScreenSaver.
2163 * FIXME: what happens if multiple vlc sessions are running at the same
2165 *****************************************************************************/
2166 static void EnableXScreenSaver( vout_thread_t *p_vout )
2168 #ifdef DPMSINFO_IN_DPMS_H
2172 if( p_vout->p_sys->i_ss_timeout )
2174 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2175 p_vout->p_sys->i_ss_interval,
2176 p_vout->p_sys->i_ss_blanking,
2177 p_vout->p_sys->i_ss_exposure );
2180 /* Restore DPMS settings */
2181 #ifdef DPMSINFO_IN_DPMS_H
2182 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2184 if( p_vout->p_sys->b_ss_dpms )
2186 DPMSEnable( p_vout->p_sys->p_display );
2192 /*****************************************************************************
2193 * DisableXScreenSaver: disable screen saver
2194 *****************************************************************************
2195 * See XEnableXScreenSaver
2196 *****************************************************************************/
2197 static void DisableXScreenSaver( vout_thread_t *p_vout )
2199 #ifdef DPMSINFO_IN_DPMS_H
2203 /* Save screen saver information */
2204 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2205 &p_vout->p_sys->i_ss_interval,
2206 &p_vout->p_sys->i_ss_blanking,
2207 &p_vout->p_sys->i_ss_exposure );
2209 /* Disable screen saver */
2210 if( p_vout->p_sys->i_ss_timeout )
2212 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2213 p_vout->p_sys->i_ss_interval,
2214 p_vout->p_sys->i_ss_blanking,
2215 p_vout->p_sys->i_ss_exposure );
2219 #ifdef DPMSINFO_IN_DPMS_H
2220 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2223 /* Save DPMS current state */
2224 DPMSInfo( p_vout->p_sys->p_display, &unused,
2225 &p_vout->p_sys->b_ss_dpms );
2226 DPMSDisable( p_vout->p_sys->p_display );
2231 /*****************************************************************************
2232 * CreateCursor: create a blank mouse pointer
2233 *****************************************************************************/
2234 static void CreateCursor( vout_thread_t *p_vout )
2236 XColor cursor_color;
2238 p_vout->p_sys->cursor_pixmap =
2239 XCreatePixmap( p_vout->p_sys->p_display,
2240 DefaultRootWindow( p_vout->p_sys->p_display ),
2243 XParseColor( p_vout->p_sys->p_display,
2244 XCreateColormap( p_vout->p_sys->p_display,
2246 p_vout->p_sys->p_display ),
2248 p_vout->p_sys->p_display,
2249 p_vout->p_sys->i_screen ),
2251 "black", &cursor_color );
2253 p_vout->p_sys->blank_cursor =
2254 XCreatePixmapCursor( p_vout->p_sys->p_display,
2255 p_vout->p_sys->cursor_pixmap,
2256 p_vout->p_sys->cursor_pixmap,
2257 &cursor_color, &cursor_color, 1, 1 );
2260 /*****************************************************************************
2261 * DestroyCursor: destroy the blank mouse pointer
2262 *****************************************************************************/
2263 static void DestroyCursor( vout_thread_t *p_vout )
2265 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2268 /*****************************************************************************
2269 * ToggleCursor: hide or show the mouse pointer
2270 *****************************************************************************
2271 * This function hides the X pointer if it is visible by setting the pointer
2272 * sprite to a blank one. To show it again, we disable the sprite.
2273 *****************************************************************************/
2274 static void ToggleCursor( vout_thread_t *p_vout )
2276 if( p_vout->p_sys->b_mouse_pointer_visible )
2278 XDefineCursor( p_vout->p_sys->p_display,
2279 p_vout->p_sys->p_win->base_window,
2280 p_vout->p_sys->blank_cursor );
2281 p_vout->p_sys->b_mouse_pointer_visible = 0;
2285 XUndefineCursor( p_vout->p_sys->p_display,
2286 p_vout->p_sys->p_win->base_window );
2287 p_vout->p_sys->b_mouse_pointer_visible = 1;
2291 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2292 /*****************************************************************************
2293 * XVideoGetPort: get YUV12 port
2294 *****************************************************************************/
2295 static int XVideoGetPort( vout_thread_t *p_vout,
2296 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
2298 XvAdaptorInfo *p_adaptor;
2300 unsigned int i_adaptor, i_num_adaptors;
2301 int i_requested_adaptor;
2302 int i_selected_port;
2304 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2309 case XvBadExtension:
2310 msg_Warn( p_vout, "XvBadExtension" );
2314 msg_Warn( p_vout, "XvBadAlloc" );
2318 msg_Warn( p_vout, "XvQueryExtension failed" );
2322 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2323 DefaultRootWindow( p_vout->p_sys->p_display ),
2324 &i_num_adaptors, &p_adaptor ) )
2329 case XvBadExtension:
2330 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2334 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2338 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2342 i_selected_port = -1;
2343 #ifdef MODULE_NAME_IS_xvmc
2344 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2346 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2348 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2350 XvImageFormatValues *p_formats;
2351 int i_format, i_num_formats;
2354 /* If we requested an adaptor and it's not this one, we aren't
2356 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2361 /* If the adaptor doesn't have the required properties, skip it */
2362 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2363 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2368 /* Check that adaptor supports our requested format... */
2369 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2370 p_adaptor[i_adaptor].base_id,
2374 i_format < i_num_formats && ( i_selected_port == -1 );
2377 XvAttribute *p_attr;
2378 int i_attr, i_num_attributes;
2380 /* If this is not the format we want, or at least a
2381 * similar one, forget it */
2382 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2387 /* Look for the first available port supporting this format */
2388 for( i_port = p_adaptor[i_adaptor].base_id;
2389 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2390 + p_adaptor[i_adaptor].num_ports) )
2391 && ( i_selected_port == -1 );
2394 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2397 i_selected_port = i_port;
2398 *pi_newchroma = p_formats[ i_format ].id;
2402 /* If no free port was found, forget it */
2403 if( i_selected_port == -1 )
2408 /* If we found a port, print information about it */
2409 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2410 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2411 (char *)&p_formats[ i_format ].id,
2412 ( p_formats[ i_format ].format == XvPacked ) ?
2413 "packed" : "planar" );
2415 /* Make sure XV_AUTOPAINT_COLORKEY is set */
2416 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2418 &i_num_attributes );
2420 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2422 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2424 const Atom autopaint =
2425 XInternAtom( p_vout->p_sys->p_display,
2426 "XV_AUTOPAINT_COLORKEY", False );
2427 XvSetPortAttribute( p_vout->p_sys->p_display,
2428 i_selected_port, autopaint, 1 );
2433 if( p_attr != NULL )
2439 if( p_formats != NULL )
2446 if( i_num_adaptors > 0 )
2448 XvFreeAdaptorInfo( p_adaptor );
2451 if( i_selected_port == -1 )
2453 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2454 if( i_requested_adaptor == -1 )
2456 msg_Warn( p_vout, "no free XVideo port found for format "
2457 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2461 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2462 "XVideo port for format 0x%.8x (%4.4s)",
2463 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2467 return i_selected_port;
2470 /*****************************************************************************
2471 * XVideoReleasePort: release YUV12 port
2472 *****************************************************************************/
2473 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2475 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2479 /*****************************************************************************
2480 * InitDisplay: open and initialize X11 device
2481 *****************************************************************************
2482 * Create a window according to video output given size, and set other
2483 * properties according to the display properties.
2484 *****************************************************************************/
2485 static int InitDisplay( vout_thread_t *p_vout )
2487 #ifdef MODULE_NAME_IS_x11
2488 XPixmapFormatValues * p_formats; /* pixmap formats */
2489 XVisualInfo * p_xvisual; /* visuals information */
2490 XVisualInfo xvisual_template; /* visual template */
2491 int i_count, i; /* array size */
2494 #ifdef HAVE_SYS_SHM_H
2495 p_vout->p_sys->b_shm = 0;
2497 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
2500 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
2502 p_vout->p_sys->b_shm =
2503 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
2506 if( !p_vout->p_sys->b_shm )
2508 msg_Warn( p_vout, "XShm video extension is unavailable" );
2513 msg_Dbg( p_vout, "disabling XShm video extension" );
2517 msg_Warn( p_vout, "XShm video extension is unavailable" );
2521 #ifdef MODULE_NAME_IS_xvideo
2522 /* XXX The brightness and contrast values should be read from environment
2523 * XXX variables... */
2525 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2526 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2530 #ifdef MODULE_NAME_IS_x11
2531 /* Initialize structure */
2532 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2534 /* Get screen depth */
2535 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2536 p_vout->p_sys->i_screen );
2537 switch( p_vout->p_sys->i_screen_depth )
2541 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2543 xvisual_template.screen = p_vout->p_sys->i_screen;
2544 xvisual_template.class = DirectColor;
2545 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2546 VisualScreenMask | VisualClassMask,
2547 &xvisual_template, &i_count );
2548 if( p_xvisual == NULL )
2550 msg_Err( p_vout, "no PseudoColor visual available" );
2551 return VLC_EGENERIC;
2553 p_vout->p_sys->i_bytes_per_pixel = 1;
2554 p_vout->output.pf_setpalette = SetPalette;
2561 * Screen depth is higher than 8bpp. TrueColor visual is used.
2563 xvisual_template.screen = p_vout->p_sys->i_screen;
2564 xvisual_template.class = TrueColor;
2565 /* In some cases, we get a truecolor class adaptor that has a different
2566 color depth. So try to get a real true color one first */
2567 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2569 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2570 VisualScreenMask | VisualClassMask |
2572 &xvisual_template, &i_count );
2573 if( p_xvisual == NULL )
2575 msg_Warn( p_vout, "No screen matching the required color depth" );
2576 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2577 VisualScreenMask | VisualClassMask,
2578 &xvisual_template, &i_count );
2579 if( p_xvisual == NULL )
2582 msg_Err( p_vout, "no TrueColor visual available" );
2583 return VLC_EGENERIC;
2587 p_vout->output.i_rmask = p_xvisual->red_mask;
2588 p_vout->output.i_gmask = p_xvisual->green_mask;
2589 p_vout->output.i_bmask = p_xvisual->blue_mask;
2591 /* There is no difference yet between 3 and 4 Bpp. The only way
2592 * to find the actual number of bytes per pixel is to list supported
2593 * pixmap formats. */
2594 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2595 p_vout->p_sys->i_bytes_per_pixel = 0;
2597 for( i = 0; i < i_count; i++ )
2599 /* Under XFree4.0, the list contains pixmap formats available
2600 * through all video depths ; so we have to check against current
2602 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2604 if( p_formats[i].bits_per_pixel / 8
2605 > (int)p_vout->p_sys->i_bytes_per_pixel )
2607 p_vout->p_sys->i_bytes_per_pixel =
2608 p_formats[i].bits_per_pixel / 8;
2612 if( p_formats ) XFree( p_formats );
2616 p_vout->p_sys->p_visual = p_xvisual->visual;
2623 #ifdef HAVE_SYS_SHM_H
2624 /*****************************************************************************
2625 * CreateShmImage: create an XImage or XvImage using shared memory extension
2626 *****************************************************************************
2627 * Prepare an XImage or XvImage for display function.
2628 * The order of the operations respects the recommandations of the mit-shm
2629 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2630 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2631 *****************************************************************************/
2632 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2633 Display* p_display, EXTRA_ARGS_SHM,
2634 int i_width, int i_height )
2636 IMAGE_TYPE *p_image;
2639 /* Create XImage / XvImage */
2640 #ifdef MODULE_NAME_IS_xvideo
2642 /* Make sure the buffer is aligned to multiple of 16 */
2643 i_height = ( i_height + 15 ) >> 4 << 4;
2644 i_width = ( i_width + 15 ) >> 4 << 4;
2646 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2647 i_width, i_height, p_shm );
2648 #elif defined(MODULE_NAME_IS_xvmc)
2649 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2650 i_width, i_height, p_shm );
2652 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2653 p_shm, i_width, i_height );
2655 if( p_image == NULL )
2657 msg_Err( p_vout, "image creation failed" );
2661 /* Allocate shared memory segment - 0776 set the access permission
2662 * rights (like umask), they are not yet supported by all X servers */
2663 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2664 if( p_shm->shmid < 0 )
2666 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2667 strerror( errno ) );
2668 IMAGE_FREE( p_image );
2672 /* Attach shared memory segment to process (read/write) */
2673 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2674 if(! p_shm->shmaddr )
2676 msg_Err( p_vout, "cannot attach shared memory (%s)",
2678 IMAGE_FREE( p_image );
2679 shmctl( p_shm->shmid, IPC_RMID, 0 );
2683 /* Read-only data. We won't be using XShmGetImage */
2684 p_shm->readOnly = True;
2686 /* Attach shared memory segment to X server */
2687 XSynchronize( p_display, True );
2689 result = XShmAttach( p_display, p_shm );
2690 if( result == False || !b_shm )
2692 msg_Err( p_vout, "cannot attach shared memory to X server" );
2693 IMAGE_FREE( p_image );
2694 shmctl( p_shm->shmid, IPC_RMID, 0 );
2695 shmdt( p_shm->shmaddr );
2698 XSynchronize( p_display, False );
2700 /* Send image to X server. This instruction is required, since having
2701 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2702 * and remember NOT to use XFlush ! */
2703 XSync( p_display, False );
2706 /* Mark the shm segment to be removed when there are no more
2707 * attachements, so it is automatic on process exit or after shmdt */
2708 shmctl( p_shm->shmid, IPC_RMID, 0 );
2715 /*****************************************************************************
2716 * CreateImage: create an XImage or XvImage
2717 *****************************************************************************
2718 * Create a simple image used as a buffer.
2719 *****************************************************************************/
2720 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2721 Display *p_display, EXTRA_ARGS,
2722 int i_width, int i_height )
2724 byte_t * p_data; /* image data storage zone */
2725 IMAGE_TYPE *p_image;
2726 #ifdef MODULE_NAME_IS_x11
2727 int i_quantum; /* XImage quantum (see below) */
2728 int i_bytes_per_line;
2731 /* Allocate memory for image */
2732 #ifdef MODULE_NAME_IS_xvideo
2734 /* Make sure the buffer is aligned to multiple of 16 */
2735 i_height = ( i_height + 15 ) >> 4 << 4;
2736 i_width = ( i_width + 15 ) >> 4 << 4;
2738 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2739 #elif defined(MODULE_NAME_IS_x11)
2740 i_bytes_per_line = i_width * i_bytes_per_pixel;
2741 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2745 msg_Err( p_vout, "out of memory" );
2749 #ifdef MODULE_NAME_IS_x11
2750 /* Optimize the quantum of a scanline regarding its size - the quantum is
2751 a diviser of the number of bits between the start of two scanlines. */
2752 if( i_bytes_per_line & 0xf )
2756 else if( i_bytes_per_line & 0x10 )
2766 /* Create XImage. p_data will be automatically freed */
2767 #ifdef MODULE_NAME_IS_xvideo
2768 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2769 (char *)p_data, i_width, i_height );
2770 #elif defined(MODULE_NAME_IS_x11)
2771 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2772 (char *)p_data, i_width, i_height, i_quantum, 0 );
2774 if( p_image == NULL )
2776 msg_Err( p_vout, "XCreateImage() failed" );
2784 /*****************************************************************************
2785 * X11ErrorHandler: replace error handler so we can intercept some of them
2786 *****************************************************************************/
2787 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2789 /* Ingnore errors on XSetInputFocus()
2790 * (they happen when a window is not yet mapped) */
2791 if( event->request_code == X_SetInputFocus )
2793 fprintf(stderr, "XSetInputFocus failed\n");
2797 if( event->request_code == 150 /* MIT-SHM */ &&
2798 event->minor_code == X_ShmAttach )
2800 fprintf(stderr, "XShmAttach failed\n");
2805 XSetErrorHandler(NULL);
2806 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2809 #ifdef MODULE_NAME_IS_x11
2810 /*****************************************************************************
2811 * SetPalette: sets an 8 bpp palette
2812 *****************************************************************************
2813 * This function sets the palette given as an argument. It does not return
2814 * anything, but could later send information on which colors it was unable
2816 *****************************************************************************/
2817 static void SetPalette( vout_thread_t *p_vout,
2818 uint16_t *red, uint16_t *green, uint16_t *blue )
2821 XColor p_colors[255];
2823 /* allocate palette */
2824 for( i = 0; i < 255; i++ )
2826 /* kludge: colors are indexed reversely because color 255 seems
2827 * to be reserved for black even if we try to set it to white */
2828 p_colors[ i ].pixel = 255 - i;
2829 p_colors[ i ].pad = 0;
2830 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2831 p_colors[ i ].red = red[ 255 - i ];
2832 p_colors[ i ].blue = blue[ 255 - i ];
2833 p_colors[ i ].green = green[ 255 - i ];
2836 XStoreColors( p_vout->p_sys->p_display,
2837 p_vout->p_sys->colormap, p_colors, 255 );
2841 /*****************************************************************************
2842 * Control: control facility for the vout
2843 *****************************************************************************/
2844 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2847 unsigned int i_width, i_height;
2848 unsigned int *pi_width, *pi_height;
2854 if( p_vout->p_sys->p_win->owner_window )
2855 return vout_ControlWindow( p_vout,
2856 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2858 pi_width = va_arg( args, unsigned int * );
2859 pi_height = va_arg( args, unsigned int * );
2861 vlc_mutex_lock( &p_vout->p_sys->lock );
2862 *pi_width = p_vout->p_sys->p_win->i_width;
2863 *pi_height = p_vout->p_sys->p_win->i_height;
2864 vlc_mutex_unlock( &p_vout->p_sys->lock );
2868 if( p_vout->p_sys->p_win->owner_window )
2869 return vout_ControlWindow( p_vout,
2870 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2872 vlc_mutex_lock( &p_vout->p_sys->lock );
2874 i_width = va_arg( args, unsigned int );
2875 i_height = va_arg( args, unsigned int );
2876 if( !i_width ) i_width = p_vout->i_window_width;
2877 if( !i_height ) i_height = p_vout->i_window_height;
2879 #ifdef MODULE_NAME_IS_xvmc
2880 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
2882 /* Update dimensions */
2883 XResizeWindow( p_vout->p_sys->p_display,
2884 p_vout->p_sys->p_win->base_window,
2885 i_width, i_height );
2886 #ifdef MODULE_NAME_IS_xvmc
2887 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
2889 vlc_mutex_unlock( &p_vout->p_sys->lock );
2893 vlc_mutex_lock( &p_vout->p_sys->lock );
2894 XUnmapWindow( p_vout->p_sys->p_display,
2895 p_vout->p_sys->original_window.base_window );
2896 vlc_mutex_unlock( &p_vout->p_sys->lock );
2900 vlc_mutex_lock( &p_vout->p_sys->lock );
2901 if( i_query == VOUT_REPARENT ) d = (Drawable)va_arg( args, int );
2903 #ifdef MODULE_NAME_IS_xvmc
2904 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
2906 XReparentWindow( p_vout->p_sys->p_display,
2907 p_vout->p_sys->original_window.base_window,
2908 DefaultRootWindow( p_vout->p_sys->p_display ),
2911 XReparentWindow( p_vout->p_sys->p_display,
2912 p_vout->p_sys->original_window.base_window,
2914 XSync( p_vout->p_sys->p_display, False );
2915 p_vout->p_sys->original_window.owner_window = 0;
2916 #ifdef MODULE_NAME_IS_xvmc
2917 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
2919 vlc_mutex_unlock( &p_vout->p_sys->lock );
2920 return vout_vaControlDefault( p_vout, i_query, args );
2922 case VOUT_SET_STAY_ON_TOP:
2923 if( p_vout->p_sys->p_win->owner_window )
2924 return vout_ControlWindow( p_vout,
2925 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2927 b_arg = va_arg( args, vlc_bool_t );
2928 vlc_mutex_lock( &p_vout->p_sys->lock );
2929 #ifdef MODULE_NAME_IS_xvmc
2930 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
2932 WindowOnTop( p_vout, b_arg );
2933 #ifdef MODULE_NAME_IS_xvmc
2934 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
2936 vlc_mutex_unlock( &p_vout->p_sys->lock );
2940 return vout_vaControlDefault( p_vout, i_query, args );
2944 /*****************************************************************************
2945 * TestNetWMSupport: tests for Extended Window Manager Hints support
2946 *****************************************************************************/
2947 static void TestNetWMSupport( vout_thread_t *p_vout )
2949 int i_ret, i_format;
2950 unsigned long i, i_items, i_bytesafter;
2951 Atom net_wm_supported;
2952 union { Atom *p_atom; unsigned char *p_char; } p_args;
2954 p_args.p_atom = NULL;
2956 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
2957 p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
2958 p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
2959 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
2962 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
2964 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
2965 DefaultRootWindow( p_vout->p_sys->p_display ),
2967 0, 16384, False, AnyPropertyType,
2969 &i_format, &i_items, &i_bytesafter,
2970 (unsigned char **)&p_args );
2972 if( i_ret != Success || i_items == 0 ) return;
2974 msg_Dbg( p_vout, "Window manager supports NetWM" );
2976 p_vout->p_sys->net_wm_state =
2977 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
2978 p_vout->p_sys->net_wm_state_fullscreen =
2979 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
2981 p_vout->p_sys->net_wm_state_above =
2982 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
2983 p_vout->p_sys->net_wm_state_below =
2984 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
2985 p_vout->p_sys->net_wm_state_stays_on_top =
2986 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
2989 for( i = 0; i < i_items; i++ )
2991 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
2994 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
2995 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
2997 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
2999 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3000 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
3002 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3004 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3005 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
3007 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3010 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3011 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
3015 XFree( p_args.p_atom );
3018 /*****************************************************************************
3019 * Key events handling
3020 *****************************************************************************/
3026 } x11keys_to_vlckeys[] =
3028 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3029 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3030 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3031 { XK_F12, KEY_F12 },
3033 { XK_Return, KEY_ENTER },
3034 { XK_KP_Enter, KEY_ENTER },
3035 { XK_space, KEY_SPACE },
3036 { XK_Escape, KEY_ESC },
3038 { XK_Menu, KEY_MENU },
3039 { XK_Left, KEY_LEFT },
3040 { XK_Right, KEY_RIGHT },
3042 { XK_Down, KEY_DOWN },
3044 { XK_Home, KEY_HOME },
3045 { XK_End, KEY_END },
3046 { XK_Page_Up, KEY_PAGEUP },
3047 { XK_Page_Down, KEY_PAGEDOWN },
3049 { XK_Insert, KEY_INSERT },
3050 { XK_Delete, KEY_DELETE },
3055 static int ConvertKey( int i_key )
3059 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3061 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3063 return x11keys_to_vlckeys[i].i_vlckey;
3070 /*****************************************************************************
3071 * WindowOnTop: Switches the "always on top" state of the video window.
3072 *****************************************************************************/
3073 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
3075 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3077 XClientMessageEvent event;
3079 memset( &event, 0, sizeof( XClientMessageEvent ) );
3081 event.type = ClientMessage;
3082 event.message_type = p_vout->p_sys->net_wm_state;
3083 event.display = p_vout->p_sys->p_display;
3084 event.window = p_vout->p_sys->p_win->base_window;
3086 event.data.l[ 0 ] = b_on_top; /* set property */
3087 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3089 XSendEvent( p_vout->p_sys->p_display,
3090 DefaultRootWindow( p_vout->p_sys->p_display ),
3091 False, SubstructureRedirectMask,
3095 /* use _NET_WM_STATE_ABOVE if window manager
3096 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3097 else if( p_vout->p_sys->b_net_wm_state_above )
3099 XClientMessageEvent event;
3101 memset( &event, 0, sizeof( XClientMessageEvent ) );
3103 event.type = ClientMessage;
3104 event.message_type = p_vout->p_sys->net_wm_state;
3105 event.display = p_vout->p_sys->p_display;
3106 event.window = p_vout->p_sys->p_win->base_window;
3108 event.data.l[ 0 ] = b_on_top; /* set property */
3109 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3111 XSendEvent( p_vout->p_sys->p_display,
3112 DefaultRootWindow( p_vout->p_sys->p_display ),
3113 False, SubstructureRedirectMask,