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.21 2001/04/27 19:29:11 massiot 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 );
538 intf_WarnMsg( 1, "vout: unhandled event %d received", xevent.type );
542 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
543 * are handled - according to the man pages, the format is always 32
545 while( XCheckTypedEvent( p_vout->p_sys->p_display,
546 ClientMessage, &xevent ) )
548 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
549 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
551 p_main->p_intf->b_die = 1;
555 intf_DbgMsg( "vout: unhandled ClientMessage received" );
559 if ( b_gofullscreen )
562 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
563 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
565 intf_DbgMsg( "vout: changing full-screen status" );
567 p_vout->p_sys->b_fullscreen = !p_vout->p_sys->b_fullscreen;
569 /* Get rid of the old window */
570 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
571 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
573 /* And create a new one */
574 if( X11CreateWindow( p_vout ) )
576 intf_ErrMsg( "vout error: cannot create X11 window" );
577 XCloseDisplay( p_vout->p_sys->p_display );
579 free( p_vout->p_sys );
583 if( X11InitDisplay( p_vout, psz_display ) )
585 intf_ErrMsg( "vout error: cannot initialize X11 display" );
586 XCloseDisplay( p_vout->p_sys->p_display );
587 free( p_vout->p_sys );
590 /* We've changed the size, update it */
591 p_vout->i_changes |= VOUT_SIZE_CHANGE;
595 * Handle vout window resizing
599 /* If interface window has been resized, change vout size */
600 intf_DbgMsg( "vout: resizing output window" );
601 p_vout->i_width = p_vout->p_sys->i_width;
602 p_vout->i_height = p_vout->p_sys->i_height;
603 p_vout->i_changes |= VOUT_SIZE_CHANGE;
605 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
606 (p_vout->i_height != p_vout->p_sys->i_height) )
608 /* If video output size has changed, change interface window size */
609 intf_DbgMsg( "vout: resizing output window" );
610 p_vout->p_sys->i_width = p_vout->i_width;
611 p_vout->p_sys->i_height = p_vout->i_height;
612 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
613 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
616 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
618 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
619 && (p_vout->i_screen_depth == 8) )
621 /* FIXME: clear flags ?? */
627 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
629 intf_DbgMsg( "vout info: resizing window" );
630 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
633 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
634 p_vout->i_width, p_vout->i_height );
636 /* Destroy XImages to change their size */
639 /* Recreate XImages. If SysInit failed, the thread can't go on. */
640 if( vout_Init( p_vout ) )
642 intf_ErrMsg( "vout error: cannot resize display" );
646 /* Tell the video output thread that it will need to rebuild YUV
647 * tables. This is needed since conversion buffer size may have
649 p_vout->i_changes |= VOUT_YUV_CHANGE;
650 intf_Msg( "vout: video display resized (%dx%d)",
651 p_vout->i_width, p_vout->i_height);
654 /* Autohide Cursour */
655 if( mdate() - p_vout->p_sys->i_lastmoved > 2000000 )
657 /* Hide the mouse automatically */
658 if( p_vout->p_sys->b_mouse )
660 X11TogglePointer( p_vout );
668 /*****************************************************************************
669 * vout_Display: displays previously rendered output
670 *****************************************************************************
671 * This function send the currently rendered image to X11 server, wait until
672 * it is displayed and switch the two rendering buffer, preparing next frame.
673 *****************************************************************************/
674 static void vout_Display( vout_thread_t *p_vout )
676 if( p_vout->p_sys->b_shm) /* XShm is used */
678 /* Display rendered image using shared memory extension */
679 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
680 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
682 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
683 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
685 /* Send the order to the X server */
686 XSync(p_vout->p_sys->p_display, False);
688 else /* regular X11 capabilities are used */
690 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
691 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
693 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
694 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
696 /* Send the order to the X server */
697 XSync(p_vout->p_sys->p_display, False);
701 /*****************************************************************************
702 * vout_SetPalette: sets an 8 bpp palette
703 *****************************************************************************
704 * This function sets the palette given as an argument. It does not return
705 * anything, but could later send information on which colors it was unable
707 *****************************************************************************/
708 static void vout_SetPalette( p_vout_thread_t p_vout,
709 u16 *red, u16 *green, u16 *blue, u16 *transp )
712 XColor p_colors[255];
714 intf_DbgMsg( "vout: Palette change called" );
716 /* allocate palette */
717 for( i = 0, j = 255; i < 255; i++, j-- )
719 /* kludge: colors are indexed reversely because color 255 seems
720 * to be reserved for black even if we try to set it to white */
721 p_colors[ i ].pixel = j;
722 p_colors[ i ].pad = 0;
723 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
724 p_colors[ i ].red = red[ j ];
725 p_colors[ i ].blue = blue[ j ];
726 p_colors[ i ].green = green[ j ];
729 XStoreColors( p_vout->p_sys->p_display,
730 p_vout->p_sys->colormap, p_colors, 256 );
733 /* following functions are local */
735 /*****************************************************************************
736 * X11CreateWindow: open and set-up X11 main window
737 *****************************************************************************/
738 static int X11CreateWindow( vout_thread_t *p_vout )
740 XSizeHints xsize_hints;
741 XSetWindowAttributes xwindow_attributes;
748 boolean_t b_configure_notify;
749 boolean_t b_map_notify;
751 /* If we're full screen, we're full screen! */
752 if( p_vout->p_sys->b_fullscreen )
754 p_vout->p_sys->i_width = DisplayWidth( p_vout->p_sys->p_display,
755 p_vout->p_sys->i_screen );
756 p_vout->p_sys->i_height = DisplayHeight( p_vout->p_sys->p_display,
757 p_vout->p_sys->i_screen );
758 p_vout->i_width = p_vout->p_sys->i_width;
759 p_vout->i_height = p_vout->p_sys->i_height;
763 /* Set main window's size */
764 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
765 VOUT_WIDTH_DEFAULT );
766 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
767 VOUT_HEIGHT_DEFAULT );
768 p_vout->i_width = p_vout->p_sys->i_width;
769 p_vout->i_height = p_vout->p_sys->i_height;
772 /* Prepare window manager hints and properties */
773 xsize_hints.base_width = p_vout->p_sys->i_width;
774 xsize_hints.base_height = p_vout->p_sys->i_height;
775 xsize_hints.flags = PSize;
776 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
777 "WM_PROTOCOLS", True );
778 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
779 "WM_DELETE_WINDOW", True );
781 /* Prepare window attributes */
782 xwindow_attributes.backing_store = Always; /* save the hidden part */
783 xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
784 p_vout->p_sys->i_screen );
785 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
788 /* Create the window and set hints - the window must receive ConfigureNotify
789 * events, and, until it is displayed, Expose and MapNotify events. */
791 p_vout->p_sys->window =
792 XCreateWindow( p_vout->p_sys->p_display,
793 DefaultRootWindow( p_vout->p_sys->p_display ),
795 p_vout->p_sys->i_width, p_vout->p_sys->i_height, 0,
797 CWBackingStore | CWBackPixel | CWEventMask,
798 &xwindow_attributes );
800 if ( p_vout->p_sys->b_fullscreen )
802 prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
803 mwmhints.flags = MWM_HINTS_DECORATIONS;
804 mwmhints.decorations = 0;
805 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
806 prop, prop, 32, PropModeReplace,
807 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
809 XSetTransientForHint( p_vout->p_sys->p_display,
810 p_vout->p_sys->window, None );
811 XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
814 /* Set window manager hints and properties: size hints, command,
815 * window's name, and accepted protocols */
816 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
818 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
819 p_main->ppsz_argv, p_main->i_argc );
820 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
821 VOUT_TITLE " (X11 output)" );
823 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
824 || (p_vout->p_sys->wm_delete_window == None)
825 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
826 &p_vout->p_sys->wm_delete_window, 1 ) )
828 /* WM_DELETE_WINDOW is not supported by window manager */
829 intf_Msg( "vout error: missing or bad window manager" );
832 /* Creation of a graphic context that doesn't generate a GraphicsExpose
833 * event when using functions like XCopyArea */
834 xgcvalues.graphics_exposures = False;
835 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
836 p_vout->p_sys->window,
837 GCGraphicsExposures, &xgcvalues);
839 /* Send orders to server, and wait until window is displayed - three
840 * events must be received: a MapNotify event, an Expose event allowing
841 * drawing in the window, and a ConfigureNotify to get the window
842 * dimensions. Once those events have been received, only ConfigureNotify
843 * events need to be received. */
845 b_configure_notify = 0;
847 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
850 XNextEvent( p_vout->p_sys->p_display, &xevent);
851 if( (xevent.type == Expose)
852 && (xevent.xexpose.window == p_vout->p_sys->window) )
856 else if( (xevent.type == MapNotify)
857 && (xevent.xmap.window == p_vout->p_sys->window) )
861 else if( (xevent.type == ConfigureNotify)
862 && (xevent.xconfigure.window == p_vout->p_sys->window) )
864 b_configure_notify = 1;
865 p_vout->p_sys->i_width = xevent.xconfigure.width;
866 p_vout->p_sys->i_height = xevent.xconfigure.height;
868 } while( !( b_expose && b_configure_notify && b_map_notify ) );
870 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
871 StructureNotifyMask | KeyPressMask |
872 ButtonPressMask | ButtonReleaseMask |
875 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
877 /* Allocate a new palette */
878 p_vout->p_sys->colormap =
879 XCreateColormap( p_vout->p_sys->p_display,
880 DefaultRootWindow( p_vout->p_sys->p_display ),
881 DefaultVisual( p_vout->p_sys->p_display,
882 p_vout->p_sys->i_screen ),
885 xwindow_attributes.colormap = p_vout->p_sys->colormap;
886 XChangeWindowAttributes( p_vout->p_sys->p_display,
887 p_vout->p_sys->window,
888 CWColormap, &xwindow_attributes );
891 if( p_vout->p_sys->b_fullscreen )
893 XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
894 RevertToNone, CurrentTime );
895 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
898 /* At this stage, the window is open, displayed, and ready to
904 /*****************************************************************************
905 * X11InitDisplay: open and initialize X11 device
906 *****************************************************************************
907 * Create a window according to video output given size, and set other
908 * properties according to the display properties.
909 *****************************************************************************/
910 static int X11InitDisplay( vout_thread_t *p_vout, char *psz_display )
912 XPixmapFormatValues * p_formats; /* pixmap formats */
913 XVisualInfo * p_xvisual; /* visuals informations */
914 XVisualInfo xvisual_template; /* visual template */
915 int i_count; /* array size */
919 /* Initialize structure */
920 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
921 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
923 if( !p_vout->p_sys->b_shm )
925 intf_Msg( "vout: XShm video extension is not available" );
928 /* Get screen depth */
929 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
930 p_vout->p_sys->i_screen );
931 switch( p_vout->i_screen_depth )
935 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
937 xvisual_template.screen = p_vout->p_sys->i_screen;
938 xvisual_template.class = DirectColor;
939 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
940 VisualScreenMask | VisualClassMask,
941 &xvisual_template, &i_count );
942 if( p_xvisual == NULL )
944 intf_ErrMsg( "vout error: no PseudoColor visual available" );
947 p_vout->i_bytes_per_pixel = 1;
954 * Screen depth is higher than 8bpp. TrueColor visual is used.
956 xvisual_template.screen = p_vout->p_sys->i_screen;
957 xvisual_template.class = TrueColor;
958 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
959 VisualScreenMask | VisualClassMask,
960 &xvisual_template, &i_count );
961 if( p_xvisual == NULL )
963 intf_ErrMsg( "vout error: no TrueColor visual available" );
966 p_vout->i_red_mask = p_xvisual->red_mask;
967 p_vout->i_green_mask = p_xvisual->green_mask;
968 p_vout->i_blue_mask = p_xvisual->blue_mask;
970 /* There is no difference yet between 3 and 4 Bpp. The only way
971 * to find the actual number of bytes per pixel is to list supported
973 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
974 p_vout->i_bytes_per_pixel = 0;
976 for( ; i_count-- ; p_formats++ )
978 /* Under XFree4.0, the list contains pixmap formats available
979 * through all video depths ; so we have to check against current
981 if( p_formats->depth == p_vout->i_screen_depth )
983 if( p_formats->bits_per_pixel / 8
984 > p_vout->i_bytes_per_pixel )
986 p_vout->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
992 p_vout->p_sys->p_visual = p_xvisual->visual;
998 /*****************************************************************************
999 * X11CreateImage: create an XImage
1000 *****************************************************************************
1001 * Create a simple XImage used as a buffer.
1002 *****************************************************************************/
1003 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
1005 byte_t * pb_data; /* image data storage zone */
1006 int i_quantum; /* XImage quantum (see below) */
1008 /* Allocate memory for image */
1009 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
1010 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
1011 if( !pb_data ) /* error */
1013 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
1017 /* Optimize the quantum of a scanline regarding its size - the quantum is
1018 a diviser of the number of bits between the start of two scanlines. */
1019 if( !(( p_vout->i_bytes_per_line ) % 32) )
1025 if( !(( p_vout->i_bytes_per_line ) % 16) )
1036 *pp_ximage = XCreateImage( p_vout->p_sys->p_display,
1037 p_vout->p_sys->p_visual, p_vout->i_screen_depth,
1038 ZPixmap, 0, pb_data,
1039 p_vout->i_width, p_vout->i_height, i_quantum, 0);
1040 if(! *pp_ximage ) /* error */
1042 intf_ErrMsg( "vout error: XCreateImage() failed" );
1050 /*****************************************************************************
1051 * X11CreateShmImage: create an XImage using shared memory extension
1052 *****************************************************************************
1053 * Prepare an XImage for DisplayX11ShmImage function.
1054 * The order of the operations respects the recommandations of the mit-shm
1055 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1057 *****************************************************************************/
1058 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
1059 XShmSegmentInfo *p_shm_info)
1063 XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
1064 p_vout->i_screen_depth, ZPixmap, 0,
1065 p_shm_info, p_vout->i_width, p_vout->i_height );
1066 if(! *pp_ximage ) /* error */
1068 intf_ErrMsg( "vout error: XShmCreateImage() failed" );
1072 /* Allocate shared memory segment - 0777 set the access permission
1073 * rights (like umask), they are not yet supported by X servers */
1075 shmget( IPC_PRIVATE, (*pp_ximage)->bytes_per_line
1076 * (*pp_ximage)->height, IPC_CREAT | 0777);
1077 if( p_shm_info->shmid < 0) /* error */
1079 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
1081 XDestroyImage( *pp_ximage );
1085 /* Attach shared memory segment to process (read/write) */
1086 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
1087 if(! p_shm_info->shmaddr )
1089 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
1091 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
1092 XDestroyImage( *pp_ximage );
1096 /* Mark the shm segment to be removed when there will be no more
1097 * attachements, so it is automatic on process exit or after shmdt */
1098 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1100 /* Attach shared memory segment to X server (read only) */
1101 p_shm_info->readOnly = True;
1102 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info )
1103 == False ) /* error */
1105 intf_ErrMsg( "vout error: cannot attach shared memory to X11 server" );
1106 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
1107 * and automatic free */
1108 XDestroyImage( *pp_ximage );
1112 /* Send image to X server. This instruction is required, since having
1113 * built a Shm XImage and not using it causes an error on XCloseDisplay */
1114 XFlush( p_vout->p_sys->p_display );
1118 /*****************************************************************************
1119 * X11DestroyImage: destroy an XImage
1120 *****************************************************************************
1121 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
1122 * destroyed (see vout_ManageOutputMethod())
1123 *****************************************************************************/
1124 static void X11DestroyImage( XImage *p_ximage )
1126 if( p_ximage != NULL )
1128 XDestroyImage( p_ximage ); /* no free() required */
1132 /*****************************************************************************
1133 * X11DestroyShmImage
1134 *****************************************************************************
1135 * Destroy XImage AND associated data. Detach shared memory segment from
1136 * server and process, then free it. If pointer is NULL, the image won't be
1137 * destroyed (see vout_ManageOutputMethod())
1138 *****************************************************************************/
1139 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
1140 XShmSegmentInfo *p_shm_info )
1142 /* If pointer is NULL, do nothing */
1143 if( p_ximage == NULL )
1148 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
1149 XDestroyImage( p_ximage );
1151 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
1152 { /* also automatic freeing... */
1153 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1159 /* WAZAAAAAAAAAAA */
1161 /*****************************************************************************
1162 * X11EnableScreenSaver: enable screen saver
1163 *****************************************************************************
1164 * This function enable the screen saver on a display after it had been
1165 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1166 * know wether the screen saver can be activated or not: if n successive calls
1167 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1168 * will be required before the screen saver could effectively be activated.
1169 *****************************************************************************/
1170 void X11EnableScreenSaver( vout_thread_t *p_vout )
1172 intf_DbgMsg( "vout: enabling screen saver" );
1173 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1174 p_vout->p_sys->i_ss_interval,
1175 p_vout->p_sys->i_ss_blanking,
1176 p_vout->p_sys->i_ss_exposure );
1179 /*****************************************************************************
1180 * X11DisableScreenSaver: disable screen saver
1181 *****************************************************************************
1182 * See XEnableScreenSaver
1183 *****************************************************************************/
1184 void X11DisableScreenSaver( vout_thread_t *p_vout )
1186 /* Save screen saver informations */
1187 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1188 &p_vout->p_sys->i_ss_interval,
1189 &p_vout->p_sys->i_ss_blanking,
1190 &p_vout->p_sys->i_ss_exposure );
1192 /* Disable screen saver */
1193 intf_DbgMsg( "vout: disabling screen saver" );
1194 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1195 p_vout->p_sys->i_ss_interval,
1196 p_vout->p_sys->i_ss_blanking,
1197 p_vout->p_sys->i_ss_exposure );
1200 /*****************************************************************************
1201 * X11TogglePointer: hide or show the mouse pointer
1202 *****************************************************************************
1203 * This function hides the X pointer if it is visible by putting it at
1204 * coordinates (32,32) and setting the pointer sprite to a blank one. To
1205 * show it again, we disable the sprite and restore the original coordinates.
1206 *****************************************************************************/
1207 void X11TogglePointer( vout_thread_t *p_vout )
1209 static Cursor cursor;
1210 static boolean_t b_cursor = 0;
1212 if( p_vout->p_sys->b_mouse )
1214 p_vout->p_sys->b_mouse = 0;
1219 Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1220 DefaultRootWindow(p_vout->p_sys->p_display),
1223 XParseColor( p_vout->p_sys->p_display,
1224 XCreateColormap( p_vout->p_sys->p_display,
1226 p_vout->p_sys->p_display ),
1228 p_vout->p_sys->p_display,
1229 p_vout->p_sys->i_screen ),
1233 cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1234 blank, blank, &color, &color, 1, 1 );
1238 XDefineCursor( p_vout->p_sys->p_display,
1239 p_vout->p_sys->window, cursor );
1243 p_vout->p_sys->b_mouse = 1;
1245 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );