1 /*****************************************************************************
2 * vout_x11.c: X11 video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
5 * $Id: vout_x11.c,v 1.17 2001/04/01 06:21:44 sam Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #define MODULE_NAME x11
26 #include "modules_inner.h"
28 /*****************************************************************************
30 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
34 #include <stdlib.h> /* free() */
35 #include <string.h> /* strerror() */
37 #ifdef HAVE_MACHINE_PARAM_H
39 #include <machine/param.h>
40 #include <sys/types.h> /* typedef ushort */
44 #include <sys/shm.h> /* shmget(), shmctl() */
46 #include <X11/Xutil.h>
47 #include <X11/keysym.h>
48 #include <X11/extensions/XShm.h>
58 #include "video_output.h"
60 #include "interface.h"
65 /*****************************************************************************
66 * vout_sys_t: video output X11 method descriptor
67 *****************************************************************************
68 * This structure is part of the video output thread descriptor.
69 * It describes the X11 specific properties of an output thread. X11 video
70 * output is performed through regular resizable windows. Windows can be
71 * dynamically resized to adapt to the size of the streams.
72 *****************************************************************************/
73 typedef struct vout_sys_s
76 boolean_t b_shm; /* shared memory extension flag */
78 /* Internal settings and properties */
79 Display * p_display; /* display pointer */
80 Visual * p_visual; /* visual pointer */
81 int i_screen; /* screen number */
82 Window window; /* root window */
83 GC gc; /* graphic context instance handler */
84 Colormap colormap; /* colormap used (8bpp only) */
86 /* Display buffers and shared memory information */
87 XImage * p_ximage[2]; /* XImage pointer */
88 XShmSegmentInfo shm_info[2]; /* shared memory zone information */
90 /* X11 generic properties */
92 Atom wm_delete_window;
94 int i_width; /* width of main window */
95 int i_height; /* height of main window */
97 /* Screen saver properties */
98 int i_ss_timeout; /* timeout */
99 int i_ss_interval; /* interval between changes */
100 int i_ss_blanking; /* blanking mode */
101 int i_ss_exposure; /* exposure mode */
103 /* Mouse pointer properties */
104 boolean_t b_mouse; /* is the mouse pointer displayed ? */
108 /*****************************************************************************
110 *****************************************************************************/
111 static int vout_Probe ( probedata_t *p_data );
112 static int vout_Create ( struct vout_thread_s * );
113 static int vout_Init ( struct vout_thread_s * );
114 static void vout_End ( struct vout_thread_s * );
115 static void vout_Destroy ( struct vout_thread_s * );
116 static int vout_Manage ( struct vout_thread_s * );
117 static void vout_Display ( struct vout_thread_s * );
118 static void vout_SetPalette( struct vout_thread_s *, u16*, u16*, u16*, u16* );
120 static int X11CreateWindow ( vout_thread_t *p_vout );
121 static int X11InitDisplay ( vout_thread_t *p_vout, char *psz_display );
123 static int X11CreateImage ( vout_thread_t *p_vout, XImage **pp_ximage );
124 static void X11DestroyImage ( XImage *p_ximage );
125 static int X11CreateShmImage ( vout_thread_t *p_vout, XImage **pp_ximage,
126 XShmSegmentInfo *p_shm_info );
127 static void X11DestroyShmImage ( vout_thread_t *p_vout, XImage *p_ximage,
128 XShmSegmentInfo *p_shm_info );
130 static void X11TogglePointer ( vout_thread_t *p_vout );
131 static void X11EnableScreenSaver ( vout_thread_t *p_vout );
132 static void X11DisableScreenSaver ( vout_thread_t *p_vout );
134 /*****************************************************************************
135 * Functions exported as capabilities. They are declared as static so that
136 * we don't pollute the namespace too much.
137 *****************************************************************************/
138 void _M( vout_getfunctions )( function_list_t * p_function_list )
140 p_function_list->pf_probe = vout_Probe;
141 p_function_list->functions.vout.pf_create = vout_Create;
142 p_function_list->functions.vout.pf_init = vout_Init;
143 p_function_list->functions.vout.pf_end = vout_End;
144 p_function_list->functions.vout.pf_destroy = vout_Destroy;
145 p_function_list->functions.vout.pf_manage = vout_Manage;
146 p_function_list->functions.vout.pf_display = vout_Display;
147 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
150 /*****************************************************************************
151 * vout_Probe: probe the video driver and return a score
152 *****************************************************************************
153 * This function tries to initialize SDL and returns a score to the
154 * plugin manager so that it can select the best plugin.
155 *****************************************************************************/
156 static int vout_Probe( probedata_t *p_data )
158 if( TestMethod( VOUT_METHOD_VAR, "x11" ) )
166 /*****************************************************************************
167 * vout_Create: allocate X11 video thread output method
168 *****************************************************************************
169 * This function allocate and initialize a X11 vout method. It uses some of the
170 * vout properties to choose the window size, and change them according to the
171 * actual properties of the display.
172 *****************************************************************************/
173 static int vout_Create( vout_thread_t *p_vout )
177 /* Allocate structure */
178 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
179 if( p_vout->p_sys == NULL )
181 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
185 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
186 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
187 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
189 if( p_vout->p_sys->p_display == NULL ) /* error */
191 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
192 free( p_vout->p_sys );
195 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
197 /* Spawn base window - this window will include the video output window,
198 * but also command buttons, subtitles and other indicators */
199 if( X11CreateWindow( p_vout ) )
201 intf_ErrMsg( "vout error: cannot create X11 window" );
202 XCloseDisplay( p_vout->p_sys->p_display );
203 free( p_vout->p_sys );
207 /* Open and initialize device. This function issues its own error messages.
208 * Since XLib is usually not thread-safe, we can't use the same display
209 * pointer than the interface or another thread. However, the root window
210 * id is still valid. */
211 if( X11InitDisplay( p_vout, psz_display ) )
213 intf_ErrMsg( "vout error: cannot initialize X11 display" );
214 XCloseDisplay( p_vout->p_sys->p_display );
215 free( p_vout->p_sys );
219 p_vout->p_sys->b_mouse = 1;
221 /* Disable screen saver and return */
222 X11DisableScreenSaver( p_vout );
227 /*****************************************************************************
228 * vout_Init: initialize X11 video thread output method
229 *****************************************************************************
230 * This function create the XImages needed by the output thread. It is called
231 * at the beginning of the thread, but also each time the window is resized.
232 *****************************************************************************/
233 static int vout_Init( vout_thread_t *p_vout )
238 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
239 p_vout->p_sys->b_shm = 0;
242 /* Create XImages using XShm extension - on failure, fall back to regular
243 * way (and destroy the first image if it was created successfully) */
244 if( p_vout->p_sys->b_shm )
246 /* Create first image */
247 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
248 &p_vout->p_sys->shm_info[0] );
249 if( !i_err ) /* first image has been created */
251 /* Create second image */
252 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
253 &p_vout->p_sys->shm_info[1] ) )
254 { /* error creating the second image */
255 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
256 &p_vout->p_sys->shm_info[0] );
260 if( i_err ) /* an error occured */
262 intf_Msg( "vout: XShm video extension unavailable" );
263 p_vout->p_sys->b_shm = 0;
267 /* Create XImages without XShm extension */
268 if( !p_vout->p_sys->b_shm )
270 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
272 intf_ErrMsg( "vout error: cannot create images" );
273 p_vout->p_sys->p_ximage[0] = NULL;
274 p_vout->p_sys->p_ximage[1] = NULL;
277 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
279 intf_ErrMsg( "vout error: cannot create images" );
280 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
281 p_vout->p_sys->p_ximage[0] = NULL;
282 p_vout->p_sys->p_ximage[1] = NULL;
287 /* Set bytes per line and initialize buffers */
288 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
289 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
290 p_vout->p_sys->p_ximage[ 1 ]->data );
294 /*****************************************************************************
295 * vout_End: terminate X11 video thread output method
296 *****************************************************************************
297 * Destroy the X11 XImages created by vout_Init. It is called at the end of
298 * the thread, but also each time the window is resized.
299 *****************************************************************************/
300 static void vout_End( vout_thread_t *p_vout )
302 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
304 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
305 &p_vout->p_sys->shm_info[0] );
306 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
307 &p_vout->p_sys->shm_info[1] );
309 else /* ...or regular XImages */
311 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
312 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
316 /*****************************************************************************
317 * vout_Destroy: destroy X11 video thread output method
318 *****************************************************************************
319 * Terminate an output method created by vout_CreateOutputMethod
320 *****************************************************************************/
321 static void vout_Destroy( vout_thread_t *p_vout )
323 /* Enable screen saver */
324 X11EnableScreenSaver( p_vout );
326 /* Destroy colormap */
327 if( p_vout->i_screen_depth == 8 )
329 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
333 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
334 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
335 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
337 XCloseDisplay( p_vout->p_sys->p_display );
339 /* Destroy structure */
340 free( p_vout->p_sys );
343 /*****************************************************************************
344 * vout_Manage: handle X11 events
345 *****************************************************************************
346 * This function should be called regularly by video output thread. It manages
347 * X11 events and allows window resizing. It returns a non null value on
349 *****************************************************************************/
350 static int vout_Manage( vout_thread_t *p_vout )
352 XEvent xevent; /* X11 event */
353 boolean_t b_resized; /* window has been resized */
354 char i_key; /* ISO Latin-1 key */
356 /* Handle X11 events: ConfigureNotify events are parsed to know if the
357 * output window's size changed, MapNotify and UnmapNotify to know if the
358 * window is mapped (and if the display is useful), and ClientMessages
359 * to intercept window destruction requests */
361 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
362 StructureNotifyMask | KeyPressMask |
363 ButtonPressMask | ButtonReleaseMask, &xevent )
366 /* ConfigureNotify event: prepare */
367 if( (xevent.type == ConfigureNotify)
368 && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
369 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
371 /* Update dimensions */
373 p_vout->p_sys->i_width = xevent.xconfigure.width;
374 p_vout->p_sys->i_height = xevent.xconfigure.height;
376 /* MapNotify event: change window status and disable screen saver */
377 else if( xevent.type == MapNotify)
379 if( (p_vout != NULL) && !p_vout->b_active )
381 X11DisableScreenSaver( p_vout );
382 p_vout->b_active = 1;
385 /* UnmapNotify event: change window status and enable screen saver */
386 else if( xevent.type == UnmapNotify )
388 if( (p_vout != NULL) && p_vout->b_active )
390 X11EnableScreenSaver( p_vout );
391 p_vout->b_active = 0;
395 else if( xevent.type == KeyPress )
397 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
399 /* FIXME: handle stuff here */
403 /* FIXME: need locking ! */
404 p_main->p_intf->b_die = 1;
410 else if( xevent.type == ButtonPress )
412 switch( ((XButtonEvent *)&xevent)->button )
415 /* in this part we will eventually manage
416 * clicks for DVD navigation for instance */
420 X11TogglePointer( p_vout );
425 else if( xevent.type == ButtonRelease )
427 switch( ((XButtonEvent *)&xevent)->button )
430 /* FIXME: need locking ! */
431 p_main->p_intf->b_menu_change = 1;
439 intf_DbgMsg( "%p -> unhandled event type %d received",
440 p_vout, xevent.type );
445 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
446 * are handled - according to the man pages, the format is always 32
448 while( XCheckTypedEvent( p_vout->p_sys->p_display,
449 ClientMessage, &xevent ) )
451 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
452 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
454 p_main->p_intf->b_die = 1;
458 intf_DbgMsg( "%p -> unhandled ClientMessage received", p_vout );
463 * Handle vout window resizing
467 /* If interface window has been resized, change vout size */
468 intf_DbgMsg( "resizing output window" );
469 p_vout->i_width = p_vout->p_sys->i_width;
470 p_vout->i_height = p_vout->p_sys->i_height;
471 p_vout->i_changes |= VOUT_SIZE_CHANGE;
473 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
474 (p_vout->i_height != p_vout->p_sys->i_height) )
476 /* If video output size has changed, change interface window size */
477 intf_DbgMsg( "resizing output window" );
478 p_vout->p_sys->i_width = p_vout->i_width;
479 p_vout->p_sys->i_height = p_vout->i_height;
480 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
481 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
484 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
486 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
487 && (p_vout->i_screen_depth == 8) )
489 /* FIXME: clear flags ?? */
495 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
497 intf_DbgMsg( "vout info: resizing window" );
498 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
501 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
502 p_vout->i_width, p_vout->i_height );
504 /* Destroy XImages to change their size */
507 /* Recreate XImages. If SysInit failed, the thread can't go on. */
508 if( vout_Init( p_vout ) )
510 intf_ErrMsg( "vout error: cannot resize display" );
514 /* Tell the video output thread that it will need to rebuild YUV
515 * tables. This is needed since conversion buffer size may have
517 p_vout->i_changes |= VOUT_YUV_CHANGE;
518 intf_Msg( "vout: video display resized (%dx%d)",
519 p_vout->i_width, p_vout->i_height);
525 /*****************************************************************************
526 * vout_Display: displays previously rendered output
527 *****************************************************************************
528 * This function send the currently rendered image to X11 server, wait until
529 * it is displayed and switch the two rendering buffer, preparing next frame.
530 *****************************************************************************/
531 static void vout_Display( vout_thread_t *p_vout )
533 if( p_vout->p_sys->b_shm) /* XShm is used */
535 /* Display rendered image using shared memory extension */
536 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
537 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
539 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
540 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
542 /* Send the order to the X server */
543 XSync(p_vout->p_sys->p_display, False);
545 else /* regular X11 capabilities are used */
547 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
548 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
550 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
551 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
553 /* Send the order to the X server */
554 XSync(p_vout->p_sys->p_display, False);
558 /*****************************************************************************
559 * vout_SetPalette: sets an 8 bpp palette
560 *****************************************************************************
561 * This function sets the palette given as an argument. It does not return
562 * anything, but could later send information on which colors it was unable
564 *****************************************************************************/
565 static void vout_SetPalette( p_vout_thread_t p_vout,
566 u16 *red, u16 *green, u16 *blue, u16 *transp )
569 XColor p_colors[255];
571 intf_DbgMsg( "Palette change called" );
573 /* allocate palette */
574 for( i = 0, j = 255; i < 255; i++, j-- )
576 /* kludge: colors are indexed reversely because color 255 seems
577 * to be reserved for black even if we try to set it to white */
578 p_colors[ i ].pixel = j;
579 p_colors[ i ].pad = 0;
580 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
581 p_colors[ i ].red = red[ j ];
582 p_colors[ i ].blue = blue[ j ];
583 p_colors[ i ].green = green[ j ];
586 XStoreColors( p_vout->p_sys->p_display,
587 p_vout->p_sys->colormap, p_colors, 256 );
590 /* following functions are local */
592 /*****************************************************************************
593 * X11CreateWindow: open and set-up X11 main window
594 *****************************************************************************/
595 static int X11CreateWindow( vout_thread_t *p_vout )
597 XSizeHints xsize_hints;
598 XSetWindowAttributes xwindow_attributes;
602 boolean_t b_configure_notify;
603 boolean_t b_map_notify;
605 /* Set main window's size */
606 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
607 VOUT_WIDTH_DEFAULT );
608 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
609 VOUT_HEIGHT_DEFAULT );
611 /* Prepare window manager hints and properties */
612 xsize_hints.base_width = p_vout->p_sys->i_width;
613 xsize_hints.base_height = p_vout->p_sys->i_height;
614 xsize_hints.flags = PSize;
615 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
616 "WM_PROTOCOLS", True );
617 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
618 "WM_DELETE_WINDOW", True );
620 /* Prepare window attributes */
621 xwindow_attributes.backing_store = Always; /* save the hidden part */
622 xwindow_attributes.background_pixel = WhitePixel( p_vout->p_sys->p_display,
623 p_vout->p_sys->i_screen );
625 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
627 /* Create the window and set hints - the window must receive ConfigureNotify
628 * events, and, until it is displayed, Expose and MapNotify events. */
629 p_vout->p_sys->window =
630 XCreateWindow( p_vout->p_sys->p_display,
631 DefaultRootWindow( p_vout->p_sys->p_display ),
633 p_vout->p_sys->i_width, p_vout->p_sys->i_height, 1,
635 CWBackingStore | CWBackPixel | CWEventMask,
636 &xwindow_attributes );
638 /* Set window manager hints and properties: size hints, command,
639 * window's name, and accepted protocols */
640 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
642 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
643 p_main->ppsz_argv, p_main->i_argc );
644 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
645 VOUT_TITLE " (X11 output)" );
647 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
648 || (p_vout->p_sys->wm_delete_window == None)
649 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
650 &p_vout->p_sys->wm_delete_window, 1 ) )
652 /* WM_DELETE_WINDOW is not supported by window manager */
653 intf_Msg( "intf error: missing or bad window manager" );
656 /* Creation of a graphic context that doesn't generate a GraphicsExpose
657 * event when using functions like XCopyArea */
658 xgcvalues.graphics_exposures = False;
659 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
660 p_vout->p_sys->window,
661 GCGraphicsExposures, &xgcvalues);
663 /* Send orders to server, and wait until window is displayed - three
664 * events must be received: a MapNotify event, an Expose event allowing
665 * drawing in the window, and a ConfigureNotify to get the window
666 * dimensions. Once those events have been received, only ConfigureNotify
667 * events need to be received. */
669 b_configure_notify = 0;
671 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
674 XNextEvent( p_vout->p_sys->p_display, &xevent);
675 if( (xevent.type == Expose)
676 && (xevent.xexpose.window == p_vout->p_sys->window) )
680 else if( (xevent.type == MapNotify)
681 && (xevent.xmap.window == p_vout->p_sys->window) )
685 else if( (xevent.type == ConfigureNotify)
686 && (xevent.xconfigure.window == p_vout->p_sys->window) )
688 b_configure_notify = 1;
689 p_vout->p_sys->i_width = xevent.xconfigure.width;
690 p_vout->p_sys->i_height = xevent.xconfigure.height;
692 } while( !( b_expose && b_configure_notify && b_map_notify ) );
694 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
695 StructureNotifyMask | KeyPressMask |
696 ButtonPressMask | ButtonReleaseMask );
698 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
700 /* Allocate a new palette */
701 p_vout->p_sys->colormap =
702 XCreateColormap( p_vout->p_sys->p_display,
703 DefaultRootWindow( p_vout->p_sys->p_display ),
704 DefaultVisual( p_vout->p_sys->p_display,
705 p_vout->p_sys->i_screen ),
708 xwindow_attributes.colormap = p_vout->p_sys->colormap;
709 XChangeWindowAttributes( p_vout->p_sys->p_display,
710 p_vout->p_sys->window,
711 CWColormap, &xwindow_attributes );
714 /* At this stage, the window is open, displayed, and ready to
719 /*****************************************************************************
720 * X11InitDisplay: open and initialize X11 device
721 *****************************************************************************
722 * Create a window according to video output given size, and set other
723 * properties according to the display properties.
724 *****************************************************************************/
725 static int X11InitDisplay( vout_thread_t *p_vout, char *psz_display )
727 XPixmapFormatValues * p_formats; /* pixmap formats */
728 XVisualInfo * p_xvisual; /* visuals informations */
729 XVisualInfo xvisual_template; /* visual template */
730 int i_count; /* array size */
732 /* Initialize structure */
733 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
734 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
736 if( !p_vout->p_sys->b_shm )
738 intf_Msg( "vout: XShm video extension is not available" );
741 /* Get screen depth */
742 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
743 p_vout->p_sys->i_screen );
744 switch( p_vout->i_screen_depth )
748 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
750 xvisual_template.screen = p_vout->p_sys->i_screen;
751 xvisual_template.class = DirectColor;
752 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
753 VisualScreenMask | VisualClassMask,
754 &xvisual_template, &i_count );
755 if( p_xvisual == NULL )
757 intf_ErrMsg( "vout error: no PseudoColor visual available" );
760 p_vout->i_bytes_per_pixel = 1;
767 * Screen depth is higher than 8bpp. TrueColor visual is used.
769 xvisual_template.screen = p_vout->p_sys->i_screen;
770 xvisual_template.class = TrueColor;
771 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
772 VisualScreenMask | VisualClassMask,
773 &xvisual_template, &i_count );
774 if( p_xvisual == NULL )
776 intf_ErrMsg( "vout error: no TrueColor visual available" );
779 p_vout->i_red_mask = p_xvisual->red_mask;
780 p_vout->i_green_mask = p_xvisual->green_mask;
781 p_vout->i_blue_mask = p_xvisual->blue_mask;
783 /* There is no difference yet between 3 and 4 Bpp. The only way
784 * to find the actual number of bytes per pixel is to list supported
786 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
787 p_vout->i_bytes_per_pixel = 0;
789 for( ; i_count-- ; p_formats++ )
791 /* Under XFree4.0, the list contains pixmap formats available
792 * through all video depths ; so we have to check against current
794 if( p_formats->depth == p_vout->i_screen_depth )
796 if( p_formats->bits_per_pixel / 8
797 > p_vout->i_bytes_per_pixel )
799 p_vout->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
805 p_vout->p_sys->p_visual = p_xvisual->visual;
811 /*****************************************************************************
812 * X11CreateImage: create an XImage
813 *****************************************************************************
814 * Create a simple XImage used as a buffer.
815 *****************************************************************************/
816 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
818 byte_t * pb_data; /* image data storage zone */
819 int i_quantum; /* XImage quantum (see below) */
821 /* Allocate memory for image */
822 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
823 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
824 if( !pb_data ) /* error */
826 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
830 /* Optimize the quantum of a scanline regarding its size - the quantum is
831 a diviser of the number of bits between the start of two scanlines. */
832 if( !(( p_vout->i_bytes_per_line ) % 32) )
838 if( !(( p_vout->i_bytes_per_line ) % 16) )
849 *pp_ximage = XCreateImage( p_vout->p_sys->p_display,
850 p_vout->p_sys->p_visual, p_vout->i_screen_depth,
852 p_vout->i_width, p_vout->i_height, i_quantum, 0);
853 if(! *pp_ximage ) /* error */
855 intf_ErrMsg( "error: XCreateImage() failed" );
863 /*****************************************************************************
864 * X11CreateShmImage: create an XImage using shared memory extension
865 *****************************************************************************
866 * Prepare an XImage for DisplayX11ShmImage function.
867 * The order of the operations respects the recommandations of the mit-shm
868 * document by J.Corbet and K.Packard. Most of the parameters were copied from
870 *****************************************************************************/
871 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
872 XShmSegmentInfo *p_shm_info)
876 XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
877 p_vout->i_screen_depth, ZPixmap, 0,
878 p_shm_info, p_vout->i_width, p_vout->i_height );
879 if(! *pp_ximage ) /* error */
881 intf_ErrMsg( "vout error: XShmCreateImage() failed" );
885 /* Allocate shared memory segment - 0777 set the access permission
886 * rights (like umask), they are not yet supported by X servers */
888 shmget( IPC_PRIVATE, (*pp_ximage)->bytes_per_line
889 * (*pp_ximage)->height, IPC_CREAT | 0777);
890 if( p_shm_info->shmid < 0) /* error */
892 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
894 XDestroyImage( *pp_ximage );
898 /* Attach shared memory segment to process (read/write) */
899 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
900 if(! p_shm_info->shmaddr )
902 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
904 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
905 XDestroyImage( *pp_ximage );
909 /* Mark the shm segment to be removed when there will be no more
910 * attachements, so it is automatic on process exit or after shmdt */
911 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
913 /* Attach shared memory segment to X server (read only) */
914 p_shm_info->readOnly = True;
915 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info )
916 == False ) /* error */
918 intf_ErrMsg( "vout error: cannot attach shared memory to X11 server" );
919 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
920 * and automatic free */
921 XDestroyImage( *pp_ximage );
925 /* Send image to X server. This instruction is required, since having
926 * built a Shm XImage and not using it causes an error on XCloseDisplay */
927 XFlush( p_vout->p_sys->p_display );
931 /*****************************************************************************
932 * X11DestroyImage: destroy an XImage
933 *****************************************************************************
934 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
935 * destroyed (see vout_ManageOutputMethod())
936 *****************************************************************************/
937 static void X11DestroyImage( XImage *p_ximage )
939 if( p_ximage != NULL )
941 XDestroyImage( p_ximage ); /* no free() required */
945 /*****************************************************************************
947 *****************************************************************************
948 * Destroy XImage AND associated data. Detach shared memory segment from
949 * server and process, then free it. If pointer is NULL, the image won't be
950 * destroyed (see vout_ManageOutputMethod())
951 *****************************************************************************/
952 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
953 XShmSegmentInfo *p_shm_info )
955 /* If pointer is NULL, do nothing */
956 if( p_ximage == NULL )
961 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
962 XDestroyImage( p_ximage );
964 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
965 { /* also automatic freeing... */
966 intf_ErrMsg( "error: cannot detach shared memory (%s)",
974 /*****************************************************************************
975 * X11EnableScreenSaver: enable screen saver
976 *****************************************************************************
977 * This function enable the screen saver on a display after it had been
978 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
979 * know wether the screen saver can be activated or not: if n successive calls
980 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
981 * will be required before the screen saver could effectively be activated.
982 *****************************************************************************/
983 void X11EnableScreenSaver( vout_thread_t *p_vout )
985 intf_DbgMsg( "intf: enabling screen saver" );
986 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
987 p_vout->p_sys->i_ss_interval,
988 p_vout->p_sys->i_ss_blanking,
989 p_vout->p_sys->i_ss_exposure );
992 /*****************************************************************************
993 * X11DisableScreenSaver: disable screen saver
994 *****************************************************************************
995 * See XEnableScreenSaver
996 *****************************************************************************/
997 void X11DisableScreenSaver( vout_thread_t *p_vout )
999 /* Save screen saver informations */
1000 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1001 &p_vout->p_sys->i_ss_interval,
1002 &p_vout->p_sys->i_ss_blanking,
1003 &p_vout->p_sys->i_ss_exposure );
1005 /* Disable screen saver */
1006 intf_DbgMsg( "vout: disabling screen saver" );
1007 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1008 p_vout->p_sys->i_ss_interval,
1009 p_vout->p_sys->i_ss_blanking,
1010 p_vout->p_sys->i_ss_exposure );
1013 /*****************************************************************************
1014 * X11TogglePointer: hide or show the mouse pointer
1015 *****************************************************************************
1016 * This function hides the X pointer if it is visible by putting it at
1017 * coordinates (32,32) and setting the pointer sprite to a blank one. To
1018 * show it again, we disable the sprite and restore the original coordinates.
1019 *****************************************************************************/
1020 void X11TogglePointer( vout_thread_t *p_vout )
1022 static Cursor cursor;
1023 static boolean_t b_cursor = 0;
1025 if( p_vout->p_sys->b_mouse )
1027 p_vout->p_sys->b_mouse = 0;
1032 Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1033 DefaultRootWindow(p_vout->p_sys->p_display),
1036 XParseColor( p_vout->p_sys->p_display,
1037 XCreateColormap( p_vout->p_sys->p_display,
1039 p_vout->p_sys->p_display ),
1041 p_vout->p_sys->p_display,
1042 p_vout->p_sys->i_screen ),
1046 cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1047 blank, blank, &color, &color, 1, 1 );
1051 XDefineCursor( p_vout->p_sys->p_display,
1052 p_vout->p_sys->window, cursor );
1056 p_vout->p_sys->b_mouse = 1;
1058 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );