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.26 2001/05/30 17:03:12 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 */
46 #include <netinet/in.h> /* BSD: struct in_addr */
49 #include <sys/shm.h> /* shmget(), shmctl() */
51 #include <X11/Xutil.h>
52 #include <X11/keysym.h>
53 #include <X11/extensions/XShm.h>
62 #include "video_output.h"
64 #include "interface.h"
67 #include "netutils.h" /* network_ChannelJoin */
72 #include "modules_export.h"
74 /*****************************************************************************
75 * vout_sys_t: video output X11 method descriptor
76 *****************************************************************************
77 * This structure is part of the video output thread descriptor.
78 * It describes the X11 specific properties of an output thread. X11 video
79 * output is performed through regular resizable windows. Windows can be
80 * dynamically resized to adapt to the size of the streams.
81 *****************************************************************************/
82 typedef struct vout_sys_s
85 boolean_t b_shm; /* shared memory extension flag */
87 /* Internal settings and properties */
88 Display * p_display; /* display pointer */
89 Visual * p_visual; /* visual pointer */
90 int i_screen; /* screen number */
91 Window window; /* root window */
92 GC gc; /* graphic context instance handler */
93 Colormap colormap; /* colormap used (8bpp only) */
95 /* Display buffers and shared memory information */
96 XImage * p_ximage[2]; /* XImage pointer */
97 XShmSegmentInfo shm_info[2]; /* shared memory zone information */
99 /* X11 generic properties */
101 Atom wm_delete_window;
103 int i_width; /* width of main window */
104 int i_height; /* height of main window */
106 /* Screen saver properties */
107 int i_ss_timeout; /* timeout */
108 int i_ss_interval; /* interval between changes */
109 int i_ss_blanking; /* blanking mode */
110 int i_ss_exposure; /* exposure mode */
112 /* Auto-hide cursor */
115 /* Mouse pointer properties */
116 boolean_t b_mouse; /* is the mouse pointer displayed ? */
120 /* Fullscreen needs to be able to hide the wm decorations */
121 #define MWM_HINTS_DECORATIONS (1L << 1)
122 #define PROP_MWM_HINTS_ELEMENTS 5
123 typedef struct mwmhints_s
132 /*****************************************************************************
134 *****************************************************************************/
135 static int vout_Probe ( probedata_t *p_data );
136 static int vout_Create ( struct vout_thread_s * );
137 static int vout_Init ( struct vout_thread_s * );
138 static void vout_End ( struct vout_thread_s * );
139 static void vout_Destroy ( struct vout_thread_s * );
140 static int vout_Manage ( struct vout_thread_s * );
141 static void vout_Display ( struct vout_thread_s * );
142 static void vout_SetPalette( struct vout_thread_s *, u16*, u16*, u16*, u16* );
144 static int X11CreateWindow ( vout_thread_t *p_vout );
145 static int X11InitDisplay ( vout_thread_t *p_vout, char *psz_display );
147 static int X11CreateImage ( vout_thread_t *p_vout, XImage **pp_ximage );
148 static void X11DestroyImage ( XImage *p_ximage );
149 static int X11CreateShmImage ( vout_thread_t *p_vout, XImage **pp_ximage,
150 XShmSegmentInfo *p_shm_info );
151 static void X11DestroyShmImage ( vout_thread_t *p_vout, XImage *p_ximage,
152 XShmSegmentInfo *p_shm_info );
154 static void X11TogglePointer ( vout_thread_t *p_vout );
155 static void X11EnableScreenSaver ( vout_thread_t *p_vout );
156 static void X11DisableScreenSaver ( vout_thread_t *p_vout );
158 /*****************************************************************************
159 * Functions exported as capabilities. They are declared as static so that
160 * we don't pollute the namespace too much.
161 *****************************************************************************/
162 void _M( vout_getfunctions )( function_list_t * p_function_list )
164 p_function_list->pf_probe = vout_Probe;
165 p_function_list->functions.vout.pf_create = vout_Create;
166 p_function_list->functions.vout.pf_init = vout_Init;
167 p_function_list->functions.vout.pf_end = vout_End;
168 p_function_list->functions.vout.pf_destroy = vout_Destroy;
169 p_function_list->functions.vout.pf_manage = vout_Manage;
170 p_function_list->functions.vout.pf_display = vout_Display;
171 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
174 /*****************************************************************************
175 * vout_Probe: probe the video driver and return a score
176 *****************************************************************************
177 * This function tries to initialize SDL and returns a score to the
178 * plugin manager so that it can select the best plugin.
179 *****************************************************************************/
180 static int vout_Probe( probedata_t *p_data )
182 if( TestMethod( VOUT_METHOD_VAR, "x11" ) )
190 /*****************************************************************************
191 * vout_Create: allocate X11 video thread output method
192 *****************************************************************************
193 * This function allocate and initialize a X11 vout method. It uses some of the
194 * vout properties to choose the window size, and change them according to the
195 * actual properties of the display.
196 *****************************************************************************/
197 static int vout_Create( vout_thread_t *p_vout )
201 /* Allocate structure */
202 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
203 if( p_vout->p_sys == NULL )
205 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
209 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
210 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
211 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
213 if( p_vout->p_sys->p_display == NULL ) /* error */
215 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
216 free( p_vout->p_sys );
219 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
222 = main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT );
224 /* Spawn base window - this window will include the video output window,
225 * but also command buttons, subtitles and other indicators */
227 if( X11CreateWindow( p_vout ) )
229 intf_ErrMsg( "vout error: cannot create X11 window" );
230 XCloseDisplay( p_vout->p_sys->p_display );
231 free( p_vout->p_sys );
235 /* Open and initialize device. This function issues its own error messages.
236 * Since XLib is usually not thread-safe, we can't use the same display
237 * pointer than the interface or another thread. However, the root window
238 * id is still valid. */
239 if( X11InitDisplay( p_vout, psz_display ) )
241 intf_ErrMsg( "vout error: cannot initialize X11 display" );
242 XCloseDisplay( p_vout->p_sys->p_display );
243 free( p_vout->p_sys );
247 p_vout->p_sys->b_mouse = 1;
249 /* Disable screen saver and return */
250 X11DisableScreenSaver( p_vout );
255 /*****************************************************************************
256 * vout_Init: initialize X11 video thread output method
257 *****************************************************************************
258 * This function create the XImages needed by the output thread. It is called
259 * at the beginning of the thread, but also each time the window is resized.
260 *****************************************************************************/
261 static int vout_Init( vout_thread_t *p_vout )
266 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
267 p_vout->p_sys->b_shm = 0;
270 /* Create XImages using XShm extension - on failure, fall back to regular
271 * way (and destroy the first image if it was created successfully) */
272 if( p_vout->p_sys->b_shm )
274 /* Create first image */
275 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
276 &p_vout->p_sys->shm_info[0] );
277 if( !i_err ) /* first image has been created */
279 /* Create second image */
280 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
281 &p_vout->p_sys->shm_info[1] ) )
282 { /* error creating the second image */
283 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
284 &p_vout->p_sys->shm_info[0] );
288 if( i_err ) /* an error occured */
290 intf_Msg( "vout: XShm video extension unavailable" );
291 p_vout->p_sys->b_shm = 0;
295 /* Create XImages without XShm extension */
296 if( !p_vout->p_sys->b_shm )
298 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
300 intf_ErrMsg( "vout error: cannot create images" );
301 p_vout->p_sys->p_ximage[0] = NULL;
302 p_vout->p_sys->p_ximage[1] = NULL;
305 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
307 intf_ErrMsg( "vout error: cannot create images" );
308 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
309 p_vout->p_sys->p_ximage[0] = NULL;
310 p_vout->p_sys->p_ximage[1] = NULL;
315 /* Set bytes per line and initialize buffers */
316 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
317 p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
318 p_vout->p_sys->p_ximage[ 1 ]->data );
320 /* Set date for autohiding cursor */
321 p_vout->p_sys->i_lastmoved = mdate();
326 /*****************************************************************************
327 * vout_End: terminate X11 video thread output method
328 *****************************************************************************
329 * Destroy the X11 XImages created by vout_Init. It is called at the end of
330 * the thread, but also each time the window is resized.
331 *****************************************************************************/
332 static void vout_End( vout_thread_t *p_vout )
334 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
336 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
337 &p_vout->p_sys->shm_info[0] );
338 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
339 &p_vout->p_sys->shm_info[1] );
341 else /* ...or regular XImages */
343 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
344 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
348 /*****************************************************************************
349 * vout_Destroy: destroy X11 video thread output method
350 *****************************************************************************
351 * Terminate an output method created by vout_CreateOutputMethod
352 *****************************************************************************/
353 static void vout_Destroy( vout_thread_t *p_vout )
355 /* Enable screen saver */
356 X11EnableScreenSaver( p_vout );
358 /* Destroy colormap */
359 if( p_vout->i_screen_depth == 8 )
361 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
365 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
366 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
367 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
369 XCloseDisplay( p_vout->p_sys->p_display );
371 /* Destroy structure */
372 free( p_vout->p_sys );
375 /*****************************************************************************
376 * vout_Manage: handle X11 events
377 *****************************************************************************
378 * This function should be called regularly by video output thread. It manages
379 * X11 events and allows window resizing. It returns a non null value on
381 *****************************************************************************/
382 static int vout_Manage( vout_thread_t *p_vout )
384 XEvent xevent; /* X11 event */
385 boolean_t b_resized; /* window has been resized */
386 char i_key; /* ISO Latin-1 key */
389 /* Handle X11 events: ConfigureNotify events are parsed to know if the
390 * output window's size changed, MapNotify and UnmapNotify to know if the
391 * window is mapped (and if the display is useful), and ClientMessages
392 * to intercept window destruction requests */
395 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
396 StructureNotifyMask | KeyPressMask |
397 ButtonPressMask | ButtonReleaseMask |
398 PointerMotionMask | Button1MotionMask , &xevent )
401 /* ConfigureNotify event: prepare */
402 if( (xevent.type == ConfigureNotify)
403 && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
404 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
406 /* Update dimensions */
408 p_vout->p_sys->i_width = xevent.xconfigure.width;
409 p_vout->p_sys->i_height = xevent.xconfigure.height;
411 /* MapNotify event: change window status and disable screen saver */
412 else if( xevent.type == MapNotify)
414 if( (p_vout != NULL) && !p_vout->b_active )
416 X11DisableScreenSaver( p_vout );
417 p_vout->b_active = 1;
420 /* UnmapNotify event: change window status and enable screen saver */
421 else if( xevent.type == UnmapNotify )
423 if( (p_vout != NULL) && p_vout->b_active )
425 X11EnableScreenSaver( p_vout );
426 p_vout->b_active = 0;
430 else if( xevent.type == KeyPress )
432 /* We may have keys like F1 trough F12, ESC ... */
433 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
434 xevent.xkey.keycode, 0 );
435 switch( x_key_symbol )
438 p_main->p_intf->b_die = 1;
441 p_main->p_intf->b_menu_change = 1;
445 * The reason why I use this instead of XK_0 is that
446 * with XLookupString, we don't have to care about
449 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
451 /* FIXME: handle stuff here */
456 p_main->p_intf->b_die = 1;
460 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
463 network_ChannelJoin( 0 );
466 network_ChannelJoin( 1 );
469 network_ChannelJoin( 2 );
472 network_ChannelJoin( 3 );
475 network_ChannelJoin( 4 );
478 network_ChannelJoin( 5 );
481 network_ChannelJoin( 6 );
484 network_ChannelJoin( 7 );
487 network_ChannelJoin( 8 );
490 network_ChannelJoin( 9 );
493 if( intf_ProcessKey( p_main->p_intf,
496 intf_DbgMsg( "vout: unhandled key '%c' (%i)",
497 (char)i_key, i_key );
506 else if( xevent.type == ButtonPress )
508 switch( ((XButtonEvent *)&xevent)->button )
511 /* in this part we will eventually manage
512 * clicks for DVD navigation for instance */
517 else if( xevent.type == ButtonRelease )
519 switch( ((XButtonEvent *)&xevent)->button )
522 /* FIXME: need locking ! */
523 p_main->p_intf->b_menu_change = 1;
528 else if( xevent.type == MotionNotify )
530 p_vout->p_sys->i_lastmoved = mdate();
531 if( ! p_vout->p_sys->b_mouse )
533 X11TogglePointer( p_vout );
539 intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
543 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
544 * are handled - according to the man pages, the format is always 32
546 while( XCheckTypedEvent( p_vout->p_sys->p_display,
547 ClientMessage, &xevent ) )
549 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
550 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
552 p_main->p_intf->b_die = 1;
556 intf_DbgMsg( "vout: unhandled ClientMessage received" );
560 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
563 /* Open display, unsing 'vlc_display' or the DISPLAY
564 * environment variable */
565 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
567 intf_DbgMsg( "vout: changing full-screen status" );
569 p_vout->b_fullscreen = !p_vout->b_fullscreen;
570 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
572 /* Get rid of the old window */
573 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
574 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
576 /* And create a new one */
577 if( X11CreateWindow( p_vout ) )
579 intf_ErrMsg( "vout error: cannot create X11 window" );
580 XCloseDisplay( p_vout->p_sys->p_display );
582 free( p_vout->p_sys );
586 if( X11InitDisplay( p_vout, psz_display ) )
588 intf_ErrMsg( "vout error: cannot initialize X11 display" );
589 XCloseDisplay( p_vout->p_sys->p_display );
590 free( p_vout->p_sys );
593 /* We've changed the size, update it */
594 p_vout->i_changes |= VOUT_SIZE_CHANGE;
598 * Handle vout window resizing
602 /* If interface window has been resized, change vout size */
603 intf_DbgMsg( "vout: resizing output window" );
604 p_vout->i_width = p_vout->p_sys->i_width;
605 p_vout->i_height = p_vout->p_sys->i_height;
606 p_vout->i_changes |= VOUT_SIZE_CHANGE;
608 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
609 (p_vout->i_height != p_vout->p_sys->i_height) )
611 /* If video output size has changed, change interface window size */
612 intf_DbgMsg( "vout: resizing output window" );
613 p_vout->p_sys->i_width = p_vout->i_width;
614 p_vout->p_sys->i_height = p_vout->i_height;
615 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
616 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
619 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
621 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
622 && (p_vout->i_screen_depth == 8) )
624 /* FIXME: clear flags ?? */
630 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
632 intf_DbgMsg( "vout info: resizing window" );
633 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
636 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
637 p_vout->i_width, p_vout->i_height );
639 /* Destroy XImages to change their size */
642 /* Recreate XImages. If SysInit failed, the thread can't go on. */
643 if( vout_Init( p_vout ) )
645 intf_ErrMsg( "vout error: cannot resize display" );
649 /* Tell the video output thread that it will need to rebuild YUV
650 * tables. This is needed since conversion buffer size may have
652 p_vout->i_changes |= VOUT_YUV_CHANGE;
653 intf_Msg( "vout: video display resized (%dx%d)",
654 p_vout->i_width, p_vout->i_height);
657 /* Autohide Cursour */
658 if( mdate() - p_vout->p_sys->i_lastmoved > 2000000 )
660 /* Hide the mouse automatically */
661 if( p_vout->p_sys->b_mouse )
663 X11TogglePointer( p_vout );
671 /*****************************************************************************
672 * vout_Display: displays previously rendered output
673 *****************************************************************************
674 * This function send the currently rendered image to X11 server, wait until
675 * it is displayed and switch the two rendering buffer, preparing next frame.
676 *****************************************************************************/
677 static void vout_Display( vout_thread_t *p_vout )
679 if( p_vout->p_sys->b_shm) /* XShm is used */
681 /* Display rendered image using shared memory extension */
682 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
683 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
685 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
686 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
688 /* Send the order to the X server */
689 XSync(p_vout->p_sys->p_display, False);
691 else /* regular X11 capabilities are used */
693 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
694 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
696 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
697 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
699 /* Send the order to the X server */
700 XSync(p_vout->p_sys->p_display, False);
704 /*****************************************************************************
705 * vout_SetPalette: sets an 8 bpp palette
706 *****************************************************************************
707 * This function sets the palette given as an argument. It does not return
708 * anything, but could later send information on which colors it was unable
710 *****************************************************************************/
711 static void vout_SetPalette( p_vout_thread_t p_vout,
712 u16 *red, u16 *green, u16 *blue, u16 *transp )
715 XColor p_colors[255];
717 intf_DbgMsg( "vout: Palette change called" );
719 /* allocate palette */
720 for( i = 0, j = 255; i < 255; i++, j-- )
722 /* kludge: colors are indexed reversely because color 255 seems
723 * to be reserved for black even if we try to set it to white */
724 p_colors[ i ].pixel = j;
725 p_colors[ i ].pad = 0;
726 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
727 p_colors[ i ].red = red[ j ];
728 p_colors[ i ].blue = blue[ j ];
729 p_colors[ i ].green = green[ j ];
732 XStoreColors( p_vout->p_sys->p_display,
733 p_vout->p_sys->colormap, p_colors, 256 );
736 /* following functions are local */
738 /*****************************************************************************
739 * X11CreateWindow: open and set-up X11 main window
740 *****************************************************************************/
741 static int X11CreateWindow( vout_thread_t *p_vout )
743 XSizeHints xsize_hints;
744 XSetWindowAttributes xwindow_attributes;
751 boolean_t b_configure_notify;
752 boolean_t b_map_notify;
754 /* If we're full screen, we're full screen! */
755 if( p_vout->b_fullscreen )
757 p_vout->p_sys->i_width = DisplayWidth( p_vout->p_sys->p_display,
758 p_vout->p_sys->i_screen );
759 p_vout->p_sys->i_height = DisplayHeight( p_vout->p_sys->p_display,
760 p_vout->p_sys->i_screen );
761 p_vout->i_width = p_vout->p_sys->i_width;
762 p_vout->i_height = p_vout->p_sys->i_height;
766 /* Set main window's size */
767 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
768 VOUT_WIDTH_DEFAULT );
769 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
770 VOUT_HEIGHT_DEFAULT );
771 p_vout->i_width = p_vout->p_sys->i_width;
772 p_vout->i_height = p_vout->p_sys->i_height;
775 /* Prepare window manager hints and properties */
776 xsize_hints.base_width = p_vout->p_sys->i_width;
777 xsize_hints.base_height = p_vout->p_sys->i_height;
778 xsize_hints.flags = PSize;
779 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
780 "WM_PROTOCOLS", True );
781 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
782 "WM_DELETE_WINDOW", True );
784 /* Prepare window attributes */
785 xwindow_attributes.backing_store = Always; /* save the hidden part */
786 xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
787 p_vout->p_sys->i_screen );
788 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
791 /* Create the window and set hints - the window must receive ConfigureNotify
792 * events, and, until it is displayed, Expose and MapNotify events. */
794 p_vout->p_sys->window =
795 XCreateWindow( p_vout->p_sys->p_display,
796 DefaultRootWindow( p_vout->p_sys->p_display ),
798 p_vout->p_sys->i_width, p_vout->p_sys->i_height, 0,
800 CWBackingStore | CWBackPixel | CWEventMask,
801 &xwindow_attributes );
803 if ( p_vout->b_fullscreen )
805 prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
806 mwmhints.flags = MWM_HINTS_DECORATIONS;
807 mwmhints.decorations = 0;
808 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
809 prop, prop, 32, PropModeReplace,
810 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
812 XSetTransientForHint( p_vout->p_sys->p_display,
813 p_vout->p_sys->window, None );
814 XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
817 /* Set window manager hints and properties: size hints, command,
818 * window's name, and accepted protocols */
819 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
821 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
822 p_main->ppsz_argv, p_main->i_argc );
823 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
824 VOUT_TITLE " (X11 output)" );
826 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
827 || (p_vout->p_sys->wm_delete_window == None)
828 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
829 &p_vout->p_sys->wm_delete_window, 1 ) )
831 /* WM_DELETE_WINDOW is not supported by window manager */
832 intf_Msg( "vout error: missing or bad window manager" );
835 /* Creation of a graphic context that doesn't generate a GraphicsExpose
836 * event when using functions like XCopyArea */
837 xgcvalues.graphics_exposures = False;
838 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
839 p_vout->p_sys->window,
840 GCGraphicsExposures, &xgcvalues);
842 /* Send orders to server, and wait until window is displayed - three
843 * events must be received: a MapNotify event, an Expose event allowing
844 * drawing in the window, and a ConfigureNotify to get the window
845 * dimensions. Once those events have been received, only ConfigureNotify
846 * events need to be received. */
848 b_configure_notify = 0;
850 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
853 XNextEvent( p_vout->p_sys->p_display, &xevent);
854 if( (xevent.type == Expose)
855 && (xevent.xexpose.window == p_vout->p_sys->window) )
859 else if( (xevent.type == MapNotify)
860 && (xevent.xmap.window == p_vout->p_sys->window) )
864 else if( (xevent.type == ConfigureNotify)
865 && (xevent.xconfigure.window == p_vout->p_sys->window) )
867 b_configure_notify = 1;
868 p_vout->p_sys->i_width = xevent.xconfigure.width;
869 p_vout->p_sys->i_height = xevent.xconfigure.height;
871 } while( !( b_expose && b_configure_notify && b_map_notify ) );
873 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
874 StructureNotifyMask | KeyPressMask |
875 ButtonPressMask | ButtonReleaseMask |
878 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
880 /* Allocate a new palette */
881 p_vout->p_sys->colormap =
882 XCreateColormap( p_vout->p_sys->p_display,
883 DefaultRootWindow( p_vout->p_sys->p_display ),
884 DefaultVisual( p_vout->p_sys->p_display,
885 p_vout->p_sys->i_screen ),
888 xwindow_attributes.colormap = p_vout->p_sys->colormap;
889 XChangeWindowAttributes( p_vout->p_sys->p_display,
890 p_vout->p_sys->window,
891 CWColormap, &xwindow_attributes );
894 if( p_vout->b_fullscreen )
896 XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
897 RevertToNone, CurrentTime );
898 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
901 /* At this stage, the window is open, displayed, and ready to
907 /*****************************************************************************
908 * X11InitDisplay: open and initialize X11 device
909 *****************************************************************************
910 * Create a window according to video output given size, and set other
911 * properties according to the display properties.
912 *****************************************************************************/
913 static int X11InitDisplay( vout_thread_t *p_vout, char *psz_display )
915 XPixmapFormatValues * p_formats; /* pixmap formats */
916 XVisualInfo * p_xvisual; /* visuals informations */
917 XVisualInfo xvisual_template; /* visual template */
918 int i_count; /* array size */
922 /* Initialize structure */
923 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
924 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
926 if( !p_vout->p_sys->b_shm )
928 intf_Msg( "vout: XShm video extension is not available" );
931 /* Get screen depth */
932 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
933 p_vout->p_sys->i_screen );
934 switch( p_vout->i_screen_depth )
938 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
940 xvisual_template.screen = p_vout->p_sys->i_screen;
941 xvisual_template.class = DirectColor;
942 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
943 VisualScreenMask | VisualClassMask,
944 &xvisual_template, &i_count );
945 if( p_xvisual == NULL )
947 intf_ErrMsg( "vout error: no PseudoColor visual available" );
950 p_vout->i_bytes_per_pixel = 1;
957 * Screen depth is higher than 8bpp. TrueColor visual is used.
959 xvisual_template.screen = p_vout->p_sys->i_screen;
960 xvisual_template.class = TrueColor;
961 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
962 VisualScreenMask | VisualClassMask,
963 &xvisual_template, &i_count );
964 if( p_xvisual == NULL )
966 intf_ErrMsg( "vout error: no TrueColor visual available" );
969 p_vout->i_red_mask = p_xvisual->red_mask;
970 p_vout->i_green_mask = p_xvisual->green_mask;
971 p_vout->i_blue_mask = p_xvisual->blue_mask;
973 /* There is no difference yet between 3 and 4 Bpp. The only way
974 * to find the actual number of bytes per pixel is to list supported
976 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
977 p_vout->i_bytes_per_pixel = 0;
979 for( ; i_count-- ; p_formats++ )
981 /* Under XFree4.0, the list contains pixmap formats available
982 * through all video depths ; so we have to check against current
984 if( p_formats->depth == p_vout->i_screen_depth )
986 if( p_formats->bits_per_pixel / 8
987 > p_vout->i_bytes_per_pixel )
989 p_vout->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
995 p_vout->p_sys->p_visual = p_xvisual->visual;
1001 /*****************************************************************************
1002 * X11CreateImage: create an XImage
1003 *****************************************************************************
1004 * Create a simple XImage used as a buffer.
1005 *****************************************************************************/
1006 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
1008 byte_t * pb_data; /* image data storage zone */
1009 int i_quantum; /* XImage quantum (see below) */
1011 /* Allocate memory for image */
1012 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
1013 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
1014 if( !pb_data ) /* error */
1016 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
1020 /* Optimize the quantum of a scanline regarding its size - the quantum is
1021 a diviser of the number of bits between the start of two scanlines. */
1022 if( !(( p_vout->i_bytes_per_line ) % 32) )
1028 if( !(( p_vout->i_bytes_per_line ) % 16) )
1039 *pp_ximage = XCreateImage( p_vout->p_sys->p_display,
1040 p_vout->p_sys->p_visual, p_vout->i_screen_depth,
1041 ZPixmap, 0, pb_data,
1042 p_vout->i_width, p_vout->i_height, i_quantum, 0);
1043 if(! *pp_ximage ) /* error */
1045 intf_ErrMsg( "vout error: XCreateImage() failed" );
1053 /*****************************************************************************
1054 * X11CreateShmImage: create an XImage using shared memory extension
1055 *****************************************************************************
1056 * Prepare an XImage for DisplayX11ShmImage function.
1057 * The order of the operations respects the recommandations of the mit-shm
1058 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1060 *****************************************************************************/
1061 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
1062 XShmSegmentInfo *p_shm_info)
1066 XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
1067 p_vout->i_screen_depth, ZPixmap, 0,
1068 p_shm_info, p_vout->i_width, p_vout->i_height );
1069 if(! *pp_ximage ) /* error */
1071 intf_ErrMsg( "vout error: XShmCreateImage() failed" );
1075 /* Allocate shared memory segment - 0777 set the access permission
1076 * rights (like umask), they are not yet supported by X servers */
1078 shmget( IPC_PRIVATE, (*pp_ximage)->bytes_per_line
1079 * (*pp_ximage)->height, IPC_CREAT | 0777);
1080 if( p_shm_info->shmid < 0) /* error */
1082 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
1084 XDestroyImage( *pp_ximage );
1088 /* Attach shared memory segment to process (read/write) */
1089 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
1090 if(! p_shm_info->shmaddr )
1092 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
1094 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
1095 XDestroyImage( *pp_ximage );
1099 /* Mark the shm segment to be removed when there will be no more
1100 * attachements, so it is automatic on process exit or after shmdt */
1101 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1103 /* Attach shared memory segment to X server (read only) */
1104 p_shm_info->readOnly = True;
1105 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info )
1106 == False ) /* error */
1108 intf_ErrMsg( "vout error: cannot attach shared memory to X11 server" );
1109 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
1110 * and automatic free */
1111 XDestroyImage( *pp_ximage );
1115 /* Send image to X server. This instruction is required, since having
1116 * built a Shm XImage and not using it causes an error on XCloseDisplay */
1117 XFlush( p_vout->p_sys->p_display );
1121 /*****************************************************************************
1122 * X11DestroyImage: destroy an XImage
1123 *****************************************************************************
1124 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
1125 * destroyed (see vout_ManageOutputMethod())
1126 *****************************************************************************/
1127 static void X11DestroyImage( XImage *p_ximage )
1129 if( p_ximage != NULL )
1131 XDestroyImage( p_ximage ); /* no free() required */
1135 /*****************************************************************************
1136 * X11DestroyShmImage
1137 *****************************************************************************
1138 * Destroy XImage AND associated data. Detach shared memory segment from
1139 * server and process, then free it. If pointer is NULL, the image won't be
1140 * destroyed (see vout_ManageOutputMethod())
1141 *****************************************************************************/
1142 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
1143 XShmSegmentInfo *p_shm_info )
1145 /* If pointer is NULL, do nothing */
1146 if( p_ximage == NULL )
1151 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
1152 XDestroyImage( p_ximage );
1154 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
1155 { /* also automatic freeing... */
1156 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1162 /* WAZAAAAAAAAAAA */
1164 /*****************************************************************************
1165 * X11EnableScreenSaver: enable screen saver
1166 *****************************************************************************
1167 * This function enable the screen saver on a display after it had been
1168 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1169 * know wether the screen saver can be activated or not: if n successive calls
1170 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1171 * will be required before the screen saver could effectively be activated.
1172 *****************************************************************************/
1173 void X11EnableScreenSaver( vout_thread_t *p_vout )
1175 intf_DbgMsg( "vout: enabling screen saver" );
1176 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1177 p_vout->p_sys->i_ss_interval,
1178 p_vout->p_sys->i_ss_blanking,
1179 p_vout->p_sys->i_ss_exposure );
1182 /*****************************************************************************
1183 * X11DisableScreenSaver: disable screen saver
1184 *****************************************************************************
1185 * See XEnableScreenSaver
1186 *****************************************************************************/
1187 void X11DisableScreenSaver( vout_thread_t *p_vout )
1189 /* Save screen saver informations */
1190 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1191 &p_vout->p_sys->i_ss_interval,
1192 &p_vout->p_sys->i_ss_blanking,
1193 &p_vout->p_sys->i_ss_exposure );
1195 /* Disable screen saver */
1196 intf_DbgMsg( "vout: disabling screen saver" );
1197 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1198 p_vout->p_sys->i_ss_interval,
1199 p_vout->p_sys->i_ss_blanking,
1200 p_vout->p_sys->i_ss_exposure );
1203 /*****************************************************************************
1204 * X11TogglePointer: hide or show the mouse pointer
1205 *****************************************************************************
1206 * This function hides the X pointer if it is visible by putting it at
1207 * coordinates (32,32) and setting the pointer sprite to a blank one. To
1208 * show it again, we disable the sprite and restore the original coordinates.
1209 *****************************************************************************/
1210 void X11TogglePointer( vout_thread_t *p_vout )
1212 static Cursor cursor;
1213 static boolean_t b_cursor = 0;
1215 if( p_vout->p_sys->b_mouse )
1217 p_vout->p_sys->b_mouse = 0;
1222 Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1223 DefaultRootWindow(p_vout->p_sys->p_display),
1226 XParseColor( p_vout->p_sys->p_display,
1227 XCreateColormap( p_vout->p_sys->p_display,
1229 p_vout->p_sys->p_display ),
1231 p_vout->p_sys->p_display,
1232 p_vout->p_sys->i_screen ),
1236 cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1237 blank, blank, &color, &color, 1, 1 );
1241 XDefineCursor( p_vout->p_sys->p_display,
1242 p_vout->p_sys->window, cursor );
1246 p_vout->p_sys->b_mouse = 1;
1248 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );