]> git.sesse.net Git - vlc/blob - plugins/gnome/vout_gnome.c
eec5edac452d3c88586821a9442ad3170606c682
[vlc] / plugins / gnome / vout_gnome.c
1 /*****************************************************************************
2  * vout_gnome.c: Gnome video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
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.
12  * 
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #ifdef SYS_BSD
33 #include <sys/types.h>                                     /* typedef ushort */
34 #endif
35
36 #include <sys/shm.h>                                   /* shmget(), shmctl() */
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39 #include <X11/extensions/XShm.h>
40
41 #include "config.h"
42 #include "common.h"
43 #include "threads.h"
44 #include "mtime.h"
45 #include "plugins.h"
46
47 #include "video.h"
48 #include "video_output.h"
49
50 #include "intf_msg.h"
51
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
61 {
62     /* User settings */
63     boolean_t           b_shm;               /* shared memory extension flag */
64
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) */
73
74     /* Display buffers and shared memory information */
75     XImage *            p_ximage[2];                       /* XImage pointer */
76     XShmSegmentInfo     shm_info[2];       /* shared memory zone information */
77 } vout_sys_t;
78
79 /*****************************************************************************
80  * Local prototypes
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
93 /*****************************************************************************
94  * vout_SysCreate: 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_SysCreate( vout_thread_t *p_vout, char *psz_display,
101                     int i_root_window, void *p_data )
102 {
103     /* Allocate structure */
104     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
105     if( p_vout->p_sys == NULL )
106     {
107         intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
108         return( 1 );
109     }
110
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 ) )
116     {
117         intf_ErrMsg("error: can't initialize X11 display\n" );
118         free( p_vout->p_sys );
119         return( 1 );
120     }
121
122     return( 0 );
123 }
124
125 /*****************************************************************************
126  * vout_SysInit: 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_SysInit( vout_thread_t *p_vout )
132 {
133     int i_err;
134
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 )
138     {
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 */
143         {
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] );
150                 i_err = 1;
151             }
152         }
153         if( i_err )                                      /* an error occured */
154         {
155             intf_Msg("XShm video extension deactivated\n" );
156             p_vout->p_sys->b_shm = 0;
157         }
158     }
159
160     /* Create XImages without XShm extension */
161     if( !p_vout->p_sys->b_shm )
162     {
163         if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
164         {
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;
168             return( 1 );
169         }
170         if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
171         {
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;
176             return( 1 );
177         }
178     }
179
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 );
184     return( 0 );
185 }
186
187 /*****************************************************************************
188  * vout_SysEnd: terminate X11 video thread output method
189  *****************************************************************************
190  * Destroy the X11 XImages created by vout_SysInit. It is called at the end of
191  * the thread, but also each time the window is resized.
192  *****************************************************************************/
193 void vout_SysEnd( vout_thread_t *p_vout )
194 {
195     if( p_vout->p_sys->b_shm )                             /* Shm XImages... */
196     {
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] );
201     }
202     else                                          /* ...or regular XImages */
203     {
204         X11DestroyImage( p_vout->p_sys->p_ximage[0] );
205         X11DestroyImage( p_vout->p_sys->p_ximage[1] );
206     }
207 }
208
209 /*****************************************************************************
210  * vout_SysDestroy: destroy X11 video thread output method
211  *****************************************************************************
212  * Terminate an output method created by vout_CreateOutputMethod
213  *****************************************************************************/
214 void vout_SysDestroy( vout_thread_t *p_vout )
215 {
216     X11CloseDisplay( p_vout );
217     free( p_vout->p_sys );
218 }
219
220 /*****************************************************************************
221  * vout_SysManage: 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
225  * error.
226  *****************************************************************************/
227 int vout_SysManage( vout_thread_t *p_vout )
228 {
229     /*
230      * Color/Grayscale or gamma change: in 8bpp, just change the colormap
231      */
232     if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE) && (p_vout->i_screen_depth == 8) )
233     {
234         /* FIXME: clear flags ?? */
235     }
236
237     /*
238      * Size change
239      */
240     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
241     {
242         intf_DbgMsg("resizing window\n");
243         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
244
245         /* Resize window */
246         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
247                        p_vout->i_width, p_vout->i_height );
248
249         /* Destroy XImages to change their size */
250         vout_SysEnd( p_vout );
251
252         /* Recreate XImages. If SysInit failed, the thread can't go on. */
253         if( vout_SysInit( p_vout ) )
254         {
255             intf_ErrMsg("error: can't resize display\n");
256             return( 1 );
257         }
258
259         /* Tell the video output thread that it will need to rebuild YUV
260          * tables. This is needed since convertion buffer size may have changed */
261         p_vout->i_changes |= VOUT_YUV_CHANGE;
262         intf_Msg("Video display resized (%dx%d)\n", p_vout->i_width, p_vout->i_height);
263     }
264
265     return 0;
266 }
267
268 /*****************************************************************************
269  * vout_SysDisplay: displays previously rendered output
270  *****************************************************************************
271  * This function send the currently rendered image to X11 server, wait until
272  * it is displayed and switch the two rendering buffer, preparing next frame.
273  *****************************************************************************/
274 void vout_SysDisplay( vout_thread_t *p_vout )
275 {
276     if( p_vout->p_sys->b_shm)                                /* XShm is used */
277     {
278         /* Display rendered image using shared memory extension */
279         XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
280                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
281                      0, 0, 0, 0,
282                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
283                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
284
285         /* Send the order to the X server */
286         XFlush(p_vout->p_sys->p_display);
287     }
288     else                                /* regular X11 capabilities are used */
289     {
290         XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
291                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
292                   0, 0, 0, 0,
293                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
294                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
295
296         /* Send the order to the X server */
297         XFlush(p_vout->p_sys->p_display);
298     }
299 }
300
301 /*****************************************************************************
302  * vout_SetPalette: sets an 8 bpp palette
303  *****************************************************************************
304  * This function sets the palette given as an argument. It does not return
305  * anything, but could later send information on which colors it was unable
306  * to set.
307  *****************************************************************************/
308 void vout_SetPalette( p_vout_thread_t p_vout,
309                       u16 *red, u16 *green, u16 *blue, u16 *transp )
310 {
311     int i;
312     XColor color[255];
313
314     intf_DbgMsg( "Palette change called\n" );
315
316     /* allocate palette */
317     for( i = 0; i < 255; i++ )
318     {
319         /* kludge: colors are indexed reversely because color 255 seems
320          * to be reserved for black even if we try to set it to white */
321         color[i].pixel = 255-i;
322         color[i].pad = 0;
323         color[i].flags = DoRed|DoGreen|DoBlue;
324         color[i].red = red[255-i];
325         color[i].blue = blue[255-i];
326         color[i].green = green[255-i];
327     }
328
329     XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );
330 }
331
332 /* following functions are local */
333
334 /*****************************************************************************
335  * X11OpenDisplay: open and initialize X11 device
336  *****************************************************************************
337  * Create a window according to video output given size, and set other
338  * properties according to the display properties.
339  *****************************************************************************/
340 static int X11OpenDisplay( vout_thread_t *p_vout, char *psz_display, Window root_window, void *p_data )
341 {
342     XPixmapFormatValues *       p_xpixmap_format;          /* pixmap formats */
343     XVisualInfo *               p_xvisual;           /* visuals informations */
344     XVisualInfo                 xvisual_template;         /* visual template */
345     int                         i_count;                       /* array size */
346
347     /* Open display */
348     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
349     if( p_vout->p_sys->p_display == NULL )
350     {
351         intf_ErrMsg("error: can't open display %s\n", psz_display );
352         return( 1 );
353     }
354
355     /* Initialize structure */
356     p_vout->p_sys->root_window  = root_window;
357     p_vout->p_sys->b_shm        = (XShmQueryExtension(p_vout->p_sys->p_display) == True);
358     p_vout->p_sys->i_screen     = DefaultScreen( p_vout->p_sys->p_display );
359     if( !p_vout->p_sys->b_shm )
360     {
361         intf_Msg("XShm video extension is not available\n");
362     }
363
364     /* Get screen depth */
365     p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
366     switch( p_vout->i_screen_depth )
367     {
368     case 8:
369         /*
370          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
371          */
372         xvisual_template.screen =   p_vout->p_sys->i_screen;
373         xvisual_template.class =    DirectColor;
374         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
375                                     &xvisual_template, &i_count );
376         if( p_xvisual == NULL )
377         {
378             intf_ErrMsg("error: no PseudoColor visual available\n");
379             XCloseDisplay( p_vout->p_sys->p_display );
380             return( 1 );
381         }
382         p_vout->i_bytes_per_pixel = 1;
383
384         /* put the colormap in place */
385         p_vout->p_sys->colormap = *(Colormap *)p_data;
386         break;
387     case 15:
388     case 16:
389     case 24:
390     default:
391         /*
392          * Screen depth is higher than 8bpp. TrueColor visual is used.
393          */
394         xvisual_template.screen =   p_vout->p_sys->i_screen;
395         xvisual_template.class =    TrueColor;
396         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
397                                     &xvisual_template, &i_count );
398         if( p_xvisual == NULL )
399         {
400             intf_ErrMsg("error: no TrueColor visual available\n");
401             XCloseDisplay( p_vout->p_sys->p_display );
402             return( 1 );
403         }
404         p_vout->i_red_mask =        p_xvisual->red_mask;
405         p_vout->i_green_mask =      p_xvisual->green_mask;
406         p_vout->i_blue_mask =       p_xvisual->blue_mask;
407
408         /* There is no difference yet between 3 and 4 Bpp. The only way to find
409          * the actual number of bytes per pixel is to list supported pixmap
410          * formats. */
411         p_xpixmap_format = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
412         p_vout->i_bytes_per_pixel = 0;
413         for( ; i_count--; p_xpixmap_format++ )
414         {
415             if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
416             {
417                 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
418             }
419         }
420         break;
421     }
422     p_vout->p_sys->p_visual = p_xvisual->visual;
423     XFree( p_xvisual );
424
425     /* Create a window */
426     if( X11CreateWindow( p_vout ) )
427     {
428         intf_ErrMsg("error: can't open a window\n");
429         XCloseDisplay( p_vout->p_sys->p_display );
430         return( 1 );
431     }
432     return( 0 );
433 }
434
435 /*****************************************************************************
436  * X11CloseDisplay: close X11 device
437  *****************************************************************************
438  * Returns all resources allocated by X11OpenDisplay and restore the original
439  * state of the display.
440  *****************************************************************************/
441 static void X11CloseDisplay( vout_thread_t *p_vout )
442 {
443     /* Destroy colormap */
444     if( p_vout->i_screen_depth == 8 )
445     {
446         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
447     }
448     
449     /* Destroy window */
450     X11DestroyWindow( p_vout );
451
452     /* FIXME: We should close the display here, but X returns an error. */
453     //XCloseDisplay( p_vout->p_sys->p_display );
454 }
455
456 /*****************************************************************************
457  * X11CreateWindow: create X11 vout window
458  *****************************************************************************
459  * The video output window will be created. Normally, this window is wether
460  * full screen or part of a parent window. Therefore, it does not need a
461  * title or other hints. Thery are still supplied in case the window would be
462  * spawned as a standalone one by the interface.
463  *****************************************************************************/
464 static int X11CreateWindow( vout_thread_t *p_vout )
465 {
466     XSetWindowAttributes    xwindow_attributes;         /* window attributes */
467     XGCValues               xgcvalues;      /* graphic context configuration */
468     XEvent                  xevent;                          /* first events */
469     boolean_t               b_expose;             /* 'expose' event received */
470     boolean_t               b_map_notify;     /* 'map_notify' event received */
471
472     /* Prepare window attributes */
473     xwindow_attributes.backing_store = Always;       /* save the hidden part */
474
475     /* Create the window and set hints */
476     p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
477                                          p_vout->p_sys->root_window,
478                                          0, 0,
479                                          p_vout->i_width, p_vout->i_height,
480                                          0, 0, 0);
481     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
482                   ExposureMask | StructureNotifyMask );
483     XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
484                              CWBackingStore, &xwindow_attributes);
485
486     /* Creation of a graphic context that doesn't generate a GraphicsExpose event
487        when using functions like XCopyArea */
488     xgcvalues.graphics_exposures = False;
489     p_vout->p_sys->gc =  XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
490                                     GCGraphicsExposures, &xgcvalues);
491
492     /* Send orders to server, and wait until window is displayed - two events
493      * must be received: a MapNotify event, an Expose event allowing drawing in the
494      * window */
495     b_expose = 0;
496     b_map_notify = 0;
497     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
498     do
499     {
500         XNextEvent( p_vout->p_sys->p_display, &xevent);
501         if( (xevent.type == Expose)
502             && (xevent.xexpose.window == p_vout->p_sys->window) )
503         {
504             b_expose = 1;
505         }
506         else if( (xevent.type == MapNotify)
507                  && (xevent.xmap.window == p_vout->p_sys->window) )
508         {
509             b_map_notify = 1;
510         }
511     }
512     while( !( b_expose && b_map_notify ) );
513     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
514
515     /* At this stage, the window is open, displayed, and ready to receive
516      * data */
517     return( 0 );
518 }
519
520 /*****************************************************************************
521  * X11DestroyWindow: destroy X11 window
522  *****************************************************************************
523  * Destroy an X11 window created by vout_CreateWindow
524  *****************************************************************************/
525 static void X11DestroyWindow( vout_thread_t *p_vout )
526 {
527     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
528     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
529     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
530 }
531
532 /*****************************************************************************
533  * X11CreateImage: create an XImage
534  *****************************************************************************
535  * Create a simple XImage used as a buffer.
536  *****************************************************************************/
537 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
538 {
539     byte_t *    pb_data;                          /* image data storage zone */
540     int         i_quantum;                     /* XImage quantum (see below) */
541
542     /* Allocate memory for image */
543     p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
544     pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
545     if( !pb_data )                                                  /* error */
546     {
547         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
548         return( 1 );
549     }
550
551     /* Optimize the quantum of a scanline regarding its size - the quantum is
552        a diviser of the number of bits between the start of two scanlines. */
553     if( !(( p_vout->i_bytes_per_line ) % 32) )
554     {
555         i_quantum = 32;
556     }
557     else
558     {
559         if( !(( p_vout->i_bytes_per_line ) % 16) )
560         {
561             i_quantum = 16;
562         }
563         else
564         {
565             i_quantum = 8;
566         }
567     }
568
569     /* Create XImage */
570     *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
571                                p_vout->i_screen_depth, ZPixmap, 0, pb_data,
572                                p_vout->i_width, p_vout->i_height, i_quantum, 0);
573     if(! *pp_ximage )                                               /* error */
574     {
575         intf_ErrMsg( "error: XCreateImage() failed\n" );
576         free( pb_data );
577         return( 1 );
578     }
579
580     return 0;
581 }
582
583 /*****************************************************************************
584  * X11CreateShmImage: create an XImage using shared memory extension
585  *****************************************************************************
586  * Prepare an XImage for DisplayX11ShmImage function.
587  * The order of the operations respects the recommandations of the mit-shm
588  * document by J.Corbet and K.Packard. Most of the parameters were copied from
589  * there.
590  *****************************************************************************/
591 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
592                               XShmSegmentInfo *p_shm_info)
593 {
594     /* Create XImage */
595     *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
596                                   p_vout->i_screen_depth, ZPixmap, 0,
597                                   p_shm_info, p_vout->i_width, p_vout->i_height );
598     if(! *pp_ximage )                                               /* error */
599     {
600         intf_ErrMsg("error: XShmCreateImage() failed\n");
601         return( 1 );
602     }
603
604     /* Allocate shared memory segment - 0777 set the access permission
605      * rights (like umask), they are not yet supported by X servers */
606     p_shm_info->shmid = shmget( IPC_PRIVATE,
607                                 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
608                                 IPC_CREAT | 0777);
609     if( p_shm_info->shmid < 0)                                      /* error */
610     {
611         intf_ErrMsg("error: can't allocate shared image data (%s)\n",
612                     strerror(errno));
613         XDestroyImage( *pp_ximage );
614         return( 1 );
615     }
616
617     /* Attach shared memory segment to process (read/write) */
618     p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
619     if(! p_shm_info->shmaddr )
620     {                                                               /* error */
621         intf_ErrMsg("error: can't attach shared memory (%s)\n",
622                     strerror(errno));
623         shmctl( p_shm_info->shmid, IPC_RMID, 0 );      /* free shared memory */
624         XDestroyImage( *pp_ximage );
625         return( 1 );
626     }
627
628     /* Mark the shm segment to be removed when there will be no more
629      * attachements, so it is automatic on process exit or after shmdt */
630     shmctl( p_shm_info->shmid, IPC_RMID, 0 );
631
632     /* Attach shared memory segment to X server (read only) */
633     p_shm_info->readOnly = True;
634     if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False )    /* error */
635     {
636         intf_ErrMsg("error: can't attach shared memory to X11 server\n");
637         shmdt( p_shm_info->shmaddr );     /* detach shared memory from process
638                                            * and automatic free                */
639         XDestroyImage( *pp_ximage );
640         return( 1 );
641     }
642
643     /* Send image to X server. This instruction is required, since having
644      * built a Shm XImage and not using it causes an error on XCloseDisplay */
645     XFlush( p_vout->p_sys->p_display );
646     return( 0 );
647 }
648
649 /*****************************************************************************
650  * X11DestroyImage: destroy an XImage
651  *****************************************************************************
652  * Destroy XImage AND associated data. If pointer is NULL, the image won't be
653  * destroyed (see vout_ManageOutputMethod())
654  *****************************************************************************/
655 static void X11DestroyImage( XImage *p_ximage )
656 {
657     if( p_ximage != NULL )
658     {
659         XDestroyImage( p_ximage );                     /* no free() required */
660     }
661 }
662
663 /*****************************************************************************
664  * X11DestroyShmImage
665  *****************************************************************************
666  * Destroy XImage AND associated data. Detach shared memory segment from
667  * server and process, then free it. If pointer is NULL, the image won't be
668  * destroyed (see vout_ManageOutputMethod())
669  *****************************************************************************/
670 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
671                                 XShmSegmentInfo *p_shm_info )
672 {
673     /* If pointer is NULL, do nothing */
674     if( p_ximage == NULL )
675     {
676         return;
677     }
678
679     XShmDetach( p_vout->p_sys->p_display, p_shm_info );     /* detach from server */
680     XDestroyImage( p_ximage );
681     if( shmdt( p_shm_info->shmaddr ) )  /* detach shared memory from process */
682     {                                   /* also automatic freeing...         */
683         intf_ErrMsg( "error: can't detach shared memory (%s)\n",
684                      strerror(errno) );
685     }
686 }
687
688