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.31 2001/09/26 12:32:25 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 */
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 /* Mouse pointer properties */
113 mtime_t i_lastmoved; /* Auto-hide cursor */
114 boolean_t b_mouse_pointer_visible;
115 Cursor blank_cursor; /* the hidden cursor */
116 Pixmap cursor_pixmap;
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 X11ToggleMousePointer ( 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 )
202 /* Allocate structure */
203 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
204 if( p_vout->p_sys == NULL )
206 intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
210 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
211 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
212 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
214 if( p_vout->p_sys->p_display == NULL ) /* error */
216 intf_ErrMsg( "vout error: cannot open display %s", psz_display );
217 free( p_vout->p_sys );
220 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
223 = main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT );
225 /* Spawn base window - this window will include the video output window,
226 * but also command buttons, subtitles and other indicators */
228 if( X11CreateWindow( p_vout ) )
230 intf_ErrMsg( "vout error: cannot create X11 window" );
231 XCloseDisplay( p_vout->p_sys->p_display );
232 free( p_vout->p_sys );
236 /* Open and initialize device. This function issues its own error messages.
237 * Since XLib is usually not thread-safe, we can't use the same display
238 * pointer than the interface or another thread. However, the root window
239 * id is still valid. */
240 if( X11InitDisplay( p_vout, psz_display ) )
242 intf_ErrMsg( "vout error: cannot initialize X11 display" );
243 XCloseDisplay( p_vout->p_sys->p_display );
244 free( p_vout->p_sys );
248 /* Create blank cursor (for mouse cursor autohiding) */
249 p_vout->p_sys->b_mouse_pointer_visible = 1;
250 p_vout->p_sys->cursor_pixmap = XCreatePixmap( p_vout->p_sys->p_display,
252 p_vout->p_sys->p_display),
255 XParseColor( p_vout->p_sys->p_display,
256 XCreateColormap( p_vout->p_sys->p_display,
258 p_vout->p_sys->p_display ),
260 p_vout->p_sys->p_display,
261 p_vout->p_sys->i_screen ),
263 "black", &cursor_color );
265 p_vout->p_sys->blank_cursor = XCreatePixmapCursor(
266 p_vout->p_sys->p_display,
267 p_vout->p_sys->cursor_pixmap,
268 p_vout->p_sys->cursor_pixmap,
270 &cursor_color, 1, 1 );
272 /* Disable screen saver and return */
273 X11DisableScreenSaver( p_vout );
278 /*****************************************************************************
279 * vout_Init: initialize X11 video thread output method
280 *****************************************************************************
281 * This function create the XImages needed by the output thread. It is called
282 * at the beginning of the thread, but also each time the window is resized.
283 *****************************************************************************/
284 static int vout_Init( vout_thread_t *p_vout )
289 /* FIXME : As of 2001-03-16, XFree4 for MacOS X does not support Xshm. */
290 p_vout->p_sys->b_shm = 0;
293 /* Create XImages using XShm extension - on failure, fall back to regular
294 * way (and destroy the first image if it was created successfully) */
295 if( p_vout->p_sys->b_shm )
297 /* Create first image */
298 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
299 &p_vout->p_sys->shm_info[0] );
300 if( !i_err ) /* first image has been created */
302 /* Create second image */
303 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
304 &p_vout->p_sys->shm_info[1] ) )
305 { /* error creating the second image */
306 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
307 &p_vout->p_sys->shm_info[0] );
311 if( i_err ) /* an error occured */
313 intf_Msg( "vout: XShm video extension unavailable" );
314 p_vout->p_sys->b_shm = 0;
318 /* Create XImages without XShm extension */
319 if( !p_vout->p_sys->b_shm )
321 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
323 intf_ErrMsg( "vout error: cannot create images" );
324 p_vout->p_sys->p_ximage[0] = NULL;
325 p_vout->p_sys->p_ximage[1] = NULL;
328 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
330 intf_ErrMsg( "vout error: cannot create images" );
331 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
332 p_vout->p_sys->p_ximage[0] = NULL;
333 p_vout->p_sys->p_ximage[1] = NULL;
338 /* Set bytes per line and initialize buffers */
339 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
340 p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
341 p_vout->p_sys->p_ximage[ 1 ]->data );
343 /* Set date for autohiding cursor */
344 p_vout->p_sys->i_lastmoved = mdate();
349 /*****************************************************************************
350 * vout_End: terminate X11 video thread output method
351 *****************************************************************************
352 * Destroy the X11 XImages created by vout_Init. It is called at the end of
353 * the thread, but also each time the window is resized.
354 *****************************************************************************/
355 static void vout_End( vout_thread_t *p_vout )
357 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
359 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
360 &p_vout->p_sys->shm_info[0] );
361 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
362 &p_vout->p_sys->shm_info[1] );
364 else /* ...or regular XImages */
366 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
367 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
371 /*****************************************************************************
372 * vout_Destroy: destroy X11 video thread output method
373 *****************************************************************************
374 * Terminate an output method created by vout_CreateOutputMethod
375 *****************************************************************************/
376 static void vout_Destroy( vout_thread_t *p_vout )
378 /* Enable screen saver */
379 X11EnableScreenSaver( p_vout );
381 /* Restore cursor if it was blanked */
382 if( !p_vout->p_sys->b_mouse_pointer_visible )
383 X11ToggleMousePointer( p_vout );
385 /* Destroy blank cursor pixmap */
386 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
388 /* Destroy colormap */
389 if( p_vout->i_screen_depth == 8 )
391 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
395 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
396 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
397 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
399 XCloseDisplay( p_vout->p_sys->p_display );
401 /* Destroy structure */
402 free( p_vout->p_sys );
405 /*****************************************************************************
406 * vout_Manage: handle X11 events
407 *****************************************************************************
408 * This function should be called regularly by video output thread. It manages
409 * X11 events and allows window resizing. It returns a non null value on
411 *****************************************************************************/
412 static int vout_Manage( vout_thread_t *p_vout )
414 XEvent xevent; /* X11 event */
415 boolean_t b_resized; /* window has been resized */
416 char i_key; /* ISO Latin-1 key */
419 /* Handle X11 events: ConfigureNotify events are parsed to know if the
420 * output window's size changed, MapNotify and UnmapNotify to know if the
421 * window is mapped (and if the display is useful), and ClientMessages
422 * to intercept window destruction requests */
425 while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
426 StructureNotifyMask | KeyPressMask |
427 ButtonPressMask | ButtonReleaseMask |
428 PointerMotionMask | Button1MotionMask , &xevent )
431 /* ConfigureNotify event: prepare */
432 if( (xevent.type == ConfigureNotify)
433 && ((xevent.xconfigure.width != p_vout->p_sys->i_width)
434 || (xevent.xconfigure.height != p_vout->p_sys->i_height)) )
436 /* Update dimensions */
438 p_vout->p_sys->i_width = xevent.xconfigure.width;
439 p_vout->p_sys->i_height = xevent.xconfigure.height;
441 /* MapNotify event: change window status and disable screen saver */
442 else if( xevent.type == MapNotify)
444 if( (p_vout != NULL) && !p_vout->b_active )
446 X11DisableScreenSaver( p_vout );
447 p_vout->b_active = 1;
450 /* UnmapNotify event: change window status and enable screen saver */
451 else if( xevent.type == UnmapNotify )
453 if( (p_vout != NULL) && p_vout->b_active )
455 X11EnableScreenSaver( p_vout );
456 p_vout->b_active = 0;
460 else if( xevent.type == KeyPress )
462 /* We may have keys like F1 trough F12, ESC ... */
463 x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
464 xevent.xkey.keycode, 0 );
465 switch( x_key_symbol )
468 p_main->p_intf->b_die = 1;
471 p_main->p_intf->b_menu_change = 1;
475 * The reason why I use this instead of XK_0 is that
476 * with XLookupString, we don't have to care about
479 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
481 /* FIXME: handle stuff here */
486 p_main->p_intf->b_die = 1;
490 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
493 network_ChannelJoin( 0 );
496 network_ChannelJoin( 1 );
499 network_ChannelJoin( 2 );
502 network_ChannelJoin( 3 );
505 network_ChannelJoin( 4 );
508 network_ChannelJoin( 5 );
511 network_ChannelJoin( 6 );
514 network_ChannelJoin( 7 );
517 network_ChannelJoin( 8 );
520 network_ChannelJoin( 9 );
523 if( intf_ProcessKey( p_main->p_intf,
526 intf_DbgMsg( "vout: unhandled key '%c' (%i)",
527 (char)i_key, i_key );
536 else if( xevent.type == ButtonPress )
538 switch( ((XButtonEvent *)&xevent)->button )
541 /* in this part we will eventually manage
542 * clicks for DVD navigation for instance */
547 else if( xevent.type == ButtonRelease )
549 switch( ((XButtonEvent *)&xevent)->button )
552 /* FIXME: need locking ! */
553 p_main->p_intf->b_menu_change = 1;
558 else if( xevent.type == MotionNotify )
560 p_vout->p_sys->i_lastmoved = mdate();
561 if( ! p_vout->p_sys->b_mouse_pointer_visible )
563 X11ToggleMousePointer( p_vout );
569 intf_WarnMsg( 3, "vout: unhandled event %d received", xevent.type );
573 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
574 * are handled - according to the man pages, the format is always 32
576 while( XCheckTypedEvent( p_vout->p_sys->p_display,
577 ClientMessage, &xevent ) )
579 if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
580 && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
582 p_main->p_intf->b_die = 1;
586 intf_DbgMsg( "vout: unhandled ClientMessage received" );
590 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
593 /* Open display, unsing 'vlc_display' or the DISPLAY
594 * environment variable */
595 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
597 intf_DbgMsg( "vout: changing full-screen status" );
599 p_vout->b_fullscreen = !p_vout->b_fullscreen;
600 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
602 /* Get rid of the old window */
603 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
604 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
606 /* And create a new one */
607 if( X11CreateWindow( p_vout ) )
609 intf_ErrMsg( "vout error: cannot create X11 window" );
610 XCloseDisplay( p_vout->p_sys->p_display );
612 free( p_vout->p_sys );
616 if( X11InitDisplay( p_vout, psz_display ) )
618 intf_ErrMsg( "vout error: cannot initialize X11 display" );
619 XCloseDisplay( p_vout->p_sys->p_display );
620 free( p_vout->p_sys );
623 /* We've changed the size, update it */
624 p_vout->i_changes |= VOUT_SIZE_CHANGE;
628 * Handle vout window resizing
632 /* If interface window has been resized, change vout size */
633 intf_DbgMsg( "vout: resizing output window" );
634 p_vout->i_width = p_vout->p_sys->i_width;
635 p_vout->i_height = p_vout->p_sys->i_height;
636 p_vout->i_changes |= VOUT_SIZE_CHANGE;
638 else if( (p_vout->i_width != p_vout->p_sys->i_width) ||
639 (p_vout->i_height != p_vout->p_sys->i_height) )
641 /* If video output size has changed, change interface window size */
642 intf_DbgMsg( "vout: resizing output window" );
643 p_vout->p_sys->i_width = p_vout->i_width;
644 p_vout->p_sys->i_height = p_vout->i_height;
645 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
646 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
649 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
651 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
652 && (p_vout->i_screen_depth == 8) )
654 /* FIXME: clear flags ?? */
660 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
662 intf_DbgMsg( "vout info: resizing window" );
663 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
666 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
667 p_vout->i_width, p_vout->i_height );
669 /* Destroy XImages to change their size */
672 /* Recreate XImages. If SysInit failed, the thread can't go on. */
673 if( vout_Init( p_vout ) )
675 intf_ErrMsg( "vout error: cannot resize display" );
679 /* Tell the video output thread that it will need to rebuild YUV
680 * tables. This is needed since conversion buffer size may have
682 p_vout->i_changes |= VOUT_YUV_CHANGE;
683 intf_Msg( "vout: video display resized (%dx%d)",
684 p_vout->i_width, p_vout->i_height);
687 /* Autohide Cursour */
688 if( mdate() - p_vout->p_sys->i_lastmoved > 2000000 )
690 /* Hide the mouse automatically */
691 if( p_vout->p_sys->b_mouse_pointer_visible )
693 X11ToggleMousePointer( p_vout );
701 /*****************************************************************************
702 * vout_Display: displays previously rendered output
703 *****************************************************************************
704 * This function send the currently rendered image to X11 server, wait until
705 * it is displayed and switch the two rendering buffer, preparing next frame.
706 *****************************************************************************/
707 static void vout_Display( vout_thread_t *p_vout )
709 if( p_vout->p_sys->b_shm) /* XShm is used */
711 /* Display rendered image using shared memory extension */
712 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
713 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
715 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
716 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, False);
718 /* Send the order to the X server */
719 XSync(p_vout->p_sys->p_display, False);
721 else /* regular X11 capabilities are used */
723 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
724 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
726 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
727 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
729 /* Send the order to the X server */
730 XSync(p_vout->p_sys->p_display, False);
734 /*****************************************************************************
735 * vout_SetPalette: sets an 8 bpp palette
736 *****************************************************************************
737 * This function sets the palette given as an argument. It does not return
738 * anything, but could later send information on which colors it was unable
740 *****************************************************************************/
741 static void vout_SetPalette( p_vout_thread_t p_vout,
742 u16 *red, u16 *green, u16 *blue, u16 *transp )
745 XColor p_colors[255];
747 intf_DbgMsg( "vout: Palette change called" );
749 /* allocate palette */
750 for( i = 0, j = 255; i < 255; i++, j-- )
752 /* kludge: colors are indexed reversely because color 255 seems
753 * to be reserved for black even if we try to set it to white */
754 p_colors[ i ].pixel = j;
755 p_colors[ i ].pad = 0;
756 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
757 p_colors[ i ].red = red[ j ];
758 p_colors[ i ].blue = blue[ j ];
759 p_colors[ i ].green = green[ j ];
762 XStoreColors( p_vout->p_sys->p_display,
763 p_vout->p_sys->colormap, p_colors, 256 );
766 /* following functions are local */
768 /*****************************************************************************
769 * X11CreateWindow: open and set-up X11 main window
770 *****************************************************************************/
771 static int X11CreateWindow( vout_thread_t *p_vout )
773 XSizeHints xsize_hints;
774 XSetWindowAttributes xwindow_attributes;
781 boolean_t b_configure_notify;
782 boolean_t b_map_notify;
784 /* If we're full screen, we're full screen! */
785 if( p_vout->b_fullscreen )
787 p_vout->p_sys->i_width = DisplayWidth( p_vout->p_sys->p_display,
788 p_vout->p_sys->i_screen );
789 p_vout->p_sys->i_height = DisplayHeight( p_vout->p_sys->p_display,
790 p_vout->p_sys->i_screen );
791 p_vout->i_width = p_vout->p_sys->i_width;
792 p_vout->i_height = p_vout->p_sys->i_height;
796 /* Set main window's size */
797 p_vout->p_sys->i_width = p_vout->i_width;
798 p_vout->p_sys->i_height = p_vout->i_height;
801 /* Prepare window manager hints and properties */
802 xsize_hints.base_width = p_vout->p_sys->i_width;
803 xsize_hints.base_height = p_vout->p_sys->i_height;
804 xsize_hints.flags = PSize;
805 p_vout->p_sys->wm_protocols = XInternAtom( p_vout->p_sys->p_display,
806 "WM_PROTOCOLS", True );
807 p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
808 "WM_DELETE_WINDOW", True );
810 /* Prepare window attributes */
811 xwindow_attributes.backing_store = Always; /* save the hidden part */
812 xwindow_attributes.background_pixel = BlackPixel( p_vout->p_sys->p_display,
813 p_vout->p_sys->i_screen );
814 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
817 /* Create the window and set hints - the window must receive ConfigureNotify
818 * events, and, until it is displayed, Expose and MapNotify events. */
820 p_vout->p_sys->window =
821 XCreateWindow( p_vout->p_sys->p_display,
822 DefaultRootWindow( p_vout->p_sys->p_display ),
824 p_vout->p_sys->i_width, p_vout->p_sys->i_height, 0,
826 CWBackingStore | CWBackPixel | CWEventMask,
827 &xwindow_attributes );
829 if ( p_vout->b_fullscreen )
831 prop = XInternAtom(p_vout->p_sys->p_display, "_MOTIF_WM_HINTS", False);
832 mwmhints.flags = MWM_HINTS_DECORATIONS;
833 mwmhints.decorations = 0;
834 XChangeProperty( p_vout->p_sys->p_display, p_vout->p_sys->window,
835 prop, prop, 32, PropModeReplace,
836 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
838 XSetTransientForHint( p_vout->p_sys->p_display,
839 p_vout->p_sys->window, None );
840 XRaiseWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
843 /* Set window manager hints and properties: size hints, command,
844 * window's name, and accepted protocols */
845 XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
847 XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
848 p_main->ppsz_argv, p_main->i_argc );
849 XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
850 VOUT_TITLE " (X11 output)" );
852 if( (p_vout->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
853 || (p_vout->p_sys->wm_delete_window == None)
854 || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
855 &p_vout->p_sys->wm_delete_window, 1 ) )
857 /* WM_DELETE_WINDOW is not supported by window manager */
858 intf_Msg( "vout error: missing or bad window manager" );
861 /* Creation of a graphic context that doesn't generate a GraphicsExpose
862 * event when using functions like XCopyArea */
863 xgcvalues.graphics_exposures = False;
864 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
865 p_vout->p_sys->window,
866 GCGraphicsExposures, &xgcvalues);
868 /* Send orders to server, and wait until window is displayed - three
869 * events must be received: a MapNotify event, an Expose event allowing
870 * drawing in the window, and a ConfigureNotify to get the window
871 * dimensions. Once those events have been received, only ConfigureNotify
872 * events need to be received. */
874 b_configure_notify = 0;
876 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
879 XNextEvent( p_vout->p_sys->p_display, &xevent);
880 if( (xevent.type == Expose)
881 && (xevent.xexpose.window == p_vout->p_sys->window) )
885 else if( (xevent.type == MapNotify)
886 && (xevent.xmap.window == p_vout->p_sys->window) )
890 else if( (xevent.type == ConfigureNotify)
891 && (xevent.xconfigure.window == p_vout->p_sys->window) )
893 b_configure_notify = 1;
894 p_vout->p_sys->i_width = xevent.xconfigure.width;
895 p_vout->p_sys->i_height = xevent.xconfigure.height;
897 } while( !( b_expose && b_configure_notify && b_map_notify ) );
899 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
900 StructureNotifyMask | KeyPressMask |
901 ButtonPressMask | ButtonReleaseMask |
904 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
906 /* Allocate a new palette */
907 p_vout->p_sys->colormap =
908 XCreateColormap( p_vout->p_sys->p_display,
909 DefaultRootWindow( p_vout->p_sys->p_display ),
910 DefaultVisual( p_vout->p_sys->p_display,
911 p_vout->p_sys->i_screen ),
914 xwindow_attributes.colormap = p_vout->p_sys->colormap;
915 XChangeWindowAttributes( p_vout->p_sys->p_display,
916 p_vout->p_sys->window,
917 CWColormap, &xwindow_attributes );
920 if( p_vout->b_fullscreen )
922 XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
923 RevertToNone, CurrentTime );
924 XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->window, 0, 0 );
927 /* At this stage, the window is open, displayed, and ready to
933 /*****************************************************************************
934 * X11InitDisplay: open and initialize X11 device
935 *****************************************************************************
936 * Create a window according to video output given size, and set other
937 * properties according to the display properties.
938 *****************************************************************************/
939 static int X11InitDisplay( vout_thread_t *p_vout, char *psz_display )
941 XPixmapFormatValues * p_formats; /* pixmap formats */
942 XVisualInfo * p_xvisual; /* visuals informations */
943 XVisualInfo xvisual_template; /* visual template */
944 int i_count; /* array size */
947 /* Initialize structure */
948 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
949 p_vout->p_sys->b_shm = ( XShmQueryExtension( p_vout->p_sys->p_display )
951 if( !p_vout->p_sys->b_shm )
953 intf_Msg( "vout: XShm video extension is not available" );
956 /* Get screen depth */
957 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
958 p_vout->p_sys->i_screen );
959 switch( p_vout->i_screen_depth )
963 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
965 xvisual_template.screen = p_vout->p_sys->i_screen;
966 xvisual_template.class = DirectColor;
967 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
968 VisualScreenMask | VisualClassMask,
969 &xvisual_template, &i_count );
970 if( p_xvisual == NULL )
972 intf_ErrMsg( "vout error: no PseudoColor visual available" );
975 p_vout->i_bytes_per_pixel = 1;
982 * Screen depth is higher than 8bpp. TrueColor visual is used.
984 xvisual_template.screen = p_vout->p_sys->i_screen;
985 xvisual_template.class = TrueColor;
986 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
987 VisualScreenMask | VisualClassMask,
988 &xvisual_template, &i_count );
989 if( p_xvisual == NULL )
991 intf_ErrMsg( "vout error: no TrueColor visual available" );
994 p_vout->i_red_mask = p_xvisual->red_mask;
995 p_vout->i_green_mask = p_xvisual->green_mask;
996 p_vout->i_blue_mask = p_xvisual->blue_mask;
998 /* There is no difference yet between 3 and 4 Bpp. The only way
999 * to find the actual number of bytes per pixel is to list supported
1000 * pixmap formats. */
1001 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1002 p_vout->i_bytes_per_pixel = 0;
1004 for( ; i_count-- ; p_formats++ )
1006 /* Under XFree4.0, the list contains pixmap formats available
1007 * through all video depths ; so we have to check against current
1009 if( p_formats->depth == p_vout->i_screen_depth )
1011 if( p_formats->bits_per_pixel / 8
1012 > p_vout->i_bytes_per_pixel )
1014 p_vout->i_bytes_per_pixel = p_formats->bits_per_pixel / 8;
1020 p_vout->p_sys->p_visual = p_xvisual->visual;
1026 /*****************************************************************************
1027 * X11CreateImage: create an XImage
1028 *****************************************************************************
1029 * Create a simple XImage used as a buffer.
1030 *****************************************************************************/
1031 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
1033 byte_t * pb_data; /* image data storage zone */
1034 int i_quantum; /* XImage quantum (see below) */
1036 /* Allocate memory for image */
1037 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
1038 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
1039 if( !pb_data ) /* error */
1041 intf_ErrMsg( "vout error: %s", strerror(ENOMEM));
1045 /* Optimize the quantum of a scanline regarding its size - the quantum is
1046 a diviser of the number of bits between the start of two scanlines. */
1047 if( !(( p_vout->i_bytes_per_line ) % 32) )
1053 if( !(( p_vout->i_bytes_per_line ) % 16) )
1064 *pp_ximage = XCreateImage( p_vout->p_sys->p_display,
1065 p_vout->p_sys->p_visual, p_vout->i_screen_depth,
1066 ZPixmap, 0, pb_data,
1067 p_vout->i_width, p_vout->i_height, i_quantum, 0);
1068 if(! *pp_ximage ) /* error */
1070 intf_ErrMsg( "vout error: XCreateImage() failed" );
1078 /*****************************************************************************
1079 * X11CreateShmImage: create an XImage using shared memory extension
1080 *****************************************************************************
1081 * Prepare an XImage for DisplayX11ShmImage function.
1082 * The order of the operations respects the recommandations of the mit-shm
1083 * document by J.Corbet and K.Packard. Most of the parameters were copied from
1085 *****************************************************************************/
1086 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
1087 XShmSegmentInfo *p_shm_info)
1091 XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
1092 p_vout->i_screen_depth, ZPixmap, 0,
1093 p_shm_info, p_vout->i_width, p_vout->i_height );
1094 if(! *pp_ximage ) /* error */
1096 intf_ErrMsg( "vout error: XShmCreateImage() failed" );
1100 /* Allocate shared memory segment - 0777 set the access permission
1101 * rights (like umask), they are not yet supported by X servers */
1103 shmget( IPC_PRIVATE, (*pp_ximage)->bytes_per_line
1104 * (*pp_ximage)->height, IPC_CREAT | 0777);
1105 if( p_shm_info->shmid < 0) /* error */
1107 intf_ErrMsg( "vout error: cannot allocate shared image data (%s)",
1109 XDestroyImage( *pp_ximage );
1113 /* Attach shared memory segment to process (read/write) */
1114 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
1115 if(! p_shm_info->shmaddr )
1117 intf_ErrMsg( "vout error: cannot attach shared memory (%s)",
1119 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
1120 XDestroyImage( *pp_ximage );
1125 /* Mark the shm segment to be removed when there will be no more
1126 * attachements, so it is automatic on process exit or after shmdt */
1127 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
1130 /* Attach shared memory segment to X server (read only) */
1131 p_shm_info->readOnly = True;
1132 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info )
1133 == False ) /* error */
1135 intf_ErrMsg( "vout error: cannot attach shared memory to X11 server" );
1136 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
1137 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
1138 * and automatic free */
1139 XDestroyImage( *pp_ximage );
1143 /* Send image to X server. This instruction is required, since having
1144 * built a Shm XImage and not using it causes an error on XCloseDisplay */
1145 XFlush( p_vout->p_sys->p_display );
1149 /*****************************************************************************
1150 * X11DestroyImage: destroy an XImage
1151 *****************************************************************************
1152 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
1153 * destroyed (see vout_ManageOutputMethod())
1154 *****************************************************************************/
1155 static void X11DestroyImage( XImage *p_ximage )
1157 if( p_ximage != NULL )
1159 XDestroyImage( p_ximage ); /* no free() required */
1163 /*****************************************************************************
1164 * X11DestroyShmImage
1165 *****************************************************************************
1166 * Destroy XImage AND associated data. Detach shared memory segment from
1167 * server and process, then free it. If pointer is NULL, the image won't be
1168 * destroyed (see vout_ManageOutputMethod())
1169 *****************************************************************************/
1170 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
1171 XShmSegmentInfo *p_shm_info )
1173 /* If pointer is NULL, do nothing */
1174 if( p_ximage == NULL )
1179 XShmDetach( p_vout->p_sys->p_display, p_shm_info );/* detach from server */
1180 XDestroyImage( p_ximage );
1182 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
1184 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
1186 intf_ErrMsg( "vout error: cannot detach shared memory (%s)",
1192 /* WAZAAAAAAAAAAA */
1194 /*****************************************************************************
1195 * X11EnableScreenSaver: enable screen saver
1196 *****************************************************************************
1197 * This function enable the screen saver on a display after it had been
1198 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1199 * know wether the screen saver can be activated or not: if n successive calls
1200 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1201 * will be required before the screen saver could effectively be activated.
1202 *****************************************************************************/
1203 void X11EnableScreenSaver( vout_thread_t *p_vout )
1205 intf_DbgMsg( "vout: enabling screen saver" );
1206 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1207 p_vout->p_sys->i_ss_interval,
1208 p_vout->p_sys->i_ss_blanking,
1209 p_vout->p_sys->i_ss_exposure );
1212 /*****************************************************************************
1213 * X11DisableScreenSaver: disable screen saver
1214 *****************************************************************************
1215 * See XEnableScreenSaver
1216 *****************************************************************************/
1217 void X11DisableScreenSaver( vout_thread_t *p_vout )
1219 /* Save screen saver informations */
1220 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1221 &p_vout->p_sys->i_ss_interval,
1222 &p_vout->p_sys->i_ss_blanking,
1223 &p_vout->p_sys->i_ss_exposure );
1225 /* Disable screen saver */
1226 intf_DbgMsg( "vout: disabling screen saver" );
1227 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1228 p_vout->p_sys->i_ss_interval,
1229 p_vout->p_sys->i_ss_blanking,
1230 p_vout->p_sys->i_ss_exposure );
1233 /*****************************************************************************
1234 * X11ToggleMousePointer: hide or show the mouse pointer
1235 *****************************************************************************
1236 * This function hides the X pointer if it is visible by putting it at
1237 * coordinates (32,32) and setting the pointer sprite to a blank one. To
1238 * show it again, we disable the sprite and restore the original coordinates.
1239 *****************************************************************************/
1240 void X11ToggleMousePointer( vout_thread_t *p_vout )
1242 if( p_vout->p_sys->b_mouse_pointer_visible )
1244 p_vout->p_sys->b_mouse_pointer_visible = 0;
1246 XDefineCursor( p_vout->p_sys->p_display,
1247 p_vout->p_sys->window, p_vout->p_sys->blank_cursor );
1251 p_vout->p_sys->b_mouse_pointer_visible = 1;
1253 XUndefineCursor( p_vout->p_sys->p_display, p_vout->p_sys->window );