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.24 2001/05/07 03:14:09 stef 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 ? */
114 /* Fullscreen needs to be able to hide the wm decorations */
115 #define MWM_HINTS_DECORATIONS (1L << 1)
116 #define PROP_MWM_HINTS_ELEMENTS 5
117 typedef struct mwmhints_s
126 /*****************************************************************************
128 *****************************************************************************/
129 static int vout_Probe ( probedata_t *p_data );
130 static int vout_Create ( struct vout_thread_s * );
131 static int vout_Init ( struct vout_thread_s * );
132 static void vout_End ( struct vout_thread_s * );
133 static void vout_Destroy ( struct vout_thread_s * );
134 static int vout_Manage ( struct vout_thread_s * );
135 static void vout_Display ( struct vout_thread_s * );
136 static void vout_SetPalette( struct vout_thread_s *, u16*, u16*, u16*, u16* );
138 static int X11CreateWindow ( vout_thread_t *p_vout );
139 static int X11InitDisplay ( vout_thread_t *p_vout, char *psz_display );
141 static int X11CreateImage ( vout_thread_t *p_vout, XImage **pp_ximage );
142 static void X11DestroyImage ( XImage *p_ximage );
143 static int X11CreateShmImage ( vout_thread_t *p_vout, XImage **pp_ximage,
144 XShmSegmentInfo *p_shm_info );
145 static void X11DestroyShmImage ( vout_thread_t *p_vout, XImage *p_ximage,
146 XShmSegmentInfo *p_shm_info );
148 static void X11TogglePointer ( vout_thread_t *p_vout );
149 static void X11EnableScreenSaver ( vout_thread_t *p_vout );
150 static void X11DisableScreenSaver ( vout_thread_t *p_vout );
152 /*****************************************************************************
153 * Functions exported as capabilities. They are declared as static so that
154 * we don't pollute the namespace too much.
155 *****************************************************************************/
156 void _M( vout_getfunctions )( function_list_t * p_function_list )
158 p_function_list->pf_probe = vout_Probe;
159 p_function_list->functions.vout.pf_create = vout_Create;
160 p_function_list->functions.vout.pf_init = vout_Init;
161 p_function_list->functions.vout.pf_end = vout_End;
162 p_function_list->functions.vout.pf_destroy = vout_Destroy;
163 p_function_list->functions.vout.pf_manage = vout_Manage;
164 p_function_list->functions.vout.pf_display = vout_Display;
165 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
168 /*****************************************************************************
169 * vout_Probe: probe the video driver and return a score
170 *****************************************************************************
171 * This function tries to initialize SDL and returns a score to the
172 * plugin manager so that it can select the best plugin.
173 *****************************************************************************/
174 static int vout_Probe( probedata_t *p_data )
176 if( TestMethod( VOUT_METHOD_VAR, "x11" ) )
184 /*****************************************************************************
185 * vout_Create: allocate X11 video thread output method
186 *****************************************************************************
187 * This function allocate and initialize a X11 vout method. It uses some of the
188 * vout properties to choose the window size, and change them according to the
189 * actual properties of the display.
190 *****************************************************************************/
191 static int vout_Create( vout_thread_t *p_vout )
195 /* Allocate structure */
196 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
197 if( p_vout->p_sys == NULL )
199 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
203 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
204 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
205 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
207 if( p_vout->p_sys->p_display == NULL ) /* error */
209 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
210 free( p_vout->p_sys );
213 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
216 = main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT );
218 /* Spawn base window - this window will include the video output window,
219 * but also command buttons, subtitles and other indicators */
221 if( X11CreateWindow( p_vout ) )
223 intf_ErrMsg( "vout error: cannot create X11 window" );
224 XCloseDisplay( p_vout->p_sys->p_display );
225 free( p_vout->p_sys );
229 /* Open and initialize device. This function issues its own error messages.
230 * Since XLib is usually not thread-safe, we can't use the same display
231 * pointer than the interface or another thread. However, the root window
232 * id is still valid. */
233 if( X11InitDisplay( p_vout, psz_display ) )
235 intf_ErrMsg( "vout error: cannot initialize X11 display" );
236 XCloseDisplay( p_vout->p_sys->p_display );
237 free( p_vout->p_sys );
241 p_vout->p_sys->b_mouse = 1;
243 /* Disable screen saver and return */
244 X11DisableScreenSaver( p_vout );
249 /*****************************************************************************
250 * vout_Init: initialize X11 video thread output method
251 *****************************************************************************
252 * This function create the XImages needed by the output thread. It is called
253 * at the beginning of the thread, but also each time the window is resized.
254 *****************************************************************************/
255 static int vout_Init( vout_thread_t *p_vout )
260 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
261 p_vout->p_sys->b_shm = 0;
264 /* Create XImages using XShm extension - on failure, fall back to regular
265 * way (and destroy the first image if it was created successfully) */
266 if( p_vout->p_sys->b_shm )
268 /* Create first image */
269 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
270 &p_vout->p_sys->shm_info[0] );
271 if( !i_err ) /* first image has been created */
273 /* Create second image */
274 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
275 &p_vout->p_sys->shm_info[1] ) )
276 { /* error creating the second image */
277 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
278 &p_vout->p_sys->shm_info[0] );
282 if( i_err ) /* an error occured */
284 intf_Msg( "vout: XShm video extension unavailable" );
285 p_vout->p_sys->b_shm = 0;
289 /* Create XImages without XShm extension */
290 if( !p_vout->p_sys->b_shm )
292 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
294 intf_ErrMsg( "vout error: cannot create images" );
295 p_vout->p_sys->p_ximage[0] = NULL;
296 p_vout->p_sys->p_ximage[1] = NULL;
299 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
301 intf_ErrMsg( "vout error: cannot create images" );
302 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
303 p_vout->p_sys->p_ximage[0] = NULL;
304 p_vout->p_sys->p_ximage[1] = NULL;
309 /* Set bytes per line and initialize buffers */
310 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
311 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
312 p_vout->p_sys->p_ximage[ 1 ]->data );
314 /* Set date for autohiding cursor */
315 p_vout->p_sys->i_lastmoved = mdate();
320 /*****************************************************************************
321 * vout_End: terminate X11 video thread output method
322 *****************************************************************************
323 * Destroy the X11 XImages created by vout_Init. It is called at the end of
324 * the thread, but also each time the window is resized.
325 *****************************************************************************/
326 static void vout_End( vout_thread_t *p_vout )
328 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
330 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
331 &p_vout->p_sys->shm_info[0] );
332 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
333 &p_vout->p_sys->shm_info[1] );
335 else /* ...or regular XImages */
337 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
338 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
342 /*****************************************************************************
343 * vout_Destroy: destroy X11 video thread output method
344 *****************************************************************************
345 * Terminate an output method created by vout_CreateOutputMethod
346 *****************************************************************************/
347 static void vout_Destroy( vout_thread_t *p_vout )
349 /* Enable screen saver */
350 X11EnableScreenSaver( p_vout );
352 /* Destroy colormap */
353 if( p_vout->i_screen_depth == 8 )
355 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
359 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
360 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
361 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
363 XCloseDisplay( p_vout->p_sys->p_display );
365 /* Destroy structure */
366 free( p_vout->p_sys );
369 /*****************************************************************************
370 * vout_Manage: handle X11 events
371 *****************************************************************************
372 * This function should be called regularly by video output thread. It manages
373 * X11 events and allows window resizing. It returns a non null value on
375 *****************************************************************************/
376 static int vout_Manage( vout_thread_t *p_vout )
378 XEvent xevent; /* X11 event */
379 boolean_t b_resized; /* window has been resized */
380 char i_key; /* ISO Latin-1 key */
383 /* Handle X11 events: ConfigureNotify events are parsed to know if the
384 * output window's size changed, MapNotify and UnmapNotify to know if the
385 * window is mapped (and if the display is useful), and ClientMessages
386 * to intercept window destruction requests */
389 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
390 StructureNotifyMask | KeyPressMask |
391 ButtonPressMask | ButtonReleaseMask |
392 PointerMotionMask | Button1MotionMask , &xevent )
395 /* ConfigureNotify event: prepare */
396 if( (xevent.type == ConfigureNotify)
397 && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
398 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
400 /* Update dimensions */
402 p_vout->p_sys->i_width = xevent.xconfigure.width;
403 p_vout->p_sys->i_height = xevent.xconfigure.height;
405 /* MapNotify event: change window status and disable screen saver */
406 else if( xevent.type == MapNotify)
408 if( (p_vout != NULL) && !p_vout->b_active )
410 X11DisableScreenSaver( p_vout );
411 p_vout->b_active = 1;
414 /* UnmapNotify event: change window status and enable screen saver */
415 else if( xevent.type == UnmapNotify )
417 if( (p_vout != NULL) && p_vout->b_active )
419 X11EnableScreenSaver( p_vout );
420 p_vout->b_active = 0;
424 else if( xevent.type == KeyPress )
426 /* We may have keys like F1 trough F12, ESC ... */
427 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
428 xevent.xkey.keycode, 0 );
429 switch( x_key_symbol )
432 p_main->p_intf->b_die = 1;
435 p_main->p_intf->b_menu_change = 1;
439 * The reason why I use this instead of XK_0 is that
440 * with XLookupString, we don't have to care about
443 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
445 /* FIXME: handle stuff here */
450 p_main->p_intf->b_die = 1;
454 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
457 network_ChannelJoin( 0 );
460 network_ChannelJoin( 1 );
463 network_ChannelJoin( 2 );
466 network_ChannelJoin( 3 );
469 network_ChannelJoin( 4 );
472 network_ChannelJoin( 5 );
475 network_ChannelJoin( 6 );
478 network_ChannelJoin( 7 );
481 network_ChannelJoin( 8 );
484 network_ChannelJoin( 9 );
487 if( intf_ProcessKey( p_main->p_intf,
490 intf_DbgMsg( "vout: unhandled key '%c' (%i)",
491 (char)i_key, i_key );
500 else if( xevent.type == ButtonPress )
502 switch( ((XButtonEvent *)&xevent)->button )
505 /* in this part we will eventually manage
506 * clicks for DVD navigation for instance */
511 else if( xevent.type == ButtonRelease )
513 switch( ((XButtonEvent *)&xevent)->button )
516 /* FIXME: need locking ! */
517 p_main->p_intf->b_menu_change = 1;
522 else if( xevent.type == MotionNotify )
524 p_vout->p_sys->i_lastmoved = mdate();
525 if( ! p_vout->p_sys->b_mouse )
527 X11TogglePointer( p_vout );
533 intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
537 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
538 * are handled - according to the man pages, the format is always 32
540 while( XCheckTypedEvent( p_vout->p_sys->p_display,
541 ClientMessage, &xevent ) )
543 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
544 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
546 p_main->p_intf->b_die = 1;
550 intf_DbgMsg( "vout: unhandled ClientMessage received" );
554 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
557 /* Open display, unsing 'vlc_display' or the DISPLAY
558 * environment variable */
559 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
561 intf_DbgMsg( "vout: changing full-screen status" );
563 p_vout->b_fullscreen = !p_vout->b_fullscreen;
564 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
566 /* Get rid of the old window */
567 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
568 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
570 /* And create a new one */
571 if( X11CreateWindow( p_vout ) )
573 intf_ErrMsg( "vout error: cannot create X11 window" );
574 XCloseDisplay( p_vout->p_sys->p_display );
576 free( p_vout->p_sys );
580 if( X11InitDisplay( p_vout, psz_display ) )
582 intf_ErrMsg( "vout error: cannot initialize X11 display" );
583 XCloseDisplay( p_vout->p_sys->p_display );
584 free( p_vout->p_sys );
587 /* We've changed the size, update it */
588 p_vout->i_changes |= VOUT_SIZE_CHANGE;
592 * Handle vout window resizing
596 /* If interface window has been resized, change vout size */
597 intf_DbgMsg( "vout: resizing output window" );
598 p_vout->i_width = p_vout->p_sys->i_width;
599 p_vout->i_height = p_vout->p_sys->i_height;
600 p_vout->i_changes |= VOUT_SIZE_CHANGE;
602 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
603 (p_vout->i_height != p_vout->p_sys->i_height) )
605 /* If video output size has changed, change interface window size */
606 intf_DbgMsg( "vout: resizing output window" );
607 p_vout->p_sys->i_width = p_vout->i_width;
608 p_vout->p_sys->i_height = p_vout->i_height;
609 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
610 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
613 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
615 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
616 && (p_vout->i_screen_depth == 8) )
618 /* FIXME: clear flags ?? */
624 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
626 intf_DbgMsg( "vout info: resizing window" );
627 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
630 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
631 p_vout->i_width, p_vout->i_height );
633 /* Destroy XImages to change their size */
636 /* Recreate XImages. If SysInit failed, the thread can't go on. */
637 if( vout_Init( p_vout ) )
639 intf_ErrMsg( "vout error: cannot resize display" );
643 /* Tell the video output thread that it will need to rebuild YUV
644 * tables. This is needed since conversion buffer size may have
646 p_vout->i_changes |= VOUT_YUV_CHANGE;
647 intf_Msg( "vout: video display resized (%dx%d)",
648 p_vout->i_width, p_vout->i_height);
651 /* Autohide Cursour */
652 if( mdate() - p_vout->p_sys->i_lastmoved > 2000000 )
654 /* Hide the mouse automatically */
655 if( p_vout->p_sys->b_mouse )
657 X11TogglePointer( p_vout );
665 /*****************************************************************************
666 * vout_Display: displays previously rendered output
667 *****************************************************************************
668 * This function send the currently rendered image to X11 server, wait until
669 * it is displayed and switch the two rendering buffer, preparing next frame.
670 *****************************************************************************/
671 static void vout_Display( vout_thread_t *p_vout )
673 if( p_vout->p_sys->b_shm) /* XShm is used */
675 /* Display rendered image using shared memory extension */
676 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
677 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
679 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
680 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
682 /* Send the order to the X server */
683 XSync(p_vout->p_sys->p_display, False);
685 else /* regular X11 capabilities are used */
687 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
688 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
690 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
691 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
693 /* Send the order to the X server */
694 XSync(p_vout->p_sys->p_display, False);
698 /*****************************************************************************
699 * vout_SetPalette: sets an 8 bpp palette
700 *****************************************************************************
701 * This function sets the palette given as an argument. It does not return
702 * anything, but could later send information on which colors it was unable
704 *****************************************************************************/
705 static void vout_SetPalette( p_vout_thread_t p_vout,
706 u16 *red, u16 *green, u16 *blue, u16 *transp )
709 XColor p_colors[255];
711 intf_DbgMsg( "vout: Palette change called" );
713 /* allocate palette */
714 for( i = 0, j = 255; i < 255; i++, j-- )
716 /* kludge: colors are indexed reversely because color 255 seems
717 * to be reserved for black even if we try to set it to white */
718 p_colors[ i ].pixel = j;
719 p_colors[ i ].pad = 0;
720 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
721 p_colors[ i ].red = red[ j ];
722 p_colors[ i ].blue = blue[ j ];
723 p_colors[ i ].green = green[ j ];
726 XStoreColors( p_vout->p_sys->p_display,
727 p_vout->p_sys->colormap, p_colors, 256 );
730 /* following functions are local */
732 /*****************************************************************************
733 * X11CreateWindow: open and set-up X11 main window
734 *****************************************************************************/
735 static int X11CreateWindow( vout_thread_t *p_vout )
737 XSizeHints xsize_hints;
738 XSetWindowAttributes xwindow_attributes;
745 boolean_t b_configure_notify;
746 boolean_t b_map_notify;
748 /* If we're full screen, we're full screen! */
749 if( p_vout->b_fullscreen )
751 p_vout->p_sys->i_width = DisplayWidth( p_vout->p_sys->p_display,
752 p_vout->p_sys->i_screen );
753 p_vout->p_sys->i_height = DisplayHeight( p_vout->p_sys->p_display,
754 p_vout->p_sys->i_screen );
755 p_vout->i_width = p_vout->p_sys->i_width;
756 p_vout->i_height = p_vout->p_sys->i_height;
760 /* Set main window's size */
761 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
762 VOUT_WIDTH_DEFAULT );
763 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
764 VOUT_HEIGHT_DEFAULT );
765 p_vout->i_width = p_vout->p_sys->i_width;
766 p_vout->i_height = p_vout->p_sys->i_height;
769 /* Prepare window manager hints and properties */
770 xsize_hints.base_width = p_vout->p_sys->i_width;
771 xsize_hints.base_height = p_vout->p_sys->i_height;
772 xsize_hints.flags = PSize;
773 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
774 "WM_PROTOCOLS", True );
775 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
776 "WM_DELETE_WINDOW", True );
778 /* Prepare window attributes */
779 xwindow_attributes.backing_store = Always; /* save the hidden part */
780 xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
781 p_vout->p_sys->i_screen );
782 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
785 /* Create the window and set hints - the window must receive ConfigureNotify
786 * events, and, until it is displayed, Expose and MapNotify events. */
788 p_vout->p_sys->window =
789 XCreateWindow( p_vout->p_sys->p_display,
790 DefaultRootWindow( p_vout->p_sys->p_display ),
792 p_vout->p_sys->i_width, p_vout->p_sys->i_height, 0,
794 CWBackingStore | CWBackPixel | CWEventMask,
795 &xwindow_attributes );
797 if ( p_vout->b_fullscreen )
799 prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
800 mwmhints.flags = MWM_HINTS_DECORATIONS;
801 mwmhints.decorations = 0;
802 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
803 prop, prop, 32, PropModeReplace,
804 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
806 XSetTransientForHint( p_vout->p_sys->p_display,
807 p_vout->p_sys->window, None );
808 XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
811 /* Set window manager hints and properties: size hints, command,
812 * window's name, and accepted protocols */
813 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
815 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
816 p_main->ppsz_argv, p_main->i_argc );
817 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
818 VOUT_TITLE " (X11 output)" );
820 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
821 || (p_vout->p_sys->wm_delete_window == None)
822 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
823 &p_vout->p_sys->wm_delete_window, 1 ) )
825 /* WM_DELETE_WINDOW is not supported by window manager */
826 intf_Msg( "vout error: missing or bad window manager" );
829 /* Creation of a graphic context that doesn't generate a GraphicsExpose
830 * event when using functions like XCopyArea */
831 xgcvalues.graphics_exposures = False;
832 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
833 p_vout->p_sys->window,
834 GCGraphicsExposures, &xgcvalues);
836 /* Send orders to server, and wait until window is displayed - three
837 * events must be received: a MapNotify event, an Expose event allowing
838 * drawing in the window, and a ConfigureNotify to get the window
839 * dimensions. Once those events have been received, only ConfigureNotify
840 * events need to be received. */
842 b_configure_notify = 0;
844 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
847 XNextEvent( p_vout->p_sys->p_display, &xevent);
848 if( (xevent.type == Expose)
849 && (xevent.xexpose.window == p_vout->p_sys->window) )
853 else if( (xevent.type == MapNotify)
854 && (xevent.xmap.window == p_vout->p_sys->window) )
858 else if( (xevent.type == ConfigureNotify)
859 && (xevent.xconfigure.window == p_vout->p_sys->window) )
861 b_configure_notify = 1;
862 p_vout->p_sys->i_width = xevent.xconfigure.width;
863 p_vout->p_sys->i_height = xevent.xconfigure.height;
865 } while( !( b_expose && b_configure_notify && b_map_notify ) );
867 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
868 StructureNotifyMask | KeyPressMask |
869 ButtonPressMask | ButtonReleaseMask |
872 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
874 /* Allocate a new palette */
875 p_vout->p_sys->colormap =
876 XCreateColormap( p_vout->p_sys->p_display,
877 DefaultRootWindow( p_vout->p_sys->p_display ),
878 DefaultVisual( p_vout->p_sys->p_display,
879 p_vout->p_sys->i_screen ),
882 xwindow_attributes.colormap = p_vout->p_sys->colormap;
883 XChangeWindowAttributes( p_vout->p_sys->p_display,
884 p_vout->p_sys->window,
885 CWColormap, &xwindow_attributes );
888 if( p_vout->b_fullscreen )
890 XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
891 RevertToNone, CurrentTime );
892 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
895 /* At this stage, the window is open, displayed, and ready to
901 /*****************************************************************************
902 * X11InitDisplay: open and initialize X11 device
903 *****************************************************************************
904 * Create a window according to video output given size, and set other
905 * properties according to the display properties.
906 *****************************************************************************/
907 static int X11InitDisplay( vout_thread_t *p_vout, char *psz_display )
909 XPixmapFormatValues * p_formats; /* pixmap formats */
910 XVisualInfo * p_xvisual; /* visuals informations */
911 XVisualInfo xvisual_template; /* visual template */
912 int i_count; /* array size */
916 /* Initialize structure */
917 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
918 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
920 if( !p_vout->p_sys->b_shm )
922 intf_Msg( "vout: XShm video extension is not available" );
925 /* Get screen depth */
926 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
927 p_vout->p_sys->i_screen );
928 switch( p_vout->i_screen_depth )
932 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
934 xvisual_template.screen = p_vout->p_sys->i_screen;
935 xvisual_template.class = DirectColor;
936 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
937 VisualScreenMask | VisualClassMask,
938 &xvisual_template, &i_count );
939 if( p_xvisual == NULL )
941 intf_ErrMsg( "vout error: no PseudoColor visual available" );
944 p_vout->i_bytes_per_pixel = 1;
951 * Screen depth is higher than 8bpp. TrueColor visual is used.
953 xvisual_template.screen = p_vout->p_sys->i_screen;
954 xvisual_template.class = TrueColor;
955 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
956 VisualScreenMask | VisualClassMask,
957 &xvisual_template, &i_count );
958 if( p_xvisual == NULL )
960 intf_ErrMsg( "vout error: no TrueColor visual available" );
963 p_vout->i_red_mask = p_xvisual->red_mask;
964 p_vout->i_green_mask = p_xvisual->green_mask;
965 p_vout->i_blue_mask = p_xvisual->blue_mask;
967 /* There is no difference yet between 3 and 4 Bpp. The only way
968 * to find the actual number of bytes per pixel is to list supported
970 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
971 p_vout->i_bytes_per_pixel = 0;
973 for( ; i_count-- ; p_formats++ )
975 /* Under XFree4.0, the list contains pixmap formats available
976 * through all video depths ; so we have to check against current
978 if( p_formats->depth == p_vout->i_screen_depth )
980 if( p_formats->bits_per_pixel / 8
981 > p_vout->i_bytes_per_pixel )
983 p_vout->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
989 p_vout->p_sys->p_visual = p_xvisual->visual;
995 /*****************************************************************************
996 * X11CreateImage: create an XImage
997 *****************************************************************************
998 * Create a simple XImage used as a buffer.
999 *****************************************************************************/
1000 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
1002 byte_t * pb_data; /* image data storage zone */
1003 int i_quantum; /* XImage quantum (see below) */
1005 /* Allocate memory for image */
1006 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
1007 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
1008 if( !pb_data ) /* error */
1010 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
1014 /* Optimize the quantum of a scanline regarding its size - the quantum is
1015 a diviser of the number of bits between the start of two scanlines. */
1016 if( !(( p_vout->i_bytes_per_line ) % 32) )
1022 if( !(( p_vout->i_bytes_per_line ) % 16) )
1033 *pp_ximage = XCreateImage( p_vout->p_sys->p_display,
1034 p_vout->p_sys->p_visual, p_vout->i_screen_depth,
1035 ZPixmap, 0, pb_data,
1036 p_vout->i_width, p_vout->i_height, i_quantum, 0);
1037 if(! *pp_ximage ) /* error */
1039 intf_ErrMsg( "vout error: XCreateImage() failed" );
1047 /*****************************************************************************
1048 * X11CreateShmImage: create an XImage using shared memory extension
1049 *****************************************************************************
1050 * Prepare an XImage for DisplayX11ShmImage function.
1051 * The order of the operations respects the recommandations of the mit-shm
1052 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1054 *****************************************************************************/
1055 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
1056 XShmSegmentInfo *p_shm_info)
1060 XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
1061 p_vout->i_screen_depth, ZPixmap, 0,
1062 p_shm_info, p_vout->i_width, p_vout->i_height );
1063 if(! *pp_ximage ) /* error */
1065 intf_ErrMsg( "vout error: XShmCreateImage() failed" );
1069 /* Allocate shared memory segment - 0777 set the access permission
1070 * rights (like umask), they are not yet supported by X servers */
1072 shmget( IPC_PRIVATE, (*pp_ximage)->bytes_per_line
1073 * (*pp_ximage)->height, IPC_CREAT | 0777);
1074 if( p_shm_info->shmid < 0) /* error */
1076 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
1078 XDestroyImage( *pp_ximage );
1082 /* Attach shared memory segment to process (read/write) */
1083 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
1084 if(! p_shm_info->shmaddr )
1086 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
1088 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
1089 XDestroyImage( *pp_ximage );
1093 /* Mark the shm segment to be removed when there will be no more
1094 * attachements, so it is automatic on process exit or after shmdt */
1095 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1097 /* Attach shared memory segment to X server (read only) */
1098 p_shm_info->readOnly = True;
1099 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info )
1100 == False ) /* error */
1102 intf_ErrMsg( "vout error: cannot attach shared memory to X11 server" );
1103 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
1104 * and automatic free */
1105 XDestroyImage( *pp_ximage );
1109 /* Send image to X server. This instruction is required, since having
1110 * built a Shm XImage and not using it causes an error on XCloseDisplay */
1111 XFlush( p_vout->p_sys->p_display );
1115 /*****************************************************************************
1116 * X11DestroyImage: destroy an XImage
1117 *****************************************************************************
1118 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
1119 * destroyed (see vout_ManageOutputMethod())
1120 *****************************************************************************/
1121 static void X11DestroyImage( XImage *p_ximage )
1123 if( p_ximage != NULL )
1125 XDestroyImage( p_ximage ); /* no free() required */
1129 /*****************************************************************************
1130 * X11DestroyShmImage
1131 *****************************************************************************
1132 * Destroy XImage AND associated data. Detach shared memory segment from
1133 * server and process, then free it. If pointer is NULL, the image won't be
1134 * destroyed (see vout_ManageOutputMethod())
1135 *****************************************************************************/
1136 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
1137 XShmSegmentInfo *p_shm_info )
1139 /* If pointer is NULL, do nothing */
1140 if( p_ximage == NULL )
1145 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
1146 XDestroyImage( p_ximage );
1148 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
1149 { /* also automatic freeing... */
1150 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1156 /* WAZAAAAAAAAAAA */
1158 /*****************************************************************************
1159 * X11EnableScreenSaver: enable screen saver
1160 *****************************************************************************
1161 * This function enable the screen saver on a display after it had been
1162 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1163 * know wether the screen saver can be activated or not: if n successive calls
1164 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1165 * will be required before the screen saver could effectively be activated.
1166 *****************************************************************************/
1167 void X11EnableScreenSaver( vout_thread_t *p_vout )
1169 intf_DbgMsg( "vout: enabling screen saver" );
1170 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1171 p_vout->p_sys->i_ss_interval,
1172 p_vout->p_sys->i_ss_blanking,
1173 p_vout->p_sys->i_ss_exposure );
1176 /*****************************************************************************
1177 * X11DisableScreenSaver: disable screen saver
1178 *****************************************************************************
1179 * See XEnableScreenSaver
1180 *****************************************************************************/
1181 void X11DisableScreenSaver( vout_thread_t *p_vout )
1183 /* Save screen saver informations */
1184 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1185 &p_vout->p_sys->i_ss_interval,
1186 &p_vout->p_sys->i_ss_blanking,
1187 &p_vout->p_sys->i_ss_exposure );
1189 /* Disable screen saver */
1190 intf_DbgMsg( "vout: disabling screen saver" );
1191 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1192 p_vout->p_sys->i_ss_interval,
1193 p_vout->p_sys->i_ss_blanking,
1194 p_vout->p_sys->i_ss_exposure );
1197 /*****************************************************************************
1198 * X11TogglePointer: hide or show the mouse pointer
1199 *****************************************************************************
1200 * This function hides the X pointer if it is visible by putting it at
1201 * coordinates (32,32) and setting the pointer sprite to a blank one. To
1202 * show it again, we disable the sprite and restore the original coordinates.
1203 *****************************************************************************/
1204 void X11TogglePointer( vout_thread_t *p_vout )
1206 static Cursor cursor;
1207 static boolean_t b_cursor = 0;
1209 if( p_vout->p_sys->b_mouse )
1211 p_vout->p_sys->b_mouse = 0;
1216 Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1217 DefaultRootWindow(p_vout->p_sys->p_display),
1220 XParseColor( p_vout->p_sys->p_display,
1221 XCreateColormap( p_vout->p_sys->p_display,
1223 p_vout->p_sys->p_display ),
1225 p_vout->p_sys->p_display,
1226 p_vout->p_sys->i_screen ),
1230 cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1231 blank, blank, &color, &color, 1, 1 );
1235 XDefineCursor( p_vout->p_sys->p_display,
1236 p_vout->p_sys->window, cursor );
1240 p_vout->p_sys->b_mouse = 1;
1242 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );