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