xcommon.c 81.7 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3 4
/*****************************************************************************
 * xcommon.c: Functions common to the X11 and XVideo plugins
 *****************************************************************************
 * Copyright (C) 1998-2001 VideoLAN
5
 * $Id: xcommon.c,v 1.40 2002/06/05 18:07:03 stef Exp $
Sam Hocevar's avatar
 
Sam Hocevar committed
6 7 8 9
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *          David Kennedy <dkennedy@tinytoad.com>
gbazin's avatar
 
gbazin committed
10
 *          Gildas Bazin <gbazin@netcourrier.com>
Sam Hocevar's avatar
 
Sam Hocevar committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <string.h>                                            /* strerror() */

34 35 36
#include <vlc/vlc.h>
#include <vlc/intf.h>
#include <vlc/vout.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
37 38

#ifdef HAVE_MACHINE_PARAM_H
Sam Hocevar's avatar
 
Sam Hocevar committed
39 40 41 42
    /* BSD */
#   include <machine/param.h>
#   include <sys/types.h>                                  /* typedef ushort */
#   include <sys/ipc.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
43 44 45
#endif

#ifndef WIN32
Sam Hocevar's avatar
 
Sam Hocevar committed
46 47 48 49 50
#   include <netinet/in.h>                            /* BSD: struct in_addr */
#endif

#ifdef HAVE_SYS_SHM_H
#   include <sys/shm.h>                                /* shmget(), shmctl() */
Sam Hocevar's avatar
 
Sam Hocevar committed
51 52 53 54 55
#endif

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
56 57 58
#ifdef HAVE_SYS_SHM_H
#   include <X11/extensions/XShm.h>
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
59 60 61
#ifdef DPMSINFO_IN_DPMS_H
#   include <X11/extensions/dpms.h>
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
62 63 64 65 66 67 68 69 70 71 72 73

#ifdef MODULE_NAME_IS_xvideo
#   include <X11/extensions/Xv.h>
#   include <X11/extensions/Xvlib.h>
#endif

#include "xcommon.h"

#include "netutils.h"                                 /* network_ChannelJoin */

#ifdef MODULE_NAME_IS_xvideo
#   define IMAGE_TYPE     XvImage
Sam Hocevar's avatar
 
Sam Hocevar committed
74 75
#   define EXTRA_ARGS     int i_xvport, int i_chroma
#   define EXTRA_ARGS_SHM int i_xvport, int i_chroma, XShmSegmentInfo *p_shm
Sam Hocevar's avatar
 
Sam Hocevar committed
76 77
#   define DATA_SIZE(p)   (p)->data_size
#   define IMAGE_FREE     XFree      /* There is nothing like XvDestroyImage */
Sam Hocevar's avatar
 
Sam Hocevar committed
78 79 80 81
#else
#   define IMAGE_TYPE     XImage
#   define EXTRA_ARGS     Visual *p_visual, int i_depth, int i_bytes_per_pixel
#   define EXTRA_ARGS_SHM Visual *p_visual, int i_depth, XShmSegmentInfo *p_shm
Sam Hocevar's avatar
 
Sam Hocevar committed
82
#   define DATA_SIZE(p)   ((p)->bytes_per_line * (p)->height)
Sam Hocevar's avatar
 
Sam Hocevar committed
83 84 85 86 87 88 89 90
#   define IMAGE_FREE     XDestroyImage
#endif

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  vout_Create    ( vout_thread_t * );
static void vout_Destroy   ( vout_thread_t * );
Sam Hocevar's avatar
 
Sam Hocevar committed
91
static void vout_Render    ( vout_thread_t *, picture_t * );
Sam Hocevar's avatar
 
Sam Hocevar committed
92 93 94 95 96 97 98 99 100 101 102 103 104
static void vout_Display   ( vout_thread_t *, picture_t * );
static int  vout_Manage    ( vout_thread_t * );
static int  vout_Init      ( vout_thread_t * );
static void vout_End       ( vout_thread_t * );

static int  InitDisplay    ( vout_thread_t * );

static int  CreateWindow   ( vout_thread_t * );
static void DestroyWindow  ( vout_thread_t * );

static int  NewPicture     ( vout_thread_t *, picture_t * );
static void FreePicture    ( vout_thread_t *, picture_t * );

105 106
static IMAGE_TYPE *CreateImage    ( vout_thread_t *, 
                                    Display *, EXTRA_ARGS, int, int );
Sam Hocevar's avatar
 
Sam Hocevar committed
107
#ifdef HAVE_SYS_SHM_H
108 109
static IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
                                    Display *, EXTRA_ARGS_SHM, int, int );
Sam Hocevar's avatar
 
Sam Hocevar committed
110
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
111

gbazin's avatar
 
gbazin committed
112 113
static void ToggleFullScreen      ( vout_thread_t * );

Sam Hocevar's avatar
 
Sam Hocevar committed
114 115 116 117 118 119 120 121
static void EnableXScreenSaver    ( vout_thread_t * );
static void DisableXScreenSaver   ( vout_thread_t * );

static void CreateCursor   ( vout_thread_t * );
static void DestroyCursor  ( vout_thread_t * );
static void ToggleCursor   ( vout_thread_t * );

#ifdef MODULE_NAME_IS_xvideo
122 123
static int  XVideoGetPort         ( vout_thread_t *, u32, u32 * );
static void XVideoReleasePort     ( vout_thread_t *, int );
Sam Hocevar's avatar
 
Sam Hocevar committed
124 125
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
126 127 128 129
#ifdef MODULE_NAME_IS_x11
static void SetPalette     ( vout_thread_t *, u16 *, u16 *, u16 * );
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
130 131 132 133 134 135
/*****************************************************************************
 * vout_sys_t: video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the X11 and XVideo specific properties of an output thread.
 *****************************************************************************/
136
struct vout_sys_s
Sam Hocevar's avatar
 
