1 /*****************************************************************************
2 * vout_xvideo.c: Xvideo video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000, 2001 VideoLAN
5 * $Id: vout_xvideo.c,v 1.18 2001/05/30 17:03:12 sam Exp $
7 * Authors: Shane Harper <shanegh@optusnet.com.au>
8 * Vincent Seguin <seguin@via.ecp.fr>
9 * Samuel Hocevar <sam@zoy.org>
10 * David Kennedy <dkennedy@tinytoad.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 #define MODULE_NAME xvideo
28 #include "modules_inner.h"
30 /*****************************************************************************
32 *****************************************************************************/
35 #include <errno.h> /* ENOMEM */
36 #include <stdlib.h> /* free() */
37 #include <string.h> /* strerror() */
39 #ifdef HAVE_MACHINE_PARAM_H
41 #include <machine/param.h>
42 #include <sys/types.h> /* typedef ushort */
47 #include <netinet/in.h> /* BSD: struct in_addr */
50 #include <sys/shm.h> /* shmget(), shmctl() */
52 #include <X11/Xutil.h>
53 #include <X11/keysym.h>
54 #include <X11/extensions/XShm.h>
55 #include <X11/extensions/Xv.h>
56 #include <X11/extensions/Xvlib.h>
65 #include "video_output.h"
67 #include "interface.h"
70 #include "netutils.h" /* network_ChannelJoin */
74 #include "stream_control.h" /* needed by input_ext-intf.h... */
75 #include "input_ext-intf.h"
78 #include "modules_export.h"
80 #define GUID_YUV12_PLANAR 0x32315659
83 /*****************************************************************************
84 * vout_sys_t: video output X11 method descriptor
85 *****************************************************************************
86 * This structure is part of the video output thread descriptor.
87 * It describes the XVideo specific properties of an output thread.
88 *****************************************************************************/
89 typedef struct vout_sys_s
93 /* this plugin (currently) requires the SHM Ext... */
94 boolean_t b_shm; /* shared memory extension flag */
97 /* Internal settings and properties */
98 Display * p_display; /* display pointer */
99 int i_screen; /* screen number */
100 Window window; /* root window */
101 GC gc; /* graphic context instance handler */
102 Window yuv_window; /* sub-window for displaying yuv video
107 /* Display buffers and shared memory information */
108 /* Note: only 1 buffer... Xv ext does double buffering. */
112 /* i_image_width & i_image_height reflect the
113 * size of the XvImage. They are used by
114 * vout_Display() to check if the image to be
115 * displayed can use the current XvImage. */
116 XShmSegmentInfo shm_info; /* shared memory zone information */
118 /* X11 generic properties */
120 Atom wm_delete_window;
122 int i_window_width; /* width of main window */
123 int i_window_height; /* height of main window */
126 /* Screen saver properties */
127 int i_ss_timeout; /* timeout */
128 int i_ss_interval; /* interval between changes */
129 int i_ss_blanking; /* blanking mode */
130 int i_ss_exposure; /* exposure mode */
132 /* Mouse pointer properties */
133 boolean_t b_mouse_pointer_visible;
134 mtime_t i_time_mouse_last_moved; /* used to auto-hide pointer*/
138 /* Fullscreen needs to be able to hide the wm decorations */
139 #define MWM_HINTS_DECORATIONS (1L << 1)
140 #define PROP_MWM_HINTS_ELEMENTS 5
141 typedef struct mwmhints_s
150 /*****************************************************************************
152 *****************************************************************************/
153 static int vout_Probe ( probedata_t * );
154 static int vout_Create ( vout_thread_t * );
155 static int vout_Init ( vout_thread_t * );
156 static void vout_End ( vout_thread_t * );
157 static void vout_Destroy ( vout_thread_t * );
158 static int vout_Manage ( vout_thread_t * );
159 static void vout_Display ( vout_thread_t * );
160 static void vout_SetPalette( vout_thread_t *, u16 *, u16 *, u16 *, u16 * );
162 static int XVideoCreateWindow ( vout_thread_t * );
163 static void XVideoDestroyWindow ( vout_thread_t *p_vout );
164 static int XVideoUpdateImgSizeIfRequired( vout_thread_t *p_vout );
165 static int XVideoCreateShmImage ( Display* dpy, int xv_port,
166 XvImage **pp_xvimage,
167 XShmSegmentInfo *p_shm_info,
168 int i_width, int i_height );
169 static void XVideoDestroyShmImage ( vout_thread_t *, XvImage *,
171 static void XVideoSetMousePointer ( const vout_thread_t * );
172 static void XVideoEnableScreenSaver ( vout_thread_t * );
173 static void XVideoDisableScreenSaver ( vout_thread_t * );
174 /*static void XVideoSetAttribute ( vout_thread_t *, char *, float );*/
176 static int XVideoCheckForXv ( Display * );
177 static int XVideoGetPort ( Display * );
178 static void XVideoOutputCoords ( const picture_t *, const boolean_t,
179 const int, const int,
180 int *, int *, int *, int * );
181 static void XVideoDisplay ( vout_thread_t * );
183 /*****************************************************************************
184 * Functions exported as capabilities. They are declared as static so that
185 * we don't pollute the namespace too much.
186 *****************************************************************************/
187 void _M( vout_getfunctions )( function_list_t * p_function_list )
189 p_function_list->pf_probe = vout_Probe;
190 p_function_list->functions.vout.pf_create = vout_Create;
191 p_function_list->functions.vout.pf_init = vout_Init;
192 p_function_list->functions.vout.pf_end = vout_End;
193 p_function_list->functions.vout.pf_destroy = vout_Destroy;
194 p_function_list->functions.vout.pf_manage = vout_Manage;
195 p_function_list->functions.vout.pf_display = vout_Display;
196 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
199 /*****************************************************************************
200 * vout_Probe: probe the video driver and return a score
201 *****************************************************************************
202 * This returns a score to the plugin manager so that it can select the best
204 *****************************************************************************/
205 static int vout_Probe( probedata_t *p_data )
207 if( TestMethod( VOUT_METHOD_VAR, "xvideo" ) )
215 /*****************************************************************************
216 * vout_Create: allocate XVideo video thread output method
217 *****************************************************************************
218 * This function allocate and initialize a XVideo vout method. It uses some of
219 * the vout properties to choose the window size, and change them according to
220 * the actual properties of the display.
221 *****************************************************************************/
222 static int vout_Create( vout_thread_t *p_vout )
226 /* Allocate structure */
227 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
228 if( p_vout->p_sys == NULL )
230 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
234 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
235 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
236 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
238 if( p_vout->p_sys->p_display == NULL ) /* error */
240 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
241 free( p_vout->p_sys );
244 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
247 = main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT );
249 if( !XVideoCheckForXv( p_vout->p_sys->p_display ) )
251 intf_ErrMsg( "vout error: no XVideo extension" );
252 XCloseDisplay( p_vout->p_sys->p_display );
253 free( p_vout->p_sys );
257 /* Spawn base window - this window will include the video output window,
258 * but also command buttons, subtitles and other indicators */
259 if( XVideoCreateWindow( p_vout ) )
261 intf_ErrMsg( "vout error: cannot create XVideo window" );
262 XCloseDisplay( p_vout->p_sys->p_display );
263 free( p_vout->p_sys );
267 if( (p_vout->p_sys->xv_port = XVideoGetPort( p_vout->p_sys->p_display ))<0 )
269 intf_DbgMsg( "Using xv port %d" , p_vout->p_sys->xv_port );
272 /* XXX The brightness and contrast values should be read from environment
273 * XXX variables... */
274 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
275 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
278 p_vout->p_sys->b_mouse_pointer_visible = 1;
280 /* Disable screen saver and return */
281 XVideoDisableScreenSaver( p_vout );
286 /*****************************************************************************
287 * vout_Init: initialize XVideo video thread output method
288 *****************************************************************************/
289 static int vout_Init( vout_thread_t *p_vout )
292 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
293 p_vout->p_sys->b_shm = 0;
295 p_vout->b_need_render = 0;
296 p_vout->p_sys->i_image_width = p_vout->p_sys->i_image_height = 0;
301 /*****************************************************************************
302 * vout_End: terminate XVideo video thread output method
303 *****************************************************************************
304 * Destroy the XvImage. It is called at the end of the thread, but also each
305 * time the image is resized.
306 *****************************************************************************/
307 static void vout_End( vout_thread_t *p_vout )
309 XVideoDestroyShmImage( p_vout, p_vout->p_sys->p_xvimage,
310 &p_vout->p_sys->shm_info );
313 /*****************************************************************************
314 * vout_Destroy: destroy XVideo video thread output method
315 *****************************************************************************
316 * Terminate an output method created by vout_Create
317 *****************************************************************************/
318 static void vout_Destroy( vout_thread_t *p_vout )
320 XVideoEnableScreenSaver( p_vout );
321 XVideoDestroyWindow( p_vout );
322 XCloseDisplay( p_vout->p_sys->p_display );
324 /* Destroy structure */
325 free( p_vout->p_sys );
328 /*****************************************************************************
329 * vout_Manage: handle X11 events
330 *****************************************************************************
331 * This function should be called regularly by video output thread. It manages
332 * X11 events and allows window resizing. It returns a non null value on
335 * XXX Should "factor-out" common code in this and the "same" fn in the x11
337 *****************************************************************************/
338 static int vout_Manage( vout_thread_t *p_vout )
340 XEvent xevent; /* X11 event */
341 char i_key; /* ISO Latin-1 key */
344 /* Handle X11 events: ConfigureNotify events are parsed to know if the
345 * output window's size changed, MapNotify and UnmapNotify to know if the
346 * window is mapped (and if the display is useful), and ClientMessages
347 * to intercept window destruction requests */
348 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
349 StructureNotifyMask | KeyPressMask |
350 ButtonPressMask | ButtonReleaseMask |
351 PointerMotionMask, &xevent )
354 /* ConfigureNotify event: prepare */
355 if( (xevent.type == ConfigureNotify)
356 /*&& ((xevent.xconfigure.width != p_vout->p_sys->i_window_width)
357 || (xevent.xconfigure.height != p_vout->p_sys->i_window_height))*/ )
359 /* Update dimensions */
360 p_vout->p_sys->i_window_width = xevent.xconfigure.width;
361 p_vout->p_sys->i_window_height = xevent.xconfigure.height;
363 /* MapNotify event: change window status and disable screen saver */
364 else if( xevent.type == MapNotify)
366 if( (p_vout != NULL) && !p_vout->b_active )
368 XVideoDisableScreenSaver( p_vout );
369 p_vout->b_active = 1;
372 /* UnmapNotify event: change window status and enable screen saver */
373 else if( xevent.type == UnmapNotify )
375 if( (p_vout != NULL) && p_vout->b_active )
377 XVideoEnableScreenSaver( p_vout );
378 p_vout->b_active = 0;
382 else if( xevent.type == KeyPress )
384 /* We may have keys like F1 trough F12, ESC ... */
385 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
386 xevent.xkey.keycode, 0 );
387 switch( x_key_symbol )
390 p_main->p_intf->b_die = 1;
393 p_main->p_intf->b_menu_change = 1;
397 * The reason why I use this instead of XK_0 is that
398 * with XLookupString, we don't have to care about
401 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
407 p_main->p_intf->b_die = 1;
411 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
414 network_ChannelJoin( 0 );
417 network_ChannelJoin( 1 );
420 network_ChannelJoin( 2 );
423 network_ChannelJoin( 3 );
426 network_ChannelJoin( 4 );
429 network_ChannelJoin( 5 );
432 network_ChannelJoin( 6 );
435 network_ChannelJoin( 7 );
438 network_ChannelJoin( 8 );
441 network_ChannelJoin( 9 );
444 if( intf_ProcessKey( p_main->p_intf,
447 intf_DbgMsg( "unhandled key '%c' (%i)",
448 (char)i_key, i_key );
457 else if( xevent.type == ButtonPress )
459 switch( ((XButtonEvent *)&xevent)->button )
462 /* in this part we will eventually manage
463 * clicks for DVD navigation for instance */
468 else if( xevent.type == ButtonRelease )
470 switch( ((XButtonEvent *)&xevent)->button )
473 /* FIXME: need locking ! */
474 p_main->p_intf->b_menu_change = 1;
479 else if( xevent.type == MotionNotify )
481 p_vout->p_sys->i_time_mouse_last_moved = mdate();
482 p_vout->p_sys->b_mouse_pointer_visible = 1;
483 XVideoSetMousePointer( p_vout );
488 intf_WarnMsg( 3, "%p -> unhandled event type %d received",
489 p_vout, xevent.type );
493 /* Handle events for YUV video output sub-window */
494 while( XCheckWindowEvent( p_vout->p_sys->p_display,
495 p_vout->p_sys->yuv_window,
496 ExposureMask, &xevent ) == True )
498 /* Window exposed (only handled if stream playback is paused) */
499 if( xevent.type == Expose )
501 if( ((XExposeEvent *)&xevent)->count == 0 )
502 /* (if this is the last a collection of expose events...) */
503 if( p_main->p_intf->p_input )
505 p_main->p_intf->p_input->stream.control.i_status )
506 XVideoDisplay( p_vout );
510 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
511 * are handled - according to the man pages, the format is always 32
513 while( XCheckTypedEvent( p_vout->p_sys->p_display,
514 ClientMessage, &xevent ) )
516 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
517 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
519 p_main->p_intf->b_die = 1;
523 intf_DbgMsg( "%p -> unhandled ClientMessage received", p_vout );
527 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
529 intf_DbgMsg( "vout: changing full-screen status" );
531 p_vout->b_fullscreen = !p_vout->b_fullscreen;
533 /* Get rid of the old window */
534 XVideoDestroyWindow( p_vout );
536 /* And create a new one */
537 if( XVideoCreateWindow( p_vout ) )
539 intf_ErrMsg( "vout error: cannot create X11 window" );
540 XCloseDisplay( p_vout->p_sys->p_display );
542 free( p_vout->p_sys );
548 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE))
550 /* FIXME: clear flags ?? */
556 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
558 intf_DbgMsg( "vout: resizing window" );
559 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
560 /* Nothing to do here...
561 * vout_Display() detects size changes of the image to be displayed and
562 * re-creates the XvImage.*/
563 intf_Msg( "vout: video display resized (%dx%d)",
564 p_vout->i_width, p_vout->i_height );
567 /* Autohide Cursor */
568 if( p_vout->p_sys->b_mouse_pointer_visible &&
569 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
571 p_vout->p_sys->b_mouse_pointer_visible = 0;
572 XVideoSetMousePointer( p_vout );
578 /*****************************************************************************
579 * vout_Display: displays previously rendered output
580 *****************************************************************************
581 * This function sends the currently rendered image to X11 server.
582 * (The Xv extension takes care of "double-buffering".)
583 *****************************************************************************/
584 static void vout_Display( vout_thread_t *p_vout )
586 boolean_t b_draw = 1;
587 int i_size = p_vout->p_rendered_pic->i_width *
588 p_vout->p_rendered_pic->i_height;
590 if( XVideoUpdateImgSizeIfRequired( p_vout ) )
593 switch( p_vout->p_rendered_pic->i_type )
595 case YUV_422_PICTURE:
596 intf_ErrMsg( "vout error: YUV_422_PICTURE not (yet) supported" );
600 case YUV_444_PICTURE:
601 intf_ErrMsg( "vout error: YUV_444_PICTURE not (yet) supported" );
605 case YUV_420_PICTURE:
606 memcpy( p_vout->p_sys->p_xvimage->data,
607 p_vout->p_rendered_pic->p_y, i_size );
608 memcpy( p_vout->p_sys->p_xvimage->data + ( i_size ),
609 p_vout->p_rendered_pic->p_v, i_size / 4 );
610 memcpy( p_vout->p_sys->p_xvimage->data + ( i_size ) + ( i_size / 4 ),
611 p_vout->p_rendered_pic->p_u, i_size / 4 );
616 XVideoDisplay( p_vout );
619 static void vout_SetPalette( p_vout_thread_t p_vout,
620 u16 *red, u16 *green, u16 *blue, u16 *transp )
625 /* following functions are local */
627 /*****************************************************************************
628 * XVideoUpdateImgSizeIfRequired
629 *****************************************************************************
630 * This function checks to see if the image to be displayed is of a different
631 * size to the last image displayed. If so, the old shm block must be
632 * destroyed and a new one created.
633 * Note: the "image size" is the size of the image to be passed to the Xv
634 * extension (which is probably different to the size of the output window).
635 *****************************************************************************/
636 static int XVideoUpdateImgSizeIfRequired( vout_thread_t *p_vout )
638 int i_img_width = p_vout->p_rendered_pic->i_width;
639 int i_img_height = p_vout->p_rendered_pic->i_height;
641 if( p_vout->p_sys->i_image_width != i_img_width
642 || p_vout->p_sys->i_image_height != i_img_height )
644 p_vout->p_sys->i_image_width = i_img_width;
645 p_vout->p_sys->i_image_height = i_img_height;
647 /* Destroy XvImage to change its size */
649 /* Note: vout_End does nothing if no XvImage to destroy. */
651 /* Create XvImage using XShm extension */
652 if( XVideoCreateShmImage( p_vout->p_sys->p_display,
653 p_vout->p_sys->xv_port,
654 &p_vout->p_sys->p_xvimage,
655 &p_vout->p_sys->shm_info,
656 i_img_width, i_img_height ) )
658 intf_ErrMsg( "vout: failed to create xvimage." );
659 p_vout->p_sys->i_image_width = 0;
663 /* Set bytes per line and initialize buffers */
664 p_vout->i_bytes_per_line =
665 (p_vout->p_sys->p_xvimage->data_size) /
666 (p_vout->p_sys->p_xvimage->height);
668 /* p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_xvimage->data ); */
674 /*****************************************************************************
675 * XVideoCheckForXv: check for the XVideo extension
676 *****************************************************************************/
677 static int XVideoCheckForXv( Display *dpy )
681 switch( XvQueryExtension( dpy, &i, &i, &i, &i, &i ) )
687 intf_ErrMsg( "vout error: XvBadExtension" );
691 intf_ErrMsg( "vout error: XvBadAlloc" );
695 intf_ErrMsg( "vout error: XvQueryExtension failed" );
700 /*****************************************************************************
701 * XVideoCreateWindow: open and set-up XVideo main window
702 *****************************************************************************/
703 static int XVideoCreateWindow( vout_thread_t *p_vout )
705 XSizeHints xsize_hints;
706 XSetWindowAttributes xwindow_attributes;
713 boolean_t b_configure_notify;
714 boolean_t b_map_notify;
717 /* Set main window's size */
718 /* If we're full screen, we're full screen! */
719 if( p_vout->b_fullscreen )
721 p_vout->p_sys->i_window_width = DisplayWidth( p_vout->p_sys->p_display,
722 p_vout->p_sys->i_screen );
723 p_vout->p_sys->i_window_height = DisplayHeight( p_vout->p_sys->p_display,
724 p_vout->p_sys->i_screen );
725 /* p_vout->i_width = p_vout->p_sys->i_window_width;
726 p_vout->i_height = p_vout->p_sys->i_window_height; */
730 p_vout->p_sys->i_window_width = main_GetIntVariable( VOUT_WIDTH_VAR,
731 VOUT_WIDTH_DEFAULT );
732 p_vout->p_sys->i_window_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
733 VOUT_HEIGHT_DEFAULT );
736 /* Prepare window manager hints and properties */
737 xsize_hints.base_width = p_vout->p_sys->i_window_width;
738 xsize_hints.base_height = p_vout->p_sys->i_window_height;
739 xsize_hints.flags = PSize;
740 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
741 "WM_PROTOCOLS", True );
742 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
743 "WM_DELETE_WINDOW", True );
745 /* Prepare window attributes */
746 xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
747 p_vout->p_sys->i_screen );
749 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
751 /* Create the window and set hints - the window must receive ConfigureNotify
752 * events, and, until it is displayed, Expose and MapNotify events. */
753 p_vout->p_sys->window =
754 XCreateWindow( p_vout->p_sys->p_display,
755 DefaultRootWindow( p_vout->p_sys->p_display ),
757 p_vout->p_sys->i_window_width,
758 p_vout->p_sys->i_window_height, 1,
760 CWBackPixel | CWEventMask,
761 &xwindow_attributes );
763 if ( p_vout->b_fullscreen )
765 prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
766 mwmhints.flags = MWM_HINTS_DECORATIONS;
767 mwmhints.decorations = 0;
768 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
769 prop, prop, 32, PropModeReplace,
770 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
772 XSetTransientForHint( p_vout->p_sys->p_display,
773 p_vout->p_sys->window, None );
774 XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
778 /* Set window manager hints and properties: size hints, command,
779 * window's name, and accepted protocols */
780 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
782 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
783 p_main->ppsz_argv, p_main->i_argc );
784 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
785 VOUT_TITLE " (XVideo output)" );
787 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
788 || (p_vout->p_sys->wm_delete_window == None)
789 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
790 &p_vout->p_sys->wm_delete_window, 1 ) )
792 /* WM_DELETE_WINDOW is not supported by window manager */
793 intf_Msg( "vout error: missing or bad window manager" );
796 /* Creation of a graphic context that doesn't generate a GraphicsExpose
797 * event when using functions like XCopyArea */
798 xgcvalues.graphics_exposures = False;
799 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
800 p_vout->p_sys->window,
801 GCGraphicsExposures, &xgcvalues);
803 /* Send orders to server, and wait until window is displayed - three
804 * events must be received: a MapNotify event, an Expose event allowing
805 * drawing in the window, and a ConfigureNotify to get the window
806 * dimensions. Once those events have been received, only ConfigureNotify
807 * events need to be received. */
809 b_configure_notify = 0;
811 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
814 XNextEvent( p_vout->p_sys->p_display, &xevent);
815 if( (xevent.type == Expose)
816 && (xevent.xexpose.window == p_vout->p_sys->window) )
820 else if( (xevent.type == MapNotify)
821 && (xevent.xmap.window == p_vout->p_sys->window) )
825 else if( (xevent.type == ConfigureNotify)
826 && (xevent.xconfigure.window == p_vout->p_sys->window) )
828 b_configure_notify = 1;
829 p_vout->p_sys->i_window_width = xevent.xconfigure.width;
830 p_vout->p_sys->i_window_height = xevent.xconfigure.height;
832 } while( !( b_expose && b_configure_notify && b_map_notify ) );
834 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
835 StructureNotifyMask | KeyPressMask |
836 ButtonPressMask | ButtonReleaseMask |
839 if( p_vout->b_fullscreen )
841 XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
842 RevertToNone, CurrentTime );
843 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
846 /* Create YUV output sub-window. */
847 p_vout->p_sys->yuv_window=XCreateSimpleWindow( p_vout->p_sys->p_display,
848 p_vout->p_sys->window, 0, 0, 1, 1, 0,
849 BlackPixel( p_vout->p_sys->p_display,
850 p_vout->p_sys->i_screen ),
851 WhitePixel( p_vout->p_sys->p_display,
852 p_vout->p_sys->i_screen ) );
854 p_vout->p_sys->yuv_gc = XCreateGC( p_vout->p_sys->p_display,
855 p_vout->p_sys->yuv_window,
856 GCGraphicsExposures, &xgcvalues );
858 XSetWindowBackground( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
859 BlackPixel(p_vout->p_sys->p_display, p_vout->p_sys->i_screen ) );
861 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
862 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
866 XVideoSetMousePointer( p_vout );
871 static void XVideoDestroyWindow( vout_thread_t *p_vout )
873 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->yuv_gc );
874 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
876 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
877 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
878 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
881 /*****************************************************************************
882 * XVideoCreateShmImage: create an XvImage using shared memory extension
883 *****************************************************************************
884 * Prepare an XvImage for display function.
885 * The order of the operations respects the recommandations of the mit-shm
886 * document by J.Corbet and K.Packard. Most of the parameters were copied from
888 *****************************************************************************/
889 static int XVideoCreateShmImage( Display* dpy, int xv_port,
890 XvImage **pp_xvimage,
891 XShmSegmentInfo *p_shm_info,
892 int i_width, int i_height )
894 *pp_xvimage = XvShmCreateImage( dpy, xv_port,
895 GUID_YUV12_PLANAR, 0,
900 intf_ErrMsg( "vout error: XvShmCreateImage failed." );
904 p_shm_info->shmid = shmget( IPC_PRIVATE, (*pp_xvimage)->data_size,
906 if( p_shm_info->shmid < 0) /* error */
908 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
913 p_shm_info->shmaddr = (*pp_xvimage)->data = shmat( p_shm_info->shmid,
915 p_shm_info->readOnly = False;
917 /* Mark the shm segment to be removed when there will be no more
918 * attachements, so it is automatic on process exit or after shmdt */
919 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
921 if( !XShmAttach( dpy, p_shm_info ) )
923 intf_ErrMsg( "vout error: XShmAttach failed" );
924 shmdt( p_shm_info->shmaddr );
928 /* Send image to X server. This instruction is required, since having
929 * built a Shm XImage and not using it causes an error on XCloseDisplay */
935 /*****************************************************************************
936 * XVideoDestroyShmImage
937 *****************************************************************************
938 * Destroy XImage AND associated data. Detach shared memory segment from
939 * server and process, then free it. If pointer is NULL, the image won't be
940 * destroyed (see vout_ManageOutputMethod())
941 *****************************************************************************/
942 static void XVideoDestroyShmImage( vout_thread_t *p_vout, XvImage *p_xvimage,
943 XShmSegmentInfo *p_shm_info )
945 /* If pointer is NULL, do nothing */
946 if( p_xvimage == NULL )
951 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
953 XDestroyImage( p_ximage ); /* XXX */
956 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
957 { /* also automatic freeing... */
958 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
963 /*****************************************************************************
964 * XVideoEnableScreenSaver: enable screen saver
965 *****************************************************************************
966 * This function enable the screen saver on a display after it had been
967 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
968 * know wether the screen saver can be activated or not: if n successive calls
969 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
970 * will be required before the screen saver could effectively be activated.
971 *****************************************************************************/
972 void XVideoEnableScreenSaver( vout_thread_t *p_vout )
974 intf_DbgMsg( "intf: enabling screen saver" );
975 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
976 p_vout->p_sys->i_ss_interval,
977 p_vout->p_sys->i_ss_blanking,
978 p_vout->p_sys->i_ss_exposure );
981 /*****************************************************************************
982 * XVideoDisableScreenSaver: disable screen saver
983 *****************************************************************************
984 * See XEnableScreenSaver
985 *****************************************************************************/
986 void XVideoDisableScreenSaver( vout_thread_t *p_vout )
988 /* Save screen saver informations */
989 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
990 &p_vout->p_sys->i_ss_interval,
991 &p_vout->p_sys->i_ss_blanking,
992 &p_vout->p_sys->i_ss_exposure );
994 /* Disable screen saver */
995 intf_DbgMsg( "intf: disabling screen saver" );
996 XSetScreenSaver( p_vout->p_sys->p_display, 0,
997 p_vout->p_sys->i_ss_interval,
998 p_vout->p_sys->i_ss_blanking,
999 p_vout->p_sys->i_ss_exposure );
1002 /*****************************************************************************
1003 * XVideoSetMousePointer: hide or show the mouse pointer
1004 *****************************************************************************
1005 * This function hides the X pointer if requested.
1006 *****************************************************************************/
1007 void XVideoSetMousePointer( const vout_thread_t *p_vout )
1009 static Cursor blank_cursor;
1010 static boolean_t b_created_blank_cursor = 0;
1012 if( !p_vout->p_sys->b_mouse_pointer_visible )
1014 if( !b_created_blank_cursor )
1017 Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1018 DefaultRootWindow(p_vout->p_sys->p_display),
1021 XParseColor( p_vout->p_sys->p_display,
1022 XCreateColormap( p_vout->p_sys->p_display,
1024 p_vout->p_sys->p_display ),
1026 p_vout->p_sys->p_display,
1027 p_vout->p_sys->i_screen ),
1031 blank_cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1032 blank, blank, &color, &color, 1, 1 );
1034 b_created_blank_cursor = 1;
1036 XDefineCursor( p_vout->p_sys->p_display,
1037 p_vout->p_sys->window, blank_cursor );
1041 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );
1045 /* This based on some code in SetBufferPicture... At the moment it's only
1046 * used by the xvideo plugin, but others may want to use it. */
1047 static void XVideoOutputCoords( const picture_t *p_pic, const boolean_t scale,
1048 const int win_w, const int win_h,
1049 int *dx, int *dy, int *w, int *h)
1053 *w = p_pic->i_width; *h = p_pic->i_height;
1058 switch( p_pic->i_aspect_ratio )
1060 case AR_3_4_PICTURE:
1064 case AR_16_9_PICTURE:
1065 *h = win_w * 9 / 16;
1068 case AR_221_1_PICTURE:
1069 *h = win_w * 100 / 221;
1072 case AR_SQUARE_PICTURE:
1074 *h = win_w * p_pic->i_height / p_pic->i_width;
1081 switch( p_pic->i_aspect_ratio )
1083 case AR_3_4_PICTURE:
1087 case AR_16_9_PICTURE:
1088 *w = win_h * 16 / 9;
1091 case AR_221_1_PICTURE:
1092 *w = win_h * 221 / 100;
1095 case AR_SQUARE_PICTURE:
1097 *w = win_h * p_pic->i_width / p_pic->i_height;
1103 /* Set picture position */
1104 *dx = (win_w - *w) / 2;
1105 *dy = (win_h - *h) / 2;
1109 static int XVideoGetPort( Display *dpy )
1113 XvAdaptorInfo *adaptor_info;
1115 switch( XvQueryAdaptors( dpy, DefaultRootWindow( dpy ),
1116 &i_adaptors, &adaptor_info ) )
1121 case XvBadExtension:
1122 intf_ErrMsg( "vout error: XvBadExtension for XvQueryAdaptors" );
1126 intf_ErrMsg( "vout error: XvBadAlloc for XvQueryAdaptors" );
1130 intf_ErrMsg( "vout error: XvQueryAdaptors failed" );
1134 for( i=0; i < i_adaptors && xv_port == -1; ++i )
1135 if( ( adaptor_info[ i ].type & XvInputMask ) &&
1136 ( adaptor_info[ i ].type & XvImageMask ) )
1138 /* check that port supports YUV12 planar format... */
1139 int port = adaptor_info[ i ].base_id;
1140 int i_num_formats, i;
1141 XvImageFormatValues *imageFormats;
1143 imageFormats = XvListImageFormats( dpy, port, &i_num_formats );
1145 for( i=0; i < i_num_formats && xv_port == -1; ++i )
1146 if( imageFormats[ i ].id == GUID_YUV12_PLANAR )
1150 intf_WarnMsg( 3, "vout: XVideo image input port %d "
1151 "does not support the YUV12 planar format which is "
1152 "currently required by the xvideo output plugin.",
1156 XFree( imageFormats );
1159 if( i_adaptors > 0 )
1160 XvFreeAdaptorInfo(adaptor_info);
1163 intf_ErrMsg( "vout error: didn't find a suitable Xvideo image input port." );
1169 /*****************************************************************************
1170 * XVideoDisplay: display image
1171 *****************************************************************************
1172 * This function displays the image stored in p_vout->p_sys->p_xvimage.
1173 * The image is scaled to fit in the output window (and to have the correct
1175 *****************************************************************************/
1176 static void XVideoDisplay( vout_thread_t *p_vout )
1178 int i_dest_width, i_dest_height, i_dest_x, i_dest_y;
1180 if( !p_vout->p_sys->p_xvimage ) return;
1182 XVideoOutputCoords( p_vout->p_rendered_pic, p_vout->b_scale,
1183 p_vout->p_sys->i_window_width,
1184 p_vout->p_sys->i_window_height,
1185 &i_dest_x, &i_dest_y,
1186 &i_dest_width, &i_dest_height);
1188 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->xv_port,
1189 p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
1190 p_vout->p_sys->p_xvimage,
1191 0 /*src_x*/, 0 /*src_y*/,
1192 p_vout->p_rendered_pic->i_width,
1193 p_vout->p_rendered_pic->i_height,
1194 0 /*dest_x*/, 0 /*dest_y*/, i_dest_width, i_dest_height,
1197 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1198 i_dest_width, i_dest_height );
1199 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1200 i_dest_x, i_dest_y );
1204 /*****************************************************************************
1205 * XVideoSetAttribute
1206 *****************************************************************************
1207 * This function can be used to set attributes, e.g. XV_BRIGHTNESS and
1208 * XV_CONTRAST. "f_value" should be in the range of 0 to 1.
1209 *****************************************************************************/
1210 static void XVideoSetAttribute( vout_thread_t *p_vout,
1211 char *attr_name, float f_value )
1214 XvAttribute *p_attrib;
1215 Display *p_dpy = p_vout->p_sys->p_display;
1216 int xv_port = p_vout->p_sys->xv_port;
1218 p_attrib = XvQueryPortAttributes( p_dpy, xv_port, &i_attrib );
1224 if( i_attrib >= 0 && !strcmp( p_attrib[ i_attrib ].name, attr_name ) )
1226 int i_sv = f_value * ( p_attrib[ i_attrib ].max_value
1227 - p_attrib[ i_attrib ].min_value + 1 )
1228 + p_attrib[ i_attrib ].min_value;
1230 XvSetPortAttribute( p_dpy, xv_port,
1231 XInternAtom( p_dpy, attr_name, False ), i_sv );
1235 } while( i_attrib > 0 );