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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
38 #include <vlc_window.h>
41 #include <errno.h> /* ENOMEM */
43 #ifdef HAVE_MACHINE_PARAM_H
45 # include <machine/param.h>
46 # include <sys/types.h> /* typedef ushort */
51 # include <netinet/in.h> /* BSD: struct in_addr */
55 #include <X11/extensions/Xsp.h>
59 # include <sys/shm.h> /* shmget(), shmctl() */
63 #include <X11/Xproto.h>
65 #include <X11/Xutil.h>
66 #include <X11/keysym.h>
67 #include <X11/XF86keysym.h>
69 # include <X11/extensions/XShm.h>
71 #ifdef DPMSINFO_IN_DPMS_H
72 # include <X11/extensions/dpms.h>
75 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
76 # include <X11/extensions/Xv.h>
77 # include <X11/extensions/Xvlib.h>
80 #ifdef MODULE_NAME_IS_glx
85 # include <X11/extensions/Xinerama.h>
88 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
89 # include <X11/extensions/xf86vmode.h>
92 #ifdef MODULE_NAME_IS_xvmc
93 # include <X11/extensions/vldXvMC.h>
94 # include "../../codec/xvmc/accel_xvmc.h"
99 /*****************************************************************************
101 *****************************************************************************/
102 int Activate ( vlc_object_t * );
103 void Deactivate ( vlc_object_t * );
105 static int InitVideo ( vout_thread_t * );
106 static void EndVideo ( vout_thread_t * );
107 static void DisplayVideo ( vout_thread_t *, picture_t * );
108 static int ManageVideo ( vout_thread_t * );
109 static int Control ( vout_thread_t *, int, va_list );
111 static int InitDisplay ( vout_thread_t * );
113 static int CreateWindow ( vout_thread_t *, x11_window_t * );
114 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
116 static int NewPicture ( vout_thread_t *, picture_t * );
117 static void FreePicture ( vout_thread_t *, picture_t * );
119 #ifndef MODULE_NAME_IS_glx
120 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
121 Display *, EXTRA_ARGS, int, int );
124 #ifdef HAVE_SYS_SHM_H
125 #ifndef MODULE_NAME_IS_glx
126 IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
127 Display *, EXTRA_ARGS_SHM, int, int );
129 static int i_shm_major = 0;
132 static void ToggleFullScreen ( vout_thread_t * );
134 static void EnableXScreenSaver ( vout_thread_t * );
135 static void DisableXScreenSaver ( vout_thread_t * );
137 static void CreateCursor ( vout_thread_t * );
138 static void DestroyCursor ( vout_thread_t * );
139 static void ToggleCursor ( vout_thread_t * );
141 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
142 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, picture_heap_t * );
143 static void XVideoReleasePort( vout_thread_t *, int );
146 #ifdef MODULE_NAME_IS_x11
147 static void SetPalette ( vout_thread_t *,
148 uint16_t *, uint16_t *, uint16_t * );
151 #ifdef MODULE_NAME_IS_xvmc
152 static void RenderVideo ( vout_thread_t *, picture_t * );
153 static int xvmc_check_yv12( Display *display, XvPortID port );
154 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
157 static void TestNetWMSupport( vout_thread_t * );
158 static int ConvertKey( int );
160 static int WindowOnTop( vout_thread_t *, bool );
162 static int X11ErrorHandler( Display *, XErrorEvent * );
165 static void EnablePixelDoubling( vout_thread_t *p_vout );
166 static void DisablePixelDoubling( vout_thread_t *p_vout );
170 static const int i_backlight_on_interval = 300;
175 /*****************************************************************************
176 * Activate: allocate X11 video thread output method
177 *****************************************************************************
178 * This function allocate and initialize a X11 vout method. It uses some of the
179 * vout properties to choose the window size, and change them according to the
180 * actual properties of the display.
181 *****************************************************************************/
182 int Activate ( vlc_object_t *p_this )
184 vout_thread_t *p_vout = (vout_thread_t *)p_this;
187 #if defined(MODULE_NAME_IS_xvmc)
190 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
192 vlc_fourcc_t i_chroma = 0;
196 p_vout->pf_init = InitVideo;
197 p_vout->pf_end = EndVideo;
198 p_vout->pf_manage = ManageVideo;
199 #ifdef MODULE_NAME_IS_xvmc
200 p_vout->pf_render = RenderVideo;
202 p_vout->pf_render = NULL;
204 p_vout->pf_display = DisplayVideo;
205 p_vout->pf_control = Control;
207 /* Allocate structure */
208 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
209 if( p_vout->p_sys == NULL )
212 vlc_mutex_init( &p_vout->p_sys->lock );
214 /* key and mouse event handling */
215 p_vout->p_sys->i_vout_event = var_CreateGetInteger( p_vout, "vout-event" );
217 /* Open display, using the "display" config variable or the DISPLAY
218 * environment variable */
219 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
221 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
223 if( p_vout->p_sys->p_display == NULL ) /* error */
225 msg_Err( p_vout, "cannot open display %s",
226 XDisplayName( psz_display ) );
227 free( p_vout->p_sys );
233 /* Replace error handler so we can intercept some non-fatal errors */
234 XSetErrorHandler( X11ErrorHandler );
236 /* Get a screen ID matching the XOpenDisplay return value */
237 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
239 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
240 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
243 if( strlen( psz_chroma ) >= 4 )
245 /* Do not use direct assignment because we are not sure of the
247 memcpy(&i_chroma, psz_chroma, 4);
256 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
257 i_chroma, (char*)&i_chroma );
261 i_chroma = p_vout->render.i_chroma;
264 /* Check that we have access to an XVideo port providing this chroma */
265 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
267 if( p_vout->p_sys->i_xvport < 0 )
269 /* If a specific chroma format was requested, then we don't try to
270 * be cleverer than the user. He knew pretty well what he wanted. */
273 XCloseDisplay( p_vout->p_sys->p_display );
274 free( p_vout->p_sys );
278 /* It failed, but it's not completely lost ! We try to open an
279 * XVideo port for an YUY2 picture. We'll need to do an YUV
280 * conversion, but at least it has got scaling. */
281 p_vout->p_sys->i_xvport =
282 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
284 if( p_vout->p_sys->i_xvport < 0 )
286 /* It failed, but it's not completely lost ! We try to open an
287 * XVideo port for a simple 16bpp RGB picture. We'll need to do
288 * an YUV conversion, but at least it has got scaling. */
289 p_vout->p_sys->i_xvport =
290 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
292 if( p_vout->p_sys->i_xvport < 0 )
294 XCloseDisplay( p_vout->p_sys->p_display );
295 free( p_vout->p_sys );
300 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
301 #elif defined(MODULE_NAME_IS_glx)
303 int i_opcode, i_evt, i_err = 0;
304 int i_maj, i_min = 0;
306 /* Check for GLX extension */
307 if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
308 &i_opcode, &i_evt, &i_err ) )
310 msg_Err( p_this, "GLX extension not supported" );
311 XCloseDisplay( p_vout->p_sys->p_display );
312 free( p_vout->p_sys );
315 if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
317 msg_Err( p_this, "glXQueryExtension failed" );
318 XCloseDisplay( p_vout->p_sys->p_display );
319 free( p_vout->p_sys );
323 /* Check GLX version */
324 if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
326 msg_Err( p_this, "glXQueryVersion failed" );
327 XCloseDisplay( p_vout->p_sys->p_display );
328 free( p_vout->p_sys );
331 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
333 p_vout->p_sys->b_glx13 = false;
334 msg_Dbg( p_this, "using GLX 1.2 API" );
338 p_vout->p_sys->b_glx13 = true;
339 msg_Dbg( p_this, "using GLX 1.3 API" );
344 /* Create blank cursor (for mouse cursor autohiding) */
345 p_vout->p_sys->i_time_mouse_last_moved = mdate();
346 p_vout->p_sys->i_mouse_hide_timeout =
347 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
348 p_vout->p_sys->b_mouse_pointer_visible = 1;
349 CreateCursor( p_vout );
351 /* Set main window's size */
352 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
353 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
354 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
355 /* Spawn base window - this window will include the video output window,
356 * but also command buttons, subtitles and other indicators */
357 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
359 msg_Err( p_vout, "cannot create X11 window" );
360 DestroyCursor( p_vout );
361 XCloseDisplay( p_vout->p_sys->p_display );
362 free( p_vout->p_sys );
366 /* Open and initialize device. */
367 if( InitDisplay( p_vout ) )
369 msg_Err( p_vout, "cannot initialize X11 display" );
370 DestroyCursor( p_vout );
371 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
372 XCloseDisplay( p_vout->p_sys->p_display );
373 free( p_vout->p_sys );
377 /* Disable screen saver */
378 DisableXScreenSaver( p_vout );
381 p_vout->p_sys->b_altfullscreen = 0;
382 p_vout->p_sys->i_time_button_last_pressed = 0;
384 TestNetWMSupport( p_vout );
386 #ifdef MODULE_NAME_IS_xvmc
387 p_vout->p_sys->p_last_subtitle_save = NULL;
388 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
390 /* Look what method was requested */
391 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
392 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
395 if( (strcmp(psz_value, "bob") == 0) ||
396 (strcmp(psz_value, "blend") == 0) )
397 p_vout->p_sys->xvmc_deinterlace_method = 2;
398 else if (strcmp(psz_value, "discard") == 0)
399 p_vout->p_sys->xvmc_deinterlace_method = 1;
401 p_vout->p_sys->xvmc_deinterlace_method = 0;
405 p_vout->p_sys->xvmc_deinterlace_method = 0;
407 /* Look what method was requested */
408 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
409 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
410 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
414 if( strncmp( psz_value, "eq", 2 ) == 0 )
415 p_vout->p_sys->xvmc_crop_style = 1;
416 else if( strncmp( psz_value, "4-16", 4 ) == 0)
417 p_vout->p_sys->xvmc_crop_style = 2;
418 else if( strncmp( psz_value, "16-4", 4 ) == 0)
419 p_vout->p_sys->xvmc_crop_style = 3;
421 p_vout->p_sys->xvmc_crop_style = 0;
425 p_vout->p_sys->xvmc_crop_style = 0;
427 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
428 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
430 if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
432 msg_Err( p_vout, "no XVMC capability found" );
433 Deactivate( p_vout );
436 subpicture_t sub_pic;
437 sub_pic.p_sys = NULL;
438 p_vout->p_sys->last_date = 0;
442 p_vout->p_sys->i_hw_scale = 1;
446 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
447 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
448 if ( p_vout->p_sys->p_octx == NULL ) {
449 msg_Err( p_vout, "Could not get osso context" );
451 msg_Dbg( p_vout, "Initialized osso context" );
455 /* Variable to indicate if the window should be on top of others */
456 /* Trigger a callback right now */
457 var_Get( p_vout, "video-on-top", &val );
458 var_Set( p_vout, "video-on-top", val );
463 /*****************************************************************************
464 * Deactivate: destroy X11 video thread output method
465 *****************************************************************************
466 * Terminate an output method created by Open
467 *****************************************************************************/
468 void Deactivate ( vlc_object_t *p_this )
470 vout_thread_t *p_vout = (vout_thread_t *)p_this;
472 /* If the fullscreen window is still open, close it */
473 if( p_vout->b_fullscreen )
475 ToggleFullScreen( p_vout );
478 /* Restore cursor if it was blanked */
479 if( !p_vout->p_sys->b_mouse_pointer_visible )
481 ToggleCursor( p_vout );
484 #ifdef MODULE_NAME_IS_x11
485 /* Destroy colormap */
486 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
488 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
490 #elif defined(MODULE_NAME_IS_xvideo)
491 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
492 #elif defined(MODULE_NAME_IS_xvmc)
493 if( p_vout->p_sys->xvmc_cap )
495 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
496 xxmc_dispose_context( p_vout );
497 if( p_vout->p_sys->old_subpic )
499 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
500 p_vout->p_sys->old_subpic = NULL;
502 if( p_vout->p_sys->new_subpic )
504 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
505 p_vout->p_sys->new_subpic = NULL;
507 free( p_vout->p_sys->xvmc_cap );
508 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
513 DisablePixelDoubling(p_vout);
516 DestroyCursor( p_vout );
517 EnableXScreenSaver( p_vout );
518 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
519 XCloseDisplay( p_vout->p_sys->p_display );
521 /* Destroy structure */
522 vlc_mutex_destroy( &p_vout->p_sys->lock );
523 #ifdef MODULE_NAME_IS_xvmc
524 free_context_lock( &p_vout->p_sys->xvmc_lock );
528 if ( p_vout->p_sys->p_octx != NULL ) {
529 msg_Dbg( p_vout, "Deinitializing osso context" );
530 osso_deinitialize( p_vout->p_sys->p_octx );
534 free( p_vout->p_sys );
537 #ifdef MODULE_NAME_IS_xvmc
539 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
542 static int xvmc_check_yv12( Display *display, XvPortID port )
544 XvImageFormatValues *formatValues;
548 formatValues = XvListImageFormats( display, port, &formats );
550 for( i = 0; i < formats; i++ )
552 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
553 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
555 XFree (formatValues);
560 XFree (formatValues);
564 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
566 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
569 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
572 int xv_double_buffer;
574 xv_double_buffer = 1;
576 XLockDisplay( p_vout->p_sys->p_display );
577 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
579 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
581 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
582 XUnlockDisplay( p_vout->p_sys->p_display );
584 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
585 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
588 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
590 vlc_xxmc_t *xxmc = NULL;
592 vlc_mutex_lock( &p_vout->p_sys->lock );
593 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
595 xxmc = &p_pic->p_sys->xxmc_data;
596 if( (!xxmc->decoded ||
597 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
599 vlc_mutex_unlock( &p_vout->p_sys->lock );
600 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
605 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
606 if (p_vout->p_sys->p_last_subtitle != NULL)
608 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
610 p_vout->p_sys->new_subpic =
611 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
612 p_vout->p_sys->xvmc_width,
613 p_vout->p_sys->xvmc_height,
614 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
616 if (p_vout->p_sys->new_subpic)
618 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
619 XvMCClearSubpicture( p_vout->p_sys->p_display,
620 p_vout->p_sys->new_subpic,
623 p_vout->p_sys->xvmc_width,
624 p_vout->p_sys->xvmc_height,
626 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
627 clear_xx44_palette( &p_vout->p_sys->palette );
629 if( sub_pic.p_sys == NULL )
631 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
632 if( sub_pic.p_sys != NULL )
634 sub_pic.p_sys->p_vout = p_vout;
635 sub_pic.p_sys->xvmc_surf = NULL;
636 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
639 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
640 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
641 sub_pic.p->i_pitch = p_vout->output.i_width;
643 memset( p_vout->p_sys->subImage->data, 0,
644 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
646 if (p_vout->p_last_subtitle != NULL)
648 blend_xx44( p_vout->p_sys->subImage->data,
649 p_vout->p_last_subtitle,
650 p_vout->p_sys->subImage->width,
651 p_vout->p_sys->subImage->height,
652 p_vout->p_sys->subImage->width,
653 &p_vout->p_sys->palette,
654 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
657 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
658 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
659 p_vout->p_sys->new_subpic,
660 p_vout->p_sys->subImage,
663 p_vout->output.i_width, /* overlay->width, */
664 p_vout->output.i_height, /* overlay->height */
667 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
668 if (p_vout->p_sys->old_subpic)
670 xxmc_xvmc_free_subpicture( p_vout,
671 p_vout->p_sys->old_subpic);
672 p_vout->p_sys->old_subpic = NULL;
674 if (p_vout->p_sys->new_subpic)
676 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
677 p_vout->p_sys->new_subpic = NULL;
678 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
679 p_vout->p_sys->xvmc_palette,
681 p_vout->p_sys->old_subpic->num_palette_entries,
682 p_vout->p_sys->old_subpic->entry_bytes,
683 p_vout->p_sys->old_subpic->component_order );
684 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
685 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
686 p_vout->p_sys->old_subpic,
687 p_vout->p_sys->xvmc_palette );
688 XvMCFlushSubpicture( p_vout->p_sys->p_display,
689 p_vout->p_sys->old_subpic);
690 XvMCSyncSubpicture( p_vout->p_sys->p_display,
691 p_vout->p_sys->old_subpic );
692 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
695 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
696 if (p_vout->p_sys->xvmc_backend_subpic )
698 XvMCBlendSubpicture( p_vout->p_sys->p_display,
699 p_pic->p_sys->xvmc_surf,
700 p_vout->p_sys->old_subpic,
703 p_vout->p_sys->xvmc_width,
704 p_vout->p_sys->xvmc_height,
707 p_vout->p_sys->xvmc_width,
708 p_vout->p_sys->xvmc_height );
712 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
713 p_pic->p_sys->xvmc_surf,
714 p_pic->p_sys->xvmc_surf,
715 p_vout->p_sys->old_subpic,
718 p_vout->p_sys->xvmc_width,
719 p_vout->p_sys->xvmc_height,
722 p_vout->p_sys->xvmc_width,
723 p_vout->p_sys->xvmc_height );
725 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
730 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
731 if( p_vout->p_sys->xvmc_backend_subpic )
733 XvMCBlendSubpicture( p_vout->p_sys->p_display,
734 p_pic->p_sys->xvmc_surf,
735 p_vout->p_sys->old_subpic,
737 p_vout->p_sys->xvmc_width,
738 p_vout->p_sys->xvmc_height,
740 p_vout->p_sys->xvmc_width,
741 p_vout->p_sys->xvmc_height );
745 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
746 p_pic->p_sys->xvmc_surf,
747 p_pic->p_sys->xvmc_surf,
748 p_vout->p_sys->old_subpic,
750 p_vout->p_sys->xvmc_width,
751 p_vout->p_sys->xvmc_height,
753 p_vout->p_sys->xvmc_width,
754 p_vout->p_sys->xvmc_height );
756 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
759 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
761 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
763 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
765 vlc_mutex_unlock( &p_vout->p_sys->lock );
770 /*****************************************************************************
771 * EnablePixelDoubling: Enables pixel doubling
772 *****************************************************************************
773 * Checks if the double size image fits in current window, and enables pixel
774 * doubling accordingly. The i_hw_scale is the integer scaling factor.
775 *****************************************************************************/
776 static void EnablePixelDoubling( vout_thread_t *p_vout )
778 int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
779 int i_vert_scale = ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
780 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
781 p_vout->p_sys->i_hw_scale = 2;
782 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
783 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
787 /*****************************************************************************
788 * DisablePixelDoubling: Disables pixel doubling
789 *****************************************************************************
790 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
791 *****************************************************************************/
792 static void DisablePixelDoubling( vout_thread_t *p_vout )
794 if ( p_vout->p_sys->i_hw_scale > 1 ) {
795 msg_Dbg( p_vout, "Disabling pixel doubling" );
796 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
797 p_vout->p_sys->i_hw_scale = 1;
804 /*****************************************************************************
805 * InitVideo: initialize X11 video thread output method
806 *****************************************************************************
807 * This function create the XImages needed by the output thread. It is called
808 * at the beginning of the thread, but also each time the window is resized.
809 *****************************************************************************/
810 static int InitVideo( vout_thread_t *p_vout )
812 unsigned int i_index = 0;
815 I_OUTPUTPICTURES = 0;
817 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
818 /* Initialize the output structure; we already found an XVideo port,
819 * and the corresponding chroma we will be using. Since we can
820 * arbitrary scale, stick to the coordinates and aspect. */
821 p_vout->output.i_width = p_vout->render.i_width;
822 p_vout->output.i_height = p_vout->render.i_height;
823 p_vout->output.i_aspect = p_vout->render.i_aspect;
825 p_vout->fmt_out = p_vout->fmt_in;
826 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
828 #if XvVersion < 2 || ( XvVersion == 2 && XvRevision < 2 )
829 switch( p_vout->output.i_chroma )
831 case VLC_FOURCC('R','V','1','6'):
832 #if defined( WORDS_BIGENDIAN )
833 p_vout->output.i_rmask = 0xf800;
834 p_vout->output.i_gmask = 0x07e0;
835 p_vout->output.i_bmask = 0x001f;
837 p_vout->output.i_rmask = 0x001f;
838 p_vout->output.i_gmask = 0x07e0;
839 p_vout->output.i_bmask = 0xf800;
842 case VLC_FOURCC('R','V','1','5'):
843 #if defined( WORDS_BIGENDIAN )
844 p_vout->output.i_rmask = 0x7c00;
845 p_vout->output.i_gmask = 0x03e0;
846 p_vout->output.i_bmask = 0x001f;
848 p_vout->output.i_rmask = 0x001f;
849 p_vout->output.i_gmask = 0x03e0;
850 p_vout->output.i_bmask = 0x7c00;
856 #elif defined(MODULE_NAME_IS_x11)
857 /* Initialize the output structure: RGB with square pixels, whatever
858 * the input format is, since it's the only format we know */
859 switch( p_vout->p_sys->i_screen_depth )
861 case 8: /* FIXME: set the palette */
862 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
864 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
866 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
869 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
871 msg_Err( p_vout, "unknown screen depth %i",
872 p_vout->p_sys->i_screen_depth );
877 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width / p_vout->p_sys->i_hw_scale,
878 p_vout->p_sys->p_win->i_height / p_vout->p_sys->i_hw_scale,
880 &p_vout->fmt_out.i_visible_width,
881 &p_vout->fmt_out.i_visible_height );
883 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
884 p_vout->p_sys->p_win->i_height,
886 &p_vout->fmt_out.i_visible_width,
887 &p_vout->fmt_out.i_visible_height );
890 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
892 p_vout->output.i_width = p_vout->fmt_out.i_width =
893 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
894 p_vout->fmt_in.i_visible_width;
895 p_vout->output.i_height = p_vout->fmt_out.i_height =
896 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
897 p_vout->fmt_in.i_visible_height;
898 p_vout->fmt_out.i_x_offset =
899 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
900 p_vout->fmt_in.i_visible_width;
901 p_vout->fmt_out.i_y_offset =
902 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
903 p_vout->fmt_in.i_visible_height;
905 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
906 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
907 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
909 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
910 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
911 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
912 p_vout->fmt_out.i_visible_width,
913 p_vout->fmt_out.i_visible_height );
916 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
917 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
921 /* Find an empty picture slot */
922 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
924 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
926 p_pic = p_vout->p_picture + i_index;
931 /* Allocate the picture */
932 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
937 p_pic->i_status = DESTROYED_PICTURE;
938 p_pic->i_type = DIRECT_PICTURE;
940 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
945 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
947 /* U and V inverted compared to I420
948 * Fixme: this should be handled by the vout core */
949 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
950 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
956 /*****************************************************************************
957 * DisplayVideo: displays previously rendered output
958 *****************************************************************************
959 * This function sends the currently rendered image to X11 server.
960 * (The Xv extension takes care of "double-buffering".)
961 *****************************************************************************/
962 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
964 unsigned int i_width, i_height, i_x, i_y;
966 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
967 p_vout->p_sys->p_win->i_height,
968 &i_x, &i_y, &i_width, &i_height );
970 vlc_mutex_lock( &p_vout->p_sys->lock );
972 #ifdef MODULE_NAME_IS_xvmc
973 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
975 vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
976 if( !xxmc->decoded ||
977 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
979 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
981 xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
982 vlc_mutex_unlock( &p_vout->p_sys->lock );
983 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
987 int src_width = p_vout->output.i_width;
988 int src_height = p_vout->output.i_height;
991 if( p_vout->p_sys->xvmc_crop_style == 1 )
998 else if( p_vout->p_sys->xvmc_crop_style == 2 )
1005 else if( p_vout->p_sys->xvmc_crop_style == 3 )
1019 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1020 { /* BOB DEINTERLACE */
1021 if( (p_pic->p_sys->nb_display == 0) ||
1022 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1024 first_field = (p_pic->b_top_field_first) ?
1025 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1029 first_field = (p_pic->b_top_field_first) ?
1030 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1035 first_field = XVMC_FRAME_PICTURE;
1038 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1039 XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
1040 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1041 XvMCPutSurface( p_vout->p_sys->p_display,
1042 p_pic->p_sys->xvmc_surf,
1043 p_vout->p_sys->p_win->video_window,
1054 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1055 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1056 { /* BOB DEINTERLACE */
1057 if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1059 mtime_t last_date = p_pic->date;
1061 vlc_mutex_lock( &p_vout->picture_lock );
1062 if( !p_vout->p_sys->last_date )
1064 p_pic->date += 20000;
1068 p_pic->date = ((3 * p_pic->date -
1069 p_vout->p_sys->last_date) / 2 );
1071 p_vout->p_sys->last_date = last_date;
1073 p_pic->p_sys->nb_display = 1;
1074 vlc_mutex_unlock( &p_vout->picture_lock );
1078 p_pic->p_sys->nb_display = 0;
1082 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1085 #ifdef HAVE_SYS_SHM_H
1086 if( p_vout->p_sys->i_shm_opcode )
1088 /* Display rendered image using shared memory extension */
1089 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1090 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1091 p_vout->p_sys->p_win->video_window,
1092 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1093 p_vout->fmt_out.i_x_offset,
1094 p_vout->fmt_out.i_y_offset,
1095 p_vout->fmt_out.i_visible_width,
1096 p_vout->fmt_out.i_visible_height,
1097 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1098 False /* Don't put True here or you'll waste your CPU */ );
1100 XShmPutImage( p_vout->p_sys->p_display,
1101 p_vout->p_sys->p_win->video_window,
1102 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1103 p_vout->fmt_out.i_x_offset,
1104 p_vout->fmt_out.i_y_offset,
1105 0 /*dest_x*/, 0 /*dest_y*/,
1106 p_vout->fmt_out.i_visible_width,
1107 p_vout->fmt_out.i_visible_height,
1108 False /* Don't put True here ! */ );
1112 #endif /* HAVE_SYS_SHM_H */
1114 /* Use standard XPutImage -- this is gonna be slow ! */
1115 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1116 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1117 p_vout->p_sys->p_win->video_window,
1118 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1119 p_vout->fmt_out.i_x_offset,
1120 p_vout->fmt_out.i_y_offset,
1121 p_vout->fmt_out.i_visible_width,
1122 p_vout->fmt_out.i_visible_height,
1123 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1125 XPutImage( p_vout->p_sys->p_display,
1126 p_vout->p_sys->p_win->video_window,
1127 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1128 p_vout->fmt_out.i_x_offset,
1129 p_vout->fmt_out.i_y_offset,
1130 0 /*dest_x*/, 0 /*dest_y*/,
1131 p_vout->fmt_out.i_visible_width,
1132 p_vout->fmt_out.i_visible_height );
1136 /* Make sure the command is sent now - do NOT use XFlush !*/
1137 XSync( p_vout->p_sys->p_display, False );
1139 vlc_mutex_unlock( &p_vout->p_sys->lock );
1142 /*****************************************************************************
1143 * ManageVideo: handle X11 events
1144 *****************************************************************************
1145 * This function should be called regularly by video output thread. It manages
1146 * X11 events and allows window resizing. It returns a non null value on
1148 *****************************************************************************/
1149 static int ManageVideo( vout_thread_t *p_vout )
1151 XEvent xevent; /* X11 event */
1154 vlc_mutex_lock( &p_vout->p_sys->lock );
1156 #ifdef MODULE_NAME_IS_xvmc
1157 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1160 /* Handle events from the owner window */
1161 if( p_vout->p_sys->p_win->owner_window )
1163 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1164 p_vout->p_sys->p_win->owner_window->handle.xid,
1165 StructureNotifyMask, &xevent ) == True )
1167 /* ConfigureNotify event: prepare */
1168 if( xevent.type == ConfigureNotify )
1170 /* Update dimensions */
1171 XResizeWindow( p_vout->p_sys->p_display,
1172 p_vout->p_sys->p_win->base_window,
1173 xevent.xconfigure.width,
1174 xevent.xconfigure.height );
1179 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1180 * output window's size changed, MapNotify and UnmapNotify to know if the
1181 * window is mapped (and if the display is useful), and ClientMessages
1182 * to intercept window destruction requests */
1184 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1185 p_vout->p_sys->p_win->base_window,
1186 StructureNotifyMask | KeyPressMask |
1187 ButtonPressMask | ButtonReleaseMask |
1188 PointerMotionMask | Button1MotionMask , &xevent )
1191 /* ConfigureNotify event: prepare */
1192 if( xevent.type == ConfigureNotify )
1194 if( (unsigned int)xevent.xconfigure.width
1195 != p_vout->p_sys->p_win->i_width
1196 || (unsigned int)xevent.xconfigure.height
1197 != p_vout->p_sys->p_win->i_height )
1199 /* Update dimensions */
1200 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1201 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1202 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1205 /* Keyboard event */
1206 else if( xevent.type == KeyPress )
1208 unsigned int state = xevent.xkey.state;
1209 KeySym x_key_symbol;
1210 char i_key; /* ISO Latin-1 key */
1212 /* We may have keys like F1 trough F12, ESC ... */
1213 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1214 xevent.xkey.keycode, 0 );
1215 val.i_int = ConvertKey( (int)x_key_symbol );
1217 xevent.xkey.state &= ~ShiftMask;
1218 xevent.xkey.state &= ~ControlMask;
1219 xevent.xkey.state &= ~Mod1Mask;
1222 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1225 * The reason why I use this instead of XK_0 is that
1226 * with XLookupString, we don't have to care about
1233 if( state & ShiftMask )
1235 val.i_int |= KEY_MODIFIER_SHIFT;
1237 if( state & ControlMask )
1239 val.i_int |= KEY_MODIFIER_CTRL;
1241 if( state & Mod1Mask )
1243 val.i_int |= KEY_MODIFIER_ALT;
1245 var_Set( p_vout->p_libvlc, "key-pressed", val );
1249 else if( xevent.type == ButtonPress )
1251 switch( ((XButtonEvent *)&xevent)->button )
1254 var_Get( p_vout, "mouse-button-down", &val );
1256 var_Set( p_vout, "mouse-button-down", val );
1258 /* detect double-clicks */
1259 if( ( ((XButtonEvent *)&xevent)->time -
1260 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1262 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1265 p_vout->p_sys->i_time_button_last_pressed =
1266 ((XButtonEvent *)&xevent)->time;
1269 var_Get( p_vout, "mouse-button-down", &val );
1271 var_Set( p_vout, "mouse-button-down", val );
1275 var_Get( p_vout, "mouse-button-down", &val );
1277 var_Set( p_vout, "mouse-button-down", val );
1281 var_Get( p_vout, "mouse-button-down", &val );
1283 var_Set( p_vout, "mouse-button-down", val );
1287 var_Get( p_vout, "mouse-button-down", &val );
1289 var_Set( p_vout, "mouse-button-down", val );
1294 else if( xevent.type == ButtonRelease )
1296 switch( ((XButtonEvent *)&xevent)->button )
1300 var_Get( p_vout, "mouse-button-down", &val );
1302 var_Set( p_vout, "mouse-button-down", val );
1304 var_SetBool( p_vout, "mouse-clicked", true );
1305 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", false );
1311 var_Get( p_vout, "mouse-button-down", &val );
1313 var_Set( p_vout, "mouse-button-down", val );
1315 var_Get( p_vout->p_libvlc, "intf-show", &val );
1316 val.b_bool = !val.b_bool;
1317 var_Set( p_vout->p_libvlc, "intf-show", val );
1323 var_Get( p_vout, "mouse-button-down", &val );
1325 var_Set( p_vout, "mouse-button-down", val );
1327 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
1332 var_Get( p_vout, "mouse-button-down", &val );
1334 var_Set( p_vout, "mouse-button-down", val );
1338 var_Get( p_vout, "mouse-button-down", &val );
1340 var_Set( p_vout, "mouse-button-down", val );
1346 else if( xevent.type == MotionNotify )
1348 unsigned int i_width, i_height, i_x, i_y;
1350 /* somewhat different use for vout_PlacePicture:
1351 * here the values are needed to give to mouse coordinates
1352 * in the original picture space */
1353 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1354 p_vout->p_sys->p_win->i_height,
1355 &i_x, &i_y, &i_width, &i_height );
1357 /* Compute the x coordinate and check if the value is
1358 in [0,p_vout->fmt_in.i_visible_width] */
1359 val.i_int = ( xevent.xmotion.x - i_x ) *
1360 p_vout->fmt_in.i_visible_width / i_width +
1361 p_vout->fmt_in.i_x_offset;
1363 if( (int)(xevent.xmotion.x - i_x) < 0 )
1365 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1366 val.i_int = p_vout->fmt_in.i_visible_width;
1368 var_Set( p_vout, "mouse-x", val );
1370 /* compute the y coordinate and check if the value is
1371 in [0,p_vout->fmt_in.i_visible_height] */
1372 val.i_int = ( xevent.xmotion.y - i_y ) *
1373 p_vout->fmt_in.i_visible_height / i_height +
1374 p_vout->fmt_in.i_y_offset;
1376 if( (int)(xevent.xmotion.y - i_y) < 0 )
1378 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1379 val.i_int = p_vout->fmt_in.i_visible_height;
1381 var_Set( p_vout, "mouse-y", val );
1383 var_SetBool( p_vout, "mouse-moved", true );
1385 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1386 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1388 ToggleCursor( p_vout );
1391 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1392 || xevent.type == MapNotify
1393 || xevent.type == UnmapNotify )
1395 /* Ignore these events */
1397 else /* Other events */
1399 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1403 /* Handle events for video output sub-window */
1404 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1405 p_vout->p_sys->p_win->video_window,
1406 ExposureMask, &xevent ) == True )
1408 /* Window exposed (only handled if stream playback is paused) */
1409 if( xevent.type == Expose )
1411 if( ((XExposeEvent *)&xevent)->count == 0 )
1413 /* (if this is the last a collection of expose events...) */
1415 #if defined(MODULE_NAME_IS_xvideo)
1416 x11_window_t *p_win = p_vout->p_sys->p_win;
1418 /* Paint the colour key if needed */
1419 if( p_vout->p_sys->b_paint_colourkey &&
1420 xevent.xexpose.window == p_win->video_window )
1422 XSetForeground( p_vout->p_sys->p_display,
1423 p_win->gc, p_vout->p_sys->i_colourkey );
1424 XFillRectangle( p_vout->p_sys->p_display,
1425 p_win->video_window, p_win->gc, 0, 0,
1426 p_win->i_width, p_win->i_height );
1431 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1433 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1434 ->stream.control.i_status )
1436 /* XVideoDisplay( p_vout )*/;
1444 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1445 * are handled - according to the man pages, the format is always 32
1447 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1448 ClientMessage, &xevent ) )
1450 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1451 && ((Atom)xevent.xclient.data.l[0]
1452 == p_vout->p_sys->p_win->wm_delete_window ) )
1454 /* the user wants to close the window */
1455 playlist_t * p_playlist = pl_Hold( p_vout );
1456 if( p_playlist != NULL )
1458 playlist_Stop( p_playlist );
1459 pl_Release( p_vout );
1467 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1469 /* Update the object variable and trigger callback */
1470 var_SetBool( p_vout, "fullscreen", !p_vout->b_fullscreen );
1472 ToggleFullScreen( p_vout );
1473 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1476 /* autoscale toggle */
1477 if( p_vout->i_changes & VOUT_SCALE_CHANGE )
1479 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
1481 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
1482 p_vout->i_zoom = ZOOM_FP_FACTOR;
1484 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1487 /* scaling factor */
1488 if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
1490 p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
1492 p_vout->b_autoscale = false;
1494 (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
1496 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1499 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1500 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1502 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1503 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1505 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1506 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1507 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1508 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1509 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1510 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1511 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1512 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1514 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1520 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1521 * the size flag inside the fullscreen routine)
1523 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1525 unsigned int i_width, i_height, i_x, i_y;
1527 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1529 #ifdef MODULE_NAME_IS_x11
1530 /* We need to signal the vout thread about the size change because it
1531 * is doing the rescaling */
1532 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1535 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1536 p_vout->p_sys->p_win->i_height,
1537 &i_x, &i_y, &i_width, &i_height );
1539 XMoveResizeWindow( p_vout->p_sys->p_display,
1540 p_vout->p_sys->p_win->video_window,
1541 i_x, i_y, i_width, i_height );
1544 /* cursor hiding depending on --vout-event option
1546 * value = 1 (Fullsupport) (default value)
1547 * or value = 2 (Fullscreen-Only) and condition met
1549 bool b_vout_event = ( ( p_vout->p_sys->i_vout_event == 1 )
1550 || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1553 /* Autohide Cursour */
1554 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved >
1555 p_vout->p_sys->i_mouse_hide_timeout )
1557 /* Hide the mouse automatically */
1558 if( b_vout_event && p_vout->p_sys->b_mouse_pointer_visible )
1560 ToggleCursor( p_vout );
1564 #ifdef MODULE_NAME_IS_xvmc
1565 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1569 if ( p_vout->p_sys->p_octx != NULL ) {
1570 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1571 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1572 msg_Err( p_vout, "Could not disable backlight blanking" );
1574 msg_Dbg( p_vout, "Backlight blanking disabled" );
1576 p_vout->p_sys->i_backlight_on_counter = 0;
1578 p_vout->p_sys->i_backlight_on_counter ++;
1583 vlc_mutex_unlock( &p_vout->p_sys->lock );
1587 /*****************************************************************************
1588 * EndVideo: terminate X11 video thread output method
1589 *****************************************************************************
1590 * Destroy the X11 XImages created by Init. It is called at the end of
1591 * the thread, but also each time the window is resized.
1592 *****************************************************************************/
1593 static void EndVideo( vout_thread_t *p_vout )
1597 /* Free the direct buffers we allocated */
1598 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1601 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1605 /* following functions are local */
1607 /*****************************************************************************
1608 * CreateWindow: open and set-up X11 main window
1609 *****************************************************************************/
1610 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1612 XSizeHints xsize_hints;
1613 XSetWindowAttributes xwindow_attributes;
1614 XGCValues xgcvalues;
1617 bool b_expose = false;
1618 bool b_configure_notify = false;
1619 bool b_map_notify = false;
1622 /* Prepare window manager hints and properties */
1623 p_win->wm_protocols =
1624 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1625 p_win->wm_delete_window =
1626 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1628 /* Never have a 0-pixel-wide window */
1629 xsize_hints.min_width = 2;
1630 xsize_hints.min_height = 1;
1632 /* Prepare window attributes */
1633 xwindow_attributes.backing_store = Always; /* save the hidden part */
1634 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1635 p_vout->p_sys->i_screen);
1636 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1638 if( !p_vout->b_fullscreen )
1640 p_win->owner_window = vout_RequestXWindow( p_vout, &p_win->i_x,
1641 &p_win->i_y, &p_win->i_width, &p_win->i_height );
1642 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1643 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1644 xsize_hints.flags = PSize | PMinSize;
1646 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1648 xsize_hints.x = p_win->i_x;
1649 xsize_hints.y = p_win->i_y;
1650 xsize_hints.flags |= PPosition;
1655 /* Fullscreen window size and position */
1656 p_win->owner_window = NULL;
1657 p_win->i_x = p_win->i_y = 0;
1659 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1661 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1664 if( !p_win->owner_window )
1666 /* Create the window and set hints - the window must receive
1667 * ConfigureNotify events, and until it is displayed, Expose and
1668 * MapNotify events. */
1670 p_win->base_window =
1671 XCreateWindow( p_vout->p_sys->p_display,
1672 DefaultRootWindow( p_vout->p_sys->p_display ),
1673 p_win->i_x, p_win->i_y,
1674 p_win->i_width, p_win->i_height,
1677 CWBackingStore | CWBackPixel | CWEventMask,
1678 &xwindow_attributes );
1680 var_Get( p_vout, "video-title", &val );
1681 if( !val.psz_string || !*val.psz_string )
1683 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1684 #ifdef MODULE_NAME_IS_x11
1685 VOUT_TITLE " (X11 output)"
1686 #elif defined(MODULE_NAME_IS_glx)
1687 VOUT_TITLE " (GLX output)"
1689 VOUT_TITLE " (XVideo output)"
1695 XStoreName( p_vout->p_sys->p_display,
1696 p_win->base_window, val.psz_string );
1698 free( val.psz_string );
1700 if( !p_vout->b_fullscreen )
1702 const char *argv[] = { "vlc", NULL };
1704 /* Set window manager hints and properties: size hints, command,
1705 * window's name, and accepted protocols */
1706 XSetWMNormalHints( p_vout->p_sys->p_display,
1707 p_win->base_window, &xsize_hints );
1708 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1711 if( !var_GetBool( p_vout, "video-deco") )
1714 mwmhints_t mwmhints;
1716 mwmhints.flags = MWM_HINTS_DECORATIONS;
1717 mwmhints.decorations = False;
1719 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1722 XChangeProperty( p_vout->p_sys->p_display,
1724 prop, prop, 32, PropModeReplace,
1725 (unsigned char *)&mwmhints,
1726 PROP_MWM_HINTS_ELEMENTS );
1734 unsigned int dummy4, dummy5;
1736 /* Select events we are interested in. */
1737 XSelectInput( p_vout->p_sys->p_display,
1738 p_win->owner_window->handle.xid, StructureNotifyMask );
1740 /* Get the parent window's geometry information */
1741 XGetGeometry( p_vout->p_sys->p_display,
1742 p_win->owner_window->handle.xid,
1743 &dummy1, &dummy2, &dummy3,
1748 /* We are already configured */
1749 b_configure_notify = true;
1751 /* From man XSelectInput: only one client at a time can select a
1752 * ButtonPress event, so we need to open a new window anyway. */
1753 p_win->base_window =
1754 XCreateWindow( p_vout->p_sys->p_display,
1755 p_win->owner_window->handle.xid,
1757 p_win->i_width, p_win->i_height,
1759 0, CopyFromParent, 0,
1760 CWBackingStore | CWBackPixel | CWEventMask,
1761 &xwindow_attributes );
1764 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1765 || (p_win->wm_delete_window == None)
1766 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1767 &p_win->wm_delete_window, 1 ) )
1769 /* WM_DELETE_WINDOW is not supported by window manager */
1770 msg_Warn( p_vout, "missing or bad window manager" );
1773 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1774 * event when using functions like XCopyArea */
1775 xgcvalues.graphics_exposures = False;
1776 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1778 GCGraphicsExposures, &xgcvalues );
1780 /* Send orders to server, and wait until window is displayed - three
1781 * events must be received: a MapNotify event, an Expose event allowing
1782 * drawing in the window, and a ConfigureNotify to get the window
1783 * dimensions. Once those events have been received, only
1784 * ConfigureNotify events need to be received. */
1785 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1788 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1789 SubstructureNotifyMask | StructureNotifyMask |
1790 ExposureMask, &xevent);
1791 if( (xevent.type == Expose)
1792 && (xevent.xexpose.window == p_win->base_window) )
1795 /* ConfigureNotify isn't sent if there isn't a window manager.
1796 * Expose should be the last event to be received so it should
1797 * be fine to assume we won't receive it anymore. */
1798 b_configure_notify = true;
1800 else if( (xevent.type == MapNotify)
1801 && (xevent.xmap.window == p_win->base_window) )
1803 b_map_notify = true;
1805 else if( (xevent.type == ConfigureNotify)
1806 && (xevent.xconfigure.window == p_win->base_window) )
1808 b_configure_notify = true;
1809 p_win->i_width = xevent.xconfigure.width;
1810 p_win->i_height = xevent.xconfigure.height;
1812 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1814 /* key and mouse events handling depending on --vout-event option
1816 * value = 1 (Fullsupport) (default value)
1817 * or value = 2 (Fullscreen-Only) and condition met
1819 bool b_vout_event = ( ( p_vout->p_sys->i_vout_event == 1 )
1820 || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1823 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1824 StructureNotifyMask | KeyPressMask |
1825 ButtonPressMask | ButtonReleaseMask |
1826 PointerMotionMask );
1828 #ifdef MODULE_NAME_IS_x11
1829 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1831 /* Allocate a new palette */
1832 p_vout->p_sys->colormap =
1833 XCreateColormap( p_vout->p_sys->p_display,
1834 DefaultRootWindow( p_vout->p_sys->p_display ),
1835 DefaultVisual( p_vout->p_sys->p_display,
1836 p_vout->p_sys->i_screen ),
1839 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1840 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1841 CWColormap, &xwindow_attributes );
1845 /* Create video output sub-window. */
1846 p_win->video_window = XCreateSimpleWindow(
1847 p_vout->p_sys->p_display,
1848 p_win->base_window, 0, 0,
1849 p_win->i_width, p_win->i_height,
1851 BlackPixel( p_vout->p_sys->p_display,
1852 p_vout->p_sys->i_screen ),
1853 WhitePixel( p_vout->p_sys->p_display,
1854 p_vout->p_sys->i_screen ) );
1856 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1857 BlackPixel( p_vout->p_sys->p_display,
1858 p_vout->p_sys->i_screen ) );
1860 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1861 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1864 /* make sure the video window will be centered in the next ManageVideo() */
1865 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1867 /* If the cursor was formerly blank than blank it again */
1868 if( !p_vout->p_sys->b_mouse_pointer_visible )
1870 ToggleCursor( p_vout );
1871 ToggleCursor( p_vout );
1874 /* Do NOT use XFlush here ! */
1875 XSync( p_vout->p_sys->p_display, False );
1877 /* At this stage, the window is open, displayed, and ready to
1879 p_vout->p_sys->p_win = p_win;
1884 /*****************************************************************************
1885 * DestroyWindow: destroy the window
1886 *****************************************************************************
1888 *****************************************************************************/
1889 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1891 /* Do NOT use XFlush here ! */
1892 XSync( p_vout->p_sys->p_display, False );
1894 if( p_win->video_window != None )
1895 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1897 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1899 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1900 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1902 vout_ReleaseWindow( p_win->owner_window );
1905 /*****************************************************************************
1906 * NewPicture: allocate a picture
1907 *****************************************************************************
1908 * Returns 0 on success, -1 otherwise
1909 *****************************************************************************/
1910 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1912 #ifndef MODULE_NAME_IS_glx
1914 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1918 /* We know the chroma, allocate a buffer which will be used
1919 * directly by the decoder */
1920 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1922 if( p_pic->p_sys == NULL )
1927 #ifdef MODULE_NAME_IS_xvmc
1928 p_pic->p_sys->p_vout = p_vout;
1929 p_pic->p_sys->xvmc_surf = NULL;
1930 p_pic->p_sys->xxmc_data.decoded = 0;
1931 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1932 // p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1933 p_pic->p_sys->nb_display = 0;
1936 /* Fill in picture_t fields */
1937 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1938 p_vout->output.i_width, p_vout->output.i_height,
1939 p_vout->output.i_aspect );
1941 #ifdef HAVE_SYS_SHM_H
1942 if( p_vout->p_sys->i_shm_opcode )
1944 /* Create image using XShm extension */
1945 p_pic->p_sys->p_image =
1946 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1947 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1948 p_vout->p_sys->i_xvport,
1949 VLC2X11_FOURCC(p_vout->output.i_chroma),
1951 p_vout->p_sys->p_visual,
1952 p_vout->p_sys->i_screen_depth,
1954 &p_pic->p_sys->shminfo,
1955 p_vout->output.i_width, p_vout->output.i_height );
1958 if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1959 #endif /* HAVE_SYS_SHM_H */
1961 /* Create image without XShm extension */
1962 p_pic->p_sys->p_image =
1963 CreateImage( p_vout, p_vout->p_sys->p_display,
1964 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1965 p_vout->p_sys->i_xvport,
1966 VLC2X11_FOURCC(p_vout->output.i_chroma),
1967 p_pic->format.i_bits_per_pixel,
1969 p_vout->p_sys->p_visual,
1970 p_vout->p_sys->i_screen_depth,
1971 p_vout->p_sys->i_bytes_per_pixel,
1973 p_vout->output.i_width, p_vout->output.i_height );
1975 #ifdef HAVE_SYS_SHM_H
1976 if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1978 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1979 p_vout->p_sys->i_shm_opcode = 0;
1981 #endif /* HAVE_SYS_SHM_H */
1984 if( p_pic->p_sys->p_image == NULL )
1986 free( p_pic->p_sys );
1990 switch( p_vout->output.i_chroma )
1992 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1993 case VLC_FOURCC('I','4','2','0'):
1994 case VLC_FOURCC('Y','V','1','2'):
1995 case VLC_FOURCC('Y','2','1','1'):
1996 case VLC_FOURCC('Y','U','Y','2'):
1997 case VLC_FOURCC('U','Y','V','Y'):
1998 case VLC_FOURCC('R','V','1','5'):
1999 case VLC_FOURCC('R','V','1','6'):
2000 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
2001 case VLC_FOURCC('R','V','3','2'):
2003 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
2006 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
2007 + p_pic->p_sys->p_image->offsets[i_plane];
2008 p_pic->p[i_plane].i_pitch =
2009 p_pic->p_sys->p_image->pitches[i_plane];
2011 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
2013 /* U and V inverted compared to I420
2014 * Fixme: this should be handled by the vout core */
2015 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
2016 + p_pic->p_sys->p_image->offsets[2];
2017 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
2018 + p_pic->p_sys->p_image->offsets[1];
2024 case VLC_FOURCC('R','G','B','2'):
2025 case VLC_FOURCC('R','V','1','6'):
2026 case VLC_FOURCC('R','V','1','5'):
2027 case VLC_FOURCC('R','V','2','4'):
2028 case VLC_FOURCC('R','V','3','2'):
2030 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
2031 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
2032 p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
2033 + p_pic->p_sys->p_image->xoffset;
2034 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
2036 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
2037 * properly by vout_InitPicture() */
2038 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
2039 * p_pic->p_sys->p_image->width;
2044 /* Unknown chroma, tell the guy to get lost */
2045 IMAGE_FREE( p_pic->p_sys->p_image );
2046 free( p_pic->p_sys );
2047 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
2048 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
2049 p_pic->i_planes = 0;
2054 VLC_UNUSED(p_vout); VLC_UNUSED(p_pic);
2056 #endif /* !MODULE_NAME_IS_glx */
2061 /*****************************************************************************
2062 * FreePicture: destroy a picture allocated with NewPicture
2063 *****************************************************************************
2064 * Destroy XImage AND associated data. If using Shm, detach shared memory
2065 * segment from server and process, then free it. The XDestroyImage manpage
2066 * says that both the image structure _and_ the data pointed to by the
2067 * image structure are freed, so no need to free p_image->data.
2068 *****************************************************************************/
2069 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2071 /* The order of operations is correct */
2072 #ifdef HAVE_SYS_SHM_H
2073 if( p_vout->p_sys->i_shm_opcode )
2075 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2076 IMAGE_FREE( p_pic->p_sys->p_image );
2078 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2079 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2081 msg_Err( p_vout, "cannot detach shared memory (%m)" );
2087 IMAGE_FREE( p_pic->p_sys->p_image );
2090 #ifdef MODULE_NAME_IS_xvmc
2091 if( p_pic->p_sys->xvmc_surf != NULL )
2093 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2094 p_pic->p_sys->xvmc_surf = NULL;
2098 /* Do NOT use XFlush here ! */
2099 XSync( p_vout->p_sys->p_display, False );
2101 free( p_pic->p_sys );
2104 /*****************************************************************************
2105 * ToggleFullScreen: Enable or disable full screen mode
2106 *****************************************************************************
2107 * This function will switch between fullscreen and window mode.
2108 *****************************************************************************/
2109 static void ToggleFullScreen ( vout_thread_t *p_vout )
2113 mwmhints_t mwmhints;
2114 XSetWindowAttributes attributes;
2116 #ifdef HAVE_XINERAMA
2120 p_vout->b_fullscreen = !p_vout->b_fullscreen;
2122 if( p_vout->b_fullscreen )
2124 msg_Dbg( p_vout, "entering fullscreen mode" );
2126 /* Getting current window position */
2128 Window* child_windows;
2129 unsigned int num_child_windows;
2132 XWindowAttributes win_attr;
2133 int screen_x,screen_y;
2135 XGetWindowAttributes(
2136 p_vout->p_sys->p_display,
2137 p_vout->p_sys->p_win->video_window,
2141 p_vout->p_sys->p_display,
2142 p_vout->p_sys->p_win->video_window,
2146 &num_child_windows);
2147 XFree(child_windows);
2149 XTranslateCoordinates(
2150 p_vout->p_sys->p_display,
2151 parent_win, win_attr.root,
2152 win_attr.x,win_attr.y,
2153 &screen_x,&screen_y,
2156 msg_Dbg( p_vout, "X %d/%d Y %d/%d", win_attr.x,screen_x,win_attr.y,screen_y);
2157 /* screen_x and screen_y are current position */
2159 p_vout->p_sys->b_altfullscreen =
2160 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2162 XUnmapWindow( p_vout->p_sys->p_display,
2163 p_vout->p_sys->p_win->base_window );
2165 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2167 CreateWindow( p_vout, p_vout->p_sys->p_win );
2168 XDestroyWindow( p_vout->p_sys->p_display,
2169 p_vout->p_sys->fullscreen_window.video_window );
2170 XReparentWindow( p_vout->p_sys->p_display,
2171 p_vout->p_sys->original_window.video_window,
2172 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2173 p_vout->p_sys->fullscreen_window.video_window =
2174 p_vout->p_sys->original_window.video_window;
2176 /* To my knowledge there are two ways to create a borderless window.
2177 * There's the generic way which is to tell x to bypass the window
2178 * manager, but this creates problems with the focus of other
2180 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2181 * luckily seems to be supported by most window managers. */
2182 if( !p_vout->p_sys->b_altfullscreen )
2184 mwmhints.flags = MWM_HINTS_DECORATIONS;
2185 mwmhints.decorations = False;
2187 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2189 XChangeProperty( p_vout->p_sys->p_display,
2190 p_vout->p_sys->p_win->base_window,
2191 prop, prop, 32, PropModeReplace,
2192 (unsigned char *)&mwmhints,
2193 PROP_MWM_HINTS_ELEMENTS );
2197 /* brute force way to remove decorations */
2198 attributes.override_redirect = True;
2199 XChangeWindowAttributes( p_vout->p_sys->p_display,
2200 p_vout->p_sys->p_win->base_window,
2204 /* Make sure the change is effective */
2205 XReparentWindow( p_vout->p_sys->p_display,
2206 p_vout->p_sys->p_win->base_window,
2207 DefaultRootWindow( p_vout->p_sys->p_display ),
2211 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2213 XClientMessageEvent event;
2215 memset( &event, 0, sizeof( XClientMessageEvent ) );
2217 event.type = ClientMessage;
2218 event.message_type = p_vout->p_sys->net_wm_state;
2219 event.display = p_vout->p_sys->p_display;
2220 event.window = p_vout->p_sys->p_win->base_window;
2222 event.data.l[ 0 ] = 1; /* set property */
2223 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2225 XSendEvent( p_vout->p_sys->p_display,
2226 DefaultRootWindow( p_vout->p_sys->p_display ),
2227 False, SubstructureRedirectMask,
2231 /* "bad fullscreen" - set this to 0. doing fullscreen this way is problematic
2232 * for many reasons and basically fights with the window manager as the wm
2233 * reparents AND vlc goes and reparents - multiple times. don't do it. it just
2234 * makes it more inefficient and less "nice" to the x11 citizenry. this turns
2237 /* explicitly asking for focus when you fullscreened is a little silly. the
2238 * window manager SHOULD be handling this itself based on its own focus
2239 * policies. if the user is "using" a given xinerama/xrandr screen or x11
2240 * multihead screen AND vlc wants to fullscreen the wm should also focus it
2241 * as its the only thing on the screen. if vlc fullscreens and its on
2242 * "another monitor" to the one the user is using - this may "steal" the focus
2243 * as really the wm should be deciding if, on fullscreening of a window
2244 * the focus should go there or not, so let the wm decided */
2246 /* Make sure the change is effective */
2247 XReparentWindow( p_vout->p_sys->p_display,
2248 p_vout->p_sys->p_win->base_window,
2249 DefaultRootWindow( p_vout->p_sys->p_display ),
2252 #ifdef HAVE_XINERAMA
2253 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2254 XineramaIsActive( p_vout->p_sys->p_display ) )
2256 XineramaScreenInfo *screens; /* infos for xinerama */
2259 msg_Dbg( p_vout, "using XFree Xinerama extension");
2261 #define SCREEN p_vout->p_sys->p_win->i_screen
2263 /* Get Information about Xinerama (num of screens) */
2264 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2267 SCREEN = config_GetInt( p_vout,
2268 MODULE_STRING "-xineramascreen" );
2270 /* just check that user has entered a good value,
2271 * otherwise use that screen where window is */
2272 if( SCREEN >= i_num_screens || SCREEN < 0 )
2274 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2275 #define left screens[SCREEN].x_org
2276 #define right left + screens[SCREEN].width
2277 #define top screens[SCREEN].y_org
2278 #define bottom top + screens[SCREEN].height
2280 for( SCREEN = i_num_screens-1; SCREEN > 0; SCREEN--)
2282 if( left <= screen_x && screen_x <= right &&
2283 top <= screen_y && screen_y <= bottom )
2292 /* Get the X/Y upper left corner coordinate of the above screen */
2293 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2294 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2296 /* Set the Height/width to the screen resolution */
2297 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2298 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2308 /* The window wasn't necessarily created at the requested size */
2309 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2311 #ifdef HAVE_XF86VIDMODE
2312 XF86VidModeModeLine mode;
2315 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2316 p_vout->p_sys->i_screen, &i_dummy,
2319 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2320 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2322 /* move cursor to the middle of the window to prevent
2323 * unwanted display move if the display is smaller than the
2325 XWarpPointer( p_vout->p_sys->p_display, None,
2326 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2327 mode.hdisplay / 2 , mode.vdisplay / 2 );
2328 /* force desktop view to upper left corner */
2329 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2330 p_vout->p_sys->i_screen, 0, 0 );
2335 p_vout->p_sys->p_win->i_width =
2336 DisplayWidth( p_vout->p_sys->p_display,
2337 p_vout->p_sys->i_screen );
2338 p_vout->p_sys->p_win->i_height =
2339 DisplayHeight( p_vout->p_sys->p_display,
2340 p_vout->p_sys->i_screen );
2345 XMoveResizeWindow( p_vout->p_sys->p_display,
2346 p_vout->p_sys->p_win->base_window,
2347 p_vout->p_sys->p_win->i_x,
2348 p_vout->p_sys->p_win->i_y,
2349 p_vout->p_sys->p_win->i_width,
2350 p_vout->p_sys->p_win->i_height );
2353 EnablePixelDoubling( p_vout );
2356 #if APPFOCUS // RASTER: let the wm do focus policy
2357 /* Activate the window (give it the focus) */
2358 XClientMessageEvent event;
2360 memset( &event, 0, sizeof( XClientMessageEvent ) );
2362 event.type = ClientMessage;
2363 event.message_type =
2364 XInternAtom( p_vout->p_sys->p_display, "_NET_ACTIVE_WINDOW", False );
2365 event.display = p_vout->p_sys->p_display;
2366 event.window = p_vout->p_sys->p_win->base_window;
2368 event.data.l[ 0 ] = 1; /* source indication (1 = from an application */
2369 event.data.l[ 1 ] = 0; /* timestamp */
2370 event.data.l[ 2 ] = 0; /* requestor's currently active window */
2371 /* XXX: window manager would be more likely to obey if we already have
2372 * an active window (and give it to the event), such as an interface */
2374 XSendEvent( p_vout->p_sys->p_display,
2375 DefaultRootWindow( p_vout->p_sys->p_display ),
2376 False, SubstructureRedirectMask,
2382 msg_Dbg( p_vout, "leaving fullscreen mode" );
2385 DisablePixelDoubling( p_vout );
2388 XReparentWindow( p_vout->p_sys->p_display,
2389 p_vout->p_sys->original_window.video_window,
2390 p_vout->p_sys->original_window.base_window, 0, 0 );
2392 p_vout->p_sys->fullscreen_window.video_window = None;
2393 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2394 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2396 XMapWindow( p_vout->p_sys->p_display,
2397 p_vout->p_sys->p_win->base_window );
2400 /* Unfortunately, using XSync() here is not enough to ensure the
2401 * window has already been mapped because the XMapWindow() request
2402 * has not necessarily been sent directly to our window (remember,
2403 * the call is first redirected to the window manager) */
2405 #if BADFS // RASTER: this is silly... if we have already mapped before
2408 XWindowEvent( p_vout->p_sys->p_display,
2409 p_vout->p_sys->p_win->base_window,
2410 StructureNotifyMask, &xevent );
2411 } while( xevent.type != MapNotify );
2413 XSync(p_vout->p_sys->p_display, False);
2416 /* Be careful, this can generate a BadMatch error if the window is not
2417 * already mapped by the server (see above) */
2418 XSetInputFocus(p_vout->p_sys->p_display,
2419 p_vout->p_sys->p_win->base_window,
2423 /* signal that the size needs to be updated */
2424 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2427 /*****************************************************************************
2428 * EnableXScreenSaver: enable screen saver
2429 *****************************************************************************
2430 * This function enables the screen saver on a display after it has been
2431 * disabled by XDisableScreenSaver.
2432 * FIXME: what happens if multiple vlc sessions are running at the same
2434 *****************************************************************************/
2435 static void EnableXScreenSaver( vout_thread_t *p_vout )
2437 #ifdef DPMSINFO_IN_DPMS_H
2441 if( p_vout->p_sys->i_ss_timeout )
2443 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2444 p_vout->p_sys->i_ss_interval,
2445 p_vout->p_sys->i_ss_blanking,
2446 p_vout->p_sys->i_ss_exposure );
2449 /* Restore DPMS settings */
2450 #ifdef DPMSINFO_IN_DPMS_H
2451 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2453 if( p_vout->p_sys->b_ss_dpms )
2455 DPMSEnable( p_vout->p_sys->p_display );
2461 /*****************************************************************************
2462 * DisableXScreenSaver: disable screen saver
2463 *****************************************************************************
2464 * See XEnableXScreenSaver
2465 *****************************************************************************/
2466 static void DisableXScreenSaver( vout_thread_t *p_vout )
2468 #ifdef DPMSINFO_IN_DPMS_H
2472 /* Save screen saver information */
2473 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2474 &p_vout->p_sys->i_ss_interval,
2475 &p_vout->p_sys->i_ss_blanking,
2476 &p_vout->p_sys->i_ss_exposure );
2478 /* Disable screen saver */
2479 if( p_vout->p_sys->i_ss_timeout )
2481 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2482 p_vout->p_sys->i_ss_interval,
2483 p_vout->p_sys->i_ss_blanking,
2484 p_vout->p_sys->i_ss_exposure );
2488 #ifdef DPMSINFO_IN_DPMS_H
2489 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2492 /* Save DPMS current state */
2493 DPMSInfo( p_vout->p_sys->p_display, &unused,
2494 &p_vout->p_sys->b_ss_dpms );
2495 DPMSDisable( p_vout->p_sys->p_display );
2500 /*****************************************************************************
2501 * CreateCursor: create a blank mouse pointer
2502 *****************************************************************************/
2503 static void CreateCursor( vout_thread_t *p_vout )
2505 XColor cursor_color;
2507 p_vout->p_sys->cursor_pixmap =
2508 XCreatePixmap( p_vout->p_sys->p_display,
2509 DefaultRootWindow( p_vout->p_sys->p_display ),
2512 XParseColor( p_vout->p_sys->p_display,
2513 XCreateColormap( p_vout->p_sys->p_display,
2515 p_vout->p_sys->p_display ),
2517 p_vout->p_sys->p_display,
2518 p_vout->p_sys->i_screen ),
2520 "black", &cursor_color );
2522 p_vout->p_sys->blank_cursor =
2523 XCreatePixmapCursor( p_vout->p_sys->p_display,
2524 p_vout->p_sys->cursor_pixmap,
2525 p_vout->p_sys->cursor_pixmap,
2526 &cursor_color, &cursor_color, 1, 1 );
2529 /*****************************************************************************
2530 * DestroyCursor: destroy the blank mouse pointer
2531 *****************************************************************************/
2532 static void DestroyCursor( vout_thread_t *p_vout )
2534 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2537 /*****************************************************************************
2538 * ToggleCursor: hide or show the mouse pointer
2539 *****************************************************************************
2540 * This function hides the X pointer if it is visible by setting the pointer
2541 * sprite to a blank one. To show it again, we disable the sprite.
2542 *****************************************************************************/
2543 static void ToggleCursor( vout_thread_t *p_vout )
2545 if( p_vout->p_sys->b_mouse_pointer_visible )
2547 XDefineCursor( p_vout->p_sys->p_display,
2548 p_vout->p_sys->p_win->base_window,
2549 p_vout->p_sys->blank_cursor );
2550 p_vout->p_sys->b_mouse_pointer_visible = 0;
2554 XUndefineCursor( p_vout->p_sys->p_display,
2555 p_vout->p_sys->p_win->base_window );
2556 p_vout->p_sys->b_mouse_pointer_visible = 1;
2560 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2561 /*****************************************************************************
2562 * XVideoGetPort: get YUV12 port
2563 *****************************************************************************/
2564 static int XVideoGetPort( vout_thread_t *p_vout,
2565 vlc_fourcc_t i_chroma, picture_heap_t *p_heap )
2567 XvAdaptorInfo *p_adaptor;
2569 unsigned int i_adaptor, i_num_adaptors;
2570 int i_requested_adaptor;
2571 int i_selected_port;
2573 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2578 case XvBadExtension:
2579 msg_Warn( p_vout, "XvBadExtension" );
2583 msg_Warn( p_vout, "XvBadAlloc" );
2587 msg_Warn( p_vout, "XvQueryExtension failed" );
2591 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2592 DefaultRootWindow( p_vout->p_sys->p_display ),
2593 &i_num_adaptors, &p_adaptor ) )
2598 case XvBadExtension:
2599 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2603 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2607 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2611 i_selected_port = -1;
2612 #ifdef MODULE_NAME_IS_xvmc
2613 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2615 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2617 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2619 XvImageFormatValues *p_formats;
2620 int i_format, i_num_formats;
2623 /* If we requested an adaptor and it's not this one, we aren't
2625 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2630 /* If the adaptor doesn't have the required properties, skip it */
2631 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2632 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2637 /* Check that adaptor supports our requested format... */
2638 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2639 p_adaptor[i_adaptor].base_id,
2643 i_format < i_num_formats && ( i_selected_port == -1 );
2646 XvAttribute *p_attr;
2647 int i_attr, i_num_attributes;
2648 Atom autopaint = None, colorkey = None;
2650 /* If this is not the format we want, or at least a
2651 * similar one, forget it */
2652 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2657 /* Look for the first available port supporting this format */
2658 for( i_port = p_adaptor[i_adaptor].base_id;
2659 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2660 + p_adaptor[i_adaptor].num_ports) )
2661 && ( i_selected_port == -1 );
2664 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2667 i_selected_port = i_port;
2668 p_heap->i_chroma = p_formats[ i_format ].id;
2669 #if XvVersion > 2 || ( XvVersion == 2 && XvRevision >= 2 )
2670 p_heap->i_rmask = p_formats[ i_format ].red_mask;
2671 p_heap->i_gmask = p_formats[ i_format ].green_mask;
2672 p_heap->i_bmask = p_formats[ i_format ].blue_mask;
2677 /* If no free port was found, forget it */
2678 if( i_selected_port == -1 )
2683 /* If we found a port, print information about it */
2684 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2685 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2686 (char *)&p_formats[ i_format ].id,
2687 ( p_formats[ i_format ].format == XvPacked ) ?
2688 "packed" : "planar" );
2690 /* Use XV_AUTOPAINT_COLORKEY if supported, otherwise we will
2691 * manually paint the colour key */
2692 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2694 &i_num_attributes );
2696 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2698 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2700 autopaint = XInternAtom( p_vout->p_sys->p_display,
2701 "XV_AUTOPAINT_COLORKEY", False );
2702 XvSetPortAttribute( p_vout->p_sys->p_display,
2703 i_selected_port, autopaint, 1 );
2705 if( !strcmp( p_attr[i_attr].name, "XV_COLORKEY" ) )
2707 /* Find out the default colour key */
2708 colorkey = XInternAtom( p_vout->p_sys->p_display,
2709 "XV_COLORKEY", False );
2710 XvGetPortAttribute( p_vout->p_sys->p_display,
2711 i_selected_port, colorkey,
2712 &p_vout->p_sys->i_colourkey );
2715 p_vout->p_sys->b_paint_colourkey =
2716 autopaint == None && colorkey != None;
2718 if( p_attr != NULL )
2724 if( p_formats != NULL )
2731 if( i_num_adaptors > 0 )
2733 XvFreeAdaptorInfo( p_adaptor );
2736 if( i_selected_port == -1 )
2738 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2739 if( i_requested_adaptor == -1 )
2741 msg_Warn( p_vout, "no free XVideo port found for format "
2742 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2746 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2747 "XVideo port for format 0x%.8x (%4.4s)",
2748 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2752 return i_selected_port;
2755 /*****************************************************************************
2756 * XVideoReleasePort: release YUV12 port
2757 *****************************************************************************/
2758 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2760 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2764 /*****************************************************************************
2765 * InitDisplay: open and initialize X11 device
2766 *****************************************************************************
2767 * Create a window according to video output given size, and set other
2768 * properties according to the display properties.
2769 *****************************************************************************/
2770 static int InitDisplay( vout_thread_t *p_vout )
2772 #ifdef MODULE_NAME_IS_x11
2773 XPixmapFormatValues * p_formats; /* pixmap formats */
2774 XVisualInfo * p_xvisual; /* visuals information */
2775 XVisualInfo xvisual_template; /* visual template */
2776 int i_count, i; /* array size */
2779 #ifdef HAVE_SYS_SHM_H
2780 p_vout->p_sys->i_shm_opcode = 0;
2782 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) > 0 )
2784 int major, evt, err;
2786 if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2788 && XShmQueryExtension( p_vout->p_sys->p_display ) )
2789 p_vout->p_sys->i_shm_opcode = major;
2791 if( p_vout->p_sys->i_shm_opcode )
2796 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2798 msg_Dbg( p_vout, "XShm video extension v%d.%d "
2799 "(with%s pixmaps, opcode: %d)",
2800 major, minor, pixmaps ? "" : "out",
2801 p_vout->p_sys->i_shm_opcode );
2804 msg_Warn( p_vout, "XShm video extension not available" );
2807 msg_Dbg( p_vout, "XShm video extension disabled" );
2810 #ifdef MODULE_NAME_IS_xvideo
2811 /* XXX The brightness and contrast values should be read from environment
2812 * XXX variables... */
2814 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2815 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2819 #ifdef MODULE_NAME_IS_x11
2820 /* Initialize structure */
2821 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2823 /* Get screen depth */
2824 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2825 p_vout->p_sys->i_screen );
2826 switch( p_vout->p_sys->i_screen_depth )
2830 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2832 xvisual_template.screen = p_vout->p_sys->i_screen;
2833 xvisual_template.class = DirectColor;
2834 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2835 VisualScreenMask | VisualClassMask,
2836 &xvisual_template, &i_count );
2837 if( p_xvisual == NULL )
2839 msg_Err( p_vout, "no PseudoColor visual available" );
2840 return VLC_EGENERIC;
2842 p_vout->p_sys->i_bytes_per_pixel = 1;
2843 p_vout->output.pf_setpalette = SetPalette;
2850 * Screen depth is higher than 8bpp. TrueColor visual is used.
2852 xvisual_template.screen = p_vout->p_sys->i_screen;
2853 xvisual_template.class = TrueColor;
2854 /* In some cases, we get a truecolor class adaptor that has a different
2855 color depth. So try to get a real true color one first */
2856 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2858 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2859 VisualScreenMask | VisualClassMask |
2861 &xvisual_template, &i_count );
2862 if( p_xvisual == NULL )
2864 msg_Warn( p_vout, "No screen matching the required color depth" );
2865 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2866 VisualScreenMask | VisualClassMask,
2867 &xvisual_template, &i_count );
2868 if( p_xvisual == NULL )
2871 msg_Err( p_vout, "no TrueColor visual available" );
2872 return VLC_EGENERIC;
2876 p_vout->output.i_rmask = p_xvisual->red_mask;
2877 p_vout->output.i_gmask = p_xvisual->green_mask;
2878 p_vout->output.i_bmask = p_xvisual->blue_mask;
2880 /* There is no difference yet between 3 and 4 Bpp. The only way
2881 * to find the actual number of bytes per pixel is to list supported
2882 * pixmap formats. */
2883 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2884 p_vout->p_sys->i_bytes_per_pixel = 0;
2886 for( i = 0; i < i_count; i++ )
2888 /* Under XFree4.0, the list contains pixmap formats available
2889 * through all video depths ; so we have to check against current
2891 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2893 if( p_formats[i].bits_per_pixel / 8
2894 > (int)p_vout->p_sys->i_bytes_per_pixel )
2896 p_vout->p_sys->i_bytes_per_pixel =
2897 p_formats[i].bits_per_pixel / 8;
2901 if( p_formats ) XFree( p_formats );
2905 p_vout->p_sys->p_visual = p_xvisual->visual;
2912 #ifndef MODULE_NAME_IS_glx
2914 #ifdef HAVE_SYS_SHM_H
2915 /*****************************************************************************
2916 * CreateShmImage: create an XImage or XvImage using shared memory extension
2917 *****************************************************************************
2918 * Prepare an XImage or XvImage for display function.
2919 * The order of the operations respects the recommandations of the mit-shm
2920 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2921 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2922 *****************************************************************************/
2923 IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2924 Display* p_display, EXTRA_ARGS_SHM,
2925 int i_width, int i_height )
2927 IMAGE_TYPE *p_image;
2930 /* Create XImage / XvImage */
2931 #ifdef MODULE_NAME_IS_xvideo
2932 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2933 i_width, i_height, p_shm );
2934 #elif defined(MODULE_NAME_IS_xvmc)
2935 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2936 i_width, i_height, p_shm );
2938 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2939 p_shm, i_width, i_height );
2941 if( p_image == NULL )
2943 msg_Err( p_vout, "image creation failed" );
2947 /* Allocate shared memory segment - 0776 set the access permission
2948 * rights (like umask), they are not yet supported by all X servers */
2949 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2950 if( p_shm->shmid < 0 )
2952 msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2953 IMAGE_FREE( p_image );
2957 /* Attach shared memory segment to process (read/write) */
2958 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2959 if(! p_shm->shmaddr )
2961 msg_Err( p_vout, "cannot attach shared memory (%m)" );
2962 IMAGE_FREE( p_image );
2963 shmctl( p_shm->shmid, IPC_RMID, 0 );
2967 /* Read-only data. We won't be using XShmGetImage */
2968 p_shm->readOnly = True;
2970 /* Attach shared memory segment to X server */
2971 XSynchronize( p_display, True );
2972 i_shm_major = p_vout->p_sys->i_shm_opcode;
2973 result = XShmAttach( p_display, p_shm );
2974 if( result == False || !i_shm_major )
2976 msg_Err( p_vout, "cannot attach shared memory to X server" );
2977 IMAGE_FREE( p_image );
2978 shmctl( p_shm->shmid, IPC_RMID, 0 );
2979 shmdt( p_shm->shmaddr );
2982 XSynchronize( p_display, False );
2984 /* Send image to X server. This instruction is required, since having
2985 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2986 * and remember NOT to use XFlush ! */
2987 XSync( p_display, False );
2990 /* Mark the shm segment to be removed when there are no more
2991 * attachements, so it is automatic on process exit or after shmdt */
2992 shmctl( p_shm->shmid, IPC_RMID, 0 );
2999 /*****************************************************************************
3000 * CreateImage: create an XImage or XvImage
3001 *****************************************************************************
3002 * Create a simple image used as a buffer.
3003 *****************************************************************************/
3004 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
3005 Display *p_display, EXTRA_ARGS,
3006 int i_width, int i_height )
3008 uint8_t * p_data; /* image data storage zone */
3009 IMAGE_TYPE *p_image;
3010 #ifdef MODULE_NAME_IS_x11
3011 int i_quantum; /* XImage quantum (see below) */
3012 int i_bytes_per_line;
3015 /* Allocate memory for image */
3016 #ifdef MODULE_NAME_IS_xvideo
3017 p_data = malloc( i_width * i_height * i_bits_per_pixel / 8 );
3018 #elif defined(MODULE_NAME_IS_x11)
3019 i_bytes_per_line = i_width * i_bytes_per_pixel;
3020 p_data = malloc( i_bytes_per_line * i_height );
3025 #ifdef MODULE_NAME_IS_x11
3026 /* Optimize the quantum of a scanline regarding its size - the quantum is
3027 a diviser of the number of bits between the start of two scanlines. */
3028 if( i_bytes_per_line & 0xf )
3032 else if( i_bytes_per_line & 0x10 )
3042 /* Create XImage. p_data will be automatically freed */
3043 #ifdef MODULE_NAME_IS_xvideo
3044 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
3045 (char *)p_data, i_width, i_height );
3046 #elif defined(MODULE_NAME_IS_x11)
3047 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
3048 (char *)p_data, i_width, i_height, i_quantum, 0 );
3050 if( p_image == NULL )
3052 msg_Err( p_vout, "XCreateImage() failed" );
3061 /*****************************************************************************
3062 * X11ErrorHandler: replace error handler so we can intercept some of them
3063 *****************************************************************************/
3064 static int X11ErrorHandler( Display * display, XErrorEvent * event )
3068 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
3070 "[????????] x11 video output error: X11 request %u.%u failed "
3071 "with error code %u:\n %s\n",
3072 event->request_code, event->minor_code, event->error_code, txt );
3074 switch( event->request_code )
3076 case X_SetInputFocus:
3077 /* Ignore errors on XSetInputFocus()
3078 * (they happen when a window is not yet mapped) */
3082 #ifdef HAVE_SYS_SHM_H
3083 if( event->request_code == i_shm_major ) /* MIT-SHM */
3086 "[????????] x11 video output notice:"
3087 " buggy X11 server claims shared memory\n"
3088 "[????????] x11 video output notice:"
3089 " support though it does not work (OpenSSH?)\n" );
3090 return i_shm_major = 0;
3095 XSetErrorHandler(NULL);
3096 return (XSetErrorHandler(X11ErrorHandler))( display, event );
3098 /* Work-around Maemo Xvideo bug */
3103 #ifdef MODULE_NAME_IS_x11
3104 /*****************************************************************************
3105 * SetPalette: sets an 8 bpp palette
3106 *****************************************************************************
3107 * This function sets the palette given as an argument. It does not return
3108 * anything, but could later send information on which colors it was unable
3110 *****************************************************************************/
3111 static void SetPalette( vout_thread_t *p_vout,
3112 uint16_t *red, uint16_t *green, uint16_t *blue )
3115 XColor p_colors[255];
3117 /* allocate palette */
3118 for( i = 0; i < 255; i++ )
3120 /* kludge: colors are indexed reversely because color 255 seems
3121 * to be reserved for black even if we try to set it to white */
3122 p_colors[ i ].pixel = 255 - i;
3123 p_colors[ i ].pad = 0;
3124 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
3125 p_colors[ i ].red = red[ 255 - i ];
3126 p_colors[ i ].blue = blue[ 255 - i ];
3127 p_colors[ i ].green = green[ 255 - i ];
3130 XStoreColors( p_vout->p_sys->p_display,
3131 p_vout->p_sys->colormap, p_colors, 255 );
3135 /*****************************************************************************
3136 * Control: control facility for the vout
3137 *****************************************************************************/
3138 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
3141 unsigned int i_width, i_height;
3142 unsigned int *pi_width, *pi_height;
3148 if( p_vout->p_sys->p_win->owner_window )
3149 return vout_ControlWindow( p_vout->p_sys->p_win->owner_window,
3152 pi_width = va_arg( args, unsigned int * );
3153 pi_height = va_arg( args, unsigned int * );
3155 vlc_mutex_lock( &p_vout->p_sys->lock );
3156 *pi_width = p_vout->p_sys->p_win->i_width;
3157 *pi_height = p_vout->p_sys->p_win->i_height;
3158 vlc_mutex_unlock( &p_vout->p_sys->lock );
3162 if( p_vout->p_sys->p_win->owner_window )
3163 return vout_ControlWindow( p_vout->p_sys->p_win->owner_window,
3166 vlc_mutex_lock( &p_vout->p_sys->lock );
3168 i_width = va_arg( args, unsigned int );
3169 i_height = va_arg( args, unsigned int );
3170 if( !i_width ) i_width = p_vout->i_window_width;
3171 if( !i_height ) i_height = p_vout->i_window_height;
3173 #ifdef MODULE_NAME_IS_xvmc
3174 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3176 /* Update dimensions */
3177 XResizeWindow( p_vout->p_sys->p_display,
3178 p_vout->p_sys->p_win->base_window,
3179 i_width, i_height );
3180 #ifdef MODULE_NAME_IS_xvmc
3181 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3183 vlc_mutex_unlock( &p_vout->p_sys->lock );
3187 vlc_mutex_lock( &p_vout->p_sys->lock );
3188 if( i_query == VOUT_REPARENT ) d = (Drawable)va_arg( args, int );
3191 #ifdef MODULE_NAME_IS_xvmc
3192 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3194 XReparentWindow( p_vout->p_sys->p_display,
3195 p_vout->p_sys->original_window.base_window,
3196 DefaultRootWindow( p_vout->p_sys->p_display ),
3200 XReparentWindow( p_vout->p_sys->p_display,
3201 p_vout->p_sys->original_window.base_window,
3203 XSync( p_vout->p_sys->p_display, False );
3204 #ifdef MODULE_NAME_IS_xvmc
3205 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3207 vlc_mutex_unlock( &p_vout->p_sys->lock );
3208 vout_ReleaseWindow( p_vout->p_sys->p_win->owner_window );
3209 p_vout->p_sys->original_window.owner_window = NULL;
3212 case VOUT_SET_STAY_ON_TOP:
3213 if( p_vout->p_sys->p_win->owner_window )
3214 return vout_ControlWindow( p_vout->p_sys->p_win->owner_window,
3217 b_arg = (bool) va_arg( args, int );
3218 vlc_mutex_lock( &p_vout->p_sys->lock );
3219 #ifdef MODULE_NAME_IS_xvmc
3220 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3222 WindowOnTop( p_vout, b_arg );
3223 #ifdef MODULE_NAME_IS_xvmc
3224 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3226 vlc_mutex_unlock( &p_vout->p_sys->lock );
3230 return vout_vaControlDefault( p_vout, i_query, args );
3234 /*****************************************************************************
3235 * TestNetWMSupport: tests for Extended Window Manager Hints support
3236 *****************************************************************************/
3237 static void TestNetWMSupport( vout_thread_t *p_vout )
3239 int i_ret, i_format;
3240 unsigned long i, i_items, i_bytesafter;
3241 Atom net_wm_supported;
3242 union { Atom *p_atom; unsigned char *p_char; } p_args;
3244 p_args.p_atom = NULL;
3246 p_vout->p_sys->b_net_wm_state_fullscreen =
3247 p_vout->p_sys->b_net_wm_state_above =
3248 p_vout->p_sys->b_net_wm_state_below =
3249 p_vout->p_sys->b_net_wm_state_stays_on_top =
3253 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3255 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3256 DefaultRootWindow( p_vout->p_sys->p_display ),
3258 0, 16384, False, AnyPropertyType,
3260 &i_format, &i_items, &i_bytesafter,
3261 (unsigned char **)&p_args );
3263 if( i_ret != Success || i_items == 0 ) return;
3265 msg_Dbg( p_vout, "Window manager supports NetWM" );
3267 p_vout->p_sys->net_wm_state =
3268 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3269 p_vout->p_sys->net_wm_state_fullscreen =
3270 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3272 p_vout->p_sys->net_wm_state_above =
3273 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3274 p_vout->p_sys->net_wm_state_below =
3275 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3276 p_vout->p_sys->net_wm_state_stays_on_top =
3277 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3280 for( i = 0; i < i_items; i++ )
3282 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3285 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3286 p_vout->p_sys->b_net_wm_state_fullscreen = true;
3288 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3290 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3291 p_vout->p_sys->b_net_wm_state_above = true;
3293 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3295 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3296 p_vout->p_sys->b_net_wm_state_below = true;
3298 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3301 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3302 p_vout->p_sys->b_net_wm_state_stays_on_top = true;
3306 XFree( p_args.p_atom );
3309 /*****************************************************************************
3310 * Key events handling
3311 *****************************************************************************/
3317 } x11keys_to_vlckeys[] =
3319 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3320 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3321 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3322 { XK_F12, KEY_F12 },
3324 { XK_Return, KEY_ENTER },
3325 { XK_KP_Enter, KEY_ENTER },
3326 { XK_space, KEY_SPACE },
3327 { XK_Escape, KEY_ESC },
3329 { XK_Menu, KEY_MENU },
3330 { XK_Left, KEY_LEFT },
3331 { XK_Right, KEY_RIGHT },
3333 { XK_Down, KEY_DOWN },
3335 { XK_Home, KEY_HOME },
3336 { XK_End, KEY_END },
3337 { XK_Page_Up, KEY_PAGEUP },
3338 { XK_Page_Down, KEY_PAGEDOWN },
3340 { XK_Insert, KEY_INSERT },
3341 { XK_Delete, KEY_DELETE },
3342 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3343 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3344 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3345 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3346 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3347 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3348 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3353 static int ConvertKey( int i_key )
3357 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3359 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3361 return x11keys_to_vlckeys[i].i_vlckey;
3368 /*****************************************************************************
3369 * WindowOnTop: Switches the "always on top" state of the video window.
3370 *****************************************************************************/
3371 static int WindowOnTop( vout_thread_t *p_vout, bool b_on_top )
3373 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3375 XClientMessageEvent event;
3377 memset( &event, 0, sizeof( XClientMessageEvent ) );
3379 event.type = ClientMessage;
3380 event.message_type = p_vout->p_sys->net_wm_state;
3381 event.display = p_vout->p_sys->p_display;
3382 event.window = p_vout->p_sys->p_win->base_window;
3384 event.data.l[ 0 ] = b_on_top; /* set property */
3385 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3387 XSendEvent( p_vout->p_sys->p_display,
3388 DefaultRootWindow( p_vout->p_sys->p_display ),
3389 False, SubstructureRedirectMask,
3393 /* use _NET_WM_STATE_ABOVE if window manager
3394 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3395 else if( p_vout->p_sys->b_net_wm_state_above )
3397 XClientMessageEvent event;
3399 memset( &event, 0, sizeof( XClientMessageEvent ) );
3401 event.type = ClientMessage;
3402 event.message_type = p_vout->p_sys->net_wm_state;
3403 event.display = p_vout->p_sys->p_display;
3404 event.window = p_vout->p_sys->p_win->base_window;
3406 event.data.l[ 0 ] = b_on_top; /* set property */
3407 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3409 XSendEvent( p_vout->p_sys->p_display,
3410 DefaultRootWindow( p_vout->p_sys->p_display ),
3411 False, SubstructureRedirectMask,