xcb_apps.c 9.73 KB
Newer Older
1 2 3 4 5 6 7
/**
 * @file xcb_apps.c
 * @brief List of application windows XCB module for VLC media player
 */
/*****************************************************************************
 * Copyright © 2009 Rémi Denis-Courmont
 *
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
 *
13
 * This program is distributed in the hope that it will be useful,
14 15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
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 29 30 31 32

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdarg.h>
#include <xcb/xcb.h>
typedef xcb_atom_t Atom;
#include <X11/Xatom.h> /* XA_WINDOW */
#include <vlc_common.h>
#include <vlc_services_discovery.h>
#include <vlc_dialog.h>
33
#include <vlc_charset.h>
34
#include <vlc_plugin.h>
35 36 37
#ifdef HAVE_SEARCH_H
# include <search.h>
#endif
38 39 40 41
#include <poll.h>

static int  Open (vlc_object_t *);
static void Close (vlc_object_t *);
42
static int vlc_sd_probe_Open (vlc_object_t *);
43 44 45 46 47 48 49 50 51 52 53 54

/*
 * Module descriptor
 */
vlc_module_begin ()
    set_shortname (N_("Screen capture"))
    set_description (N_("Screen capture"))
    set_category (CAT_PLAYLIST)
    set_subcategory (SUBCAT_PLAYLIST_SD)
    set_capability ("services_discovery", 0)
    set_callbacks (Open, Close)

55
    add_shortcut ("apps", "screen")
56 57

    VLC_SD_PROBE_SUBMODULE
58 59 60 61 62 63 64
vlc_module_end ()

struct services_discovery_sys_t
{
    xcb_connection_t *conn;
    vlc_thread_t      thread;
    xcb_atom_t        net_client_list;
65
    xcb_atom_t        net_wm_name;
66
    xcb_window_t      root_window;
67
    void             *apps;
68 69 70
};

static void *Run (void *);
71 72 73
static void UpdateApps (services_discovery_t *);
static void DelApp (void *);
static void AddDesktop(services_discovery_t *);
74

75 76 77 78
static int vlc_sd_probe_Open (vlc_object_t *obj)
{
    vlc_probe_t *probe = (vlc_probe_t *)obj;

79
    char *display = var_InheritString (obj, "x11-display");
80 81 82
    xcb_connection_t *conn = xcb_connect (display, NULL);
    free (display);
    if (xcb_connection_has_error (conn))
83
        return VLC_PROBE_CONTINUE;
84
    xcb_disconnect (conn);
85
    return vlc_sd_probe_Add (probe, "xcb_apps{longname=\"Screen capture\"}",
86
                             N_("Screen capture"), SD_CAT_DEVICES);
87 88
}

89 90 91 92 93 94 95 96 97 98 99 100 101
/**
 * Probes and initializes.
 */
static int Open (vlc_object_t *obj)
{
    services_discovery_t *sd = (services_discovery_t *)obj;
    services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));

    if (p_sys == NULL)
        return VLC_ENOMEM;
    sd->p_sys = p_sys;

    /* Connect to X server */
102
    char *display = var_InheritString (obj, "x11-display");
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    int snum;
    xcb_connection_t *conn = xcb_connect (display, &snum);
    free (display);
    if (xcb_connection_has_error (conn))
    {
        free (p_sys);
        return VLC_EGENERIC;
    }
    p_sys->conn = conn;

    /* Find configured screen */
    const xcb_setup_t *setup = xcb_get_setup (conn);
    const xcb_screen_t *scr = NULL;
    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 (obj, "bad X11 screen number");
        goto error;
    }

132
    /* Add a permanent item for the entire desktop */
133
    AddDesktop (sd);
134

135 136 137 138 139 140
    p_sys->root_window = scr->root;
    xcb_change_window_attributes (conn, scr->root, XCB_CW_EVENT_MASK,
                               &(uint32_t) { XCB_EVENT_MASK_PROPERTY_CHANGE });

    /* TODO: check that _NET_CLIENT_LIST is in _NET_SUPPORTED
     * (and _NET_SUPPORTING_WM_CHECK) */
141 142 143 144
    xcb_intern_atom_reply_t *r;
    xcb_intern_atom_cookie_t ncl, nwn;

    ncl = xcb_intern_atom (conn, 1, strlen ("_NET_CLIENT_LIST"),
145
                          "_NET_CLIENT_LIST");
146 147 148
    nwn = xcb_intern_atom (conn, 0, strlen ("_NET_WM_NAME"), "_NET_WM_NAME");

    r = xcb_intern_atom_reply (conn, ncl, NULL);
149 150
    if (r == NULL || r->atom == 0)
    {
151 152 153
        dialog_Fatal (sd, _("Screen capture"),
            _("Your window manager does not provide a list of applications."));
        msg_Err (sd, "client list not supported (_NET_CLIENT_LIST absent)");
154
    }
155
    p_sys->net_client_list = r ? r->atom : 0;
156
    free (r);
157 158 159 160 161 162
    r = xcb_intern_atom_reply (conn, nwn, NULL);
    if (r != NULL)
    {
        p_sys->net_wm_name = r->atom;
        free (r);
    }
163

164 165
    p_sys->apps = NULL;
    UpdateApps (sd);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
        goto error;
    return VLC_SUCCESS;

error:
    xcb_disconnect (p_sys->conn);
    free (p_sys);
    return VLC_EGENERIC;
}


/**
 * Releases resources
 */
static void Close (vlc_object_t *obj)
{
    services_discovery_t *sd = (services_discovery_t *)obj;
    services_discovery_sys_t *p_sys = sd->p_sys;

    vlc_cancel (p_sys->thread);
    vlc_join (p_sys->thread, NULL);
    xcb_disconnect (p_sys->conn);
189
    tdestroy (p_sys->apps, DelApp);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    free (p_sys);
}

static void *Run (void *data)
{
    services_discovery_t *sd = data;
    services_discovery_sys_t *p_sys = sd->p_sys;
    xcb_connection_t *conn = p_sys->conn;
    int fd = xcb_get_file_descriptor (conn);
    if (fd == -1)
        return NULL;

    while (!xcb_connection_has_error (conn))
    {
        xcb_generic_event_t *ev;
        struct pollfd ufd = { .fd = fd, .events = POLLIN, };

        poll (&ufd, 1, -1);

        int canc = vlc_savecancel ();
        while ((ev = xcb_poll_for_event (conn)) != NULL)
        {
            if ((ev->response_type & 0x7F) == XCB_PROPERTY_NOTIFY)
            {
                 const xcb_property_notify_event_t *pn =
                     (xcb_property_notify_event_t *)ev;
                 if (pn->atom == p_sys->net_client_list)
217
                     UpdateApps (sd);
218 219 220 221 222 223 224 225
            }
            free (ev);
        }
        vlc_restorecancel (canc);
    }
    return NULL;
}

226
/*** Application windows ***/
227 228 229 230 231 232 233
struct app
{
    xcb_window_t          xid; /* must be first for cmpapp */
    input_item_t         *item;
    services_discovery_t *owner;
};

234
static struct app *AddApp (services_discovery_t *sd, xcb_window_t xid)
235
{
236 237 238
    services_discovery_sys_t *p_sys = sd->p_sys;
    char *mrl, *name;

239 240 241
    if (asprintf (&mrl, "window://0x%"PRIx8, xid) == -1)
        return NULL;

242 243 244 245 246 247 248 249 250 251 252 253 254
    xcb_get_property_reply_t *r =
        xcb_get_property_reply (p_sys->conn,
            xcb_get_property (p_sys->conn, 0, xid, p_sys->net_wm_name, 0,
                              0, 1023 /* max size */), NULL);
    if (r != NULL)
    {
        name = strndup (xcb_get_property_value (r),
                        xcb_get_property_value_length (r));
        if (name != NULL)
            EnsureUTF8 (name); /* don't trust third party apps too much ;-) */
        free (r);
    }
    /* TODO: use WM_NAME (Latin-1) for very old apps */
255 256
    else
        name = NULL;
257

258
    input_item_t *item = input_item_NewWithType (mrl, name ? name : mrl,
259 260 261
                                                 0, NULL, 0, -1,
                                                 ITEM_TYPE_CARD /* FIXME */);
    free (mrl);
262
    free (name);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    if (item == NULL)
        return NULL;

    struct app *app = malloc (sizeof (*app));
    if (app == NULL)
    {
        vlc_gc_decref (item);
        return NULL;
    }
    app->xid = xid;
    app->item = item;
    app->owner = sd;
    services_discovery_AddItem (sd, item, _("Applications"));
    return app;
}

279
static void DelApp (void *data)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
{
    struct app *app = data;

    services_discovery_RemoveItem (app->owner, app->item);
    vlc_gc_decref (app->item);
    free (app);
}

static int cmpapp (const void *a, const void *b)
{
    xcb_window_t wa = *(xcb_window_t *)a;
    xcb_window_t wb = *(xcb_window_t *)b;

    if (wa > wb)
        return 1;
    if (wa < wb)
        return -1;
    return 0;
} 

300
static void UpdateApps (services_discovery_t *sd)
301 302 303 304 305 306 307 308 309 310 311 312 313 314
{
    services_discovery_sys_t *p_sys = sd->p_sys;
    xcb_connection_t *conn = p_sys->conn;

    xcb_get_property_reply_t *r =
        xcb_get_property_reply (conn,
            xcb_get_property (conn, false, p_sys->root_window,
                              p_sys->net_client_list, XA_WINDOW, 0, 1024),
            NULL);
    if (r == NULL)
        return; /* FIXME: remove all entries */

    xcb_window_t *ent = xcb_get_property_value (r);
    int n = xcb_get_property_value_length (r) / 4;
315
    void *newnodes = NULL, *oldnodes = p_sys->apps;
316 317 318 319 320 321 322 323 324 325 326 327 328 329

    for (int i = 0; i < n; i++)
    {
        xcb_window_t id = *(ent++);
        struct app *app;

        struct app **pa = tfind (&id, &oldnodes, cmpapp);
        if (pa != NULL) /* existing entry */
        {
            app = *pa;
            tdelete (app, &oldnodes, cmpapp);
        }
        else /* new entry */
        {
330
            app = AddApp (sd, id);
331 332 333 334 335 336
            if (app == NULL)
                continue;
        }

        pa = tsearch (app, &newnodes, cmpapp);
        if (pa == NULL /* OOM */ || *pa != app /* buggy window manager */)
337
            DelApp (app);
338 339 340 341
    }
    free (r);

    /* Remove old nodes */
342 343
    tdestroy (oldnodes, DelApp);
    p_sys->apps = newnodes;
344
}
345

346 347
/*** Whole desktop ***/
static void AddDesktop(services_discovery_t *sd)
348 349 350
{
    input_item_t *item;

351
    item = input_item_NewWithType ("screen://", _("Desktop"),
352 353 354 355 356 357
                                   0, NULL, 0, -1, ITEM_TYPE_CARD);
    if (item == NULL)
        return;

    services_discovery_AddItem (sd, item, NULL);
}