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