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 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
40 #ifdef HAVE_MACHINE_PARAM_H
42 # include <machine/param.h>
43 # include <sys/types.h> /* typedef ushort */
48 # include <netinet/in.h> /* BSD: struct in_addr */
52 #include <X11/extensions/Xsp.h>
56 # include <sys/shm.h> /* shmget(), shmctl() */
60 #include <X11/Xproto.h>
62 #include <X11/Xutil.h>
63 #include <X11/keysym.h>
64 #include <X11/XF86keysym.h>
66 # include <X11/extensions/XShm.h>
68 #ifdef DPMSINFO_IN_DPMS_H
69 # include <X11/extensions/dpms.h>
72 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
73 # include <X11/extensions/Xv.h>
74 # include <X11/extensions/Xvlib.h>
77 #ifdef MODULE_NAME_IS_glx
82 # include <X11/extensions/Xinerama.h>
85 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
86 # include <X11/extensions/xf86vmode.h>
89 #ifdef MODULE_NAME_IS_xvmc
90 # include <X11/extensions/vldXvMC.h>
91 # include "../../codec/xvmc/accel_xvmc.h"
96 /*****************************************************************************
98 *****************************************************************************/
99 int E_(Activate) ( vlc_object_t * );
100 void E_(Deactivate) ( vlc_object_t * );
102 static int InitVideo ( vout_thread_t * );
103 static void EndVideo ( vout_thread_t * );
104 static void DisplayVideo ( vout_thread_t *, picture_t * );
105 static int ManageVideo ( vout_thread_t * );
106 static int Control ( vout_thread_t *, int, va_list );
108 static int InitDisplay ( vout_thread_t * );
110 static int CreateWindow ( vout_thread_t *, x11_window_t * );
111 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
113 static int NewPicture ( vout_thread_t *, picture_t * );
114 static void FreePicture ( vout_thread_t *, picture_t * );
116 static IMAGE_TYPE *CreateImage ( vout_thread_t *,
117 Display *, EXTRA_ARGS, int, int );
118 #ifdef HAVE_SYS_SHM_H
119 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
120 Display *, EXTRA_ARGS_SHM, int, int );
121 static vlc_bool_t b_shm = VLC_TRUE;
124 static void ToggleFullScreen ( vout_thread_t * );
126 static void EnableXScreenSaver ( vout_thread_t * );
127 static void DisableXScreenSaver ( vout_thread_t * );
129 static void CreateCursor ( vout_thread_t * );
130 static void DestroyCursor ( vout_thread_t * );
131 static void ToggleCursor ( vout_thread_t * );
133 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
134 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
135 static void XVideoReleasePort( vout_thread_t *, int );
138 #ifdef MODULE_NAME_IS_x11
139 static void SetPalette ( vout_thread_t *,
140 uint16_t *, uint16_t *, uint16_t * );
143 #ifdef MODULE_NAME_IS_xvmc
144 static void RenderVideo ( vout_thread_t *, picture_t * );
145 static int xvmc_check_yv12( Display *display, XvPortID port );
146 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
149 static void TestNetWMSupport( vout_thread_t * );
150 static int ConvertKey( int );
152 static int WindowOnTop( vout_thread_t *, vlc_bool_t );
154 static int X11ErrorHandler( Display *, XErrorEvent * );
157 static void EnablePixelDoubling( vout_thread_t *p_vout );
158 static void DisablePixelDoubling( vout_thread_t *p_vout );
162 static const int i_backlight_on_interval = 300;
167 /*****************************************************************************
168 * Activate: allocate X11 video thread output method
169 *****************************************************************************
170 * This function allocate and initialize a X11 vout method. It uses some of the
171 * vout properties to choose the window size, and change them according to the
172 * actual properties of the display.
173 *****************************************************************************/
174 int E_(Activate) ( vlc_object_t *p_this )
176 vout_thread_t *p_vout = (vout_thread_t *)p_this;
179 #if defined(MODULE_NAME_IS_xvmc)
182 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
184 vlc_fourcc_t i_chroma = 0;
185 vlc_bool_t b_chroma = 0;
188 p_vout->pf_init = InitVideo;
189 p_vout->pf_end = EndVideo;
190 p_vout->pf_manage = ManageVideo;
191 #ifdef MODULE_NAME_IS_xvmc
192 p_vout->pf_render = RenderVideo;
194 p_vout->pf_render = NULL;
196 p_vout->pf_display = DisplayVideo;
197 p_vout->pf_control = Control;
199 /* Allocate structure */
200 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
201 if( p_vout->p_sys == NULL )
203 msg_Err( p_vout, "out of memory" );
207 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
209 /* Open display, using the "display" config variable or the DISPLAY
210 * environment variable */
211 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
213 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
215 if( p_vout->p_sys->p_display == NULL ) /* error */
217 msg_Err( p_vout, "cannot open display %s",
218 XDisplayName( psz_display ) );
219 free( p_vout->p_sys );
220 if( psz_display ) free( psz_display );
223 if( psz_display ) free( psz_display );
225 /* Replace error handler so we can intercept some non-fatal errors */
226 XSetErrorHandler( X11ErrorHandler );
228 /* Get a screen ID matching the XOpenDisplay return value */
229 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
231 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
232 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
235 if( strlen( psz_chroma ) >= 4 )
237 /* Do not use direct assignment because we are not sure of the
239 memcpy(&i_chroma, psz_chroma, 4);
248 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
249 i_chroma, (char*)&i_chroma );
253 i_chroma = p_vout->render.i_chroma;
256 /* Check that we have access to an XVideo port providing this chroma */
257 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
258 &p_vout->output.i_chroma );
259 if( p_vout->p_sys->i_xvport < 0 )
261 /* If a specific chroma format was requested, then we don't try to
262 * be cleverer than the user. He knew pretty well what he wanted. */
265 XCloseDisplay( p_vout->p_sys->p_display );
266 free( p_vout->p_sys );
270 /* It failed, but it's not completely lost ! We try to open an
271 * XVideo port for an YUY2 picture. We'll need to do an YUV
272 * conversion, but at least it has got scaling. */
273 p_vout->p_sys->i_xvport =
274 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
275 &p_vout->output.i_chroma );
276 if( p_vout->p_sys->i_xvport < 0 )
278 /* It failed, but it's not completely lost ! We try to open an
279 * XVideo port for a simple 16bpp RGB picture. We'll need to do
280 * an YUV conversion, but at least it has got scaling. */
281 p_vout->p_sys->i_xvport =
282 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
283 &p_vout->output.i_chroma );
284 if( p_vout->p_sys->i_xvport < 0 )
286 XCloseDisplay( p_vout->p_sys->p_display );
287 free( p_vout->p_sys );
292 p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
293 #elif defined(MODULE_NAME_IS_glx)
295 int i_opcode, i_evt, i_err = 0;
296 int i_maj, i_min = 0;
298 /* Check for GLX extension */
299 if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
300 &i_opcode, &i_evt, &i_err ) )
302 msg_Err( p_this, "GLX extension not supported" );
303 XCloseDisplay( p_vout->p_sys->p_display );
304 free( p_vout->p_sys );
307 if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
309 msg_Err( p_this, "glXQueryExtension failed" );
310 XCloseDisplay( p_vout->p_sys->p_display );
311 free( p_vout->p_sys );
315 /* Check GLX version */
316 if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
318 msg_Err( p_this, "glXQueryVersion failed" );
319 XCloseDisplay( p_vout->p_sys->p_display );
320 free( p_vout->p_sys );
323 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
325 p_vout->p_sys->b_glx13 = VLC_FALSE;
326 msg_Dbg( p_this, "using GLX 1.2 API" );
330 p_vout->p_sys->b_glx13 = VLC_TRUE;
331 msg_Dbg( p_this, "using GLX 1.3 API" );
336 /* Create blank cursor (for mouse cursor autohiding) */
337 p_vout->p_sys->i_time_mouse_last_moved = mdate();
338 p_vout->p_sys->b_mouse_pointer_visible = 1;
339 CreateCursor( p_vout );
341 /* Set main window's size */
342 p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
343 p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
344 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
345 /* Spawn base window - this window will include the video output window,
346 * but also command buttons, subtitles and other indicators */
347 if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
349 msg_Err( p_vout, "cannot create X11 window" );
350 DestroyCursor( p_vout );
351 XCloseDisplay( p_vout->p_sys->p_display );
352 free( p_vout->p_sys );
356 /* Open and initialize device. */
357 if( InitDisplay( p_vout ) )
359 msg_Err( p_vout, "cannot initialize X11 display" );
360 DestroyCursor( p_vout );
361 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
362 XCloseDisplay( p_vout->p_sys->p_display );
363 free( p_vout->p_sys );
367 /* Disable screen saver */
368 DisableXScreenSaver( p_vout );
371 p_vout->p_sys->b_altfullscreen = 0;
372 p_vout->p_sys->i_time_button_last_pressed = 0;
374 TestNetWMSupport( p_vout );
376 #ifdef MODULE_NAME_IS_xvmc
377 p_vout->p_sys->p_last_subtitle_save = NULL;
378 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
380 /* Look what method was requested */
381 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
382 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
385 if( (strcmp(psz_value, "bob") == 0) ||
386 (strcmp(psz_value, "blend") == 0) )
387 p_vout->p_sys->xvmc_deinterlace_method = 2;
388 else if (strcmp(psz_value, "discard") == 0)
389 p_vout->p_sys->xvmc_deinterlace_method = 1;
391 p_vout->p_sys->xvmc_deinterlace_method = 0;
395 p_vout->p_sys->xvmc_deinterlace_method = 0;
397 /* Look what method was requested */
398 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
399 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
400 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
404 if( strncmp( psz_value, "eq", 2 ) == 0 )
405 p_vout->p_sys->xvmc_crop_style = 1;
406 else if( strncmp( psz_value, "4-16", 4 ) == 0)
407 p_vout->p_sys->xvmc_crop_style = 2;
408 else if( strncmp( psz_value, "16-4", 4 ) == 0)
409 p_vout->p_sys->xvmc_crop_style = 3;
411 p_vout->p_sys->xvmc_crop_style = 0;
415 p_vout->p_sys->xvmc_crop_style = 0;
417 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
418 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
420 if( !checkXvMCCap( p_vout ) )
422 msg_Err( p_vout, "no XVMC capability found" );
423 E_(Deactivate)( p_vout );
426 sub_pic.p_sys = NULL;
427 p_vout->p_sys->last_date = 0;
431 p_vout->p_sys->i_hw_scale = 1;
435 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
436 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
437 if ( p_vout->p_sys->p_octx == NULL ) {
438 msg_Err( p_vout, "Could not get osso context" );
440 msg_Dbg( p_vout, "Initialized osso context" );
444 /* Variable to indicate if the window should be on top of others */
445 /* Trigger a callback right now */
446 var_Get( p_vout, "video-on-top", &val );
447 var_Set( p_vout, "video-on-top", val );
452 /*****************************************************************************
453 * Deactivate: destroy X11 video thread output method
454 *****************************************************************************
455 * Terminate an output method created by Open
456 *****************************************************************************/
457 void E_(Deactivate) ( vlc_object_t *p_this )
459 vout_thread_t *p_vout = (vout_thread_t *)p_this;
461 /* If the fullscreen window is still open, close it */
462 if( p_vout->b_fullscreen )
464 ToggleFullScreen( p_vout );
467 /* Restore cursor if it was blanked */
468 if( !p_vout->p_sys->b_mouse_pointer_visible )
470 ToggleCursor( p_vout );
473 #ifdef MODULE_NAME_IS_x11
474 /* Destroy colormap */
475 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
477 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
479 #elif defined(MODULE_NAME_IS_xvideo)
480 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
481 #elif defined(MODULE_NAME_IS_xvmc)
482 if( p_vout->p_sys->xvmc_cap )
484 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
485 xxmc_dispose_context( p_vout );
486 if( p_vout->p_sys->old_subpic )
488 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
489 p_vout->p_sys->old_subpic = NULL;
491 if( p_vout->p_sys->new_subpic )
493 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
494 p_vout->p_sys->new_subpic = NULL;
496 free( p_vout->p_sys->xvmc_cap );
497 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
502 DisablePixelDoubling(p_vout);
505 DestroyCursor( p_vout );
506 EnableXScreenSaver( p_vout );
507 DestroyWindow( p_vout, &p_vout->p_sys->original_window );
508 XCloseDisplay( p_vout->p_sys->p_display );
510 /* Destroy structure */
511 vlc_mutex_destroy( &p_vout->p_sys->lock );
512 #ifdef MODULE_NAME_IS_xvmc
513 free_context_lock( &p_vout->p_sys->xvmc_lock );
517 if ( p_vout->p_sys->p_octx != NULL ) {
518 msg_Dbg( p_vout, "Deinitializing osso context" );
519 osso_deinitialize( p_vout->p_sys->p_octx );
523 free( p_vout->p_sys );
526 #ifdef MODULE_NAME_IS_xvmc
528 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
531 static int xvmc_check_yv12( Display *display, XvPortID port )
533 XvImageFormatValues *formatValues;
537 formatValues = XvListImageFormats( display, port, &formats );
539 for( i = 0; i < formats; i++ )
541 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
542 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
544 XFree (formatValues);
549 XFree (formatValues);
553 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
555 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
558 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
561 int xv_double_buffer;
563 xv_double_buffer = 1;
565 XLockDisplay( p_vout->p_sys->p_display );
566 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
568 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
570 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
571 XUnlockDisplay( p_vout->p_sys->p_display );
573 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
574 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
577 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
579 vlc_xxmc_t *xxmc = NULL;
581 vlc_mutex_lock( &p_vout->p_sys->lock );
582 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
584 xxmc = &p_pic->p_sys->xxmc_data;
585 if( (!xxmc->decoded ||
586 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
588 vlc_mutex_unlock( &p_vout->p_sys->lock );
589 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
593 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
595 if (p_vout->p_last_subtitle != NULL)
597 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_last_subtitle )
599 p_vout->p_sys->new_subpic =
600 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
601 p_vout->p_sys->xvmc_width,
602 p_vout->p_sys->xvmc_height,
603 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
605 if (p_vout->p_sys->new_subpic)
607 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
608 XvMCClearSubpicture( p_vout->p_sys->p_display,
609 p_vout->p_sys->new_subpic,
612 p_vout->p_sys->xvmc_width,
613 p_vout->p_sys->xvmc_height,
615 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
616 clear_xx44_palette( &p_vout->p_sys->palette );
618 if( sub_pic.p_sys == NULL )
620 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
621 if( sub_pic.p_sys != NULL )
623 sub_pic.p_sys->p_vout = p_vout;
624 sub_pic.p_sys->xvmc_surf = NULL;
625 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
628 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
629 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
630 sub_pic.p->i_pitch = p_vout->output.i_width;
632 memset( p_vout->p_sys->subImage->data, 0,
633 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
635 if (p_vout->p_last_subtitle != NULL)
637 blend_xx44( p_vout->p_sys->subImage->data,
638 p_vout->p_last_subtitle,
639 p_vout->p_sys->subImage->width,
640 p_vout->p_sys->subImage->height,
641 p_vout->p_sys->subImage->width,
642 &p_vout->p_sys->palette,
643 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
646 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
647 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
648 p_vout->p_sys->new_subpic,
649 p_vout->p_sys->subImage,
652 p_vout->output.i_width, /* overlay->width, */
653 p_vout->output.i_height, /* overlay->height */
656 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
657 if (p_vout->p_sys->old_subpic)
659 xxmc_xvmc_free_subpicture( p_vout,
660 p_vout->p_sys->old_subpic);
661 p_vout->p_sys->old_subpic = NULL;
663 if (p_vout->p_sys->new_subpic)
665 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
666 p_vout->p_sys->new_subpic = NULL;
667 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
668 p_vout->p_sys->xvmc_palette,
670 p_vout->p_sys->old_subpic->num_palette_entries,
671 p_vout->p_sys->old_subpic->entry_bytes,
672 p_vout->p_sys->old_subpic->component_order );
673 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
674 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
675 p_vout->p_sys->old_subpic,
676 p_vout->p_sys->xvmc_palette );
677 XvMCFlushSubpicture( p_vout->p_sys->p_display,
678 p_vout->p_sys->old_subpic);
679 XvMCSyncSubpicture( p_vout->p_sys->p_display,
680 p_vout->p_sys->old_subpic );
681 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
684 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
685 if (p_vout->p_sys->xvmc_backend_subpic )
687 XvMCBlendSubpicture( p_vout->p_sys->p_display,
688 p_pic->p_sys->xvmc_surf,
689 p_vout->p_sys->old_subpic,
692 p_vout->p_sys->xvmc_width,
693 p_vout->p_sys->xvmc_height,
696 p_vout->p_sys->xvmc_width,
697 p_vout->p_sys->xvmc_height );
701 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
702 p_pic->p_sys->xvmc_surf,
703 p_pic->p_sys->xvmc_surf,
704 p_vout->p_sys->old_subpic,
707 p_vout->p_sys->xvmc_width,
708 p_vout->p_sys->xvmc_height,
711 p_vout->p_sys->xvmc_width,
712 p_vout->p_sys->xvmc_height );
714 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
719 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
720 if( p_vout->p_sys->xvmc_backend_subpic )
722 XvMCBlendSubpicture( p_vout->p_sys->p_display,
723 p_pic->p_sys->xvmc_surf,
724 p_vout->p_sys->old_subpic,
726 p_vout->p_sys->xvmc_width,
727 p_vout->p_sys->xvmc_height,
729 p_vout->p_sys->xvmc_width,
730 p_vout->p_sys->xvmc_height );
734 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
735 p_pic->p_sys->xvmc_surf,
736 p_pic->p_sys->xvmc_surf,
737 p_vout->p_sys->old_subpic,
739 p_vout->p_sys->xvmc_width,
740 p_vout->p_sys->xvmc_height,
742 p_vout->p_sys->xvmc_width,
743 p_vout->p_sys->xvmc_height );
745 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
748 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
750 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
751 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
753 vlc_mutex_unlock( &p_vout->p_sys->lock );
758 /*****************************************************************************
759 * EnablePixelDoubling: Enables pixel doubling
760 *****************************************************************************
761 * Checks if the double size image fits in current window, and enables pixel
762 * doubling accordingly. The i_hw_scale is the integer scaling factor.
763 *****************************************************************************/
764 static void EnablePixelDoubling( vout_thread_t *p_vout )
766 int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
767 int i_vert_scale = ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
768 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
769 p_vout->p_sys->i_hw_scale = 2;
770 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
771 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
775 /*****************************************************************************
776 * DisablePixelDoubling: Disables pixel doubling
777 *****************************************************************************
778 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
779 *****************************************************************************/
780 static void DisablePixelDoubling( vout_thread_t *p_vout )
782 if ( p_vout->p_sys->i_hw_scale > 1 ) {
783 msg_Dbg( p_vout, "Disabling pixel doubling" );
784 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
785 p_vout->p_sys->i_hw_scale = 1;
792 /*****************************************************************************
793 * InitVideo: initialize X11 video thread output method
794 *****************************************************************************
795 * This function create the XImages needed by the output thread. It is called
796 * at the beginning of the thread, but also each time the window is resized.
797 *****************************************************************************/
798 static int InitVideo( vout_thread_t *p_vout )
800 unsigned int i_index = 0;
803 I_OUTPUTPICTURES = 0;
805 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
806 /* Initialize the output structure; we already found an XVideo port,
807 * and the corresponding chroma we will be using. Since we can
808 * arbitrary scale, stick to the coordinates and aspect. */
809 p_vout->output.i_width = p_vout->render.i_width;
810 p_vout->output.i_height = p_vout->render.i_height;
811 p_vout->output.i_aspect = p_vout->render.i_aspect;
813 p_vout->fmt_out = p_vout->fmt_in;
814 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
816 switch( p_vout->output.i_chroma )
818 case VLC_FOURCC('R','V','1','6'):
819 #if defined( WORDS_BIGENDIAN )
820 p_vout->output.i_rmask = 0xf800;
821 p_vout->output.i_gmask = 0x07e0;
822 p_vout->output.i_bmask = 0x001f;
824 p_vout->output.i_rmask = 0x001f;
825 p_vout->output.i_gmask = 0x07e0;
826 p_vout->output.i_bmask = 0xf800;
829 case VLC_FOURCC('R','V','1','5'):
830 #if defined( WORDS_BIGENDIAN )
831 p_vout->output.i_rmask = 0x7c00;
832 p_vout->output.i_gmask = 0x03e0;
833 p_vout->output.i_bmask = 0x001f;
835 p_vout->output.i_rmask = 0x001f;
836 p_vout->output.i_gmask = 0x03e0;
837 p_vout->output.i_bmask = 0x7c00;
842 #elif defined(MODULE_NAME_IS_x11)
843 /* Initialize the output structure: RGB with square pixels, whatever
844 * the input format is, since it's the only format we know */
845 switch( p_vout->p_sys->i_screen_depth )
847 case 8: /* FIXME: set the palette */
848 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
850 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
852 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
855 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
857 msg_Err( p_vout, "unknown screen depth %i",
858 p_vout->p_sys->i_screen_depth );
863 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width / p_vout->p_sys->i_hw_scale,
864 p_vout->p_sys->p_win->i_height / p_vout->p_sys->i_hw_scale,
866 &p_vout->fmt_out.i_visible_width,
867 &p_vout->fmt_out.i_visible_height );
869 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
870 p_vout->p_sys->p_win->i_height,
872 &p_vout->fmt_out.i_visible_width,
873 &p_vout->fmt_out.i_visible_height );
876 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
878 p_vout->output.i_width = p_vout->fmt_out.i_width =
879 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
880 p_vout->fmt_in.i_visible_width;
881 p_vout->output.i_height = p_vout->fmt_out.i_height =
882 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
883 p_vout->fmt_in.i_visible_height;
884 p_vout->fmt_out.i_x_offset =
885 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
886 p_vout->fmt_in.i_visible_width;
887 p_vout->fmt_out.i_y_offset =
888 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
889 p_vout->fmt_in.i_visible_height;
891 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
892 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
893 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
895 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
896 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
897 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
898 p_vout->fmt_out.i_visible_width,
899 p_vout->fmt_out.i_visible_height );
902 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
903 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
907 /* Find an empty picture slot */
908 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
910 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
912 p_pic = p_vout->p_picture + i_index;
917 /* Allocate the picture */
918 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
923 p_pic->i_status = DESTROYED_PICTURE;
924 p_pic->i_type = DIRECT_PICTURE;
926 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
931 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
933 /* U and V inverted compared to I420
934 * Fixme: this should be handled by the vout core */
935 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
936 p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
942 /*****************************************************************************
943 * DisplayVideo: displays previously rendered output
944 *****************************************************************************
945 * This function sends the currently rendered image to X11 server.
946 * (The Xv extension takes care of "double-buffering".)
947 *****************************************************************************/
948 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
950 unsigned int i_width, i_height, i_x, i_y;
952 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
953 p_vout->p_sys->p_win->i_height,
954 &i_x, &i_y, &i_width, &i_height );
956 vlc_mutex_lock( &p_vout->p_sys->lock );
958 #ifdef MODULE_NAME_IS_xvmc
959 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
961 vlc_xxmc_t *xxmc = &p_picture->p_sys->xxmc_data;
962 if( !xxmc->decoded ||
963 !xxmc_xvmc_surface_valid( p_vout, p_picture->p_sys->xvmc_surf ) )
965 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
967 xxmc_xvmc_surface_valid( p_vout, p_picture->p_sys->xvmc_surf ) );
968 vlc_mutex_unlock( &p_vout->p_sys->lock );
969 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
973 src_width = p_vout->output.i_width;
974 src_height = p_vout->output.i_height;
976 if( p_vout->p_sys->xvmc_crop_style == 1 )
983 else if( p_vout->p_sys->xvmc_crop_style == 2 )
990 else if( p_vout->p_sys->xvmc_crop_style == 3 )
1003 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
1004 { /* BOB DEINTERLACE */
1005 if( (p_picture->p_sys->nb_display == 0) ||
1006 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
1008 first_field = (p_picture->b_top_field_first) ?
1009 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
1013 first_field = (p_picture->b_top_field_first) ?
1014 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
1019 first_field = XVMC_FRAME_PICTURE;
1022 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1023 XvMCFlushSurface( p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf );
1024 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1025 XvMCPutSurface( p_vout->p_sys->p_display,
1026 p_picture->p_sys->xvmc_surf,
1027 p_vout->p_sys->p_win->video_window,
1038 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1039 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1040 { /* BOB DEINTERLACE */
1041 if( p_picture->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1043 mtime_t last_date = p_picture->date;
1045 vlc_mutex_lock( &p_vout->picture_lock );
1046 if( !p_vout->p_sys->last_date )
1048 p_picture->date += 20000;
1052 p_picture->date = ((3 * p_picture->date -
1053 p_vout->p_sys->last_date) / 2 );
1055 p_vout->p_sys->last_date = last_date;
1056 p_picture->b_force = 1;
1057 p_picture->p_sys->nb_display = 1;
1058 vlc_mutex_unlock( &p_vout->picture_lock );
1062 p_picture->p_sys->nb_display = 0;
1063 p_picture->b_force = 0;
1066 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1069 #ifdef HAVE_SYS_SHM_H
1070 if( p_vout->p_sys->b_shm )
1072 /* Display rendered image using shared memory extension */
1073 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1074 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1075 p_vout->p_sys->p_win->video_window,
1076 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1077 p_vout->fmt_out.i_x_offset,
1078 p_vout->fmt_out.i_y_offset,
1079 p_vout->fmt_out.i_visible_width,
1080 p_vout->fmt_out.i_visible_height,
1081 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1082 False /* Don't put True here or you'll waste your CPU */ );
1084 XShmPutImage( p_vout->p_sys->p_display,
1085 p_vout->p_sys->p_win->video_window,
1086 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1087 p_vout->fmt_out.i_x_offset,
1088 p_vout->fmt_out.i_y_offset,
1089 0 /*dest_x*/, 0 /*dest_y*/,
1090 p_vout->fmt_out.i_visible_width,
1091 p_vout->fmt_out.i_visible_height,
1092 False /* Don't put True here ! */ );
1096 #endif /* HAVE_SYS_SHM_H */
1098 /* Use standard XPutImage -- this is gonna be slow ! */
1099 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1100 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1101 p_vout->p_sys->p_win->video_window,
1102 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1103 p_vout->fmt_out.i_x_offset,
1104 p_vout->fmt_out.i_y_offset,
1105 p_vout->fmt_out.i_visible_width,
1106 p_vout->fmt_out.i_visible_height,
1107 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1109 XPutImage( p_vout->p_sys->p_display,
1110 p_vout->p_sys->p_win->video_window,
1111 p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1112 p_vout->fmt_out.i_x_offset,
1113 p_vout->fmt_out.i_y_offset,
1114 0 /*dest_x*/, 0 /*dest_y*/,
1115 p_vout->fmt_out.i_visible_width,
1116 p_vout->fmt_out.i_visible_height );
1120 /* Make sure the command is sent now - do NOT use XFlush !*/
1121 XSync( p_vout->p_sys->p_display, False );
1123 vlc_mutex_unlock( &p_vout->p_sys->lock );
1126 /*****************************************************************************
1127 * ManageVideo: handle X11 events
1128 *****************************************************************************
1129 * This function should be called regularly by video output thread. It manages
1130 * X11 events and allows window resizing. It returns a non null value on
1132 *****************************************************************************/
1133 static int ManageVideo( vout_thread_t *p_vout )
1135 XEvent xevent; /* X11 event */
1138 vlc_mutex_lock( &p_vout->p_sys->lock );
1140 #ifdef MODULE_NAME_IS_xvmc
1141 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1144 /* Handle events from the owner window */
1145 if( p_vout->p_sys->p_win->owner_window )
1147 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1148 p_vout->p_sys->p_win->owner_window,
1149 StructureNotifyMask, &xevent ) == True )
1151 /* ConfigureNotify event: prepare */
1152 if( xevent.type == ConfigureNotify )
1154 /* Update dimensions */
1155 XResizeWindow( p_vout->p_sys->p_display,
1156 p_vout->p_sys->p_win->base_window,
1157 xevent.xconfigure.width,
1158 xevent.xconfigure.height );
1163 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1164 * output window's size changed, MapNotify and UnmapNotify to know if the
1165 * window is mapped (and if the display is useful), and ClientMessages
1166 * to intercept window destruction requests */
1168 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1169 p_vout->p_sys->p_win->base_window,
1170 StructureNotifyMask | KeyPressMask |
1171 ButtonPressMask | ButtonReleaseMask |
1172 PointerMotionMask | Button1MotionMask , &xevent )
1175 /* ConfigureNotify event: prepare */
1176 if( xevent.type == ConfigureNotify )
1178 if( (unsigned int)xevent.xconfigure.width
1179 != p_vout->p_sys->p_win->i_width
1180 || (unsigned int)xevent.xconfigure.height
1181 != p_vout->p_sys->p_win->i_height )
1183 /* Update dimensions */
1184 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1185 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
1186 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
1189 /* Keyboard event */
1190 else if( xevent.type == KeyPress )
1192 unsigned int state = xevent.xkey.state;
1193 KeySym x_key_symbol;
1194 char i_key; /* ISO Latin-1 key */
1196 /* We may have keys like F1 trough F12, ESC ... */
1197 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1198 xevent.xkey.keycode, 0 );
1199 val.i_int = ConvertKey( (int)x_key_symbol );
1201 xevent.xkey.state &= ~ShiftMask;
1202 xevent.xkey.state &= ~ControlMask;
1203 xevent.xkey.state &= ~Mod1Mask;
1206 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1209 * The reason why I use this instead of XK_0 is that
1210 * with XLookupString, we don't have to care about
1217 if( state & ShiftMask )
1219 val.i_int |= KEY_MODIFIER_SHIFT;
1221 if( state & ControlMask )
1223 val.i_int |= KEY_MODIFIER_CTRL;
1225 if( state & Mod1Mask )
1227 val.i_int |= KEY_MODIFIER_ALT;
1229 var_Set( p_vout->p_libvlc, "key-pressed", val );
1233 else if( xevent.type == ButtonPress )
1235 switch( ((XButtonEvent *)&xevent)->button )
1238 var_Get( p_vout, "mouse-button-down", &val );
1240 var_Set( p_vout, "mouse-button-down", val );
1242 /* detect double-clicks */
1243 if( ( ((XButtonEvent *)&xevent)->time -
1244 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1246 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1249 p_vout->p_sys->i_time_button_last_pressed =
1250 ((XButtonEvent *)&xevent)->time;
1253 var_Get( p_vout, "mouse-button-down", &val );
1255 var_Set( p_vout, "mouse-button-down", val );
1259 var_Get( p_vout, "mouse-button-down", &val );
1261 var_Set( p_vout, "mouse-button-down", val );
1265 var_Get( p_vout, "mouse-button-down", &val );
1267 var_Set( p_vout, "mouse-button-down", val );
1271 var_Get( p_vout, "mouse-button-down", &val );
1273 var_Set( p_vout, "mouse-button-down", val );
1278 else if( xevent.type == ButtonRelease )
1280 switch( ((XButtonEvent *)&xevent)->button )
1283 var_Get( p_vout, "mouse-button-down", &val );
1285 var_Set( p_vout, "mouse-button-down", val );
1287 val.b_bool = VLC_TRUE;
1288 var_Set( p_vout, "mouse-clicked", val );
1293 playlist_t *p_playlist;
1295 var_Get( p_vout, "mouse-button-down", &val );
1297 var_Set( p_vout, "mouse-button-down", val );
1299 p_playlist = vlc_object_find( p_vout,
1300 VLC_OBJECT_PLAYLIST,
1302 if( p_playlist != NULL )
1305 var_Get( p_playlist, "intf-show", &val );
1306 val.b_bool = !val.b_bool;
1307 var_Set( p_playlist, "intf-show", val );
1308 vlc_object_release( p_playlist );
1315 intf_thread_t *p_intf;
1316 playlist_t *p_playlist;
1318 var_Get( p_vout, "mouse-button-down", &val );
1320 var_Set( p_vout, "mouse-button-down", val );
1321 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
1325 p_intf->b_menu_change = 1;
1326 vlc_object_release( p_intf );
1329 p_playlist = vlc_object_find( p_vout,
1330 VLC_OBJECT_PLAYLIST,
1332 if( p_playlist != NULL )
1334 vlc_value_t val; val.b_bool = VLC_TRUE;
1335 var_Set( p_playlist, "intf-popupmenu", val );
1336 vlc_object_release( p_playlist );
1342 var_Get( p_vout, "mouse-button-down", &val );
1344 var_Set( p_vout, "mouse-button-down", val );
1348 var_Get( p_vout, "mouse-button-down", &val );
1350 var_Set( p_vout, "mouse-button-down", val );
1356 else if( xevent.type == MotionNotify )
1358 unsigned int i_width, i_height, i_x, i_y;
1361 /* somewhat different use for vout_PlacePicture:
1362 * here the values are needed to give to mouse coordinates
1363 * in the original picture space */
1364 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
1365 p_vout->p_sys->p_win->i_height,
1366 &i_x, &i_y, &i_width, &i_height );
1368 val.i_int = ( xevent.xmotion.x - i_x ) *
1369 p_vout->fmt_in.i_visible_width / i_width +
1370 p_vout->fmt_in.i_x_offset;
1371 var_Set( p_vout, "mouse-x", val );
1372 val.i_int = ( xevent.xmotion.y - i_y ) *
1373 p_vout->fmt_in.i_visible_height / i_height +
1374 p_vout->fmt_in.i_y_offset;
1375 var_Set( p_vout, "mouse-y", val );
1377 val.b_bool = VLC_TRUE;
1378 var_Set( p_vout, "mouse-moved", val );
1380 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1381 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1383 ToggleCursor( p_vout );
1386 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1387 || xevent.type == MapNotify
1388 || xevent.type == UnmapNotify )
1390 /* Ignore these events */
1392 else /* Other events */
1394 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1398 /* Handle events for video output sub-window */
1399 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1400 p_vout->p_sys->p_win->video_window,
1401 ExposureMask, &xevent ) == True )
1403 /* Window exposed (only handled if stream playback is paused) */
1404 if( xevent.type == Expose )
1406 if( ((XExposeEvent *)&xevent)->count == 0 )
1408 /* (if this is the last a collection of expose events...) */
1410 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1412 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1413 ->stream.control.i_status )
1415 /* XVideoDisplay( p_vout )*/;
1423 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1424 * are handled - according to the man pages, the format is always 32
1426 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1427 ClientMessage, &xevent ) )
1429 if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
1430 && ((Atom)xevent.xclient.data.l[0]
1431 == p_vout->p_sys->p_win->wm_delete_window ) )
1433 /* the user wants to close the window */
1434 playlist_t * p_playlist =
1435 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1437 if( p_playlist != NULL )
1439 playlist_Stop( p_playlist );
1440 vlc_object_release( p_playlist );
1448 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1452 /* Update the object variable and trigger callback */
1453 val.b_bool = !p_vout->b_fullscreen;
1456 * FIXME FIXME FIXME FIXME: EXPLICIT HACK.
1457 * On the one hand, we cannot hold the lock while triggering a
1458 * callback, as it causes a deadlock with video-on-top handling.
1459 * On the other hand, we have to lock while triggering the
1461 * 1/ make sure video-on-top remains in sync with fullscreen
1462 * (i.e. unlocking creates a race condition if fullscreen is
1463 * switched on and off VERY FAST).
1464 * 2/ avoid possible corruption bugs if another thread gets the
1465 * mutex and modifies our data in-between.
1467 * This is obviously contradictory. Correct solutions may include:
1468 * - putting the fullscreen NAND video-on-top logic out of libvlc,
1469 * back into the video output plugins (ugly code duplication...),
1470 * - serializing fullscreen and video-on-top handling properly
1471 * instead of doing it via the fullscreen callback. That's got to
1472 * be the correct one.
1474 #ifdef MODULE_NAME_IS_xvmc
1475 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1477 vlc_mutex_unlock( &p_vout->p_sys->lock );
1479 var_Set( p_vout, "fullscreen", val );
1481 vlc_mutex_lock( &p_vout->p_sys->lock );
1482 #ifdef MODULE_NAME_IS_xvmc
1483 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
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 =
1622 (Window)vout_RequestWindow( p_vout, &p_win->i_x, &p_win->i_y,
1623 &p_win->i_width, &p_win->i_height );
1625 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1626 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1627 xsize_hints.flags = PSize | PMinSize;
1629 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1631 xsize_hints.x = p_win->i_x;
1632 xsize_hints.y = p_win->i_y;
1633 xsize_hints.flags |= PPosition;
1638 /* Fullscreen window size and position */
1639 p_win->owner_window = 0;
1640 p_win->i_x = p_win->i_y = 0;
1642 DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1644 DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1647 if( !p_win->owner_window )
1649 /* Create the window and set hints - the window must receive
1650 * ConfigureNotify events, and until it is displayed, Expose and
1651 * MapNotify events. */
1653 p_win->base_window =
1654 XCreateWindow( p_vout->p_sys->p_display,
1655 DefaultRootWindow( p_vout->p_sys->p_display ),
1656 p_win->i_x, p_win->i_y,
1657 p_win->i_width, p_win->i_height,
1660 CWBackingStore | CWBackPixel | CWEventMask,
1661 &xwindow_attributes );
1663 var_Get( p_vout, "video-title", &val );
1664 if( !val.psz_string || !*val.psz_string )
1666 XStoreName( p_vout->p_sys->p_display, p_win->base_window,
1667 #ifdef MODULE_NAME_IS_x11
1668 VOUT_TITLE " (X11 output)"
1669 #elif defined(MODULE_NAME_IS_glx)
1670 VOUT_TITLE " (GLX output)"
1672 VOUT_TITLE " (XVideo output)"
1678 XStoreName( p_vout->p_sys->p_display,
1679 p_win->base_window, val.psz_string );
1681 if( val.psz_string ) free( val.psz_string );
1683 if( !p_vout->b_fullscreen )
1685 /* Set window manager hints and properties: size hints, command,
1686 * window's name, and accepted protocols */
1687 XSetWMNormalHints( p_vout->p_sys->p_display,
1688 p_win->base_window, &xsize_hints );
1689 XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
1690 p_vout->p_libvlc->ppsz_argv, p_vout->p_libvlc->i_argc );
1692 if( !var_GetBool( p_vout, "video-deco") )
1695 mwmhints_t mwmhints;
1697 mwmhints.flags = MWM_HINTS_DECORATIONS;
1698 mwmhints.decorations = False;
1700 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1703 XChangeProperty( p_vout->p_sys->p_display,
1705 prop, prop, 32, PropModeReplace,
1706 (unsigned char *)&mwmhints,
1707 PROP_MWM_HINTS_ELEMENTS );
1715 unsigned int dummy4, dummy5;
1717 /* Select events we are interested in. */
1718 XSelectInput( p_vout->p_sys->p_display, p_win->owner_window,
1719 StructureNotifyMask );
1721 /* Get the parent window's geometry information */
1722 XGetGeometry( p_vout->p_sys->p_display, p_win->owner_window,
1723 &dummy1, &dummy2, &dummy3,
1728 /* We are already configured */
1729 b_configure_notify = VLC_TRUE;
1731 /* From man XSelectInput: only one client at a time can select a
1732 * ButtonPress event, so we need to open a new window anyway. */
1733 p_win->base_window =
1734 XCreateWindow( p_vout->p_sys->p_display,
1735 p_win->owner_window,
1737 p_win->i_width, p_win->i_height,
1739 0, CopyFromParent, 0,
1740 CWBackingStore | CWBackPixel | CWEventMask,
1741 &xwindow_attributes );
1744 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1745 || (p_win->wm_delete_window == None)
1746 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1747 &p_win->wm_delete_window, 1 ) )
1749 /* WM_DELETE_WINDOW is not supported by window manager */
1750 msg_Warn( p_vout, "missing or bad window manager" );
1753 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1754 * event when using functions like XCopyArea */
1755 xgcvalues.graphics_exposures = False;
1756 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1758 GCGraphicsExposures, &xgcvalues );
1760 /* Send orders to server, and wait until window is displayed - three
1761 * events must be received: a MapNotify event, an Expose event allowing
1762 * drawing in the window, and a ConfigureNotify to get the window
1763 * dimensions. Once those events have been received, only
1764 * ConfigureNotify events need to be received. */
1765 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1768 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1769 SubstructureNotifyMask | StructureNotifyMask |
1770 ExposureMask, &xevent);
1771 if( (xevent.type == Expose)
1772 && (xevent.xexpose.window == p_win->base_window) )
1774 b_expose = VLC_TRUE;
1775 /* ConfigureNotify isn't sent if there isn't a window manager.
1776 * Expose should be the last event to be received so it should
1777 * be fine to assume we won't receive it anymore. */
1778 b_configure_notify = VLC_TRUE;
1780 else if( (xevent.type == MapNotify)
1781 && (xevent.xmap.window == p_win->base_window) )
1783 b_map_notify = VLC_TRUE;
1785 else if( (xevent.type == ConfigureNotify)
1786 && (xevent.xconfigure.window == p_win->base_window) )
1788 b_configure_notify = VLC_TRUE;
1789 p_win->i_width = xevent.xconfigure.width;
1790 p_win->i_height = xevent.xconfigure.height;
1792 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1794 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1795 StructureNotifyMask | KeyPressMask |
1796 ButtonPressMask | ButtonReleaseMask |
1797 PointerMotionMask );
1799 #ifdef MODULE_NAME_IS_x11
1800 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1802 /* Allocate a new palette */
1803 p_vout->p_sys->colormap =
1804 XCreateColormap( p_vout->p_sys->p_display,
1805 DefaultRootWindow( p_vout->p_sys->p_display ),
1806 DefaultVisual( p_vout->p_sys->p_display,
1807 p_vout->p_sys->i_screen ),
1810 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1811 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1812 CWColormap, &xwindow_attributes );
1816 /* Create video output sub-window. */
1817 p_win->video_window = XCreateSimpleWindow(
1818 p_vout->p_sys->p_display,
1819 p_win->base_window, 0, 0,
1820 p_win->i_width, p_win->i_height,
1822 BlackPixel( p_vout->p_sys->p_display,
1823 p_vout->p_sys->i_screen ),
1824 WhitePixel( p_vout->p_sys->p_display,
1825 p_vout->p_sys->i_screen ) );
1827 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1828 BlackPixel( p_vout->p_sys->p_display,
1829 p_vout->p_sys->i_screen ) );
1831 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1832 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1835 /* make sure the video window will be centered in the next ManageVideo() */
1836 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1838 /* If the cursor was formerly blank than blank it again */
1839 if( !p_vout->p_sys->b_mouse_pointer_visible )
1841 ToggleCursor( p_vout );
1842 ToggleCursor( p_vout );
1845 /* Do NOT use XFlush here ! */
1846 XSync( p_vout->p_sys->p_display, False );
1848 /* At this stage, the window is open, displayed, and ready to
1850 p_vout->p_sys->p_win = p_win;
1855 /*****************************************************************************
1856 * DestroyWindow: destroy the window
1857 *****************************************************************************
1859 *****************************************************************************/
1860 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1862 /* Do NOT use XFlush here ! */
1863 XSync( p_vout->p_sys->p_display, False );
1865 if( p_win->video_window != None )
1866 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1868 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1870 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1871 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1873 if( p_win->owner_window )
1874 vout_ReleaseWindow( p_vout, (void *)p_win->owner_window );
1877 /*****************************************************************************
1878 * NewPicture: allocate a picture
1879 *****************************************************************************
1880 * Returns 0 on success, -1 otherwise
1881 *****************************************************************************/
1882 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1884 #ifndef MODULE_NAME_IS_glx
1886 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1890 /* We know the chroma, allocate a buffer which will be used
1891 * directly by the decoder */
1892 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1894 if( p_pic->p_sys == NULL )
1899 #ifdef MODULE_NAME_IS_xvmc
1900 p_pic->p_sys->p_vout = p_vout;
1901 p_pic->p_sys->xvmc_surf = NULL;
1902 p_pic->p_sys->xxmc_data.decoded = 0;
1903 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1904 p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1905 p_pic->p_sys->nb_display = 0;
1908 /* Fill in picture_t fields */
1909 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
1910 p_vout->output.i_width, p_vout->output.i_height,
1911 p_vout->output.i_aspect );
1913 #ifdef HAVE_SYS_SHM_H
1914 if( p_vout->p_sys->b_shm )
1916 /* Create image using XShm extension */
1917 p_pic->p_sys->p_image =
1918 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1919 # if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1920 p_vout->p_sys->i_xvport,
1921 VLC2X11_FOURCC(p_vout->output.i_chroma),
1923 p_vout->p_sys->p_visual,
1924 p_vout->p_sys->i_screen_depth,
1926 &p_pic->p_sys->shminfo,
1927 p_vout->output.i_width, p_vout->output.i_height );
1930 if( !p_vout->p_sys->b_shm || !p_pic->p_sys->p_image )
1931 #endif /* HAVE_SYS_SHM_H */
1933 /* Create image without XShm extension */
1934 p_pic->p_sys->p_image =
1935 CreateImage( p_vout, p_vout->p_sys->p_display,
1936 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1937 p_vout->p_sys->i_xvport,
1938 VLC2X11_FOURCC(p_vout->output.i_chroma),
1939 p_pic->format.i_bits_per_pixel,
1941 p_vout->p_sys->p_visual,
1942 p_vout->p_sys->i_screen_depth,
1943 p_vout->p_sys->i_bytes_per_pixel,
1945 p_vout->output.i_width, p_vout->output.i_height );
1947 #ifdef HAVE_SYS_SHM_H
1948 if( p_pic->p_sys->p_image && p_vout->p_sys->b_shm )
1950 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1951 p_vout->p_sys->b_shm = VLC_FALSE;
1953 #endif /* HAVE_SYS_SHM_H */
1956 if( p_pic->p_sys->p_image == NULL )
1958 free( p_pic->p_sys );
1962 switch( p_vout->output.i_chroma )
1964 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1965 case VLC_FOURCC('I','4','2','0'):
1966 case VLC_FOURCC('Y','V','1','2'):
1967 case VLC_FOURCC('Y','2','1','1'):
1968 case VLC_FOURCC('Y','U','Y','2'):
1969 case VLC_FOURCC('U','Y','V','Y'):
1970 case VLC_FOURCC('R','V','1','5'):
1971 case VLC_FOURCC('R','V','1','6'):
1972 case VLC_FOURCC('R','V','2','4'): /* Fixme: pixel pitch == 4 ? */
1973 case VLC_FOURCC('R','V','3','2'):
1975 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1978 p_pic->p[i_plane].p_pixels = p_pic->p_sys->p_image->data
1979 + p_pic->p_sys->p_image->offsets[i_plane];
1980 p_pic->p[i_plane].i_pitch =
1981 p_pic->p_sys->p_image->pitches[i_plane];
1983 if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
1985 /* U and V inverted compared to I420
1986 * Fixme: this should be handled by the vout core */
1987 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1988 + p_pic->p_sys->p_image->offsets[2];
1989 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1990 + p_pic->p_sys->p_image->offsets[1];
1995 case VLC_FOURCC('R','G','B','2'):
1996 case VLC_FOURCC('R','V','1','6'):
1997 case VLC_FOURCC('R','V','1','5'):
1998 case VLC_FOURCC('R','V','2','4'):
1999 case VLC_FOURCC('R','V','3','2'):
2001 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
2002 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
2003 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
2004 + p_pic->p_sys->p_image->xoffset;
2005 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
2007 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
2008 * properly by vout_InitPicture() */
2009 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
2010 * p_pic->p_sys->p_image->width;
2015 /* Unknown chroma, tell the guy to get lost */
2016 IMAGE_FREE( p_pic->p_sys->p_image );
2017 free( p_pic->p_sys );
2018 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
2019 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
2020 p_pic->i_planes = 0;
2024 #endif /* !MODULE_NAME_IS_glx */
2029 /*****************************************************************************
2030 * FreePicture: destroy a picture allocated with NewPicture
2031 *****************************************************************************
2032 * Destroy XImage AND associated data. If using Shm, detach shared memory
2033 * segment from server and process, then free it. The XDestroyImage manpage
2034 * says that both the image structure _and_ the data pointed to by the
2035 * image structure are freed, so no need to free p_image->data.
2036 *****************************************************************************/
2037 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
2039 /* The order of operations is correct */
2040 #ifdef HAVE_SYS_SHM_H
2041 if( p_vout->p_sys->b_shm )
2043 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
2044 IMAGE_FREE( p_pic->p_sys->p_image );
2046 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
2047 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
2049 msg_Err( p_vout, "cannot detach shared memory (%s)",
2056 IMAGE_FREE( p_pic->p_sys->p_image );
2059 #ifdef MODULE_NAME_IS_xvmc
2060 if( p_pic->p_sys->xvmc_surf != NULL )
2062 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
2063 p_pic->p_sys->xvmc_surf = NULL;
2067 /* Do NOT use XFlush here ! */
2068 XSync( p_vout->p_sys->p_display, False );
2070 free( p_pic->p_sys );
2073 /*****************************************************************************
2074 * ToggleFullScreen: Enable or disable full screen mode
2075 *****************************************************************************
2076 * This function will switch between fullscreen and window mode.
2077 *****************************************************************************/
2078 static void ToggleFullScreen ( vout_thread_t *p_vout )
2082 mwmhints_t mwmhints;
2083 XSetWindowAttributes attributes;
2085 #ifdef HAVE_XINERAMA
2089 p_vout->b_fullscreen = !p_vout->b_fullscreen;
2091 if( p_vout->b_fullscreen )
2093 msg_Dbg( p_vout, "entering fullscreen mode" );
2095 p_vout->p_sys->b_altfullscreen =
2096 config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
2098 XUnmapWindow( p_vout->p_sys->p_display,
2099 p_vout->p_sys->p_win->base_window );
2101 p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
2103 CreateWindow( p_vout, p_vout->p_sys->p_win );
2104 XDestroyWindow( p_vout->p_sys->p_display,
2105 p_vout->p_sys->fullscreen_window.video_window );
2106 XReparentWindow( p_vout->p_sys->p_display,
2107 p_vout->p_sys->original_window.video_window,
2108 p_vout->p_sys->fullscreen_window.base_window, 0, 0 );
2109 p_vout->p_sys->fullscreen_window.video_window =
2110 p_vout->p_sys->original_window.video_window;
2112 /* To my knowledge there are two ways to create a borderless window.
2113 * There's the generic way which is to tell x to bypass the window
2114 * manager, but this creates problems with the focus of other
2116 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
2117 * luckily seems to be supported by most window managers. */
2118 if( !p_vout->p_sys->b_altfullscreen )
2120 mwmhints.flags = MWM_HINTS_DECORATIONS;
2121 mwmhints.decorations = False;
2123 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
2125 XChangeProperty( p_vout->p_sys->p_display,
2126 p_vout->p_sys->p_win->base_window,
2127 prop, prop, 32, PropModeReplace,
2128 (unsigned char *)&mwmhints,
2129 PROP_MWM_HINTS_ELEMENTS );
2133 /* brute force way to remove decorations */
2134 attributes.override_redirect = True;
2135 XChangeWindowAttributes( p_vout->p_sys->p_display,
2136 p_vout->p_sys->p_win->base_window,
2140 /* Make sure the change is effective */
2141 XReparentWindow( p_vout->p_sys->p_display,
2142 p_vout->p_sys->p_win->base_window,
2143 DefaultRootWindow( p_vout->p_sys->p_display ),
2147 if( p_vout->p_sys->b_net_wm_state_fullscreen )
2149 XClientMessageEvent event;
2151 memset( &event, 0, sizeof( XClientMessageEvent ) );
2153 event.type = ClientMessage;
2154 event.message_type = p_vout->p_sys->net_wm_state;
2155 event.display = p_vout->p_sys->p_display;
2156 event.window = p_vout->p_sys->p_win->base_window;
2158 event.data.l[ 0 ] = 1; /* set property */
2159 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_fullscreen;
2161 XSendEvent( p_vout->p_sys->p_display,
2162 DefaultRootWindow( p_vout->p_sys->p_display ),
2163 False, SubstructureRedirectMask,
2167 /* Make sure the change is effective */
2168 XReparentWindow( p_vout->p_sys->p_display,
2169 p_vout->p_sys->p_win->base_window,
2170 DefaultRootWindow( p_vout->p_sys->p_display ),
2173 #ifdef HAVE_XINERAMA
2174 if( XineramaQueryExtension( p_vout->p_sys->p_display, &i_d1, &i_d2 ) &&
2175 XineramaIsActive( p_vout->p_sys->p_display ) )
2177 XineramaScreenInfo *screens; /* infos for xinerama */
2180 msg_Dbg( p_vout, "using XFree Xinerama extension");
2182 #define SCREEN p_vout->p_sys->p_win->i_screen
2184 /* Get Information about Xinerama (num of screens) */
2185 screens = XineramaQueryScreens( p_vout->p_sys->p_display,
2188 SCREEN = config_GetInt( p_vout,
2189 MODULE_STRING "-xineramascreen" );
2191 /* just check that user has entered a good value */
2192 if( SCREEN >= i_num_screens || SCREEN < 0 )
2194 msg_Dbg( p_vout, "requested screen number invalid (%d/%d)", SCREEN, i_num_screens );
2198 /* Get the X/Y upper left corner coordinate of the above screen */
2199 p_vout->p_sys->p_win->i_x = screens[SCREEN].x_org;
2200 p_vout->p_sys->p_win->i_y = screens[SCREEN].y_org;
2202 /* Set the Height/width to the screen resolution */
2203 p_vout->p_sys->p_win->i_width = screens[SCREEN].width;
2204 p_vout->p_sys->p_win->i_height = screens[SCREEN].height;
2214 /* The window wasn't necessarily created at the requested size */
2215 p_vout->p_sys->p_win->i_x = p_vout->p_sys->p_win->i_y = 0;
2217 #ifdef HAVE_XF86VIDMODE
2218 XF86VidModeModeLine mode;
2221 if( XF86VidModeGetModeLine( p_vout->p_sys->p_display,
2222 p_vout->p_sys->i_screen, &i_dummy,
2225 p_vout->p_sys->p_win->i_width = mode.hdisplay;
2226 p_vout->p_sys->p_win->i_height = mode.vdisplay;
2228 /* move cursor to the middle of the window to prevent
2229 * unwanted display move if the display is smaller than the
2231 XWarpPointer( p_vout->p_sys->p_display, None,
2232 p_vout->p_sys->p_win->base_window, 0, 0, 0, 0,
2233 mode.hdisplay / 2 , mode.vdisplay / 2 );
2234 /* force desktop view to upper left corner */
2235 XF86VidModeSetViewPort( p_vout->p_sys->p_display,
2236 p_vout->p_sys->i_screen, 0, 0 );
2241 p_vout->p_sys->p_win->i_width =
2242 DisplayWidth( p_vout->p_sys->p_display,
2243 p_vout->p_sys->i_screen );
2244 p_vout->p_sys->p_win->i_height =
2245 DisplayHeight( p_vout->p_sys->p_display,
2246 p_vout->p_sys->i_screen );
2251 XMoveResizeWindow( p_vout->p_sys->p_display,
2252 p_vout->p_sys->p_win->base_window,
2253 p_vout->p_sys->p_win->i_x,
2254 p_vout->p_sys->p_win->i_y,
2255 p_vout->p_sys->p_win->i_width,
2256 p_vout->p_sys->p_win->i_height );
2259 EnablePixelDoubling( p_vout );
2265 msg_Dbg( p_vout, "leaving fullscreen mode" );
2268 DisablePixelDoubling( p_vout );
2271 XReparentWindow( p_vout->p_sys->p_display,
2272 p_vout->p_sys->original_window.video_window,
2273 p_vout->p_sys->original_window.base_window, 0, 0 );
2275 p_vout->p_sys->fullscreen_window.video_window = None;
2276 DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
2277 p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
2279 XMapWindow( p_vout->p_sys->p_display,
2280 p_vout->p_sys->p_win->base_window );
2283 /* Unfortunately, using XSync() here is not enough to ensure the
2284 * window has already been mapped because the XMapWindow() request
2285 * has not necessarily been sent directly to our window (remember,
2286 * the call is first redirected to the window manager) */
2289 XWindowEvent( p_vout->p_sys->p_display,
2290 p_vout->p_sys->p_win->base_window,
2291 StructureNotifyMask, &xevent );
2292 } while( xevent.type != MapNotify );
2294 /* Be careful, this can generate a BadMatch error if the window is not
2295 * already mapped by the server (see above) */
2296 XSetInputFocus(p_vout->p_sys->p_display,
2297 p_vout->p_sys->p_win->base_window,
2301 /* signal that the size needs to be updated */
2302 p_vout->i_changes |= VOUT_SIZE_CHANGE;
2305 /*****************************************************************************
2306 * EnableXScreenSaver: enable screen saver
2307 *****************************************************************************
2308 * This function enables the screen saver on a display after it has been
2309 * disabled by XDisableScreenSaver.
2310 * FIXME: what happens if multiple vlc sessions are running at the same
2312 *****************************************************************************/
2313 static void EnableXScreenSaver( vout_thread_t *p_vout )
2315 #ifdef DPMSINFO_IN_DPMS_H
2319 if( p_vout->p_sys->i_ss_timeout )
2321 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2322 p_vout->p_sys->i_ss_interval,
2323 p_vout->p_sys->i_ss_blanking,
2324 p_vout->p_sys->i_ss_exposure );
2327 /* Restore DPMS settings */
2328 #ifdef DPMSINFO_IN_DPMS_H
2329 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2331 if( p_vout->p_sys->b_ss_dpms )
2333 DPMSEnable( p_vout->p_sys->p_display );
2339 /*****************************************************************************
2340 * DisableXScreenSaver: disable screen saver
2341 *****************************************************************************
2342 * See XEnableXScreenSaver
2343 *****************************************************************************/
2344 static void DisableXScreenSaver( vout_thread_t *p_vout )
2346 #ifdef DPMSINFO_IN_DPMS_H
2350 /* Save screen saver information */
2351 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2352 &p_vout->p_sys->i_ss_interval,
2353 &p_vout->p_sys->i_ss_blanking,
2354 &p_vout->p_sys->i_ss_exposure );
2356 /* Disable screen saver */
2357 if( p_vout->p_sys->i_ss_timeout )
2359 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2360 p_vout->p_sys->i_ss_interval,
2361 p_vout->p_sys->i_ss_blanking,
2362 p_vout->p_sys->i_ss_exposure );
2366 #ifdef DPMSINFO_IN_DPMS_H
2367 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2370 /* Save DPMS current state */
2371 DPMSInfo( p_vout->p_sys->p_display, &unused,
2372 &p_vout->p_sys->b_ss_dpms );
2373 DPMSDisable( p_vout->p_sys->p_display );
2378 /*****************************************************************************
2379 * CreateCursor: create a blank mouse pointer
2380 *****************************************************************************/
2381 static void CreateCursor( vout_thread_t *p_vout )
2383 XColor cursor_color;
2385 p_vout->p_sys->cursor_pixmap =
2386 XCreatePixmap( p_vout->p_sys->p_display,
2387 DefaultRootWindow( p_vout->p_sys->p_display ),
2390 XParseColor( p_vout->p_sys->p_display,
2391 XCreateColormap( p_vout->p_sys->p_display,
2393 p_vout->p_sys->p_display ),
2395 p_vout->p_sys->p_display,
2396 p_vout->p_sys->i_screen ),
2398 "black", &cursor_color );
2400 p_vout->p_sys->blank_cursor =
2401 XCreatePixmapCursor( p_vout->p_sys->p_display,
2402 p_vout->p_sys->cursor_pixmap,
2403 p_vout->p_sys->cursor_pixmap,
2404 &cursor_color, &cursor_color, 1, 1 );
2407 /*****************************************************************************
2408 * DestroyCursor: destroy the blank mouse pointer
2409 *****************************************************************************/
2410 static void DestroyCursor( vout_thread_t *p_vout )
2412 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2415 /*****************************************************************************
2416 * ToggleCursor: hide or show the mouse pointer
2417 *****************************************************************************
2418 * This function hides the X pointer if it is visible by setting the pointer
2419 * sprite to a blank one. To show it again, we disable the sprite.
2420 *****************************************************************************/
2421 static void ToggleCursor( vout_thread_t *p_vout )
2423 if( p_vout->p_sys->b_mouse_pointer_visible )
2425 XDefineCursor( p_vout->p_sys->p_display,
2426 p_vout->p_sys->p_win->base_window,
2427 p_vout->p_sys->blank_cursor );
2428 p_vout->p_sys->b_mouse_pointer_visible = 0;
2432 XUndefineCursor( p_vout->p_sys->p_display,
2433 p_vout->p_sys->p_win->base_window );
2434 p_vout->p_sys->b_mouse_pointer_visible = 1;
2438 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2439 /*****************************************************************************
2440 * XVideoGetPort: get YUV12 port
2441 *****************************************************************************/
2442 static int XVideoGetPort( vout_thread_t *p_vout,
2443 vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
2445 XvAdaptorInfo *p_adaptor;
2447 unsigned int i_adaptor, i_num_adaptors;
2448 int i_requested_adaptor;
2449 int i_selected_port;
2451 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2456 case XvBadExtension:
2457 msg_Warn( p_vout, "XvBadExtension" );
2461 msg_Warn( p_vout, "XvBadAlloc" );
2465 msg_Warn( p_vout, "XvQueryExtension failed" );
2469 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2470 DefaultRootWindow( p_vout->p_sys->p_display ),
2471 &i_num_adaptors, &p_adaptor ) )
2476 case XvBadExtension:
2477 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2481 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2485 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2489 i_selected_port = -1;
2490 #ifdef MODULE_NAME_IS_xvmc
2491 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2493 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2495 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2497 XvImageFormatValues *p_formats;
2498 int i_format, i_num_formats;
2501 /* If we requested an adaptor and it's not this one, we aren't
2503 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2508 /* If the adaptor doesn't have the required properties, skip it */
2509 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2510 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2515 /* Check that adaptor supports our requested format... */
2516 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2517 p_adaptor[i_adaptor].base_id,
2521 i_format < i_num_formats && ( i_selected_port == -1 );
2524 XvAttribute *p_attr;
2525 int i_attr, i_num_attributes;
2527 /* If this is not the format we want, or at least a
2528 * similar one, forget it */
2529 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2534 /* Look for the first available port supporting this format */
2535 for( i_port = p_adaptor[i_adaptor].base_id;
2536 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2537 + p_adaptor[i_adaptor].num_ports) )
2538 && ( i_selected_port == -1 );
2541 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2544 i_selected_port = i_port;
2545 *pi_newchroma = p_formats[ i_format ].id;
2549 /* If no free port was found, forget it */
2550 if( i_selected_port == -1 )
2555 /* If we found a port, print information about it */
2556 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2557 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2558 (char *)&p_formats[ i_format ].id,
2559 ( p_formats[ i_format ].format == XvPacked ) ?
2560 "packed" : "planar" );
2562 /* Make sure XV_AUTOPAINT_COLORKEY is set */
2563 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2565 &i_num_attributes );
2567 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2569 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2571 const Atom autopaint =
2572 XInternAtom( p_vout->p_sys->p_display,
2573 "XV_AUTOPAINT_COLORKEY", False );
2574 XvSetPortAttribute( p_vout->p_sys->p_display,
2575 i_selected_port, autopaint, 1 );
2580 if( p_attr != NULL )
2586 if( p_formats != NULL )
2593 if( i_num_adaptors > 0 )
2595 XvFreeAdaptorInfo( p_adaptor );
2598 if( i_selected_port == -1 )
2600 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2601 if( i_requested_adaptor == -1 )
2603 msg_Warn( p_vout, "no free XVideo port found for format "
2604 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2608 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2609 "XVideo port for format 0x%.8x (%4.4s)",
2610 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2614 return i_selected_port;
2617 /*****************************************************************************
2618 * XVideoReleasePort: release YUV12 port
2619 *****************************************************************************/
2620 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2622 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2626 /*****************************************************************************
2627 * InitDisplay: open and initialize X11 device
2628 *****************************************************************************
2629 * Create a window according to video output given size, and set other
2630 * properties according to the display properties.
2631 *****************************************************************************/
2632 static int InitDisplay( vout_thread_t *p_vout )
2634 #ifdef MODULE_NAME_IS_x11
2635 XPixmapFormatValues * p_formats; /* pixmap formats */
2636 XVisualInfo * p_xvisual; /* visuals information */
2637 XVisualInfo xvisual_template; /* visual template */
2638 int i_count, i; /* array size */
2641 #ifdef HAVE_SYS_SHM_H
2642 p_vout->p_sys->b_shm = 0;
2644 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
2647 /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
2649 p_vout->p_sys->b_shm =
2650 ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
2653 if( !p_vout->p_sys->b_shm )
2655 msg_Warn( p_vout, "XShm video extension is unavailable" );
2660 msg_Dbg( p_vout, "disabling XShm video extension" );
2664 msg_Warn( p_vout, "XShm video extension is unavailable" );
2668 #ifdef MODULE_NAME_IS_xvideo
2669 /* XXX The brightness and contrast values should be read from environment
2670 * XXX variables... */
2672 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2673 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2677 #ifdef MODULE_NAME_IS_x11
2678 /* Initialize structure */
2679 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2681 /* Get screen depth */
2682 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2683 p_vout->p_sys->i_screen );
2684 switch( p_vout->p_sys->i_screen_depth )
2688 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2690 xvisual_template.screen = p_vout->p_sys->i_screen;
2691 xvisual_template.class = DirectColor;
2692 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2693 VisualScreenMask | VisualClassMask,
2694 &xvisual_template, &i_count );
2695 if( p_xvisual == NULL )
2697 msg_Err( p_vout, "no PseudoColor visual available" );
2698 return VLC_EGENERIC;
2700 p_vout->p_sys->i_bytes_per_pixel = 1;
2701 p_vout->output.pf_setpalette = SetPalette;
2708 * Screen depth is higher than 8bpp. TrueColor visual is used.
2710 xvisual_template.screen = p_vout->p_sys->i_screen;
2711 xvisual_template.class = TrueColor;
2712 /* In some cases, we get a truecolor class adaptor that has a different
2713 color depth. So try to get a real true color one first */
2714 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2716 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2717 VisualScreenMask | VisualClassMask |
2719 &xvisual_template, &i_count );
2720 if( p_xvisual == NULL )
2722 msg_Warn( p_vout, "No screen matching the required color depth" );
2723 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2724 VisualScreenMask | VisualClassMask,
2725 &xvisual_template, &i_count );
2726 if( p_xvisual == NULL )
2729 msg_Err( p_vout, "no TrueColor visual available" );
2730 return VLC_EGENERIC;
2734 p_vout->output.i_rmask = p_xvisual->red_mask;
2735 p_vout->output.i_gmask = p_xvisual->green_mask;
2736 p_vout->output.i_bmask = p_xvisual->blue_mask;
2738 /* There is no difference yet between 3 and 4 Bpp. The only way
2739 * to find the actual number of bytes per pixel is to list supported
2740 * pixmap formats. */
2741 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2742 p_vout->p_sys->i_bytes_per_pixel = 0;
2744 for( i = 0; i < i_count; i++ )
2746 /* Under XFree4.0, the list contains pixmap formats available
2747 * through all video depths ; so we have to check against current
2749 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2751 if( p_formats[i].bits_per_pixel / 8
2752 > (int)p_vout->p_sys->i_bytes_per_pixel )
2754 p_vout->p_sys->i_bytes_per_pixel =
2755 p_formats[i].bits_per_pixel / 8;
2759 if( p_formats ) XFree( p_formats );
2763 p_vout->p_sys->p_visual = p_xvisual->visual;
2770 #ifdef HAVE_SYS_SHM_H
2771 /*****************************************************************************
2772 * CreateShmImage: create an XImage or XvImage using shared memory extension
2773 *****************************************************************************
2774 * Prepare an XImage or XvImage for display function.
2775 * The order of the operations respects the recommandations of the mit-shm
2776 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2777 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2778 *****************************************************************************/
2779 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2780 Display* p_display, EXTRA_ARGS_SHM,
2781 int i_width, int i_height )
2783 IMAGE_TYPE *p_image;
2786 /* Create XImage / XvImage */
2787 #ifdef MODULE_NAME_IS_xvideo
2789 /* Make sure the buffer is aligned to multiple of 16 */
2790 i_height = ( i_height + 15 ) >> 4 << 4;
2791 i_width = ( i_width + 15 ) >> 4 << 4;
2793 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2794 i_width, i_height, p_shm );
2795 #elif defined(MODULE_NAME_IS_xvmc)
2796 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2797 i_width, i_height, p_shm );
2799 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2800 p_shm, i_width, i_height );
2802 if( p_image == NULL )
2804 msg_Err( p_vout, "image creation failed" );
2808 /* Allocate shared memory segment - 0776 set the access permission
2809 * rights (like umask), they are not yet supported by all X servers */
2810 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2811 if( p_shm->shmid < 0 )
2813 msg_Err( p_vout, "cannot allocate shared image data (%s)",
2814 strerror( errno ) );
2815 IMAGE_FREE( p_image );
2819 /* Attach shared memory segment to process (read/write) */
2820 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2821 if(! p_shm->shmaddr )
2823 msg_Err( p_vout, "cannot attach shared memory (%s)",
2825 IMAGE_FREE( p_image );
2826 shmctl( p_shm->shmid, IPC_RMID, 0 );
2830 /* Read-only data. We won't be using XShmGetImage */
2831 p_shm->readOnly = True;
2833 /* Attach shared memory segment to X server */
2834 XSynchronize( p_display, True );
2836 result = XShmAttach( p_display, p_shm );
2837 if( result == False || !b_shm )
2839 msg_Err( p_vout, "cannot attach shared memory to X server" );
2840 IMAGE_FREE( p_image );
2841 shmctl( p_shm->shmid, IPC_RMID, 0 );
2842 shmdt( p_shm->shmaddr );
2845 XSynchronize( p_display, False );
2847 /* Send image to X server. This instruction is required, since having
2848 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2849 * and remember NOT to use XFlush ! */
2850 XSync( p_display, False );
2853 /* Mark the shm segment to be removed when there are no more
2854 * attachements, so it is automatic on process exit or after shmdt */
2855 shmctl( p_shm->shmid, IPC_RMID, 0 );
2862 /*****************************************************************************
2863 * CreateImage: create an XImage or XvImage
2864 *****************************************************************************
2865 * Create a simple image used as a buffer.
2866 *****************************************************************************/
2867 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2868 Display *p_display, EXTRA_ARGS,
2869 int i_width, int i_height )
2871 byte_t * p_data; /* image data storage zone */
2872 IMAGE_TYPE *p_image;
2873 #ifdef MODULE_NAME_IS_x11
2874 int i_quantum; /* XImage quantum (see below) */
2875 int i_bytes_per_line;
2878 /* Allocate memory for image */
2879 #ifdef MODULE_NAME_IS_xvideo
2881 /* Make sure the buffer is aligned to multiple of 16 */
2882 i_height = ( i_height + 15 ) >> 4 << 4;
2883 i_width = ( i_width + 15 ) >> 4 << 4;
2885 p_data = (byte_t *) malloc( i_width * i_height * i_bits_per_pixel / 8 );
2886 #elif defined(MODULE_NAME_IS_x11)
2887 i_bytes_per_line = i_width * i_bytes_per_pixel;
2888 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2892 msg_Err( p_vout, "out of memory" );
2896 #ifdef MODULE_NAME_IS_x11
2897 /* Optimize the quantum of a scanline regarding its size - the quantum is
2898 a diviser of the number of bits between the start of two scanlines. */
2899 if( i_bytes_per_line & 0xf )
2903 else if( i_bytes_per_line & 0x10 )
2913 /* Create XImage. p_data will be automatically freed */
2914 #ifdef MODULE_NAME_IS_xvideo
2915 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2916 (char *)p_data, i_width, i_height );
2917 #elif defined(MODULE_NAME_IS_x11)
2918 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2919 (char *)p_data, i_width, i_height, i_quantum, 0 );
2921 if( p_image == NULL )
2923 msg_Err( p_vout, "XCreateImage() failed" );
2931 /*****************************************************************************
2932 * X11ErrorHandler: replace error handler so we can intercept some of them
2933 *****************************************************************************/
2934 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2936 switch( event->request_code )
2938 case X_SetInputFocus:
2939 /* Ignore errors on XSetInputFocus()
2940 * (they happen when a window is not yet mapped) */
2943 case 150: /* MIT-SHM */
2944 case 146: /* MIT-SHM too, what gives? */
2945 if( event->minor_code == X_ShmAttach )
2953 XSetErrorHandler(NULL);
2954 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2957 #ifdef MODULE_NAME_IS_x11
2958 /*****************************************************************************
2959 * SetPalette: sets an 8 bpp palette
2960 *****************************************************************************
2961 * This function sets the palette given as an argument. It does not return
2962 * anything, but could later send information on which colors it was unable
2964 *****************************************************************************/
2965 static void SetPalette( vout_thread_t *p_vout,
2966 uint16_t *red, uint16_t *green, uint16_t *blue )
2969 XColor p_colors[255];
2971 /* allocate palette */
2972 for( i = 0; i < 255; i++ )
2974 /* kludge: colors are indexed reversely because color 255 seems
2975 * to be reserved for black even if we try to set it to white */
2976 p_colors[ i ].pixel = 255 - i;
2977 p_colors[ i ].pad = 0;
2978 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2979 p_colors[ i ].red = red[ 255 - i ];
2980 p_colors[ i ].blue = blue[ 255 - i ];
2981 p_colors[ i ].green = green[ 255 - i ];
2984 XStoreColors( p_vout->p_sys->p_display,
2985 p_vout->p_sys->colormap, p_colors, 255 );
2989 /*****************************************************************************
2990 * Control: control facility for the vout
2991 *****************************************************************************/
2992 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2995 unsigned int i_width, i_height;
2996 unsigned int *pi_width, *pi_height;
3002 if( p_vout->p_sys->p_win->owner_window )
3003 return vout_ControlWindow( p_vout,
3004 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3006 pi_width = va_arg( args, unsigned int * );
3007 pi_height = va_arg( args, unsigned int * );
3009 vlc_mutex_lock( &p_vout->p_sys->lock );
3010 *pi_width = p_vout->p_sys->p_win->i_width;
3011 *pi_height = p_vout->p_sys->p_win->i_height;
3012 vlc_mutex_unlock( &p_vout->p_sys->lock );
3016 if( p_vout->p_sys->p_win->owner_window )
3017 return vout_ControlWindow( p_vout,
3018 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3020 vlc_mutex_lock( &p_vout->p_sys->lock );
3022 i_width = va_arg( args, unsigned int );
3023 i_height = va_arg( args, unsigned int );
3024 if( !i_width ) i_width = p_vout->i_window_width;
3025 if( !i_height ) i_height = p_vout->i_window_height;
3027 #ifdef MODULE_NAME_IS_xvmc
3028 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3030 /* Update dimensions */
3031 XResizeWindow( p_vout->p_sys->p_display,
3032 p_vout->p_sys->p_win->base_window,
3033 i_width, i_height );
3034 #ifdef MODULE_NAME_IS_xvmc
3035 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3037 vlc_mutex_unlock( &p_vout->p_sys->lock );
3041 vlc_mutex_lock( &p_vout->p_sys->lock );
3042 XUnmapWindow( p_vout->p_sys->p_display,
3043 p_vout->p_sys->original_window.base_window );
3044 vlc_mutex_unlock( &p_vout->p_sys->lock );
3048 vlc_mutex_lock( &p_vout->p_sys->lock );
3049 if( i_query == VOUT_REPARENT ) d = (Drawable)va_arg( args, int );
3051 #ifdef MODULE_NAME_IS_xvmc
3052 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3054 XReparentWindow( p_vout->p_sys->p_display,
3055 p_vout->p_sys->original_window.base_window,
3056 DefaultRootWindow( p_vout->p_sys->p_display ),
3059 XReparentWindow( p_vout->p_sys->p_display,
3060 p_vout->p_sys->original_window.base_window,
3062 XSync( p_vout->p_sys->p_display, False );
3063 p_vout->p_sys->original_window.owner_window = 0;
3064 #ifdef MODULE_NAME_IS_xvmc
3065 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3067 vlc_mutex_unlock( &p_vout->p_sys->lock );
3068 return vout_vaControlDefault( p_vout, i_query, args );
3070 case VOUT_SET_STAY_ON_TOP:
3071 if( p_vout->p_sys->p_win->owner_window )
3072 return vout_ControlWindow( p_vout,
3073 (void *)p_vout->p_sys->p_win->owner_window, i_query, args);
3075 b_arg = va_arg( args, vlc_bool_t );
3076 vlc_mutex_lock( &p_vout->p_sys->lock );
3077 #ifdef MODULE_NAME_IS_xvmc
3078 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
3080 WindowOnTop( p_vout, b_arg );
3081 #ifdef MODULE_NAME_IS_xvmc
3082 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
3084 vlc_mutex_unlock( &p_vout->p_sys->lock );
3088 return vout_vaControlDefault( p_vout, i_query, args );
3092 /*****************************************************************************
3093 * TestNetWMSupport: tests for Extended Window Manager Hints support
3094 *****************************************************************************/
3095 static void TestNetWMSupport( vout_thread_t *p_vout )
3097 int i_ret, i_format;
3098 unsigned long i, i_items, i_bytesafter;
3099 Atom net_wm_supported;
3100 union { Atom *p_atom; unsigned char *p_char; } p_args;
3102 p_args.p_atom = NULL;
3104 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_FALSE;
3105 p_vout->p_sys->b_net_wm_state_above = VLC_FALSE;
3106 p_vout->p_sys->b_net_wm_state_below = VLC_FALSE;
3107 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_FALSE;
3110 XInternAtom( p_vout->p_sys->p_display, "_NET_SUPPORTED", False );
3112 i_ret = XGetWindowProperty( p_vout->p_sys->p_display,
3113 DefaultRootWindow( p_vout->p_sys->p_display ),
3115 0, 16384, False, AnyPropertyType,
3117 &i_format, &i_items, &i_bytesafter,
3118 (unsigned char **)&p_args );
3120 if( i_ret != Success || i_items == 0 ) return;
3122 msg_Dbg( p_vout, "Window manager supports NetWM" );
3124 p_vout->p_sys->net_wm_state =
3125 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE", False );
3126 p_vout->p_sys->net_wm_state_fullscreen =
3127 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_FULLSCREEN",
3129 p_vout->p_sys->net_wm_state_above =
3130 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_ABOVE", False );
3131 p_vout->p_sys->net_wm_state_below =
3132 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_BELOW", False );
3133 p_vout->p_sys->net_wm_state_stays_on_top =
3134 XInternAtom( p_vout->p_sys->p_display, "_NET_WM_STATE_STAYS_ON_TOP",
3137 for( i = 0; i < i_items; i++ )
3139 if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_fullscreen )
3142 "Window manager supports _NET_WM_STATE_FULLSCREEN" );
3143 p_vout->p_sys->b_net_wm_state_fullscreen = VLC_TRUE;
3145 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_above )
3147 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_ABOVE" );
3148 p_vout->p_sys->b_net_wm_state_above = VLC_TRUE;
3150 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_below )
3152 msg_Dbg( p_vout, "Window manager supports _NET_WM_STATE_BELOW" );
3153 p_vout->p_sys->b_net_wm_state_below = VLC_TRUE;
3155 else if( p_args.p_atom[i] == p_vout->p_sys->net_wm_state_stays_on_top )
3158 "Window manager supports _NET_WM_STATE_STAYS_ON_TOP" );
3159 p_vout->p_sys->b_net_wm_state_stays_on_top = VLC_TRUE;
3163 XFree( p_args.p_atom );
3166 /*****************************************************************************
3167 * Key events handling
3168 *****************************************************************************/
3174 } x11keys_to_vlckeys[] =
3176 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
3177 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
3178 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
3179 { XK_F12, KEY_F12 },
3181 { XK_Return, KEY_ENTER },
3182 { XK_KP_Enter, KEY_ENTER },
3183 { XK_space, KEY_SPACE },
3184 { XK_Escape, KEY_ESC },
3186 { XK_Menu, KEY_MENU },
3187 { XK_Left, KEY_LEFT },
3188 { XK_Right, KEY_RIGHT },
3190 { XK_Down, KEY_DOWN },
3192 { XK_Home, KEY_HOME },
3193 { XK_End, KEY_END },
3194 { XK_Page_Up, KEY_PAGEUP },
3195 { XK_Page_Down, KEY_PAGEDOWN },
3197 { XK_Insert, KEY_INSERT },
3198 { XK_Delete, KEY_DELETE },
3199 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
3200 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
3201 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
3202 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
3203 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
3204 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
3205 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
3210 static int ConvertKey( int i_key )
3214 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
3216 if( x11keys_to_vlckeys[i].i_x11key == i_key )
3218 return x11keys_to_vlckeys[i].i_vlckey;
3225 /*****************************************************************************
3226 * WindowOnTop: Switches the "always on top" state of the video window.
3227 *****************************************************************************/
3228 static int WindowOnTop( vout_thread_t *p_vout, vlc_bool_t b_on_top )
3230 if( p_vout->p_sys->b_net_wm_state_stays_on_top )
3232 XClientMessageEvent event;
3234 memset( &event, 0, sizeof( XClientMessageEvent ) );
3236 event.type = ClientMessage;
3237 event.message_type = p_vout->p_sys->net_wm_state;
3238 event.display = p_vout->p_sys->p_display;
3239 event.window = p_vout->p_sys->p_win->base_window;
3241 event.data.l[ 0 ] = b_on_top; /* set property */
3242 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_stays_on_top;
3244 XSendEvent( p_vout->p_sys->p_display,
3245 DefaultRootWindow( p_vout->p_sys->p_display ),
3246 False, SubstructureRedirectMask,
3250 /* use _NET_WM_STATE_ABOVE if window manager
3251 * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
3252 else if( p_vout->p_sys->b_net_wm_state_above )
3254 XClientMessageEvent event;
3256 memset( &event, 0, sizeof( XClientMessageEvent ) );
3258 event.type = ClientMessage;
3259 event.message_type = p_vout->p_sys->net_wm_state;
3260 event.display = p_vout->p_sys->p_display;
3261 event.window = p_vout->p_sys->p_win->base_window;
3263 event.data.l[ 0 ] = b_on_top; /* set property */
3264 event.data.l[ 1 ] = p_vout->p_sys->net_wm_state_above;
3266 XSendEvent( p_vout->p_sys->p_display,
3267 DefaultRootWindow( p_vout->p_sys->p_display ),
3268 False, SubstructureRedirectMask,