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 static 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 *, vlc_bool_t );
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;
192 vlc_bool_t b_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 );
227 if( psz_display ) free( psz_display );
230 if( psz_display ) free( psz_display );
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 = VLC_FALSE;
333 msg_Dbg( p_this, "using GLX 1.2 API" );
337 p_vout->p_sys->b_glx13 = VLC_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->b_mouse_pointer_visible = 1;
346 CreateCursor( p_vout );
348 /* Set main window's size */
349 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
350 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
351 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
352 /* Spawn base window - this window will include the video output window,
353 * but also command buttons, subtitles and other indicators */
354 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
356 msg_Err( p_vout, "cannot create X11 window" );
357 DestroyCursor( p_vout );
358 XCloseDisplay( p_vout->p_sys->p_display );
359 free( p_vout->p_sys );
363 /* Open and initialize device. */
364 if( InitDisplay( p_vout ) )
366 msg_Err( p_vout, "cannot initialize X11 display" );
367 DestroyCursor( p_vout );
368 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
369 XCloseDisplay( p_vout->p_sys->p_display );
370 free( p_vout->p_sys );
374 /* Disable screen saver */
375 DisableXScreenSaver( p_vout );
378 p_vout->p_sys->b_altfullscreen = 0;
379 p_vout->p_sys->i_time_button_last_pressed = 0;
381 TestNetWMSupport( p_vout );
383 #ifdef MODULE_NAME_IS_xvmc
384 p_vout->p_sys->p_last_subtitle_save = NULL;
385 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
387 /* Look what method was requested */
388 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
389 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
392 if( (strcmp(psz_value, "bob") == 0) ||
393 (strcmp(psz_value, "blend") == 0) )
394 p_vout->p_sys->xvmc_deinterlace_method = 2;
395 else if (strcmp(psz_value, "discard") == 0)
396 p_vout->p_sys->xvmc_deinterlace_method = 1;
398 p_vout->p_sys->xvmc_deinterlace_method = 0;
402 p_vout->p_sys->xvmc_deinterlace_method = 0;
404 /* Look what method was requested */
405 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
406 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
407 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
411 if( strncmp( psz_value, "eq", 2 ) == 0 )
412 p_vout->p_sys->xvmc_crop_style = 1;
413 else if( strncmp( psz_value, "4-16", 4 ) == 0)
414 p_vout->p_sys->xvmc_crop_style = 2;
415 else if( strncmp( psz_value, "16-4", 4 ) == 0)
416 p_vout->p_sys->xvmc_crop_style = 3;
418 p_vout->p_sys->xvmc_crop_style = 0;
422 p_vout->p_sys->xvmc_crop_style = 0;
424 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
425 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
427 if( !checkXvMCCap( p_vout ) )
429 msg_Err( p_vout, "no XVMC capability found" );
430 E_(Deactivate)( p_vout );
433 subpicture_t sub_pic;
434 sub_pic.p_sys = NULL;
435 p_vout->p_sys->last_date = 0;
439 p_vout->p_sys->i_hw_scale = 1;
443 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
444 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
445 if ( p_vout->p_sys->p_octx == NULL ) {
446 msg_Err( p_vout, "Could not get osso context" );
448 msg_Dbg( p_vout, "Initialized osso context" );
452 /* Variable to indicate if the window should be on top of others */
453 /* Trigger a callback right now */
454 var_Get( p_vout, "video-on-top", &val );
455 var_Set( p_vout, "video-on-top", val );
460 /*****************************************************************************
461 * Deactivate: destroy X11 video thread output method
462 *****************************************************************************
463 * Terminate an output method created by Open
464 *****************************************************************************/
465 void E_(Deactivate) ( vlc_object_t *p_this )
467 vout_thread_t *p_vout = (vout_thread_t *)p_this;
469 /* If the fullscreen window is still open, close it */
470 if( p_vout->b_fullscreen )
472 ToggleFullScreen( p_vout );
475 /* Restore cursor if it was blanked */
476 if( !p_vout->p_sys->b_mouse_pointer_visible )
478 ToggleCursor( p_vout );
481 #ifdef MODULE_NAME_IS_x11
482 /* Destroy colormap */
483 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
485 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
487 #elif defined(MODULE_NAME_IS_xvideo)
488 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
489 #elif defined(MODULE_NAME_IS_xvmc)
490 if( p_vout->p_sys->xvmc_cap )
492 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
493 xxmc_dispose_context( p_vout );
494 if( p_vout->p_sys->old_subpic )
496 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
497 p_vout->p_sys->old_subpic = NULL;
499 if( p_vout->p_sys->new_subpic )
501 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
502 p_vout->p_sys->new_subpic = NULL;
504 free( p_vout->p_sys->xvmc_cap );
505 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
510 DisablePixelDoubling(p_vout);
513 DestroyCursor( p_vout );
514 EnableXScreenSaver( p_vout );
515 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
516 XCloseDisplay( p_vout->p_sys->p_display );
518 /* Destroy structure */
519 vlc_mutex_destroy( &p_vout->p_sys->lock );
520 #ifdef MODULE_NAME_IS_xvmc
521 free_context_lock( &p_vout->p_sys->xvmc_lock );
525 if ( p_vout->p_sys->p_octx != NULL ) {
526 msg_Dbg( p_vout, "Deinitializing osso context" );
527 osso_deinitialize( p_vout->p_sys->p_octx );
531 free( p_vout->p_sys );
534 #ifdef MODULE_NAME_IS_xvmc
536 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
539 static int xvmc_check_yv12( Display *display, XvPortID port )
541 XvImageFormatValues *formatValues;
545 formatValues = XvListImageFormats( display, port, &formats );
547 for( i = 0; i < formats; i++ )
549 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
550 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
552 XFree (formatValues);
557 XFree (formatValues);
561 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
563 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
566 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
569 int xv_double_buffer;
571 xv_double_buffer = 1;
573 XLockDisplay( p_vout->p_sys->p_display );
574 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
576 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
578 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
579 XUnlockDisplay( p_vout->p_sys->p_display );
581 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
582 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
585 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
587 vlc_xxmc_t *xxmc = NULL;
589 vlc_mutex_lock( &p_vout->p_sys->lock );
590 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
592 xxmc = &p_pic->p_sys->xxmc_data;
593 if( (!xxmc->decoded ||
594 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
596 vlc_mutex_unlock( &p_vout->p_sys->lock );
597 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
602 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
603 if (p_vout->p_sys->p_last_subtitle != NULL)
605 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
607 p_vout->p_sys->new_subpic =
608 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
609 p_vout->p_sys->xvmc_width,
610 p_vout->p_sys->xvmc_height,
611 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
613 if (p_vout->p_sys->new_subpic)
615 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
616 XvMCClearSubpicture( p_vout->p_sys->p_display,
617 p_vout->p_sys->new_subpic,
620 p_vout->p_sys->xvmc_width,
621 p_vout->p_sys->xvmc_height,
623 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
624 clear_xx44_palette( &p_vout->p_sys->palette );
626 if( sub_pic.p_sys == NULL )
628 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
629 if( sub_pic.p_sys != NULL )
631 sub_pic.p_sys->p_vout = p_vout;
632 sub_pic.p_sys->xvmc_surf = NULL;
633 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
636 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
637 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
638 sub_pic.p->i_pitch = p_vout->output.i_width;
640 memset( p_vout->p_sys->subImage->data, 0,
641 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
643 if (p_vout->p_last_subtitle != NULL)
645 blend_xx44( p_vout->p_sys->subImage->data,
646 p_vout->p_last_subtitle,
647 p_vout->p_sys->subImage->width,
648 p_vout->p_sys->subImage->height,
649 p_vout->p_sys->subImage->width,
650 &p_vout->p_sys->palette,
651 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
654 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
655 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
656 p_vout->p_sys->new_subpic,
657 p_vout->p_sys->subImage,
660 p_vout->output.i_width, /* overlay->width, */
661 p_vout->output.i_height, /* overlay->height */
664 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
665 if (p_vout->p_sys->old_subpic)
667 xxmc_xvmc_free_subpicture( p_vout,
668 p_vout->p_sys->old_subpic);
669 p_vout->p_sys->old_subpic = NULL;
671 if (p_vout->p_sys->new_subpic)
673 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
674 p_vout->p_sys->new_subpic = NULL;
675 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
676 p_vout->p_sys->xvmc_palette,
678 p_vout->p_sys->old_subpic->num_palette_entries,
679 p_vout->p_sys->old_subpic->entry_bytes,
680 p_vout->p_sys->old_subpic->component_order );
681 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
682 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
683 p_vout->p_sys->old_subpic,
684 p_vout->p_sys->xvmc_palette );
685 XvMCFlushSubpicture( p_vout->p_sys->p_display,
686 p_vout->p_sys->old_subpic);
687 XvMCSyncSubpicture( p_vout->p_sys->p_display,
688 p_vout->p_sys->old_subpic );
689 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
692 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
693 if (p_vout->p_sys->xvmc_backend_subpic )
695 XvMCBlendSubpicture( p_vout->p_sys->p_display,
696 p_pic->p_sys->xvmc_surf,
697 p_vout->p_sys->old_subpic,
700 p_vout->p_sys->xvmc_width,
701 p_vout->p_sys->xvmc_height,
704 p_vout->p_sys->xvmc_width,
705 p_vout->p_sys->xvmc_height );
709 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
710 p_pic->p_sys->xvmc_surf,
711 p_pic->p_sys->xvmc_surf,
712 p_vout->p_sys->old_subpic,
715 p_vout->p_sys->xvmc_width,
716 p_vout->p_sys->xvmc_height,
719 p_vout->p_sys->xvmc_width,
720 p_vout->p_sys->xvmc_height );
722 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
727 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
728 if( p_vout->p_sys->xvmc_backend_subpic )
730 XvMCBlendSubpicture( p_vout->p_sys->p_display,
731 p_pic->p_sys->xvmc_surf,
732 p_vout->p_sys->old_subpic,
734 p_vout->p_sys->xvmc_width,
735 p_vout->p_sys->xvmc_height,
737 p_vout->p_sys->xvmc_width,
738 p_vout->p_sys->xvmc_height );
742 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
743 p_pic->p_sys->xvmc_surf,
744 p_pic->p_sys->xvmc_surf,
745 p_vout->p_sys->old_subpic,
747 p_vout->p_sys->xvmc_width,
748 p_vout->p_sys->xvmc_height,
750 p_vout->p_sys->xvmc_width,
751 p_vout->p_sys->xvmc_height );
753 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
756 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
758 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
760 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
762 vlc_mutex_unlock( &p_vout->p_sys->lock );
767 /*****************************************************************************
768 * EnablePixelDoubling: Enables pixel doubling
769 *****************************************************************************
770 * Checks if the double size image fits in current window, and enables pixel
771 * doubling accordingly. The i_hw_scale is the integer scaling factor.
772 *****************************************************************************/
773 static void EnablePixelDoubling( vout_thread_t *p_vout )
775 int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
776 int i_vert_scale = ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
777 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
778 p_vout->p_sys->i_hw_scale = 2;
779 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
780 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
784 /*****************************************************************************
785 * DisablePixelDoubling: Disables pixel doubling
786 *****************************************************************************
787 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
788 *****************************************************************************/
789 static void DisablePixelDoubling( vout_thread_t *p_vout )
791 if ( p_vout->p_sys->i_hw_scale > 1 ) {
792 msg_Dbg( p_vout, "Disabling pixel doubling" );
793 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
794 p_vout->p_sys->i_hw_scale = 1;
801 /*****************************************************************************
802 * InitVideo: initialize X11 video thread output method
803 *****************************************************************************
804 * This function create the XImages needed by the output thread. It is called
805 * at the beginning of the thread, but also each time the window is resized.
806 *****************************************************************************/
807 static int InitVideo( vout_thread_t *p_vout )
809 unsigned int i_index = 0;
812 I_OUTPUTPICTURES = 0;
814 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
815 /* Initialize the output structure; we already found an XVideo port,
816 * and the corresponding chroma we will be using. Since we can
817 * arbitrary scale, stick to the coordinates and aspect. */
818 p_vout->output.i_width = p_vout->render.i_width;
819 p_vout->output.i_height = p_vout->render.i_height;
820 p_vout->output.i_aspect = p_vout->render.i_aspect;
822 p_vout->fmt_out = p_vout->fmt_in;
823 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
825 switch( p_vout->output.i_chroma )
827 case VLC_FOURCC('R','V','1','6'):
828 #if defined( WORDS_BIGENDIAN )
829 p_vout->output.i_rmask = 0xf800;
830 p_vout->output.i_gmask = 0x07e0;
831 p_vout->output.i_bmask = 0x001f;
833 p_vout->output.i_rmask = 0x001f;
834 p_vout->output.i_gmask = 0x07e0;
835 p_vout->output.i_bmask = 0xf800;
838 case VLC_FOURCC('R','V','1','5'):
839 #if defined( WORDS_BIGENDIAN )
840 p_vout->output.i_rmask = 0x7c00;
841 p_vout->output.i_gmask = 0x03e0;
842 p_vout->output.i_bmask = 0x001f;
844 p_vout->output.i_rmask = 0x001f;
845 p_vout->output.i_gmask = 0x03e0;
846 p_vout->output.i_bmask = 0x7c00;
851 #elif defined(MODULE_NAME_IS_x11)
852 /* Initialize the output structure: RGB with square pixels, whatever
853 * the input format is, since it's the only format we know */
854 switch( p_vout->p_sys->i_screen_depth )
856 case 8: /* FIXME: set the palette */
857 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
859 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
861 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
864 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
866 msg_Err( p_vout, "unknown screen depth %i",
867 p_vout->p_sys->i_screen_depth );
872 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width / p_vout->p_sys->i_hw_scale,
873 p_vout->p_sys->p_win->i_height / p_vout->p_sys->i_hw_scale,
875 &p_vout->fmt_out.i_visible_width,
876 &p_vout->fmt_out.i_visible_height );
878 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
879 p_vout->p_sys->p_win->i_height,
881 &p_vout->fmt_out.i_visible_width,
882 &p_vout->fmt_out.i_visible_height );
885 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
887 p_vout->output.i_width = p_vout->fmt_out.i_width =
888 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
889 p_vout->fmt_in.i_visible_width;
890 p_vout->output.i_height = p_vout->fmt_out.i_height =
891 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
892 p_vout->fmt_in.i_visible_height;
893 p_vout->fmt_out.i_x_offset =
894 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
895 p_vout->fmt_in.i_visible_width;
896 p_vout->fmt_out.i_y_offset =
897 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
898 p_vout->fmt_in.i_visible_height;
900 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
901 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
902 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
904 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
905 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
906 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
907 p_vout->fmt_out.i_visible_width,
908 p_vout->fmt_out.i_visible_height );
911 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
912 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
916 /* Find an empty picture slot */
917 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
919 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
921 p_pic = p_vout->p_picture + i_index;
926 /* Allocate the picture */
927 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
932 p_pic->i_status = DESTROYED_PICTURE;
933 p_pic->i_type = DIRECT_PICTURE;
935 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
940 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
942 /* U and V inverted compared to I420
943 * Fixme: this should be handled by the vout core */
944 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
945 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
951 /*****************************************************************************
952 * DisplayVideo: displays previously rendered output
953 *****************************************************************************
954 * This function sends the currently rendered image to X11 server.
955 * (The Xv extension takes care of "double-buffering".)
956 *****************************************************************************/
957 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
959 unsigned int i_width, i_height, i_x, i_y;
961 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
962 p_vout->p_sys->p_win->i_height,
963 &i_x, &i_y, &i_width, &i_height );
965 vlc_mutex_lock( &p_vout->p_sys->lock );
967 #ifdef MODULE_NAME_IS_xvmc
968 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
970 vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
971 if( !xxmc->decoded ||
972 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
974 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
976 xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
977 vlc_mutex_unlock( &p_vout->p_sys->lock );
978 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
982 int src_width = p_vout->output.i_width;
983 int src_height = p_vout->output.i_height;
986 if( p_vout->p_sys->xvmc_crop_style == 1 )
993 else if( p_vout->p_sys->xvmc_crop_style == 2 )
1000 else if( p_vout->p_sys->xvmc_crop_style == 3 )
1014 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1015 { /* BOB DEINTERLACE */
1016 if( (p_pic->p_sys->nb_display == 0) ||
1017 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1019 first_field = (p_pic->b_top_field_first) ?
1020 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1024 first_field = (p_pic->b_top_field_first) ?
1025 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1030 first_field = XVMC_FRAME_PICTURE;
1033 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1034 XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
1035 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1036 XvMCPutSurface( p_vout->p_sys->p_display,
1037 p_pic->p_sys->xvmc_surf,
1038 p_vout->p_sys->p_win->video_window,
1049 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1050 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1051 { /* BOB DEINTERLACE */
1052 if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1054 mtime_t last_date = p_pic->date;
1056 vlc_mutex_lock( &p_vout->picture_lock );
1057 if( !p_vout->p_sys->last_date )
1059 p_pic->date += 20000;
1063 p_pic->date = ((3 * p_pic->date -
1064 p_vout->p_sys->last_date) / 2 );
1066 p_vout->p_sys->last_date = last_date;
1068 p_pic->p_sys->nb_display = 1;
1069 vlc_mutex_unlock( &p_vout->picture_lock );
1073 p_pic->p_sys->nb_display = 0;
1077 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1080 #ifdef HAVE_SYS_SHM_H
1081 if( p_vout->p_sys->i_shm_opcode )
1083 /* Display rendered image using shared memory extension */
1084 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1085 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1086 p_vout->p_sys->p_win->video_window,
1087 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1088 p_vout->fmt_out.i_x_offset,
1089 p_vout->fmt_out.i_y_offset,
1090 p_vout->fmt_out.i_visible_width,
1091 p_vout->fmt_out.i_visible_height,
1092 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1093 False /* Don't put True here or you'll waste your CPU */ );
1095 XShmPutImage( p_vout->p_sys->p_display,
1096 p_vout->p_sys->p_win->video_window,
1097 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1098 p_vout->fmt_out.i_x_offset,
1099 p_vout->fmt_out.i_y_offset,
1100 0 /*dest_x*/, 0 /*dest_y*/,
1101 p_vout->fmt_out.i_visible_width,
1102 p_vout->fmt_out.i_visible_height,
1103 False /* Don't put True here ! */ );
1107 #endif /* HAVE_SYS_SHM_H */
1109 /* Use standard XPutImage -- this is gonna be slow ! */
1110 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1111 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1112 p_vout->p_sys->p_win->video_window,
1113 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1114 p_vout->fmt_out.i_x_offset,
1115 p_vout->fmt_out.i_y_offset,
1116 p_vout->fmt_out.i_visible_width,
1117 p_vout->fmt_out.i_visible_height,
1118 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1120 XPutImage( p_vout->p_sys->p_display,
1121 p_vout->p_sys->p_win->video_window,
1122 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1123 p_vout->fmt_out.i_x_offset,
1124 p_vout->fmt_out.i_y_offset,
1125 0 /*dest_x*/, 0 /*dest_y*/,
1126 p_vout->fmt_out.i_visible_width,
1127 p_vout->fmt_out.i_visible_height );
1131 /* Make sure the command is sent now - do NOT use XFlush !*/
1132 XSync( p_vout->p_sys->p_display, False );
1134 vlc_mutex_unlock( &p_vout->p_sys->lock );
1137 /*****************************************************************************
1138 * ManageVideo: handle X11 events
1139 *****************************************************************************
1140 * This function should be called regularly by video output thread. It manages
1141 * X11 events and allows window resizing. It returns a non null value on
1143 *****************************************************************************/
1144 static int ManageVideo( vout_thread_t *p_vout )
1146 XEvent xevent; /* X11 event */
1149 vlc_mutex_lock( &p_vout->p_sys->lock );
1151 #ifdef MODULE_NAME_IS_xvmc
1152 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1155 /* Handle events from the owner window */
1156 if( p_vout->p_sys->p_win->owner_window )
1158 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1159 p_vout->p_sys->p_win->owner_window,
1160 StructureNotifyMask, &xevent ) == True )
1162 /* ConfigureNotify event: prepare */
1163 if( xevent.type == ConfigureNotify )
1165 /* Update dimensions */
1166 XResizeWindow( p_vout->p_sys->p_display,
1167 p_vout->p_sys->p_win->base_window,
1168 xevent.xconfigure.width,
1169 xevent.xconfigure.height );
1174 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1175 * output window's size changed, MapNotify and UnmapNotify to know if the
1176 * window is mapped (and if the display is useful), and ClientMessages
1177 * to intercept window destruction requests */
1179 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1180 p_vout->p_sys->p_win->base_window,
1181 StructureNotifyMask | KeyPressMask |
1182 ButtonPressMask | ButtonReleaseMask |
1183 PointerMotionMask | Button1MotionMask , &xevent )
1186 /* ConfigureNotify event: prepare */
1187 if( xevent.type == ConfigureNotify )
1189 if( (unsigned int)xevent.xconfigure.width
1190 != p_vout->p_sys->p_win->i_width
1191 || (unsigned int)xevent.xconfigure.height
1192 != p_vout->p_sys->p_win->i_height )
1194 /* Update dimensions */
1195 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1196 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1197 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1200 /* Keyboard event */
1201 else if( xevent.type == KeyPress )
1203 unsigned int state = xevent.xkey.state;
1204 KeySym x_key_symbol;
1205 char i_key; /* ISO Latin-1 key */
1207 /* We may have keys like F1 trough F12, ESC ... */
1208 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1209 xevent.xkey.keycode, 0 );
1210 val.i_int = ConvertKey( (int)x_key_symbol );
1212 xevent.xkey.state &= ~ShiftMask;
1213 xevent.xkey.state &= ~ControlMask;
1214 xevent.xkey.state &= ~Mod1Mask;
1217 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1220 * The reason why I use this instead of XK_0 is that
1221 * with XLookupString, we don't have to care about
1228 if( state & ShiftMask )
1230 val.i_int |= KEY_MODIFIER_SHIFT;
1232 if( state & ControlMask )
1234 val.i_int |= KEY_MODIFIER_CTRL;
1236 if( state & Mod1Mask )
1238 val.i_int |= KEY_MODIFIER_ALT;
1240 var_Set( p_vout->p_libvlc, "key-pressed", val );
1244 else if( xevent.type == ButtonPress )
1246 switch( ((XButtonEvent *)&xevent)->button )
1249 var_Get( p_vout, "mouse-button-down", &val );
1251 var_Set( p_vout, "mouse-button-down", val );
1253 /* detect double-clicks */
1254 if( ( ((XButtonEvent *)&xevent)->time -
1255 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1257 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1260 p_vout->p_sys->i_time_button_last_pressed =
1261 ((XButtonEvent *)&xevent)->time;
1264 var_Get( p_vout, "mouse-button-down", &val );
1266 var_Set( p_vout, "mouse-button-down", val );
1270 var_Get( p_vout, "mouse-button-down", &val );
1272 var_Set( p_vout, "mouse-button-down", val );
1276 var_Get( p_vout, "mouse-button-down", &val );
1278 var_Set( p_vout, "mouse-button-down", val );
1282 var_Get( p_vout, "mouse-button-down", &val );
1284 var_Set( p_vout, "mouse-button-down", val );
1289 else if( xevent.type == ButtonRelease )
1291 switch( ((XButtonEvent *)&xevent)->button )
1294 var_Get( p_vout, "mouse-button-down", &val );
1296 var_Set( p_vout, "mouse-button-down", val );
1298 val.b_bool = VLC_TRUE;
1299 var_Set( p_vout, "mouse-clicked", val );
1304 playlist_t *p_playlist;
1306 var_Get( p_vout, "mouse-button-down", &val );
1308 var_Set( p_vout, "mouse-button-down", val );
1310 p_playlist = vlc_object_find( p_vout,
1311 VLC_OBJECT_PLAYLIST,
1313 if( p_playlist != NULL )
1316 var_Get( p_playlist, "intf-show", &val );
1317 val.b_bool = !val.b_bool;
1318 var_Set( p_playlist, "intf-show", val );
1319 vlc_object_release( p_playlist );
1326 intf_thread_t *p_intf;
1327 playlist_t *p_playlist;
1329 var_Get( p_vout, "mouse-button-down", &val );
1331 var_Set( p_vout, "mouse-button-down", val );
1332 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
1336 p_intf->b_menu_change = 1;
1337 vlc_object_release( p_intf );
1340 p_playlist = vlc_object_find( p_vout,
1341 VLC_OBJECT_PLAYLIST,
1343 if( p_playlist != NULL )
1345 vlc_value_t val; val.b_bool = VLC_TRUE;
1346 var_Set( p_playlist, "intf-popupmenu", val );
1347 vlc_object_release( p_playlist );
1353 var_Get( p_vout, "mouse-button-down", &val );
1355 var_Set( p_vout, "mouse-button-down", val );
1359 var_Get( p_vout, "mouse-button-down", &val );
1361 var_Set( p_vout, "mouse-button-down", val );
1367 else if( xevent.type == MotionNotify )
1369 unsigned int i_width, i_height, i_x, i_y;
1372 /* somewhat different use for vout_PlacePicture:
1373 * here the values are needed to give to mouse coordinates
1374 * in the original picture space */
1375 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1376 p_vout->p_sys->p_win->i_height,
1377 &i_x, &i_y, &i_width, &i_height );
1379 /* Compute the x coordinate and check if the value is
1380 in [0,p_vout->fmt_in.i_visible_width] */
1381 val.i_int = ( xevent.xmotion.x - i_x ) *
1382 p_vout->fmt_in.i_visible_width / i_width +
1383 p_vout->fmt_in.i_x_offset;
1385 if( (int)(xevent.xmotion.x - i_x) < 0 )
1387 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1388 val.i_int = p_vout->fmt_in.i_visible_width;
1390 var_Set( p_vout, "mouse-x", val );
1392 /* compute the y coordinate and check if the value is
1393 in [0,p_vout->fmt_in.i_visible_height] */
1394 val.i_int = ( xevent.xmotion.y - i_y ) *
1395 p_vout->fmt_in.i_visible_height / i_height +
1396 p_vout->fmt_in.i_y_offset;
1398 if( (int)(xevent.xmotion.y - i_y) < 0 )
1400 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1401 val.i_int = p_vout->fmt_in.i_visible_height;
1403 var_Set( p_vout, "mouse-y", val );
1405 val.b_bool = VLC_TRUE;
1406 var_Set( p_vout, "mouse-moved", val );
1408 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1409 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1411 ToggleCursor( p_vout );
1414 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1415 || xevent.type == MapNotify
1416 || xevent.type == UnmapNotify )
1418 /* Ignore these events */
1420 else /* Other events */
1422 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1426 /* Handle events for video output sub-window */
1427 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1428 p_vout->p_sys->p_win->video_window,
1429 ExposureMask, &xevent ) == True )
1431 /* Window exposed (only handled if stream playback is paused) */
1432 if( xevent.type == Expose )
1434 if( ((XExposeEvent *)&xevent)->count == 0 )
1436 /* (if this is the last a collection of expose events...) */
1438 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1440 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1441 ->stream.control.i_status )
1443 /* XVideoDisplay( p_vout )*/;
1451 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1452 * are handled - according to the man pages, the format is always 32
1454 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1455 ClientMessage, &xevent ) )
1457 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1458 && ((Atom)xevent.xclient.data.l[0]
1459 == p_vout->p_sys->p_win->wm_delete_window ) )
1461 /* the user wants to close the window */
1462 playlist_t * p_playlist =
1463 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1465 if( p_playlist != NULL )
1467 playlist_Stop( p_playlist );
1468 vlc_object_release( p_playlist );
1476 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1478 vlc_value_t val_fs, val_ontop;
1480 /* Update the object variable and trigger callback */
1481 val_fs.b_bool = !p_vout->b_fullscreen;
1483 var_Set( p_vout, "fullscreen", val_fs );
1485 /* Disable "always on top" in fullscreen mode */
1486 var_Get( p_vout, "video-on-top", &val_ontop );
1487 if( val_ontop.b_bool )
1488 WindowOnTop( p_vout, val_fs.b_bool );
1490 ToggleFullScreen( p_vout );
1491 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1494 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1495 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1497 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1498 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1500 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1501 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1502 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1503 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1504 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1505 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1506 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1507 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1509 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1515 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1516 * the size flag inside the fullscreen routine)
1518 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1520 unsigned int i_width, i_height, i_x, i_y;
1522 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1524 #ifdef MODULE_NAME_IS_x11
1525 /* We need to signal the vout thread about the size change because it
1526 * is doing the rescaling */
1527 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1530 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1531 p_vout->p_sys->p_win->i_height,
1532 &i_x, &i_y, &i_width, &i_height );
1534 XMoveResizeWindow( p_vout->p_sys->p_display,
1535 p_vout->p_sys->p_win->video_window,
1536 i_x, i_y, i_width, i_height );
1539 /* Autohide Cursour */
1540 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
1542 /* Hide the mouse automatically */
1543 if( p_vout->p_sys->b_mouse_pointer_visible )
1545 ToggleCursor( p_vout );
1549 #ifdef MODULE_NAME_IS_xvmc
1550 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1554 if ( p_vout->p_sys->p_octx != NULL ) {
1555 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1556 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1557 msg_Err( p_vout, "Could not disable backlight blanking" );
1559 msg_Dbg( p_vout, "Backlight blanking disabled" );
1561 p_vout->p_sys->i_backlight_on_counter = 0;
1563 p_vout->p_sys->i_backlight_on_counter ++;
1568 vlc_mutex_unlock( &p_vout->p_sys->lock );
1572 /*****************************************************************************
1573 * EndVideo: terminate X11 video thread output method
1574 *****************************************************************************
1575 * Destroy the X11 XImages created by Init. It is called at the end of
1576 * the thread, but also each time the window is resized.
1577 *****************************************************************************/
1578 static void EndVideo( vout_thread_t *p_vout )
1582 /* Free the direct buffers we allocated */
1583 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1586 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1590 /* following functions are local */
1592 /*****************************************************************************
1593 * CreateWindow: open and set-up X11 main window
1594 *****************************************************************************/
1595 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1597 XSizeHints xsize_hints;
1598 XSetWindowAttributes xwindow_attributes;
1599 XGCValues xgcvalues;
1602 vlc_bool_t b_expose = VLC_FALSE;
1603 vlc_bool_t b_configure_notify = VLC_FALSE;
1604 vlc_bool_t b_map_notify = VLC_FALSE;
1607 /* Prepare window manager hints and properties */
1608 p_win->wm_protocols =
1609 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1610 p_win->wm_delete_window =
1611 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1613 /* Never have a 0-pixel-wide window */
1614 xsize_hints.min_width = 2;
1615 xsize_hints.min_height = 1;
1617 /* Prepare window attributes */
1618 xwindow_attributes.backing_store = Always; /* save the hidden part */
1619 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1620 p_vout->p_sys->i_screen);
1621 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1623 if( !p_vout->b_fullscreen )
1625 p_win->owner_window = (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
1626 &p_win->i_width, &p_win->i_height );
1628 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1629 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1630 xsize_hints.flags = PSize | PMinSize;
1632 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1634 xsize_hints.x = p_win->i_x;
1635 xsize_hints.y = p_win->i_y;
1636 xsize_hints.flags |= PPosition;
1641 /* Fullscreen window size and position */
1642 p_win->owner_window = 0;
1643 p_win->i_x = p_win->i_y = 0;
1645 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1647 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1650 if( !p_win->owner_window )
1652 /* Create the window and set hints - the window must receive
1653 * ConfigureNotify events, and until it is displayed, Expose and
1654 * MapNotify events. */
1656 p_win->base_window =
1657 XCreateWindow( p_vout->p_sys->p_display,
1658 DefaultRootWindow( p_vout->p_sys->p_display ),
1659 p_win->i_x, p_win->i_y,
1660 p_win->i_width, p_win->i_height,
1663 CWBackingStore | CWBackPixel | CWEventMask,
1664 &xwindow_attributes );
1666 var_Get( p_vout, "video-title", &val );
1667 if( !val.psz_string || !*val.psz_string )
1669 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1670 #ifdef MODULE_NAME_IS_x11
1671 VOUT_TITLE " (X11 output)"
1672 #elif defined(MODULE_NAME_IS_glx)
1673 VOUT_TITLE " (GLX output)"
1675 VOUT_TITLE " (XVideo output)"
1681 XStoreName( p_vout->p_sys->p_display,
1682 p_win->base_window, val.psz_string );
1684 if( val.psz_string ) free( val.psz_string );
1686 if( !p_vout->b_fullscreen )
1688 const char *argv[] = { "vlc", NULL };
1690 /* Set window manager hints and properties: size hints, command,
1691 * window's name, and accepted protocols */
1692 XSetWMNormalHints( p_vout->p_sys->p_display,
1693 p_win->base_window, &xsize_hints );
1694 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1697 if( !var_GetBool( p_vout, "video-deco") )
1700 mwmhints_t mwmhints;
1702 mwmhints.flags = MWM_HINTS_DECORATIONS;
1703 mwmhints.decorations = False;
1705 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1708 XChangeProperty( p_vout->p_sys->p_display,
1710 prop, prop, 32, PropModeReplace,
1711 (unsigned char *)&mwmhints,
1712 PROP_MWM_HINTS_ELEMENTS );
1720 unsigned int dummy4, dummy5;
1722 /* Select events we are interested in. */
1723 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1724 StructureNotifyMask );
1726 /* Get the parent window's geometry information */
1727 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1728 &dummy1, &dummy2, &dummy3,
1733 /* We are already configured */
1734 b_configure_notify = VLC_TRUE;
1736 /* From man XSelectInput: only one client at a time can select a
1737 * ButtonPress event, so we need to open a new window anyway. */
1738 p_win->base_window =
1739 XCreateWindow( p_vout->p_sys->p_display,
1740 p_win->owner_window,
1742 p_win->i_width, p_win->i_height,
1744 0, CopyFromParent, 0,
1745 CWBackingStore | CWBackPixel | CWEventMask,
1746 &xwindow_attributes );
1749 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1750 || (p_win->wm_delete_window == None)
1751 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1752 &p_win->wm_delete_window, 1 ) )
1754 /* WM_DELETE_WINDOW is not supported by window manager */
1755 msg_Warn( p_vout, "missing or bad window manager" );
1758 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1759 * event when using functions like XCopyArea */
1760 xgcvalues.graphics_exposures = False;
1761 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1763 GCGraphicsExposures, &xgcvalues );
1765 /* Send orders to server, and wait until window is displayed - three
1766 * events must be received: a MapNotify event, an Expose event allowing
1767 * drawing in the window, and a ConfigureNotify to get the window
1768 * dimensions. Once those events have been received, only
1769 * ConfigureNotify events need to be received. */
1770 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1773 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1774 SubstructureNotifyMask | StructureNotifyMask |
1775 ExposureMask, &xevent);
1776 if( (xevent.type == Expose)
1777 && (xevent.xexpose.window == p_win->base_window) )
1779 b_expose = VLC_TRUE;
1780 /* ConfigureNotify isn't sent if there isn't a window manager.
1781 * Expose should be the last event to be received so it should
1782 * be fine to assume we won't receive it anymore. */
1783 b_configure_notify = VLC_TRUE;
1785 else if( (xevent.type == MapNotify)
1786 && (xevent.xmap.window == p_win->base_window) )
1788 b_map_notify = VLC_TRUE;
1790 else if( (xevent.type == ConfigureNotify)
1791 && (xevent.xconfigure.window == p_win->base_window) )
1793 b_configure_notify = VLC_TRUE;
1794 p_win->i_width = xevent.xconfigure.width;
1795 p_win->i_height = xevent.xconfigure.height;
1797 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1799 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1800 StructureNotifyMask | KeyPressMask |
1801 ButtonPressMask | ButtonReleaseMask |
1802 PointerMotionMask );
1804 #ifdef MODULE_NAME_IS_x11
1805 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1807 /* Allocate a new palette */
1808 p_vout->p_sys->colormap =
1809 XCreateColormap( p_vout->p_sys->p_display,
1810 DefaultRootWindow( p_vout->p_sys->p_display ),
1811 DefaultVisual( p_vout->p_sys->p_display,
1812 p_vout->p_sys->i_screen ),
1815 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1816 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1817 CWColormap, &xwindow_attributes );
1821 /* Create video output sub-window. */
1822 p_win->video_window = XCreateSimpleWindow(
1823 p_vout->p_sys->p_display,
1824 p_win->base_window, 0, 0,
1825 p_win->i_width, p_win->i_height,
1827 BlackPixel( p_vout->p_sys->p_display,
1828 p_vout->p_sys->i_screen ),
1829 WhitePixel( p_vout->p_sys->p_display,
1830 p_vout->p_sys->i_screen ) );
1832 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1833 BlackPixel( p_vout->p_sys->p_display,
1834 p_vout->p_sys->i_screen ) );
1836 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1837 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1840 /* make sure the video window will be centered in the next ManageVideo() */
1841 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1843 /* If the cursor was formerly blank than blank it again */
1844 if( !p_vout->p_sys->b_mouse_pointer_visible )
1846 ToggleCursor( p_vout );
1847 ToggleCursor( p_vout );
1850 /* Do NOT use XFlush here ! */
1851 XSync( p_vout->p_sys->p_display, False );
1853 /* At this stage, the window is open, displayed, and ready to
1855 p_vout->p_sys->p_win = p_win;
1860 /*****************************************************************************
1861 * DestroyWindow: destroy the window
1862 *****************************************************************************
1864 *****************************************************************************/
1865 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1867 /* Do NOT use XFlush here ! */
1868 XSync( p_vout->p_sys->p_display, False );
1870 if( p_win->video_window != None )
1871 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1873 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1875 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1876 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1878 if( p_win->owner_window )
1879 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1882 /*****************************************************************************
1883 * NewPicture: allocate a picture
1884 *****************************************************************************
1885 * Returns 0 on success, -1 otherwise
1886 *****************************************************************************/
1887 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1889 #ifndef MODULE_NAME_IS_glx
1891 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1895 /* We know the chroma, allocate a buffer which will be used
1896 * directly by the decoder */
1897 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1899 if( p_pic->p_sys == NULL )
1904 #ifdef MODULE_NAME_IS_xvmc
1905 p_pic->p_sys->p_vout = p_vout;
1906 p_pic->p_sys->xvmc_surf = NULL;
1907 p_pic->p_sys->xxmc_data.decoded = 0;
1908 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1909 // p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1910 p_pic->p_sys->nb_display = 0;
1913 /* Fill in picture_t fields */
1914 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1915 p_vout->output.i_width, p_vout->output.i_height,
1916 p_vout->output.i_aspect );
1918 #ifdef HAVE_SYS_SHM_H
1919 if( p_vout->p_sys->i_shm_opcode )
1921 /* Create image using XShm extension */
1922 p_pic->p_sys->p_image =
1923 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1924 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1925 p_vout->p_sys->i_xvport,
1926 VLC2X11_FOURCC(p_vout->output.i_chroma),
1928 p_vout->p_sys->p_visual,
1929 p_vout->p_sys->i_screen_depth,
1931 &p_pic->p_sys->shminfo,
1932 p_vout->output.i_width, p_vout->output.i_height );
1935 if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1936 #endif /* HAVE_SYS_SHM_H */
1938 /* Create image without XShm extension */
1939 p_pic->p_sys->p_image =
1940 CreateImage( p_vout, p_vout->p_sys->p_display,
1941 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1942 p_vout->p_sys->i_xvport,
1943 VLC2X11_FOURCC(p_vout->output.i_chroma),
1944 p_pic->format.i_bits_per_pixel,
1946 p_vout->p_sys->p_visual,
1947 p_vout->p_sys->i_screen_depth,
1948 p_vout->p_sys->i_bytes_per_pixel,
1950 p_vout->output.i_width, p_vout->output.i_height );
1952 #ifdef HAVE_SYS_SHM_H
1953 if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1955 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1956 p_vout->p_sys->i_shm_opcode = 0;
1958 #endif /* HAVE_SYS_SHM_H */
1961 if( p_pic->p_sys->p_image == NULL )
1963 free( p_pic->p_sys );
1967 switch( p_vout->output.i_chroma )
1969 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1970 case VLC_FOURCC('I','4','2','0'):
1971 case VLC_FOURCC('Y','V','1','2'):
1972 case VLC_FOURCC('Y','2','1','1'):
1973 case VLC_FOURCC('Y','U','Y','2'):
1974 case VLC_FOURCC('U','Y','V','Y'):
1975 case VLC_FOURCC('R','V','1','5'):
1976 case VLC_FOURCC('R','V','1','6'):
1977 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1978 case VLC_FOURCC('R','V','3','2'):
1980 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1983 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1984 + p_pic->p_sys->p_image->offsets[i_plane];
1985 p_pic->p[i_plane].i_pitch =
1986 p_pic->p_sys->p_image->pitches[i_plane];
1988 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1990 /* U and V inverted compared to I420
1991 * Fixme: this should be handled by the vout core */
1992 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1993 + p_pic->p_sys->p_image->offsets[2];
1994 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1995 + p_pic->p_sys->p_image->offsets[1];
2000 case VLC_FOURCC('R','G','B','2'):
2001 case VLC_FOURCC('R','V','1','6'):
2002 case VLC_FOURCC('R','V','1','5'):
2003 case VLC_FOURCC('R','V','2','4'):
2004 case VLC_FOURCC('R','V','3','2'):
2006 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
2007 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
2008 p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
2009 + p_pic->p_sys->p_image->xoffset;
2010 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
2012 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
2013 * properly by vout_InitPicture() */
2014 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
2015 * p_pic->p_sys->p_image->width;
2020 /* Unknown chroma, tell the guy to get lost */
2021 IMAGE_FREE( p_pic->p_sys->p_image );
2022 free( p_pic->p_sys );
2023 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
2024 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
2025 p_pic->i_planes = 0;
2030 VLC_UNUSED(p_vout); VLC_UNUSED(p_pic);
2032 #endif /* !MODULE_NAME_IS_glx */
2037 /*****************************************************************************
2038 * FreePicture: destroy a picture allocated with NewPicture
2039 *****************************************************************************
2040 * Destroy XImage AND associated data. If using Shm, detach shared memory
2041 * segment from server and process, then free it. The XDestroyImage manpage
2042 * says that both the image structure _and_ the data pointed to by the
2043 * image structure are freed, so no need to free p_image->data.
2044 *****************************************************************************/
2045 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2047 /* The order of operations is correct */
2048 #ifdef HAVE_SYS_SHM_H
2049 if( p_vout->p_sys->i_shm_opcode )
2051 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2052 IMAGE_FREE( p_pic->p_sys->p_image );
2054 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2055 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2057 msg_Err( p_vout, "cannot detach shared memory (%m)" );
2063 IMAGE_FREE( p_pic->p_sys->p_image );
2066 #ifdef MODULE_NAME_IS_xvmc
2067 if( p_pic->p_sys->xvmc_surf != NULL )
2069 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2070 p_pic->p_sys->xvmc_surf = NULL;
2074 /* Do NOT use XFlush here ! */
2075 XSync( p_vout->p_sys->p_display, False );
2077 free( p_pic->p_sys );
2080 /*****************************************************************************
2081 * ToggleFullScreen: Enable or disable full screen mode
2082 *****************************************************************************
2083 * This function will switch between fullscreen and window mode.
2084 *****************************************************************************/
2085 static void ToggleFullScreen ( vout_thread_t *p_vout )
2089 mwmhints_t mwmhints;
2090 XSetWindowAttributes attributes;
2092 #ifdef HAVE_XINERAMA
2096 p_vout->b_fullscreen = !p_vout->b_fullscreen;
2098 if( p_vout->b_fullscreen )
2100 msg_Dbg( p_vout, "entering fullscreen mode" );
2102 p_vout->p_sys->b_altfullscreen =
2103 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2105 XUnmapWindow( p_vout->p_sys->p_display,
2106 p_vout->p_sys->p_win->base_window );
2108 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2110 CreateWindow( p_vout, p_vout->p_sys->p_win );
2111 XDestroyWindow( p_vout->p_sys->p_display,
2112 p_vout->p_sys->fullscreen_window.video_window );
2113 XReparentWindow( p_vout->p_sys->p_display,
2114 p_vout->p_sys->original_window.video_window,
2115 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2116 p_vout->p_sys->fullscreen_window.video_window =
2117 p_vout->p_sys->original_window.video_window;
2119 /* To my knowledge there are two ways to create a borderless window.
2120 * There's the generic way which is to tell x to bypass the window
2121 * manager, but this creates problems with the focus of other
2123 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2124 * luckily seems to be supported by most window managers. */
2125 if( !p_vout->p_sys->b_altfullscreen )
2127 mwmhints.flags = MWM_HINTS_DECORATIONS;
2128 mwmhints.decorations = False;
2130 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2132 XChangeProperty( p_vout->p_sys->p_display,
2133 p_vout->p_sys->p_win->base_window,
2134 prop, prop, 32, PropModeReplace,
2135 (unsigned char *)&mwmhints,
2136 PROP_MWM_HINTS_ELEMENTS );
2140 /* brute force way to remove decorations */
2141 attributes.override_redirect = True;
2142 XChangeWindowAttributes( p_vout->p_sys->p_display,
2143 p_vout->p_sys->p_win->base_window,
2147 /* Make sure the change is effective */
2148 XReparentWindow( p_vout->p_sys->p_display,
2149 p_vout->p_sys->p_win->base_window,
2150 DefaultRootWindow( p_vout->p_sys->p_display ),
2154 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2156 XClientMessageEvent event;
2158 memset( &event, 0, sizeof( XClientMessageEvent ) );
2160 event.type = ClientMessage;
2161 event.message_type = p_vout->p_sys->net_wm_state;
2162 event.display = p_vout->p_sys->p_display;
2163 event.window = p_vout->p_sys->p_win->base_window;
2165 event.data.l[ 0 ] = 1; /* set property */
2166 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2168 XSendEvent( p_vout->p_sys->p_display,
2169 DefaultRootWindow( p_vout->p_sys->p_display ),
2170 False, SubstructureRedirectMask,
2174 /* Make sure the change is effective */
2175 XReparentWindow( p_vout->p_sys->p_display,
2176 p_vout->p_sys->p_win->base_window,
2177 DefaultRootWindow( p_vout->p_sys->p_display ),
2180 #ifdef HAVE_XINERAMA
2181 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2182 XineramaIsActive( p_vout->p_sys->p_display ) )
2184 XineramaScreenInfo *screens; /* infos for xinerama */
2187 msg_Dbg( p_vout, "using XFree Xinerama extension");
2189 #define SCREEN p_vout->p_sys->p_win->i_screen
2191 /* Get Information about Xinerama (num of screens) */
2192 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2195 SCREEN = config_GetInt( p_vout,
2196 MODULE_STRING "-xineramascreen" );
2198 /* just check that user has entered a good value */
2199 if( SCREEN >= i_num_screens || SCREEN < 0 )
2201 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2205 /* Get the X/Y upper left corner coordinate of the above screen */
2206 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2207 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2209 /* Set the Height/width to the screen resolution */
2210 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2211 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2221 /* The window wasn't necessarily created at the requested size */
2222 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2224 #ifdef HAVE_XF86VIDMODE
2225 XF86VidModeModeLine mode;
2228 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2229 p_vout->p_sys->i_screen, &i_dummy,
2232 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2233 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2235 /* move cursor to the middle of the window to prevent
2236 * unwanted display move if the display is smaller than the
2238 XWarpPointer( p_vout->p_sys->p_display, None,
2239 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2240 mode.hdisplay / 2 , mode.vdisplay / 2 );
2241 /* force desktop view to upper left corner */
2242 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2243 p_vout->p_sys->i_screen, 0, 0 );
2248 p_vout->p_sys->p_win->i_width =
2249 DisplayWidth( p_vout->p_sys->p_display,
2250 p_vout->p_sys->i_screen );
2251 p_vout->p_sys->p_win->i_height =
2252 DisplayHeight( p_vout->p_sys->p_display,
2253 p_vout->p_sys->i_screen );
2258 XMoveResizeWindow( p_vout->p_sys->p_display,
2259 p_vout->p_sys->p_win->base_window,
2260 p_vout->p_sys->p_win->i_x,
2261 p_vout->p_sys->p_win->i_y,
2262 p_vout->p_sys->p_win->i_width,
2263 p_vout->p_sys->p_win->i_height );
2266 EnablePixelDoubling( p_vout );
2269 /* Activate the window (give it the focus) */
2270 XClientMessageEvent event;
2272 memset( &event, 0, sizeof( XClientMessageEvent ) );
2274 event.type = ClientMessage;
2275 event.message_type =
2276 XInternAtom( p_vout->p_sys->p_display, "_NET_ACTIVE_WINDOW", False );
2277 event.display = p_vout->p_sys->p_display;
2278 event.window = p_vout->p_sys->p_win->base_window;
2280 event.data.l[ 0 ] = 1; /* source indication (1 = from an application */
2281 event.data.l[ 1 ] = 0; /* timestamp */
2282 event.data.l[ 2 ] = 0; /* requestor's currently active window */
2283 /* XXX: window manager would be more likely to obey if we already have
2284 * an active window (and give it to the event), such as an interface */
2286 XSendEvent( p_vout->p_sys->p_display,
2287 DefaultRootWindow( p_vout->p_sys->p_display ),
2288 False, SubstructureRedirectMask,
2293 msg_Dbg( p_vout, "leaving fullscreen mode" );
2296 DisablePixelDoubling( p_vout );
2299 XReparentWindow( p_vout->p_sys->p_display,
2300 p_vout->p_sys->original_window.video_window,
2301 p_vout->p_sys->original_window.base_window, 0, 0 );
2303 p_vout->p_sys->fullscreen_window.video_window = None;
2304 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2305 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2307 XMapWindow( p_vout->p_sys->p_display,
2308 p_vout->p_sys->p_win->base_window );
2311 /* Unfortunately, using XSync() here is not enough to ensure the
2312 * window has already been mapped because the XMapWindow() request
2313 * has not necessarily been sent directly to our window (remember,
2314 * the call is first redirected to the window manager) */
2317 XWindowEvent( p_vout->p_sys->p_display,
2318 p_vout->p_sys->p_win->base_window,
2319 StructureNotifyMask, &xevent );
2320 } while( xevent.type != MapNotify );
2322 /* Be careful, this can generate a BadMatch error if the window is not
2323 * already mapped by the server (see above) */
2324 XSetInputFocus(p_vout->p_sys->p_display,
2325 p_vout->p_sys->p_win->base_window,
2329 /* signal that the size needs to be updated */
2330 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2333 /*****************************************************************************
2334 * EnableXScreenSaver: enable screen saver
2335 *****************************************************************************
2336 * This function enables the screen saver on a display after it has been
2337 * disabled by XDisableScreenSaver.
2338 * FIXME: what happens if multiple vlc sessions are running at the same
2340 *****************************************************************************/
2341 static void EnableXScreenSaver( vout_thread_t *p_vout )
2343 #ifdef DPMSINFO_IN_DPMS_H
2347 if( p_vout->p_sys->i_ss_timeout )
2349 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2350 p_vout->p_sys->i_ss_interval,
2351 p_vout->p_sys->i_ss_blanking,
2352 p_vout->p_sys->i_ss_exposure );
2355 /* Restore DPMS settings */
2356 #ifdef DPMSINFO_IN_DPMS_H
2357 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2359 if( p_vout->p_sys->b_ss_dpms )
2361 DPMSEnable( p_vout->p_sys->p_display );
2367 /*****************************************************************************
2368 * DisableXScreenSaver: disable screen saver
2369 *****************************************************************************
2370 * See XEnableXScreenSaver
2371 *****************************************************************************/
2372 static void DisableXScreenSaver( vout_thread_t *p_vout )
2374 #ifdef DPMSINFO_IN_DPMS_H
2378 /* Save screen saver information */
2379 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2380 &p_vout->p_sys->i_ss_interval,
2381 &p_vout->p_sys->i_ss_blanking,
2382 &p_vout->p_sys->i_ss_exposure );
2384 /* Disable screen saver */
2385 if( p_vout->p_sys->i_ss_timeout )
2387 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2388 p_vout->p_sys->i_ss_interval,
2389 p_vout->p_sys->i_ss_blanking,
2390 p_vout->p_sys->i_ss_exposure );
2394 #ifdef DPMSINFO_IN_DPMS_H
2395 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2398 /* Save DPMS current state */
2399 DPMSInfo( p_vout->p_sys->p_display, &unused,
2400 &p_vout->p_sys->b_ss_dpms );
2401 DPMSDisable( p_vout->p_sys->p_display );
2406 /*****************************************************************************
2407 * CreateCursor: create a blank mouse pointer
2408 *****************************************************************************/
2409 static void CreateCursor( vout_thread_t *p_vout )
2411 XColor cursor_color;
2413 p_vout->p_sys->cursor_pixmap =
2414 XCreatePixmap( p_vout->p_sys->p_display,
2415 DefaultRootWindow( p_vout->p_sys->p_display ),
2418 XParseColor( p_vout->p_sys->p_display,
2419 XCreateColormap( p_vout->p_sys->p_display,
2421 p_vout->p_sys->p_display ),
2423 p_vout->p_sys->p_display,
2424 p_vout->p_sys->i_screen ),
2426 "black", &cursor_color );
2428 p_vout->p_sys->blank_cursor =
2429 XCreatePixmapCursor( p_vout->p_sys->p_display,
2430 p_vout->p_sys->cursor_pixmap,
2431 p_vout->p_sys->cursor_pixmap,
2432 &cursor_color, &cursor_color, 1, 1 );
2435 /*****************************************************************************
2436 * DestroyCursor: destroy the blank mouse pointer
2437 *****************************************************************************/
2438 static void DestroyCursor( vout_thread_t *p_vout )
2440 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2443 /*****************************************************************************
2444 * ToggleCursor: hide or show the mouse pointer
2445 *****************************************************************************
2446 * This function hides the X pointer if it is visible by setting the pointer
2447 * sprite to a blank one. To show it again, we disable the sprite.
2448 *****************************************************************************/
2449 static void ToggleCursor( vout_thread_t *p_vout )
2451 if( p_vout->p_sys->b_mouse_pointer_visible )
2453 XDefineCursor( p_vout->p_sys->p_display,
2454 p_vout->p_sys->p_win->base_window,
2455 p_vout->p_sys->blank_cursor );
2456 p_vout->p_sys->b_mouse_pointer_visible = 0;
2460 XUndefineCursor( p_vout->p_sys->p_display,
2461 p_vout->p_sys->p_win->base_window );
2462 p_vout->p_sys->b_mouse_pointer_visible = 1;
2466 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2467 /*****************************************************************************
2468 * XVideoGetPort: get YUV12 port
2469 *****************************************************************************/
2470 static int XVideoGetPort( vout_thread_t *p_vout,
2471 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
2473 XvAdaptorInfo *p_adaptor;
2475 unsigned int i_adaptor, i_num_adaptors;
2476 int i_requested_adaptor;
2477 int i_selected_port;
2479 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2484 case XvBadExtension:
2485 msg_Warn( p_vout, "XvBadExtension" );
2489 msg_Warn( p_vout, "XvBadAlloc" );
2493 msg_Warn( p_vout, "XvQueryExtension failed" );
2497 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2498 DefaultRootWindow( p_vout->p_sys->p_display ),
2499 &i_num_adaptors, &p_adaptor ) )
2504 case XvBadExtension:
2505 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2509 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2513 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2517 i_selected_port = -1;
2518 #ifdef MODULE_NAME_IS_xvmc
2519 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2521 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2523 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2525 XvImageFormatValues *p_formats;
2526 int i_format, i_num_formats;
2529 /* If we requested an adaptor and it's not this one, we aren't
2531 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2536 /* If the adaptor doesn't have the required properties, skip it */
2537 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2538 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2543 /* Check that adaptor supports our requested format... */
2544 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2545 p_adaptor[i_adaptor].base_id,
2549 i_format < i_num_formats && ( i_selected_port == -1 );
2552 XvAttribute *p_attr;
2553 int i_attr, i_num_attributes;
2555 /* If this is not the format we want, or at least a
2556 * similar one, forget it */
2557 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2562 /* Look for the first available port supporting this format */
2563 for( i_port = p_adaptor[i_adaptor].base_id;
2564 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2565 + p_adaptor[i_adaptor].num_ports) )
2566 && ( i_selected_port == -1 );
2569 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2572 i_selected_port = i_port;
2573 *pi_newchroma = p_formats[ i_format ].id;
2577 /* If no free port was found, forget it */
2578 if( i_selected_port == -1 )
2583 /* If we found a port, print information about it */
2584 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2585 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2586 (char *)&p_formats[ i_format ].id,
2587 ( p_formats[ i_format ].format == XvPacked ) ?
2588 "packed" : "planar" );
2590 /* Make sure XV_AUTOPAINT_COLORKEY is set */
2591 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2593 &i_num_attributes );
2595 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2597 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2599 const Atom autopaint =
2600 XInternAtom( p_vout->p_sys->p_display,
2601 "XV_AUTOPAINT_COLORKEY", False );
2602 XvSetPortAttribute( p_vout->p_sys->p_display,
2603 i_selected_port, autopaint, 1 );
2608 if( p_attr != NULL )
2614 if( p_formats != NULL )
2621 if( i_num_adaptors > 0 )
2623 XvFreeAdaptorInfo( p_adaptor );
2626 if( i_selected_port == -1 )
2628 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2629 if( i_requested_adaptor == -1 )
2631 msg_Warn( p_vout, "no free XVideo port found for format "
2632 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2636 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2637 "XVideo port for format 0x%.8x (%4.4s)",
2638 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2642 return i_selected_port;
2645 /*****************************************************************************
2646 * XVideoReleasePort: release YUV12 port
2647 *****************************************************************************/
2648 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2650 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2654 /*****************************************************************************
2655 * InitDisplay: open and initialize X11 device
2656 *****************************************************************************
2657 * Create a window according to video output given size, and set other
2658 * properties according to the display properties.
2659 *****************************************************************************/
2660 static int InitDisplay( vout_thread_t *p_vout )
2662 #ifdef MODULE_NAME_IS_x11
2663 XPixmapFormatValues * p_formats; /* pixmap formats */
2664 XVisualInfo * p_xvisual; /* visuals information */
2665 XVisualInfo xvisual_template; /* visual template */
2666 int i_count, i; /* array size */
2669 #ifdef HAVE_SYS_SHM_H
2670 p_vout->p_sys->i_shm_opcode = 0;
2672 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
2674 int major, evt, err;
2676 if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2678 && XShmQueryExtension( p_vout->p_sys->p_display ) )
2679 p_vout->p_sys->i_shm_opcode = major;
2681 if( p_vout->p_sys->i_shm_opcode )
2686 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2688 msg_Dbg( p_vout, "XShm video extension v%d.%d "
2689 "(with%s pixmaps, opcode: %d)",
2690 major, minor, pixmaps ? "" : "out",
2691 p_vout->p_sys->i_shm_opcode );
2694 msg_Warn( p_vout, "XShm video extension not available" );
2697 msg_Dbg( p_vout, "XShm video extension disabled" );
2700 #ifdef MODULE_NAME_IS_xvideo
2701 /* XXX The brightness and contrast values should be read from environment
2702 * XXX variables... */
2704 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2705 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2709 #ifdef MODULE_NAME_IS_x11
2710 /* Initialize structure */
2711 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2713 /* Get screen depth */
2714 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2715 p_vout->p_sys->i_screen );
2716 switch( p_vout->p_sys->i_screen_depth )
2720 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2722 xvisual_template.screen = p_vout->p_sys->i_screen;
2723 xvisual_template.class = DirectColor;
2724 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2725 VisualScreenMask | VisualClassMask,
2726 &xvisual_template, &i_count );
2727 if( p_xvisual == NULL )
2729 msg_Err( p_vout, "no PseudoColor visual available" );
2730 return VLC_EGENERIC;
2732 p_vout->p_sys->i_bytes_per_pixel = 1;
2733 p_vout->output.pf_setpalette = SetPalette;
2740 * Screen depth is higher than 8bpp. TrueColor visual is used.
2742 xvisual_template.screen = p_vout->p_sys->i_screen;
2743 xvisual_template.class = TrueColor;
2744 /* In some cases, we get a truecolor class adaptor that has a different
2745 color depth. So try to get a real true color one first */
2746 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2748 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2749 VisualScreenMask | VisualClassMask |
2751 &xvisual_template, &i_count );
2752 if( p_xvisual == NULL )
2754 msg_Warn( p_vout, "No screen matching the required color depth" );
2755 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2756 VisualScreenMask | VisualClassMask,
2757 &xvisual_template, &i_count );
2758 if( p_xvisual == NULL )
2761 msg_Err( p_vout, "no TrueColor visual available" );
2762 return VLC_EGENERIC;
2766 p_vout->output.i_rmask = p_xvisual->red_mask;
2767 p_vout->output.i_gmask = p_xvisual->green_mask;
2768 p_vout->output.i_bmask = p_xvisual->blue_mask;
2770 /* There is no difference yet between 3 and 4 Bpp. The only way
2771 * to find the actual number of bytes per pixel is to list supported
2772 * pixmap formats. */
2773 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2774 p_vout->p_sys->i_bytes_per_pixel = 0;
2776 for( i = 0; i < i_count; i++ )
2778 /* Under XFree4.0, the list contains pixmap formats available
2779 * through all video depths ; so we have to check against current
2781 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2783 if( p_formats[i].bits_per_pixel / 8
2784 > (int)p_vout->p_sys->i_bytes_per_pixel )
2786 p_vout->p_sys->i_bytes_per_pixel =
2787 p_formats[i].bits_per_pixel / 8;
2791 if( p_formats ) XFree( p_formats );
2795 p_vout->p_sys->p_visual = p_xvisual->visual;
2802 #ifndef MODULE_NAME_IS_glx
2804 #ifdef HAVE_SYS_SHM_H
2805 /*****************************************************************************
2806 * CreateShmImage: create an XImage or XvImage using shared memory extension
2807 *****************************************************************************
2808 * Prepare an XImage or XvImage for display function.
2809 * The order of the operations respects the recommandations of the mit-shm
2810 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2811 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2812 *****************************************************************************/
2813 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2814 Display* p_display, EXTRA_ARGS_SHM,
2815 int i_width, int i_height )
2817 IMAGE_TYPE *p_image;
2820 /* Create XImage / XvImage */
2821 #ifdef MODULE_NAME_IS_xvideo
2823 /* Make sure the buffer is aligned to multiple of 16 */
2824 i_height = ( i_height + 15 ) >> 4 << 4;
2825 i_width = ( i_width + 15 ) >> 4 << 4;
2827 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2828 i_width, i_height, p_shm );
2829 #elif defined(MODULE_NAME_IS_xvmc)
2830 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2831 i_width, i_height, p_shm );
2833 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2834 p_shm, i_width, i_height );
2836 if( p_image == NULL )
2838 msg_Err( p_vout, "image creation failed" );
2842 /* Allocate shared memory segment - 0776 set the access permission
2843 * rights (like umask), they are not yet supported by all X servers */
2844 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2845 if( p_shm->shmid < 0 )
2847 msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2848 IMAGE_FREE( p_image );
2852 /* Attach shared memory segment to process (read/write) */
2853 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2854 if(! p_shm->shmaddr )
2856 msg_Err( p_vout, "cannot attach shared memory (%m)" );
2857 IMAGE_FREE( p_image );
2858 shmctl( p_shm->shmid, IPC_RMID, 0 );
2862 /* Read-only data. We won't be using XShmGetImage */
2863 p_shm->readOnly = True;
2865 /* Attach shared memory segment to X server */
2866 XSynchronize( p_display, True );
2867 i_shm_major = p_vout->p_sys->i_shm_opcode;
2868 result = XShmAttach( p_display, p_shm );
2869 if( result == False || !i_shm_major )
2871 msg_Err( p_vout, "cannot attach shared memory to X server" );
2872 IMAGE_FREE( p_image );
2873 shmctl( p_shm->shmid, IPC_RMID, 0 );
2874 shmdt( p_shm->shmaddr );
2877 XSynchronize( p_display, False );
2879 /* Send image to X server. This instruction is required, since having
2880 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2881 * and remember NOT to use XFlush ! */
2882 XSync( p_display, False );
2885 /* Mark the shm segment to be removed when there are no more
2886 * attachements, so it is automatic on process exit or after shmdt */
2887 shmctl( p_shm->shmid, IPC_RMID, 0 );
2894 /*****************************************************************************
2895 * CreateImage: create an XImage or XvImage
2896 *****************************************************************************
2897 * Create a simple image used as a buffer.
2898 *****************************************************************************/
2899 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2900 Display *p_display, EXTRA_ARGS,
2901 int i_width, int i_height )
2903 byte_t * p_data; /* image data storage zone */
2904 IMAGE_TYPE *p_image;
2905 #ifdef MODULE_NAME_IS_x11
2906 int i_quantum; /* XImage quantum (see below) */
2907 int i_bytes_per_line;
2910 /* Allocate memory for image */
2911 #ifdef MODULE_NAME_IS_xvideo
2913 /* Make sure the buffer is aligned to multiple of 16 */
2914 i_height = ( i_height + 15 ) >> 4 << 4;
2915 i_width = ( i_width + 15 ) >> 4 << 4;
2917 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2918 #elif defined(MODULE_NAME_IS_x11)
2919 i_bytes_per_line = i_width * i_bytes_per_pixel;
2920 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2924 msg_Err( p_vout, "out of memory" );
2928 #ifdef MODULE_NAME_IS_x11
2929 /* Optimize the quantum of a scanline regarding its size - the quantum is
2930 a diviser of the number of bits between the start of two scanlines. */
2931 if( i_bytes_per_line & 0xf )
2935 else if( i_bytes_per_line & 0x10 )
2945 /* Create XImage. p_data will be automatically freed */
2946 #ifdef MODULE_NAME_IS_xvideo
2947 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2948 (char *)p_data, i_width, i_height );
2949 #elif defined(MODULE_NAME_IS_x11)
2950 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2951 (char *)p_data, i_width, i_height, i_quantum, 0 );
2953 if( p_image == NULL )
2955 msg_Err( p_vout, "XCreateImage() failed" );
2964 /*****************************************************************************
2965 * X11ErrorHandler: replace error handler so we can intercept some of them
2966 *****************************************************************************/
2967 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2971 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
2973 "[????????] x11 video output error: X11 request %u.%u failed "
2974 "with error code %u:\n %s\n",
2975 event->request_code, event->minor_code, event->error_code, txt );
2977 switch( event->request_code )
2979 case X_SetInputFocus:
2980 /* Ignore errors on XSetInputFocus()
2981 * (they happen when a window is not yet mapped) */
2985 #ifdef HAVE_SYS_SHM_H
2986 if( event->request_code == i_shm_major ) /* MIT-SHM */
2987 return i_shm_major = 0;
2990 XSetErrorHandler(NULL);
2991 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2994 #ifdef MODULE_NAME_IS_x11
2995 /*****************************************************************************
2996 * SetPalette: sets an 8 bpp palette
2997 *****************************************************************************
2998 * This function sets the palette given as an argument. It does not return
2999 * anything, but could later send information on which colors it was unable
3001 *****************************************************************************/
3002 static void SetPalette( vout_thread_t *p_vout,
3003 uint16_t *red, uint16_t *green, uint16_t *blue )
3006 XColor p_colors[255];
3008 /* allocate palette */
3009 for( i = 0; i < 255; i++ )
3011 /* kludge: colors are indexed reversely because color 255 seems
3012 * to be reserved for black even if we try to set it to white */
3013 p_colors[ i ].pixel = 255 - i;
3014 p_colors[ i ].pad = 0;
3015 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
3016 p_colors[ i ].red = red[ 255 - i ];
3017 p_colors[ i ].blue = blue[ 255 - i ];
3018 p_colors[ i ].green = green[ 255 - i ];
3021 XStoreColors( p_vout->p_sys->p_display,
3022 p_vout->p_sys->colormap, p_colors, 255 );
3026 /*****************************************************************************
3027 * Control: control facility for the vout
3028 *****************************************************************************/
3029 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
3032 unsigned int i_width, i_height;
3033 unsigned int *pi_width, *pi_height;
3039 if( p_vout->p_sys->p_win->owner_window )
3040 return vout_ControlWindow( p_vout,
3041 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3043 pi_width = va_arg( args, unsigned int * );
3044 pi_height = va_arg( args, unsigned int * );
3046 vlc_mutex_lock( &p_vout->p_sys->lock );
3047 *pi_width = p_vout->p_sys->p_win->i_width;
3048 *pi_height = p_vout->p_sys->p_win->i_height;
3049 vlc_mutex_unlock( &p_vout->p_sys->lock );
3053 if( p_vout->p_sys->p_win->owner_window )
3054 return vout_ControlWindow( p_vout,
3055 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3057 vlc_mutex_lock( &p_vout->p_sys->lock );
3059 i_width = va_arg( args, unsigned int );
3060 i_height = va_arg( args, unsigned int );
3061 if( !i_width ) i_width = p_vout->i_window_width;
3062 if( !i_height ) i_height = p_vout->i_window_height;
3064 #ifdef MODULE_NAME_IS_xvmc
3065 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3067 /* Update dimensions */
3068 XResizeWindow( p_vout->p_sys->p_display,
3069 p_vout->p_sys->p_win->base_window,
3070 i_width, i_height );
3071 #ifdef MODULE_NAME_IS_xvmc
3072 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3074 vlc_mutex_unlock( &p_vout->p_sys->lock );
3078 vlc_mutex_lock( &p_vout->p_sys->lock );
3079 XUnmapWindow( p_vout->p_sys->p_display,
3080 p_vout->p_sys->original_window.base_window );
3081 vlc_mutex_unlock( &p_vout->p_sys->lock );
3085 vlc_mutex_lock( &p_vout->p_sys->lock );
3086 if( i_query == VOUT_REPARENT ) d = (Drawable)va_arg( args, int );
3089 #ifdef MODULE_NAME_IS_xvmc
3090 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3092 XReparentWindow( p_vout->p_sys->p_display,
3093 p_vout->p_sys->original_window.base_window,
3094 DefaultRootWindow( p_vout->p_sys->p_display ),
3098 XReparentWindow( p_vout->p_sys->p_display,
3099 p_vout->p_sys->original_window.base_window,
3101 XSync( p_vout->p_sys->p_display, False );
3102 p_vout->p_sys->original_window.owner_window = 0;
3103 #ifdef MODULE_NAME_IS_xvmc
3104 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3106 vlc_mutex_unlock( &p_vout->p_sys->lock );
3107 return vout_vaControlDefault( p_vout, i_query, args );
3109 case VOUT_SET_STAY_ON_TOP:
3110 if( p_vout->p_sys->p_win->owner_window )
3111 return vout_ControlWindow( p_vout,
3112 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3114 b_arg = va_arg( args, vlc_bool_t );
3115 vlc_mutex_lock( &p_vout->p_sys->lock );
3116 #ifdef MODULE_NAME_IS_xvmc
3117 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3119 WindowOnTop( p_vout, b_arg );
3120 #ifdef MODULE_NAME_IS_xvmc
3121 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3123 vlc_mutex_unlock( &p_vout->p_sys->lock );
3127 return vout_vaControlDefault( p_vout, i_query, args );
3131 /*****************************************************************************
3132 * TestNetWMSupport: tests for Extended Window Manager Hints support
3133 *****************************************************************************/
3134 static void TestNetWMSupport( vout_thread_t *p_vout )
3136 int i_ret, i_format;
3137 unsigned long i, i_items, i_bytesafter;
3138 Atom net_wm_supported;
3139 union { Atom *p_atom; unsigned char *p_char; } p_args;
3141 p_args.p_atom = NULL;
3143 p_vout->p_sys->b_net_wm_state_fullscreen =
3144 p_vout->p_sys->b_net_wm_state_above =
3145 p_vout->p_sys->b_net_wm_state_below =
3146 p_vout->p_sys->b_net_wm_state_stays_on_top =
3150 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3152 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3153 DefaultRootWindow( p_vout->p_sys->p_display ),
3155 0, 16384, False, AnyPropertyType,
3157 &i_format, &i_items, &i_bytesafter,
3158 (unsigned char **)&p_args );
3160 if( i_ret != Success || i_items == 0 ) return;
3162 msg_Dbg( p_vout, "Window manager supports NetWM" );
3164 p_vout->p_sys->net_wm_state =
3165 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3166 p_vout->p_sys->net_wm_state_fullscreen =
3167 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3169 p_vout->p_sys->net_wm_state_above =
3170 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3171 p_vout->p_sys->net_wm_state_below =
3172 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3173 p_vout->p_sys->net_wm_state_stays_on_top =
3174 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3177 for( i = 0; i < i_items; i++ )
3179 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3182 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3183 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
3185 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3187 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3188 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
3190 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3192 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3193 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
3195 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3198 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3199 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
3203 XFree( p_args.p_atom );
3206 /*****************************************************************************
3207 * Key events handling
3208 *****************************************************************************/
3214 } x11keys_to_vlckeys[] =
3216 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3217 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3218 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3219 { XK_F12, KEY_F12 },
3221 { XK_Return, KEY_ENTER },
3222 { XK_KP_Enter, KEY_ENTER },
3223 { XK_space, KEY_SPACE },
3224 { XK_Escape, KEY_ESC },
3226 { XK_Menu, KEY_MENU },
3227 { XK_Left, KEY_LEFT },
3228 { XK_Right, KEY_RIGHT },
3230 { XK_Down, KEY_DOWN },
3232 { XK_Home, KEY_HOME },
3233 { XK_End, KEY_END },
3234 { XK_Page_Up, KEY_PAGEUP },
3235 { XK_Page_Down, KEY_PAGEDOWN },
3237 { XK_Insert, KEY_INSERT },
3238 { XK_Delete, KEY_DELETE },
3239 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3240 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3241 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3242 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3243 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3244 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3245 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3250 static int ConvertKey( int i_key )
3254 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3256 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3258 return x11keys_to_vlckeys[i].i_vlckey;
3265 /*****************************************************************************
3266 * WindowOnTop: Switches the "always on top" state of the video window.
3267 *****************************************************************************/
3268 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
3270 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3272 XClientMessageEvent event;
3274 memset( &event, 0, sizeof( XClientMessageEvent ) );
3276 event.type = ClientMessage;
3277 event.message_type = p_vout->p_sys->net_wm_state;
3278 event.display = p_vout->p_sys->p_display;
3279 event.window = p_vout->p_sys->p_win->base_window;
3281 event.data.l[ 0 ] = b_on_top; /* set property */
3282 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3284 XSendEvent( p_vout->p_sys->p_display,
3285 DefaultRootWindow( p_vout->p_sys->p_display ),
3286 False, SubstructureRedirectMask,
3290 /* use _NET_WM_STATE_ABOVE if window manager
3291 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3292 else if( p_vout->p_sys->b_net_wm_state_above )
3294 XClientMessageEvent event;
3296 memset( &event, 0, sizeof( XClientMessageEvent ) );
3298 event.type = ClientMessage;
3299 event.message_type = p_vout->p_sys->net_wm_state;
3300 event.display = p_vout->p_sys->p_display;
3301 event.window = p_vout->p_sys->p_win->base_window;
3303 event.data.l[ 0 ] = b_on_top; /* set property */
3304 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3306 XSendEvent( p_vout->p_sys->p_display,
3307 DefaultRootWindow( p_vout->p_sys->p_display ),
3308 False, SubstructureRedirectMask,