summaryrefslogtreecommitdiff
path: root/src/vdagent/display.c
blob: 9569173f3533daa67c58c2c6d945bb90ebe5f987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
188
189
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
217
218
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*  display.c vdagent display source code

 Copyright 2020 Red Hat, Inc.

 Red Hat Authors:
 Julien Ropé <jrope@redhat.com>

 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <glib.h>
#ifdef WITH_GTK
#include <gdk/gdk.h>
#include <gtk/gtk.h>    // for GTK_CHECK_VERSION
#if GTK_CHECK_VERSION(3, 98, 0)
    #include <gdk/wayland/gdkwayland.h>
    #include <gdk/x11/gdkx.h>
#else
    #ifdef GDK_WINDOWING_X11
         #include <gdk/gdkx.h>
    #endif
#endif
#endif
#include <syslog.h>
#include "x11.h"
#include "x11-priv.h"

#include "device-info.h"
#include "vdagentd-proto.h"

#include "display.h"

/**
 * VDAgentDisplay and the vdagent_display_*() functions are used as wrappers for display-related
 * operations.
 * They allow vdagent code to call generic display functions that are independent from the underlying
 * API (X11/GTK/etc).
 *
 * The display.c file contains the actual implementation and chooses what API will be called.
 * The x11.c and x11-randr.c files contains the x11-specific functions.
 */
struct VDAgentDisplay {
    // association between SPICE display ID and expected connector name
    GHashTable *connector_mapping;
    struct vdagent_x11 *x11;
    UdscsConnection *vdagentd;
    int debug;
    GIOChannel *x11_channel;
};

static gint vdagent_guest_xorg_resolution_compare(gconstpointer a, gconstpointer b)
{
    struct vdagentd_guest_xorg_resolution *ptr_a, *ptr_b;

    ptr_a = (struct vdagentd_guest_xorg_resolution *)a;
    ptr_b = (struct vdagentd_guest_xorg_resolution *)b;

    return ptr_a->display_id - ptr_b->display_id;
}

static GArray *vdagent_gtk_get_resolutions(VDAgentDisplay *display,
                                           int *width, int *height, int *screen_count)
{
#ifdef USE_GTK_FOR_MONITORS
    if (!GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
        return NULL;
    }

    GArray *res_array = g_array_new(FALSE, FALSE, sizeof(struct vdagentd_guest_xorg_resolution));
    int i;
    GListModel *monitors = NULL;

    GdkDisplay *gdk_display = gdk_display_get_default();

    // Make sure GDK is aware of the changes we want to send.
    // TODO: This may be removed if we get a notification of change from GDK itself,
    // but with X11 notification, we end up sending obsolete information.
    gdk_display_sync(gdk_display);

    monitors = gdk_display_get_monitors(gdk_display);
    *screen_count = g_list_model_get_n_items(monitors);

    for (i = 0; i < *screen_count; i++) {
        struct vdagentd_guest_xorg_resolution curr;

        GdkMonitor *monitor = (GdkMonitor*)g_list_model_get_item(monitors, i);
        GdkRectangle geometry;

        gdk_monitor_get_geometry(monitor, &geometry);
        curr.x = geometry.x;
        curr.y = geometry.y;
        curr.height = geometry.height;
        curr.width = geometry.width;

        // compute the size of the desktop based on the dimension of the monitors
        // TODO: check for a specific API giving us that information (not found in GTK ?)
        if (curr.x + curr.width > *width) {
            *width = curr.x + curr.width;
        }
        if (curr.y + curr.height > *height) {
            *height = curr.y + curr.height;
        }

        // retrieve the Spice Display ID based on the connector name
        const char *name = gdk_monitor_get_connector(monitor);
        if (!name) {
            syslog(LOG_WARNING, "Unknown connector for monitor %d", i);
            continue;
        }

        gpointer value;
        if (g_hash_table_lookup_extended(display->connector_mapping, name, NULL, &value)) {
            curr.display_id = GPOINTER_TO_UINT(value);
            syslog(LOG_DEBUG, "Found monitor %s with geometry %dx%d+%d-%d - associating it to SPICE display #%d",
                   name, curr.width, curr.height, curr.x, curr.y, curr.display_id);
            g_array_append_val(res_array, curr);
        } else {
            syslog(LOG_DEBUG, "No SPICE display found for connector %s", name);
        }
    }

    if (res_array->len == 0) {
        syslog(LOG_DEBUG, "No Spice display ID matching - assuming display ID == Monitor index");
        for (i = 0; i < *screen_count; i++) {
            struct vdagentd_guest_xorg_resolution res;
            GdkMonitor *monitor = (GdkMonitor*)g_list_model_get_item(monitors, i);
            GdkRectangle geometry;

            gdk_monitor_get_geometry(monitor, &geometry);
            res.x = geometry.x;
            res.y = geometry.y;
            res.height = geometry.height;
            res.width = geometry.width;
            res.display_id = i;

            g_array_append_val(res_array, res);
        }
    }

    return res_array;
#else
    return NULL;
#endif
}

void vdagent_display_send_daemon_guest_res(VDAgentDisplay *display, gboolean update)
{
    GArray *res_array;
    int width = 0, height = 0, screen_count = 0;

    res_array = vdagent_gtk_get_resolutions(display, &width, &height, &screen_count);
    if (res_array == NULL) {
        if (display->x11->dont_send_guest_xorg_res) {
            return;
        }

        res_array = vdagent_x11_get_resolutions(display->x11, update,
                                                &width, &height, &screen_count);
        if (res_array == NULL) {
            return;
        }
    }

    if (res_array->len < g_hash_table_size(display->connector_mapping)) {
        // Complete the array with disabled displays.
        // We need to send 0x0 resolution to let the daemon know the display is not there anymore.

        syslog(LOG_DEBUG, "%d/%d displays found - completing with disabled displays.",
               res_array->len, g_hash_table_size(display->connector_mapping));

        GHashTableIter iter;
        gpointer key, value;
        g_hash_table_iter_init(&iter, display->connector_mapping);
        while (g_hash_table_iter_next(&iter, &key, &value)) {
            bool found = false;
            int display_id = GPOINTER_TO_INT(value);
            for (int i = 0; i < res_array->len; i++) {
                struct vdagentd_guest_xorg_resolution *res =
                    (struct vdagentd_guest_xorg_resolution*)res_array->data;
                if (res[i].display_id == display_id) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                struct vdagentd_guest_xorg_resolution res;

                res.x = 0;
                res.y = 0;
                res.height = 0;
                res.width = 0;
                res.display_id = display_id;

                g_array_append_val(res_array, res);
            }
        }
    }

    // sort the list to make sure we send them in the display_id order
    g_array_sort(res_array, vdagent_guest_xorg_resolution_compare);

    if (display->debug) {
        syslog(LOG_DEBUG, "Sending guest screen resolutions to vdagentd:");
        if (res_array->len > screen_count) {
            syslog(LOG_DEBUG, "(NOTE: list may contain overlapping areas when "
                              "multiple spice displays show the same guest output)");
        }
        struct vdagentd_guest_xorg_resolution *res =
            (struct vdagentd_guest_xorg_resolution*)res_array->data;
        for (int i = 0; i < res_array->len; i++) {
            syslog(LOG_DEBUG, "   display_id=%d - %dx%d%+d%+d",
                   res[i].display_id, res[i].width, res[i].height, res[i].x, res[i].y);
        }
    }

    udscs_write(display->vdagentd, VDAGENTD_GUEST_XORG_RESOLUTION, width, height,
                (uint8_t *)res_array->data,
                res_array->len * sizeof(struct vdagentd_guest_xorg_resolution));
    g_array_free(res_array, TRUE);
}

static gchar *vdagent_display_get_wm_name(VDAgentDisplay *display)
{
#ifdef GDK_WINDOWING_X11
    // With GTK4, screen have disappear, and with it the access to the window manager name
    // Use the X11 call instead.
#if ! GTK_CHECK_VERSION(3, 98, 0)
    GdkDisplay *gdk_display = gdk_display_get_default();
    if (GDK_IS_X11_DISPLAY(gdk_display))
        return g_strdup(gdk_x11_screen_get_window_manager_name(
            gdk_display_get_default_screen(gdk_display)));
    return g_strdup("unsupported");
#endif
#endif
    return vdagent_x11_get_wm_name(display->x11);
}


struct vdagent_x11* vdagent_display_get_x11(VDAgentDisplay *display)
{
    return display->x11;
}

static gboolean x11_io_channel_cb(GIOChannel *source, GIOCondition condition, gpointer data)
{
    VDAgentDisplay *display = data;
    vdagent_x11_do_read(display->x11);

    return G_SOURCE_CONTINUE;
}

VDAgentDisplay* vdagent_display_create(UdscsConnection *vdagentd, int debug, int sync)
{
    VDAgentDisplay *display;
    gchar *net_wm_name = NULL;

    display = g_new0(VDAgentDisplay, 1);
    display->vdagentd = vdagentd;
    display->debug = debug;

    display->x11 = vdagent_x11_create(vdagentd, debug, sync);
    if (display->x11 == NULL) {
        g_free(display);
        return NULL;
    }

    display->x11->vdagent_display = display;
    display->connector_mapping = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

    display->x11_channel = g_io_channel_unix_new(vdagent_x11_get_fd(display->x11));
    if (display->x11_channel == NULL) {
        vdagent_x11_destroy(display->x11, TRUE);
        g_free(display);
        return NULL;
    }

    g_io_add_watch(display->x11_channel, G_IO_IN, x11_io_channel_cb, display);


    /* Since we are started at the same time as the wm,
       sometimes we need to wait a bit for the _NET_WM_NAME to show up. */
    for (int i = 0; i < 9; i++) {
        g_free(net_wm_name);
        net_wm_name = vdagent_display_get_wm_name(display);
        if (strcmp(net_wm_name, "unknown"))
            break;
        usleep(100000);
    }
    if (display->debug)
        syslog(LOG_DEBUG, "%s: net_wm_name=\"%s\", has icons=%d",
               __func__, net_wm_name, vdagent_display_has_icons_on_desktop(display));
    g_free(net_wm_name);

    vdagent_display_send_daemon_guest_res(display, TRUE);
    return display;
}

void vdagent_display_destroy(VDAgentDisplay *display, int vdagentd_disconnected)
{
    if (!display) {
        return;
    }

    g_hash_table_destroy(display->connector_mapping);

    g_clear_pointer(&display->x11_channel, g_io_channel_unref);
    vdagent_x11_destroy(display->x11, vdagentd_disconnected);
    g_free(display);
}

/* Function used to determine the default location to save file-xfers,
   xdg desktop dir or xdg download dir. We err on the safe side and use a
   whitelist approach, so any unknown desktop will end up with saving
   file-xfers to the xdg download dir, and opening the xdg download dir with
   xdg-open when the file-xfer completes. */
gboolean vdagent_display_has_icons_on_desktop(VDAgentDisplay *display)
{
    static const char * const wms_with_icons_on_desktop[] = {
        "Metacity", /* GNOME-2 or GNOME-3 fallback */
        "Xfwm4",    /* Xfce */
        "Marco",    /* Mate */
        "Metacity (Marco)", /* Mate, newer */
        NULL
    };
    gchar *net_wm_name = vdagent_display_get_wm_name(display);
    int i;

    for (i = 0; wms_with_icons_on_desktop[i]; i++)
        if (!strcmp(net_wm_name, wms_with_icons_on_desktop[i])) {
            g_free(net_wm_name);
            return TRUE;
        }

    g_free(net_wm_name);
    return FALSE;
}

