]> git.sesse.net Git - vlc/blob - plugins/x11/vout_x11.c
Bon, puisque �a semble commiter sous BeOS, je commite.
[vlc] / plugins / x11 / vout_x11.c
1 /*****************************************************************************
2  * vout_x11.c: X11 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 sextension 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)
233         && (p_vout->i_screen_depth == 8) )
234     {
235         /* FIXME: clear flags ?? */
236     }
237
238     /*
239      * Size change
240      */
241     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
242     {
243         intf_DbgMsg("resizing window\n");
244         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
245
246         /* Resize window */
247         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
248                        p_vout->i_width, p_vout->i_height );
249
250         /* Destroy XImages to change their size */
251         vout_SysEnd( p_vout );
252
253         /* Recreate XImages. If SysInit failed, the thread can't go on. */
254         if( vout_SysInit( p_vout ) )
255         {
256             intf_ErrMsg("error: can't resize display\n");
257             return( 1 );
258         }
259
260         /* Tell the video output thread that it will need to rebuild YUV
261          * tables. This is needed since conversion buffer size may have
262          * changed */
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);
265     }
266
267     return 0;
268 }
269
270 /*****************************************************************************
271  * vout_SysDisplay: 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_SysDisplay( vout_thread_t *p_vout )
277 {
278     if( p_vout->p_sys->b_shm)                                /* XShm is used */
279     {
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 ],
283                      0, 0, 0, 0,
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);
286
287         /* Send the order to the X server */
288         XFlush(p_vout->p_sys->p_display);
289     }
290     else                                /* regular X11 capabilities are used */
291     {
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 ],
294                   0, 0, 0, 0,
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);
297
298         /* Send the order to the X server */
299         XFlush(p_vout->p_sys->p_display);
300     }
301 }
302
303 /*****************************************************************************
304  * vout_SetPalette: 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
308  * to set.
309  *****************************************************************************/
310 void vout_SetPalette( p_vout_thread_t p_vout,
311                       u16 *red, u16 *green, u16 *blue, u16 *transp )
312 {
313     int i;
314     XColor color[255];
315
316     intf_DbgMsg( "Palette change called\n" );
317
318     /* allocate palette */
319     for( i = 0; i < 255; i++ )
320     {
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;
324         color[i].pad = 0;
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];
329     }
330
331     XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );
332 }
333
334 /* following functions are local */
335
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 )
343 {
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 */
348
349     /* Open display */
350     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
351     if( p_vout->p_sys->p_display == NULL )
352     {
353         intf_ErrMsg("error: can't open display %s\n", psz_display );
354         return( 1 );
355     }
356
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 )
362     {
363         intf_Msg("XShm video extension is not available\n");
364     }
365
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 )
369     {
370     case 8:
371         /*
372          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
373          */
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 )
379         {
380             intf_ErrMsg("error: no PseudoColor visual available\n");
381             XCloseDisplay( p_vout->p_sys->p_display );
382             return( 1 );
383         }
384         p_vout->i_bytes_per_pixel = 1;
385
386         /* put the colormap in place */
387         p_vout->p_sys->colormap = *(Colormap *)p_data;
388         break;
389     case 15:
390     case 16:
391     case 24:
392     default:
393         /*
394          * Screen depth is higher than 8bpp. TrueColor visual is used.
395          */
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 )
401         {
402             intf_ErrMsg("error: no TrueColor visual available\n");
403             XCloseDisplay( p_vout->p_sys->p_display );
404             return( 1 );
405         }
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;
409
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
412          * formats. */
413         p_xpixmap_format = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
414
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++ )
418         {
419             if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
420             {
421                 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
422             }
423         }
424         break;
425     }
426     p_vout->p_sys->p_visual = p_xvisual->visual;
427     XFree( p_xvisual );
428
429     /* Create a window */
430     if( X11CreateWindow( p_vout ) )
431     {
432         intf_ErrMsg("error: can't open a window\n");
433         XCloseDisplay( p_vout->p_sys->p_display );
434         return( 1 );
435     }
436     return( 0 );
437 }
438
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 )
446 {
447     /* Destroy colormap */
448     if( p_vout->i_screen_depth == 8 )
449     {
450         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
451     }
452     
453     /* Destroy window */
454     X11DestroyWindow( p_vout );
455
456     /* FIXME: We should close the display here, but X returns an error. */
457     //XCloseDisplay( p_vout->p_sys->p_display );
458 }
459
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 )
469 {
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 */
475
476     /* Prepare window attributes */
477     xwindow_attributes.backing_store = Always;       /* save the hidden part */
478
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,
482                                          0, 0,
483                                          p_vout->i_width, p_vout->i_height,
484                                          0, 0, 0);
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);
489
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);
495
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
498      * window */
499     b_expose = 0;
500     b_map_notify = 0;
501     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
502     do
503     {
504         XNextEvent( p_vout->p_sys->p_display, &xevent);
505         if( (xevent.type == Expose)
506             && (xevent.xexpose.window == p_vout->p_sys->window) )
507         {
508             b_expose = 1;
509         }
510         else if( (xevent.type == MapNotify)
511                  && (xevent.xmap.window == p_vout->p_sys->window) )
512         {
513             b_map_notify = 1;
514         }
515     }
516     while( !( b_expose && b_map_notify ) );
517     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
518
519     /* At this stage, the window is open, displayed, and ready to receive
520      * data */
521     return( 0 );
522 }
523
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 )
530 {
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 );
534 }
535
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 )
542 {
543     byte_t *    pb_data;                          /* image data storage zone */
544     int         i_quantum;                     /* XImage quantum (see below) */
545
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 */
550     {
551         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
552         return( 1 );
553     }
554
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) )
558     {
559         i_quantum = 32;
560     }
561     else
562     {
563         if( !(( p_vout->i_bytes_per_line ) % 16) )
564         {
565             i_quantum = 16;
566         }
567         else
568         {
569             i_quantum = 8;
570         }
571     }
572
573     /* Create XImage */
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 */
578     {
579         intf_ErrMsg( "error: XCreateImage() failed\n" );
580         free( pb_data );
581         return( 1 );
582     }
583
584     return 0;
585 }
586
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
593  * there.
594  *****************************************************************************/
595 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
596                               XShmSegmentInfo *p_shm_info)
597 {
598     /* Create XImage */
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 */
603     {
604         intf_ErrMsg("error: XShmCreateImage() failed\n");
605         return( 1 );
606     }
607
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,
612                                 IPC_CREAT | 0777);
613     if( p_shm_info->shmid < 0)                                      /* error */
614     {
615         intf_ErrMsg("error: can't allocate shared image data (%s)\n",
616                     strerror(errno));
617         XDestroyImage( *pp_ximage );
618         return( 1 );
619     }
620
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 )
624     {                                                               /* error */
625         intf_ErrMsg("error: can't attach shared memory (%s)\n",
626                     strerror(errno));
627         shmctl( p_shm_info->shmid, IPC_RMID, 0 );      /* free shared memory */
628         XDestroyImage( *pp_ximage );
629         return( 1 );
630     }
631
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 );
635
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 */
639     {
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 );
644         return( 1 );
645     }
646
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 );
650     return( 0 );
651 }
652
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 )
660 {
661     if( p_ximage != NULL )
662     {
663         XDestroyImage( p_ximage );                     /* no free() required */
664     }
665 }
666
667 /*****************************************************************************
668  * X11DestroyShmImage
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 )
676 {
677     /* If pointer is NULL, do nothing */
678     if( p_ximage == NULL )
679     {
680         return;
681     }
682
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",
688                      strerror(errno) );
689     }
690 }
691
692