xcommon.c 121 KB
Newer Older
1
2
3
/*****************************************************************************
 * xcommon.c: Functions common to the X11 and XVideo plugins
 *****************************************************************************
4
 * Copyright (C) 1998-2006 the VideoLAN team
5
 * $Id$
6
7
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8
 *          Sam Hocevar <sam@zoy.org>
9
 *          David Kennedy <dkennedy@tinytoad.com>
10
 *          Gildas Bazin <gbazin@videolan.org>
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
26
27
28
29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
30
31
32
33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
zorglub's avatar
zorglub committed
35
36
37
#include <vlc_interface.h>
#include <vlc_playlist.h>
#include <vlc_vout.h>
38
#include <vlc_window.h>
gbazin's avatar
   
gbazin committed
39
#include <vlc_keys.h>
40

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
41
42
#include <errno.h>                                                 /* ENOMEM */

43
44
45
46
47
48
49
50
51
52
53
#ifdef HAVE_MACHINE_PARAM_H
    /* BSD */
#   include <machine/param.h>
#   include <sys/types.h>                                  /* typedef ushort */
#   include <sys/ipc.h>
#endif

#ifndef WIN32
#   include <netinet/in.h>                            /* BSD: struct in_addr */
#endif

54
55
56
57
#ifdef HAVE_XSP
#include <X11/extensions/Xsp.h>
#endif

58
59
60
61
62
#ifdef HAVE_SYS_SHM_H
#   include <sys/shm.h>                                /* shmget(), shmctl() */
#endif

#include <X11/Xlib.h>
63
#include <X11/Xproto.h>
64
#include <X11/Xmd.h>
65
66
#include <X11/Xutil.h>
#include <X11/keysym.h>
67
#include <X11/XF86keysym.h>
68
69
70
71
72
73
74
#ifdef HAVE_SYS_SHM_H
#   include <X11/extensions/XShm.h>
#endif
#ifdef DPMSINFO_IN_DPMS_H
#   include <X11/extensions/dpms.h>
#endif

75
#if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
76
77
78
79
#   include <X11/extensions/Xv.h>
#   include <X11/extensions/Xvlib.h>
#endif

80
81
82
83
#ifdef MODULE_NAME_IS_glx
#   include <GL/glx.h>
#endif

gbazin's avatar
   
gbazin committed
84
85
86
87
#ifdef HAVE_XINERAMA
#   include <X11/extensions/Xinerama.h>
#endif

88
89
90
91
#ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
#   include <X11/extensions/xf86vmode.h>
#endif

92
93
94
95
96
#ifdef MODULE_NAME_IS_xvmc
#   include <X11/extensions/vldXvMC.h>
#   include "../../codec/xvmc/accel_xvmc.h"
#endif

97
98
99
100
101
#include "xcommon.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102
103
int  Activate   ( vlc_object_t * );
void Deactivate ( vlc_object_t * );
104
105
106
107
108

static int  InitVideo      ( vout_thread_t * );
static void EndVideo       ( vout_thread_t * );
static void DisplayVideo   ( vout_thread_t *, picture_t * );
static int  ManageVideo    ( vout_thread_t * );
109
static int  Control        ( vout_thread_t *, int, va_list );
110
111
112
113
114
115
116
117
118

static int  InitDisplay    ( vout_thread_t * );

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

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

Rafaël Carré's avatar
Rafaël Carré committed
119
#ifndef MODULE_NAME_IS_glx
120
static IMAGE_TYPE *CreateImage    ( vout_thread_t *,
121
                                    Display *, EXTRA_ARGS, int, int );
Rafaël Carré's avatar
Rafaël Carré committed
122
123
#endif

124
#ifdef HAVE_SYS_SHM_H
Rafaël Carré's avatar
Rafaël Carré committed
125
#ifndef MODULE_NAME_IS_glx
126
IMAGE_TYPE *CreateShmImage ( vout_thread_t *,
127
                                    Display *, EXTRA_ARGS_SHM, int, int );
Rafaël Carré's avatar
Rafaël Carré committed
128
#endif
129
static int i_shm_major = 0;
130
131
132
133
134
135
136
137
138
139
140
#endif

static void ToggleFullScreen      ( vout_thread_t * );

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 * );

141
#if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
142
static int  XVideoGetPort    ( vout_thread_t *, vlc_fourcc_t, picture_heap_t * );
143
144
145
146
static void XVideoReleasePort( vout_thread_t *, int );
#endif

#ifdef MODULE_NAME_IS_x11
147
148
static void SetPalette     ( vout_thread_t *,
                             uint16_t *, uint16_t *, uint16_t * );
149
150
#endif

151
152
153
154
155
156
#ifdef MODULE_NAME_IS_xvmc
static void RenderVideo    ( vout_thread_t *, picture_t * );
static int  xvmc_check_yv12( Display *display, XvPortID port );
static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
#endif

gbazin's avatar
   
gbazin committed
157
static void TestNetWMSupport( vout_thread_t * );
gbazin's avatar
   
gbazin committed
158
159
static int ConvertKey( int );

160
static int WindowOnTop( vout_thread_t *, bool );
gbazin's avatar
   
gbazin committed
161

162
163
static int X11ErrorHandler( Display *, XErrorEvent * );

164
165
166
167
168
169
170
171
172
173
174
#ifdef HAVE_XSP
static void EnablePixelDoubling( vout_thread_t *p_vout );
static void DisablePixelDoubling( vout_thread_t *p_vout );
#endif

#ifdef HAVE_OSSO
static const int i_backlight_on_interval = 300;
#endif



175
176
177
178
179
180
181
/*****************************************************************************
 * Activate: 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.
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
182
int Activate ( vlc_object_t *p_this )
183
184
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
gbazin's avatar
   
gbazin committed
185
    char *        psz_display;
186
    vlc_value_t   val;
187
188
189
190
#if defined(MODULE_NAME_IS_xvmc)
    char *psz_value;
#endif
#if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
191
192
    char *       psz_chroma;
    vlc_fourcc_t i_chroma = 0;
193
    bool   b_chroma = 0;
194
195
196
197
198
#endif

    p_vout->pf_init = InitVideo;
    p_vout->pf_end = EndVideo;
    p_vout->pf_manage = ManageVideo;
199
200
201
#ifdef MODULE_NAME_IS_xvmc
    p_vout->pf_render = RenderVideo;
#else
202
    p_vout->pf_render = NULL;
203
#endif
204
    p_vout->pf_display = DisplayVideo;
205
    p_vout->pf_control = Control;
206
207
208
209

    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
210
        return VLC_ENOMEM;
211

212
213
214
    /* key and mouse event handling */
    p_vout->p_sys->i_vout_event = var_CreateGetInteger( p_vout, "vout-event" );

215
    /* Open display, using the "display" config variable or the DISPLAY
216
217
218
219
220
     * environment variable */
    psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );

    p_vout->p_sys->p_display = XOpenDisplay( psz_display );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221
    if( p_vout->p_sys->p_display == NULL )                         /* error */
222
223
224
225
    {
        msg_Err( p_vout, "cannot open display %s",
                         XDisplayName( psz_display ) );
        free( p_vout->p_sys );
ivoire's avatar
ivoire committed
226
        free( psz_display );
227
        return VLC_EGENERIC;
228
    }
ivoire's avatar
ivoire committed
229
    free( psz_display );
230

231
232
233
    /* Replace error handler so we can intercept some non-fatal errors */
    XSetErrorHandler( X11ErrorHandler );

234
235
236
    /* Get a screen ID matching the XOpenDisplay return value */
    p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );

237
#if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
238
239
240
241
242
    psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
    if( psz_chroma )
    {
        if( strlen( psz_chroma ) >= 4 )
        {
243
244
245
            /* Do not use direct assignment because we are not sure of the
             * alignment. */
            memcpy(&i_chroma, psz_chroma, 4);
246
247
248
249
250
251
252
253
            b_chroma = 1;
        }

        free( psz_chroma );
    }

    if( b_chroma )
    {
254
        msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
255
256
257
258
259
260
261
262
                 i_chroma, (char*)&i_chroma );
    }
    else
    {
        i_chroma = p_vout->render.i_chroma;
    }

    /* Check that we have access to an XVideo port providing this chroma */
263
    p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
264
                                             &p_vout->output );
265
266
267
    if( p_vout->p_sys->i_xvport < 0 )
    {
        /* If a specific chroma format was requested, then we don't try to
268
         * be cleverer than the user. He knew pretty well what he wanted. */
269
270
271
272
        if( b_chroma )
        {
            XCloseDisplay( p_vout->p_sys->p_display );
            free( p_vout->p_sys );
273
            return VLC_EGENERIC;
274
275
276
277
278
279
        }

        /* It failed, but it's not completely lost ! We try to open an
         * XVideo port for an YUY2 picture. We'll need to do an YUV
         * conversion, but at least it has got scaling. */
        p_vout->p_sys->i_xvport =
280
                        XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
281
                                               &p_vout->output );
282
283
284
285
286
287
        if( p_vout->p_sys->i_xvport < 0 )
        {
            /* 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. */
            p_vout->p_sys->i_xvport =
288
                            XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
289
                                                   &p_vout->output );
290
291
292
293
            if( p_vout->p_sys->i_xvport < 0 )
            {
                XCloseDisplay( p_vout->p_sys->p_display );
                free( p_vout->p_sys );
294
                return VLC_EGENERIC;
295
296
297
            }
        }
    }
298
    p_vout->output.i_chroma = X112VLC_FOURCC(p_vout->output.i_chroma);
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#elif defined(MODULE_NAME_IS_glx)
    {
        int i_opcode, i_evt, i_err = 0;
        int i_maj, i_min = 0;

        /* Check for GLX extension */
        if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
                              &i_opcode, &i_evt, &i_err ) )
        {
            msg_Err( p_this, "GLX extension not supported" );
            XCloseDisplay( p_vout->p_sys->p_display );
            free( p_vout->p_sys );
            return VLC_EGENERIC;
        }
        if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
        {
            msg_Err( p_this, "glXQueryExtension failed" );
            XCloseDisplay( p_vout->p_sys->p_display );
            free( p_vout->p_sys );
            return VLC_EGENERIC;
        }

        /* Check GLX version */
        if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
        {
            msg_Err( p_this, "glXQueryVersion failed" );
            XCloseDisplay( p_vout->p_sys->p_display );
            free( p_vout->p_sys );
            return VLC_EGENERIC;
        }
        if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
        {
331
            p_vout->p_sys->b_glx13 = false;
332
333
334
335
            msg_Dbg( p_this, "using GLX 1.2 API" );
        }
        else
        {
336
            p_vout->p_sys->b_glx13 = true;
337
338
339
            msg_Dbg( p_this, "using GLX 1.3 API" );
        }
    }
340
341
342
343
#endif

    /* Create blank cursor (for mouse cursor autohiding) */
    p_vout->p_sys->i_time_mouse_last_moved = mdate();
344
345
    p_vout->p_sys->i_mouse_hide_timeout =
        var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
346
347
348
349
350
351
    p_vout->p_sys->b_mouse_pointer_visible = 1;
    CreateCursor( p_vout );

    /* Set main window's size */
    p_vout->p_sys->original_window.i_width = p_vout->i_window_width;
    p_vout->p_sys->original_window.i_height = p_vout->i_window_height;
