]> git.sesse.net Git - vlc/blob - modules/video_output/x11/xcommon.c
*: a new mouse gestures interface, currently supporting back, forward, quit
[vlc] / modules / video_output / x11 / xcommon.c
1 /*****************************************************************************
2  * xcommon.c: Functions common to the X11 and XVideo plugins
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: xcommon.c,v 1.14 2003/02/09 23:42:06 sigmunau Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          David Kennedy <dkennedy@tinytoad.com>
10  *          Gildas Bazin <gbazin@netcourrier.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <string.h>                                            /* strerror() */
33
34 #include <vlc/vlc.h>
35 #include <vlc/intf.h>
36 #include <vlc/vout.h>
37
38 #ifdef HAVE_MACHINE_PARAM_H
39     /* BSD */
40 #   include <machine/param.h>
41 #   include <sys/types.h>                                  /* typedef ushort */
42 #   include <sys/ipc.h>
43 #endif
44
45 #ifndef WIN32
46 #   include <netinet/in.h>                            /* BSD: struct in_addr */
47 #endif
48
49 #ifdef HAVE_SYS_SHM_H
50 #   include <sys/shm.h>                                /* shmget(), shmctl() */
51 #endif
52
53 #include <X11/Xlib.h>
54 #include <X11/Xmd.h>
55 #include <X11/Xutil.h>
56 #include <X11/keysym.h>
57 #ifdef HAVE_SYS_SHM_H
58 #   include <X11/extensions/XShm.h>
59 #endif
60 #ifdef DPMSINFO_IN_DPMS_H
61 #   include <X11/extensions/dpms.h>
62 #endif
63
64 #ifdef MODULE_NAME_IS_xvideo
65 #   include <X11/extensions/Xv.h>
66 #   include <X11/extensions/Xvlib.h>
67 #endif
68
69 #include "netutils.h"                                 /* network_ChannelJoin */
70
71 #include "xcommon.h"
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 int  E_(Activate)   ( vlc_object_t * );
77 void E_(Deactivate) ( vlc_object_t * );
78
79 static int  InitVideo      ( vout_thread_t * );
80 static void EndVideo       ( vout_thread_t * );
81 static void DisplayVideo   ( vout_thread_t *, picture_t * );
82 static int  ManageVideo    ( vout_thread_t * );
83
84 static int  InitDisplay    ( vout_thread_t * );
85
86 static int  CreateWindow   ( vout_thread_t *, x11_window_t * );
87 static void DestroyWindow  ( vout_thread_t *, x11_window_t * );
88
89 static int  NewPicture     ( vout_thread_t *, picture_t * );
90 static void FreePicture    ( vout_thread_t *, picture_t * );
91
92 static IMAGE_TYPE *CreateImage    ( vout_thread_t *,
93                                     Display *, EXTRA_ARGS, int, int );
94 #ifdef HAVE_SYS_SHM_H
95 static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
96                                     Display *, EXTRA_ARGS_SHM, int, int );
97 #endif
98
99 static void ToggleFullScreen      ( vout_thread_t * );
100
101 static void EnableXScreenSaver    ( vout_thread_t * );
102 static void DisableXScreenSaver   ( vout_thread_t * );
103
104 static void CreateCursor   ( vout_thread_t * );
105 static void DestroyCursor  ( vout_thread_t * );
106 static void ToggleCursor   ( vout_thread_t * );
107
108 #ifdef MODULE_NAME_IS_xvideo
109 static int  XVideoGetPort    ( vout_thread_t *, vlc_fourcc_t, vlc_fourcc_t * );
110 static void XVideoReleasePort( vout_thread_t *, int );
111 #endif
112
113 #ifdef MODULE_NAME_IS_x11
114 static void SetPalette     ( vout_thread_t *,
115                              uint16_t *, uint16_t *, uint16_t * );
116 #endif
117
118 /*****************************************************************************
119  * Activate: allocate X11 video thread output method
120  *****************************************************************************
121  * This function allocate and initialize a X11 vout method. It uses some of the
122  * vout properties to choose the window size, and change them according to the
123  * actual properties of the display.
124  *****************************************************************************/
125 int E_(Activate) ( vlc_object_t *p_this )
126 {
127     vout_thread_t *p_vout = (vout_thread_t *)p_this;
128     char *       psz_display;
129 #ifdef MODULE_NAME_IS_xvideo
130     char *       psz_chroma;
131     vlc_fourcc_t i_chroma = 0;
132     vlc_bool_t   b_chroma = 0;
133 #endif
134
135     p_vout->pf_init = InitVideo;
136     p_vout->pf_end = EndVideo;
137     p_vout->pf_manage = ManageVideo;
138     p_vout->pf_render = NULL;
139     p_vout->pf_display = DisplayVideo;
140
141     /* Allocate structure */
142     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
143     if( p_vout->p_sys == NULL )
144     {
145         msg_Err( p_vout, "out of memory" );
146         return VLC_ENOMEM;
147     }
148
149     /* Open display, using the "display" config variable or the DISPLAY
150      * environment variable */
151     psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
152
153     p_vout->p_sys->p_display = XOpenDisplay( psz_display );
154
155     if( p_vout->p_sys->p_display == NULL )                          /* error */
156     {
157         msg_Err( p_vout, "cannot open display %s",
158                          XDisplayName( psz_display ) );
159         free( p_vout->p_sys );
160         if( psz_display ) free( psz_display );
161         return VLC_EGENERIC;
162     }
163     if( psz_display ) free( psz_display );
164
165     /* Get a screen ID matching the XOpenDisplay return value */
166     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
167
168 #ifdef MODULE_NAME_IS_xvideo
169     psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
170     if( psz_chroma )
171     {
172         if( strlen( psz_chroma ) >= 4 )
173         {
174             i_chroma  = (unsigned char)psz_chroma[0] <<  0;
175             i_chroma |= (unsigned char)psz_chroma[1] <<  8;
176             i_chroma |= (unsigned char)psz_chroma[2] << 16;
177             i_chroma |= (unsigned char)psz_chroma[3] << 24;
178
179             b_chroma = 1;
180         }
181
182         free( psz_chroma );
183     }
184
185     if( b_chroma )
186     {
187         msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
188                  i_chroma, (char*)&i_chroma );
189     }
190     else
191     {
192         i_chroma = p_vout->render.i_chroma;
193     }
194
195     /* Check that we have access to an XVideo port providing this chroma */
196     p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, i_chroma,
197                                              &p_vout->output.i_chroma );
198     if( p_vout->p_sys->i_xvport < 0 )
199     {
200         /* If a specific chroma format was requested, then we don't try to
201          * be cleverer than the user. He knew pretty well what he wanted. */
202         if( b_chroma )
203         {
204             XCloseDisplay( p_vout->p_sys->p_display );
205             free( p_vout->p_sys );
206             return VLC_EGENERIC;
207         }
208
209         /* It failed, but it's not completely lost ! We try to open an
210          * XVideo port for an YUY2 picture. We'll need to do an YUV
211          * conversion, but at least it has got scaling. */
212         p_vout->p_sys->i_xvport =
213                         XVideoGetPort( p_vout, VLC_FOURCC('Y','U','Y','2'),
214                                                &p_vout->output.i_chroma );
215         if( p_vout->p_sys->i_xvport < 0 )
216         {
217             /* It failed, but it's not completely lost ! We try to open an
218              * XVideo port for a simple 16bpp RGB picture. We'll need to do
219              * an YUV conversion, but at least it has got scaling. */
220             p_vout->p_sys->i_xvport =
221                             XVideoGetPort( p_vout, VLC_FOURCC('R','V','1','6'),
222                                                    &p_vout->output.i_chroma );
223             if( p_vout->p_sys->i_xvport < 0 )
224             {
225                 XCloseDisplay( p_vout->p_sys->p_display );
226                 free( p_vout->p_sys );
227                 return VLC_EGENERIC;
228             }
229         }
230     }
231 #endif
232
233     /* Create blank cursor (for mouse cursor autohiding) */
234     p_vout->p_sys->i_time_mouse_last_moved = mdate();
235     p_vout->p_sys->b_mouse_pointer_visible = 1;
236     CreateCursor( p_vout );
237
238     /* Set main window's size */
239     p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
240     p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
241
242     /* Spawn base window - this window will include the video output window,
243      * but also command buttons, subtitles and other indicators */
244     if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
245     {
246         msg_Err( p_vout, "cannot create X11 window" );
247         DestroyCursor( p_vout );
248         XCloseDisplay( p_vout->p_sys->p_display );
249         free( p_vout->p_sys );
250         return VLC_EGENERIC;
251     }
252
253     /* Open and initialize device. */
254     if( InitDisplay( p_vout ) )
255     {
256         msg_Err( p_vout, "cannot initialize X11 display" );
257         DestroyCursor( p_vout );
258         DestroyWindow( p_vout, &p_vout->p_sys->original_window );
259         XCloseDisplay( p_vout->p_sys->p_display );
260         free( p_vout->p_sys );
261         return VLC_EGENERIC;
262     }
263
264     /* Disable screen saver */
265     DisableXScreenSaver( p_vout );
266
267     /* Misc init */
268     p_vout->p_sys->b_altfullscreen = 0;
269     p_vout->p_sys->i_time_button_last_pressed = 0;
270
271     return VLC_SUCCESS;
272 }
273
274 /*****************************************************************************
275  * Deactivate: destroy X11 video thread output method
276  *****************************************************************************
277  * Terminate an output method created by Open
278  *****************************************************************************/
279 void E_(Deactivate) ( vlc_object_t *p_this )
280 {
281     vout_thread_t *p_vout = (vout_thread_t *)p_this;
282
283     /* If the fullscreen window is still open, close it */
284     if( p_vout->b_fullscreen )
285     {
286         ToggleFullScreen( p_vout );
287     }
288
289     /* Restore cursor if it was blanked */
290     if( !p_vout->p_sys->b_mouse_pointer_visible )
291     {
292         ToggleCursor( p_vout );
293     }
294
295 #ifdef MODULE_NAME_IS_x11
296     /* Destroy colormap */
297     if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
298     {
299         XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
300     }
301 #else
302     XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
303 #endif
304
305     DestroyCursor( p_vout );
306     EnableXScreenSaver( p_vout );
307     DestroyWindow( p_vout, &p_vout->p_sys->original_window );
308
309     XCloseDisplay( p_vout->p_sys->p_display );
310
311     /* Destroy structure */
312     free( p_vout->p_sys );
313 }
314
315 /*****************************************************************************
316  * InitVideo: initialize X11 video thread output method
317  *****************************************************************************
318  * This function create the XImages needed by the output thread. It is called
319  * at the beginning of the thread, but also each time the window is resized.
320  *****************************************************************************/
321 static int InitVideo( vout_thread_t *p_vout )
322 {
323     int i_index;
324     picture_t *p_pic;
325
326     I_OUTPUTPICTURES = 0;
327
328 #ifdef MODULE_NAME_IS_xvideo
329     /* Initialize the output structure; we already found an XVideo port,
330      * and the corresponding chroma we will be using. Since we can
331      * arbitrary scale, stick to the coordinates and aspect. */
332     p_vout->output.i_width  = p_vout->render.i_width;
333     p_vout->output.i_height = p_vout->render.i_height;
334     p_vout->output.i_aspect = p_vout->render.i_aspect;
335
336     switch( p_vout->output.i_chroma )
337     {
338         case VLC_FOURCC('R','V','1','5'):
339             p_vout->output.i_rmask = 0x001f;
340             p_vout->output.i_gmask = 0x07e0;
341             p_vout->output.i_bmask = 0xf800;
342             break;
343         case VLC_FOURCC('R','V','1','6'):
344             p_vout->output.i_rmask = 0x001f;
345             p_vout->output.i_gmask = 0x03e0;
346             p_vout->output.i_bmask = 0x7c00;
347             break;
348     }
349
350 #else
351     /* Initialize the output structure: RGB with square pixels, whatever
352      * the input format is, since it's the only format we know */
353     switch( p_vout->p_sys->i_screen_depth )
354     {
355         case 8: /* FIXME: set the palette */
356             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
357         case 15:
358             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
359         case 16:
360             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
361         case 24:
362             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
363         case 32:
364             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
365         default:
366             msg_Err( p_vout, "unknown screen depth %i",
367                      p_vout->p_sys->i_screen_depth );
368             return VLC_SUCCESS;
369     }
370
371     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
372                        p_vout->p_sys->p_win->i_height,
373                        &i_index, &i_index,
374                        &p_vout->output.i_width, &p_vout->output.i_height );
375
376     /* Assume we have square pixels */
377     p_vout->output.i_aspect = p_vout->output.i_width
378                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
379 #endif
380
381     /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
382     while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
383     {
384         p_pic = NULL;
385
386         /* Find an empty picture slot */
387         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
388         {
389           if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
390             {
391                 p_pic = p_vout->p_picture + i_index;
392                 break;
393             }
394         }
395
396         /* Allocate the picture */
397         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
398         {
399             break;
400         }
401
402         p_pic->i_status = DESTROYED_PICTURE;
403         p_pic->i_type   = DIRECT_PICTURE;
404
405         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
406
407         I_OUTPUTPICTURES++;
408     }
409
410     return VLC_SUCCESS;
411 }
412
413  /*****************************************************************************
414  * DisplayVideo: displays previously rendered output
415  *****************************************************************************
416  * This function sends the currently rendered image to X11 server.
417  * (The Xv extension takes care of "double-buffering".)
418  *****************************************************************************/
419 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
420 {
421     int i_width, i_height, i_x, i_y;
422
423     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
424                        p_vout->p_sys->p_win->i_height,
425                        &i_x, &i_y, &i_width, &i_height );
426
427 #ifdef HAVE_SYS_SHM_H
428     if( p_vout->p_sys->b_shm )
429     {
430         /* Display rendered image using shared memory extension */
431 #   ifdef MODULE_NAME_IS_xvideo
432         XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
433                        p_vout->p_sys->p_win->video_window,
434                        p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
435                        0 /*src_x*/, 0 /*src_y*/,
436                        p_vout->output.i_width, p_vout->output.i_height,
437                        0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
438                        False /* Don't put True here or you'll waste your CPU */ );
439 #   else
440         XShmPutImage( p_vout->p_sys->p_display,
441                       p_vout->p_sys->p_win->video_window,
442                       p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
443                       0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
444                       p_vout->output.i_width, p_vout->output.i_height,
445                       False /* Don't put True here ! */ );
446 #   endif
447     }
448     else
449 #endif /* HAVE_SYS_SHM_H */
450     {
451         /* Use standard XPutImage -- this is gonna be slow ! */
452 #ifdef MODULE_NAME_IS_xvideo
453         XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
454                     p_vout->p_sys->p_win->video_window,
455                     p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
456                     0 /*src_x*/, 0 /*src_y*/,
457                     p_vout->output.i_width, p_vout->output.i_height,
458                     0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
459 #else
460         XPutImage( p_vout->p_sys->p_display,
461                    p_vout->p_sys->p_win->video_window,
462                    p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
463                    0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
464                    p_vout->output.i_width, p_vout->output.i_height );
465 #endif
466     }
467
468     /* Make sure the command is sent now - do NOT use XFlush !*/
469     XSync( p_vout->p_sys->p_display, False );
470 }
471
472 /*****************************************************************************
473  * ManageVideo: handle X11 events
474  *****************************************************************************
475  * This function should be called regularly by video output thread. It manages
476  * X11 events and allows window resizing. It returns a non null value on
477  * error.
478  *****************************************************************************/
479 static int ManageVideo( vout_thread_t *p_vout )
480 {
481     XEvent      xevent;                                         /* X11 event */
482     char        i_key;                                    /* ISO Latin-1 key */
483     KeySym      x_key_symbol;
484     vlc_value_t val;
485
486     /* Handle X11 events: ConfigureNotify events are parsed to know if the
487      * output window's size changed, MapNotify and UnmapNotify to know if the
488      * window is mapped (and if the display is useful), and ClientMessages
489      * to intercept window destruction requests */
490
491     while( XCheckWindowEvent( p_vout->p_sys->p_display,
492                               p_vout->p_sys->p_win->base_window,
493                               StructureNotifyMask | KeyPressMask |
494                               ButtonPressMask | ButtonReleaseMask |
495                               PointerMotionMask | Button1MotionMask , &xevent )
496            == True )
497     {
498         /* ConfigureNotify event: prepare  */
499         if( xevent.type == ConfigureNotify )
500         {
501             if( (unsigned int)xevent.xconfigure.width
502                    != p_vout->p_sys->p_win->i_width
503               || (unsigned int)xevent.xconfigure.height
504                     != p_vout->p_sys->p_win->i_height )
505             {
506                 /* Update dimensions */
507                 p_vout->i_changes |= VOUT_SIZE_CHANGE;
508                 p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
509                 p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
510             }
511         }
512         /* Keyboard event */
513         else if( xevent.type == KeyPress )
514         {
515             /* We may have keys like F1 trough F12, ESC ... */
516             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
517                                              xevent.xkey.keycode, 0 );
518             switch( (int)x_key_symbol )
519             {
520             case XK_Escape:
521                 if( p_vout->b_fullscreen )
522                 {
523                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
524                 }
525                 else
526                 {
527                     p_vout->p_vlc->b_die = 1;
528                 }
529                 break;
530             case XK_Menu:
531                 {
532                     intf_thread_t *p_intf;
533                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
534                                                       FIND_ANYWHERE );
535                     if( p_intf )
536                     {
537                         p_intf->b_menu_change = 1;
538                         vlc_object_release( p_intf );
539                     }
540                 }
541                 break;
542             case XK_Left:
543                 input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
544                 break;
545             case XK_Right:
546                 input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
547                 break;
548             case XK_Up:
549                 input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
550                 break;
551             case XK_Down:
552                 input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
553                 break;
554             case XK_Home:
555                 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
556                 break;
557             case XK_End:
558                 input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
559                 break;
560             case XK_Page_Up:
561                 input_Seek( p_vout, 900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
562                 break;
563             case XK_Page_Down:
564                 input_Seek( p_vout, -900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
565                 break;
566             case XK_space:
567                 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
568                 break;
569
570             case XK_F1: network_ChannelJoin( p_vout, 1 ); break;
571             case XK_F2: network_ChannelJoin( p_vout, 2 ); break;
572             case XK_F3: network_ChannelJoin( p_vout, 3 ); break;
573             case XK_F4: network_ChannelJoin( p_vout, 4 ); break;
574             case XK_F5: network_ChannelJoin( p_vout, 5 ); break;
575             case XK_F6: network_ChannelJoin( p_vout, 6 ); break;
576             case XK_F7: network_ChannelJoin( p_vout, 7 ); break;
577             case XK_F8: network_ChannelJoin( p_vout, 8 ); break;
578             case XK_F9: network_ChannelJoin( p_vout, 9 ); break;
579             case XK_F10: network_ChannelJoin( p_vout, 10 ); break;
580             case XK_F11: network_ChannelJoin( p_vout, 11 ); break;
581             case XK_F12: network_ChannelJoin( p_vout, 12 ); break;
582
583             default:
584                 /* "Normal Keys"
585                  * The reason why I use this instead of XK_0 is that
586                  * with XLookupString, we don't have to care about
587                  * keymaps. */
588
589                 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
590                 {
591                     /* FIXME: handle stuff here */
592                     switch( i_key )
593                     {
594                     case 'q':
595                     case 'Q':
596                         p_vout->p_vlc->b_die = 1;
597                         break;
598                     case 'f':
599                     case 'F':
600                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
601                         break;
602
603                     default:
604                         break;
605                     }
606                 }
607                 break;
608             }
609         }
610         /* Mouse click */
611         else if( xevent.type == ButtonPress )
612         {
613             switch( ((XButtonEvent *)&xevent)->button )
614             {
615                 case Button1:
616                     var_Get( p_vout, "mouse-button-down", &val );
617                     val.i_int |= 1;
618                     var_Set( p_vout, "mouse-button-down", val );
619                     
620                     /* detect double-clicks */
621                     if( ( ((XButtonEvent *)&xevent)->time -
622                           p_vout->p_sys->i_time_button_last_pressed ) < 300 )
623                     {
624                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
625                     }
626
627                     p_vout->p_sys->i_time_button_last_pressed =
628                         ((XButtonEvent *)&xevent)->time;
629                     break;
630                 case Button2:
631                     var_Get( p_vout, "mouse-button-down", &val );
632                     val.i_int |= 2;
633                     var_Set( p_vout, "mouse-button-down", val );
634                     break;
635                 
636                 case Button3:
637                     var_Get( p_vout, "mouse-button-down", &val );
638                     val.i_int |= 4;
639                     var_Set( p_vout, "mouse-button-down", val );
640                     break;
641                 
642                 case Button4:
643                     var_Get( p_vout, "mouse-button-down", &val );
644                     val.i_int |= 8;
645                     var_Set( p_vout, "mouse-button-down", val );
646                     input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
647                     break;
648
649                 case Button5:
650                     var_Get( p_vout, "mouse-button-down", &val );
651                     val.i_int |= 16;
652                     var_Set( p_vout, "mouse-button-down", val );
653                     input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
654                     break;
655             }
656         }
657         /* Mouse release */
658         else if( xevent.type == ButtonRelease )
659         {
660             switch( ((XButtonEvent *)&xevent)->button )
661             {
662                 case Button1:
663                     var_Get( p_vout, "mouse-button-down", &val );
664                     val.i_int &= ~1;
665                     var_Set( p_vout, "mouse-button-down", val );
666
667                     val.b_bool = VLC_TRUE;
668                     var_Set( p_vout, "mouse-clicked", val );
669                     break;
670                     
671                 case Button2:
672                     var_Get( p_vout, "mouse-button-down", &val );
673                     val.i_int &= ~2;
674                     var_Set( p_vout, "mouse-button-down", val );
675                     break;
676                     
677                 case Button3:
678                     {
679                         intf_thread_t *p_intf;
680                         var_Get( p_vout, "mouse-button-down", &val );
681                         val.i_int &= ~4;
682                         var_Set( p_vout, "mouse-button-down", val );
683                         p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
684                                                           FIND_ANYWHERE );
685                         if( p_intf )
686                         {
687                             p_intf->b_menu_change = 1;
688                             vlc_object_release( p_intf );
689                         }
690                     }
691                     break;
692
693                 case Button4:
694                     var_Get( p_vout, "mouse-button-down", &val );
695                     val.i_int &= ~8;
696                     var_Set( p_vout, "mouse-button-down", val );
697                     break;
698
699                 case Button5:
700                     var_Get( p_vout, "mouse-button-down", &val );
701                     val.i_int &= ~16;
702                     var_Set( p_vout, "mouse-button-down", val );
703                     break;
704                     
705             }
706         }
707         /* Mouse move */
708         else if( xevent.type == MotionNotify )
709         {
710             int i_width, i_height, i_x, i_y;
711             vlc_value_t val;
712
713             /* somewhat different use for vout_PlacePicture:
714              * here the values are needed to give to mouse coordinates
715              * in the original picture space */
716             vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
717                                p_vout->p_sys->p_win->i_height,
718                                &i_x, &i_y, &i_width, &i_height );
719
720             val.i_int = ( xevent.xmotion.x - i_x )
721                          * p_vout->render.i_width / i_width;
722             var_Set( p_vout, "mouse-x", val );
723             val.i_int = ( xevent.xmotion.y - i_y )
724                          * p_vout->render.i_height / i_height;
725             var_Set( p_vout, "mouse-y", val );
726
727             val.b_bool = VLC_TRUE;
728             var_Set( p_vout, "mouse-moved", val );
729
730             p_vout->p_sys->i_time_mouse_last_moved = mdate();
731             if( ! p_vout->p_sys->b_mouse_pointer_visible )
732             {
733                 ToggleCursor( p_vout );
734             }
735         }
736         else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
737                   || xevent.type == MapNotify
738                   || xevent.type == UnmapNotify )
739         {
740             /* Ignore these events */
741         }
742         else /* Other events */
743         {
744             msg_Warn( p_vout, "unhandled event %d received", xevent.type );
745         }
746     }
747
748     /* Handle events for video output sub-window */
749     while( XCheckWindowEvent( p_vout->p_sys->p_display,
750                               p_vout->p_sys->p_win->video_window,
751                               ExposureMask, &xevent ) == True )
752     {
753         /* Window exposed (only handled if stream playback is paused) */
754         if( xevent.type == Expose )
755         {
756             if( ((XExposeEvent *)&xevent)->count == 0 )
757             {
758                 /* (if this is the last a collection of expose events...) */
759 #if 0
760                 if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
761                 {
762                     if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
763                                                    ->stream.control.i_status )
764                     {
765                         /* XVideoDisplay( p_vout )*/;
766                     }
767                 }
768 #endif
769             }
770         }
771     }
772
773     /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
774      * are handled - according to the man pages, the format is always 32
775      * in this case */
776     while( XCheckTypedEvent( p_vout->p_sys->p_display,
777                              ClientMessage, &xevent ) )
778     {
779         if( (xevent.xclient.message_type == p_vout->p_sys->p_win->wm_protocols)
780                && ((Atom)xevent.xclient.data.l[0]
781                      == p_vout->p_sys->p_win->wm_delete_window ) )
782         {
783             p_vout->p_vlc->b_die = 1;
784         }
785     }
786
787     /*
788      * Fullscreen Change
789      */
790     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
791     {
792         ToggleFullScreen( p_vout );
793         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
794     }
795
796     /*
797      * Size change
798      *
799      * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
800      *  the size flag inside the fullscreen routine)
801      */
802     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
803     {
804         int i_width, i_height, i_x, i_y;
805
806         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
807
808 #ifdef MODULE_NAME_IS_x11
809         /* We need to signal the vout thread about the size change because it
810          * is doing the rescaling */
811         p_vout->i_changes |= VOUT_SIZE_CHANGE;
812 #endif
813
814         vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
815                            p_vout->p_sys->p_win->i_height,
816                            &i_x, &i_y, &i_width, &i_height );
817
818         XResizeWindow( p_vout->p_sys->p_display,
819                        p_vout->p_sys->p_win->video_window, i_width, i_height );
820
821         XMoveWindow( p_vout->p_sys->p_display,
822                      p_vout->p_sys->p_win->video_window, i_x, i_y );
823     }
824
825     /* Autohide Cursour */
826     if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
827     {
828         /* Hide the mouse automatically */
829         if( p_vout->p_sys->b_mouse_pointer_visible )
830         {
831             ToggleCursor( p_vout );
832         }
833     }
834
835     return 0;
836 }
837
838 /*****************************************************************************
839  * EndVideo: terminate X11 video thread output method
840  *****************************************************************************
841  * Destroy the X11 XImages created by Init. It is called at the end of
842  * the thread, but also each time the window is resized.
843  *****************************************************************************/
844 static void EndVideo( vout_thread_t *p_vout )
845 {
846     int i_index;
847
848     /* Free the direct buffers we allocated */
849     for( i_index = I_OUTPUTPICTURES ; i_index ; )
850     {
851         i_index--;
852         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
853     }
854 }
855
856 /* following functions are local */
857
858 /*****************************************************************************
859  * CreateWindow: open and set-up X11 main window
860  *****************************************************************************/
861 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
862 {
863     XSizeHints              xsize_hints;
864     XSetWindowAttributes    xwindow_attributes;
865     XGCValues               xgcvalues;
866     XEvent                  xevent;
867
868     vlc_bool_t              b_expose;
869     vlc_bool_t              b_configure_notify;
870     vlc_bool_t              b_map_notify;
871
872     vlc_value_t             val;
873     long long int           i_drawable;
874
875     /* Prepare window manager hints and properties */
876     xsize_hints.base_width          = p_win->i_width;
877     xsize_hints.base_height         = p_win->i_height;
878     xsize_hints.flags               = PSize;
879     p_win->wm_protocols =
880              XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
881     p_win->wm_delete_window =
882              XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
883
884     /* Prepare window attributes */
885     xwindow_attributes.backing_store = Always;       /* save the hidden part */
886     xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
887                                                      p_vout->p_sys->i_screen);
888     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
889
890     /* Check whether someone provided us with a window ID */
891     var_Get( p_vout->p_vlc, "drawable", &val );
892     i_drawable = p_vout->b_fullscreen ? 0 : val.i_int;
893
894     if( !i_drawable )
895     {
896         p_win->b_owned = VLC_TRUE;
897
898         /* Create the window and set hints - the window must receive
899          * ConfigureNotify events, and until it is displayed, Expose and
900          * MapNotify events. */
901
902         p_win->base_window =
903             XCreateWindow( p_vout->p_sys->p_display,
904                            DefaultRootWindow( p_vout->p_sys->p_display ),
905                            0, 0,
906                            p_win->i_width, p_win->i_height,
907                            0,
908                            0, InputOutput, 0,
909                            CWBackingStore | CWBackPixel | CWEventMask,
910                            &xwindow_attributes );
911
912         if( !p_vout->b_fullscreen )
913         {
914             /* Set window manager hints and properties: size hints, command,
915              * window's name, and accepted protocols */
916             XSetWMNormalHints( p_vout->p_sys->p_display,
917                                p_win->base_window, &xsize_hints );
918             XSetCommand( p_vout->p_sys->p_display, p_win->base_window,
919                          p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
920
921             XStoreName( p_vout->p_sys->p_display, p_win->base_window,
922 #ifdef MODULE_NAME_IS_x11
923                         VOUT_TITLE " (X11 output)"
924 #else
925                         VOUT_TITLE " (XVideo output)"
926 #endif
927                       );
928         }
929     }
930     else
931     {
932         p_win->b_owned = VLC_FALSE;
933         p_win->base_window = i_drawable;
934
935         XChangeWindowAttributes( p_vout->p_sys->p_display,
936                                  p_win->base_window,
937                                  CWBackingStore | CWBackPixel | CWEventMask,
938                                  &xwindow_attributes );
939     }
940
941     if( (p_win->wm_protocols == None)        /* use WM_DELETE_WINDOW */
942         || (p_win->wm_delete_window == None)
943         || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
944                              &p_win->wm_delete_window, 1 ) )
945     {
946         /* WM_DELETE_WINDOW is not supported by window manager */
947         msg_Warn( p_vout, "missing or bad window manager" );
948     }
949
950     /* Creation of a graphic context that doesn't generate a GraphicsExpose
951      * event when using functions like XCopyArea */
952     xgcvalues.graphics_exposures = False;
953     p_win->gc = XCreateGC( p_vout->p_sys->p_display,
954                            p_win->base_window,
955                            GCGraphicsExposures, &xgcvalues );
956
957     if( p_win->b_owned )
958     {
959         /* Send orders to server, and wait until window is displayed - three
960          * events must be received: a MapNotify event, an Expose event allowing
961          * drawing in the window, and a ConfigureNotify to get the window
962          * dimensions. Once those events have been received, only
963          * ConfigureNotify events need to be received. */
964         b_expose = 0;
965         b_configure_notify = 0;
966         b_map_notify = 0;
967         XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
968         do
969         {
970             XNextEvent( p_vout->p_sys->p_display, &xevent);
971             if( (xevent.type == Expose)
972                 && (xevent.xexpose.window == p_win->base_window) )
973             {
974                 b_expose = 1;
975             }
976             else if( (xevent.type == MapNotify)
977                      && (xevent.xmap.window == p_win->base_window) )
978             {
979                 b_map_notify = 1;
980             }
981             else if( (xevent.type == ConfigureNotify)
982                      && (xevent.xconfigure.window == p_win->base_window) )
983             {
984                 b_configure_notify = 1;
985                 p_win->i_width = xevent.xconfigure.width;
986                 p_win->i_height = xevent.xconfigure.height;
987             }
988         } while( !( b_expose && b_configure_notify && b_map_notify ) );
989     }
990     else
991     {
992         /* Get the window's geometry information */
993         Window dummy1;
994         unsigned int dummy2, dummy3;
995         XGetGeometry( p_vout->p_sys->p_display, p_win->base_window,
996                       &dummy1, &dummy2, &dummy3,
997                       &p_win->i_width,
998                       &p_win->i_height,
999                       &dummy2, &dummy3 );
1000     }
1001
1002     XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1003                   StructureNotifyMask | KeyPressMask |
1004                   ButtonPressMask | ButtonReleaseMask |
1005                   PointerMotionMask );
1006
1007 #ifdef MODULE_NAME_IS_x11
1008     if( p_win->b_owned &&
1009          XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1010     {
1011         /* Allocate a new palette */
1012         p_vout->p_sys->colormap =
1013             XCreateColormap( p_vout->p_sys->p_display,
1014                              DefaultRootWindow( p_vout->p_sys->p_display ),
1015                              DefaultVisual( p_vout->p_sys->p_display,
1016                                             p_vout->p_sys->i_screen ),
1017                              AllocAll );
1018
1019         xwindow_attributes.colormap = p_vout->p_sys->colormap;
1020         XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1021                                  CWColormap, &xwindow_attributes );
1022     }
1023 #endif
1024
1025     /* Create video output sub-window. */
1026     p_win->video_window =  XCreateSimpleWindow(
1027                                       p_vout->p_sys->p_display,
1028                                       p_win->base_window, 0, 0,
1029                                       p_win->i_width, p_win->i_height,
1030                                       0,
1031                                       BlackPixel( p_vout->p_sys->p_display,
1032                                                   p_vout->p_sys->i_screen ),
1033                                       WhitePixel( p_vout->p_sys->p_display,
1034                                                   p_vout->p_sys->i_screen ) );
1035
1036     XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1037                           BlackPixel( p_vout->p_sys->p_display,
1038                                       p_vout->p_sys->i_screen ) );
1039
1040     XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1041     XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1042                   ExposureMask );
1043
1044     /* make sure the video window will be centered in the next ManageVideo() */
1045     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1046
1047     /* If the cursor was formerly blank than blank it again */
1048     if( !p_vout->p_sys->b_mouse_pointer_visible )
1049     {
1050         ToggleCursor( p_vout );
1051         ToggleCursor( p_vout );
1052     }
1053
1054     /* Do NOT use XFlush here ! */
1055     XSync( p_vout->p_sys->p_display, False );
1056
1057     /* At this stage, the window is open, displayed, and ready to
1058      * receive data */
1059     p_vout->p_sys->p_win = p_win;
1060
1061     return VLC_SUCCESS;
1062 }
1063
1064 /*****************************************************************************
1065  * DestroyWindow: destroy the window
1066  *****************************************************************************
1067  *
1068  *****************************************************************************/
1069 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1070 {
1071     /* Do NOT use XFlush here ! */
1072     XSync( p_vout->p_sys->p_display, False );
1073
1074     XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1075     XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1076
1077     if( p_win->b_owned )
1078     {
1079         XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1080         XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1081     }
1082 }
1083
1084 /*****************************************************************************
1085  * NewPicture: allocate a picture
1086  *****************************************************************************
1087  * Returns 0 on success, -1 otherwise
1088  *****************************************************************************/
1089 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1090 {
1091     /* We know the chroma, allocate a buffer which will be used
1092      * directly by the decoder */
1093     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1094
1095     if( p_pic->p_sys == NULL )
1096     {
1097         return -1;
1098     }
1099
1100 #ifdef HAVE_SYS_SHM_H
1101     if( p_vout->p_sys->b_shm )
1102     {
1103         /* Create image using XShm extension */
1104         p_pic->p_sys->p_image =
1105             CreateShmImage( p_vout, p_vout->p_sys->p_display,
1106 #   ifdef MODULE_NAME_IS_xvideo
1107                             p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1108 #   else
1109                             p_vout->p_sys->p_visual,
1110                             p_vout->p_sys->i_screen_depth,
1111 #   endif
1112                             &p_pic->p_sys->shminfo,
1113                             p_vout->output.i_width, p_vout->output.i_height );
1114     }
1115     else
1116 #endif /* HAVE_SYS_SHM_H */
1117     {
1118         /* Create image without XShm extension */
1119         p_pic->p_sys->p_image =
1120             CreateImage( p_vout, p_vout->p_sys->p_display,
1121 #ifdef MODULE_NAME_IS_xvideo
1122                          p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
1123 #else
1124                          p_vout->p_sys->p_visual,
1125                          p_vout->p_sys->i_screen_depth,
1126                          p_vout->p_sys->i_bytes_per_pixel,
1127 #endif
1128                          p_vout->output.i_width, p_vout->output.i_height );
1129     }
1130
1131     if( p_pic->p_sys->p_image == NULL )
1132     {
1133         free( p_pic->p_sys );
1134         return -1;
1135     }
1136
1137     switch( p_vout->output.i_chroma )
1138     {
1139 #ifdef MODULE_NAME_IS_xvideo
1140         case VLC_FOURCC('I','4','2','0'):
1141
1142             p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1143                                + p_pic->p_sys->p_image->offsets[0];
1144             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1145             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1146             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1147             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1148
1149             p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1150                                + p_pic->p_sys->p_image->offsets[1];
1151             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1152             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1153             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1154             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1155
1156             p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1157                                + p_pic->p_sys->p_image->offsets[2];
1158             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1159             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1160             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1161             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1162
1163             p_pic->i_planes = 3;
1164             break;
1165
1166         case VLC_FOURCC('Y','V','1','2'):
1167
1168             p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
1169                                + p_pic->p_sys->p_image->offsets[0];
1170             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1171             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
1172             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1173             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p[Y_PLANE].i_pitch;
1174             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
1175
1176             p_pic->U_PIXELS = p_pic->p_sys->p_image->data
1177                                + p_pic->p_sys->p_image->offsets[2];
1178             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1179             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[2];
1180             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1181             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1182
1183             p_pic->V_PIXELS = p_pic->p_sys->p_image->data
1184                                + p_pic->p_sys->p_image->offsets[1];
1185             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1186             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
1187             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1188             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2;
1189
1190             p_pic->i_planes = 3;
1191             break;
1192
1193         case VLC_FOURCC('Y','2','1','1'):
1194
1195             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1196                                   + p_pic->p_sys->p_image->offsets[0];
1197             p_pic->p->i_lines = p_vout->output.i_height;
1198             /* XXX: this just looks so plain wrong... check it out ! */
1199             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1200             p_pic->p->i_pixel_pitch = 4;
1201             p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1202
1203             p_pic->i_planes = 1;
1204             break;
1205
1206         case VLC_FOURCC('Y','U','Y','2'):
1207         case VLC_FOURCC('U','Y','V','Y'):
1208
1209             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1210                                   + p_pic->p_sys->p_image->offsets[0];
1211             p_pic->p->i_lines = p_vout->output.i_height;
1212             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1213             p_pic->p->i_pixel_pitch = 4;
1214             p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
1215
1216             p_pic->i_planes = 1;
1217             break;
1218
1219         case VLC_FOURCC('R','V','1','5'):
1220
1221             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1222                                   + p_pic->p_sys->p_image->offsets[0];
1223             p_pic->p->i_lines = p_vout->output.i_height;
1224             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1225             p_pic->p->i_pixel_pitch = 2;
1226             p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1227
1228             p_pic->i_planes = 1;
1229             break;
1230
1231         case VLC_FOURCC('R','V','1','6'):
1232
1233             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1234                                   + p_pic->p_sys->p_image->offsets[0];
1235             p_pic->p->i_lines = p_vout->output.i_height;
1236             p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1237             p_pic->p->i_pixel_pitch = 2;
1238             p_pic->p->i_visible_pitch = p_vout->output.i_width * 2;
1239
1240             p_pic->i_planes = 1;
1241             break;
1242
1243 #else
1244         case VLC_FOURCC('R','G','B','2'):
1245
1246             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1247                                   + p_pic->p_sys->p_image->xoffset;
1248             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1249             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1250             p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1251             p_pic->p->i_visible_pitch = p_pic->p_sys->p_image->width;
1252
1253             p_pic->i_planes = 1;
1254
1255             break;
1256
1257         case VLC_FOURCC('R','V','1','6'):
1258         case VLC_FOURCC('R','V','1','5'):
1259
1260             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1261                                   + p_pic->p_sys->p_image->xoffset;
1262             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1263             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1264             p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1265             p_pic->p->i_visible_pitch = 2 * p_pic->p_sys->p_image->width;
1266
1267             p_pic->i_planes = 1;
1268
1269             break;
1270
1271         case VLC_FOURCC('R','V','3','2'):
1272         case VLC_FOURCC('R','V','2','4'):
1273
1274             p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1275                                   + p_pic->p_sys->p_image->xoffset;
1276             p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1277             p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1278             p_pic->p->i_pixel_pitch = p_pic->p_sys->p_image->depth;
1279             p_pic->p->i_visible_pitch = 4 * p_pic->p_sys->p_image->width;
1280
1281             p_pic->i_planes = 1;
1282
1283             break;
1284 #endif
1285
1286         default:
1287             /* Unknown chroma, tell the guy to get lost */
1288             IMAGE_FREE( p_pic->p_sys->p_image );
1289             free( p_pic->p_sys );
1290             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1291                      p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1292             p_pic->i_planes = 0;
1293             return -1;
1294     }
1295
1296     return 0;
1297 }
1298
1299 /*****************************************************************************
1300  * FreePicture: destroy a picture allocated with NewPicture
1301  *****************************************************************************
1302  * Destroy XImage AND associated data. If using Shm, detach shared memory
1303  * segment from server and process, then free it. The XDestroyImage manpage
1304  * says that both the image structure _and_ the data pointed to by the
1305  * image structure are freed, so no need to free p_image->data.
1306  *****************************************************************************/
1307 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1308 {
1309     /* The order of operations is correct */
1310 #ifdef HAVE_SYS_SHM_H
1311     if( p_vout->p_sys->b_shm )
1312     {
1313         XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1314         IMAGE_FREE( p_pic->p_sys->p_image );
1315
1316         shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1317         if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1318         {
1319             msg_Err( p_vout, "cannot detach shared memory (%s)",
1320                              strerror(errno) );
1321         }
1322     }
1323     else
1324 #endif
1325     {
1326         IMAGE_FREE( p_pic->p_sys->p_image );
1327     }
1328
1329     /* Do NOT use XFlush here ! */
1330     XSync( p_vout->p_sys->p_display, False );
1331
1332     free( p_pic->p_sys );
1333 }
1334
1335 /*****************************************************************************
1336  * ToggleFullScreen: Enable or disable full screen mode
1337  *****************************************************************************
1338  * This function will switch between fullscreen and window mode.
1339  *****************************************************************************/
1340 static void ToggleFullScreen ( vout_thread_t *p_vout )
1341 {
1342     Atom prop;
1343     XEvent xevent;
1344     mwmhints_t mwmhints;
1345     XSetWindowAttributes attributes;
1346
1347     p_vout->b_fullscreen = !p_vout->b_fullscreen;
1348
1349     if( p_vout->b_fullscreen )
1350     {
1351         msg_Dbg( p_vout, "entering fullscreen mode" );
1352
1353         p_vout->p_sys->b_altfullscreen =
1354             config_GetInt( p_vout, MODULE_STRING "-altfullscreen" );
1355
1356         if( p_vout->p_sys->p_win->b_owned )
1357         {
1358             XUnmapWindow( p_vout->p_sys->p_display,
1359                           p_vout->p_sys->p_win->base_window);
1360         }
1361
1362         p_vout->p_sys->p_win = &p_vout->p_sys->fullscreen_window;
1363
1364         /* fullscreen window size and position */
1365         p_vout->p_sys->p_win->i_width =
1366             DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1367         p_vout->p_sys->p_win->i_height =
1368             DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1369
1370         CreateWindow( p_vout, p_vout->p_sys->p_win );
1371
1372         /* To my knowledge there are two ways to create a borderless window.
1373          * There's the generic way which is to tell x to bypass the window
1374          * manager, but this creates problems with the focus of other
1375          * applications.
1376          * The other way is to use the motif property "_MOTIF_WM_HINTS" which
1377          * luckily seems to be supported by most window managers. */
1378         if( !p_vout->p_sys->b_altfullscreen )
1379         {
1380             mwmhints.flags = MWM_HINTS_DECORATIONS;
1381             mwmhints.decorations = False;
1382
1383             prop = XInternAtom( p_vout->p_sys->p_display, "_MOTIF_WM_HINTS",
1384                                 False );
1385             XChangeProperty( p_vout->p_sys->p_display,
1386                              p_vout->p_sys->p_win->base_window,
1387                              prop, prop, 32, PropModeReplace,
1388                              (unsigned char *)&mwmhints,
1389                              PROP_MWM_HINTS_ELEMENTS );
1390         }
1391         else
1392         {
1393             /* brute force way to remove decorations */
1394             attributes.override_redirect = True;
1395             XChangeWindowAttributes( p_vout->p_sys->p_display,
1396                                      p_vout->p_sys->p_win->base_window,
1397                                      CWOverrideRedirect,
1398                                      &attributes);
1399         }
1400
1401         /* Make sure the change is effective */
1402         XReparentWindow( p_vout->p_sys->p_display,
1403                          p_vout->p_sys->p_win->base_window,
1404                          DefaultRootWindow( p_vout->p_sys->p_display ),
1405                          0, 0 );
1406
1407         /* fullscreen window size and position */
1408         p_vout->p_sys->p_win->i_width =
1409             DisplayWidth( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1410         p_vout->p_sys->p_win->i_height =
1411             DisplayHeight( p_vout->p_sys->p_display, p_vout->p_sys->i_screen );
1412
1413         XMoveResizeWindow( p_vout->p_sys->p_display,
1414                            p_vout->p_sys->p_win->base_window,
1415                            0, 0,
1416                            p_vout->p_sys->p_win->i_width,
1417                            p_vout->p_sys->p_win->i_height );
1418     }
1419     else
1420     {
1421         msg_Dbg( p_vout, "leaving fullscreen mode" );
1422         DestroyWindow( p_vout, &p_vout->p_sys->fullscreen_window );
1423         p_vout->p_sys->p_win = &p_vout->p_sys->original_window;
1424
1425         if( p_vout->p_sys->p_win->b_owned )
1426         {
1427             XMapWindow( p_vout->p_sys->p_display,
1428                         p_vout->p_sys->p_win->base_window);
1429         }
1430     }
1431
1432     /* Unfortunately, using XSync() here is not enough to ensure the
1433      * window has already been mapped because the XMapWindow() request
1434      * has not necessarily been sent directly to our window (remember,
1435      * the call is first redirected to the window manager) */
1436     if( p_vout->p_sys->p_win->b_owned )
1437     {
1438         do
1439         {
1440             XWindowEvent( p_vout->p_sys->p_display,
1441                           p_vout->p_sys->p_win->base_window,
1442                           StructureNotifyMask, &xevent );
1443         } while( xevent.type != MapNotify );
1444     }
1445     else
1446     {
1447         XSync( p_vout->p_sys->p_display, False );
1448     }
1449
1450     /* Becareful, this can generate a BadMatch error if the window is not
1451      * already mapped by the server (see above) */
1452     XSetInputFocus(p_vout->p_sys->p_display,
1453                    p_vout->p_sys->p_win->base_window,
1454                    RevertToParent,
1455                    CurrentTime);
1456
1457     /* signal that the size needs to be updated */
1458     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1459 }
1460
1461 /*****************************************************************************
1462  * EnableXScreenSaver: enable screen saver
1463  *****************************************************************************
1464  * This function enables the screen saver on a display after it has been
1465  * disabled by XDisableScreenSaver.
1466  * FIXME: what happens if multiple vlc sessions are running at the same
1467  *        time ???
1468  *****************************************************************************/
1469 static void EnableXScreenSaver( vout_thread_t *p_vout )
1470 {
1471 #ifdef DPMSINFO_IN_DPMS_H
1472     int dummy;
1473 #endif
1474
1475     if( p_vout->p_sys->i_ss_timeout )
1476     {
1477         XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1478                          p_vout->p_sys->i_ss_interval,
1479                          p_vout->p_sys->i_ss_blanking,
1480                          p_vout->p_sys->i_ss_exposure );
1481     }
1482
1483     /* Restore DPMS settings */
1484 #ifdef DPMSINFO_IN_DPMS_H
1485     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1486     {
1487         if( p_vout->p_sys->b_ss_dpms )
1488         {
1489             DPMSEnable( p_vout->p_sys->p_display );
1490         }
1491     }
1492 #endif
1493 }
1494
1495 /*****************************************************************************
1496  * DisableXScreenSaver: disable screen saver
1497  *****************************************************************************
1498  * See XEnableXScreenSaver
1499  *****************************************************************************/
1500 static void DisableXScreenSaver( vout_thread_t *p_vout )
1501 {
1502 #ifdef DPMSINFO_IN_DPMS_H
1503     int dummy;
1504 #endif
1505
1506     /* Save screen saver informations */
1507     XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1508                      &p_vout->p_sys->i_ss_interval,
1509                      &p_vout->p_sys->i_ss_blanking,
1510                      &p_vout->p_sys->i_ss_exposure );
1511
1512     /* Disable screen saver */
1513     if( p_vout->p_sys->i_ss_timeout )
1514     {
1515         XSetScreenSaver( p_vout->p_sys->p_display, 0,
1516                          p_vout->p_sys->i_ss_interval,
1517                          p_vout->p_sys->i_ss_blanking,
1518                          p_vout->p_sys->i_ss_exposure );
1519     }
1520
1521     /* Disable DPMS */
1522 #ifdef DPMSINFO_IN_DPMS_H
1523     if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1524     {
1525         CARD16 unused;
1526         /* Save DPMS current state */
1527         DPMSInfo( p_vout->p_sys->p_display, &unused,
1528                   &p_vout->p_sys->b_ss_dpms );
1529         DPMSDisable( p_vout->p_sys->p_display );
1530    }
1531 #endif
1532 }
1533
1534 /*****************************************************************************
1535  * CreateCursor: create a blank mouse pointer
1536  *****************************************************************************/
1537 static void CreateCursor( vout_thread_t *p_vout )
1538 {
1539     XColor cursor_color;
1540
1541     p_vout->p_sys->cursor_pixmap =
1542         XCreatePixmap( p_vout->p_sys->p_display,
1543                        DefaultRootWindow( p_vout->p_sys->p_display ),
1544                        1, 1, 1 );
1545
1546     XParseColor( p_vout->p_sys->p_display,
1547                  XCreateColormap( p_vout->p_sys->p_display,
1548                                   DefaultRootWindow(
1549                                                     p_vout->p_sys->p_display ),
1550                                   DefaultVisual(
1551                                                 p_vout->p_sys->p_display,
1552                                                 p_vout->p_sys->i_screen ),
1553                                   AllocNone ),
1554                  "black", &cursor_color );
1555
1556     p_vout->p_sys->blank_cursor =
1557         XCreatePixmapCursor( p_vout->p_sys->p_display,
1558                              p_vout->p_sys->cursor_pixmap,
1559                              p_vout->p_sys->cursor_pixmap,
1560                              &cursor_color, &cursor_color, 1, 1 );
1561 }
1562
1563 /*****************************************************************************
1564  * DestroyCursor: destroy the blank mouse pointer
1565  *****************************************************************************/
1566 static void DestroyCursor( vout_thread_t *p_vout )
1567 {
1568     XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
1569 }
1570
1571 /*****************************************************************************
1572  * ToggleCursor: hide or show the mouse pointer
1573  *****************************************************************************
1574  * This function hides the X pointer if it is visible by setting the pointer
1575  * sprite to a blank one. To show it again, we disable the sprite.
1576  *****************************************************************************/
1577 static void ToggleCursor( vout_thread_t *p_vout )
1578 {
1579     if( p_vout->p_sys->b_mouse_pointer_visible )
1580     {
1581         XDefineCursor( p_vout->p_sys->p_display,
1582                        p_vout->p_sys->p_win->base_window,
1583                        p_vout->p_sys->blank_cursor );
1584         p_vout->p_sys->b_mouse_pointer_visible = 0;
1585     }
1586     else
1587     {
1588         XUndefineCursor( p_vout->p_sys->p_display,
1589                          p_vout->p_sys->p_win->base_window );
1590         p_vout->p_sys->b_mouse_pointer_visible = 1;
1591     }
1592 }
1593
1594 #ifdef MODULE_NAME_IS_xvideo
1595 /*****************************************************************************
1596  * XVideoGetPort: get YUV12 port
1597  *****************************************************************************/
1598 static int XVideoGetPort( vout_thread_t *p_vout,
1599                           vlc_fourcc_t i_chroma, vlc_fourcc_t *pi_newchroma )
1600 {
1601     XvAdaptorInfo *p_adaptor;
1602     unsigned int i;
1603     int i_adaptor, i_num_adaptors, i_requested_adaptor;
1604     int i_selected_port;
1605
1606     switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
1607     {
1608         case Success:
1609             break;
1610
1611         case XvBadExtension:
1612             msg_Warn( p_vout, "XvBadExtension" );
1613             return -1;
1614
1615         case XvBadAlloc:
1616             msg_Warn( p_vout, "XvBadAlloc" );
1617             return -1;
1618
1619         default:
1620             msg_Warn( p_vout, "XvQueryExtension failed" );
1621             return -1;
1622     }
1623
1624     switch( XvQueryAdaptors( p_vout->p_sys->p_display,
1625                              DefaultRootWindow( p_vout->p_sys->p_display ),
1626                              &i_num_adaptors, &p_adaptor ) )
1627     {
1628         case Success:
1629             break;
1630
1631         case XvBadExtension:
1632             msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
1633             return -1;
1634
1635         case XvBadAlloc:
1636             msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
1637             return -1;
1638
1639         default:
1640             msg_Warn( p_vout, "XvQueryAdaptors failed" );
1641             return -1;
1642     }
1643
1644     i_selected_port = -1;
1645     i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
1646
1647     for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
1648     {
1649         XvImageFormatValues *p_formats;
1650         int i_format, i_num_formats;
1651         int i_port;
1652
1653         /* If we requested an adaptor and it's not this one, we aren't
1654          * interested */
1655         if( i_requested_adaptor != -1 && i_adaptor != i_requested_adaptor )
1656         {
1657             continue;
1658         }
1659
1660         /* If the adaptor doesn't have the required properties, skip it */
1661         if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
1662             !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
1663         {
1664             continue;
1665         }
1666
1667         /* Check that adaptor supports our requested format... */
1668         p_formats = XvListImageFormats( p_vout->p_sys->p_display,
1669                                         p_adaptor[i_adaptor].base_id,
1670                                         &i_num_formats );
1671
1672         for( i_format = 0;
1673              i_format < i_num_formats && ( i_selected_port == -1 );
1674              i_format++ )
1675         {
1676             /* Code removed, we can get this through xvinfo anyway */
1677 #if 0
1678             XvEncodingInfo  *p_enc;
1679             int             i_enc, i_num_encodings;
1680             XvAttribute     *p_attr;
1681             int             i_attr, i_num_attributes;
1682 #endif
1683
1684             /* If this is not the format we want, or at least a
1685              * similar one, forget it */
1686             if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
1687             {
1688                 continue;
1689             }
1690
1691             /* Look for the first available port supporting this format */
1692             for( i_port = p_adaptor[i_adaptor].base_id;
1693                  ( i_port < (int)(p_adaptor[i_adaptor].base_id
1694                                    + p_adaptor[i_adaptor].num_ports) )
1695                    && ( i_selected_port == -1 );
1696                  i_port++ )
1697             {
1698                 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
1699                      == Success )
1700                 {
1701                     i_selected_port = i_port;
1702                     *pi_newchroma = p_formats[ i_format ].id;
1703                 }
1704             }
1705
1706             /* If no free port was found, forget it */
1707             if( i_selected_port == -1 )
1708             {
1709                 continue;
1710             }
1711
1712             /* If we found a port, print information about it */
1713             msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
1714                      i_adaptor, i_selected_port, p_formats[ i_format ].id,
1715                      (char *)&p_formats[ i_format ].id,
1716                      ( p_formats[ i_format ].format == XvPacked ) ?
1717                          "packed" : "planar" );
1718
1719 #if 0
1720             msg_Dbg( p_vout, " encoding list:" );
1721
1722             if( XvQueryEncodings( p_vout->p_sys->p_display, i_selected_port,
1723                                   &i_num_encodings, &p_enc )
1724                  != Success )
1725             {
1726                 msg_Dbg( p_vout, "  XvQueryEncodings failed" );
1727                 continue;
1728             }
1729
1730             for( i_enc = 0; i_enc < i_num_encodings; i_enc++ )
1731             {
1732                 msg_Dbg( p_vout, "  id=%ld, name=%s, size=%ldx%ld,"
1733                                       " numerator=%d, denominator=%d",
1734                              p_enc[i_enc].encoding_id, p_enc[i_enc].name,
1735                              p_enc[i_enc].width, p_enc[i_enc].height,
1736                              p_enc[i_enc].rate.numerator,
1737                              p_enc[i_enc].rate.denominator );
1738             }
1739
1740             if( p_enc != NULL )
1741             {
1742                 XvFreeEncodingInfo( p_enc );
1743             }
1744
1745             msg_Dbg( p_vout, " attribute list:" );
1746             p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
1747                                             i_selected_port,
1748                                             &i_num_attributes );
1749             for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
1750             {
1751                 msg_Dbg( p_vout, "  name=%s, flags=[%s%s ], min=%i, max=%i",
1752                       p_attr[i_attr].name,
1753                       (p_attr[i_attr].flags & XvGettable) ? " get" : "",
1754                       (p_attr[i_attr].flags & XvSettable) ? " set" : "",
1755                       p_attr[i_attr].min_value, p_attr[i_attr].max_value );
1756             }
1757
1758             if( p_attr != NULL )
1759             {
1760                 XFree( p_attr );
1761             }
1762 #endif
1763         }
1764
1765         if( p_formats != NULL )
1766         {
1767             XFree( p_formats );
1768         }
1769
1770     }
1771
1772     if( i_num_adaptors > 0 )
1773     {
1774         XvFreeAdaptorInfo( p_adaptor );
1775     }
1776
1777     if( i_selected_port == -1 )
1778     {
1779         if( i_requested_adaptor == -1 )
1780         {
1781             msg_Warn( p_vout, "no free XVideo port found for format "
1782                        "0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
1783         }
1784         else
1785         {
1786             msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
1787                        "XVideo port for format 0x%.8x (%4.4s)",
1788                        i_requested_adaptor, i_chroma, (char*)&i_chroma );
1789         }
1790     }
1791
1792     return i_selected_port;
1793 }
1794
1795 /*****************************************************************************
1796  * XVideoReleasePort: release YUV12 port
1797  *****************************************************************************/
1798 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
1799 {
1800     XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
1801 }
1802 #endif
1803
1804 /*****************************************************************************
1805  * InitDisplay: open and initialize X11 device
1806  *****************************************************************************
1807  * Create a window according to video output given size, and set other
1808  * properties according to the display properties.
1809  *****************************************************************************/
1810 static int InitDisplay( vout_thread_t *p_vout )
1811 {
1812 #ifdef MODULE_NAME_IS_x11
1813     XPixmapFormatValues *       p_formats;                 /* pixmap formats */
1814     XVisualInfo *               p_xvisual;           /* visuals informations */
1815     XVisualInfo                 xvisual_template;         /* visual template */
1816     int                         i_count;                       /* array size */
1817 #endif
1818
1819 #ifdef HAVE_SYS_SHM_H
1820     p_vout->p_sys->b_shm = 0;
1821
1822     if( config_GetInt( p_vout, MODULE_STRING "-shm" ) )
1823     {
1824 #   ifdef SYS_DARWIN
1825         /* FIXME: As of 2001-03-16, XFree4 for MacOS X does not support Xshm */
1826 #   else
1827         p_vout->p_sys->b_shm =
1828                   ( XShmQueryExtension( p_vout->p_sys->p_display ) == True );
1829 #   endif
1830
1831         if( !p_vout->p_sys->b_shm )
1832         {
1833             msg_Warn( p_vout, "XShm video extension is unavailable" );
1834         }
1835     }
1836     else
1837     {
1838         msg_Dbg( p_vout, "disabling XShm video extension" );
1839     }
1840
1841 #else
1842     msg_Warn( p_vout, "XShm video extension is unavailable" );
1843
1844 #endif
1845
1846 #ifdef MODULE_NAME_IS_xvideo
1847     /* XXX The brightness and contrast values should be read from environment
1848      * XXX variables... */
1849 #if 0
1850     XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
1851     XVideoSetAttribute( p_vout, "XV_CONTRAST",   0.5 );
1852 #endif
1853 #endif
1854
1855 #ifdef MODULE_NAME_IS_x11
1856     /* Initialize structure */
1857     p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
1858
1859     /* Get screen depth */
1860     p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
1861                                                    p_vout->p_sys->i_screen );
1862     switch( p_vout->p_sys->i_screen_depth )
1863     {
1864     case 8:
1865         /*
1866          * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
1867          */
1868         xvisual_template.screen =   p_vout->p_sys->i_screen;
1869         xvisual_template.class =    DirectColor;
1870         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1871                                     VisualScreenMask | VisualClassMask,
1872                                     &xvisual_template, &i_count );
1873         if( p_xvisual == NULL )
1874         {
1875             msg_Err( p_vout, "no PseudoColor visual available" );
1876             return VLC_EGENERIC;
1877         }
1878         p_vout->p_sys->i_bytes_per_pixel = 1;
1879         p_vout->output.pf_setpalette = SetPalette;
1880         break;
1881     case 15:
1882     case 16:
1883     case 24:
1884     default:
1885         /*
1886          * Screen depth is higher than 8bpp. TrueColor visual is used.
1887          */
1888         xvisual_template.screen =   p_vout->p_sys->i_screen;
1889         xvisual_template.class =    TrueColor;
1890         p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
1891                                     VisualScreenMask | VisualClassMask,
1892                                     &xvisual_template, &i_count );
1893         if( p_xvisual == NULL )
1894         {
1895             msg_Err( p_vout, "no TrueColor visual available" );
1896             return VLC_EGENERIC;
1897         }
1898
1899         p_vout->output.i_rmask = p_xvisual->red_mask;
1900         p_vout->output.i_gmask = p_xvisual->green_mask;
1901         p_vout->output.i_bmask = p_xvisual->blue_mask;
1902
1903         /* There is no difference yet between 3 and 4 Bpp. The only way
1904          * to find the actual number of bytes per pixel is to list supported
1905          * pixmap formats. */
1906         p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
1907         p_vout->p_sys->i_bytes_per_pixel = 0;
1908
1909         for( ; i_count-- ; p_formats++ )
1910         {
1911             /* Under XFree4.0, the list contains pixmap formats available
1912              * through all video depths ; so we have to check against current
1913              * depth. */
1914             if( p_formats->depth == (int)p_vout->p_sys->i_screen_depth )
1915             {
1916                 if( p_formats->bits_per_pixel / 8
1917                         > (int)p_vout->p_sys->i_bytes_per_pixel )
1918                 {
1919                     p_vout->p_sys->i_bytes_per_pixel =
1920                                                p_formats->bits_per_pixel / 8;
1921                 }
1922             }
1923         }
1924         break;
1925     }
1926     p_vout->p_sys->p_visual = p_xvisual->visual;
1927     XFree( p_xvisual );
1928 #endif
1929
1930     return VLC_SUCCESS;
1931 }
1932
1933 #ifdef HAVE_SYS_SHM_H
1934 /*****************************************************************************
1935  * CreateShmImage: create an XImage or XvImage using shared memory extension
1936  *****************************************************************************
1937  * Prepare an XImage or XvImage for display function.
1938  * The order of the operations respects the recommandations of the mit-shm
1939  * document by J.Corbet and K.Packard. Most of the parameters were copied from
1940  * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
1941  *****************************************************************************/
1942 static IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
1943                                     Display* p_display, EXTRA_ARGS_SHM,
1944                                     int i_width, int i_height )
1945 {
1946     IMAGE_TYPE *p_image;
1947
1948     /* Create XImage / XvImage */
1949 #ifdef MODULE_NAME_IS_xvideo
1950     p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
1951                                 i_width, i_height, p_shm );
1952 #else
1953     p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
1954                                p_shm, i_width, i_height );
1955 #endif
1956     if( p_image == NULL )
1957     {
1958         msg_Err( p_vout, "image creation failed" );
1959         return NULL;
1960     }
1961
1962     /* Allocate shared memory segment - 0776 set the access permission
1963      * rights (like umask), they are not yet supported by all X servers */
1964     p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0776 );
1965     if( p_shm->shmid < 0 )
1966     {
1967         msg_Err( p_vout, "cannot allocate shared image data (%s)",
1968                          strerror( errno ) );
1969         IMAGE_FREE( p_image );
1970         return NULL;
1971     }
1972
1973     /* Attach shared memory segment to process (read/write) */
1974     p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
1975     if(! p_shm->shmaddr )
1976     {
1977         msg_Err( p_vout, "cannot attach shared memory (%s)",
1978                          strerror(errno));
1979         IMAGE_FREE( p_image );
1980         shmctl( p_shm->shmid, IPC_RMID, 0 );
1981         return NULL;
1982     }
1983
1984     /* Read-only data. We won't be using XShmGetImage */
1985     p_shm->readOnly = True;
1986
1987     /* Attach shared memory segment to X server */
1988     if( XShmAttach( p_display, p_shm ) == False )
1989     {
1990         msg_Err( p_vout, "cannot attach shared memory to X server" );
1991         IMAGE_FREE( p_image );
1992         shmctl( p_shm->shmid, IPC_RMID, 0 );
1993         shmdt( p_shm->shmaddr );
1994         return NULL;
1995     }
1996
1997     /* Send image to X server. This instruction is required, since having
1998      * built a Shm XImage and not using it causes an error on XCloseDisplay,
1999      * and remember NOT to use XFlush ! */
2000     XSync( p_display, False );
2001
2002 #if 0
2003     /* Mark the shm segment to be removed when there are no more
2004      * attachements, so it is automatic on process exit or after shmdt */
2005     shmctl( p_shm->shmid, IPC_RMID, 0 );
2006 #endif
2007
2008     return p_image;
2009 }
2010 #endif
2011
2012 /*****************************************************************************
2013  * CreateImage: create an XImage or XvImage
2014  *****************************************************************************
2015  * Create a simple image used as a buffer.
2016  *****************************************************************************/
2017 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2018                                  Display *p_display, EXTRA_ARGS,
2019                                  int i_width, int i_height )
2020 {
2021     byte_t *    p_data;                           /* image data storage zone */
2022     IMAGE_TYPE *p_image;
2023 #ifdef MODULE_NAME_IS_x11
2024     int         i_quantum;                     /* XImage quantum (see below) */
2025     int         i_bytes_per_line;
2026 #endif
2027
2028     /* Allocate memory for image */
2029 #ifdef MODULE_NAME_IS_xvideo
2030     p_data = (byte_t *) malloc( i_width * i_height * 2 ); /* XXX */
2031 #else
2032     i_bytes_per_line = i_width * i_bytes_per_pixel;
2033     p_data = (byte_t *) malloc( i_bytes_per_line * i_height );
2034 #endif
2035     if( !p_data )
2036     {
2037         msg_Err( p_vout, "out of memory" );
2038         return NULL;
2039     }
2040
2041 #ifdef MODULE_NAME_IS_x11
2042     /* Optimize the quantum of a scanline regarding its size - the quantum is
2043        a diviser of the number of bits between the start of two scanlines. */
2044     if( i_bytes_per_line & 0xf )
2045     {
2046         i_quantum = 0x8;
2047     }
2048     else if( i_bytes_per_line & 0x10 )
2049     {
2050         i_quantum = 0x10;
2051     }
2052     else
2053     {
2054         i_quantum = 0x20;
2055     }
2056 #endif
2057
2058     /* Create XImage. p_data will be automatically freed */
2059 #ifdef MODULE_NAME_IS_xvideo
2060     p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2061                              p_data, i_width, i_height );
2062 #else
2063     p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2064                             p_data, i_width, i_height, i_quantum, 0 );
2065 #endif
2066     if( p_image == NULL )
2067     {
2068         msg_Err( p_vout, "XCreateImage() failed" );
2069         free( p_data );
2070         return NULL;
2071     }
2072
2073     return p_image;
2074 }
2075
2076 #ifdef MODULE_NAME_IS_x11
2077 /*****************************************************************************
2078  * SetPalette: sets an 8 bpp palette
2079  *****************************************************************************
2080  * This function sets the palette given as an argument. It does not return
2081  * anything, but could later send information on which colors it was unable
2082  * to set.
2083  *****************************************************************************/
2084 static void SetPalette( vout_thread_t *p_vout,
2085                         uint16_t *red, uint16_t *green, uint16_t *blue )
2086 {
2087     int i;
2088     XColor p_colors[255];
2089
2090     /* allocate palette */
2091     for( i = 0; i < 255; i++ )
2092     {
2093         /* kludge: colors are indexed reversely because color 255 seems
2094          * to be reserved for black even if we try to set it to white */
2095         p_colors[ i ].pixel = 255 - i;
2096         p_colors[ i ].pad   = 0;
2097         p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2098         p_colors[ i ].red   = red[ 255 - i ];
2099         p_colors[ i ].blue  = blue[ 255 - i ];
2100         p_colors[ i ].green = green[ 255 - i ];
2101     }
2102
2103     XStoreColors( p_vout->p_sys->p_display,
2104                   p_vout->p_sys->colormap, p_colors, 255 );
2105 }
2106 #endif