Sam Hocevar committed
137 138 139 140 141 142 143
{
    /* Internal settings and properties */
    Display *           p_display;                        /* display pointer */

    Visual *            p_visual;                          /* visual pointer */
    int                 i_screen;                           /* screen number */
    GC                  gc;              /* graphic context instance handler */
gbazin's avatar
 
gbazin committed
144 145
    Window              window;                               /* root window */
    Window              video_window;     /* sub-window for displaying video */
Sam Hocevar's avatar
 
Sam Hocevar committed
146

Sam Hocevar's avatar
 
Sam Hocevar committed
147
#ifdef HAVE_SYS_SHM_H
148
    vlc_bool_t          b_shm;               /* shared memory extension flag */
Sam Hocevar's avatar
 
Sam Hocevar committed
149
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

#ifdef MODULE_NAME_IS_xvideo
    int                 i_xvport;
#else
    Colormap            colormap;               /* colormap used (8bpp only) */

    int                 i_screen_depth;
    int                 i_bytes_per_pixel;
    int                 i_bytes_per_line;
#endif

    /* X11 generic properties */
    Atom                wm_protocols;
    Atom                wm_delete_window;

    int                 i_width;                     /* width of main window */
    int                 i_height;                   /* height of main window */
167
    vlc_bool_t          b_altfullscreen;          /* which fullscreen method */
Sam Hocevar's avatar
 
Sam Hocevar committed
168

gbazin's avatar
 
gbazin committed
169 170 171 172 173
    /* Backup of window position and size before fullscreen switch */
    int                 i_width_backup;
    int                 i_height_backup;
    int                 i_xpos_backup;
    int                 i_ypos_backup;
gbazin's avatar
 
gbazin committed
174 175 176 177
    int                 i_width_backup_2;
    int                 i_height_backup_2;
    int                 i_xpos_backup_2;
    int                 i_ypos_backup_2;
gbazin's avatar
 
gbazin committed
178

Sam Hocevar's avatar
 
Sam Hocevar committed
179 180 181 182 183
    /* Screen saver properties */
    int                 i_ss_timeout;                             /* timeout */
    int                 i_ss_interval;           /* interval between changes */
    int                 i_ss_blanking;                      /* blanking mode */
    int                 i_ss_exposure;                      /* exposure mode */
Sam Hocevar's avatar
 
Sam Hocevar committed
184
#ifdef DPMSINFO_IN_DPMS_H
gbazin's avatar
 
gbazin committed
185
    BOOL                b_ss_dpms;                              /* DPMS mode */
Sam Hocevar's avatar
 
Sam Hocevar committed
186
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
187 188

    /* Mouse pointer properties */
189
    vlc_bool_t          b_mouse_pointer_visible;
Sam Hocevar's avatar
 
Sam Hocevar committed
190 191
    mtime_t             i_time_mouse_last_moved; /* used to auto-hide pointer*/
    Cursor              blank_cursor;                   /* the hidden cursor */
gbazin's avatar
 
gbazin committed
192
    mtime_t             i_time_button_last_pressed;   /* to track dbl-clicks */
Sam Hocevar's avatar
 
Sam Hocevar committed
193
    Pixmap              cursor_pixmap;
194
};
Sam Hocevar's avatar
 
Sam Hocevar committed
195 196 197 198 199 200 201

/*****************************************************************************
 * picture_sys_t: direct buffer method descriptor
 *****************************************************************************
 * This structure is part of the picture descriptor, it describes the
 * XVideo specific properties of a direct buffer.
 *****************************************************************************/
202
struct picture_sys_s
Sam Hocevar's avatar
 
Sam Hocevar committed
203 204 205
{
    IMAGE_TYPE *        p_image;

Sam Hocevar's avatar
 
Sam Hocevar committed
206
#ifdef HAVE_SYS_SHM_H
Sam Hocevar's avatar
 
Sam Hocevar committed
207
    XShmSegmentInfo     shminfo;       /* shared memory zone information */
Sam Hocevar's avatar
 
Sam Hocevar committed
208
#endif
209
};
Sam Hocevar's avatar
 
Sam Hocevar committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

/*****************************************************************************
 * mwmhints_t: window manager hints
 *****************************************************************************
 * Fullscreen needs to be able to hide the wm decorations so we provide
 * this structure to make it easier.
 *****************************************************************************/
#define MWM_HINTS_DECORATIONS   (1L << 1)
#define PROP_MWM_HINTS_ELEMENTS 5
typedef struct mwmhints_s
{
    u32 flags;
    u32 functions;
    u32 decorations;
    s32 input_mode;
    u32 status;
} mwmhints_t;

/*****************************************************************************
 * Chroma defines
 *****************************************************************************/
#ifdef MODULE_NAME_IS_xvideo
Sam Hocevar's avatar
 
Sam Hocevar committed
232
#   define MAX_DIRECTBUFFERS 10
Sam Hocevar's avatar
 
Sam Hocevar committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#else
#   define MAX_DIRECTBUFFERS 2
#endif

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
void _M( vout_getfunctions )( function_list_t * p_function_list )
{
    p_function_list->functions.vout.pf_create     = vout_Create;
    p_function_list->functions.vout.pf_init       = vout_Init;
    p_function_list->functions.vout.pf_end        = vout_End;
    p_function_list->functions.vout.pf_destroy    = vout_Destroy;
    p_function_list->functions.vout.pf_manage     = vout_Manage;
Sam Hocevar's avatar
 
Sam Hocevar committed
248
    p_function_list->functions.vout.pf_render     = vout_Render;
Sam Hocevar's avatar
 
Sam Hocevar committed
249 250 251 252 253 254 255 256 257 258 259 260
    p_function_list->functions.vout.pf_display    = vout_Display;
}

/*****************************************************************************
 * vout_Create: allocate X11 video thread output method
 *****************************************************************************
 * This function allocate and initialize a X11 vout method. It uses some of the
 * vout properties to choose the window size, and change them according to the
 * actual properties of the display.
 *****************************************************************************/
static int vout_Create( vout_thread_t *p_vout )
{
261
    char *     psz_display;
262
#ifdef MODULE_NAME_IS_xvideo
263 264 265
    char *     psz_chroma;
    u32        i_chroma = 0;
    vlc_bool_t b_chroma = 0;
266
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
267 268 269 270 271

    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
272
        msg_Err( p_vout, "out of memory" );
Sam Hocevar's avatar
 
Sam Hocevar committed
273 274 275
        return( 1 );
    }

gbazin's avatar
 
gbazin committed
276
    /* Open display, unsing the "display" config variable or the DISPLAY
gbazin's avatar
 
gbazin committed
277
     * environment variable */
278
    psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
gbazin's avatar
 
gbazin committed
279

Sam Hocevar's avatar
 
Sam Hocevar committed
280 281 282 283
    p_vout->p_sys->p_display = XOpenDisplay( psz_display );

    if( p_vout->p_sys->p_display == NULL )                          /* error */
    {
284 285
        msg_Err( p_vout, "cannot open display %s",
                         XDisplayName( psz_display ) );
Sam Hocevar's avatar
 
Sam Hocevar committed
286
        free( p_vout->p_sys );
gbazin's avatar
 
gbazin committed
287
        if( psz_display ) free( psz_display );
Sam Hocevar's avatar
 
Sam Hocevar committed
288 289
        return( 1 );
    }
gbazin's avatar
 
gbazin committed
290 291
    if( psz_display ) free( psz_display );

Sam Hocevar's avatar
 
Sam Hocevar committed
292 293 294
    p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );

#ifdef MODULE_NAME_IS_xvideo
295
    psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    if( psz_chroma )
    {
        if( strlen( psz_chroma ) >= 4 )
        {
            i_chroma  = (unsigned char)psz_chroma[0] <<  0;
            i_chroma |= (unsigned char)psz_chroma[1] <<  8;
            i_chroma |= (unsigned char)psz_chroma[2] << 16;
            i_chroma |= (unsigned char)psz_chroma[3] << 24;

            b_chroma = 1;
        }

        free( psz_chroma );
    }

    if( b_chroma )
    {
313 314
        msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)", 
                 i_chroma, (char*)&i_chroma );
315 316 317 318 319 320
    }
    else
    {
        i_chroma = p_vout->render.i_chroma;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
321
    /* Check that we have access to an XVideo port providing this chroma */
322
    p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, i_chroma,
Sam Hocevar's avatar
 
Sam Hocevar committed
323
                                             &p_vout->output.i_chroma );
Sam Hocevar's avatar
 
Sam Hocevar committed
324 325
    if( p_vout->p_sys->i_xvport < 0 )
    {
326 327 328 329 330 331 332 333 334
        /* If a specific chroma format was requested, then we don't try to
         * be cleverer than the user. He knows pretty well what he wants. */
        if( b_chroma )
        {
            XCloseDisplay( p_vout->p_sys->p_display );
            free( p_vout->p_sys );
            return 1;
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
335
        /* It failed, but it's not completely lost ! We try to open an
Sam Hocevar's avatar
 
Sam Hocevar committed
336 337
         * XVideo port for an YUY2 picture. We'll need to do an YUV
         * conversion, but at least it has got scaling. */
338
        p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, FOURCC_YUY2,
Sam Hocevar's avatar
 
Sam Hocevar committed
339
                                                 &p_vout->output.i_chroma );
Sam Hocevar's avatar
 
Sam Hocevar committed
340 341
        if( p_vout->p_sys->i_xvport < 0 )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
342 343 344
            /* It failed, but it's not completely lost ! We try to open an
             * XVideo port for a simple 16bpp RGB picture. We'll need to do
             * an YUV conversion, but at least it has got scaling. */
345
            p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, FOURCC_RV16,
Sam Hocevar's avatar
 
Sam Hocevar committed
346 347 348 349 350 351 352
                                                     &p_vout->output.i_chroma );
            if( p_vout->p_sys->i_xvport < 0 )
            {
                XCloseDisplay( p_vout->p_sys->p_display );
                free( p_vout->p_sys );
                return 1;
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
353
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
354 355 356
    }
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
357
    /* Create blank cursor (for mouse cursor autohiding) */
gbazin's avatar
 
gbazin committed
358
    p_vout->p_sys->i_time_mouse_last_moved = mdate();
Sam Hocevar's avatar
 
Sam Hocevar committed
359 360 361
    p_vout->p_sys->b_mouse_pointer_visible = 1;
    CreateCursor( p_vout );

Sam Hocevar's avatar
 
Sam Hocevar committed
362 363 364 365
    /* Spawn base window - this window will include the video output window,
     * but also command buttons, subtitles and other indicators */
    if( CreateWindow( p_vout ) )
    {
366
        msg_Err( p_vout, "cannot create X11 window" );
Sam Hocevar's avatar
 
Sam Hocevar committed
367
        DestroyCursor( p_vout );
Sam Hocevar's avatar
 
Sam Hocevar committed
368 369 370 371 372 373 374 375
        XCloseDisplay( p_vout->p_sys->p_display );
        free( p_vout->p_sys );
        return( 1 );
    }

    /* Open and initialize device. */
    if( InitDisplay( p_vout ) )
    {
376
        msg_Err( p_vout, "cannot initialize X11 display" );
Sam Hocevar's avatar
 
Sam Hocevar committed
377
        DestroyCursor( p_vout );
gbazin's avatar
 
gbazin committed
378
        DestroyWindow( p_vout );
Sam Hocevar's avatar
 
Sam Hocevar committed
379 380 381 382 383
        XCloseDisplay( p_vout->p_sys->p_display );
        free( p_vout->p_sys );
        return( 1 );
    }

gbazin's avatar
 
gbazin committed
384
    /* Disable screen saver */
Sam Hocevar's avatar
 
Sam Hocevar committed
385 386
    DisableXScreenSaver( p_vout );

gbazin's avatar
 
gbazin committed
387 388
    /* Misc init */
    p_vout->p_sys->b_altfullscreen = 0;
gbazin's avatar
 
gbazin committed
389
    p_vout->p_sys->i_time_button_last_pressed = 0;
gbazin's avatar
 
gbazin committed
390

Sam Hocevar's avatar
 
Sam Hocevar committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    return( 0 );
}

/*****************************************************************************
 * vout_Destroy: destroy X11 video thread output method
 *****************************************************************************
 * Terminate an output method created by vout_CreateOutputMethod
 *****************************************************************************/
static void vout_Destroy( vout_thread_t *p_vout )
{
    /* Restore cursor if it was blanked */
    if( !p_vout->p_sys->b_mouse_pointer_visible )
    {
        ToggleCursor( p_vout );
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
407
#ifdef MODULE_NAME_IS_x11
Sam Hocevar's avatar
 
Sam Hocevar committed
408
    /* Destroy colormap */
Sam Hocevar's avatar
 
Sam Hocevar committed
409
    if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
Sam Hocevar's avatar
 
Sam Hocevar committed
410 411 412
    {
        XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
413
#else
414
    XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
Sam Hocevar's avatar
 
Sam Hocevar committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
#endif

    DestroyCursor( p_vout );
    EnableXScreenSaver( p_vout );
    DestroyWindow( p_vout );

    XCloseDisplay( p_vout->p_sys->p_display );

    /* Destroy structure */
    free( p_vout->p_sys );
}

/*****************************************************************************
 * vout_Init: initialize X11 video thread output method
 *****************************************************************************
 * This function create the XImages needed by the output thread. It is called
 * at the beginning of the thread, but also each time the window is resized.
 *****************************************************************************/
static int vout_Init( vout_thread_t *p_vout )
{
    int i_index;
    picture_t *p_pic;

    I_OUTPUTPICTURES = 0;

#ifdef MODULE_NAME_IS_xvideo
Sam Hocevar's avatar
 
Sam Hocevar committed
441 442 443 444 445 446
    /* Initialize the output structure; we already found an XVideo port,
     * and the corresponding chroma we will be using. Since we can
     * arbitrary scale, stick to the coordinates and aspect. */
    p_vout->output.i_width  = p_vout->render.i_width;
    p_vout->output.i_height = p_vout->render.i_height;
    p_vout->output.i_aspect = p_vout->render.i_aspect;
Sam Hocevar's avatar
 
Sam Hocevar committed
447

Sam Hocevar's avatar
 
Sam Hocevar committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461
    switch( p_vout->output.i_chroma )
    {
        case FOURCC_RV15:
            p_vout->output.i_rmask = 0x001f;
            p_vout->output.i_gmask = 0x07e0;
            p_vout->output.i_bmask = 0xf800;
            break;
        case FOURCC_RV16:
            p_vout->output.i_rmask = 0x001f;
            p_vout->output.i_gmask = 0x03e0;
            p_vout->output.i_bmask = 0x7c00;
            break;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
462 463 464
#else
    /* Initialize the output structure: RGB with square pixels, whatever
     * the input format is, since it's the only format we know */
Sam Hocevar's avatar
 
Sam Hocevar committed
465 466 467
    switch( p_vout->p_sys->i_screen_depth )
    {
        case 8: /* FIXME: set the palette */
Sam Hocevar's avatar
 
Sam Hocevar committed
468
            p_vout->output.i_chroma = FOURCC_RGB2; break;
Sam Hocevar's avatar
 
Sam Hocevar committed
469
        case 15:
Sam Hocevar's avatar
 
Sam Hocevar committed
470
            p_vout->output.i_chroma = FOURCC_RV15; break;
Sam Hocevar's avatar
 
Sam Hocevar committed
471
        case 16:
Sam Hocevar's avatar
 
Sam Hocevar committed
472
            p_vout->output.i_chroma = FOURCC_RV16; break;
Sam Hocevar's avatar
 
Sam Hocevar committed
473
        case 24:
Sam Hocevar's avatar
 
Sam Hocevar committed
474
            p_vout->output.i_chroma = FOURCC_RV24; break;
Sam Hocevar's avatar
 
Sam Hocevar committed
475
        case 32:
Sam Hocevar's avatar
 
Sam Hocevar committed
476
            p_vout->output.i_chroma = FOURCC_RV24; break;
Sam Hocevar's avatar
 
Sam Hocevar committed
477
        default:
478 479
            msg_Err( p_vout, "unknown screen depth %i",
                     p_vout->p_sys->i_screen_depth );
Sam Hocevar's avatar
 
Sam Hocevar committed
480 481 482
            return( 0 );
    }

gbazin's avatar
 
gbazin committed
483 484 485 486
    vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
                       p_vout->p_sys->i_height,
                       &i_index, &i_index,
                       &p_vout->output.i_width, &p_vout->output.i_height );
Sam Hocevar's avatar
 
Sam Hocevar committed
487 488

    /* Assume we have square pixels */
gbazin's avatar
 
gbazin committed
489 490
    p_vout->output.i_aspect = p_vout->output.i_width
                               * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
Sam Hocevar's avatar
 
Sam Hocevar committed
491 492 493 494 495 496 497 498 499 500
#endif

    /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
    while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
    {
        p_pic = NULL;

        /* Find an empty picture slot */
        for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
        {
gbazin's avatar
 
gbazin committed
501
          if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
Sam Hocevar's avatar
 
Sam Hocevar committed
502 503 504 505 506 507 508 509 510 511 512 513
            {
                p_pic = p_vout->p_picture + i_index;
                break;
            }
        }

        /* Allocate the picture */
        if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
        {
            break;
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
514 515
        p_pic->i_status = DESTROYED_PICTURE;
        p_pic->i_type   = DIRECT_PICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
516 517 518 519 520 521 522 523 524 525

        PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;

        I_OUTPUTPICTURES++;
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
526 527 528 529 530 531 532 533
 * vout_Render: render previously calculated output
 *****************************************************************************/
static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
{
    ;
}

 /*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
534 535 536 537 538 539 540 541 542 543 544 545
 * vout_Display: displays previously rendered output
 *****************************************************************************
 * This function sends the currently rendered image to X11 server.
 * (The Xv extension takes care of "double-buffering".)
 *****************************************************************************/
static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
{
    int i_width, i_height, i_x, i_y;

    vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
                       &i_x, &i_y, &i_width, &i_height );

Sam Hocevar's avatar
 
Sam Hocevar committed
546
#ifdef HAVE_SYS_SHM_H
Sam Hocevar's avatar
 
Sam Hocevar committed
547 548 549
    if( p_vout->p_sys->b_shm )
    {
        /* Display rendered image using shared memory extension */
Sam Hocevar's avatar
 
Sam Hocevar committed
550
#   ifdef MODULE_NAME_IS_xvideo
Sam Hocevar's avatar
 
Sam Hocevar committed
551
        XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
gbazin's avatar
 
gbazin committed
552
                       p_vout->p_sys->video_window, p_vout->p_sys->gc,
Sam Hocevar's avatar
 
Sam Hocevar committed
553 554 555 556
                       p_pic->p_sys->p_image, 0 /*src_x*/, 0 /*src_y*/,
                       p_vout->output.i_width, p_vout->output.i_height,
                       0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
                       False /* Don't put True here or you'll waste your CPU */ );
Sam Hocevar's avatar
 
Sam Hocevar committed
557
#   else
gbazin's avatar
 
gbazin committed
558
        XShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->video_window,
Sam Hocevar's avatar
 
Sam Hocevar committed
559 560 561 562
                      p_vout->p_sys->gc, p_pic->p_sys->p_image,
                      0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
                      p_vout->output.i_width, p_vout->output.i_height,
                      False /* Don't put True here ! */ );
Sam Hocevar's avatar
 
Sam Hocevar committed
563
#   endif
Sam Hocevar's avatar
 
Sam Hocevar committed
564 565
    }
    else
Sam Hocevar's avatar
 
Sam Hocevar committed
566
#endif /* HAVE_SYS_SHM_H */
Sam Hocevar's avatar
 
Sam Hocevar committed
567 568 569 570
    {
        /* Use standard XPutImage -- this is gonna be slow ! */
#ifdef MODULE_NAME_IS_xvideo
        XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
gbazin's avatar
 
gbazin committed
571
                    p_vout->p_sys->video_window, p_vout->p_sys->gc,
Sam Hocevar's avatar
 
Sam Hocevar committed
572 573 574 575
                    p_pic->p_sys->p_image, 0 /*src_x*/, 0 /*src_y*/,
                    p_vout->output.i_width, p_vout->output.i_height,
                    0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
#else
gbazin's avatar
 
gbazin committed
576
        XPutImage( p_vout->p_sys->p_display, p_vout->p_sys->video_window,
Sam Hocevar's avatar
 
Sam Hocevar committed
577 578 579 580 581 582
                   p_vout->p_sys->gc, p_pic->p_sys->p_image,
                   0 /*src_x*/, 0 /*src_y*/, 0 /*dest_x*/, 0 /*dest_y*/,
                   p_vout->output.i_width, p_vout->output.i_height );
#endif
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
583 584
    /* Make sure the command is sent now - do NOT use XFlush !*/
    XSync( p_vout->p_sys->p_display, False );
Sam Hocevar's avatar
 
Sam Hocevar committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
}

/*****************************************************************************
 * vout_Manage: handle X11 events
 *****************************************************************************
 * This function should be called regularly by video output thread. It manages
 * X11 events and allows window resizing. It returns a non null value on
 * error.
 *****************************************************************************/
static int vout_Manage( vout_thread_t *p_vout )
{
    XEvent      xevent;                                         /* X11 event */
    char        i_key;                                    /* ISO Latin-1 key */
    KeySym      x_key_symbol;

    /* Handle X11 events: ConfigureNotify events are parsed to know if the
     * output window's size changed, MapNotify and UnmapNotify to know if the
     * window is mapped (and if the display is useful), and ClientMessages
     * to intercept window destruction requests */

    while( XCheckWindowEvent( p_vout->p_sys->p_display, p_vout->p_sys->window,
                              StructureNotifyMask | KeyPressMask |
                              ButtonPressMask | ButtonReleaseMask | 
                              PointerMotionMask | Button1MotionMask , &xevent )
           == True )
    {
        /* ConfigureNotify event: prepare  */
Sam Hocevar's avatar
 
Sam Hocevar committed
612
        if( xevent.type == ConfigureNotify )
Sam Hocevar's avatar
 
Sam Hocevar committed
613
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
614 615 616 617 618 619 620 621
            if( (xevent.xconfigure.width != p_vout->p_sys->i_width)
                 || (xevent.xconfigure.height != p_vout->p_sys->i_height) )
            {
                /* Update dimensions */
                p_vout->i_changes |= VOUT_SIZE_CHANGE;
                p_vout->p_sys->i_width = xevent.xconfigure.width;
                p_vout->p_sys->i_height = xevent.xconfigure.height;
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
622 623 624 625 626 627 628 629 630
        }
        /* Keyboard event */
        else if( xevent.type == KeyPress )
        {
            /* We may have keys like F1 trough F12, ESC ... */
            x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
                                             xevent.xkey.keycode, 0 );
            switch( x_key_symbol )
            {
631 632 633 634 635 636 637 638 639 640 641 642 643
            case XK_Escape:
                if( p_vout->b_fullscreen )
                {
                    p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
                }
                else
                {
                    p_vout->p_vlc->b_die = 1;
                }
                break;
            case XK_Menu:
                {
                    intf_thread_t *p_intf;
644 645
                    p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
                                                      FIND_ANYWHERE );
646
                    if( p_intf )
Sam Hocevar's avatar
 
Sam Hocevar committed
647
                    {
648 649
                        p_intf->b_menu_change = 1;
                        vlc_object_release( p_intf );
Sam Hocevar's avatar
 
Sam Hocevar committed
650
                    }
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
                }
                break;
            case XK_Left:
                input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
                break;
            case XK_Right:
                input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
                break;
            case XK_Up:
                input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
                break;
            case XK_Down:
                input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
                break;
            case XK_Home:
                input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_SET );
                break;
            case XK_End:
                input_Seek( p_vout, 0, INPUT_SEEK_BYTES | INPUT_SEEK_END );
                break;
            case XK_Page_Up:
                input_Seek( p_vout, 900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
                break;
            case XK_Page_Down:
                input_Seek( p_vout, -900, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
                break;
            case XK_space:
                input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
                break;

Sam Hocevar's avatar
Sam Hocevar committed
681 682 683 684 685 686 687 688 689 690 691 692 693
            case XK_F1: network_ChannelJoin( p_vout, 1 ); break;
            case XK_F2: network_ChannelJoin( p_vout, 2 ); break;
            case XK_F3: network_ChannelJoin( p_vout, 3 ); break;
            case XK_F4: network_ChannelJoin( p_vout, 4 ); break;
            case XK_F5: network_ChannelJoin( p_vout, 5 ); break;
            case XK_F6: network_ChannelJoin( p_vout, 6 ); break;
            case XK_F7: network_ChannelJoin( p_vout, 7 ); break;
            case XK_F8: network_ChannelJoin( p_vout, 8 ); break;
            case XK_F9: network_ChannelJoin( p_vout, 9 ); break;
            case XK_F10: network_ChannelJoin( p_vout, 10 ); break;
            case XK_F11: network_ChannelJoin( p_vout, 11 ); break;
            case XK_F12: network_ChannelJoin( p_vout, 12 ); break;

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
            default:
                /* "Normal Keys"
                 * The reason why I use this instead of XK_0 is that 
                 * with XLookupString, we don't have to care about
                 * keymaps. */

                if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
                {
                    /* FIXME: handle stuff here */
                    switch( i_key )
                    {
                    case 'q':
                    case 'Q':
                        p_vout->p_vlc->b_die = 1;
                        break;
                    case 'f':
                    case 'F':
                        p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
                        break;

                    default:
                        break;
                    }
                }
Sam Hocevar's avatar
 
Sam Hocevar committed
718 719 720 721 722 723
                break;
            }
        }
        /* Mouse click */
        else if( xevent.type == ButtonPress )
        {
724 725 726 727 728 729
            p_vout->i_mouse_x = (int)( (float)xevent.xmotion.x
                / p_vout->p_sys->i_width * p_vout->render.i_width );
            p_vout->i_mouse_y = (int)( (float)xevent.xmotion.y
                / p_vout->p_sys->i_height * p_vout->render.i_height );
            p_vout->i_mouse_button = 1;

Sam Hocevar's avatar
 
Sam Hocevar committed
730 731 732 733
            switch( ((XButtonEvent *)&xevent)->button )
            {
                case Button1:
                    /* In this part we will eventually manage
gbazin's avatar
 
gbazin committed
734 735 736 737 738
                     * clicks for DVD navigation for instance. */

                    /* detect double-clicks */
                    if( ( ((XButtonEvent *)&xevent)->time -
                          p_vout->p_sys->i_time_button_last_pressed ) < 300 )
739
                    {
gbazin's avatar
 
gbazin committed
740
                        p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
741
                    }
gbazin's avatar
 
gbazin committed
742 743 744

                    p_vout->p_sys->i_time_button_last_pressed =
                        ((XButtonEvent *)&xevent)->time;
Sam Hocevar's avatar
 
Sam Hocevar committed
745 746 747
                    break;

                case Button4:
748
                    input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
Sam Hocevar's avatar
 
Sam Hocevar committed
749 750 751
                    break;

                case Button5:
752
                    input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
Sam Hocevar's avatar
 
Sam Hocevar committed
753 754 755 756 757 758 759 760 761
                    break;
            }
        }
        /* Mouse release */
        else if( xevent.type == ButtonRelease )
        {
            switch( ((XButtonEvent *)&xevent)->button )
            {
                case Button3:
762 763
                    {
                        intf_thread_t *p_intf;
764 765
                        p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
                                                          FIND_ANYWHERE );
766 767 768 769 770 771
                        if( p_intf )
                        {
                            p_intf->b_menu_change = 1;
                            vlc_object_release( p_intf );
                        }
                    }
Sam Hocevar's avatar
 
Sam Hocevar committed
772 773 774 775 776 777 778 779 780 781 782
                    break;
            }
        }
        /* Mouse move */
        else if( xevent.type == MotionNotify )
        {
            p_vout->p_sys->i_time_mouse_last_moved = mdate();
            if( ! p_vout->p_sys->b_mouse_pointer_visible )
            {
                ToggleCursor( p_vout ); 
            }
783 784 785 786 787
            
            p_vout->i_mouse_x = (int)( (float)xevent.xmotion.x
                / p_vout->p_sys->i_width * p_vout->render.i_width );
            p_vout->i_mouse_y = (int)( (float)xevent.xmotion.y
                / p_vout->p_sys->i_height * p_vout->render.i_height );
Sam Hocevar's avatar
 
Sam Hocevar committed
788
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
789 790 791 792 793
        /* Reparent move -- XXX: why are we getting this ? */
        else if( xevent.type == ReparentNotify )
        {
            ;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
794 795 796
        /* Other event */
        else
        {
797
            msg_Warn( p_vout, "unhandled event %d received", xevent.type );
Sam Hocevar's avatar
 
Sam Hocevar committed
798 799 800
        }
    }

gbazin's avatar
 
gbazin committed
801
    /* Handle events for video output sub-window */
Sam Hocevar's avatar
 
Sam Hocevar committed
802
    while( XCheckWindowEvent( p_vout->p_sys->p_display,
gbazin's avatar
 
gbazin committed
803
                              p_vout->p_sys->video_window,
Sam Hocevar's avatar
 
Sam Hocevar committed
804 805 806 807 808 809 810 811
                              ExposureMask, &xevent ) == True )
    {
        /* Window exposed (only handled if stream playback is paused) */
        if( xevent.type == Expose )
        {
            if( ((XExposeEvent *)&xevent)->count == 0 )
            {
                /* (if this is the last a collection of expose events...) */
812 813
#if 0
                if( p_vout->p_vlc->p_input_bank->pp_input[0] != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
814
                {
815 816
                    if( PAUSE_S == p_vout->p_vlc->p_input_bank->pp_input[0]
                                                   ->stream.control.i_status )
Sam Hocevar's avatar
 
Sam Hocevar committed
817
                    {
gbazin's avatar
 
gbazin committed
818
                        /* XVideoDisplay( p_vout )*/;
Sam Hocevar's avatar
 
Sam Hocevar committed
819 820
                    }
                }
821
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
822 823 824 825 826 827 828 829 830 831 832 833 834
            }
        }
    }

    /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
     * are handled - according to the man pages, the format is always 32
     * in this case */
    while( XCheckTypedEvent( p_vout->p_sys->p_display,
                             ClientMessage, &xevent ) )
    {
        if( (xevent.xclient.message_type == p_vout->p_sys->wm_protocols)
            && (xevent.xclient.data.l[0] == p_vout->p_sys->wm_delete_window ) )
        {
835
            p_vout->p_vlc->b_die = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
836 837 838
        }
    }

gbazin's avatar
 
gbazin committed
839 840 841
    /*
     * Fullscreen Change
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
842 843
    if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
    {
gbazin's avatar
 
gbazin committed
844
        ToggleFullScreen( p_vout );
Sam Hocevar's avatar
 
Sam Hocevar committed
845 846 847 848 849 850
        p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;

    }

    /*
     * Size change
gbazin's avatar
 
gbazin committed
851 852 853
     *
     * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
     *  the size flag inside the fullscreen routine)
Sam Hocevar's avatar
 
Sam Hocevar committed
854 855 856
     */
    if( p_vout->i_changes & VOUT_SIZE_CHANGE )
    {
gbazin's avatar
 
gbazin committed
857 858
        int i_width, i_height, i_x, i_y;

Sam Hocevar's avatar
 
Sam Hocevar committed
859 860
        p_vout->i_changes &= ~VOUT_SIZE_CHANGE;

861 862
        msg_Dbg( p_vout, "video display resized (%dx%d)",
                         p_vout->p_sys->i_width, p_vout->p_sys->i_height );
gbazin's avatar
 
gbazin committed
863 864
 
#ifdef MODULE_NAME_IS_x11
gbazin's avatar
 
gbazin committed
865 866 867
        /* We need to signal the vout thread about the size change because it
         * is doing the rescaling */
        p_vout->i_changes |= VOUT_SIZE_CHANGE;
gbazin's avatar
 
gbazin committed
868
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
869

gbazin's avatar
 
gbazin committed
870 871 872 873
        vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
                           p_vout->p_sys->i_height,
                           &i_x, &i_y, &i_width, &i_height );

gbazin's avatar
 
gbazin committed
874
        XResizeWindow( p_vout->p_sys->p_display, p_vout->p_sys->video_window,
gbazin's avatar
 
gbazin committed
875 876
                       i_width, i_height );
        
gbazin's avatar
 
gbazin committed
877
        XMoveWindow( p_vout->p_sys->p_display, p_vout->p_sys->video_window,
gbazin's avatar
 
gbazin committed
878
                     i_x, i_y );
Sam Hocevar's avatar
 
Sam Hocevar committed
879 880 881 882 883 884 885 886
    }

    /* Autohide Cursour */
    if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 )
    {
        /* Hide the mouse automatically */
        if( p_vout->p_sys->b_mouse_pointer_visible )
        {
gbazin's avatar
 
gbazin committed
887
            ToggleCursor( p_vout );
Sam Hocevar's avatar
 
Sam Hocevar committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
        }
    }

    return 0;
}

/*****************************************************************************
 * vout_End: terminate X11 video thread output method
 *****************************************************************************
 * Destroy the X11 XImages created by vout_Init. It is called at the end of
 * the thread, but also each time the window is resized.
 *****************************************************************************/
static void vout_End( vout_thread_t *p_vout )
{
    int i_index;

    /* Free the direct buffers we allocated */
    for( i_index = I_OUTPUTPICTURES ; i_index ; )
    {
        i_index--;
        FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
    }
}

/* following functions are local */

/*****************************************************************************
 * CreateWindow: open and set-up X11 main window
 *****************************************************************************/
static int CreateWindow( vout_thread_t *p_vout )
{
    XSizeHints              xsize_hints;
    XSetWindowAttributes    xwindow_attributes;
    XGCValues               xgcvalues;
    XEvent                  xevent;

924 925 926
    vlc_bool_t              b_expose;
    vlc_bool_t              b_configure_notify;
    vlc_bool_t              b_map_notify;
Sam Hocevar's avatar
 
Sam Hocevar committed
927

gbazin's avatar
 
gbazin committed
928
    /* Set main window's size */
gbazin's avatar
 
gbazin committed
929 930
    p_vout->p_sys->i_width = p_vout->i_window_width;
    p_vout->p_sys->i_height = p_vout->i_window_height;
Sam Hocevar's avatar
 
Sam Hocevar committed
931 932 933 934 935 936 937 938 939 940 941 942

    /* Prepare window manager hints and properties */
    xsize_hints.base_width          = p_vout->p_sys->i_width;
    xsize_hints.base_height         = p_vout->p_sys->i_height;
    xsize_hints.flags               = PSize;
    p_vout->p_sys->wm_protocols     = XInternAtom( p_vout->p_sys->p_display,
                                                   "WM_PROTOCOLS", True );
    p_vout->p_sys->wm_delete_window = XInternAtom( p_vout->p_sys->p_display,
                                                   "WM_DELETE_WINDOW", True );

    /* Prepare window attributes */
    xwindow_attributes.backing_store = Always;       /* save the hidden part */
gbazin's avatar
 
gbazin committed
943 944
    xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
                                                     p_vout->p_sys->i_screen);
Sam Hocevar's avatar
 
Sam Hocevar committed
945 946 947
    xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
    

gbazin's avatar
 
gbazin committed
948 949 950
    /* Create the window and set hints - the window must receive
     * ConfigureNotify events, and until it is displayed, Expose and
     * MapNotify events. */
Sam Hocevar's avatar
 
Sam Hocevar committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966

    p_vout->p_sys->window =
        XCreateWindow( p_vout->p_sys->p_display,
                       DefaultRootWindow( p_vout->p_sys->p_display ),
                       0, 0,
                       p_vout->p_sys->i_width,
                       p_vout->p_sys->i_height,
                       0,
                       0, InputOutput, 0,
                       CWBackingStore | CWBackPixel | CWEventMask,
                       &xwindow_attributes );

    /* Set window manager hints and properties: size hints, command,
     * window's name, and accepted protocols */
    XSetWMNormalHints( p_vout->p_sys->p_display, p_vout->p_sys->window,
                       &xsize_hints );
967 968
    /* XXX: DISABLED! makes browsers crash */
#if 0
Sam Hocevar's avatar
 
Sam Hocevar committed
969
    XSetCommand( p_vout->p_sys->p_display, p_vout->p_sys->window,
970 971
                 p_vout->p_vlc->ppsz_argv, p_vout->p_vlc->i_argc );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985
    XStoreName( p_vout->p_sys->p_display, p_vout->p_sys->window,
#ifdef MODULE_NAME_IS_x11
                VOUT_TITLE " (X11 output)"
#else
                VOUT_TITLE " (XVideo output)"
#endif
              );

    if( (p_vout->p_sys->wm_protocols == None)        /* use WM_DELETE_WINDOW */
        || (p_vout->p_sys->wm_delete_window == None)
        || !XSetWMProtocols( p_vout->p_sys->p_display, p_vout->p_sys->window,
                             &p_vout->p_sys->wm_delete_window, 1 ) )
    {
        /* WM_DELETE_WINDOW is not supported by window manager */
986
        msg_Warn( p_vout, "missing or bad window manager" );
gbazin's avatar
 
gbazin committed
987
    } 
Sam Hocevar's avatar
 
Sam Hocevar committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

    /* Creation of a graphic context that doesn't generate a GraphicsExpose
     * event when using functions like XCopyArea */
    xgcvalues.graphics_exposures = False;
    p_vout->p_sys->gc = XCreateGC( p_vout->p_sys->p_display,
                                   p_vout->p_sys->window,
                                   GCGraphicsExposures, &xgcvalues);

    /* Send orders to server, and wait until window is displayed - three
     * events must be received: a MapNotify event, an Expose event allowing
     * drawing in the window, and a ConfigureNotify to get the window
     * dimensions. Once those events have been received, only ConfigureNotify
     * events need to be received. */
    b_expose = 0;
    b_configure_notify = 0;
    b_map_notify = 0;
    XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window);
    do
    {
        XNextEvent( p_vout->p_sys->p_display, &xevent);
        if( (xevent.type == Expose)
            && (xevent.xexpose.window == p_vout->p_sys->window) )
        {
            b_expose = 1;
        }
        else if( (xevent.type == MapNotify)
                 && (xevent.xmap.window == p_vout->p_sys->window) )
        {
            b_map_notify = 1;
        }
        else if( (xevent.type == ConfigureNotify)
                 && (xevent.xconfigure.window == p_vout->p_sys->window) )
        {
            b_configure_notify = 1;
            p_vout->p_sys->i_width = xevent.xconfigure.width;
            p_vout->p_sys->i_height = xevent.xconfigure.height;
        }
    } while( !( b_expose && b_configure_notify && b_map_notify ) );

    XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->window,
                  StructureNotifyMask | KeyPressMask |
                  ButtonPressMask | ButtonReleaseMask | 
                  PointerMotionMask );

#ifdef MODULE_NAME_IS_x11
    if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
    {
        /* Allocate a new palette */
        p_vout->p_sys->colormap =
            XCreateColormap( p_vout->p_sys->p_display,
                             DefaultRootWindow( p_vout->p_sys->p_display ),
                             DefaultVisual( p_vout->p_sys->p_display,
                                            p_vout->p_sys->i_screen ),
                             AllocAll );

        xwindow_attributes.colormap = p_vout->p_sys->colormap;
        XChangeWindowAttributes( p_vout->p_sys->p_display,
                                 p_vout->p_sys->window,
                                 CWColormap, &xwindow_attributes );
    }

gbazin's avatar
 
gbazin committed
1049 1050
#endif
    /* Create video output sub-window. */
1051
    p_vout->p_sys->video_window =  XCreateSimpleWindow(
gbazin's avatar
 
gbazin committed
1052 1053 1054 1055 1056 1057 1058 1059 1060
                                      p_vout->p_sys->p_display,
                                      p_vout->p_sys->window, 0, 0,
                                      p_vout->p_sys->i_width,
                                      p_vout->p_sys->i_height,
                                      0,
                                      BlackPixel( p_vout->p_sys->p_display,
                                                  p_vout->p_sys->i_screen ),
                                      WhitePixel( p_vout->p_sys->p_display,
                                                  p_vout->p_sys->i_screen ) );
1061

gbazin's avatar
 
gbazin committed
1062 1063 1064 1065
    XSetWindowBackground( p_vout->p_sys->p_display,
                          p_vout->p_sys->video_window,
                          BlackPixel( p_vout->p_sys->p_display,
                                      p_vout->p_sys->i_screen ) );
Sam Hocevar's avatar
 
Sam Hocevar committed
1066
    
gbazin's avatar
 
gbazin committed
1067 1068
    XMapWindow( p_vout->p_sys->p_display, p_vout->p_sys->video_window );
    XSelectInput( p_vout->p_sys->p_display, p_vout->p_sys->video_window,
Sam Hocevar's avatar
 
Sam Hocevar committed
1069
                  ExposureMask );
gbazin's avatar
 
gbazin committed
1070

gbazin's avatar
 
gbazin committed
1071
    /* make sure the video window will be centered in the next vout_Manage() */
gbazin's avatar
 
gbazin committed
1072
    p_vout->i_changes |= VOUT_SIZE_CHANGE;
gbazin's avatar
 
gbazin committed
1073

Sam Hocevar's avatar
 
Sam Hocevar committed
1074 1075 1076 1077 1078 1079 1080
    /* If the cursor was formerly blank than blank it again */
    if( !p_vout->p_sys->b_mouse_pointer_visible )
    {
        ToggleCursor( p_vout );
        ToggleCursor( p_vout );
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
1081
    /* Do NOT use XFlush here ! */
Sam Hocevar's avatar
 
Sam Hocevar committed
1082 1083 1084 1085 1086 1087 1088 1089
    XSync( p_vout->p_sys->p_display, False );

    /* At this stage, the window is open, displayed, and ready to
     * receive data */

    return( 0 );
}

