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>
40 #include <errno.h> /* ENOMEM */
42 #ifdef HAVE_MACHINE_PARAM_H
44 # include <machine/param.h>
45 # include <sys/types.h> /* typedef ushort */
50 # include <netinet/in.h> /* BSD: struct in_addr */
54 #include <X11/extensions/Xsp.h>
58 # include <sys/shm.h> /* shmget(), shmctl() */
62 #include <X11/Xproto.h>
64 #include <X11/Xutil.h>
65 #include <X11/keysym.h>
66 #include <X11/XF86keysym.h>
68 # include <X11/extensions/XShm.h>
70 #ifdef DPMSINFO_IN_DPMS_H
71 # include <X11/extensions/dpms.h>
74 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
75 # include <X11/extensions/Xv.h>
76 # include <X11/extensions/Xvlib.h>
79 #ifdef MODULE_NAME_IS_glx
84 # include <X11/extensions/Xinerama.h>
87 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
88 # include <X11/extensions/xf86vmode.h>
91 #ifdef MODULE_NAME_IS_xvmc
92 # include <X11/extensions/vldXvMC.h>
93 # include "../../codec/xvmc/accel_xvmc.h"
98 /*****************************************************************************
100 *****************************************************************************/
101 int Activate ( vlc_object_t * );
102 void Deactivate ( vlc_object_t * );
104 static int InitVideo ( vout_thread_t * );
105 static void EndVideo ( vout_thread_t * );
106 static void DisplayVideo ( vout_thread_t *, picture_t * );
107 static int ManageVideo ( vout_thread_t * );
108 static int Control ( vout_thread_t *, int, va_list );
110 static int InitDisplay ( vout_thread_t * );
112 static int CreateWindow ( vout_thread_t *, x11_window_t * );
113 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
115 static int NewPicture ( vout_thread_t *, picture_t * );
116 static void FreePicture ( vout_thread_t *, picture_t * );
118 #ifndef MODULE_NAME_IS_glx
119 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
120 Display *, EXTRA_ARGS, int, int );
123 #ifdef HAVE_SYS_SHM_H
124 #ifndef MODULE_NAME_IS_glx
125 IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
126 Display *, EXTRA_ARGS_SHM, int, int );
128 static int i_shm_major = 0;
131 static void ToggleFullScreen ( vout_thread_t * );
133 static void EnableXScreenSaver ( vout_thread_t * );
134 static void DisableXScreenSaver ( vout_thread_t * );
136 static void CreateCursor ( vout_thread_t * );
137 static void DestroyCursor ( vout_thread_t * );
138 static void ToggleCursor ( vout_thread_t * );
140 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
141 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
142 static void XVideoReleasePort( vout_thread_t *, int );
145 #ifdef MODULE_NAME_IS_x11
146 static void SetPalette ( vout_thread_t *,
147 uint16_t *, uint16_t *, uint16_t * );
150 #ifdef MODULE_NAME_IS_xvmc
151 static void RenderVideo ( vout_thread_t *, picture_t * );
152 static int xvmc_check_yv12( Display *display, XvPortID port );
153 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
156 static void TestNetWMSupport( vout_thread_t * );
157 static int ConvertKey( int );
159 static int WindowOnTop( vout_thread_t *, bool );
161 static int X11ErrorHandler( Display *, XErrorEvent * );
164 static void EnablePixelDoubling( vout_thread_t *p_vout );
165 static void DisablePixelDoubling( vout_thread_t *p_vout );
169 static const int i_backlight_on_interval = 300;
174 /*****************************************************************************
175 * Activate: allocate X11 video thread output method
176 *****************************************************************************
177 * This function allocate and initialize a X11 vout method. It uses some of the
178 * vout properties to choose the window size, and change them according to the
179 * actual properties of the display.
180 *****************************************************************************/
181 int Activate ( vlc_object_t *p_this )
183 vout_thread_t *p_vout = (vout_thread_t *)p_this;
186 #if defined(MODULE_NAME_IS_xvmc)
189 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
191 vlc_fourcc_t i_chroma = 0;
195 p_vout->pf_init = InitVideo;
196 p_vout->pf_end = EndVideo;
197 p_vout->pf_manage = ManageVideo;
198 #ifdef MODULE_NAME_IS_xvmc
199 p_vout->pf_render = RenderVideo;
201 p_vout->pf_render = NULL;
203 p_vout->pf_display = DisplayVideo;
204 p_vout->pf_control = Control;
206 /* Allocate structure */
207 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
208 if( p_vout->p_sys == NULL )
211 vlc_mutex_init( &p_vout->p_sys->lock );
213 /* Open display, using the "display" config variable or the DISPLAY
214 * environment variable */
215 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
217 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
219 if( p_vout->p_sys->p_display == NULL ) /* error */
221 msg_Err( p_vout, "cannot open display %s",
222 XDisplayName( psz_display ) );
223 free( p_vout->p_sys );
229 /* Replace error handler so we can intercept some non-fatal errors */
230 XSetErrorHandler( X11ErrorHandler );
232 /* Get a screen ID matching the XOpenDisplay return value */
233 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
235 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
236 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
239 if( strlen( psz_chroma ) >= 4 )
241 /* Do not use direct assignment because we are not sure of the
243 memcpy(&i_chroma, psz_chroma, 4);
252 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
253 i_chroma, (char*)&i_chroma );
257 i_chroma = p_vout->render.i_chroma;
260 /* Check that we have access to an XVideo port providing this chroma */
261 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
262 &p_vout->output.i_chroma );
263 if( p_vout->p_sys->i_xvport < 0 )
265 /* If a specific chroma format was requested, then we don't try to
266 * be cleverer than the user. He knew pretty well what he wanted. */
269 XCloseDisplay( p_vout->p_sys->p_display );
270 free( p_vout->p_sys );
274 /* It failed, but it's not completely lost ! We try to open an
275 * XVideo port for an YUY2 picture. We'll need to do an YUV
276 * conversion, but at least it has got scaling. */
277 p_vout->p_sys->i_xvport =
278 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
279 &p_vout->output.i_chroma );
280 if( p_vout->p_sys->i_xvport < 0 )
282 /* It failed, but it's not completely lost ! We try to open an
283 * XVideo port for a simple 16bpp RGB picture. We'll need to do
284 * an YUV conversion, but at least it has got scaling. */
285 p_vout->p_sys->i_xvport =
286 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
287 &p_vout->output.i_chroma );
288 if( p_vout->p_sys->i_xvport < 0 )
290 XCloseDisplay( p_vout->p_sys->p_display );
291 free( p_vout->p_sys );
296 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
297 #elif defined(MODULE_NAME_IS_glx)
299 int i_opcode, i_evt, i_err = 0;
300 int i_maj, i_min = 0;
302 /* Check for GLX extension */
303 if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
304 &i_opcode, &i_evt, &i_err ) )
306 msg_Err( p_this, "GLX extension not supported" );
307 XCloseDisplay( p_vout->p_sys->p_display );
308 free( p_vout->p_sys );
311 if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
313 msg_Err( p_this, "glXQueryExtension failed" );
314 XCloseDisplay( p_vout->p_sys->p_display );
315 free( p_vout->p_sys );
319 /* Check GLX version */
320 if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
322 msg_Err( p_this, "glXQueryVersion failed" );
323 XCloseDisplay( p_vout->p_sys->p_display );
324 free( p_vout->p_sys );
327 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
329 p_vout->p_sys->b_glx13 = false;
330 msg_Dbg( p_this, "using GLX 1.2 API" );
334 p_vout->p_sys->b_glx13 = true;
335 msg_Dbg( p_this, "using GLX 1.3 API" );
340 /* Create blank cursor (for mouse cursor autohiding) */
341 p_vout->p_sys->i_time_mouse_last_moved = mdate();
342 p_vout->p_sys->i_mouse_hide_timeout =
343 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
344 p_vout->p_sys->b_mouse_pointer_visible = 1;
345 CreateCursor( p_vout );
347 /* Set main window's size */
348 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
349 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
350 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
351 /* Spawn base window - this window will include the video output window,
352 * but also command buttons, subtitles and other indicators */
353 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
355 msg_Err( p_vout, "cannot create X11 window" );
356 DestroyCursor( p_vout );
357 XCloseDisplay( p_vout->p_sys->p_display );
358 free( p_vout->p_sys );
362 /* Open and initialize device. */
363 if( InitDisplay( p_vout ) )
365 msg_Err( p_vout, "cannot initialize X11 display" );
366 DestroyCursor( p_vout );
367 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
368 XCloseDisplay( p_vout->p_sys->p_display );
369 free( p_vout->p_sys );
373 /* Disable screen saver */
374 DisableXScreenSaver( p_vout );
377 p_vout->p_sys->b_altfullscreen = 0;
378 p_vout->p_sys->i_time_button_last_pressed = 0;
380 TestNetWMSupport( p_vout );
382 #ifdef MODULE_NAME_IS_xvmc
383 p_vout->p_sys->p_last_subtitle_save = NULL;
384 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
386 /* Look what method was requested */
387 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
388 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
391 if( (strcmp(psz_value, "bob") == 0) ||
392 (strcmp(psz_value, "blend") == 0) )
393 p_vout->p_sys->xvmc_deinterlace_method = 2;
394 else if (strcmp(psz_value, "discard") == 0)
395 p_vout->p_sys->xvmc_deinterlace_method = 1;
397 p_vout->p_sys->xvmc_deinterlace_method = 0;
401 p_vout->p_sys->xvmc_deinterlace_method = 0;
403 /* Look what method was requested */
404 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
405 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
406 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
410 if( strncmp( psz_value, "eq", 2 ) == 0 )
411 p_vout->p_sys->xvmc_crop_style = 1;
412 else if( strncmp( psz_value, "4-16", 4 ) == 0)
413 p_vout->p_sys->xvmc_crop_style = 2;
414 else if( strncmp( psz_value, "16-4", 4 ) == 0)
415 p_vout->p_sys->xvmc_crop_style = 3;
417 p_vout->p_sys->xvmc_crop_style = 0;
421 p_vout->p_sys->xvmc_crop_style = 0;
423 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
424 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
426 if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
428 msg_Err( p_vout, "no XVMC capability found" );
429 Deactivate( p_vout );
432 subpicture_t sub_pic;
433 sub_pic.p_sys = NULL;
434 p_vout->p_sys->last_date = 0;
438 p_vout->p_sys->i_hw_scale = 1;
442 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
443 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
444 if ( p_vout->p_sys->p_octx == NULL ) {
445 msg_Err( p_vout, "Could not get osso context" );
447 msg_Dbg( p_vout, "Initialized osso context" );
451 /* Variable to indicate if the window should be on top of others */
452 /* Trigger a callback right now */
453 var_Get( p_vout, "video-on-top", &val );
454 var_Set( p_vout, "video-on-top", val );
459 /*****************************************************************************
460 * Deactivate: destroy X11 video thread output method
461 *****************************************************************************
462 * Terminate an output method created by Open
463 *****************************************************************************/
464 void Deactivate ( vlc_object_t *p_this )
466 vout_thread_t *p_vout = (vout_thread_t *)p_this;
468 /* If the fullscreen window is still open, close it */
469 if( p_vout->b_fullscreen )
471 ToggleFullScreen( p_vout );
474 /* Restore cursor if it was blanked */
475 if( !p_vout->p_sys->b_mouse_pointer_visible )
477 ToggleCursor( p_vout );
480 #ifdef MODULE_NAME_IS_x11
481 /* Destroy colormap */
482 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
484 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
486 #elif defined(MODULE_NAME_IS_xvideo)
487 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
488 #elif defined(MODULE_NAME_IS_xvmc)
489 if( p_vout->p_sys->xvmc_cap )
491 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
492 xxmc_dispose_context( p_vout );
493 if( p_vout->p_sys->old_subpic )
495 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
496 p_vout->p_sys->old_subpic = NULL;
498 if( p_vout->p_sys->new_subpic )
500 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
501 p_vout->p_sys->new_subpic = NULL;
503 free( p_vout->p_sys->xvmc_cap );
504 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
509 DisablePixelDoubling(p_vout);
512 DestroyCursor( p_vout );
513 EnableXScreenSaver( p_vout );
514 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
515 XCloseDisplay( p_vout->p_sys->p_display );
517 /* Destroy structure */
518 vlc_mutex_destroy( &p_vout->p_sys->lock );
519 #ifdef MODULE_NAME_IS_xvmc
520 free_context_lock( &p_vout->p_sys->xvmc_lock );
524 if ( p_vout->p_sys->p_octx != NULL ) {
525 msg_Dbg( p_vout, "Deinitializing osso context" );
526 osso_deinitialize( p_vout->p_sys->p_octx );
530 free( p_vout->p_sys );
533 #ifdef MODULE_NAME_IS_xvmc
535 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
538 static int xvmc_check_yv12( Display *display, XvPortID port )
540 XvImageFormatValues *formatValues;
544 formatValues = XvListImageFormats( display, port, &formats );
546 for( i = 0; i < formats; i++ )
548 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
549 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
551 XFree (formatValues);
556 XFree (formatValues);
560 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
562 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
565 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
568 int xv_double_buffer;
570 xv_double_buffer = 1;
572 XLockDisplay( p_vout->p_sys->p_display );
573 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
575 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
577 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
578 XUnlockDisplay( p_vout->p_sys->p_display );
580 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
581 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
584 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
586 vlc_xxmc_t *xxmc = NULL;
588 vlc_mutex_lock( &p_vout->p_sys->lock );
589 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
591 xxmc = &p_pic->p_sys->xxmc_data;
592 if( (!xxmc->decoded ||
593 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
595 vlc_mutex_unlock( &p_vout->p_sys->lock );
596 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
601 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
602 if (p_vout->p_sys->p_last_subtitle != NULL)
604 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
606 p_vout->p_sys->new_subpic =
607 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
608 p_vout->p_sys->xvmc_width,
609 p_vout->p_sys->xvmc_height,
610 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
612 if (p_vout->p_sys->new_subpic)
614 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
615 XvMCClearSubpicture( p_vout->p_sys->p_display,
616 p_vout->p_sys->new_subpic,
619 p_vout->p_sys->xvmc_width,
620 p_vout->p_sys->xvmc_height,
622 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
623 clear_xx44_palette( &p_vout->p_sys->palette );
625 if( sub_pic.p_sys == NULL )
627 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
628 if( sub_pic.p_sys != NULL )
630 sub_pic.p_sys->p_vout = p_vout;
631 sub_pic.p_sys->xvmc_surf = NULL;
632 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
635 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
636 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
637 sub_pic.p->i_pitch = p_vout->output.i_width;
639 memset( p_vout->p_sys->subImage->data, 0,
640 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
642 if (p_vout->p_last_subtitle != NULL)
644 blend_xx44( p_vout->p_sys->subImage->data,
645 p_vout->p_last_subtitle,
646 p_vout->p_sys->subImage->width,
647 p_vout->p_sys->subImage->height,
648 p_vout->p_sys->subImage->width,
649 &p_vout->p_sys->palette,
650 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
653 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
654 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
655 p_vout->p_sys->new_subpic,
656 p_vout->p_sys->subImage,
659 p_vout->output.i_width, /* overlay->width, */
660 p_vout->output.i_height, /* overlay->height */
663 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
664 if (p_vout->p_sys->old_subpic)
666 xxmc_xvmc_free_subpicture( p_vout,
667 p_vout->p_sys->old_subpic);
668 p_vout->p_sys->old_subpic = NULL;
670 if (p_vout->p_sys->new_subpic)
672 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
673 p_vout->p_sys->new_subpic = NULL;
674 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
675 p_vout->p_sys->xvmc_palette,
677 p_vout->p_sys->old_subpic->num_palette_entries,
678 p_vout->p_sys->old_subpic->entry_bytes,
679 p_vout->p_sys->old_subpic->component_order );
680 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
681 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
682 p_vout->p_sys->old_subpic,
683 p_vout->p_sys->xvmc_palette );
684 XvMCFlushSubpicture( p_vout->p_sys->p_display,
685 p_vout->p_sys->old_subpic);
686 XvMCSyncSubpicture( p_vout->p_sys->p_display,
687 p_vout->p_sys->old_subpic );
688 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
691 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
692 if (p_vout->p_sys->xvmc_backend_subpic )
694 XvMCBlendSubpicture( p_vout->p_sys->p_display,
695 p_pic->p_sys->xvmc_surf,
696 p_vout->p_sys->old_subpic,
699 p_vout->p_sys->xvmc_width,
700 p_vout->p_sys->xvmc_height,
703 p_vout->p_sys->xvmc_width,
704 p_vout->p_sys->xvmc_height );
708 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
709 p_pic->p_sys->xvmc_surf,
710 p_pic->p_sys->xvmc_surf,
711 p_vout->p_sys->old_subpic,
714 p_vout->p_sys->xvmc_width,
715 p_vout->p_sys->xvmc_height,
718 p_vout->p_sys->xvmc_width,
719 p_vout->p_sys->xvmc_height );
721 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
726 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
727 if( p_vout->p_sys->xvmc_backend_subpic )
729 XvMCBlendSubpicture( p_vout->p_sys->p_display,
730 p_pic->p_sys->xvmc_surf,
731 p_vout->p_sys->old_subpic,
733 p_vout->p_sys->xvmc_width,
734 p_vout->p_sys->xvmc_height,
736 p_vout->p_sys->xvmc_width,
737 p_vout->p_sys->xvmc_height );
741 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
742 p_pic->p_sys->xvmc_surf,
743 p_pic->p_sys->xvmc_surf,
744 p_vout->p_sys->old_subpic,
746 p_vout->p_sys->xvmc_width,
747 p_vout->p_sys->xvmc_height,
749 p_vout->p_sys->xvmc_width,
750 p_vout->p_sys->xvmc_height );
752 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
755 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
757 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
759 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
761 vlc_mutex_unlock( &p_vout->p_sys->lock );
766 /*****************************************************************************
767 * EnablePixelDoubling: Enables pixel doubling
768 *****************************************************************************
769 * Checks if the double size image fits in current window, and enables pixel
770 * doubling accordingly. The i_hw_scale is the integer scaling factor.
771 *****************************************************************************/
772 static void EnablePixelDoubling( vout_thread_t *p_vout )
774 int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
775 int i_vert_scale = ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
776 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
777 p_vout->p_sys->i_hw_scale = 2;
778 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
779 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
783 /*****************************************************************************
784 * DisablePixelDoubling: Disables pixel doubling
785 *****************************************************************************
786 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
787 *****************************************************************************/
788 static void DisablePixelDoubling( vout_thread_t *p_vout )
790 if ( p_vout->p_sys->i_hw_scale > 1 ) {
791 msg_Dbg( p_vout, "Disabling pixel doubling" );
792 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
793 p_vout->p_sys->i_hw_scale = 1;
800 /*****************************************************************************
801 * InitVideo: initialize X11 video thread output method
802 *****************************************************************************
803 * This function create the XImages needed by the output thread. It is called
804 * at the beginning of the thread, but also each time the window is resized.
805 *****************************************************************************/
806 static int InitVideo( vout_thread_t *p_vout )
808 unsigned int i_index = 0;
811 I_OUTPUTPICTURES = 0;
813 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
814 /* Initialize the output structure; we already found an XVideo port,
815 * and the corresponding chroma we will be using. Since we can
816 * arbitrary scale, stick to the coordinates and aspect. */
817 p_vout->output.i_width = p_vout->render.i_width;
818 p_vout->output.i_height = p_vout->render.i_height;
819 p_vout->output.i_aspect = p_vout->render.i_aspect;
821 p_vout->fmt_out = p_vout->fmt_in;
822 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
824 switch( p_vout->output.i_chroma )
826 case VLC_FOURCC('R','V','1','6'):
827 #if defined( WORDS_BIGENDIAN )
828 p_vout->output.i_rmask = 0xf800;
829 p_vout->output.i_gmask = 0x07e0;
830 p_vout->output.i_bmask = 0x001f;
832 p_vout->output.i_rmask = 0x001f;
833 p_vout->output.i_gmask = 0x07e0;
834 p_vout->output.i_bmask = 0xf800;
837 case VLC_FOURCC('R','V','1','5'):
838 #if defined( WORDS_BIGENDIAN )
839 p_vout->output.i_rmask = 0x7c00;
840 p_vout->output.i_gmask = 0x03e0;
841 p_vout->output.i_bmask = 0x001f;
843 p_vout->output.i_rmask = 0x001f;
844 p_vout->output.i_gmask = 0x03e0;
845 p_vout->output.i_bmask = 0x7c00;
850 #elif defined(MODULE_NAME_IS_x11)
851 /* Initialize the output structure: RGB with square pixels, whatever
852 * the input format is, since it's the only format we know */
853 switch( p_vout->p_sys->i_screen_depth )
855 case 8: /* FIXME: set the palette */
856 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
858 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
860 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
863 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
865 msg_Err( p_vout, "unknown screen depth %i",
866 p_vout->p_sys->i_screen_depth );
871 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width / p_vout->p_sys->i_hw_scale,
872 p_vout->p_sys->p_win->i_height / p_vout->p_sys->i_hw_scale,
874 &p_vout->fmt_out.i_visible_width,
875 &p_vout->fmt_out.i_visible_height );
877 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
878 p_vout->p_sys->p_win->i_height,
880 &p_vout->fmt_out.i_visible_width,
881 &p_vout->fmt_out.i_visible_height );
884 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
886 p_vout->output.i_width = p_vout->fmt_out.i_width =
887 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
888 p_vout->fmt_in.i_visible_width;
889 p_vout->output.i_height = p_vout->fmt_out.i_height =
890 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
891 p_vout->fmt_in.i_visible_height;
892 p_vout->fmt_out.i_x_offset =
893 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
894 p_vout->fmt_in.i_visible_width;
895 p_vout->fmt_out.i_y_offset =
896 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
897 p_vout->fmt_in.i_visible_height;
899 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
900 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
901 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
903 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
904 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
905 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
906 p_vout->fmt_out.i_visible_width,
907 p_vout->fmt_out.i_visible_height );
910 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
911 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
915 /* Find an empty picture slot */
916 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
918 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
920 p_pic = p_vout->p_picture + i_index;
925 /* Allocate the picture */
926 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
931 p_pic->i_status = DESTROYED_PICTURE;
932 p_pic->i_type = DIRECT_PICTURE;
934 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
939 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
941 /* U and V inverted compared to I420
942 * Fixme: this should be handled by the vout core */
943 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
944 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
950 /*****************************************************************************
951 * DisplayVideo: displays previously rendered output
952 *****************************************************************************
953 * This function sends the currently rendered image to X11 server.
954 * (The Xv extension takes care of "double-buffering".)
955 *****************************************************************************/
956 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
958 unsigned int i_width, i_height, i_x, i_y;
960 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
961 p_vout->p_sys->p_win->i_height,
962 &i_x, &i_y, &i_width, &i_height );
964 vlc_mutex_lock( &p_vout->p_sys->lock );
966 #ifdef MODULE_NAME_IS_xvmc
967 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
969 vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
970 if( !xxmc->decoded ||
971 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
973 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
975 xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
976 vlc_mutex_unlock( &p_vout->p_sys->lock );
977 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
981 int src_width = p_vout->output.i_width;
982 int src_height = p_vout->output.i_height;
985 if( p_vout->p_sys->xvmc_crop_style == 1 )
992 else if( p_vout->p_sys->xvmc_crop_style == 2 )
999 else if( p_vout->p_sys->xvmc_crop_style == 3 )
1013 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1014 { /* BOB DEINTERLACE */
1015 if( (p_pic->p_sys->nb_display == 0) ||
1016 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1018 first_field = (p_pic->b_top_field_first) ?
1019 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1023 first_field = (p_pic->b_top_field_first) ?
1024 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1029 first_field = XVMC_FRAME_PICTURE;
1032 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1033 XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
1034 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1035 XvMCPutSurface( p_vout->p_sys->p_display,
1036 p_pic->p_sys->xvmc_surf,
1037 p_vout->p_sys->p_win->video_window,
1048 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1049 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1050 { /* BOB DEINTERLACE */
1051 if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1053 mtime_t last_date = p_pic->date;
1055 vlc_mutex_lock( &p_vout->picture_lock );
1056 if( !p_vout->p_sys->last_date )
1058 p_pic->date += 20000;
1062 p_pic->date = ((3 * p_pic->date -
1063 p_vout->p_sys->last_date) / 2 );
1065 p_vout->p_sys->last_date = last_date;
1067 p_pic->p_sys->nb_display = 1;
1068 vlc_mutex_unlock( &p_vout->picture_lock );
1072 p_pic->p_sys->nb_display = 0;
1076 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1079 #ifdef HAVE_SYS_SHM_H
1080 if( p_vout->p_sys->i_shm_opcode )
1082 /* Display rendered image using shared memory extension */
1083 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1084 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1085 p_vout->p_sys->p_win->video_window,
1086 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1087 p_vout->fmt_out.i_x_offset,
1088 p_vout->fmt_out.i_y_offset,
1089 p_vout->fmt_out.i_visible_width,
1090 p_vout->fmt_out.i_visible_height,
1091 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1092 False /* Don't put True here or you'll waste your CPU */ );
1094 XShmPutImage( p_vout->p_sys->p_display,
1095 p_vout->p_sys->p_win->video_window,
1096 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1097 p_vout->fmt_out.i_x_offset,
1098 p_vout->fmt_out.i_y_offset,
1099 0 /*dest_x*/, 0 /*dest_y*/,
1100 p_vout->fmt_out.i_visible_width,
1101 p_vout->fmt_out.i_visible_height,
1102 False /* Don't put True here ! */ );
1106 #endif /* HAVE_SYS_SHM_H */
1108 /* Use standard XPutImage -- this is gonna be slow ! */
1109 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1110 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1111 p_vout->p_sys->p_win->video_window,
1112 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1113 p_vout->fmt_out.i_x_offset,
1114 p_vout->fmt_out.i_y_offset,
1115 p_vout->fmt_out.i_visible_width,
1116 p_vout->fmt_out.i_visible_height,
1117 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1119 XPutImage( p_vout->p_sys->p_display,
1120 p_vout->p_sys->p_win->video_window,
1121 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1122 p_vout->fmt_out.i_x_offset,
1123 p_vout->fmt_out.i_y_offset,
1124 0 /*dest_x*/, 0 /*dest_y*/,
1125 p_vout->fmt_out.i_visible_width,
1126 p_vout->fmt_out.i_visible_height );
1130 /* Make sure the command is sent now - do NOT use XFlush !*/
1131 XSync( p_vout->p_sys->p_display, False );
1133 vlc_mutex_unlock( &p_vout->p_sys->lock );
1136 /*****************************************************************************
1137 * ManageVideo: handle X11 events
1138 *****************************************************************************
1139 * This function should be called regularly by video output thread. It manages
1140 * X11 events and allows window resizing. It returns a non null value on
1142 *****************************************************************************/
1143 static int ManageVideo( vout_thread_t *p_vout )
1145 XEvent xevent; /* X11 event */
1148 vlc_mutex_lock( &p_vout->p_sys->lock );
1150 #ifdef MODULE_NAME_IS_xvmc
1151 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1154 /* Handle events from the owner window */
1155 if( p_vout->p_sys->p_win->owner_window )
1157 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1158 p_vout->p_sys->p_win->owner_window,
1159 StructureNotifyMask, &xevent ) == True )
1161 /* ConfigureNotify event: prepare */
1162 if( xevent.type == ConfigureNotify )
1164 /* Update dimensions */
1165 XResizeWindow( p_vout->p_sys->p_display,
1166 p_vout->p_sys->p_win->base_window,
1167 xevent.xconfigure.width,
1168 xevent.xconfigure.height );
1173 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1174 * output window's size changed, MapNotify and UnmapNotify to know if the
1175 * window is mapped (and if the display is useful), and ClientMessages
1176 * to intercept window destruction requests */
1178 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1179 p_vout->p_sys->p_win->base_window,
1180 StructureNotifyMask | KeyPressMask |
1181 ButtonPressMask | ButtonReleaseMask |
1182 PointerMotionMask | Button1MotionMask , &xevent )
1185 /* ConfigureNotify event: prepare */
1186 if( xevent.type == ConfigureNotify )
1188 if( (unsigned int)xevent.xconfigure.width
1189 != p_vout->p_sys->p_win->i_width
1190 || (unsigned int)xevent.xconfigure.height
1191 != p_vout->p_sys->p_win->i_height )
1193 /* Update dimensions */
1194 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1195 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1196 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1199 /* Keyboard event */
1200 else if( xevent.type == KeyPress )
1202 unsigned int state = xevent.xkey.state;
1203 KeySym x_key_symbol;
1204 char i_key; /* ISO Latin-1 key */
1206 /* We may have keys like F1 trough F12, ESC ... */
1207 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1208 xevent.xkey.keycode, 0 );
1209 val.i_int = ConvertKey( (int)x_key_symbol );
1211 xevent.xkey.state &= ~ShiftMask;
1212 xevent.xkey.state &= ~ControlMask;
1213 xevent.xkey.state &= ~Mod1Mask;
1216 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1219 * The reason why I use this instead of XK_0 is that
1220 * with XLookupString, we don't have to care about
1227 if( state & ShiftMask )
1229 val.i_int |= KEY_MODIFIER_SHIFT;
1231 if( state & ControlMask )
1233 val.i_int |= KEY_MODIFIER_CTRL;
1235 if( state & Mod1Mask )
1237 val.i_int |= KEY_MODIFIER_ALT;
1239 var_Set( p_vout->p_libvlc, "key-pressed", val );
1243 else if( xevent.type == ButtonPress )
1245 switch( ((XButtonEvent *)&xevent)->button )
1248 var_Get( p_vout, "mouse-button-down", &val );
1250 var_Set( p_vout, "mouse-button-down", val );
1252 /* detect double-clicks */
1253 if( ( ((XButtonEvent *)&xevent)->time -
1254 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1256 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1259 p_vout->p_sys->i_time_button_last_pressed =
1260 ((XButtonEvent *)&xevent)->time;
1263 var_Get( p_vout, "mouse-button-down", &val );
1265 var_Set( p_vout, "mouse-button-down", val );
1269 var_Get( p_vout, "mouse-button-down", &val );
1271 var_Set( p_vout, "mouse-button-down", val );
1275 var_Get( p_vout, "mouse-button-down", &val );
1277 var_Set( p_vout, "mouse-button-down", val );
1281 var_Get( p_vout, "mouse-button-down", &val );
1283 var_Set( p_vout, "mouse-button-down", val );
1288 else if( xevent.type == ButtonRelease )
1290 switch( ((XButtonEvent *)&xevent)->button )
1294 var_Get( p_vout, "mouse-button-down", &val );
1296 var_Set( p_vout, "mouse-button-down", val );
1299 var_Set( p_vout, "mouse-clicked", val );
1301 vlc_value_t val; val.b_bool = false;
1302 var_Set( p_vout->p_libvlc, "intf-popupmenu", val );
1308 var_Get( p_vout, "mouse-button-down", &val );
1310 var_Set( p_vout, "mouse-button-down", val );
1312 var_Get( p_vout->p_libvlc, "intf-show", &val );
1313 val.b_bool = !val.b_bool;
1314 var_Set( p_vout->p_libvlc, "intf-show", val );
1320 intf_thread_t *p_intf;
1322 var_Get( p_vout, "mouse-button-down", &val );
1324 var_Set( p_vout, "mouse-button-down", val );
1325 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
1329 p_intf->b_menu_change = 1;
1330 vlc_object_release( p_intf );
1333 vlc_value_t val; val.b_bool = true;
1334 var_Set( p_vout->p_libvlc, "intf-popupmenu", val );
1339 var_Get( p_vout, "mouse-button-down", &val );
1341 var_Set( p_vout, "mouse-button-down", val );
1345 var_Get( p_vout, "mouse-button-down", &val );
1347 var_Set( p_vout, "mouse-button-down", val );
1353 else if( xevent.type == MotionNotify )
1355 unsigned int i_width, i_height, i_x, i_y;
1358 /* somewhat different use for vout_PlacePicture:
1359 * here the values are needed to give to mouse coordinates
1360 * in the original picture space */
1361 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1362 p_vout->p_sys->p_win->i_height,
1363 &i_x, &i_y, &i_width, &i_height );
1365 /* Compute the x coordinate and check if the value is
1366 in [0,p_vout->fmt_in.i_visible_width] */
1367 val.i_int = ( xevent.xmotion.x - i_x ) *
1368 p_vout->fmt_in.i_visible_width / i_width +
1369 p_vout->fmt_in.i_x_offset;
1371 if( (int)(xevent.xmotion.x - i_x) < 0 )
1373 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1374 val.i_int = p_vout->fmt_in.i_visible_width;
1376 var_Set( p_vout, "mouse-x", val );
1378 /* compute the y coordinate and check if the value is
1379 in [0,p_vout->fmt_in.i_visible_height] */
1380 val.i_int = ( xevent.xmotion.y - i_y ) *
1381 p_vout->fmt_in.i_visible_height / i_height +
1382 p_vout->fmt_in.i_y_offset;
1384 if( (int)(xevent.xmotion.y - i_y) < 0 )
1386 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1387 val.i_int = p_vout->fmt_in.i_visible_height;
1389 var_Set( p_vout, "mouse-y", val );
1392 var_Set( p_vout, "mouse-moved", val );
1394 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1395 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1397 ToggleCursor( p_vout );
1400 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1401 || xevent.type == MapNotify
1402 || xevent.type == UnmapNotify )
1404 /* Ignore these events */
1406 else /* Other events */
1408 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1412 /* Handle events for video output sub-window */
1413 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1414 p_vout->p_sys->p_win->video_window,
1415 ExposureMask, &xevent ) == True )
1417 /* Window exposed (only handled if stream playback is paused) */
1418 if( xevent.type == Expose )
1420 if( ((XExposeEvent *)&xevent)->count == 0 )
1422 /* (if this is the last a collection of expose events...) */
1424 #if defined(MODULE_NAME_IS_xvideo)
1425 x11_window_t *p_win = p_vout->p_sys->p_win;
1427 /* Paint the colour key if needed */
1428 if( p_vout->p_sys->b_paint_colourkey &&
1429 xevent.xexpose.window == p_win->video_window )
1431 XSetForeground( p_vout->p_sys->p_display,
1432 p_win->gc, p_vout->p_sys->i_colourkey );
1433 XFillRectangle( p_vout->p_sys->p_display,
1434 p_win->video_window, p_win->gc, 0, 0,
1435 p_win->i_width, p_win->i_height );
1440 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1442 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1443 ->stream.control.i_status )
1445 /* XVideoDisplay( p_vout )*/;
1453 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1454 * are handled - according to the man pages, the format is always 32
1456 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1457 ClientMessage, &xevent ) )
1459 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1460 && ((Atom)xevent.xclient.data.l[0]
1461 == p_vout->p_sys->p_win->wm_delete_window ) )
1463 /* the user wants to close the window */
1464 playlist_t * p_playlist = pl_Yield( p_vout );
1465 if( p_playlist != NULL )
1467 playlist_Stop( p_playlist );
1468 pl_Release( p_playlist );
1476 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1478 /* Update the object variable and trigger callback */
1479 var_SetBool( p_vout, "fullscreen", !p_vout->b_fullscreen );
1481 ToggleFullScreen( p_vout );
1482 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1485 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1486 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1488 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1489 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1491 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1492 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1493 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1494 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1495 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1496 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1497 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1498 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1500 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1506 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1507 * the size flag inside the fullscreen routine)
1509 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1511 unsigned int i_width, i_height, i_x, i_y;
1513 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1515 #ifdef MODULE_NAME_IS_x11
1516 /* We need to signal the vout thread about the size change because it
1517 * is doing the rescaling */
1518 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1521 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1522 p_vout->p_sys->p_win->i_height,
1523 &i_x, &i_y, &i_width, &i_height );
1525 XMoveResizeWindow( p_vout->p_sys->p_display,
1526 p_vout->p_sys->p_win->video_window,
1527 i_x, i_y, i_width, i_height );
1530 /* Autohide Cursour */
1531 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved >
1532 p_vout->p_sys->i_mouse_hide_timeout )
1534 /* Hide the mouse automatically */
1535 if( p_vout->p_sys->b_mouse_pointer_visible )
1537 ToggleCursor( p_vout );
1541 #ifdef MODULE_NAME_IS_xvmc
1542 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1546 if ( p_vout->p_sys->p_octx != NULL ) {
1547 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1548 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1549 msg_Err( p_vout, "Could not disable backlight blanking" );
1551 msg_Dbg( p_vout, "Backlight blanking disabled" );
1553 p_vout->p_sys->i_backlight_on_counter = 0;
1555 p_vout->p_sys->i_backlight_on_counter ++;
1560 vlc_mutex_unlock( &p_vout->p_sys->lock );
1564 /*****************************************************************************
1565 * EndVideo: terminate X11 video thread output method
1566 *****************************************************************************
1567 * Destroy the X11 XImages created by Init. It is called at the end of
1568 * the thread, but also each time the window is resized.
1569 *****************************************************************************/
1570 static void EndVideo( vout_thread_t *p_vout )
1574 /* Free the direct buffers we allocated */
1575 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1578 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1582 /* following functions are local */
1584 /*****************************************************************************
1585 * CreateWindow: open and set-up X11 main window
1586 *****************************************************************************/
1587 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1589 XSizeHints xsize_hints;
1590 XSetWindowAttributes xwindow_attributes;
1591 XGCValues xgcvalues;
1594 bool b_expose = false;
1595 bool b_configure_notify = false;
1596 bool b_map_notify = false;
1599 /* Prepare window manager hints and properties */
1600 p_win->wm_protocols =
1601 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1602 p_win->wm_delete_window =
1603 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1605 /* Never have a 0-pixel-wide window */
1606 xsize_hints.min_width = 2;
1607 xsize_hints.min_height = 1;
1609 /* Prepare window attributes */
1610 xwindow_attributes.backing_store = Always; /* save the hidden part */
1611 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1612 p_vout->p_sys->i_screen);
1613 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1615 if( !p_vout->b_fullscreen )
1617 void *ptr = vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
1618 &p_win->i_width, &p_win->i_height );
1619 p_win->owner_window = (uintptr_t)ptr;
1620 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1621 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1622 xsize_hints.flags = PSize | PMinSize;
1624 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1626 xsize_hints.x = p_win->i_x;
1627 xsize_hints.y = p_win->i_y;
1628 xsize_hints.flags |= PPosition;
1633 /* Fullscreen window size and position */
1634 p_win->owner_window = 0;
1635 p_win->i_x = p_win->i_y = 0;
1637 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1639 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1642 if( !p_win->owner_window )
1644 /* Create the window and set hints - the window must receive
1645 * ConfigureNotify events, and until it is displayed, Expose and
1646 * MapNotify events. */
1648 p_win->base_window =
1649 XCreateWindow( p_vout->p_sys->p_display,
1650 DefaultRootWindow( p_vout->p_sys->p_display ),
1651 p_win->i_x, p_win->i_y,
1652 p_win->i_width, p_win->i_height,
1655 CWBackingStore | CWBackPixel | CWEventMask,
1656 &xwindow_attributes );
1658 var_Get( p_vout, "video-title", &val );
1659 if( !val.psz_string || !*val.psz_string )
1661 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1662 #ifdef MODULE_NAME_IS_x11
1663 VOUT_TITLE " (X11 output)"
1664 #elif defined(MODULE_NAME_IS_glx)
1665 VOUT_TITLE " (GLX output)"
1667 VOUT_TITLE " (XVideo output)"
1673 XStoreName( p_vout->p_sys->p_display,
1674 p_win->base_window, val.psz_string );
1676 free( val.psz_string );
1678 if( !p_vout->b_fullscreen )
1680 const char *argv[] = { "vlc", NULL };
1682 /* Set window manager hints and properties: size hints, command,
1683 * window's name, and accepted protocols */
1684 XSetWMNormalHints( p_vout->p_sys->p_display,
1685 p_win->base_window, &xsize_hints );
1686 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1689 if( !var_GetBool( p_vout, "video-deco") )
1692 mwmhints_t mwmhints;
1694 mwmhints.flags = MWM_HINTS_DECORATIONS;
1695 mwmhints.decorations = False;
1697 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1700 XChangeProperty( p_vout->p_sys->p_display,
1702 prop, prop, 32, PropModeReplace,
1703 (unsigned char *)&mwmhints,
1704 PROP_MWM_HINTS_ELEMENTS );
1712 unsigned int dummy4, dummy5;
1714 /* Select events we are interested in. */
1715 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1716 StructureNotifyMask );
1718 /* Get the parent window's geometry information */
1719 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1720 &dummy1, &dummy2, &dummy3,
1725 /* We are already configured */
1726 b_configure_notify = true;
1728 /* From man XSelectInput: only one client at a time can select a
1729 * ButtonPress event, so we need to open a new window anyway. */
1730 p_win->base_window =
1731 XCreateWindow( p_vout->p_sys->p_display,
1732 p_win->owner_window,
1734 p_win->i_width, p_win->i_height,
1736 0, CopyFromParent, 0,
1737 CWBackingStore | CWBackPixel | CWEventMask,
1738 &xwindow_attributes );
1741 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1742 || (p_win->wm_delete_window == None)
1743 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1744 &p_win->wm_delete_window, 1 ) )
1746 /* WM_DELETE_WINDOW is not supported by window manager */
1747 msg_Warn( p_vout, "missing or bad window manager" );
1750 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1751 * event when using functions like XCopyArea */
1752 xgcvalues.graphics_exposures = False;
1753 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1755 GCGraphicsExposures, &xgcvalues );
1757 /* Send orders to server, and wait until window is displayed - three
1758 * events must be received: a MapNotify event, an Expose event allowing
1759 * drawing in the window, and a ConfigureNotify to get the window
1760 * dimensions. Once those events have been received, only
1761 * ConfigureNotify events need to be received. */
1762 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1765 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1766 SubstructureNotifyMask | StructureNotifyMask |
1767 ExposureMask, &xevent);
1768 if( (xevent.type == Expose)
1769 && (xevent.xexpose.window == p_win->base_window) )
1772 /* ConfigureNotify isn't sent if there isn't a window manager.
1773 * Expose should be the last event to be received so it should
1774 * be fine to assume we won't receive it anymore. */
1775 b_configure_notify = true;
1777 else if( (xevent.type == MapNotify)
1778 && (xevent.xmap.window == p_win->base_window) )
1780 b_map_notify = true;
1782 else if( (xevent.type == ConfigureNotify)
1783 && (xevent.xconfigure.window == p_win->base_window) )
1785 b_configure_notify = true;
1786 p_win->i_width = xevent.xconfigure.width;
1787 p_win->i_height = xevent.xconfigure.height;
1789 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1791 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1792 StructureNotifyMask | KeyPressMask |
1793 ButtonPressMask | ButtonReleaseMask |
1794 PointerMotionMask );
1796 #ifdef MODULE_NAME_IS_x11
1797 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1799 /* Allocate a new palette */
1800 p_vout->p_sys->colormap =
1801 XCreateColormap( p_vout->p_sys->p_display,
1802 DefaultRootWindow( p_vout->p_sys->p_display ),
1803 DefaultVisual( p_vout->p_sys->p_display,
1804 p_vout->p_sys->i_screen ),
1807 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1808 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1809 CWColormap, &xwindow_attributes );
1813 /* Create video output sub-window. */
1814 p_win->video_window = XCreateSimpleWindow(
1815 p_vout->p_sys->p_display,
1816 p_win->base_window, 0, 0,
1817 p_win->i_width, p_win->i_height,
1819 BlackPixel( p_vout->p_sys->p_display,
1820 p_vout->p_sys->i_screen ),
1821 WhitePixel( p_vout->p_sys->p_display,
1822 p_vout->p_sys->i_screen ) );
1824 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1825 BlackPixel( p_vout->p_sys->p_display,
1826 p_vout->p_sys->i_screen ) );
1828 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1829 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1832 /* make sure the video window will be centered in the next ManageVideo() */
1833 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1835 /* If the cursor was formerly blank than blank it again */
1836 if( !p_vout->p_sys->b_mouse_pointer_visible )
1838 ToggleCursor( p_vout );
1839 ToggleCursor( p_vout );
1842 /* Do NOT use XFlush here ! */
1843 XSync( p_vout->p_sys->p_display, False );
1845 /* At this stage, the window is open, displayed, and ready to
1847 p_vout->p_sys->p_win = p_win;
1852 /*****************************************************************************
1853 * DestroyWindow: destroy the window
1854 *****************************************************************************
1856 *****************************************************************************/
1857 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1859 /* Do NOT use XFlush here ! */
1860 XSync( p_vout->p_sys->p_display, False );
1862 if( p_win->video_window != None )
1863 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1865 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1867 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1868 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1870 if( p_win->owner_window )
1871 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1874 /*****************************************************************************
1875 * NewPicture: allocate a picture
1876 *****************************************************************************
1877 * Returns 0 on success, -1 otherwise
1878 *****************************************************************************/
1879 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1881 #ifndef MODULE_NAME_IS_glx
1883 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1887 /* We know the chroma, allocate a buffer which will be used
1888 * directly by the decoder */
1889 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1891 if( p_pic->p_sys == NULL )
1896 #ifdef MODULE_NAME_IS_xvmc
1897 p_pic->p_sys->p_vout = p_vout;
1898 p_pic->p_sys->xvmc_surf = NULL;
1899 p_pic->p_sys->xxmc_data.decoded = 0;
1900 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1901 // p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1902 p_pic->p_sys->nb_display = 0;
1905 /* Fill in picture_t fields */
1906 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1907 p_vout->output.i_width, p_vout->output.i_height,
1908 p_vout->output.i_aspect );
1910 #ifdef HAVE_SYS_SHM_H
1911 if( p_vout->p_sys->i_shm_opcode )
1913 /* Create image using XShm extension */
1914 p_pic->p_sys->p_image =
1915 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1916 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1917 p_vout->p_sys->i_xvport,
1918 VLC2X11_FOURCC(p_vout->output.i_chroma),
1920 p_vout->p_sys->p_visual,
1921 p_vout->p_sys->i_screen_depth,
1923 &p_pic->p_sys->shminfo,
1924 p_vout->output.i_width, p_vout->output.i_height );
1927 if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1928 #endif /* HAVE_SYS_SHM_H */
1930 /* Create image without XShm extension */
1931 p_pic->p_sys->p_image =
1932 CreateImage( p_vout, p_vout->p_sys->p_display,
1933 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1934 p_vout->p_sys->i_xvport,
1935 VLC2X11_FOURCC(p_vout->output.i_chroma),
1936 p_pic->format.i_bits_per_pixel,
1938 p_vout->p_sys->p_visual,
1939 p_vout->p_sys->i_screen_depth,
1940 p_vout->p_sys->i_bytes_per_pixel,
1942 p_vout->output.i_width, p_vout->output.i_height );
1944 #ifdef HAVE_SYS_SHM_H
1945 if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1947 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1948 p_vout->p_sys->i_shm_opcode = 0;
1950 #endif /* HAVE_SYS_SHM_H */
1953 if( p_pic->p_sys->p_image == NULL )
1955 free( p_pic->p_sys );
1959 switch( p_vout->output.i_chroma )
1961 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1962 case VLC_FOURCC('I','4','2','0'):
1963 case VLC_FOURCC('Y','V','1','2'):
1964 case VLC_FOURCC('Y','2','1','1'):
1965 case VLC_FOURCC('Y','U','Y','2'):
1966 case VLC_FOURCC('U','Y','V','Y'):
1967 case VLC_FOURCC('R','V','1','5'):
1968 case VLC_FOURCC('R','V','1','6'):
1969 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1970 case VLC_FOURCC('R','V','3','2'):
1972 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1975 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1976 + p_pic->p_sys->p_image->offsets[i_plane];
1977 p_pic->p[i_plane].i_pitch =
1978 p_pic->p_sys->p_image->pitches[i_plane];
1980 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1982 /* U and V inverted compared to I420
1983 * Fixme: this should be handled by the vout core */
1984 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1985 + p_pic->p_sys->p_image->offsets[2];
1986 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1987 + p_pic->p_sys->p_image->offsets[1];
1993 case VLC_FOURCC('R','G','B','2'):
1994 case VLC_FOURCC('R','V','1','6'):
1995 case VLC_FOURCC('R','V','1','5'):
1996 case VLC_FOURCC('R','V','2','4'):
1997 case VLC_FOURCC('R','V','3','2'):
1999 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
2000 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
2001 p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
2002 + p_pic->p_sys->p_image->xoffset;
2003 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
2005 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
2006 * properly by vout_InitPicture() */
2007 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
2008 * p_pic->p_sys->p_image->width;
2013 /* Unknown chroma, tell the guy to get lost */
2014 IMAGE_FREE( p_pic->p_sys->p_image );
2015 free( p_pic->p_sys );
2016 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
2017 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
2018 p_pic->i_planes = 0;
2023 VLC_UNUSED(p_vout); VLC_UNUSED(p_pic);
2025 #endif /* !MODULE_NAME_IS_glx */
2030 /*****************************************************************************
2031 * FreePicture: destroy a picture allocated with NewPicture
2032 *****************************************************************************
2033 * Destroy XImage AND associated data. If using Shm, detach shared memory
2034 * segment from server and process, then free it. The XDestroyImage manpage
2035 * says that both the image structure _and_ the data pointed to by the
2036 * image structure are freed, so no need to free p_image->data.
2037 *****************************************************************************/
2038 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2040 /* The order of operations is correct */
2041 #ifdef HAVE_SYS_SHM_H
2042 if( p_vout->p_sys->i_shm_opcode )
2044 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2045 IMAGE_FREE( p_pic->p_sys->p_image );
2047 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2048 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2050 msg_Err( p_vout, "cannot detach shared memory (%m)" );
2056 IMAGE_FREE( p_pic->p_sys->p_image );
2059 #ifdef MODULE_NAME_IS_xvmc
2060 if( p_pic->p_sys->xvmc_surf != NULL )
2062 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2063 p_pic->p_sys->xvmc_surf = NULL;
2067 /* Do NOT use XFlush here ! */
2068 XSync( p_vout->p_sys->p_display, False );
2070 free( p_pic->p_sys );
2073 /*****************************************************************************
2074 * ToggleFullScreen: Enable or disable full screen mode
2075 *****************************************************************************
2076 * This function will switch between fullscreen and window mode.
2077 *****************************************************************************/
2078 static void ToggleFullScreen ( vout_thread_t *p_vout )
2082 mwmhints_t mwmhints;
2083 XSetWindowAttributes attributes;
2085 #ifdef HAVE_XINERAMA
2089 p_vout->b_fullscreen = !p_vout->b_fullscreen;
2091 if( p_vout->b_fullscreen )
2093 msg_Dbg( p_vout, "entering fullscreen mode" );
2095 p_vout->p_sys->b_altfullscreen =
2096 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2098 XUnmapWindow( p_vout->p_sys->p_display,
2099 p_vout->p_sys->p_win->base_window );
2101 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2103 CreateWindow( p_vout, p_vout->p_sys->p_win );
2104 XDestroyWindow( p_vout->p_sys->p_display,
2105 p_vout->p_sys->fullscreen_window.video_window );
2106 XReparentWindow( p_vout->p_sys->p_display,
2107 p_vout->p_sys->original_window.video_window,
2108 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2109 p_vout->p_sys->fullscreen_window.video_window =
2110 p_vout->p_sys->original_window.video_window;
2112 /* To my knowledge there are two ways to create a borderless window.
2113 * There's the generic way which is to tell x to bypass the window
2114 * manager, but this creates problems with the focus of other
2116 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2117 * luckily seems to be supported by most window managers. */
2118 if( !p_vout->p_sys->b_altfullscreen )
2120 mwmhints.flags = MWM_HINTS_DECORATIONS;
2121 mwmhints.decorations = False;
2123 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2125 XChangeProperty( p_vout->p_sys->p_display,
2126 p_vout->p_sys->p_win->base_window,
2127 prop, prop, 32, PropModeReplace,
2128 (unsigned char *)&mwmhints,
2129 PROP_MWM_HINTS_ELEMENTS );
2133 /* brute force way to remove decorations */
2134 attributes.override_redirect = True;
2135 XChangeWindowAttributes( p_vout->p_sys->p_display,
2136 p_vout->p_sys->p_win->base_window,
2140 /* Make sure the change is effective */
2141 XReparentWindow( p_vout->p_sys->p_display,
2142 p_vout->p_sys->p_win->base_window,
2143 DefaultRootWindow( p_vout->p_sys->p_display ),
2147 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2149 XClientMessageEvent event;
2151 memset( &event, 0, sizeof( XClientMessageEvent ) );
2153 event.type = ClientMessage;
2154 event.message_type = p_vout->p_sys->net_wm_state;
2155 event.display = p_vout->p_sys->p_display;
2156 event.window = p_vout->p_sys->p_win->base_window;
2158 event.data.l[ 0 ] = 1; /* set property */
2159 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2161 XSendEvent( p_vout->p_sys->p_display,
2162 DefaultRootWindow( p_vout->p_sys->p_display ),
2163 False, SubstructureRedirectMask,
2167 /* Make sure the change is effective */
2168 XReparentWindow( p_vout->p_sys->p_display,
2169 p_vout->p_sys->p_win->base_window,
2170 DefaultRootWindow( p_vout->p_sys->p_display ),
2173 #ifdef HAVE_XINERAMA
2174 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2175 XineramaIsActive( p_vout->p_sys->p_display ) )
2177 XineramaScreenInfo *screens; /* infos for xinerama */
2180 msg_Dbg( p_vout, "using XFree Xinerama extension");
2182 #define SCREEN p_vout->p_sys->p_win->i_screen
2184 /* Get Information about Xinerama (num of screens) */
2185 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2188 SCREEN = config_GetInt( p_vout,
2189 MODULE_STRING "-xineramascreen" );
2191 /* just check that user has entered a good value */
2192 if( SCREEN >= i_num_screens || SCREEN < 0 )
2194 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2198 /* Get the X/Y upper left corner coordinate of the above screen */
2199 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2200 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2202 /* Set the Height/width to the screen resolution */
2203 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2204 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2214 /* The window wasn't necessarily created at the requested size */
2215 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2217 #ifdef HAVE_XF86VIDMODE
2218 XF86VidModeModeLine mode;
2221 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2222 p_vout->p_sys->i_screen, &i_dummy,
2225 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2226 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2228 /* move cursor to the middle of the window to prevent
2229 * unwanted display move if the display is smaller than the
2231 XWarpPointer( p_vout->p_sys->p_display, None,
2232 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2233 mode.hdisplay / 2 , mode.vdisplay / 2 );
2234 /* force desktop view to upper left corner */
2235 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2236 p_vout->p_sys->i_screen, 0, 0 );
2241 p_vout->p_sys->p_win->i_width =
2242 DisplayWidth( p_vout->p_sys->p_display,
2243 p_vout->p_sys->i_screen );
2244 p_vout->p_sys->p_win->i_height =
2245 DisplayHeight( p_vout->p_sys->p_display,
2246 p_vout->p_sys->i_screen );
2251 XMoveResizeWindow( p_vout->p_sys->p_display,
2252 p_vout->p_sys->p_win->base_window,
2253 p_vout->p_sys->p_win->i_x,
2254 p_vout->p_sys->p_win->i_y,
2255 p_vout->p_sys->p_win->i_width,
2256 p_vout->p_sys->p_win->i_height );
2259 EnablePixelDoubling( p_vout );
2262 /* Activate the window (give it the focus) */
2263 XClientMessageEvent event;
2265 memset( &event, 0, sizeof( XClientMessageEvent ) );
2267 event.type = ClientMessage;
2268 event.message_type =
2269 XInternAtom( p_vout->p_sys->p_display, "_NET_ACTIVE_WINDOW", False );
2270 event.display = p_vout->p_sys->p_display;
2271 event.window = p_vout->p_sys->p_win->base_window;
2273 event.data.l[ 0 ] = 1; /* source indication (1 = from an application */
2274 event.data.l[ 1 ] = 0; /* timestamp */
2275 event.data.l[ 2 ] = 0; /* requestor's currently active window */
2276 /* XXX: window manager would be more likely to obey if we already have
2277 * an active window (and give it to the event), such as an interface */
2279 XSendEvent( p_vout->p_sys->p_display,
2280 DefaultRootWindow( p_vout->p_sys->p_display ),
2281 False, SubstructureRedirectMask,
2286 msg_Dbg( p_vout, "leaving fullscreen mode" );
2289 DisablePixelDoubling( p_vout );
2292 XReparentWindow( p_vout->p_sys->p_display,
2293 p_vout->p_sys->original_window.video_window,
2294 p_vout->p_sys->original_window.base_window, 0, 0 );
2296 p_vout->p_sys->fullscreen_window.video_window = None;
2297 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2298 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2300 XMapWindow( p_vout->p_sys->p_display,
2301 p_vout->p_sys->p_win->base_window );
2304 /* Unfortunately, using XSync() here is not enough to ensure the
2305 * window has already been mapped because the XMapWindow() request
2306 * has not necessarily been sent directly to our window (remember,
2307 * the call is first redirected to the window manager) */
2310 XWindowEvent( p_vout->p_sys->p_display,
2311 p_vout->p_sys->p_win->base_window,
2312 StructureNotifyMask, &xevent );
2313 } while( xevent.type != MapNotify );
2315 /* Be careful, this can generate a BadMatch error if the window is not
2316 * already mapped by the server (see above) */
2317 XSetInputFocus(p_vout->p_sys->p_display,
2318 p_vout->p_sys->p_win->base_window,
2322 /* signal that the size needs to be updated */
2323 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2326 /*****************************************************************************
2327 * EnableXScreenSaver: enable screen saver
2328 *****************************************************************************
2329 * This function enables the screen saver on a display after it has been
2330 * disabled by XDisableScreenSaver.
2331 * FIXME: what happens if multiple vlc sessions are running at the same
2333 *****************************************************************************/
2334 static void EnableXScreenSaver( vout_thread_t *p_vout )
2336 #ifdef DPMSINFO_IN_DPMS_H
2340 if( p_vout->p_sys->i_ss_timeout )
2342 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2343 p_vout->p_sys->i_ss_interval,
2344 p_vout->p_sys->i_ss_blanking,
2345 p_vout->p_sys->i_ss_exposure );
2348 /* Restore DPMS settings */
2349 #ifdef DPMSINFO_IN_DPMS_H
2350 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2352 if( p_vout->p_sys->b_ss_dpms )
2354 DPMSEnable( p_vout->p_sys->p_display );
2360 /*****************************************************************************
2361 * DisableXScreenSaver: disable screen saver
2362 *****************************************************************************
2363 * See XEnableXScreenSaver
2364 *****************************************************************************/
2365 static void DisableXScreenSaver( vout_thread_t *p_vout )
2367 #ifdef DPMSINFO_IN_DPMS_H
2371 /* Save screen saver information */
2372 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2373 &p_vout->p_sys->i_ss_interval,
2374 &p_vout->p_sys->i_ss_blanking,
2375 &p_vout->p_sys->i_ss_exposure );
2377 /* Disable screen saver */
2378 if( p_vout->p_sys->i_ss_timeout )
2380 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2381 p_vout->p_sys->i_ss_interval,
2382 p_vout->p_sys->i_ss_blanking,
2383 p_vout->p_sys->i_ss_exposure );
2387 #ifdef DPMSINFO_IN_DPMS_H
2388 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2391 /* Save DPMS current state */
2392 DPMSInfo( p_vout->p_sys->p_display, &unused,
2393 &p_vout->p_sys->b_ss_dpms );
2394 DPMSDisable( p_vout->p_sys->p_display );
2399 /*****************************************************************************
2400 * CreateCursor: create a blank mouse pointer
2401 *****************************************************************************/
2402 static void CreateCursor( vout_thread_t *p_vout )
2404 XColor cursor_color;
2406 p_vout->p_sys->cursor_pixmap =
2407 XCreatePixmap( p_vout->p_sys->p_display,
2408 DefaultRootWindow( p_vout->p_sys->p_display ),
2411 XParseColor( p_vout->p_sys->p_display,
2412 XCreateColormap( p_vout->p_sys->p_display,
2414 p_vout->p_sys->p_display ),
2416 p_vout->p_sys->p_display,
2417 p_vout->p_sys->i_screen ),
2419 "black", &cursor_color );
2421 p_vout->p_sys->blank_cursor =
2422 XCreatePixmapCursor( p_vout->p_sys->p_display,
2423 p_vout->p_sys->cursor_pixmap,
2424 p_vout->p_sys->cursor_pixmap,
2425 &cursor_color, &cursor_color, 1, 1 );
2428 /*****************************************************************************
2429 * DestroyCursor: destroy the blank mouse pointer
2430 *****************************************************************************/
2431 static void DestroyCursor( vout_thread_t *p_vout )
2433 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2436 /*****************************************************************************
2437 * ToggleCursor: hide or show the mouse pointer
2438 *****************************************************************************
2439 * This function hides the X pointer if it is visible by setting the pointer
2440 * sprite to a blank one. To show it again, we disable the sprite.
2441 *****************************************************************************/
2442 static void ToggleCursor( vout_thread_t *p_vout )
2444 if( p_vout->p_sys->b_mouse_pointer_visible )
2446 XDefineCursor( p_vout->p_sys->p_display,
2447 p_vout->p_sys->p_win->base_window,
2448 p_vout->p_sys->blank_cursor );
2449 p_vout->p_sys->b_mouse_pointer_visible = 0;
2453 XUndefineCursor( p_vout->p_sys->p_display,
2454 p_vout->p_sys->p_win->base_window );
2455 p_vout->p_sys->b_mouse_pointer_visible = 1;
2459 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2460 /*****************************************************************************
2461 * XVideoGetPort: get YUV12 port
2462 *****************************************************************************/
2463 static int XVideoGetPort( vout_thread_t *p_vout,
2464 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
2466 XvAdaptorInfo *p_adaptor;
2468 unsigned int i_adaptor, i_num_adaptors;
2469 int i_requested_adaptor;
2470 int i_selected_port;
2472 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2477 case XvBadExtension:
2478 msg_Warn( p_vout, "XvBadExtension" );
2482 msg_Warn( p_vout, "XvBadAlloc" );
2486 msg_Warn( p_vout, "XvQueryExtension failed" );
2490 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2491 DefaultRootWindow( p_vout->p_sys->p_display ),
2492 &i_num_adaptors, &p_adaptor ) )
2497 case XvBadExtension:
2498 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2502 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2506 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2510 i_selected_port = -1;
2511 #ifdef MODULE_NAME_IS_xvmc
2512 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2514 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2516 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2518 XvImageFormatValues *p_formats;
2519 int i_format, i_num_formats;
2522 /* If we requested an adaptor and it's not this one, we aren't
2524 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2529 /* If the adaptor doesn't have the required properties, skip it */
2530 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2531 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2536 /* Check that adaptor supports our requested format... */
2537 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2538 p_adaptor[i_adaptor].base_id,
2542 i_format < i_num_formats && ( i_selected_port == -1 );
2545 XvAttribute *p_attr;
2546 int i_attr, i_num_attributes;
2547 Atom autopaint = None, colorkey = None;
2549 /* If this is not the format we want, or at least a
2550 * similar one, forget it */
2551 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2556 /* Look for the first available port supporting this format */
2557 for( i_port = p_adaptor[i_adaptor].base_id;
2558 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2559 + p_adaptor[i_adaptor].num_ports) )
2560 && ( i_selected_port == -1 );
2563 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2566 i_selected_port = i_port;
2567 *pi_newchroma = p_formats[ i_format ].id;
2571 /* If no free port was found, forget it */
2572 if( i_selected_port == -1 )
2577 /* If we found a port, print information about it */
2578 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2579 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2580 (char *)&p_formats[ i_format ].id,
2581 ( p_formats[ i_format ].format == XvPacked ) ?
2582 "packed" : "planar" );
2584 /* Use XV_AUTOPAINT_COLORKEY if supported, otherwise we will
2585 * manually paint the colour key */
2586 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2588 &i_num_attributes );
2590 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2592 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2594 autopaint = XInternAtom( p_vout->p_sys->p_display,
2595 "XV_AUTOPAINT_COLORKEY", False );
2596 XvSetPortAttribute( p_vout->p_sys->p_display,
2597 i_selected_port, autopaint, 1 );
2599 if( !strcmp( p_attr[i_attr].name, "XV_COLORKEY" ) )
2601 /* Find out the default colour key */
2602 colorkey = XInternAtom( p_vout->p_sys->p_display,
2603 "XV_COLORKEY", False );
2604 XvGetPortAttribute( p_vout->p_sys->p_display,
2605 i_selected_port, colorkey,
2606 &p_vout->p_sys->i_colourkey );
2609 p_vout->p_sys->b_paint_colourkey =
2610 autopaint == None && colorkey != None;
2612 if( p_attr != NULL )
2618 if( p_formats != NULL )
2625 if( i_num_adaptors > 0 )
2627 XvFreeAdaptorInfo( p_adaptor );
2630 if( i_selected_port == -1 )
2632 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2633 if( i_requested_adaptor == -1 )
2635 msg_Warn( p_vout, "no free XVideo port found for format "
2636 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2640 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2641 "XVideo port for format 0x%.8x (%4.4s)",
2642 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2646 return i_selected_port;
2649 /*****************************************************************************
2650 * XVideoReleasePort: release YUV12 port
2651 *****************************************************************************/
2652 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2654 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2658 /*****************************************************************************
2659 * InitDisplay: open and initialize X11 device
2660 *****************************************************************************
2661 * Create a window according to video output given size, and set other
2662 * properties according to the display properties.
2663 *****************************************************************************/
2664 static int InitDisplay( vout_thread_t *p_vout )
2666 #ifdef MODULE_NAME_IS_x11
2667 XPixmapFormatValues * p_formats; /* pixmap formats */
2668 XVisualInfo * p_xvisual; /* visuals information */
2669 XVisualInfo xvisual_template; /* visual template */
2670 int i_count, i; /* array size */
2673 #ifdef HAVE_SYS_SHM_H
2674 p_vout->p_sys->i_shm_opcode = 0;
2676 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
2678 int major, evt, err;
2680 if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2682 && XShmQueryExtension( p_vout->p_sys->p_display ) )
2683 p_vout->p_sys->i_shm_opcode = major;
2685 if( p_vout->p_sys->i_shm_opcode )
2690 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2692 msg_Dbg( p_vout, "XShm video extension v%d.%d "
2693 "(with%s pixmaps, opcode: %d)",
2694 major, minor, pixmaps ? "" : "out",
2695 p_vout->p_sys->i_shm_opcode );
2698 msg_Warn( p_vout, "XShm video extension not available" );
2701 msg_Dbg( p_vout, "XShm video extension disabled" );
2704 #ifdef MODULE_NAME_IS_xvideo
2705 /* XXX The brightness and contrast values should be read from environment
2706 * XXX variables... */
2708 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2709 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2713 #ifdef MODULE_NAME_IS_x11
2714 /* Initialize structure */
2715 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2717 /* Get screen depth */
2718 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2719 p_vout->p_sys->i_screen );
2720 switch( p_vout->p_sys->i_screen_depth )
2724 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2726 xvisual_template.screen = p_vout->p_sys->i_screen;
2727 xvisual_template.class = DirectColor;
2728 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2729 VisualScreenMask | VisualClassMask,
2730 &xvisual_template, &i_count );
2731 if( p_xvisual == NULL )
2733 msg_Err( p_vout, "no PseudoColor visual available" );
2734 return VLC_EGENERIC;
2736 p_vout->p_sys->i_bytes_per_pixel = 1;
2737 p_vout->output.pf_setpalette = SetPalette;
2744 * Screen depth is higher than 8bpp. TrueColor visual is used.
2746 xvisual_template.screen = p_vout->p_sys->i_screen;
2747 xvisual_template.class = TrueColor;
2748 /* In some cases, we get a truecolor class adaptor that has a different
2749 color depth. So try to get a real true color one first */
2750 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2752 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2753 VisualScreenMask | VisualClassMask |
2755 &xvisual_template, &i_count );
2756 if( p_xvisual == NULL )
2758 msg_Warn( p_vout, "No screen matching the required color depth" );
2759 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2760 VisualScreenMask | VisualClassMask,
2761 &xvisual_template, &i_count );
2762 if( p_xvisual == NULL )
2765 msg_Err( p_vout, "no TrueColor visual available" );
2766 return VLC_EGENERIC;
2770 p_vout->output.i_rmask = p_xvisual->red_mask;
2771 p_vout->output.i_gmask = p_xvisual->green_mask;
2772 p_vout->output.i_bmask = p_xvisual->blue_mask;
2774 /* There is no difference yet between 3 and 4 Bpp. The only way
2775 * to find the actual number of bytes per pixel is to list supported
2776 * pixmap formats. */
2777 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2778 p_vout->p_sys->i_bytes_per_pixel = 0;
2780 for( i = 0; i < i_count; i++ )
2782 /* Under XFree4.0, the list contains pixmap formats available
2783 * through all video depths ; so we have to check against current
2785 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2787 if( p_formats[i].bits_per_pixel / 8
2788 > (int)p_vout->p_sys->i_bytes_per_pixel )
2790 p_vout->p_sys->i_bytes_per_pixel =
2791 p_formats[i].bits_per_pixel / 8;
2795 if( p_formats ) XFree( p_formats );
2799 p_vout->p_sys->p_visual = p_xvisual->visual;
2806 #ifndef MODULE_NAME_IS_glx
2808 #ifdef HAVE_SYS_SHM_H
2809 /*****************************************************************************
2810 * CreateShmImage: create an XImage or XvImage using shared memory extension
2811 *****************************************************************************
2812 * Prepare an XImage or XvImage for display function.
2813 * The order of the operations respects the recommandations of the mit-shm
2814 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2815 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2816 *****************************************************************************/
2817 IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2818 Display* p_display, EXTRA_ARGS_SHM,
2819 int i_width, int i_height )
2821 IMAGE_TYPE *p_image;
2824 /* Create XImage / XvImage */
2825 #ifdef MODULE_NAME_IS_xvideo
2826 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2827 i_width, i_height, p_shm );
2828 #elif defined(MODULE_NAME_IS_xvmc)
2829 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2830 i_width, i_height, p_shm );
2832 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2833 p_shm, i_width, i_height );
2835 if( p_image == NULL )
2837 msg_Err( p_vout, "image creation failed" );
2841 /* Allocate shared memory segment - 0776 set the access permission
2842 * rights (like umask), they are not yet supported by all X servers */
2843 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2844 if( p_shm->shmid < 0 )
2846 msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2847 IMAGE_FREE( p_image );
2851 /* Attach shared memory segment to process (read/write) */
2852 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2853 if(! p_shm->shmaddr )
2855 msg_Err( p_vout, "cannot attach shared memory (%m)" );
2856 IMAGE_FREE( p_image );
2857 shmctl( p_shm->shmid, IPC_RMID, 0 );
2861 /* Read-only data. We won't be using XShmGetImage */
2862 p_shm->readOnly = True;
2864 /* Attach shared memory segment to X server */
2865 XSynchronize( p_display, True );
2866 i_shm_major = p_vout->p_sys->i_shm_opcode;
2867 result = XShmAttach( p_display, p_shm );
2868 if( result == False || !i_shm_major )
2870 msg_Err( p_vout, "cannot attach shared memory to X server" );
2871 IMAGE_FREE( p_image );
2872 shmctl( p_shm->shmid, IPC_RMID, 0 );
2873 shmdt( p_shm->shmaddr );
2876 XSynchronize( p_display, False );
2878 /* Send image to X server. This instruction is required, since having
2879 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2880 * and remember NOT to use XFlush ! */
2881 XSync( p_display, False );
2884 /* Mark the shm segment to be removed when there are no more
2885 * attachements, so it is automatic on process exit or after shmdt */
2886 shmctl( p_shm->shmid, IPC_RMID, 0 );
2893 /*****************************************************************************
2894 * CreateImage: create an XImage or XvImage
2895 *****************************************************************************
2896 * Create a simple image used as a buffer.
2897 *****************************************************************************/
2898 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2899 Display *p_display, EXTRA_ARGS,
2900 int i_width, int i_height )
2902 uint8_t * p_data; /* image data storage zone */
2903 IMAGE_TYPE *p_image;
2904 #ifdef MODULE_NAME_IS_x11
2905 int i_quantum; /* XImage quantum (see below) */
2906 int i_bytes_per_line;
2909 /* Allocate memory for image */
2910 #ifdef MODULE_NAME_IS_xvideo
2911 p_data = malloc( i_width * i_height * i_bits_per_pixel / 8 );
2912 #elif defined(MODULE_NAME_IS_x11)
2913 i_bytes_per_line = i_width * i_bytes_per_pixel;
2914 p_data = malloc( i_bytes_per_line * i_height );
2919 #ifdef MODULE_NAME_IS_x11
2920 /* Optimize the quantum of a scanline regarding its size - the quantum is
2921 a diviser of the number of bits between the start of two scanlines. */
2922 if( i_bytes_per_line & 0xf )
2926 else if( i_bytes_per_line & 0x10 )
2936 /* Create XImage. p_data will be automatically freed */
2937 #ifdef MODULE_NAME_IS_xvideo
2938 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2939 (char *)p_data, i_width, i_height );
2940 #elif defined(MODULE_NAME_IS_x11)
2941 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2942 (char *)p_data, i_width, i_height, i_quantum, 0 );
2944 if( p_image == NULL )
2946 msg_Err( p_vout, "XCreateImage() failed" );
2955 /*****************************************************************************
2956 * X11ErrorHandler: replace error handler so we can intercept some of them
2957 *****************************************************************************/
2958 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2962 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
2964 "[????????] x11 video output error: X11 request %u.%u failed "
2965 "with error code %u:\n %s\n",
2966 event->request_code, event->minor_code, event->error_code, txt );
2968 switch( event->request_code )
2970 case X_SetInputFocus:
2971 /* Ignore errors on XSetInputFocus()
2972 * (they happen when a window is not yet mapped) */
2976 #ifdef HAVE_SYS_SHM_H
2977 if( event->request_code == i_shm_major ) /* MIT-SHM */
2980 "[????????] x11 video output notice:"
2981 " buggy X11 server claims shared memory\n"
2982 "[????????] x11 video output notice:"
2983 " support though it does not work (OpenSSH?)\n" );
2984 return i_shm_major = 0;
2989 XSetErrorHandler(NULL);
2990 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2992 /* Work-around Maemo Xvideo bug */
2997 #ifdef MODULE_NAME_IS_x11
2998 /*****************************************************************************
2999 * SetPalette: sets an 8 bpp palette
3000 *****************************************************************************
3001 * This function sets the palette given as an argument. It does not return
3002 * anything, but could later send information on which colors it was unable
3004 *****************************************************************************/
3005 static void SetPalette( vout_thread_t *p_vout,
3006 uint16_t *red, uint16_t *green, uint16_t *blue )
3009 XColor p_colors[255];
3011 /* allocate palette */
3012 for( i = 0; i < 255; i++ )
3014 /* kludge: colors are indexed reversely because color 255 seems
3015 * to be reserved for black even if we try to set it to white */
3016 p_colors[ i ].pixel = 255 - i;
3017 p_colors[ i ].pad = 0;
3018 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
3019 p_colors[ i ].red = red[ 255 - i ];
3020 p_colors[ i ].blue = blue[ 255 - i ];
3021 p_colors[ i ].green = green[ 255 - i ];
3024 XStoreColors( p_vout->p_sys->p_display,
3025 p_vout->p_sys->colormap, p_colors, 255 );
3029 /*****************************************************************************
3030 * Control: control facility for the vout
3031 *****************************************************************************/
3032 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
3035 unsigned int i_width, i_height;
3036 unsigned int *pi_width, *pi_height;
3042 if( p_vout->p_sys->p_win->owner_window )
3043 return vout_ControlWindow( p_vout,
3044 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3046 pi_width = va_arg( args, unsigned int * );
3047 pi_height = va_arg( args, unsigned int * );
3049 vlc_mutex_lock( &p_vout->p_sys->lock );
3050 *pi_width = p_vout->p_sys->p_win->i_width;
3051 *pi_height = p_vout->p_sys->p_win->i_height;
3052 vlc_mutex_unlock( &p_vout->p_sys->lock );
3056 if( p_vout->p_sys->p_win->owner_window )
3057 return vout_ControlWindow( p_vout,
3058 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3060 vlc_mutex_lock( &p_vout->p_sys->lock );
3062 i_width = va_arg( args, unsigned int );
3063 i_height = va_arg( args, unsigned int );
3064 if( !i_width ) i_width = p_vout->i_window_width;
3065 if( !i_height ) i_height = p_vout->i_window_height;
3067 #ifdef MODULE_NAME_IS_xvmc
3068 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3070 /* Update dimensions */
3071 XResizeWindow( p_vout->p_sys->p_display,
3072 p_vout->p_sys->p_win->base_window,
3073 i_width, i_height );
3074 #ifdef MODULE_NAME_IS_xvmc
3075 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3077 vlc_mutex_unlock( &p_vout->p_sys->lock );
3081 vlc_mutex_lock( &p_vout->p_sys->lock );
3082 XUnmapWindow( p_vout->p_sys->p_display,
3083 p_vout->p_sys->original_window.base_window );
3084 vlc_mutex_unlock( &p_vout->p_sys->lock );
3088 vlc_mutex_lock( &p_vout->p_sys->lock );
3089 if( i_query == VOUT_REPARENT ) d = (Drawable)va_arg( args, int );
3092 #ifdef MODULE_NAME_IS_xvmc
3093 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3095 XReparentWindow( p_vout->p_sys->p_display,
3096 p_vout->p_sys->original_window.base_window,
3097 DefaultRootWindow( p_vout->p_sys->p_display ),
3101 XReparentWindow( p_vout->p_sys->p_display,
3102 p_vout->p_sys->original_window.base_window,
3104 XSync( p_vout->p_sys->p_display, False );
3105 p_vout->p_sys->original_window.owner_window = 0;
3106 #ifdef MODULE_NAME_IS_xvmc
3107 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3109 vlc_mutex_unlock( &p_vout->p_sys->lock );
3110 return vout_vaControlDefault( p_vout, i_query, args );
3112 case VOUT_SET_STAY_ON_TOP:
3113 if( p_vout->p_sys->p_win->owner_window )
3114 return vout_ControlWindow( p_vout,
3115 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3117 b_arg = (bool) va_arg( args, int );
3118 vlc_mutex_lock( &p_vout->p_sys->lock );
3119 #ifdef MODULE_NAME_IS_xvmc
3120 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3122 WindowOnTop( p_vout, b_arg );
3123 #ifdef MODULE_NAME_IS_xvmc
3124 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3126 vlc_mutex_unlock( &p_vout->p_sys->lock );
3130 return vout_vaControlDefault( p_vout, i_query, args );
3134 /*****************************************************************************
3135 * TestNetWMSupport: tests for Extended Window Manager Hints support
3136 *****************************************************************************/
3137 static void TestNetWMSupport( vout_thread_t *p_vout )
3139 int i_ret, i_format;
3140 unsigned long i, i_items, i_bytesafter;
3141 Atom net_wm_supported;
3142 union { Atom *p_atom; unsigned char *p_char; } p_args;
3144 p_args.p_atom = NULL;
3146 p_vout->p_sys->b_net_wm_state_fullscreen =
3147 p_vout->p_sys->b_net_wm_state_above =
3148 p_vout->p_sys->b_net_wm_state_below =
3149 p_vout->p_sys->b_net_wm_state_stays_on_top =
3153 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3155 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3156 DefaultRootWindow( p_vout->p_sys->p_display ),
3158 0, 16384, False, AnyPropertyType,
3160 &i_format, &i_items, &i_bytesafter,
3161 (unsigned char **)&p_args );
3163 if( i_ret != Success || i_items == 0 ) return;
3165 msg_Dbg( p_vout, "Window manager supports NetWM" );
3167 p_vout->p_sys->net_wm_state =
3168 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3169 p_vout->p_sys->net_wm_state_fullscreen =
3170 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3172 p_vout->p_sys->net_wm_state_above =
3173 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3174 p_vout->p_sys->net_wm_state_below =
3175 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3176 p_vout->p_sys->net_wm_state_stays_on_top =
3177 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3180 for( i = 0; i < i_items; i++ )
3182 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3185 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3186 p_vout->p_sys->b_net_wm_state_fullscreen = true;
3188 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3190 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3191 p_vout->p_sys->b_net_wm_state_above = true;
3193 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3195 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3196 p_vout->p_sys->b_net_wm_state_below = true;
3198 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3201 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3202 p_vout->p_sys->b_net_wm_state_stays_on_top = true;
3206 XFree( p_args.p_atom );
3209 /*****************************************************************************
3210 * Key events handling
3211 *****************************************************************************/
3217 } x11keys_to_vlckeys[] =
3219 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3220 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3221 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3222 { XK_F12, KEY_F12 },
3224 { XK_Return, KEY_ENTER },
3225 { XK_KP_Enter, KEY_ENTER },
3226 { XK_space, KEY_SPACE },
3227 { XK_Escape, KEY_ESC },
3229 { XK_Menu, KEY_MENU },
3230 { XK_Left, KEY_LEFT },
3231 { XK_Right, KEY_RIGHT },
3233 { XK_Down, KEY_DOWN },
3235 { XK_Home, KEY_HOME },
3236 { XK_End, KEY_END },
3237 { XK_Page_Up, KEY_PAGEUP },
3238 { XK_Page_Down, KEY_PAGEDOWN },
3240 { XK_Insert, KEY_INSERT },
3241 { XK_Delete, KEY_DELETE },
3242 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3243 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3244 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3245 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3246 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3247 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3248 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3253 static int ConvertKey( int i_key )
3257 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3259 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3261 return x11keys_to_vlckeys[i].i_vlckey;
3268 /*****************************************************************************
3269 * WindowOnTop: Switches the "always on top" state of the video window.
3270 *****************************************************************************/
3271 static int WindowOnTop( vout_thread_t *p_vout, bool b_on_top )
3273 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3275 XClientMessageEvent event;
3277 memset( &event, 0, sizeof( XClientMessageEvent ) );
3279 event.type = ClientMessage;
3280 event.message_type = p_vout->p_sys->net_wm_state;
3281 event.display = p_vout->p_sys->p_display;
3282 event.window = p_vout->p_sys->p_win->base_window;
3284 event.data.l[ 0 ] = b_on_top; /* set property */
3285 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3287 XSendEvent( p_vout->p_sys->p_display,
3288 DefaultRootWindow( p_vout->p_sys->p_display ),
3289 False, SubstructureRedirectMask,
3293 /* use _NET_WM_STATE_ABOVE if window manager
3294 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3295 else if( p_vout->p_sys->b_net_wm_state_above )
3297 XClientMessageEvent event;
3299 memset( &event, 0, sizeof( XClientMessageEvent ) );
3301 event.type = ClientMessage;
3302 event.message_type = p_vout->p_sys->net_wm_state;
3303 event.display = p_vout->p_sys->p_display;
3304 event.window = p_vout->p_sys->p_win->base_window;
3306 event.data.l[ 0 ] = b_on_top; /* set property */
3307 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3309 XSendEvent( p_vout->p_sys->p_display,
3310 DefaultRootWindow( p_vout->p_sys->p_display ),
3311 False, SubstructureRedirectMask,