static bool has_zero_based_display_id(VDAgentDisplay *display)
{
    // Older QXL drivers numbered their outputs starting with
    // 0. This contrasts with most drivers who start numbering
    // outputs with 1.  In this case, the expected drm connector
    // name will need to be decremented before comparing to the
    // display manager output name
    bool ret = false;
#ifdef USE_GTK_FOR_MONITORS
    GdkDisplay *gdk_display = gdk_display_get_default();
    if (GDK_IS_WAYLAND_DISPLAY(gdk_display)) {
        gdk_display_sync(gdk_display);

        GListModel *monitors = gdk_display_get_monitors(gdk_display);
        int screen_count = g_list_model_get_n_items(monitors);
        for (int i = 0; i < screen_count; i++) {
            GdkMonitor *monitor = (GdkMonitor *)g_list_model_get_item(monitors, i);
            const char *name = gdk_monitor_get_connector(monitor);
            if (!name) {
                continue;
            }

            if (strcmp(name, "Virtual-0") == 0) {
                ret = true;
                break;
            }
        }
    }
    else // otherwise, use the X11 code (below)
#endif
    {
        XRRScreenResources *xres = display->x11->randr.res;
        Display *xdisplay = display->x11->display;
        for (int i = 0; i < xres->noutput; ++i) {
            XRROutputInfo *oinfo = XRRGetOutputInfo(xdisplay, xres, xres->outputs[i]);
            if (!oinfo) {
                syslog(LOG_WARNING, "Unable to lookup XRandr output info for output %li",
                       xres->outputs[i]);
                return false;
            }
            if (strcmp(oinfo->name, "Virtual-0") == 0) {
                ret = true;
                XRRFreeOutputInfo(oinfo);
                break;
            }
            XRRFreeOutputInfo(oinfo);
        }
    }
    return ret;
}

// handle the device info message from the server. This will allow us to
// maintain a mapping from spice display id to xrandr output
void vdagent_display_handle_graphics_device_info(VDAgentDisplay *display, uint8_t *data,
        size_t size)
{
    VDAgentGraphicsDeviceInfo *graphics_device_info = (VDAgentGraphicsDeviceInfo *)data;
    VDAgentDeviceDisplayInfo *device_display_info = graphics_device_info->display_info;

    void *buffer_end = data + size;
    bool decrement_id = has_zero_based_display_id(display);

    syslog(LOG_INFO, "Received Graphics Device Info:");

    for (size_t i = 0; i < graphics_device_info->count; ++i) {
        if ((void*) device_display_info > buffer_end ||
                (void*) (&device_display_info->device_address +
                    device_display_info->device_address_len) > buffer_end) {
            syslog(LOG_ERR, "Malformed graphics_display_info message, "
                   "extends beyond the end of the buffer");
            break;
        }

        // make sure the string is terminated:
        if (device_display_info->device_address_len > 0) {
            device_display_info->device_address[device_display_info->device_address_len - 1] = '\0';
        } else {
            syslog(LOG_WARNING, "Zero length device_address received for channel_id: %u, monitor_id: %u",
                   device_display_info->channel_id, device_display_info->monitor_id);
        }

        // Get the expected connector name from hardware info. Store it with the SPICE display ID.
        char expected_name[100];
        int ret = get_connector_name_for_device_info(device_display_info, expected_name,
                                                     sizeof(expected_name), decrement_id);
        if (ret == 0) {
            g_hash_table_insert(display->connector_mapping,
                                g_strdup(expected_name),
                                GUINT_TO_POINTER(device_display_info->channel_id + device_display_info->monitor_id));
            syslog(LOG_DEBUG, "Mapping connector %s to display #%d", expected_name,
                   (device_display_info->channel_id + device_display_info->monitor_id));
        }

        // Also map the SPICE display ID to the corresponding X server object.
        vdagent_x11_handle_device_display_info(display->x11, device_display_info, decrement_id);

        device_display_info = (VDAgentDeviceDisplayInfo*) ((char*) device_display_info +
            sizeof(VDAgentDeviceDisplayInfo) + device_display_info->device_address_len);
    }

    // make sure daemon is up-to-date with (possibly updated) device IDs
    vdagent_display_send_daemon_guest_res(display, TRUE);
}

/*
 * Set monitor configuration according to client request.
 *
 * On exit send current configuration to client, regardless of error.
 *
 * Errors:
 *  screen size too large for driver to handle. (we set the largest/smallest possible)
 *  no randr support in X server.
 *  invalid configuration request from client.
 */
void vdagent_display_set_monitor_config(VDAgentDisplay *display, VDAgentMonitorsConfig *mon_config,
        int fallback)
{
#ifdef USE_GTK_FOR_MONITORS
    if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
        // FIXME: there is no equivalent call to set the monitor config under wayland
        // Send the configuration back - the client need to know the resolution was not taken into account.
        vdagent_display_send_daemon_guest_res(display, TRUE);
        return;
    }
#endif
    vdagent_x11_set_monitor_config(display->x11, mon_config, fallback);
}