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