352
    var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
353
354
355
356
357
358
359
360
    /* Spawn base window - this window will include the video output window,
     * but also command buttons, subtitles and other indicators */
    if( CreateWindow( p_vout, &p_vout->p_sys->original_window ) )
    {
        msg_Err( p_vout, "cannot create X11 window" );
        DestroyCursor( p_vout );
        XCloseDisplay( p_vout->p_sys->p_display );
        free( p_vout->p_sys );
361
        return VLC_EGENERIC;
362
363
364
365
366
367
368
369
370
371
    }

    /* Open and initialize device. */
    if( InitDisplay( p_vout ) )
    {
        msg_Err( p_vout, "cannot initialize X11 display" );
        DestroyCursor( p_vout );
        DestroyWindow( p_vout, &p_vout->p_sys->original_window );
        XCloseDisplay( p_vout->p_sys->p_display );
        free( p_vout->p_sys );
372
        return VLC_EGENERIC;
373
374
375
376
377
378
379
380
381
    }

    /* Disable screen saver */
    DisableXScreenSaver( p_vout );

    /* Misc init */
    p_vout->p_sys->b_altfullscreen = 0;
    p_vout->p_sys->i_time_button_last_pressed = 0;

gbazin's avatar
   
gbazin committed
382
383
    TestNetWMSupport( p_vout );

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#ifdef MODULE_NAME_IS_xvmc
    p_vout->p_sys->p_last_subtitle_save = NULL;
    psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );

    /* Look what method was requested */
    //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
    //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
    if( psz_value )
    {
        if( (strcmp(psz_value, "bob") == 0) ||
            (strcmp(psz_value, "blend") == 0) )
           p_vout->p_sys->xvmc_deinterlace_method = 2;
        else if (strcmp(psz_value, "discard") == 0)
           p_vout->p_sys->xvmc_deinterlace_method = 1;
        else
           p_vout->p_sys->xvmc_deinterlace_method = 0;
        free(psz_value );
    }
    else
        p_vout->p_sys->xvmc_deinterlace_method = 0;

    /* Look what method was requested */
    //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
    //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
    psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );

    if( psz_value )
    {
        if( strncmp( psz_value, "eq", 2 ) == 0 )
           p_vout->p_sys->xvmc_crop_style = 1;
        else if( strncmp( psz_value, "4-16", 4 ) == 0)
           p_vout->p_sys->xvmc_crop_style = 2;
        else if( strncmp( psz_value, "16-4", 4 ) == 0)
           p_vout->p_sys->xvmc_crop_style = 3;
        else
           p_vout->p_sys->xvmc_crop_style = 0;
        free( psz_value );
    }
    else
        p_vout->p_sys->xvmc_crop_style = 0;

    msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
    msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);

428
    if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
429
430
    {
        msg_Err( p_vout, "no XVMC capability found" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
431
        Deactivate( p_vout );
432
433
        return VLC_EGENERIC;
    }
434
    subpicture_t sub_pic;
435
436
437
438
    sub_pic.p_sys = NULL;
    p_vout->p_sys->last_date = 0;
#endif

439
440
441
#ifdef HAVE_XSP
    p_vout->p_sys->i_hw_scale = 1;
#endif
Jean-Paul Saman's avatar
Jean-Paul Saman committed
442

443
#ifdef HAVE_OSSO
444
    p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
445
446
447
448
449
450
451
    p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
    if ( p_vout->p_sys->p_octx == NULL ) {
        msg_Err( p_vout, "Could not get osso context" );
    } else {
        msg_Dbg( p_vout, "Initialized osso context" );
    }
#endif
Jean-Paul Saman's avatar
Jean-Paul Saman committed
452

453
    /* Variable to indicate if the window should be on top of others */
gbazin's avatar
   
gbazin committed
454
455
456
457
    /* Trigger a callback right now */
    var_Get( p_vout, "video-on-top", &val );
    var_Set( p_vout, "video-on-top", val );

458
    return VLC_SUCCESS;
459
460
461
462
463
464
465
}

/*****************************************************************************
 * Deactivate: destroy X11 video thread output method
 *****************************************************************************
 * Terminate an output method created by Open
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
466
void Deactivate ( vlc_object_t *p_this )
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

    /* If the fullscreen window is still open, close it */
    if( p_vout->b_fullscreen )
    {
        ToggleFullScreen( p_vout );
    }

    /* Restore cursor if it was blanked */
    if( !p_vout->p_sys->b_mouse_pointer_visible )
    {
        ToggleCursor( p_vout );
    }

#ifdef MODULE_NAME_IS_x11
    /* Destroy colormap */
    if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
    {
        XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
    }
488
#elif defined(MODULE_NAME_IS_xvideo)
489
    XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#elif defined(MODULE_NAME_IS_xvmc)
    if( p_vout->p_sys->xvmc_cap )
    {
        xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
        xxmc_dispose_context( p_vout );
        if( p_vout->p_sys->old_subpic )
        {
            xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
            p_vout->p_sys->old_subpic = NULL;
        }
        if( p_vout->p_sys->new_subpic )
        {
            xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
            p_vout->p_sys->new_subpic = NULL;
        }
        free( p_vout->p_sys->xvmc_cap );
        xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
    }
508
509
#endif

510
511
512
#ifdef HAVE_XSP
    DisablePixelDoubling(p_vout);
#endif
513

514
515
516
517
518
519
    DestroyCursor( p_vout );
    EnableXScreenSaver( p_vout );
    DestroyWindow( p_vout, &p_vout->p_sys->original_window );
    XCloseDisplay( p_vout->p_sys->p_display );

    /* Destroy structure */
520
521
522
523
#ifdef MODULE_NAME_IS_xvmc
    free_context_lock( &p_vout->p_sys->xvmc_lock );
#endif

524
525
526
527
528
529
#ifdef HAVE_OSSO
    if ( p_vout->p_sys->p_octx != NULL ) {
        msg_Dbg( p_vout, "Deinitializing osso context" );
        osso_deinitialize( p_vout->p_sys->p_octx );
    }
#endif
Jean-Paul Saman's avatar
Jean-Paul Saman committed
530

531
532
533
    free( p_vout->p_sys );
}

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#ifdef MODULE_NAME_IS_xvmc

#define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')

/* called xlocked */
static int xvmc_check_yv12( Display *display, XvPortID port )
{
    XvImageFormatValues *formatValues;
    int                  formats;
    int                  i;

    formatValues = XvListImageFormats( display, port, &formats );

    for( i = 0; i < formats; i++ )
    {
        if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
            ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
        {
            XFree (formatValues);
            return 0;
        }
    }

    XFree (formatValues);
    return 1;
}

static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
{
    XvMCSyncSurface( p_vout->p_sys->p_display, srf );
}

static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
{
    Atom         atom;
    int          xv_double_buffer;

    xv_double_buffer = 1;

    XLockDisplay( p_vout->p_sys->p_display );
    atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
#if 0
    XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
#endif
    XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
    XUnlockDisplay( p_vout->p_sys->p_display );

    //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
    //    "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
}

static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
{
    vlc_xxmc_t *xxmc = NULL;

    xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );

    xxmc = &p_pic->p_sys->xxmc_data;
    if( (!xxmc->decoded ||
        !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
    {
        xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
        return;
    }

599
#if 0
600
    vlc_mutex_lock( &p_vout->lastsubtitle_lock );
601
    if (p_vout->p_sys->p_last_subtitle != NULL)
602
    {
603
        if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
        {
            p_vout->p_sys->new_subpic =
                xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
                    p_vout->p_sys->xvmc_width,
                    p_vout->p_sys->xvmc_height,
                    p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );

            if (p_vout->p_sys->new_subpic)
            {
                XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
                XvMCClearSubpicture( p_vout->p_sys->p_display,
                        p_vout->p_sys->new_subpic,
                        0,
                        0,
                        p_vout->p_sys->xvmc_width,
                        p_vout->p_sys->xvmc_height,
                        0x00 );
                XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
                clear_xx44_palette( &p_vout->p_sys->palette );

                if( sub_pic.p_sys == NULL )
                {
                    sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
                    if( sub_pic.p_sys != NULL )
                    {
                        sub_pic.p_sys->p_vout = p_vout;
                        sub_pic.p_sys->xvmc_surf = NULL;
                        sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
                    }
                }
                sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
                sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
                sub_pic.p->i_pitch = p_vout->output.i_width;

                memset( p_vout->p_sys->subImage->data, 0,
                        (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );

                if (p_vout->p_last_subtitle != NULL)
                {
                    blend_xx44( p_vout->p_sys->subImage->data,
                                p_vout->p_last_subtitle,
                                p_vout->p_sys->subImage->width,
                                p_vout->p_sys->subImage->height,
                                p_vout->p_sys->subImage->width,
                                &p_vout->p_sys->palette,
                                (p_vout->p_sys->subImage->id == FOURCC_IA44) );
                }

                XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
                XvMCCompositeSubpicture( p_vout->p_sys->p_display,
                                         p_vout->p_sys->new_subpic,
                                         p_vout->p_sys->subImage,
                                         0, /* overlay->x */
                                         0, /* overlay->y */
                                         p_vout->output.i_width, /* overlay->width, */
                                         p_vout->output.i_height, /* overlay->height */
                                         0, /* overlay->x */
                                         0 ); /*overlay->y */
                XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
                if (p_vout->p_sys->old_subpic)
                {
                    xxmc_xvmc_free_subpicture( p_vout,
                                               p_vout->p_sys->old_subpic);
                    p_vout->p_sys->old_subpic = NULL;
                }
                if (p_vout->p_sys->new_subpic)
                {
                    p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
                    p_vout->p_sys->new_subpic = NULL;
                    xx44_to_xvmc_palette( &p_vout->p_sys->palette,
                            p_vout->p_sys->xvmc_palette,
                            0,
                            p_vout->p_sys->old_subpic->num_palette_entries,
                            p_vout->p_sys->old_subpic->entry_bytes,
                            p_vout->p_sys->old_subpic->component_order );
                    XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
                    XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
                                              p_vout->p_sys->old_subpic,
                                              p_vout->p_sys->xvmc_palette );
                    XvMCFlushSubpicture( p_vout->p_sys->p_display,
                                         p_vout->p_sys->old_subpic);
                    XvMCSyncSubpicture( p_vout->p_sys->p_display,
                                        p_vout->p_sys->old_subpic );
                    XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
                }

                XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
                if (p_vout->p_sys->xvmc_backend_subpic )
                {
                    XvMCBlendSubpicture( p_vout->p_sys->p_display,
                                         p_pic->p_sys->xvmc_surf,
                                         p_vout->p_sys->old_subpic,
                                         0,
                                         0,
                                         p_vout->p_sys->xvmc_width,
                                         p_vout->p_sys->xvmc_height,
                                         0,
                                         0,
                                         p_vout->p_sys->xvmc_width,
                                         p_vout->p_sys->xvmc_height );
                }
                else
                {
                    XvMCBlendSubpicture2( p_vout->p_sys->p_display,
                                          p_pic->p_sys->xvmc_surf,
                                          p_pic->p_sys->xvmc_surf,
                                          p_vout->p_sys->old_subpic,
                                          0,
                                          0,
                                          p_vout->p_sys->xvmc_width,
                                          p_vout->p_sys->xvmc_height,
                                          0,
                                          0,
                                          p_vout->p_sys->xvmc_width,
                                          p_vout->p_sys->xvmc_height );
               }
               XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
            }
        }
        else
        {
            XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
            if( p_vout->p_sys->xvmc_backend_subpic )
            {
                XvMCBlendSubpicture( p_vout->p_sys->p_display,
                                     p_pic->p_sys->xvmc_surf,
                                     p_vout->p_sys->old_subpic,
                                     0, 0,
                                     p_vout->p_sys->xvmc_width,
                                     p_vout->p_sys->xvmc_height,
                                     0, 0,
                                     p_vout->p_sys->xvmc_width,
                                     p_vout->p_sys->xvmc_height );
            }
            else
            {
                XvMCBlendSubpicture2( p_vout->p_sys->p_display,
                                      p_pic->p_sys->xvmc_surf,
                                      p_pic->p_sys->xvmc_surf,
                                      p_vout->p_sys->old_subpic,
                                      0, 0,
                                      p_vout->p_sys->xvmc_width,
                                      p_vout->p_sys->xvmc_height,
                                      0, 0,
                                      p_vout->p_sys->xvmc_width,
                                      p_vout->p_sys->xvmc_height );
            }
            XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
        }
    }
    p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;

    vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
757
#endif
758
759
760
761
    xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
}
#endif

762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#ifdef HAVE_XSP
/*****************************************************************************
 * EnablePixelDoubling: Enables pixel doubling
 *****************************************************************************
 * Checks if the double size image fits in current window, and enables pixel
 * doubling accordingly. The i_hw_scale is the integer scaling factor.
 *****************************************************************************/
static void EnablePixelDoubling( vout_thread_t *p_vout )
{
    int i_hor_scale = ( p_vout->p_sys->p_win->i_width ) / p_vout->render.i_width;
    int i_vert_scale =  ( p_vout->p_sys->p_win->i_height ) / p_vout->render.i_height;
    if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
        p_vout->p_sys->i_hw_scale = 2;
        msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
        XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
    }
}

/*****************************************************************************
 * DisablePixelDoubling: Disables pixel doubling
 *****************************************************************************
 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
 *****************************************************************************/
static void DisablePixelDoubling( vout_thread_t *p_vout )
{
    if ( p_vout->p_sys->i_hw_scale > 1 ) {
        msg_Dbg( p_vout, "Disabling pixel doubling" );
        XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
        p_vout->p_sys->i_hw_scale = 1;
    }
}
#endif



797
798
799
800
801
802
803
804
/*****************************************************************************
 * InitVideo: 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 InitVideo( vout_thread_t *p_vout )
{
805
    unsigned int i_index = 0;
806
807
808
809
    picture_t *p_pic;

    I_OUTPUTPICTURES = 0;

810
#if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
811
812
813
814
815
816
817
    /* 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;

818
819
820
    p_vout->fmt_out = p_vout->fmt_in;
    p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;

821
#if XvVersion < 2 || ( XvVersion == 2 && XvRevision < 2 )
822
823
    switch( p_vout->output.i_chroma )
    {
824
825
826
827
828
829
        case VLC_FOURCC('R','V','1','6'):
#if defined( WORDS_BIGENDIAN )
            p_vout->output.i_rmask = 0xf800;
            p_vout->output.i_gmask = 0x07e0;
            p_vout->output.i_bmask = 0x001f;
#else
830
831
832
            p_vout->output.i_rmask = 0x001f;
            p_vout->output.i_gmask = 0x07e0;
            p_vout->output.i_bmask = 0xf800;
833
#endif
834
            break;
835
836
837
838
839
840
        case VLC_FOURCC('R','V','1','5'):
#if defined( WORDS_BIGENDIAN )
            p_vout->output.i_rmask = 0x7c00;
            p_vout->output.i_gmask = 0x03e0;
            p_vout->output.i_bmask = 0x001f;
#else
841
842
843
            p_vout->output.i_rmask = 0x001f;
            p_vout->output.i_gmask = 0x03e0;
            p_vout->output.i_bmask = 0x7c00;
844
#endif
845
846
            break;
    }
847
#endif
848

849
#elif defined(MODULE_NAME_IS_x11)
850
851
852
853
854
855
856
857
858
859
860
861
    /* Initialize the output structure: RGB with square pixels, whatever
     * the input format is, since it's the only format we know */
    switch( p_vout->p_sys->i_screen_depth )
    {
        case 8: /* FIXME: set the palette */
            p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
        case 15:
            p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
        case 16:
            p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
        case 24:
        case 32:
gbazin's avatar
   
gbazin committed
862
            p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
863
864
865
        default:
            msg_Err( p_vout, "unknown screen depth %i",
                     p_vout->p_sys->i_screen_depth );
866
            return VLC_SUCCESS;
867
868
    }

869
870
871
872
873
874
875
#ifdef HAVE_XSP
    vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width  / p_vout->p_sys->i_hw_scale,
                       p_vout->p_sys->p_win->i_height  / p_vout->p_sys->i_hw_scale,
                       &i_index, &i_index,
                       &p_vout->fmt_out.i_visible_width,
                       &p_vout->fmt_out.i_visible_height );
#else
876
877
878
    vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
                       p_vout->p_sys->p_win->i_height,
                       &i_index, &i_index,
879
880
                       &p_vout->fmt_out.i_visible_width,
                       &p_vout->fmt_out.i_visible_height );
881
#endif
882

883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
    p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;

    p_vout->output.i_width = p_vout->fmt_out.i_width =
        p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
        p_vout->fmt_in.i_visible_width;
    p_vout->output.i_height = p_vout->fmt_out.i_height =
        p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
        p_vout->fmt_in.i_visible_height;
    p_vout->fmt_out.i_x_offset =
        p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
        p_vout->fmt_in.i_visible_width;
    p_vout->fmt_out.i_y_offset =
        p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
        p_vout->fmt_in.i_visible_height;

    p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
    p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
        p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;

    msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
             p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
             p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
             p_vout->fmt_out.i_visible_width,
             p_vout->fmt_out.i_visible_height );
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
#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++ )
        {
          if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
            {
                p_pic = p_vout->p_picture + i_index;
                break;
            }
        }

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

        p_pic->i_status = DESTROYED_PICTURE;
        p_pic->i_type   = DIRECT_PICTURE;

        PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;

        I_OUTPUTPICTURES++;
    }

938
939
940
941
942
    if( p_vout->output.i_chroma == VLC_FOURCC('Y','V','1','2') )
    {
        /* U and V inverted compared to I420
         * Fixme: this should be handled by the vout core */
        p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
943
        p_vout->fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
944
945
    }

946
    return VLC_SUCCESS;
947
948
}

949
/*****************************************************************************
950
951
952
953
954
955
956
 * DisplayVideo: displays previously rendered output
 *****************************************************************************
 * This function sends the currently rendered image to X11 server.
 * (The Xv extension takes care of "double-buffering".)
 *****************************************************************************/
static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
{
957
    unsigned int i_width, i_height, i_x, i_y;
958
959
960
961
962

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

963
964
965
#ifdef MODULE_NAME_IS_xvmc
    xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );

966
    vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
967
    if( !xxmc->decoded ||
968
        !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
969
970
971
    {
      msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
               xxmc->decoded,
972
               xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
973
974
975
976
      xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
      return;
    }

977
978
979
    int src_width = p_vout->output.i_width;
    int src_height = p_vout->output.i_height;
    int src_x, src_y;
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007

    if( p_vout->p_sys->xvmc_crop_style == 1 )
    {
        src_x = 20;
        src_y = 20;
        src_width -= 40;
        src_height -= 40;
    }
    else if( p_vout->p_sys->xvmc_crop_style == 2 )
    {
        src_x = 20;
        src_y = 40;
        src_width -= 40;
        src_height -= 80;
    }
    else if( p_vout->p_sys->xvmc_crop_style == 3 )
    {
        src_x = 40;
        src_y = 20;
        src_width -= 80;
        src_height -= 40;
    }
    else
    {
        src_x = 0;
        src_y = 0;
    }

1008
    int first_field;
1009
1010
    if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
    {   /* BOB DEINTERLACE */
1011
        if( (p_pic->p_sys->nb_display == 0) ||
1012
1013
            (p_vout->p_sys->xvmc_deinterlace_method == 1) )
        {
1014
            first_field = (p_pic->b_top_field_first) ?
1015
1016
1017
1018
                                XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
        }
        else
        {
1019
            first_field = (p_pic->b_top_field_first) ?
1020
1021
1022
1023
1024
1025
1026
1027
1028
                                XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
        }
    }
    else
    {
        first_field = XVMC_FRAME_PICTURE;
     }

    XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1029
    XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
1030
1031
    /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
    XvMCPutSurface( p_vout->p_sys->p_display,
1032
                    p_pic->p_sys->xvmc_surf,
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
                    p_vout->p_sys->p_win->video_window,
                    src_x,
                    src_y,
                    src_width,
                    src_height,
                    0 /*dest_x*/,
                    0 /*dest_y*/,
                    i_width,
                    i_height,
                    first_field);

    XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1045
    if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1046
    {   /* BOB DEINTERLACE */
1047
        if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1048
        {
1049
            mtime_t last_date = p_pic->date;
1050
1051
1052
1053

            vlc_mutex_lock( &p_vout->picture_lock );
            if( !p_vout->p_sys->last_date )
            {
1054
                p_pic->date += 20000;
1055
1056
1057
            }
            else
            {
1058
                p_pic->date = ((3 * p_pic->date -
1059
1060
1061
                                    p_vout->p_sys->last_date) / 2 );
            }
            p_vout->p_sys->last_date = last_date;
1062
1063
            p_pic->b_force = 1;
            p_pic->p_sys->nb_display = 1;
1064
1065
1066
1067
            vlc_mutex_unlock( &p_vout->picture_lock );
        }
        else
        {
1068
1069
            p_pic->p_sys->nb_display = 0;
            p_pic->b_force = 0;
1070
1071
1072
1073
1074
        }
    }
    xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
#endif

1075
#ifdef HAVE_SYS_SHM_H
1076
    if( p_vout->p_sys->i_shm_opcode )
1077
1078
    {
        /* Display rendered image using shared memory extension */
1079
#   if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1080
1081
1082
        XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
                       p_vout->p_sys->p_win->video_window,
                       p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1083
1084
                       p_vout->fmt_out.i_x_offset,
                       p_vout->fmt_out.i_y_offset,
1085
1086
                       p_vout->fmt_out.i_visible_width,
                       p_vout->fmt_out.i_visible_height,
1087
1088
1089
1090
1091
1092
                       0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
                       False /* Don't put True here or you'll waste your CPU */ );
#   else
        XShmPutImage( p_vout->p_sys->p_display,
                      p_vout->p_sys->p_win->video_window,
                      p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1093
1094
1095
1096
1097
                      p_vout->fmt_out.i_x_offset,
                      p_vout->fmt_out.i_y_offset,
                      0 /*dest_x*/, 0 /*dest_y*/,
                      p_vout->fmt_out.i_visible_width,
                      p_vout->fmt_out.i_visible_height,
1098
1099
1100
1101
1102
1103
1104
                      False /* Don't put True here ! */ );
#   endif
    }
    else
#endif /* HAVE_SYS_SHM_H */
    {
        /* Use standard XPutImage -- this is gonna be slow ! */
1105
#if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1106
1107
1108
        XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
                    p_vout->p_sys->p_win->video_window,
                    p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1109
1110
                    p_vout->fmt_out.i_x_offset,
                    p_vout->fmt_out.i_y_offset,
1111
1112
                    p_vout->fmt_out.i_visible_width,
                    p_vout->fmt_out.i_visible_height,
1113
1114
1115
1116
1117
                    0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
#else
        XPutImage( p_vout->p_sys->p_display,
                   p_vout->p_sys->p_win->video_window,
                   p_vout->p_sys->p_win->gc, p_pic->p_sys->p_image,
1118
1119
1120
1121
1122
                   p_vout->fmt_out.i_x_offset,
                   p_vout->fmt_out.i_y_offset,
                   0 /*dest_x*/, 0 /*dest_y*/,
                   p_vout->fmt_out.i_visible_width,
                   p_vout->fmt_out.i_visible_height );
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
#endif
    }

    /* Make sure the command is sent now - do NOT use XFlush !*/
    XSync( p_vout->p_sys->p_display, False );
}

/*****************************************************************************
 * ManageVideo: 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 ManageVideo( vout_thread_t *p_vout )
{
    XEvent      xevent;                                         /* X11 event */
1140
    vlc_value_t val;
1141

1142
1143
1144
1145
#ifdef MODULE_NAME_IS_xvmc
    xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
#endif

1146
1147
1148
1149
    /* Handle events from the owner window */
    if( p_vout->p_sys->p_win->owner_window )
    {
        while( XCheckWindowEvent( p_vout->p_sys->p_display,
1150
                                p_vout->p_sys->p_win->owner_window->handle.xid,
1151
1152
1153
1154
1155
                                  StructureNotifyMask, &xevent ) == True )
        {
            /* ConfigureNotify event: prepare  */
            if( xevent.type == ConfigureNotify )
            {
1156
1157
1158
1159
1160
                /* Update dimensions */
                XResizeWindow( p_vout->p_sys->p_display,
                               p_vout->p_sys->p_win->base_window,
                               xevent.xconfigure.width,
                               xevent.xconfigure.height );
1161
1162
1163
1164
            }
        }
    }

1165
1166
1167
1168
1169
1170
1171
    /* 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->p_win->base_window,
gbazin's avatar
   
gbazin committed
1172
                              StructureNotifyMask | KeyPressMask |
1173
                              ButtonPressMask | ButtonReleaseMask |
gbazin's avatar
   
gbazin committed
1174
1175
                              PointerMotionMask | Button1MotionMask , &xevent )
           == True )
1176
1177
1178
1179
    {
        /* ConfigureNotify event: prepare  */
        if( xevent.type == ConfigureNotify )
        {
1180
1181
1182
1183
            if( (unsigned int)xevent.xconfigure.width
                   != p_vout->p_sys->p_win->i_width
              || (unsigned int)xevent.xconfigure.height
                    != p_vout->p_sys->p_win->i_height )
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
            {
                /* Update dimensions */
                p_vout->i_changes |= VOUT_SIZE_CHANGE;
                p_vout->p_sys->p_win->i_width = xevent.xconfigure.width;
                p_vout->p_sys->p_win->i_height = xevent.xconfigure.height;
            }
        }
        /* Keyboard event */
        else if( xevent.type == KeyPress )
        {
gbazin's avatar
   
gbazin committed
1194
1195
1196
1197
            unsigned int state = xevent.xkey.state;
            KeySym x_key_symbol;
            char i_key;                                   /* ISO Latin-1 key */

1198
1199
1200
            /* We may have keys like F1 trough F12, ESC ... */
            x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
                                             xevent.xkey.keycode, 0 );
gbazin's avatar
   
gbazin committed
1201
            val.i_int = ConvertKey( (int)x_key_symbol );
1202

gbazin's avatar
   
gbazin committed
1203
1204
1205
1206
1207
1208
1209
            xevent.xkey.state &= ~ShiftMask;
            xevent.xkey.state &= ~ControlMask;
            xevent.xkey.state &= ~Mod1Mask;

            if( !val.i_int &&
                XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
            {
1210
                /* "Normal Keys"
1211
                 * The reason why I use this instead of XK_0 is that
1212
1213
                 * with XLookupString, we don't have to care about
                 * keymaps. */
gbazin's avatar
   
gbazin committed
1214
                val.i_int = i_key;
1215
            }
gbazin's avatar
   
gbazin committed
1216
1217

            if( val.i_int )
sigmunau's avatar
sigmunau committed
1218
            {
gbazin's avatar
   
gbazin committed
1219
                if( state & ShiftMask )
sigmunau's avatar
sigmunau committed
1220
1221
1222
                {
                    val.i_int |= KEY_MODIFIER_SHIFT;
                }
gbazin's avatar
   
gbazin committed
1223
                if( state & ControlMask )
sigmunau's avatar
sigmunau committed
1224
1225
1226
                {
                    val.i_int |= KEY_MODIFIER_CTRL;
                }
gbazin's avatar
   
gbazin committed
1227
                if( state & Mod1Mask )
sigmunau's avatar
sigmunau committed
1228
1229
1230
                {
                    val.i_int |= KEY_MODIFIER_ALT;
                }
1231
                var_Set( p_vout->p_libvlc, "key-pressed", val );
sigmunau's avatar
sigmunau committed
1232
            }
1233
1234
1235
1236
1237
1238
1239
        }
        /* Mouse click */
        else if( xevent.type == ButtonPress )
        {
            switch( ((XButtonEvent *)&xevent)->button )
            {
                case Button1:
1240
1241
1242
                    var_Get( p_vout, "mouse-button-down", &val );
                    val.i_int |= 1;
                    var_Set( p_vout, "mouse-button-down", val );
1243

1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
                    /* detect double-clicks */
                    if( ( ((XButtonEvent *)&xevent)->time -
                          p_vout->p_sys->i_time_button_last_pressed ) < 300 )
                    {
                        p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
                    }

                    p_vout->p_sys->i_time_button_last_pressed =
                        ((XButtonEvent *)&xevent)->time;
                    break;
1254
1255
1256
1257
1258
                case Button2:
                    var_Get( p_vout, "mouse-button-down", &val );
                    val.i_int |= 2;
                    var_Set( p_vout, "mouse-button-down", val );
                    break;
1259

1260
1261
1262
1263
1264
                case Button3:
                    var_Get( p_vout, "mouse-button-down", &val );
                    val.i_int |= 4;
                    var_Set( p_vout, "mouse-button-down", val );
                    break;
1265

1266
                case Button4:
1267
1268
1269
                    var_Get( p_vout, "mouse-button-down", &val );
                    val.i_int |= 8;
                    var_Set( p_vout, "mouse-button-down", val );
1270
1271
1272
                    break;

                case Button5:
1273
1274
1275
                    var_Get( p_vout, "mouse-button-down", &val );
                    val.i_int |= 16;
                    var_Set( p_vout, "mouse-button-down", val );
1276
1277
1278
1279
1280
1281
1282
1283
                    break;
            }
        }
        /* Mouse release */
        else if( xevent.type == ButtonRelease )
        {
            switch( ((XButtonEvent *)&xevent)->button )
            {
1284
                case Button1:
1285
1286
1287
1288
1289
                    {
                        var_Get( p_vout, "mouse-button-down", &val );
                        val.i_int &= ~1;
                        var_Set( p_vout, "mouse-button-down", val );

1290
                        var_SetBool( p_vout, "mouse-clicked", true );
ivoire's avatar
ivoire committed
1291
                        var_SetBool( p_vout->p_libvlc, "intf-popupmenu", false );
1292
                    }
1293
                    break;
1294

1295
                case Button2:
1296
1297
1298
1299
1300
                    {
                        var_Get( p_vout, "mouse-button-down", &val );
                        val.i_int &= ~2;
                        var_Set( p_vout, "mouse-button-down", val );

1301
1302
1303
                        var_Get( p_vout->p_libvlc, "intf-show", &val );
                        val.b_bool = !val.b_bool;
                        var_Set( p_vout->p_libvlc, "intf-show", val );
1304
                    }
1305
                    break;
1306

1307
1308
                case Button3:
                    {
1309
1310
1311
                        var_Get( p_vout, "mouse-button-down", &val );
                        val.i_int &= ~4;
                        var_Set( p_vout, "mouse-button-down", val );
gbazin's avatar
   
gbazin committed
1312

ivoire's avatar
ivoire committed
1313
                        var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
1314
1315
                    }
                    break;
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327

                case Button4:
                    var_Get( p_vout, "mouse-button-down", &val );
                    val.i_int &= ~8;
                    var_Set( p_vout, "mouse-button-down", val );
                    break;

                case Button5:
                    var_Get( p_vout, "mouse-button-down", &val );
                    val.i_int &= ~16;
                    var_Set( p_vout, "mouse-button-down", val );
                    break;
1328

1329
1330
1331
1332
1333
            }
        }
        /* Mouse move */
        else if( xevent.type == MotionNotify )
        {
1334
            unsigned int i_width, i_height, i_x, i_y;
1335
1336
1337
1338
1339
1340
1341
1342

            /* somewhat different use for vout_PlacePicture:
             * here the values are needed to give to mouse coordinates
             * in the original picture space */
            vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
                               p_vout->p_sys->p_win->i_height,
                               &i_x, &i_y, &i_width, &i_height );

1343
1344
            /* Compute the x coordinate and check if the value is
               in [0,p_vout->fmt_in.i_visible_width] */
1345
1346
1347
            val.i_int = ( xevent.xmotion.x - i_x ) *
                p_vout->fmt_in.i_visible_width / i_width +
                p_vout->fmt_in.i_x_offset;
1348
1349
1350
1351
1352
1353

            if( (int)(xevent.xmotion.x - i_x) < 0 )
                val.i_int = 0;
            else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
                val.i_int = p_vout->fmt_in.i_visible_width;

1354
            var_Set( p_vout, "mouse-x", val );
1355
1356
1357

            /* compute the y coordinate and check if the value is
               in [0,p_vout->fmt_in.i_visible_height] */
1358
1359
1360
            val.i_int = ( xevent.xmotion.y - i_y ) *
                p_vout->fmt_in.i_visible_height / i_height +
                p_vout->fmt_in.i_y_offset;
1361
1362
1363
1364
1365
1366

            if( (int)(xevent.xmotion.y - i_y) < 0 )
                val.i_int = 0;
            else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
                val.i_int = p_vout->fmt_in.i_visible_height;

1367
1368
            var_Set( p_vout, "mouse-y", val );

ivoire's avatar
ivoire committed
1369
            var_SetBool( p_vout, "mouse-moved", true );
1370

1371
1372
1373
            p_vout->p_sys->i_time_mouse_last_moved = mdate();
            if( ! p_vout->p_sys->b_mouse_pointer_visible )
            {
1374
                ToggleCursor( p_vout );
1375
1376
            }
        }
1377
1378
1379
        else if( xevent.type == ReparentNotify /* XXX: why do we get this? */