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.25 2001/05/25 13:20:09 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>
63 #include "video_output.h"
65 #include "interface.h"
68 #include "netutils.h" /* network_ChannelJoin */
72 /*****************************************************************************
73 * vout_sys_t: video output X11 method descriptor
74 *****************************************************************************
75 * This structure is part of the video output thread descriptor.
76 * It describes the X11 specific properties of an output thread. X11 video
77 * output is performed through regular resizable windows. Windows can be
78 * dynamically resized to adapt to the size of the streams.
79 *****************************************************************************/
80 typedef struct vout_sys_s
83 boolean_t b_shm; /* shared memory extension flag */
85 /* Internal settings and properties */
86 Display * p_display; /* display pointer */
87 Visual * p_visual; /* visual pointer */
88 int i_screen; /* screen number */
89 Window window; /* root window */
90 GC gc; /* graphic context instance handler */
91 Colormap colormap; /* colormap used (8bpp only) */
93 /* Display buffers and shared memory information */
94 XImage * p_ximage[2]; /* XImage pointer */
95 XShmSegmentInfo shm_info[2]; /* shared memory zone information */
97 /* X11 generic properties */
99 Atom wm_delete_window;
101 int i_width; /* width of main window */
102 int i_height; /* height of main window */
104 /* Screen saver properties */
105 int i_ss_timeout; /* timeout */
106 int i_ss_interval; /* interval between changes */
107 int i_ss_blanking; /* blanking mode */
108 int i_ss_exposure; /* exposure mode */
110 /* Auto-hide cursor */
113 /* Mouse pointer properties */
114 boolean_t b_mouse; /* is the mouse pointer displayed ? */
118 /* Fullscreen needs to be able to hide the wm decorations */
119 #define MWM_HINTS_DECORATIONS (1L << 1)
120 #define PROP_MWM_HINTS_ELEMENTS 5
121 typedef struct mwmhints_s
130 /*****************************************************************************
132 *****************************************************************************/
133 static int vout_Probe ( probedata_t *p_data );
134 static int vout_Create ( struct vout_thread_s * );
135 static int vout_Init ( struct vout_thread_s * );
136 static void vout_End ( struct vout_thread_s * );
137 static void vout_Destroy ( struct vout_thread_s * );
138 static int vout_Manage ( struct vout_thread_s * );
139 static void vout_Display ( struct vout_thread_s * );
140 static void vout_SetPalette( struct vout_thread_s *, u16*, u16*, u16*, u16* );
142 static int X11CreateWindow ( vout_thread_t *p_vout );
143 static int X11InitDisplay ( vout_thread_t *p_vout, char *psz_display );
145 static int X11CreateImage ( vout_thread_t *p_vout, XImage **pp_ximage );
146 static void X11DestroyImage ( XImage *p_ximage );
147 static int X11CreateShmImage ( vout_thread_t *p_vout, XImage **pp_ximage,
148 XShmSegmentInfo *p_shm_info );
149 static void X11DestroyShmImage ( vout_thread_t *p_vout, XImage *p_ximage,
150 XShmSegmentInfo *p_shm_info );
152 static void X11TogglePointer ( vout_thread_t *p_vout );
153 static void X11EnableScreenSaver ( vout_thread_t *p_vout );
154 static void X11DisableScreenSaver ( vout_thread_t *p_vout );
156 /*****************************************************************************
157 * Functions exported as capabilities. They are declared as static so that
158 * we don't pollute the namespace too much.
159 *****************************************************************************/
160 void _M( vout_getfunctions )( function_list_t * p_function_list )
162 p_function_list->pf_probe = vout_Probe;
163 p_function_list->functions.vout.pf_create = vout_Create;
164 p_function_list->functions.vout.pf_init = vout_Init;
165 p_function_list->functions.vout.pf_end = vout_End;
166 p_function_list->functions.vout.pf_destroy = vout_Destroy;
167 p_function_list->functions.vout.pf_manage = vout_Manage;
168 p_function_list->functions.vout.pf_display = vout_Display;
169 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
172 /*****************************************************************************
173 * vout_Probe: probe the video driver and return a score
174 *****************************************************************************
175 * This function tries to initialize SDL and returns a score to the
176 * plugin manager so that it can select the best plugin.
177 *****************************************************************************/
178 static int vout_Probe( probedata_t *p_data )
180 if( TestMethod( VOUT_METHOD_VAR, "x11" ) )
188 /*****************************************************************************
189 * vout_Create: allocate X11 video thread output method
190 *****************************************************************************
191 * This function allocate and initialize a X11 vout method. It uses some of the
192 * vout properties to choose the window size, and change them according to the
193 * actual properties of the display.
194 *****************************************************************************/
195 static int vout_Create( vout_thread_t *p_vout )
199 /* Allocate structure */
200 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
201 if( p_vout->p_sys == NULL )
203 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
207 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
208 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
209 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
211 if( p_vout->p_sys->p_display == NULL ) /* error */
213 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
214 free( p_vout->p_sys );
217 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
220 = main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT );
222 /* Spawn base window - this window will include the video output window,
223 * but also command buttons, subtitles and other indicators */
225 if( X11CreateWindow( p_vout ) )
227 intf_ErrMsg( "vout error: cannot create X11 window" );
228 XCloseDisplay( p_vout->p_sys->p_display );
229 free( p_vout->p_sys );
233 /* Open and initialize device. This function issues its own error messages.
234 * Since XLib is usually not thread-safe, we can't use the same display
235 * pointer than the interface or another thread. However, the root window
236 * id is still valid. */
237 if( X11InitDisplay( p_vout, psz_display ) )
239 intf_ErrMsg( "vout error: cannot initialize X11 display" );
240 XCloseDisplay( p_vout->p_sys->p_display );
241 free( p_vout->p_sys );
245 p_vout->p_sys->b_mouse = 1;
247 /* Disable screen saver and return */
248 X11DisableScreenSaver( p_vout );
253 /*****************************************************************************
254 * vout_Init: initialize X11 video thread output method
255 *****************************************************************************
256 * This function create the XImages needed by the output thread. It is called
257 * at the beginning of the thread, but also each time the window is resized.
258 *****************************************************************************/
259 static int vout_Init( vout_thread_t *p_vout )
264 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
265 p_vout->p_sys->b_shm = 0;
268 /* Create XImages using XShm extension - on failure, fall back to regular
269 * way (and destroy the first image if it was created successfully) */
270 if( p_vout->p_sys->b_shm )
272 /* Create first image */
273 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
274 &p_vout->p_sys->shm_info[0] );
275 if( !i_err ) /* first image has been created */
277 /* Create second image */
278 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
279 &p_vout->p_sys->shm_info[1] ) )
280 { /* error creating the second image */
281 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
282 &p_vout->p_sys->shm_info[0] );
286 if( i_err ) /* an error occured */
288 intf_Msg( "vout: XShm video extension unavailable" );
289 p_vout->p_sys->b_shm = 0;
293 /* Create XImages without XShm extension */
294 if( !p_vout->p_sys->b_shm )
296 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
298 intf_ErrMsg( "vout error: cannot create images" );
299 p_vout->p_sys->p_ximage[0] = NULL;
300 p_vout->p_sys->p_ximage[1] = NULL;
303 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
305 intf_ErrMsg( "vout error: cannot create images" );
306 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
307 p_vout->p_sys->p_ximage[0] = NULL;
308 p_vout->p_sys->p_ximage[1] = NULL;
313 /* Set bytes per line and initialize buffers */
314 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
315 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
316 p_vout->p_sys->p_ximage[ 1 ]->data );
318 /* Set date for autohiding cursor */
319 p_vout->p_sys->i_lastmoved = mdate();
324 /*****************************************************************************
325 * vout_End: terminate X11 video thread output method
326 *****************************************************************************
327 * Destroy the X11 XImages created by vout_Init. It is called at the end of
328 * the thread, but also each time the window is resized.
329 *****************************************************************************/
330 static void vout_End( vout_thread_t *p_vout )
332 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
334 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
335 &p_vout->p_sys->shm_info[0] );
336 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
337 &p_vout->p_sys->shm_info[1] );
339 else /* ...or regular XImages */
341 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
342 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
346 /*****************************************************************************
347 * vout_Destroy: destroy X11 video thread output method
348 *****************************************************************************
349 * Terminate an output method created by vout_CreateOutputMethod
350 *****************************************************************************/
351 static void vout_Destroy( vout_thread_t *p_vout )
353 /* Enable screen saver */
354 X11EnableScreenSaver( p_vout );
356 /* Destroy colormap */
357 if( p_vout->i_screen_depth == 8 )
359 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
363 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
364 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
365 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
367 XCloseDisplay( p_vout->p_sys->p_display );
369 /* Destroy structure */
370 free( p_vout->p_sys );
373 /*****************************************************************************
374 * vout_Manage: handle X11 events
375 *****************************************************************************
376 * This function should be called regularly by video output thread. It manages
377 * X11 events and allows window resizing. It returns a non null value on
379 *****************************************************************************/
380 static int vout_Manage( vout_thread_t *p_vout )
382 XEvent xevent; /* X11 event */
383 boolean_t b_resized; /* window has been resized */
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 */
393 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
394 StructureNotifyMask | KeyPressMask |
395 ButtonPressMask | ButtonReleaseMask |
396 PointerMotionMask | Button1MotionMask , &xevent )
399 /* ConfigureNotify event: prepare */
400 if( (xevent.type == ConfigureNotify)
401 && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
402 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
404 /* Update dimensions */
406 p_vout->p_sys->i_width = xevent.xconfigure.width;
407 p_vout->p_sys->i_height = xevent.xconfigure.height;
409 /* MapNotify event: change window status and disable screen saver */
410 else if( xevent.type == MapNotify)
412 if( (p_vout != NULL) && !p_vout->b_active )
414 X11DisableScreenSaver( p_vout );
415 p_vout->b_active = 1;
418 /* UnmapNotify event: change window status and enable screen saver */
419 else if( xevent.type == UnmapNotify )
421 if( (p_vout != NULL) && p_vout->b_active )
423 X11EnableScreenSaver( p_vout );
424 p_vout->b_active = 0;
428 else if( xevent.type == KeyPress )
430 /* We may have keys like F1 trough F12, ESC ... */
431 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
432 xevent.xkey.keycode, 0 );
433 switch( x_key_symbol )
436 p_main->p_intf->b_die = 1;
439 p_main->p_intf->b_menu_change = 1;
443 * The reason why I use this instead of XK_0 is that
444 * with XLookupString, we don't have to care about
447 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
449 /* FIXME: handle stuff here */
454 p_main->p_intf->b_die = 1;
458 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
461 network_ChannelJoin( 0 );
464 network_ChannelJoin( 1 );
467 network_ChannelJoin( 2 );
470 network_ChannelJoin( 3 );
473 network_ChannelJoin( 4 );
476 network_ChannelJoin( 5 );
479 network_ChannelJoin( 6 );
482 network_ChannelJoin( 7 );
485 network_ChannelJoin( 8 );
488 network_ChannelJoin( 9 );
491 if( intf_ProcessKey( p_main->p_intf,
494 intf_DbgMsg( "vout: unhandled key '%c' (%i)",
495 (char)i_key, i_key );
504 else if( xevent.type == ButtonPress )
506 switch( ((XButtonEvent *)&xevent)->button )
509 /* in this part we will eventually manage
510 * clicks for DVD navigation for instance */
515 else if( xevent.type == ButtonRelease )
517 switch( ((XButtonEvent *)&xevent)->button )
520 /* FIXME: need locking ! */
521 p_main->p_intf->b_menu_change = 1;
526 else if( xevent.type == MotionNotify )
528 p_vout->p_sys->i_lastmoved = mdate();
529 if( ! p_vout->p_sys->b_mouse )
531 X11TogglePointer( p_vout );
537 intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
541 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
542 * are handled - according to the man pages, the format is always 32
544 while( XCheckTypedEvent( p_vout->p_sys->p_display,
545 ClientMessage, &xevent ) )
547 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
548 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
550 p_main->p_intf->b_die = 1;
554 intf_DbgMsg( "vout: unhandled ClientMessage received" );
558 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
561 /* Open display, unsing 'vlc_display' or the DISPLAY
562 * environment variable */
563 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
565 intf_DbgMsg( "vout: changing full-screen status" );
567 p_vout->b_fullscreen = !p_vout->b_fullscreen;
568 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
570 /* Get rid of the old window */
571 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
572 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
574 /* And create a new one */
575 if( X11CreateWindow( p_vout ) )
577 intf_ErrMsg( "vout error: cannot create X11 window" );
578 XCloseDisplay( p_vout->p_sys->p_display );
580 free( p_vout->p_sys );
584 if( X11InitDisplay( p_vout, psz_display ) )
586 intf_ErrMsg( "vout error: cannot initialize X11 display" );
587 XCloseDisplay( p_vout->p_sys->p_display );
588 free( p_vout->p_sys );
591 /* We've changed the size, update it */
592 p_vout->i_changes |= VOUT_SIZE_CHANGE;
596 * Handle vout window resizing
600 /* If interface window has been resized, change vout size */
601 intf_DbgMsg( "vout: resizing output window" );
602 p_vout->i_width = p_vout->p_sys->i_width;
603 p_vout->i_height = p_vout->p_sys->i_height;
604 p_vout->i_changes |= VOUT_SIZE_CHANGE;
606 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
607 (p_vout->i_height != p_vout->p_sys->i_height) )
609 /* If video output size has changed, change interface window size */
610 intf_DbgMsg( "vout: resizing output window" );
611 p_vout->p_sys->i_width = p_vout->i_width;
612 p_vout->p_sys->i_height = p_vout->i_height;
613 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
614 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
617 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
619 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
620 && (p_vout->i_screen_depth == 8) )
622 /* FIXME: clear flags ?? */
628 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
630 intf_DbgMsg( "vout info: resizing window" );
631 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
634 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
635 p_vout->i_width, p_vout->i_height );
637 /* Destroy XImages to change their size */
640 /* Recreate XImages. If SysInit failed, the thread can't go on. */
641 if( vout_Init( p_vout ) )
643 intf_ErrMsg( "vout error: cannot resize display" );
647 /* Tell the video output thread that it will need to rebuild YUV
648 * tables. This is needed since conversion buffer size may have
650 p_vout->i_changes |= VOUT_YUV_CHANGE;
651 intf_Msg( "vout: video display resized (%dx%d)",
652 p_vout->i_width, p_vout->i_height);
655 /* Autohide Cursour */
656 if( mdate() - p_vout->p_sys->i_lastmoved > 2000000 )
658 /* Hide the mouse automatically */
659 if( p_vout->p_sys->b_mouse )
661 X11TogglePointer( p_vout );
669 /*****************************************************************************
670 * vout_Display: displays previously rendered output
671 *****************************************************************************
672 * This function send the currently rendered image to X11 server, wait until
673 * it is displayed and switch the two rendering buffer, preparing next frame.
674 *****************************************************************************/
675 static void vout_Display( vout_thread_t *p_vout )
677 if( p_vout->p_sys->b_shm) /* XShm is used */
679 /* Display rendered image using shared memory extension */
680 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
681 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
683 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
684 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
686 /* Send the order to the X server */
687 XSync(p_vout->p_sys->p_display, False);
689 else /* regular X11 capabilities are used */
691 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
692 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
694 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
695 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
697 /* Send the order to the X server */
698 XSync(p_vout->p_sys->p_display, False);
702 /*****************************************************************************
703 * vout_SetPalette: sets an 8 bpp palette
704 *****************************************************************************
705 * This function sets the palette given as an argument. It does not return
706 * anything, but could later send information on which colors it was unable
708 *****************************************************************************/
709 static void vout_SetPalette( p_vout_thread_t p_vout,
710 u16 *red, u16 *green, u16 *blue, u16 *transp )
713 XColor p_colors[255];
715 intf_DbgMsg( "vout: Palette change called" );
717 /* allocate palette */
718 for( i = 0, j = 255; i < 255; i++, j-- )
720 /* kludge: colors are indexed reversely because color 255 seems
721 * to be reserved for black even if we try to set it to white */
722 p_colors[ i ].pixel = j;
723 p_colors[ i ].pad = 0;
724 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
725 p_colors[ i ].red = red[ j ];
726 p_colors[ i ].blue = blue[ j ];
727 p_colors[ i ].green = green[ j ];
730 XStoreColors( p_vout->p_sys->p_display,
731 p_vout->p_sys->colormap, p_colors, 256 );
734 /* following functions are local */
736 /*****************************************************************************
737 * X11CreateWindow: open and set-up X11 main window
738 *****************************************************************************/
739 static int X11CreateWindow( vout_thread_t *p_vout )
741 XSizeHints xsize_hints;
742 XSetWindowAttributes xwindow_attributes;
749 boolean_t b_configure_notify;
750 boolean_t b_map_notify;
752 /* If we're full screen, we're full screen! */
753 if( p_vout->b_fullscreen )
755 p_vout->p_sys->i_width = DisplayWidth( p_vout->p_sys->p_display,
756 p_vout->p_sys->i_screen );
757 p_vout->p_sys->i_height = DisplayHeight( p_vout->p_sys->p_display,
758 p_vout->p_sys->i_screen );
759 p_vout->i_width = p_vout->p_sys->i_width;
760 p_vout->i_height = p_vout->p_sys->i_height;
764 /* Set main window's size */
765 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
766 VOUT_WIDTH_DEFAULT );
767 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
768 VOUT_HEIGHT_DEFAULT );
769 p_vout->i_width = p_vout->p_sys->i_width;
770 p_vout->i_height = p_vout->p_sys->i_height;
773 /* Prepare window manager hints and properties */
774 xsize_hints.base_width = p_vout->p_sys->i_width;
775 xsize_hints.base_height = p_vout->p_sys->i_height;
776 xsize_hints.flags = PSize;
777 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
778 "WM_PROTOCOLS", True );
779 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
780 "WM_DELETE_WINDOW", True );
782 /* Prepare window attributes */
783 xwindow_attributes.backing_store = Always; /* save the hidden part */
784 xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
785 p_vout->p_sys->i_screen );
786 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
789 /* Create the window and set hints - the window must receive ConfigureNotify
790 * events, and, until it is displayed, Expose and MapNotify events. */
792 p_vout->p_sys->window =
793 XCreateWindow( p_vout->p_sys->p_display,
794 DefaultRootWindow( p_vout->p_sys->p_display ),
796 p_vout->p_sys->i_width, p_vout->p_sys->i_height, 0,
798 CWBackingStore | CWBackPixel | CWEventMask,
799 &xwindow_attributes );
801 if ( p_vout->b_fullscreen )
803 prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
804 mwmhints.flags = MWM_HINTS_DECORATIONS;
805 mwmhints.decorations = 0;
806 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
807 prop, prop, 32, PropModeReplace,
808 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
810 XSetTransientForHint( p_vout->p_sys->p_display,
811 p_vout->p_sys->window, None );
812 XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
815 /* Set window manager hints and properties: size hints, command,
816 * window's name, and accepted protocols */
817 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
819 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
820 p_main->ppsz_argv, p_main->i_argc );
821 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
822 VOUT_TITLE " (X11 output)" );
824 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
825 || (p_vout->p_sys->wm_delete_window == None)
826 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
827 &p_vout->p_sys->wm_delete_window, 1 ) )
829 /* WM_DELETE_WINDOW is not supported by window manager */
830 intf_Msg( "vout error: missing or bad window manager" );
833 /* Creation of a graphic context that doesn't generate a GraphicsExpose
834 * event when using functions like XCopyArea */
835 xgcvalues.graphics_exposures = False;
836 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
837 p_vout->p_sys->window,
838 GCGraphicsExposures, &xgcvalues);
840 /* Send orders to server, and wait until window is displayed - three
841 * events must be received: a MapNotify event, an Expose event allowing
842 * drawing in the window, and a ConfigureNotify to get the window
843 * dimensions. Once those events have been received, only ConfigureNotify
844 * events need to be received. */
846 b_configure_notify = 0;
848 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
851 XNextEvent( p_vout->p_sys->p_display, &xevent);
852 if( (xevent.type == Expose)
853 && (xevent.xexpose.window == p_vout->p_sys->window) )
857 else if( (xevent.type == MapNotify)
858 && (xevent.xmap.window == p_vout->p_sys->window) )
862 else if( (xevent.type == ConfigureNotify)
863 && (xevent.xconfigure.window == p_vout->p_sys->window) )
865 b_configure_notify = 1;
866 p_vout->p_sys->i_width = xevent.xconfigure.width;
867 p_vout->p_sys->i_height = xevent.xconfigure.height;
869 } while( !( b_expose && b_configure_notify && b_map_notify ) );
871 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
872 StructureNotifyMask | KeyPressMask |
873 ButtonPressMask | ButtonReleaseMask |
876 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
878 /* Allocate a new palette */
879 p_vout->p_sys->colormap =
880 XCreateColormap( p_vout->p_sys->p_display,
881 DefaultRootWindow( p_vout->p_sys->p_display ),
882 DefaultVisual( p_vout->p_sys->p_display,
883 p_vout->p_sys->i_screen ),
886 xwindow_attributes.colormap = p_vout->p_sys->colormap;
887 XChangeWindowAttributes( p_vout->p_sys->p_display,
888 p_vout->p_sys->window,
889 CWColormap, &xwindow_attributes );
892 if( p_vout->b_fullscreen )
894 XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
895 RevertToNone, CurrentTime );
896 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
899 /* At this stage, the window is open, displayed, and ready to
905 /*****************************************************************************
906 * X11InitDisplay: open and initialize X11 device
907 *****************************************************************************
908 * Create a window according to video output given size, and set other
909 * properties according to the display properties.
910 *****************************************************************************/
911 static int X11InitDisplay( vout_thread_t *p_vout, char *psz_display )
913 XPixmapFormatValues * p_formats; /* pixmap formats */
914 XVisualInfo * p_xvisual; /* visuals informations */
915 XVisualInfo xvisual_template; /* visual template */
916 int i_count; /* array size */
920 /* Initialize structure */
921 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
922 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
924 if( !p_vout->p_sys->b_shm )
926 intf_Msg( "vout: XShm video extension is not available" );
929 /* Get screen depth */
930 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
931 p_vout->p_sys->i_screen );
932 switch( p_vout->i_screen_depth )
936 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
938 xvisual_template.screen = p_vout->p_sys->i_screen;
939 xvisual_template.class = DirectColor;
940 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
941 VisualScreenMask | VisualClassMask,
942 &xvisual_template, &i_count );
943 if( p_xvisual == NULL )
945 intf_ErrMsg( "vout error: no PseudoColor visual available" );
948 p_vout->i_bytes_per_pixel = 1;
955 * Screen depth is higher than 8bpp. TrueColor visual is used.
957 xvisual_template.screen = p_vout->p_sys->i_screen;
958 xvisual_template.class = TrueColor;
959 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
960 VisualScreenMask | VisualClassMask,
961 &xvisual_template, &i_count );
962 if( p_xvisual == NULL )
964 intf_ErrMsg( "vout error: no TrueColor visual available" );
967 p_vout->i_red_mask = p_xvisual->red_mask;
968 p_vout->i_green_mask = p_xvisual->green_mask;
969 p_vout->i_blue_mask = p_xvisual->blue_mask;
971 /* There is no difference yet between 3 and 4 Bpp. The only way
972 * to find the actual number of bytes per pixel is to list supported
974 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
975 p_vout->i_bytes_per_pixel = 0;
977 for( ; i_count-- ; p_formats++ )
979 /* Under XFree4.0, the list contains pixmap formats available
980 * through all video depths ; so we have to check against current
982 if( p_formats->depth == p_vout->i_screen_depth )
984 if( p_formats->bits_per_pixel / 8
985 > p_vout->i_bytes_per_pixel )
987 p_vout->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
993 p_vout->p_sys->p_visual = p_xvisual->visual;
999 /*****************************************************************************
1000 * X11CreateImage: create an XImage
1001 *****************************************************************************
1002 * Create a simple XImage used as a buffer.
1003 *****************************************************************************/
1004 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
1006 byte_t * pb_data; /* image data storage zone */
1007 int i_quantum; /* XImage quantum (see below) */
1009 /* Allocate memory for image */
1010 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
1011 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
1012 if( !pb_data ) /* error */
1014 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
1018 /* Optimize the quantum of a scanline regarding its size - the quantum is
1019 a diviser of the number of bits between the start of two scanlines. */
1020 if( !(( p_vout->i_bytes_per_line ) % 32) )
1026 if( !(( p_vout->i_bytes_per_line ) % 16) )
1037 *pp_ximage = XCreateImage( p_vout->p_sys->p_display,
1038 p_vout->p_sys->p_visual, p_vout->i_screen_depth,
1039 ZPixmap, 0, pb_data,
1040 p_vout->i_width, p_vout->i_height, i_quantum, 0);
1041 if(! *pp_ximage ) /* error */
1043 intf_ErrMsg( "vout error: XCreateImage() failed" );
1051 /*****************************************************************************
1052 * X11CreateShmImage: create an XImage using shared memory extension
1053 *****************************************************************************
1054 * Prepare an XImage for DisplayX11ShmImage function.
1055 * The order of the operations respects the recommandations of the mit-shm
1056 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1058 *****************************************************************************/
1059 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
1060 XShmSegmentInfo *p_shm_info)
1064 XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
1065 p_vout->i_screen_depth, ZPixmap, 0,
1066 p_shm_info, p_vout->i_width, p_vout->i_height );
1067 if(! *pp_ximage ) /* error */
1069 intf_ErrMsg( "vout error: XShmCreateImage() failed" );
1073 /* Allocate shared memory segment - 0777 set the access permission
1074 * rights (like umask), they are not yet supported by X servers */
1076 shmget( IPC_PRIVATE, (*pp_ximage)->bytes_per_line
1077 * (*pp_ximage)->height, IPC_CREAT | 0777);
1078 if( p_shm_info->shmid < 0) /* error */
1080 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
1082 XDestroyImage( *pp_ximage );
1086 /* Attach shared memory segment to process (read/write) */
1087 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
1088 if(! p_shm_info->shmaddr )
1090 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
1092 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
1093 XDestroyImage( *pp_ximage );
1097 /* Mark the shm segment to be removed when there will be no more
1098 * attachements, so it is automatic on process exit or after shmdt */
1099 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1101 /* Attach shared memory segment to X server (read only) */
1102 p_shm_info->readOnly = True;
1103 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info )
1104 == False ) /* error */
1106 intf_ErrMsg( "vout error: cannot attach shared memory to X11 server" );
1107 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
1108 * and automatic free */
1109 XDestroyImage( *pp_ximage );
1113 /* Send image to X server. This instruction is required, since having
1114 * built a Shm XImage and not using it causes an error on XCloseDisplay */
1115 XFlush( p_vout->p_sys->p_display );
1119 /*****************************************************************************
1120 * X11DestroyImage: destroy an XImage
1121 *****************************************************************************
1122 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
1123 * destroyed (see vout_ManageOutputMethod())
1124 *****************************************************************************/
1125 static void X11DestroyImage( XImage *p_ximage )
1127 if( p_ximage != NULL )
1129 XDestroyImage( p_ximage ); /* no free() required */
1133 /*****************************************************************************
1134 * X11DestroyShmImage
1135 *****************************************************************************
1136 * Destroy XImage AND associated data. Detach shared memory segment from
1137 * server and process, then free it. If pointer is NULL, the image won't be
1138 * destroyed (see vout_ManageOutputMethod())
1139 *****************************************************************************/
1140 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
1141 XShmSegmentInfo *p_shm_info )
1143 /* If pointer is NULL, do nothing */
1144 if( p_ximage == NULL )
1149 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
1150 XDestroyImage( p_ximage );
1152 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
1153 { /* also automatic freeing... */
1154 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1160 /* WAZAAAAAAAAAAA */
1162 /*****************************************************************************
1163 * X11EnableScreenSaver: enable screen saver
1164 *****************************************************************************
1165 * This function enable the screen saver on a display after it had been
1166 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1167 * know wether the screen saver can be activated or not: if n successive calls
1168 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1169 * will be required before the screen saver could effectively be activated.
1170 *****************************************************************************/
1171 void X11EnableScreenSaver( vout_thread_t *p_vout )
1173 intf_DbgMsg( "vout: enabling screen saver" );
1174 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1175 p_vout->p_sys->i_ss_interval,
1176 p_vout->p_sys->i_ss_blanking,
1177 p_vout->p_sys->i_ss_exposure );
1180 /*****************************************************************************
1181 * X11DisableScreenSaver: disable screen saver
1182 *****************************************************************************
1183 * See XEnableScreenSaver
1184 *****************************************************************************/
1185 void X11DisableScreenSaver( vout_thread_t *p_vout )
1187 /* Save screen saver informations */
1188 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1189 &p_vout->p_sys->i_ss_interval,
1190 &p_vout->p_sys->i_ss_blanking,
1191 &p_vout->p_sys->i_ss_exposure );
1193 /* Disable screen saver */
1194 intf_DbgMsg( "vout: disabling screen saver" );
1195 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1196 p_vout->p_sys->i_ss_interval,
1197 p_vout->p_sys->i_ss_blanking,
1198 p_vout->p_sys->i_ss_exposure );
1201 /*****************************************************************************
1202 * X11TogglePointer: hide or show the mouse pointer
1203 *****************************************************************************
1204 * This function hides the X pointer if it is visible by putting it at
1205 * coordinates (32,32) and setting the pointer sprite to a blank one. To
1206 * show it again, we disable the sprite and restore the original coordinates.
1207 *****************************************************************************/
1208 void X11TogglePointer( vout_thread_t *p_vout )
1210 static Cursor cursor;
1211 static boolean_t b_cursor = 0;
1213 if( p_vout->p_sys->b_mouse )
1215 p_vout->p_sys->b_mouse = 0;
1220 Pixmap blank = XCreatePixmap( p_vout->p_sys->p_display,
1221 DefaultRootWindow(p_vout->p_sys->p_display),
1224 XParseColor( p_vout->p_sys->p_display,
1225 XCreateColormap( p_vout->p_sys->p_display,
1227 p_vout->p_sys->p_display ),
1229 p_vout->p_sys->p_display,
1230 p_vout->p_sys->i_screen ),
1234 cursor = XCreatePixmapCursor( p_vout->p_sys->p_display,
1235 blank, blank, &color, &color, 1, 1 );
1239 XDefineCursor( p_vout->p_sys->p_display,
1240 p_vout->p_sys->window, cursor );
1244 p_vout->p_sys->b_mouse = 1;
1246 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );