window.c 22.3 KB
Newer Older
1 2 3 4 5 6 7
/**
 * @file window.c
 * @brief X C Bindings window provider module for VLC media player
 */
/*****************************************************************************
 * Copyright © 2009 Rémi Denis-Courmont
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
8 9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
12
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
17
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
22 23 24 25 26 27 28

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdarg.h>
#include <assert.h>
29
#include <poll.h>
30 31
#include <unistd.h> /* gethostname() and sysconf() */
#include <limits.h> /* _POSIX_HOST_NAME_MAX */
32 33

#include <xcb/xcb.h>
34 35
typedef xcb_atom_t Atom;
#include <X11/Xatom.h> /* XA_WM_NAME */
36 37 38

#include <vlc_common.h>
#include <vlc_plugin.h>
39
#include <vlc_vout_window.h>
40

41
#include "events.h"
42 43 44 45

struct vout_window_sys_t
{
    xcb_connection_t *conn;
46 47 48
#ifdef HAVE_XCB_KEYSYMS
    xcb_key_symbols_t *keys;
#endif
49
    vlc_thread_t thread;
50 51 52 53

    xcb_window_t root;
    xcb_atom_t wm_state;
    xcb_atom_t wm_state_above;
54
    xcb_atom_t wm_state_below;
55
    xcb_atom_t wm_state_fullscreen;
56 57

    bool embedded;
58
};
59

60 61 62 63 64 65 66 67 68 69 70
static xcb_cursor_t CursorCreate(xcb_connection_t *conn, xcb_window_t root)
{
    xcb_cursor_t cur = xcb_generate_id(conn);
    xcb_pixmap_t pix = xcb_generate_id(conn);

    xcb_create_pixmap(conn, 1, pix, root, 1, 1);
    xcb_create_cursor(conn, cur, pix, pix, 0, 0, 0, 0, 0, 0, 1, 1);
    return cur;
}

static int ProcessEvent(vout_window_t *wnd, xcb_generic_event_t *ev)
71 72
{
    vout_window_sys_t *sys = wnd->sys;
73
    int ret = 0;
74

75
#ifdef HAVE_XCB_KEYSYMS
76
    if (sys->keys != NULL && XCB_keyHandler_Process(sys->keys, ev, wnd) == 0)
77
        return 0;
78
#endif
79 80 81

    switch (ev->response_type & 0x7f)
    {
82 83 84 85
        /* Note a direct mapping of buttons from XCB to VLC is assumed. */
        case XCB_BUTTON_PRESS:
        {
            xcb_button_release_event_t *bpe = (void *)ev;
86

87
            vout_window_ReportMousePressed(wnd, bpe->detail - 1);
88
            ret = 1;
89 90 91 92 93 94
            break;
        }

        case XCB_BUTTON_RELEASE:
        {
            xcb_button_release_event_t *bre = (void *)ev;
95

96
            vout_window_ReportMouseReleased(wnd, bre->detail - 1);
97
            ret = 1;
98 99 100 101 102 103
            break;
        }

        case XCB_MOTION_NOTIFY:
        {
            xcb_motion_notify_event_t *mne = (void *)ev;
104

105
            vout_window_ReportMouseMoved(wnd, mne->event_x, mne->event_y);
106
            ret = 1;
107 108 109
            break;
        }

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        case XCB_CONFIGURE_NOTIFY:
        {
            xcb_configure_notify_event_t *cne = (void *)ev;
            vout_window_ReportSize (wnd, cne->width, cne->height);
            break;
        }
        case XCB_DESTROY_NOTIFY:
            vout_window_ReportClose (wnd);
            break;

        case XCB_MAPPING_NOTIFY:
            break;

        default:
            msg_Dbg (wnd, "unhandled event %"PRIu8, ev->response_type);
    }

    free (ev);
128
    return ret;
129 130 131 132 133 134 135 136
}

