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