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.20 2001/04/21 22:49:24 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 #define MODULE_NAME x11
27 #include "modules_inner.h"
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <errno.h> /* ENOMEM */
35 #include <stdlib.h> /* free() */
36 #include <string.h> /* strerror() */
38 #ifdef HAVE_MACHINE_PARAM_H
40 #include <machine/param.h>
41 #include <sys/types.h> /* typedef ushort */
45 #include <sys/shm.h> /* shmget(), shmctl() */
47 #include <X11/Xutil.h>
48 #include <X11/keysym.h>
49 #include <X11/extensions/XShm.h>
59 #include "video_output.h"
61 #include "interface.h"
64 #include "netutils.h" /* network_ChannelJoin */
68 /*****************************************************************************
69 * vout_sys_t: video output X11 method descriptor
70 *****************************************************************************
71 * This structure is part of the video output thread descriptor.
72 * It describes the X11 specific properties of an output thread. X11 video
73 * output is performed through regular resizable windows. Windows can be
74 * dynamically resized to adapt to the size of the streams.
75 *****************************************************************************/
76 typedef struct vout_sys_s
79 boolean_t b_shm; /* shared memory extension flag */
81 /* Internal settings and properties */
82 Display * p_display; /* display pointer */
83 Visual * p_visual; /* visual pointer */
84 int i_screen; /* screen number */
85 Window window; /* root window */
86 GC gc; /* graphic context instance handler */
87 Colormap colormap; /* colormap used (8bpp only) */
89 /* Display buffers and shared memory information */
90 XImage * p_ximage[2]; /* XImage pointer */
91 XShmSegmentInfo shm_info[2]; /* shared memory zone information */
93 /* X11 generic properties */
95 Atom wm_delete_window;
97 int i_width; /* width of main window */
98 int i_height; /* height of main window */
100 /* Screen saver properties */
101 int i_ss_timeout; /* timeout */
102 int i_ss_interval; /* interval between changes */
103 int i_ss_blanking; /* blanking mode */
104 int i_ss_exposure; /* exposure mode */
106 /* Auto-hide cursor */
109 /* Mouse pointer properties */
110 boolean_t b_mouse; /* is the mouse pointer displayed ? */
112 /* Displaying fullscreen */
113 boolean_t b_fullscreen;
117 /* Fullscreen needs to be able to hide the wm decorations */
118 #define MWM_HINTS_DECORATIONS (1L << 1)
119 #define PROP_MWM_HINTS_ELEMENTS 5
120 typedef struct mwmhints_s
129 /*****************************************************************************
131 *****************************************************************************/
132 static int vout_Probe ( probedata_t *p_data );
133 static int vout_Create ( struct vout_thread_s * );
134 static int vout_Init ( struct vout_thread_s * );
135 static void vout_End ( struct vout_thread_s * );
136 static void vout_Destroy ( struct vout_thread_s * );
137 static int vout_Manage ( struct vout_thread_s * );
138 static void vout_Display ( struct vout_thread_s * );
139 static void vout_SetPalette( struct vout_thread_s *, u16*, u16*, u16*, u16* );
141 static int X11CreateWindow ( vout_thread_t *p_vout );
142 static int X11InitDisplay ( vout_thread_t *p_vout, char *psz_display );
144 static int X11CreateImage ( vout_thread_t *p_vout, XImage **pp_ximage );
145 static void X11DestroyImage ( XImage *p_ximage );
146 static int X11CreateShmImage ( vout_thread_t *p_vout, XImage **pp_ximage,
147 XShmSegmentInfo *p_shm_info );
148 static void X11DestroyShmImage ( vout_thread_t *p_vout, XImage *p_ximage,
149 XShmSegmentInfo *p_shm_info );
151 static void X11TogglePointer ( vout_thread_t *p_vout );
152 static void X11EnableScreenSaver ( vout_thread_t *p_vout );
153 static void X11DisableScreenSaver ( vout_thread_t *p_vout );
155 /*****************************************************************************
156 * Functions exported as capabilities. They are declared as static so that
157 * we don't pollute the namespace too much.
158 *****************************************************************************/
159 void _M( vout_getfunctions )( function_list_t * p_function_list )
161 p_function_list->pf_probe = vout_Probe;
162 p_function_list->functions.vout.pf_create = vout_Create;
163 p_function_list->functions.vout.pf_init = vout_Init;
164 p_function_list->functions.vout.pf_end = vout_End;
165 p_function_list->functions.vout.pf_destroy = vout_Destroy;
166 p_function_list->functions.vout.pf_manage = vout_Manage;
167 p_function_list->functions.vout.pf_display = vout_Display;
168 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
171 /*****************************************************************************
172 * vout_Probe: probe the video driver and return a score
173 *****************************************************************************
174 * This function tries to initialize SDL and returns a score to the
175 * plugin manager so that it can select the best plugin.
176 *****************************************************************************/
177 static int vout_Probe( probedata_t *p_data )
179 if( TestMethod( VOUT_METHOD_VAR, "x11" ) )
187 /*****************************************************************************
188 * vout_Create: allocate X11 video thread output method
189 *****************************************************************************
190 * This function allocate and initialize a X11 vout method. It uses some of the
191 * vout properties to choose the window size, and change them according to the
192 * actual properties of the display.
193 *****************************************************************************/
194 static int vout_Create( vout_thread_t *p_vout )
198 /* Allocate structure */
199 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
200 if( p_vout->p_sys == NULL )
202 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
206 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
207 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
208 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
210 if( p_vout->p_sys->p_display == NULL ) /* error */
212 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
213 free( p_vout->p_sys );
216 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
218 p_vout->p_sys->b_fullscreen
219 = main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT );
221 /* Spawn base window - this window will include the video output window,
222 * but also command buttons, subtitles and other indicators */
224 if( X11CreateWindow( p_vout ) )
226 intf_ErrMsg( "vout error: cannot create X11 window" );
227 XCloseDisplay( p_vout->p_sys->p_display );
228 free( p_vout->p_sys );
232 /* Open and initialize device. This function issues its own error messages.
233 * Since XLib is usually not thread-safe, we can't use the same display
234 * pointer than the interface or another thread. However, the root window
235 * id is still valid. */
236 if( X11InitDisplay( p_vout, psz_display ) )
238 intf_ErrMsg( "vout error: cannot initialize X11 display" );
239 XCloseDisplay( p_vout->p_sys->p_display );
240 free( p_vout->p_sys );
244 p_vout->p_sys->b_mouse = 1;
246 /* Disable screen saver and return */
247 X11DisableScreenSaver( p_vout );
252 /*****************************************************************************
253 * vout_Init: initialize X11 video thread output method
254 *****************************************************************************
255 * This function create the XImages needed by the output thread. It is called
256 * at the beginning of the thread, but also each time the window is resized.
257 *****************************************************************************/
258 static int vout_Init( vout_thread_t *p_vout )
263 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
264 p_vout->p_sys->b_shm = 0;
267 /* Create XImages using XShm extension - on failure, fall back to regular
268 * way (and destroy the first image if it was created successfully) */
269 if( p_vout->p_sys->b_shm )
271 /* Create first image */
272 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
273 &p_vout->p_sys->shm_info[0] );
274 if( !i_err ) /* first image has been created */
276 /* Create second image */
277 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
278 &p_vout->p_sys->shm_info[1] ) )
279 { /* error creating the second image */
280 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
281 &p_vout->p_sys->shm_info[0] );
285 if( i_err ) /* an error occured */
287 intf_Msg( "vout: XShm video extension unavailable" );
288 p_vout->p_sys->b_shm = 0;
292 /* Create XImages without XShm extension */
293 if( !p_vout->p_sys->b_shm )
295 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
297 intf_ErrMsg( "vout error: cannot create images" );
298 p_vout->p_sys->p_ximage[0] = NULL;
299 p_vout->p_sys->p_ximage[1] = NULL;
302 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
304 intf_ErrMsg( "vout error: cannot create images" );
305 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
306 p_vout->p_sys->p_ximage[0] = NULL;
307 p_vout->p_sys->p_ximage[1] = NULL;
312 /* Set bytes per line and initialize buffers */
313 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
314 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
315 p_vout->p_sys->p_ximage[ 1 ]->data );
317 /* Set date for autohiding cursor */
318 p_vout->p_sys->i_lastmoved = mdate();
323 /*****************************************************************************
324 * vout_End: terminate X11 video thread output method
325 *****************************************************************************
326 * Destroy the X11 XImages created by vout_Init. It is called at the end of
327 * the thread, but also each time the window is resized.
328 *****************************************************************************/
329 static void vout_End( vout_thread_t *p_vout )
331 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
333 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
334 &p_vout->p_sys->shm_info[0] );
335 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
336 &p_vout->p_sys->shm_info[1] );
338 else /* ...or regular XImages */
340 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
341 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
345 /*****************************************************************************
346 * vout_Destroy: destroy X11 video thread output method
347 *****************************************************************************
348 * Terminate an output method created by vout_CreateOutputMethod
349 *****************************************************************************/
350 static void vout_Destroy( vout_thread_t *p_vout )
352 /* Enable screen saver */
353 X11EnableScreenSaver( p_vout );
355 /* Destroy colormap */
356 if( p_vout->i_screen_depth == 8 )
358 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
362 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
363 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
364 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
366 XCloseDisplay( p_vout->p_sys->p_display );
368 /* Destroy structure */
369 free( p_vout->p_sys );
372 /*****************************************************************************
373 * vout_Manage: handle X11 events
374 *****************************************************************************
375 * This function should be called regularly by video output thread. It manages
376 * X11 events and allows window resizing. It returns a non null value on
378 *****************************************************************************/
379 static int vout_Manage( vout_thread_t *p_vout )
381 XEvent xevent; /* X11 event */
382 boolean_t b_resized; /* window has been resized */
383 boolean_t b_gofullscreen; /* user wants full-screen */
384 char i_key; /* ISO Latin-1 key */
387 /* Handle X11 events: ConfigureNotify events are parsed to know if the
388 * output window's size changed, MapNotify and UnmapNotify to know if the
389 * window is mapped (and if the display is useful), and ClientMessages
390 * to intercept window destruction requests */
394 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
395 StructureNotifyMask | KeyPressMask |
396 ButtonPressMask | ButtonReleaseMask |
397 PointerMotionMask | Button1MotionMask , &xevent )
400 /* ConfigureNotify event: prepare */
401 if( (xevent.type == ConfigureNotify)
402 && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
403 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
405 /* Update dimensions */
407 p_vout->p_sys->i_width = xevent.xconfigure.width;
408 p_vout->p_sys->i_height = xevent.xconfigure.height;
410 /* MapNotify event: change window status and disable screen saver */
411 else if( xevent.type == MapNotify)
413 if( (p_vout != NULL) && !p_vout->b_active )
415 X11DisableScreenSaver( p_vout );
416 p_vout->b_active = 1;
419 /* UnmapNotify event: change window status and enable screen saver */
420 else if( xevent.type == UnmapNotify )
422 if( (p_vout != NULL) && p_vout->b_active )
424 X11EnableScreenSaver( p_vout );
425 p_vout->b_active = 0;
429 else if( xevent.type == KeyPress )
431 /* We may have keys like F1 trough F12, ESC ... */
432 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
433 xevent.xkey.keycode, 0 );
434 switch( x_key_symbol )
437 p_main->p_intf->b_die = 1;
440 p_main->p_intf->b_menu_change = 1;
444 * The reason why I use this instead of XK_0 is that
445 * with XLookupString, we don't have to care about
448 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
450 /* FIXME: handle stuff here */
455 p_main->p_intf->b_die = 1;
462 network_ChannelJoin( 0 );
465 network_ChannelJoin( 1 );
468 network_ChannelJoin( 2 );
471 network_ChannelJoin( 3 );
474 network_ChannelJoin( 4 );
477 network_ChannelJoin( 5 );
480 network_ChannelJoin( 6 );
483 network_ChannelJoin( 7 );
486 network_ChannelJoin( 8 );
489 network_ChannelJoin( 9 );
492 if( intf_ProcessKey( p_main->p_intf,
495 intf_DbgMsg( "vout: unhandled key '%c' (%i)",
496 (char)i_key, i_key );
505 else if( xevent.type == ButtonPress )
507 switch( ((XButtonEvent *)&xevent)->button )
510 /* in this part we will eventually manage
511 * clicks for DVD navigation for instance */
516 else if( xevent.type == ButtonRelease )
518 switch( ((XButtonEvent *)&xevent)->button )
521 /* FIXME: need locking ! */
522 p_main->p_intf->b_menu_change = 1;
527 else if( xevent.type == MotionNotify )
529 p_vout->p_sys->i_lastmoved = mdate();
530 if( ! p_vout->p_sys->b_mouse )
532 X11TogglePointer( p_vout );
539 intf_DbgMsg( "vout: unhandled event %d received", xevent.type );
544 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
545 * are handled - according to the man pages, the format is always 32
547 while( XCheckTypedEvent( p_vout->p_sys->p_display,
548 ClientMessage, &xevent ) )
550 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
551 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
553 p_main->p_intf->b_die = 1;
557 intf_DbgMsg( "vout: unhandled ClientMessage received" );
561 if ( b_gofullscreen )
564 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
565 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
567 intf_DbgMsg( "vout: changing full-screen status" );
569 p_vout->p_sys->b_fullscreen = !p_vout->p_sys->b_fullscreen;
571 /* Get rid of the old window */
572 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
573 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
575 /* And create a new one */
576 if( X11CreateWindow( p_vout ) )
578 intf_ErrMsg( "vout error: cannot create X11 window" );
579 XCloseDisplay( p_vout->p_sys->p_display );
581 free( p_vout->p_sys );
585 if( X11InitDisplay( p_vout, psz_display ) )
587 intf_ErrMsg( "vout error: cannot initialize X11 display" );
588 XCloseDisplay( p_vout->p_sys->p_display );
589 free( p_vout->p_sys );
592 /* We've changed the size, update it */
593 p_vout->i_changes |= VOUT_SIZE_CHANGE;
597 * Handle vout window resizing
601 /* If interface window has been resized, change vout size */
602 intf_DbgMsg( "vout: resizing output window" );
603 p_vout->i_width = p_vout->p_sys->i_width;
604 p_vout->i_height = p_vout->p_sys->i_height;
605 p_vout->i_changes |= VOUT_SIZE_CHANGE;
607 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
608 (p_vout->i_height != p_vout->p_sys->i_height) )
610 /* If video output size has changed, change interface window size */
611 intf_DbgMsg( "vout: resizing output window" );
612 p_vout->p_sys->i_width = p_vout->i_width;
613 p_vout->p_sys->i_height = p_vout->i_height;
614 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
615 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
618 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
620 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
621 && (p_vout->i_screen_depth == 8) )
623 /* FIXME: clear flags ?? */
629 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
631 intf_DbgMsg( "vout info: resizing window" );
632 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
635 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
636 p_vout->i_width, p_vout->i_height );
638 /* Destroy XImages to change their size */
641 /* Recreate XImages. If SysInit failed, the thread can't go on. */
642 if( vout_Init( p_vout ) )
644 intf_ErrMsg( "vout error: cannot resize display" );
648 /* Tell the video output thread that it will need to rebuild YUV
649 * tables. This is needed since conversion buffer size may have
651 p_vout->i_changes |= VOUT_YUV_CHANGE;
652 intf_Msg( "vout: video display resized (%dx%d)",
653 p_vout->i_width, p_vout->i_height);
656 /* Autohide Cursour */
657 if( mdate() - p_vout->p_sys->i_lastmoved > 2000000 )
659 /* Hide the mouse automatically */
660 if( p_vout->p_sys->b_mouse )
662 X11TogglePointer( p_vout );
670 /*****************************************************************************
671 * vout_Display: displays previously rendered output
672 *****************************************************************************
673 * This function send the currently rendered image to X11 server, wait until
674 * it is displayed and switch the two rendering buffer, preparing next frame.
675 *****************************************************************************/
676 static void vout_Display( vout_thread_t *p_vout )
678 if( p_vout->p_sys->b_shm) /* XShm is used */
680 /* Display rendered image using shared memory extension */
681 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
682 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
684 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
685 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
687 /* Send the order to the X server */
688 XSync(p_vout->p_sys->p_display, False);
690 else /* regular X11 capabilities are used */
692 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
693 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
695 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
696 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
698 /* Send the order to the X server */
699 XSync(p_vout->p_sys->p_display, False);
703 /*****************************************************************************
704 * vout_SetPalette: sets an 8 bpp palette
705 *****************************************************************************
706 * This function sets the palette given as an argument. It does not return
707 * anything, but could later send information on which colors it was unable
709 *****************************************************************************/
710 static void vout_SetPalette( p_vout_thread_t p_vout,
711 u16 *red, u16 *green, u16 *blue, u16 *transp )
714 XColor p_colors[255];
716 intf_DbgMsg( "vout: Palette change called" );
718 /* allocate palette */
719 for( i = 0, j = 255; i < 255; i++, j-- )
721 /* kludge: colors are indexed reversely because color 255 seems
722 * to be reserved for black even if we try to set it to white */
723 p_colors[ i ].pixel = j;
724 p_colors[ i ].pad = 0;
725 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
726 p_colors[ i ].red = red[ j ];
727 p_colors[ i ].blue = blue[ j ];
728 p_colors[ i ].green = green[ j ];
731 XStoreColors( p_vout->p_sys->p_display,
732 p_vout->p_sys->colormap, p_colors, 256 );
735 /* following functions are local */
737 /*****************************************************************************
738 * X11CreateWindow: open and set-up X11 main window
739 *****************************************************************************/
740 static int X11CreateWindow( vout_thread_t *p_vout )
742 XSizeHints xsize_hints;
743 XSetWindowAttributes xwindow_attributes;
750 boolean_t b_configure_notify;
751 boolean_t b_map_notify;
753 /* If we're full screen, we're full screen! */
754 if( p_vout->p_sys->b_fullscreen )
756 p_vout->p_sys->i_width = DisplayWidth( p_vout->p_sys->p_display,
757 p_vout->p_sys->i_screen );
758 p_vout->p_sys->i_height = DisplayHeight( p_vout->p_sys->p_display,
759 p_vout->p_sys->i_screen );
760 p_vout->i_width = p_vout->p_sys->i_width;
761 p_vout->i_height = p_vout->p_sys->i_height;
765 /* Set main window's size */
766 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
767 VOUT_WIDTH_DEFAULT );
768 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
769 VOUT_HEIGHT_DEFAULT );
770 p_vout->i_width = p_vout->p_sys->i_width;
771 p_vout->i_height = p_vout->p_sys->i_height;
774 /* Prepare window manager hints and properties */
775 xsize_hints.base_width = p_vout->p_sys->i_width;
776 xsize_hints.base_height = p_vout->p_sys->i_height;
777 xsize_hints.flags = PSize;
778 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
779 "WM_PROTOCOLS", True );
780 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
781 "WM_DELETE_WINDOW", True );
783 /* Prepare window attributes */
784 xwindow_attributes.backing_store = Always; /* save the hidden part */
785 xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
786 p_vout->p_sys->i_screen );
787 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
790 /* Create the window and set hints - the window must receive ConfigureNotify
791 * events, and, until it is displayed, Expose and MapNotify events. */
793 p_vout->p_sys->window =
794 XCreateWindow( p_vout->p_sys->p_display,
795 DefaultRootWindow( p_vout->p_sys->p_display ),
797 p_vout->p_sys->i_width, p_vout->p_sys->i_height, 0,
799 CWBackingStore | CWBackPixel | CWEventMask,
800 &xwindow_attributes );
802 if ( p_vout->p_sys->b_fullscreen )
804 prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
805 mwmhints.flags = MWM_HINTS_DECORATIONS;
806 mwmhints.decorations = 0;
807 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
808 prop, prop, 32, PropModeReplace,
809 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
811 XSetTransientForHint( p_vout->p_sys->p_display,
812 p_vout->p_sys->window, None );
813 XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
816 /* Set window manager hints and properties: size hints, command,
817 * window's name, and accepted protocols */
818 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
820 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
821 p_main->ppsz_argv, p_main->i_argc );
822 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
823 VOUT_TITLE " (X11 output)" );
825 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
826 || (p_vout->p_sys->wm_delete_window == None)
827 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
828 &p_vout->p_sys->wm_delete_window, 1 ) )
830 /* WM_DELETE_WINDOW is not supported by window manager */
831 intf_Msg( "vout error: missing or bad window manager" );
834 /* Creation of a graphic context that doesn't generate a GraphicsExpose
835 * event when using functions like XCopyArea */
836 xgcvalues.graphics_exposures = False;
837 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
838 p_vout->p_sys->window,
839 GCGraphicsExposures, &xgcvalues);
841 /* Send orders to server, and wait until window is displayed - three
842 * events must be received: a MapNotify event, an Expose event allowing
843 * drawing in the window, and a ConfigureNotify to get the window
844 * dimensions. Once those events have been received, only ConfigureNotify
845 * events need to be received. */
847 b_configure_notify = 0;
849 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
852 XNextEvent( p_vout->p_sys->p_display, &xevent);
853 if( (xevent.type == Expose)
854 && (xevent.xexpose.window == p_vout->p_sys->window) )
858 else if( (xevent.type == MapNotify)
859 && (xevent.xmap.window == p_vout->p_sys->window) )
863 else if( (xevent.type == ConfigureNotify)
864 && (xevent.xconfigure.window == p_vout->p_sys->window) )
866 b_configure_notify = 1;
867 p_vout->p_sys->i_width = xevent.xconfigure.width;
868 p_vout->p_sys->i_height = xevent.xconfigure.height;
870 } while( !( b_expose && b_configure_notify && b_map_notify ) );
872 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
873 StructureNotifyMask | KeyPressMask |
874 ButtonPressMask | ButtonReleaseMask |
877 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
879 /* Allocate a new palette */
880 p_vout->p_sys->colormap =
881 XCreateColormap( p_vout->p_sys->p_display,
882 DefaultRootWindow( p_vout->p_sys->p_display ),
883 DefaultVisual( p_vout->p_sys->p_display,
884 p_vout->p_sys->i_screen ),
887 xwindow_attributes.colormap = p_vout->p_sys->colormap;
888 XChangeWindowAttributes( p_vout->p_sys->p_display,
889 p_vout->p_sys->window,
890 CWColormap, &xwindow_attributes );
893 if( p_vout->p_sys->b_fullscreen )
895 XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
896 RevertToNone, CurrentTime );
897 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
900 /* At this stage, the window is open, displayed, and ready to
906 /*****************************************************************************
907 * X11InitDisplay: open and initialize X11 device
908 *****************************************************************************
909 * Create a window according to video output given size, and set other
910 * properties according to the display properties.
911 *****************************************************************************/
912 static int X11InitDisplay( vout_thread_t *p_vout, char *psz_display )
914 XPixmapFormatValues * p_formats; /* pixmap formats */
915 XVisualInfo * p_xvisual; /* visuals informations */
916 XVisualInfo xvisual_template; /* visual template */
917 int i_count; /* array size */
921 /* Initialize structure */
922 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
923 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
925 if( !p_vout->p_sys->b_shm )
927 intf_Msg( "vout: XShm video extension is not available" );
930 /* Get screen depth */
931 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
932 p_vout->p_sys->i_screen );
933 switch( p_vout->i_screen_depth )
937 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
939 xvisual_template.screen = p_vout->p_sys->i_screen;
940 xvisual_template.class = DirectColor;
941 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
942 VisualScreenMask | VisualClassMask,
943 &xvisual_template, &i_count );
944 if( p_xvisual == NULL )
946 intf_ErrMsg( "vout error: no PseudoColor visual available" );
949 p_vout->i_bytes_per_pixel = 1;
956 * Screen depth is higher than 8bpp. TrueColor visual is used.
958 xvisual_template.screen = p_vout->p_sys->i_screen;
959 xvisual_template.class = TrueColor;
960 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
961 VisualScreenMask | VisualClassMask,
962 &xvisual_template, &i_count );
963 if( p_xvisual == NULL )
965 intf_ErrMsg( "vout error: no TrueColor visual available" );
968 p_vout->i_red_mask = p_xvisual->red_mask;
969 p_vout->i_green_mask = p_xvisual->green_mask;
970 p_vout->i_blue_mask = p_xvisual->blue_mask;
972 /* There is no difference yet between 3 and 4 Bpp. The only way
973 * to find the actual number of bytes per pixel is to list supported
975 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
976 p_vout->i_bytes_per_pixel = 0;
978 for( ; i_count-- ; p_formats++ )
980 /* Under XFree4.0, the list contains pixmap formats available
981 * through all video depths ; so we have to check against current
983 if( p_formats->depth == p_vout->i_screen_depth )
985 if( p_formats->bits_per_pixel / 8
986 > p_vout->i_bytes_per_pixel )
988 p_vout->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
994 p_vout->p_sys->p_visual = p_xvisual->visual;
1000 /*****************************************************************************
1001 * X11CreateImage: create an XImage
1002 *****************************************************************************
1003 * Create a simple XImage used as a buffer.
1004 *****************************************************************************/
1005 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
1007 byte_t * pb_data; /* image data storage zone */
1008 int i_quantum; /* XImage quantum (see below) */
1010 /* Allocate memory for image */
1011 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
1012 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
1013 if( !pb_data ) /* error */
1015 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
1019 /* Optimize the quantum of a scanline regarding its size - the quantum is
1020 a diviser of the number of bits between the start of two scanlines. */
1021 if( !(( p_vout->i_bytes_per_line ) % 32) )
1027 if( !(( p_vout->i_bytes_per_line ) % 16) )
1038 *pp_ximage = XCreateImage( p_vout->p_sys->p_display,
1039 p_vout->p_sys->p_visual, p_vout->i_screen_depth,
1040 ZPixmap, 0, pb_data,
1041 p_vout->i_width, p_vout->i_height, i_quantum, 0);
1042 if(! *pp_ximage ) /* error */
1044 intf_ErrMsg( "vout error: XCreateImage() failed" );
1052 /*****************************************************************************
1053 * X11CreateShmImage: create an XImage using shared memory extension
1054 *****************************************************************************
1055 * Prepare an XImage for DisplayX11ShmImage function.
1056 * The order of the operations respects the recommandations of the mit-shm
1057 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1059 *****************************************************************************/
1060 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
1061 XShmSegmentInfo *p_shm_info)
1065 XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
1066 p_vout->i_screen_depth, ZPixmap, 0,
1067 p_shm_info, p_vout->i_width, p_vout->i_height );
1068 if(! *pp_ximage ) /* error */
1070 intf_ErrMsg( "vout error: XShmCreateImage() failed" );
1074 /* Allocate shared memory segment - 0777 set the access permission
1075 * rights (like umask), they are not yet supported by X servers */
1077 shmget( IPC_PRIVATE, (*pp_ximage)->bytes_per_line
1078 * (*pp_ximage)->height, IPC_CREAT | 0777);
1079 if( p_shm_info->shmid < 0) /* error */
1081 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
1083 XDestroyImage( *pp_ximage );
1087 /* Attach shared memory segment to process (read/write) */
1088 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
1089 if(! p_shm_info->shmaddr )
1091 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
1093 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
1094 XDestroyImage( *pp_ximage );
1098 /* Mark the shm segment to be removed when there will be no more
1099 * attachements, so it is automatic on process exit or after shmdt */
1100 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1102 /* Attach shared memory segment to X server (read only) */
1103 p_shm_info->readOnly = True;
1104 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info )
1105 == False ) /* error */
1107 intf_ErrMsg( "vout error: cannot attach shared memory to X11 server" );
1108 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
1109 * and automatic free */
1110 XDestroyImage( *pp_ximage );
1114 /* Send image to X server. This instruction is required, since having
1115 * built a Shm XImage and not using it causes an error on XCloseDisplay */
1116 XFlush( p_vout->p_sys->p_display );
1120 /*****************************************************************************
1121 * X11DestroyImage: destroy an XImage
1122 *****************************************************************************
1123 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
1124 * destroyed (see vout_ManageOutputMethod())
1125 *****************************************************************************/
1126 static void X11DestroyImage( XImage *p_ximage )
1128 if( p_ximage != NULL )
1130 XDestroyImage( p_ximage ); /* no free() required */
1134 /*****************************************************************************
1135 * X11DestroyShmImage
1136 *****************************************************************************
1137 * Destroy XImage AND associated data. Detach shared memory segment from
1138 * server and process, then free it. If pointer is NULL, the image won't be
1139 * destroyed (see vout_ManageOutputMethod())
1140 *****************************************************************************/
1141 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
1142 XShmSegmentInfo *p_shm_info )
1144 /* If pointer is NULL, do nothing */
1145 if( p_ximage == NULL )
1150 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
1151 XDestroyImage( p_ximage );
1153 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
1154 { /* also automatic freeing... */
1155 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1161 /* WAZAAAAAAAAAAA */
1163 /*****************************************************************************
1164 * X11EnableScreenSaver: enable screen saver
1165 *****************************************************************************
1166 * This function enable the screen saver on a display after it had been
1167 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1168 * know wether the screen saver can be activated or not: if n successive calls
1169 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1170 * will be required before the screen saver could effectively be activated.
1171 *****************************************************************************/
1172 void X11EnableScreenSaver( vout_thread_t *p_vout )
1174 intf_DbgMsg( "vout: enabling screen saver" );
1175 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1176 p_vout->p_sys->i_ss_interval,
1177 p_vout->p_sys->i_ss_blanking,
1178 p_vout->p_sys->i_ss_exposure );
1181 /*****************************************************************************
1182 * X11DisableScreenSaver: disable screen saver
1183 *****************************************************************************
1184 * See XEnableScreenSaver
1185 *****************************************************************************/
1186 void X11DisableScreenSaver( vout_thread_t *p_vout )
1188 /* Save screen saver informations */
1189 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1190 &p_vout->p_sys->i_ss_interval,
1191 &p_vout->p_sys->i_ss_blanking,
1192 &p_vout->p_sys->i_ss_exposure );
1194 /* Disable screen saver */
1195 intf_DbgMsg( "vout: disabling screen saver" );
1196 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1197 p_vout->p_sys->i_ss_interval,
1198 p_vout->p_sys->i_ss_blanking,
1199 p_vout->p_sys->i_ss_exposure );
1202 /*****************************************************************************
1203 * X11TogglePointer: hide or show the mouse pointer
1204 *****************************************************************************
1205 * This function hides the X pointer if it is visible by putting it at
1206 * coordinates (32,32) and setting the pointer sprite to a blank one. To
1207 * show it again, we disable the sprite and restore the original coordinates.
1208 *****************************************************************************/
1209 void X11TogglePointer( vout_thread_t *p_vout )
1211 static Cursor cursor;
1212 static boolean_t b_cursor = 0;
1214 if( p_vout->p_sys->b_mouse )
1216 p_vout->p_sys->b_mouse = 0;
1221 Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1222 DefaultRootWindow(p_vout->p_sys->p_display),
1225 XParseColor( p_vout->p_sys->p_display,
1226 XCreateColormap( p_vout->p_sys->p_display,
1228 p_vout->p_sys->p_display ),
1230 p_vout->p_sys->p_display,
1231 p_vout->p_sys->i_screen ),
1235 cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1236 blank, blank, &color, &color, 1, 1 );
1240 XDefineCursor( p_vout->p_sys->p_display,
1241 p_vout->p_sys->window, cursor );
1245 p_vout->p_sys->b_mouse = 1;
1247 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );