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.7 2001/01/05 18:46:44 massiot Exp $
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
33 #ifdef HAVE_MACHINE_PARAM_H
35 #include <machine/param.h>
36 #include <sys/types.h> /* typedef ushort */
40 #include <sys/shm.h> /* shmget(), shmctl() */
42 #include <X11/Xutil.h>
43 #include <X11/extensions/XShm.h>
52 #include "video_output.h"
56 /*****************************************************************************
57 * vout_sys_t: video output X11 method descriptor
58 *****************************************************************************
59 * This structure is part of the video output thread descriptor.
60 * It describes the X11 specific properties of an output thread. X11 video
61 * output is performed through regular resizable windows. Windows can be
62 * dynamically resized to adapt to the size of the streams.
63 *****************************************************************************/
64 typedef struct vout_sys_s
67 boolean_t b_shm; /* shared memory extension flag */
69 /* Internal settings and properties */
70 Display * p_display; /* display pointer */
71 Visual * p_visual; /* visual pointer */
72 int i_screen; /* screen number */
73 Window root_window; /* root window */
74 Window window; /* window instance handler */
75 GC gc; /* graphic context instance handler */
76 Colormap colormap; /* colormap used (8bpp only) */
78 /* Display buffers and shared memory information */
79 XImage * p_ximage[2]; /* XImage pointer */
80 XShmSegmentInfo shm_info[2]; /* shared memory zone information */
83 /*****************************************************************************
85 *****************************************************************************/
86 static int X11OpenDisplay ( vout_thread_t *p_vout, char *psz_display, Window root_window, void *p_data );
87 static void X11CloseDisplay ( vout_thread_t *p_vout );
88 static int X11CreateWindow ( vout_thread_t *p_vout );
89 static void X11DestroyWindow ( vout_thread_t *p_vout );
90 static int X11CreateImage ( vout_thread_t *p_vout, XImage **pp_ximage );
91 static void X11DestroyImage ( XImage *p_ximage );
92 static int X11CreateShmImage ( vout_thread_t *p_vout, XImage **pp_ximage,
93 XShmSegmentInfo *p_shm_info );
94 static void X11DestroyShmImage ( vout_thread_t *p_vout, XImage *p_ximage,
95 XShmSegmentInfo *p_shm_info );
97 /*****************************************************************************
98 * vout_X11Create: allocate X11 video thread output method
99 *****************************************************************************
100 * This function allocate and initialize a X11 vout method. It uses some of the
101 * vout properties to choose the window size, and change them according to the
102 * actual properties of the display.
103 *****************************************************************************/
104 int vout_X11Create( vout_thread_t *p_vout, char *psz_display,
105 int i_root_window, void *p_data )
107 /* Allocate structure */
108 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
109 if( p_vout->p_sys == NULL )
111 intf_ErrMsg("error: %s", strerror(ENOMEM) );
115 /* Open and initialize device. This function issues its own error messages.
116 * Since XLib is usually not thread-safe, we can't use the same display
117 * pointer than the interface or another thread. However, the root window
118 * id is still valid. */
119 if( X11OpenDisplay( p_vout, psz_display, i_root_window, p_data ) )
121 intf_ErrMsg("error: can't initialize X11 display" );
122 free( p_vout->p_sys );
129 /*****************************************************************************
130 * vout_X11Init: initialize X11 video thread output method
131 *****************************************************************************
132 * This function create the XImages needed by the output thread. It is called
133 * at the beginning of the thread, but also each time the window is resized.
134 *****************************************************************************/
135 int vout_X11Init( vout_thread_t *p_vout )
139 /* Create XImages using XShm extension - on failure, fall back to regular
140 * way (and destroy the first image if it was created successfully) */
141 if( p_vout->p_sys->b_shm )
143 /* Create first image */
144 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
145 &p_vout->p_sys->shm_info[0] );
146 if( !i_err ) /* first image has been created */
148 /* Create second image */
149 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
150 &p_vout->p_sys->shm_info[1] ) )
151 { /* error creating the second image */
152 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
153 &p_vout->p_sys->shm_info[0] );
157 if( i_err ) /* an error occured */
159 intf_Msg("XShm video sextension deactivated" );
160 p_vout->p_sys->b_shm = 0;
164 /* Create XImages without XShm extension */
165 if( !p_vout->p_sys->b_shm )
167 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
169 intf_ErrMsg("error: can't create images");
170 p_vout->p_sys->p_ximage[0] = NULL;
171 p_vout->p_sys->p_ximage[1] = NULL;
174 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
176 intf_ErrMsg("error: can't create images");
177 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
178 p_vout->p_sys->p_ximage[0] = NULL;
179 p_vout->p_sys->p_ximage[1] = NULL;
184 /* Set bytes per line and initialize buffers */
185 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
186 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
187 p_vout->p_sys->p_ximage[ 1 ]->data );
191 /*****************************************************************************
192 * vout_X11End: terminate X11 video thread output method
193 *****************************************************************************
194 * Destroy the X11 XImages created by vout_X11Init. It is called at the end of
195 * the thread, but also each time the window is resized.
196 *****************************************************************************/
197 void vout_X11End( vout_thread_t *p_vout )
199 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
201 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
202 &p_vout->p_sys->shm_info[0] );
203 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
204 &p_vout->p_sys->shm_info[1] );
206 else /* ...or regular XImages */
208 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
209 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
213 /*****************************************************************************
214 * vout_X11Destroy: destroy X11 video thread output method
215 *****************************************************************************
216 * Terminate an output method created by vout_CreateOutputMethod
217 *****************************************************************************/
218 void vout_X11Destroy( vout_thread_t *p_vout )
220 X11CloseDisplay( p_vout );
221 free( p_vout->p_sys );
224 /*****************************************************************************
225 * vout_X11Manage: handle X11 events
226 *****************************************************************************
227 * This function should be called regularly by video output thread. It manages
228 * X11 events and allows window resizing. It returns a non null value on
230 *****************************************************************************/
231 int vout_X11Manage( vout_thread_t *p_vout )
234 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
236 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
237 && (p_vout->i_screen_depth == 8) )
239 /* FIXME: clear flags ?? */
245 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
247 intf_DbgMsg("resizing window");
248 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
251 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
252 p_vout->i_width, p_vout->i_height );
254 /* Destroy XImages to change their size */
255 vout_X11End( p_vout );
257 /* Recreate XImages. If SysInit failed, the thread can't go on. */
258 if( vout_X11Init( p_vout ) )
260 intf_ErrMsg("error: can't resize display");
264 /* Tell the video output thread that it will need to rebuild YUV
265 * tables. This is needed since conversion buffer size may have
267 p_vout->i_changes |= VOUT_YUV_CHANGE;
268 intf_Msg("Video display resized (%dx%d)", p_vout->i_width, p_vout->i_height);
274 /*****************************************************************************
275 * vout_X11Display: displays previously rendered output
276 *****************************************************************************
277 * This function send the currently rendered image to X11 server, wait until
278 * it is displayed and switch the two rendering buffer, preparing next frame.
279 *****************************************************************************/
280 void vout_X11Display( vout_thread_t *p_vout )
282 if( p_vout->p_sys->b_shm) /* XShm is used */
284 /* Display rendered image using shared memory extension */
285 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
286 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
288 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
289 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
291 /* Send the order to the X server */
292 XFlush(p_vout->p_sys->p_display);
294 else /* regular X11 capabilities are used */
296 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
297 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
299 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
300 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
302 /* Send the order to the X server */
303 XFlush(p_vout->p_sys->p_display);
307 /*****************************************************************************
308 * vout_X11SetPalette: sets an 8 bpp palette
309 *****************************************************************************
310 * This function sets the palette given as an argument. It does not return
311 * anything, but could later send information on which colors it was unable
313 *****************************************************************************/
314 void vout_X11SetPalette( p_vout_thread_t p_vout,
315 u16 *red, u16 *green, u16 *blue, u16 *transp )
320 intf_DbgMsg( "Palette change called" );
322 /* allocate palette */
323 for( i = 0; i < 255; i++ )
325 /* kludge: colors are indexed reversely because color 255 seems
326 * to be reserved for black even if we try to set it to white */
327 color[i].pixel = 255-i;
329 color[i].flags = DoRed|DoGreen|DoBlue;
330 color[i].red = red[255-i];
331 color[i].blue = blue[255-i];
332 color[i].green = green[255-i];
335 XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );
338 /* following functions are local */
340 /*****************************************************************************
341 * X11OpenDisplay: open and initialize X11 device
342 *****************************************************************************
343 * Create a window according to video output given size, and set other
344 * properties according to the display properties.
345 *****************************************************************************/
346 static int X11OpenDisplay( vout_thread_t *p_vout, char *psz_display, Window root_window, void *p_data )
348 XPixmapFormatValues * p_xpixmap_format; /* pixmap formats */
349 XVisualInfo * p_xvisual; /* visuals informations */
350 XVisualInfo xvisual_template; /* visual template */
351 int i_count; /* array size */
354 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
355 if( p_vout->p_sys->p_display == NULL )
357 intf_ErrMsg("error: can't open display %s", psz_display );
361 /* Initialize structure */
362 p_vout->p_sys->root_window = root_window;
363 p_vout->p_sys->b_shm = (XShmQueryExtension(p_vout->p_sys->p_display) == True);
364 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
365 if( !p_vout->p_sys->b_shm )
367 intf_Msg("XShm video extension is not available");
370 /* Get screen depth */
371 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
372 switch( p_vout->i_screen_depth )
376 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
378 xvisual_template.screen = p_vout->p_sys->i_screen;
379 xvisual_template.class = DirectColor;
380 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
381 &xvisual_template, &i_count );
382 if( p_xvisual == NULL )
384 intf_ErrMsg("error: no PseudoColor visual available");
385 XCloseDisplay( p_vout->p_sys->p_display );
388 p_vout->i_bytes_per_pixel = 1;
390 /* put the colormap in place */
391 p_vout->p_sys->colormap = *(Colormap *)p_data;
398 * Screen depth is higher than 8bpp. TrueColor visual is used.
400 xvisual_template.screen = p_vout->p_sys->i_screen;
401 xvisual_template.class = TrueColor;
402 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
403 &xvisual_template, &i_count );
404 if( p_xvisual == NULL )
406 intf_ErrMsg("error: no TrueColor visual available");
407 XCloseDisplay( p_vout->p_sys->p_display );
410 p_vout->i_red_mask = p_xvisual->red_mask;
411 p_vout->i_green_mask = p_xvisual->green_mask;
412 p_vout->i_blue_mask = p_xvisual->blue_mask;
414 /* There is no difference yet between 3 and 4 Bpp. The only way to find
415 * the actual number of bytes per pixel is to list supported pixmap
417 p_xpixmap_format = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
420 /* Under XFree4.0, the list contains pixmap formats available through
421 * all video depths ; so we have to check against current depth. */
422 p_vout->i_bytes_per_pixel = 0;
423 for( ; i_count-- ; p_xpixmap_format++ )
425 if( p_xpixmap_format->depth == p_vout->i_screen_depth )
427 if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
429 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
435 p_vout->p_sys->p_visual = p_xvisual->visual;
438 /* Create a window */
439 if( X11CreateWindow( p_vout ) )
441 intf_ErrMsg("error: can't open a window");
442 XCloseDisplay( p_vout->p_sys->p_display );
448 /*****************************************************************************
449 * X11CloseDisplay: close X11 device
450 *****************************************************************************
451 * Returns all resources allocated by X11OpenDisplay and restore the original
452 * state of the display.
453 *****************************************************************************/
454 static void X11CloseDisplay( vout_thread_t *p_vout )
456 /* Destroy colormap */
457 if( p_vout->i_screen_depth == 8 )
459 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
463 X11DestroyWindow( p_vout );
465 /* FIXME: We should close the display here, but X returns an error. */
466 //XCloseDisplay( p_vout->p_sys->p_display );
469 /*****************************************************************************
470 * X11CreateWindow: create X11 vout window
471 *****************************************************************************
472 * The video output window will be created. Normally, this window is wether
473 * full screen or part of a parent window. Therefore, it does not need a
474 * title or other hints. Thery are still supplied in case the window would be
475 * spawned as a standalone one by the interface.
476 *****************************************************************************/
477 static int X11CreateWindow( vout_thread_t *p_vout )
479 XSetWindowAttributes xwindow_attributes; /* window attributes */
480 XGCValues xgcvalues; /* graphic context configuration */
481 XEvent xevent; /* first events */
482 boolean_t b_expose; /* 'expose' event received */
483 boolean_t b_map_notify; /* 'map_notify' event received */
485 /* Prepare window attributes */
486 xwindow_attributes.backing_store = Always; /* save the hidden part */
488 /* Create the window and set hints */
489 p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
490 p_vout->p_sys->root_window,
492 p_vout->i_width, p_vout->i_height,
494 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
495 ExposureMask | StructureNotifyMask );
496 XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
497 CWBackingStore, &xwindow_attributes);
499 /* Creation of a graphic context that doesn't generate a GraphicsExpose event
500 when using functions like XCopyArea */
501 xgcvalues.graphics_exposures = False;
502 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
503 GCGraphicsExposures, &xgcvalues);
505 /* Send orders to server, and wait until window is displayed - two events
506 * must be received: a MapNotify event, an Expose event allowing drawing in the
510 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
513 XNextEvent( p_vout->p_sys->p_display, &xevent);
514 if( (xevent.type == Expose)
515 && (xevent.xexpose.window == p_vout->p_sys->window) )
519 else if( (xevent.type == MapNotify)
520 && (xevent.xmap.window == p_vout->p_sys->window) )
525 while( !( b_expose && b_map_notify ) );
526 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
528 /* At this stage, the window is open, displayed, and ready to receive
533 /*****************************************************************************
534 * X11DestroyWindow: destroy X11 window
535 *****************************************************************************
536 * Destroy an X11 window created by vout_CreateWindow
537 *****************************************************************************/
538 static void X11DestroyWindow( vout_thread_t *p_vout )
540 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
541 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
542 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
545 /*****************************************************************************
546 * X11CreateImage: create an XImage
547 *****************************************************************************
548 * Create a simple XImage used as a buffer.
549 *****************************************************************************/
550 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
552 byte_t * pb_data; /* image data storage zone */
553 int i_quantum; /* XImage quantum (see below) */
555 /* Allocate memory for image */
556 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
557 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
558 if( !pb_data ) /* error */
560 intf_ErrMsg("error: %s", strerror(ENOMEM));
564 /* Optimize the quantum of a scanline regarding its size - the quantum is
565 a diviser of the number of bits between the start of two scanlines. */
566 if( !(( p_vout->i_bytes_per_line ) % 32) )
572 if( !(( p_vout->i_bytes_per_line ) % 16) )
583 *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
584 p_vout->i_screen_depth, ZPixmap, 0, pb_data,
585 p_vout->i_width, p_vout->i_height, i_quantum, 0);
586 if(! *pp_ximage ) /* error */
588 intf_ErrMsg( "error: XCreateImage() failed" );
596 /*****************************************************************************
597 * X11CreateShmImage: create an XImage using shared memory extension
598 *****************************************************************************
599 * Prepare an XImage for DisplayX11ShmImage function.
600 * The order of the operations respects the recommandations of the mit-shm
601 * document by J.Corbet and K.Packard. Most of the parameters were copied from
603 *****************************************************************************/
604 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
605 XShmSegmentInfo *p_shm_info)
608 *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
609 p_vout->i_screen_depth, ZPixmap, 0,
610 p_shm_info, p_vout->i_width, p_vout->i_height );
611 if(! *pp_ximage ) /* error */
613 intf_ErrMsg("error: XShmCreateImage() failed");
617 /* Allocate shared memory segment - 0777 set the access permission
618 * rights (like umask), they are not yet supported by X servers */
619 p_shm_info->shmid = shmget( IPC_PRIVATE,
620 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
622 if( p_shm_info->shmid < 0) /* error */
624 intf_ErrMsg("error: can't allocate shared image data (%s)",
626 XDestroyImage( *pp_ximage );
630 /* Attach shared memory segment to process (read/write) */
631 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
632 if(! p_shm_info->shmaddr )
634 intf_ErrMsg("error: can't attach shared memory (%s)",
636 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
637 XDestroyImage( *pp_ximage );
641 /* Mark the shm segment to be removed when there will be no more
642 * attachements, so it is automatic on process exit or after shmdt */
643 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
645 /* Attach shared memory segment to X server (read only) */
646 p_shm_info->readOnly = True;
647 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False ) /* error */
649 intf_ErrMsg("error: can't attach shared memory to X11 server");
650 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
651 * and automatic free */
652 XDestroyImage( *pp_ximage );
656 /* Send image to X server. This instruction is required, since having
657 * built a Shm XImage and not using it causes an error on XCloseDisplay */
658 XFlush( p_vout->p_sys->p_display );
662 /*****************************************************************************
663 * X11DestroyImage: destroy an XImage
664 *****************************************************************************
665 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
666 * destroyed (see vout_ManageOutputMethod())
667 *****************************************************************************/
668 static void X11DestroyImage( XImage *p_ximage )
670 if( p_ximage != NULL )
672 XDestroyImage( p_ximage ); /* no free() required */
676 /*****************************************************************************
678 *****************************************************************************
679 * Destroy XImage AND associated data. Detach shared memory segment from
680 * server and process, then free it. If pointer is NULL, the image won't be
681 * destroyed (see vout_ManageOutputMethod())
682 *****************************************************************************/
683 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
684 XShmSegmentInfo *p_shm_info )
686 /* If pointer is NULL, do nothing */
687 if( p_ximage == NULL )
692 XShmDetach( p_vout->p_sys->p_display, p_shm_info ); /* detach from server */
693 XDestroyImage( p_ximage );
694 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
695 { /* also automatic freeing... */
696 intf_ErrMsg( "error: can't detach shared memory (%s)",