]> git.sesse.net Git - vlc/blob - plugins/gnome/vout_gnome.c
. autod�tection des plugins
[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,
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 );
93
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 )
103 {
104     /* Allocate structure */
105     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
106     if( p_vout->p_sys == NULL )
107     {
108         intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
109         return( 1 );
110     }
111
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 ) )
117     {
118         intf_ErrMsg("error: can't initialize X11 display\n" );
119         free( p_vout->p_sys );
120         return( 1 );
121     }
122
123     return( 0 );
124 }
125
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 )
133 {
134     int i_err;
135
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 )
139     {
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 */
144         {
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] );
151                 i_err = 1;
152             }
153         }
154         if( i_err )                                      /* an error occured */
155         {
156             intf_Msg("XShm video extension deactivated\n" );
157             p_vout->p_sys->b_shm = 0;
158         }
159     }
160
161     /* Create XImages without XShm extension */
162     if( !p_vout->p_sys->b_shm )
163     {
164         if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
165         {
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;
169             return( 1 );
170         }
171         if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
172         {
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;
177             return( 1 );
178         }
179     }
180
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 );
185     return( 0 );
186 }
187
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 )
195 {
196     if( p_vout->p_sys->b_shm )                             /* Shm XImages... */
197     {
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] );
202     }
203     else                                          /* ...or regular XImages */
204     {
205         X11DestroyImage( p_vout->p_sys->p_ximage[0] );
206         X11DestroyImage( p_vout->p_sys->p_ximage[1] );
207     }
208 }
209
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 )
216 {
217     X11CloseDisplay( p_vout );
218     free( p_vout->p_sys );
219 }
220
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
226  * error.
227  *****************************************************************************/
228 int vout_GnomeManage( vout_thread_t *p_vout )
229 {
230     /*
231      * Color/Grayscale or gamma change: in 8bpp, just change the colormap
232      */
233     if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE) && (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_GnomeEnd( p_vout );
252
253         /* Recreate XImages. If SysInit failed, the thread can't go on. */
254         if( vout_GnomeInit( 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 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);
264     }
265
266     return 0;
267 }
268
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 )
276 {
277     if( p_vout->p_sys->b_shm)                                /* XShm is used */
278     {
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 ],
282                      0, 0, 0, 0,
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);
285
286         /* Send the order to the X server */
287         XFlush(p_vout->p_sys->p_display);
288     }
289     else                                /* regular X11 capabilities are used */
290     {
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 ],
293                   0, 0, 0, 0,
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);
296
297         /* Send the order to the X server */
298         XFlush(p_vout->p_sys->p_display);
299     }
300 }
301
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
307  * to set.
308  *****************************************************************************/
309 void vout_GnomeSetPalette( p_vout_thread_t p_vout,
310                            u16 *red, u16 *green, u16 *blue, u16 *transp )
311 {
312     int i;
313     XColor color[255];
314
315     intf_DbgMsg( "Palette change called\n" );
316
317     /* allocate palette */
318     for( i = 0; i < 255; i++ )
319     {
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;
323         color[i].pad = 0;
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];
328     }
329
330     XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );
331 }
332
333 /* following functions are local */
334
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 )
342 {
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 */
347
348     /* Open display */
349     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
350     if( p_vout->p_sys->p_display == NULL )
351     {
352         intf_ErrMsg("error: can't open display %s\n", psz_display );
353         return( 1 );
354     }
355
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 )
361     {
362         intf_Msg("XShm video extension is not available\n");
363     }
364
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 )
368     {
369     case 8:
370         /*
371          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
372          */
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 )
378         {
379             intf_ErrMsg("error: no PseudoColor visual available\n");
380             XCloseDisplay( p_vout->p_sys->p_display );
381             return( 1 );
382         }
383         p_vout->i_bytes_per_pixel = 1;
384
385         /* put the colormap in place */
386         p_vout->p_sys->colormap = *(Colormap *)p_data;
387         break;
388     case 15:
389     case 16:
390     case 24:
391     default:
392         /*
393          * Screen depth is higher than 8bpp. TrueColor visual is used.
394          */
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 )
400         {
401             intf_ErrMsg("error: no TrueColor visual available\n");
402             XCloseDisplay( p_vout->p_sys->p_display );
403             return( 1 );
404         }
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;
408
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
411          * formats. */
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++ )
415         {
416             if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
417             {
418                 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
419             }
420         }
421         break;
422     }
423     p_vout->p_sys->p_visual = p_xvisual->visual;
424     XFree( p_xvisual );
425
426     /* Create a window */
427     if( X11CreateWindow( p_vout ) )
428     {
429         intf_ErrMsg("error: can't open a window\n");
430         XCloseDisplay( p_vout->p_sys->p_display );
431         return( 1 );
432     }
433     return( 0 );
434 }
435
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 )
443 {
444     /* Destroy colormap */
445     if( p_vout->i_screen_depth == 8 )
446     {
447         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
448     }
449     
450     /* Destroy window */
451     X11DestroyWindow( p_vout );
452
453     /* FIXME: We should close the display here, but X returns an error. */
454     //XCloseDisplay( p_vout->p_sys->p_display );
455 }
456
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 )
466 {
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 */
472
473     /* Prepare window attributes */
474     xwindow_attributes.backing_store = Always;       /* save the hidden part */
475
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,
479                                          0, 0,
480                                          p_vout->i_width, p_vout->i_height,
481                                          0, 0, 0);
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);
486
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);
492
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
495      * window */
496     b_expose = 0;
497     b_map_notify = 0;
498     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
499     do
500     {
501         XNextEvent( p_vout->p_sys->p_display, &xevent);
502         if( (xevent.type == Expose)
503             && (xevent.xexpose.window == p_vout->p_sys->window) )
504         {
505             b_expose = 1;
506         }
507         else if( (xevent.type == MapNotify)
508                  && (xevent.xmap.window == p_vout->p_sys->window) )
509         {
510             b_map_notify = 1;
511         }
512     }
513     while( !( b_expose && b_map_notify ) );
514     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
515
516     /* At this stage, the window is open, displayed, and ready to receive
517      * data */
518     return( 0 );
519 }
520
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 )
527 {
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 );
531 }
532
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 )
539 {
540     byte_t *    pb_data;                          /* image data storage zone */
541     int         i_quantum;                     /* XImage quantum (see below) */
542
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 */
547     {
548         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
549         return( 1 );
550     }
551
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) )
555     {
556         i_quantum = 32;
557     }
558     else
559     {
560         if( !(( p_vout->i_bytes_per_line ) % 16) )
561         {
562             i_quantum = 16;
563         }
564         else
565         {
566             i_quantum = 8;
567         }
568     }
569
570     /* Create XImage */
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 */
575     {
576         intf_ErrMsg( "error: XCreateImage() failed\n" );
577         free( pb_data );
578         return( 1 );
579     }
580
581     return 0;
582 }
583
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
590  * there.
591  *****************************************************************************/
592 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
593                               XShmSegmentInfo *p_shm_info)
594 {
595     /* Create XImage */
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 */
600     {
601         intf_ErrMsg("error: XShmCreateImage() failed\n");
602         return( 1 );
603     }
604
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,
609                                 IPC_CREAT | 0777);
610     if( p_shm_info->shmid < 0)                                      /* error */
611     {
612         intf_ErrMsg("error: can't allocate shared image data (%s)\n",
613                     strerror(errno));
614         XDestroyImage( *pp_ximage );
615         return( 1 );
616     }
617
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 )
621     {                                                               /* error */
622         intf_ErrMsg("error: can't attach shared memory (%s)\n",
623                     strerror(errno));
624         shmctl( p_shm_info->shmid, IPC_RMID, 0 );      /* free shared memory */
625         XDestroyImage( *pp_ximage );
626         return( 1 );
627     }
628
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 );
632
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 */
636     {
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 );
641         return( 1 );
642     }
643
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 );
647     return( 0 );
648 }
649
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 )
657 {
658     if( p_ximage != NULL )
659     {
660         XDestroyImage( p_ximage );                     /* no free() required */
661     }
662 }
663
664 /*****************************************************************************
665  * X11DestroyShmImage
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 )
673 {
674     /* If pointer is NULL, do nothing */
675     if( p_ximage == NULL )
676     {
677         return;
678     }
679
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",
685                      strerror(errno) );
686     }
687 }
688
689