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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
38 #include <vlc_vout_window.h>
41 #include <errno.h> /* ENOMEM */
43 #ifdef HAVE_MACHINE_PARAM_H
45 # include <machine/param.h>
46 # include <sys/types.h> /* typedef ushort */
51 # include <netinet/in.h> /* BSD: struct in_addr */
55 #include <X11/extensions/Xsp.h>
59 # include <sys/shm.h> /* shmget(), shmctl() */
63 #include <X11/Xproto.h>
65 #include <X11/Xutil.h>
66 #include <X11/keysym.h>
67 #include <X11/XF86keysym.h>
69 # include <X11/extensions/XShm.h>
71 #ifdef DPMSINFO_IN_DPMS_H
72 # include <X11/extensions/dpms.h>
75 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
76 # include <X11/extensions/Xv.h>
77 # include <X11/extensions/Xvlib.h>
80 #ifdef MODULE_NAME_IS_glx
84 #ifdef MODULE_NAME_IS_xvmc
85 # include <X11/extensions/vldXvMC.h>
86 # include "../../codec/xvmc/accel_xvmc.h"
91 /*****************************************************************************
93 *****************************************************************************/
94 int Activate ( vlc_object_t * );
95 void Deactivate ( vlc_object_t * );
97 static int InitVideo ( vout_thread_t * );
98 static void EndVideo ( vout_thread_t * );
99 static void DisplayVideo ( vout_thread_t *, picture_t * );
100 static int ManageVideo ( vout_thread_t * );
101 static int Control ( vout_thread_t *, int, va_list );
103 static int InitDisplay ( vout_thread_t * );
105 static int CreateWindow ( vout_thread_t *, x11_window_t * );
106 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
108 static int NewPicture ( vout_thread_t *, picture_t * );
109 static void FreePicture ( vout_thread_t *, picture_t * );
111 #ifdef HAVE_SYS_SHM_H
112 static int i_shm_major = 0;
115 static void ToggleFullScreen ( vout_thread_t * );
117 static void EnableXScreenSaver ( vout_thread_t * );
118 static void DisableXScreenSaver ( vout_thread_t * );
120 static void CreateCursor ( vout_thread_t * );
121 static void DestroyCursor ( vout_thread_t * );
122 static void ToggleCursor ( vout_thread_t * );
124 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
125 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, picture_heap_t * );
126 static void XVideoReleasePort( vout_thread_t *, int );
129 #ifdef MODULE_NAME_IS_x11
130 static void SetPalette ( vout_thread_t *,
131 uint16_t *, uint16_t *, uint16_t * );
134 #ifdef MODULE_NAME_IS_xvmc
135 static void RenderVideo ( vout_thread_t *, picture_t * );
136 static int xvmc_check_yv12( Display *display, XvPortID port );
137 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
140 static int ConvertKey( int );
142 static int X11ErrorHandler( Display *, XErrorEvent * );
145 static void EnablePixelDoubling( vout_thread_t *p_vout );
146 static void DisablePixelDoubling( vout_thread_t *p_vout );
150 static const int i_backlight_on_interval = 300;
155 /*****************************************************************************
156 * Activate: allocate X11 video thread output method
157 *****************************************************************************
158 * This function allocate and initialize a X11 vout method. It uses some of the
159 * vout properties to choose the window size, and change them according to the
160 * actual properties of the display.
161 *****************************************************************************/
162 int Activate ( vlc_object_t *p_this )
164 vout_thread_t *p_vout = (vout_thread_t *)p_this;
166 #if defined(MODULE_NAME_IS_xvmc)
169 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
171 vlc_fourcc_t i_chroma = 0;
175 p_vout->pf_init = InitVideo;
176 p_vout->pf_end = EndVideo;
177 p_vout->pf_manage = ManageVideo;
178 #ifdef MODULE_NAME_IS_xvmc
179 p_vout->pf_render = RenderVideo;
181 p_vout->pf_render = NULL;
183 p_vout->pf_display = DisplayVideo;
184 p_vout->pf_control = Control;
186 /* Allocate structure */
187 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
188 if( p_vout->p_sys == NULL )
191 /* key and mouse event handling */
192 p_vout->p_sys->i_vout_event = var_CreateGetInteger( p_vout, "vout-event" );
194 /* Open display, using the "display" config variable or the DISPLAY
195 * environment variable */
196 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
198 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
200 if( p_vout->p_sys->p_display == NULL ) /* error */
202 msg_Err( p_vout, "cannot open display %s",
203 XDisplayName( psz_display ) );
204 free( p_vout->p_sys );
210 /* Replace error handler so we can intercept some non-fatal errors */
211 XSetErrorHandler( X11ErrorHandler );
213 /* Get a screen ID matching the XOpenDisplay return value */
214 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
216 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
217 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
220 if( strlen( psz_chroma ) >= 4 )
222 /* Do not use direct assignment because we are not sure of the
224 memcpy(&i_chroma, psz_chroma, 4);
233 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
234 i_chroma, (char*)&i_chroma );
238 i_chroma = p_vout->render.i_chroma;
241 /* Check that we have access to an XVideo port providing this chroma */
242 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
244 if( p_vout->p_sys->i_xvport < 0 )
246 /* If a specific chroma format was requested, then we don't try to
247 * be cleverer than the user. He knew pretty well what he wanted. */
250 XCloseDisplay( p_vout->p_sys->p_display );
251 free( p_vout->p_sys );
255 /* It failed, but it's not completely lost ! We try to open an
256 * XVideo port for an YUY2 picture. We'll need to do an YUV
257 * conversion, but at least it has got scaling. */
258 p_vout->p_sys->i_xvport =
259 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
261 if( p_vout->p_sys->i_xvport < 0 )
263 /* It failed, but it's not completely lost ! We try to open an
264 * XVideo port for a simple 16bpp RGB picture. We'll need to do
265 * an YUV conversion, but at least it has got scaling. */
266 p_vout->p_sys->i_xvport =
267 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
269 if( p_vout->p_sys->i_xvport < 0 )
271 XCloseDisplay( p_vout->p_sys->p_display );
272 free( p_vout->p_sys );
277 p_vout->output.i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, X112VLC_FOURCC(p_vout->output.i_chroma) );
278 #elif defined(MODULE_NAME_IS_glx)
280 int i_opcode, i_evt, i_err = 0;
281 int i_maj, i_min = 0;
283 /* Check for GLX extension */
284 if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
285 &i_opcode, &i_evt, &i_err ) )
287 msg_Err( p_this, "GLX extension not supported" );
288 XCloseDisplay( p_vout->p_sys->p_display );
289 free( p_vout->p_sys );
292 if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
294 msg_Err( p_this, "glXQueryExtension failed" );
295 XCloseDisplay( p_vout->p_sys->p_display );
296 free( p_vout->p_sys );
300 /* Check GLX version */
301 if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
303 msg_Err( p_this, "glXQueryVersion failed" );
304 XCloseDisplay( p_vout->p_sys->p_display );
305 free( p_vout->p_sys );
308 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
310 p_vout->p_sys->b_glx13 = false;
311 msg_Dbg( p_this, "using GLX 1.2 API" );
315 p_vout->p_sys->b_glx13 = true;
316 msg_Dbg( p_this, "using GLX 1.3 API" );
321 /* Create blank cursor (for mouse cursor autohiding) */
322 p_vout->p_sys->i_time_mouse_last_moved = mdate();
323 p_vout->p_sys->i_mouse_hide_timeout =
324 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
325 p_vout->p_sys->b_mouse_pointer_visible = 1;
326 CreateCursor( p_vout );
328 /* Set main window's size */
329 p_vout->p_sys->window.i_x = 0;
330 p_vout->p_sys->window.i_y = 0;
331 p_vout->p_sys->window.i_width = p_vout->i_window_width;
332 p_vout->p_sys->window.i_height = p_vout->i_window_height;
333 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
334 /* Spawn base window - this window will include the video output window,
335 * but also command buttons, subtitles and other indicators */
336 if( CreateWindow( p_vout, &p_vout->p_sys->window ) )
338 msg_Err( p_vout, "cannot create X11 window" );
339 DestroyCursor( p_vout );
340 XCloseDisplay( p_vout->p_sys->p_display );
341 free( p_vout->p_sys );
345 /* Open and initialize device. */
346 if( InitDisplay( p_vout ) )
348 msg_Err( p_vout, "cannot initialize X11 display" );
349 DestroyCursor( p_vout );
350 DestroyWindow( p_vout, &p_vout->p_sys->window );
351 XCloseDisplay( p_vout->p_sys->p_display );
352 free( p_vout->p_sys );
356 /* Disable screen saver */
357 DisableXScreenSaver( p_vout );
360 p_vout->p_sys->b_altfullscreen = 0;
361 p_vout->p_sys->i_time_button_last_pressed = 0;
363 #ifdef MODULE_NAME_IS_xvmc
364 p_vout->p_sys->p_last_subtitle_save = NULL;
365 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
367 /* Look what method was requested */
368 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
369 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
372 if( (strcmp(psz_value, "bob") == 0) ||
373 (strcmp(psz_value, "blend") == 0) )
374 p_vout->p_sys->xvmc_deinterlace_method = 2;
375 else if (strcmp(psz_value, "discard") == 0)
376 p_vout->p_sys->xvmc_deinterlace_method = 1;
378 p_vout->p_sys->xvmc_deinterlace_method = 0;
382 p_vout->p_sys->xvmc_deinterlace_method = 0;
384 /* Look what method was requested */
385 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
386 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
387 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
391 if( strncmp( psz_value, "eq", 2 ) == 0 )
392 p_vout->p_sys->xvmc_crop_style = 1;
393 else if( strncmp( psz_value, "4-16", 4 ) == 0)
394 p_vout->p_sys->xvmc_crop_style = 2;
395 else if( strncmp( psz_value, "16-4", 4 ) == 0)
396 p_vout->p_sys->xvmc_crop_style = 3;
398 p_vout->p_sys->xvmc_crop_style = 0;
402 p_vout->p_sys->xvmc_crop_style = 0;
404 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
405 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
407 if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
409 msg_Err( p_vout, "no XVMC capability found" );
410 Deactivate( p_this );
413 subpicture_t sub_pic;
414 sub_pic.p_sys = NULL;
415 p_vout->p_sys->last_date = 0;
419 p_vout->p_sys->i_hw_scale = 1;
423 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
424 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
425 if ( p_vout->p_sys->p_octx == NULL ) {
426 msg_Err( p_vout, "Could not get osso context" );
428 msg_Dbg( p_vout, "Initialized osso context" );
432 /* Variable to indicate if the window should be on top of others */
433 /* Trigger a callback right now */
434 var_TriggerCallback( p_vout, "video-on-top" );
439 /*****************************************************************************
440 * Deactivate: destroy X11 video thread output method
441 *****************************************************************************
442 * Terminate an output method created by Open
443 *****************************************************************************/
444 void Deactivate ( vlc_object_t *p_this )
446 vout_thread_t *p_vout = (vout_thread_t *)p_this;
448 /* Restore cursor if it was blanked */
449 if( !p_vout->p_sys->b_mouse_pointer_visible )
451 ToggleCursor( p_vout );
454 #ifdef MODULE_NAME_IS_x11
455 /* Destroy colormap */
456 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
458 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
460 #elif defined(MODULE_NAME_IS_xvideo)
461 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
462 #elif defined(MODULE_NAME_IS_xvmc)
463 if( p_vout->p_sys->xvmc_cap )
465 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
466 xxmc_dispose_context( p_vout );
467 if( p_vout->p_sys->old_subpic )
469 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
470 p_vout->p_sys->old_subpic = NULL;
472 if( p_vout->p_sys->new_subpic )
474 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
475 p_vout->p_sys->new_subpic = NULL;
477 free( p_vout->p_sys->xvmc_cap );
478 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
483 DisablePixelDoubling(p_vout);
486 DestroyCursor( p_vout );
487 EnableXScreenSaver( p_vout );
488 DestroyWindow( p_vout, &p_vout->p_sys->window );
489 XCloseDisplay( p_vout->p_sys->p_display );
491 /* Destroy structure */
492 #ifdef MODULE_NAME_IS_xvmc
493 free_context_lock( &p_vout->p_sys->xvmc_lock );
497 if ( p_vout->p_sys->p_octx != NULL ) {
498 msg_Dbg( p_vout, "Deinitializing osso context" );
499 osso_deinitialize( p_vout->p_sys->p_octx );
503 free( p_vout->p_sys );
506 #ifdef MODULE_NAME_IS_xvmc
508 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
511 static int xvmc_check_yv12( Display *display, XvPortID port )
513 XvImageFormatValues *formatValues;
517 formatValues = XvListImageFormats( display, port, &formats );
519 for( i = 0; i < formats; i++ )
521 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
522 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
524 XFree (formatValues);
529 XFree (formatValues);
533 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
535 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
538 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
541 int xv_double_buffer;
543 xv_double_buffer = 1;
545 XLockDisplay( p_vout->p_sys->p_display );
546 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
548 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
550 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
551 XUnlockDisplay( p_vout->p_sys->p_display );
553 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
554 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
557 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
559 vlc_xxmc_t *xxmc = NULL;
561 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
563 xxmc = &p_pic->p_sys->xxmc_data;
564 if( (!xxmc->decoded ||
565 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
567 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
572 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
573 if (p_vout->p_sys->p_last_subtitle != NULL)
575 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
577 p_vout->p_sys->new_subpic =
578 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
579 p_vout->p_sys->xvmc_width,
580 p_vout->p_sys->xvmc_height,
581 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
583 if (p_vout->p_sys->new_subpic)
585 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
586 XvMCClearSubpicture( p_vout->p_sys->p_display,
587 p_vout->p_sys->new_subpic,
590 p_vout->p_sys->xvmc_width,
591 p_vout->p_sys->xvmc_height,
593 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
594 clear_xx44_palette( &p_vout->p_sys->palette );
596 if( sub_pic.p_sys == NULL )
598 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
599 if( sub_pic.p_sys != NULL )
601 sub_pic.p_sys->p_vout = p_vout;
602 sub_pic.p_sys->xvmc_surf = NULL;
603 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
606 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
607 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
608 sub_pic.p->i_pitch = p_vout->output.i_width;
610 memset( p_vout->p_sys->subImage->data, 0,
611 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
613 if (p_vout->p_last_subtitle != NULL)
615 blend_xx44( p_vout->p_sys->subImage->data,
616 p_vout->p_last_subtitle,
617 p_vout->p_sys->subImage->width,
618 p_vout->p_sys->subImage->height,
619 p_vout->p_sys->subImage->width,
620 &p_vout->p_sys->palette,
621 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
624 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
625 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
626 p_vout->p_sys->new_subpic,
627 p_vout->p_sys->subImage,
630 p_vout->output.i_width, /* overlay->width, */
631 p_vout->output.i_height, /* overlay->height */
634 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
635 if (p_vout->p_sys->old_subpic)
637 xxmc_xvmc_free_subpicture( p_vout,
638 p_vout->p_sys->old_subpic);
639 p_vout->p_sys->old_subpic = NULL;
641 if (p_vout->p_sys->new_subpic)
643 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
644 p_vout->p_sys->new_subpic = NULL;
645 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
646 p_vout->p_sys->xvmc_palette,
648 p_vout->p_sys->old_subpic->num_palette_entries,
649 p_vout->p_sys->old_subpic->entry_bytes,
650 p_vout->p_sys->old_subpic->component_order );
651 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
652 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
653 p_vout->p_sys->old_subpic,
654 p_vout->p_sys->xvmc_palette );
655 XvMCFlushSubpicture( p_vout->p_sys->p_display,
656 p_vout->p_sys->old_subpic);
657 XvMCSyncSubpicture( p_vout->p_sys->p_display,
658 p_vout->p_sys->old_subpic );
659 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
662 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
663 if (p_vout->p_sys->xvmc_backend_subpic )
665 XvMCBlendSubpicture( p_vout->p_sys->p_display,
666 p_pic->p_sys->xvmc_surf,
667 p_vout->p_sys->old_subpic,
670 p_vout->p_sys->xvmc_width,
671 p_vout->p_sys->xvmc_height,
674 p_vout->p_sys->xvmc_width,
675 p_vout->p_sys->xvmc_height );
679 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
680 p_pic->p_sys->xvmc_surf,
681 p_pic->p_sys->xvmc_surf,
682 p_vout->p_sys->old_subpic,
685 p_vout->p_sys->xvmc_width,
686 p_vout->p_sys->xvmc_height,
689 p_vout->p_sys->xvmc_width,
690 p_vout->p_sys->xvmc_height );
692 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
697 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
698 if( p_vout->p_sys->xvmc_backend_subpic )
700 XvMCBlendSubpicture( p_vout->p_sys->p_display,
701 p_pic->p_sys->xvmc_surf,
702 p_vout->p_sys->old_subpic,
704 p_vout->p_sys->xvmc_width,
705 p_vout->p_sys->xvmc_height,
707 p_vout->p_sys->xvmc_width,
708 p_vout->p_sys->xvmc_height );
712 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
713 p_pic->p_sys->xvmc_surf,
714 p_pic->p_sys->xvmc_surf,
715 p_vout->p_sys->old_subpic,
717 p_vout->p_sys->xvmc_width,
718 p_vout->p_sys->xvmc_height,
720 p_vout->p_sys->xvmc_width,
721 p_vout->p_sys->xvmc_height );
723 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
726 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
728 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
730 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
735 /*****************************************************************************
736 * EnablePixelDoubling: Enables pixel doubling
737 *****************************************************************************
738 * Checks if the double size image fits in current window, and enables pixel
739 * doubling accordingly. The i_hw_scale is the integer scaling factor.
740 *****************************************************************************/
741 static void EnablePixelDoubling( vout_thread_t *p_vout )
743 int i_hor_scale = ( p_vout->p_sys->window.i_width ) / p_vout->render.i_width;
744 int i_vert_scale = ( p_vout->p_sys->window.i_height ) / p_vout->render.i_height;
745 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
746 p_vout->p_sys->i_hw_scale = 2;
747 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
748 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
752 /*****************************************************************************
753 * DisablePixelDoubling: Disables pixel doubling
754 *****************************************************************************
755 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
756 *****************************************************************************/
757 static void DisablePixelDoubling( vout_thread_t *p_vout )
759 if ( p_vout->p_sys->i_hw_scale > 1 ) {
760 msg_Dbg( p_vout, "Disabling pixel doubling" );
761 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
762 p_vout->p_sys->i_hw_scale = 1;
769 /*****************************************************************************
770 * InitVideo: initialize X11 video thread output method
771 *****************************************************************************
772 * This function create the XImages needed by the output thread. It is called
773 * at the beginning of the thread, but also each time the window is resized.
774 *****************************************************************************/
775 static int InitVideo( vout_thread_t *p_vout )
777 unsigned int i_index = 0;
780 I_OUTPUTPICTURES = 0;
782 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
783 /* Initialize the output structure; we already found an XVideo port,
784 * and the corresponding chroma we will be using. Since we can
785 * arbitrary scale, stick to the coordinates and aspect. */
786 p_vout->output.i_width = p_vout->render.i_width;
787 p_vout->output.i_height = p_vout->render.i_height;
788 p_vout->output.i_aspect = p_vout->render.i_aspect;
790 p_vout->fmt_out = p_vout->fmt_in;
791 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
793 #if XvVersion < 2 || ( XvVersion == 2 && XvRevision < 2 )
794 switch( p_vout->output.i_chroma )
796 case VLC_CODEC_RGB16:
797 #if defined( WORDS_BIGENDIAN )
798 p_vout->output.i_rmask = 0xf800;
799 p_vout->output.i_gmask = 0x07e0;
800 p_vout->output.i_bmask = 0x001f;
802 p_vout->output.i_rmask = 0x001f;
803 p_vout->output.i_gmask = 0x07e0;
804 p_vout->output.i_bmask = 0xf800;
807 case VLC_CODEC_RGB15:
808 #if defined( WORDS_BIGENDIAN )
809 p_vout->output.i_rmask = 0x7c00;
810 p_vout->output.i_gmask = 0x03e0;
811 p_vout->output.i_bmask = 0x001f;
813 p_vout->output.i_rmask = 0x001f;
814 p_vout->output.i_gmask = 0x03e0;
815 p_vout->output.i_bmask = 0x7c00;
821 #elif defined(MODULE_NAME_IS_x11)
822 /* Initialize the output structure: RGB with square pixels, whatever
823 * the input format is, since it's the only format we know */
824 switch( p_vout->p_sys->i_screen_depth )
826 case 8: /* FIXME: set the palette */
827 p_vout->output.i_chroma = VLC_CODEC_RGB8; break;
829 p_vout->output.i_chroma = VLC_CODEC_RGB15; break;
831 p_vout->output.i_chroma = VLC_CODEC_RGB16; break;
834 p_vout->output.i_chroma = VLC_CODEC_RGB32; break;
836 msg_Err( p_vout, "unknown screen depth %i",
837 p_vout->p_sys->i_screen_depth );
842 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width / p_vout->p_sys->i_hw_scale,
843 p_vout->p_sys->window.i_height / p_vout->p_sys->i_hw_scale,
845 &p_vout->fmt_out.i_visible_width,
846 &p_vout->fmt_out.i_visible_height );
848 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
849 p_vout->p_sys->window.i_height,
851 &p_vout->fmt_out.i_visible_width,
852 &p_vout->fmt_out.i_visible_height );
855 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
857 p_vout->output.i_width = p_vout->fmt_out.i_width =
858 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
859 p_vout->fmt_in.i_visible_width;
860 p_vout->output.i_height = p_vout->fmt_out.i_height =
861 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
862 p_vout->fmt_in.i_visible_height;
863 p_vout->fmt_out.i_x_offset =
864 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
865 p_vout->fmt_in.i_visible_width;
866 p_vout->fmt_out.i_y_offset =
867 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
868 p_vout->fmt_in.i_visible_height;
870 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
871 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
872 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
874 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
875 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
876 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
877 p_vout->fmt_out.i_visible_width,
878 p_vout->fmt_out.i_visible_height );
881 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
882 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
886 /* Find an empty picture slot */
887 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
889 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
891 p_pic = p_vout->p_picture + i_index;
896 /* Allocate the picture */
897 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
902 p_pic->i_status = DESTROYED_PICTURE;
903 p_pic->i_type = DIRECT_PICTURE;
905 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
910 if( p_vout->output.i_chroma == VLC_CODEC_YV12 )
912 /* U and V inverted compared to I420
913 * Fixme: this should be handled by the vout core */
914 p_vout->output.i_chroma = VLC_CODEC_I420;
915 p_vout->fmt_out.i_chroma = VLC_CODEC_I420;
921 /*****************************************************************************
922 * DisplayVideo: displays previously rendered output
923 *****************************************************************************
924 * This function sends the currently rendered image to X11 server.
925 * (The Xv extension takes care of "double-buffering".)
926 *****************************************************************************/
927 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
929 unsigned int i_width, i_height, i_x, i_y;
931 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
932 p_vout->p_sys->window.i_height,
933 &i_x, &i_y, &i_width, &i_height );
935 #ifdef MODULE_NAME_IS_xvmc
936 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
938 vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
939 if( !xxmc->decoded ||
940 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
942 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
944 xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
945 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
949 int src_width = p_vout->output.i_width;
950 int src_height = p_vout->output.i_height;
953 if( p_vout->p_sys->xvmc_crop_style == 1 )
960 else if( p_vout->p_sys->xvmc_crop_style == 2 )
967 else if( p_vout->p_sys->xvmc_crop_style == 3 )
981 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
982 { /* BOB DEINTERLACE */
983 if( (p_pic->p_sys->nb_display == 0) ||
984 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
986 first_field = (p_pic->b_top_field_first) ?
987 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
991 first_field = (p_pic->b_top_field_first) ?
992 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
997 first_field = XVMC_FRAME_PICTURE;
1000 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1001 XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
1002 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1003 XvMCPutSurface( p_vout->p_sys->p_display,
1004 p_pic->p_sys->xvmc_surf,
1005 p_vout->p_sys->window.video_window,
1016 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1017 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1018 { /* BOB DEINTERLACE */
1019 if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1021 mtime_t last_date = p_pic->date;
1023 vlc_mutex_lock( &p_vout->picture_lock );
1024 if( !p_vout->p_sys->last_date )
1026 p_pic->date += 20000;
1030 p_pic->date = ((3 * p_pic->date -
1031 p_vout->p_sys->last_date) / 2 );
1033 p_vout->p_sys->last_date = last_date;
1035 p_pic->p_sys->nb_display = 1;
1036 vlc_mutex_unlock( &p_vout->picture_lock );
1040 p_pic->p_sys->nb_display = 0;
1044 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1047 #ifdef HAVE_SYS_SHM_H
1048 if( p_vout->p_sys->i_shm_opcode )
1050 /* Display rendered image using shared memory extension */
1051 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1052 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1053 p_vout->p_sys->window.video_window,
1054 p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1055 p_vout->fmt_out.i_x_offset,
1056 p_vout->fmt_out.i_y_offset,
1057 p_vout->fmt_out.i_visible_width,
1058 p_vout->fmt_out.i_visible_height,
1059 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1060 False /* Don't put True here or you'll waste your CPU */ );
1062 XShmPutImage( p_vout->p_sys->p_display,
1063 p_vout->p_sys->window.video_window,
1064 p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1065 p_vout->fmt_out.i_x_offset,
1066 p_vout->fmt_out.i_y_offset,
1067 0 /*dest_x*/, 0 /*dest_y*/,
1068 p_vout->fmt_out.i_visible_width,
1069 p_vout->fmt_out.i_visible_height,
1070 False /* Don't put True here ! */ );
1074 #endif /* HAVE_SYS_SHM_H */
1076 /* Use standard XPutImage -- this is gonna be slow ! */
1077 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1078 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1079 p_vout->p_sys->window.video_window,
1080 p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1081 p_vout->fmt_out.i_x_offset,
1082 p_vout->fmt_out.i_y_offset,
1083 p_vout->fmt_out.i_visible_width,
1084 p_vout->fmt_out.i_visible_height,
1085 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1087 XPutImage( p_vout->p_sys->p_display,
1088 p_vout->p_sys->window.video_window,
1089 p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1090 p_vout->fmt_out.i_x_offset,
1091 p_vout->fmt_out.i_y_offset,
1092 0 /*dest_x*/, 0 /*dest_y*/,
1093 p_vout->fmt_out.i_visible_width,
1094 p_vout->fmt_out.i_visible_height );
1098 /* Make sure the command is sent now - do NOT use XFlush !*/
1099 XSync( p_vout->p_sys->p_display, False );
1102 /*****************************************************************************
1103 * ManageVideo: handle X11 events
1104 *****************************************************************************
1105 * This function should be called regularly by video output thread. It manages
1106 * X11 events and allows window resizing. It returns a non null value on
1108 *****************************************************************************/
1109 static int ManageVideo( vout_thread_t *p_vout )
1111 XEvent xevent; /* X11 event */
1114 #ifdef MODULE_NAME_IS_xvmc
1115 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1118 /* Handle events from the owner window */
1119 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1120 p_vout->p_sys->window.owner_window->handle.xid,
1121 StructureNotifyMask, &xevent ) == True )
1123 /* ConfigureNotify event: prepare */
1124 if( xevent.type == ConfigureNotify )
1125 /* Update dimensions */
1126 XResizeWindow( p_vout->p_sys->p_display,
1127 p_vout->p_sys->window.base_window,
1128 xevent.xconfigure.width,
1129 xevent.xconfigure.height );
1132 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1133 * output window's size changed, MapNotify and UnmapNotify to know if the
1134 * window is mapped (and if the display is useful), and ClientMessages
1135 * to intercept window destruction requests */
1137 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1138 p_vout->p_sys->window.base_window,
1139 StructureNotifyMask | KeyPressMask |
1140 ButtonPressMask | ButtonReleaseMask |
1141 PointerMotionMask | Button1MotionMask , &xevent )
1144 /* ConfigureNotify event: prepare */
1145 if( xevent.type == ConfigureNotify )
1147 if( (unsigned int)xevent.xconfigure.width
1148 != p_vout->p_sys->window.i_width
1149 || (unsigned int)xevent.xconfigure.height
1150 != p_vout->p_sys->window.i_height )
1152 /* Update dimensions */
1153 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1154 p_vout->p_sys->window.i_width = xevent.xconfigure.width;
1155 p_vout->p_sys->window.i_height = xevent.xconfigure.height;
1158 /* Keyboard event */
1159 else if( xevent.type == KeyPress )
1161 unsigned int state = xevent.xkey.state;
1162 KeySym x_key_symbol;
1163 char i_key; /* ISO Latin-1 key */
1165 /* We may have keys like F1 trough F12, ESC ... */
1166 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
1167 xevent.xkey.keycode, 0 );
1168 val.i_int = ConvertKey( (int)x_key_symbol );
1170 xevent.xkey.state &= ~ShiftMask;
1171 xevent.xkey.state &= ~ControlMask;
1172 xevent.xkey.state &= ~Mod1Mask;
1175 XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1178 * The reason why I use this instead of XK_0 is that
1179 * with XLookupString, we don't have to care about
1186 if( state & ShiftMask )
1188 val.i_int |= KEY_MODIFIER_SHIFT;
1190 if( state & ControlMask )
1192 val.i_int |= KEY_MODIFIER_CTRL;
1194 if( state & Mod1Mask )
1196 val.i_int |= KEY_MODIFIER_ALT;
1198 var_Set( p_vout->p_libvlc, "key-pressed", val );
1202 else if( xevent.type == ButtonPress )
1204 switch( ((XButtonEvent *)&xevent)->button )
1207 var_Get( p_vout, "mouse-button-down", &val );
1209 var_Set( p_vout, "mouse-button-down", val );
1211 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", false );
1213 /* detect double-clicks */
1214 if( ( ((XButtonEvent *)&xevent)->time -
1215 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1217 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1220 p_vout->p_sys->i_time_button_last_pressed =
1221 ((XButtonEvent *)&xevent)->time;
1224 var_Get( p_vout, "mouse-button-down", &val );
1226 var_Set( p_vout, "mouse-button-down", val );
1230 var_Get( p_vout, "mouse-button-down", &val );
1232 var_Set( p_vout, "mouse-button-down", val );
1233 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
1237 var_Get( p_vout, "mouse-button-down", &val );
1239 var_Set( p_vout, "mouse-button-down", val );
1243 var_Get( p_vout, "mouse-button-down", &val );
1245 var_Set( p_vout, "mouse-button-down", val );
1250 else if( xevent.type == ButtonRelease )
1252 switch( ((XButtonEvent *)&xevent)->button )
1256 var_Get( p_vout, "mouse-button-down", &val );
1258 var_Set( p_vout, "mouse-button-down", val );
1260 var_SetBool( p_vout, "mouse-clicked", true );
1266 var_Get( p_vout, "mouse-button-down", &val );
1268 var_Set( p_vout, "mouse-button-down", val );
1270 var_Get( p_vout->p_libvlc, "intf-show", &val );
1271 val.b_bool = !val.b_bool;
1272 var_Set( p_vout->p_libvlc, "intf-show", val );
1278 var_Get( p_vout, "mouse-button-down", &val );
1280 var_Set( p_vout, "mouse-button-down", val );
1286 var_Get( p_vout, "mouse-button-down", &val );
1288 var_Set( p_vout, "mouse-button-down", val );
1292 var_Get( p_vout, "mouse-button-down", &val );
1294 var_Set( p_vout, "mouse-button-down", val );
1300 else if( xevent.type == MotionNotify )
1302 unsigned int i_width, i_height, i_x, i_y;
1304 /* somewhat different use for vout_PlacePicture:
1305 * here the values are needed to give to mouse coordinates
1306 * in the original picture space */
1307 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
1308 p_vout->p_sys->window.i_height,
1309 &i_x, &i_y, &i_width, &i_height );
1311 /* Compute the x coordinate and check if the value is
1312 in [0,p_vout->fmt_in.i_visible_width] */
1313 val.i_int = ( xevent.xmotion.x - i_x ) *
1314 p_vout->fmt_in.i_visible_width / i_width +
1315 p_vout->fmt_in.i_x_offset;
1317 if( (int)(xevent.xmotion.x - i_x) < 0 )
1319 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1320 val.i_int = p_vout->fmt_in.i_visible_width;
1322 var_Set( p_vout, "mouse-x", val );
1324 /* compute the y coordinate and check if the value is
1325 in [0,p_vout->fmt_in.i_visible_height] */
1326 val.i_int = ( xevent.xmotion.y - i_y ) *
1327 p_vout->fmt_in.i_visible_height / i_height +
1328 p_vout->fmt_in.i_y_offset;
1330 if( (int)(xevent.xmotion.y - i_y) < 0 )
1332 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1333 val.i_int = p_vout->fmt_in.i_visible_height;
1335 var_Set( p_vout, "mouse-y", val );
1337 var_SetBool( p_vout, "mouse-moved", true );
1339 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1340 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1342 ToggleCursor( p_vout );
1345 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1346 || xevent.type == MapNotify
1347 || xevent.type == UnmapNotify )
1349 /* Ignore these events */
1351 else /* Other events */
1353 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1357 /* Handle events for video output sub-window */
1358 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1359 p_vout->p_sys->window.video_window,
1360 ExposureMask, &xevent ) == True )
1362 /* Window exposed (only handled if stream playback is paused) */
1363 if( xevent.type == Expose )
1365 if( ((XExposeEvent *)&xevent)->count == 0 )
1367 /* (if this is the last a collection of expose events...) */
1369 #if defined(MODULE_NAME_IS_xvideo)
1370 x11_window_t *p_win = &p_vout->p_sys->window;
1372 /* Paint the colour key if needed */
1373 if( p_vout->p_sys->b_paint_colourkey &&
1374 xevent.xexpose.window == p_win->video_window )
1376 XSetForeground( p_vout->p_sys->p_display,
1377 p_win->gc, p_vout->p_sys->i_colourkey );
1378 XFillRectangle( p_vout->p_sys->p_display,
1379 p_win->video_window, p_win->gc, 0, 0,
1380 p_win->i_width, p_win->i_height );
1385 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1387 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1388 ->stream.control.i_status )
1390 /* XVideoDisplay( p_vout )*/;
1398 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1399 * are handled - according to the man pages, the format is always 32
1401 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1402 ClientMessage, &xevent ) )
1404 if( (xevent.xclient.message_type == p_vout->p_sys->window.wm_protocols)
1405 && ((Atom)xevent.xclient.data.l[0]
1406 == p_vout->p_sys->window.wm_delete_window ) )
1408 /* the user wants to close the window */
1409 playlist_t * p_playlist = pl_Hold( p_vout );
1410 if( p_playlist != NULL )
1412 playlist_Stop( p_playlist );
1413 pl_Release( p_vout );
1421 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1423 /* Update the object variable and trigger callback */
1424 var_SetBool( p_vout, "fullscreen", !p_vout->b_fullscreen );
1426 ToggleFullScreen( p_vout );
1427 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1430 /* autoscale toggle */
1431 if( p_vout->i_changes & VOUT_SCALE_CHANGE )
1433 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
1435 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
1436 p_vout->i_zoom = ZOOM_FP_FACTOR;
1438 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1441 /* scaling factor */
1442 if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
1444 p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
1446 p_vout->b_autoscale = false;
1448 (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
1450 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1453 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1454 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1456 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1457 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1459 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1460 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1461 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1462 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1463 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1464 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1465 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1466 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1468 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1474 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1475 * the size flag inside the fullscreen routine)
1477 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1479 unsigned int i_width, i_height, i_x, i_y;
1481 #ifdef MODULE_NAME_IS_x11
1482 /* We need to signal the vout thread about the size change because it
1483 * is doing the rescaling */
1485 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1488 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
1489 p_vout->p_sys->window.i_height,
1490 &i_x, &i_y, &i_width, &i_height );
1492 XMoveResizeWindow( p_vout->p_sys->p_display,
1493 p_vout->p_sys->window.video_window,
1494 i_x, i_y, i_width, i_height );
1497 /* cursor hiding depending on --vout-event option
1499 * value = 1 (Fullsupport) (default value)
1500 * or value = 2 (Fullscreen-Only) and condition met
1502 bool b_vout_event = ( ( p_vout->p_sys->i_vout_event == 1 )
1503 || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1506 /* Autohide Cursour */
1507 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved >
1508 p_vout->p_sys->i_mouse_hide_timeout )
1510 /* Hide the mouse automatically */
1511 if( b_vout_event && p_vout->p_sys->b_mouse_pointer_visible )
1513 ToggleCursor( p_vout );
1517 #ifdef MODULE_NAME_IS_xvmc
1518 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1522 if ( p_vout->p_sys->p_octx != NULL ) {
1523 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1524 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1525 msg_Err( p_vout, "Could not disable backlight blanking" );
1527 msg_Dbg( p_vout, "Backlight blanking disabled" );
1529 p_vout->p_sys->i_backlight_on_counter = 0;
1531 p_vout->p_sys->i_backlight_on_counter ++;
1538 /*****************************************************************************
1539 * EndVideo: terminate X11 video thread output method
1540 *****************************************************************************
1541 * Destroy the X11 XImages created by Init. It is called at the end of
1542 * the thread, but also each time the window is resized.
1543 *****************************************************************************/
1544 static void EndVideo( vout_thread_t *p_vout )
1548 /* Free the direct buffers we allocated */
1549 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1552 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1556 /* following functions are local */
1558 /*****************************************************************************
1559 * CreateWindow: open and set-up X11 main window
1560 *****************************************************************************/
1561 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1563 XSizeHints xsize_hints;
1564 XSetWindowAttributes xwindow_attributes;
1565 XGCValues xgcvalues;
1568 bool b_map_notify = false;
1570 /* Prepare window manager hints and properties */
1571 p_win->wm_protocols =
1572 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1573 p_win->wm_delete_window =
1574 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1576 /* Never have a 0-pixel-wide window */
1577 xsize_hints.min_width = 2;
1578 xsize_hints.min_height = 1;
1580 /* Prepare window attributes */
1581 xwindow_attributes.backing_store = Always; /* save the hidden part */
1582 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1583 p_vout->p_sys->i_screen);
1584 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1587 vout_window_cfg_t wnd_cfg;
1588 memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
1589 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
1590 wnd_cfg.x = p_win->i_x;
1591 wnd_cfg.y = p_win->i_y;
1592 wnd_cfg.width = p_win->i_width;
1593 wnd_cfg.height = p_win->i_height;
1595 p_win->owner_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
1596 if( !p_win->owner_window )
1597 return VLC_EGENERIC;
1598 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1599 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1600 xsize_hints.flags = PSize | PMinSize;
1602 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1604 xsize_hints.x = p_win->i_x;
1605 xsize_hints.y = p_win->i_y;
1606 xsize_hints.flags |= PPosition;
1609 /* Select events we are interested in. */
1610 XSelectInput( p_vout->p_sys->p_display,
1611 p_win->owner_window->handle.xid, StructureNotifyMask );
1613 /* Get the parent window's geometry information */
1614 XGetGeometry( p_vout->p_sys->p_display,
1615 p_win->owner_window->handle.xid,
1616 &(Window){ 0 }, &(int){ 0 }, &(int){ 0 },
1619 &(unsigned){ 0 }, &(unsigned){ 0 } );
1621 /* From man XSelectInput: only one client at a time can select a
1622 * ButtonPress event, so we need to open a new window anyway. */
1623 p_win->base_window =
1624 XCreateWindow( p_vout->p_sys->p_display,
1625 p_win->owner_window->handle.xid,
1627 p_win->i_width, p_win->i_height,
1629 0, CopyFromParent, 0,
1630 CWBackingStore | CWBackPixel | CWEventMask,
1631 &xwindow_attributes );
1634 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1635 || (p_win->wm_delete_window == None)
1636 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1637 &p_win->wm_delete_window, 1 ) )
1639 /* WM_DELETE_WINDOW is not supported by window manager */
1640 msg_Warn( p_vout, "missing or bad window manager" );
1643 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1644 * event when using functions like XCopyArea */
1645 xgcvalues.graphics_exposures = False;
1646 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1648 GCGraphicsExposures, &xgcvalues );
1650 /* Wait till the window is mapped */
1651 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1654 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1655 SubstructureNotifyMask | StructureNotifyMask, &xevent);
1656 if( (xevent.type == MapNotify)
1657 && (xevent.xmap.window == p_win->base_window) )
1659 b_map_notify = true;
1661 else if( (xevent.type == ConfigureNotify)
1662 && (xevent.xconfigure.window == p_win->base_window) )
1664 p_win->i_width = xevent.xconfigure.width;
1665 p_win->i_height = xevent.xconfigure.height;
1667 } while( !b_map_notify );
1669 /* key and mouse events handling depending on --vout-event option
1671 * value = 1 (Fullsupport) (default value)
1672 * or value = 2 (Fullscreen-Only) and condition met
1674 bool b_vout_event = ( ( p_vout->p_sys->i_vout_event == 1 )
1675 || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1678 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1679 StructureNotifyMask | KeyPressMask |
1680 ButtonPressMask | ButtonReleaseMask |
1681 PointerMotionMask );
1683 #ifdef MODULE_NAME_IS_x11
1684 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1686 /* Allocate a new palette */
1687 p_vout->p_sys->colormap =
1688 XCreateColormap( p_vout->p_sys->p_display,
1689 DefaultRootWindow( p_vout->p_sys->p_display ),
1690 DefaultVisual( p_vout->p_sys->p_display,
1691 p_vout->p_sys->i_screen ),
1694 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1695 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1696 CWColormap, &xwindow_attributes );
1700 /* Create video output sub-window. */
1701 p_win->video_window = XCreateSimpleWindow(
1702 p_vout->p_sys->p_display,
1703 p_win->base_window, 0, 0,
1704 p_win->i_width, p_win->i_height,
1706 BlackPixel( p_vout->p_sys->p_display,
1707 p_vout->p_sys->i_screen ),
1708 WhitePixel( p_vout->p_sys->p_display,
1709 p_vout->p_sys->i_screen ) );
1711 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1712 BlackPixel( p_vout->p_sys->p_display,
1713 p_vout->p_sys->i_screen ) );
1715 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1716 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1719 /* make sure the video window will be centered in the next ManageVideo() */
1720 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1722 /* If the cursor was formerly blank than blank it again */
1723 if( !p_vout->p_sys->b_mouse_pointer_visible )
1725 ToggleCursor( p_vout );
1726 ToggleCursor( p_vout );
1729 /* Do NOT use XFlush here ! */
1730 XSync( p_vout->p_sys->p_display, False );
1735 /*****************************************************************************
1736 * DestroyWindow: destroy the window
1737 *****************************************************************************
1739 *****************************************************************************/
1740 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1742 /* Do NOT use XFlush here ! */
1743 XSync( p_vout->p_sys->p_display, False );
1745 if( p_win->video_window != None )
1746 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1748 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1750 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1751 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1753 /* make sure base window is destroyed before proceeding further */
1754 bool b_destroy_notify = false;
1758 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1759 SubstructureNotifyMask | StructureNotifyMask, &xevent);
1760 if( (xevent.type == DestroyNotify)
1761 && (xevent.xmap.window == p_win->base_window) )
1763 b_destroy_notify = true;
1765 } while( !b_destroy_notify );
1767 vout_window_Delete( p_win->owner_window );
1770 /*****************************************************************************
1771 * NewPicture: allocate a picture
1772 *****************************************************************************
1773 * Returns 0 on success, -1 otherwise
1774 *****************************************************************************/
1775 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1777 #ifndef MODULE_NAME_IS_glx
1779 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1783 /* We know the chroma, allocate a buffer which will be used
1784 * directly by the decoder */
1785 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1787 if( p_pic->p_sys == NULL )
1792 #ifdef MODULE_NAME_IS_xvmc
1793 p_pic->p_sys->p_vout = p_vout;
1794 p_pic->p_sys->xvmc_surf = NULL;
1795 p_pic->p_sys->xxmc_data.decoded = 0;
1796 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1797 // p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1798 p_pic->p_sys->nb_display = 0;
1801 /* Fill in picture_t fields */
1802 if( picture_Setup( p_pic, p_vout->output.i_chroma,
1803 p_vout->output.i_width, p_vout->output.i_height,
1804 p_vout->output.i_aspect ) )
1807 #ifdef HAVE_SYS_SHM_H
1808 if( p_vout->p_sys->i_shm_opcode )
1810 /* Create image using XShm extension */
1811 p_pic->p_sys->p_image =
1812 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1813 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1814 p_vout->p_sys->i_xvport,
1815 VLC2X11_FOURCC(p_vout->output.i_chroma),
1817 p_vout->p_sys->p_visual,
1818 p_vout->p_sys->i_screen_depth,
1820 &p_pic->p_sys->shminfo,
1821 p_vout->output.i_width, p_vout->output.i_height );
1824 if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1825 #endif /* HAVE_SYS_SHM_H */
1827 /* Create image without XShm extension */
1828 p_pic->p_sys->p_image =
1829 CreateImage( p_vout, p_vout->p_sys->p_display,
1830 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1831 p_vout->p_sys->i_xvport,
1832 VLC2X11_FOURCC(p_vout->output.i_chroma),
1833 p_pic->format.i_bits_per_pixel,
1835 p_vout->p_sys->p_visual,
1836 p_vout->p_sys->i_screen_depth,
1837 p_vout->p_sys->i_bytes_per_pixel,
1839 p_vout->output.i_width, p_vout->output.i_height );
1841 #ifdef HAVE_SYS_SHM_H
1842 if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1844 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1845 p_vout->p_sys->i_shm_opcode = 0;
1847 #endif /* HAVE_SYS_SHM_H */
1850 if( p_pic->p_sys->p_image == NULL )
1852 free( p_pic->p_sys );
1856 switch( p_vout->output.i_chroma )
1858 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1859 case VLC_CODEC_I420:
1860 case VLC_CODEC_YV12:
1861 case VLC_CODEC_Y211:
1862 case VLC_CODEC_YUYV:
1863 case VLC_CODEC_UYVY:
1864 case VLC_CODEC_RGB15:
1865 case VLC_CODEC_RGB16:
1866 case VLC_CODEC_RGB24: /* Fixme: pixel pitch == 4 ? */
1867 case VLC_CODEC_RGB32:
1869 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1872 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1873 + p_pic->p_sys->p_image->offsets[i_plane];
1874 p_pic->p[i_plane].i_pitch =
1875 p_pic->p_sys->p_image->pitches[i_plane];
1877 if( p_vout->output.i_chroma == VLC_CODEC_YV12 )
1879 /* U and V inverted compared to I420
1880 * Fixme: this should be handled by the vout core */
1881 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1882 + p_pic->p_sys->p_image->offsets[2];
1883 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1884 + p_pic->p_sys->p_image->offsets[1];
1890 case VLC_CODEC_RGB8:
1891 case VLC_CODEC_RGB16:
1892 case VLC_CODEC_RGB15:
1893 case VLC_CODEC_RGB24:
1894 case VLC_CODEC_RGB32:
1896 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1897 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1898 p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1899 + p_pic->p_sys->p_image->xoffset;
1900 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1902 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1903 * properly by picture_Setup() */
1904 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1905 * p_pic->p_sys->p_image->width;
1910 /* Unknown chroma, tell the guy to get lost */
1911 IMAGE_FREE( p_pic->p_sys->p_image );
1912 free( p_pic->p_sys );
1913 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1914 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1915 p_pic->i_planes = 0;
1920 VLC_UNUSED(p_vout); VLC_UNUSED(p_pic);
1922 #endif /* !MODULE_NAME_IS_glx */
1927 /*****************************************************************************
1928 * FreePicture: destroy a picture allocated with NewPicture
1929 *****************************************************************************
1930 * Destroy XImage AND associated data. If using Shm, detach shared memory
1931 * segment from server and process, then free it. The XDestroyImage manpage
1932 * says that both the image structure _and_ the data pointed to by the
1933 * image structure are freed, so no need to free p_image->data.
1934 *****************************************************************************/
1935 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1937 /* The order of operations is correct */
1938 #ifdef HAVE_SYS_SHM_H
1939 if( p_vout->p_sys->i_shm_opcode )
1941 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1942 IMAGE_FREE( p_pic->p_sys->p_image );
1944 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1945 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1947 msg_Err( p_vout, "cannot detach shared memory (%m)" );
1953 IMAGE_FREE( p_pic->p_sys->p_image );
1956 #ifdef MODULE_NAME_IS_xvmc
1957 if( p_pic->p_sys->xvmc_surf != NULL )
1959 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
1960 p_pic->p_sys->xvmc_surf = NULL;
1964 /* Do NOT use XFlush here ! */
1965 XSync( p_vout->p_sys->p_display, False );
1967 free( p_pic->p_sys );
1970 /*****************************************************************************
1971 * ToggleFullScreen: Enable or disable full screen mode
1972 *****************************************************************************
1973 * This function will switch between fullscreen and window mode.
1974 *****************************************************************************/
1975 static void ToggleFullScreen ( vout_thread_t *p_vout )
1977 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1978 vout_window_SetFullScreen( p_vout->p_sys->window.owner_window,
1979 p_vout->b_fullscreen );
1982 if( p_vout->b_fullscreen )
1983 EnablePixelDoubling( p_vout );
1985 DisablePixelDoubling( p_vout );
1989 /*****************************************************************************
1990 * EnableXScreenSaver: enable screen saver
1991 *****************************************************************************
1992 * This function enables the screen saver on a display after it has been
1993 * disabled by XDisableScreenSaver.
1994 * FIXME: what happens if multiple vlc sessions are running at the same
1996 *****************************************************************************/
1997 static void EnableXScreenSaver( vout_thread_t *p_vout )
1999 #ifdef DPMSINFO_IN_DPMS_H
2003 if( p_vout->p_sys->i_ss_timeout )
2005 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
2006 p_vout->p_sys->i_ss_interval,
2007 p_vout->p_sys->i_ss_blanking,
2008 p_vout->p_sys->i_ss_exposure );
2011 /* Restore DPMS settings */
2012 #ifdef DPMSINFO_IN_DPMS_H
2013 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2015 if( p_vout->p_sys->b_ss_dpms )
2017 DPMSEnable( p_vout->p_sys->p_display );
2023 /*****************************************************************************
2024 * DisableXScreenSaver: disable screen saver
2025 *****************************************************************************
2026 * See XEnableXScreenSaver
2027 *****************************************************************************/
2028 static void DisableXScreenSaver( vout_thread_t *p_vout )
2030 #ifdef DPMSINFO_IN_DPMS_H
2034 /* Save screen saver information */
2035 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
2036 &p_vout->p_sys->i_ss_interval,
2037 &p_vout->p_sys->i_ss_blanking,
2038 &p_vout->p_sys->i_ss_exposure );
2040 /* Disable screen saver */
2041 if( p_vout->p_sys->i_ss_timeout )
2043 XSetScreenSaver( p_vout->p_sys->p_display, 0,
2044 p_vout->p_sys->i_ss_interval,
2045 p_vout->p_sys->i_ss_blanking,
2046 p_vout->p_sys->i_ss_exposure );
2050 #ifdef DPMSINFO_IN_DPMS_H
2051 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2054 /* Save DPMS current state */
2055 DPMSInfo( p_vout->p_sys->p_display, &unused,
2056 &p_vout->p_sys->b_ss_dpms );
2057 DPMSDisable( p_vout->p_sys->p_display );
2062 /*****************************************************************************
2063 * CreateCursor: create a blank mouse pointer
2064 *****************************************************************************/
2065 static void CreateCursor( vout_thread_t *p_vout )
2067 XColor cursor_color;
2069 p_vout->p_sys->cursor_pixmap =
2070 XCreatePixmap( p_vout->p_sys->p_display,
2071 DefaultRootWindow( p_vout->p_sys->p_display ),
2074 XParseColor( p_vout->p_sys->p_display,
2075 XCreateColormap( p_vout->p_sys->p_display,
2077 p_vout->p_sys->p_display ),
2079 p_vout->p_sys->p_display,
2080 p_vout->p_sys->i_screen ),
2082 "black", &cursor_color );
2084 p_vout->p_sys->blank_cursor =
2085 XCreatePixmapCursor( p_vout->p_sys->p_display,
2086 p_vout->p_sys->cursor_pixmap,
2087 p_vout->p_sys->cursor_pixmap,
2088 &cursor_color, &cursor_color, 1, 1 );
2091 /*****************************************************************************
2092 * DestroyCursor: destroy the blank mouse pointer
2093 *****************************************************************************/
2094 static void DestroyCursor( vout_thread_t *p_vout )
2096 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2099 /*****************************************************************************
2100 * ToggleCursor: hide or show the mouse pointer
2101 *****************************************************************************
2102 * This function hides the X pointer if it is visible by setting the pointer
2103 * sprite to a blank one. To show it again, we disable the sprite.
2104 *****************************************************************************/
2105 static void ToggleCursor( vout_thread_t *p_vout )
2107 if( p_vout->p_sys->b_mouse_pointer_visible )
2109 XDefineCursor( p_vout->p_sys->p_display,
2110 p_vout->p_sys->window.base_window,
2111 p_vout->p_sys->blank_cursor );
2112 p_vout->p_sys->b_mouse_pointer_visible = 0;
2116 XUndefineCursor( p_vout->p_sys->p_display,
2117 p_vout->p_sys->window.base_window );
2118 p_vout->p_sys->b_mouse_pointer_visible = 1;
2122 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2123 /*****************************************************************************
2124 * XVideoGetPort: get YUV12 port
2125 *****************************************************************************/
2126 static int XVideoGetPort( vout_thread_t *p_vout,
2127 vlc_fourcc_t i_chroma, picture_heap_t *p_heap )
2129 XvAdaptorInfo *p_adaptor;
2131 unsigned int i_adaptor, i_num_adaptors;
2132 int i_requested_adaptor;
2133 int i_selected_port;
2135 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2140 case XvBadExtension:
2141 msg_Warn( p_vout, "XvBadExtension" );
2145 msg_Warn( p_vout, "XvBadAlloc" );
2149 msg_Warn( p_vout, "XvQueryExtension failed" );
2153 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2154 DefaultRootWindow( p_vout->p_sys->p_display ),
2155 &i_num_adaptors, &p_adaptor ) )
2160 case XvBadExtension:
2161 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2165 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2169 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2173 i_selected_port = -1;
2174 #ifdef MODULE_NAME_IS_xvmc
2175 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2177 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2179 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2181 XvImageFormatValues *p_formats;
2182 int i_format, i_num_formats;
2185 /* If we requested an adaptor and it's not this one, we aren't
2187 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2192 /* If the adaptor doesn't have the required properties, skip it */
2193 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2194 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2199 /* Check that adaptor supports our requested format... */
2200 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2201 p_adaptor[i_adaptor].base_id,
2205 i_format < i_num_formats && ( i_selected_port == -1 );
2208 XvAttribute *p_attr;
2209 int i_attr, i_num_attributes;
2210 Atom autopaint = None, colorkey = None;
2212 /* If this is not the format we want, or at least a
2213 * similar one, forget it */
2214 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2219 /* Look for the first available port supporting this format */
2220 for( i_port = p_adaptor[i_adaptor].base_id;
2221 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2222 + p_adaptor[i_adaptor].num_ports) )
2223 && ( i_selected_port == -1 );
2226 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2229 i_selected_port = i_port;
2230 p_heap->i_chroma = p_formats[ i_format ].id;
2231 #if XvVersion > 2 || ( XvVersion == 2 && XvRevision >= 2 )
2232 p_heap->i_rmask = p_formats[ i_format ].red_mask;
2233 p_heap->i_gmask = p_formats[ i_format ].green_mask;
2234 p_heap->i_bmask = p_formats[ i_format ].blue_mask;
2239 /* If no free port was found, forget it */
2240 if( i_selected_port == -1 )
2245 /* If we found a port, print information about it */
2246 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2247 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2248 (char *)&p_formats[ i_format ].id,
2249 ( p_formats[ i_format ].format == XvPacked ) ?
2250 "packed" : "planar" );
2252 /* Use XV_AUTOPAINT_COLORKEY if supported, otherwise we will
2253 * manually paint the colour key */
2254 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2256 &i_num_attributes );
2258 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2260 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2262 autopaint = XInternAtom( p_vout->p_sys->p_display,
2263 "XV_AUTOPAINT_COLORKEY", False );
2264 XvSetPortAttribute( p_vout->p_sys->p_display,
2265 i_selected_port, autopaint, 1 );
2267 if( !strcmp( p_attr[i_attr].name, "XV_COLORKEY" ) )
2269 /* Find out the default colour key */
2270 colorkey = XInternAtom( p_vout->p_sys->p_display,
2271 "XV_COLORKEY", False );
2272 XvGetPortAttribute( p_vout->p_sys->p_display,
2273 i_selected_port, colorkey,
2274 &p_vout->p_sys->i_colourkey );
2277 p_vout->p_sys->b_paint_colourkey =
2278 autopaint == None && colorkey != None;
2280 if( p_attr != NULL )
2286 if( p_formats != NULL )
2293 if( i_num_adaptors > 0 )
2295 XvFreeAdaptorInfo( p_adaptor );
2298 if( i_selected_port == -1 )
2300 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2301 if( i_requested_adaptor == -1 )
2303 msg_Warn( p_vout, "no free XVideo port found for format "
2304 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2308 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2309 "XVideo port for format 0x%.8x (%4.4s)",
2310 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2314 return i_selected_port;
2317 /*****************************************************************************
2318 * XVideoReleasePort: release YUV12 port
2319 *****************************************************************************/
2320 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2322 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2326 /*****************************************************************************
2327 * InitDisplay: open and initialize X11 device
2328 *****************************************************************************
2329 * Create a window according to video output given size, and set other
2330 * properties according to the display properties.
2331 *****************************************************************************/
2332 static int InitDisplay( vout_thread_t *p_vout )
2334 #ifdef MODULE_NAME_IS_x11
2335 XPixmapFormatValues * p_formats; /* pixmap formats */
2336 XVisualInfo * p_xvisual; /* visuals information */
2337 XVisualInfo xvisual_template; /* visual template */
2338 int i_count, i; /* array size */
2341 #ifdef HAVE_SYS_SHM_H
2342 p_vout->p_sys->i_shm_opcode = 0;
2344 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) > 0 )
2346 int major, evt, err;
2348 if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2350 && XShmQueryExtension( p_vout->p_sys->p_display ) )
2351 p_vout->p_sys->i_shm_opcode = major;
2353 if( p_vout->p_sys->i_shm_opcode )
2358 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2360 msg_Dbg( p_vout, "XShm video extension v%d.%d "
2361 "(with%s pixmaps, opcode: %d)",
2362 major, minor, pixmaps ? "" : "out",
2363 p_vout->p_sys->i_shm_opcode );
2366 msg_Warn( p_vout, "XShm video extension not available" );
2369 msg_Dbg( p_vout, "XShm video extension disabled" );
2372 #ifdef MODULE_NAME_IS_xvideo
2373 /* XXX The brightness and contrast values should be read from environment
2374 * XXX variables... */
2376 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2377 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2381 #ifdef MODULE_NAME_IS_x11
2382 /* Initialize structure */
2383 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2385 /* Get screen depth */
2386 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2387 p_vout->p_sys->i_screen );
2388 switch( p_vout->p_sys->i_screen_depth )
2392 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2394 xvisual_template.screen = p_vout->p_sys->i_screen;
2395 xvisual_template.class = DirectColor;
2396 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2397 VisualScreenMask | VisualClassMask,
2398 &xvisual_template, &i_count );
2399 if( p_xvisual == NULL )
2401 msg_Err( p_vout, "no PseudoColor visual available" );
2402 return VLC_EGENERIC;
2404 p_vout->p_sys->i_bytes_per_pixel = 1;
2405 p_vout->output.pf_setpalette = SetPalette;
2412 * Screen depth is higher than 8bpp. TrueColor visual is used.
2414 xvisual_template.screen = p_vout->p_sys->i_screen;
2415 xvisual_template.class = TrueColor;
2416 /* In some cases, we get a truecolor class adaptor that has a different
2417 color depth. So try to get a real true color one first */
2418 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2420 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2421 VisualScreenMask | VisualClassMask |
2423 &xvisual_template, &i_count );
2424 if( p_xvisual == NULL )
2426 msg_Warn( p_vout, "No screen matching the required color depth" );
2427 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2428 VisualScreenMask | VisualClassMask,
2429 &xvisual_template, &i_count );
2430 if( p_xvisual == NULL )
2433 msg_Err( p_vout, "no TrueColor visual available" );
2434 return VLC_EGENERIC;
2438 p_vout->output.i_rmask = p_xvisual->red_mask;
2439 p_vout->output.i_gmask = p_xvisual->green_mask;
2440 p_vout->output.i_bmask = p_xvisual->blue_mask;
2442 /* There is no difference yet between 3 and 4 Bpp. The only way
2443 * to find the actual number of bytes per pixel is to list supported
2444 * pixmap formats. */
2445 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2446 p_vout->p_sys->i_bytes_per_pixel = 0;
2448 for( i = 0; i < i_count; i++ )
2450 /* Under XFree4.0, the list contains pixmap formats available
2451 * through all video depths ; so we have to check against current
2453 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2455 if( p_formats[i].bits_per_pixel / 8
2456 > (int)p_vout->p_sys->i_bytes_per_pixel )
2458 p_vout->p_sys->i_bytes_per_pixel =
2459 p_formats[i].bits_per_pixel / 8;
2463 if( p_formats ) XFree( p_formats );
2467 p_vout->p_sys->p_visual = p_xvisual->visual;
2474 #ifndef MODULE_NAME_IS_glx
2476 #ifdef HAVE_SYS_SHM_H
2477 /*****************************************************************************
2478 * CreateShmImage: create an XImage or XvImage using shared memory extension
2479 *****************************************************************************
2480 * Prepare an XImage or XvImage for display function.
2481 * The order of the operations respects the recommandations of the mit-shm
2482 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2483 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2484 *****************************************************************************/
2485 IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2486 Display* p_display, EXTRA_ARGS_SHM,
2487 int i_width, int i_height )
2489 IMAGE_TYPE *p_image;
2492 /* Create XImage / XvImage */
2493 #ifdef MODULE_NAME_IS_xvideo
2494 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2495 i_width, i_height, p_shm );
2496 #elif defined(MODULE_NAME_IS_xvmc)
2497 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2498 i_width, i_height, p_shm );
2500 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2501 p_shm, i_width, i_height );
2503 if( p_image == NULL )
2505 msg_Err( p_vout, "image creation failed" );
2509 /* For too big image, the buffer returned is sometimes too small, prevent
2510 * VLC to segfault because of it
2511 * FIXME is it normal ? Is there a way to detect it
2512 * before (XvQueryBestSize did not) ? */
2513 if( p_image->width < i_width || p_image->height < i_height )
2515 msg_Err( p_vout, "cannot allocate shared image data with the right size "
2516 "(%dx%d instead of %dx%d)",
2517 p_image->width, p_image->height,
2518 i_width, i_height );
2519 IMAGE_FREE( p_image );
2523 /* Allocate shared memory segment. */
2524 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0600 );
2525 if( p_shm->shmid < 0 )
2527 msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2528 IMAGE_FREE( p_image );
2532 /* Attach shared memory segment to process (read/write) */
2533 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2534 if(! p_shm->shmaddr )
2536 msg_Err( p_vout, "cannot attach shared memory (%m)" );
2537 IMAGE_FREE( p_image );
2538 shmctl( p_shm->shmid, IPC_RMID, 0 );
2542 /* Read-only data. We won't be using XShmGetImage */
2543 p_shm->readOnly = True;
2545 /* Attach shared memory segment to X server */
2546 XSynchronize( p_display, True );
2547 i_shm_major = p_vout->p_sys->i_shm_opcode;
2548 result = XShmAttach( p_display, p_shm );
2549 if( result == False || !i_shm_major )
2551 msg_Err( p_vout, "cannot attach shared memory to X server" );
2552 IMAGE_FREE( p_image );
2553 shmctl( p_shm->shmid, IPC_RMID, 0 );
2554 shmdt( p_shm->shmaddr );
2557 XSynchronize( p_display, False );
2559 /* Send image to X server. This instruction is required, since having
2560 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2561 * and remember NOT to use XFlush ! */
2562 XSync( p_display, False );
2565 /* Mark the shm segment to be removed when there are no more
2566 * attachements, so it is automatic on process exit or after shmdt */
2567 shmctl( p_shm->shmid, IPC_RMID, 0 );
2574 /*****************************************************************************
2575 * CreateImage: create an XImage or XvImage
2576 *****************************************************************************
2577 * Create a simple image used as a buffer.
2578 *****************************************************************************/
2579 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2580 Display *p_display, EXTRA_ARGS,
2581 int i_width, int i_height )
2583 uint8_t * p_data; /* image data storage zone */
2584 IMAGE_TYPE *p_image;
2585 #ifdef MODULE_NAME_IS_x11
2586 int i_quantum; /* XImage quantum (see below) */
2587 int i_bytes_per_line;
2590 /* Allocate memory for image */
2591 #ifdef MODULE_NAME_IS_xvideo
2592 p_data = malloc( i_width * i_height * i_bits_per_pixel / 8 );
2593 #elif defined(MODULE_NAME_IS_x11)
2594 i_bytes_per_line = i_width * i_bytes_per_pixel;
2595 p_data = malloc( i_bytes_per_line * i_height );
2600 #ifdef MODULE_NAME_IS_x11
2601 /* Optimize the quantum of a scanline regarding its size - the quantum is
2602 a diviser of the number of bits between the start of two scanlines. */
2603 if( i_bytes_per_line & 0xf )
2607 else if( i_bytes_per_line & 0x10 )
2617 /* Create XImage. p_data will be automatically freed */
2618 #ifdef MODULE_NAME_IS_xvideo
2619 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2620 (char *)p_data, i_width, i_height );
2621 #elif defined(MODULE_NAME_IS_x11)
2622 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2623 (char *)p_data, i_width, i_height, i_quantum, 0 );
2625 if( p_image == NULL )
2627 msg_Err( p_vout, "XCreateImage() failed" );
2636 /*****************************************************************************
2637 * X11ErrorHandler: replace error handler so we can intercept some of them
2638 *****************************************************************************/
2639 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2643 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
2645 "[????????] x11 video output error: X11 request %u.%u failed "
2646 "with error code %u:\n %s\n",
2647 event->request_code, event->minor_code, event->error_code, txt );
2649 switch( event->request_code )
2651 case X_SetInputFocus:
2652 /* Ignore errors on XSetInputFocus()
2653 * (they happen when a window is not yet mapped) */
2657 #ifdef HAVE_SYS_SHM_H
2658 if( event->request_code == i_shm_major ) /* MIT-SHM */
2661 "[????????] x11 video output notice:"
2662 " buggy X11 server claims shared memory\n"
2663 "[????????] x11 video output notice:"
2664 " support though it does not work (OpenSSH?)\n" );
2665 return i_shm_major = 0;
2670 XSetErrorHandler(NULL);
2671 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2673 /* Work-around Maemo Xvideo bug */
2678 #ifdef MODULE_NAME_IS_x11
2679 /*****************************************************************************
2680 * SetPalette: sets an 8 bpp palette
2681 *****************************************************************************
2682 * This function sets the palette given as an argument. It does not return
2683 * anything, but could later send information on which colors it was unable
2685 *****************************************************************************/
2686 static void SetPalette( vout_thread_t *p_vout,
2687 uint16_t *red, uint16_t *green, uint16_t *blue )
2690 XColor p_colors[255];
2692 /* allocate palette */
2693 for( i = 0; i < 255; i++ )
2695 /* kludge: colors are indexed reversely because color 255 seems
2696 * to be reserved for black even if we try to set it to white */
2697 p_colors[ i ].pixel = 255 - i;
2698 p_colors[ i ].pad = 0;
2699 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2700 p_colors[ i ].red = red[ 255 - i ];
2701 p_colors[ i ].blue = blue[ 255 - i ];
2702 p_colors[ i ].green = green[ 255 - i ];
2705 XStoreColors( p_vout->p_sys->p_display,
2706 p_vout->p_sys->colormap, p_colors, 255 );
2710 /*****************************************************************************
2711 * Control: control facility for the vout
2712 *****************************************************************************/
2713 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2715 unsigned int i_width, i_height;
2720 i_width = va_arg( args, unsigned int );
2721 i_height = va_arg( args, unsigned int );
2722 if( !i_width ) i_width = p_vout->i_window_width;
2723 if( !i_height ) i_height = p_vout->i_window_height;
2725 return vout_window_SetSize( p_vout->p_sys->window.owner_window,
2728 case VOUT_SET_STAY_ON_TOP:
2729 return vout_window_SetOnTop( p_vout->p_sys->window.owner_window,
2730 va_arg( args, int ) );
2733 return VLC_EGENERIC;
2737 /*****************************************************************************
2738 * Key events handling
2739 *****************************************************************************/
2745 } x11keys_to_vlckeys[] =
2747 { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
2748 { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
2749 { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
2750 { XK_F12, KEY_F12 },
2752 { XK_Return, KEY_ENTER },
2753 { XK_KP_Enter, KEY_ENTER },
2754 { XK_space, KEY_SPACE },
2755 { XK_Escape, KEY_ESC },
2757 { XK_Menu, KEY_MENU },
2758 { XK_Left, KEY_LEFT },
2759 { XK_Right, KEY_RIGHT },
2761 { XK_Down, KEY_DOWN },
2763 { XK_Home, KEY_HOME },
2764 { XK_End, KEY_END },
2765 { XK_Page_Up, KEY_PAGEUP },
2766 { XK_Page_Down, KEY_PAGEDOWN },
2768 { XK_Insert, KEY_INSERT },
2769 { XK_Delete, KEY_DELETE },
2770 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
2771 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
2772 { XF86XK_AudioMute, KEY_VOLUME_MUTE },
2773 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
2774 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
2775 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
2776 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
2781 static int ConvertKey( int i_key )
2785 for( i = 0; x11keys_to_vlckeys[i].i_x11key != 0; i++ )
2787 if( x11keys_to_vlckeys[i].i_x11key == i_key )
2789 return x11keys_to_vlckeys[i].i_vlckey;