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 *****************************************************************************/
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
40 #include <errno.h> /* ENOMEM */
42 #ifdef HAVE_MACHINE_PARAM_H
44 # include <machine/param.h>
45 # include <sys/types.h> /* typedef ushort */
50 # include <netinet/in.h> /* BSD: struct in_addr */
54 #include <X11/extensions/Xsp.h>
58 # include <sys/shm.h> /* shmget(), shmctl() */
62 #include <X11/Xproto.h>
64 #include <X11/Xutil.h>
65 #include <X11/keysym.h>
66 #include <X11/XF86keysym.h>
68 # include <X11/extensions/XShm.h>
70 #ifdef DPMSINFO_IN_DPMS_H
71 # include <X11/extensions/dpms.h>
74 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
75 # include <X11/extensions/Xv.h>
76 # include <X11/extensions/Xvlib.h>
79 #ifdef MODULE_NAME_IS_glx
84 # include <X11/extensions/Xinerama.h>
87 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
88 # include <X11/extensions/xf86vmode.h>
91 #ifdef MODULE_NAME_IS_xvmc
92 # include <X11/extensions/vldXvMC.h>
93 # include "../../codec/xvmc/accel_xvmc.h"
98 /*****************************************************************************
100 *****************************************************************************/
101 int E_(Activate) ( vlc_object_t * );
102 void E_(Deactivate) ( vlc_object_t * );
104 static int InitVideo ( vout_thread_t * );
105 static void EndVideo ( vout_thread_t * );
106 static void DisplayVideo ( vout_thread_t *, picture_t * );
107 static int ManageVideo ( vout_thread_t * );
108 static int Control ( vout_thread_t *, int, va_list );
110 static int InitDisplay ( vout_thread_t * );
112 static int CreateWindow ( vout_thread_t *, x11_window_t * );
113 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
115 static int NewPicture ( vout_thread_t *, picture_t * );
116 static void FreePicture ( vout_thread_t *, picture_t * );
118 #ifndef MODULE_NAME_IS_glx
119 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
120 Display *, EXTRA_ARGS, int, int );
123 #ifdef HAVE_SYS_SHM_H
124 #ifndef MODULE_NAME_IS_glx
125 IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
126 Display *, EXTRA_ARGS_SHM, int, int );
128 static int i_shm_major = 0;
131 static void ToggleFullScreen ( vout_thread_t * );
133 static void EnableXScreenSaver ( vout_thread_t * );
134 static void DisableXScreenSaver ( vout_thread_t * );
136 static void CreateCursor ( vout_thread_t * );
137 static void DestroyCursor ( vout_thread_t * );
138 static void ToggleCursor ( vout_thread_t * );
140 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
141 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
142 static void XVideoReleasePort( vout_thread_t *, int );
145 #ifdef MODULE_NAME_IS_x11
146 static void SetPalette ( vout_thread_t *,
147 uint16_t *, uint16_t *, uint16_t * );
150 #ifdef MODULE_NAME_IS_xvmc
151 static void RenderVideo ( vout_thread_t *, picture_t * );
152 static int xvmc_check_yv12( Display *display, XvPortID port );
153 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
156 static void TestNetWMSupport( vout_thread_t * );
157 static int ConvertKey( int );
159 static int WindowOnTop( vout_thread_t *, bool );
161 static int X11ErrorHandler( Display *, XErrorEvent * );
164 static void EnablePixelDoubling( vout_thread_t *p_vout );
165 static void DisablePixelDoubling( vout_thread_t *p_vout );
169 static const int i_backlight_on_interval = 300;
174 /*****************************************************************************
175 * Activate: allocate X11 video thread output method
176 *****************************************************************************
177 * This function allocate and initialize a X11 vout method. It uses some of the
178 * vout properties to choose the window size, and change them according to the
179 * actual properties of the display.
180 *****************************************************************************/
181 int E_(Activate) ( vlc_object_t *p_this )
183 vout_thread_t *p_vout = (vout_thread_t *)p_this;
186 #if defined(MODULE_NAME_IS_xvmc)
189 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
191 vlc_fourcc_t i_chroma = 0;
195 p_vout->pf_init = InitVideo;
196 p_vout->pf_end = EndVideo;
197 p_vout->pf_manage = ManageVideo;
198 #ifdef MODULE_NAME_IS_xvmc
199 p_vout->pf_render = RenderVideo;
201 p_vout->pf_render = NULL;
203 p_vout->pf_display = DisplayVideo;
204 p_vout->pf_control = Control;
206 /* Allocate structure */
207 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
208 if( p_vout->p_sys == NULL )
210 msg_Err( p_vout, "out of memory" );
214 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
216 /* Open display, using the "display" config variable or the DISPLAY
217 * environment variable */
218 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
220 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
222 if( p_vout->p_sys->p_display == NULL ) /* error */
224 msg_Err( p_vout, "cannot open display %s",
225 XDisplayName( psz_display ) );
226 free( p_vout->p_sys );
232 /* Replace error handler so we can intercept some non-fatal errors */
233 XSetErrorHandler( X11ErrorHandler );
235 /* Get a screen ID matching the XOpenDisplay return value */
236 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
238 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
239 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
242 if( strlen( psz_chroma ) >= 4 )
244 /* Do not use direct assignment because we are not sure of the
246 memcpy(&i_chroma, psz_chroma, 4);
255 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
256 i_chroma, (char*)&i_chroma );
260 i_chroma = p_vout->render.i_chroma;
263 /* Check that we have access to an XVideo port providing this chroma */
264 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
265 &p_vout->output.i_chroma );
266 if( p_vout->p_sys->i_xvport < 0 )
268 /* If a specific chroma format was requested, then we don't try to
269 * be cleverer than the user. He knew pretty well what he wanted. */
272 XCloseDisplay( p_vout->p_sys->p_display );
273 free( p_vout->p_sys );
277 /* It failed, but it's not completely lost ! We try to open an
278 * XVideo port for an YUY2 picture. We'll need to do an YUV
279 * conversion, but at least it has got scaling. */
280 p_vout->p_sys->i_xvport =
281 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
282 &p_vout->output.i_chroma );
283 if( p_vout->p_sys->i_xvport < 0 )
285 /* It failed, but it's not completely lost ! We try to open an
286 * XVideo port for a simple 16bpp RGB picture. We'll need to do
287 * an YUV conversion, but at least it has got scaling. */
288 p_vout->p_sys->i_xvport =
289 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
290 &p_vout->output.i_chroma );
291 if( p_vout->p_sys->i_xvport < 0 )
293 XCloseDisplay( p_vout->p_sys->p_display );
294 free( p_vout->p_sys );
299 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
300 #elif defined(MODULE_NAME_IS_glx)
302 int i_opcode, i_evt, i_err = 0;
303 int i_maj, i_min = 0;
305 /* Check for GLX extension */
306 if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
307 &i_opcode, &i_evt, &i_err ) )
309 msg_Err( p_this, "GLX extension not supported" );
310 XCloseDisplay( p_vout->p_sys->p_display );
311 free( p_vout->p_sys );
314 if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
316 msg_Err( p_this, "glXQueryExtension failed" );
317 XCloseDisplay( p_vout->p_sys->p_display );
318 free( p_vout->p_sys );
322 /* Check GLX version */
323 if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
325 msg_Err( p_this, "glXQueryVersion failed" );
326 XCloseDisplay( p_vout->p_sys->p_display );
327 free( p_vout->p_sys );
330 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
332 p_vout->p_sys->b_glx13 = false;
333 msg_Dbg( p_this, "using GLX 1.2 API" );
337 p_vout->p_sys->b_glx13 = true;
338 msg_Dbg( p_this, "using GLX 1.3 API" );
343 /* Create blank cursor (for mouse cursor autohiding) */
344 p_vout->p_sys->i_time_mouse_last_moved = mdate();
345 p_vout->p_sys->i_mouse_hide_timeout =
346 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
347 p_vout->p_sys->b_mouse_pointer_visible = 1;
348 CreateCursor( p_vout );
350 /* Set main window's size */
351 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
352 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
353 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
354 /* Spawn base window - this window will include the video output window,
355 * but also command buttons, subtitles and other indicators */
356 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
358 msg_Err( p_vout, "cannot create X11 window" );
359 DestroyCursor( p_vout );
360 XCloseDisplay( p_vout->p_sys->p_display );
361 free( p_vout->p_sys );
365 /* Open and initialize device. */
366 if( InitDisplay( p_vout ) )
368 msg_Err( p_vout, "cannot initialize X11 display" );
369 DestroyCursor( p_vout );
370 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
371 XCloseDisplay( p_vout->p_sys->p_display );
372 free( p_vout->p_sys );
376 /* Disable screen saver */
377 DisableXScreenSaver( p_vout );
380 p_vout->p_sys->b_altfullscreen = 0;
381 p_vout->p_sys->i_time_button_last_pressed = 0;
383 TestNetWMSupport( p_vout );
385 #ifdef MODULE_NAME_IS_xvmc
386 p_vout->p_sys->p_last_subtitle_save = NULL;
387 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
389 /* Look what method was requested */
390 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
391 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
394 if( (strcmp(psz_value, "bob") == 0) ||
395 (strcmp(psz_value, "blend") == 0) )
396 p_vout->p_sys->xvmc_deinterlace_method = 2;
397 else if (strcmp(psz_value, "discard") == 0)
398 p_vout->p_sys->xvmc_deinterlace_method = 1;
400 p_vout->p_sys->xvmc_deinterlace_method = 0;
404 p_vout->p_sys->xvmc_deinterlace_method = 0;
406 /* Look what method was requested */
407 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
408 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
409 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
413 if( strncmp( psz_value, "eq", 2 ) == 0 )
414 p_vout->p_sys->xvmc_crop_style = 1;
415 else if( strncmp( psz_value, "4-16", 4 ) == 0)
416 p_vout->p_sys->xvmc_crop_style = 2;
417 else if( strncmp( psz_value, "16-4", 4 ) == 0)
418 p_vout->p_sys->xvmc_crop_style = 3;
420 p_vout->p_sys->xvmc_crop_style = 0;
424 p_vout->p_sys->xvmc_crop_style = 0;
426 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
427 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
429 if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
431 msg_Err( p_vout, "no XVMC capability found" );
432 E_(Deactivate)( p_vout );
435 subpicture_t sub_pic;
436 sub_pic.p_sys = NULL;
437 p_vout->p_sys->last_date = 0;
441 p_vout->p_sys->i_hw_scale = 1;
445 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
446 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
447 if ( p_vout->p_sys->p_octx == NULL ) {
448 msg_Err( p_vout, "Could not get osso context" );
450 msg_Dbg( p_vout, "Initialized osso context" );
454 /* Variable to indicate if the window should be on top of others */
455 /* Trigger a callback right now */
456 var_Get( p_vout, "video-on-top", &val );
457 var_Set( p_vout, "video-on-top", val );
462 /*****************************************************************************
463 * Deactivate: destroy X11 video thread output method
464 *****************************************************************************
465 * Terminate an output method created by Open
466 *****************************************************************************/
467 void E_(Deactivate) ( vlc_object_t *p_this )
469 vout_thread_t *p_vout = (vout_thread_t *)p_this;
471 /* If the fullscreen window is still open, close it */
472 if( p_vout->b_fullscreen )
474 ToggleFullScreen( p_vout );
477 /* Restore cursor if it was blanked */
478 if( !p_vout->p_sys->b_mouse_pointer_visible )
480 ToggleCursor( p_vout );
483 #ifdef MODULE_NAME_IS_x11
484 /* Destroy colormap */
485 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
487 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
489 #elif defined(MODULE_NAME_IS_xvideo)
490 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
491 #elif defined(MODULE_NAME_IS_xvmc)
492 if( p_vout->p_sys->xvmc_cap )
494 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
495 xxmc_dispose_context( p_vout );
496 if( p_vout->p_sys->old_subpic )
498 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
499 p_vout->p_sys->old_subpic = NULL;
501 if( p_vout->p_sys->new_subpic )
503 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
504 p_vout->p_sys->new_subpic = NULL;
506 free( p_vout->p_sys->xvmc_cap );
507 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
512 DisablePixelDoubling(p_vout);
515 DestroyCursor( p_vout );
516 EnableXScreenSaver( p_vout );
517 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
518 XCloseDisplay( p_vout->p_sys->p_display );
520 /* Destroy structure */
521 vlc_mutex_destroy( &p_vout->p_sys->lock );
522 #ifdef MODULE_NAME_IS_xvmc
523 free_context_lock( &p_vout->p_sys->xvmc_lock );
527 if ( p_vout->p_sys->p_octx != NULL ) {
528 msg_Dbg( p_vout, "Deinitializing osso context" );
529 osso_deinitialize( p_vout->p_sys->p_octx );
533 free( p_vout->p_sys );
536 #ifdef MODULE_NAME_IS_xvmc
538 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
541 static int xvmc_check_yv12( Display *display, XvPortID port )
543 XvImageFormatValues *formatValues;
547 formatValues = XvListImageFormats( display, port, &formats );
549 for( i = 0; i < formats; i++ )
551 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
552 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
554 XFree (formatValues);
559 XFree (formatValues);
563 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
565 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
568 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
571 int xv_double_buffer;
573 xv_double_buffer = 1;
575 XLockDisplay( p_vout->p_sys->p_display );
576 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
578 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
580 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
581 XUnlockDisplay( p_vout->p_sys->p_display );
583 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
584 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
587 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
589 vlc_xxmc_t *xxmc = NULL;
591 vlc_mutex_lock( &p_vout->p_sys->lock );
592 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
594 xxmc = &p_pic->p_sys->xxmc_data;
595 if( (!xxmc->decoded ||
596 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
598 vlc_mutex_unlock( &p_vout->p_sys->lock );
599 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
604 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
605 if (p_vout->p_sys->p_last_subtitle != NULL)
607 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
609 p_vout->p_sys->new_subpic =
610 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
611 p_vout->p_sys->xvmc_width,
612 p_vout->p_sys->xvmc_height,
613 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
615 if (p_vout->p_sys->new_subpic)
617 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
618 XvMCClearSubpicture( p_vout->p_sys->p_display,
619 p_vout->p_sys->new_subpic,
622 p_vout->p_sys->xvmc_width,
623 p_vout->p_sys->xvmc_height,
625 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
626 clear_xx44_palette( &p_vout->p_sys->palette );
628 if( sub_pic.p_sys == NULL )
630 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
631 if( sub_pic.p_sys != NULL )
633 sub_pic.p_sys->p_vout = p_vout;
634 sub_pic.p_sys->xvmc_surf = NULL;
635 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
638 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
639 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
640 sub_pic.p->i_pitch = p_vout->output.i_width;
642 memset( p_vout->p_sys->subImage->data, 0,
643 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
645 if (p_vout->p_last_subtitle != NULL)
647 blend_xx44( p_vout->p_sys->subImage->data,
648 p_vout->p_last_subtitle,
649 p_vout->p_sys->subImage->width,
650 p_vout->p_sys->subImage->height,
651 p_vout->p_sys->subImage->width,
652 &p_vout->p_sys->palette,
653 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
656 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
657 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
658 p_vout->p_sys->new_subpic,
659 p_vout->p_sys->subImage,
662 p_vout->output.i_width, /* overlay->width, */
663 p_vout->output.i_height, /* overlay->height */
666 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
667 if (p_vout->p_sys->old_subpic)
669 xxmc_xvmc_free_subpicture( p_vout,
670 p_vout->p_sys->old_subpic);
671 p_vout->p_sys->old_subpic = NULL;
673 if (p_vout->p_sys->new_subpic)
675 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
676 p_vout->p_sys->new_subpic = NULL;
677 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
678 p_vout->p_sys->xvmc_palette,
680 p_vout->p_sys->old_subpic->num_palette_entries,
681 p_vout->p_sys->old_subpic->entry_bytes,
682 p_vout->p_sys->old_subpic->component_order );
683 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
684 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
685 p_vout->p_sys->old_subpic,
686 p_vout->p_sys->xvmc_palette );
687 XvMCFlushSubpicture( p_vout->p_sys->p_display,
688 p_vout->p_sys->old_subpic);
689 XvMCSyncSubpicture( p_vout->p_sys->p_display,
690 p_vout->p_sys->old_subpic );
691 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
694 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
695 if (p_vout->p_sys->xvmc_backend_subpic )
697 XvMCBlendSubpicture( p_vout->p_sys->p_display,
698 p_pic->p_sys->xvmc_surf,
699 p_vout->p_sys->old_subpic,
702 p_vout->p_sys->xvmc_width,
703 p_vout->p_sys->xvmc_height,
706 p_vout->p_sys->xvmc_width,
707 p_vout->p_sys->xvmc_height );
711 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
712 p_pic->p_sys->xvmc_surf,
713 p_pic->p_sys->xvmc_surf,
714 p_vout->p_sys->old_subpic,
717 p_vout->p_sys->xvmc_width,
718 p_vout->p_sys->xvmc_height,
721 p_vout->p_sys->xvmc_width,
722 p_vout->p_sys->xvmc_height );
724 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
729 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
730 if( p_vout->p_sys->xvmc_backend_subpic )
732 XvMCBlendSubpicture( p_vout->p_sys->p_display,
733 p_pic->p_sys->xvmc_surf,
734 p_vout->p_sys->old_subpic,
736 p_vout->p_sys->xvmc_width,
737 p_vout->p_sys->xvmc_height,
739 p_vout->p_sys->xvmc_width,
740 p_vout->p_sys->xvmc_height );
744 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
745 p_pic->p_sys->xvmc_surf,
746 p_pic->p_sys->xvmc_surf,
747 p_vout->p_sys->old_subpic,
749 p_vout->p_sys->xvmc_width,
750 p_vout->p_sys->xvmc_height,
752 p_vout->p_sys->xvmc_width,
753 p_vout->p_sys->xvmc_height );
755 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
758 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
760 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
762 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
764 vlc_mutex_unlock( &p_vout->p_sys->lock );
769 /*****************************************************************************
770 * EnablePixelDoubling: Enables pixel doubling
771 *****************************************************************************
772 * Checks if the double size image fits in current window, and enables pixel
773 * doubling accordingly. The i_hw_scale is the integer scaling factor.
774 *****************************************************************************/
775 static void EnablePixelDoubling( vout_thread_t *p_vout )
777 int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
778 int i_vert_scale = ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
779 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
780 p_vout->p_sys->i_hw_scale = 2;
781 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
782 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
786 /*****************************************************************************
787 * DisablePixelDoubling: Disables pixel doubling
788 *****************************************************************************
789 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
790 *****************************************************************************/
791 static void DisablePixelDoubling( vout_thread_t *p_vout )
793 if ( p_vout->p_sys->i_hw_scale > 1 ) {
794 msg_Dbg( p_vout, "Disabling pixel doubling" );
795 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
796 p_vout->p_sys->i_hw_scale = 1;
803 /*****************************************************************************
804 * InitVideo: initialize X11 video thread output method
805 *****************************************************************************
806 * This function create the XImages needed by the output thread. It is called
807 * at the beginning of the thread, but also each time the window is resized.
808 *****************************************************************************/
809 static int InitVideo( vout_thread_t *p_vout )
811 unsigned int i_index = 0;
814 I_OUTPUTPICTURES = 0;
816 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
817 /* Initialize the output structure; we already found an XVideo port,
818 * and the corresponding chroma we will be using. Since we can
819 * arbitrary scale, stick to the coordinates and aspect. */
820 p_vout->output.i_width = p_vout->render.i_width;
821 p_vout->output.i_height = p_vout->render.i_height;
822 p_vout->output.i_aspect = p_vout->render.i_aspect;
824 p_vout->fmt_out = p_vout->fmt_in;
825 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
827 switch( p_vout->output.i_chroma )
829 case VLC_FOURCC('R','V','1','6'):
830 #if defined( WORDS_BIGENDIAN )
831 p_vout->output.i_rmask = 0xf800;
832 p_vout->output.i_gmask = 0x07e0;
833 p_vout->output.i_bmask = 0x001f;
835 p_vout->output.i_rmask = 0x001f;
836 p_vout->output.i_gmask = 0x07e0;
837 p_vout->output.i_bmask = 0xf800;
840 case VLC_FOURCC('R','V','1','5'):
841 #if defined( WORDS_BIGENDIAN )
842 p_vout->output.i_rmask = 0x7c00;
843 p_vout->output.i_gmask = 0x03e0;
844 p_vout->output.i_bmask = 0x001f;
846 p_vout->output.i_rmask = 0x001f;
847 p_vout->output.i_gmask = 0x03e0;
848 p_vout->output.i_bmask = 0x7c00;
853 #elif defined(MODULE_NAME_IS_x11)
854 /* Initialize the output structure: RGB with square pixels, whatever
855 * the input format is, since it's the only format we know */
856 switch( p_vout->p_sys->i_screen_depth )
858 case 8: /* FIXME: set the palette */
859 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
861 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
863 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
866 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
868 msg_Err( p_vout, "unknown screen depth %i",
869 p_vout->p_sys->i_screen_depth );
874 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width / p_vout->p_sys->i_hw_scale,
875 p_vout->p_sys->p_win->i_height / p_vout->p_sys->i_hw_scale,
877 &p_vout->fmt_out.i_visible_width,
878 &p_vout->fmt_out.i_visible_height );
880 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
881 p_vout->p_sys->p_win->i_height,
883 &p_vout->fmt_out.i_visible_width,
884 &p_vout->fmt_out.i_visible_height );
887 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
889 p_vout->output.i_width = p_vout->fmt_out.i_width =
890 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
891 p_vout->fmt_in.i_visible_width;
892 p_vout->output.i_height = p_vout->fmt_out.i_height =
893 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
894 p_vout->fmt_in.i_visible_height;
895 p_vout->fmt_out.i_x_offset =
896 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
897 p_vout->fmt_in.i_visible_width;
898 p_vout->fmt_out.i_y_offset =
899 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
900 p_vout->fmt_in.i_visible_height;
902 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
903 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
904 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
906 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
907 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
908 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
909 p_vout->fmt_out.i_visible_width,
910 p_vout->fmt_out.i_visible_height );
913 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
914 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
918 /* Find an empty picture slot */
919 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
921 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
923 p_pic = p_vout->p_picture + i_index;
928 /* Allocate the picture */
929 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
934 p_pic->i_status = DESTROYED_PICTURE;
935 p_pic->i_type = DIRECT_PICTURE;
937 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
942 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
944 /* U and V inverted compared to I420
945 * Fixme: this should be handled by the vout core */
946 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
947 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
953 /*****************************************************************************
954 * DisplayVideo: displays previously rendered output
955 *****************************************************************************
956 * This function sends the currently rendered image to X11 server.
957 * (The Xv extension takes care of "double-buffering".)
958 *****************************************************************************/
959 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
961 unsigned int i_width, i_height, i_x, i_y;
963 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
964 p_vout->p_sys->p_win->i_height,
965 &i_x, &i_y, &i_width, &i_height );
967 vlc_mutex_lock( &p_vout->p_sys->lock );
969 #ifdef MODULE_NAME_IS_xvmc
970 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
972 vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
973 if( !xxmc->decoded ||
974 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
976 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
978 xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
979 vlc_mutex_unlock( &p_vout->p_sys->lock );
980 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
984 int src_width = p_vout->output.i_width;
985 int src_height = p_vout->output.i_height;
988 if( p_vout->p_sys->xvmc_crop_style == 1 )
995 else if( p_vout->p_sys->xvmc_crop_style == 2 )
1002 else if( p_vout->p_sys->xvmc_crop_style == 3 )
1016 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1017 { /* BOB DEINTERLACE */
1018 if( (p_pic->p_sys->nb_display == 0) ||
1019 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1021 first_field = (p_pic->b_top_field_first) ?
1022 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1026 first_field = (p_pic->b_top_field_first) ?
1027 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1032 first_field = XVMC_FRAME_PICTURE;
1035 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1036 XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
1037 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1038 XvMCPutSurface( p_vout->p_sys->p_display,
1039 p_pic->p_sys->xvmc_surf,
1040 p_vout->p_sys->p_win->video_window,
1051 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1052 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1053 { /* BOB DEINTERLACE */
1054 if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1056 mtime_t last_date = p_pic->date;
1058 vlc_mutex_lock( &p_vout->picture_lock );
1059 if( !p_vout->p_sys->last_date )
1061 p_pic->date += 20000;
1065 p_pic->date = ((3 * p_pic->date -
1066 p_vout->p_sys->last_date) / 2 );
1068 p_vout->p_sys->last_date = last_date;
1070 p_pic->p_sys->nb_display = 1;
1071 vlc_mutex_unlock( &p_vout->picture_lock );
1075 p_pic->p_sys->nb_display = 0;
1079 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1082 #ifdef HAVE_SYS_SHM_H
1083 if( p_vout->p_sys->i_shm_opcode )
1085 /* Display rendered image using shared memory extension */
1086 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1087 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1088 p_vout->p_sys->p_win->video_window,
1089 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1090 p_vout->fmt_out.i_x_offset,
1091 p_vout->fmt_out.i_y_offset,
1092 p_vout->fmt_out.i_visible_width,
1093 p_vout->fmt_out.i_visible_height,
1094 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1095 False /* Don't put True here or you'll waste your CPU */ );
1097 XShmPutImage( p_vout->p_sys->p_display,
1098 p_vout->p_sys->p_win->video_window,
1099 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1100 p_vout->fmt_out.i_x_offset,
1101 p_vout->fmt_out.i_y_offset,
1102 0 /*dest_x*/, 0 /*dest_y*/,
1103 p_vout->fmt_out.i_visible_width,
1104 p_vout->fmt_out.i_visible_height,
1105 False /* Don't put True here ! */ );
1109 #endif /* HAVE_SYS_SHM_H */
1111 /* Use standard XPutImage -- this is gonna be slow ! */
1112 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1113 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1114 p_vout->p_sys->p_win->video_window,
1115 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1116 p_vout->fmt_out.i_x_offset,
1117 p_vout->fmt_out.i_y_offset,
1118 p_vout->fmt_out.i_visible_width,
1119 p_vout->fmt_out.i_visible_height,
1120 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1122 XPutImage( p_vout->p_sys->p_display,
1123 p_vout->p_sys->p_win->video_window,
1124 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1125 p_vout->fmt_out.i_x_offset,
1126 p_vout->fmt_out.i_y_offset,
1127 0 /*dest_x*/, 0 /*dest_y*/,
1128 p_vout->fmt_out.i_visible_width,
1129 p_vout->fmt_out.i_visible_height );
1133 /* Make sure the command is sent now - do NOT use XFlush !*/
1134 XSync( p_vout->p_sys->p_display, False );
1136 vlc_mutex_unlock( &p_vout->p_sys->lock );
1139 /*****************************************************************************
1140 * ManageVideo: handle X11 events
1141 *****************************************************************************
1142 * This function should be called regularly by video output thread. It manages
1143 * X11 events and allows window resizing. It returns a non null value on
1145 *****************************************************************************/
1146 static int ManageVideo( vout_thread_t *p_vout )
1148 XEvent xevent; /* X11 event */
1151 vlc_mutex_lock( &p_vout->p_sys->lock );
1153 #ifdef MODULE_NAME_IS_xvmc
1154 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1157 /* Handle events from the owner window */
1158 if( p_vout->p_sys->p_win->owner_window )
1160 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1161 p_vout->p_sys->p_win->owner_window,
1162 StructureNotifyMask, &xevent ) == True )
1164 /* ConfigureNotify event: prepare */
1165 if( xevent.type == ConfigureNotify )
1167 /* Update dimensions */
1168 XResizeWindow( p_vout->p_sys->p_display,
1169 p_vout->p_sys->p_win->base_window,
1170 xevent.xconfigure.width,
1171 xevent.xconfigure.height );
1176 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1177 * output window's size changed, MapNotify and UnmapNotify to know if the
1178 * window is mapped (and if the display is useful), and ClientMessages
1179 * to intercept window destruction requests */
1181 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1182 p_vout->p_sys->p_win->base_window,
1183 StructureNotifyMask | KeyPressMask |
1184 ButtonPressMask | ButtonReleaseMask |
1185 PointerMotionMask | Button1MotionMask , &xevent )
1188 /* ConfigureNotify event: prepare */
1189 if( xevent.type == ConfigureNotify )
1191 if( (unsigned int)xevent.xconfigure.width
1192 != p_vout->p_sys->p_win->i_width
1193 || (unsigned int)xevent.xconfigure.height
1194 != p_vout->p_sys->p_win->i_height )
1196 /* Update dimensions */
1197 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1198 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1199 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1202 /* Keyboard event */
1203 else if( xevent.type == KeyPress )
1205 unsigned int state = xevent.xkey.state;
1206 KeySym x_key_symbol;
1207 char i_key; /* ISO Latin-1 key */
1209 /* We may have keys like F1 trough F12, ESC ... */
1210 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1211 xevent.xkey.keycode, 0 );
1212 val.i_int = ConvertKey( (int)x_key_symbol );
1214 xevent.xkey.state &= ~ShiftMask;
1215 xevent.xkey.state &= ~ControlMask;
1216 xevent.xkey.state &= ~Mod1Mask;
1219 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1222 * The reason why I use this instead of XK_0 is that
1223 * with XLookupString, we don't have to care about
1230 if( state & ShiftMask )
1232 val.i_int |= KEY_MODIFIER_SHIFT;
1234 if( state & ControlMask )
1236 val.i_int |= KEY_MODIFIER_CTRL;
1238 if( state & Mod1Mask )
1240 val.i_int |= KEY_MODIFIER_ALT;
1242 var_Set( p_vout->p_libvlc, "key-pressed", val );
1246 else if( xevent.type == ButtonPress )
1248 switch( ((XButtonEvent *)&xevent)->button )
1251 var_Get( p_vout, "mouse-button-down", &val );
1253 var_Set( p_vout, "mouse-button-down", val );
1255 /* detect double-clicks */
1256 if( ( ((XButtonEvent *)&xevent)->time -
1257 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1259 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1262 p_vout->p_sys->i_time_button_last_pressed =
1263 ((XButtonEvent *)&xevent)->time;
1266 var_Get( p_vout, "mouse-button-down", &val );
1268 var_Set( p_vout, "mouse-button-down", val );
1272 var_Get( p_vout, "mouse-button-down", &val );
1274 var_Set( p_vout, "mouse-button-down", val );
1278 var_Get( p_vout, "mouse-button-down", &val );
1280 var_Set( p_vout, "mouse-button-down", val );
1284 var_Get( p_vout, "mouse-button-down", &val );
1286 var_Set( p_vout, "mouse-button-down", val );
1291 else if( xevent.type == ButtonRelease )
1293 switch( ((XButtonEvent *)&xevent)->button )
1296 var_Get( p_vout, "mouse-button-down", &val );
1298 var_Set( p_vout, "mouse-button-down", val );
1301 var_Set( p_vout, "mouse-clicked", val );
1306 playlist_t *p_playlist;
1308 var_Get( p_vout, "mouse-button-down", &val );
1310 var_Set( p_vout, "mouse-button-down", val );
1312 p_playlist = vlc_object_find( p_vout,
1313 VLC_OBJECT_PLAYLIST,
1315 if( p_playlist != NULL )
1318 var_Get( p_playlist, "intf-show", &val );
1319 val.b_bool = !val.b_bool;
1320 var_Set( p_playlist, "intf-show", val );
1321 vlc_object_release( p_playlist );
1328 intf_thread_t *p_intf;
1329 playlist_t *p_playlist;
1331 var_Get( p_vout, "mouse-button-down", &val );
1333 var_Set( p_vout, "mouse-button-down", val );
1334 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
1338 p_intf->b_menu_change = 1;
1339 vlc_object_release( p_intf );
1342 p_playlist = vlc_object_find( p_vout,
1343 VLC_OBJECT_PLAYLIST,
1345 if( p_playlist != NULL )
1347 vlc_value_t val; val.b_bool = true;
1348 var_Set( p_playlist, "intf-popupmenu", val );
1349 vlc_object_release( p_playlist );
1355 var_Get( p_vout, "mouse-button-down", &val );
1357 var_Set( p_vout, "mouse-button-down", val );
1361 var_Get( p_vout, "mouse-button-down", &val );
1363 var_Set( p_vout, "mouse-button-down", val );
1369 else if( xevent.type == MotionNotify )
1371 unsigned int i_width, i_height, i_x, i_y;
1374 /* somewhat different use for vout_PlacePicture:
1375 * here the values are needed to give to mouse coordinates
1376 * in the original picture space */
1377 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1378 p_vout->p_sys->p_win->i_height,
1379 &i_x, &i_y, &i_width, &i_height );
1381 /* Compute the x coordinate and check if the value is
1382 in [0,p_vout->fmt_in.i_visible_width] */
1383 val.i_int = ( xevent.xmotion.x - i_x ) *
1384 p_vout->fmt_in.i_visible_width / i_width +
1385 p_vout->fmt_in.i_x_offset;
1387 if( (int)(xevent.xmotion.x - i_x) < 0 )
1389 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1390 val.i_int = p_vout->fmt_in.i_visible_width;
1392 var_Set( p_vout, "mouse-x", val );
1394 /* compute the y coordinate and check if the value is
1395 in [0,p_vout->fmt_in.i_visible_height] */
1396 val.i_int = ( xevent.xmotion.y - i_y ) *
1397 p_vout->fmt_in.i_visible_height / i_height +
1398 p_vout->fmt_in.i_y_offset;
1400 if( (int)(xevent.xmotion.y - i_y) < 0 )
1402 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1403 val.i_int = p_vout->fmt_in.i_visible_height;
1405 var_Set( p_vout, "mouse-y", val );
1408 var_Set( p_vout, "mouse-moved", val );
1410 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1411 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1413 ToggleCursor( p_vout );
1416 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1417 || xevent.type == MapNotify
1418 || xevent.type == UnmapNotify )
1420 /* Ignore these events */
1422 else /* Other events */
1424 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1428 /* Handle events for video output sub-window */
1429 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1430 p_vout->p_sys->p_win->video_window,
1431 ExposureMask, &xevent ) == True )
1433 /* Window exposed (only handled if stream playback is paused) */
1434 if( xevent.type == Expose )
1436 if( ((XExposeEvent *)&xevent)->count == 0 )
1438 /* (if this is the last a collection of expose events...) */
1440 #if defined(MODULE_NAME_IS_xvideo)
1441 x11_window_t *p_win = p_vout->p_sys->p_win;
1443 /* Paint the colour key if needed */
1444 if( p_vout->p_sys->b_paint_colourkey &&
1445 xevent.xexpose.window == p_win->video_window )
1447 XSetForeground( p_vout->p_sys->p_display,
1448 p_win->gc, p_vout->p_sys->i_colourkey );
1449 XFillRectangle( p_vout->p_sys->p_display,
1450 p_win->video_window, p_win->gc, 0, 0,
1451 p_win->i_width, p_win->i_height );
1456 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1458 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1459 ->stream.control.i_status )
1461 /* XVideoDisplay( p_vout )*/;
1469 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1470 * are handled - according to the man pages, the format is always 32
1472 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1473 ClientMessage, &xevent ) )
1475 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1476 && ((Atom)xevent.xclient.data.l[0]
1477 == p_vout->p_sys->p_win->wm_delete_window ) )
1479 /* the user wants to close the window */
1480 playlist_t * p_playlist =
1481 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1483 if( p_playlist != NULL )
1485 playlist_Stop( p_playlist );
1486 vlc_object_release( p_playlist );
1494 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1496 vlc_value_t val_fs, val_ontop;
1498 /* Update the object variable and trigger callback */
1499 val_fs.b_bool = !p_vout->b_fullscreen;
1501 var_Set( p_vout, "fullscreen", val_fs );
1503 /* Disable "always on top" in fullscreen mode */
1504 var_Get( p_vout, "video-on-top", &val_ontop );
1505 if( val_ontop.b_bool )
1506 WindowOnTop( p_vout, val_fs.b_bool );
1508 ToggleFullScreen( p_vout );
1509 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1512 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1513 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1515 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1516 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1518 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1519 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1520 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1521 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1522 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1523 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1524 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1525 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1527 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1533 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1534 * the size flag inside the fullscreen routine)
1536 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1538 unsigned int i_width, i_height, i_x, i_y;
1540 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1542 #ifdef MODULE_NAME_IS_x11
1543 /* We need to signal the vout thread about the size change because it
1544 * is doing the rescaling */
1545 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1548 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1549 p_vout->p_sys->p_win->i_height,
1550 &i_x, &i_y, &i_width, &i_height );
1552 XMoveResizeWindow( p_vout->p_sys->p_display,
1553 p_vout->p_sys->p_win->video_window,
1554 i_x, i_y, i_width, i_height );
1557 /* Autohide Cursour */
1558 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved >
1559 p_vout->p_sys->i_mouse_hide_timeout )
1561 /* Hide the mouse automatically */
1562 if( p_vout->p_sys->b_mouse_pointer_visible )
1564 ToggleCursor( p_vout );
1568 #ifdef MODULE_NAME_IS_xvmc
1569 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1573 if ( p_vout->p_sys->p_octx != NULL ) {
1574 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1575 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1576 msg_Err( p_vout, "Could not disable backlight blanking" );
1578 msg_Dbg( p_vout, "Backlight blanking disabled" );
1580 p_vout->p_sys->i_backlight_on_counter = 0;
1582 p_vout->p_sys->i_backlight_on_counter ++;
1587 vlc_mutex_unlock( &p_vout->p_sys->lock );
1591 /*****************************************************************************
1592 * EndVideo: terminate X11 video thread output method
1593 *****************************************************************************
1594 * Destroy the X11 XImages created by Init. It is called at the end of
1595 * the thread, but also each time the window is resized.
1596 *****************************************************************************/
1597 static void EndVideo( vout_thread_t *p_vout )
1601 /* Free the direct buffers we allocated */
1602 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1605 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1609 /* following functions are local */
1611 /*****************************************************************************
1612 * CreateWindow: open and set-up X11 main window
1613 *****************************************************************************/
1614 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1616 XSizeHints xsize_hints;
1617 XSetWindowAttributes xwindow_attributes;
1618 XGCValues xgcvalues;
1621 bool b_expose = false;
1622 bool b_configure_notify = false;
1623 bool b_map_notify = false;
1626 /* Prepare window manager hints and properties */
1627 p_win->wm_protocols =
1628 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1629 p_win->wm_delete_window =
1630 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1632 /* Never have a 0-pixel-wide window */
1633 xsize_hints.min_width = 2;
1634 xsize_hints.min_height = 1;
1636 /* Prepare window attributes */
1637 xwindow_attributes.backing_store = Always; /* save the hidden part */
1638 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1639 p_vout->p_sys->i_screen);
1640 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1642 if( !p_vout->b_fullscreen )
1644 p_win->owner_window = (Window)
1645 vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
1646 &p_win->i_width, &p_win->i_height );
1648 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1649 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1650 xsize_hints.flags = PSize | PMinSize;
1652 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1654 xsize_hints.x = p_win->i_x;
1655 xsize_hints.y = p_win->i_y;
1656 xsize_hints.flags |= PPosition;
1661 /* Fullscreen window size and position */
1662 p_win->owner_window = 0;
1663 p_win->i_x = p_win->i_y = 0;
1665 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1667 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1670 if( !p_win->owner_window )
1672 /* Create the window and set hints - the window must receive
1673 * ConfigureNotify events, and until it is displayed, Expose and
1674 * MapNotify events. */
1676 p_win->base_window =
1677 XCreateWindow( p_vout->p_sys->p_display,
1678 DefaultRootWindow( p_vout->p_sys->p_display ),
1679 p_win->i_x, p_win->i_y,
1680 p_win->i_width, p_win->i_height,
1683 CWBackingStore | CWBackPixel | CWEventMask,
1684 &xwindow_attributes );
1686 var_Get( p_vout, "video-title", &val );
1687 if( !val.psz_string || !*val.psz_string )
1689 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1690 #ifdef MODULE_NAME_IS_x11
1691 VOUT_TITLE " (X11 output)"
1692 #elif defined(MODULE_NAME_IS_glx)
1693 VOUT_TITLE " (GLX output)"
1695 VOUT_TITLE " (XVideo output)"
1701 XStoreName( p_vout->p_sys->p_display,
1702 p_win->base_window, val.psz_string );
1704 free( val.psz_string );
1706 if( !p_vout->b_fullscreen )
1708 const char *argv[] = { "vlc", NULL };
1710 /* Set window manager hints and properties: size hints, command,
1711 * window's name, and accepted protocols */
1712 XSetWMNormalHints( p_vout->p_sys->p_display,
1713 p_win->base_window, &xsize_hints );
1714 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1717 if( !var_GetBool( p_vout, "video-deco") )
1720 mwmhints_t mwmhints;
1722 mwmhints.flags = MWM_HINTS_DECORATIONS;
1723 mwmhints.decorations = False;
1725 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1728 XChangeProperty( p_vout->p_sys->p_display,
1730 prop, prop, 32, PropModeReplace,
1731 (unsigned char *)&mwmhints,
1732 PROP_MWM_HINTS_ELEMENTS );
1740 unsigned int dummy4, dummy5;
1742 /* Select events we are interested in. */
1743 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1744 StructureNotifyMask );
1746 /* Get the parent window's geometry information */
1747 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1748 &dummy1, &dummy2, &dummy3,
1753 /* We are already configured */
1754 b_configure_notify = true;
1756 /* From man XSelectInput: only one client at a time can select a
1757 * ButtonPress event, so we need to open a new window anyway. */
1758 p_win->base_window =
1759 XCreateWindow( p_vout->p_sys->p_display,
1760 p_win->owner_window,
1762 p_win->i_width, p_win->i_height,
1764 0, CopyFromParent, 0,
1765 CWBackingStore | CWBackPixel | CWEventMask,
1766 &xwindow_attributes );
1769 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1770 || (p_win->wm_delete_window == None)
1771 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1772 &p_win->wm_delete_window, 1 ) )
1774 /* WM_DELETE_WINDOW is not supported by window manager */
1775 msg_Warn( p_vout, "missing or bad window manager" );
1778 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1779 * event when using functions like XCopyArea */
1780 xgcvalues.graphics_exposures = False;
1781 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1783 GCGraphicsExposures, &xgcvalues );
1785 /* Send orders to server, and wait until window is displayed - three
1786 * events must be received: a MapNotify event, an Expose event allowing
1787 * drawing in the window, and a ConfigureNotify to get the window
1788 * dimensions. Once those events have been received, only
1789 * ConfigureNotify events need to be received. */
1790 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1793 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1794 SubstructureNotifyMask | StructureNotifyMask |
1795 ExposureMask, &xevent);
1796 if( (xevent.type == Expose)
1797 && (xevent.xexpose.window == p_win->base_window) )
1800 /* ConfigureNotify isn't sent if there isn't a window manager.
1801 * Expose should be the last event to be received so it should
1802 * be fine to assume we won't receive it anymore. */
1803 b_configure_notify = true;
1805 else if( (xevent.type == MapNotify)
1806 && (xevent.xmap.window == p_win->base_window) )
1808 b_map_notify = true;
1810 else if( (xevent.type == ConfigureNotify)
1811 && (xevent.xconfigure.window == p_win->base_window) )
1813 b_configure_notify = true;
1814 p_win->i_width = xevent.xconfigure.width;
1815 p_win->i_height = xevent.xconfigure.height;
1817 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1819 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1820 StructureNotifyMask | KeyPressMask |
1821 ButtonPressMask | ButtonReleaseMask |
1822 PointerMotionMask );
1824 #ifdef MODULE_NAME_IS_x11
1825 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1827 /* Allocate a new palette */
1828 p_vout->p_sys->colormap =
1829 XCreateColormap( p_vout->p_sys->p_display,
1830 DefaultRootWindow( p_vout->p_sys->p_display ),
1831 DefaultVisual( p_vout->p_sys->p_display,
1832 p_vout->p_sys->i_screen ),
1835 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1836 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1837 CWColormap, &xwindow_attributes );
1841 /* Create video output sub-window. */
1842 p_win->video_window = XCreateSimpleWindow(
1843 p_vout->p_sys->p_display,
1844 p_win->base_window, 0, 0,
1845 p_win->i_width, p_win->i_height,
1847 BlackPixel( p_vout->p_sys->p_display,
1848 p_vout->p_sys->i_screen ),
1849 WhitePixel( p_vout->p_sys->p_display,
1850 p_vout->p_sys->i_screen ) );
1852 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1853 BlackPixel( p_vout->p_sys->p_display,
1854 p_vout->p_sys->i_screen ) );
1856 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1857 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1860 /* make sure the video window will be centered in the next ManageVideo() */
1861 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1863 /* If the cursor was formerly blank than blank it again */
1864 if( !p_vout->p_sys->b_mouse_pointer_visible )
1866 ToggleCursor( p_vout );
1867 ToggleCursor( p_vout );
1870 /* Do NOT use XFlush here ! */
1871 XSync( p_vout->p_sys->p_display, False );
1873 /* At this stage, the window is open, displayed, and ready to
1875 p_vout->p_sys->p_win = p_win;
1880 /*****************************************************************************
1881 * DestroyWindow: destroy the window
1882 *****************************************************************************
1884 *****************************************************************************/
1885 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1887 /* Do NOT use XFlush here ! */
1888 XSync( p_vout->p_sys->p_display, False );
1890 if( p_win->video_window != None )
1891 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1893 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1895 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1896 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1898 if( p_win->owner_window )
1899 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1902 /*****************************************************************************
1903 * NewPicture: allocate a picture
1904 *****************************************************************************
1905 * Returns 0 on success, -1 otherwise
1906 *****************************************************************************/
1907 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1909 #ifndef MODULE_NAME_IS_glx
1911 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1915 /* We know the chroma, allocate a buffer which will be used
1916 * directly by the decoder */
1917 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1919 if( p_pic->p_sys == NULL )
1924 #ifdef MODULE_NAME_IS_xvmc
1925 p_pic->p_sys->p_vout = p_vout;
1926 p_pic->p_sys->xvmc_surf = NULL;
1927 p_pic->p_sys->xxmc_data.decoded = 0;
1928 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1929 // p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1930 p_pic->p_sys->nb_display = 0;
1933 /* Fill in picture_t fields */
1934 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1935 p_vout->output.i_width, p_vout->output.i_height,
1936 p_vout->output.i_aspect );
1938 #ifdef HAVE_SYS_SHM_H
1939 if( p_vout->p_sys->i_shm_opcode )
1941 /* Create image using XShm extension */
1942 p_pic->p_sys->p_image =
1943 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1944 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1945 p_vout->p_sys->i_xvport,
1946 VLC2X11_FOURCC(p_vout->output.i_chroma),
1948 p_vout->p_sys->p_visual,
1949 p_vout->p_sys->i_screen_depth,
1951 &p_pic->p_sys->shminfo,
1952 p_vout->output.i_width, p_vout->output.i_height );
1955 if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1956 #endif /* HAVE_SYS_SHM_H */
1958 /* Create image without XShm extension */
1959 p_pic->p_sys->p_image =
1960 CreateImage( p_vout, p_vout->p_sys->p_display,
1961 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1962 p_vout->p_sys->i_xvport,
1963 VLC2X11_FOURCC(p_vout->output.i_chroma),
1964 p_pic->format.i_bits_per_pixel,
1966 p_vout->p_sys->p_visual,
1967 p_vout->p_sys->i_screen_depth,
1968 p_vout->p_sys->i_bytes_per_pixel,
1970 p_vout->output.i_width, p_vout->output.i_height );
1972 #ifdef HAVE_SYS_SHM_H
1973 if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1975 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1976 p_vout->p_sys->i_shm_opcode = 0;
1978 #endif /* HAVE_SYS_SHM_H */
1981 if( p_pic->p_sys->p_image == NULL )
1983 free( p_pic->p_sys );
1987 switch( p_vout->output.i_chroma )
1989 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1990 case VLC_FOURCC('I','4','2','0'):
1991 case VLC_FOURCC('Y','V','1','2'):
1992 case VLC_FOURCC('Y','2','1','1'):
1993 case VLC_FOURCC('Y','U','Y','2'):
1994 case VLC_FOURCC('U','Y','V','Y'):
1995 case VLC_FOURCC('R','V','1','5'):
1996 case VLC_FOURCC('R','V','1','6'):
1997 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1998 case VLC_FOURCC('R','V','3','2'):
2000 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
2003 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
2004 + p_pic->p_sys->p_image->offsets[i_plane];
2005 p_pic->p[i_plane].i_pitch =
2006 p_pic->p_sys->p_image->pitches[i_plane];
2008 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
2010 /* U and V inverted compared to I420
2011 * Fixme: this should be handled by the vout core */
2012 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
2013 + p_pic->p_sys->p_image->offsets[2];
2014 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
2015 + p_pic->p_sys->p_image->offsets[1];
2020 case VLC_FOURCC('R','G','B','2'):
2021 case VLC_FOURCC('R','V','1','6'):
2022 case VLC_FOURCC('R','V','1','5'):
2023 case VLC_FOURCC('R','V','2','4'):
2024 case VLC_FOURCC('R','V','3','2'):
2026 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
2027 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
2028 p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
2029 + p_pic->p_sys->p_image->xoffset;
2030 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
2032 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
2033 * properly by vout_InitPicture() */
2034 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
2035 * p_pic->p_sys->p_image->width;
2040 /* Unknown chroma, tell the guy to get lost */
2041 IMAGE_FREE( p_pic->p_sys->p_image );
2042 free( p_pic->p_sys );
2043 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
2044 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
2045 p_pic->i_planes = 0;
2050 VLC_UNUSED(p_vout); VLC_UNUSED(p_pic);
2052 #endif /* !MODULE_NAME_IS_glx */
2057 /*****************************************************************************
2058 * FreePicture: destroy a picture allocated with NewPicture
2059 *****************************************************************************
2060 * Destroy XImage AND associated data. If using Shm, detach shared memory
2061 * segment from server and process, then free it. The XDestroyImage manpage
2062 * says that both the image structure _and_ the data pointed to by the
2063 * image structure are freed, so no need to free p_image->data.
2064 *****************************************************************************/
2065 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2067 /* The order of operations is correct */
2068 #ifdef HAVE_SYS_SHM_H
2069 if( p_vout->p_sys->i_shm_opcode )
2071 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2072 IMAGE_FREE( p_pic->p_sys->p_image );
2074 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2075 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2077 msg_Err( p_vout, "cannot detach shared memory (%m)" );
2083 IMAGE_FREE( p_pic->p_sys->p_image );
2086 #ifdef MODULE_NAME_IS_xvmc
2087 if( p_pic->p_sys->xvmc_surf != NULL )
2089 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2090 p_pic->p_sys->xvmc_surf = NULL;
2094 /* Do NOT use XFlush here ! */
2095 XSync( p_vout->p_sys->p_display, False );
2097 free( p_pic->p_sys );
2100 /*****************************************************************************
2101 * ToggleFullScreen: Enable or disable full screen mode
2102 *****************************************************************************
2103 * This function will switch between fullscreen and window mode.
2104 *****************************************************************************/
2105 static void ToggleFullScreen ( vout_thread_t *p_vout )
2109 mwmhints_t mwmhints;
2110 XSetWindowAttributes attributes;
2112 #ifdef HAVE_XINERAMA
2116 p_vout->b_fullscreen = !p_vout->b_fullscreen;
2118 if( p_vout->b_fullscreen )
2120 msg_Dbg( p_vout, "entering fullscreen mode" );
2122 p_vout->p_sys->b_altfullscreen =
2123 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2125 XUnmapWindow( p_vout->p_sys->p_display,
2126 p_vout->p_sys->p_win->base_window );
2128 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2130 CreateWindow( p_vout, p_vout->p_sys->p_win );
2131 XDestroyWindow( p_vout->p_sys->p_display,
2132 p_vout->p_sys->fullscreen_window.video_window );
2133 XReparentWindow( p_vout->p_sys->p_display,
2134 p_vout->p_sys->original_window.video_window,
2135 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2136 p_vout->p_sys->fullscreen_window.video_window =
2137 p_vout->p_sys->original_window.video_window;
2139 /* To my knowledge there are two ways to create a borderless window.
2140 * There's the generic way which is to tell x to bypass the window
2141 * manager, but this creates problems with the focus of other
2143 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2144 * luckily seems to be supported by most window managers. */
2145 if( !p_vout->p_sys->b_altfullscreen )
2147 mwmhints.flags = MWM_HINTS_DECORATIONS;
2148 mwmhints.decorations = False;
2150 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2152 XChangeProperty( p_vout->p_sys->p_display,
2153 p_vout->p_sys->p_win->base_window,
2154 prop, prop, 32, PropModeReplace,
2155 (unsigned char *)&mwmhints,
2156 PROP_MWM_HINTS_ELEMENTS );
2160 /* brute force way to remove decorations */
2161 attributes.override_redirect = True;
2162 XChangeWindowAttributes( p_vout->p_sys->p_display,
2163 p_vout->p_sys->p_win->base_window,
2167 /* Make sure the change is effective */
2168 XReparentWindow( p_vout->p_sys->p_display,
2169 p_vout->p_sys->p_win->base_window,
2170 DefaultRootWindow( p_vout->p_sys->p_display ),
2174 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2176 XClientMessageEvent event;
2178 memset( &event, 0, sizeof( XClientMessageEvent ) );
2180 event.type = ClientMessage;
2181 event.message_type = p_vout->p_sys->net_wm_state;
2182 event.display = p_vout->p_sys->p_display;
2183 event.window = p_vout->p_sys->p_win->base_window;
2185 event.data.l[ 0 ] = 1; /* set property */
2186 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2188 XSendEvent( p_vout->p_sys->p_display,
2189 DefaultRootWindow( p_vout->p_sys->p_display ),
2190 False, SubstructureRedirectMask,
2194 /* Make sure the change is effective */
2195 XReparentWindow( p_vout->p_sys->p_display,
2196 p_vout->p_sys->p_win->base_window,
2197 DefaultRootWindow( p_vout->p_sys->p_display ),
2200 #ifdef HAVE_XINERAMA
2201 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2202 XineramaIsActive( p_vout->p_sys->p_display ) )
2204 XineramaScreenInfo *screens; /* infos for xinerama */
2207 msg_Dbg( p_vout, "using XFree Xinerama extension");
2209 #define SCREEN p_vout->p_sys->p_win->i_screen
2211 /* Get Information about Xinerama (num of screens) */
2212 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2215 SCREEN = config_GetInt( p_vout,
2216 MODULE_STRING "-xineramascreen" );
2218 /* just check that user has entered a good value */
2219 if( SCREEN >= i_num_screens || SCREEN < 0 )
2221 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2225 /* Get the X/Y upper left corner coordinate of the above screen */
2226 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2227 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2229 /* Set the Height/width to the screen resolution */
2230 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2231 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2241 /* The window wasn't necessarily created at the requested size */
2242 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2244 #ifdef HAVE_XF86VIDMODE
2245 XF86VidModeModeLine mode;
2248 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2249 p_vout->p_sys->i_screen, &i_dummy,
2252 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2253 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2255 /* move cursor to the middle of the window to prevent
2256 * unwanted display move if the display is smaller than the
2258 XWarpPointer( p_vout->p_sys->p_display, None,
2259 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2260 mode.hdisplay / 2 , mode.vdisplay / 2 );
2261 /* force desktop view to upper left corner */
2262 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2263 p_vout->p_sys->i_screen, 0, 0 );
2268 p_vout->p_sys->p_win->i_width =
2269 DisplayWidth( p_vout->p_sys->p_display,
2270 p_vout->p_sys->i_screen );
2271 p_vout->p_sys->p_win->i_height =
2272 DisplayHeight( p_vout->p_sys->p_display,
2273 p_vout->p_sys->i_screen );
2278 XMoveResizeWindow( p_vout->p_sys->p_display,
2279 p_vout->p_sys->p_win->base_window,
2280 p_vout->p_sys->p_win->i_x,
2281 p_vout->p_sys->p_win->i_y,
2282 p_vout->p_sys->p_win->i_width,
2283 p_vout->p_sys->p_win->i_height );
2286 EnablePixelDoubling( p_vout );
2289 /* Activate the window (give it the focus) */
2290 XClientMessageEvent event;
2292 memset( &event, 0, sizeof( XClientMessageEvent ) );
2294 event.type = ClientMessage;
2295 event.message_type =
2296 XInternAtom( p_vout->p_sys->p_display, "_NET_ACTIVE_WINDOW", False );
2297 event.display = p_vout->p_sys->p_display;
2298 event.window = p_vout->p_sys->p_win->base_window;
2300 event.data.l[ 0 ] = 1; /* source indication (1 = from an application */
2301 event.data.l[ 1 ] = 0; /* timestamp */
2302 event.data.l[ 2 ] = 0; /* requestor's currently active window */
2303 /* XXX: window manager would be more likely to obey if we already have
2304 * an active window (and give it to the event), such as an interface */
2306 XSendEvent( p_vout->p_sys->p_display,
2307 DefaultRootWindow( p_vout->p_sys->p_display ),
2308 False, SubstructureRedirectMask,
2313 msg_Dbg( p_vout, "leaving fullscreen mode" );
2316 DisablePixelDoubling( p_vout );
2319 XReparentWindow( p_vout->p_sys->p_display,
2320 p_vout->p_sys->original_window.video_window,
2321 p_vout->p_sys->original_window.base_window, 0, 0 );
2323 p_vout->p_sys->fullscreen_window.video_window = None;
2324 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2325 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2327 XMapWindow( p_vout->p_sys->p_display,
2328 p_vout->p_sys->p_win->base_window );
2331 /* Unfortunately, using XSync() here is not enough to ensure the
2332 * window has already been mapped because the XMapWindow() request
2333 * has not necessarily been sent directly to our window (remember,
2334 * the call is first redirected to the window manager) */
2337 XWindowEvent( p_vout->p_sys->p_display,
2338 p_vout->p_sys->p_win->base_window,
2339 StructureNotifyMask, &xevent );
2340 } while( xevent.type != MapNotify );
2342 /* Be careful, this can generate a BadMatch error if the window is not
2343 * already mapped by the server (see above) */
2344 XSetInputFocus(p_vout->p_sys->p_display,
2345 p_vout->p_sys->p_win->base_window,
2349 /* signal that the size needs to be updated */
2350 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2353 /*****************************************************************************
2354 * EnableXScreenSaver: enable screen saver
2355 *****************************************************************************
2356 * This function enables the screen saver on a display after it has been
2357 * disabled by XDisableScreenSaver.
2358 * FIXME: what happens if multiple vlc sessions are running at the same
2360 *****************************************************************************/
2361 static void EnableXScreenSaver( vout_thread_t *p_vout )
2363 #ifdef DPMSINFO_IN_DPMS_H
2367 if( p_vout->p_sys->i_ss_timeout )
2369 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2370 p_vout->p_sys->i_ss_interval,
2371 p_vout->p_sys->i_ss_blanking,
2372 p_vout->p_sys->i_ss_exposure );
2375 /* Restore DPMS settings */
2376 #ifdef DPMSINFO_IN_DPMS_H
2377 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2379 if( p_vout->p_sys->b_ss_dpms )
2381 DPMSEnable( p_vout->p_sys->p_display );
2387 /*****************************************************************************
2388 * DisableXScreenSaver: disable screen saver
2389 *****************************************************************************
2390 * See XEnableXScreenSaver
2391 *****************************************************************************/
2392 static void DisableXScreenSaver( vout_thread_t *p_vout )
2394 #ifdef DPMSINFO_IN_DPMS_H
2398 /* Save screen saver information */
2399 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2400 &p_vout->p_sys->i_ss_interval,
2401 &p_vout->p_sys->i_ss_blanking,
2402 &p_vout->p_sys->i_ss_exposure );
2404 /* Disable screen saver */
2405 if( p_vout->p_sys->i_ss_timeout )
2407 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2408 p_vout->p_sys->i_ss_interval,
2409 p_vout->p_sys->i_ss_blanking,
2410 p_vout->p_sys->i_ss_exposure );
2414 #ifdef DPMSINFO_IN_DPMS_H
2415 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2418 /* Save DPMS current state */
2419 DPMSInfo( p_vout->p_sys->p_display, &unused,
2420 &p_vout->p_sys->b_ss_dpms );
2421 DPMSDisable( p_vout->p_sys->p_display );
2426 /*****************************************************************************
2427 * CreateCursor: create a blank mouse pointer
2428 *****************************************************************************/
2429 static void CreateCursor( vout_thread_t *p_vout )
2431 XColor cursor_color;
2433 p_vout->p_sys->cursor_pixmap =
2434 XCreatePixmap( p_vout->p_sys->p_display,
2435 DefaultRootWindow( p_vout->p_sys->p_display ),
2438 XParseColor( p_vout->p_sys->p_display,
2439 XCreateColormap( p_vout->p_sys->p_display,
2441 p_vout->p_sys->p_display ),
2443 p_vout->p_sys->p_display,
2444 p_vout->p_sys->i_screen ),
2446 "black", &cursor_color );
2448 p_vout->p_sys->blank_cursor =
2449 XCreatePixmapCursor( p_vout->p_sys->p_display,
2450 p_vout->p_sys->cursor_pixmap,
2451 p_vout->p_sys->cursor_pixmap,
2452 &cursor_color, &cursor_color, 1, 1 );
2455 /*****************************************************************************
2456 * DestroyCursor: destroy the blank mouse pointer
2457 *****************************************************************************/
2458 static void DestroyCursor( vout_thread_t *p_vout )
2460 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2463 /*****************************************************************************
2464 * ToggleCursor: hide or show the mouse pointer
2465 *****************************************************************************
2466 * This function hides the X pointer if it is visible by setting the pointer
2467 * sprite to a blank one. To show it again, we disable the sprite.
2468 *****************************************************************************/
2469 static void ToggleCursor( vout_thread_t *p_vout )
2471 if( p_vout->p_sys->b_mouse_pointer_visible )
2473 XDefineCursor( p_vout->p_sys->p_display,
2474 p_vout->p_sys->p_win->base_window,
2475 p_vout->p_sys->blank_cursor );
2476 p_vout->p_sys->b_mouse_pointer_visible = 0;
2480 XUndefineCursor( p_vout->p_sys->p_display,
2481 p_vout->p_sys->p_win->base_window );
2482 p_vout->p_sys->b_mouse_pointer_visible = 1;
2486 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2487 /*****************************************************************************
2488 * XVideoGetPort: get YUV12 port
2489 *****************************************************************************/
2490 static int XVideoGetPort( vout_thread_t *p_vout,
2491 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
2493 XvAdaptorInfo *p_adaptor;
2495 unsigned int i_adaptor, i_num_adaptors;
2496 int i_requested_adaptor;
2497 int i_selected_port;
2499 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2504 case XvBadExtension:
2505 msg_Warn( p_vout, "XvBadExtension" );
2509 msg_Warn( p_vout, "XvBadAlloc" );
2513 msg_Warn( p_vout, "XvQueryExtension failed" );
2517 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2518 DefaultRootWindow( p_vout->p_sys->p_display ),
2519 &i_num_adaptors, &p_adaptor ) )
2524 case XvBadExtension:
2525 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2529 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2533 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2537 i_selected_port = -1;
2538 #ifdef MODULE_NAME_IS_xvmc
2539 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2541 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2543 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2545 XvImageFormatValues *p_formats;
2546 int i_format, i_num_formats;
2549 /* If we requested an adaptor and it's not this one, we aren't
2551 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2556 /* If the adaptor doesn't have the required properties, skip it */
2557 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2558 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2563 /* Check that adaptor supports our requested format... */
2564 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2565 p_adaptor[i_adaptor].base_id,
2569 i_format < i_num_formats && ( i_selected_port == -1 );
2572 XvAttribute *p_attr;
2573 int i_attr, i_num_attributes;
2574 Atom autopaint = None, colorkey = None;
2576 /* If this is not the format we want, or at least a
2577 * similar one, forget it */
2578 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2583 /* Look for the first available port supporting this format */
2584 for( i_port = p_adaptor[i_adaptor].base_id;
2585 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2586 + p_adaptor[i_adaptor].num_ports) )
2587 && ( i_selected_port == -1 );
2590 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2593 i_selected_port = i_port;
2594 *pi_newchroma = p_formats[ i_format ].id;
2598 /* If no free port was found, forget it */
2599 if( i_selected_port == -1 )
2604 /* If we found a port, print information about it */
2605 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2606 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2607 (char *)&p_formats[ i_format ].id,
2608 ( p_formats[ i_format ].format == XvPacked ) ?
2609 "packed" : "planar" );
2611 /* Use XV_AUTOPAINT_COLORKEY if supported, otherwise we will
2612 * manually paint the colour key */
2613 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2615 &i_num_attributes );
2617 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2619 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2621 autopaint = XInternAtom( p_vout->p_sys->p_display,
2622 "XV_AUTOPAINT_COLORKEY", False );
2623 XvSetPortAttribute( p_vout->p_sys->p_display,
2624 i_selected_port, autopaint, 1 );
2626 if( !strcmp( p_attr[i_attr].name, "XV_COLORKEY" ) )
2628 /* Find out the default colour key */
2629 colorkey = XInternAtom( p_vout->p_sys->p_display,
2630 "XV_COLORKEY", False );
2631 XvGetPortAttribute( p_vout->p_sys->p_display,
2632 i_selected_port, colorkey,
2633 &p_vout->p_sys->i_colourkey );
2636 p_vout->p_sys->b_paint_colourkey =
2637 autopaint == None && colorkey != None;
2639 if( p_attr != NULL )
2645 if( p_formats != NULL )
2652 if( i_num_adaptors > 0 )
2654 XvFreeAdaptorInfo( p_adaptor );
2657 if( i_selected_port == -1 )
2659 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2660 if( i_requested_adaptor == -1 )
2662 msg_Warn( p_vout, "no free XVideo port found for format "
2663 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2667 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2668 "XVideo port for format 0x%.8x (%4.4s)",
2669 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2673 return i_selected_port;
2676 /*****************************************************************************
2677 * XVideoReleasePort: release YUV12 port
2678 *****************************************************************************/
2679 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2681 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2685 /*****************************************************************************
2686 * InitDisplay: open and initialize X11 device
2687 *****************************************************************************
2688 * Create a window according to video output given size, and set other
2689 * properties according to the display properties.
2690 *****************************************************************************/
2691 static int InitDisplay( vout_thread_t *p_vout )
2693 #ifdef MODULE_NAME_IS_x11
2694 XPixmapFormatValues * p_formats; /* pixmap formats */
2695 XVisualInfo * p_xvisual; /* visuals information */
2696 XVisualInfo xvisual_template; /* visual template */
2697 int i_count, i; /* array size */
2700 #ifdef HAVE_SYS_SHM_H
2701 p_vout->p_sys->i_shm_opcode = 0;
2703 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
2705 int major, evt, err;
2707 if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2709 && XShmQueryExtension( p_vout->p_sys->p_display ) )
2710 p_vout->p_sys->i_shm_opcode = major;
2712 if( p_vout->p_sys->i_shm_opcode )
2717 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2719 msg_Dbg( p_vout, "XShm video extension v%d.%d "
2720 "(with%s pixmaps, opcode: %d)",
2721 major, minor, pixmaps ? "" : "out",
2722 p_vout->p_sys->i_shm_opcode );
2725 msg_Warn( p_vout, "XShm video extension not available" );
2728 msg_Dbg( p_vout, "XShm video extension disabled" );
2731 #ifdef MODULE_NAME_IS_xvideo
2732 /* XXX The brightness and contrast values should be read from environment
2733 * XXX variables... */
2735 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2736 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2740 #ifdef MODULE_NAME_IS_x11
2741 /* Initialize structure */
2742 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2744 /* Get screen depth */
2745 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2746 p_vout->p_sys->i_screen );
2747 switch( p_vout->p_sys->i_screen_depth )
2751 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2753 xvisual_template.screen = p_vout->p_sys->i_screen;
2754 xvisual_template.class = DirectColor;
2755 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2756 VisualScreenMask | VisualClassMask,
2757 &xvisual_template, &i_count );
2758 if( p_xvisual == NULL )
2760 msg_Err( p_vout, "no PseudoColor visual available" );
2761 return VLC_EGENERIC;
2763 p_vout->p_sys->i_bytes_per_pixel = 1;
2764 p_vout->output.pf_setpalette = SetPalette;
2771 * Screen depth is higher than 8bpp. TrueColor visual is used.
2773 xvisual_template.screen = p_vout->p_sys->i_screen;
2774 xvisual_template.class = TrueColor;
2775 /* In some cases, we get a truecolor class adaptor that has a different
2776 color depth. So try to get a real true color one first */
2777 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2779 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2780 VisualScreenMask | VisualClassMask |
2782 &xvisual_template, &i_count );
2783 if( p_xvisual == NULL )
2785 msg_Warn( p_vout, "No screen matching the required color depth" );
2786 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2787 VisualScreenMask | VisualClassMask,
2788 &xvisual_template, &i_count );
2789 if( p_xvisual == NULL )
2792 msg_Err( p_vout, "no TrueColor visual available" );
2793 return VLC_EGENERIC;
2797 p_vout->output.i_rmask = p_xvisual->red_mask;
2798 p_vout->output.i_gmask = p_xvisual->green_mask;
2799 p_vout->output.i_bmask = p_xvisual->blue_mask;
2801 /* There is no difference yet between 3 and 4 Bpp. The only way
2802 * to find the actual number of bytes per pixel is to list supported
2803 * pixmap formats. */
2804 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2805 p_vout->p_sys->i_bytes_per_pixel = 0;
2807 for( i = 0; i < i_count; i++ )
2809 /* Under XFree4.0, the list contains pixmap formats available
2810 * through all video depths ; so we have to check against current
2812 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2814 if( p_formats[i].bits_per_pixel / 8
2815 > (int)p_vout->p_sys->i_bytes_per_pixel )
2817 p_vout->p_sys->i_bytes_per_pixel =
2818 p_formats[i].bits_per_pixel / 8;
2822 if( p_formats ) XFree( p_formats );
2826 p_vout->p_sys->p_visual = p_xvisual->visual;
2833 #ifndef MODULE_NAME_IS_glx
2835 #ifdef HAVE_SYS_SHM_H
2836 /*****************************************************************************
2837 * CreateShmImage: create an XImage or XvImage using shared memory extension
2838 *****************************************************************************
2839 * Prepare an XImage or XvImage for display function.
2840 * The order of the operations respects the recommandations of the mit-shm
2841 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2842 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2843 *****************************************************************************/
2844 IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2845 Display* p_display, EXTRA_ARGS_SHM,
2846 int i_width, int i_height )
2848 IMAGE_TYPE *p_image;
2851 /* Create XImage / XvImage */
2852 #ifdef MODULE_NAME_IS_xvideo
2854 /* Make sure the buffer is aligned to multiple of 16 */
2855 i_height = ( i_height + 15 ) >> 4 << 4;
2856 i_width = ( i_width + 15 ) >> 4 << 4;
2858 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2859 i_width, i_height, p_shm );
2860 #elif defined(MODULE_NAME_IS_xvmc)
2861 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2862 i_width, i_height, p_shm );
2864 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2865 p_shm, i_width, i_height );
2867 if( p_image == NULL )
2869 msg_Err( p_vout, "image creation failed" );
2873 /* Allocate shared memory segment - 0776 set the access permission
2874 * rights (like umask), they are not yet supported by all X servers */
2875 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2876 if( p_shm->shmid < 0 )
2878 msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2879 IMAGE_FREE( p_image );
2883 /* Attach shared memory segment to process (read/write) */
2884 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2885 if(! p_shm->shmaddr )
2887 msg_Err( p_vout, "cannot attach shared memory (%m)" );
2888 IMAGE_FREE( p_image );
2889 shmctl( p_shm->shmid, IPC_RMID, 0 );
2893 /* Read-only data. We won't be using XShmGetImage */
2894 p_shm->readOnly = True;
2896 /* Attach shared memory segment to X server */
2897 XSynchronize( p_display, True );
2898 i_shm_major = p_vout->p_sys->i_shm_opcode;
2899 result = XShmAttach( p_display, p_shm );
2900 if( result == False || !i_shm_major )
2902 msg_Err( p_vout, "cannot attach shared memory to X server" );
2903 IMAGE_FREE( p_image );
2904 shmctl( p_shm->shmid, IPC_RMID, 0 );
2905 shmdt( p_shm->shmaddr );
2908 XSynchronize( p_display, False );
2910 /* Send image to X server. This instruction is required, since having
2911 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2912 * and remember NOT to use XFlush ! */
2913 XSync( p_display, False );
2916 /* Mark the shm segment to be removed when there are no more
2917 * attachements, so it is automatic on process exit or after shmdt */
2918 shmctl( p_shm->shmid, IPC_RMID, 0 );
2925 /*****************************************************************************
2926 * CreateImage: create an XImage or XvImage
2927 *****************************************************************************
2928 * Create a simple image used as a buffer.
2929 *****************************************************************************/
2930 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2931 Display *p_display, EXTRA_ARGS,
2932 int i_width, int i_height )
2934 uint8_t * p_data; /* image data storage zone */
2935 IMAGE_TYPE *p_image;
2936 #ifdef MODULE_NAME_IS_x11
2937 int i_quantum; /* XImage quantum (see below) */
2938 int i_bytes_per_line;
2941 /* Allocate memory for image */
2942 #ifdef MODULE_NAME_IS_xvideo
2944 /* Make sure the buffer is aligned to multiple of 16 */
2945 i_height = ( i_height + 15 ) >> 4 << 4;
2946 i_width = ( i_width + 15 ) >> 4 << 4;
2948 p_data = malloc( i_width * i_height * i_bits_per_pixel / 8 );
2949 #elif defined(MODULE_NAME_IS_x11)
2950 i_bytes_per_line = i_width * i_bytes_per_pixel;
2951 p_data = malloc( i_bytes_per_line * i_height );
2955 msg_Err( p_vout, "out of memory" );
2959 #ifdef MODULE_NAME_IS_x11
2960 /* Optimize the quantum of a scanline regarding its size - the quantum is
2961 a diviser of the number of bits between the start of two scanlines. */
2962 if( i_bytes_per_line & 0xf )
2966 else if( i_bytes_per_line & 0x10 )
2976 /* Create XImage. p_data will be automatically freed */
2977 #ifdef MODULE_NAME_IS_xvideo
2978 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2979 (char *)p_data, i_width, i_height );
2980 #elif defined(MODULE_NAME_IS_x11)
2981 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2982 (char *)p_data, i_width, i_height, i_quantum, 0 );
2984 if( p_image == NULL )
2986 msg_Err( p_vout, "XCreateImage() failed" );
2995 /*****************************************************************************
2996 * X11ErrorHandler: replace error handler so we can intercept some of them
2997 *****************************************************************************/
2998 static int X11ErrorHandler( Display * display, XErrorEvent * event )
3002 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
3004 "[????????] x11 video output error: X11 request %u.%u failed "
3005 "with error code %u:\n %s\n",
3006 event->request_code, event->minor_code, event->error_code, txt );
3008 switch( event->request_code )
3010 case X_SetInputFocus:
3011 /* Ignore errors on XSetInputFocus()
3012 * (they happen when a window is not yet mapped) */
3016 #ifdef HAVE_SYS_SHM_H
3017 if( event->request_code == i_shm_major ) /* MIT-SHM */
3018 return i_shm_major = 0;
3021 XSetErrorHandler(NULL);
3022 return (XSetErrorHandler(X11ErrorHandler))( display, event );
3025 #ifdef MODULE_NAME_IS_x11
3026 /*****************************************************************************
3027 * SetPalette: sets an 8 bpp palette
3028 *****************************************************************************
3029 * This function sets the palette given as an argument. It does not return
3030 * anything, but could later send information on which colors it was unable
3032 *****************************************************************************/
3033 static void SetPalette( vout_thread_t *p_vout,
3034 uint16_t *red, uint16_t *green, uint16_t *blue )
3037 XColor p_colors[255];
3039 /* allocate palette */
3040 for( i = 0; i < 255; i++ )
3042 /* kludge: colors are indexed reversely because color 255 seems
3043 * to be reserved for black even if we try to set it to white */
3044 p_colors[ i ].pixel = 255 - i;
3045 p_colors[ i ].pad = 0;
3046 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
3047 p_colors[ i ].red = red[ 255 - i ];
3048 p_colors[ i ].blue = blue[ 255 - i ];
3049 p_colors[ i ].green = green[ 255 - i ];
3052 XStoreColors( p_vout->p_sys->p_display,
3053 p_vout->p_sys->colormap, p_colors, 255 );
3057 /*****************************************************************************
3058 * Control: control facility for the vout
3059 *****************************************************************************/
3060 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
3063 unsigned int i_width, i_height;
3064 unsigned int *pi_width, *pi_height;
3070 if( p_vout->p_sys->p_win->owner_window )
3071 return vout_ControlWindow( p_vout,
3072 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3074 pi_width = va_arg( args, unsigned int * );
3075 pi_height = va_arg( args, unsigned int * );
3077 vlc_mutex_lock( &p_vout->p_sys->lock );
3078 *pi_width = p_vout->p_sys->p_win->i_width;
3079 *pi_height = p_vout->p_sys->p_win->i_height;
3080 vlc_mutex_unlock( &p_vout->p_sys->lock );
3084 if( p_vout->p_sys->p_win->owner_window )
3085 return vout_ControlWindow( p_vout,
3086 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3088 vlc_mutex_lock( &p_vout->p_sys->lock );
3090 i_width = va_arg( args, unsigned int );
3091 i_height = va_arg( args, unsigned int );
3092 if( !i_width ) i_width = p_vout->i_window_width;
3093 if( !i_height ) i_height = p_vout->i_window_height;
3095 #ifdef MODULE_NAME_IS_xvmc
3096 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3098 /* Update dimensions */
3099 XResizeWindow( p_vout->p_sys->p_display,
3100 p_vout->p_sys->p_win->base_window,
3101 i_width, i_height );
3102 #ifdef MODULE_NAME_IS_xvmc
3103 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3105 vlc_mutex_unlock( &p_vout->p_sys->lock );
3109 vlc_mutex_lock( &p_vout->p_sys->lock );
3110 XUnmapWindow( p_vout->p_sys->p_display,
3111 p_vout->p_sys->original_window.base_window );
3112 vlc_mutex_unlock( &p_vout->p_sys->lock );
3116 vlc_mutex_lock( &p_vout->p_sys->lock );
3117 if( i_query == VOUT_REPARENT ) d = (Drawable)va_arg( args, int );
3120 #ifdef MODULE_NAME_IS_xvmc
3121 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3123 XReparentWindow( p_vout->p_sys->p_display,
3124 p_vout->p_sys->original_window.base_window,
3125 DefaultRootWindow( p_vout->p_sys->p_display ),
3129 XReparentWindow( p_vout->p_sys->p_display,
3130 p_vout->p_sys->original_window.base_window,
3132 XSync( p_vout->p_sys->p_display, False );
3133 p_vout->p_sys->original_window.owner_window = 0;
3134 #ifdef MODULE_NAME_IS_xvmc
3135 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3137 vlc_mutex_unlock( &p_vout->p_sys->lock );
3138 return vout_vaControlDefault( p_vout, i_query, args );
3140 case VOUT_SET_STAY_ON_TOP:
3141 if( p_vout->p_sys->p_win->owner_window )
3142 return vout_ControlWindow( p_vout,
3143 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3145 b_arg = (bool) va_arg( args, int );
3146 vlc_mutex_lock( &p_vout->p_sys->lock );
3147 #ifdef MODULE_NAME_IS_xvmc
3148 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3150 WindowOnTop( p_vout, b_arg );
3151 #ifdef MODULE_NAME_IS_xvmc
3152 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3154 vlc_mutex_unlock( &p_vout->p_sys->lock );
3158 return vout_vaControlDefault( p_vout, i_query, args );
3162 /*****************************************************************************
3163 * TestNetWMSupport: tests for Extended Window Manager Hints support
3164 *****************************************************************************/
3165 static void TestNetWMSupport( vout_thread_t *p_vout )
3167 int i_ret, i_format;
3168 unsigned long i, i_items, i_bytesafter;
3169 Atom net_wm_supported;
3170 union { Atom *p_atom; unsigned char *p_char; } p_args;
3172 p_args.p_atom = NULL;
3174 p_vout->p_sys->b_net_wm_state_fullscreen =
3175 p_vout->p_sys->b_net_wm_state_above =
3176 p_vout->p_sys->b_net_wm_state_below =
3177 p_vout->p_sys->b_net_wm_state_stays_on_top =
3181 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3183 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3184 DefaultRootWindow( p_vout->p_sys->p_display ),
3186 0, 16384, False, AnyPropertyType,
3188 &i_format, &i_items, &i_bytesafter,
3189 (unsigned char **)&p_args );
3191 if( i_ret != Success || i_items == 0 ) return;
3193 msg_Dbg( p_vout, "Window manager supports NetWM" );
3195 p_vout->p_sys->net_wm_state =
3196 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3197 p_vout->p_sys->net_wm_state_fullscreen =
3198 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3200 p_vout->p_sys->net_wm_state_above =
3201 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3202 p_vout->p_sys->net_wm_state_below =
3203 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3204 p_vout->p_sys->net_wm_state_stays_on_top =
3205 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3208 for( i = 0; i < i_items; i++ )
3210 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3213 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3214 p_vout->p_sys->b_net_wm_state_fullscreen = true;
3216 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3218 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3219 p_vout->p_sys->b_net_wm_state_above = true;
3221 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3223 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3224 p_vout->p_sys->b_net_wm_state_below = true;
3226 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3229 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3230 p_vout->p_sys->b_net_wm_state_stays_on_top = true;
3234 XFree( p_args.p_atom );
3237 /*****************************************************************************
3238 * Key events handling
3239 *****************************************************************************/
3245 } x11keys_to_vlckeys[] =
3247 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3248 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3249 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3250 { XK_F12, KEY_F12 },
3252 { XK_Return, KEY_ENTER },
3253 { XK_KP_Enter, KEY_ENTER },
3254 { XK_space, KEY_SPACE },
3255 { XK_Escape, KEY_ESC },
3257 { XK_Menu, KEY_MENU },
3258 { XK_Left, KEY_LEFT },
3259 { XK_Right, KEY_RIGHT },
3261 { XK_Down, KEY_DOWN },
3263 { XK_Home, KEY_HOME },
3264 { XK_End, KEY_END },
3265 { XK_Page_Up, KEY_PAGEUP },
3266 { XK_Page_Down, KEY_PAGEDOWN },
3268 { XK_Insert, KEY_INSERT },
3269 { XK_Delete, KEY_DELETE },
3270 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3271 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3272 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3273 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3274 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3275 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3276 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3281 static int ConvertKey( int i_key )
3285 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3287 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3289 return x11keys_to_vlckeys[i].i_vlckey;
3296 /*****************************************************************************
3297 * WindowOnTop: Switches the "always on top" state of the video window.
3298 *****************************************************************************/
3299 static int WindowOnTop( vout_thread_t *p_vout, bool b_on_top )
3301 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3303 XClientMessageEvent event;
3305 memset( &event, 0, sizeof( XClientMessageEvent ) );
3307 event.type = ClientMessage;
3308 event.message_type = p_vout->p_sys->net_wm_state;
3309 event.display = p_vout->p_sys->p_display;
3310 event.window = p_vout->p_sys->p_win->base_window;
3312 event.data.l[ 0 ] = b_on_top; /* set property */
3313 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3315 XSendEvent( p_vout->p_sys->p_display,
3316 DefaultRootWindow( p_vout->p_sys->p_display ),
3317 False, SubstructureRedirectMask,
3321 /* use _NET_WM_STATE_ABOVE if window manager
3322 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3323 else if( p_vout->p_sys->b_net_wm_state_above )
3325 XClientMessageEvent event;
3327 memset( &event, 0, sizeof( XClientMessageEvent ) );
3329 event.type = ClientMessage;
3330 event.message_type = p_vout->p_sys->net_wm_state;
3331 event.display = p_vout->p_sys->p_display;
3332 event.window = p_vout->p_sys->p_win->base_window;
3334 event.data.l[ 0 ] = b_on_top; /* set property */
3335 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3337 XSendEvent( p_vout->p_sys->p_display,
3338 DefaultRootWindow( p_vout->p_sys->p_display ),
3339 False, SubstructureRedirectMask,