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