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 *****************************************************************************/
31 #include <vlc_interface.h>
32 #include <vlc_playlist.h>
36 #include <errno.h> /* ENOMEM */
38 #ifdef HAVE_MACHINE_PARAM_H
40 # include <machine/param.h>
41 # include <sys/types.h> /* typedef ushort */
46 # include <netinet/in.h> /* BSD: struct in_addr */
50 #include <X11/extensions/Xsp.h>
54 # include <sys/shm.h> /* shmget(), shmctl() */
58 #include <X11/Xproto.h>
60 #include <X11/Xutil.h>
61 #include <X11/keysym.h>
62 #include <X11/XF86keysym.h>
64 # include <X11/extensions/XShm.h>
66 #ifdef DPMSINFO_IN_DPMS_H
67 # include <X11/extensions/dpms.h>
70 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
71 # include <X11/extensions/Xv.h>
72 # include <X11/extensions/Xvlib.h>
75 #ifdef MODULE_NAME_IS_glx
80 # include <X11/extensions/Xinerama.h>
83 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
84 # include <X11/extensions/xf86vmode.h>
87 #ifdef MODULE_NAME_IS_xvmc
88 # include <X11/extensions/vldXvMC.h>
89 # include "../../codec/xvmc/accel_xvmc.h"
94 /*****************************************************************************
96 *****************************************************************************/
97 int E_(Activate) ( vlc_object_t * );
98 void E_(Deactivate) ( vlc_object_t * );
100 static int InitVideo ( vout_thread_t * );
101 static void EndVideo ( vout_thread_t * );
102 static void DisplayVideo ( vout_thread_t *, picture_t * );
103 static int ManageVideo ( vout_thread_t * );
104 static int Control ( vout_thread_t *, int, va_list );
106 static int InitDisplay ( vout_thread_t * );
108 static int CreateWindow ( vout_thread_t *, x11_window_t * );
109 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
111 static int NewPicture ( vout_thread_t *, picture_t * );
112 static void FreePicture ( vout_thread_t *, picture_t * );
114 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
115 Display *, EXTRA_ARGS, int, int );
116 #ifdef HAVE_SYS_SHM_H
117 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
118 Display *, EXTRA_ARGS_SHM, int, int );
119 static vlc_bool_t b_shm = VLC_TRUE;
122 static void ToggleFullScreen ( vout_thread_t * );
124 static void EnableXScreenSaver ( vout_thread_t * );
125 static void DisableXScreenSaver ( vout_thread_t * );
127 static void CreateCursor ( vout_thread_t * );
128 static void DestroyCursor ( vout_thread_t * );
129 static void ToggleCursor ( vout_thread_t * );
131 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
132 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
133 static void XVideoReleasePort( vout_thread_t *, int );
136 #ifdef MODULE_NAME_IS_x11
137 static void SetPalette ( vout_thread_t *,
138 uint16_t *, uint16_t *, uint16_t * );
141 #ifdef MODULE_NAME_IS_xvmc
142 static void RenderVideo ( vout_thread_t *, picture_t * );
143 static int xvmc_check_yv12( Display *display, XvPortID port );
144 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
147 static void TestNetWMSupport( vout_thread_t * );
148 static int ConvertKey( int );
150 static int WindowOnTop( vout_thread_t *, vlc_bool_t );
152 static int X11ErrorHandler( Display *, XErrorEvent * );
155 static void EnablePixelDoubling( vout_thread_t *p_vout );
156 static void DisablePixelDoubling( vout_thread_t *p_vout );
160 static const int i_backlight_on_interval = 300;
165 /*****************************************************************************
166 * Activate: allocate X11 video thread output method
167 *****************************************************************************
168 * This function allocate and initialize a X11 vout method. It uses some of the
169 * vout properties to choose the window size, and change them according to the
170 * actual properties of the display.
171 *****************************************************************************/
172 int E_(Activate) ( vlc_object_t *p_this )
174 vout_thread_t *p_vout = (vout_thread_t *)p_this;
177 #if defined(MODULE_NAME_IS_xvmc)
180 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
182 vlc_fourcc_t i_chroma = 0;
183 vlc_bool_t b_chroma = 0;
186 p_vout->pf_init = InitVideo;
187 p_vout->pf_end = EndVideo;
188 p_vout->pf_manage = ManageVideo;
189 #ifdef MODULE_NAME_IS_xvmc
190 p_vout->pf_render = RenderVideo;
192 p_vout->pf_render = NULL;
194 p_vout->pf_display = DisplayVideo;
195 p_vout->pf_control = Control;
197 /* Allocate structure */
198 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
199 if( p_vout->p_sys == NULL )
201 msg_Err( p_vout, "out of memory" );
205 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
207 /* Open display, using the "display" config variable or the DISPLAY
208 * environment variable */
209 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
211 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
213 if( p_vout->p_sys->p_display == NULL ) /* error */
215 msg_Err( p_vout, "cannot open display %s",
216 XDisplayName( psz_display ) );
217 free( p_vout->p_sys );
218 if( psz_display ) free( psz_display );
221 if( psz_display ) free( psz_display );
223 /* Replace error handler so we can intercept some non-fatal errors */
224 XSetErrorHandler( X11ErrorHandler );
226 /* Get a screen ID matching the XOpenDisplay return value */
227 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
229 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
230 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
233 if( strlen( psz_chroma ) >= 4 )
235 /* Do not use direct assignment because we are not sure of the
237 memcpy(&i_chroma, psz_chroma, 4);
246 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
247 i_chroma, (char*)&i_chroma );
251 i_chroma = p_vout->render.i_chroma;
254 /* Check that we have access to an XVideo port providing this chroma */
255 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
256 &p_vout->output.i_chroma );
257 if( p_vout->p_sys->i_xvport < 0 )
259 /* If a specific chroma format was requested, then we don't try to
260 * be cleverer than the user. He knew pretty well what he wanted. */
263 XCloseDisplay( p_vout->p_sys->p_display );
264 free( p_vout->p_sys );
268 /* It failed, but it's not completely lost ! We try to open an
269 * XVideo port for an YUY2 picture. We'll need to do an YUV
270 * conversion, but at least it has got scaling. */
271 p_vout->p_sys->i_xvport =
272 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
273 &p_vout->output.i_chroma );
274 if( p_vout->p_sys->i_xvport < 0 )
276 /* It failed, but it's not completely lost ! We try to open an
277 * XVideo port for a simple 16bpp RGB picture. We'll need to do
278 * an YUV conversion, but at least it has got scaling. */
279 p_vout->p_sys->i_xvport =
280 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
281 &p_vout->output.i_chroma );
282 if( p_vout->p_sys->i_xvport < 0 )
284 XCloseDisplay( p_vout->p_sys->p_display );
285 free( p_vout->p_sys );
290 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
291 #elif defined(MODULE_NAME_IS_glx)
293 int i_opcode, i_evt, i_err = 0;
294 int i_maj, i_min = 0;
296 /* Check for GLX extension */
297 if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
298 &i_opcode, &i_evt, &i_err ) )
300 msg_Err( p_this, "GLX extension not supported" );
301 XCloseDisplay( p_vout->p_sys->p_display );
302 free( p_vout->p_sys );
305 if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
307 msg_Err( p_this, "glXQueryExtension failed" );
308 XCloseDisplay( p_vout->p_sys->p_display );
309 free( p_vout->p_sys );
313 /* Check GLX version */
314 if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
316 msg_Err( p_this, "glXQueryVersion failed" );
317 XCloseDisplay( p_vout->p_sys->p_display );
318 free( p_vout->p_sys );
321 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
323 p_vout->p_sys->b_glx13 = VLC_FALSE;
324 msg_Dbg( p_this, "using GLX 1.2 API" );
328 p_vout->p_sys->b_glx13 = VLC_TRUE;
329 msg_Dbg( p_this, "using GLX 1.3 API" );
334 /* Create blank cursor (for mouse cursor autohiding) */
335 p_vout->p_sys->i_time_mouse_last_moved = mdate();
336 p_vout->p_sys->b_mouse_pointer_visible = 1;
337 CreateCursor( p_vout );
339 /* Set main window's size */
340 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
341 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
342 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
343 /* Spawn base window - this window will include the video output window,
344 * but also command buttons, subtitles and other indicators */
345 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
347 msg_Err( p_vout, "cannot create X11 window" );
348 DestroyCursor( p_vout );
349 XCloseDisplay( p_vout->p_sys->p_display );
350 free( p_vout->p_sys );
354 /* Open and initialize device. */
355 if( InitDisplay( p_vout ) )
357 msg_Err( p_vout, "cannot initialize X11 display" );
358 DestroyCursor( p_vout );
359 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
360 XCloseDisplay( p_vout->p_sys->p_display );
361 free( p_vout->p_sys );
365 /* Disable screen saver */
366 DisableXScreenSaver( p_vout );
369 p_vout->p_sys->b_altfullscreen = 0;
370 p_vout->p_sys->i_time_button_last_pressed = 0;
372 TestNetWMSupport( p_vout );
374 #ifdef MODULE_NAME_IS_xvmc
375 p_vout->p_sys->p_last_subtitle_save = NULL;
376 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
378 /* Look what method was requested */
379 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
380 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
383 if( (strcmp(psz_value, "bob") == 0) ||
384 (strcmp(psz_value, "blend") == 0) )
385 p_vout->p_sys->xvmc_deinterlace_method = 2;
386 else if (strcmp(psz_value, "discard") == 0)
387 p_vout->p_sys->xvmc_deinterlace_method = 1;
389 p_vout->p_sys->xvmc_deinterlace_method = 0;
393 p_vout->p_sys->xvmc_deinterlace_method = 0;
395 /* Look what method was requested */
396 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
397 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
398 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
402 if( strncmp( psz_value, "eq", 2 ) == 0 )
403 p_vout->p_sys->xvmc_crop_style = 1;
404 else if( strncmp( psz_value, "4-16", 4 ) == 0)
405 p_vout->p_sys->xvmc_crop_style = 2;
406 else if( strncmp( psz_value, "16-4", 4 ) == 0)
407 p_vout->p_sys->xvmc_crop_style = 3;
409 p_vout->p_sys->xvmc_crop_style = 0;
413 p_vout->p_sys->xvmc_crop_style = 0;
415 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
416 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
418 if( !checkXvMCCap( p_vout ) )
420 msg_Err( p_vout, "no XVMC capability found" );
421 E_(Deactivate)( p_vout );
424 sub_pic.p_sys = NULL;
425 p_vout->p_sys->last_date = 0;
429 p_vout->p_sys->i_hw_scale = 1;
433 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
434 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
435 if ( p_vout->p_sys->p_octx == NULL ) {
436 msg_Err( p_vout, "Could not get osso context" );
438 msg_Dbg( p_vout, "Initialized osso context" );
442 /* Variable to indicate if the window should be on top of others */
443 /* Trigger a callback right now */
444 var_Get( p_vout, "video-on-top", &val );
445 var_Set( p_vout, "video-on-top", val );
450 /*****************************************************************************
451 * Deactivate: destroy X11 video thread output method
452 *****************************************************************************
453 * Terminate an output method created by Open
454 *****************************************************************************/
455 void E_(Deactivate) ( vlc_object_t *p_this )
457 vout_thread_t *p_vout = (vout_thread_t *)p_this;
459 /* If the fullscreen window is still open, close it */
460 if( p_vout->b_fullscreen )
462 ToggleFullScreen( p_vout );
465 /* Restore cursor if it was blanked */
466 if( !p_vout->p_sys->b_mouse_pointer_visible )
468 ToggleCursor( p_vout );
471 #ifdef MODULE_NAME_IS_x11
472 /* Destroy colormap */
473 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
475 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
477 #elif defined(MODULE_NAME_IS_xvideo)
478 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
479 #elif defined(MODULE_NAME_IS_xvmc)
480 if( p_vout->p_sys->xvmc_cap )
482 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
483 xxmc_dispose_context( p_vout );
484 if( p_vout->p_sys->old_subpic )
486 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
487 p_vout->p_sys->old_subpic = NULL;
489 if( p_vout->p_sys->new_subpic )
491 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
492 p_vout->p_sys->new_subpic = NULL;
494 free( p_vout->p_sys->xvmc_cap );
495 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
500 DisablePixelDoubling(p_vout);
503 DestroyCursor( p_vout );
504 EnableXScreenSaver( p_vout );
505 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
506 XCloseDisplay( p_vout->p_sys->p_display );
508 /* Destroy structure */
509 vlc_mutex_destroy( &p_vout->p_sys->lock );
510 #ifdef MODULE_NAME_IS_xvmc
511 free_context_lock( &p_vout->p_sys->xvmc_lock );
515 if ( p_vout->p_sys->p_octx != NULL ) {
516 msg_Dbg( p_vout, "Deinitializing osso context" );
517 osso_deinitialize( p_vout->p_sys->p_octx );
521 free( p_vout->p_sys );
524 #ifdef MODULE_NAME_IS_xvmc
526 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
529 static int xvmc_check_yv12( Display *display, XvPortID port )
531 XvImageFormatValues *formatValues;
535 formatValues = XvListImageFormats( display, port, &formats );
537 for( i = 0; i < formats; i++ )
539 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
540 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
542 XFree (formatValues);
547 XFree (formatValues);
551 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
553 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
556 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
559 int xv_double_buffer;
561 xv_double_buffer = 1;
563 XLockDisplay( p_vout->p_sys->p_display );
564 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
566 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
568 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
569 XUnlockDisplay( p_vout->p_sys->p_display );
571 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
572 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
575 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
577 vlc_xxmc_t *xxmc = NULL;
579 vlc_mutex_lock( &p_vout->p_sys->lock );
580 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
582 xxmc = &p_pic->p_sys->xxmc_data;
583 if( (!xxmc->decoded ||
584 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
586 vlc_mutex_unlock( &p_vout->p_sys->lock );
587 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
591 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
593 if (p_vout->p_last_subtitle != NULL)
595 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_last_subtitle )
597 p_vout->p_sys->new_subpic =
598 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
599 p_vout->p_sys->xvmc_width,
600 p_vout->p_sys->xvmc_height,
601 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
603 if (p_vout->p_sys->new_subpic)
605 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
606 XvMCClearSubpicture( p_vout->p_sys->p_display,
607 p_vout->p_sys->new_subpic,
610 p_vout->p_sys->xvmc_width,
611 p_vout->p_sys->xvmc_height,
613 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
614 clear_xx44_palette( &p_vout->p_sys->palette );
616 if( sub_pic.p_sys == NULL )
618 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
619 if( sub_pic.p_sys != NULL )
621 sub_pic.p_sys->p_vout = p_vout;
622 sub_pic.p_sys->xvmc_surf = NULL;
623 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
626 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
627 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
628 sub_pic.p->i_pitch = p_vout->output.i_width;
630 memset( p_vout->p_sys->subImage->data, 0,
631 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
633 if (p_vout->p_last_subtitle != NULL)
635 blend_xx44( p_vout->p_sys->subImage->data,
636 p_vout->p_last_subtitle,
637 p_vout->p_sys->subImage->width,
638 p_vout->p_sys->subImage->height,
639 p_vout->p_sys->subImage->width,
640 &p_vout->p_sys->palette,
641 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
644 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
645 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
646 p_vout->p_sys->new_subpic,
647 p_vout->p_sys->subImage,
650 p_vout->output.i_width, /* overlay->width, */
651 p_vout->output.i_height, /* overlay->height */
654 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
655 if (p_vout->p_sys->old_subpic)
657 xxmc_xvmc_free_subpicture( p_vout,
658 p_vout->p_sys->old_subpic);
659 p_vout->p_sys->old_subpic = NULL;
661 if (p_vout->p_sys->new_subpic)
663 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
664 p_vout->p_sys->new_subpic = NULL;
665 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
666 p_vout->p_sys->xvmc_palette,
668 p_vout->p_sys->old_subpic->num_palette_entries,
669 p_vout->p_sys->old_subpic->entry_bytes,
670 p_vout->p_sys->old_subpic->component_order );
671 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
672 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
673 p_vout->p_sys->old_subpic,
674 p_vout->p_sys->xvmc_palette );
675 XvMCFlushSubpicture( p_vout->p_sys->p_display,
676 p_vout->p_sys->old_subpic);
677 XvMCSyncSubpicture( p_vout->p_sys->p_display,
678 p_vout->p_sys->old_subpic );
679 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
682 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
683 if (p_vout->p_sys->xvmc_backend_subpic )
685 XvMCBlendSubpicture( p_vout->p_sys->p_display,
686 p_pic->p_sys->xvmc_surf,
687 p_vout->p_sys->old_subpic,
690 p_vout->p_sys->xvmc_width,
691 p_vout->p_sys->xvmc_height,
694 p_vout->p_sys->xvmc_width,
695 p_vout->p_sys->xvmc_height );
699 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
700 p_pic->p_sys->xvmc_surf,
701 p_pic->p_sys->xvmc_surf,
702 p_vout->p_sys->old_subpic,
705 p_vout->p_sys->xvmc_width,
706 p_vout->p_sys->xvmc_height,
709 p_vout->p_sys->xvmc_width,
710 p_vout->p_sys->xvmc_height );
712 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
717 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
718 if( p_vout->p_sys->xvmc_backend_subpic )
720 XvMCBlendSubpicture( p_vout->p_sys->p_display,
721 p_pic->p_sys->xvmc_surf,
722 p_vout->p_sys->old_subpic,
724 p_vout->p_sys->xvmc_width,
725 p_vout->p_sys->xvmc_height,
727 p_vout->p_sys->xvmc_width,
728 p_vout->p_sys->xvmc_height );
732 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
733 p_pic->p_sys->xvmc_surf,
734 p_pic->p_sys->xvmc_surf,
735 p_vout->p_sys->old_subpic,
737 p_vout->p_sys->xvmc_width,
738 p_vout->p_sys->xvmc_height,
740 p_vout->p_sys->xvmc_width,
741 p_vout->p_sys->xvmc_height );
743 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
746 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
748 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
749 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
751 vlc_mutex_unlock( &p_vout->p_sys->lock );
756 /*****************************************************************************
757 * EnablePixelDoubling: Enables pixel doubling
758 *****************************************************************************
759 * Checks if the double size image fits in current window, and enables pixel
760 * doubling accordingly. The i_hw_scale is the integer scaling factor.
761 *****************************************************************************/
762 static void EnablePixelDoubling( vout_thread_t *p_vout )
764 int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
765 int i_vert_scale = ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
766 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
767 p_vout->p_sys->i_hw_scale = 2;
768 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
769 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
773 /*****************************************************************************
774 * DisablePixelDoubling: Disables pixel doubling
775 *****************************************************************************
776 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
777 *****************************************************************************/
778 static void DisablePixelDoubling( vout_thread_t *p_vout )
780 if ( p_vout->p_sys->i_hw_scale > 1 ) {
781 msg_Dbg( p_vout, "Disabling pixel doubling" );
782 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
783 p_vout->p_sys->i_hw_scale = 1;
790 /*****************************************************************************
791 * InitVideo: initialize X11 video thread output method
792 *****************************************************************************
793 * This function create the XImages needed by the output thread. It is called
794 * at the beginning of the thread, but also each time the window is resized.
795 *****************************************************************************/
796 static int InitVideo( vout_thread_t *p_vout )
798 unsigned int i_index = 0;
801 I_OUTPUTPICTURES = 0;
803 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
804 /* Initialize the output structure; we already found an XVideo port,
805 * and the corresponding chroma we will be using. Since we can
806 * arbitrary scale, stick to the coordinates and aspect. */
807 p_vout->output.i_width = p_vout->render.i_width;
808 p_vout->output.i_height = p_vout->render.i_height;
809 p_vout->output.i_aspect = p_vout->render.i_aspect;
811 p_vout->fmt_out = p_vout->fmt_in;
812 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
814 switch( p_vout->output.i_chroma )
816 case VLC_FOURCC('R','V','1','6'):
817 #if defined( WORDS_BIGENDIAN )
818 p_vout->output.i_rmask = 0xf800;
819 p_vout->output.i_gmask = 0x07e0;
820 p_vout->output.i_bmask = 0x001f;
822 p_vout->output.i_rmask = 0x001f;
823 p_vout->output.i_gmask = 0x07e0;
824 p_vout->output.i_bmask = 0xf800;
827 case VLC_FOURCC('R','V','1','5'):
828 #if defined( WORDS_BIGENDIAN )
829 p_vout->output.i_rmask = 0x7c00;
830 p_vout->output.i_gmask = 0x03e0;
831 p_vout->output.i_bmask = 0x001f;
833 p_vout->output.i_rmask = 0x001f;
834 p_vout->output.i_gmask = 0x03e0;
835 p_vout->output.i_bmask = 0x7c00;
840 #elif defined(MODULE_NAME_IS_x11)
841 /* Initialize the output structure: RGB with square pixels, whatever
842 * the input format is, since it's the only format we know */
843 switch( p_vout->p_sys->i_screen_depth )
845 case 8: /* FIXME: set the palette */
846 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
848 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
850 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
853 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
855 msg_Err( p_vout, "unknown screen depth %i",
856 p_vout->p_sys->i_screen_depth );
861 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width / p_vout->p_sys->i_hw_scale,
862 p_vout->p_sys->p_win->i_height / p_vout->p_sys->i_hw_scale,
864 &p_vout->fmt_out.i_visible_width,
865 &p_vout->fmt_out.i_visible_height );
867 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
868 p_vout->p_sys->p_win->i_height,
870 &p_vout->fmt_out.i_visible_width,
871 &p_vout->fmt_out.i_visible_height );
874 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
876 p_vout->output.i_width = p_vout->fmt_out.i_width =
877 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
878 p_vout->fmt_in.i_visible_width;
879 p_vout->output.i_height = p_vout->fmt_out.i_height =
880 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
881 p_vout->fmt_in.i_visible_height;
882 p_vout->fmt_out.i_x_offset =
883 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
884 p_vout->fmt_in.i_visible_width;
885 p_vout->fmt_out.i_y_offset =
886 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
887 p_vout->fmt_in.i_visible_height;
889 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
890 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
891 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
893 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
894 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
895 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
896 p_vout->fmt_out.i_visible_width,
897 p_vout->fmt_out.i_visible_height );
900 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
901 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
905 /* Find an empty picture slot */
906 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
908 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
910 p_pic = p_vout->p_picture + i_index;
915 /* Allocate the picture */
916 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
921 p_pic->i_status = DESTROYED_PICTURE;
922 p_pic->i_type = DIRECT_PICTURE;
924 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
929 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
931 /* U and V inverted compared to I420
932 * Fixme: this should be handled by the vout core */
933 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
934 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
940 /*****************************************************************************
941 * DisplayVideo: displays previously rendered output
942 *****************************************************************************
943 * This function sends the currently rendered image to X11 server.
944 * (The Xv extension takes care of "double-buffering".)
945 *****************************************************************************/
946 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
948 unsigned int i_width, i_height, i_x, i_y;
950 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
951 p_vout->p_sys->p_win->i_height,
952 &i_x, &i_y, &i_width, &i_height );
954 vlc_mutex_lock( &p_vout->p_sys->lock );
956 #ifdef MODULE_NAME_IS_xvmc
957 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
959 vlc_xxmc_t *xxmc = &p_picture->p_sys->xxmc_data;
960 if( !xxmc->decoded ||
961 !xxmc_xvmc_surface_valid( p_vout, p_picture->p_sys->xvmc_surf ) )
963 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
965 xxmc_xvmc_surface_valid( p_vout, p_picture->p_sys->xvmc_surf ) );
966 vlc_mutex_unlock( &p_vout->p_sys->lock );
967 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
971 src_width = p_vout->output.i_width;
972 src_height = p_vout->output.i_height;
974 if( p_vout->p_sys->xvmc_crop_style == 1 )
981 else if( p_vout->p_sys->xvmc_crop_style == 2 )
988 else if( p_vout->p_sys->xvmc_crop_style == 3 )
1001 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1002 { /* BOB DEINTERLACE */
1003 if( (p_picture->p_sys->nb_display == 0) ||
1004 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1006 first_field = (p_picture->b_top_field_first) ?
1007 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1011 first_field = (p_picture->b_top_field_first) ?
1012 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1017 first_field = XVMC_FRAME_PICTURE;
1020 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1021 XvMCFlushSurface( p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf );
1022 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1023 XvMCPutSurface( p_vout->p_sys->p_display,
1024 p_picture->p_sys->xvmc_surf,
1025 p_vout->p_sys->p_win->video_window,
1036 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1037 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1038 { /* BOB DEINTERLACE */
1039 if( p_picture->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1041 mtime_t last_date = p_picture->date;
1043 vlc_mutex_lock( &p_vout->picture_lock );
1044 if( !p_vout->p_sys->last_date )
1046 p_picture->date += 20000;
1050 p_picture->date = ((3 * p_picture->date -
1051 p_vout->p_sys->last_date) / 2 );
1053 p_vout->p_sys->last_date = last_date;
1054 p_picture->b_force = 1;
1055 p_picture->p_sys->nb_display = 1;
1056 vlc_mutex_unlock( &p_vout->picture_lock );
1060 p_picture->p_sys->nb_display = 0;
1061 p_picture->b_force = 0;
1064 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1067 #ifdef HAVE_SYS_SHM_H
1068 if( p_vout->p_sys->b_shm )
1070 /* Display rendered image using shared memory extension */
1071 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1072 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1073 p_vout->p_sys->p_win->video_window,
1074 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1075 p_vout->fmt_out.i_x_offset,
1076 p_vout->fmt_out.i_y_offset,
1077 p_vout->fmt_out.i_visible_width,
1078 p_vout->fmt_out.i_visible_height,
1079 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1080 False /* Don't put True here or you'll waste your CPU */ );
1082 XShmPutImage( p_vout->p_sys->p_display,
1083 p_vout->p_sys->p_win->video_window,
1084 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1085 p_vout->fmt_out.i_x_offset,
1086 p_vout->fmt_out.i_y_offset,
1087 0 /*dest_x*/, 0 /*dest_y*/,
1088 p_vout->fmt_out.i_visible_width,
1089 p_vout->fmt_out.i_visible_height,
1090 False /* Don't put True here ! */ );
1094 #endif /* HAVE_SYS_SHM_H */
1096 /* Use standard XPutImage -- this is gonna be slow ! */
1097 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1098 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1099 p_vout->p_sys->p_win->video_window,
1100 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1101 p_vout->fmt_out.i_x_offset,
1102 p_vout->fmt_out.i_y_offset,
1103 p_vout->fmt_out.i_visible_width,
1104 p_vout->fmt_out.i_visible_height,
1105 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1107 XPutImage( p_vout->p_sys->p_display,
1108 p_vout->p_sys->p_win->video_window,
1109 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1110 p_vout->fmt_out.i_x_offset,
1111 p_vout->fmt_out.i_y_offset,
1112 0 /*dest_x*/, 0 /*dest_y*/,
1113 p_vout->fmt_out.i_visible_width,
1114 p_vout->fmt_out.i_visible_height );
1118 /* Make sure the command is sent now - do NOT use XFlush !*/
1119 XSync( p_vout->p_sys->p_display, False );
1121 vlc_mutex_unlock( &p_vout->p_sys->lock );
1124 /*****************************************************************************
1125 * ManageVideo: handle X11 events
1126 *****************************************************************************
1127 * This function should be called regularly by video output thread. It manages
1128 * X11 events and allows window resizing. It returns a non null value on
1130 *****************************************************************************/
1131 static int ManageVideo( vout_thread_t *p_vout )
1133 XEvent xevent; /* X11 event */
1136 vlc_mutex_lock( &p_vout->p_sys->lock );
1138 #ifdef MODULE_NAME_IS_xvmc
1139 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1142 /* Handle events from the owner window */
1143 if( p_vout->p_sys->p_win->owner_window )
1145 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1146 p_vout->p_sys->p_win->owner_window,
1147 StructureNotifyMask, &xevent ) == True )
1149 /* ConfigureNotify event: prepare */
1150 if( xevent.type == ConfigureNotify )
1152 /* Update dimensions */
1153 XResizeWindow( p_vout->p_sys->p_display,
1154 p_vout->p_sys->p_win->base_window,
1155 xevent.xconfigure.width,
1156 xevent.xconfigure.height );
1161 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1162 * output window's size changed, MapNotify and UnmapNotify to know if the
1163 * window is mapped (and if the display is useful), and ClientMessages
1164 * to intercept window destruction requests */
1166 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1167 p_vout->p_sys->p_win->base_window,
1168 StructureNotifyMask | KeyPressMask |
1169 ButtonPressMask | ButtonReleaseMask |
1170 PointerMotionMask | Button1MotionMask , &xevent )
1173 /* ConfigureNotify event: prepare */
1174 if( xevent.type == ConfigureNotify )
1176 if( (unsigned int)xevent.xconfigure.width
1177 != p_vout->p_sys->p_win->i_width
1178 || (unsigned int)xevent.xconfigure.height
1179 != p_vout->p_sys->p_win->i_height )
1181 /* Update dimensions */
1182 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1183 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1184 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1187 /* Keyboard event */
1188 else if( xevent.type == KeyPress )
1190 unsigned int state = xevent.xkey.state;
1191 KeySym x_key_symbol;
1192 char i_key; /* ISO Latin-1 key */
1194 /* We may have keys like F1 trough F12, ESC ... */
1195 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1196 xevent.xkey.keycode, 0 );
1197 val.i_int = ConvertKey( (int)x_key_symbol );
1199 xevent.xkey.state &= ~ShiftMask;
1200 xevent.xkey.state &= ~ControlMask;
1201 xevent.xkey.state &= ~Mod1Mask;
1204 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1207 * The reason why I use this instead of XK_0 is that
1208 * with XLookupString, we don't have to care about
1215 if( state & ShiftMask )
1217 val.i_int |= KEY_MODIFIER_SHIFT;
1219 if( state & ControlMask )
1221 val.i_int |= KEY_MODIFIER_CTRL;
1223 if( state & Mod1Mask )
1225 val.i_int |= KEY_MODIFIER_ALT;
1227 var_Set( p_vout->p_libvlc, "key-pressed", val );
1231 else if( xevent.type == ButtonPress )
1233 switch( ((XButtonEvent *)&xevent)->button )
1236 var_Get( p_vout, "mouse-button-down", &val );
1238 var_Set( p_vout, "mouse-button-down", val );
1240 /* detect double-clicks */
1241 if( ( ((XButtonEvent *)&xevent)->time -
1242 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1244 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1247 p_vout->p_sys->i_time_button_last_pressed =
1248 ((XButtonEvent *)&xevent)->time;
1251 var_Get( p_vout, "mouse-button-down", &val );
1253 var_Set( p_vout, "mouse-button-down", val );
1257 var_Get( p_vout, "mouse-button-down", &val );
1259 var_Set( p_vout, "mouse-button-down", val );
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 );
1276 else if( xevent.type == ButtonRelease )
1278 switch( ((XButtonEvent *)&xevent)->button )
1281 var_Get( p_vout, "mouse-button-down", &val );
1283 var_Set( p_vout, "mouse-button-down", val );
1285 val.b_bool = VLC_TRUE;
1286 var_Set( p_vout, "mouse-clicked", val );
1291 playlist_t *p_playlist;
1293 var_Get( p_vout, "mouse-button-down", &val );
1295 var_Set( p_vout, "mouse-button-down", val );
1297 p_playlist = vlc_object_find( p_vout,
1298 VLC_OBJECT_PLAYLIST,
1300 if( p_playlist != NULL )
1303 var_Get( p_playlist, "intf-show", &val );
1304 val.b_bool = !val.b_bool;
1305 var_Set( p_playlist, "intf-show", val );
1306 vlc_object_release( p_playlist );
1313 intf_thread_t *p_intf;
1314 playlist_t *p_playlist;
1316 var_Get( p_vout, "mouse-button-down", &val );
1318 var_Set( p_vout, "mouse-button-down", val );
1319 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
1323 p_intf->b_menu_change = 1;
1324 vlc_object_release( p_intf );
1327 p_playlist = vlc_object_find( p_vout,
1328 VLC_OBJECT_PLAYLIST,
1330 if( p_playlist != NULL )
1332 vlc_value_t val; val.b_bool = VLC_TRUE;
1333 var_Set( p_playlist, "intf-popupmenu", val );
1334 vlc_object_release( p_playlist );
1340 var_Get( p_vout, "mouse-button-down", &val );
1342 var_Set( p_vout, "mouse-button-down", val );
1346 var_Get( p_vout, "mouse-button-down", &val );
1348 var_Set( p_vout, "mouse-button-down", val );
1354 else if( xevent.type == MotionNotify )
1356 unsigned int i_width, i_height, i_x, i_y;
1359 /* somewhat different use for vout_PlacePicture:
1360 * here the values are needed to give to mouse coordinates
1361 * in the original picture space */
1362 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1363 p_vout->p_sys->p_win->i_height,
1364 &i_x, &i_y, &i_width, &i_height );
1366 val.i_int = ( xevent.xmotion.x - i_x ) *
1367 p_vout->fmt_in.i_visible_width / i_width +
1368 p_vout->fmt_in.i_x_offset;
1369 var_Set( p_vout, "mouse-x", val );
1370 val.i_int = ( xevent.xmotion.y - i_y ) *
1371 p_vout->fmt_in.i_visible_height / i_height +
1372 p_vout->fmt_in.i_y_offset;
1373 var_Set( p_vout, "mouse-y", val );
1375 val.b_bool = VLC_TRUE;
1376 var_Set( p_vout, "mouse-moved", val );
1378 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1379 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1381 ToggleCursor( p_vout );
1384 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1385 || xevent.type == MapNotify
1386 || xevent.type == UnmapNotify )
1388 /* Ignore these events */
1390 else /* Other events */
1392 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1396 /* Handle events for video output sub-window */
1397 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1398 p_vout->p_sys->p_win->video_window,
1399 ExposureMask, &xevent ) == True )
1401 /* Window exposed (only handled if stream playback is paused) */
1402 if( xevent.type == Expose )
1404 if( ((XExposeEvent *)&xevent)->count == 0 )
1406 /* (if this is the last a collection of expose events...) */
1408 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1410 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1411 ->stream.control.i_status )
1413 /* XVideoDisplay( p_vout )*/;
1421 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1422 * are handled - according to the man pages, the format is always 32
1424 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1425 ClientMessage, &xevent ) )
1427 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1428 && ((Atom)xevent.xclient.data.l[0]
1429 == p_vout->p_sys->p_win->wm_delete_window ) )
1431 /* the user wants to close the window */
1432 playlist_t * p_playlist =
1433 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1435 if( p_playlist != NULL )
1437 playlist_Stop( p_playlist );
1438 vlc_object_release( p_playlist );
1446 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1448 vlc_value_t val_fs, val_ontop;
1450 /* Update the object variable and trigger callback */
1451 val_fs.b_bool = !p_vout->b_fullscreen;
1453 var_Set( p_vout, "fullscreen", val_fs );
1455 /* Disable "always on top" in fullscreen mode */
1456 var_Get( p_vout, "video-on-top", &val_ontop );
1457 if( val_ontop.b_bool )
1458 WindowOnTop( p_vout, val_fs.b_bool );
1460 ToggleFullScreen( p_vout );
1461 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1464 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1465 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1467 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1468 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1470 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1471 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1472 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1473 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1474 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1475 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1476 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1477 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1479 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1485 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1486 * the size flag inside the fullscreen routine)
1488 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1490 unsigned int i_width, i_height, i_x, i_y;
1492 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1494 #ifdef MODULE_NAME_IS_x11
1495 /* We need to signal the vout thread about the size change because it
1496 * is doing the rescaling */
1497 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1500 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1501 p_vout->p_sys->p_win->i_height,
1502 &i_x, &i_y, &i_width, &i_height );
1504 XMoveResizeWindow( p_vout->p_sys->p_display,
1505 p_vout->p_sys->p_win->video_window,
1506 i_x, i_y, i_width, i_height );
1509 /* Autohide Cursour */
1510 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
1512 /* Hide the mouse automatically */
1513 if( p_vout->p_sys->b_mouse_pointer_visible )
1515 ToggleCursor( p_vout );
1519 #ifdef MODULE_NAME_IS_xvmc
1520 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1524 if ( p_vout->p_sys->p_octx != NULL ) {
1525 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1526 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1527 msg_Err( p_vout, "Could not disable backlight blanking" );
1529 msg_Dbg( p_vout, "Backlight blanking disabled" );
1531 p_vout->p_sys->i_backlight_on_counter = 0;
1533 p_vout->p_sys->i_backlight_on_counter ++;
1538 vlc_mutex_unlock( &p_vout->p_sys->lock );
1542 /*****************************************************************************
1543 * EndVideo: terminate X11 video thread output method
1544 *****************************************************************************
1545 * Destroy the X11 XImages created by Init. It is called at the end of
1546 * the thread, but also each time the window is resized.
1547 *****************************************************************************/
1548 static void EndVideo( vout_thread_t *p_vout )
1552 /* Free the direct buffers we allocated */
1553 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1556 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1560 /* following functions are local */
1562 /*****************************************************************************
1563 * CreateWindow: open and set-up X11 main window
1564 *****************************************************************************/
1565 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1567 XSizeHints xsize_hints;
1568 XSetWindowAttributes xwindow_attributes;
1569 XGCValues xgcvalues;
1572 vlc_bool_t b_expose = VLC_FALSE;
1573 vlc_bool_t b_configure_notify = VLC_FALSE;
1574 vlc_bool_t b_map_notify = VLC_FALSE;
1577 /* Prepare window manager hints and properties */
1578 p_win->wm_protocols =
1579 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1580 p_win->wm_delete_window =
1581 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1583 /* Never have a 0-pixel-wide window */
1584 xsize_hints.min_width = 2;
1585 xsize_hints.min_height = 1;
1587 /* Prepare window attributes */
1588 xwindow_attributes.backing_store = Always; /* save the hidden part */
1589 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1590 p_vout->p_sys->i_screen);
1591 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1593 if( !p_vout->b_fullscreen )
1595 p_win->owner_window =
1596 (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
1597 &p_win->i_width, &p_win->i_height );
1599 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1600 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1601 xsize_hints.flags = PSize | PMinSize;
1603 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1605 xsize_hints.x = p_win->i_x;
1606 xsize_hints.y = p_win->i_y;
1607 xsize_hints.flags |= PPosition;
1612 /* Fullscreen window size and position */
1613 p_win->owner_window = 0;
1614 p_win->i_x = p_win->i_y = 0;
1616 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1618 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1621 if( !p_win->owner_window )
1623 /* Create the window and set hints - the window must receive
1624 * ConfigureNotify events, and until it is displayed, Expose and
1625 * MapNotify events. */
1627 p_win->base_window =
1628 XCreateWindow( p_vout->p_sys->p_display,
1629 DefaultRootWindow( p_vout->p_sys->p_display ),
1630 p_win->i_x, p_win->i_y,
1631 p_win->i_width, p_win->i_height,
1634 CWBackingStore | CWBackPixel | CWEventMask,
1635 &xwindow_attributes );
1637 var_Get( p_vout, "video-title", &val );
1638 if( !val.psz_string || !*val.psz_string )
1640 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1641 #ifdef MODULE_NAME_IS_x11
1642 VOUT_TITLE " (X11 output)"
1643 #elif defined(MODULE_NAME_IS_glx)
1644 VOUT_TITLE " (GLX output)"
1646 VOUT_TITLE " (XVideo output)"
1652 XStoreName( p_vout->p_sys->p_display,
1653 p_win->base_window, val.psz_string );
1655 if( val.psz_string ) free( val.psz_string );
1657 if( !p_vout->b_fullscreen )
1659 /* Set window manager hints and properties: size hints, command,
1660 * window's name, and accepted protocols */
1661 XSetWMNormalHints( p_vout->p_sys->p_display,
1662 p_win->base_window, &xsize_hints );
1663 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1664 p_vout->p_libvlc->ppsz_argv, p_vout->p_libvlc->i_argc );
1666 if( !var_GetBool( p_vout, "video-deco") )
1669 mwmhints_t mwmhints;
1671 mwmhints.flags = MWM_HINTS_DECORATIONS;
1672 mwmhints.decorations = False;
1674 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1677 XChangeProperty( p_vout->p_sys->p_display,
1679 prop, prop, 32, PropModeReplace,
1680 (unsigned char *)&mwmhints,
1681 PROP_MWM_HINTS_ELEMENTS );
1689 unsigned int dummy4, dummy5;
1691 /* Select events we are interested in. */
1692 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1693 StructureNotifyMask );
1695 /* Get the parent window's geometry information */
1696 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1697 &dummy1, &dummy2, &dummy3,
1702 /* We are already configured */
1703 b_configure_notify = VLC_TRUE;
1705 /* From man XSelectInput: only one client at a time can select a
1706 * ButtonPress event, so we need to open a new window anyway. */
1707 p_win->base_window =
1708 XCreateWindow( p_vout->p_sys->p_display,
1709 p_win->owner_window,
1711 p_win->i_width, p_win->i_height,
1713 0, CopyFromParent, 0,
1714 CWBackingStore | CWBackPixel | CWEventMask,
1715 &xwindow_attributes );
1718 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1719 || (p_win->wm_delete_window == None)
1720 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1721 &p_win->wm_delete_window, 1 ) )
1723 /* WM_DELETE_WINDOW is not supported by window manager */
1724 msg_Warn( p_vout, "missing or bad window manager" );
1727 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1728 * event when using functions like XCopyArea */
1729 xgcvalues.graphics_exposures = False;
1730 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1732 GCGraphicsExposures, &xgcvalues );
1734 /* Send orders to server, and wait until window is displayed - three
1735 * events must be received: a MapNotify event, an Expose event allowing
1736 * drawing in the window, and a ConfigureNotify to get the window
1737 * dimensions. Once those events have been received, only
1738 * ConfigureNotify events need to be received. */
1739 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1742 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1743 SubstructureNotifyMask | StructureNotifyMask |
1744 ExposureMask, &xevent);
1745 if( (xevent.type == Expose)
1746 && (xevent.xexpose.window == p_win->base_window) )
1748 b_expose = VLC_TRUE;
1749 /* ConfigureNotify isn't sent if there isn't a window manager.
1750 * Expose should be the last event to be received so it should
1751 * be fine to assume we won't receive it anymore. */
1752 b_configure_notify = VLC_TRUE;
1754 else if( (xevent.type == MapNotify)
1755 && (xevent.xmap.window == p_win->base_window) )
1757 b_map_notify = VLC_TRUE;
1759 else if( (xevent.type == ConfigureNotify)
1760 && (xevent.xconfigure.window == p_win->base_window) )
1762 b_configure_notify = VLC_TRUE;
1763 p_win->i_width = xevent.xconfigure.width;
1764 p_win->i_height = xevent.xconfigure.height;
1766 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1768 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1769 StructureNotifyMask | KeyPressMask |
1770 ButtonPressMask | ButtonReleaseMask |
1771 PointerMotionMask );
1773 #ifdef MODULE_NAME_IS_x11
1774 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1776 /* Allocate a new palette */
1777 p_vout->p_sys->colormap =
1778 XCreateColormap( p_vout->p_sys->p_display,
1779 DefaultRootWindow( p_vout->p_sys->p_display ),
1780 DefaultVisual( p_vout->p_sys->p_display,
1781 p_vout->p_sys->i_screen ),
1784 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1785 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1786 CWColormap, &xwindow_attributes );
1790 /* Create video output sub-window. */
1791 p_win->video_window = XCreateSimpleWindow(
1792 p_vout->p_sys->p_display,
1793 p_win->base_window, 0, 0,
1794 p_win->i_width, p_win->i_height,
1796 BlackPixel( p_vout->p_sys->p_display,
1797 p_vout->p_sys->i_screen ),
1798 WhitePixel( p_vout->p_sys->p_display,
1799 p_vout->p_sys->i_screen ) );
1801 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1802 BlackPixel( p_vout->p_sys->p_display,
1803 p_vout->p_sys->i_screen ) );
1805 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1806 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1809 /* make sure the video window will be centered in the next ManageVideo() */
1810 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1812 /* If the cursor was formerly blank than blank it again */
1813 if( !p_vout->p_sys->b_mouse_pointer_visible )
1815 ToggleCursor( p_vout );
1816 ToggleCursor( p_vout );
1819 /* Do NOT use XFlush here ! */
1820 XSync( p_vout->p_sys->p_display, False );
1822 /* At this stage, the window is open, displayed, and ready to
1824 p_vout->p_sys->p_win = p_win;
1829 /*****************************************************************************
1830 * DestroyWindow: destroy the window
1831 *****************************************************************************
1833 *****************************************************************************/
1834 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1836 /* Do NOT use XFlush here ! */
1837 XSync( p_vout->p_sys->p_display, False );
1839 if( p_win->video_window != None )
1840 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1842 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1844 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1845 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1847 if( p_win->owner_window )
1848 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1851 /*****************************************************************************
1852 * NewPicture: allocate a picture
1853 *****************************************************************************
1854 * Returns 0 on success, -1 otherwise
1855 *****************************************************************************/
1856 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1858 #ifndef MODULE_NAME_IS_glx
1860 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1864 /* We know the chroma, allocate a buffer which will be used
1865 * directly by the decoder */
1866 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1868 if( p_pic->p_sys == NULL )
1873 #ifdef MODULE_NAME_IS_xvmc
1874 p_pic->p_sys->p_vout = p_vout;
1875 p_pic->p_sys->xvmc_surf = NULL;
1876 p_pic->p_sys->xxmc_data.decoded = 0;
1877 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1878 p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1879 p_pic->p_sys->nb_display = 0;
1882 /* Fill in picture_t fields */
1883 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1884 p_vout->output.i_width, p_vout->output.i_height,
1885 p_vout->output.i_aspect );
1887 #ifdef HAVE_SYS_SHM_H
1888 if( p_vout->p_sys->b_shm )
1890 /* Create image using XShm extension */
1891 p_pic->p_sys->p_image =
1892 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1893 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1894 p_vout->p_sys->i_xvport,
1895 VLC2X11_FOURCC(p_vout->output.i_chroma),
1897 p_vout->p_sys->p_visual,
1898 p_vout->p_sys->i_screen_depth,
1900 &p_pic->p_sys->shminfo,
1901 p_vout->output.i_width, p_vout->output.i_height );
1904 if( !p_vout->p_sys->b_shm || !p_pic->p_sys->p_image )
1905 #endif /* HAVE_SYS_SHM_H */
1907 /* Create image without XShm extension */
1908 p_pic->p_sys->p_image =
1909 CreateImage( p_vout, p_vout->p_sys->p_display,
1910 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1911 p_vout->p_sys->i_xvport,
1912 VLC2X11_FOURCC(p_vout->output.i_chroma),
1913 p_pic->format.i_bits_per_pixel,
1915 p_vout->p_sys->p_visual,
1916 p_vout->p_sys->i_screen_depth,
1917 p_vout->p_sys->i_bytes_per_pixel,
1919 p_vout->output.i_width, p_vout->output.i_height );
1921 #ifdef HAVE_SYS_SHM_H
1922 if( p_pic->p_sys->p_image && p_vout->p_sys->b_shm )
1924 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1925 p_vout->p_sys->b_shm = VLC_FALSE;
1927 #endif /* HAVE_SYS_SHM_H */
1930 if( p_pic->p_sys->p_image == NULL )
1932 free( p_pic->p_sys );
1936 switch( p_vout->output.i_chroma )
1938 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1939 case VLC_FOURCC('I','4','2','0'):
1940 case VLC_FOURCC('Y','V','1','2'):
1941 case VLC_FOURCC('Y','2','1','1'):
1942 case VLC_FOURCC('Y','U','Y','2'):
1943 case VLC_FOURCC('U','Y','V','Y'):
1944 case VLC_FOURCC('R','V','1','5'):
1945 case VLC_FOURCC('R','V','1','6'):
1946 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1947 case VLC_FOURCC('R','V','3','2'):
1949 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1952 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1953 + p_pic->p_sys->p_image->offsets[i_plane];
1954 p_pic->p[i_plane].i_pitch =
1955 p_pic->p_sys->p_image->pitches[i_plane];
1957 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1959 /* U and V inverted compared to I420
1960 * Fixme: this should be handled by the vout core */
1961 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1962 + p_pic->p_sys->p_image->offsets[2];
1963 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1964 + p_pic->p_sys->p_image->offsets[1];
1969 case VLC_FOURCC('R','G','B','2'):
1970 case VLC_FOURCC('R','V','1','6'):
1971 case VLC_FOURCC('R','V','1','5'):
1972 case VLC_FOURCC('R','V','2','4'):
1973 case VLC_FOURCC('R','V','3','2'):
1975 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1976 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1977 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1978 + p_pic->p_sys->p_image->xoffset;
1979 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1981 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1982 * properly by vout_InitPicture() */
1983 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1984 * p_pic->p_sys->p_image->width;
1989 /* Unknown chroma, tell the guy to get lost */
1990 IMAGE_FREE( p_pic->p_sys->p_image );
1991 free( p_pic->p_sys );
1992 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1993 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1994 p_pic->i_planes = 0;
1998 #endif /* !MODULE_NAME_IS_glx */
2003 /*****************************************************************************
2004 * FreePicture: destroy a picture allocated with NewPicture
2005 *****************************************************************************
2006 * Destroy XImage AND associated data. If using Shm, detach shared memory
2007 * segment from server and process, then free it. The XDestroyImage manpage
2008 * says that both the image structure _and_ the data pointed to by the
2009 * image structure are freed, so no need to free p_image->data.
2010 *****************************************************************************/
2011 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2013 /* The order of operations is correct */
2014 #ifdef HAVE_SYS_SHM_H
2015 if( p_vout->p_sys->b_shm )
2017 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2018 IMAGE_FREE( p_pic->p_sys->p_image );
2020 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2021 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2023 msg_Err( p_vout, "cannot detach shared memory (%s)",
2030 IMAGE_FREE( p_pic->p_sys->p_image );
2033 #ifdef MODULE_NAME_IS_xvmc
2034 if( p_pic->p_sys->xvmc_surf != NULL )
2036 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2037 p_pic->p_sys->xvmc_surf = NULL;
2041 /* Do NOT use XFlush here ! */
2042 XSync( p_vout->p_sys->p_display, False );
2044 free( p_pic->p_sys );
2047 /*****************************************************************************
2048 * ToggleFullScreen: Enable or disable full screen mode
2049 *****************************************************************************
2050 * This function will switch between fullscreen and window mode.
2051 *****************************************************************************/
2052 static void ToggleFullScreen ( vout_thread_t *p_vout )
2056 mwmhints_t mwmhints;
2057 XSetWindowAttributes attributes;
2059 #ifdef HAVE_XINERAMA
2063 p_vout->b_fullscreen = !p_vout->b_fullscreen;
2065 if( p_vout->b_fullscreen )
2067 msg_Dbg( p_vout, "entering fullscreen mode" );
2069 p_vout->p_sys->b_altfullscreen =
2070 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2072 XUnmapWindow( p_vout->p_sys->p_display,
2073 p_vout->p_sys->p_win->base_window );
2075 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2077 CreateWindow( p_vout, p_vout->p_sys->p_win );
2078 XDestroyWindow( p_vout->p_sys->p_display,
2079 p_vout->p_sys->fullscreen_window.video_window );
2080 XReparentWindow( p_vout->p_sys->p_display,
2081 p_vout->p_sys->original_window.video_window,
2082 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2083 p_vout->p_sys->fullscreen_window.video_window =
2084 p_vout->p_sys->original_window.video_window;
2086 /* To my knowledge there are two ways to create a borderless window.
2087 * There's the generic way which is to tell x to bypass the window
2088 * manager, but this creates problems with the focus of other
2090 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2091 * luckily seems to be supported by most window managers. */
2092 if( !p_vout->p_sys->b_altfullscreen )
2094 mwmhints.flags = MWM_HINTS_DECORATIONS;
2095 mwmhints.decorations = False;
2097 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2099 XChangeProperty( p_vout->p_sys->p_display,
2100 p_vout->p_sys->p_win->base_window,
2101 prop, prop, 32, PropModeReplace,
2102 (unsigned char *)&mwmhints,
2103 PROP_MWM_HINTS_ELEMENTS );
2107 /* brute force way to remove decorations */
2108 attributes.override_redirect = True;
2109 XChangeWindowAttributes( p_vout->p_sys->p_display,
2110 p_vout->p_sys->p_win->base_window,
2114 /* Make sure the change is effective */
2115 XReparentWindow( p_vout->p_sys->p_display,
2116 p_vout->p_sys->p_win->base_window,
2117 DefaultRootWindow( p_vout->p_sys->p_display ),
2121 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2123 XClientMessageEvent event;
2125 memset( &event, 0, sizeof( XClientMessageEvent ) );
2127 event.type = ClientMessage;
2128 event.message_type = p_vout->p_sys->net_wm_state;
2129 event.display = p_vout->p_sys->p_display;
2130 event.window = p_vout->p_sys->p_win->base_window;
2132 event.data.l[ 0 ] = 1; /* set property */
2133 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2135 XSendEvent( p_vout->p_sys->p_display,
2136 DefaultRootWindow( p_vout->p_sys->p_display ),
2137 False, SubstructureRedirectMask,
2141 /* Make sure the change is effective */
2142 XReparentWindow( p_vout->p_sys->p_display,
2143 p_vout->p_sys->p_win->base_window,
2144 DefaultRootWindow( p_vout->p_sys->p_display ),
2147 #ifdef HAVE_XINERAMA
2148 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2149 XineramaIsActive( p_vout->p_sys->p_display ) )
2151 XineramaScreenInfo *screens; /* infos for xinerama */
2154 msg_Dbg( p_vout, "using XFree Xinerama extension");
2156 #define SCREEN p_vout->p_sys->p_win->i_screen
2158 /* Get Information about Xinerama (num of screens) */
2159 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2162 SCREEN = config_GetInt( p_vout,
2163 MODULE_STRING "-xineramascreen" );
2165 /* just check that user has entered a good value */
2166 if( SCREEN >= i_num_screens || SCREEN < 0 )
2168 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2172 /* Get the X/Y upper left corner coordinate of the above screen */
2173 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2174 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2176 /* Set the Height/width to the screen resolution */
2177 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2178 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2188 /* The window wasn't necessarily created at the requested size */
2189 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2191 #ifdef HAVE_XF86VIDMODE
2192 XF86VidModeModeLine mode;
2195 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2196 p_vout->p_sys->i_screen, &i_dummy,
2199 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2200 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2202 /* move cursor to the middle of the window to prevent
2203 * unwanted display move if the display is smaller than the
2205 XWarpPointer( p_vout->p_sys->p_display, None,
2206 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2207 mode.hdisplay / 2 , mode.vdisplay / 2 );
2208 /* force desktop view to upper left corner */
2209 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2210 p_vout->p_sys->i_screen, 0, 0 );
2215 p_vout->p_sys->p_win->i_width =
2216 DisplayWidth( p_vout->p_sys->p_display,
2217 p_vout->p_sys->i_screen );
2218 p_vout->p_sys->p_win->i_height =
2219 DisplayHeight( p_vout->p_sys->p_display,
2220 p_vout->p_sys->i_screen );
2225 XMoveResizeWindow( p_vout->p_sys->p_display,
2226 p_vout->p_sys->p_win->base_window,
2227 p_vout->p_sys->p_win->i_x,
2228 p_vout->p_sys->p_win->i_y,
2229 p_vout->p_sys->p_win->i_width,
2230 p_vout->p_sys->p_win->i_height );
2233 EnablePixelDoubling( p_vout );
2239 msg_Dbg( p_vout, "leaving fullscreen mode" );
2242 DisablePixelDoubling( p_vout );
2245 XReparentWindow( p_vout->p_sys->p_display,
2246 p_vout->p_sys->original_window.video_window,
2247 p_vout->p_sys->original_window.base_window, 0, 0 );
2249 p_vout->p_sys->fullscreen_window.video_window = None;
2250 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2251 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2253 XMapWindow( p_vout->p_sys->p_display,
2254 p_vout->p_sys->p_win->base_window );
2257 /* Unfortunately, using XSync() here is not enough to ensure the
2258 * window has already been mapped because the XMapWindow() request
2259 * has not necessarily been sent directly to our window (remember,
2260 * the call is first redirected to the window manager) */
2263 XWindowEvent( p_vout->p_sys->p_display,
2264 p_vout->p_sys->p_win->base_window,
2265 StructureNotifyMask, &xevent );
2266 } while( xevent.type != MapNotify );
2268 /* Be careful, this can generate a BadMatch error if the window is not
2269 * already mapped by the server (see above) */
2270 XSetInputFocus(p_vout->p_sys->p_display,
2271 p_vout->p_sys->p_win->base_window,
2275 /* signal that the size needs to be updated */
2276 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2279 /*****************************************************************************
2280 * EnableXScreenSaver: enable screen saver
2281 *****************************************************************************
2282 * This function enables the screen saver on a display after it has been
2283 * disabled by XDisableScreenSaver.
2284 * FIXME: what happens if multiple vlc sessions are running at the same
2286 *****************************************************************************/
2287 static void EnableXScreenSaver( vout_thread_t *p_vout )
2289 #ifdef DPMSINFO_IN_DPMS_H
2293 if( p_vout->p_sys->i_ss_timeout )
2295 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2296 p_vout->p_sys->i_ss_interval,
2297 p_vout->p_sys->i_ss_blanking,
2298 p_vout->p_sys->i_ss_exposure );
2301 /* Restore DPMS settings */
2302 #ifdef DPMSINFO_IN_DPMS_H
2303 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2305 if( p_vout->p_sys->b_ss_dpms )
2307 DPMSEnable( p_vout->p_sys->p_display );
2313 /*****************************************************************************
2314 * DisableXScreenSaver: disable screen saver
2315 *****************************************************************************
2316 * See XEnableXScreenSaver
2317 *****************************************************************************/
2318 static void DisableXScreenSaver( vout_thread_t *p_vout )
2320 #ifdef DPMSINFO_IN_DPMS_H
2324 /* Save screen saver information */
2325 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2326 &p_vout->p_sys->i_ss_interval,
2327 &p_vout->p_sys->i_ss_blanking,
2328 &p_vout->p_sys->i_ss_exposure );
2330 /* Disable screen saver */
2331 if( p_vout->p_sys->i_ss_timeout )
2333 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2334 p_vout->p_sys->i_ss_interval,
2335 p_vout->p_sys->i_ss_blanking,
2336 p_vout->p_sys->i_ss_exposure );
2340 #ifdef DPMSINFO_IN_DPMS_H
2341 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2344 /* Save DPMS current state */
2345 DPMSInfo( p_vout->p_sys->p_display, &unused,
2346 &p_vout->p_sys->b_ss_dpms );
2347 DPMSDisable( p_vout->p_sys->p_display );
2352 /*****************************************************************************
2353 * CreateCursor: create a blank mouse pointer
2354 *****************************************************************************/
2355 static void CreateCursor( vout_thread_t *p_vout )
2357 XColor cursor_color;
2359 p_vout->p_sys->cursor_pixmap =
2360 XCreatePixmap( p_vout->p_sys->p_display,
2361 DefaultRootWindow( p_vout->p_sys->p_display ),
2364 XParseColor( p_vout->p_sys->p_display,
2365 XCreateColormap( p_vout->p_sys->p_display,
2367 p_vout->p_sys->p_display ),
2369 p_vout->p_sys->p_display,
2370 p_vout->p_sys->i_screen ),
2372 "black", &cursor_color );
2374 p_vout->p_sys->blank_cursor =
2375 XCreatePixmapCursor( p_vout->p_sys->p_display,
2376 p_vout->p_sys->cursor_pixmap,
2377 p_vout->p_sys->cursor_pixmap,
2378 &cursor_color, &cursor_color, 1, 1 );
2381 /*****************************************************************************
2382 * DestroyCursor: destroy the blank mouse pointer
2383 *****************************************************************************/
2384 static void DestroyCursor( vout_thread_t *p_vout )
2386 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2389 /*****************************************************************************
2390 * ToggleCursor: hide or show the mouse pointer
2391 *****************************************************************************
2392 * This function hides the X pointer if it is visible by setting the pointer
2393 * sprite to a blank one. To show it again, we disable the sprite.
2394 *****************************************************************************/
2395 static void ToggleCursor( vout_thread_t *p_vout )
2397 if( p_vout->p_sys->b_mouse_pointer_visible )
2399 XDefineCursor( p_vout->p_sys->p_display,
2400 p_vout->p_sys->p_win->base_window,
2401 p_vout->p_sys->blank_cursor );
2402 p_vout->p_sys->b_mouse_pointer_visible = 0;
2406 XUndefineCursor( p_vout->p_sys->p_display,
2407 p_vout->p_sys->p_win->base_window );
2408 p_vout->p_sys->b_mouse_pointer_visible = 1;
2412 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2413 /*****************************************************************************
2414 * XVideoGetPort: get YUV12 port
2415 *****************************************************************************/
2416 static int XVideoGetPort( vout_thread_t *p_vout,
2417 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
2419 XvAdaptorInfo *p_adaptor;
2421 unsigned int i_adaptor, i_num_adaptors;
2422 int i_requested_adaptor;
2423 int i_selected_port;
2425 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2430 case XvBadExtension:
2431 msg_Warn( p_vout, "XvBadExtension" );
2435 msg_Warn( p_vout, "XvBadAlloc" );
2439 msg_Warn( p_vout, "XvQueryExtension failed" );
2443 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2444 DefaultRootWindow( p_vout->p_sys->p_display ),
2445 &i_num_adaptors, &p_adaptor ) )
2450 case XvBadExtension:
2451 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2455 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2459 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2463 i_selected_port = -1;
2464 #ifdef MODULE_NAME_IS_xvmc
2465 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2467 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2469 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2471 XvImageFormatValues *p_formats;
2472 int i_format, i_num_formats;
2475 /* If we requested an adaptor and it's not this one, we aren't
2477 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2482 /* If the adaptor doesn't have the required properties, skip it */
2483 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2484 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2489 /* Check that adaptor supports our requested format... */
2490 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2491 p_adaptor[i_adaptor].base_id,
2495 i_format < i_num_formats && ( i_selected_port == -1 );
2498 XvAttribute *p_attr;
2499 int i_attr, i_num_attributes;
2501 /* If this is not the format we want, or at least a
2502 * similar one, forget it */
2503 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2508 /* Look for the first available port supporting this format */
2509 for( i_port = p_adaptor[i_adaptor].base_id;
2510 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2511 + p_adaptor[i_adaptor].num_ports) )
2512 && ( i_selected_port == -1 );
2515 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2518 i_selected_port = i_port;
2519 *pi_newchroma = p_formats[ i_format ].id;
2523 /* If no free port was found, forget it */
2524 if( i_selected_port == -1 )
2529 /* If we found a port, print information about it */
2530 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2531 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2532 (char *)&p_formats[ i_format ].id,
2533 ( p_formats[ i_format ].format == XvPacked ) ?
2534 "packed" : "planar" );
2536 /* Make sure XV_AUTOPAINT_COLORKEY is set */
2537 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2539 &i_num_attributes );
2541 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2543 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2545 const Atom autopaint =
2546 XInternAtom( p_vout->p_sys->p_display,
2547 "XV_AUTOPAINT_COLORKEY", False );
2548 XvSetPortAttribute( p_vout->p_sys->p_display,
2549 i_selected_port, autopaint, 1 );
2554 if( p_attr != NULL )
2560 if( p_formats != NULL )
2567 if( i_num_adaptors > 0 )
2569 XvFreeAdaptorInfo( p_adaptor );
2572 if( i_selected_port == -1 )
2574 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2575 if( i_requested_adaptor == -1 )
2577 msg_Warn( p_vout, "no free XVideo port found for format "
2578 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2582 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2583 "XVideo port for format 0x%.8x (%4.4s)",
2584 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2588 return i_selected_port;
2591 /*****************************************************************************
2592 * XVideoReleasePort: release YUV12 port
2593 *****************************************************************************/
2594 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2596 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2600 /*****************************************************************************
2601 * InitDisplay: open and initialize X11 device
2602 *****************************************************************************
2603 * Create a window according to video output given size, and set other
2604 * properties according to the display properties.
2605 *****************************************************************************/
2606 static int InitDisplay( vout_thread_t *p_vout )
2608 #ifdef MODULE_NAME_IS_x11
2609 XPixmapFormatValues * p_formats; /* pixmap formats */
2610 XVisualInfo * p_xvisual; /* visuals information */
2611 XVisualInfo xvisual_template; /* visual template */
2612 int i_count, i; /* array size */
2615 #ifdef HAVE_SYS_SHM_H
2616 p_vout->p_sys->b_shm = 0;
2618 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
2621 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
2623 p_vout->p_sys->b_shm =
2624 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
2625 if( p_vout->p_sys->b_shm )
2630 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2632 msg_Dbg( p_vout, "XShm video extension v%d.%d (with%s pixmaps)",
2633 major, minor, pixmaps ? "" : "out" );
2638 if( !p_vout->p_sys->b_shm )
2640 msg_Warn( p_vout, "XShm video extension is unavailable" );
2645 msg_Dbg( p_vout, "disabling XShm video extension" );
2649 msg_Warn( p_vout, "XShm video extension is unavailable" );
2653 #ifdef MODULE_NAME_IS_xvideo
2654 /* XXX The brightness and contrast values should be read from environment
2655 * XXX variables... */
2657 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2658 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2662 #ifdef MODULE_NAME_IS_x11
2663 /* Initialize structure */
2664 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2666 /* Get screen depth */
2667 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2668 p_vout->p_sys->i_screen );
2669 switch( p_vout->p_sys->i_screen_depth )
2673 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2675 xvisual_template.screen = p_vout->p_sys->i_screen;
2676 xvisual_template.class = DirectColor;
2677 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2678 VisualScreenMask | VisualClassMask,
2679 &xvisual_template, &i_count );
2680 if( p_xvisual == NULL )
2682 msg_Err( p_vout, "no PseudoColor visual available" );
2683 return VLC_EGENERIC;
2685 p_vout->p_sys->i_bytes_per_pixel = 1;
2686 p_vout->output.pf_setpalette = SetPalette;
2693 * Screen depth is higher than 8bpp. TrueColor visual is used.
2695 xvisual_template.screen = p_vout->p_sys->i_screen;
2696 xvisual_template.class = TrueColor;
2697 /* In some cases, we get a truecolor class adaptor that has a different
2698 color depth. So try to get a real true color one first */
2699 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2701 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2702 VisualScreenMask | VisualClassMask |
2704 &xvisual_template, &i_count );
2705 if( p_xvisual == NULL )
2707 msg_Warn( p_vout, "No screen matching the required color depth" );
2708 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2709 VisualScreenMask | VisualClassMask,
2710 &xvisual_template, &i_count );
2711 if( p_xvisual == NULL )
2714 msg_Err( p_vout, "no TrueColor visual available" );
2715 return VLC_EGENERIC;
2719 p_vout->output.i_rmask = p_xvisual->red_mask;
2720 p_vout->output.i_gmask = p_xvisual->green_mask;
2721 p_vout->output.i_bmask = p_xvisual->blue_mask;
2723 /* There is no difference yet between 3 and 4 Bpp. The only way
2724 * to find the actual number of bytes per pixel is to list supported
2725 * pixmap formats. */
2726 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2727 p_vout->p_sys->i_bytes_per_pixel = 0;
2729 for( i = 0; i < i_count; i++ )
2731 /* Under XFree4.0, the list contains pixmap formats available
2732 * through all video depths ; so we have to check against current
2734 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2736 if( p_formats[i].bits_per_pixel / 8
2737 > (int)p_vout->p_sys->i_bytes_per_pixel )
2739 p_vout->p_sys->i_bytes_per_pixel =
2740 p_formats[i].bits_per_pixel / 8;
2744 if( p_formats ) XFree( p_formats );
2748 p_vout->p_sys->p_visual = p_xvisual->visual;
2755 #ifdef HAVE_SYS_SHM_H
2756 /*****************************************************************************
2757 * CreateShmImage: create an XImage or XvImage using shared memory extension
2758 *****************************************************************************
2759 * Prepare an XImage or XvImage for display function.
2760 * The order of the operations respects the recommandations of the mit-shm
2761 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2762 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2763 *****************************************************************************/
2764 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2765 Display* p_display, EXTRA_ARGS_SHM,
2766 int i_width, int i_height )
2768 IMAGE_TYPE *p_image;
2771 /* Create XImage / XvImage */
2772 #ifdef MODULE_NAME_IS_xvideo
2774 /* Make sure the buffer is aligned to multiple of 16 */
2775 i_height = ( i_height + 15 ) >> 4 << 4;
2776 i_width = ( i_width + 15 ) >> 4 << 4;
2778 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2779 i_width, i_height, p_shm );
2780 #elif defined(MODULE_NAME_IS_xvmc)
2781 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2782 i_width, i_height, p_shm );
2784 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2785 p_shm, i_width, i_height );
2787 if( p_image == NULL )
2789 msg_Err( p_vout, "image creation failed" );
2793 /* Allocate shared memory segment - 0776 set the access permission
2794 * rights (like umask), they are not yet supported by all X servers */
2795 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2796 if( p_shm->shmid < 0 )
2798 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2799 strerror( errno ) );
2800 IMAGE_FREE( p_image );
2804 /* Attach shared memory segment to process (read/write) */
2805 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2806 if(! p_shm->shmaddr )
2808 msg_Err( p_vout, "cannot attach shared memory (%s)",
2810 IMAGE_FREE( p_image );
2811 shmctl( p_shm->shmid, IPC_RMID, 0 );
2815 /* Read-only data. We won't be using XShmGetImage */
2816 p_shm->readOnly = True;
2818 /* Attach shared memory segment to X server */
2819 XSynchronize( p_display, True );
2821 result = XShmAttach( p_display, p_shm );
2822 if( result == False || !b_shm )
2824 msg_Err( p_vout, "cannot attach shared memory to X server" );
2825 IMAGE_FREE( p_image );
2826 shmctl( p_shm->shmid, IPC_RMID, 0 );
2827 shmdt( p_shm->shmaddr );
2830 XSynchronize( p_display, False );
2832 /* Send image to X server. This instruction is required, since having
2833 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2834 * and remember NOT to use XFlush ! */
2835 XSync( p_display, False );
2838 /* Mark the shm segment to be removed when there are no more
2839 * attachements, so it is automatic on process exit or after shmdt */
2840 shmctl( p_shm->shmid, IPC_RMID, 0 );
2847 /*****************************************************************************
2848 * CreateImage: create an XImage or XvImage
2849 *****************************************************************************
2850 * Create a simple image used as a buffer.
2851 *****************************************************************************/
2852 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2853 Display *p_display, EXTRA_ARGS,
2854 int i_width, int i_height )
2856 byte_t * p_data; /* image data storage zone */
2857 IMAGE_TYPE *p_image;
2858 #ifdef MODULE_NAME_IS_x11
2859 int i_quantum; /* XImage quantum (see below) */
2860 int i_bytes_per_line;
2863 /* Allocate memory for image */
2864 #ifdef MODULE_NAME_IS_xvideo
2866 /* Make sure the buffer is aligned to multiple of 16 */
2867 i_height = ( i_height + 15 ) >> 4 << 4;
2868 i_width = ( i_width + 15 ) >> 4 << 4;
2870 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2871 #elif defined(MODULE_NAME_IS_x11)
2872 i_bytes_per_line = i_width * i_bytes_per_pixel;
2873 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2877 msg_Err( p_vout, "out of memory" );
2881 #ifdef MODULE_NAME_IS_x11
2882 /* Optimize the quantum of a scanline regarding its size - the quantum is
2883 a diviser of the number of bits between the start of two scanlines. */
2884 if( i_bytes_per_line & 0xf )
2888 else if( i_bytes_per_line & 0x10 )
2898 /* Create XImage. p_data will be automatically freed */
2899 #ifdef MODULE_NAME_IS_xvideo
2900 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2901 (char *)p_data, i_width, i_height );
2902 #elif defined(MODULE_NAME_IS_x11)
2903 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2904 (char *)p_data, i_width, i_height, i_quantum, 0 );
2906 if( p_image == NULL )
2908 msg_Err( p_vout, "XCreateImage() failed" );
2916 /*****************************************************************************
2917 * X11ErrorHandler: replace error handler so we can intercept some of them
2918 *****************************************************************************/
2919 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2923 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
2925 "[????????] x11 video output error: X11 request %u.%u failed "
2926 "with error code %u:\n %s\n",
2927 event->request_code, event->minor_code, event->error_code, txt );
2929 switch( event->request_code )
2931 case X_SetInputFocus:
2932 /* Ignore errors on XSetInputFocus()
2933 * (they happen when a window is not yet mapped) */
2936 case 150: /* MIT-SHM */
2937 case 146: /* MIT-SHM too, what gives? */
2938 if( event->minor_code == X_ShmAttach )
2946 XSetErrorHandler(NULL);
2947 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2950 #ifdef MODULE_NAME_IS_x11
2951 /*****************************************************************************
2952 * SetPalette: sets an 8 bpp palette
2953 *****************************************************************************
2954 * This function sets the palette given as an argument. It does not return
2955 * anything, but could later send information on which colors it was unable
2957 *****************************************************************************/
2958 static void SetPalette( vout_thread_t *p_vout,
2959 uint16_t *red, uint16_t *green, uint16_t *blue )
2962 XColor p_colors[255];
2964 /* allocate palette */
2965 for( i = 0; i < 255; i++ )
2967 /* kludge: colors are indexed reversely because color 255 seems
2968 * to be reserved for black even if we try to set it to white */
2969 p_colors[ i ].pixel = 255 - i;
2970 p_colors[ i ].pad = 0;
2971 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2972 p_colors[ i ].red = red[ 255 - i ];
2973 p_colors[ i ].blue = blue[ 255 - i ];
2974 p_colors[ i ].green = green[ 255 - i ];
2977 XStoreColors( p_vout->p_sys->p_display,
2978 p_vout->p_sys->colormap, p_colors, 255 );
2982 /*****************************************************************************
2983 * Control: control facility for the vout
2984 *****************************************************************************/
2985 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2988 unsigned int i_width, i_height;
2989 unsigned int *pi_width, *pi_height;
2995 if( p_vout->p_sys->p_win->owner_window )
2996 return vout_ControlWindow( p_vout,
2997 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
2999 pi_width = va_arg( args, unsigned int * );
3000 pi_height = va_arg( args, unsigned int * );
3002 vlc_mutex_lock( &p_vout->p_sys->lock );
3003 *pi_width = p_vout->p_sys->p_win->i_width;
3004 *pi_height = p_vout->p_sys->p_win->i_height;
3005 vlc_mutex_unlock( &p_vout->p_sys->lock );
3009 if( p_vout->p_sys->p_win->owner_window )
3010 return vout_ControlWindow( p_vout,
3011 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3013 vlc_mutex_lock( &p_vout->p_sys->lock );
3015 i_width = va_arg( args, unsigned int );
3016 i_height = va_arg( args, unsigned int );
3017 if( !i_width ) i_width = p_vout->i_window_width;
3018 if( !i_height ) i_height = p_vout->i_window_height;
3020 #ifdef MODULE_NAME_IS_xvmc
3021 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3023 /* Update dimensions */
3024 XResizeWindow( p_vout->p_sys->p_display,
3025 p_vout->p_sys->p_win->base_window,
3026 i_width, i_height );
3027 #ifdef MODULE_NAME_IS_xvmc
3028 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3030 vlc_mutex_unlock( &p_vout->p_sys->lock );
3034 vlc_mutex_lock( &p_vout->p_sys->lock );
3035 XUnmapWindow( p_vout->p_sys->p_display,
3036 p_vout->p_sys->original_window.base_window );
3037 vlc_mutex_unlock( &p_vout->p_sys->lock );
3041 vlc_mutex_lock( &p_vout->p_sys->lock );
3042 if( i_query == VOUT_REPARENT ) d = (Drawable)va_arg( args, int );
3044 #ifdef MODULE_NAME_IS_xvmc
3045 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3047 XReparentWindow( p_vout->p_sys->p_display,
3048 p_vout->p_sys->original_window.base_window,
3049 DefaultRootWindow( p_vout->p_sys->p_display ),
3052 XReparentWindow( p_vout->p_sys->p_display,
3053 p_vout->p_sys->original_window.base_window,
3055 XSync( p_vout->p_sys->p_display, False );
3056 p_vout->p_sys->original_window.owner_window = 0;
3057 #ifdef MODULE_NAME_IS_xvmc
3058 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3060 vlc_mutex_unlock( &p_vout->p_sys->lock );
3061 return vout_vaControlDefault( p_vout, i_query, args );
3063 case VOUT_SET_STAY_ON_TOP:
3064 if( p_vout->p_sys->p_win->owner_window )
3065 return vout_ControlWindow( p_vout,
3066 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3068 b_arg = va_arg( args, vlc_bool_t );
3069 vlc_mutex_lock( &p_vout->p_sys->lock );
3070 #ifdef MODULE_NAME_IS_xvmc
3071 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3073 WindowOnTop( p_vout, b_arg );
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 return vout_vaControlDefault( p_vout, i_query, args );
3085 /*****************************************************************************
3086 * TestNetWMSupport: tests for Extended Window Manager Hints support
3087 *****************************************************************************/
3088 static void TestNetWMSupport( vout_thread_t *p_vout )
3090 int i_ret, i_format;
3091 unsigned long i, i_items, i_bytesafter;
3092 Atom net_wm_supported;
3093 union { Atom *p_atom; unsigned char *p_char; } p_args;
3095 p_args.p_atom = NULL;
3097 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
3098 p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
3099 p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
3100 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
3103 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3105 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3106 DefaultRootWindow( p_vout->p_sys->p_display ),
3108 0, 16384, False, AnyPropertyType,
3110 &i_format, &i_items, &i_bytesafter,
3111 (unsigned char **)&p_args );
3113 if( i_ret != Success || i_items == 0 ) return;
3115 msg_Dbg( p_vout, "Window manager supports NetWM" );
3117 p_vout->p_sys->net_wm_state =
3118 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3119 p_vout->p_sys->net_wm_state_fullscreen =
3120 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3122 p_vout->p_sys->net_wm_state_above =
3123 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3124 p_vout->p_sys->net_wm_state_below =
3125 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3126 p_vout->p_sys->net_wm_state_stays_on_top =
3127 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3130 for( i = 0; i < i_items; i++ )
3132 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3135 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3136 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
3138 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3140 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3141 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
3143 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3145 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3146 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
3148 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3151 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3152 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
3156 XFree( p_args.p_atom );
3159 /*****************************************************************************
3160 * Key events handling
3161 *****************************************************************************/
3167 } x11keys_to_vlckeys[] =
3169 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3170 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3171 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3172 { XK_F12, KEY_F12 },
3174 { XK_Return, KEY_ENTER },
3175 { XK_KP_Enter, KEY_ENTER },
3176 { XK_space, KEY_SPACE },
3177 { XK_Escape, KEY_ESC },
3179 { XK_Menu, KEY_MENU },
3180 { XK_Left, KEY_LEFT },
3181 { XK_Right, KEY_RIGHT },
3183 { XK_Down, KEY_DOWN },
3185 { XK_Home, KEY_HOME },
3186 { XK_End, KEY_END },
3187 { XK_Page_Up, KEY_PAGEUP },
3188 { XK_Page_Down, KEY_PAGEDOWN },
3190 { XK_Insert, KEY_INSERT },
3191 { XK_Delete, KEY_DELETE },
3192 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3193 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3194 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3195 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3196 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3197 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3198 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3203 static int ConvertKey( int i_key )
3207 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3209 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3211 return x11keys_to_vlckeys[i].i_vlckey;
3218 /*****************************************************************************
3219 * WindowOnTop: Switches the "always on top" state of the video window.
3220 *****************************************************************************/
3221 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
3223 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3225 XClientMessageEvent event;
3227 memset( &event, 0, sizeof( XClientMessageEvent ) );
3229 event.type = ClientMessage;
3230 event.message_type = p_vout->p_sys->net_wm_state;
3231 event.display = p_vout->p_sys->p_display;
3232 event.window = p_vout->p_sys->p_win->base_window;
3234 event.data.l[ 0 ] = b_on_top; /* set property */
3235 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3237 XSendEvent( p_vout->p_sys->p_display,
3238 DefaultRootWindow( p_vout->p_sys->p_display ),
3239 False, SubstructureRedirectMask,
3243 /* use _NET_WM_STATE_ABOVE if window manager
3244 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3245 else if( p_vout->p_sys->b_net_wm_state_above )
3247 XClientMessageEvent event;
3249 memset( &event, 0, sizeof( XClientMessageEvent ) );
3251 event.type = ClientMessage;
3252 event.message_type = p_vout->p_sys->net_wm_state;
3253 event.display = p_vout->p_sys->p_display;
3254 event.window = p_vout->p_sys->p_win->base_window;
3256 event.data.l[ 0 ] = b_on_top; /* set property */
3257 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3259 XSendEvent( p_vout->p_sys->p_display,
3260 DefaultRootWindow( p_vout->p_sys->p_display ),
3261 False, SubstructureRedirectMask,