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\n", 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\n" );
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\n" );
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\n");
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\n");
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\n");
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\n");
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)\n", 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\n" );
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\n", 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\n");
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\n");
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\n");
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 );
415 /* FIXME: under XFree4.0, we can get some strange values. Check this */
416 p_vout->i_bytes_per_pixel = 0;
417 for( ; i_count--; p_xpixmap_format++ )
419 if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
421 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
426 p_vout->p_sys->p_visual = p_xvisual->visual;
429 /* Create a window */
430 if( X11CreateWindow( p_vout ) )
432 intf_ErrMsg("error: can't open a window\n");
433 XCloseDisplay( p_vout->p_sys->p_display );
439 /*****************************************************************************
440 * X11CloseDisplay: close X11 device
441 *****************************************************************************
442 * Returns all resources allocated by X11OpenDisplay and restore the original
443 * state of the display.
444 *****************************************************************************/
445 static void X11CloseDisplay( vout_thread_t *p_vout )
447 /* Destroy colormap */
448 if( p_vout->i_screen_depth == 8 )
450 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
454 X11DestroyWindow( p_vout );
456 /* FIXME: We should close the display here, but X returns an error. */
457 //XCloseDisplay( p_vout->p_sys->p_display );
460 /*****************************************************************************
461 * X11CreateWindow: create X11 vout window
462 *****************************************************************************
463 * The video output window will be created. Normally, this window is wether
464 * full screen or part of a parent window. Therefore, it does not need a
465 * title or other hints. Thery are still supplied in case the window would be
466 * spawned as a standalone one by the interface.
467 *****************************************************************************/
468 static int X11CreateWindow( vout_thread_t *p_vout )
470 XSetWindowAttributes xwindow_attributes; /* window attributes */
471 XGCValues xgcvalues; /* graphic context configuration */
472 XEvent xevent; /* first events */
473 boolean_t b_expose; /* 'expose' event received */
474 boolean_t b_map_notify; /* 'map_notify' event received */
476 /* Prepare window attributes */
477 xwindow_attributes.backing_store = Always; /* save the hidden part */
479 /* Create the window and set hints */
480 p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
481 p_vout->p_sys->root_window,
483 p_vout->i_width, p_vout->i_height,
485 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
486 ExposureMask | StructureNotifyMask );
487 XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
488 CWBackingStore, &xwindow_attributes);
490 /* Creation of a graphic context that doesn't generate a GraphicsExpose event
491 when using functions like XCopyArea */
492 xgcvalues.graphics_exposures = False;
493 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
494 GCGraphicsExposures, &xgcvalues);
496 /* Send orders to server, and wait until window is displayed - two events
497 * must be received: a MapNotify event, an Expose event allowing drawing in the
501 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
504 XNextEvent( p_vout->p_sys->p_display, &xevent);
505 if( (xevent.type == Expose)
506 && (xevent.xexpose.window == p_vout->p_sys->window) )
510 else if( (xevent.type == MapNotify)
511 && (xevent.xmap.window == p_vout->p_sys->window) )
516 while( !( b_expose && b_map_notify ) );
517 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
519 /* At this stage, the window is open, displayed, and ready to receive
524 /*****************************************************************************
525 * X11DestroyWindow: destroy X11 window
526 *****************************************************************************
527 * Destroy an X11 window created by vout_CreateWindow
528 *****************************************************************************/
529 static void X11DestroyWindow( vout_thread_t *p_vout )
531 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
532 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
533 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
536 /*****************************************************************************
537 * X11CreateImage: create an XImage
538 *****************************************************************************
539 * Create a simple XImage used as a buffer.
540 *****************************************************************************/
541 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
543 byte_t * pb_data; /* image data storage zone */
544 int i_quantum; /* XImage quantum (see below) */
546 /* Allocate memory for image */
547 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
548 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
549 if( !pb_data ) /* error */
551 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
555 /* Optimize the quantum of a scanline regarding its size - the quantum is
556 a diviser of the number of bits between the start of two scanlines. */
557 if( !(( p_vout->i_bytes_per_line ) % 32) )
563 if( !(( p_vout->i_bytes_per_line ) % 16) )
574 *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
575 p_vout->i_screen_depth, ZPixmap, 0, pb_data,
576 p_vout->i_width, p_vout->i_height, i_quantum, 0);
577 if(! *pp_ximage ) /* error */
579 intf_ErrMsg( "error: XCreateImage() failed\n" );
587 /*****************************************************************************
588 * X11CreateShmImage: create an XImage using shared memory extension
589 *****************************************************************************
590 * Prepare an XImage for DisplayX11ShmImage function.
591 * The order of the operations respects the recommandations of the mit-shm
592 * document by J.Corbet and K.Packard. Most of the parameters were copied from
594 *****************************************************************************/
595 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
596 XShmSegmentInfo *p_shm_info)
599 *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
600 p_vout->i_screen_depth, ZPixmap, 0,
601 p_shm_info, p_vout->i_width, p_vout->i_height );
602 if(! *pp_ximage ) /* error */
604 intf_ErrMsg("error: XShmCreateImage() failed\n");
608 /* Allocate shared memory segment - 0777 set the access permission
609 * rights (like umask), they are not yet supported by X servers */
610 p_shm_info->shmid = shmget( IPC_PRIVATE,
611 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
613 if( p_shm_info->shmid < 0) /* error */
615 intf_ErrMsg("error: can't allocate shared image data (%s)\n",
617 XDestroyImage( *pp_ximage );
621 /* Attach shared memory segment to process (read/write) */
622 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
623 if(! p_shm_info->shmaddr )
625 intf_ErrMsg("error: can't attach shared memory (%s)\n",
627 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
628 XDestroyImage( *pp_ximage );
632 /* Mark the shm segment to be removed when there will be no more
633 * attachements, so it is automatic on process exit or after shmdt */
634 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
636 /* Attach shared memory segment to X server (read only) */
637 p_shm_info->readOnly = True;
638 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False ) /* error */
640 intf_ErrMsg("error: can't attach shared memory to X11 server\n");
641 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
642 * and automatic free */
643 XDestroyImage( *pp_ximage );
647 /* Send image to X server. This instruction is required, since having
648 * built a Shm XImage and not using it causes an error on XCloseDisplay */
649 XFlush( p_vout->p_sys->p_display );
653 /*****************************************************************************
654 * X11DestroyImage: destroy an XImage
655 *****************************************************************************
656 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
657 * destroyed (see vout_ManageOutputMethod())
658 *****************************************************************************/
659 static void X11DestroyImage( XImage *p_ximage )
661 if( p_ximage != NULL )
663 XDestroyImage( p_ximage ); /* no free() required */
667 /*****************************************************************************
669 *****************************************************************************
670 * Destroy XImage AND associated data. Detach shared memory segment from
671 * server and process, then free it. If pointer is NULL, the image won't be
672 * destroyed (see vout_ManageOutputMethod())
673 *****************************************************************************/
674 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
675 XShmSegmentInfo *p_shm_info )
677 /* If pointer is NULL, do nothing */
678 if( p_ximage == NULL )
683 XShmDetach( p_vout->p_sys->p_display, p_shm_info ); /* detach from server */
684 XDestroyImage( p_ximage );
685 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
686 { /* also automatic freeing... */
687 intf_ErrMsg( "error: can't detach shared memory (%s)\n",