1 /*****************************************************************************
2 * xcommon.c: Functions common to the X11 and XVideo plugins
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: xcommon.c,v 1.5 2002/01/04 14:01:34 sam Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * David Kennedy <dkennedy@tinytoad.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
33 #include <videolan/vlc.h>
35 #ifdef HAVE_MACHINE_PARAM_H
37 #include <machine/param.h>
38 #include <sys/types.h> /* typedef ushort */
43 #include <netinet/in.h> /* BSD: struct in_addr */
46 #include <sys/shm.h> /* shmget(), shmctl() */
48 #include <X11/Xutil.h>
49 #include <X11/keysym.h>
50 #include <X11/extensions/XShm.h>
51 #include <X11/extensions/dpms.h>
53 #ifdef MODULE_NAME_IS_xvideo
54 # include <X11/extensions/Xv.h>
55 # include <X11/extensions/Xvlib.h>
59 #include "video_output.h"
62 #include "interface.h"
63 #include "netutils.h" /* network_ChannelJoin */
65 #include "stream_control.h" /* needed by input_ext-intf.h... */
66 #include "input_ext-intf.h"
68 #ifdef MODULE_NAME_IS_xvideo
69 # define IMAGE_TYPE XvImage
70 # define EXTRA_ARGS int i_xvport, int i_chroma
71 # define EXTRA_ARGS_SHM int i_xvport, int i_chroma, XShmSegmentInfo *p_shm
72 # define DATA_SIZE(p) (p)->data_size
73 # define IMAGE_FREE XFree /* There is nothing like XvDestroyImage */
75 # define IMAGE_TYPE XImage
76 # define EXTRA_ARGS Visual *p_visual, int i_depth, int i_bytes_per_pixel
77 # define EXTRA_ARGS_SHM Visual *p_visual, int i_depth, XShmSegmentInfo *p_shm
78 # define DATA_SIZE(p) ((p)->bytes_per_line * (p)->height)
79 # define IMAGE_FREE XDestroyImage
82 /*****************************************************************************
84 *****************************************************************************/
85 static int vout_Probe ( probedata_t * );
86 static int vout_Create ( vout_thread_t * );
87 static void vout_Destroy ( vout_thread_t * );
88 static void vout_Render ( vout_thread_t *, picture_t * );
89 static void vout_Display ( vout_thread_t *, picture_t * );
90 static int vout_Manage ( vout_thread_t * );
91 static int vout_Init ( vout_thread_t * );
92 static void vout_End ( vout_thread_t * );
94 static int InitDisplay ( vout_thread_t * );
96 static int CreateWindow ( vout_thread_t * );
97 static void DestroyWindow ( vout_thread_t * );
99 static int NewPicture ( vout_thread_t *, picture_t * );
100 static void FreePicture ( vout_thread_t *, picture_t * );
102 static IMAGE_TYPE *CreateImage ( Display *, EXTRA_ARGS, int, int );
103 static IMAGE_TYPE *CreateShmImage ( Display *, EXTRA_ARGS_SHM, int, int );
105 static void ToggleFullScreen ( vout_thread_t * );
107 static void EnableXScreenSaver ( vout_thread_t * );
108 static void DisableXScreenSaver ( vout_thread_t * );
110 static void CreateCursor ( vout_thread_t * );
111 static void DestroyCursor ( vout_thread_t * );
112 static void ToggleCursor ( vout_thread_t * );
114 #ifdef MODULE_NAME_IS_xvideo
115 static int XVideoGetPort ( Display *, int );
116 static void XVideoReleasePort ( Display *, int );
119 /*****************************************************************************
120 * vout_sys_t: video output method descriptor
121 *****************************************************************************
122 * This structure is part of the video output thread descriptor.
123 * It describes the X11 and XVideo specific properties of an output thread.
124 *****************************************************************************/
125 typedef struct vout_sys_s
127 /* Internal settings and properties */
128 Display * p_display; /* display pointer */
130 Visual * p_visual; /* visual pointer */
131 int i_screen; /* screen number */
132 Window window; /* root window */
133 GC gc; /* graphic context instance handler */
135 boolean_t b_shm; /* shared memory extension flag */
137 #ifdef MODULE_NAME_IS_xvideo
138 Window yuv_window; /* sub-window for displaying yuv video
141 u32 i_chroma; /* the chroma we will select */
143 Colormap colormap; /* colormap used (8bpp only) */
146 int i_bytes_per_pixel;
147 int i_bytes_per_line;
153 /* X11 generic properties */
155 Atom wm_delete_window;
157 int i_width; /* width of main window */
158 int i_height; /* height of main window */
160 /* Backup of window position and size before fullscreen switch */
166 /* Screen saver properties */
167 int i_ss_timeout; /* timeout */
168 int i_ss_interval; /* interval between changes */
169 int i_ss_blanking; /* blanking mode */
170 int i_ss_exposure; /* exposure mode */
171 BOOL b_ss_dpms; /* DPMS mode */
173 /* Mouse pointer properties */
174 boolean_t b_mouse_pointer_visible;
175 mtime_t i_time_mouse_last_moved; /* used to auto-hide pointer*/
176 Cursor blank_cursor; /* the hidden cursor */
177 Pixmap cursor_pixmap;
181 /*****************************************************************************
182 * picture_sys_t: direct buffer method descriptor
183 *****************************************************************************
184 * This structure is part of the picture descriptor, it describes the
185 * XVideo specific properties of a direct buffer.
186 *****************************************************************************/
187 typedef struct picture_sys_s
189 IMAGE_TYPE * p_image;
191 XShmSegmentInfo shminfo; /* shared memory zone information */
195 /*****************************************************************************
196 * mwmhints_t: window manager hints
197 *****************************************************************************
198 * Fullscreen needs to be able to hide the wm decorations so we provide
199 * this structure to make it easier.
200 *****************************************************************************/
201 #define MWM_HINTS_DECORATIONS (1L << 1)
202 #define PROP_MWM_HINTS_ELEMENTS 5
203 typedef struct mwmhints_s
212 /*****************************************************************************
214 *****************************************************************************/
215 #ifdef MODULE_NAME_IS_xvideo
216 # define MAX_DIRECTBUFFERS 5
218 # define MAX_DIRECTBUFFERS 2
221 /*****************************************************************************
222 * Seeking function TODO: put this in a generic location !
223 *****************************************************************************/
224 static __inline__ void vout_Seek( off_t i_seek )
226 #define area p_main->p_intf->p_input->stream.p_selected_area
227 off_t i_tell = area->i_tell;
229 i_tell += i_seek * (off_t)50 * p_main->p_intf->p_input->stream.i_mux_rate;
231 i_tell = ( i_tell <= 0/*area->i_start*/ ) ? 0/*area->i_start*/
232 : ( i_tell >= area->i_size ) ? area->i_size
235 input_Seek( p_main->p_intf->p_input, i_tell );
239 /*****************************************************************************
240 * Return the best suited FourCC value for a given chroma. We use this
241 * because a decoder may output FOURCC_IYUV, which is exactly the same as
242 * FOURCC_I420, but X servers usually know FOURCC_I420 and not FOURCC_IYUV.
243 *****************************************************************************/
244 static __inline__ u32 BestChroma( u32 i_chroma )
246 /* XXX: don't forget to update vout_Init if you change this */
249 /* These ones are all the same */
255 /* These ones are all the same */
260 /* We know this one */
264 /* This is seldom supported, but we know how to convert */
274 /*****************************************************************************
275 * Functions exported as capabilities. They are declared as static so that
276 * we don't pollute the namespace too much.
277 *****************************************************************************/
278 void _M( vout_getfunctions )( function_list_t * p_function_list )
280 p_function_list->pf_probe = vout_Probe;
281 p_function_list->functions.vout.pf_create = vout_Create;
282 p_function_list->functions.vout.pf_init = vout_Init;
283 p_function_list->functions.vout.pf_end = vout_End;
284 p_function_list->functions.vout.pf_destroy = vout_Destroy;
285 p_function_list->functions.vout.pf_manage = vout_Manage;
286 p_function_list->functions.vout.pf_render = vout_Render;
287 p_function_list->functions.vout.pf_display = vout_Display;
288 p_function_list->functions.vout.pf_setpalette = NULL;
291 /*****************************************************************************
292 * vout_Probe: probe the video driver and return a score
293 *****************************************************************************
294 * This function tries to initialize SDL and returns a score to the
295 * plugin manager so that it can select the best plugin.
296 *****************************************************************************/
297 static int vout_Probe( probedata_t *p_data )
299 Display *p_display; /* display pointer */
301 #ifdef MODULE_NAME_IS_xvideo
305 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
306 psz_display = XDisplayName( main_GetPszVariable(VOUT_DISPLAY_VAR, NULL) );
307 p_display = XOpenDisplay( psz_display );
308 if( p_display == NULL ) /* error */
310 intf_WarnMsg( 3, "vout: cannot open display %s", psz_display );
314 #ifdef MODULE_NAME_IS_xvideo
315 /* Check that there is an available XVideo port */
316 i_xvport = XVideoGetPort( p_display, BestChroma( p_data->vout.i_chroma ) );
319 /* It failed, but it's not completely lost ! We try to open an
320 * XVideo port for a simple 16bpp RGB picture */
321 i_xvport = XVideoGetPort( p_display, FOURCC_RV16 );
324 XCloseDisplay( p_display );
328 XVideoReleasePort( p_display, i_xvport );
331 /* Clean-up everyting */
332 XCloseDisplay( p_display );
334 #ifdef MODULE_NAME_IS_xvideo
341 /*****************************************************************************
342 * vout_Create: allocate X11 video thread output method
343 *****************************************************************************
344 * This function allocate and initialize a X11 vout method. It uses some of the
345 * vout properties to choose the window size, and change them according to the
346 * actual properties of the display.
347 *****************************************************************************/
348 static int vout_Create( vout_thread_t *p_vout )
352 /* Allocate structure */
353 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
354 if( p_vout->p_sys == NULL )
356 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
360 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
361 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
362 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
364 if( p_vout->p_sys->p_display == NULL ) /* error */
366 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
367 free( p_vout->p_sys );
370 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
372 #ifdef MODULE_NAME_IS_xvideo
373 /* Try to guess the chroma format we will be using */
374 p_vout->p_sys->i_chroma = BestChroma( p_vout->render.i_chroma );
376 /* Check that we have access to an XVideo port providing this chroma */
377 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout->p_sys->p_display,
378 p_vout->p_sys->i_chroma );
379 if( p_vout->p_sys->i_xvport < 0 )
381 /* It failed, but it's not completely lost ! We try to open an
382 * XVideo port for a simple 16bpp RGB picture */
383 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout->p_sys->p_display,
385 if( p_vout->p_sys->i_xvport < 0 )
387 XCloseDisplay( p_vout->p_sys->p_display );
388 free( p_vout->p_sys );
392 /* This one worked ! That's better than nothing, it has HW scaling. */
393 p_vout->p_sys->i_chroma = FOURCC_RV16;
397 /* Create blank cursor (for mouse cursor autohiding) */
398 p_vout->p_sys->b_mouse_pointer_visible = 1;
399 CreateCursor( p_vout );
401 /* Spawn base window - this window will include the video output window,
402 * but also command buttons, subtitles and other indicators */
403 if( CreateWindow( p_vout ) )
405 intf_ErrMsg( "vout error: cannot create X11 window" );
406 DestroyCursor( p_vout );
407 XCloseDisplay( p_vout->p_sys->p_display );
408 free( p_vout->p_sys );
412 /* Open and initialize device. */
413 if( InitDisplay( p_vout ) )
415 intf_ErrMsg( "vout error: cannot initialize X11 display" );
416 DestroyCursor( p_vout );
417 DestroyWindow( p_vout );
418 XCloseDisplay( p_vout->p_sys->p_display );
419 free( p_vout->p_sys );
423 /* Disable screen saver and return */
424 DisableXScreenSaver( p_vout );
429 /*****************************************************************************
430 * vout_Destroy: destroy X11 video thread output method
431 *****************************************************************************
432 * Terminate an output method created by vout_CreateOutputMethod
433 *****************************************************************************/
434 static void vout_Destroy( vout_thread_t *p_vout )
436 /* Restore cursor if it was blanked */
437 if( !p_vout->p_sys->b_mouse_pointer_visible )
439 ToggleCursor( p_vout );
442 #ifdef MODULE_NAME_IS_xvideo
443 XVideoReleasePort( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport );
446 /* Destroy colormap */
447 if( p_vout->p_sys->i_screen_depth == 8 )
449 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
454 DestroyCursor( p_vout );
455 EnableXScreenSaver( p_vout );
456 DestroyWindow( p_vout );
458 XCloseDisplay( p_vout->p_sys->p_display );
460 /* Destroy structure */
461 free( p_vout->p_sys );
464 /*****************************************************************************
465 * vout_Init: initialize X11 video thread output method
466 *****************************************************************************
467 * This function create the XImages needed by the output thread. It is called
468 * at the beginning of the thread, but also each time the window is resized.
469 *****************************************************************************/
470 static int vout_Init( vout_thread_t *p_vout )
475 I_OUTPUTPICTURES = 0;
477 #ifdef MODULE_NAME_IS_xvideo
478 /* Initialize the output structure ; we already found an XVideo port,
479 * which either suits p_vout->render.i_chroma or p_vout->p_sys->i_chroma
480 * if nothing was found at the first attempt. */
481 switch( p_vout->render.i_chroma )
483 /* These ones are equivalent to what we chose instead, for instance
484 * we tell the video output we can do FOURCC_IYUV even though we
485 * actually do FOURCC_I420. */
494 p_vout->output.i_chroma = p_vout->render.i_chroma;
495 p_vout->output.i_width = p_vout->render.i_width;
496 p_vout->output.i_height = p_vout->render.i_height;
497 p_vout->output.i_aspect = p_vout->render.i_aspect;
500 /* These ones aren't equivalent to what we chose, so we need to
501 * warn the decoder that we chose another chroma and that it has
502 * conversion to do. */
504 /* At least for this one we can have the aspect ratio */
506 p_vout->output.i_chroma = p_vout->p_sys->i_chroma;
507 p_vout->output.i_width = p_vout->render.i_width;
508 p_vout->output.i_height = p_vout->render.i_height;
509 p_vout->output.i_aspect = p_vout->render.i_aspect;
512 /* Here we don't even control the aspect ratio */
514 p_vout->output.i_chroma = p_vout->p_sys->i_chroma;
515 p_vout->output.i_width = p_vout->render.i_width;
516 p_vout->output.i_height = p_vout->render.i_height;
517 p_vout->output.i_aspect = p_vout->p_sys->i_width
518 * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
523 /* Initialize the output structure: RGB with square pixels, whatever
524 * the input format is, since it's the only format we know */
525 switch( p_vout->p_sys->i_screen_depth )
527 case 8: /* FIXME: set the palette */
528 p_vout->output.i_chroma = FOURCC_BI_RGB; break;
530 p_vout->output.i_chroma = FOURCC_RV15; break;
532 p_vout->output.i_chroma = FOURCC_RV16; break;
534 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
536 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
538 intf_ErrMsg( "vout error: unknown screen depth" );
542 p_vout->output.i_width = p_vout->p_sys->i_width;
543 p_vout->output.i_height = p_vout->p_sys->i_height;
545 /* Assume we have square pixels */
546 p_vout->output.i_aspect = p_vout->p_sys->i_width
547 * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
550 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
551 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
555 /* Find an empty picture slot */
556 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
558 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
560 p_pic = p_vout->p_picture + i_index;
565 /* Allocate the picture */
566 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
571 p_pic->i_status = DESTROYED_PICTURE;
572 p_pic->i_type = DIRECT_PICTURE;
574 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
582 /*****************************************************************************
583 * vout_Render: render previously calculated output
584 *****************************************************************************/
585 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
590 /*****************************************************************************
591 * vout_Display: displays previously rendered output
592 *****************************************************************************
593 * This function sends the currently rendered image to X11 server.
594 * (The Xv extension takes care of "double-buffering".)
595 *****************************************************************************/
596 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
598 int i_width, i_height, i_x, i_y;
600 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
601 &i_x, &i_y, &i_width, &i_height );
603 if( p_vout->p_sys->b_shm )
605 /* Display rendered image using shared memory extension */
606 #ifdef MODULE_NAME_IS_xvideo
607 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
608 p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
609 p_pic->p_sys->p_image, 0 /*src_x*/, 0 /*src_y*/,
610 p_vout->output.i_width, p_vout->output.i_height,
611 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
612 False /* Don't put True here or you'll waste your CPU */ );
614 XShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->window,
615 p_vout->p_sys->gc, p_pic->p_sys->p_image,
616 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
617 p_vout->output.i_width, p_vout->output.i_height,
618 False /* Don't put True here ! */ );
623 /* Use standard XPutImage -- this is gonna be slow ! */
624 #ifdef MODULE_NAME_IS_xvideo
625 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
626 p_vout->p_sys->yuv_window, p_vout->p_sys->gc,
627 p_pic->p_sys->p_image, 0 /*src_x*/, 0 /*src_y*/,
628 p_vout->output.i_width, p_vout->output.i_height,
629 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
631 XPutImage( p_vout->p_sys->p_display, p_vout->p_sys->window,
632 p_vout->p_sys->gc, p_pic->p_sys->p_image,
633 0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
634 p_vout->output.i_width, p_vout->output.i_height );
638 /* Make sure the command is sent now */
639 XFlush( p_vout->p_sys->p_display );
642 /*****************************************************************************
643 * vout_Manage: handle X11 events
644 *****************************************************************************
645 * This function should be called regularly by video output thread. It manages
646 * X11 events and allows window resizing. It returns a non null value on
648 *****************************************************************************/
649 static int vout_Manage( vout_thread_t *p_vout )
651 XEvent xevent; /* X11 event */
652 boolean_t b_resized; /* window has been resized */
653 char i_key; /* ISO Latin-1 key */
656 /* Handle X11 events: ConfigureNotify events are parsed to know if the
657 * output window's size changed, MapNotify and UnmapNotify to know if the
658 * window is mapped (and if the display is useful), and ClientMessages
659 * to intercept window destruction requests */
662 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
663 StructureNotifyMask | KeyPressMask |
664 ButtonPressMask | ButtonReleaseMask |
665 PointerMotionMask | Button1MotionMask , &xevent )
668 /* ConfigureNotify event: prepare */
669 if( (xevent.type == ConfigureNotify)
670 && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
671 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
673 /* Update dimensions */
675 p_vout->i_changes |= VOUT_SIZE_CHANGE;
676 p_vout->p_sys->i_width = xevent.xconfigure.width;
677 p_vout->p_sys->i_height = xevent.xconfigure.height;
679 /* MapNotify event: change window status and disable screen saver */
680 else if( xevent.type == MapNotify)
682 if( (p_vout != NULL) && !p_vout->b_active )
684 DisableXScreenSaver( p_vout );
685 p_vout->b_active = 1;
688 /* UnmapNotify event: change window status and enable screen saver */
689 else if( xevent.type == UnmapNotify )
691 if( (p_vout != NULL) && p_vout->b_active )
693 EnableXScreenSaver( p_vout );
694 p_vout->b_active = 0;
698 else if( xevent.type == KeyPress )
700 /* We may have keys like F1 trough F12, ESC ... */
701 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
702 xevent.xkey.keycode, 0 );
703 switch( x_key_symbol )
706 p_main->p_intf->b_die = 1;
709 p_main->p_intf->b_menu_change = 1;
724 input_Seek( p_main->p_intf->p_input,
725 p_main->p_intf->p_input->stream.p_selected_area->i_start );
728 input_Seek( p_main->p_intf->p_input,
729 p_main->p_intf->p_input->stream.p_selected_area->i_size );
738 input_SetStatus( p_main->p_intf->p_input,
739 INPUT_STATUS_PAUSE );
744 * The reason why I use this instead of XK_0 is that
745 * with XLookupString, we don't have to care about
748 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
750 /* FIXME: handle stuff here */
755 p_main->p_intf->b_die = 1;
759 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
762 case '0': network_ChannelJoin( 0 ); break;
763 case '1': network_ChannelJoin( 1 ); break;
764 case '2': network_ChannelJoin( 2 ); break;
765 case '3': network_ChannelJoin( 3 ); break;
766 case '4': network_ChannelJoin( 4 ); break;
767 case '5': network_ChannelJoin( 5 ); break;
768 case '6': network_ChannelJoin( 6 ); break;
769 case '7': network_ChannelJoin( 7 ); break;
770 case '8': network_ChannelJoin( 8 ); break;
771 case '9': network_ChannelJoin( 9 ); break;
774 intf_DbgMsg( "vout: unhandled key '%c' (%i)",
775 (char)i_key, i_key );
783 else if( xevent.type == ButtonPress )
785 switch( ((XButtonEvent *)&xevent)->button )
788 /* In this part we will eventually manage
789 * clicks for DVD navigation for instance. For the
790 * moment just pause the stream. */
791 input_SetStatus( p_main->p_intf->p_input,
792 INPUT_STATUS_PAUSE );
805 else if( xevent.type == ButtonRelease )
807 switch( ((XButtonEvent *)&xevent)->button )
810 /* FIXME: need locking ! */
811 p_main->p_intf->b_menu_change = 1;
816 else if( xevent.type == MotionNotify )
818 p_vout->p_sys->i_time_mouse_last_moved = mdate();
819 if( ! p_vout->p_sys->b_mouse_pointer_visible )
821 ToggleCursor( p_vout );
827 intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
831 #ifdef MODULE_NAME_IS_xvideo
832 /* Handle events for YUV video output sub-window */
833 while( XCheckWindowEvent( p_vout->p_sys->p_display,
834 p_vout->p_sys->yuv_window,
835 ExposureMask, &xevent ) == True )
837 /* Window exposed (only handled if stream playback is paused) */
838 if( xevent.type == Expose )
840 if( ((XExposeEvent *)&xevent)->count == 0 )
842 /* (if this is the last a collection of expose events...) */
843 if( p_main->p_intf->p_input != NULL )
846 p_main->p_intf->p_input->stream.control.i_status )
848 /* XVideoDisplay( p_vout )*/;
856 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
857 * are handled - according to the man pages, the format is always 32
859 while( XCheckTypedEvent( p_vout->p_sys->p_display,
860 ClientMessage, &xevent ) )
862 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
863 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
865 p_main->p_intf->b_die = 1;
869 intf_DbgMsg( "vout: unhandled ClientMessage received" );
876 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
878 ToggleFullScreen( p_vout );
879 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
884 #ifdef MODULE_NAME_IS_x11
886 * Handle vout window resizing
891 /* If interface window has been resized, change vout size */
892 intf_DbgMsg( "vout: resizing output window" );
893 p_vout->i_width = p_vout->p_sys->i_width;
894 p_vout->i_height = p_vout->p_sys->i_height;
895 p_vout->i_changes |= VOUT_SIZE_CHANGE;
897 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
898 (p_vout->i_height != p_vout->p_sys->i_height) )
900 /* If video output size has changed, change interface window size */
901 intf_DbgMsg( "vout: resizing output window" );
902 p_vout->p_sys->i_width = p_vout->i_width;
903 p_vout->p_sys->i_height = p_vout->i_height;
904 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
905 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
908 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
910 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
911 && (p_vout->i_screen_depth == 8) )
913 /* FIXME: clear flags ?? */
919 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
921 intf_DbgMsg( "vout info: resizing window" );
922 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
925 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
926 p_vout->i_width, p_vout->i_height );
928 /* Destroy XImages to change their size */
931 /* Recreate XImages. If SysInit failed, the thread can't go on. */
932 if( vout_Init( p_vout ) )
934 intf_ErrMsg( "vout error: cannot resize display" );
938 /* Tell the video output thread that it will need to rebuild YUV
939 * tables. This is needed since conversion buffer size may have
941 p_vout->i_changes |= VOUT_YUV_CHANGE;
942 intf_Msg( "vout: video display resized (%dx%d)",
943 p_vout->i_width, p_vout->i_height);
950 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
952 int i_width, i_height, i_x, i_y;
954 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
956 intf_WarnMsg( 3, "vout: video display resized (%dx%d)",
957 p_vout->p_sys->i_width,
958 p_vout->p_sys->i_height );
960 vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
961 p_vout->p_sys->i_height,
962 &i_x, &i_y, &i_width, &i_height );
964 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
967 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
973 /* Autohide Cursour */
974 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
976 /* Hide the mouse automatically */
977 if( p_vout->p_sys->b_mouse_pointer_visible )
979 ToggleCursor( p_vout );
986 /*****************************************************************************
987 * vout_End: terminate X11 video thread output method
988 *****************************************************************************
989 * Destroy the X11 XImages created by vout_Init. It is called at the end of
990 * the thread, but also each time the window is resized.
991 *****************************************************************************/
992 static void vout_End( vout_thread_t *p_vout )
996 /* Free the direct buffers we allocated */
997 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1000 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1004 /* following functions are local */
1006 /*****************************************************************************
1007 * CreateWindow: open and set-up X11 main window
1008 *****************************************************************************/
1009 static int CreateWindow( vout_thread_t *p_vout )
1011 XSizeHints xsize_hints;
1012 XSetWindowAttributes xwindow_attributes;
1013 XGCValues xgcvalues;
1017 boolean_t b_configure_notify;
1018 boolean_t b_map_notify;
1020 /* Set main window's size */
1021 if( p_vout->render.i_height * p_vout->render.i_aspect
1022 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1024 p_vout->p_sys->i_width = p_vout->render.i_height
1025 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
1026 p_vout->p_sys->i_height = p_vout->render.i_height;
1030 p_vout->p_sys->i_width = p_vout->render.i_width;
1031 p_vout->p_sys->i_height = p_vout->render.i_width
1032 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
1036 if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 300 )
1038 p_vout->p_sys->i_width <<= 1;
1039 p_vout->p_sys->i_height <<= 1;
1041 else if( p_vout->p_sys->i_width <= 400
1042 && p_vout->p_sys->i_height <= 400 )
1044 p_vout->p_sys->i_width += p_vout->p_sys->i_width >> 1;
1045 p_vout->p_sys->i_height += p_vout->p_sys->i_height >> 1;
1049 /* Prepare window manager hints and properties */
1050 xsize_hints.base_width = p_vout->p_sys->i_width;
1051 xsize_hints.base_height = p_vout->p_sys->i_height;
1052 xsize_hints.flags = PSize;
1053 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
1054 "WM_PROTOCOLS", True );
1055 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
1056 "WM_DELETE_WINDOW", True );
1058 /* Prepare window attributes */
1059 xwindow_attributes.backing_store = Always; /* save the hidden part */
1060 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1061 p_vout->p_sys->i_screen);
1062 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1065 /* Create the window and set hints - the window must receive
1066 * ConfigureNotify events, and until it is displayed, Expose and
1067 * MapNotify events. */
1069 p_vout->p_sys->window =
1070 XCreateWindow( p_vout->p_sys->p_display,
1071 DefaultRootWindow( p_vout->p_sys->p_display ),
1073 p_vout->p_sys->i_width,
1074 p_vout->p_sys->i_height,
1077 CWBackingStore | CWBackPixel | CWEventMask,
1078 &xwindow_attributes );
1080 /* Set window manager hints and properties: size hints, command,
1081 * window's name, and accepted protocols */
1082 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
1084 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
1085 p_main->ppsz_argv, p_main->i_argc );
1086 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
1087 #ifdef MODULE_NAME_IS_x11
1088 VOUT_TITLE " (X11 output)"
1090 VOUT_TITLE " (XVideo output)"
1094 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
1095 || (p_vout->p_sys->wm_delete_window == None)
1096 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
1097 &p_vout->p_sys->wm_delete_window, 1 ) )
1099 /* WM_DELETE_WINDOW is not supported by window manager */
1100 intf_Msg( "vout error: missing or bad window manager" );
1103 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1104 * event when using functions like XCopyArea */
1105 xgcvalues.graphics_exposures = False;
1106 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
1107 p_vout->p_sys->window,
1108 GCGraphicsExposures, &xgcvalues);
1110 /* Send orders to server, and wait until window is displayed - three
1111 * events must be received: a MapNotify event, an Expose event allowing
1112 * drawing in the window, and a ConfigureNotify to get the window
1113 * dimensions. Once those events have been received, only ConfigureNotify
1114 * events need to be received. */
1116 b_configure_notify = 0;
1118 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
1121 XNextEvent( p_vout->p_sys->p_display, &xevent);
1122 if( (xevent.type == Expose)
1123 && (xevent.xexpose.window == p_vout->p_sys->window) )
1127 else if( (xevent.type == MapNotify)
1128 && (xevent.xmap.window == p_vout->p_sys->window) )
1132 else if( (xevent.type == ConfigureNotify)
1133 && (xevent.xconfigure.window == p_vout->p_sys->window) )
1135 b_configure_notify = 1;
1136 p_vout->p_sys->i_width = xevent.xconfigure.width;
1137 p_vout->p_sys->i_height = xevent.xconfigure.height;
1139 } while( !( b_expose && b_configure_notify && b_map_notify ) );
1141 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
1142 StructureNotifyMask | KeyPressMask |
1143 ButtonPressMask | ButtonReleaseMask |
1144 PointerMotionMask );
1146 #ifdef MODULE_NAME_IS_x11
1147 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1149 /* Allocate a new palette */
1150 p_vout->p_sys->colormap =
1151 XCreateColormap( p_vout->p_sys->p_display,
1152 DefaultRootWindow( p_vout->p_sys->p_display ),
1153 DefaultVisual( p_vout->p_sys->p_display,
1154 p_vout->p_sys->i_screen ),
1157 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1158 XChangeWindowAttributes( p_vout->p_sys->p_display,
1159 p_vout->p_sys->window,
1160 CWColormap, &xwindow_attributes );
1164 /* Create YUV output sub-window. */
1165 p_vout->p_sys->yuv_window = XCreateSimpleWindow( p_vout->p_sys->p_display,
1166 p_vout->p_sys->window, 0, 0,
1167 p_vout->p_sys->i_width,
1168 p_vout->p_sys->i_height,
1170 BlackPixel( p_vout->p_sys->p_display,
1171 p_vout->p_sys->i_screen ),
1172 WhitePixel( p_vout->p_sys->p_display,
1173 p_vout->p_sys->i_screen ) );
1175 XSetWindowBackground( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1176 BlackPixel(p_vout->p_sys->p_display, p_vout->p_sys->i_screen ) );
1178 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
1179 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window,
1183 /* If the cursor was formerly blank than blank it again */
1184 if( !p_vout->p_sys->b_mouse_pointer_visible )
1186 ToggleCursor( p_vout );
1187 ToggleCursor( p_vout );
1190 XSync( p_vout->p_sys->p_display, False );
1192 /* At this stage, the window is open, displayed, and ready to
1198 /*****************************************************************************
1199 * DestroyWindow: destroy the window
1200 *****************************************************************************
1202 *****************************************************************************/
1203 static void DestroyWindow( vout_thread_t *p_vout )
1205 XSync( p_vout->p_sys->p_display, False );
1207 #ifdef MODULE_NAME_IS_xvideo
1208 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->yuv_window );
1211 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
1212 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
1213 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
1216 /*****************************************************************************
1217 * NewPicture: allocate a picture
1218 *****************************************************************************
1219 * Returns 0 on success, -1 otherwise
1220 *****************************************************************************/
1221 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1223 /* We know the chroma, allocate a buffer which will be used
1224 * directly by the decoder */
1225 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1227 if( p_pic->p_sys == NULL )
1232 if( p_vout->p_sys->b_shm )
1234 /* Create image using XShm extension */
1235 p_pic->p_sys->p_image =
1236 CreateShmImage( p_vout->p_sys->p_display,
1237 #ifdef MODULE_NAME_IS_xvideo
1238 p_vout->p_sys->i_xvport,
1239 p_vout->p_sys->i_chroma,
1241 p_vout->p_sys->p_visual,
1242 p_vout->p_sys->i_screen_depth,
1244 &p_pic->p_sys->shminfo,
1245 p_vout->output.i_width, p_vout->output.i_height );
1249 /* Create image without XShm extension */
1250 p_pic->p_sys->p_image =
1251 CreateImage( p_vout->p_sys->p_display,
1252 #ifdef MODULE_NAME_IS_xvideo
1253 p_vout->p_sys->i_xvport,
1254 p_vout->p_sys->i_chroma,
1256 p_vout->p_sys->p_visual,
1257 p_vout->p_sys->i_screen_depth,
1258 p_vout->p_sys->i_bytes_per_pixel,
1260 p_vout->output.i_width, p_vout->output.i_height );
1263 if( p_pic->p_sys->p_image == NULL )
1265 free( p_pic->p_sys );
1269 #ifdef MODULE_NAME_IS_xvideo
1270 switch( p_vout->p_sys->i_chroma )
1274 p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1275 + p_pic->p_sys->p_image->offsets[0];
1276 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1277 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1278 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1279 p_pic->p[Y_PLANE].b_margin = 0;
1281 p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1282 + p_pic->p_sys->p_image->offsets[1];
1283 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1284 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1285 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1286 p_pic->p[Y_PLANE].b_margin = 0;
1288 p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1289 + p_pic->p_sys->p_image->offsets[2];
1290 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1291 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1292 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1293 p_pic->p[Y_PLANE].b_margin = 0;
1295 p_pic->i_planes = 3;
1300 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1301 + p_pic->p_sys->p_image->offsets[0];
1302 p_pic->p->i_lines = p_vout->output.i_height;
1303 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1304 p_pic->p->i_pixel_bytes = 1;
1305 p_pic->p->b_margin = 0;
1307 p_pic->i_planes = 1;
1312 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1313 + p_pic->p_sys->p_image->offsets[0];
1314 p_pic->p->i_lines = p_vout->output.i_height;
1315 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1316 p_pic->p->i_pixel_bytes = 2;
1317 p_pic->p->b_margin = 0;
1319 p_pic->i_planes = 1;
1324 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1325 + p_pic->p_sys->p_image->offsets[0];
1326 p_pic->p->i_lines = p_vout->output.i_height;
1327 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1328 p_pic->p->i_pixel_bytes = 2;
1329 p_pic->p->b_margin = 0;
1331 p_pic->p->i_red_mask = 0x001f;
1332 p_pic->p->i_green_mask = 0x03e0;
1333 p_pic->p->i_blue_mask = 0x7c00;
1335 p_pic->i_planes = 1;
1339 /* Unknown chroma, tell the guy to get lost */
1340 IMAGE_FREE( p_pic->p_sys->p_image );
1341 free( p_pic->p_sys );
1342 intf_ErrMsg( "vout error: never heard of chroma 0x%.8x",
1343 p_vout->p_sys->i_chroma );
1344 p_pic->i_planes = 0;
1348 switch( p_vout->output.i_chroma )
1352 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1353 + p_pic->p_sys->p_image->xoffset;
1354 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1355 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1356 p_pic->p->i_pixel_bytes = p_pic->p_sys->p_image->depth;
1358 if( p_pic->p->i_pitch == 2 * p_pic->p_sys->p_image->width )
1360 p_pic->p->b_margin = 0;
1364 p_pic->p->b_margin = 1;
1365 p_pic->p->b_hidden = 1;
1366 p_pic->p->i_visible_bytes = 2 * p_pic->p_sys->p_image->width;
1369 p_pic->p->i_red_mask = p_pic->p_sys->p_image->red_mask;
1370 p_pic->p->i_green_mask = p_pic->p_sys->p_image->green_mask;
1371 p_pic->p->i_blue_mask = p_pic->p_sys->p_image->blue_mask;
1373 p_pic->i_planes = 1;
1378 /* Unknown chroma, tell the guy to get lost */
1379 IMAGE_FREE( p_pic->p_sys->p_image );
1380 free( p_pic->p_sys );
1381 intf_ErrMsg( "vout error: never heard of chroma 0x%.8x",
1382 p_vout->output.i_chroma );
1383 p_pic->i_planes = 0;
1391 /*****************************************************************************
1392 * FreePicture: destroy a picture allocated with NewPicture
1393 *****************************************************************************
1394 * Destroy XImage AND associated data. If using Shm, detach shared memory
1395 * segment from server and process, then free it. The XDestroyImage manpage
1396 * says that both the image structure _and_ the data pointed to by the
1397 * image structure are freed, so no need to free p_image->data.
1398 *****************************************************************************/
1399 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1401 /* The order of operations is correct */
1402 if( p_vout->p_sys->b_shm )
1404 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1405 IMAGE_FREE( p_pic->p_sys->p_image );
1407 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1408 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1410 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1416 IMAGE_FREE( p_pic->p_sys->p_image );
1419 XSync( p_vout->p_sys->p_display, False );
1421 free( p_pic->p_sys );
1424 /*****************************************************************************
1425 * ToggleFullScreen: Enable or disable full screen mode
1426 *****************************************************************************
1427 * This function will switch between fullscreen and window mode.
1429 *****************************************************************************/
1430 static void ToggleFullScreen ( vout_thread_t *p_vout )
1433 mwmhints_t mwmhints;
1434 int i_xpos, i_ypos, i_width, i_height;
1436 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1438 if( p_vout->b_fullscreen )
1440 Window next_parent, parent, *p_dummy, dummy1;
1441 unsigned int dummy2, dummy3;
1443 intf_WarnMsg( 3, "vout: entering fullscreen mode" );
1445 /* Save current window coordinates so they can be restored when
1446 * we exit from fullscreen mode */
1448 /* find the real parent, which means the which is a direct child of
1449 * the root window */
1450 next_parent = parent = p_vout->p_sys->window;
1451 while( next_parent != DefaultRootWindow( p_vout->p_sys->p_display ) )
1453 parent = next_parent;
1454 XQueryTree( p_vout->p_sys->p_display,
1460 XFree((void *)p_dummy);
1463 XGetGeometry( p_vout->p_sys->p_display,
1464 p_vout->p_sys->window,
1468 &p_vout->p_sys->i_width_backup,
1469 &p_vout->p_sys->i_height_backup,
1472 XTranslateCoordinates( p_vout->p_sys->p_display,
1474 DefaultRootWindow( p_vout->p_sys->p_display ),
1477 &p_vout->p_sys->i_xpos_backup,
1478 &p_vout->p_sys->i_ypos_backup,
1481 mwmhints.flags = MWM_HINTS_DECORATIONS;
1482 mwmhints.decorations = 0;
1486 i_width = DisplayWidth( p_vout->p_sys->p_display,
1487 p_vout->p_sys->i_screen );
1488 i_height = DisplayHeight( p_vout->p_sys->p_display,
1489 p_vout->p_sys->i_screen );
1492 /* Being a transient window allows us to really be fullscreen (display
1493 * over the taskbar for instance) but then we end-up with the same
1494 * result as with the brute force method */
1495 XSetTransientForHint( p_vout->p_sys->p_display,
1496 p_vout->p_sys->window, None );
1501 intf_WarnMsg( 3, "vout: leaving fullscreen mode" );
1503 mwmhints.flags = MWM_HINTS_DECORATIONS;
1504 mwmhints.decorations = 1;
1506 i_xpos = p_vout->p_sys->i_xpos_backup;
1507 i_ypos = p_vout->p_sys->i_ypos_backup;
1508 i_width = p_vout->p_sys->i_width_backup;
1509 i_height = p_vout->p_sys->i_height_backup;
1512 /* To my knowledge there are two ways to create a borderless window.
1513 * There's the generic way which is to tell x to bypass the window manager,
1514 * but this creates problems with the focus of other applications.
1515 * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1516 * luckily seems to be supported by most window managers.
1518 prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1520 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
1521 prop, prop, 32, PropModeReplace,
1522 (unsigned char *)&mwmhints,
1523 PROP_MWM_HINTS_ELEMENTS );
1524 #if 0 /* brute force way to remove decorations */
1525 XSetWindowAttributes attributes;
1526 attributes.override_redirect = True;
1527 XChangeWindowAttributes( p_vout->p_sys->p_display,
1528 p_vout->p_sys->window,
1533 /* We need to unmap and remap the window if we want the window
1534 * manager to take our changes into effect */
1535 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
1536 XMapRaised( p_vout->p_sys->p_display, p_vout->p_sys->window);
1537 XMoveResizeWindow( p_vout->p_sys->p_display,
1538 p_vout->p_sys->window,
1543 XFlush( p_vout->p_sys->p_display );
1546 /*****************************************************************************
1547 * EnableXScreenSaver: enable screen saver
1548 *****************************************************************************
1549 * This function enables the screen saver on a display after it has been
1550 * disabled by XDisableScreenSaver.
1551 * FIXME: what happens if multiple vlc sessions are running at the same
1553 *****************************************************************************/
1554 static void EnableXScreenSaver( vout_thread_t *p_vout )
1558 intf_DbgMsg( "vout: enabling screen saver" );
1559 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1560 p_vout->p_sys->i_ss_interval,
1561 p_vout->p_sys->i_ss_blanking,
1562 p_vout->p_sys->i_ss_exposure );
1564 /* Restore DPMS settings */
1565 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1567 if( p_vout->p_sys->b_ss_dpms )
1568 DPMSEnable( p_vout->p_sys->p_display );
1572 /*****************************************************************************
1573 * DisableXScreenSaver: disable screen saver
1574 *****************************************************************************
1575 * See XEnableXScreenSaver
1576 *****************************************************************************/
1577 static void DisableXScreenSaver( vout_thread_t *p_vout )
1581 /* Save screen saver informations */
1582 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1583 &p_vout->p_sys->i_ss_interval,
1584 &p_vout->p_sys->i_ss_blanking,
1585 &p_vout->p_sys->i_ss_exposure );
1587 /* Disable screen saver */
1588 intf_DbgMsg( "vout: disabling screen saver" );
1589 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1590 p_vout->p_sys->i_ss_interval,
1591 p_vout->p_sys->i_ss_blanking,
1592 p_vout->p_sys->i_ss_exposure );
1595 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1598 /* Save DPMS current state */
1599 DPMSInfo( p_vout->p_sys->p_display, &dummy,
1600 &p_vout->p_sys->b_ss_dpms );
1601 intf_DbgMsg( "vout: disabling DPMS" );
1602 DPMSDisable( p_vout->p_sys->p_display );
1606 /*****************************************************************************
1607 * CreateCursor: create a blank mouse pointer
1608 *****************************************************************************/
1609 static void CreateCursor( vout_thread_t *p_vout )
1611 XColor cursor_color;
1613 p_vout->p_sys->cursor_pixmap =
1614 XCreatePixmap( p_vout->p_sys->p_display,
1615 DefaultRootWindow( p_vout->p_sys->p_display ),
1618 XParseColor( p_vout->p_sys->p_display,
1619 XCreateColormap( p_vout->p_sys->p_display,
1621 p_vout->p_sys->p_display ),
1623 p_vout->p_sys->p_display,
1624 p_vout->p_sys->i_screen ),
1626 "black", &cursor_color );
1628 p_vout->p_sys->blank_cursor =
1629 XCreatePixmapCursor( p_vout->p_sys->p_display,
1630 p_vout->p_sys->cursor_pixmap,
1631 p_vout->p_sys->cursor_pixmap,
1632 &cursor_color, &cursor_color, 1, 1 );
1635 /*****************************************************************************
1636 * DestroyCursor: destroy the blank mouse pointer
1637 *****************************************************************************/
1638 static void DestroyCursor( vout_thread_t *p_vout )
1640 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1643 /*****************************************************************************
1644 * ToggleCursor: hide or show the mouse pointer
1645 *****************************************************************************
1646 * This function hides the X pointer if it is visible by setting the pointer
1647 * sprite to a blank one. To show it again, we disable the sprite.
1648 *****************************************************************************/
1649 static void ToggleCursor( vout_thread_t *p_vout )
1651 if( p_vout->p_sys->b_mouse_pointer_visible )
1653 XDefineCursor( p_vout->p_sys->p_display,
1654 p_vout->p_sys->window,
1655 p_vout->p_sys->blank_cursor );
1656 p_vout->p_sys->b_mouse_pointer_visible = 0;
1660 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );
1661 p_vout->p_sys->b_mouse_pointer_visible = 1;
1665 #ifdef MODULE_NAME_IS_xvideo
1666 /*****************************************************************************
1667 * XVideoGetPort: get YUV12 port
1668 *****************************************************************************/
1669 static int XVideoGetPort( Display *dpy, int i_id )
1671 XvAdaptorInfo *p_adaptor;
1673 int i_adaptor, i_num_adaptors, i_requested_adaptor;
1674 int i_selected_port;
1676 switch( XvQueryExtension( dpy, &i, &i, &i, &i, &i ) )
1681 case XvBadExtension:
1682 intf_WarnMsg( 3, "vout error: XvBadExtension" );
1686 intf_WarnMsg( 3, "vout error: XvBadAlloc" );
1690 intf_WarnMsg( 3, "vout error: XvQueryExtension failed" );
1694 switch( XvQueryAdaptors( dpy, DefaultRootWindow( dpy ),
1695 &i_num_adaptors, &p_adaptor ) )
1700 case XvBadExtension:
1701 intf_WarnMsg( 3, "vout error: XvBadExtension for XvQueryAdaptors" );
1705 intf_WarnMsg( 3, "vout error: XvBadAlloc for XvQueryAdaptors" );
1709 intf_WarnMsg( 3, "vout error: XvQueryAdaptors failed" );
1713 i_selected_port = -1;
1714 i_requested_adaptor = main_GetIntVariable( VOUT_XVADAPTOR_VAR, -1 );
1716 /* No special xv port has been requested so try all of them */
1717 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1719 XvImageFormatValues *p_formats;
1720 int i_format, i_num_formats;
1723 /* If we requested an adaptor and it's not this one, we aren't
1725 if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1730 /* If the adaptor doesn't have the required properties, skip it */
1731 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1732 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1737 /* Check that port supports YUV12 planar format... */
1738 p_formats = XvListImageFormats( dpy, p_adaptor[i_adaptor].base_id,
1741 for( i_format = 0; i_format < i_num_formats; i_format++ )
1743 XvEncodingInfo *p_enc;
1744 int i_enc, i_num_encodings;
1745 XvAttribute *p_attr;
1746 int i_attr, i_num_attributes;
1748 /* If this is not the format we want, forget it */
1749 if( p_formats[ i_format ].id != i_id )
1754 /* Look for the first available port supporting this format */
1755 for( i_port = p_adaptor[i_adaptor].base_id;
1756 ( i_port < p_adaptor[i_adaptor].base_id
1757 + p_adaptor[i_adaptor].num_ports )
1758 && ( i_selected_port == -1 );
1761 if( XvGrabPort( dpy, i_port, CurrentTime ) == Success )
1763 i_selected_port = i_port;
1767 /* If no free port was found, forget it */
1768 if( i_selected_port == -1 )
1773 /* If we found a port, print information about it */
1774 intf_WarnMsg( 3, "vout: found adaptor %i, port %i, "
1775 "image format 0x%x (%4.4s) %s",
1776 i_adaptor, i_selected_port,
1777 p_formats[ i_format ].id,
1778 (char *)&p_formats[ i_format ].id,
1779 ( p_formats[ i_format ].format
1780 == XvPacked ) ? "packed" : "planar" );
1782 intf_WarnMsg( 10, " encoding list:" );
1784 if( XvQueryEncodings( dpy, i_selected_port,
1785 &i_num_encodings, &p_enc )
1788 intf_WarnMsg( 10, " XvQueryEncodings failed" );
1792 for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1794 intf_WarnMsg( 10, " id=%ld, name=%s, size=%ldx%ld,"
1795 " numerator=%d, denominator=%d",
1796 p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1797 p_enc[i_enc].width, p_enc[i_enc].height,
1798 p_enc[i_enc].rate.numerator,
1799 p_enc[i_enc].rate.denominator );
1804 XvFreeEncodingInfo( p_enc );
1807 intf_WarnMsg( 10, " attribute list:" );
1808 p_attr = XvQueryPortAttributes( dpy, i_selected_port,
1809 &i_num_attributes );
1810 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1813 " name=%s, flags=[%s%s ], min=%i, max=%i",
1814 p_attr[i_attr].name,
1815 (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1816 (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1817 p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1820 if( p_attr != NULL )
1826 if( p_formats != NULL )
1833 if( i_num_adaptors > 0 )
1835 XvFreeAdaptorInfo( p_adaptor );
1838 if( i_selected_port == -1 )
1840 if( i_requested_adaptor == -1 )
1842 intf_WarnMsg( 3, "vout: no free XVideo port found for format "
1847 intf_WarnMsg( 3, "vout: XVideo adaptor %i does not have a free "
1848 "XVideo port for format 0x%.8x",
1849 i_requested_adaptor, i_id );
1853 return( i_selected_port );
1856 /*****************************************************************************
1857 * XVideoReleasePort: release YUV12 port
1858 *****************************************************************************/
1859 static void XVideoReleasePort( Display *dpy, int i_port )
1861 XvUngrabPort( dpy, i_port, CurrentTime );
1865 /*****************************************************************************
1866 * InitDisplay: open and initialize X11 device
1867 *****************************************************************************
1868 * Create a window according to video output given size, and set other
1869 * properties according to the display properties.
1870 *****************************************************************************/
1871 static int InitDisplay( vout_thread_t *p_vout )
1873 #ifdef MODULE_NAME_IS_x11
1874 XPixmapFormatValues * p_formats; /* pixmap formats */
1875 XVisualInfo * p_xvisual; /* visuals informations */
1876 XVisualInfo xvisual_template; /* visual template */
1877 int i_count; /* array size */
1881 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
1882 p_vout->p_sys->b_shm = 0;
1884 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
1887 if( !p_vout->p_sys->b_shm )
1889 intf_WarnMsg( 1, "vout warning: XShm video extension is unavailable" );
1892 #ifdef MODULE_NAME_IS_xvideo
1893 /* XXX The brightness and contrast values should be read from environment
1894 * XXX variables... */
1896 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1897 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
1901 #ifdef MODULE_NAME_IS_x11
1902 /* Initialize structure */
1903 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1905 /* Get screen depth */
1906 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1907 p_vout->p_sys->i_screen );
1908 switch( p_vout->p_sys->i_screen_depth )
1912 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1914 xvisual_template.screen = p_vout->p_sys->i_screen;
1915 xvisual_template.class = DirectColor;
1916 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1917 VisualScreenMask | VisualClassMask,
1918 &xvisual_template, &i_count );
1919 if( p_xvisual == NULL )
1921 intf_ErrMsg( "vout error: no PseudoColor visual available" );
1924 p_vout->p_sys->i_bytes_per_pixel = 1;
1931 * Screen depth is higher than 8bpp. TrueColor visual is used.
1933 xvisual_template.screen = p_vout->p_sys->i_screen;
1934 xvisual_template.class = TrueColor;
1935 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1936 VisualScreenMask | VisualClassMask,
1937 &xvisual_template, &i_count );
1938 if( p_xvisual == NULL )
1940 intf_ErrMsg( "vout error: no TrueColor visual available" );
1943 p_vout->p_sys->i_red_mask = p_xvisual->red_mask;
1944 p_vout->p_sys->i_green_mask = p_xvisual->green_mask;
1945 p_vout->p_sys->i_blue_mask = p_xvisual->blue_mask;
1947 /* There is no difference yet between 3 and 4 Bpp. The only way
1948 * to find the actual number of bytes per pixel is to list supported
1949 * pixmap formats. */
1950 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1951 p_vout->p_sys->i_bytes_per_pixel = 0;
1953 for( ; i_count-- ; p_formats++ )
1955 /* Under XFree4.0, the list contains pixmap formats available
1956 * through all video depths ; so we have to check against current
1958 if( p_formats->depth == p_vout->p_sys->i_screen_depth )
1960 if( p_formats->bits_per_pixel / 8
1961 > p_vout->p_sys->i_bytes_per_pixel )
1963 p_vout->p_sys->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
1969 p_vout->p_sys->p_visual = p_xvisual->visual;
1976 /*****************************************************************************
1977 * CreateShmImage: create an XImage or XvImage using shared memory extension
1978 *****************************************************************************
1979 * Prepare an XImage or XvImage for display function.
1980 * The order of the operations respects the recommandations of the mit-shm
1981 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1982 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1983 *****************************************************************************/
1984 static IMAGE_TYPE * CreateShmImage( Display* p_display, EXTRA_ARGS_SHM,
1985 int i_width, int i_height )
1987 IMAGE_TYPE *p_image;
1989 /* Create XImage / XvImage */
1990 #ifdef MODULE_NAME_IS_xvideo
1991 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1992 i_width, i_height, p_shm );
1994 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1995 p_shm, i_width, i_height );
1997 if( p_image == NULL )
1999 intf_ErrMsg( "vout error: image creation failed." );
2003 /* Allocate shared memory segment - 0776 set the access permission
2004 * rights (like umask), they are not yet supported by all X servers */
2005 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
2006 if( p_shm->shmid < 0 )
2008 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
2009 strerror( errno ) );
2010 IMAGE_FREE( p_image );
2014 /* Attach shared memory segment to process (read/write) */
2015 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2016 if(! p_shm->shmaddr )
2018 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
2020 IMAGE_FREE( p_image );
2021 shmctl( p_shm->shmid, IPC_RMID, 0 );
2025 /* Read-only data. We won't be using XShmGetImage */
2026 p_shm->readOnly = True;
2028 /* Attach shared memory segment to X server */
2029 if( XShmAttach( p_display, p_shm ) == False )
2031 intf_ErrMsg( "vout error: cannot attach shared memory to X server" );
2032 IMAGE_FREE( p_image );
2033 shmctl( p_shm->shmid, IPC_RMID, 0 );
2034 shmdt( p_shm->shmaddr );
2038 /* Send image to X server. This instruction is required, since having
2039 * built a Shm XImage and not using it causes an error on XCloseDisplay */
2040 XSync( p_display, False );
2043 /* Mark the shm segment to be removed when there are no more
2044 * attachements, so it is automatic on process exit or after shmdt */
2045 shmctl( p_shm->shmid, IPC_RMID, 0 );
2051 /*****************************************************************************
2052 * CreateImage: create an XImage or XvImage
2053 *****************************************************************************
2054 * Create a simple image used as a buffer.
2055 *****************************************************************************/
2056 static IMAGE_TYPE * CreateImage( Display *p_display, EXTRA_ARGS,
2057 int i_width, int i_height )
2059 byte_t * p_data; /* image data storage zone */
2060 IMAGE_TYPE *p_image;
2061 #ifdef MODULE_NAME_IS_x11
2062 int i_quantum; /* XImage quantum (see below) */
2063 int i_bytes_per_line;
2066 /* Allocate memory for image */
2067 #ifdef MODULE_NAME_IS_xvideo
2068 p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2070 i_bytes_per_line = i_width * i_bytes_per_pixel;
2071 p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2075 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
2079 #ifdef MODULE_NAME_IS_x11
2080 /* Optimize the quantum of a scanline regarding its size - the quantum is
2081 a diviser of the number of bits between the start of two scanlines. */
2082 if( i_bytes_per_line & 0xf )
2086 else if( i_bytes_per_line & 0x10 )
2096 /* Create XImage. p_data will be automatically freed */
2097 #ifdef MODULE_NAME_IS_xvideo
2098 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2099 p_data, i_width, i_height );
2101 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2102 p_data, i_width, i_height, i_quantum, 0 );
2104 if( p_image == NULL )
2106 intf_ErrMsg( "vout error: XCreateImage() failed" );