. nouveaux plugins - ne fonctionnent pas encore tous
[vlc.git] / plugins / gnome / vout_gnome.c
1 /*****************************************************************************
2  * vout_gnome.c: Gnome video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #ifdef SYS_BSD
33 #include <sys/types.h>                                     /* typedef ushort */
34 #endif
35
36 #include <sys/shm.h>                                   /* shmget(), shmctl() */
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39 #include <X11/extensions/XShm.h>
40
41 #include "config.h"
42 #include "common.h"
43 #include "threads.h"
44 #include "mtime.h"
45 #include "plugins.h"
46
47 #include "video.h"
48 #include "video_output.h"
49
50 #include "intf_msg.h"
51
52 /*****************************************************************************
53  * vout_sys_t: video output X11 method descriptor
54  *****************************************************************************
55  * This structure is part of the video output thread descriptor.
56  * It describes the X11 specific properties of an output thread. X11 video
57  * output is performed through regular resizable windows. Windows can be
58  * dynamically resized to adapt to the size of the streams.
59  *****************************************************************************/
60 typedef struct vout_sys_s
61 {
62     /* User settings */
63     boolean_t           b_shm;               /* shared memory extension flag */
64
65     /* Internal settings and properties */
66     Display *           p_display;                        /* display pointer */
67     Visual *            p_visual;                          /* visual pointer */
68     int                 i_screen;                           /* screen number */
69     Window              root_window;                          /* root window */
70     Window              window;                   /* window instance handler */
71     GC                  gc;              /* graphic context instance handler */
72     Colormap            colormap;               /* colormap used (8bpp only) */
73
74     /* Display buffers and shared memory information */
75     XImage *            p_ximage[2];                       /* XImage pointer */
76     XShmSegmentInfo     shm_info[2];       /* shared memory zone information */
77 } vout_sys_t;
78
79 /*****************************************************************************
80  * Local prototypes
81  *****************************************************************************/
82 static int  X11OpenDisplay      ( vout_thread_t *p_vout, char *psz_display, 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 convertion 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         p_vout->i_bytes_per_pixel = 0;
387         for( ; i_count--; p_xpixmap_format++ )
388         {
389             if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
390             {
391                 p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
392             }
393         }
394         break;
395     }
396     p_vout->p_sys->p_visual = p_xvisual->visual;
397     XFree( p_xvisual );
398
399     /* Create a window */
400     if( X11CreateWindow( p_vout ) )
401     {
402         intf_ErrMsg("error: can't open a window\n");
403         XCloseDisplay( p_vout->p_sys->p_display );
404         return( 1 );
405     }
406     return( 0 );
407 }
408
409 /*****************************************************************************
410  * X11CloseDisplay: close X11 device
411  *****************************************************************************
412  * Returns all resources allocated by X11OpenDisplay and restore the original
413  * state of the display.
414  *****************************************************************************/
415 static void X11CloseDisplay( vout_thread_t *p_vout )
416 {
417     /* Destroy colormap */
418     if( p_vout->i_screen_depth == 8 )
419     {
420         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
421     }
422     
423     /* Destroy window */
424     X11DestroyWindow( p_vout );
425
426     /* FIXME: We should close the display here, but X returns an error. */
427     //XCloseDisplay( p_vout->p_sys->p_display );
428 }
429
430 /*****************************************************************************
431  * X11CreateWindow: create X11 vout window
432  *****************************************************************************
433  * The video output window will be created. Normally, this window is wether
434  * full screen or part of a parent window. Therefore, it does not need a
435  * title or other hints. Thery are still supplied in case the window would be
436  * spawned as a standalone one by the interface.
437  *****************************************************************************/
438 static int X11CreateWindow( vout_thread_t *p_vout )
439 {
440     XSetWindowAttributes    xwindow_attributes;         /* window attributes */
441     XGCValues               xgcvalues;      /* graphic context configuration */
442     XEvent                  xevent;                          /* first events */
443     boolean_t               b_expose;             /* 'expose' event received */
444     boolean_t               b_map_notify;     /* 'map_notify' event received */
445
446     /* Prepare window attributes */
447     xwindow_attributes.backing_store = Always;       /* save the hidden part */
448
449     /* Create the window and set hints */
450     p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
451                                          p_vout->p_sys->root_window,
452                                          0, 0,
453                                          p_vout->i_width, p_vout->i_height,
454                                          0, 0, 0);
455     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
456                   ExposureMask | StructureNotifyMask );
457     XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
458                              CWBackingStore, &xwindow_attributes);
459
460     /* Creation of a graphic context that doesn't generate a GraphicsExpose event
461        when using functions like XCopyArea */
462     xgcvalues.graphics_exposures = False;
463     p_vout->p_sys->gc =  XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
464                                     GCGraphicsExposures, &xgcvalues);
465
466     /* Send orders to server, and wait until window is displayed - two events
467      * must be received: a MapNotify event, an Expose event allowing drawing in the
468      * window */
469     b_expose = 0;
470     b_map_notify = 0;
471     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
472     do
473     {
474         XNextEvent( p_vout->p_sys->p_display, &xevent);
475         if( (xevent.type == Expose)
476             && (xevent.xexpose.window == p_vout->p_sys->window) )
477         {
478             b_expose = 1;
479         }
480         else if( (xevent.type == MapNotify)
481                  && (xevent.xmap.window == p_vout->p_sys->window) )
482         {
483             b_map_notify = 1;
484         }
485     }
486     while( !( b_expose && b_map_notify ) );
487     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
488
489     /* At this stage, the window is open, displayed, and ready to receive
490      * data */
491     return( 0 );
492 }
493
494 /*****************************************************************************
495  * X11DestroyWindow: destroy X11 window
496  *****************************************************************************
497  * Destroy an X11 window created by vout_CreateWindow
498  *****************************************************************************/
499 static void X11DestroyWindow( vout_thread_t *p_vout )
500 {
501     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
502     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
503     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
504 }
505
506 /*****************************************************************************
507  * X11CreateImage: create an XImage
508  *****************************************************************************
509  * Create a simple XImage used as a buffer.
510  *****************************************************************************/
511 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
512 {
513     byte_t *    pb_data;                          /* image data storage zone */
514     int         i_quantum;                     /* XImage quantum (see below) */
515
516     /* Allocate memory for image */
517     p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
518     pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
519     if( !pb_data )                                                  /* error */
520     {
521         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
522         return( 1 );
523     }
524
525     /* Optimize the quantum of a scanline regarding its size - the quantum is
526        a diviser of the number of bits between the start of two scanlines. */
527     if( !(( p_vout->i_bytes_per_line ) % 32) )
528     {
529         i_quantum = 32;
530     }
531     else
532     {
533         if( !(( p_vout->i_bytes_per_line ) % 16) )
534         {
535             i_quantum = 16;
536         }
537         else
538         {
539             i_quantum = 8;
540         }
541     }
542
543     /* Create XImage */
544     *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
545                                p_vout->i_screen_depth, ZPixmap, 0, pb_data,
546                                p_vout->i_width, p_vout->i_height, i_quantum, 0);
547     if(! *pp_ximage )                                               /* error */
548     {
549         intf_ErrMsg( "error: XCreateImage() failed\n" );
550         free( pb_data );
551         return( 1 );
552     }
553
554     return 0;
555 }
556
557 /*****************************************************************************
558  * X11CreateShmImage: create an XImage using shared memory extension
559  *****************************************************************************
560  * Prepare an XImage for DisplayX11ShmImage function.
561  * The order of the operations respects the recommandations of the mit-shm
562  * document by J.Corbet and K.Packard. Most of the parameters were copied from
563  * there.
564  *****************************************************************************/
565 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
566                               XShmSegmentInfo *p_shm_info)
567 {
568     /* Create XImage */
569     *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
570                                   p_vout->i_screen_depth, ZPixmap, 0,
571                                   p_shm_info, p_vout->i_width, p_vout->i_height );
572     if(! *pp_ximage )                                               /* error */
573     {
574         intf_ErrMsg("error: XShmCreateImage() failed\n");
575         return( 1 );
576     }
577
578     /* Allocate shared memory segment - 0777 set the access permission
579      * rights (like umask), they are not yet supported by X servers */
580     p_shm_info->shmid = shmget( IPC_PRIVATE,
581                                 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
582                                 IPC_CREAT | 0777);
583     if( p_shm_info->shmid < 0)                                      /* error */
584     {
585         intf_ErrMsg("error: can't allocate shared image data (%s)\n",
586                     strerror(errno));
587         XDestroyImage( *pp_ximage );
588         return( 1 );
589     }
590
591     /* Attach shared memory segment to process (read/write) */
592     p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
593     if(! p_shm_info->shmaddr )
594     {                                                               /* error */
595         intf_ErrMsg("error: can't attach shared memory (%s)\n",
596                     strerror(errno));
597         shmctl( p_shm_info->shmid, IPC_RMID, 0 );      /* free shared memory */
598         XDestroyImage( *pp_ximage );
599         return( 1 );
600     }
601
602     /* Mark the shm segment to be removed when there will be no more
603      * attachements, so it is automatic on process exit or after shmdt */
604     shmctl( p_shm_info->shmid, IPC_RMID, 0 );
605
606     /* Attach shared memory segment to X server (read only) */
607     p_shm_info->readOnly = True;
608     if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False )    /* error */
609     {
610         intf_ErrMsg("error: can't attach shared memory to X11 server\n");
611         shmdt( p_shm_info->shmaddr );     /* detach shared memory from process
612                                            * and automatic free                */
613         XDestroyImage( *pp_ximage );
614         return( 1 );
615     }
616
617     /* Send image to X server. This instruction is required, since having
618      * built a Shm XImage and not using it causes an error on XCloseDisplay */
619     XFlush( p_vout->p_sys->p_display );
620     return( 0 );
621 }
622
623 /*****************************************************************************
624  * X11DestroyImage: destroy an XImage
625  *****************************************************************************
626  * Destroy XImage AND associated data. If pointer is NULL, the image won't be
627  * destroyed (see vout_ManageOutputMethod())
628  *****************************************************************************/
629 static void X11DestroyImage( XImage *p_ximage )
630 {
631     if( p_ximage != NULL )
632     {
633         XDestroyImage( p_ximage );                     /* no free() required */
634     }
635 }
636
637 /*****************************************************************************
638  * X11DestroyShmImage
639  *****************************************************************************
640  * Destroy XImage AND associated data. Detach shared memory segment from
641  * server and process, then free it. If pointer is NULL, the image won't be
642  * destroyed (see vout_ManageOutputMethod())
643  *****************************************************************************/
644 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
645                                 XShmSegmentInfo *p_shm_info )
646 {
647     /* If pointer is NULL, do nothing */
648     if( p_ximage == NULL )
649     {
650         return;
651     }
652
653     XShmDetach( p_vout->p_sys->p_display, p_shm_info );     /* detach from server */
654     XDestroyImage( p_ximage );
655     if( shmdt( p_shm_info->shmaddr ) )  /* detach shared memory from process */
656     {                                   /* also automatic freeing...         */
657         intf_ErrMsg( "error: can't detach shared memory (%s)\n",
658                      strerror(errno) );
659     }
660 }
661
662 /*****************************************************************************
663  * X11SetPalette: sets an 8 bpp palette
664  *****************************************************************************
665  * This function sets the palette given as an argument. It does not return
666  * anything, but could later send information on which colors it was unable
667  * to set.
668  *****************************************************************************/
669 static void X11SetPalette       ( p_vout_thread_t p_vout,
670                                   u16 *red, u16 *green, u16 *blue, u16 *transp )
671 {
672     int i;
673     XColor color[255];
674
675     intf_DbgMsg( "Palette change called\n" );
676
677     /* allocate palette */
678     for( i = 0; i < 255; i++ )
679     {
680         /* kludge: colors are indexed reversely because color 255 seems
681          * to be reserved for black even if we try to set it to white */
682         color[i].pixel = 255-i;
683         color[i].pad = 0;
684         color[i].flags = DoRed|DoGreen|DoBlue;
685         color[i].red = red[255-i];
686         color[i].blue = blue[255-i];
687         color[i].green = green[255-i];
688     }
689
690     XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );
691 }
692