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