1 /*****************************************************************************
2 * vout_gnome.c: Gnome 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,
83 Window root_window, void *p_data );
84 static void X11CloseDisplay ( vout_thread_t *p_vout );
85 static int X11CreateWindow ( vout_thread_t *p_vout );
86 static void X11DestroyWindow ( vout_thread_t *p_vout );
87 static int X11CreateImage ( vout_thread_t *p_vout, XImage **pp_ximage );
88 static void X11DestroyImage ( XImage *p_ximage );
89 static int X11CreateShmImage ( vout_thread_t *p_vout, XImage **pp_ximage,
90 XShmSegmentInfo *p_shm_info );
91 static void X11DestroyShmImage ( vout_thread_t *p_vout, XImage *p_ximage,
92 XShmSegmentInfo *p_shm_info );
94 /*****************************************************************************
95 * vout_GnomeCreate: allocate X11 video thread output method
96 *****************************************************************************
97 * This function allocate and initialize a X11 vout method. It uses some of the
98 * vout properties to choose the window size, and change them according to the
99 * actual properties of the display.
100 *****************************************************************************/
101 int vout_GnomeCreate( vout_thread_t *p_vout, char *psz_display,
102 int i_root_window, void *p_data )
104 /* Allocate structure */
105 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
106 if( p_vout->p_sys == NULL )
108 intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
112 /* Open and initialize device. This function issues its own error messages.
113 * Since XLib is usually not thread-safe, we can't use the same display
114 * pointer than the interface or another thread. However, the root window
115 * id is still valid. */
116 if( X11OpenDisplay( p_vout, psz_display, i_root_window, p_data ) )
118 intf_ErrMsg("error: can't initialize X11 display\n" );
119 free( p_vout->p_sys );
126 /*****************************************************************************
127 * vout_GnomeInit: initialize X11 video thread output method
128 *****************************************************************************
129 * This function create the XImages needed by the output thread. It is called
130 * at the beginning of the thread, but also each time the window is resized.
131 *****************************************************************************/
132 int vout_GnomeInit( vout_thread_t *p_vout )
136 /* Create XImages using XShm extension - on failure, fall back to regular
137 * way (and destroy the first image if it was created successfully) */
138 if( p_vout->p_sys->b_shm )
140 /* Create first image */
141 i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
142 &p_vout->p_sys->shm_info[0] );
143 if( !i_err ) /* first image has been created */
145 /* Create second image */
146 if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
147 &p_vout->p_sys->shm_info[1] ) )
148 { /* error creating the second image */
149 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
150 &p_vout->p_sys->shm_info[0] );
154 if( i_err ) /* an error occured */
156 intf_Msg("XShm video extension deactivated\n" );
157 p_vout->p_sys->b_shm = 0;
161 /* Create XImages without XShm extension */
162 if( !p_vout->p_sys->b_shm )
164 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
166 intf_ErrMsg("error: can't create images\n");
167 p_vout->p_sys->p_ximage[0] = NULL;
168 p_vout->p_sys->p_ximage[1] = NULL;
171 if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
173 intf_ErrMsg("error: can't create images\n");
174 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
175 p_vout->p_sys->p_ximage[0] = NULL;
176 p_vout->p_sys->p_ximage[1] = NULL;
181 /* Set bytes per line and initialize buffers */
182 p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
183 vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
184 p_vout->p_sys->p_ximage[ 1 ]->data );
188 /*****************************************************************************
189 * vout_GnomeEnd: terminate X11 video thread output method
190 *****************************************************************************
191 * Destroy the X11 XImages created by vout_SysInit. It is called at the end of
192 * the thread, but also each time the window is resized.
193 *****************************************************************************/
194 void vout_GnomeEnd( vout_thread_t *p_vout )
196 if( p_vout->p_sys->b_shm ) /* Shm XImages... */
198 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
199 &p_vout->p_sys->shm_info[0] );
200 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
201 &p_vout->p_sys->shm_info[1] );
203 else /* ...or regular XImages */
205 X11DestroyImage( p_vout->p_sys->p_ximage[0] );
206 X11DestroyImage( p_vout->p_sys->p_ximage[1] );
210 /*****************************************************************************
211 * vout_GnomeDestroy: destroy X11 video thread output method
212 *****************************************************************************
213 * Terminate an output method created by vout_CreateOutputMethod
214 *****************************************************************************/
215 void vout_GnomeDestroy( vout_thread_t *p_vout )
217 X11CloseDisplay( p_vout );
218 free( p_vout->p_sys );
221 /*****************************************************************************
222 * vout_GnomeManage: handle X11 events
223 *****************************************************************************
224 * This function should be called regularly by video output thread. It manages
225 * X11 events and allows window resizing. It returns a non null value on
227 *****************************************************************************/
228 int vout_GnomeManage( vout_thread_t *p_vout )
231 * Color/Grayscale or gamma change: in 8bpp, just change the colormap
233 if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE) && (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_GnomeEnd( p_vout );
253 /* Recreate XImages. If SysInit failed, the thread can't go on. */
254 if( vout_GnomeInit( 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 convertion buffer size may have changed */
262 p_vout->i_changes |= VOUT_YUV_CHANGE;
263 intf_Msg("Video display resized (%dx%d)\n", p_vout->i_width, p_vout->i_height);
269 /*****************************************************************************
270 * vout_GnomeDisplay: displays previously rendered output
271 *****************************************************************************
272 * This function send the currently rendered image to X11 server, wait until
273 * it is displayed and switch the two rendering buffer, preparing next frame.
274 *****************************************************************************/
275 void vout_GnomeDisplay( vout_thread_t *p_vout )
277 if( p_vout->p_sys->b_shm) /* XShm is used */
279 /* Display rendered image using shared memory extension */
280 XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
281 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
283 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
284 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
286 /* Send the order to the X server */
287 XFlush(p_vout->p_sys->p_display);
289 else /* regular X11 capabilities are used */
291 XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
292 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
294 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
295 p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
297 /* Send the order to the X server */
298 XFlush(p_vout->p_sys->p_display);
302 /*****************************************************************************
303 * vout_GnomeSetPalette: sets an 8 bpp palette
304 *****************************************************************************
305 * This function sets the palette given as an argument. It does not return
306 * anything, but could later send information on which colors it was unable
308 *****************************************************************************/
309 void vout_GnomeSetPalette( p_vout_thread_t p_vout,
310 u16 *red, u16 *green, u16 *blue, u16 *transp )
315 intf_DbgMsg( "Palette change called\n" );
317 /* allocate palette */
318 for( i = 0; i < 255; i++ )
320 /* kludge: colors are indexed reversely because color 255 seems
321 * to be reserved for black even if we try to set it to white */
322 color[i].pixel = 255-i;
324 color[i].flags = DoRed|DoGreen|DoBlue;
325 color[i].red = red[255-i];
326 color[i].blue = blue[255-i];
327 color[i].green = green[255-i];
330 XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );
333 /* following functions are local */
335 /*****************************************************************************
336 * X11OpenDisplay: open and initialize X11 device
337 *****************************************************************************
338 * Create a window according to video output given size, and set other
339 * properties according to the display properties.
340 *****************************************************************************/
341 static int X11OpenDisplay( vout_thread_t *p_vout, char *psz_display, Window root_window, void *p_data )
343 XPixmapFormatValues * p_xpixmap_format; /* pixmap formats */
344 XVisualInfo * p_xvisual; /* visuals informations */
345 XVisualInfo xvisual_template; /* visual template */
346 int i_count; /* array size */
349 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
350 if( p_vout->p_sys->p_display == NULL )
352 intf_ErrMsg("error: can't open display %s\n", psz_display );
356 /* Initialize structure */
357 p_vout->p_sys->root_window = root_window;
358 p_vout->p_sys->b_shm = (XShmQueryExtension(p_vout->p_sys->p_display) == True);
359 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
360 if( !p_vout->p_sys->b_shm )
362 intf_Msg("XShm video extension is not available\n");
365 /* Get screen depth */
366 p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
367 switch( p_vout->i_screen_depth )
371 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
373 xvisual_template.screen = p_vout->p_sys->i_screen;
374 xvisual_template.class = DirectColor;
375 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
376 &xvisual_template, &i_count );
377 if( p_xvisual == NULL )
379 intf_ErrMsg("error: no PseudoColor visual available\n");
380 XCloseDisplay( p_vout->p_sys->p_display );
383 p_vout->i_bytes_per_pixel = 1;
385 /* put the colormap in place */
386 p_vout->p_sys->colormap = *(Colormap *)p_data;
393 * Screen depth is higher than 8bpp. TrueColor visual is used.
395 xvisual_template.screen = p_vout->p_sys->i_screen;
396 xvisual_template.class = TrueColor;
397 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
398 &xvisual_template, &i_count );
399 if( p_xvisual == NULL )
401 intf_ErrMsg("error: no TrueColor visual available\n");
402 XCloseDisplay( p_vout->p_sys->p_display );
405 p_vout->i_red_mask = p_xvisual->red_mask;
406 p_vout->i_green_mask = p_xvisual->green_mask;
407 p_vout->i_blue_mask = p_xvisual->blue_mask;
409 /* There is no difference yet between 3 and 4 Bpp. The only way to find
410 * the actual number of bytes per pixel is to list supported pixmap
412 p_xpixmap_format = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
413 p_vout->i_bytes_per_pixel = 0;
414 for( ; i_count--; p_xpixmap_format++ )
416 if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
418 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
423 p_vout->p_sys->p_visual = p_xvisual->visual;
426 /* Create a window */
427 if( X11CreateWindow( p_vout ) )
429 intf_ErrMsg("error: can't open a window\n");
430 XCloseDisplay( p_vout->p_sys->p_display );
436 /*****************************************************************************
437 * X11CloseDisplay: close X11 device
438 *****************************************************************************
439 * Returns all resources allocated by X11OpenDisplay and restore the original
440 * state of the display.
441 *****************************************************************************/
442 static void X11CloseDisplay( vout_thread_t *p_vout )
444 /* Destroy colormap */
445 if( p_vout->i_screen_depth == 8 )
447 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
451 X11DestroyWindow( p_vout );
453 /* FIXME: We should close the display here, but X returns an error. */
454 //XCloseDisplay( p_vout->p_sys->p_display );
457 /*****************************************************************************
458 * X11CreateWindow: create X11 vout window
459 *****************************************************************************
460 * The video output window will be created. Normally, this window is wether
461 * full screen or part of a parent window. Therefore, it does not need a
462 * title or other hints. Thery are still supplied in case the window would be
463 * spawned as a standalone one by the interface.
464 *****************************************************************************/
465 static int X11CreateWindow( vout_thread_t *p_vout )
467 XSetWindowAttributes xwindow_attributes; /* window attributes */
468 XGCValues xgcvalues; /* graphic context configuration */
469 XEvent xevent; /* first events */
470 boolean_t b_expose; /* 'expose' event received */
471 boolean_t b_map_notify; /* 'map_notify' event received */
473 /* Prepare window attributes */
474 xwindow_attributes.backing_store = Always; /* save the hidden part */
476 /* Create the window and set hints */
477 p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
478 p_vout->p_sys->root_window,
480 p_vout->i_width, p_vout->i_height,
482 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
483 ExposureMask | StructureNotifyMask );
484 XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
485 CWBackingStore, &xwindow_attributes);
487 /* Creation of a graphic context that doesn't generate a GraphicsExpose event
488 when using functions like XCopyArea */
489 xgcvalues.graphics_exposures = False;
490 p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
491 GCGraphicsExposures, &xgcvalues);
493 /* Send orders to server, and wait until window is displayed - two events
494 * must be received: a MapNotify event, an Expose event allowing drawing in the
498 XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
501 XNextEvent( p_vout->p_sys->p_display, &xevent);
502 if( (xevent.type == Expose)
503 && (xevent.xexpose.window == p_vout->p_sys->window) )
507 else if( (xevent.type == MapNotify)
508 && (xevent.xmap.window == p_vout->p_sys->window) )
513 while( !( b_expose && b_map_notify ) );
514 XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
516 /* At this stage, the window is open, displayed, and ready to receive
521 /*****************************************************************************
522 * X11DestroyWindow: destroy X11 window
523 *****************************************************************************
524 * Destroy an X11 window created by vout_CreateWindow
525 *****************************************************************************/
526 static void X11DestroyWindow( vout_thread_t *p_vout )
528 XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
529 XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
530 XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
533 /*****************************************************************************
534 * X11CreateImage: create an XImage
535 *****************************************************************************
536 * Create a simple XImage used as a buffer.
537 *****************************************************************************/
538 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
540 byte_t * pb_data; /* image data storage zone */
541 int i_quantum; /* XImage quantum (see below) */
543 /* Allocate memory for image */
544 p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
545 pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
546 if( !pb_data ) /* error */
548 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
552 /* Optimize the quantum of a scanline regarding its size - the quantum is
553 a diviser of the number of bits between the start of two scanlines. */
554 if( !(( p_vout->i_bytes_per_line ) % 32) )
560 if( !(( p_vout->i_bytes_per_line ) % 16) )
571 *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
572 p_vout->i_screen_depth, ZPixmap, 0, pb_data,
573 p_vout->i_width, p_vout->i_height, i_quantum, 0);
574 if(! *pp_ximage ) /* error */
576 intf_ErrMsg( "error: XCreateImage() failed\n" );
584 /*****************************************************************************
585 * X11CreateShmImage: create an XImage using shared memory extension
586 *****************************************************************************
587 * Prepare an XImage for DisplayX11ShmImage function.
588 * The order of the operations respects the recommandations of the mit-shm
589 * document by J.Corbet and K.Packard. Most of the parameters were copied from
591 *****************************************************************************/
592 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
593 XShmSegmentInfo *p_shm_info)
596 *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
597 p_vout->i_screen_depth, ZPixmap, 0,
598 p_shm_info, p_vout->i_width, p_vout->i_height );
599 if(! *pp_ximage ) /* error */
601 intf_ErrMsg("error: XShmCreateImage() failed\n");
605 /* Allocate shared memory segment - 0777 set the access permission
606 * rights (like umask), they are not yet supported by X servers */
607 p_shm_info->shmid = shmget( IPC_PRIVATE,
608 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
610 if( p_shm_info->shmid < 0) /* error */
612 intf_ErrMsg("error: can't allocate shared image data (%s)\n",
614 XDestroyImage( *pp_ximage );
618 /* Attach shared memory segment to process (read/write) */
619 p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
620 if(! p_shm_info->shmaddr )
622 intf_ErrMsg("error: can't attach shared memory (%s)\n",
624 shmctl( p_shm_info->shmid, IPC_RMID, 0 ); /* free shared memory */
625 XDestroyImage( *pp_ximage );
629 /* Mark the shm segment to be removed when there will be no more
630 * attachements, so it is automatic on process exit or after shmdt */
631 shmctl( p_shm_info->shmid, IPC_RMID, 0 );
633 /* Attach shared memory segment to X server (read only) */
634 p_shm_info->readOnly = True;
635 if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False ) /* error */
637 intf_ErrMsg("error: can't attach shared memory to X11 server\n");
638 shmdt( p_shm_info->shmaddr ); /* detach shared memory from process
639 * and automatic free */
640 XDestroyImage( *pp_ximage );
644 /* Send image to X server. This instruction is required, since having
645 * built a Shm XImage and not using it causes an error on XCloseDisplay */
646 XFlush( p_vout->p_sys->p_display );
650 /*****************************************************************************
651 * X11DestroyImage: destroy an XImage
652 *****************************************************************************
653 * Destroy XImage AND associated data. If pointer is NULL, the image won't be
654 * destroyed (see vout_ManageOutputMethod())
655 *****************************************************************************/
656 static void X11DestroyImage( XImage *p_ximage )
658 if( p_ximage != NULL )
660 XDestroyImage( p_ximage ); /* no free() required */
664 /*****************************************************************************
666 *****************************************************************************
667 * Destroy XImage AND associated data. Detach shared memory segment from
668 * server and process, then free it. If pointer is NULL, the image won't be
669 * destroyed (see vout_ManageOutputMethod())
670 *****************************************************************************/
671 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
672 XShmSegmentInfo *p_shm_info )
674 /* If pointer is NULL, do nothing */
675 if( p_ximage == NULL )
680 XShmDetach( p_vout->p_sys->p_display, p_shm_info ); /* detach from server */
681 XDestroyImage( p_ximage );
682 if( shmdt( p_shm_info->shmaddr ) ) /* detach shared memory from process */
683 { /* also automatic freeing... */
684 intf_ErrMsg( "error: can't detach shared memory (%s)\n",