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 #ifdef HAVE_SYS_SHM_H
120 static int i_shm_major = 0;
123 static void ToggleFullScreen ( vout_thread_t * );
125 static void EnableXScreenSaver ( vout_thread_t * );
126 static void DisableXScreenSaver ( vout_thread_t * );
128 static void CreateCursor ( vout_thread_t * );
129 static void DestroyCursor ( vout_thread_t * );
130 static void ToggleCursor ( vout_thread_t * );
132 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
133 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, picture_heap_t * );
134 static void XVideoReleasePort( vout_thread_t *, int );
137 #ifdef MODULE_NAME_IS_x11
138 static void SetPalette ( vout_thread_t *,
139 uint16_t *, uint16_t *, uint16_t * );
142 #ifdef MODULE_NAME_IS_xvmc
143 static void RenderVideo ( vout_thread_t *, picture_t * );
144 static int xvmc_check_yv12( Display *display, XvPortID port );
145 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
148 static void TestNetWMSupport( vout_thread_t * );
149 static int ConvertKey( int );
151 static int WindowOnTop( vout_thread_t *, bool );
153 static int X11ErrorHandler( Display *, XErrorEvent * );
156 static void EnablePixelDoubling( vout_thread_t *p_vout );
157 static void DisablePixelDoubling( vout_thread_t *p_vout );
161 static const int i_backlight_on_interval = 300;
166 /*****************************************************************************
167 * Activate: allocate X11 video thread output method
168 *****************************************************************************
169 * This function allocate and initialize a X11 vout method. It uses some of the
170 * vout properties to choose the window size, and change them according to the
171 * actual properties of the display.
172 *****************************************************************************/
173 int Activate ( vlc_object_t *p_this )
175 vout_thread_t *p_vout = (vout_thread_t *)p_this;
178 #if defined(MODULE_NAME_IS_xvmc)
181 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
183 vlc_fourcc_t i_chroma = 0;
187 p_vout->pf_init = InitVideo;
188 p_vout->pf_end = EndVideo;
189 p_vout->pf_manage = ManageVideo;
190 #ifdef MODULE_NAME_IS_xvmc
191 p_vout->pf_render = RenderVideo;
193 p_vout->pf_render = NULL;
195 p_vout->pf_display = DisplayVideo;
196 p_vout->pf_control = Control;
198 /* Allocate structure */
199 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
200 if( p_vout->p_sys == NULL )
203 /* key and mouse event handling */
204 p_vout->p_sys->i_vout_event = var_CreateGetInteger( p_vout, "vout-event" );
206 /* Open display, using the "display" config variable or the DISPLAY
207 * environment variable */
208 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
210 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
212 if( p_vout->p_sys->p_display == NULL ) /* error */
214 msg_Err( p_vout, "cannot open display %s",
215 XDisplayName( psz_display ) );
216 free( p_vout->p_sys );
222 /* Replace error handler so we can intercept some non-fatal errors */
223 XSetErrorHandler( X11ErrorHandler );
225 /* Get a screen ID matching the XOpenDisplay return value */
226 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
228 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
229 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
232 if( strlen( psz_chroma ) >= 4 )
234 /* Do not use direct assignment because we are not sure of the
236 memcpy(&i_chroma, psz_chroma, 4);
245 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
246 i_chroma, (char*)&i_chroma );
250 i_chroma = p_vout->render.i_chroma;
253 /* Check that we have access to an XVideo port providing this chroma */
254 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
256 if( p_vout->p_sys->i_xvport < 0 )
258 /* If a specific chroma format was requested, then we don't try to
259 * be cleverer than the user. He knew pretty well what he wanted. */
262 XCloseDisplay( p_vout->p_sys->p_display );
263 free( p_vout->p_sys );
267 /* It failed, but it's not completely lost ! We try to open an
268 * XVideo port for an YUY2 picture. We'll need to do an YUV
269 * conversion, but at least it has got scaling. */
270 p_vout->p_sys->i_xvport =
271 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
273 if( p_vout->p_sys->i_xvport < 0 )
275 /* It failed, but it's not completely lost ! We try to open an
276 * XVideo port for a simple 16bpp RGB picture. We'll need to do
277 * an YUV conversion, but at least it has got scaling. */
278 p_vout->p_sys->i_xvport =
279 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
281 if( p_vout->p_sys->i_xvport < 0 )
283 XCloseDisplay( p_vout->p_sys->p_display );
284 free( p_vout->p_sys );
289 p_vout->output.i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, X112VLC_FOURCC(p_vout->output.i_chroma) );
290 #elif defined(MODULE_NAME_IS_glx)
292 int i_opcode, i_evt, i_err = 0;
293 int i_maj, i_min = 0;
295 /* Check for GLX extension */
296 if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
297 &i_opcode, &i_evt, &i_err ) )
299 msg_Err( p_this, "GLX extension not supported" );
300 XCloseDisplay( p_vout->p_sys->p_display );
301 free( p_vout->p_sys );
304 if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
306 msg_Err( p_this, "glXQueryExtension failed" );
307 XCloseDisplay( p_vout->p_sys->p_display );
308 free( p_vout->p_sys );
312 /* Check GLX version */
313 if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
315 msg_Err( p_this, "glXQueryVersion failed" );
316 XCloseDisplay( p_vout->p_sys->p_display );
317 free( p_vout->p_sys );
320 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
322 p_vout->p_sys->b_glx13 = false;
323 msg_Dbg( p_this, "using GLX 1.2 API" );
327 p_vout->p_sys->b_glx13 = true;
328 msg_Dbg( p_this, "using GLX 1.3 API" );
333 /* Create blank cursor (for mouse cursor autohiding) */
334 p_vout->p_sys->i_time_mouse_last_moved = mdate();
335 p_vout->p_sys->i_mouse_hide_timeout =
336 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
337 p_vout->p_sys->b_mouse_pointer_visible = 1;
338 CreateCursor( p_vout );
340 /* Set main window's size */
341 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
342 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
343 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
344 /* Spawn base window - this window will include the video output window,
345 * but also command buttons, subtitles and other indicators */
346 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
348 msg_Err( p_vout, "cannot create X11 window" );
349 DestroyCursor( p_vout );
350 XCloseDisplay( p_vout->p_sys->p_display );
351 free( p_vout->p_sys );
355 /* Open and initialize device. */
356 if( InitDisplay( p_vout ) )
358 msg_Err( p_vout, "cannot initialize X11 display" );
359 DestroyCursor( p_vout );
360 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
361 XCloseDisplay( p_vout->p_sys->p_display );
362 free( p_vout->p_sys );
366 /* Disable screen saver */
367 DisableXScreenSaver( p_vout );
370 p_vout->p_sys->b_altfullscreen = 0;
371 p_vout->p_sys->i_time_button_last_pressed = 0;
373 TestNetWMSupport( p_vout );
375 #ifdef MODULE_NAME_IS_xvmc
376 p_vout->p_sys->p_last_subtitle_save = NULL;
377 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
379 /* Look what method was requested */
380 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
381 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
384 if( (strcmp(psz_value, "bob") == 0) ||
385 (strcmp(psz_value, "blend") == 0) )
386 p_vout->p_sys->xvmc_deinterlace_method = 2;
387 else if (strcmp(psz_value, "discard") == 0)
388 p_vout->p_sys->xvmc_deinterlace_method = 1;
390 p_vout->p_sys->xvmc_deinterlace_method = 0;
394 p_vout->p_sys->xvmc_deinterlace_method = 0;
396 /* Look what method was requested */
397 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
398 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
399 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
403 if( strncmp( psz_value, "eq", 2 ) == 0 )
404 p_vout->p_sys->xvmc_crop_style = 1;
405 else if( strncmp( psz_value, "4-16", 4 ) == 0)
406 p_vout->p_sys->xvmc_crop_style = 2;
407 else if( strncmp( psz_value, "16-4", 4 ) == 0)
408 p_vout->p_sys->xvmc_crop_style = 3;
410 p_vout->p_sys->xvmc_crop_style = 0;
414 p_vout->p_sys->xvmc_crop_style = 0;
416 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
417 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
419 if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
421 msg_Err( p_vout, "no XVMC capability found" );
422 Deactivate( p_this );
425 subpicture_t sub_pic;
426 sub_pic.p_sys = NULL;
427 p_vout->p_sys->last_date = 0;
431 p_vout->p_sys->i_hw_scale = 1;
435 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
436 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
437 if ( p_vout->p_sys->p_octx == NULL ) {
438 msg_Err( p_vout, "Could not get osso context" );
440 msg_Dbg( p_vout, "Initialized osso context" );
444 /* Variable to indicate if the window should be on top of others */
445 /* Trigger a callback right now */
446 var_Get( p_vout, "video-on-top", &val );
447 var_Set( p_vout, "video-on-top", val );
452 /*****************************************************************************
453 * Deactivate: destroy X11 video thread output method
454 *****************************************************************************
455 * Terminate an output method created by Open
456 *****************************************************************************/
457 void Deactivate ( vlc_object_t *p_this )
459 vout_thread_t *p_vout = (vout_thread_t *)p_this;
461 /* If the fullscreen window is still open, close it */
462 if( p_vout->b_fullscreen )
464 ToggleFullScreen( p_vout );
467 /* Restore cursor if it was blanked */
468 if( !p_vout->p_sys->b_mouse_pointer_visible )
470 ToggleCursor( p_vout );
473 #ifdef MODULE_NAME_IS_x11
474 /* Destroy colormap */
475 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
477 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
479 #elif defined(MODULE_NAME_IS_xvideo)
480 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
481 #elif defined(MODULE_NAME_IS_xvmc)
482 if( p_vout->p_sys->xvmc_cap )
484 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
485 xxmc_dispose_context( p_vout );
486 if( p_vout->p_sys->old_subpic )
488 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
489 p_vout->p_sys->old_subpic = NULL;
491 if( p_vout->p_sys->new_subpic )
493 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
494 p_vout->p_sys->new_subpic = NULL;
496 free( p_vout->p_sys->xvmc_cap );
497 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
502 DisablePixelDoubling(p_vout);
505 DestroyCursor( p_vout );
506 EnableXScreenSaver( p_vout );
507 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
508 XCloseDisplay( p_vout->p_sys->p_display );
510 /* Destroy structure */
511 #ifdef MODULE_NAME_IS_xvmc
512 free_context_lock( &p_vout->p_sys->xvmc_lock );
516 if ( p_vout->p_sys->p_octx != NULL ) {
517 msg_Dbg( p_vout, "Deinitializing osso context" );
518 osso_deinitialize( p_vout->p_sys->p_octx );
522 free( p_vout->p_sys );
525 #ifdef MODULE_NAME_IS_xvmc
527 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
530 static int xvmc_check_yv12( Display *display, XvPortID port )
532 XvImageFormatValues *formatValues;
536 formatValues = XvListImageFormats( display, port, &formats );
538 for( i = 0; i < formats; i++ )
540 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
541 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
543 XFree (formatValues);
548 XFree (formatValues);
552 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
554 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
557 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
560 int xv_double_buffer;
562 xv_double_buffer = 1;
564 XLockDisplay( p_vout->p_sys->p_display );
565 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
567 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
569 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
570 XUnlockDisplay( p_vout->p_sys->p_display );
572 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
573 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
576 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
578 vlc_xxmc_t *xxmc = NULL;
580 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
582 xxmc = &p_pic->p_sys->xxmc_data;
583 if( (!xxmc->decoded ||
584 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
586 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
591 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
592 if (p_vout->p_sys->p_last_subtitle != NULL)
594 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
596 p_vout->p_sys->new_subpic =
597 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
598 p_vout->p_sys->xvmc_width,
599 p_vout->p_sys->xvmc_height,
600 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
602 if (p_vout->p_sys->new_subpic)
604 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
605 XvMCClearSubpicture( p_vout->p_sys->p_display,
606 p_vout->p_sys->new_subpic,
609 p_vout->p_sys->xvmc_width,
610 p_vout->p_sys->xvmc_height,
612 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
613 clear_xx44_palette( &p_vout->p_sys->palette );
615 if( sub_pic.p_sys == NULL )
617 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
618 if( sub_pic.p_sys != NULL )
620 sub_pic.p_sys->p_vout = p_vout;
621 sub_pic.p_sys->xvmc_surf = NULL;
622 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
625 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
626 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
627 sub_pic.p->i_pitch = p_vout->output.i_width;
629 memset( p_vout->p_sys->subImage->data, 0,
630 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
632 if (p_vout->p_last_subtitle != NULL)
634 blend_xx44( p_vout->p_sys->subImage->data,
635 p_vout->p_last_subtitle,
636 p_vout->p_sys->subImage->width,
637 p_vout->p_sys->subImage->height,
638 p_vout->p_sys->subImage->width,
639 &p_vout->p_sys->palette,
640 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
643 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
644 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
645 p_vout->p_sys->new_subpic,
646 p_vout->p_sys->subImage,
649 p_vout->output.i_width, /* overlay->width, */
650 p_vout->output.i_height, /* overlay->height */
653 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
654 if (p_vout->p_sys->old_subpic)
656 xxmc_xvmc_free_subpicture( p_vout,
657 p_vout->p_sys->old_subpic);
658 p_vout->p_sys->old_subpic = NULL;
660 if (p_vout->p_sys->new_subpic)
662 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
663 p_vout->p_sys->new_subpic = NULL;
664 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
665 p_vout->p_sys->xvmc_palette,
667 p_vout->p_sys->old_subpic->num_palette_entries,
668 p_vout->p_sys->old_subpic->entry_bytes,
669 p_vout->p_sys->old_subpic->component_order );
670 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
671 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
672 p_vout->p_sys->old_subpic,
673 p_vout->p_sys->xvmc_palette );
674 XvMCFlushSubpicture( p_vout->p_sys->p_display,
675 p_vout->p_sys->old_subpic);
676 XvMCSyncSubpicture( p_vout->p_sys->p_display,
677 p_vout->p_sys->old_subpic );
678 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
681 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
682 if (p_vout->p_sys->xvmc_backend_subpic )
684 XvMCBlendSubpicture( p_vout->p_sys->p_display,
685 p_pic->p_sys->xvmc_surf,
686 p_vout->p_sys->old_subpic,
689 p_vout->p_sys->xvmc_width,
690 p_vout->p_sys->xvmc_height,
693 p_vout->p_sys->xvmc_width,
694 p_vout->p_sys->xvmc_height );
698 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
699 p_pic->p_sys->xvmc_surf,
700 p_pic->p_sys->xvmc_surf,
701 p_vout->p_sys->old_subpic,
704 p_vout->p_sys->xvmc_width,
705 p_vout->p_sys->xvmc_height,
708 p_vout->p_sys->xvmc_width,
709 p_vout->p_sys->xvmc_height );
711 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
716 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
717 if( p_vout->p_sys->xvmc_backend_subpic )
719 XvMCBlendSubpicture( p_vout->p_sys->p_display,
720 p_pic->p_sys->xvmc_surf,
721 p_vout->p_sys->old_subpic,
723 p_vout->p_sys->xvmc_width,
724 p_vout->p_sys->xvmc_height,
726 p_vout->p_sys->xvmc_width,
727 p_vout->p_sys->xvmc_height );
731 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
732 p_pic->p_sys->xvmc_surf,
733 p_pic->p_sys->xvmc_surf,
734 p_vout->p_sys->old_subpic,
736 p_vout->p_sys->xvmc_width,
737 p_vout->p_sys->xvmc_height,
739 p_vout->p_sys->xvmc_width,
740 p_vout->p_sys->xvmc_height );
742 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
745 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
747 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
749 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
754 /*****************************************************************************
755 * EnablePixelDoubling: Enables pixel doubling
756 *****************************************************************************
757 * Checks if the double size image fits in current window, and enables pixel
758 * doubling accordingly. The i_hw_scale is the integer scaling factor.
759 *****************************************************************************/
760 static void EnablePixelDoubling( vout_thread_t *p_vout )
762 int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
763 int i_vert_scale = ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
764 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
765 p_vout->p_sys->i_hw_scale = 2;
766 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
767 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
771 /*****************************************************************************
772 * DisablePixelDoubling: Disables pixel doubling
773 *****************************************************************************
774 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
775 *****************************************************************************/
776 static void DisablePixelDoubling( vout_thread_t *p_vout )
778 if ( p_vout->p_sys->i_hw_scale > 1 ) {
779 msg_Dbg( p_vout, "Disabling pixel doubling" );
780 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
781 p_vout->p_sys->i_hw_scale = 1;
788 /*****************************************************************************
789 * InitVideo: initialize X11 video thread output method
790 *****************************************************************************
791 * This function create the XImages needed by the output thread. It is called
792 * at the beginning of the thread, but also each time the window is resized.
793 *****************************************************************************/
794 static int InitVideo( vout_thread_t *p_vout )
796 unsigned int i_index = 0;
799 I_OUTPUTPICTURES = 0;
801 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
802 /* Initialize the output structure; we already found an XVideo port,
803 * and the corresponding chroma we will be using. Since we can
804 * arbitrary scale, stick to the coordinates and aspect. */
805 p_vout->output.i_width = p_vout->render.i_width;
806 p_vout->output.i_height = p_vout->render.i_height;
807 p_vout->output.i_aspect = p_vout->render.i_aspect;
809 p_vout->fmt_out = p_vout->fmt_in;
810 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
812 #if XvVersion < 2 || ( XvVersion == 2 && XvRevision < 2 )
813 switch( p_vout->output.i_chroma )
815 case VLC_CODEC_RGB16:
816 #if defined( WORDS_BIGENDIAN )
817 p_vout->output.i_rmask = 0xf800;
818 p_vout->output.i_gmask = 0x07e0;
819 p_vout->output.i_bmask = 0x001f;
821 p_vout->output.i_rmask = 0x001f;
822 p_vout->output.i_gmask = 0x07e0;
823 p_vout->output.i_bmask = 0xf800;
826 case VLC_CODEC_RGB15:
827 #if defined( WORDS_BIGENDIAN )
828 p_vout->output.i_rmask = 0x7c00;
829 p_vout->output.i_gmask = 0x03e0;
830 p_vout->output.i_bmask = 0x001f;
832 p_vout->output.i_rmask = 0x001f;
833 p_vout->output.i_gmask = 0x03e0;
834 p_vout->output.i_bmask = 0x7c00;
840 #elif defined(MODULE_NAME_IS_x11)
841 /* Initialize the output structure: RGB with square pixels, whatever
842 * the input format is, since it's the only format we know */
843 switch( p_vout->p_sys->i_screen_depth )
845 case 8: /* FIXME: set the palette */
846 p_vout->output.i_chroma = VLC_CODEC_RGB8; break;
848 p_vout->output.i_chroma = VLC_CODEC_RGB15; break;
850 p_vout->output.i_chroma = VLC_CODEC_RGB16; break;
853 p_vout->output.i_chroma = VLC_CODEC_RGB32; break;
855 msg_Err( p_vout, "unknown screen depth %i",
856 p_vout->p_sys->i_screen_depth );
861 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width / p_vout->p_sys->i_hw_scale,
862 p_vout->p_sys->p_win->i_height / p_vout->p_sys->i_hw_scale,
864 &p_vout->fmt_out.i_visible_width,
865 &p_vout->fmt_out.i_visible_height );
867 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
868 p_vout->p_sys->p_win->i_height,
870 &p_vout->fmt_out.i_visible_width,
871 &p_vout->fmt_out.i_visible_height );
874 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
876 p_vout->output.i_width = p_vout->fmt_out.i_width =
877 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
878 p_vout->fmt_in.i_visible_width;
879 p_vout->output.i_height = p_vout->fmt_out.i_height =
880 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
881 p_vout->fmt_in.i_visible_height;
882 p_vout->fmt_out.i_x_offset =
883 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
884 p_vout->fmt_in.i_visible_width;
885 p_vout->fmt_out.i_y_offset =
886 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
887 p_vout->fmt_in.i_visible_height;
889 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
890 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
891 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
893 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
894 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
895 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
896 p_vout->fmt_out.i_visible_width,
897 p_vout->fmt_out.i_visible_height );
900 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
901 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
905 /* Find an empty picture slot */
906 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
908 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
910 p_pic = p_vout->p_picture + i_index;
915 /* Allocate the picture */
916 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
921 p_pic->i_status = DESTROYED_PICTURE;
922 p_pic->i_type = DIRECT_PICTURE;
924 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
929 if( p_vout->output.i_chroma == VLC_CODEC_YV12 )
931 /* U and V inverted compared to I420
932 * Fixme: this should be handled by the vout core */
933 p_vout->output.i_chroma = VLC_CODEC_I420;
934 p_vout->fmt_out.i_chroma = VLC_CODEC_I420;
940 /*****************************************************************************
941 * DisplayVideo: displays previously rendered output
942 *****************************************************************************
943 * This function sends the currently rendered image to X11 server.
944 * (The Xv extension takes care of "double-buffering".)
945 *****************************************************************************/
946 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
948 unsigned int i_width, i_height, i_x, i_y;
950 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
951 p_vout->p_sys->p_win->i_height,
952 &i_x, &i_y, &i_width, &i_height );
954 #ifdef MODULE_NAME_IS_xvmc
955 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
957 vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
958 if( !xxmc->decoded ||
959 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
961 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
963 xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
964 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
968 int src_width = p_vout->output.i_width;
969 int src_height = p_vout->output.i_height;
972 if( p_vout->p_sys->xvmc_crop_style == 1 )
979 else if( p_vout->p_sys->xvmc_crop_style == 2 )
986 else if( p_vout->p_sys->xvmc_crop_style == 3 )
1000 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1001 { /* BOB DEINTERLACE */
1002 if( (p_pic->p_sys->nb_display == 0) ||
1003 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1005 first_field = (p_pic->b_top_field_first) ?
1006 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1010 first_field = (p_pic->b_top_field_first) ?
1011 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1016 first_field = XVMC_FRAME_PICTURE;
1019 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1020 XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
1021 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1022 XvMCPutSurface( p_vout->p_sys->p_display,
1023 p_pic->p_sys->xvmc_surf,
1024 p_vout->p_sys->p_win->video_window,
1035 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1036 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1037 { /* BOB DEINTERLACE */
1038 if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1040 mtime_t last_date = p_pic->date;
1042 vlc_mutex_lock( &p_vout->picture_lock );
1043 if( !p_vout->p_sys->last_date )
1045 p_pic->date += 20000;
1049 p_pic->date = ((3 * p_pic->date -
1050 p_vout->p_sys->last_date) / 2 );
1052 p_vout->p_sys->last_date = last_date;
1054 p_pic->p_sys->nb_display = 1;
1055 vlc_mutex_unlock( &p_vout->picture_lock );
1059 p_pic->p_sys->nb_display = 0;
1063 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1066 #ifdef HAVE_SYS_SHM_H
1067 if( p_vout->p_sys->i_shm_opcode )
1069 /* Display rendered image using shared memory extension */
1070 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1071 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1072 p_vout->p_sys->p_win->video_window,
1073 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1074 p_vout->fmt_out.i_x_offset,
1075 p_vout->fmt_out.i_y_offset,
1076 p_vout->fmt_out.i_visible_width,
1077 p_vout->fmt_out.i_visible_height,
1078 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1079 False /* Don't put True here or you'll waste your CPU */ );
1081 XShmPutImage( p_vout->p_sys->p_display,
1082 p_vout->p_sys->p_win->video_window,
1083 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1084 p_vout->fmt_out.i_x_offset,
1085 p_vout->fmt_out.i_y_offset,
1086 0 /*dest_x*/, 0 /*dest_y*/,
1087 p_vout->fmt_out.i_visible_width,
1088 p_vout->fmt_out.i_visible_height,
1089 False /* Don't put True here ! */ );
1093 #endif /* HAVE_SYS_SHM_H */
1095 /* Use standard XPutImage -- this is gonna be slow ! */
1096 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1097 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1098 p_vout->p_sys->p_win->video_window,
1099 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1100 p_vout->fmt_out.i_x_offset,
1101 p_vout->fmt_out.i_y_offset,
1102 p_vout->fmt_out.i_visible_width,
1103 p_vout->fmt_out.i_visible_height,
1104 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1106 XPutImage( p_vout->p_sys->p_display,
1107 p_vout->p_sys->p_win->video_window,
1108 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1109 p_vout->fmt_out.i_x_offset,
1110 p_vout->fmt_out.i_y_offset,
1111 0 /*dest_x*/, 0 /*dest_y*/,
1112 p_vout->fmt_out.i_visible_width,
1113 p_vout->fmt_out.i_visible_height );
1117 /* Make sure the command is sent now - do NOT use XFlush !*/
1118 XSync( p_vout->p_sys->p_display, False );
1121 /*****************************************************************************
1122 * ManageVideo: handle X11 events
1123 *****************************************************************************
1124 * This function should be called regularly by video output thread. It manages
1125 * X11 events and allows window resizing. It returns a non null value on
1127 *****************************************************************************/
1128 static int ManageVideo( vout_thread_t *p_vout )
1130 XEvent xevent; /* X11 event */
1133 #ifdef MODULE_NAME_IS_xvmc
1134 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1137 /* Handle events from the owner window */
1138 if( p_vout->p_sys->p_win->owner_window )
1140 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1141 p_vout->p_sys->p_win->owner_window->handle.xid,
1142 StructureNotifyMask, &xevent ) == True )
1144 /* ConfigureNotify event: prepare */
1145 if( xevent.type == ConfigureNotify )
1147 /* Update dimensions */
1148 XResizeWindow( p_vout->p_sys->p_display,
1149 p_vout->p_sys->p_win->base_window,
1150 xevent.xconfigure.width,
1151 xevent.xconfigure.height );
1156 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1157 * output window's size changed, MapNotify and UnmapNotify to know if the
1158 * window is mapped (and if the display is useful), and ClientMessages
1159 * to intercept window destruction requests */
1161 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1162 p_vout->p_sys->p_win->base_window,
1163 StructureNotifyMask | KeyPressMask |
1164 ButtonPressMask | ButtonReleaseMask |
1165 PointerMotionMask | Button1MotionMask , &xevent )
1168 /* ConfigureNotify event: prepare */
1169 if( xevent.type == ConfigureNotify )
1171 if( (unsigned int)xevent.xconfigure.width
1172 != p_vout->p_sys->p_win->i_width
1173 || (unsigned int)xevent.xconfigure.height
1174 != p_vout->p_sys->p_win->i_height )
1176 /* Update dimensions */
1177 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1178 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1179 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1182 /* Keyboard event */
1183 else if( xevent.type == KeyPress )
1185 unsigned int state = xevent.xkey.state;
1186 KeySym x_key_symbol;
1187 char i_key; /* ISO Latin-1 key */
1189 /* We may have keys like F1 trough F12, ESC ... */
1190 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1191 xevent.xkey.keycode, 0 );
1192 val.i_int = ConvertKey( (int)x_key_symbol );
1194 xevent.xkey.state &= ~ShiftMask;
1195 xevent.xkey.state &= ~ControlMask;
1196 xevent.xkey.state &= ~Mod1Mask;
1199 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1202 * The reason why I use this instead of XK_0 is that
1203 * with XLookupString, we don't have to care about
1210 if( state & ShiftMask )
1212 val.i_int |= KEY_MODIFIER_SHIFT;
1214 if( state & ControlMask )
1216 val.i_int |= KEY_MODIFIER_CTRL;
1218 if( state & Mod1Mask )
1220 val.i_int |= KEY_MODIFIER_ALT;
1222 var_Set( p_vout->p_libvlc, "key-pressed", val );
1226 else if( xevent.type == ButtonPress )
1228 switch( ((XButtonEvent *)&xevent)->button )
1231 var_Get( p_vout, "mouse-button-down", &val );
1233 var_Set( p_vout, "mouse-button-down", val );
1235 /* detect double-clicks */
1236 if( ( ((XButtonEvent *)&xevent)->time -
1237 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1239 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1242 p_vout->p_sys->i_time_button_last_pressed =
1243 ((XButtonEvent *)&xevent)->time;
1246 var_Get( p_vout, "mouse-button-down", &val );
1248 var_Set( p_vout, "mouse-button-down", val );
1252 var_Get( p_vout, "mouse-button-down", &val );
1254 var_Set( p_vout, "mouse-button-down", val );
1258 var_Get( p_vout, "mouse-button-down", &val );
1260 var_Set( p_vout, "mouse-button-down", val );
1264 var_Get( p_vout, "mouse-button-down", &val );
1266 var_Set( p_vout, "mouse-button-down", val );
1271 else if( xevent.type == ButtonRelease )
1273 switch( ((XButtonEvent *)&xevent)->button )
1277 var_Get( p_vout, "mouse-button-down", &val );
1279 var_Set( p_vout, "mouse-button-down", val );
1281 var_SetBool( p_vout, "mouse-clicked", true );
1282 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", false );
1288 var_Get( p_vout, "mouse-button-down", &val );
1290 var_Set( p_vout, "mouse-button-down", val );
1292 var_Get( p_vout->p_libvlc, "intf-show", &val );
1293 val.b_bool = !val.b_bool;
1294 var_Set( p_vout->p_libvlc, "intf-show", val );
1300 var_Get( p_vout, "mouse-button-down", &val );
1302 var_Set( p_vout, "mouse-button-down", val );
1304 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
1309 var_Get( p_vout, "mouse-button-down", &val );
1311 var_Set( p_vout, "mouse-button-down", val );
1315 var_Get( p_vout, "mouse-button-down", &val );
1317 var_Set( p_vout, "mouse-button-down", val );
1323 else if( xevent.type == MotionNotify )
1325 unsigned int i_width, i_height, i_x, i_y;
1327 /* somewhat different use for vout_PlacePicture:
1328 * here the values are needed to give to mouse coordinates
1329 * in the original picture space */
1330 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1331 p_vout->p_sys->p_win->i_height,
1332 &i_x, &i_y, &i_width, &i_height );
1334 /* Compute the x coordinate and check if the value is
1335 in [0,p_vout->fmt_in.i_visible_width] */
1336 val.i_int = ( xevent.xmotion.x - i_x ) *
1337 p_vout->fmt_in.i_visible_width / i_width +
1338 p_vout->fmt_in.i_x_offset;
1340 if( (int)(xevent.xmotion.x - i_x) < 0 )
1342 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1343 val.i_int = p_vout->fmt_in.i_visible_width;
1345 var_Set( p_vout, "mouse-x", val );
1347 /* compute the y coordinate and check if the value is
1348 in [0,p_vout->fmt_in.i_visible_height] */
1349 val.i_int = ( xevent.xmotion.y - i_y ) *
1350 p_vout->fmt_in.i_visible_height / i_height +
1351 p_vout->fmt_in.i_y_offset;
1353 if( (int)(xevent.xmotion.y - i_y) < 0 )
1355 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1356 val.i_int = p_vout->fmt_in.i_visible_height;
1358 var_Set( p_vout, "mouse-y", val );
1360 var_SetBool( p_vout, "mouse-moved", true );
1362 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1363 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1365 ToggleCursor( p_vout );
1368 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1369 || xevent.type == MapNotify
1370 || xevent.type == UnmapNotify )
1372 /* Ignore these events */
1374 else /* Other events */
1376 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1380 /* Handle events for video output sub-window */
1381 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1382 p_vout->p_sys->p_win->video_window,
1383 ExposureMask, &xevent ) == True )
1385 /* Window exposed (only handled if stream playback is paused) */
1386 if( xevent.type == Expose )
1388 if( ((XExposeEvent *)&xevent)->count == 0 )
1390 /* (if this is the last a collection of expose events...) */
1392 #if defined(MODULE_NAME_IS_xvideo)
1393 x11_window_t *p_win = p_vout->p_sys->p_win;
1395 /* Paint the colour key if needed */
1396 if( p_vout->p_sys->b_paint_colourkey &&
1397 xevent.xexpose.window == p_win->video_window )
1399 XSetForeground( p_vout->p_sys->p_display,
1400 p_win->gc, p_vout->p_sys->i_colourkey );
1401 XFillRectangle( p_vout->p_sys->p_display,
1402 p_win->video_window, p_win->gc, 0, 0,
1403 p_win->i_width, p_win->i_height );
1408 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1410 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1411 ->stream.control.i_status )
1413 /* XVideoDisplay( p_vout )*/;
1421 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1422 * are handled - according to the man pages, the format is always 32
1424 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1425 ClientMessage, &xevent ) )
1427 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1428 && ((Atom)xevent.xclient.data.l[0]
1429 == p_vout->p_sys->p_win->wm_delete_window ) )
1431 /* the user wants to close the window */
1432 playlist_t * p_playlist = pl_Hold( p_vout );
1433 if( p_playlist != NULL )
1435 playlist_Stop( p_playlist );
1436 pl_Release( p_vout );
1444 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1446 /* Update the object variable and trigger callback */
1447 var_SetBool( p_vout, "fullscreen", !p_vout->b_fullscreen );
1449 ToggleFullScreen( p_vout );
1450 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1453 /* autoscale toggle */
1454 if( p_vout->i_changes & VOUT_SCALE_CHANGE )
1456 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
1458 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
1459 p_vout->i_zoom = ZOOM_FP_FACTOR;
1461 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1464 /* scaling factor */
1465 if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
1467 p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
1469 p_vout->b_autoscale = false;
1471 (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
1473 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1476 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1477 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1479 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1480 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1482 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1483 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1484 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1485 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1486 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1487 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1488 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1489 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1491 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1497 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1498 * the size flag inside the fullscreen routine)
1500 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1502 unsigned int i_width, i_height, i_x, i_y;
1504 #ifdef MODULE_NAME_IS_x11
1505 /* We need to signal the vout thread about the size change because it
1506 * is doing the rescaling */
1508 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1511 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1512 p_vout->p_sys->p_win->i_height,
1513 &i_x, &i_y, &i_width, &i_height );
1515 XMoveResizeWindow( p_vout->p_sys->p_display,
1516 p_vout->p_sys->p_win->video_window,
1517 i_x, i_y, i_width, i_height );
1520 /* cursor hiding depending on --vout-event option
1522 * value = 1 (Fullsupport) (default value)
1523 * or value = 2 (Fullscreen-Only) and condition met
1525 bool b_vout_event = ( ( p_vout->p_sys->i_vout_event == 1 )
1526 || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1529 /* Autohide Cursour */
1530 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved >
1531 p_vout->p_sys->i_mouse_hide_timeout )
1533 /* Hide the mouse automatically */
1534 if( b_vout_event && p_vout->p_sys->b_mouse_pointer_visible )
1536 ToggleCursor( p_vout );
1540 #ifdef MODULE_NAME_IS_xvmc
1541 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1545 if ( p_vout->p_sys->p_octx != NULL ) {
1546 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1547 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1548 msg_Err( p_vout, "Could not disable backlight blanking" );
1550 msg_Dbg( p_vout, "Backlight blanking disabled" );
1552 p_vout->p_sys->i_backlight_on_counter = 0;
1554 p_vout->p_sys->i_backlight_on_counter ++;
1561 /*****************************************************************************
1562 * EndVideo: terminate X11 video thread output method
1563 *****************************************************************************
1564 * Destroy the X11 XImages created by Init. It is called at the end of
1565 * the thread, but also each time the window is resized.
1566 *****************************************************************************/
1567 static void EndVideo( vout_thread_t *p_vout )
1571 /* Free the direct buffers we allocated */
1572 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1575 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1579 /* following functions are local */
1581 /*****************************************************************************
1582 * CreateWindow: open and set-up X11 main window
1583 *****************************************************************************/
1584 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1586 XSizeHints xsize_hints;
1587 XSetWindowAttributes xwindow_attributes;
1588 XGCValues xgcvalues;
1591 bool b_map_notify = false;
1594 /* Prepare window manager hints and properties */
1595 p_win->wm_protocols =
1596 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1597 p_win->wm_delete_window =
1598 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1600 /* Never have a 0-pixel-wide window */
1601 xsize_hints.min_width = 2;
1602 xsize_hints.min_height = 1;
1604 /* Prepare window attributes */
1605 xwindow_attributes.backing_store = Always; /* save the hidden part */
1606 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1607 p_vout->p_sys->i_screen);
1608 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1610 if( !p_vout->b_fullscreen )
1612 p_win->owner_window = vout_RequestXWindow( p_vout, &p_win->i_x,
1613 &p_win->i_y, &p_win->i_width, &p_win->i_height );
1614 if( !p_win->owner_window )
1615 return VLC_EGENERIC;
1616 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1617 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1618 xsize_hints.flags = PSize | PMinSize;
1620 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1622 xsize_hints.x = p_win->i_x;
1623 xsize_hints.y = p_win->i_y;
1624 xsize_hints.flags |= PPosition;
1627 /* Select events we are interested in. */
1628 XSelectInput( p_vout->p_sys->p_display,
1629 p_win->owner_window->handle.xid, StructureNotifyMask );
1631 /* Get the parent window's geometry information */
1632 XGetGeometry( p_vout->p_sys->p_display,
1633 p_win->owner_window->handle.xid,
1634 &(Window){ 0 }, &(int){ 0 }, &(int){ 0 },
1637 &(unsigned){ 0 }, &(unsigned){ 0 } );
1639 /* From man XSelectInput: only one client at a time can select a
1640 * ButtonPress event, so we need to open a new window anyway. */
1641 p_win->base_window =
1642 XCreateWindow( p_vout->p_sys->p_display,
1643 p_win->owner_window->handle.xid,
1645 p_win->i_width, p_win->i_height,
1647 0, CopyFromParent, 0,
1648 CWBackingStore | CWBackPixel | CWEventMask,
1649 &xwindow_attributes );
1653 /* Fullscreen window size and position */
1654 p_win->owner_window = NULL;
1655 p_win->i_x = p_win->i_y = 0;
1657 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1659 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1661 /* Create the window and set hints - the window must receive
1662 * ConfigureNotify events, and until it is displayed, Expose and
1663 * MapNotify events. */
1665 p_win->base_window =
1666 XCreateWindow( p_vout->p_sys->p_display,
1667 DefaultRootWindow( p_vout->p_sys->p_display ),
1668 p_win->i_x, p_win->i_y,
1669 p_win->i_width, p_win->i_height,
1672 CWBackingStore | CWBackPixel | CWEventMask,
1673 &xwindow_attributes );
1675 var_Get( p_vout, "video-title", &val );
1676 if( !val.psz_string || !*val.psz_string )
1678 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1679 #ifdef MODULE_NAME_IS_x11
1680 VOUT_TITLE " (X11 output)"
1681 #elif defined(MODULE_NAME_IS_glx)
1682 VOUT_TITLE " (GLX output)"
1684 VOUT_TITLE " (XVideo output)"
1690 XStoreName( p_vout->p_sys->p_display,
1691 p_win->base_window, val.psz_string );
1693 free( val.psz_string );
1696 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1697 || (p_win->wm_delete_window == None)
1698 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1699 &p_win->wm_delete_window, 1 ) )
1701 /* WM_DELETE_WINDOW is not supported by window manager */
1702 msg_Warn( p_vout, "missing or bad window manager" );
1705 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1706 * event when using functions like XCopyArea */
1707 xgcvalues.graphics_exposures = False;
1708 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1710 GCGraphicsExposures, &xgcvalues );
1712 /* Wait till the window is mapped */
1713 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1716 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1717 SubstructureNotifyMask | StructureNotifyMask, &xevent);
1718 if( (xevent.type == MapNotify)
1719 && (xevent.xmap.window == p_win->base_window) )
1721 b_map_notify = true;
1723 else if( (xevent.type == ConfigureNotify)
1724 && (xevent.xconfigure.window == p_win->base_window) )
1726 p_win->i_width = xevent.xconfigure.width;
1727 p_win->i_height = xevent.xconfigure.height;
1729 } while( !b_map_notify );
1731 /* key and mouse events handling depending on --vout-event option
1733 * value = 1 (Fullsupport) (default value)
1734 * or value = 2 (Fullscreen-Only) and condition met
1736 bool b_vout_event = ( ( p_vout->p_sys->i_vout_event == 1 )
1737 || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1740 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1741 StructureNotifyMask | KeyPressMask |
1742 ButtonPressMask | ButtonReleaseMask |
1743 PointerMotionMask );
1745 #ifdef MODULE_NAME_IS_x11
1746 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1748 /* Allocate a new palette */
1749 p_vout->p_sys->colormap =
1750 XCreateColormap( p_vout->p_sys->p_display,
1751 DefaultRootWindow( p_vout->p_sys->p_display ),
1752 DefaultVisual( p_vout->p_sys->p_display,
1753 p_vout->p_sys->i_screen ),
1756 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1757 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1758 CWColormap, &xwindow_attributes );
1762 /* Create video output sub-window. */
1763 p_win->video_window = XCreateSimpleWindow(
1764 p_vout->p_sys->p_display,
1765 p_win->base_window, 0, 0,
1766 p_win->i_width, p_win->i_height,
1768 BlackPixel( p_vout->p_sys->p_display,
1769 p_vout->p_sys->i_screen ),
1770 WhitePixel( p_vout->p_sys->p_display,
1771 p_vout->p_sys->i_screen ) );
1773 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1774 BlackPixel( p_vout->p_sys->p_display,
1775 p_vout->p_sys->i_screen ) );
1777 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1778 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1781 /* make sure the video window will be centered in the next ManageVideo() */
1782 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1784 /* If the cursor was formerly blank than blank it again */
1785 if( !p_vout->p_sys->b_mouse_pointer_visible )
1787 ToggleCursor( p_vout );
1788 ToggleCursor( p_vout );
1791 /* Do NOT use XFlush here ! */
1792 XSync( p_vout->p_sys->p_display, False );
1794 /* At this stage, the window is open, displayed, and ready to
1796 p_vout->p_sys->p_win = p_win;
1801 /*****************************************************************************
1802 * DestroyWindow: destroy the window
1803 *****************************************************************************
1805 *****************************************************************************/
1806 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1808 /* Do NOT use XFlush here ! */
1809 XSync( p_vout->p_sys->p_display, False );
1811 if( p_win->video_window != None )
1812 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1814 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1816 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1817 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1819 /* make sure base window is destroyed before proceeding further */
1820 bool b_destroy_notify = false;
1824 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1825 SubstructureNotifyMask | StructureNotifyMask, &xevent);
1826 if( (xevent.type == DestroyNotify)
1827 && (xevent.xmap.window == p_win->base_window) )
1829 b_destroy_notify = true;
1831 } while( !b_destroy_notify );
1833 vout_ReleaseWindow( p_win->owner_window );
1836 /*****************************************************************************
1837 * NewPicture: allocate a picture
1838 *****************************************************************************
1839 * Returns 0 on success, -1 otherwise
1840 *****************************************************************************/
1841 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1843 #ifndef MODULE_NAME_IS_glx
1845 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1849 /* We know the chroma, allocate a buffer which will be used
1850 * directly by the decoder */
1851 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1853 if( p_pic->p_sys == NULL )
1858 #ifdef MODULE_NAME_IS_xvmc
1859 p_pic->p_sys->p_vout = p_vout;
1860 p_pic->p_sys->xvmc_surf = NULL;
1861 p_pic->p_sys->xxmc_data.decoded = 0;
1862 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1863 // p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1864 p_pic->p_sys->nb_display = 0;
1867 /* Fill in picture_t fields */
1868 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1869 p_vout->output.i_width, p_vout->output.i_height,
1870 p_vout->output.i_aspect );
1872 #ifdef HAVE_SYS_SHM_H
1873 if( p_vout->p_sys->i_shm_opcode )
1875 /* Create image using XShm extension */
1876 p_pic->p_sys->p_image =
1877 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1878 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1879 p_vout->p_sys->i_xvport,
1880 VLC2X11_FOURCC(p_vout->output.i_chroma),
1882 p_vout->p_sys->p_visual,
1883 p_vout->p_sys->i_screen_depth,
1885 &p_pic->p_sys->shminfo,
1886 p_vout->output.i_width, p_vout->output.i_height );
1889 if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1890 #endif /* HAVE_SYS_SHM_H */
1892 /* Create image without XShm extension */
1893 p_pic->p_sys->p_image =
1894 CreateImage( p_vout, p_vout->p_sys->p_display,
1895 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1896 p_vout->p_sys->i_xvport,
1897 VLC2X11_FOURCC(p_vout->output.i_chroma),
1898 p_pic->format.i_bits_per_pixel,
1900 p_vout->p_sys->p_visual,
1901 p_vout->p_sys->i_screen_depth,
1902 p_vout->p_sys->i_bytes_per_pixel,
1904 p_vout->output.i_width, p_vout->output.i_height );
1906 #ifdef HAVE_SYS_SHM_H
1907 if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1909 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1910 p_vout->p_sys->i_shm_opcode = 0;
1912 #endif /* HAVE_SYS_SHM_H */
1915 if( p_pic->p_sys->p_image == NULL )
1917 free( p_pic->p_sys );
1921 switch( p_vout->output.i_chroma )
1923 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1924 case VLC_CODEC_I420:
1925 case VLC_CODEC_YV12:
1926 case VLC_CODEC_Y211:
1927 case VLC_CODEC_YUYV:
1928 case VLC_CODEC_UYVY:
1929 case VLC_CODEC_RGB15:
1930 case VLC_CODEC_RGB16:
1931 case VLC_CODEC_RGB24: /* Fixme: pixel pitch == 4 ? */
1932 case VLC_CODEC_RGB32:
1934 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1937 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1938 + p_pic->p_sys->p_image->offsets[i_plane];
1939 p_pic->p[i_plane].i_pitch =
1940 p_pic->p_sys->p_image->pitches[i_plane];
1942 if( p_vout->output.i_chroma == VLC_CODEC_YV12 )
1944 /* U and V inverted compared to I420
1945 * Fixme: this should be handled by the vout core */
1946 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1947 + p_pic->p_sys->p_image->offsets[2];
1948 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1949 + p_pic->p_sys->p_image->offsets[1];
1955 case VLC_CODEC_RGB8:
1956 case VLC_CODEC_RGB16:
1957 case VLC_CODEC_RGB15:
1958 case VLC_CODEC_RGB24:
1959 case VLC_CODEC_RGB32:
1961 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1962 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1963 p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1964 + p_pic->p_sys->p_image->xoffset;
1965 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1967 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1968 * properly by vout_InitPicture() */
1969 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1970 * p_pic->p_sys->p_image->width;
1975 /* Unknown chroma, tell the guy to get lost */
1976 IMAGE_FREE( p_pic->p_sys->p_image );
1977 free( p_pic->p_sys );
1978 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1979 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1980 p_pic->i_planes = 0;
1985 VLC_UNUSED(p_vout); VLC_UNUSED(p_pic);
1987 #endif /* !MODULE_NAME_IS_glx */
1992 /*****************************************************************************
1993 * FreePicture: destroy a picture allocated with NewPicture
1994 *****************************************************************************
1995 * Destroy XImage AND associated data. If using Shm, detach shared memory
1996 * segment from server and process, then free it. The XDestroyImage manpage
1997 * says that both the image structure _and_ the data pointed to by the
1998 * image structure are freed, so no need to free p_image->data.
1999 *****************************************************************************/
2000 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2002 /* The order of operations is correct */
2003 #ifdef HAVE_SYS_SHM_H
2004 if( p_vout->p_sys->i_shm_opcode )
2006 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2007 IMAGE_FREE( p_pic->p_sys->p_image );
2009 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2010 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2012 msg_Err( p_vout, "cannot detach shared memory (%m)" );
2018 IMAGE_FREE( p_pic->p_sys->p_image );
2021 #ifdef MODULE_NAME_IS_xvmc
2022 if( p_pic->p_sys->xvmc_surf != NULL )
2024 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2025 p_pic->p_sys->xvmc_surf = NULL;
2029 /* Do NOT use XFlush here ! */
2030 XSync( p_vout->p_sys->p_display, False );
2032 free( p_pic->p_sys );
2035 /*****************************************************************************
2036 * ToggleFullScreen: Enable or disable full screen mode
2037 *****************************************************************************
2038 * This function will switch between fullscreen and window mode.
2039 *****************************************************************************/
2040 static void ToggleFullScreen ( vout_thread_t *p_vout )
2043 mwmhints_t mwmhints;
2044 XSetWindowAttributes attributes;
2046 #ifdef HAVE_XINERAMA
2050 p_vout->b_fullscreen = !p_vout->b_fullscreen;
2052 if( p_vout->b_fullscreen )
2054 msg_Dbg( p_vout, "entering fullscreen mode" );
2056 /* Getting current window position */
2058 Window* child_windows;
2059 unsigned int num_child_windows;
2062 XWindowAttributes win_attr;
2063 int screen_x,screen_y;
2065 XGetWindowAttributes(
2066 p_vout->p_sys->p_display,
2067 p_vout->p_sys->p_win->video_window,
2071 p_vout->p_sys->p_display,
2072 p_vout->p_sys->p_win->video_window,
2076 &num_child_windows);
2077 XFree(child_windows);
2079 XTranslateCoordinates(
2080 p_vout->p_sys->p_display,
2081 parent_win, win_attr.root,
2082 win_attr.x,win_attr.y,
2083 &screen_x,&screen_y,
2086 msg_Dbg( p_vout, "X %d/%d Y %d/%d", win_attr.x,screen_x,win_attr.y,screen_y);
2087 /* screen_x and screen_y are current position */
2089 p_vout->p_sys->b_altfullscreen =
2090 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2092 XUnmapWindow( p_vout->p_sys->p_display,
2093 p_vout->p_sys->p_win->base_window );
2095 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2097 CreateWindow( p_vout, p_vout->p_sys->p_win );
2098 XDestroyWindow( p_vout->p_sys->p_display,
2099 p_vout->p_sys->fullscreen_window.video_window );
2100 XReparentWindow( p_vout->p_sys->p_display,
2101 p_vout->p_sys->original_window.video_window,
2102 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2103 p_vout->p_sys->fullscreen_window.video_window =
2104 p_vout->p_sys->original_window.video_window;
2106 /* To my knowledge there are two ways to create a borderless window.
2107 * There's the generic way which is to tell x to bypass the window
2108 * manager, but this creates problems with the focus of other
2110 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2111 * luckily seems to be supported by most window managers. */
2112 if( !p_vout->p_sys->b_altfullscreen )
2114 mwmhints.flags = MWM_HINTS_DECORATIONS;
2115 mwmhints.decorations = False;
2117 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2119 XChangeProperty( p_vout->p_sys->p_display,
2120 p_vout->p_sys->p_win->base_window,
2121 prop, prop, 32, PropModeReplace,
2122 (unsigned char *)&mwmhints,
2123 PROP_MWM_HINTS_ELEMENTS );
2127 /* brute force way to remove decorations */
2128 attributes.override_redirect = True;
2129 XChangeWindowAttributes( p_vout->p_sys->p_display,
2130 p_vout->p_sys->p_win->base_window,
2134 /* Make sure the change is effective */
2135 XReparentWindow( p_vout->p_sys->p_display,
2136 p_vout->p_sys->p_win->base_window,
2137 DefaultRootWindow( p_vout->p_sys->p_display ),
2141 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2143 XClientMessageEvent event;
2145 memset( &event, 0, sizeof( XClientMessageEvent ) );
2147 event.type = ClientMessage;
2148 event.message_type = p_vout->p_sys->net_wm_state;
2149 event.display = p_vout->p_sys->p_display;
2150 event.window = p_vout->p_sys->p_win->base_window;
2152 event.data.l[ 0 ] = 1; /* set property */
2153 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2155 XSendEvent( p_vout->p_sys->p_display,
2156 DefaultRootWindow( p_vout->p_sys->p_display ),
2157 False, SubstructureRedirectMask,
2161 /* "bad fullscreen" - set this to 0. doing fullscreen this way is problematic
2162 * for many reasons and basically fights with the window manager as the wm
2163 * reparents AND vlc goes and reparents - multiple times. don't do it. it just
2164 * makes it more inefficient and less "nice" to the x11 citizenry. this turns
2167 /* explicitly asking for focus when you fullscreened is a little silly. the
2168 * window manager SHOULD be handling this itself based on its own focus
2169 * policies. if the user is "using" a given xinerama/xrandr screen or x11
2170 * multihead screen AND vlc wants to fullscreen the wm should also focus it
2171 * as its the only thing on the screen. if vlc fullscreens and its on
2172 * "another monitor" to the one the user is using - this may "steal" the focus
2173 * as really the wm should be deciding if, on fullscreening of a window
2174 * the focus should go there or not, so let the wm decided */
2176 /* Make sure the change is effective */
2177 XReparentWindow( p_vout->p_sys->p_display,
2178 p_vout->p_sys->p_win->base_window,
2179 DefaultRootWindow( p_vout->p_sys->p_display ),
2182 #ifdef HAVE_XINERAMA
2183 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2184 XineramaIsActive( p_vout->p_sys->p_display ) )
2186 XineramaScreenInfo *screens; /* infos for xinerama */
2189 msg_Dbg( p_vout, "using XFree Xinerama extension");
2191 #define SCREEN p_vout->p_sys->p_win->i_screen
2193 /* Get Information about Xinerama (num of screens) */
2194 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2197 SCREEN = config_GetInt( p_vout,
2198 MODULE_STRING "-xineramascreen" );
2200 /* just check that user has entered a good value,
2201 * otherwise use that screen where window is */
2202 if( SCREEN >= i_num_screens || SCREEN < 0 )
2204 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2205 #define left screens[SCREEN].x_org
2206 #define right left + screens[SCREEN].width
2207 #define top screens[SCREEN].y_org
2208 #define bottom top + screens[SCREEN].height
2210 for( SCREEN = i_num_screens-1; SCREEN > 0; SCREEN--)
2212 if( left <= screen_x && screen_x <= right &&
2213 top <= screen_y && screen_y <= bottom )
2222 /* Get the X/Y upper left corner coordinate of the above screen */
2223 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2224 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2226 /* Set the Height/width to the screen resolution */
2227 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2228 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2238 /* The window wasn't necessarily created at the requested size */
2239 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2241 #ifdef HAVE_XF86VIDMODE
2242 XF86VidModeModeLine mode;
2245 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2246 p_vout->p_sys->i_screen, &i_dummy,
2249 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2250 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2252 /* move cursor to the middle of the window to prevent
2253 * unwanted display move if the display is smaller than the
2255 XWarpPointer( p_vout->p_sys->p_display, None,
2256 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2257 mode.hdisplay / 2 , mode.vdisplay / 2 );
2258 /* force desktop view to upper left corner */
2259 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2260 p_vout->p_sys->i_screen, 0, 0 );
2265 p_vout->p_sys->p_win->i_width =
2266 DisplayWidth( p_vout->p_sys->p_display,
2267 p_vout->p_sys->i_screen );
2268 p_vout->p_sys->p_win->i_height =
2269 DisplayHeight( p_vout->p_sys->p_display,
2270 p_vout->p_sys->i_screen );
2275 XMoveResizeWindow( p_vout->p_sys->p_display,
2276 p_vout->p_sys->p_win->base_window,
2277 p_vout->p_sys->p_win->i_x,
2278 p_vout->p_sys->p_win->i_y,
2279 p_vout->p_sys->p_win->i_width,
2280 p_vout->p_sys->p_win->i_height );
2283 EnablePixelDoubling( p_vout );
2286 #if APPFOCUS // RASTER: let the wm do focus policy
2287 /* Activate the window (give it the focus) */
2288 XClientMessageEvent event;
2290 memset( &event, 0, sizeof( XClientMessageEvent ) );
2292 event.type = ClientMessage;
2293 event.message_type =
2294 XInternAtom( p_vout->p_sys->p_display, "_NET_ACTIVE_WINDOW", False );
2295 event.display = p_vout->p_sys->p_display;
2296 event.window = p_vout->p_sys->p_win->base_window;
2298 event.data.l[ 0 ] = 1; /* source indication (1 = from an application */
2299 event.data.l[ 1 ] = 0; /* timestamp */
2300 event.data.l[ 2 ] = 0; /* requestor's currently active window */
2301 /* XXX: window manager would be more likely to obey if we already have
2302 * an active window (and give it to the event), such as an interface */
2304 XSendEvent( p_vout->p_sys->p_display,
2305 DefaultRootWindow( p_vout->p_sys->p_display ),
2306 False, SubstructureRedirectMask,
2312 msg_Dbg( p_vout, "leaving fullscreen mode" );
2315 DisablePixelDoubling( p_vout );
2318 XReparentWindow( p_vout->p_sys->p_display,
2319 p_vout->p_sys->original_window.video_window,
2320 p_vout->p_sys->original_window.base_window, 0, 0 );
2322 p_vout->p_sys->fullscreen_window.video_window = None;
2323 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2324 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2326 XMapWindow( p_vout->p_sys->p_display,
2327 p_vout->p_sys->p_win->base_window );
2330 /* Unfortunately, using XSync() here is not enough to ensure the
2331 * window has already been mapped because the XMapWindow() request
2332 * has not necessarily been sent directly to our window (remember,
2333 * the call is first redirected to the window manager) */
2335 #if BADFS // RASTER: this is silly... if we have already mapped before
2339 XWindowEvent( p_vout->p_sys->p_display,
2340 p_vout->p_sys->p_win->base_window,
2341 StructureNotifyMask, &xevent );
2342 } while( xevent.type != MapNotify );
2344 XSync(p_vout->p_sys->p_display, False);
2347 /* Be careful, this can generate a BadMatch error if the window is not
2348 * already mapped by the server (see above) */
2349 XSetInputFocus(p_vout->p_sys->p_display,
2350 p_vout->p_sys->p_win->base_window,
2354 /* signal that the size needs to be updated */
2355 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2358 /*****************************************************************************
2359 * EnableXScreenSaver: enable screen saver
2360 *****************************************************************************
2361 * This function enables the screen saver on a display after it has been
2362 * disabled by XDisableScreenSaver.
2363 * FIXME: what happens if multiple vlc sessions are running at the same
2365 *****************************************************************************/
2366 static void EnableXScreenSaver( vout_thread_t *p_vout )
2368 #ifdef DPMSINFO_IN_DPMS_H
2372 if( p_vout->p_sys->i_ss_timeout )
2374 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2375 p_vout->p_sys->i_ss_interval,
2376 p_vout->p_sys->i_ss_blanking,
2377 p_vout->p_sys->i_ss_exposure );
2380 /* Restore DPMS settings */
2381 #ifdef DPMSINFO_IN_DPMS_H
2382 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2384 if( p_vout->p_sys->b_ss_dpms )
2386 DPMSEnable( p_vout->p_sys->p_display );
2392 /*****************************************************************************
2393 * DisableXScreenSaver: disable screen saver
2394 *****************************************************************************
2395 * See XEnableXScreenSaver
2396 *****************************************************************************/
2397 static void DisableXScreenSaver( vout_thread_t *p_vout )
2399 #ifdef DPMSINFO_IN_DPMS_H
2403 /* Save screen saver information */
2404 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2405 &p_vout->p_sys->i_ss_interval,
2406 &p_vout->p_sys->i_ss_blanking,
2407 &p_vout->p_sys->i_ss_exposure );
2409 /* Disable screen saver */
2410 if( p_vout->p_sys->i_ss_timeout )
2412 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2413 p_vout->p_sys->i_ss_interval,
2414 p_vout->p_sys->i_ss_blanking,
2415 p_vout->p_sys->i_ss_exposure );
2419 #ifdef DPMSINFO_IN_DPMS_H
2420 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2423 /* Save DPMS current state */
2424 DPMSInfo( p_vout->p_sys->p_display, &unused,
2425 &p_vout->p_sys->b_ss_dpms );
2426 DPMSDisable( p_vout->p_sys->p_display );
2431 /*****************************************************************************
2432 * CreateCursor: create a blank mouse pointer
2433 *****************************************************************************/
2434 static void CreateCursor( vout_thread_t *p_vout )
2436 XColor cursor_color;
2438 p_vout->p_sys->cursor_pixmap =
2439 XCreatePixmap( p_vout->p_sys->p_display,
2440 DefaultRootWindow( p_vout->p_sys->p_display ),
2443 XParseColor( p_vout->p_sys->p_display,
2444 XCreateColormap( p_vout->p_sys->p_display,
2446 p_vout->p_sys->p_display ),
2448 p_vout->p_sys->p_display,
2449 p_vout->p_sys->i_screen ),
2451 "black", &cursor_color );
2453 p_vout->p_sys->blank_cursor =
2454 XCreatePixmapCursor( p_vout->p_sys->p_display,
2455 p_vout->p_sys->cursor_pixmap,
2456 p_vout->p_sys->cursor_pixmap,
2457 &cursor_color, &cursor_color, 1, 1 );
2460 /*****************************************************************************
2461 * DestroyCursor: destroy the blank mouse pointer
2462 *****************************************************************************/
2463 static void DestroyCursor( vout_thread_t *p_vout )
2465 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2468 /*****************************************************************************
2469 * ToggleCursor: hide or show the mouse pointer
2470 *****************************************************************************
2471 * This function hides the X pointer if it is visible by setting the pointer
2472 * sprite to a blank one. To show it again, we disable the sprite.
2473 *****************************************************************************/
2474 static void ToggleCursor( vout_thread_t *p_vout )
2476 if( p_vout->p_sys->b_mouse_pointer_visible )
2478 XDefineCursor( p_vout->p_sys->p_display,
2479 p_vout->p_sys->p_win->base_window,
2480 p_vout->p_sys->blank_cursor );
2481 p_vout->p_sys->b_mouse_pointer_visible = 0;
2485 XUndefineCursor( p_vout->p_sys->p_display,
2486 p_vout->p_sys->p_win->base_window );
2487 p_vout->p_sys->b_mouse_pointer_visible = 1;
2491 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2492 /*****************************************************************************
2493 * XVideoGetPort: get YUV12 port
2494 *****************************************************************************/
2495 static int XVideoGetPort( vout_thread_t *p_vout,
2496 vlc_fourcc_t i_chroma, picture_heap_t *p_heap )
2498 XvAdaptorInfo *p_adaptor;
2500 unsigned int i_adaptor, i_num_adaptors;
2501 int i_requested_adaptor;
2502 int i_selected_port;
2504 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2509 case XvBadExtension:
2510 msg_Warn( p_vout, "XvBadExtension" );
2514 msg_Warn( p_vout, "XvBadAlloc" );
2518 msg_Warn( p_vout, "XvQueryExtension failed" );
2522 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2523 DefaultRootWindow( p_vout->p_sys->p_display ),
2524 &i_num_adaptors, &p_adaptor ) )
2529 case XvBadExtension:
2530 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2534 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2538 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2542 i_selected_port = -1;
2543 #ifdef MODULE_NAME_IS_xvmc
2544 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2546 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2548 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2550 XvImageFormatValues *p_formats;
2551 int i_format, i_num_formats;
2554 /* If we requested an adaptor and it's not this one, we aren't
2556 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2561 /* If the adaptor doesn't have the required properties, skip it */
2562 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2563 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2568 /* Check that adaptor supports our requested format... */
2569 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2570 p_adaptor[i_adaptor].base_id,
2574 i_format < i_num_formats && ( i_selected_port == -1 );
2577 XvAttribute *p_attr;
2578 int i_attr, i_num_attributes;
2579 Atom autopaint = None, colorkey = None;
2581 /* If this is not the format we want, or at least a
2582 * similar one, forget it */
2583 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2588 /* Look for the first available port supporting this format */
2589 for( i_port = p_adaptor[i_adaptor].base_id;
2590 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2591 + p_adaptor[i_adaptor].num_ports) )
2592 && ( i_selected_port == -1 );
2595 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2598 i_selected_port = i_port;
2599 p_heap->i_chroma = p_formats[ i_format ].id;
2600 #if XvVersion > 2 || ( XvVersion == 2 && XvRevision >= 2 )
2601 p_heap->i_rmask = p_formats[ i_format ].red_mask;
2602 p_heap->i_gmask = p_formats[ i_format ].green_mask;
2603 p_heap->i_bmask = p_formats[ i_format ].blue_mask;
2608 /* If no free port was found, forget it */
2609 if( i_selected_port == -1 )
2614 /* If we found a port, print information about it */
2615 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2616 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2617 (char *)&p_formats[ i_format ].id,
2618 ( p_formats[ i_format ].format == XvPacked ) ?
2619 "packed" : "planar" );
2621 /* Use XV_AUTOPAINT_COLORKEY if supported, otherwise we will
2622 * manually paint the colour key */
2623 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2625 &i_num_attributes );
2627 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2629 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2631 autopaint = XInternAtom( p_vout->p_sys->p_display,
2632 "XV_AUTOPAINT_COLORKEY", False );
2633 XvSetPortAttribute( p_vout->p_sys->p_display,
2634 i_selected_port, autopaint, 1 );
2636 if( !strcmp( p_attr[i_attr].name, "XV_COLORKEY" ) )
2638 /* Find out the default colour key */
2639 colorkey = XInternAtom( p_vout->p_sys->p_display,
2640 "XV_COLORKEY", False );
2641 XvGetPortAttribute( p_vout->p_sys->p_display,
2642 i_selected_port, colorkey,
2643 &p_vout->p_sys->i_colourkey );
2646 p_vout->p_sys->b_paint_colourkey =
2647 autopaint == None && colorkey != None;
2649 if( p_attr != NULL )
2655 if( p_formats != NULL )
2662 if( i_num_adaptors > 0 )
2664 XvFreeAdaptorInfo( p_adaptor );
2667 if( i_selected_port == -1 )
2669 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2670 if( i_requested_adaptor == -1 )
2672 msg_Warn( p_vout, "no free XVideo port found for format "
2673 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2677 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2678 "XVideo port for format 0x%.8x (%4.4s)",
2679 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2683 return i_selected_port;
2686 /*****************************************************************************
2687 * XVideoReleasePort: release YUV12 port
2688 *****************************************************************************/
2689 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2691 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2695 /*****************************************************************************
2696 * InitDisplay: open and initialize X11 device
2697 *****************************************************************************
2698 * Create a window according to video output given size, and set other
2699 * properties according to the display properties.
2700 *****************************************************************************/
2701 static int InitDisplay( vout_thread_t *p_vout )
2703 #ifdef MODULE_NAME_IS_x11
2704 XPixmapFormatValues * p_formats; /* pixmap formats */
2705 XVisualInfo * p_xvisual; /* visuals information */
2706 XVisualInfo xvisual_template; /* visual template */
2707 int i_count, i; /* array size */
2710 #ifdef HAVE_SYS_SHM_H
2711 p_vout->p_sys->i_shm_opcode = 0;
2713 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) > 0 )
2715 int major, evt, err;
2717 if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2719 && XShmQueryExtension( p_vout->p_sys->p_display ) )
2720 p_vout->p_sys->i_shm_opcode = major;
2722 if( p_vout->p_sys->i_shm_opcode )
2727 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2729 msg_Dbg( p_vout, "XShm video extension v%d.%d "
2730 "(with%s pixmaps, opcode: %d)",
2731 major, minor, pixmaps ? "" : "out",
2732 p_vout->p_sys->i_shm_opcode );
2735 msg_Warn( p_vout, "XShm video extension not available" );
2738 msg_Dbg( p_vout, "XShm video extension disabled" );
2741 #ifdef MODULE_NAME_IS_xvideo
2742 /* XXX The brightness and contrast values should be read from environment
2743 * XXX variables... */
2745 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2746 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2750 #ifdef MODULE_NAME_IS_x11
2751 /* Initialize structure */
2752 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2754 /* Get screen depth */
2755 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2756 p_vout->p_sys->i_screen );
2757 switch( p_vout->p_sys->i_screen_depth )
2761 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2763 xvisual_template.screen = p_vout->p_sys->i_screen;
2764 xvisual_template.class = DirectColor;
2765 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2766 VisualScreenMask | VisualClassMask,
2767 &xvisual_template, &i_count );
2768 if( p_xvisual == NULL )
2770 msg_Err( p_vout, "no PseudoColor visual available" );
2771 return VLC_EGENERIC;
2773 p_vout->p_sys->i_bytes_per_pixel = 1;
2774 p_vout->output.pf_setpalette = SetPalette;
2781 * Screen depth is higher than 8bpp. TrueColor visual is used.
2783 xvisual_template.screen = p_vout->p_sys->i_screen;
2784 xvisual_template.class = TrueColor;
2785 /* In some cases, we get a truecolor class adaptor that has a different
2786 color depth. So try to get a real true color one first */
2787 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2789 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2790 VisualScreenMask | VisualClassMask |
2792 &xvisual_template, &i_count );
2793 if( p_xvisual == NULL )
2795 msg_Warn( p_vout, "No screen matching the required color depth" );
2796 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2797 VisualScreenMask | VisualClassMask,
2798 &xvisual_template, &i_count );
2799 if( p_xvisual == NULL )
2802 msg_Err( p_vout, "no TrueColor visual available" );
2803 return VLC_EGENERIC;
2807 p_vout->output.i_rmask = p_xvisual->red_mask;
2808 p_vout->output.i_gmask = p_xvisual->green_mask;
2809 p_vout->output.i_bmask = p_xvisual->blue_mask;
2811 /* There is no difference yet between 3 and 4 Bpp. The only way
2812 * to find the actual number of bytes per pixel is to list supported
2813 * pixmap formats. */
2814 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2815 p_vout->p_sys->i_bytes_per_pixel = 0;
2817 for( i = 0; i < i_count; i++ )
2819 /* Under XFree4.0, the list contains pixmap formats available
2820 * through all video depths ; so we have to check against current
2822 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2824 if( p_formats[i].bits_per_pixel / 8
2825 > (int)p_vout->p_sys->i_bytes_per_pixel )
2827 p_vout->p_sys->i_bytes_per_pixel =
2828 p_formats[i].bits_per_pixel / 8;
2832 if( p_formats ) XFree( p_formats );
2836 p_vout->p_sys->p_visual = p_xvisual->visual;
2843 #ifndef MODULE_NAME_IS_glx
2845 #ifdef HAVE_SYS_SHM_H
2846 /*****************************************************************************
2847 * CreateShmImage: create an XImage or XvImage using shared memory extension
2848 *****************************************************************************
2849 * Prepare an XImage or XvImage for display function.
2850 * The order of the operations respects the recommandations of the mit-shm
2851 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2852 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2853 *****************************************************************************/
2854 IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2855 Display* p_display, EXTRA_ARGS_SHM,
2856 int i_width, int i_height )
2858 IMAGE_TYPE *p_image;
2861 /* Create XImage / XvImage */
2862 #ifdef MODULE_NAME_IS_xvideo
2863 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2864 i_width, i_height, p_shm );
2865 #elif defined(MODULE_NAME_IS_xvmc)
2866 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2867 i_width, i_height, p_shm );
2869 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2870 p_shm, i_width, i_height );
2872 if( p_image == NULL )
2874 msg_Err( p_vout, "image creation failed" );
2878 /* For too big image, the buffer returned is sometimes too small, prevent
2879 * VLC to segfault because of it
2880 * FIXME is it normal ? Is there a way to detect it
2881 * before (XvQueryBestSize did not) ? */
2882 if( p_image->width < i_width || p_image->height < i_height )
2884 msg_Err( p_vout, "cannot allocate shared image data with the right size "
2885 "(%dx%d instead of %dx%d)",
2886 p_image->width, p_image->height,
2887 i_width, i_height );
2888 IMAGE_FREE( p_image );
2892 /* Allocate shared memory segment - 0776 set the access permission
2893 * rights (like umask), they are not yet supported by all X servers */
2894 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2895 if( p_shm->shmid < 0 )
2897 msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2898 IMAGE_FREE( p_image );
2902 /* Attach shared memory segment to process (read/write) */
2903 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2904 if(! p_shm->shmaddr )
2906 msg_Err( p_vout, "cannot attach shared memory (%m)" );
2907 IMAGE_FREE( p_image );
2908 shmctl( p_shm->shmid, IPC_RMID, 0 );
2912 /* Read-only data. We won't be using XShmGetImage */
2913 p_shm->readOnly = True;
2915 /* Attach shared memory segment to X server */
2916 XSynchronize( p_display, True );
2917 i_shm_major = p_vout->p_sys->i_shm_opcode;
2918 result = XShmAttach( p_display, p_shm );
2919 if( result == False || !i_shm_major )
2921 msg_Err( p_vout, "cannot attach shared memory to X server" );
2922 IMAGE_FREE( p_image );
2923 shmctl( p_shm->shmid, IPC_RMID, 0 );
2924 shmdt( p_shm->shmaddr );
2927 XSynchronize( p_display, False );
2929 /* Send image to X server. This instruction is required, since having
2930 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2931 * and remember NOT to use XFlush ! */
2932 XSync( p_display, False );
2935 /* Mark the shm segment to be removed when there are no more
2936 * attachements, so it is automatic on process exit or after shmdt */
2937 shmctl( p_shm->shmid, IPC_RMID, 0 );
2944 /*****************************************************************************
2945 * CreateImage: create an XImage or XvImage
2946 *****************************************************************************
2947 * Create a simple image used as a buffer.
2948 *****************************************************************************/
2949 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2950 Display *p_display, EXTRA_ARGS,
2951 int i_width, int i_height )
2953 uint8_t * p_data; /* image data storage zone */
2954 IMAGE_TYPE *p_image;
2955 #ifdef MODULE_NAME_IS_x11
2956 int i_quantum; /* XImage quantum (see below) */
2957 int i_bytes_per_line;
2960 /* Allocate memory for image */
2961 #ifdef MODULE_NAME_IS_xvideo
2962 p_data = malloc( i_width * i_height * i_bits_per_pixel / 8 );
2963 #elif defined(MODULE_NAME_IS_x11)
2964 i_bytes_per_line = i_width * i_bytes_per_pixel;
2965 p_data = malloc( i_bytes_per_line * i_height );
2970 #ifdef MODULE_NAME_IS_x11
2971 /* Optimize the quantum of a scanline regarding its size - the quantum is
2972 a diviser of the number of bits between the start of two scanlines. */
2973 if( i_bytes_per_line & 0xf )
2977 else if( i_bytes_per_line & 0x10 )
2987 /* Create XImage. p_data will be automatically freed */
2988 #ifdef MODULE_NAME_IS_xvideo
2989 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2990 (char *)p_data, i_width, i_height );
2991 #elif defined(MODULE_NAME_IS_x11)
2992 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2993 (char *)p_data, i_width, i_height, i_quantum, 0 );
2995 if( p_image == NULL )
2997 msg_Err( p_vout, "XCreateImage() failed" );
3006 /*****************************************************************************
3007 * X11ErrorHandler: replace error handler so we can intercept some of them
3008 *****************************************************************************/
3009 static int X11ErrorHandler( Display * display, XErrorEvent * event )
3013 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
3015 "[????????] x11 video output error: X11 request %u.%u failed "
3016 "with error code %u:\n %s\n",
3017 event->request_code, event->minor_code, event->error_code, txt );
3019 switch( event->request_code )
3021 case X_SetInputFocus:
3022 /* Ignore errors on XSetInputFocus()
3023 * (they happen when a window is not yet mapped) */
3027 #ifdef HAVE_SYS_SHM_H
3028 if( event->request_code == i_shm_major ) /* MIT-SHM */
3031 "[????????] x11 video output notice:"
3032 " buggy X11 server claims shared memory\n"
3033 "[????????] x11 video output notice:"
3034 " support though it does not work (OpenSSH?)\n" );
3035 return i_shm_major = 0;
3040 XSetErrorHandler(NULL);
3041 return (XSetErrorHandler(X11ErrorHandler))( display, event );
3043 /* Work-around Maemo Xvideo bug */
3048 #ifdef MODULE_NAME_IS_x11
3049 /*****************************************************************************
3050 * SetPalette: sets an 8 bpp palette
3051 *****************************************************************************
3052 * This function sets the palette given as an argument. It does not return
3053 * anything, but could later send information on which colors it was unable
3055 *****************************************************************************/
3056 static void SetPalette( vout_thread_t *p_vout,
3057 uint16_t *red, uint16_t *green, uint16_t *blue )
3060 XColor p_colors[255];
3062 /* allocate palette */
3063 for( i = 0; i < 255; i++ )
3065 /* kludge: colors are indexed reversely because color 255 seems
3066 * to be reserved for black even if we try to set it to white */
3067 p_colors[ i ].pixel = 255 - i;
3068 p_colors[ i ].pad = 0;
3069 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
3070 p_colors[ i ].red = red[ 255 - i ];
3071 p_colors[ i ].blue = blue[ 255 - i ];
3072 p_colors[ i ].green = green[ 255 - i ];
3075 XStoreColors( p_vout->p_sys->p_display,
3076 p_vout->p_sys->colormap, p_colors, 255 );
3080 /*****************************************************************************
3081 * Control: control facility for the vout
3082 *****************************************************************************/
3083 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
3086 unsigned int i_width, i_height;
3091 if( p_vout->p_sys->p_win->owner_window )
3092 return vout_ControlWindow( p_vout->p_sys->p_win->owner_window,
3095 i_width = va_arg( args, unsigned int );
3096 i_height = va_arg( args, unsigned int );
3097 if( !i_width ) i_width = p_vout->i_window_width;
3098 if( !i_height ) i_height = p_vout->i_window_height;
3100 #ifdef MODULE_NAME_IS_xvmc
3101 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3103 /* Update dimensions */
3104 XResizeWindow( p_vout->p_sys->p_display,
3105 p_vout->p_sys->p_win->base_window,
3106 i_width, i_height );
3107 #ifdef MODULE_NAME_IS_xvmc
3108 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3112 case VOUT_SET_STAY_ON_TOP:
3113 if( p_vout->p_sys->p_win->owner_window )
3114 return vout_ControlWindow( p_vout->p_sys->p_win->owner_window,
3117 b_arg = (bool) va_arg( args, int );
3118 #ifdef MODULE_NAME_IS_xvmc
3119 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3121 WindowOnTop( p_vout, b_arg );
3122 #ifdef MODULE_NAME_IS_xvmc
3123 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3128 return VLC_EGENERIC;
3132 /*****************************************************************************
3133 * TestNetWMSupport: tests for Extended Window Manager Hints support
3134 *****************************************************************************/
3135 static void TestNetWMSupport( vout_thread_t *p_vout )
3137 int i_ret, i_format;
3138 unsigned long i, i_items, i_bytesafter;
3139 Atom net_wm_supported;
3140 union { Atom *p_atom; unsigned char *p_char; } p_args;
3142 p_args.p_atom = NULL;
3144 p_vout->p_sys->b_net_wm_state_fullscreen =
3145 p_vout->p_sys->b_net_wm_state_above =
3146 p_vout->p_sys->b_net_wm_state_below =
3147 p_vout->p_sys->b_net_wm_state_stays_on_top =
3151 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3153 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3154 DefaultRootWindow( p_vout->p_sys->p_display ),
3156 0, 16384, False, AnyPropertyType,
3158 &i_format, &i_items, &i_bytesafter,
3159 (unsigned char **)&p_args );
3161 if( i_ret != Success || i_items == 0 ) return;
3163 msg_Dbg( p_vout, "Window manager supports NetWM" );
3165 p_vout->p_sys->net_wm_state =
3166 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3167 p_vout->p_sys->net_wm_state_fullscreen =
3168 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3170 p_vout->p_sys->net_wm_state_above =
3171 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3172 p_vout->p_sys->net_wm_state_below =
3173 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3174 p_vout->p_sys->net_wm_state_stays_on_top =
3175 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3178 for( i = 0; i < i_items; i++ )
3180 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3183 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3184 p_vout->p_sys->b_net_wm_state_fullscreen = true;
3186 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3188 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3189 p_vout->p_sys->b_net_wm_state_above = true;
3191 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3193 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3194 p_vout->p_sys->b_net_wm_state_below = true;
3196 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3199 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3200 p_vout->p_sys->b_net_wm_state_stays_on_top = true;
3204 XFree( p_args.p_atom );
3207 /*****************************************************************************
3208 * Key events handling
3209 *****************************************************************************/
3215 } x11keys_to_vlckeys[] =
3217 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3218 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3219 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3220 { XK_F12, KEY_F12 },
3222 { XK_Return, KEY_ENTER },
3223 { XK_KP_Enter, KEY_ENTER },
3224 { XK_space, KEY_SPACE },
3225 { XK_Escape, KEY_ESC },
3227 { XK_Menu, KEY_MENU },
3228 { XK_Left, KEY_LEFT },
3229 { XK_Right, KEY_RIGHT },
3231 { XK_Down, KEY_DOWN },
3233 { XK_Home, KEY_HOME },
3234 { XK_End, KEY_END },
3235 { XK_Page_Up, KEY_PAGEUP },
3236 { XK_Page_Down, KEY_PAGEDOWN },
3238 { XK_Insert, KEY_INSERT },
3239 { XK_Delete, KEY_DELETE },
3240 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3241 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3242 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3243 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3244 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3245 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3246 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3251 static int ConvertKey( int i_key )
3255 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3257 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3259 return x11keys_to_vlckeys[i].i_vlckey;
3266 /*****************************************************************************
3267 * WindowOnTop: Switches the "always on top" state of the video window.
3268 *****************************************************************************/
3269 static int WindowOnTop( vout_thread_t *p_vout, bool b_on_top )
3271 XClientMessageEvent event;
3273 memset( &event, 0, sizeof( XClientMessageEvent ) );
3274 event.type = ClientMessage;
3275 event.message_type = p_vout->p_sys->net_wm_state;
3276 event.display = p_vout->p_sys->p_display;
3277 event.window = p_vout->p_sys->p_win->base_window;
3279 event.data.l[ 0 ] = b_on_top; /* set property */
3281 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3282 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3283 else if( p_vout->p_sys->b_net_wm_state_above )
3284 /* use _NET_WM_STATE_ABOVE if window manager
3285 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3286 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3288 return VLC_EGENERIC;
3290 XSendEvent( p_vout->p_sys->p_display,
3291 DefaultRootWindow( p_vout->p_sys->p_display ),
3292 False, SubstructureRedirectMask,