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.22 2001/07/10 06:07:53 gbazin 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 XVideoDestroyWindow( p_vout );
270 XCloseDisplay( p_vout->p_sys->p_display );
271 free( p_vout->p_sys );
274 intf_DbgMsg( "Using xv port %d" , p_vout->p_sys->xv_port );
277 /* XXX The brightness and contrast values should be read from environment
278 * XXX variables... */
279 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
280 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
283 p_vout->p_sys->b_mouse_pointer_visible = 1;
285 /* Disable screen saver and return */
286 XVideoDisableScreenSaver( p_vout );
291 /*****************************************************************************
292 * vout_Init: initialize XVideo video thread output method
293 *****************************************************************************/
294 static int vout_Init( vout_thread_t *p_vout )
297 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
298 p_vout->p_sys->b_shm = 0;
300 p_vout->b_need_render = 0;
301 p_vout->p_sys->i_image_width = p_vout->p_sys->i_image_height = 0;
306 /*****************************************************************************
307 * vout_End: terminate XVideo video thread output method
308 *****************************************************************************
309 * Destroy the XvImage. It is called at the end of the thread, but also each
310 * time the image is resized.
311 *****************************************************************************/
312 static void vout_End( vout_thread_t *p_vout )
314 XVideoDestroyShmImage( p_vout, p_vout->p_sys->p_xvimage,
315 &p_vout->p_sys->shm_info );
318 /*****************************************************************************
319 * vout_Destroy: destroy XVideo video thread output method
320 *****************************************************************************
321 * Terminate an output method created by vout_Create
322 *****************************************************************************/
323 static void vout_Destroy( vout_thread_t *p_vout )
325 XVideoEnableScreenSaver( p_vout );
326 XVideoDestroyWindow( p_vout );
327 XCloseDisplay( p_vout->p_sys->p_display );
329 /* Destroy structure */
330 free( p_vout->p_sys );
333 /*****************************************************************************
334 * vout_Manage: handle X11 events
335 *****************************************************************************
336 * This function should be called regularly by video output thread. It manages
337 * X11 events and allows window resizing. It returns a non null value on
340 * XXX Should "factor-out" common code in this and the "same" fn in the x11
342 *****************************************************************************/
343 static int vout_Manage( vout_thread_t *p_vout )
345 XEvent xevent; /* X11 event */
346 char i_key; /* ISO Latin-1 key */
349 /* Handle X11 events: ConfigureNotify events are parsed to know if the
350 * output window's size changed, MapNotify and UnmapNotify to know if the
351 * window is mapped (and if the display is useful), and ClientMessages
352 * to intercept window destruction requests */
353 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
354 StructureNotifyMask | KeyPressMask |
355 ButtonPressMask | ButtonReleaseMask |
356 PointerMotionMask, &xevent )
359 /* ConfigureNotify event: prepare */
360 if( (xevent.type == ConfigureNotify)
361 /*&& ((xevent.xconfigure.width != p_vout->p_sys->i_window_width)
362 || (xevent.xconfigure.height != p_vout->p_sys->i_window_height))*/ )
364 /* Update dimensions */
365 p_vout->p_sys->i_window_width = xevent.xconfigure.width;
366 p_vout->p_sys->i_window_height = xevent.xconfigure.height;
368 /* MapNotify event: change window status and disable screen saver */
369 else if( xevent.type == MapNotify)
371 if( (p_vout != NULL) && !p_vout->b_active )
373 XVideoDisableScreenSaver( p_vout );
374 p_vout->b_active = 1;
377 /* UnmapNotify event: change window status and enable screen saver */
378 else if( xevent.type == UnmapNotify )
380 if( (p_vout != NULL) && p_vout->b_active )
382 XVideoEnableScreenSaver( p_vout );
383 p_vout->b_active = 0;
387 else if( xevent.type == KeyPress )
389 /* We may have keys like F1 trough F12, ESC ... */
390 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
391 xevent.xkey.keycode, 0 );
392 switch( x_key_symbol )
395 p_main->p_intf->b_die = 1;
398 p_main->p_intf->b_menu_change = 1;
402 * The reason why I use this instead of XK_0 is that
403 * with XLookupString, we don't have to care about
406 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
412 p_main->p_intf->b_die = 1;
416 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
419 network_ChannelJoin( 0 );
422 network_ChannelJoin( 1 );
425 network_ChannelJoin( 2 );
428 network_ChannelJoin( 3 );
431 network_ChannelJoin( 4 );
434 network_ChannelJoin( 5 );
437 network_ChannelJoin( 6 );
440 network_ChannelJoin( 7 );
443 network_ChannelJoin( 8 );
446 network_ChannelJoin( 9 );
449 if( intf_ProcessKey( p_main->p_intf,
452 intf_DbgMsg( "unhandled key '%c' (%i)",
453 (char)i_key, i_key );
462 else if( xevent.type == ButtonPress )
464 switch( ((XButtonEvent *)&xevent)->button )
467 /* in this part we will eventually manage
468 * clicks for DVD navigation for instance */
473 else if( xevent.type == ButtonRelease )
475 switch( ((XButtonEvent *)&xevent)->button )
478 /* FIXME: need locking ! */
479 p_main->p_intf->b_menu_change = 1;
484 else if( xevent.type == MotionNotify )
486 p_vout->p_sys->i_time_mouse_last_moved = mdate();
487 p_vout->p_sys->b_mouse_pointer_visible = 1;
488 XVideoSetMousePointer( p_vout );
493 intf_WarnMsg( 3, "%p -> unhandled event type %d received",
494 p_vout, xevent.type );
498 /* Handle events for YUV video output sub-window */
499 while( XCheckWindowEvent( p_vout->p_sys->p_display,
500 p_vout->p_sys->yuv_window,
501 ExposureMask, &xevent ) == True )
503 /* Window exposed (only handled if stream playback is paused) */
504 if( xevent.type == Expose )
506 if( ((XExposeEvent *)&xevent)->count == 0 )
507 /* (if this is the last a collection of expose events...) */
508 if( p_main->p_intf->p_input )
510 p_main->p_intf->p_input->stream.control.i_status )
511 XVideoDisplay( p_vout );
515 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
516 * are handled - according to the man pages, the format is always 32
518 while( XCheckTypedEvent( p_vout->p_sys->p_display,
519 ClientMessage, &xevent ) )
521 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
522 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
524 p_main->p_intf->b_die = 1;
528 intf_DbgMsg( "%p -> unhandled ClientMessage received", p_vout );
532 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
534 intf_DbgMsg( "vout: changing full-screen status" );
536 p_vout->b_fullscreen = !p_vout->b_fullscreen;
538 /* Get rid of the old window */
539 XVideoDestroyWindow( p_vout );
541 /* And create a new one */
542 if( XVideoCreateWindow( p_vout ) )
544 intf_ErrMsg( "vout error: cannot create X11 window" );
545 XCloseDisplay( p_vout->p_sys->p_display );
547 free( p_vout->p_sys );
553 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE))
555 /* FIXME: clear flags ?? */
561 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
563 intf_DbgMsg( "vout: resizing window" );
564 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
565 /* Nothing to do here...
566 * vout_Display() detects size changes of the image to be displayed and
567 * re-creates the XvImage.*/
568 intf_Msg( "vout: video display resized (%dx%d)",
569 p_vout->i_width, p_vout->i_height );
572 /* Autohide Cursor */
573 if( p_vout->p_sys->b_mouse_pointer_visible &&
574 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
576 p_vout->p_sys->b_mouse_pointer_visible = 0;
577 XVideoSetMousePointer( p_vout );
583 /*****************************************************************************
584 * vout_Display: displays previously rendered output
585 *****************************************************************************
586 * This function sends the currently rendered image to X11 server.
587 * (The Xv extension takes care of "double-buffering".)
588 *****************************************************************************/
589 static void vout_Display( vout_thread_t *p_vout )
591 boolean_t b_draw = 1;
592 int i_size = p_vout->p_rendered_pic->i_width *
593 p_vout->p_rendered_pic->i_height;
595 if( XVideoUpdateImgSizeIfRequired( p_vout ) )
600 switch( p_vout->p_rendered_pic->i_type )
602 case YUV_422_PICTURE:
603 intf_ErrMsg( "vout error: YUV_422_PICTURE not (yet) supported" );
607 case YUV_444_PICTURE:
608 intf_ErrMsg( "vout error: YUV_444_PICTURE not (yet) supported" );
612 case YUV_420_PICTURE:
613 memcpy( p_vout->p_sys->p_xvimage->data,
614 p_vout->p_rendered_pic->p_y, i_size );
615 memcpy( p_vout->p_sys->p_xvimage->data + ( i_size ),
616 p_vout->p_rendered_pic->p_v, i_size / 4 );
617 memcpy( p_vout->p_sys->p_xvimage->data + ( i_size ) + ( i_size / 4 ),
618 p_vout->p_rendered_pic->p_u, i_size / 4 );
624 XVideoDisplay( p_vout );
628 static void vout_SetPalette( p_vout_thread_t p_vout,
629 u16 *red, u16 *green, u16 *blue, u16 *transp )
634 /* following functions are local */
636 /*****************************************************************************
637 * XVideoUpdateImgSizeIfRequired
638 *****************************************************************************
639 * This function checks to see if the image to be displayed is of a different
640 * size to the last image displayed. If so, the old shm block must be
641 * destroyed and a new one created.
642 * Note: the "image size" is the size of the image to be passed to the Xv
643 * extension (which is probably different to the size of the output window).
644 *****************************************************************************/
645 static int XVideoUpdateImgSizeIfRequired( vout_thread_t *p_vout )
647 int i_img_width = p_vout->p_rendered_pic->i_width;
648 int i_img_height = p_vout->p_rendered_pic->i_height;
650 if( p_vout->p_sys->i_image_width != i_img_width
651 || p_vout->p_sys->i_image_height != i_img_height )
653 if( p_vout->p_sys->i_image_width != 0
654 && p_vout->p_sys->i_image_height != 0 )
656 /* Destroy XvImage to change its size */
660 p_vout->p_sys->i_image_width = i_img_width;
661 p_vout->p_sys->i_image_height = i_img_height;
663 /* Create XvImage using XShm extension */
664 if( XVideoCreateShmImage( p_vout->p_sys->p_display,
665 p_vout->p_sys->xv_port,
666 &p_vout->p_sys->p_xvimage,
667 &p_vout->p_sys->shm_info,
668 i_img_width, i_img_height ) )
670 intf_ErrMsg( "vout: failed to create xvimage." );
671 p_vout->p_sys->i_image_width = 0;
675 /* Set bytes per line and initialize buffers */
676 p_vout->i_bytes_per_line =
677 (p_vout->p_sys->p_xvimage->data_size) /
678 (p_vout->p_sys->p_xvimage->height);
680 /* p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_xvimage->data ); */
686 /*****************************************************************************
687 * XVideoCheckForXv: check for the XVideo extension
688 *****************************************************************************/
689 static int XVideoCheckForXv( Display *dpy )
693 switch( XvQueryExtension( dpy, &i, &i, &i, &i, &i ) )
699 intf_ErrMsg( "vout error: XvBadExtension" );
703 intf_ErrMsg( "vout error: XvBadAlloc" );
707 intf_ErrMsg( "vout error: XvQueryExtension failed" );
712 /*****************************************************************************
713 * XVideoCreateWindow: open and set-up XVideo main window
714 *****************************************************************************/
715 static int XVideoCreateWindow( vout_thread_t *p_vout )
717 XSizeHints xsize_hints;
718 XSetWindowAttributes xwindow_attributes;
725 boolean_t b_configure_notify;
726 boolean_t b_map_notify;
729 /* Set main window's size */
730 /* If we're full screen, we're full screen! */
731 if( p_vout->b_fullscreen )
733 p_vout->p_sys->i_window_width = DisplayWidth( p_vout->p_sys->p_display,
734 p_vout->p_sys->i_screen );
735 p_vout->p_sys->i_window_height = DisplayHeight( p_vout->p_sys->p_display,
736 p_vout->p_sys->i_screen );
737 /* p_vout->i_width = p_vout->p_sys->i_window_width;
738 p_vout->i_height = p_vout->p_sys->i_window_height; */
742 p_vout->p_sys->i_window_width = main_GetIntVariable( VOUT_WIDTH_VAR,
743 VOUT_WIDTH_DEFAULT );
744 p_vout->p_sys->i_window_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
745 VOUT_HEIGHT_DEFAULT );
748 /* Prepare window manager hints and properties */
749 xsize_hints.base_width = p_vout->p_sys->i_window_width;
750 xsize_hints.base_height = p_vout->p_sys->i_window_height;
751 xsize_hints.flags = PSize;
752 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
753 "WM_PROTOCOLS", True );
754 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
755 "WM_DELETE_WINDOW", True );
757 /* Prepare window attributes */
758 xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
759 p_vout->p_sys->i_screen );
761 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
763 /* Create the window and set hints - the window must receive ConfigureNotify
764 * events, and, until it is displayed, Expose and MapNotify events. */
765 p_vout->p_sys->window =
766 XCreateWindow( p_vout->p_sys->p_display,
767 DefaultRootWindow( p_vout->p_sys->p_display ),
769 p_vout->p_sys->i_window_width,
770 p_vout->p_sys->i_window_height, 1,
772 CWBackPixel | CWEventMask,
773 &xwindow_attributes );
775 if ( p_vout->b_fullscreen )
777 prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
778 mwmhints.flags = MWM_HINTS_DECORATIONS;
779 mwmhints.decorations = 0;
780 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
781 prop, prop, 32, PropModeReplace,
782 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
784 XSetTransientForHint( p_vout->p_sys->p_display,
785 p_vout->p_sys->window, None );
786 XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
790 /* Set window manager hints and properties: size hints, command,
791 * window's name, and accepted protocols */
792 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
794 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
795 p_main->ppsz_argv, p_main->i_argc );
796 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
797 VOUT_TITLE " (XVideo output)" );
799 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
800 || (p_vout->p_sys->wm_delete_window == None)
801 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
802 &p_vout->p_sys->wm_delete_window, 1 ) )
804 /* WM_DELETE_WINDOW is not supported by window manager */
805 intf_Msg( "vout error: missing or bad window manager" );
808 /* Creation of a graphic context that doesn't generate a GraphicsExpose
809 * event when using functions like XCopyArea */
810 xgcvalues.graphics_exposures = False;
811 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
812 p_vout->p_sys->window,
813 GCGraphicsExposures, &xgcvalues);
815 /* Send orders to server, and wait until window is displayed - three
816 * events must be received: a MapNotify event, an Expose event allowing
817 * drawing in the window, and a ConfigureNotify to get the window
818 * dimensions. Once those events have been received, only ConfigureNotify
819 * events need to be received. */
821 b_configure_notify = 0;
823 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
826 XNextEvent( p_vout->p_sys->p_display, &xevent);
827 if( (xevent.type == Expose)
828 && (xevent.xexpose.window == p_vout->p_sys->window) )
832 else if( (xevent.type == MapNotify)
833 && (xevent.xmap.window == p_vout->p_sys->window) )
837 else if( (xevent.type == ConfigureNotify)
838 && (xevent.xconfigure.window == p_vout->p_sys->window) )
840 b_configure_notify = 1;
841 p_vout->p_sys->i_window_width = xevent.xconfigure.width;
842 p_vout->p_sys->i_window_height = xevent.xconfigure.height;
844 } while( !( b_expose && b_configure_notify && b_map_notify ) );
846 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
847 StructureNotifyMask | KeyPressMask |
848 ButtonPressMask | ButtonReleaseMask |
851 if( p_vout->b_fullscreen )
853 XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
854 RevertToNone, CurrentTime );
855 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
858 /* Create YUV output sub-window. */
859 p_vout->p_sys->yuv_window=XCreateSimpleWindow( p_vout->p_sys->p_display,
860 p_vout->p_sys->window, 0, 0, 1, 1, 0,
861 BlackPixel( p_vout->p_sys->p_display,
862 p_vout->p_sys->i_screen ),
863 WhitePixel( p_vout->p_sys->p_display,
864 p_vout->p_sys->i_screen ) );
866 p_vout->p_sys->yuv_gc = XCreateGC( p_vout->p_sys->p_display,
867 p_vout->p_sys->yuv_window,
868 GCGraphicsExposures, &xgcvalues );
870 XSetWindowBackground( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
871 BlackPixel(p_vout->p_sys->p_display, p_vout->p_sys->i_screen ) );
873 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
874 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
878 XVideoSetMousePointer( p_vout );
883 static void XVideoDestroyWindow( vout_thread_t *p_vout )
885 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->yuv_gc );
886 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
888 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
889 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
890 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
893 /*****************************************************************************
894 * XVideoCreateShmImage: create an XvImage using shared memory extension
895 *****************************************************************************
896 * Prepare an XvImage for display function.
897 * The order of the operations respects the recommandations of the mit-shm
898 * document by J.Corbet and K.Packard. Most of the parameters were copied from
900 *****************************************************************************/
901 static int XVideoCreateShmImage( Display* dpy, int xv_port,
902 XvImage **pp_xvimage,
903 XShmSegmentInfo *p_shm_info,
904 int i_width, int i_height )
906 *pp_xvimage = XvShmCreateImage( dpy, xv_port,
907 GUID_YUV12_PLANAR, 0,
912 intf_ErrMsg( "vout error: XvShmCreateImage failed." );
916 p_shm_info->shmid = shmget( IPC_PRIVATE, (*pp_xvimage)->data_size,
918 if( p_shm_info->shmid < 0) /* error */
920 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
925 p_shm_info->shmaddr = (*pp_xvimage)->data = shmat( p_shm_info->shmid,
927 p_shm_info->readOnly = False;
930 /* Mark the shm segment to be removed when there will be no more
931 * attachements, so it is automatic on process exit or after shmdt */
932 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
935 if( !XShmAttach( dpy, p_shm_info ) )
937 intf_ErrMsg( "vout error: XShmAttach failed" );
938 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
939 shmdt( p_shm_info->shmaddr );
943 /* Send image to X server. This instruction is required, since having
944 * built a Shm XImage and not using it causes an error on XCloseDisplay */
950 /*****************************************************************************
951 * XVideoDestroyShmImage
952 *****************************************************************************
953 * Destroy XImage AND associated data. Detach shared memory segment from
954 * server and process, then free it. If pointer is NULL, the image won't be
955 * destroyed (see vout_ManageOutputMethod())
956 *****************************************************************************/
957 static void XVideoDestroyShmImage( vout_thread_t *p_vout, XvImage *p_xvimage,
958 XShmSegmentInfo *p_shm_info )
960 /* If pointer is NULL, do nothing */
961 if( p_xvimage == NULL )
966 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
968 XDestroyImage( p_ximage ); /* XXX */
971 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
973 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
975 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
980 /*****************************************************************************
981 * XVideoEnableScreenSaver: enable screen saver
982 *****************************************************************************
983 * This function enable the screen saver on a display after it had been
984 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
985 * know wether the screen saver can be activated or not: if n successive calls
986 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
987 * will be required before the screen saver could effectively be activated.
988 *****************************************************************************/
989 void XVideoEnableScreenSaver( vout_thread_t *p_vout )
991 intf_DbgMsg( "intf: enabling screen saver" );
992 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
993 p_vout->p_sys->i_ss_interval,
994 p_vout->p_sys->i_ss_blanking,
995 p_vout->p_sys->i_ss_exposure );
998 /*****************************************************************************
999 * XVideoDisableScreenSaver: disable screen saver
1000 *****************************************************************************
1001 * See XEnableScreenSaver
1002 *****************************************************************************/
1003 void XVideoDisableScreenSaver( vout_thread_t *p_vout )
1005 /* Save screen saver informations */
1006 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1007 &p_vout->p_sys->i_ss_interval,
1008 &p_vout->p_sys->i_ss_blanking,
1009 &p_vout->p_sys->i_ss_exposure );
1011 /* Disable screen saver */
1012 intf_DbgMsg( "intf: disabling screen saver" );
1013 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1014 p_vout->p_sys->i_ss_interval,
1015 p_vout->p_sys->i_ss_blanking,
1016 p_vout->p_sys->i_ss_exposure );
1019 /*****************************************************************************
1020 * XVideoSetMousePointer: hide or show the mouse pointer
1021 *****************************************************************************
1022 * This function hides the X pointer if requested.
1023 *****************************************************************************/
1024 void XVideoSetMousePointer( const vout_thread_t *p_vout )
1026 static Cursor blank_cursor;
1027 static boolean_t b_created_blank_cursor = 0;
1029 if( !p_vout->p_sys->b_mouse_pointer_visible )
1031 if( !b_created_blank_cursor )
1034 Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1035 DefaultRootWindow(p_vout->p_sys->p_display),
1038 XParseColor( p_vout->p_sys->p_display,
1039 XCreateColormap( p_vout->p_sys->p_display,
1041 p_vout->p_sys->p_display ),
1043 p_vout->p_sys->p_display,
1044 p_vout->p_sys->i_screen ),
1048 blank_cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1049 blank, blank, &color, &color, 1, 1 );
1051 b_created_blank_cursor = 1;
1053 XDefineCursor( p_vout->p_sys->p_display,
1054 p_vout->p_sys->window, blank_cursor );
1058 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );
1062 /* This based on some code in SetBufferPicture... At the moment it's only
1063 * used by the xvideo plugin, but others may want to use it. */
1064 static void XVideoOutputCoords( const picture_t *p_pic, const boolean_t scale,
1065 const int win_w, const int win_h,
1066 int *dx, int *dy, int *w, int *h)
1070 *w = p_pic->i_width; *h = p_pic->i_height;
1075 switch( p_pic->i_aspect_ratio )
1077 case AR_3_4_PICTURE:
1081 case AR_16_9_PICTURE:
1082 *h = win_w * 9 / 16;
1085 case AR_221_1_PICTURE:
1086 *h = win_w * 100 / 221;
1089 case AR_SQUARE_PICTURE:
1091 *h = win_w * p_pic->i_height / p_pic->i_width;
1098 switch( p_pic->i_aspect_ratio )
1100 case AR_3_4_PICTURE:
1104 case AR_16_9_PICTURE:
1105 *w = win_h * 16 / 9;
1108 case AR_221_1_PICTURE:
1109 *w = win_h * 221 / 100;
1112 case AR_SQUARE_PICTURE:
1114 *w = win_h * p_pic->i_width / p_pic->i_height;
1120 /* Set picture position */
1121 *dx = (win_w - *w) / 2;
1122 *dy = (win_h - *h) / 2;
1126 static int XVideoGetPort( Display *dpy )
1130 XvAdaptorInfo *adaptor_info;
1132 switch( XvQueryAdaptors( dpy, DefaultRootWindow( dpy ),
1133 &i_adaptors, &adaptor_info ) )
1138 case XvBadExtension:
1139 intf_ErrMsg( "vout error: XvBadExtension for XvQueryAdaptors" );
1143 intf_ErrMsg( "vout error: XvBadAlloc for XvQueryAdaptors" );
1147 intf_ErrMsg( "vout error: XvQueryAdaptors failed" );
1151 for( i=0; i < i_adaptors && xv_port == -1; ++i )
1153 if( ( adaptor_info[ i ].type & XvInputMask ) &&
1154 ( adaptor_info[ i ].type & XvImageMask ) )
1156 /* check that port supports YUV12 planar format... */
1157 int port = adaptor_info[ i ].base_id;
1158 int i_num_formats, i;
1159 XvImageFormatValues *imageFormats;
1161 imageFormats = XvListImageFormats( dpy, port, &i_num_formats );
1163 for( i=0; i < i_num_formats && xv_port == -1; ++i )
1165 if( imageFormats[ i ].id == GUID_YUV12_PLANAR )
1173 intf_WarnMsg( 3, "vout: XVideo image input port %d "
1174 "does not support the YUV12 planar format which is "
1175 "currently required by the xvideo output plugin",
1181 XFree( imageFormats );
1186 if( i_adaptors > 0 )
1188 XvFreeAdaptorInfo(adaptor_info);
1193 intf_ErrMsg( "vout error: no suitable Xvideo image input port" );
1200 /*****************************************************************************
1201 * XVideoDisplay: display image
1202 *****************************************************************************
1203 * This function displays the image stored in p_vout->p_sys->p_xvimage.
1204 * The image is scaled to fit in the output window (and to have the correct
1206 *****************************************************************************/
1207 static void XVideoDisplay( vout_thread_t *p_vout )
1209 int i_dest_width, i_dest_height, i_dest_x, i_dest_y;
1211 if( !p_vout->p_sys->p_xvimage )
1216 XVideoOutputCoords( p_vout->p_rendered_pic, p_vout->b_scale,
1217 p_vout->p_sys->i_window_width,
1218 p_vout->p_sys->i_window_height,
1219 &i_dest_x, &i_dest_y,
1220 &i_dest_width, &i_dest_height);
1222 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->xv_port,
1223 p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
1224 p_vout->p_sys->p_xvimage,
1225 0 /*src_x*/, 0 /*src_y*/,
1226 p_vout->p_rendered_pic->i_width,
1227 p_vout->p_rendered_pic->i_height,
1228 0 /*dest_x*/, 0 /*dest_y*/, i_dest_width, i_dest_height,
1231 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1232 i_dest_width, i_dest_height );
1233 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1234 i_dest_x, i_dest_y );
1236 /* Send the order to the X server */
1237 XSync( p_vout->p_sys->p_display, False );
1241 /*****************************************************************************
1242 * XVideoSetAttribute
1243 *****************************************************************************
1244 * This function can be used to set attributes, e.g. XV_BRIGHTNESS and
1245 * XV_CONTRAST. "f_value" should be in the range of 0 to 1.
1246 *****************************************************************************/
1247 static void XVideoSetAttribute( vout_thread_t *p_vout,
1248 char *attr_name, float f_value )
1251 XvAttribute *p_attrib;
1252 Display *p_dpy = p_vout->p_sys->p_display;
1253 int xv_port = p_vout->p_sys->xv_port;
1255 p_attrib = XvQueryPortAttributes( p_dpy, xv_port, &i_attrib );
1261 if( i_attrib >= 0 && !strcmp( p_attrib[ i_attrib ].name, attr_name ) )
1263 int i_sv = f_value * ( p_attrib[ i_attrib ].max_value
1264 - p_attrib[ i_attrib ].min_value + 1 )
1265 + p_attrib[ i_attrib ].min_value;
1267 XvSetPortAttribute( p_dpy, xv_port,
1268 XInternAtom( p_dpy, attr_name, False ), i_sv );
1272 } while( i_attrib > 0 );