gbazin's avatar
 
gbazin committed
1090 1091 1092 1093 1094
/*****************************************************************************
 * DestroyWindow: destroy the window
 *****************************************************************************
 *
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
1095 1096
static void DestroyWindow( vout_thread_t *p_vout )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
1097
    /* Do NOT use XFlush here ! */
Sam Hocevar's avatar
 
Sam Hocevar committed
1098 1099
    XSync( p_vout->p_sys->p_display, False );

gbazin's avatar
 
gbazin committed
1100
    XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->video_window );
Sam Hocevar's avatar
 
Sam Hocevar committed
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
    XUnmapWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
    XFreeGC( p_vout->p_sys->p_display, p_vout->p_sys->gc );
    XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
}

/*****************************************************************************
 * NewPicture: allocate a picture
 *****************************************************************************
 * Returns 0 on success, -1 otherwise
 *****************************************************************************/
static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
1113 1114 1115 1116 1117
    /* We know the chroma, allocate a buffer which will be used
     * directly by the decoder */
    p_pic->p_sys = malloc( sizeof( picture_sys_t ) );

    if( p_pic->p_sys == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
1118
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
1119 1120 1121
        return -1;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
1122
#ifdef HAVE_SYS_SHM_H
Sam Hocevar's avatar
 
Sam Hocevar committed
1123 1124 1125 1126
    if( p_vout->p_sys->b_shm )
    {
        /* Create image using XShm extension */
        p_pic->p_sys->p_image =
1127
            CreateShmImage( p_vout, p_vout->p_sys->p_display,
Sam Hocevar's avatar
 
Sam Hocevar committed
1128
#   ifdef MODULE_NAME_IS_xvideo
1129
                            p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
Sam Hocevar's avatar
 
Sam Hocevar committed
1130
#   else
Sam Hocevar's avatar
 
Sam Hocevar committed
1131 1132
                            p_vout->p_sys->p_visual,
                            p_vout->p_sys->i_screen_depth,
Sam Hocevar's avatar
 
Sam Hocevar committed
1133
#   endif
Sam Hocevar's avatar
 
Sam Hocevar committed
1134 1135 1136 1137
                            &p_pic->p_sys->shminfo,
                            p_vout->output.i_width, p_vout->output.i_height );
    }
    else
Sam Hocevar's avatar
 
Sam Hocevar committed
1138
#endif /* HAVE_SYS_SHM_H */
Sam Hocevar's avatar
 
Sam Hocevar committed
1139 1140 1141
    {
        /* Create image without XShm extension */
        p_pic->p_sys->p_image =
1142
            CreateImage( p_vout, p_vout->p_sys->p_display,
Sam Hocevar's avatar
 
Sam Hocevar committed
1143
#ifdef MODULE_NAME_IS_xvideo
1144
                         p_vout->p_sys->i_xvport, p_vout->output.i_chroma,
Sam Hocevar's avatar
 
Sam Hocevar committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
#else
                         p_vout->p_sys->p_visual,
                         p_vout->p_sys->i_screen_depth, 
                         p_vout->p_sys->i_bytes_per_pixel,
#endif
                         p_vout->output.i_width, p_vout->output.i_height );
    }

    if( p_pic->p_sys->p_image == NULL )
    {
        free( p_pic->p_sys );
        return -1;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
1159
    switch( p_vout->output.i_chroma )
Sam Hocevar's avatar
 
Sam Hocevar committed
1160
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
1161
#ifdef MODULE_NAME_IS_xvideo
Sam Hocevar's avatar
 
Sam Hocevar committed
1162
        case FOURCC_I420:
Sam Hocevar's avatar
 
Sam Hocevar committed
1163

Sam Hocevar's avatar
 
Sam Hocevar committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
            p_pic->Y_PIXELS = p_pic->p_sys->p_image->data
                               + p_pic->p_sys->p_image->offsets[0];
            p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
            p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[0];
            p_pic->p[Y_PLANE].i_pixel_bytes = 1;
            p_pic->p[Y_PLANE].b_margin = 0;

            p_pic->U_PIXELS = p_pic->p_sys->p_image->data
                               + p_pic->p_sys->p_image->offsets[1];
            p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
            p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_image->pitches[1];
Sam Hocevar's avatar