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 );
92 static void X11SetPalette ( p_vout_thread_t p_vout,
93 u16 *red, u16 *green, u16 *blue, u16 *transp );
95 /*****************************************************************************
96 * vout_SysCreate: allocate X11 video thread output method
97 *****************************************************************************
98 * This function allocate and initialize a X11 vout method. It uses some of the
99 * vout properties to choose the window size, and change them according to the
100 * actual properties of the display.
101 *****************************************************************************/
102 int vout_SysCreate( vout_thread_t *p_vout, char *psz_display,
103 int i_root_window, void *p_data )
105 /* Allocate structure */
106 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
107 if( p_vout->p_sys == NULL )
109 intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
113 /* Open and initialize device. This function issues its own error messages.
114 * Since XLib is usually not thread-safe, we can't use the same display
115 * pointer than the interface or another thread. However, the root window
116 * id is still valid. */
117 if( X11OpenDisplay( p_vout, psz_display, i_root_window, p_data ) )
119 intf_ErrMsg("error: can't initialize X11 display\n" );
120 free( p_vout->p_sys );
127 /*****************************************************************************
128 * vout_SysInit: initialize X11 video thread output method
129 *****************************************************************************
130 * This function create the XImages needed by the output thread. It is called
131 * at the beginning of the thread, but also each time the window is resized.
132 *****************************************************************************/
133 int vout_SysInit( vout_thread_t *p_vout )
137 /* Initialize palette changing procedure */
138 p_vout->p_set_palette = X11SetPalette;
140 /* Create XImages using XShm extension - on failure, fall back to regular
141 * way (and destroy the first image if it was created successfully) */
142 if( p_vout->p_sys->b_shm )
144 /* Create first image */
145 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
146 &p_vout->p_sys->shm_info[0] );
147 if( !i_err ) /* first image has been created */
149 /* Create second image */
150 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
151 &p_vout->p_sys->shm_info[1] ) )
152 { /* error creating the second image */
153 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
154 &p_vout->p_sys->shm_info[0] );
158 if( i_err ) /* an error occured */
160 intf_Msg("XShm video sextension desactivated\n" );
161 p_vout->p_sys->b_shm = 0;
165 /* Create XImages without XShm extension */
166 if( !p_vout->p_sys->b_shm )
168 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
170 intf_ErrMsg("error: can't create images\n");
171 p_vout->p_sys->p_ximage[0] = NULL;
172 p_vout->p_sys->p_ximage[1] = NULL;
175 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
177 intf_ErrMsg("error: can't create images\n");
178 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
179 p_vout->p_sys->p_ximage[0] = NULL;
180 p_vout->p_sys->p_ximage[1] = NULL;
185 /* Set bytes per line and initialize buffers */
186 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
187 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
188 p_vout->p_sys->p_ximage[ 1 ]->data );
192 /*****************************************************************************
193 * vout_SysEnd: terminate X11 video thread output method
194 *****************************************************************************
195 * Destroy the X11 XImages created by vout_SysInit. It is called at the end of
196 * the thread, but also each time the window is resized.
197 *****************************************************************************/
198 void vout_SysEnd( vout_thread_t *p_vout )
200 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
202 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
203 &p_vout->p_sys->shm_info[0] );
204 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
205 &p_vout->p_sys->shm_info[1] );
207 else /* ...or regular XImages */
209 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
210 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
214 /*****************************************************************************
215 * vout_SysDestroy: destroy X11 video thread output method
216 *****************************************************************************
217 * Terminate an output method created by vout_CreateOutputMethod
218 *****************************************************************************/
219 void vout_SysDestroy( vout_thread_t *p_vout )
221 X11CloseDisplay( p_vout );
222 free( p_vout->p_sys );
225 /*****************************************************************************
226 * vout_SysManage: handle X11 events
227 *****************************************************************************
228 * This function should be called regularly by video output thread. It manages
229 * X11 events and allows window resizing. It returns a non null value on
231 *****************************************************************************/
232 int vout_SysManage( vout_thread_t *p_vout )
235 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
237 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE) && (p_vout->i_screen_depth == 8) )
239 /* FIXME: clear flags ?? */
245 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
247 intf_DbgMsg("resizing window\n");
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_SysEnd( p_vout );
257 /* Recreate XImages. If SysInit failed, the thread can't go on. */
258 if( vout_SysInit( p_vout ) )
260 intf_ErrMsg("error: can't resize display\n");
264 /* Tell the video output thread that it will need to rebuild YUV
265 * tables. This is needed since conversion buffer size may have changed */
266 p_vout->i_changes |= VOUT_YUV_CHANGE;
267 intf_Msg("Video display resized (%dx%d)\n", p_vout->i_width, p_vout->i_height);
273 /*****************************************************************************
274 * vout_SysDisplay: displays previously rendered output
275 *****************************************************************************
276 * This function send the currently rendered image to X11 server, wait until
277 * it is displayed and switch the two rendering buffer, preparing next frame.
278 *****************************************************************************/
279 void vout_SysDisplay( vout_thread_t *p_vout )
281 if( p_vout->p_sys->b_shm) /* XShm is used */
283 /* Display rendered image using shared memory extension */
284 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
285 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
287 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
288 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
290 /* Send the order to the X server */
291 XFlush(p_vout->p_sys->p_display);
293 else /* regular X11 capabilities are used */
295 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
296 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
298 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
299 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
301 /* Send the order to the X server */
302 XFlush(p_vout->p_sys->p_display);
306 /* following functions are local */
308 /*****************************************************************************
309 * X11OpenDisplay: open and initialize X11 device
310 *****************************************************************************
311 * Create a window according to video output given size, and set other
312 * properties according to the display properties.
313 *****************************************************************************/
314 static int X11OpenDisplay( vout_thread_t *p_vout, char *psz_display, Window root_window, void *p_data )
316 XPixmapFormatValues * p_xpixmap_format; /* pixmap formats */
317 XVisualInfo * p_xvisual; /* visuals informations */
318 XVisualInfo xvisual_template; /* visual template */
319 int i_count; /* array size */
322 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
323 if( p_vout->p_sys->p_display == NULL )
325 intf_ErrMsg("error: can't open display %s\n", psz_display );
329 /* Initialize structure */
330 p_vout->p_sys->root_window = root_window;
331 p_vout->p_sys->b_shm = (XShmQueryExtension(p_vout->p_sys->p_display) == True);
332 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
333 if( !p_vout->p_sys->b_shm )
335 intf_Msg("XShm video extension is not available\n");
338 /* Get screen depth */
339 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
340 switch( p_vout->i_screen_depth )
344 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
346 xvisual_template.screen = p_vout->p_sys->i_screen;
347 xvisual_template.class = DirectColor;
348 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
349 &xvisual_template, &i_count );
350 if( p_xvisual == NULL )
352 intf_ErrMsg("error: no PseudoColor visual available\n");
353 XCloseDisplay( p_vout->p_sys->p_display );
356 p_vout->i_bytes_per_pixel = 1;
358 /* put the colormap in place */
359 p_vout->p_sys->colormap = *(Colormap *)p_data;
366 * Screen depth is higher than 8bpp. TrueColor visual is used.
368 xvisual_template.screen = p_vout->p_sys->i_screen;
369 xvisual_template.class = TrueColor;
370 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
371 &xvisual_template, &i_count );
372 if( p_xvisual == NULL )
374 intf_ErrMsg("error: no TrueColor visual available\n");
375 XCloseDisplay( p_vout->p_sys->p_display );
378 p_vout->i_red_mask = p_xvisual->red_mask;
379 p_vout->i_green_mask = p_xvisual->green_mask;
380 p_vout->i_blue_mask = p_xvisual->blue_mask;
382 /* There is no difference yet between 3 and 4 Bpp. The only way to find
383 * the actual number of bytes per pixel is to list supported pixmap
385 p_xpixmap_format = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
387 /* FIXME: under XFree4.0, we can get some strange values. Check this */
388 p_vout->i_bytes_per_pixel = 0;
389 for( ; i_count--; p_xpixmap_format++ )
391 if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
393 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
398 p_vout->p_sys->p_visual = p_xvisual->visual;
401 /* Create a window */
402 if( X11CreateWindow( p_vout ) )
404 intf_ErrMsg("error: can't open a window\n");
405 XCloseDisplay( p_vout->p_sys->p_display );
411 /*****************************************************************************
412 * X11CloseDisplay: close X11 device
413 *****************************************************************************
414 * Returns all resources allocated by X11OpenDisplay and restore the original
415 * state of the display.
416 *****************************************************************************/
417 static void X11CloseDisplay( vout_thread_t *p_vout )
419 /* Destroy colormap */
420 if( p_vout->i_screen_depth == 8 )
422 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
426 X11DestroyWindow( p_vout );
428 /* FIXME: We should close the display here, but X returns an error. */
429 //XCloseDisplay( p_vout->p_sys->p_display );
432 /*****************************************************************************
433 * X11CreateWindow: create X11 vout window
434 *****************************************************************************
435 * The video output window will be created. Normally, this window is wether
436 * full screen or part of a parent window. Therefore, it does not need a
437 * title or other hints. Thery are still supplied in case the window would be
438 * spawned as a standalone one by the interface.
439 *****************************************************************************/
440 static int X11CreateWindow( vout_thread_t *p_vout )
442 XSetWindowAttributes xwindow_attributes; /* window attributes */
443 XGCValues xgcvalues; /* graphic context configuration */
444 XEvent xevent; /* first events */
445 boolean_t b_expose; /* 'expose' event received */
446 boolean_t b_map_notify; /* 'map_notify' event received */
448 /* Prepare window attributes */
449 xwindow_attributes.backing_store = Always; /* save the hidden part */
451 /* Create the window and set hints */
452 p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
453 p_vout->p_sys->root_window,
455 p_vout->i_width, p_vout->i_height,
457 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
458 ExposureMask | StructureNotifyMask );
459 XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
460 CWBackingStore, &xwindow_attributes);
462 /* Creation of a graphic context that doesn't generate a GraphicsExpose event
463 when using functions like XCopyArea */
464 xgcvalues.graphics_exposures = False;
465 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
466 GCGraphicsExposures, &xgcvalues);
468 /* Send orders to server, and wait until window is displayed - two events
469 * must be received: a MapNotify event, an Expose event allowing drawing in the
473 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
476 XNextEvent( p_vout->p_sys->p_display, &xevent);
477 if( (xevent.type == Expose)
478 && (xevent.xexpose.window == p_vout->p_sys->window) )
482 else if( (xevent.type == MapNotify)
483 && (xevent.xmap.window == p_vout->p_sys->window) )
488 while( !( b_expose && b_map_notify ) );
489 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
491 /* At this stage, the window is open, displayed, and ready to receive
496 /*****************************************************************************
497 * X11DestroyWindow: destroy X11 window
498 *****************************************************************************
499 * Destroy an X11 window created by vout_CreateWindow
500 *****************************************************************************/
501 static void X11DestroyWindow( vout_thread_t *p_vout )
503 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
504 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
505 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
508 /*****************************************************************************
509 * X11CreateImage: create an XImage
510 *****************************************************************************
511 * Create a simple XImage used as a buffer.
512 *****************************************************************************/
513 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
515 byte_t * pb_data; /* image data storage zone */
516 int i_quantum; /* XImage quantum (see below) */
518 /* Allocate memory for image */
519 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
520 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
521 if( !pb_data ) /* error */
523 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
527 /* Optimize the quantum of a scanline regarding its size - the quantum is
528 a diviser of the number of bits between the start of two scanlines. */
529 if( !(( p_vout->i_bytes_per_line ) % 32) )
535 if( !(( p_vout->i_bytes_per_line ) % 16) )
546 *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
547 p_vout->i_screen_depth, ZPixmap, 0, pb_data,
548 p_vout->i_width, p_vout->i_height, i_quantum, 0);
549 if(! *pp_ximage ) /* error */
551 intf_ErrMsg( "error: XCreateImage() failed\n" );
559 /*****************************************************************************
560 * X11CreateShmImage: create an XImage using shared memory extension
561 *****************************************************************************
562 * Prepare an XImage for DisplayX11ShmImage function.
563 * The order of the operations respects the recommandations of the mit-shm
564 * document by J.Corbet and K.Packard. Most of the parameters were copied from
566 *****************************************************************************/
567 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
568 XShmSegmentInfo *p_shm_info)
571 *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
572 p_vout->i_screen_depth, ZPixmap, 0,
573 p_shm_info, p_vout->i_width, p_vout->i_height );
574 if(! *pp_ximage ) /* error */
576 intf_ErrMsg("error: XShmCreateImage() failed\n");
580 /* Allocate shared memory segment - 0777 set the access permission
581 * rights (like umask), they are not yet supported by X servers */
582 p_shm_info->shmid = shmget( IPC_PRIVATE,
583 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
585 if( p_shm_info->shmid < 0) /* error */
587 intf_ErrMsg("error: can't allocate shared image data (%s)\n",
589 XDestroyImage( *pp_ximage );
593 /* Attach shared memory segment to process (read/write) */
594 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
595 if(! p_shm_info->shmaddr )
597 intf_ErrMsg("error: can't attach shared memory (%s)\n",
599 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
600 XDestroyImage( *pp_ximage );
604 /* Mark the shm segment to be removed when there will be no more
605 * attachements, so it is automatic on process exit or after shmdt */
606 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
608 /* Attach shared memory segment to X server (read only) */
609 p_shm_info->readOnly = True;
610 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False ) /* error */
612 intf_ErrMsg("error: can't attach shared memory to X11 server\n");
613 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
614 * and automatic free */
615 XDestroyImage( *pp_ximage );
619 /* Send image to X server. This instruction is required, since having
620 * built a Shm XImage and not using it causes an error on XCloseDisplay */
621 XFlush( p_vout->p_sys->p_display );
625 /*****************************************************************************
626 * X11DestroyImage: destroy an XImage
627 *****************************************************************************
628 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
629 * destroyed (see vout_ManageOutputMethod())
630 *****************************************************************************/
631 static void X11DestroyImage( XImage *p_ximage )
633 if( p_ximage != NULL )
635 XDestroyImage( p_ximage ); /* no free() required */
639 /*****************************************************************************
641 *****************************************************************************
642 * Destroy XImage AND associated data. Detach shared memory segment from
643 * server and process, then free it. If pointer is NULL, the image won't be
644 * destroyed (see vout_ManageOutputMethod())
645 *****************************************************************************/
646 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
647 XShmSegmentInfo *p_shm_info )
649 /* If pointer is NULL, do nothing */
650 if( p_ximage == NULL )
655 XShmDetach( p_vout->p_sys->p_display, p_shm_info ); /* detach from server */
656 XDestroyImage( p_ximage );
657 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
658 { /* also automatic freeing... */
659 intf_ErrMsg( "error: can't detach shared memory (%s)\n",
664 /*****************************************************************************
665 * X11SetPalette: sets an 8 bpp palette
666 *****************************************************************************
667 * This function sets the palette given as an argument. It does not return
668 * anything, but could later send information on which colors it was unable
670 *****************************************************************************/
671 static void X11SetPalette ( p_vout_thread_t p_vout,
672 u16 *red, u16 *green, u16 *blue, u16 *transp )
677 intf_DbgMsg( "Palette change called\n" );
679 /* allocate palette */
680 for( i = 0; i < 255; i++ )
682 /* kludge: colors are indexed reversely because color 255 seems
683 * to be reserved for black even if we try to set it to white */
684 color[i].pixel = 255-i;
686 color[i].flags = DoRed|DoGreen|DoBlue;
687 color[i].red = red[255-i];
688 color[i].blue = blue[255-i];
689 color[i].green = green[255-i];
692 XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );