]> git.sesse.net Git - vlc/blob - plugins/x11/vout_xvideo.c
dafaa437680f4210a5fcf1fc05f15fafea983e56
[vlc] / plugins / x11 / vout_xvideo.c
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.30 2001/11/07 02:10:14 stef Exp $
6  *
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>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 #define MODULE_NAME xvideo
28 #include "modules_inner.h"
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include "defs.h"
34
35 #include <errno.h>                                                 /* ENOMEM */
36 #include <stdlib.h>                                                /* free() */
37 #include <string.h>                                            /* strerror() */
38
39 #ifdef HAVE_MACHINE_PARAM_H
40 /* BSD */
41 #include <machine/param.h>
42 #include <sys/types.h>                                     /* typedef ushort */
43 #include <sys/ipc.h>
44 #endif
45
46 #ifndef WIN32
47 #include <netinet/in.h>                               /* BSD: struct in_addr */
48 #endif
49
50 #include <sys/shm.h>                                   /* shmget(), shmctl() */
51 #include <X11/Xlib.h>
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>
57 #include <X11/extensions/dpms.h>
58
59 #include "config.h"
60 #include "common.h"
61 #include "threads.h"
62 #include "mtime.h"
63 #include "tests.h"
64
65 #include "video.h"
66 #include "video_output.h"
67
68 #include "interface.h"
69 #include "intf_msg.h"
70
71 #include "netutils.h"                                 /* network_ChannelJoin */
72
73 #include "main.h"
74
75 #include "stream_control.h"                 /* needed by input_ext-intf.h... */
76 #include "input_ext-intf.h"
77
78 #include "modules.h"
79 #include "modules_export.h"
80
81 #define GUID_YUV12_PLANAR 0x32315659
82
83
84 /*****************************************************************************
85  * vout_sys_t: video output X11 method descriptor
86  *****************************************************************************
87  * This structure is part of the video output thread descriptor.
88  * It describes the XVideo specific properties of an output thread.
89  *****************************************************************************/
90 typedef struct vout_sys_s
91 {
92     /* User settings */
93 #if 0
94     /* this plugin (currently) requires the SHM Ext... */
95     boolean_t           b_shm;               /* shared memory extension flag */
96 #endif
97
98     /* Internal settings and properties */
99     Display *           p_display;                        /* display pointer */
100     int                 i_screen;                           /* screen number */
101     Window              window;                               /* root window */
102     GC                  gc;              /* graphic context instance handler */
103     Window              yuv_window;   /* sub-window for displaying yuv video
104                                                                         data */
105     GC                  yuv_gc;
106     int                 xv_port;
107
108     /* Display buffers and shared memory information */
109     /* Note: only 1 buffer... Xv ext does double buffering. */
110     XvImage *           p_xvimage;
111     int                 i_image_width;
112     int                 i_image_height;
113                                 /* i_image_width & i_image_height reflect the
114                                  * size of the XvImage. They are used by
115                                  * vout_Display() to check if the image to be
116                                  * displayed can use the current XvImage. */
117     XShmSegmentInfo     shm_info;       /* shared memory zone information */
118
119     /* X11 generic properties */
120     Atom                wm_protocols;
121     Atom                wm_delete_window;
122
123     int                 i_window_width;              /* width of main window */
124     int                 i_window_height;            /* height of main window */
125
126
127     /* Screen saver properties */
128     int                 i_ss_timeout;                             /* timeout */
129     int                 i_ss_interval;           /* interval between changes */
130     int                 i_ss_blanking;                      /* blanking mode */
131     int                 i_ss_exposure;                      /* exposure mode */
132     
133     /* Mouse pointer properties */
134     boolean_t           b_mouse_pointer_visible;
135     mtime_t             i_time_mouse_last_moved; /* used to auto-hide pointer*/
136     Cursor              blank_cursor;                   /* the hidden cursor */
137     Pixmap              cursor_pixmap;
138
139 } vout_sys_t;
140
141 /* Fullscreen needs to be able to hide the wm decorations */
142 #define MWM_HINTS_DECORATIONS   (1L << 1)
143 #define PROP_MWM_HINTS_ELEMENTS 5
144 typedef struct mwmhints_s
145 {
146     u32 flags;
147     u32 functions;
148     u32 decorations;
149     s32 input_mode;
150     u32 status;
151 } mwmhints_t;
152
153 /*****************************************************************************
154  * Local prototypes
155  *****************************************************************************/
156 static int  vout_Probe     ( probedata_t * );
157 static int  vout_Create    ( vout_thread_t * );
158 static int  vout_Init      ( vout_thread_t * );
159 static void vout_End       ( vout_thread_t * );
160 static void vout_Destroy   ( vout_thread_t * );
161 static int  vout_Manage    ( vout_thread_t * );
162 static void vout_Display   ( vout_thread_t * );
163 static void vout_SetPalette( vout_thread_t *, u16 *, u16 *, u16 *, u16 * );
164
165 static int  XVideoCreateWindow       ( vout_thread_t * );
166 static void XVideoDestroyWindow      ( vout_thread_t *p_vout );
167 static int  XVideoUpdateImgSizeIfRequired( vout_thread_t *p_vout );
168 static int  XVideoCreateShmImage     ( Display* dpy, int xv_port,
169                                        XvImage **pp_xvimage,
170                                        XShmSegmentInfo *p_shm_info,
171                                        int i_width, int i_height );
172 static void XVideoDestroyShmImage    ( vout_thread_t *, XvImage *,
173                                        XShmSegmentInfo * );
174 static void X11ToggleMousePointer    ( vout_thread_t * );
175 static void XVideoEnableScreenSaver  ( vout_thread_t * );
176 static void XVideoDisableScreenSaver ( vout_thread_t * );
177 /*static void XVideoSetAttribute       ( vout_thread_t *, char *, float );*/
178
179 static int  XVideoCheckForXv         ( Display * );
180 static int  XVideoGetPort            ( Display * );
181 static void XVideoOutputCoords       ( const picture_t *, const boolean_t,
182                                        const int, const int,
183                                        int *, int *, int *, int * );
184 static void XVideoDisplay            ( vout_thread_t * );
185
186 /*****************************************************************************
187  * Functions exported as capabilities. They are declared as static so that
188  * we don't pollute the namespace too much.
189  *****************************************************************************/
190 void _M( vout_getfunctions )( function_list_t * p_function_list )
191 {
192     p_function_list->pf_probe = vout_Probe;
193     p_function_list->functions.vout.pf_create     = vout_Create;
194     p_function_list->functions.vout.pf_init       = vout_Init;
195     p_function_list->functions.vout.pf_end        = vout_End;
196     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
197     p_function_list->functions.vout.pf_manage     = vout_Manage;
198     p_function_list->functions.vout.pf_display    = vout_Display;
199     p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
200 }
201
202 /*****************************************************************************
203  * vout_Probe: probe the video driver and return a score
204  *****************************************************************************
205  * This returns a score to the plugin manager so that it can select the best
206  * plugin.
207  *****************************************************************************/
208 static int vout_Probe( probedata_t *p_data )
209 {
210     Display *p_display;                                   /* display pointer */
211     char    *psz_display;
212
213     /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
214     psz_display = XDisplayName( main_GetPszVariable(VOUT_DISPLAY_VAR, NULL) );
215     p_display = XOpenDisplay( psz_display );
216     if( p_display == NULL )                                         /* error */
217     {
218         intf_WarnMsg( 3, "vout: Xvideo cannot open display %s", psz_display );
219         intf_WarnMsg( 3, "vout: Xvideo not supported" );
220         return( 0 );
221     }
222     
223     if( !XVideoCheckForXv( p_display ) )
224     {
225         intf_WarnMsg( 3, "vout: Xvideo not supported" );
226         XCloseDisplay( p_display );
227         return( 0 );
228     }
229
230     if( XVideoGetPort( p_display ) < 0 )
231     {
232         intf_WarnMsg( 3, "vout: Xvideo not supported" );
233         XCloseDisplay( p_display );
234         return( 0 );
235     }
236
237     /* Clean-up everyting */
238     XCloseDisplay( p_display );
239
240     if( TestMethod( VOUT_METHOD_VAR, "xvideo" ) )
241     {
242         return( 999 );
243     }
244
245     return( 150 );
246 }
247
248 /*****************************************************************************
249  * vout_Create: allocate XVideo video thread output method
250  *****************************************************************************
251  * This function allocate and initialize a XVideo vout method. It uses some of
252  * the vout properties to choose the window size, and change them according to
253  * the actual properties of the display.
254  *****************************************************************************/
255 static int vout_Create( vout_thread_t *p_vout )
256 {
257     char *psz_display;
258     XColor cursor_color;
259
260     /* Allocate structure */
261     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
262     if( p_vout->p_sys == NULL )
263     {
264         intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
265         return( 1 );
266     }
267
268     /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
269     psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
270     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
271
272     if( p_vout->p_sys->p_display == NULL )                          /* error */
273     {
274         intf_ErrMsg( "vout error: cannot open display %s", psz_display );
275         free( p_vout->p_sys );
276         return( 1 );
277     }
278     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
279
280     p_vout->b_fullscreen
281         = main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT );
282     
283     if( !XVideoCheckForXv( p_vout->p_sys->p_display ) )
284     {
285         intf_ErrMsg( "vout error: no XVideo extension" );
286         XCloseDisplay( p_vout->p_sys->p_display );
287         free( p_vout->p_sys );
288         return( 1 );
289     }
290
291     /* Check we have access to a video port */
292     if( (p_vout->p_sys->xv_port = XVideoGetPort(p_vout->p_sys->p_display)) <0 )
293     {
294         intf_ErrMsg( "vout error: cannot get XVideo port" );
295         XCloseDisplay( p_vout->p_sys->p_display );
296         free( p_vout->p_sys );
297         return 1;
298     }
299     intf_DbgMsg( "Using xv port %d" , p_vout->p_sys->xv_port );
300
301     /* Create blank cursor (for mouse cursor autohiding) */
302     p_vout->p_sys->b_mouse_pointer_visible = 1;
303     p_vout->p_sys->cursor_pixmap = XCreatePixmap( p_vout->p_sys->p_display,
304                                                   DefaultRootWindow(
305                                                      p_vout->p_sys->p_display),
306                                                   1, 1, 1 );
307     
308     XParseColor( p_vout->p_sys->p_display,
309                  XCreateColormap( p_vout->p_sys->p_display,
310                                   DefaultRootWindow(
311                                                     p_vout->p_sys->p_display ),
312                                   DefaultVisual(
313                                                 p_vout->p_sys->p_display,
314                                                 p_vout->p_sys->i_screen ),
315                                   AllocNone ),
316                  "black", &cursor_color );
317     
318     p_vout->p_sys->blank_cursor = XCreatePixmapCursor(
319                                       p_vout->p_sys->p_display,
320                                       p_vout->p_sys->cursor_pixmap,
321                                       p_vout->p_sys->cursor_pixmap,
322                                       &cursor_color,
323                                       &cursor_color, 1, 1 );    
324
325     /* Spawn base window - this window will include the video output window,
326      * but also command buttons, subtitles and other indicators */
327     if( XVideoCreateWindow( p_vout ) )
328     {
329         intf_ErrMsg( "vout error: cannot create XVideo window" );
330         XCloseDisplay( p_vout->p_sys->p_display );
331         free( p_vout->p_sys );
332         return( 1 );
333     }
334
335     /* p_vout->pf_setbuffers( p_vout, NULL, NULL ); */
336
337 #if 0
338     /* XXX The brightness and contrast values should be read from environment
339      * XXX variables... */
340     XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
341     XVideoSetAttribute( p_vout, "XV_CONTRAST",   0.5 );
342 #endif
343
344     /* Disable screen saver and return */
345     XVideoDisableScreenSaver( p_vout );
346
347     return( 0 );
348 }
349
350 /*****************************************************************************
351  * vout_Init: initialize XVideo video thread output method
352  *****************************************************************************/
353 static int vout_Init( vout_thread_t *p_vout )
354 {
355 #ifdef SYS_DARWIN
356     /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
357     p_vout->p_sys->b_shm = 0;
358 #endif
359     p_vout->b_need_render = 0;
360     p_vout->p_sys->i_image_width = p_vout->p_sys->i_image_height = 0;
361
362     return( 0 );
363 }
364
365 /*****************************************************************************
366  * vout_End: terminate XVideo video thread output method
367  *****************************************************************************
368  * Destroy the XvImage. It is called at the end of the thread, but also each
369  * time the image is resized.
370  *****************************************************************************/
371 static void vout_End( vout_thread_t *p_vout )
372 {
373     XVideoDestroyShmImage( p_vout, p_vout->p_sys->p_xvimage,
374                            &p_vout->p_sys->shm_info );
375 }
376
377 /*****************************************************************************
378  * vout_Destroy: destroy XVideo video thread output method
379  *****************************************************************************
380  * Terminate an output method created by vout_Create
381  *****************************************************************************/
382 static void vout_Destroy( vout_thread_t *p_vout )
383 {
384     /* Restore cursor if it was blanked */
385     if( !p_vout->p_sys->b_mouse_pointer_visible )
386         X11ToggleMousePointer( p_vout );
387
388     /* Destroy blank cursor pixmap */
389     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
390
391     XVideoEnableScreenSaver( p_vout );
392     XVideoDestroyWindow( p_vout );
393     XCloseDisplay( p_vout->p_sys->p_display );
394
395     /* Destroy structure */
396     free( p_vout->p_sys );
397 }
398
399 /*****************************************************************************
400  * vout_Manage: handle X11 events
401  *****************************************************************************
402  * This function should be called regularly by video output thread. It manages
403  * X11 events and allows window resizing. It returns a non null value on
404  * error.
405  *
406  * XXX  Should "factor-out" common code in this and the "same" fn in the x11
407  * XXX  plugin!
408  *****************************************************************************/
409 static int vout_Manage( vout_thread_t *p_vout )
410 {
411     XEvent      xevent;                                         /* X11 event */
412     char        i_key;                                    /* ISO Latin-1 key */
413     KeySym      x_key_symbol;
414
415     /* Handle X11 events: ConfigureNotify events are parsed to know if the
416      * output window's size changed, MapNotify and UnmapNotify to know if the
417      * window is mapped (and if the display is useful), and ClientMessages
418      * to intercept window destruction requests */
419     while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
420                               StructureNotifyMask | KeyPressMask |
421                               ButtonPressMask | ButtonReleaseMask | 
422                               PointerMotionMask, &xevent )
423            == True )
424     {
425         /* ConfigureNotify event: prepare  */
426         if( (xevent.type == ConfigureNotify)
427             /*&& ((xevent.xconfigure.width != p_vout->p_sys->i_window_width)
428                 || (xevent.xconfigure.height != p_vout->p_sys->i_window_height))*/ )
429         {
430             /* Update dimensions */
431             p_vout->p_sys->i_window_width = xevent.xconfigure.width;
432             p_vout->p_sys->i_window_height = xevent.xconfigure.height;
433 //            p_vout->i_changes |= VOUT_SIZE_CHANGE;
434         }
435         /* MapNotify event: change window status and disable screen saver */
436         else if( xevent.type == MapNotify)
437         {
438             if( (p_vout != NULL) && !p_vout->b_active )
439             {
440                 XVideoDisableScreenSaver( p_vout );
441                 p_vout->b_active = 1;
442             }
443         }
444         /* UnmapNotify event: change window status and enable screen saver */
445         else if( xevent.type == UnmapNotify )
446         {
447             if( (p_vout != NULL) && p_vout->b_active )
448             {
449                 XVideoEnableScreenSaver( p_vout );
450                 p_vout->b_active = 0;
451             }
452         }
453         /* Keyboard event */
454         else if( xevent.type == KeyPress )
455         {
456             /* We may have keys like F1 trough F12, ESC ... */
457             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
458                                              xevent.xkey.keycode, 0 );
459             switch( x_key_symbol )
460             {
461                  case XK_Escape:
462                      p_main->p_intf->b_die = 1;
463                      break;
464                  case XK_Menu:
465                      p_main->p_intf->b_menu_change = 1;
466                      break;
467                  default:
468                      /* "Normal Keys"
469                       * The reason why I use this instead of XK_0 is that 
470                       * with XLookupString, we don't have to care about
471                       * keymaps. */
472
473                     if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
474                     {
475                         switch( i_key )
476                         {
477                         case 'q':
478                         case 'Q':
479                             p_main->p_intf->b_die = 1;
480                             break;
481                         case 'f':
482                         case 'F':
483                             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
484                             break;
485                         case '0':
486                             network_ChannelJoin( 0 );
487                             break;
488                         case '1':
489                             network_ChannelJoin( 1 );
490                             break;
491                         case '2':
492                             network_ChannelJoin( 2 );
493                             break;
494                         case '3':
495                             network_ChannelJoin( 3 );
496                             break;
497                         case '4':
498                             network_ChannelJoin( 4 );
499                             break;
500                         case '5':
501                             network_ChannelJoin( 5 );
502                             break;
503                         case '6':
504                             network_ChannelJoin( 6 );
505                             break;
506                         case '7':
507                             network_ChannelJoin( 7 );
508                             break;
509                         case '8':
510                             network_ChannelJoin( 8 );
511                             break;
512                         case '9':
513                             network_ChannelJoin( 9 );
514                             break;
515                         default:
516                             if( intf_ProcessKey( p_main->p_intf, 
517                                                  (char )i_key ) )
518                             {
519                                intf_DbgMsg( "unhandled key '%c' (%i)", 
520                                             (char)i_key, i_key );
521                             }
522                             break;
523                         }
524                     }
525                 break;
526             }
527         }
528         /* Mouse click */
529         else if( xevent.type == ButtonPress )
530         {
531             switch( ((XButtonEvent *)&xevent)->button )
532             {
533                 case Button1:
534                     /* in this part we will eventually manage
535                      * clicks for DVD navigation for instance */
536                     break;
537             }
538         }
539         /* Mouse release */
540         else if( xevent.type == ButtonRelease )
541         {
542             switch( ((XButtonEvent *)&xevent)->button )
543             {
544                 case Button3:
545                     /* FIXME: need locking ! */
546                     p_main->p_intf->b_menu_change = 1;
547                     break;
548             }
549         }
550         /* Mouse move */
551         else if( xevent.type == MotionNotify )
552         {
553             p_vout->p_sys->i_time_mouse_last_moved = mdate();
554             if( !p_vout->p_sys->b_mouse_pointer_visible )
555                 X11ToggleMousePointer( p_vout ); 
556         }
557         /* Other event */
558         else
559         {
560             intf_WarnMsg( 3, "%p -> unhandled event type %d received",
561                          p_vout, xevent.type );
562         }
563     }
564
565     /* Handle events for YUV video output sub-window */
566     while( XCheckWindowEvent( p_vout->p_sys->p_display,
567                               p_vout->p_sys->yuv_window,
568                               ExposureMask, &xevent ) == True )
569     {
570         /* Window exposed (only handled if stream playback is paused) */
571         if( xevent.type == Expose )
572         {
573             if( ((XExposeEvent *)&xevent)->count == 0 )
574                 /* (if this is the last a collection of expose events...) */
575                 if( p_main->p_intf->p_input )
576                     if( PAUSE_S ==
577                             p_main->p_intf->p_input->stream.control.i_status )
578                         XVideoDisplay( p_vout );
579         }
580     }
581         
582     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
583      * are handled - according to the man pages, the format is always 32
584      * in this case */
585     while( XCheckTypedEvent( p_vout->p_sys->p_display,
586                              ClientMessage, &xevent ) )
587     {
588         if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
589             && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
590         {
591             p_main->p_intf->b_die = 1;
592         }
593         else
594         {
595             intf_DbgMsg( "%p -> unhandled ClientMessage received", p_vout );
596         }
597     }
598
599     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
600     {
601         intf_DbgMsg( "vout: changing full-screen status" );
602
603         p_vout->b_fullscreen = !p_vout->b_fullscreen;
604
605         /* Get rid of the old window */
606         XVideoDestroyWindow( p_vout );
607
608         /* And create a new one */
609         if( XVideoCreateWindow( p_vout ) )
610         {
611             intf_ErrMsg( "vout error: cannot create X11 window" );
612             XCloseDisplay( p_vout->p_sys->p_display );
613
614             free( p_vout->p_sys );
615             return( 1 );
616         }
617     }
618
619     
620     if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE))
621     {
622         /* FIXME: clear flags ?? */
623     }
624
625     /*
626      * Size change
627      */
628     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
629     {
630         intf_DbgMsg( "vout: resizing window" );
631         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
632
633         p_vout->i_width = p_vout->p_sys->i_window_width;
634         p_vout->i_height = p_vout->p_sys->i_window_height;
635
636         intf_WarnMsg( 3, "vout: video display resized (%dx%d)",
637                       p_vout->i_width, p_vout->i_height );
638     }
639
640     /* Autohide Cursor */
641     if( p_vout->p_sys->b_mouse_pointer_visible &&
642         mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
643     {
644         X11ToggleMousePointer( p_vout );
645     }
646     
647     return 0;
648 }
649
650 /*****************************************************************************
651  * vout_Display: displays previously rendered output
652  *****************************************************************************
653  * This function sends the currently rendered image to X11 server.
654  * (The Xv extension takes care of "double-buffering".)
655  *****************************************************************************/
656 static void vout_Display( vout_thread_t *p_vout )
657 {
658     boolean_t b_draw = 1;
659     int i_size = p_vout->p_rendered_pic->i_width *
660                    p_vout->p_rendered_pic->i_height;
661
662     if( XVideoUpdateImgSizeIfRequired( p_vout ) )
663     {
664         return;
665     }
666
667     switch( p_vout->p_rendered_pic->i_type )
668     {
669     case YUV_422_PICTURE:
670         intf_ErrMsg( "vout error: YUV_422_PICTURE not (yet) supported" );
671         b_draw = 0;
672         break;
673
674     case YUV_444_PICTURE:
675         intf_ErrMsg( "vout error: YUV_444_PICTURE not (yet) supported" );
676         b_draw = 0;
677         break;
678
679     case YUV_420_PICTURE:
680         memcpy( p_vout->p_sys->p_xvimage->data,
681                 p_vout->p_rendered_pic->p_y, i_size );
682         memcpy( p_vout->p_sys->p_xvimage->data + ( i_size ),
683                 p_vout->p_rendered_pic->p_v, i_size / 4 );
684         memcpy( p_vout->p_sys->p_xvimage->data + ( i_size ) + ( i_size / 4 ),
685                 p_vout->p_rendered_pic->p_u, i_size / 4 );
686         break;
687     }
688
689     if( b_draw )
690     {
691         XVideoDisplay( p_vout );
692     }
693 }
694
695 static void vout_SetPalette( p_vout_thread_t p_vout,
696                              u16 *red, u16 *green, u16 *blue, u16 *transp )
697 {
698     return;
699 }
700
701 /* following functions are local */
702
703 /*****************************************************************************
704  * XVideoUpdateImgSizeIfRequired 
705  *****************************************************************************
706  * This function checks to see if the image to be displayed is of a different
707  * size to the last image displayed. If so, the old shm block must be
708  * destroyed and a new one created.
709  * Note: the "image size" is the size of the image to be passed to the Xv
710  * extension (which is probably different to the size of the output window).
711  *****************************************************************************/
712 static int XVideoUpdateImgSizeIfRequired( vout_thread_t *p_vout )
713 {
714     int i_img_width         = p_vout->p_rendered_pic->i_width;
715     int i_img_height        = p_vout->p_rendered_pic->i_height;
716
717     if( p_vout->p_sys->i_image_width != i_img_width
718             || p_vout->p_sys->i_image_height != i_img_height )
719     {
720         if( p_vout->p_sys->i_image_width != 0
721              && p_vout->p_sys->i_image_height != 0 )
722         {
723             /* Destroy XvImage to change its size */
724             vout_End( p_vout );
725         }
726
727         p_vout->p_sys->i_image_width  = i_img_width;
728         p_vout->p_sys->i_image_height = i_img_height;
729
730         /* Create XvImage using XShm extension */
731         if( XVideoCreateShmImage( p_vout->p_sys->p_display,
732                                   p_vout->p_sys->xv_port,
733                                   &p_vout->p_sys->p_xvimage,
734                                   &p_vout->p_sys->shm_info,
735                                   i_img_width, i_img_height ) )
736         {
737             intf_ErrMsg( "vout: failed to create xvimage." );
738             p_vout->p_sys->i_image_width = 0;
739             return( 1 );
740         }
741
742         /* Set bytes per line and initialize buffers */
743         p_vout->i_bytes_per_line =
744             (p_vout->p_sys->p_xvimage->data_size) /
745             (p_vout->p_sys->p_xvimage->height);
746
747     }
748
749     return( 0 );
750 }
751
752 /*****************************************************************************
753  * XVideoCheckForXv: check for the XVideo extension
754  *****************************************************************************/
755 static int XVideoCheckForXv( Display *dpy )
756 {
757     unsigned int i;
758
759     switch( XvQueryExtension( dpy, &i, &i, &i, &i, &i ) )
760     {
761         case Success:
762             return( 1 );
763
764         case XvBadExtension:
765             intf_WarnMsg( 3, "vout error: XvBadExtension" );
766             return( 0 );
767
768         case XvBadAlloc:
769             intf_WarnMsg( 3, "vout error: XvBadAlloc" );
770             return( 0 );
771
772         default:
773             intf_WarnMsg( 3, "vout error: XvQueryExtension failed" );
774             return( 0 );
775     }
776 }
777
778 /*****************************************************************************
779  * XVideoCreateWindow: open and set-up XVideo main window
780  *****************************************************************************/
781 static int XVideoCreateWindow( vout_thread_t *p_vout )
782 {
783     XSizeHints              xsize_hints;
784     XSetWindowAttributes    xwindow_attributes;
785     XGCValues               xgcvalues;
786     XEvent                  xevent;
787     Atom                    prop;
788     mwmhints_t              mwmhints;
789     
790     boolean_t               b_expose;
791     boolean_t               b_configure_notify;
792     boolean_t               b_map_notify;
793
794
795     /* Set main window's size */
796     /* If we're full screen, we're full screen! */
797     if( p_vout->b_fullscreen )
798     {
799         p_vout->p_sys->i_window_width = DisplayWidth( p_vout->p_sys->p_display,
800                                                       p_vout->p_sys->i_screen );
801         p_vout->p_sys->i_window_height =  DisplayHeight( p_vout->p_sys->p_display,
802                                                          p_vout->p_sys->i_screen );
803     }
804     else
805     {
806         p_vout->p_sys->i_window_width =  p_vout->i_width;
807         p_vout->p_sys->i_window_height = p_vout->i_height;
808     }
809
810     /* Prepare window manager hints and properties */
811     xsize_hints.base_width          = p_vout->p_sys->i_window_width;
812     xsize_hints.base_height         = p_vout->p_sys->i_window_height;
813     xsize_hints.flags               = PSize;
814     p_vout->p_sys->wm_protocols     = XInternAtom( p_vout->p_sys->p_display,
815                                                    "WM_PROTOCOLS", True );
816     p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
817                                                    "WM_DELETE_WINDOW", True );
818
819     /* Prepare window attributes */
820     xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
821                                                       p_vout->p_sys->i_screen );
822
823     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
824
825     /* Create the window and set hints - the window must receive ConfigureNotify
826      * events, and, until it is displayed, Expose and MapNotify events. */
827     p_vout->p_sys->window =
828             XCreateWindow( p_vout->p_sys->p_display,
829                            DefaultRootWindow( p_vout->p_sys->p_display ),
830                            0, 0,
831                            p_vout->p_sys->i_window_width,
832                            p_vout->p_sys->i_window_height, 1,
833                            0, InputOutput, 0,
834                            CWBackPixel | CWEventMask,
835                            &xwindow_attributes );
836
837     if ( p_vout->b_fullscreen )
838     {
839         prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
840         mwmhints.flags = MWM_HINTS_DECORATIONS;
841         mwmhints.decorations = 0;
842         XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
843                          prop, prop, 32, PropModeReplace,
844                          (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
845
846         XSetTransientForHint( p_vout->p_sys->p_display,
847                               p_vout->p_sys->window, None );
848         XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
849     }
850
851     
852     /* Set window manager hints and properties: size hints, command,
853      * window's name, and accepted protocols */
854     XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
855                        &xsize_hints );
856     XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
857                  p_main->ppsz_argv, p_main->i_argc );
858     XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
859                 VOUT_TITLE " (XVideo output)" );
860
861     if( (p_vout->p_sys->wm_protocols == None)        /* use WM_DELETE_WINDOW */
862         || (p_vout->p_sys->wm_delete_window == None)
863         || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
864                              &p_vout->p_sys->wm_delete_window, 1 ) )
865     {
866         /* WM_DELETE_WINDOW is not supported by window manager */
867         intf_Msg( "vout error: missing or bad window manager" );
868     }
869
870     /* Creation of a graphic context that doesn't generate a GraphicsExpose
871      * event when using functions like XCopyArea */
872     xgcvalues.graphics_exposures = False;
873     p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
874                                    p_vout->p_sys->window,
875                                    GCGraphicsExposures, &xgcvalues);
876
877     /* Send orders to server, and wait until window is displayed - three
878      * events must be received: a MapNotify event, an Expose event allowing
879      * drawing in the window, and a ConfigureNotify to get the window
880      * dimensions. Once those events have been received, only ConfigureNotify
881      * events need to be received. */
882     b_expose = 0;
883     b_configure_notify = 0;
884     b_map_notify = 0;
885     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
886     do
887     {
888         XNextEvent( p_vout->p_sys->p_display, &xevent);
889         if( (xevent.type == Expose)
890             && (xevent.xexpose.window == p_vout->p_sys->window) )
891         {
892             b_expose = 1;
893         }
894         else if( (xevent.type == MapNotify)
895                  && (xevent.xmap.window == p_vout->p_sys->window) )
896         {
897             b_map_notify = 1;
898         }
899         else if( (xevent.type == ConfigureNotify)
900                  && (xevent.xconfigure.window == p_vout->p_sys->window) )
901         {
902             b_configure_notify = 1;
903             p_vout->p_sys->i_window_width = xevent.xconfigure.width;
904             p_vout->p_sys->i_window_height = xevent.xconfigure.height;
905         }
906     } while( !( b_expose && b_configure_notify && b_map_notify ) );
907
908     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
909                   StructureNotifyMask | KeyPressMask |
910                   ButtonPressMask | ButtonReleaseMask | 
911                   PointerMotionMask );
912
913     if( p_vout->b_fullscreen )
914     {
915         XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
916                         RevertToNone, CurrentTime );
917         XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
918     }
919
920     /* Create YUV output sub-window. */
921     p_vout->p_sys->yuv_window=XCreateSimpleWindow( p_vout->p_sys->p_display,
922                          p_vout->p_sys->window, 0, 0, 1, 1, 0,
923                          BlackPixel( p_vout->p_sys->p_display,
924                                          p_vout->p_sys->i_screen ),
925                          WhitePixel( p_vout->p_sys->p_display,
926                                          p_vout->p_sys->i_screen ) );
927  
928     p_vout->p_sys->yuv_gc = XCreateGC( p_vout->p_sys->p_display,
929                                         p_vout->p_sys->yuv_window,
930                                         GCGraphicsExposures, &xgcvalues );
931
932     XSetWindowBackground( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
933              BlackPixel(p_vout->p_sys->p_display, p_vout->p_sys->i_screen ) );
934
935     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
936     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
937                   ExposureMask );
938
939     /* If the cursor was formerly blank than blank it again */
940     if( !p_vout->p_sys->b_mouse_pointer_visible )
941     {
942         X11ToggleMousePointer( p_vout );
943         X11ToggleMousePointer( p_vout );
944     }
945
946     return( 0 );
947 }
948
949 static void XVideoDestroyWindow( vout_thread_t *p_vout )
950 {
951     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->yuv_gc );
952     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
953
954     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
955     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
956     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
957 }
958
959 /*****************************************************************************
960  * XVideoCreateShmImage: create an XvImage using shared memory extension
961  *****************************************************************************
962  * Prepare an XvImage for display function.
963  * The order of the operations respects the recommandations of the mit-shm
964  * document by J.Corbet and K.Packard. Most of the parameters were copied from
965  * there.
966  *****************************************************************************/
967 static int XVideoCreateShmImage( Display* dpy, int xv_port,
968                                     XvImage **pp_xvimage,
969                                     XShmSegmentInfo *p_shm_info,
970                                     int i_width, int i_height )
971 {
972     *pp_xvimage = XvShmCreateImage( dpy, xv_port,
973                                     GUID_YUV12_PLANAR, 0,
974                                     i_width, i_height,
975                                     p_shm_info );
976     if( !(*pp_xvimage) )
977     {
978         intf_ErrMsg( "vout error: XvShmCreateImage failed." );
979         return( -1 );
980     }
981
982     p_shm_info->shmid    = shmget( IPC_PRIVATE, (*pp_xvimage)->data_size,
983                                    IPC_CREAT | 0777 );
984     if( p_shm_info->shmid < 0)                                      /* error */
985     {
986         intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
987                     strerror(errno));
988         return( 1 );
989     }
990
991     p_shm_info->shmaddr  = (*pp_xvimage)->data = shmat( p_shm_info->shmid,
992                                                         0, 0 );
993     p_shm_info->readOnly = False;
994
995 #if 0
996     /* Mark the shm segment to be removed when there will be no more
997      * attachements, so it is automatic on process exit or after shmdt */
998     shmctl( p_shm_info->shmid, IPC_RMID, 0 );
999 #endif
1000
1001     if( !XShmAttach( dpy, p_shm_info ) )
1002     {
1003         intf_ErrMsg( "vout error: XShmAttach failed" );
1004         shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1005         shmdt( p_shm_info->shmaddr );
1006         return( -1 );
1007     }
1008
1009     /* Send image to X server. This instruction is required, since having
1010      * built a Shm XImage and not using it causes an error on XCloseDisplay */
1011     XFlush( dpy );
1012
1013     return( 0 );
1014 }
1015
1016 /*****************************************************************************
1017  * XVideoDestroyShmImage
1018  *****************************************************************************
1019  * Destroy XImage AND associated data. Detach shared memory segment from
1020  * server and process, then free it. If pointer is NULL, the image won't be
1021  * destroyed (see vout_ManageOutputMethod())
1022  *****************************************************************************/
1023 static void XVideoDestroyShmImage( vout_thread_t *p_vout, XvImage *p_xvimage,
1024                                    XShmSegmentInfo *p_shm_info )
1025 {
1026     /* If pointer is NULL, do nothing */
1027     if( p_xvimage == NULL )
1028     {
1029         return;
1030     }
1031
1032     XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
1033 #if 0
1034     XDestroyImage( p_ximage ); /* XXX */
1035 #endif
1036
1037     shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1038
1039     if( shmdt( p_shm_info->shmaddr ) )  /* detach shared memory from process */
1040     {
1041         intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1042                      strerror(errno) );
1043     }
1044 }
1045
1046 /*****************************************************************************
1047  * XVideoEnableScreenSaver: enable screen saver
1048  *****************************************************************************
1049  * This function enable the screen saver on a display after it had been
1050  * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1051  * know wether the screen saver can be activated or not: if n successive calls
1052  * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1053  * will be required before the screen saver could effectively be activated.
1054  *****************************************************************************/
1055 void XVideoEnableScreenSaver( vout_thread_t *p_vout )
1056 {
1057     intf_DbgMsg( "intf: enabling screen saver" );
1058     XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1059                      p_vout->p_sys->i_ss_interval,
1060                      p_vout->p_sys->i_ss_blanking,
1061                      p_vout->p_sys->i_ss_exposure );
1062
1063     DPMSEnable( p_vout->p_sys->p_display );
1064 }
1065
1066 /*****************************************************************************
1067  * XVideoDisableScreenSaver: disable screen saver
1068  *****************************************************************************
1069  * See XEnableScreenSaver
1070  *****************************************************************************/
1071 void XVideoDisableScreenSaver( vout_thread_t *p_vout )
1072 {
1073     /* Save screen saver informations */
1074     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1075                      &p_vout->p_sys->i_ss_interval,
1076                      &p_vout->p_sys->i_ss_blanking,
1077                      &p_vout->p_sys->i_ss_exposure );
1078
1079     /* Disable screen saver */
1080     intf_DbgMsg( "intf: disabling screen saver" );
1081     XSetScreenSaver( p_vout->p_sys->p_display, 0,
1082                      p_vout->p_sys->i_ss_interval,
1083                      p_vout->p_sys->i_ss_blanking,
1084                      p_vout->p_sys->i_ss_exposure );
1085
1086     DPMSDisable( p_vout->p_sys->p_display );
1087 }
1088
1089 /*****************************************************************************
1090  * X11ToggleMousePointer: hide or show the mouse pointer
1091  *****************************************************************************
1092  * This function hides the X pointer if requested.
1093  *****************************************************************************/
1094 void X11ToggleMousePointer( vout_thread_t *p_vout )
1095 {
1096
1097     if( p_vout->p_sys->b_mouse_pointer_visible )
1098     {
1099         XDefineCursor( p_vout->p_sys->p_display,
1100                        p_vout->p_sys->window,
1101                        p_vout->p_sys->blank_cursor );
1102         p_vout->p_sys->b_mouse_pointer_visible = 0;
1103     }
1104     else
1105     {
1106         XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );
1107         p_vout->p_sys->b_mouse_pointer_visible = 1;
1108     }
1109 }
1110
1111 /* This based on some code in SetBufferPicture... At the moment it's only
1112  * used by the xvideo plugin, but others may want to use it. */
1113 static void XVideoOutputCoords( const picture_t *p_pic, const boolean_t scale,
1114                                 const int win_w, const int win_h,
1115                                 int *dx, int *dy, int *w, int *h )
1116 {
1117 }
1118
1119
1120 /*****************************************************************************
1121  * XVideoGetPort: get YUV12 port
1122  *****************************************************************************
1123  * 
1124  *****************************************************************************/
1125 static int XVideoGetPort( Display *dpy )
1126 {
1127     XvAdaptorInfo *p_adaptor;
1128     int i_adaptor, i_num_adaptors, i_requested_adaptor;
1129     int i_selected_port;
1130
1131     switch( XvQueryAdaptors( dpy, DefaultRootWindow( dpy ),
1132                              &i_num_adaptors, &p_adaptor ) )
1133     {
1134         case Success:
1135             break;
1136
1137         case XvBadExtension:
1138             intf_WarnMsg( 3, "vout error: XvBadExtension for XvQueryAdaptors" );
1139             return( -1 );
1140
1141         case XvBadAlloc:
1142             intf_WarnMsg( 3, "vout error: XvBadAlloc for XvQueryAdaptors" );
1143             return( -1 );
1144
1145         default:
1146             intf_WarnMsg( 3, "vout error: XvQueryAdaptors failed" );
1147             return( -1 );
1148     }
1149
1150     i_selected_port = -1;
1151     i_requested_adaptor = main_GetIntVariable( VOUT_XVADAPTOR_VAR, -1 );
1152
1153     /* No special xv port has been requested so try all of them */
1154     for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1155     {
1156         int i_port;
1157
1158         /* If we requested an adaptor and it's not this one, we aren't
1159          * interested */
1160         if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1161         {
1162             continue;
1163         }
1164
1165         /* If the adaptor doesn't have the required properties, skip it */
1166         if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1167             !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1168         {
1169             continue;
1170         }
1171
1172         for( i_port = p_adaptor[i_adaptor].base_id;
1173              i_port < p_adaptor[i_adaptor].base_id
1174                        + p_adaptor[i_adaptor].num_ports;
1175              i_port++ )
1176         {
1177             XvImageFormatValues *p_formats;
1178             int i_format, i_num_formats;
1179
1180             /* If we already found a port, we aren't interested */
1181             if( i_selected_port != -1 )
1182             {
1183                 continue;
1184             }
1185
1186             /* Check that port supports YUV12 planar format... */
1187             p_formats = XvListImageFormats( dpy, i_port, &i_num_formats );
1188
1189             for( i_format = 0; i_format < i_num_formats; i_format++ )
1190             {
1191                 XvEncodingInfo  *p_enc;
1192                 int             i_enc, i_num_encodings;
1193                 XvAttribute     *p_attr;
1194                 int             i_attr, i_num_attributes;
1195
1196                 if( p_formats[ i_format ].id != GUID_YUV12_PLANAR )
1197                 {
1198                     continue;
1199                 }
1200
1201                 /* Found a matching port, print a description of this port */
1202                 i_selected_port = i_port;
1203
1204                 intf_WarnMsg( 3, "vout: XVideoGetPort found adaptor %i port %i",
1205                                  i_adaptor, i_port);
1206                 intf_WarnMsg( 3, "  image format 0x%x (%4.4s) %s supported",
1207                                  p_formats[ i_format ].id,
1208                                  (char *)&p_formats[ i_format ].id,
1209                                  ( p_formats[ i_format ].format
1210                                     == XvPacked ) ? "packed" : "planar" );
1211
1212                 intf_WarnMsg( 4, " encoding list:" );
1213
1214                 if( XvQueryEncodings( dpy, i_port, &i_num_encodings, &p_enc )
1215                      != Success )
1216                 {
1217                     intf_WarnMsg( 4, "  XvQueryEncodings failed" );
1218                     continue;
1219                 }
1220
1221                 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1222                 {
1223                     intf_WarnMsg( 4, "  id=%ld, name=%s, size=%ldx%ld,"
1224                                      " numerator=%d, denominator=%d",
1225                                   p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1226                                   p_enc[i_enc].width, p_enc[i_enc].height,
1227                                   p_enc[i_enc].rate.numerator,
1228                                   p_enc[i_enc].rate.denominator );
1229                 }
1230
1231                 if( p_enc != NULL )
1232                 {
1233                     XvFreeEncodingInfo( p_enc );
1234                 }
1235
1236                 intf_WarnMsg( 4, " attribute list:" );
1237                 p_attr = XvQueryPortAttributes( dpy, i_port,
1238                                                 &i_num_attributes );
1239                 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1240                 {
1241                     intf_WarnMsg( 4,
1242                           "  name=%s, flags=[%s%s ], min=%i, max=%i",
1243                           p_attr[i_attr].name,
1244                           (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1245                           (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1246                           p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1247                 }
1248
1249                 if( p_attr != NULL )
1250                 {
1251                     XFree( p_attr );
1252                 }
1253             }
1254
1255             if( p_formats != NULL )
1256             {
1257                 XFree( p_formats );
1258             }
1259         }
1260     }
1261
1262     if( i_num_adaptors > 0 )
1263     {
1264         XvFreeAdaptorInfo( p_adaptor );
1265     }
1266
1267     if( i_selected_port == -1 )
1268     {
1269         if( i_requested_adaptor == -1 )
1270         {
1271             intf_WarnMsg( 3, "vout: no XVideo port found supporting YUV12" );
1272         }
1273         else
1274         {
1275             intf_WarnMsg( 3, "vout: XVideo adaptor %i does not support YUV12",
1276                              i_requested_adaptor );
1277         }
1278     }
1279
1280     return( i_selected_port );
1281 }
1282
1283
1284 /*****************************************************************************
1285  * XVideoDisplay: display image
1286  *****************************************************************************
1287  * This function displays the image stored in p_vout->p_sys->p_xvimage.
1288  * The image is scaled to fit in the output window (and to have the correct
1289  * aspect ratio).
1290  *****************************************************************************/
1291 static void XVideoDisplay( vout_thread_t *p_vout )
1292 {
1293     int         i_dest_width, i_dest_height;
1294     int         i_dest_x, i_dest_y;
1295
1296     if( !p_vout->p_sys->p_xvimage )
1297     {
1298         return;
1299     }
1300
1301     i_dest_height = p_vout->p_sys->i_window_height >
1302                         p_vout->p_rendered_pic->i_height
1303                   ? p_vout->p_sys->i_window_height
1304                   : p_vout->p_rendered_pic->i_height;
1305     i_dest_width = p_vout->p_sys->i_window_width >
1306                         p_vout->p_rendered_pic->i_width
1307                  ? p_vout->p_sys->i_window_width
1308                  : p_vout->p_rendered_pic->i_width;
1309         
1310     if( p_vout->b_scale )
1311     {
1312         int   i_ratio = 900 * i_dest_width / i_dest_height;
1313         
1314         switch( p_vout->p_rendered_pic->i_aspect_ratio )
1315         {
1316             case AR_3_4_PICTURE:
1317                 if( i_ratio < 1200 )
1318                 {
1319                     i_dest_width = i_dest_height * 4 / 3;
1320                 }
1321                 else
1322                 {
1323                     i_dest_height = i_dest_width * 3 / 4;
1324                 }
1325                 i_ratio = 1200;
1326                 break;
1327
1328             case AR_16_9_PICTURE:
1329                 if( i_ratio < 1600 )
1330                 {
1331                     i_dest_width = i_dest_height * 16 / 9;
1332                 }
1333                 else
1334                 {
1335                     i_dest_height = i_dest_width * 9 / 16;
1336                 }
1337                 i_ratio = 1600;
1338                 break;
1339
1340             case AR_221_1_PICTURE:
1341                 if( i_ratio < 1989 )
1342                 {
1343                     i_dest_width = i_dest_height * 221 / 100;
1344                 }
1345                 else
1346                 {
1347                     i_dest_height = i_dest_width * 100 / 221;
1348                 }
1349                 i_ratio = 1989;
1350                 break;
1351
1352             case AR_SQUARE_PICTURE:
1353             default:
1354                 if( i_ratio < 900 )
1355                 {
1356                     i_dest_width = i_dest_height * p_vout->p_rendered_pic->i_width / p_vout->p_rendered_pic->i_height;
1357                 }
1358                 else
1359                 {
1360                     i_dest_height = i_dest_width * p_vout->p_rendered_pic->i_height / p_vout->p_rendered_pic->i_width;
1361                 }
1362                 i_ratio = 900;
1363                 break;
1364         }
1365
1366         if( i_dest_width >
1367             DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen ) )
1368         {
1369             i_dest_width = DisplayWidth( p_vout->p_sys->p_display,
1370                                          p_vout->p_sys->i_screen );
1371             i_dest_height = 900 * i_dest_width / i_ratio;
1372         }
1373         else if( i_dest_height >
1374             DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen ) )
1375         {
1376             i_dest_height = DisplayHeight( p_vout->p_sys->p_display,
1377                                            p_vout->p_sys->i_screen );
1378             i_dest_width = i_ratio * i_dest_height / 900;
1379         }
1380     }
1381
1382     XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->xv_port,
1383                    p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
1384                    p_vout->p_sys->p_xvimage,
1385                    0 /*src_x*/, 0 /*src_y*/,
1386                    p_vout->p_rendered_pic->i_width,
1387                    p_vout->p_rendered_pic->i_height,
1388                    0 /*dest_x*/, 0 /*dest_y*/, i_dest_width, i_dest_height,
1389                    False );
1390     
1391     /* YUV window */
1392     XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1393                    i_dest_width, i_dest_height );
1394
1395     /* Root window */
1396     if( ( ( i_dest_width != p_vout->p_sys->i_window_width ) ||
1397           ( i_dest_height != p_vout->p_sys->i_window_height ) ) &&
1398         ! p_vout->b_fullscreen )
1399     {
1400         p_vout->p_sys->i_window_width = i_dest_width;
1401         p_vout->p_sys->i_window_height = i_dest_height;
1402 //        p_vout->i_changes |= VOUT_SIZE_CHANGE;
1403         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
1404                        i_dest_width, i_dest_height );
1405     }
1406     
1407     /* Set picture position */
1408     i_dest_x = (p_vout->p_sys->i_window_width - i_dest_width) / 2;
1409     i_dest_y = (p_vout->p_sys->i_window_height - i_dest_height) / 2;
1410     
1411     XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1412                  i_dest_x, i_dest_y );
1413     
1414     /* Send the order to the X server */
1415     XSync( p_vout->p_sys->p_display, False );
1416 }
1417
1418 #if 0
1419 /*****************************************************************************
1420  * XVideoSetAttribute
1421  *****************************************************************************
1422  * This function can be used to set attributes, e.g. XV_BRIGHTNESS and
1423  * XV_CONTRAST. "f_value" should be in the range of 0 to 1.
1424  *****************************************************************************/
1425 static void XVideoSetAttribute( vout_thread_t *p_vout,
1426                                 char *attr_name, float f_value )
1427 {
1428     int             i_attrib;
1429     XvAttribute    *p_attrib;
1430     Display        *p_dpy   = p_vout->p_sys->p_display;
1431     int             xv_port = p_vout->p_sys->xv_port;
1432
1433     p_attrib = XvQueryPortAttributes( p_dpy, xv_port, &i_attrib );
1434
1435     do
1436     {
1437         i_attrib--;
1438
1439         if( i_attrib >= 0 && !strcmp( p_attrib[ i_attrib ].name, attr_name ) )
1440         {
1441             int i_sv = f_value * ( p_attrib[ i_attrib ].max_value
1442                                     - p_attrib[ i_attrib ].min_value + 1 )
1443                         + p_attrib[ i_attrib ].min_value;
1444
1445             XvSetPortAttribute( p_dpy, xv_port,
1446                             XInternAtom( p_dpy, attr_name, False ), i_sv );
1447             break;
1448         }
1449
1450     } while( i_attrib > 0 );
1451
1452     if( p_attrib )
1453         XFree( p_attrib );
1454 }
1455 #endif
1456