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 sub_pic.p_sys = NULL;
434 p_vout->p_sys->last_date = 0;
438 p_vout->p_sys->i_hw_scale = 1;
442 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
443 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
444 if ( p_vout->p_sys->p_octx == NULL ) {
445 msg_Err( p_vout, "Could not get osso context" );
447 msg_Dbg( p_vout, "Initialized osso context" );
451 /* Variable to indicate if the window should be on top of others */
452 /* Trigger a callback right now */
453 var_Get( p_vout, "video-on-top", &val );
454 var_Set( p_vout, "video-on-top", val );
459 /*****************************************************************************
460 * Deactivate: destroy X11 video thread output method
461 *****************************************************************************
462 * Terminate an output method created by Open
463 *****************************************************************************/
464 void E_(Deactivate) ( vlc_object_t *p_this )
466 vout_thread_t *p_vout = (vout_thread_t *)p_this;
468 /* If the fullscreen window is still open, close it */
469 if( p_vout->b_fullscreen )
471 ToggleFullScreen( p_vout );
474 /* Restore cursor if it was blanked */
475 if( !p_vout->p_sys->b_mouse_pointer_visible )
477 ToggleCursor( p_vout );
480 #ifdef MODULE_NAME_IS_x11
481 /* Destroy colormap */
482 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
484 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
486 #elif defined(MODULE_NAME_IS_xvideo)
487 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
488 #elif defined(MODULE_NAME_IS_xvmc)
489 if( p_vout->p_sys->xvmc_cap )
491 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
492 xxmc_dispose_context( p_vout );
493 if( p_vout->p_sys->old_subpic )
495 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
496 p_vout->p_sys->old_subpic = NULL;
498 if( p_vout->p_sys->new_subpic )
500 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
501 p_vout->p_sys->new_subpic = NULL;
503 free( p_vout->p_sys->xvmc_cap );
504 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
509 DisablePixelDoubling(p_vout);
512 DestroyCursor( p_vout );
513 EnableXScreenSaver( p_vout );
514 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
515 XCloseDisplay( p_vout->p_sys->p_display );
517 /* Destroy structure */
518 vlc_mutex_destroy( &p_vout->p_sys->lock );
519 #ifdef MODULE_NAME_IS_xvmc
520 free_context_lock( &p_vout->p_sys->xvmc_lock );
524 if ( p_vout->p_sys->p_octx != NULL ) {
525 msg_Dbg( p_vout, "Deinitializing osso context" );
526 osso_deinitialize( p_vout->p_sys->p_octx );
530 free( p_vout->p_sys );
533 #ifdef MODULE_NAME_IS_xvmc
535 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
538 static int xvmc_check_yv12( Display *display, XvPortID port )
540 XvImageFormatValues *formatValues;
544 formatValues = XvListImageFormats( display, port, &formats );
546 for( i = 0; i < formats; i++ )
548 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
549 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
551 XFree (formatValues);
556 XFree (formatValues);
560 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
562 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
565 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
568 int xv_double_buffer;
570 xv_double_buffer = 1;
572 XLockDisplay( p_vout->p_sys->p_display );
573 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
575 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
577 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
578 XUnlockDisplay( p_vout->p_sys->p_display );
580 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
581 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
584 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
586 vlc_xxmc_t *xxmc = NULL;
588 vlc_mutex_lock( &p_vout->p_sys->lock );
589 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
591 xxmc = &p_pic->p_sys->xxmc_data;
592 if( (!xxmc->decoded ||
593 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
595 vlc_mutex_unlock( &p_vout->p_sys->lock );
596 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
600 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
602 if (p_vout->p_last_subtitle != NULL)
604 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_last_subtitle )
606 p_vout->p_sys->new_subpic =
607 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
608 p_vout->p_sys->xvmc_width,
609 p_vout->p_sys->xvmc_height,
610 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
612 if (p_vout->p_sys->new_subpic)
614 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
615 XvMCClearSubpicture( p_vout->p_sys->p_display,
616 p_vout->p_sys->new_subpic,
619 p_vout->p_sys->xvmc_width,
620 p_vout->p_sys->xvmc_height,
622 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
623 clear_xx44_palette( &p_vout->p_sys->palette );
625 if( sub_pic.p_sys == NULL )
627 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
628 if( sub_pic.p_sys != NULL )
630 sub_pic.p_sys->p_vout = p_vout;
631 sub_pic.p_sys->xvmc_surf = NULL;
632 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
635 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
636 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
637 sub_pic.p->i_pitch = p_vout->output.i_width;
639 memset( p_vout->p_sys->subImage->data, 0,
640 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
642 if (p_vout->p_last_subtitle != NULL)
644 blend_xx44( p_vout->p_sys->subImage->data,
645 p_vout->p_last_subtitle,
646 p_vout->p_sys->subImage->width,
647 p_vout->p_sys->subImage->height,
648 p_vout->p_sys->subImage->width,
649 &p_vout->p_sys->palette,
650 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
653 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
654 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
655 p_vout->p_sys->new_subpic,
656 p_vout->p_sys->subImage,
659 p_vout->output.i_width, /* overlay->width, */
660 p_vout->output.i_height, /* overlay->height */
663 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
664 if (p_vout->p_sys->old_subpic)
666 xxmc_xvmc_free_subpicture( p_vout,
667 p_vout->p_sys->old_subpic);
668 p_vout->p_sys->old_subpic = NULL;
670 if (p_vout->p_sys->new_subpic)
672 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
673 p_vout->p_sys->new_subpic = NULL;
674 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
675 p_vout->p_sys->xvmc_palette,
677 p_vout->p_sys->old_subpic->num_palette_entries,
678 p_vout->p_sys->old_subpic->entry_bytes,
679 p_vout->p_sys->old_subpic->component_order );
680 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
681 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
682 p_vout->p_sys->old_subpic,
683 p_vout->p_sys->xvmc_palette );
684 XvMCFlushSubpicture( p_vout->p_sys->p_display,
685 p_vout->p_sys->old_subpic);
686 XvMCSyncSubpicture( p_vout->p_sys->p_display,
687 p_vout->p_sys->old_subpic );
688 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
691 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
692 if (p_vout->p_sys->xvmc_backend_subpic )
694 XvMCBlendSubpicture( p_vout->p_sys->p_display,
695 p_pic->p_sys->xvmc_surf,
696 p_vout->p_sys->old_subpic,
699 p_vout->p_sys->xvmc_width,
700 p_vout->p_sys->xvmc_height,
703 p_vout->p_sys->xvmc_width,
704 p_vout->p_sys->xvmc_height );
708 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
709 p_pic->p_sys->xvmc_surf,
710 p_pic->p_sys->xvmc_surf,
711 p_vout->p_sys->old_subpic,
714 p_vout->p_sys->xvmc_width,
715 p_vout->p_sys->xvmc_height,
718 p_vout->p_sys->xvmc_width,
719 p_vout->p_sys->xvmc_height );
721 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
726 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
727 if( p_vout->p_sys->xvmc_backend_subpic )
729 XvMCBlendSubpicture( p_vout->p_sys->p_display,
730 p_pic->p_sys->xvmc_surf,
731 p_vout->p_sys->old_subpic,
733 p_vout->p_sys->xvmc_width,
734 p_vout->p_sys->xvmc_height,
736 p_vout->p_sys->xvmc_width,
737 p_vout->p_sys->xvmc_height );
741 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
742 p_pic->p_sys->xvmc_surf,
743 p_pic->p_sys->xvmc_surf,
744 p_vout->p_sys->old_subpic,
746 p_vout->p_sys->xvmc_width,
747 p_vout->p_sys->xvmc_height,
749 p_vout->p_sys->xvmc_width,
750 p_vout->p_sys->xvmc_height );
752 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
755 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
757 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
758 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
760 vlc_mutex_unlock( &p_vout->p_sys->lock );
765 /*****************************************************************************
766 * EnablePixelDoubling: Enables pixel doubling
767 *****************************************************************************
768 * Checks if the double size image fits in current window, and enables pixel
769 * doubling accordingly. The i_hw_scale is the integer scaling factor.
770 *****************************************************************************/
771 static void EnablePixelDoubling( vout_thread_t *p_vout )
773 int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
774 int i_vert_scale = ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
775 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
776 p_vout->p_sys->i_hw_scale = 2;
777 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
778 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
782 /*****************************************************************************
783 * DisablePixelDoubling: Disables pixel doubling
784 *****************************************************************************
785 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
786 *****************************************************************************/
787 static void DisablePixelDoubling( vout_thread_t *p_vout )
789 if ( p_vout->p_sys->i_hw_scale > 1 ) {
790 msg_Dbg( p_vout, "Disabling pixel doubling" );
791 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
792 p_vout->p_sys->i_hw_scale = 1;
799 /*****************************************************************************
800 * InitVideo: initialize X11 video thread output method
801 *****************************************************************************
802 * This function create the XImages needed by the output thread. It is called
803 * at the beginning of the thread, but also each time the window is resized.
804 *****************************************************************************/
805 static int InitVideo( vout_thread_t *p_vout )
807 unsigned int i_index = 0;
810 I_OUTPUTPICTURES = 0;
812 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
813 /* Initialize the output structure; we already found an XVideo port,
814 * and the corresponding chroma we will be using. Since we can
815 * arbitrary scale, stick to the coordinates and aspect. */
816 p_vout->output.i_width = p_vout->render.i_width;
817 p_vout->output.i_height = p_vout->render.i_height;
818 p_vout->output.i_aspect = p_vout->render.i_aspect;
820 p_vout->fmt_out = p_vout->fmt_in;
821 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
823 switch( p_vout->output.i_chroma )
825 case VLC_FOURCC('R','V','1','6'):
826 #if defined( WORDS_BIGENDIAN )
827 p_vout->output.i_rmask = 0xf800;
828 p_vout->output.i_gmask = 0x07e0;
829 p_vout->output.i_bmask = 0x001f;
831 p_vout->output.i_rmask = 0x001f;
832 p_vout->output.i_gmask = 0x07e0;
833 p_vout->output.i_bmask = 0xf800;
836 case VLC_FOURCC('R','V','1','5'):
837 #if defined( WORDS_BIGENDIAN )
838 p_vout->output.i_rmask = 0x7c00;
839 p_vout->output.i_gmask = 0x03e0;
840 p_vout->output.i_bmask = 0x001f;
842 p_vout->output.i_rmask = 0x001f;
843 p_vout->output.i_gmask = 0x03e0;
844 p_vout->output.i_bmask = 0x7c00;
849 #elif defined(MODULE_NAME_IS_x11)
850 /* Initialize the output structure: RGB with square pixels, whatever
851 * the input format is, since it's the only format we know */
852 switch( p_vout->p_sys->i_screen_depth )
854 case 8: /* FIXME: set the palette */
855 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
857 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
859 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
862 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
864 msg_Err( p_vout, "unknown screen depth %i",
865 p_vout->p_sys->i_screen_depth );
870 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width / p_vout->p_sys->i_hw_scale,
871 p_vout->p_sys->p_win->i_height / p_vout->p_sys->i_hw_scale,
873 &p_vout->fmt_out.i_visible_width,
874 &p_vout->fmt_out.i_visible_height );
876 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
877 p_vout->p_sys->p_win->i_height,
879 &p_vout->fmt_out.i_visible_width,
880 &p_vout->fmt_out.i_visible_height );
883 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
885 p_vout->output.i_width = p_vout->fmt_out.i_width =
886 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
887 p_vout->fmt_in.i_visible_width;
888 p_vout->output.i_height = p_vout->fmt_out.i_height =
889 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
890 p_vout->fmt_in.i_visible_height;
891 p_vout->fmt_out.i_x_offset =
892 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
893 p_vout->fmt_in.i_visible_width;
894 p_vout->fmt_out.i_y_offset =
895 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
896 p_vout->fmt_in.i_visible_height;
898 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
899 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
900 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
902 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
903 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
904 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
905 p_vout->fmt_out.i_visible_width,
906 p_vout->fmt_out.i_visible_height );
909 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
910 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
914 /* Find an empty picture slot */
915 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
917 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
919 p_pic = p_vout->p_picture + i_index;
924 /* Allocate the picture */
925 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
930 p_pic->i_status = DESTROYED_PICTURE;
931 p_pic->i_type = DIRECT_PICTURE;
933 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
938 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
940 /* U and V inverted compared to I420
941 * Fixme: this should be handled by the vout core */
942 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
943 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
949 /*****************************************************************************
950 * DisplayVideo: displays previously rendered output
951 *****************************************************************************
952 * This function sends the currently rendered image to X11 server.
953 * (The Xv extension takes care of "double-buffering".)
954 *****************************************************************************/
955 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
957 unsigned int i_width, i_height, i_x, i_y;
959 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
960 p_vout->p_sys->p_win->i_height,
961 &i_x, &i_y, &i_width, &i_height );
963 vlc_mutex_lock( &p_vout->p_sys->lock );
965 #ifdef MODULE_NAME_IS_xvmc
966 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
968 vlc_xxmc_t *xxmc = &p_picture->p_sys->xxmc_data;
969 if( !xxmc->decoded ||
970 !xxmc_xvmc_surface_valid( p_vout, p_picture->p_sys->xvmc_surf ) )
972 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
974 xxmc_xvmc_surface_valid( p_vout, p_picture->p_sys->xvmc_surf ) );
975 vlc_mutex_unlock( &p_vout->p_sys->lock );
976 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
980 src_width = p_vout->output.i_width;
981 src_height = p_vout->output.i_height;
983 if( p_vout->p_sys->xvmc_crop_style == 1 )
990 else if( p_vout->p_sys->xvmc_crop_style == 2 )
997 else if( p_vout->p_sys->xvmc_crop_style == 3 )
1010 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1011 { /* BOB DEINTERLACE */
1012 if( (p_picture->p_sys->nb_display == 0) ||
1013 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1015 first_field = (p_picture->b_top_field_first) ?
1016 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1020 first_field = (p_picture->b_top_field_first) ?
1021 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1026 first_field = XVMC_FRAME_PICTURE;
1029 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1030 XvMCFlushSurface( p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf );
1031 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1032 XvMCPutSurface( p_vout->p_sys->p_display,
1033 p_picture->p_sys->xvmc_surf,
1034 p_vout->p_sys->p_win->video_window,
1045 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1046 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1047 { /* BOB DEINTERLACE */
1048 if( p_picture->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1050 mtime_t last_date = p_picture->date;
1052 vlc_mutex_lock( &p_vout->picture_lock );
1053 if( !p_vout->p_sys->last_date )
1055 p_picture->date += 20000;
1059 p_picture->date = ((3 * p_picture->date -
1060 p_vout->p_sys->last_date) / 2 );
1062 p_vout->p_sys->last_date = last_date;
1063 p_picture->b_force = 1;
1064 p_picture->p_sys->nb_display = 1;
1065 vlc_mutex_unlock( &p_vout->picture_lock );
1069 p_picture->p_sys->nb_display = 0;
1070 p_picture->b_force = 0;
1073 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1076 #ifdef HAVE_SYS_SHM_H
1077 if( p_vout->p_sys->i_shm_opcode )
1079 /* Display rendered image using shared memory extension */
1080 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1081 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1082 p_vout->p_sys->p_win->video_window,
1083 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1084 p_vout->fmt_out.i_x_offset,
1085 p_vout->fmt_out.i_y_offset,
1086 p_vout->fmt_out.i_visible_width,
1087 p_vout->fmt_out.i_visible_height,
1088 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1089 False /* Don't put True here or you'll waste your CPU */ );
1091 XShmPutImage( p_vout->p_sys->p_display,
1092 p_vout->p_sys->p_win->video_window,
1093 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1094 p_vout->fmt_out.i_x_offset,
1095 p_vout->fmt_out.i_y_offset,
1096 0 /*dest_x*/, 0 /*dest_y*/,
1097 p_vout->fmt_out.i_visible_width,
1098 p_vout->fmt_out.i_visible_height,
1099 False /* Don't put True here ! */ );
1103 #endif /* HAVE_SYS_SHM_H */
1105 /* Use standard XPutImage -- this is gonna be slow ! */
1106 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1107 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1108 p_vout->p_sys->p_win->video_window,
1109 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1110 p_vout->fmt_out.i_x_offset,
1111 p_vout->fmt_out.i_y_offset,
1112 p_vout->fmt_out.i_visible_width,
1113 p_vout->fmt_out.i_visible_height,
1114 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1116 XPutImage( p_vout->p_sys->p_display,
1117 p_vout->p_sys->p_win->video_window,
1118 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1119 p_vout->fmt_out.i_x_offset,
1120 p_vout->fmt_out.i_y_offset,
1121 0 /*dest_x*/, 0 /*dest_y*/,
1122 p_vout->fmt_out.i_visible_width,
1123 p_vout->fmt_out.i_visible_height );
1127 /* Make sure the command is sent now - do NOT use XFlush !*/
1128 XSync( p_vout->p_sys->p_display, False );
1130 vlc_mutex_unlock( &p_vout->p_sys->lock );
1133 /*****************************************************************************
1134 * ManageVideo: handle X11 events
1135 *****************************************************************************
1136 * This function should be called regularly by video output thread. It manages
1137 * X11 events and allows window resizing. It returns a non null value on
1139 *****************************************************************************/
1140 static int ManageVideo( vout_thread_t *p_vout )
1142 XEvent xevent; /* X11 event */
1145 vlc_mutex_lock( &p_vout->p_sys->lock );
1147 #ifdef MODULE_NAME_IS_xvmc
1148 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1151 /* Handle events from the owner window */
1152 if( p_vout->p_sys->p_win->owner_window )
1154 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1155 p_vout->p_sys->p_win->owner_window,
1156 StructureNotifyMask, &xevent ) == True )
1158 /* ConfigureNotify event: prepare */
1159 if( xevent.type == ConfigureNotify )
1161 /* Update dimensions */
1162 XResizeWindow( p_vout->p_sys->p_display,
1163 p_vout->p_sys->p_win->base_window,
1164 xevent.xconfigure.width,
1165 xevent.xconfigure.height );
1170 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1171 * output window's size changed, MapNotify and UnmapNotify to know if the
1172 * window is mapped (and if the display is useful), and ClientMessages
1173 * to intercept window destruction requests */
1175 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1176 p_vout->p_sys->p_win->base_window,
1177 StructureNotifyMask | KeyPressMask |
1178 ButtonPressMask | ButtonReleaseMask |
1179 PointerMotionMask | Button1MotionMask , &xevent )
1182 /* ConfigureNotify event: prepare */
1183 if( xevent.type == ConfigureNotify )
1185 if( (unsigned int)xevent.xconfigure.width
1186 != p_vout->p_sys->p_win->i_width
1187 || (unsigned int)xevent.xconfigure.height
1188 != p_vout->p_sys->p_win->i_height )
1190 /* Update dimensions */
1191 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1192 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1193 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1196 /* Keyboard event */
1197 else if( xevent.type == KeyPress )
1199 unsigned int state = xevent.xkey.state;
1200 KeySym x_key_symbol;
1201 char i_key; /* ISO Latin-1 key */
1203 /* We may have keys like F1 trough F12, ESC ... */
1204 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1205 xevent.xkey.keycode, 0 );
1206 val.i_int = ConvertKey( (int)x_key_symbol );
1208 xevent.xkey.state &= ~ShiftMask;
1209 xevent.xkey.state &= ~ControlMask;
1210 xevent.xkey.state &= ~Mod1Mask;
1213 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1216 * The reason why I use this instead of XK_0 is that
1217 * with XLookupString, we don't have to care about
1224 if( state & ShiftMask )
1226 val.i_int |= KEY_MODIFIER_SHIFT;
1228 if( state & ControlMask )
1230 val.i_int |= KEY_MODIFIER_CTRL;
1232 if( state & Mod1Mask )
1234 val.i_int |= KEY_MODIFIER_ALT;
1236 var_Set( p_vout->p_libvlc, "key-pressed", val );
1240 else if( xevent.type == ButtonPress )
1242 switch( ((XButtonEvent *)&xevent)->button )
1245 var_Get( p_vout, "mouse-button-down", &val );
1247 var_Set( p_vout, "mouse-button-down", val );
1249 /* detect double-clicks */
1250 if( ( ((XButtonEvent *)&xevent)->time -
1251 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1253 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1256 p_vout->p_sys->i_time_button_last_pressed =
1257 ((XButtonEvent *)&xevent)->time;
1260 var_Get( p_vout, "mouse-button-down", &val );
1262 var_Set( p_vout, "mouse-button-down", val );
1266 var_Get( p_vout, "mouse-button-down", &val );
1268 var_Set( p_vout, "mouse-button-down", val );
1272 var_Get( p_vout, "mouse-button-down", &val );
1274 var_Set( p_vout, "mouse-button-down", val );
1278 var_Get( p_vout, "mouse-button-down", &val );
1280 var_Set( p_vout, "mouse-button-down", val );
1285 else if( xevent.type == ButtonRelease )
1287 switch( ((XButtonEvent *)&xevent)->button )
1290 var_Get( p_vout, "mouse-button-down", &val );
1292 var_Set( p_vout, "mouse-button-down", val );
1294 val.b_bool = VLC_TRUE;
1295 var_Set( p_vout, "mouse-clicked", val );
1300 playlist_t *p_playlist;
1302 var_Get( p_vout, "mouse-button-down", &val );
1304 var_Set( p_vout, "mouse-button-down", val );
1306 p_playlist = vlc_object_find( p_vout,
1307 VLC_OBJECT_PLAYLIST,
1309 if( p_playlist != NULL )
1312 var_Get( p_playlist, "intf-show", &val );
1313 val.b_bool = !val.b_bool;
1314 var_Set( p_playlist, "intf-show", val );
1315 vlc_object_release( p_playlist );
1322 intf_thread_t *p_intf;
1323 playlist_t *p_playlist;
1325 var_Get( p_vout, "mouse-button-down", &val );
1327 var_Set( p_vout, "mouse-button-down", val );
1328 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
1332 p_intf->b_menu_change = 1;
1333 vlc_object_release( p_intf );
1336 p_playlist = vlc_object_find( p_vout,
1337 VLC_OBJECT_PLAYLIST,
1339 if( p_playlist != NULL )
1341 vlc_value_t val; val.b_bool = VLC_TRUE;
1342 var_Set( p_playlist, "intf-popupmenu", val );
1343 vlc_object_release( p_playlist );
1349 var_Get( p_vout, "mouse-button-down", &val );
1351 var_Set( p_vout, "mouse-button-down", val );
1355 var_Get( p_vout, "mouse-button-down", &val );
1357 var_Set( p_vout, "mouse-button-down", val );
1363 else if( xevent.type == MotionNotify )
1365 unsigned int i_width, i_height, i_x, i_y;
1368 /* somewhat different use for vout_PlacePicture:
1369 * here the values are needed to give to mouse coordinates
1370 * in the original picture space */
1371 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1372 p_vout->p_sys->p_win->i_height,
1373 &i_x, &i_y, &i_width, &i_height );
1375 /* Compute the x coordinate and check if the value is
1376 in [0,p_vout->fmt_in.i_visible_width] */
1377 val.i_int = ( xevent.xmotion.x - i_x ) *
1378 p_vout->fmt_in.i_visible_width / i_width +
1379 p_vout->fmt_in.i_x_offset;
1381 if( (int)(xevent.xmotion.x - i_x) < 0 )
1383 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1384 val.i_int = p_vout->fmt_in.i_visible_width;
1386 var_Set( p_vout, "mouse-x", val );
1388 /* compute the y coordinate and check if the value is
1389 in [0,p_vout->fmt_in.i_visible_height] */
1390 val.i_int = ( xevent.xmotion.y - i_y ) *
1391 p_vout->fmt_in.i_visible_height / i_height +
1392 p_vout->fmt_in.i_y_offset;
1394 if( (int)(xevent.xmotion.y - i_y) < 0 )
1396 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1397 val.i_int = p_vout->fmt_in.i_visible_height;
1399 var_Set( p_vout, "mouse-y", val );
1401 val.b_bool = VLC_TRUE;
1402 var_Set( p_vout, "mouse-moved", val );
1404 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1405 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1407 ToggleCursor( p_vout );
1410 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1411 || xevent.type == MapNotify
1412 || xevent.type == UnmapNotify )
1414 /* Ignore these events */
1416 else /* Other events */
1418 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1422 /* Handle events for video output sub-window */
1423 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1424 p_vout->p_sys->p_win->video_window,
1425 ExposureMask, &xevent ) == True )
1427 /* Window exposed (only handled if stream playback is paused) */
1428 if( xevent.type == Expose )
1430 if( ((XExposeEvent *)&xevent)->count == 0 )
1432 /* (if this is the last a collection of expose events...) */
1434 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1436 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1437 ->stream.control.i_status )
1439 /* XVideoDisplay( p_vout )*/;
1447 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1448 * are handled - according to the man pages, the format is always 32
1450 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1451 ClientMessage, &xevent ) )
1453 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1454 && ((Atom)xevent.xclient.data.l[0]
1455 == p_vout->p_sys->p_win->wm_delete_window ) )
1457 /* the user wants to close the window */
1458 playlist_t * p_playlist =
1459 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1461 if( p_playlist != NULL )
1463 playlist_Stop( p_playlist );
1464 vlc_object_release( p_playlist );
1472 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1474 vlc_value_t val_fs, val_ontop;
1476 /* Update the object variable and trigger callback */
1477 val_fs.b_bool = !p_vout->b_fullscreen;
1479 var_Set( p_vout, "fullscreen", val_fs );
1481 /* Disable "always on top" in fullscreen mode */
1482 var_Get( p_vout, "video-on-top", &val_ontop );
1483 if( val_ontop.b_bool )
1484 WindowOnTop( p_vout, val_fs.b_bool );
1486 ToggleFullScreen( p_vout );
1487 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1490 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1491 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1493 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1494 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1496 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1497 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1498 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1499 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1500 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1501 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1502 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1503 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1505 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1511 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1512 * the size flag inside the fullscreen routine)
1514 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1516 unsigned int i_width, i_height, i_x, i_y;
1518 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1520 #ifdef MODULE_NAME_IS_x11
1521 /* We need to signal the vout thread about the size change because it
1522 * is doing the rescaling */
1523 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1526 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1527 p_vout->p_sys->p_win->i_height,
1528 &i_x, &i_y, &i_width, &i_height );
1530 XMoveResizeWindow( p_vout->p_sys->p_display,
1531 p_vout->p_sys->p_win->video_window,
1532 i_x, i_y, i_width, i_height );
1535 /* Autohide Cursour */
1536 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
1538 /* Hide the mouse automatically */
1539 if( p_vout->p_sys->b_mouse_pointer_visible )
1541 ToggleCursor( p_vout );
1545 #ifdef MODULE_NAME_IS_xvmc
1546 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1550 if ( p_vout->p_sys->p_octx != NULL ) {
1551 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1552 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1553 msg_Err( p_vout, "Could not disable backlight blanking" );
1555 msg_Dbg( p_vout, "Backlight blanking disabled" );
1557 p_vout->p_sys->i_backlight_on_counter = 0;
1559 p_vout->p_sys->i_backlight_on_counter ++;
1564 vlc_mutex_unlock( &p_vout->p_sys->lock );
1568 /*****************************************************************************
1569 * EndVideo: terminate X11 video thread output method
1570 *****************************************************************************
1571 * Destroy the X11 XImages created by Init. It is called at the end of
1572 * the thread, but also each time the window is resized.
1573 *****************************************************************************/
1574 static void EndVideo( vout_thread_t *p_vout )
1578 /* Free the direct buffers we allocated */
1579 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1582 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1586 /* following functions are local */
1588 /*****************************************************************************
1589 * CreateWindow: open and set-up X11 main window
1590 *****************************************************************************/
1591 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1593 XSizeHints xsize_hints;
1594 XSetWindowAttributes xwindow_attributes;
1595 XGCValues xgcvalues;
1598 vlc_bool_t b_expose = VLC_FALSE;
1599 vlc_bool_t b_configure_notify = VLC_FALSE;
1600 vlc_bool_t b_map_notify = VLC_FALSE;
1603 /* Prepare window manager hints and properties */
1604 p_win->wm_protocols =
1605 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1606 p_win->wm_delete_window =
1607 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1609 /* Never have a 0-pixel-wide window */
1610 xsize_hints.min_width = 2;
1611 xsize_hints.min_height = 1;
1613 /* Prepare window attributes */
1614 xwindow_attributes.backing_store = Always; /* save the hidden part */
1615 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1616 p_vout->p_sys->i_screen);
1617 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1619 if( !p_vout->b_fullscreen )
1621 p_win->owner_window = (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
1622 &p_win->i_width, &p_win->i_height );
1624 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1625 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1626 xsize_hints.flags = PSize | PMinSize;
1628 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1630 xsize_hints.x = p_win->i_x;
1631 xsize_hints.y = p_win->i_y;
1632 xsize_hints.flags |= PPosition;
1637 /* Fullscreen window size and position */
1638 p_win->owner_window = 0;
1639 p_win->i_x = p_win->i_y = 0;
1641 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1643 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1646 if( !p_win->owner_window )
1648 /* Create the window and set hints - the window must receive
1649 * ConfigureNotify events, and until it is displayed, Expose and
1650 * MapNotify events. */
1652 p_win->base_window =
1653 XCreateWindow( p_vout->p_sys->p_display,
1654 DefaultRootWindow( p_vout->p_sys->p_display ),
1655 p_win->i_x, p_win->i_y,
1656 p_win->i_width, p_win->i_height,
1659 CWBackingStore | CWBackPixel | CWEventMask,
1660 &xwindow_attributes );
1662 var_Get( p_vout, "video-title", &val );
1663 if( !val.psz_string || !*val.psz_string )
1665 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1666 #ifdef MODULE_NAME_IS_x11
1667 VOUT_TITLE " (X11 output)"
1668 #elif defined(MODULE_NAME_IS_glx)
1669 VOUT_TITLE " (GLX output)"
1671 VOUT_TITLE " (XVideo output)"
1677 XStoreName( p_vout->p_sys->p_display,
1678 p_win->base_window, val.psz_string );
1680 if( val.psz_string ) free( val.psz_string );
1682 if( !p_vout->b_fullscreen )
1684 const char *argv[] = { "vlc", NULL };
1686 /* Set window manager hints and properties: size hints, command,
1687 * window's name, and accepted protocols */
1688 XSetWMNormalHints( p_vout->p_sys->p_display,
1689 p_win->base_window, &xsize_hints );
1690 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1693 if( !var_GetBool( p_vout, "video-deco") )
1696 mwmhints_t mwmhints;
1698 mwmhints.flags = MWM_HINTS_DECORATIONS;
1699 mwmhints.decorations = False;
1701 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1704 XChangeProperty( p_vout->p_sys->p_display,
1706 prop, prop, 32, PropModeReplace,
1707 (unsigned char *)&mwmhints,
1708 PROP_MWM_HINTS_ELEMENTS );
1716 unsigned int dummy4, dummy5;
1718 /* Select events we are interested in. */
1719 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1720 StructureNotifyMask );
1722 /* Get the parent window's geometry information */
1723 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1724 &dummy1, &dummy2, &dummy3,
1729 /* We are already configured */
1730 b_configure_notify = VLC_TRUE;
1732 /* From man XSelectInput: only one client at a time can select a
1733 * ButtonPress event, so we need to open a new window anyway. */
1734 p_win->base_window =
1735 XCreateWindow( p_vout->p_sys->p_display,
1736 p_win->owner_window,
1738 p_win->i_width, p_win->i_height,
1740 0, CopyFromParent, 0,
1741 CWBackingStore | CWBackPixel | CWEventMask,
1742 &xwindow_attributes );
1745 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1746 || (p_win->wm_delete_window == None)
1747 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1748 &p_win->wm_delete_window, 1 ) )
1750 /* WM_DELETE_WINDOW is not supported by window manager */
1751 msg_Warn( p_vout, "missing or bad window manager" );
1754 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1755 * event when using functions like XCopyArea */
1756 xgcvalues.graphics_exposures = False;
1757 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1759 GCGraphicsExposures, &xgcvalues );
1761 /* Send orders to server, and wait until window is displayed - three
1762 * events must be received: a MapNotify event, an Expose event allowing
1763 * drawing in the window, and a ConfigureNotify to get the window
1764 * dimensions. Once those events have been received, only
1765 * ConfigureNotify events need to be received. */
1766 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1769 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1770 SubstructureNotifyMask | StructureNotifyMask |
1771 ExposureMask, &xevent);
1772 if( (xevent.type == Expose)
1773 && (xevent.xexpose.window == p_win->base_window) )
1775 b_expose = VLC_TRUE;
1776 /* ConfigureNotify isn't sent if there isn't a window manager.
1777 * Expose should be the last event to be received so it should
1778 * be fine to assume we won't receive it anymore. */
1779 b_configure_notify = VLC_TRUE;
1781 else if( (xevent.type == MapNotify)
1782 && (xevent.xmap.window == p_win->base_window) )
1784 b_map_notify = VLC_TRUE;
1786 else if( (xevent.type == ConfigureNotify)
1787 && (xevent.xconfigure.window == p_win->base_window) )
1789 b_configure_notify = VLC_TRUE;
1790 p_win->i_width = xevent.xconfigure.width;
1791 p_win->i_height = xevent.xconfigure.height;
1793 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1795 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1796 StructureNotifyMask | KeyPressMask |
1797 ButtonPressMask | ButtonReleaseMask |
1798 PointerMotionMask );
1800 #ifdef MODULE_NAME_IS_x11
1801 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1803 /* Allocate a new palette */
1804 p_vout->p_sys->colormap =
1805 XCreateColormap( p_vout->p_sys->p_display,
1806 DefaultRootWindow( p_vout->p_sys->p_display ),
1807 DefaultVisual( p_vout->p_sys->p_display,
1808 p_vout->p_sys->i_screen ),
1811 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1812 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1813 CWColormap, &xwindow_attributes );
1817 /* Create video output sub-window. */
1818 p_win->video_window = XCreateSimpleWindow(
1819 p_vout->p_sys->p_display,
1820 p_win->base_window, 0, 0,
1821 p_win->i_width, p_win->i_height,
1823 BlackPixel( p_vout->p_sys->p_display,
1824 p_vout->p_sys->i_screen ),
1825 WhitePixel( p_vout->p_sys->p_display,
1826 p_vout->p_sys->i_screen ) );
1828 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1829 BlackPixel( p_vout->p_sys->p_display,
1830 p_vout->p_sys->i_screen ) );
1832 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1833 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1836 /* make sure the video window will be centered in the next ManageVideo() */
1837 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1839 /* If the cursor was formerly blank than blank it again */
1840 if( !p_vout->p_sys->b_mouse_pointer_visible )
1842 ToggleCursor( p_vout );
1843 ToggleCursor( p_vout );
1846 /* Do NOT use XFlush here ! */
1847 XSync( p_vout->p_sys->p_display, False );
1849 /* At this stage, the window is open, displayed, and ready to
1851 p_vout->p_sys->p_win = p_win;
1856 /*****************************************************************************
1857 * DestroyWindow: destroy the window
1858 *****************************************************************************
1860 *****************************************************************************/
1861 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1863 /* Do NOT use XFlush here ! */
1864 XSync( p_vout->p_sys->p_display, False );
1866 if( p_win->video_window != None )
1867 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1869 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1871 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1872 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1874 if( p_win->owner_window )
1875 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1878 /*****************************************************************************
1879 * NewPicture: allocate a picture
1880 *****************************************************************************
1881 * Returns 0 on success, -1 otherwise
1882 *****************************************************************************/
1883 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1885 #ifndef MODULE_NAME_IS_glx
1887 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1891 /* We know the chroma, allocate a buffer which will be used
1892 * directly by the decoder */
1893 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1895 if( p_pic->p_sys == NULL )
1900 #ifdef MODULE_NAME_IS_xvmc
1901 p_pic->p_sys->p_vout = p_vout;
1902 p_pic->p_sys->xvmc_surf = NULL;
1903 p_pic->p_sys->xxmc_data.decoded = 0;
1904 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1905 p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1906 p_pic->p_sys->nb_display = 0;
1909 /* Fill in picture_t fields */
1910 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1911 p_vout->output.i_width, p_vout->output.i_height,
1912 p_vout->output.i_aspect );
1914 #ifdef HAVE_SYS_SHM_H
1915 if( p_vout->p_sys->i_shm_opcode )
1917 /* Create image using XShm extension */
1918 p_pic->p_sys->p_image =
1919 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1920 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1921 p_vout->p_sys->i_xvport,
1922 VLC2X11_FOURCC(p_vout->output.i_chroma),
1924 p_vout->p_sys->p_visual,
1925 p_vout->p_sys->i_screen_depth,
1927 &p_pic->p_sys->shminfo,
1928 p_vout->output.i_width, p_vout->output.i_height );
1931 if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1932 #endif /* HAVE_SYS_SHM_H */
1934 /* Create image without XShm extension */
1935 p_pic->p_sys->p_image =
1936 CreateImage( p_vout, p_vout->p_sys->p_display,
1937 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1938 p_vout->p_sys->i_xvport,
1939 VLC2X11_FOURCC(p_vout->output.i_chroma),
1940 p_pic->format.i_bits_per_pixel,
1942 p_vout->p_sys->p_visual,
1943 p_vout->p_sys->i_screen_depth,
1944 p_vout->p_sys->i_bytes_per_pixel,
1946 p_vout->output.i_width, p_vout->output.i_height );
1948 #ifdef HAVE_SYS_SHM_H
1949 if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1951 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1952 p_vout->p_sys->i_shm_opcode = 0;
1954 #endif /* HAVE_SYS_SHM_H */
1957 if( p_pic->p_sys->p_image == NULL )
1959 free( p_pic->p_sys );
1963 switch( p_vout->output.i_chroma )
1965 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1966 case VLC_FOURCC('I','4','2','0'):
1967 case VLC_FOURCC('Y','V','1','2'):
1968 case VLC_FOURCC('Y','2','1','1'):
1969 case VLC_FOURCC('Y','U','Y','2'):
1970 case VLC_FOURCC('U','Y','V','Y'):
1971 case VLC_FOURCC('R','V','1','5'):
1972 case VLC_FOURCC('R','V','1','6'):
1973 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1974 case VLC_FOURCC('R','V','3','2'):
1976 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1979 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1980 + p_pic->p_sys->p_image->offsets[i_plane];
1981 p_pic->p[i_plane].i_pitch =
1982 p_pic->p_sys->p_image->pitches[i_plane];
1984 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1986 /* U and V inverted compared to I420
1987 * Fixme: this should be handled by the vout core */
1988 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1989 + p_pic->p_sys->p_image->offsets[2];
1990 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1991 + p_pic->p_sys->p_image->offsets[1];
1996 case VLC_FOURCC('R','G','B','2'):
1997 case VLC_FOURCC('R','V','1','6'):
1998 case VLC_FOURCC('R','V','1','5'):
1999 case VLC_FOURCC('R','V','2','4'):
2000 case VLC_FOURCC('R','V','3','2'):
2002 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
2003 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
2004 p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
2005 + p_pic->p_sys->p_image->xoffset;
2006 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
2008 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
2009 * properly by vout_InitPicture() */
2010 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
2011 * p_pic->p_sys->p_image->width;
2016 /* Unknown chroma, tell the guy to get lost */
2017 IMAGE_FREE( p_pic->p_sys->p_image );
2018 free( p_pic->p_sys );
2019 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
2020 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
2021 p_pic->i_planes = 0;
2026 VLC_UNUSED(p_vout); VLC_UNUSED(p_pic);
2028 #endif /* !MODULE_NAME_IS_glx */
2033 /*****************************************************************************
2034 * FreePicture: destroy a picture allocated with NewPicture
2035 *****************************************************************************
2036 * Destroy XImage AND associated data. If using Shm, detach shared memory
2037 * segment from server and process, then free it. The XDestroyImage manpage
2038 * says that both the image structure _and_ the data pointed to by the
2039 * image structure are freed, so no need to free p_image->data.
2040 *****************************************************************************/
2041 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2043 /* The order of operations is correct */
2044 #ifdef HAVE_SYS_SHM_H
2045 if( p_vout->p_sys->i_shm_opcode )
2047 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2048 IMAGE_FREE( p_pic->p_sys->p_image );
2050 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2051 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2053 msg_Err( p_vout, "cannot detach shared memory (%m)" );
2059 IMAGE_FREE( p_pic->p_sys->p_image );
2062 #ifdef MODULE_NAME_IS_xvmc
2063 if( p_pic->p_sys->xvmc_surf != NULL )
2065 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2066 p_pic->p_sys->xvmc_surf = NULL;
2070 /* Do NOT use XFlush here ! */
2071 XSync( p_vout->p_sys->p_display, False );
2073 free( p_pic->p_sys );
2076 /*****************************************************************************
2077 * ToggleFullScreen: Enable or disable full screen mode
2078 *****************************************************************************
2079 * This function will switch between fullscreen and window mode.
2080 *****************************************************************************/
2081 static void ToggleFullScreen ( vout_thread_t *p_vout )
2085 mwmhints_t mwmhints;
2086 XSetWindowAttributes attributes;
2088 #ifdef HAVE_XINERAMA
2092 p_vout->b_fullscreen = !p_vout->b_fullscreen;
2094 if( p_vout->b_fullscreen )
2096 msg_Dbg( p_vout, "entering fullscreen mode" );
2098 p_vout->p_sys->b_altfullscreen =
2099 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2101 XUnmapWindow( p_vout->p_sys->p_display,
2102 p_vout->p_sys->p_win->base_window );
2104 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2106 CreateWindow( p_vout, p_vout->p_sys->p_win );
2107 XDestroyWindow( p_vout->p_sys->p_display,
2108 p_vout->p_sys->fullscreen_window.video_window );
2109 XReparentWindow( p_vout->p_sys->p_display,
2110 p_vout->p_sys->original_window.video_window,
2111 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2112 p_vout->p_sys->fullscreen_window.video_window =
2113 p_vout->p_sys->original_window.video_window;
2115 /* To my knowledge there are two ways to create a borderless window.
2116 * There's the generic way which is to tell x to bypass the window
2117 * manager, but this creates problems with the focus of other
2119 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2120 * luckily seems to be supported by most window managers. */
2121 if( !p_vout->p_sys->b_altfullscreen )
2123 mwmhints.flags = MWM_HINTS_DECORATIONS;
2124 mwmhints.decorations = False;
2126 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2128 XChangeProperty( p_vout->p_sys->p_display,
2129 p_vout->p_sys->p_win->base_window,
2130 prop, prop, 32, PropModeReplace,
2131 (unsigned char *)&mwmhints,
2132 PROP_MWM_HINTS_ELEMENTS );
2136 /* brute force way to remove decorations */
2137 attributes.override_redirect = True;
2138 XChangeWindowAttributes( p_vout->p_sys->p_display,
2139 p_vout->p_sys->p_win->base_window,
2143 /* Make sure the change is effective */
2144 XReparentWindow( p_vout->p_sys->p_display,
2145 p_vout->p_sys->p_win->base_window,
2146 DefaultRootWindow( p_vout->p_sys->p_display ),
2150 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2152 XClientMessageEvent event;
2154 memset( &event, 0, sizeof( XClientMessageEvent ) );
2156 event.type = ClientMessage;
2157 event.message_type = p_vout->p_sys->net_wm_state;
2158 event.display = p_vout->p_sys->p_display;
2159 event.window = p_vout->p_sys->p_win->base_window;
2161 event.data.l[ 0 ] = 1; /* set property */
2162 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2164 XSendEvent( p_vout->p_sys->p_display,
2165 DefaultRootWindow( p_vout->p_sys->p_display ),
2166 False, SubstructureRedirectMask,
2170 /* Make sure the change is effective */
2171 XReparentWindow( p_vout->p_sys->p_display,
2172 p_vout->p_sys->p_win->base_window,
2173 DefaultRootWindow( p_vout->p_sys->p_display ),
2176 #ifdef HAVE_XINERAMA
2177 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2178 XineramaIsActive( p_vout->p_sys->p_display ) )
2180 XineramaScreenInfo *screens; /* infos for xinerama */
2183 msg_Dbg( p_vout, "using XFree Xinerama extension");
2185 #define SCREEN p_vout->p_sys->p_win->i_screen
2187 /* Get Information about Xinerama (num of screens) */
2188 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2191 SCREEN = config_GetInt( p_vout,
2192 MODULE_STRING "-xineramascreen" );
2194 /* just check that user has entered a good value */
2195 if( SCREEN >= i_num_screens || SCREEN < 0 )
2197 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2201 /* Get the X/Y upper left corner coordinate of the above screen */
2202 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2203 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2205 /* Set the Height/width to the screen resolution */
2206 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2207 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2217 /* The window wasn't necessarily created at the requested size */
2218 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2220 #ifdef HAVE_XF86VIDMODE
2221 XF86VidModeModeLine mode;
2224 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2225 p_vout->p_sys->i_screen, &i_dummy,
2228 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2229 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2231 /* move cursor to the middle of the window to prevent
2232 * unwanted display move if the display is smaller than the
2234 XWarpPointer( p_vout->p_sys->p_display, None,
2235 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2236 mode.hdisplay / 2 , mode.vdisplay / 2 );
2237 /* force desktop view to upper left corner */
2238 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2239 p_vout->p_sys->i_screen, 0, 0 );
2244 p_vout->p_sys->p_win->i_width =
2245 DisplayWidth( p_vout->p_sys->p_display,
2246 p_vout->p_sys->i_screen );
2247 p_vout->p_sys->p_win->i_height =
2248 DisplayHeight( p_vout->p_sys->p_display,
2249 p_vout->p_sys->i_screen );
2254 XMoveResizeWindow( p_vout->p_sys->p_display,
2255 p_vout->p_sys->p_win->base_window,
2256 p_vout->p_sys->p_win->i_x,
2257 p_vout->p_sys->p_win->i_y,
2258 p_vout->p_sys->p_win->i_width,
2259 p_vout->p_sys->p_win->i_height );
2262 EnablePixelDoubling( p_vout );
2265 /* Activate the window (give it the focus) */
2266 XClientMessageEvent event;
2268 memset( &event, 0, sizeof( XClientMessageEvent ) );
2270 event.type = ClientMessage;
2271 event.message_type =
2272 XInternAtom( p_vout->p_sys->p_display, "_NET_ACTIVE_WINDOW", False );
2273 event.display = p_vout->p_sys->p_display;
2274 event.window = p_vout->p_sys->p_win->base_window;
2276 event.data.l[ 0 ] = 1; /* source indication (1 = from an application */
2277 event.data.l[ 1 ] = 0; /* timestamp */
2278 event.data.l[ 2 ] = 0; /* requestor's currently active window */
2279 /* XXX: window manager would be more likely to obey if we already have
2280 * an active window (and give it to the event), such as an interface */
2282 XSendEvent( p_vout->p_sys->p_display,
2283 DefaultRootWindow( p_vout->p_sys->p_display ),
2284 False, SubstructureRedirectMask,
2289 msg_Dbg( p_vout, "leaving fullscreen mode" );
2292 DisablePixelDoubling( p_vout );
2295 XReparentWindow( p_vout->p_sys->p_display,
2296 p_vout->p_sys->original_window.video_window,
2297 p_vout->p_sys->original_window.base_window, 0, 0 );
2299 p_vout->p_sys->fullscreen_window.video_window = None;
2300 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2301 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2303 XMapWindow( p_vout->p_sys->p_display,
2304 p_vout->p_sys->p_win->base_window );
2307 /* Unfortunately, using XSync() here is not enough to ensure the
2308 * window has already been mapped because the XMapWindow() request
2309 * has not necessarily been sent directly to our window (remember,
2310 * the call is first redirected to the window manager) */
2313 XWindowEvent( p_vout->p_sys->p_display,
2314 p_vout->p_sys->p_win->base_window,
2315 StructureNotifyMask, &xevent );
2316 } while( xevent.type != MapNotify );
2318 /* Be careful, this can generate a BadMatch error if the window is not
2319 * already mapped by the server (see above) */
2320 XSetInputFocus(p_vout->p_sys->p_display,
2321 p_vout->p_sys->p_win->base_window,
2325 /* signal that the size needs to be updated */
2326 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2329 /*****************************************************************************
2330 * EnableXScreenSaver: enable screen saver
2331 *****************************************************************************
2332 * This function enables the screen saver on a display after it has been
2333 * disabled by XDisableScreenSaver.
2334 * FIXME: what happens if multiple vlc sessions are running at the same
2336 *****************************************************************************/
2337 static void EnableXScreenSaver( vout_thread_t *p_vout )
2339 #ifdef DPMSINFO_IN_DPMS_H
2343 if( p_vout->p_sys->i_ss_timeout )
2345 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2346 p_vout->p_sys->i_ss_interval,
2347 p_vout->p_sys->i_ss_blanking,
2348 p_vout->p_sys->i_ss_exposure );
2351 /* Restore DPMS settings */
2352 #ifdef DPMSINFO_IN_DPMS_H
2353 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2355 if( p_vout->p_sys->b_ss_dpms )
2357 DPMSEnable( p_vout->p_sys->p_display );
2363 /*****************************************************************************
2364 * DisableXScreenSaver: disable screen saver
2365 *****************************************************************************
2366 * See XEnableXScreenSaver
2367 *****************************************************************************/
2368 static void DisableXScreenSaver( vout_thread_t *p_vout )
2370 #ifdef DPMSINFO_IN_DPMS_H
2374 /* Save screen saver information */
2375 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2376 &p_vout->p_sys->i_ss_interval,
2377 &p_vout->p_sys->i_ss_blanking,
2378 &p_vout->p_sys->i_ss_exposure );
2380 /* Disable screen saver */
2381 if( p_vout->p_sys->i_ss_timeout )
2383 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2384 p_vout->p_sys->i_ss_interval,
2385 p_vout->p_sys->i_ss_blanking,
2386 p_vout->p_sys->i_ss_exposure );
2390 #ifdef DPMSINFO_IN_DPMS_H
2391 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2394 /* Save DPMS current state */
2395 DPMSInfo( p_vout->p_sys->p_display, &unused,
2396 &p_vout->p_sys->b_ss_dpms );
2397 DPMSDisable( p_vout->p_sys->p_display );
2402 /*****************************************************************************
2403 * CreateCursor: create a blank mouse pointer
2404 *****************************************************************************/
2405 static void CreateCursor( vout_thread_t *p_vout )
2407 XColor cursor_color;
2409 p_vout->p_sys->cursor_pixmap =
2410 XCreatePixmap( p_vout->p_sys->p_display,
2411 DefaultRootWindow( p_vout->p_sys->p_display ),
2414 XParseColor( p_vout->p_sys->p_display,
2415 XCreateColormap( p_vout->p_sys->p_display,
2417 p_vout->p_sys->p_display ),
2419 p_vout->p_sys->p_display,
2420 p_vout->p_sys->i_screen ),
2422 "black", &cursor_color );
2424 p_vout->p_sys->blank_cursor =
2425 XCreatePixmapCursor( p_vout->p_sys->p_display,
2426 p_vout->p_sys->cursor_pixmap,
2427 p_vout->p_sys->cursor_pixmap,
2428 &cursor_color, &cursor_color, 1, 1 );
2431 /*****************************************************************************
2432 * DestroyCursor: destroy the blank mouse pointer
2433 *****************************************************************************/
2434 static void DestroyCursor( vout_thread_t *p_vout )
2436 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2439 /*****************************************************************************
2440 * ToggleCursor: hide or show the mouse pointer
2441 *****************************************************************************
2442 * This function hides the X pointer if it is visible by setting the pointer
2443 * sprite to a blank one. To show it again, we disable the sprite.
2444 *****************************************************************************/
2445 static void ToggleCursor( vout_thread_t *p_vout )
2447 if( p_vout->p_sys->b_mouse_pointer_visible )
2449 XDefineCursor( p_vout->p_sys->p_display,
2450 p_vout->p_sys->p_win->base_window,
2451 p_vout->p_sys->blank_cursor );
2452 p_vout->p_sys->b_mouse_pointer_visible = 0;
2456 XUndefineCursor( p_vout->p_sys->p_display,
2457 p_vout->p_sys->p_win->base_window );
2458 p_vout->p_sys->b_mouse_pointer_visible = 1;
2462 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2463 /*****************************************************************************
2464 * XVideoGetPort: get YUV12 port
2465 *****************************************************************************/
2466 static int XVideoGetPort( vout_thread_t *p_vout,
2467 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
2469 XvAdaptorInfo *p_adaptor;
2471 unsigned int i_adaptor, i_num_adaptors;
2472 int i_requested_adaptor;
2473 int i_selected_port;
2475 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2480 case XvBadExtension:
2481 msg_Warn( p_vout, "XvBadExtension" );
2485 msg_Warn( p_vout, "XvBadAlloc" );
2489 msg_Warn( p_vout, "XvQueryExtension failed" );
2493 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2494 DefaultRootWindow( p_vout->p_sys->p_display ),
2495 &i_num_adaptors, &p_adaptor ) )
2500 case XvBadExtension:
2501 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2505 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2509 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2513 i_selected_port = -1;
2514 #ifdef MODULE_NAME_IS_xvmc
2515 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2517 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2519 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2521 XvImageFormatValues *p_formats;
2522 int i_format, i_num_formats;
2525 /* If we requested an adaptor and it's not this one, we aren't
2527 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2532 /* If the adaptor doesn't have the required properties, skip it */
2533 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2534 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2539 /* Check that adaptor supports our requested format... */
2540 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2541 p_adaptor[i_adaptor].base_id,
2545 i_format < i_num_formats && ( i_selected_port == -1 );
2548 XvAttribute *p_attr;
2549 int i_attr, i_num_attributes;
2551 /* If this is not the format we want, or at least a
2552 * similar one, forget it */
2553 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2558 /* Look for the first available port supporting this format */
2559 for( i_port = p_adaptor[i_adaptor].base_id;
2560 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2561 + p_adaptor[i_adaptor].num_ports) )
2562 && ( i_selected_port == -1 );
2565 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2568 i_selected_port = i_port;
2569 *pi_newchroma = p_formats[ i_format ].id;
2573 /* If no free port was found, forget it */
2574 if( i_selected_port == -1 )
2579 /* If we found a port, print information about it */
2580 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2581 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2582 (char *)&p_formats[ i_format ].id,
2583 ( p_formats[ i_format ].format == XvPacked ) ?
2584 "packed" : "planar" );
2586 /* Make sure XV_AUTOPAINT_COLORKEY is set */
2587 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2589 &i_num_attributes );
2591 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2593 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2595 const Atom autopaint =
2596 XInternAtom( p_vout->p_sys->p_display,
2597 "XV_AUTOPAINT_COLORKEY", False );
2598 XvSetPortAttribute( p_vout->p_sys->p_display,
2599 i_selected_port, autopaint, 1 );
2604 if( p_attr != NULL )
2610 if( p_formats != NULL )
2617 if( i_num_adaptors > 0 )
2619 XvFreeAdaptorInfo( p_adaptor );
2622 if( i_selected_port == -1 )
2624 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2625 if( i_requested_adaptor == -1 )
2627 msg_Warn( p_vout, "no free XVideo port found for format "
2628 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2632 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2633 "XVideo port for format 0x%.8x (%4.4s)",
2634 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2638 return i_selected_port;
2641 /*****************************************************************************
2642 * XVideoReleasePort: release YUV12 port
2643 *****************************************************************************/
2644 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2646 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2650 /*****************************************************************************
2651 * InitDisplay: open and initialize X11 device
2652 *****************************************************************************
2653 * Create a window according to video output given size, and set other
2654 * properties according to the display properties.
2655 *****************************************************************************/
2656 static int InitDisplay( vout_thread_t *p_vout )
2658 #ifdef MODULE_NAME_IS_x11
2659 XPixmapFormatValues * p_formats; /* pixmap formats */
2660 XVisualInfo * p_xvisual; /* visuals information */
2661 XVisualInfo xvisual_template; /* visual template */
2662 int i_count, i; /* array size */
2665 #ifdef HAVE_SYS_SHM_H
2666 p_vout->p_sys->i_shm_opcode = 0;
2668 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
2670 int major, evt, err;
2672 if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2674 && XShmQueryExtension( p_vout->p_sys->p_display ) )
2675 p_vout->p_sys->i_shm_opcode = major;
2677 if( p_vout->p_sys->i_shm_opcode )
2682 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2684 msg_Dbg( p_vout, "XShm video extension v%d.%d "
2685 "(with%s pixmaps, opcode: %d)",
2686 major, minor, pixmaps ? "" : "out",
2687 p_vout->p_sys->i_shm_opcode );
2690 msg_Warn( p_vout, "XShm video extension not available" );
2693 msg_Dbg( p_vout, "XShm video extension disabled" );
2696 #ifdef MODULE_NAME_IS_xvideo
2697 /* XXX The brightness and contrast values should be read from environment
2698 * XXX variables... */
2700 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2701 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2705 #ifdef MODULE_NAME_IS_x11
2706 /* Initialize structure */
2707 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2709 /* Get screen depth */
2710 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2711 p_vout->p_sys->i_screen );
2712 switch( p_vout->p_sys->i_screen_depth )
2716 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2718 xvisual_template.screen = p_vout->p_sys->i_screen;
2719 xvisual_template.class = DirectColor;
2720 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2721 VisualScreenMask | VisualClassMask,
2722 &xvisual_template, &i_count );
2723 if( p_xvisual == NULL )
2725 msg_Err( p_vout, "no PseudoColor visual available" );
2726 return VLC_EGENERIC;
2728 p_vout->p_sys->i_bytes_per_pixel = 1;
2729 p_vout->output.pf_setpalette = SetPalette;
2736 * Screen depth is higher than 8bpp. TrueColor visual is used.
2738 xvisual_template.screen = p_vout->p_sys->i_screen;
2739 xvisual_template.class = TrueColor;
2740 /* In some cases, we get a truecolor class adaptor that has a different
2741 color depth. So try to get a real true color one first */
2742 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2744 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2745 VisualScreenMask | VisualClassMask |
2747 &xvisual_template, &i_count );
2748 if( p_xvisual == NULL )
2750 msg_Warn( p_vout, "No screen matching the required color depth" );
2751 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2752 VisualScreenMask | VisualClassMask,
2753 &xvisual_template, &i_count );
2754 if( p_xvisual == NULL )
2757 msg_Err( p_vout, "no TrueColor visual available" );
2758 return VLC_EGENERIC;
2762 p_vout->output.i_rmask = p_xvisual->red_mask;
2763 p_vout->output.i_gmask = p_xvisual->green_mask;
2764 p_vout->output.i_bmask = p_xvisual->blue_mask;
2766 /* There is no difference yet between 3 and 4 Bpp. The only way
2767 * to find the actual number of bytes per pixel is to list supported
2768 * pixmap formats. */
2769 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2770 p_vout->p_sys->i_bytes_per_pixel = 0;
2772 for( i = 0; i < i_count; i++ )
2774 /* Under XFree4.0, the list contains pixmap formats available
2775 * through all video depths ; so we have to check against current
2777 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2779 if( p_formats[i].bits_per_pixel / 8
2780 > (int)p_vout->p_sys->i_bytes_per_pixel )
2782 p_vout->p_sys->i_bytes_per_pixel =
2783 p_formats[i].bits_per_pixel / 8;
2787 if( p_formats ) XFree( p_formats );
2791 p_vout->p_sys->p_visual = p_xvisual->visual;
2798 #ifndef MODULE_NAME_IS_glx
2800 #ifdef HAVE_SYS_SHM_H
2801 /*****************************************************************************
2802 * CreateShmImage: create an XImage or XvImage using shared memory extension
2803 *****************************************************************************
2804 * Prepare an XImage or XvImage for display function.
2805 * The order of the operations respects the recommandations of the mit-shm
2806 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2807 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2808 *****************************************************************************/
2809 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2810 Display* p_display, EXTRA_ARGS_SHM,
2811 int i_width, int i_height )
2813 IMAGE_TYPE *p_image;
2816 /* Create XImage / XvImage */
2817 #ifdef MODULE_NAME_IS_xvideo
2819 /* Make sure the buffer is aligned to multiple of 16 */
2820 i_height = ( i_height + 15 ) >> 4 << 4;
2821 i_width = ( i_width + 15 ) >> 4 << 4;
2823 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2824 i_width, i_height, p_shm );
2825 #elif defined(MODULE_NAME_IS_xvmc)
2826 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2827 i_width, i_height, p_shm );
2829 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2830 p_shm, i_width, i_height );
2832 if( p_image == NULL )
2834 msg_Err( p_vout, "image creation failed" );
2838 /* Allocate shared memory segment - 0776 set the access permission
2839 * rights (like umask), they are not yet supported by all X servers */
2840 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2841 if( p_shm->shmid < 0 )
2843 msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2844 IMAGE_FREE( p_image );
2848 /* Attach shared memory segment to process (read/write) */
2849 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2850 if(! p_shm->shmaddr )
2852 msg_Err( p_vout, "cannot attach shared memory (%m)" );
2853 IMAGE_FREE( p_image );
2854 shmctl( p_shm->shmid, IPC_RMID, 0 );
2858 /* Read-only data. We won't be using XShmGetImage */
2859 p_shm->readOnly = True;
2861 /* Attach shared memory segment to X server */
2862 XSynchronize( p_display, True );
2863 i_shm_major = p_vout->p_sys->i_shm_opcode;
2864 result = XShmAttach( p_display, p_shm );
2865 if( result == False || !i_shm_major )
2867 msg_Err( p_vout, "cannot attach shared memory to X server" );
2868 IMAGE_FREE( p_image );
2869 shmctl( p_shm->shmid, IPC_RMID, 0 );
2870 shmdt( p_shm->shmaddr );
2873 XSynchronize( p_display, False );
2875 /* Send image to X server. This instruction is required, since having
2876 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2877 * and remember NOT to use XFlush ! */
2878 XSync( p_display, False );
2881 /* Mark the shm segment to be removed when there are no more
2882 * attachements, so it is automatic on process exit or after shmdt */
2883 shmctl( p_shm->shmid, IPC_RMID, 0 );
2890 /*****************************************************************************
2891 * CreateImage: create an XImage or XvImage
2892 *****************************************************************************
2893 * Create a simple image used as a buffer.
2894 *****************************************************************************/
2895 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2896 Display *p_display, EXTRA_ARGS,
2897 int i_width, int i_height )
2899 byte_t * p_data; /* image data storage zone */
2900 IMAGE_TYPE *p_image;
2901 #ifdef MODULE_NAME_IS_x11
2902 int i_quantum; /* XImage quantum (see below) */
2903 int i_bytes_per_line;
2906 /* Allocate memory for image */
2907 #ifdef MODULE_NAME_IS_xvideo
2909 /* Make sure the buffer is aligned to multiple of 16 */
2910 i_height = ( i_height + 15 ) >> 4 << 4;
2911 i_width = ( i_width + 15 ) >> 4 << 4;
2913 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2914 #elif defined(MODULE_NAME_IS_x11)
2915 i_bytes_per_line = i_width * i_bytes_per_pixel;
2916 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2920 msg_Err( p_vout, "out of memory" );
2924 #ifdef MODULE_NAME_IS_x11
2925 /* Optimize the quantum of a scanline regarding its size - the quantum is
2926 a diviser of the number of bits between the start of two scanlines. */
2927 if( i_bytes_per_line & 0xf )
2931 else if( i_bytes_per_line & 0x10 )
2941 /* Create XImage. p_data will be automatically freed */
2942 #ifdef MODULE_NAME_IS_xvideo
2943 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2944 (char *)p_data, i_width, i_height );
2945 #elif defined(MODULE_NAME_IS_x11)
2946 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2947 (char *)p_data, i_width, i_height, i_quantum, 0 );
2949 if( p_image == NULL )
2951 msg_Err( p_vout, "XCreateImage() failed" );
2960 /*****************************************************************************
2961 * X11ErrorHandler: replace error handler so we can intercept some of them
2962 *****************************************************************************/
2963 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2967 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
2969 "[????????] x11 video output error: X11 request %u.%u failed "
2970 "with error code %u:\n %s\n",
2971 event->request_code, event->minor_code, event->error_code, txt );
2973 switch( event->request_code )
2975 case X_SetInputFocus:
2976 /* Ignore errors on XSetInputFocus()
2977 * (they happen when a window is not yet mapped) */
2981 #ifdef HAVE_SYS_SHM_H
2982 if( event->request_code == i_shm_major ) /* MIT-SHM */
2983 return i_shm_major = 0;
2986 XSetErrorHandler(NULL);
2987 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2990 #ifdef MODULE_NAME_IS_x11
2991 /*****************************************************************************
2992 * SetPalette: sets an 8 bpp palette
2993 *****************************************************************************
2994 * This function sets the palette given as an argument. It does not return
2995 * anything, but could later send information on which colors it was unable
2997 *****************************************************************************/
2998 static void SetPalette( vout_thread_t *p_vout,
2999 uint16_t *red, uint16_t *green, uint16_t *blue )
3002 XColor p_colors[255];
3004 /* allocate palette */
3005 for( i = 0; i < 255; i++ )
3007 /* kludge: colors are indexed reversely because color 255 seems
3008 * to be reserved for black even if we try to set it to white */
3009 p_colors[ i ].pixel = 255 - i;
3010 p_colors[ i ].pad = 0;
3011 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
3012 p_colors[ i ].red = red[ 255 - i ];
3013 p_colors[ i ].blue = blue[ 255 - i ];
3014 p_colors[ i ].green = green[ 255 - i ];
3017 XStoreColors( p_vout->p_sys->p_display,
3018 p_vout->p_sys->colormap, p_colors, 255 );
3022 /*****************************************************************************
3023 * Control: control facility for the vout
3024 *****************************************************************************/
3025 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
3028 unsigned int i_width, i_height;
3029 unsigned int *pi_width, *pi_height;
3035 if( p_vout->p_sys->p_win->owner_window )
3036 return vout_ControlWindow( p_vout,
3037 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3039 pi_width = va_arg( args, unsigned int * );
3040 pi_height = va_arg( args, unsigned int * );
3042 vlc_mutex_lock( &p_vout->p_sys->lock );
3043 *pi_width = p_vout->p_sys->p_win->i_width;
3044 *pi_height = p_vout->p_sys->p_win->i_height;
3045 vlc_mutex_unlock( &p_vout->p_sys->lock );
3049 if( p_vout->p_sys->p_win->owner_window )
3050 return vout_ControlWindow( p_vout,
3051 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3053 vlc_mutex_lock( &p_vout->p_sys->lock );
3055 i_width = va_arg( args, unsigned int );
3056 i_height = va_arg( args, unsigned int );
3057 if( !i_width ) i_width = p_vout->i_window_width;
3058 if( !i_height ) i_height = p_vout->i_window_height;
3060 #ifdef MODULE_NAME_IS_xvmc
3061 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3063 /* Update dimensions */
3064 XResizeWindow( p_vout->p_sys->p_display,
3065 p_vout->p_sys->p_win->base_window,
3066 i_width, i_height );
3067 #ifdef MODULE_NAME_IS_xvmc
3068 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3070 vlc_mutex_unlock( &p_vout->p_sys->lock );
3074 vlc_mutex_lock( &p_vout->p_sys->lock );
3075 XUnmapWindow( p_vout->p_sys->p_display,
3076 p_vout->p_sys->original_window.base_window );
3077 vlc_mutex_unlock( &p_vout->p_sys->lock );
3081 vlc_mutex_lock( &p_vout->p_sys->lock );
3082 if( i_query == VOUT_REPARENT ) d = (Drawable)va_arg( args, int );
3084 #ifdef MODULE_NAME_IS_xvmc
3085 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3087 XReparentWindow( p_vout->p_sys->p_display,
3088 p_vout->p_sys->original_window.base_window,
3089 DefaultRootWindow( p_vout->p_sys->p_display ),
3092 XReparentWindow( p_vout->p_sys->p_display,
3093 p_vout->p_sys->original_window.base_window,
3095 XSync( p_vout->p_sys->p_display, False );
3096 p_vout->p_sys->original_window.owner_window = 0;
3097 #ifdef MODULE_NAME_IS_xvmc
3098 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3100 vlc_mutex_unlock( &p_vout->p_sys->lock );
3101 return vout_vaControlDefault( p_vout, i_query, args );
3103 case VOUT_SET_STAY_ON_TOP:
3104 if( p_vout->p_sys->p_win->owner_window )
3105 return vout_ControlWindow( p_vout,
3106 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3108 b_arg = va_arg( args, vlc_bool_t );
3109 vlc_mutex_lock( &p_vout->p_sys->lock );
3110 #ifdef MODULE_NAME_IS_xvmc
3111 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3113 WindowOnTop( p_vout, b_arg );
3114 #ifdef MODULE_NAME_IS_xvmc
3115 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3117 vlc_mutex_unlock( &p_vout->p_sys->lock );
3121 return vout_vaControlDefault( p_vout, i_query, args );
3125 /*****************************************************************************
3126 * TestNetWMSupport: tests for Extended Window Manager Hints support
3127 *****************************************************************************/
3128 static void TestNetWMSupport( vout_thread_t *p_vout )
3130 int i_ret, i_format;
3131 unsigned long i, i_items, i_bytesafter;
3132 Atom net_wm_supported;
3133 union { Atom *p_atom; unsigned char *p_char; } p_args;
3135 p_args.p_atom = NULL;
3137 p_vout->p_sys->b_net_wm_state_fullscreen =
3138 p_vout->p_sys->b_net_wm_state_above =
3139 p_vout->p_sys->b_net_wm_state_below =
3140 p_vout->p_sys->b_net_wm_state_stays_on_top =
3144 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3146 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3147 DefaultRootWindow( p_vout->p_sys->p_display ),
3149 0, 16384, False, AnyPropertyType,
3151 &i_format, &i_items, &i_bytesafter,
3152 (unsigned char **)&p_args );
3154 if( i_ret != Success || i_items == 0 ) return;
3156 msg_Dbg( p_vout, "Window manager supports NetWM" );
3158 p_vout->p_sys->net_wm_state =
3159 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3160 p_vout->p_sys->net_wm_state_fullscreen =
3161 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3163 p_vout->p_sys->net_wm_state_above =
3164 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3165 p_vout->p_sys->net_wm_state_below =
3166 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3167 p_vout->p_sys->net_wm_state_stays_on_top =
3168 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3171 for( i = 0; i < i_items; i++ )
3173 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3176 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3177 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
3179 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3181 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3182 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
3184 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3186 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3187 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
3189 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3192 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3193 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
3197 XFree( p_args.p_atom );
3200 /*****************************************************************************
3201 * Key events handling
3202 *****************************************************************************/
3208 } x11keys_to_vlckeys[] =
3210 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3211 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3212 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3213 { XK_F12, KEY_F12 },
3215 { XK_Return, KEY_ENTER },
3216 { XK_KP_Enter, KEY_ENTER },
3217 { XK_space, KEY_SPACE },
3218 { XK_Escape, KEY_ESC },
3220 { XK_Menu, KEY_MENU },
3221 { XK_Left, KEY_LEFT },
3222 { XK_Right, KEY_RIGHT },
3224 { XK_Down, KEY_DOWN },
3226 { XK_Home, KEY_HOME },
3227 { XK_End, KEY_END },
3228 { XK_Page_Up, KEY_PAGEUP },
3229 { XK_Page_Down, KEY_PAGEDOWN },
3231 { XK_Insert, KEY_INSERT },
3232 { XK_Delete, KEY_DELETE },
3233 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3234 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3235 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3236 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3237 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3238 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3239 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3244 static int ConvertKey( int i_key )
3248 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3250 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3252 return x11keys_to_vlckeys[i].i_vlckey;
3259 /*****************************************************************************
3260 * WindowOnTop: Switches the "always on top" state of the video window.
3261 *****************************************************************************/
3262 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
3264 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3266 XClientMessageEvent event;
3268 memset( &event, 0, sizeof( XClientMessageEvent ) );
3270 event.type = ClientMessage;
3271 event.message_type = p_vout->p_sys->net_wm_state;
3272 event.display = p_vout->p_sys->p_display;
3273 event.window = p_vout->p_sys->p_win->base_window;
3275 event.data.l[ 0 ] = b_on_top; /* set property */
3276 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3278 XSendEvent( p_vout->p_sys->p_display,
3279 DefaultRootWindow( p_vout->p_sys->p_display ),
3280 False, SubstructureRedirectMask,
3284 /* use _NET_WM_STATE_ABOVE if window manager
3285 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3286 else if( p_vout->p_sys->b_net_wm_state_above )
3288 XClientMessageEvent event;
3290 memset( &event, 0, sizeof( XClientMessageEvent ) );
3292 event.type = ClientMessage;
3293 event.message_type = p_vout->p_sys->net_wm_state;
3294 event.display = p_vout->p_sys->p_display;
3295 event.window = p_vout->p_sys->p_win->base_window;
3297 event.data.l[ 0 ] = b_on_top; /* set property */
3298 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3300 XSendEvent( p_vout->p_sys->p_display,
3301 DefaultRootWindow( p_vout->p_sys->p_display ),
3302 False, SubstructureRedirectMask,