]> git.sesse.net Git - vlc/blob - plugins/gnome/vout_gnome.c
. should compile & run on Solaris with ./configure --disable-dsp
[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", 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" );
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" );
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");
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");
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");
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");
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)", 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" );
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", 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");
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");
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");
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
415         /* Under XFree 4, XListPixmapFormats returns a list of available
416          * formats under each screen depth ; so we have to select the one
417          * for the current display. */
418         for( ; i_count--; p_xpixmap_format++ )
419         {
420             if( p_vout->i_screen_depth == p_xpixmap_format->depth )
421             {
422                 if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
423                 {
424                     p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
425                 }
426             }
427         }
428         break;
429     }
430     p_vout->p_sys->p_visual = p_xvisual->visual;
431     XFree( p_xvisual );
432
433     /* Create a window */
434     if( X11CreateWindow( p_vout ) )
435     {
436         intf_ErrMsg("error: can't open a window");
437         XCloseDisplay( p_vout->p_sys->p_display );
438         return( 1 );
439     }
440     return( 0 );
441 }
442
443 /*****************************************************************************
444  * X11CloseDisplay: close X11 device
445  *****************************************************************************
446  * Returns all resources allocated by X11OpenDisplay and restore the original
447  * state of the display.
448  *****************************************************************************/
449 static void X11CloseDisplay( vout_thread_t *p_vout )
450 {
451     /* Destroy colormap */
452     if( p_vout->i_screen_depth == 8 )
453     {
454         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
455     }
456     
457     /* Destroy window */
458     X11DestroyWindow( p_vout );
459
460     /* FIXME: We should close the display here, but X returns an error. */
461     //XCloseDisplay( p_vout->p_sys->p_display );
462 }
463
464 /*****************************************************************************
465  * X11CreateWindow: create X11 vout window
466  *****************************************************************************
467  * The video output window will be created. Normally, this window is wether
468  * full screen or part of a parent window. Therefore, it does not need a
469  * title or other hints. Thery are still supplied in case the window would be
470  * spawned as a standalone one by the interface.
471  *****************************************************************************/
472 static int X11CreateWindow( vout_thread_t *p_vout )
473 {
474     XSetWindowAttributes    xwindow_attributes;         /* window attributes */
475     XGCValues               xgcvalues;      /* graphic context configuration */
476     XEvent                  xevent;                          /* first events */
477     boolean_t               b_expose;             /* 'expose' event received */
478     boolean_t               b_map_notify;     /* 'map_notify' event received */
479
480     /* Prepare window attributes */
481     xwindow_attributes.backing_store = Always;       /* save the hidden part */
482
483     /* Create the window and set hints */
484     p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
485                                          p_vout->p_sys->root_window,
486                                          0, 0,
487                                          p_vout->i_width, p_vout->i_height,
488                                          0, 0, 0);
489     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
490                   ExposureMask | StructureNotifyMask );
491     XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
492                              CWBackingStore, &xwindow_attributes);
493
494     /* Creation of a graphic context that doesn't generate a GraphicsExpose event
495        when using functions like XCopyArea */
496     xgcvalues.graphics_exposures = False;
497     p_vout->p_sys->gc =  XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
498                                     GCGraphicsExposures, &xgcvalues);
499
500     /* Send orders to server, and wait until window is displayed - two events
501      * must be received: a MapNotify event, an Expose event allowing drawing in the
502      * window */
503     b_expose = 0;
504     b_map_notify = 0;
505     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
506     do
507     {
508         XNextEvent( p_vout->p_sys->p_display, &xevent);
509         if( (xevent.type == Expose)
510             && (xevent.xexpose.window == p_vout->p_sys->window) )
511         {
512             b_expose = 1;
513         }
514         else if( (xevent.type == MapNotify)
515                  && (xevent.xmap.window == p_vout->p_sys->window) )
516         {
517             b_map_notify = 1;
518         }
519     }
520     while( !( b_expose && b_map_notify ) );
521     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
522
523     /* At this stage, the window is open, displayed, and ready to receive
524      * data */
525     return( 0 );
526 }
527
528 /*****************************************************************************
529  * X11DestroyWindow: destroy X11 window
530  *****************************************************************************
531  * Destroy an X11 window created by vout_CreateWindow
532  *****************************************************************************/
533 static void X11DestroyWindow( vout_thread_t *p_vout )
534 {
535     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
536     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
537     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
538 }
539
540 /*****************************************************************************
541  * X11CreateImage: create an XImage
542  *****************************************************************************
543  * Create a simple XImage used as a buffer.
544  *****************************************************************************/
545 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
546 {
547     byte_t *    pb_data;                          /* image data storage zone */
548     int         i_quantum;                     /* XImage quantum (see below) */
549
550     /* Allocate memory for image */
551     p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
552     pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
553     if( !pb_data )                                                  /* error */
554     {
555         intf_ErrMsg("error: %s", strerror(ENOMEM));
556         return( 1 );
557     }
558
559     /* Optimize the quantum of a scanline regarding its size - the quantum is
560        a diviser of the number of bits between the start of two scanlines. */
561     if( !(( p_vout->i_bytes_per_line ) % 32) )
562     {
563         i_quantum = 32;
564     }
565     else
566     {
567         if( !(( p_vout->i_bytes_per_line ) % 16) )
568         {
569             i_quantum = 16;
570         }
571         else
572         {
573             i_quantum = 8;
574         }
575     }
576
577     /* Create XImage */
578     *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
579                                p_vout->i_screen_depth, ZPixmap, 0, pb_data,
580                                p_vout->i_width, p_vout->i_height, i_quantum, 0);
581     if(! *pp_ximage )                                               /* error */
582     {
583         intf_ErrMsg( "error: XCreateImage() failed" );
584         free( pb_data );
585         return( 1 );
586     }
587
588     return 0;
589 }
590
591 /*****************************************************************************
592  * X11CreateShmImage: create an XImage using shared memory extension
593  *****************************************************************************
594  * Prepare an XImage for DisplayX11ShmImage function.
595  * The order of the operations respects the recommandations of the mit-shm
596  * document by J.Corbet and K.Packard. Most of the parameters were copied from
597  * there.
598  *****************************************************************************/
599 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
600                               XShmSegmentInfo *p_shm_info)
601 {
602     /* Create XImage */
603     *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
604                                   p_vout->i_screen_depth, ZPixmap, 0,
605                                   p_shm_info, p_vout->i_width, p_vout->i_height );
606     if(! *pp_ximage )                                               /* error */
607     {
608         intf_ErrMsg("error: XShmCreateImage() failed");
609         return( 1 );
610     }
611
612     /* Allocate shared memory segment - 0777 set the access permission
613      * rights (like umask), they are not yet supported by X servers */
614     p_shm_info->shmid = shmget( IPC_PRIVATE,
615                                 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
616                                 IPC_CREAT | 0777);
617     if( p_shm_info->shmid < 0)                                      /* error */
618     {
619         intf_ErrMsg("error: can't allocate shared image data (%s)",
620                     strerror(errno));
621         XDestroyImage( *pp_ximage );
622         return( 1 );
623     }
624
625     /* Attach shared memory segment to process (read/write) */
626     p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
627     if(! p_shm_info->shmaddr )
628     {                                                               /* error */
629         intf_ErrMsg("error: can't attach shared memory (%s)",
630                     strerror(errno));
631         shmctl( p_shm_info->shmid, IPC_RMID, 0 );      /* free shared memory */
632         XDestroyImage( *pp_ximage );
633         return( 1 );
634     }
635
636     /* Mark the shm segment to be removed when there will be no more
637      * attachements, so it is automatic on process exit or after shmdt */
638     shmctl( p_shm_info->shmid, IPC_RMID, 0 );
639
640     /* Attach shared memory segment to X server (read only) */
641     p_shm_info->readOnly = True;
642     if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False )    /* error */
643     {
644         intf_ErrMsg("error: can't attach shared memory to X11 server");
645         shmdt( p_shm_info->shmaddr );     /* detach shared memory from process
646                                            * and automatic free                */
647         XDestroyImage( *pp_ximage );
648         return( 1 );
649     }
650
651     /* Send image to X server. This instruction is required, since having
652      * built a Shm XImage and not using it causes an error on XCloseDisplay */
653     XFlush( p_vout->p_sys->p_display );
654     return( 0 );
655 }
656
657 /*****************************************************************************
658  * X11DestroyImage: destroy an XImage
659  *****************************************************************************
660  * Destroy XImage AND associated data. If pointer is NULL, the image won't be
661  * destroyed (see vout_ManageOutputMethod())
662  *****************************************************************************/
663 static void X11DestroyImage( XImage *p_ximage )
664 {
665     if( p_ximage != NULL )
666     {
667         XDestroyImage( p_ximage );                     /* no free() required */
668     }
669 }
670
671 /*****************************************************************************
672  * X11DestroyShmImage
673  *****************************************************************************
674  * Destroy XImage AND associated data. Detach shared memory segment from
675  * server and process, then free it. If pointer is NULL, the image won't be
676  * destroyed (see vout_ManageOutputMethod())
677  *****************************************************************************/
678 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
679                                 XShmSegmentInfo *p_shm_info )
680 {
681     /* If pointer is NULL, do nothing */
682     if( p_ximage == NULL )
683     {
684         return;
685     }
686
687     XShmDetach( p_vout->p_sys->p_display, p_shm_info );     /* detach from server */
688     XDestroyImage( p_ximage );
689     if( shmdt( p_shm_info->shmaddr ) )  /* detach shared memory from process */
690     {                                   /* also automatic freeing...         */
691         intf_ErrMsg( "error: can't detach shared memory (%s)",
692                      strerror(errno) );
693     }
694 }
695
696