/** Background thread for X11 events handling */
static void *Thread (void *data)
{
    vout_window_t *wnd = data;
    vout_window_sys_t *p_sys = wnd->sys;
    xcb_connection_t *conn = p_sys->conn;
137 138 139 140 141 142 143 144
    struct pollfd ufd = {
        .fd = xcb_get_file_descriptor(conn),
        .events = POLLIN,
    };
    xcb_cursor_t cursor = CursorCreate(conn, p_sys->root); /* blank cursor */
    mtime_t lifetime = var_InheritInteger(wnd, "mouse-hide-timeout")
                       * (CLOCK_FREQ / 1000);
    mtime_t deadline = INT64_MAX;
145

146
    if (ufd.fd == -1)
147 148 149 150
        return NULL;

    for (;;)
    {
151
        int timeout = -1;
152

153 154 155 156 157 158 159
        if (deadline != INT64_MAX)
        {
            mtime_t delay = deadline - mdate();
            timeout = (delay > 0) ? delay / (CLOCK_FREQ / 1000) : 0;
        }

        int val = poll(&ufd, 1, timeout);
160 161

        int canc = vlc_savecancel ();
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

        if (val == 0)
        {   /* timeout: hide cursor */
            xcb_change_window_attributes(conn, wnd->handle.xid,
                                         XCB_CW_CURSOR, &cursor);
            xcb_flush(conn);
            deadline = INT64_MAX;
        }
        else
        {
            xcb_generic_event_t *ev;
            bool show_cursor = false;

            while ((ev = xcb_poll_for_event (conn)) != NULL)
                show_cursor = ProcessEvent(wnd, ev) || show_cursor;

            if (show_cursor)
            {
                xcb_change_window_attributes(conn, wnd->handle.xid,
                                             XCB_CW_CURSOR,
                                             &(uint32_t){ XCB_CURSOR_NONE });
                xcb_flush(conn);
                deadline = mdate() + lifetime;
            }
        }

188 189 190 191 192 193 194 195 196 197 198
        vlc_restorecancel (canc);

        if (xcb_connection_has_error (conn))
        {
            msg_Err (wnd, "X server failure");
            break;
        }
    }
    return NULL;
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212
/** Sets the EWMH state of the (unmapped) window */
static void set_wm_state (vout_window_t *wnd, const vout_window_cfg_t *cfg)
{
    vout_window_sys_t *sys = wnd->sys;
    xcb_atom_t data[1];
    uint32_t len = 0;

    if (cfg->is_fullscreen)
        data[len++] = sys->wm_state_fullscreen;

    xcb_change_property (sys->conn, XCB_PROP_MODE_REPLACE, wnd->handle.xid,
                         sys->wm_state, XA_ATOM, 32, len, data);
}

213 214 215 216
#define NET_WM_STATE_REMOVE 0
#define NET_WM_STATE_ADD    1
#define NET_WM_STATE_TOGGLE 2

217 218
/** Changes the EWMH state of the (mapped) window */
static void change_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
{
    vout_window_sys_t *sys = wnd->sys;
    /* From EWMH "_WM_STATE" */
    xcb_client_message_event_t ev = {
         .response_type = XCB_CLIENT_MESSAGE,
         .format = 32,
         .window = wnd->handle.xid,
         .type = sys->wm_state,
    };

    ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
    ev.data.data32[1] = state;
    ev.data.data32[2] = 0;
    ev.data.data32[3] = 1;

    /* From ICCCM "Changing Window State" */
    xcb_send_event (sys->conn, 0, sys->root,
                    XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
                    XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
                    (const char *)&ev);
}

static int Control (vout_window_t *wnd, int cmd, va_list ap)
{
    vout_window_sys_t *p_sys = wnd->sys;
    xcb_connection_t *conn = p_sys->conn;

    switch (cmd)
    {
        case VOUT_WINDOW_SET_SIZE:
        {
            if (p_sys->embedded)
                return VLC_EGENERIC;

            unsigned width = va_arg (ap, unsigned);
            unsigned height = va_arg (ap, unsigned);
            const uint32_t values[] = { width, height, };

            xcb_configure_window (conn, wnd->handle.xid,
                                  XCB_CONFIG_WINDOW_WIDTH |
                                  XCB_CONFIG_WINDOW_HEIGHT, values);
            break;
        }

        case VOUT_WINDOW_SET_STATE:
        {
            unsigned state = va_arg (ap, unsigned);
            bool above = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
            bool below = (state & VOUT_WINDOW_STATE_BELOW) != 0;

269 270
            change_wm_state (wnd, above, p_sys->wm_state_above);
            change_wm_state (wnd, below, p_sys->wm_state_below);
271 272 273 274 275 276
            break;
        }

        case VOUT_WINDOW_SET_FULLSCREEN:
        {
            bool fs = va_arg (ap, int);
277
            change_wm_state (wnd, fs, p_sys->wm_state_fullscreen);
278 279 280 281 282 283 284 285 286 287 288
            break;
        }

        default:
            msg_Err (wnd, "request %d not implemented", cmd);
            return VLC_EGENERIC;
    }
    xcb_flush (p_sys->conn);
    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
289
/** Set an X window property from a nul-terminated string */
290 291 292 293 294 295 296 297
static inline
void set_string (xcb_connection_t *conn, xcb_window_t window,
                 xcb_atom_t type, xcb_atom_t atom, const char *str)
{
    xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom, type,
                         /* format */ 8, strlen (str), str);
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
298
/** Set an X window string property */
299 300 301 302
static inline
void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
                     xcb_atom_t atom, const char *value)
{
303
    set_string (conn, window, XA_STRING, atom, value);
304 305
}

306 307 308 309 310 311 312 313 314 315 316 317 318
static inline
void set_wm_hints (xcb_connection_t *conn, xcb_window_t window)
{
    static const uint32_t wm_hints[8] = {
        3, /* flags: Input, Initial state */
        1, /* input: True */
        1, /* initial state: Normal */
        0, 0, 0, 0, 0, /* Icon */
    };
    xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_HINTS,
                         XA_WM_HINTS, 32, 8, wm_hints);
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
319
/** Set the Window ICCCM client machine property */
320 321 322
static inline
void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
{
323 324 325 326 327
    char* hostname;
    long host_name_max = sysconf (_SC_HOST_NAME_MAX);
    if (host_name_max <= 0) host_name_max = _POSIX_HOST_NAME_MAX;
    hostname = malloc (host_name_max);
    if(!hostname) return;
328

329
    if (gethostname (hostname, host_name_max) == 0)
330
    {
331
        hostname[host_name_max - 1] = '\0';
332 333
        set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
    }
334
    free(hostname);
335 336
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
337
/** Request the X11 server to internalize a string into an atom */
338 339 340 341 342 343
static inline
xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
{
    return xcb_intern_atom (c, 0, strlen (s), s);
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
344
/** Extract the X11 atom from an intern request cookie */
345 346 347 348 349 350 351 352 353 354 355 356 357 358
static
xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
{
    xcb_intern_atom_reply_t *reply;
    xcb_atom_t atom;

    reply = xcb_intern_atom_reply (conn, ck, NULL);
    if (reply == NULL)
        return 0;

    atom = reply->atom;
    free (reply);
    return atom;
}
359

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
360 361 362
static void CacheAtoms (vout_window_sys_t *p_sys)
{
    xcb_connection_t *conn = p_sys->conn;
363
    xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck,
364
                             wm_state_below_ck, wm_state_fs_ck;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
365 366 367

    wm_state_ck = intern_string (conn, "_NET_WM_STATE");
    wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
Rémi Denis-Courmont's avatar
Typo  
Rémi Denis-Courmont committed
368
    wm_state_below_ck = intern_string (conn, "_NET_WM_STATE_BELOW");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
369 370 371 372
    wm_state_fs_ck = intern_string (conn, "_NET_WM_STATE_FULLSCREEN");

    p_sys->wm_state = get_atom (conn, wm_state_ck);
    p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
373
    p_sys->wm_state_below = get_atom (conn, wm_state_below_ck);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
374 375 376
    p_sys->wm_state_fullscreen = get_atom (conn, wm_state_fs_ck);
}

377 378 379
/**
 * Create an X11 window.
 */
380
static int Open (vout_window_t *wnd, const vout_window_cfg_t *cfg)
381
{
382 383 384 385
    if (cfg->type != VOUT_WINDOW_TYPE_INVALID
     && cfg->type != VOUT_WINDOW_TYPE_XID)
        return VLC_EGENERIC;

386 387 388
    xcb_generic_error_t *err;
    xcb_void_cookie_t ck;

389
    vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
390 391
    if (p_sys == NULL)
        return VLC_ENOMEM;
392
    p_sys->embedded = false;
393

394
    /* Connect to X */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
395
    char *display = var_InheritString (wnd, "x11-display");
396 397 398 399 400 401
    int snum;

    xcb_connection_t *conn = xcb_connect (display, &snum);
    if (xcb_connection_has_error (conn) /*== NULL*/)
        goto error;

402 403
    /* Find configured screen */
    const xcb_setup_t *setup = xcb_get_setup (conn);
404
    const xcb_screen_t *scr = NULL;
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
         i.rem > 0; xcb_screen_next (&i))
    {
        if (snum == 0)
        {
            scr = i.data;
            break;
        }
        snum--;
    }
    if (scr == NULL)
    {
        msg_Err (wnd, "bad X11 screen number");
        goto error;
    }

421
    /* Create window */
422 423
    const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
    uint32_t values[2] = {
424 425
        /* XCB_CW_BACK_PIXEL */
        scr->black_pixel,
426
        /* XCB_CW_EVENT_MASK */
427 428
        XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_POINTER_MOTION |
        XCB_EVENT_MASK_STRUCTURE_NOTIFY,
429
    };
430

431 432 433 434
    if (var_InheritBool(wnd, "mouse-events"))
        values[1] |= XCB_EVENT_MASK_BUTTON_PRESS
                   | XCB_EVENT_MASK_BUTTON_RELEASE;

435 436
    xcb_window_t window = xcb_generate_id (conn);
    ck = xcb_create_window_checked (conn, scr->root_depth, window, scr->root,
437
                                    0, 0, cfg->width, cfg->height, 0,
438
                                    XCB_WINDOW_CLASS_INPUT_OUTPUT,
439
                                    scr->root_visual, mask, values);
440 441 442 443
    err = xcb_request_check (conn, ck);
    if (err)
    {
        msg_Err (wnd, "creating window: X11 error %d", err->error_code);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
444
        free (err);
445 446 447
        goto error;
    }

448
    wnd->type = VOUT_WINDOW_TYPE_XID;
449
    wnd->handle.xid = window;
450
    wnd->display.x11 = display;
451
    wnd->control = Control;
452
    wnd->sys = p_sys;
453 454

    p_sys->conn = conn;
455
#ifdef HAVE_XCB_KEYSYMS
456
    if (var_InheritBool (wnd, "keyboard-events"))
457
        p_sys->keys = xcb_key_symbols_alloc(conn);
458 459
    else
        p_sys->keys = NULL;
460
#endif
461 462
    p_sys->root = scr->root;

463 464
    /* ICCCM
     * No cut&paste nor drag&drop, only Window Manager communication. */
465 466 467 468
    set_ascii_prop (conn, window, XA_WM_NAME,
    /* xgettext: This is a plain ASCII spelling of "VLC media player"
       for the ICCCM window name. This must be pure ASCII.
       The limitation is partially with ICCCM and partially with VLC.
469 470
       For Latin script languages, you may need to strip accents.
       For other scripts, you will need to transliterate into Latin. */
471 472
                    vlc_pgettext ("ASCII", "VLC media player"));

473
    set_ascii_prop (conn, window, XA_WM_ICON_NAME,
474 475
    /* xgettext: This is a plain ASCII spelling of "VLC"
       for the ICCCM window name. This must be pure ASCII. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
476
                    vlc_pgettext ("ASCII", "VLC"));
477
    set_wm_hints (conn, window);
478
    xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_CLASS,
479
                         XA_STRING, 8, 8, "vlc\0Vlc");
480
    set_hostname_prop (conn, window);
481

482 483 484 485 486 487 488
    /* EWMH */
    xcb_intern_atom_cookie_t utf8_string_ck
        = intern_string (conn, "UTF8_STRING");;
    xcb_intern_atom_cookie_t net_wm_name_ck
        = intern_string (conn, "_NET_WM_NAME");
    xcb_intern_atom_cookie_t net_wm_icon_name_ck
        = intern_string (conn, "_NET_WM_ICON_NAME");
Nick Pope's avatar
Nick Pope committed
489 490
    xcb_intern_atom_cookie_t wm_window_role_ck
        = intern_string (conn, "WM_WINDOW_ROLE");
491

492 493 494
    xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);

    xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
495
    char *title = var_InheritString (wnd, "video-title");
496 497 498 499 500 501 502 503 504 505
    if (title)
    {
        set_string (conn, window, utf8, net_wm_name, title);
        free (title);
    }
    else
        set_string (conn, window, utf8, net_wm_name, _("VLC media player"));

    xcb_atom_t net_wm_icon_name = get_atom (conn, net_wm_icon_name_ck);
    set_string (conn, window, utf8, net_wm_icon_name, _("VLC"));
506

Nick Pope's avatar
Nick Pope committed
507 508 509
    xcb_atom_t wm_window_role = get_atom (conn, wm_window_role_ck);
    set_ascii_prop (conn, window, wm_window_role, "vlc-video");

510 511
    /* Cache any EWMH atom we may need later */
    CacheAtoms (p_sys);
512

513 514 515
    /* Set initial window state */
    set_wm_state (wnd, cfg);

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
516 517
    /* Make the window visible */
    xcb_map_window (conn, window);
518

519 520 521 522 523 524 525 526 527
    /* Get the initial mapped size (may differ from requested size) */
    xcb_get_geometry_reply_t *geo =
        xcb_get_geometry_reply (conn, xcb_get_geometry (conn, window), NULL);
    if (geo != NULL)
    {
        vout_window_ReportSize(wnd, geo->width, geo->height);
        free (geo);
    }

528 529
    /* Create the event thread. It will dequeue all events, so any checked
     * request from this thread must be completed at this point. */
530
    if (vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
531
    {
532
#ifdef HAVE_XCB_KEYSYMS
533
        if (p_sys->keys != NULL)
534 535
            xcb_key_symbols_free(p_sys->keys);
#endif
536
        goto error;
537
    }
538

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
539
    xcb_flush (conn); /* Make sure map_window is sent (should be useless) */
540 541 542 543
    return VLC_SUCCESS;

error:
    xcb_disconnect (conn);
544
    free (display);
545
    free (p_sys);
546 547 548 549 550 551 552
    return VLC_EGENERIC;
}


/**
 * Destroys the X11 window.
 */
553
static void Close (vout_window_t *wnd)
554
{
555
    vout_window_sys_t *p_sys = wnd->sys;
556
    xcb_connection_t *conn = p_sys->conn;
557

558 559
    vlc_cancel (p_sys->thread);
    vlc_join (p_sys->thread, NULL);
560
#ifdef HAVE_XCB_KEYSYMS
561
    if (p_sys->keys != NULL)
562 563
        xcb_key_symbols_free(p_sys->keys);
#endif
564

565
    xcb_disconnect (conn);
566
    free (wnd->display.x11);
567 568 569
    free (p_sys);
}

570 571 572 573 574 575 576 577
/*** Embedded drawable support ***/

static vlc_mutex_t serializer = VLC_STATIC_MUTEX;

/** Acquire a drawable */
static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
{
    xcb_window_t *used;
578
    size_t n = 0;
579

580
    if (var_Create (obj->obj.libvlc, "xid-in-use", VLC_VAR_ADDRESS))
581 582 583 584 585
        return VLC_ENOMEM;

    /* Keep a list of busy drawables, so we don't overlap videos if there are
     * more than one video track in the stream. */
    vlc_mutex_lock (&serializer);
586
    used = var_GetAddress (obj->obj.libvlc, "xid-in-use");
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    if (used != NULL)
    {
        while (used[n])
        {
            if (used[n] == window)
                goto skip;
            n++;
        }
    }

    used = realloc (used, sizeof (*used) * (n + 2));
    if (used != NULL)
    {
        used[n] = window;
        used[n + 1] = 0;
602
        var_SetAddress (obj->obj.libvlc, "xid-in-use", used);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    }
    else
    {
skip:
        msg_Warn (obj, "X11 drawable 0x%08"PRIx8" is busy", window);
        window = 0;
    }
    vlc_mutex_unlock (&serializer);

    return (window == 0) ? VLC_EGENERIC : VLC_SUCCESS;
}

/** Remove this drawable from the list of busy ones */
static void ReleaseDrawable (vlc_object_t *obj, xcb_window_t window)
{
    xcb_window_t *used;
    size_t n = 0;

    vlc_mutex_lock (&serializer);
622
    used = var_GetAddress (obj->obj.libvlc, "xid-in-use");
623 624 625 626 627 628 629 630 631 632
    assert (used);
    while (used[n] != window)
    {
        assert (used[n]);
        n++;
    }
    do
        used[n] = used[n + 1];
    while (used[++n]);

633 634 635 636 637
    if (!used[0])
        var_SetAddress (obj->obj.libvlc, "xid-in-use", NULL);
    else
        used = NULL;

638 639
    vlc_mutex_unlock (&serializer);

640 641
    free( used );

642
    /* Variables are reference-counted... */
643
    var_Destroy (obj->obj.libvlc, "xid-in-use");
644 645 646 647 648
}

/**
 * Wrap an existing X11 window to embed the video.
 */
649
static int EmOpen (vout_window_t *wnd, const vout_window_cfg_t *cfg)
650
{
651 652 653 654
    if (cfg->type != VOUT_WINDOW_TYPE_INVALID
     && cfg->type != VOUT_WINDOW_TYPE_XID)
        return VLC_EGENERIC;

655
    xcb_window_t window = var_InheritInteger (wnd, "drawable-xid");
656 657 658
    if (window == 0)
        return VLC_EGENERIC;

659
    if (AcquireDrawable (VLC_OBJECT(wnd), window))
660 661 662 663 664 665 666
        return VLC_EGENERIC;

    vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
    xcb_connection_t *conn = xcb_connect (NULL, NULL);
    if (p_sys == NULL || xcb_connection_has_error (conn))
        goto error;

667
    p_sys->embedded = true;
668
    wnd->type = VOUT_WINDOW_TYPE_XID;
669
    wnd->display.x11 = NULL;
670
    wnd->handle.xid = window;
671 672 673 674 675
    wnd->control = Control;
    wnd->sys = p_sys;

    p_sys->conn = conn;

676 677
    /* Subscribe to window events (_before_ the geometry is queried) */
    uint32_t mask = XCB_CW_EVENT_MASK;
678 679 680
    const uint32_t ovalue = XCB_EVENT_MASK_POINTER_MOTION
                          | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
    uint32_t value = ovalue;
681 682 683

    xcb_change_window_attributes (conn, window, mask, &value);

684 685 686 687
    xcb_get_geometry_reply_t *geo =
        xcb_get_geometry_reply (conn, xcb_get_geometry (conn, window), NULL);
    if (geo == NULL)
    {
688
        msg_Err (wnd, "bad X11 window 0x%08"PRIx8, window);
689 690 691
        goto error;
    }
    p_sys->root = geo->root;
692
    vout_window_ReportSize(wnd, geo->width, geo->height);
693 694
    free (geo);

695
#ifdef HAVE_XCB_KEYSYMS
696
    /* Try to subscribe to keyboard and mouse events (only one X11 client can
697
     * subscribe to input events, so this can fail). */
698
    if (var_InheritBool (wnd, "keyboard-events"))
699
    {
700
        p_sys->keys = xcb_key_symbols_alloc(conn);
701
        if (p_sys->keys != NULL)
702
            value |= XCB_EVENT_MASK_KEY_PRESS;
703
    }
704 705
    else
        p_sys->keys = NULL;
706
#endif
707

708 709 710 711
    if (var_InheritBool(wnd, "mouse-events"))
        value |= XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE;

    if (value != ovalue)
712 713
        xcb_change_window_attributes (conn, window, mask, &value);

714
    CacheAtoms (p_sys);
715
    if (vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
716
    {
717
#ifdef HAVE_XCB_KEYSYMS
718
        if (p_sys->keys != NULL)
719 720
            xcb_key_symbols_free(p_sys->keys);
#endif
721
        goto error;
722
    }
723 724

    xcb_flush (conn);
725
    (void) cfg;
726 727 728 729 730
    return VLC_SUCCESS;

error:
    xcb_disconnect (conn);
    free (p_sys);
731
    ReleaseDrawable (VLC_OBJECT(wnd), window);
732 733 734
    return VLC_EGENERIC;
}

735
static void EmClose (vout_window_t *wnd)
736
{
737
    xcb_window_t window = wnd->handle.xid;
738

739 740
    Close (wnd);
    ReleaseDrawable (VLC_OBJECT(wnd), window);
741
}
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

#define DISPLAY_TEXT N_("X11 display")
#define DISPLAY_LONGTEXT N_( \
    "Video will be rendered with this X11 display. " \
    "If empty, the default display will be used.")

#define XID_TEXT N_("X11 window ID")
#define XID_LONGTEXT N_( \
    "Video will be embedded in this pre-existing window. " \
    "If zero, a new window will be created.")

/*
 * Module descriptor
 */
vlc_module_begin ()
    set_shortname (N_("X window"))
    set_description (N_("X11 video window (XCB)"))
    set_category (CAT_VIDEO)
    set_subcategory (SUBCAT_VIDEO_VOUT)
    set_capability ("vout window", 10)
    set_callbacks (Open, Close)

    /* Obsolete since 1.1.0: */
    add_obsolete_bool ("x11-altfullscreen")
    add_obsolete_bool ("xvideo-altfullscreen")
    add_obsolete_bool ("xvmc-altfullscreen")
    add_obsolete_bool ("glx-altfullscreen")

    add_submodule ()
    set_shortname (N_("Drawable"))
    set_description (N_("Embedded window video"))
    set_category (CAT_VIDEO)
    set_subcategory (SUBCAT_VIDEO_VOUT)
    set_capability ("vout window", 70)
    set_callbacks (EmOpen, EmClose)
    add_shortcut ("embed-xid")

    add_string ("x11-display", NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
    add_integer ("drawable-xid", 0, XID_TEXT, XID_LONGTEXT, true)
        change_volatile ()
vlc_module_end ()