]> git.sesse.net Git - vlc/blob - plugins/x11/vout_x11.c
* updated version information to 0.2.60 -- today's release
[vlc] / plugins / x11 / vout_x11.c
1 /*****************************************************************************
2  * vout_x11.c: X11 video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  * $Id: vout_x11.c,v 1.10 2001/02/14 07:48:18 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <string.h>                                            /* strerror() */
33
34 #ifdef HAVE_MACHINE_PARAM_H
35 /* BSD */
36 #include <machine/param.h>
37 #include <sys/types.h>                                     /* typedef ushort */
38 #include <sys/ipc.h>
39 #endif
40
41 #include <sys/shm.h>                                   /* shmget(), shmctl() */
42 #include <X11/Xlib.h>
43 #include <X11/Xutil.h>
44 #include <X11/keysym.h>
45 #include <X11/extensions/XShm.h>
46
47 #include "config.h"
48 #include "common.h"
49 #include "threads.h"
50 #include "mtime.h"
51 #include "tests.h"
52 #include "modules.h"
53
54 #include "video.h"
55 #include "video_output.h"
56
57 #include "interface.h"
58 #include "intf_msg.h"
59
60 /*****************************************************************************
61  * vout_sys_t: video output X11 method descriptor
62  *****************************************************************************
63  * This structure is part of the video output thread descriptor.
64  * It describes the X11 specific properties of an output thread. X11 video
65  * output is performed through regular resizable windows. Windows can be
66  * dynamically resized to adapt to the size of the streams.
67  *****************************************************************************/
68 typedef struct vout_sys_s
69 {
70     /* User settings */
71     boolean_t           b_shm;               /* shared memory extension flag */
72
73     /* Internal settings and properties */
74     Display *           p_display;                        /* display pointer */
75     Visual *            p_visual;                          /* visual pointer */
76     int                 i_screen;                           /* screen number */
77     Window              root_window;                          /* root window */
78     Window              window;                   /* window instance handler */
79     GC                  gc;              /* graphic context instance handler */
80     Colormap            colormap;               /* colormap used (8bpp only) */
81
82     /* Display buffers and shared memory information */
83     XImage *            p_ximage[2];                       /* XImage pointer */
84     XShmSegmentInfo     shm_info[2];       /* shared memory zone information */
85
86     /* X11 generic properties */
87     Atom                wm_protocols;
88     Atom                wm_delete_window;
89
90     int                 i_width;                     /* width of main window */
91     int                 i_height;                   /* height of main window */
92
93     /* Screen saver properties */
94     int                 i_ss_count;              /* enabling/disabling count */
95     int                 i_ss_timeout;                             /* timeout */
96     int                 i_ss_interval;           /* interval between changes */
97     int                 i_ss_blanking;                      /* blanking mode */
98     int                 i_ss_exposure;                      /* exposure mode */
99
100     /* Mouse pointer properties */
101     boolean_t           b_mouse;         /* is the mouse pointer displayed ? */
102
103 } vout_sys_t;
104
105 /*****************************************************************************
106  * Local prototypes
107  *****************************************************************************/
108 static int  vout_Probe     ( probedata_t *p_data );
109 static int  vout_Create    ( struct vout_thread_s * );
110 static int  vout_Init      ( struct vout_thread_s * );
111 static void vout_End       ( struct vout_thread_s * );
112 static void vout_Destroy   ( struct vout_thread_s * );
113 static int  vout_Manage    ( struct vout_thread_s * );
114 static void vout_Display   ( struct vout_thread_s * );
115 static void vout_SetPalette( struct vout_thread_s *, u16*, u16*, u16*, u16* );
116
117 static int  X11OpenDisplay      ( vout_thread_t *p_vout );
118 static void X11CloseDisplay     ( vout_thread_t *p_vout );
119 static int  X11CreateWindow     ( vout_thread_t *p_vout );
120 static void X11DestroyWindow    ( vout_thread_t *p_vout );
121 static int  X11CreateImage      ( vout_thread_t *p_vout, XImage **pp_ximage );
122 static void X11DestroyImage     ( XImage *p_ximage );
123 static int  X11CreateShmImage   ( vout_thread_t *p_vout, XImage **pp_ximage,
124                                   XShmSegmentInfo *p_shm_info );
125 static void X11DestroyShmImage  ( vout_thread_t *p_vout, XImage *p_ximage,
126                                   XShmSegmentInfo *p_shm_info );
127
128 /*****************************************************************************
129  * Functions exported as capabilities. They are declared as static so that
130  * we don't pollute the namespace too much.
131  *****************************************************************************/
132 void vout_getfunctions( function_list_t * p_function_list )
133 {
134     p_function_list->pf_probe = vout_Probe;
135     p_function_list->functions.vout.pf_create     = vout_Create;
136     p_function_list->functions.vout.pf_init       = vout_Init;
137     p_function_list->functions.vout.pf_end        = vout_End;
138     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
139     p_function_list->functions.vout.pf_manage     = vout_Manage;
140     p_function_list->functions.vout.pf_display    = vout_Display;
141     p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
142 }
143
144 /*****************************************************************************
145  * vout_Probe: probe the video driver and return a score
146  *****************************************************************************
147  * This function tries to initialize SDL and returns a score to the
148  * plugin manager so that it can select the best plugin.
149  *****************************************************************************/
150 static int vout_Probe( probedata_t *p_data )
151 {
152     if( TestMethod( VOUT_METHOD_VAR, "x11" ) )
153     {
154         return( 999 );
155     }
156
157     return( 50 );
158 }
159
160 /*****************************************************************************
161  * vout_Create: allocate X11 video thread output method
162  *****************************************************************************
163  * This function allocate and initialize a X11 vout method. It uses some of the
164  * vout properties to choose the window size, and change them according to the
165  * actual properties of the display.
166  *****************************************************************************/
167 static int vout_Create( vout_thread_t *p_vout )
168 {
169     /* Allocate structure */
170     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
171     if( p_vout->p_sys == NULL )
172     {
173         intf_ErrMsg("error: %s", strerror(ENOMEM) );
174         return( 1 );
175     }
176
177     /* Open and initialize device. This function issues its own error messages.
178      * Since XLib is usually not thread-safe, we can't use the same display
179      * pointer than the interface or another thread. However, the root window
180      * id is still valid. */
181     if( X11OpenDisplay( p_vout ) )
182     {
183         intf_ErrMsg("error: can't initialize X11 display" );
184         free( p_vout->p_sys );
185         return( 1 );
186     }
187
188     return( 0 );
189 }
190
191 /*****************************************************************************
192  * vout_Init: initialize X11 video thread output method
193  *****************************************************************************
194  * This function create the XImages needed by the output thread. It is called
195  * at the beginning of the thread, but also each time the window is resized.
196  *****************************************************************************/
197 static int vout_Init( vout_thread_t *p_vout )
198 {
199     int i_err;
200
201     /* Create XImages using XShm extension - on failure, fall back to regular
202      * way (and destroy the first image if it was created successfully) */
203     if( p_vout->p_sys->b_shm )
204     {
205         /* Create first image */
206         i_err = X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[0],
207                                    &p_vout->p_sys->shm_info[0] );
208         if( !i_err )                         /* first image has been created */
209         {
210             /* Create second image */
211             if( X11CreateShmImage( p_vout, &p_vout->p_sys->p_ximage[1],
212                                    &p_vout->p_sys->shm_info[1] ) )
213             {                             /* error creating the second image */
214                 X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
215                                     &p_vout->p_sys->shm_info[0] );
216                 i_err = 1;
217             }
218         }
219         if( i_err )                                      /* an error occured */
220         {
221             intf_Msg("vout: XShm video sextension deactivated" );
222             p_vout->p_sys->b_shm = 0;
223         }
224     }
225
226     /* Create XImages without XShm extension */
227     if( !p_vout->p_sys->b_shm )
228     {
229         if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[0] ) )
230         {
231             intf_ErrMsg("error: can't create images");
232             p_vout->p_sys->p_ximage[0] = NULL;
233             p_vout->p_sys->p_ximage[1] = NULL;
234             return( 1 );
235         }
236         if( X11CreateImage( p_vout, &p_vout->p_sys->p_ximage[1] ) )
237         {
238             intf_ErrMsg("error: can't create images");
239             X11DestroyImage( p_vout->p_sys->p_ximage[0] );
240             p_vout->p_sys->p_ximage[0] = NULL;
241             p_vout->p_sys->p_ximage[1] = NULL;
242             return( 1 );
243         }
244     }
245
246     /* Set bytes per line and initialize buffers */
247     p_vout->i_bytes_per_line = p_vout->p_sys->p_ximage[0]->bytes_per_line;
248     vout_SetBuffers( p_vout, p_vout->p_sys->p_ximage[ 0 ]->data,
249                      p_vout->p_sys->p_ximage[ 1 ]->data );
250     return( 0 );
251 }
252
253 /*****************************************************************************
254  * vout_End: terminate X11 video thread output method
255  *****************************************************************************
256  * Destroy the X11 XImages created by vout_Init. It is called at the end of
257  * the thread, but also each time the window is resized.
258  *****************************************************************************/
259 static void vout_End( vout_thread_t *p_vout )
260 {
261     if( p_vout->p_sys->b_shm )                             /* Shm XImages... */
262     {
263         X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[0],
264                             &p_vout->p_sys->shm_info[0] );
265         X11DestroyShmImage( p_vout, p_vout->p_sys->p_ximage[1],
266                             &p_vout->p_sys->shm_info[1] );
267     }
268     else                                          /* ...or regular XImages */
269     {
270         X11DestroyImage( p_vout->p_sys->p_ximage[0] );
271         X11DestroyImage( p_vout->p_sys->p_ximage[1] );
272     }
273 }
274
275 /*****************************************************************************
276  * vout_Destroy: destroy X11 video thread output method
277  *****************************************************************************
278  * Terminate an output method created by vout_CreateOutputMethod
279  *****************************************************************************/
280 static void vout_Destroy( vout_thread_t *p_vout )
281 {
282     X11CloseDisplay( p_vout );
283     free( p_vout->p_sys );
284 }
285
286 /*****************************************************************************
287  * vout_Manage: handle X11 events
288  *****************************************************************************
289  * This function should be called regularly by video output thread. It manages
290  * X11 events and allows window resizing. It returns a non null value on
291  * error.
292  *****************************************************************************/
293 static int vout_Manage( vout_thread_t *p_vout )
294 {
295     /*
296      * Color/Grayscale or gamma change: in 8bpp, just change the colormap
297      */
298     if( (p_vout->i_changes & VOUT_GRAYSCALE_CHANGE)
299         && (p_vout->i_screen_depth == 8) )
300     {
301         /* FIXME: clear flags ?? */
302     }
303
304     /*
305      * Size change
306      */
307     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
308     {
309         intf_DbgMsg("resizing window");
310         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
311
312         /* Resize window */
313         XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->window,
314                        p_vout->i_width, p_vout->i_height );
315
316         /* Destroy XImages to change their size */
317         vout_End( p_vout );
318
319         /* Recreate XImages. If SysInit failed, the thread can't go on. */
320         if( vout_Init( p_vout ) )
321         {
322             intf_ErrMsg("error: can't resize display");
323             return( 1 );
324         }
325
326         /* Tell the video output thread that it will need to rebuild YUV
327          * tables. This is needed since conversion buffer size may have
328          * changed */
329         p_vout->i_changes |= VOUT_YUV_CHANGE;
330         intf_Msg("vout: video display resized (%dx%d)", p_vout->i_width, p_vout->i_height);
331     }
332
333     return 0;
334 }
335
336 /*****************************************************************************
337  * vout_Display: displays previously rendered output
338  *****************************************************************************
339  * This function send the currently rendered image to X11 server, wait until
340  * it is displayed and switch the two rendering buffer, preparing next frame.
341  *****************************************************************************/
342 static void vout_Display( vout_thread_t *p_vout )
343 {
344     if( p_vout->p_sys->b_shm)                                /* XShm is used */
345     {
346         /* Display rendered image using shared memory extension */
347         XShmPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
348                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
349                      0, 0, 0, 0,
350                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
351                      p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height, True);
352
353         /* Send the order to the X server */
354         XSync(p_vout->p_sys->p_display, False);
355     }
356     else                                /* regular X11 capabilities are used */
357     {
358         XPutImage(p_vout->p_sys->p_display, p_vout->p_sys->window, p_vout->p_sys->gc,
359                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ],
360                   0, 0, 0, 0,
361                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->width,
362                   p_vout->p_sys->p_ximage[ p_vout->i_buffer_index ]->height);
363
364         /* Send the order to the X server */
365         XSync(p_vout->p_sys->p_display, False);
366     }
367 }
368
369 /*****************************************************************************
370  * vout_SetPalette: sets an 8 bpp palette
371  *****************************************************************************
372  * This function sets the palette given as an argument. It does not return
373  * anything, but could later send information on which colors it was unable
374  * to set.
375  *****************************************************************************/
376 static void vout_SetPalette( p_vout_thread_t p_vout,
377                              u16 *red, u16 *green, u16 *blue, u16 *transp )
378 {
379     int i;
380     XColor color[255];
381
382     intf_DbgMsg( "Palette change called" );
383
384     /* allocate palette */
385     for( i = 0; i < 255; i++ )
386     {
387         /* kludge: colors are indexed reversely because color 255 seems
388          * to be reserved for black even if we try to set it to white */
389         color[i].pixel = 255-i;
390         color[i].pad = 0;
391         color[i].flags = DoRed|DoGreen|DoBlue;
392         color[i].red = red[255-i];
393         color[i].blue = blue[255-i];
394         color[i].green = green[255-i];
395     }
396
397     XStoreColors( p_vout->p_sys->p_display, p_vout->p_sys->colormap, color, 256 );
398 }
399
400 /* following functions are local */
401
402 /*****************************************************************************
403  * X11OpenDisplay: open and initialize X11 device
404  *****************************************************************************
405  * Create a window according to video output given size, and set other
406  * properties according to the display properties.
407  *****************************************************************************/
408 static int X11OpenDisplay( vout_thread_t *p_vout )
409 {
410     XPixmapFormatValues *       p_xpixmap_format;          /* pixmap formats */
411     XVisualInfo *               p_xvisual;           /* visuals informations */
412     XVisualInfo                 xvisual_template;         /* visual template */
413     int                         i_count;                       /* array size */
414
415     /* Open display */
416     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
417     if( p_vout->p_sys->p_display == NULL )
418     {
419         intf_ErrMsg("error: can't open display %s", psz_display );
420         return( 1 );
421     }
422
423     /* Initialize structure */
424     p_vout->p_sys->root_window  = root_window;
425     p_vout->p_sys->b_shm        = (XShmQueryExtension(p_vout->p_sys->p_display) == True);
426     p_vout->p_sys->i_screen     = DefaultScreen( p_vout->p_sys->p_display );
427     if( !p_vout->p_sys->b_shm )
428     {
429         intf_Msg("vout: XShm video extension is not available");
430     }
431
432     /* Get screen depth */
433     p_vout->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
434     switch( p_vout->i_screen_depth )
435     {
436     case 8:
437         /*
438          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
439          */
440         xvisual_template.screen =   p_vout->p_sys->i_screen;
441         xvisual_template.class =    DirectColor;
442         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
443                                     &xvisual_template, &i_count );
444         if( p_xvisual == NULL )
445         {
446             intf_ErrMsg("error: no PseudoColor visual available");
447             XCloseDisplay( p_vout->p_sys->p_display );
448             return( 1 );
449         }
450         p_vout->i_bytes_per_pixel = 1;
451
452         /* put the colormap in place */
453         p_vout->p_sys->colormap = *(Colormap *)p_data;
454         break;
455     case 15:
456     case 16:
457     case 24:
458     default:
459         /*
460          * Screen depth is higher than 8bpp. TrueColor visual is used.
461          */
462         xvisual_template.screen =   p_vout->p_sys->i_screen;
463         xvisual_template.class =    TrueColor;
464         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display, VisualScreenMask | VisualClassMask,
465                                     &xvisual_template, &i_count );
466         if( p_xvisual == NULL )
467         {
468             intf_ErrMsg("error: no TrueColor visual available");
469             XCloseDisplay( p_vout->p_sys->p_display );
470             return( 1 );
471         }
472         p_vout->i_red_mask =        p_xvisual->red_mask;
473         p_vout->i_green_mask =      p_xvisual->green_mask;
474         p_vout->i_blue_mask =       p_xvisual->blue_mask;
475
476         /* There is no difference yet between 3 and 4 Bpp. The only way to find
477          * the actual number of bytes per pixel is to list supported pixmap
478          * formats. */
479         p_xpixmap_format = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
480
481
482         /* Under XFree4.0, the list contains pixmap formats available through 
483          * all video depths ; so we have to check against current depth. */
484         p_vout->i_bytes_per_pixel = 0;
485         for( ; i_count-- ; p_xpixmap_format++ )
486         {
487             if( p_xpixmap_format->depth == p_vout->i_screen_depth )
488             {
489                 if( p_xpixmap_format->bits_per_pixel / 8 > p_vout->i_bytes_per_pixel )
490                 {
491                     p_vout->i_bytes_per_pixel = p_xpixmap_format->bits_per_pixel / 8;
492                 }
493             }
494         }
495         break;
496     }
497     p_vout->p_sys->p_visual = p_xvisual->visual;
498     XFree( p_xvisual );
499
500     /* Create a window */
501     if( X11CreateWindow( p_vout ) )
502     {
503         intf_ErrMsg("error: can't open a window");
504         XCloseDisplay( p_vout->p_sys->p_display );
505         return( 1 );
506     }
507     return( 0 );
508 }
509
510 /*****************************************************************************
511  * X11CloseDisplay: close X11 device
512  *****************************************************************************
513  * Returns all resources allocated by X11OpenDisplay and restore the original
514  * state of the display.
515  *****************************************************************************/
516 static void X11CloseDisplay( vout_thread_t *p_vout )
517 {
518     /* Destroy colormap */
519     if( p_vout->i_screen_depth == 8 )
520     {
521         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
522     }
523     
524     /* Destroy window */
525     X11DestroyWindow( p_vout );
526
527     /* FIXME: We should close the display here, but X returns an error. */
528     //XCloseDisplay( p_vout->p_sys->p_display );
529 }
530
531 /*****************************************************************************
532  * X11CreateWindow: create X11 vout window
533  *****************************************************************************
534  * The video output window will be created. Normally, this window is wether
535  * full screen or part of a parent window. Therefore, it does not need a
536  * title or other hints. Thery are still supplied in case the window would be
537  * spawned as a standalone one by the interface.
538  *****************************************************************************/
539 static int X11CreateWindow( vout_thread_t *p_vout )
540 {
541     XSetWindowAttributes    xwindow_attributes;         /* window attributes */
542     XGCValues               xgcvalues;      /* graphic context configuration */
543     XEvent                  xevent;                          /* first events */
544     boolean_t               b_expose;             /* 'expose' event received */
545     boolean_t               b_map_notify;     /* 'map_notify' event received */
546
547     /* Prepare window attributes */
548     xwindow_attributes.backing_store = Always;       /* save the hidden part */
549
550     /* Create the window and set hints */
551     p_vout->p_sys->window = XCreateSimpleWindow( p_vout->p_sys->p_display,
552                                          p_vout->p_sys->root_window,
553                                          0, 0,
554                                          p_vout->i_width, p_vout->i_height,
555                                          0, 0, 0);
556     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
557                   ExposureMask | StructureNotifyMask );
558     XChangeWindowAttributes( p_vout->p_sys->p_display, p_vout->p_sys->window,
559                              CWBackingStore, &xwindow_attributes);
560
561     /* Creation of a graphic context that doesn't generate a GraphicsExpose event
562        when using functions like XCopyArea */
563     xgcvalues.graphics_exposures = False;
564     p_vout->p_sys->gc =  XCreateGC( p_vout->p_sys->p_display, p_vout->p_sys->window,
565                                     GCGraphicsExposures, &xgcvalues);
566
567     /* Send orders to server, and wait until window is displayed - two events
568      * must be received: a MapNotify event, an Expose event allowing drawing in the
569      * window */
570     b_expose = 0;
571     b_map_notify = 0;
572     XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
573     do
574     {
575         XNextEvent( p_vout->p_sys->p_display, &xevent);
576         if( (xevent.type == Expose)
577             && (xevent.xexpose.window == p_vout->p_sys->window) )
578         {
579             b_expose = 1;
580         }
581         else if( (xevent.type == MapNotify)
582                  && (xevent.xmap.window == p_vout->p_sys->window) )
583         {
584             b_map_notify = 1;
585         }
586     }
587     while( !( b_expose && b_map_notify ) );
588     XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window, 0 );
589
590     /* At this stage, the window is open, displayed, and ready to receive
591      * data */
592     return( 0 );
593 }
594
595 /*****************************************************************************
596  * X11DestroyWindow: destroy X11 window
597  *****************************************************************************
598  * Destroy an X11 window created by vout_CreateWindow
599  *****************************************************************************/
600 static void X11DestroyWindow( vout_thread_t *p_vout )
601 {
602     XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
603     XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
604     XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
605 }
606
607 /*****************************************************************************
608  * X11CreateImage: create an XImage
609  *****************************************************************************
610  * Create a simple XImage used as a buffer.
611  *****************************************************************************/
612 static int X11CreateImage( vout_thread_t *p_vout, XImage **pp_ximage )
613 {
614     byte_t *    pb_data;                          /* image data storage zone */
615     int         i_quantum;                     /* XImage quantum (see below) */
616
617     /* Allocate memory for image */
618     p_vout->i_bytes_per_line = p_vout->i_width * p_vout->i_bytes_per_pixel;
619     pb_data = (byte_t *) malloc( p_vout->i_bytes_per_line * p_vout->i_height );
620     if( !pb_data )                                                  /* error */
621     {
622         intf_ErrMsg("error: %s", strerror(ENOMEM));
623         return( 1 );
624     }
625
626     /* Optimize the quantum of a scanline regarding its size - the quantum is
627        a diviser of the number of bits between the start of two scanlines. */
628     if( !(( p_vout->i_bytes_per_line ) % 32) )
629     {
630         i_quantum = 32;
631     }
632     else
633     {
634         if( !(( p_vout->i_bytes_per_line ) % 16) )
635         {
636             i_quantum = 16;
637         }
638         else
639         {
640             i_quantum = 8;
641         }
642     }
643
644     /* Create XImage */
645     *pp_ximage = XCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
646                                p_vout->i_screen_depth, ZPixmap, 0, pb_data,
647                                p_vout->i_width, p_vout->i_height, i_quantum, 0);
648     if(! *pp_ximage )                                               /* error */
649     {
650         intf_ErrMsg( "error: XCreateImage() failed" );
651         free( pb_data );
652         return( 1 );
653     }
654
655     return 0;
656 }
657
658 /*****************************************************************************
659  * X11CreateShmImage: create an XImage using shared memory extension
660  *****************************************************************************
661  * Prepare an XImage for DisplayX11ShmImage function.
662  * The order of the operations respects the recommandations of the mit-shm
663  * document by J.Corbet and K.Packard. Most of the parameters were copied from
664  * there.
665  *****************************************************************************/
666 static int X11CreateShmImage( vout_thread_t *p_vout, XImage **pp_ximage,
667                               XShmSegmentInfo *p_shm_info)
668 {
669     /* Create XImage */
670     *pp_ximage = XShmCreateImage( p_vout->p_sys->p_display, p_vout->p_sys->p_visual,
671                                   p_vout->i_screen_depth, ZPixmap, 0,
672                                   p_shm_info, p_vout->i_width, p_vout->i_height );
673     if(! *pp_ximage )                                               /* error */
674     {
675         intf_ErrMsg("error: XShmCreateImage() failed");
676         return( 1 );
677     }
678
679     /* Allocate shared memory segment - 0777 set the access permission
680      * rights (like umask), they are not yet supported by X servers */
681     p_shm_info->shmid = shmget( IPC_PRIVATE,
682                                 (*pp_ximage)->bytes_per_line * (*pp_ximage)->height,
683                                 IPC_CREAT | 0777);
684     if( p_shm_info->shmid < 0)                                      /* error */
685     {
686         intf_ErrMsg("error: can't allocate shared image data (%s)",
687                     strerror(errno));
688         XDestroyImage( *pp_ximage );
689         return( 1 );
690     }
691
692     /* Attach shared memory segment to process (read/write) */
693     p_shm_info->shmaddr = (*pp_ximage)->data = shmat(p_shm_info->shmid, 0, 0);
694     if(! p_shm_info->shmaddr )
695     {                                                               /* error */
696         intf_ErrMsg("error: can't attach shared memory (%s)",
697                     strerror(errno));
698         shmctl( p_shm_info->shmid, IPC_RMID, 0 );      /* free shared memory */
699         XDestroyImage( *pp_ximage );
700         return( 1 );
701     }
702
703     /* Mark the shm segment to be removed when there will be no more
704      * attachements, so it is automatic on process exit or after shmdt */
705     shmctl( p_shm_info->shmid, IPC_RMID, 0 );
706
707     /* Attach shared memory segment to X server (read only) */
708     p_shm_info->readOnly = True;
709     if( XShmAttach( p_vout->p_sys->p_display, p_shm_info ) == False )    /* error */
710     {
711         intf_ErrMsg("error: can't attach shared memory to X11 server");
712         shmdt( p_shm_info->shmaddr );     /* detach shared memory from process
713                                            * and automatic free                */
714         XDestroyImage( *pp_ximage );
715         return( 1 );
716     }
717
718     /* Send image to X server. This instruction is required, since having
719      * built a Shm XImage and not using it causes an error on XCloseDisplay */
720     XFlush( p_vout->p_sys->p_display );
721     return( 0 );
722 }
723
724 /*****************************************************************************
725  * X11DestroyImage: destroy an XImage
726  *****************************************************************************
727  * Destroy XImage AND associated data. If pointer is NULL, the image won't be
728  * destroyed (see vout_ManageOutputMethod())
729  *****************************************************************************/
730 static void X11DestroyImage( XImage *p_ximage )
731 {
732     if( p_ximage != NULL )
733     {
734         XDestroyImage( p_ximage );                     /* no free() required */
735     }
736 }
737
738 /*****************************************************************************
739  * X11DestroyShmImage
740  *****************************************************************************
741  * Destroy XImage AND associated data. Detach shared memory segment from
742  * server and process, then free it. If pointer is NULL, the image won't be
743  * destroyed (see vout_ManageOutputMethod())
744  *****************************************************************************/
745 static void X11DestroyShmImage( vout_thread_t *p_vout, XImage *p_ximage,
746                                 XShmSegmentInfo *p_shm_info )
747 {
748     /* If pointer is NULL, do nothing */
749     if( p_ximage == NULL )
750     {
751         return;
752     }
753
754     XShmDetach( p_vout->p_sys->p_display, p_shm_info );     /* detach from server */
755     XDestroyImage( p_ximage );
756     if( shmdt( p_shm_info->shmaddr ) )  /* detach shared memory from process */
757     {                                   /* also automatic freeing...         */
758         intf_ErrMsg( "error: can't detach shared memory (%s)",
759                      strerror(errno) );
760     }
761 }
762
763 /*****************************************************************************
764  * Local prototypes
765  *****************************************************************************/
766 static int  X11CreateWindow             ( intf_thread_t *p_intf );
767 static void X11DestroyWindow            ( intf_thread_t *p_intf );
768 static void X11ManageWindow             ( intf_thread_t *p_intf );
769 static void X11EnableScreenSaver        ( intf_thread_t *p_intf );
770 static void X11DisableScreenSaver       ( intf_thread_t *p_intf );
771 static void X11TogglePointer            ( intf_thread_t *p_intf );
772
773 /*****************************************************************************
774  * intf_X11Create: initialize and create window
775  *****************************************************************************/
776 int intf_X11Create( intf_thread_t *p_intf )
777 {
778     char       *psz_display;
779
780     /* Allocate instance and initialize some members */
781     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
782     if( p_intf->p_sys == NULL )
783     {
784         intf_ErrMsg("error: %s", strerror(ENOMEM));
785         return( 1 );
786     }
787
788     /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
789     psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
790     p_intf->p_sys->p_display = XOpenDisplay( psz_display );
791     if( !p_intf->p_sys->p_display )                                 /* error */
792     {
793         intf_ErrMsg("error: can't open display %s", psz_display );
794         free( p_intf->p_sys );
795         return( 1 );
796     }
797     p_intf->p_sys->i_screen = DefaultScreen( p_intf->p_sys->p_display );
798
799     /* Spawn base window - this window will include the video output window,
800      * but also command buttons, subtitles and other indicators */
801     if( X11CreateWindow( p_intf ) )
802     {
803         intf_ErrMsg("error: can't create interface window" );
804         XCloseDisplay( p_intf->p_sys->p_display );
805         free( p_intf->p_sys );
806         return( 1 );
807     }
808
809     /* Spawn video output thread */
810     if( p_main->b_video )
811     {
812         p_intf->p_vout = vout_CreateThread( psz_display, p_intf->p_sys->window,
813                                             p_intf->p_sys->i_width,
814                                             p_intf->p_sys->i_height, NULL, 0,
815                                             (void *)&p_intf->p_sys->colormap );
816
817         if( p_intf->p_vout == NULL )                                /* error */
818         {
819             intf_ErrMsg("error: can't create video output thread" );
820             X11DestroyWindow( p_intf );
821             XCloseDisplay( p_intf->p_sys->p_display );
822             free( p_intf->p_sys );
823             return( 1 );
824         }
825     }
826
827     p_intf->p_sys->b_mouse = 1;
828
829     /* bind keys */
830     intf_AssignNormalKeys( p_intf );
831     
832     /* Disable screen saver and return */
833     p_intf->p_sys->i_ss_count = 1;
834     X11DisableScreenSaver( p_intf );
835     return( 0 );
836 }
837
838 /*****************************************************************************
839  * intf_X11Destroy: destroy interface window
840  *****************************************************************************/
841 void intf_X11Destroy( intf_thread_t *p_intf )
842 {
843     /* Enable screen saver */
844     X11EnableScreenSaver( p_intf );
845
846     /* Close input thread, if any (blocking) */
847     if( p_intf->p_input )
848     {
849         input_DestroyThread( p_intf->p_input, NULL );
850     }
851
852     /* Close video output thread, if any (blocking) */
853     if( p_intf->p_vout )
854     {
855         vout_DestroyThread( p_intf->p_vout, NULL );
856     }
857
858     /* Close main window and display */
859     X11DestroyWindow( p_intf );
860     XCloseDisplay( p_intf->p_sys->p_display );
861
862     /* Destroy structure */
863     free( p_intf->p_sys );
864 }
865
866
867 /*****************************************************************************
868  * intf_X11Manage: event loop
869  *****************************************************************************/
870 void intf_X11Manage( intf_thread_t *p_intf )
871 {
872     /* Manage main window */
873     X11ManageWindow( p_intf );
874 }
875
876 /* following functions are local */
877
878 /*****************************************************************************
879  * X11CreateWindow: open and set-up X11 main window
880  *****************************************************************************/
881 static int X11CreateWindow( intf_thread_t *p_intf )
882 {
883     XSizeHints              xsize_hints;
884     XSetWindowAttributes    xwindow_attributes;
885     XGCValues               xgcvalues;
886     XEvent                  xevent;
887     boolean_t               b_expose;
888     boolean_t               b_configure_notify;
889     boolean_t               b_map_notify;
890
891     /* Set main window's size */
892     p_intf->p_sys->i_width =  main_GetIntVariable( VOUT_WIDTH_VAR,
893                                                    VOUT_WIDTH_DEFAULT );
894     p_intf->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
895                                                    VOUT_HEIGHT_DEFAULT );
896
897     /* Prepare window manager hints and properties */
898     xsize_hints.base_width =            p_intf->p_sys->i_width;
899     xsize_hints.base_height =           p_intf->p_sys->i_height;
900     xsize_hints.flags =                 PSize;
901     p_intf->p_sys->wm_protocols =       XInternAtom( p_intf->p_sys->p_display,
902                                                      "WM_PROTOCOLS", True );
903     p_intf->p_sys->wm_delete_window =   XInternAtom( p_intf->p_sys->p_display,
904                                                      "WM_DELETE_WINDOW", True );
905
906     /* Prepare window attributes */
907     xwindow_attributes.backing_store = Always;       /* save the hidden part */
908     xwindow_attributes.background_pixel = WhitePixel( p_intf->p_sys->p_display,
909                                                       p_intf->p_sys->i_screen );
910
911     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
912
913     /* Create the window and set hints - the window must receive ConfigureNotify
914      * events, and, until it is displayed, Expose and MapNotify events. */
915     p_intf->p_sys->window =
916             XCreateWindow( p_intf->p_sys->p_display,
917                            DefaultRootWindow( p_intf->p_sys->p_display ),
918                            0, 0,
919                            p_intf->p_sys->i_width, p_intf->p_sys->i_height, 1,
920                            0, InputOutput, 0,
921                            CWBackingStore | CWBackPixel | CWEventMask,
922                            &xwindow_attributes );
923
924     /* Set window manager hints and properties: size hints, command,
925      * window's name, and accepted protocols */
926     XSetWMNormalHints( p_intf->p_sys->p_display, p_intf->p_sys->window,
927                        &xsize_hints );
928     XSetCommand( p_intf->p_sys->p_display, p_intf->p_sys->window,
929                  p_main->ppsz_argv, p_main->i_argc );
930     XStoreName( p_intf->p_sys->p_display, p_intf->p_sys->window, VOUT_TITLE );
931
932     if( (p_intf->p_sys->wm_protocols == None)        /* use WM_DELETE_WINDOW */
933         || (p_intf->p_sys->wm_delete_window == None)
934         || !XSetWMProtocols( p_intf->p_sys->p_display, p_intf->p_sys->window,
935                              &p_intf->p_sys->wm_delete_window, 1 ) )
936     {
937         /* WM_DELETE_WINDOW is not supported by window manager */
938         intf_Msg("intf error: missing or bad window manager - please exit program kindly.");
939     }
940
941     /* Creation of a graphic context that doesn't generate a GraphicsExpose
942      * event when using functions like XCopyArea */
943     xgcvalues.graphics_exposures = False;
944     p_intf->p_sys->gc =  XCreateGC( p_intf->p_sys->p_display, p_intf->p_sys->window,
945                                     GCGraphicsExposures, &xgcvalues);
946
947     /* Send orders to server, and wait until window is displayed - three
948      * events must be received: a MapNotify event, an Expose event allowing
949      * drawing in the window, and a ConfigureNotify to get the window
950      * dimensions. Once those events have been received, only ConfigureNotify
951      * events need to be received. */
952     b_expose = 0;
953     b_configure_notify = 0;
954     b_map_notify = 0;
955     XMapWindow( p_intf->p_sys->p_display, p_intf->p_sys->window);
956     do
957     {
958         XNextEvent( p_intf->p_sys->p_display, &xevent);
959         if( (xevent.type == Expose)
960             && (xevent.xexpose.window == p_intf->p_sys->window) )
961         {
962             b_expose = 1;
963         }
964         else if( (xevent.type == MapNotify)
965                  && (xevent.xmap.window == p_intf->p_sys->window) )
966         {
967             b_map_notify = 1;
968         }
969         else if( (xevent.type == ConfigureNotify)
970                  && (xevent.xconfigure.window == p_intf->p_sys->window) )
971         {
972             b_configure_notify = 1;
973             p_intf->p_sys->i_width = xevent.xconfigure.width;
974             p_intf->p_sys->i_height = xevent.xconfigure.height;
975         }
976     } while( !( b_expose && b_configure_notify && b_map_notify ) );
977
978     XSelectInput( p_intf->p_sys->p_display, p_intf->p_sys->window,
979                   StructureNotifyMask | KeyPressMask | ButtonPressMask );
980
981     if( XDefaultDepth(p_intf->p_sys->p_display, p_intf->p_sys->i_screen) == 8 )
982     {
983         /* Allocate a new palette */
984         p_intf->p_sys->colormap = XCreateColormap( p_intf->p_sys->p_display,
985                               DefaultRootWindow( p_intf->p_sys->p_display ),
986                               DefaultVisual( p_intf->p_sys->p_display,
987                                              p_intf->p_sys->i_screen ),
988                               AllocAll );
989
990         xwindow_attributes.colormap = p_intf->p_sys->colormap;
991         XChangeWindowAttributes( p_intf->p_sys->p_display,
992                                  p_intf->p_sys->window,
993                                  CWColormap, &xwindow_attributes );
994     }
995
996     /* At this stage, the window is open, displayed, and ready to receive data */
997     return( 0 );
998 }
999
1000 /*****************************************************************************
1001  * X11DestroyWindow: destroy X11 main window
1002  *****************************************************************************/
1003 static void X11DestroyWindow( intf_thread_t *p_intf )
1004 {
1005     XUnmapWindow( p_intf->p_sys->p_display, p_intf->p_sys->window );
1006     XFreeGC( p_intf->p_sys->p_display, p_intf->p_sys->gc );
1007     XDestroyWindow( p_intf->p_sys->p_display, p_intf->p_sys->window );
1008 }
1009
1010 /*****************************************************************************
1011  * X11ManageWindow: manage X11 main window
1012  *****************************************************************************/
1013 static void X11ManageWindow( intf_thread_t *p_intf )
1014 {
1015     XEvent      xevent;                                         /* X11 event */
1016     boolean_t   b_resized;                        /* window has been resized */
1017     char        i_key;                                    /* ISO Latin-1 key */
1018
1019     /* Handle X11 events: ConfigureNotify events are parsed to know if the
1020      * output window's size changed, MapNotify and UnmapNotify to know if the
1021      * window is mapped (and if the display is useful), and ClientMessages
1022      * to intercept window destruction requests */
1023     b_resized = 0;
1024     while( XCheckWindowEvent( p_intf->p_sys->p_display, p_intf->p_sys->window,
1025                               StructureNotifyMask | KeyPressMask |
1026                               ButtonPressMask, &xevent ) == True )
1027     {
1028         /* ConfigureNotify event: prepare  */
1029         if( (xevent.type == ConfigureNotify)
1030             && ((xevent.xconfigure.width != p_intf->p_sys->i_width)
1031                 || (xevent.xconfigure.height != p_intf->p_sys->i_height)) )
1032         {
1033             /* Update dimensions */
1034             b_resized = 1;
1035             p_intf->p_sys->i_width = xevent.xconfigure.width;
1036             p_intf->p_sys->i_height = xevent.xconfigure.height;
1037         }
1038         /* MapNotify event: change window status and disable screen saver */
1039         else if( xevent.type == MapNotify)
1040         {
1041             if( (p_intf->p_vout != NULL) && !p_intf->p_vout->b_active )
1042             {
1043                 X11DisableScreenSaver( p_intf );
1044                 p_intf->p_vout->b_active = 1;
1045             }
1046         }
1047         /* UnmapNotify event: change window status and enable screen saver */
1048         else if( xevent.type == UnmapNotify )
1049         {
1050             if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_active )
1051             {
1052                 X11EnableScreenSaver( p_intf );
1053                 p_intf->p_vout->b_active = 0;
1054             }
1055         }
1056         /* Keyboard event */
1057         else if( xevent.type == KeyPress )
1058         {
1059             if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
1060             {
1061                 if( intf_ProcessKey( p_intf, i_key ) )
1062                 {
1063                     intf_DbgMsg("unhandled key '%c' (%i)", (char) i_key, i_key );
1064                 }
1065             }
1066         }
1067         /* Mouse click */
1068         else if( xevent.type == ButtonPress )
1069         {
1070             switch( ((XButtonEvent *)&xevent)->button )
1071             {
1072                 case Button1:
1073                     /* in this part we will eventually manage
1074                      * clicks for DVD navigation for instance */
1075                     break;
1076
1077                 case Button2:
1078                     X11TogglePointer( p_intf );
1079                     break;
1080
1081                 case Button3:
1082                     vlc_mutex_lock( &p_intf->p_vout->change_lock );
1083                     p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
1084                     p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
1085                     vlc_mutex_unlock( &p_intf->p_vout->change_lock );
1086                     break;
1087             }
1088         }
1089 #ifdef DEBUG
1090         /* Other event */
1091         else
1092         {
1093             intf_DbgMsg( "%p -> unhandled event type %d received",
1094                          p_intf, xevent.type );
1095         }
1096 #endif
1097     }
1098
1099     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1100      * are handled - according to the man pages, the format is always 32
1101      * in this case */
1102     while( XCheckTypedEvent( p_intf->p_sys->p_display,
1103                              ClientMessage, &xevent ) )
1104     {
1105         if( (xevent.xclient.message_type == p_intf->p_sys->wm_protocols)
1106             && (xevent.xclient.data.l[0] == p_intf->p_sys->wm_delete_window ) )
1107         {
1108             p_intf->b_die = 1;
1109         }
1110         else
1111         {
1112             intf_DbgMsg( "%p -> unhandled ClientMessage received", p_intf );
1113         }
1114     }
1115
1116     /*
1117      * Handle vout or interface windows resizing
1118      */
1119     if( p_intf->p_vout != NULL )
1120     {
1121         if( b_resized )
1122         {
1123             /* If interface window has been resized, change vout size */
1124             intf_DbgMsg( "resizing output window" );
1125             vlc_mutex_lock( &p_intf->p_vout->change_lock );
1126             p_intf->p_vout->i_width =  p_intf->p_sys->i_width;
1127             p_intf->p_vout->i_height = p_intf->p_sys->i_height;
1128             p_intf->p_vout->i_changes |= VOUT_SIZE_CHANGE;
1129             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
1130         }
1131         else if( (p_intf->p_vout->i_width  != p_intf->p_sys->i_width) ||
1132                  (p_intf->p_vout->i_height != p_intf->p_sys->i_height) )
1133         {
1134            /* If video output size has changed, change interface window size */
1135             intf_DbgMsg( "resizing output window" );
1136             p_intf->p_sys->i_width =    p_intf->p_vout->i_width;
1137             p_intf->p_sys->i_height =   p_intf->p_vout->i_height;
1138             XResizeWindow( p_intf->p_sys->p_display, p_intf->p_sys->window,
1139                            p_intf->p_sys->i_width, p_intf->p_sys->i_height );
1140         }
1141     }
1142 }
1143
1144 /*****************************************************************************
1145  * X11EnableScreenSaver: enable screen saver
1146  *****************************************************************************
1147  * This function enable the screen saver on a display after it had been
1148  * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
1149  * know wether the screen saver can be activated or not: if n successive calls
1150  * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
1151  * will be required before the screen saver could effectively be activated.
1152  *****************************************************************************/
1153 void X11EnableScreenSaver( intf_thread_t *p_intf )
1154 {
1155     if( p_intf->p_sys->i_ss_count++ == 0 )
1156     {
1157         intf_DbgMsg( "intf: enabling screen saver" );
1158         XSetScreenSaver( p_intf->p_sys->p_display, p_intf->p_sys->i_ss_timeout,
1159                          p_intf->p_sys->i_ss_interval, p_intf->p_sys->i_ss_blanking,
1160                          p_intf->p_sys->i_ss_exposure );
1161     }
1162 }
1163
1164 /*****************************************************************************
1165  * X11DisableScreenSaver: disable screen saver
1166  *****************************************************************************
1167  * See XEnableScreenSaver
1168  *****************************************************************************/
1169 void X11DisableScreenSaver( intf_thread_t *p_intf )
1170 {
1171     if( --p_intf->p_sys->i_ss_count == 0 )
1172     {
1173         /* Save screen saver informations */
1174         XGetScreenSaver( p_intf->p_sys->p_display, &p_intf->p_sys->i_ss_timeout,
1175                          &p_intf->p_sys->i_ss_interval, &p_intf->p_sys->i_ss_blanking,
1176                          &p_intf->p_sys->i_ss_exposure );
1177
1178         /* Disable screen saver */
1179         intf_DbgMsg("intf: disabling screen saver");
1180         XSetScreenSaver( p_intf->p_sys->p_display, 0,
1181                          p_intf->p_sys->i_ss_interval, p_intf->p_sys->i_ss_blanking,
1182                          p_intf->p_sys->i_ss_exposure );
1183     }
1184 }
1185
1186 /*****************************************************************************
1187  * X11TogglePointer: hide or show the mouse pointer
1188  *****************************************************************************
1189  * This function hides the X pointer if it is visible by putting it at
1190  * coordinates (32,32) and setting the pointer sprite to a blank one. To
1191  * show it again, we disable the sprite and restore the original coordinates.
1192  *****************************************************************************/
1193 void X11TogglePointer( intf_thread_t *p_intf )
1194 {
1195     static Cursor cursor;
1196     static boolean_t b_cursor = 0;
1197
1198     if( p_intf->p_sys->b_mouse )
1199     {
1200         p_intf->p_sys->b_mouse = 0;
1201
1202         if( !b_cursor )
1203         {
1204             XColor color;
1205             Pixmap blank = XCreatePixmap( p_intf->p_sys->p_display,
1206                                DefaultRootWindow(p_intf->p_sys->p_display),
1207                                1, 1, 1 );
1208
1209             XParseColor( p_intf->p_sys->p_display,
1210                          XCreateColormap( p_intf->p_sys->p_display,
1211                                           DefaultRootWindow(
1212                                                   p_intf->p_sys->p_display ),
1213                                           DefaultVisual(
1214                                                   p_intf->p_sys->p_display,
1215                                                   p_intf->p_sys->i_screen ),
1216                                           AllocNone ),
1217                          "black", &color );
1218
1219             cursor = XCreatePixmapCursor( p_intf->p_sys->p_display,
1220                            blank, blank, &color, &color, 1, 1 );
1221
1222             b_cursor = 1;
1223         }
1224         XDefineCursor( p_intf->p_sys->p_display,
1225                        p_intf->p_sys->window, cursor );
1226     }
1227     else
1228     {
1229         p_intf->p_sys->b_mouse = 1;
1230
1231         XUndefineCursor( p_intf->p_sys->p_display, p_intf->p_sys->window );
1232     }
1233 }