1 /*****************************************************************************
2 * vout_x11.c: X11 video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
33 #include <sys/types.h> /* typedef ushort */
36 #include <sys/shm.h> /* shmget(), shmctl() */
38 #include <X11/Xutil.h>
39 #include <X11/extensions/XShm.h>
48 #include "video_output.h"
52 /*****************************************************************************
53 * vout_sys_t: video output X11 method descriptor
54 *****************************************************************************
55 * This structure is part of the video output thread descriptor.
56 * It describes the X11 specific properties of an output thread. X11 video
57 * output is performed through regular resizable windows. Windows can be
58 * dynamically resized to adapt to the size of the streams.
59 *****************************************************************************/
60 typedef struct vout_sys_s
63 boolean_t b_shm; /* shared memory extension flag */
65 /* Internal settings and properties */
66 Display * p_display; /* display pointer */
67 Visual * p_visual; /* visual pointer */
68 int i_screen; /* screen number */
69 Window root_window; /* root window */
70 Window window; /* window instance handler */
71 GC gc; /* graphic context instance handler */
72 Colormap colormap; /* colormap used (8bpp only) */
74 /* Display buffers and shared memory information */
75 XImage * p_ximage[2]; /* XImage pointer */
76 XShmSegmentInfo shm_info[2]; /* shared memory zone information */
79 /*****************************************************************************
81 *****************************************************************************/
82 static int X11OpenDisplay ( vout_thread_t *p_vout, char *psz_display, Window root_window, void *p_data );
83 static void X11CloseDisplay ( vout_thread_t *p_vout );
84 static int X11CreateWindow ( vout_thread_t *p_vout );
85 static void X11DestroyWindow ( vout_thread_t *p_vout );
86 static int X11CreateImage ( vout_thread_t *p_vout, XImage **pp_ximage );
87 static void X11DestroyImage ( XImage *p_ximage );
88 static int X11CreateShmImage ( vout_thread_t *p_vout, XImage **pp_ximage,
89 XShmSegmentInfo *p_shm_info );
90 static void X11DestroyShmImage ( vout_thread_t *p_vout, XImage *p_ximage,
91 XShmSegmentInfo *p_shm_info );
93 /*****************************************************************************
94 * vout_X11Create: allocate X11 video thread output method
95 *****************************************************************************
96 * This function allocate and initialize a X11 vout method. It uses some of the
97 * vout properties to choose the window size, and change them according to the
98 * actual properties of the display.
99 *****************************************************************************/
100 int vout_X11Create( vout_thread_t *p_vout, char *psz_display,
101 int i_root_window, void *p_data )
103 /* Allocate structure */
104 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
105 if( p_vout->p_sys == NULL )
107 intf_ErrMsg("error: %s", strerror(ENOMEM) );
111 /* Open and initialize device. This function issues its own error messages.
112 * Since XLib is usually not thread-safe, we can't use the same display
113 * pointer than the interface or another thread. However, the root window
114 * id is still valid. */
115 if( X11OpenDisplay( p_vout, psz_display, i_root_window, p_data ) )
117 intf_ErrMsg("error: can't initialize X11 display" );
118 free( p_vout->p_sys );
125 /*****************************************************************************
126 * vout_X11Init: initialize X11 video thread output method
127 *****************************************************************************
128 * This function create the XImages needed by the output thread. It is called
129 * at the beginning of the thread, but also each time the window is resized.
130 *****************************************************************************/
131 int vout_X11Init( vout_thread_t *p_vout )
135 /* Create XImages using XShm extension - on failure, fall back to regular
136 * way (and destroy the first image if it was created successfully) */
137 if( p_vout->p_sys->b_shm )
139 /* Create first image */
140 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
141 &p_vout->p_sys->shm_info[0] );
142 if( !i_err ) /* first image has been created */
144 /* Create second image */
145 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
146 &p_vout->p_sys->shm_info[1] ) )
147 { /* error creating the second image */
148 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
149 &p_vout->p_sys->shm_info[0] );
153 if( i_err ) /* an error occured */
155 intf_Msg("XShm video sextension deactivated" );
156 p_vout->p_sys->b_shm = 0;
160 /* Create XImages without XShm extension */
161 if( !p_vout->p_sys->b_shm )
163 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
165 intf_ErrMsg("error: can't create images");
166 p_vout->p_sys->p_ximage[0] = NULL;
167 p_vout->p_sys->p_ximage[1] = NULL;
170 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
172 intf_ErrMsg("error: can't create images");
173 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
174 p_vout->p_sys->p_ximage[0] = NULL;
175 p_vout->p_sys->p_ximage[1] = NULL;
180 /* Set bytes per line and initialize buffers */
181 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
182 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
183 p_vout->p_sys->p_ximage[ 1 ]->data );
187 /*****************************************************************************
188 * vout_X11End: terminate X11 video thread output method
189 *****************************************************************************
190 * Destroy the X11 XImages created by vout_X11Init. It is called at the end of
191 * the thread, but also each time the window is resized.
192 *****************************************************************************/
193 void vout_X11End( vout_thread_t *p_vout )
195 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
197 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
198 &p_vout->p_sys->shm_info[0] );
199 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
200 &p_vout->p_sys->shm_info[1] );
202 else /* ...or regular XImages */
204 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
205 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
209 /*****************************************************************************
210 * vout_X11Destroy: destroy X11 video thread output method
211 *****************************************************************************
212 * Terminate an output method created by vout_CreateOutputMethod
213 *****************************************************************************/
214 void vout_X11Destroy( vout_thread_t *p_vout )
216 X11CloseDisplay( p_vout );
217 free( p_vout->p_sys );
220 /*****************************************************************************
221 * vout_X11Manage: handle X11 events
222 *****************************************************************************
223 * This function should be called regularly by video output thread. It manages
224 * X11 events and allows window resizing. It returns a non null value on
226 *****************************************************************************/
227 int vout_X11Manage( vout_thread_t *p_vout )
230 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
232 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
233 && (p_vout->i_screen_depth == 8) )
235 /* FIXME: clear flags ?? */
241 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
243 intf_DbgMsg("resizing window");
244 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
247 XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
248 p_vout->i_width, p_vout->i_height );
250 /* Destroy XImages to change their size */
251 vout_X11End( p_vout );
253 /* Recreate XImages. If SysInit failed, the thread can't go on. */
254 if( vout_X11Init( p_vout ) )
256 intf_ErrMsg("error: can't resize display");
260 /* Tell the video output thread that it will need to rebuild YUV
261 * tables. This is needed since conversion buffer size may have
263 p_vout->i_changes |= VOUT_YUV_CHANGE;
264 intf_Msg("Video display resized (%dx%d)", p_vout->i_width, p_vout->i_height);
270 /*****************************************************************************
271 * vout_X11Display: displays previously rendered output
272 *****************************************************************************
273 * This function send the currently rendered image to X11 server, wait until
274 * it is displayed and switch the two rendering buffer, preparing next frame.
275 *****************************************************************************/
276 void vout_X11Display( vout_thread_t *p_vout )
278 if( p_vout->p_sys->b_shm) /* XShm is used */
280 /* Display rendered image using shared memory extension */
281 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
282 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
284 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
285 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
287 /* Send the order to the X server */
288 XFlush(p_vout->p_sys->p_display);
290 else /* regular X11 capabilities are used */
292 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
293 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
295 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
296 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
298 /* Send the order to the X server */
299 XFlush(p_vout->p_sys->p_display);
303 /*****************************************************************************
304 * vout_X11SetPalette: sets an 8 bpp palette
305 *****************************************************************************
306 * This function sets the palette given as an argument. It does not return
307 * anything, but could later send information on which colors it was unable
309 *****************************************************************************/
310 void vout_X11SetPalette( p_vout_thread_t p_vout,
311 u16 *red, u16 *green, u16 *blue, u16 *transp )
316 intf_DbgMsg( "Palette change called" );
318 /* allocate palette */
319 for( i = 0; i < 255; i++ )
321 /* kludge: colors are indexed reversely because color 255 seems
322 * to be reserved for black even if we try to set it to white */
323 color[i].pixel = 255-i;
325 color[i].flags = DoRed|DoGreen|DoBlue;
326 color[i].red = red[255-i];
327 color[i].blue = blue[255-i];
328 color[i].green = green[255-i];
331 XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );
334 /* following functions are local */
336 /*****************************************************************************
337 * X11OpenDisplay: open and initialize X11 device
338 *****************************************************************************
339 * Create a window according to video output given size, and set other
340 * properties according to the display properties.
341 *****************************************************************************/
342 static int X11OpenDisplay( vout_thread_t *p_vout, char *psz_display, Window root_window, void *p_data )
344 XPixmapFormatValues * p_xpixmap_format; /* pixmap formats */
345 XVisualInfo * p_xvisual; /* visuals informations */
346 XVisualInfo xvisual_template; /* visual template */
347 int i_count; /* array size */
350 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
351 if( p_vout->p_sys->p_display == NULL )
353 intf_ErrMsg("error: can't open display %s", psz_display );
357 /* Initialize structure */
358 p_vout->p_sys->root_window = root_window;
359 p_vout->p_sys->b_shm = (XShmQueryExtension(p_vout->p_sys->p_display) == True);
360 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
361 if( !p_vout->p_sys->b_shm )
363 intf_Msg("XShm video extension is not available");
366 /* Get screen depth */
367 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
368 switch( p_vout->i_screen_depth )
372 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
374 xvisual_template.screen = p_vout->p_sys->i_screen;
375 xvisual_template.class = DirectColor;
376 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
377 &xvisual_template, &i_count );
378 if( p_xvisual == NULL )
380 intf_ErrMsg("error: no PseudoColor visual available");
381 XCloseDisplay( p_vout->p_sys->p_display );
384 p_vout->i_bytes_per_pixel = 1;
386 /* put the colormap in place */
387 p_vout->p_sys->colormap = *(Colormap *)p_data;
394 * Screen depth is higher than 8bpp. TrueColor visual is used.
396 xvisual_template.screen = p_vout->p_sys->i_screen;
397 xvisual_template.class = TrueColor;
398 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
399 &xvisual_template, &i_count );
400 if( p_xvisual == NULL )
402 intf_ErrMsg("error: no TrueColor visual available");
403 XCloseDisplay( p_vout->p_sys->p_display );
406 p_vout->i_red_mask = p_xvisual->red_mask;
407 p_vout->i_green_mask = p_xvisual->green_mask;
408 p_vout->i_blue_mask = p_xvisual->blue_mask;
410 /* There is no difference yet between 3 and 4 Bpp. The only way to find
411 * the actual number of bytes per pixel is to list supported pixmap
413 p_xpixmap_format = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
416 /* Under XFree4.0, the list contains pixmap formats available through
417 * all video depths ; so we have to check against current depth. */
418 p_vout->i_bytes_per_pixel = 0;
419 for( ; i_count-- ; p_xpixmap_format++ )
421 if( p_xpixmap_format->depth == p_vout->i_screen_depth )
423 if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
425 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
431 p_vout->p_sys->p_visual = p_xvisual->visual;
434 /* Create a window */
435 if( X11CreateWindow( p_vout ) )
437 intf_ErrMsg("error: can't open a window");
438 XCloseDisplay( p_vout->p_sys->p_display );
444 /*****************************************************************************
445 * X11CloseDisplay: close X11 device
446 *****************************************************************************
447 * Returns all resources allocated by X11OpenDisplay and restore the original
448 * state of the display.
449 *****************************************************************************/
450 static void X11CloseDisplay( vout_thread_t *p_vout )
452 /* Destroy colormap */
453 if( p_vout->i_screen_depth == 8 )
455 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
459 X11DestroyWindow( p_vout );
461 /* FIXME: We should close the display here, but X returns an error. */
462 //XCloseDisplay( p_vout->p_sys->p_display );
465 /*****************************************************************************
466 * X11CreateWindow: create X11 vout window
467 *****************************************************************************
468 * The video output window will be created. Normally, this window is wether
469 * full screen or part of a parent window. Therefore, it does not need a
470 * title or other hints. Thery are still supplied in case the window would be
471 * spawned as a standalone one by the interface.
472 *****************************************************************************/
473 static int X11CreateWindow( vout_thread_t *p_vout )
475 XSetWindowAttributes xwindow_attributes; /* window attributes */
476 XGCValues xgcvalues; /* graphic context configuration */
477 XEvent xevent; /* first events */
478 boolean_t b_expose; /* 'expose' event received */
479 boolean_t b_map_notify; /* 'map_notify' event received */
481 /* Prepare window attributes */
482 xwindow_attributes.backing_store = Always; /* save the hidden part */
484 /* Create the window and set hints */
485 p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
486 p_vout->p_sys->root_window,
488 p_vout->i_width, p_vout->i_height,
490 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
491 ExposureMask | StructureNotifyMask );
492 XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
493 CWBackingStore, &xwindow_attributes);
495 /* Creation of a graphic context that doesn't generate a GraphicsExpose event
496 when using functions like XCopyArea */
497 xgcvalues.graphics_exposures = False;
498 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
499 GCGraphicsExposures, &xgcvalues);
501 /* Send orders to server, and wait until window is displayed - two events
502 * must be received: a MapNotify event, an Expose event allowing drawing in the
506 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
509 XNextEvent( p_vout->p_sys->p_display, &xevent);
510 if( (xevent.type == Expose)
511 && (xevent.xexpose.window == p_vout->p_sys->window) )
515 else if( (xevent.type == MapNotify)
516 && (xevent.xmap.window == p_vout->p_sys->window) )
521 while( !( b_expose && b_map_notify ) );
522 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
524 /* At this stage, the window is open, displayed, and ready to receive
529 /*****************************************************************************
530 * X11DestroyWindow: destroy X11 window
531 *****************************************************************************
532 * Destroy an X11 window created by vout_CreateWindow
533 *****************************************************************************/
534 static void X11DestroyWindow( vout_thread_t *p_vout )
536 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
537 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
538 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
541 /*****************************************************************************
542 * X11CreateImage: create an XImage
543 *****************************************************************************
544 * Create a simple XImage used as a buffer.
545 *****************************************************************************/
546 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
548 byte_t * pb_data; /* image data storage zone */
549 int i_quantum; /* XImage quantum (see below) */
551 /* Allocate memory for image */
552 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
553 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
554 if( !pb_data ) /* error */
556 intf_ErrMsg("error: %s", strerror(ENOMEM));
560 /* Optimize the quantum of a scanline regarding its size - the quantum is
561 a diviser of the number of bits between the start of two scanlines. */
562 if( !(( p_vout->i_bytes_per_line ) % 32) )
568 if( !(( p_vout->i_bytes_per_line ) % 16) )
579 *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
580 p_vout->i_screen_depth, ZPixmap, 0, pb_data,
581 p_vout->i_width, p_vout->i_height, i_quantum, 0);
582 if(! *pp_ximage ) /* error */
584 intf_ErrMsg( "error: XCreateImage() failed" );
592 /*****************************************************************************
593 * X11CreateShmImage: create an XImage using shared memory extension
594 *****************************************************************************
595 * Prepare an XImage for DisplayX11ShmImage function.
596 * The order of the operations respects the recommandations of the mit-shm
597 * document by J.Corbet and K.Packard. Most of the parameters were copied from
599 *****************************************************************************/
600 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
601 XShmSegmentInfo *p_shm_info)
604 *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
605 p_vout->i_screen_depth, ZPixmap, 0,
606 p_shm_info, p_vout->i_width, p_vout->i_height );
607 if(! *pp_ximage ) /* error */
609 intf_ErrMsg("error: XShmCreateImage() failed");
613 /* Allocate shared memory segment - 0777 set the access permission
614 * rights (like umask), they are not yet supported by X servers */
615 p_shm_info->shmid = shmget( IPC_PRIVATE,
616 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
618 if( p_shm_info->shmid < 0) /* error */
620 intf_ErrMsg("error: can't allocate shared image data (%s)",
622 XDestroyImage( *pp_ximage );
626 /* Attach shared memory segment to process (read/write) */
627 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
628 if(! p_shm_info->shmaddr )
630 intf_ErrMsg("error: can't attach shared memory (%s)",
632 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
633 XDestroyImage( *pp_ximage );
637 /* Mark the shm segment to be removed when there will be no more
638 * attachements, so it is automatic on process exit or after shmdt */
639 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
641 /* Attach shared memory segment to X server (read only) */
642 p_shm_info->readOnly = True;
643 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False ) /* error */
645 intf_ErrMsg("error: can't attach shared memory to X11 server");
646 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
647 * and automatic free */
648 XDestroyImage( *pp_ximage );
652 /* Send image to X server. This instruction is required, since having
653 * built a Shm XImage and not using it causes an error on XCloseDisplay */
654 XFlush( p_vout->p_sys->p_display );
658 /*****************************************************************************
659 * X11DestroyImage: destroy an XImage
660 *****************************************************************************
661 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
662 * destroyed (see vout_ManageOutputMethod())
663 *****************************************************************************/
664 static void X11DestroyImage( XImage *p_ximage )
666 if( p_ximage != NULL )
668 XDestroyImage( p_ximage ); /* no free() required */
672 /*****************************************************************************
674 *****************************************************************************
675 * Destroy XImage AND associated data. Detach shared memory segment from
676 * server and process, then free it. If pointer is NULL, the image won't be
677 * destroyed (see vout_ManageOutputMethod())
678 *****************************************************************************/
679 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
680 XShmSegmentInfo *p_shm_info )
682 /* If pointer is NULL, do nothing */
683 if( p_ximage == NULL )
688 XShmDetach( p_vout->p_sys->p_display, p_shm_info ); /* detach from server */
689 XDestroyImage( p_ximage );
690 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
691 { /* also automatic freeing... */
692 intf_ErrMsg( "error: can't detach shared memory (%s)",