summaryrefslogtreecommitdiffstats
path: root/vdagent-x11.c
blob: 7019f11b3e1329aff0e27b6f2a1d6ce4ab9a6386 (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
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
599
600
601
602
603
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
/*  vdagent-x11.c vdagent x11 code

    Copyright 2010 Red Hat, Inc.

    Red Hat Authors:
    Hans de Goede <hdegoede@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/>.
*/

/* Note *all* X11 calls in this file which do not wait for a result must be
   followed by an XFlush, given that the X11 code pumping the event loop
   (and thus flushing queued writes) is only called when there is data to be
   read from the X11 socket. */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/Xfixes.h>
#include "vdagentd-proto.h"
#include "vdagent-x11.h"

enum { owner_none, owner_guest, owner_client };

const char *utf8_atom_names[] = {
    "UTF8_STRING",
    "text/plain;charset=UTF-8",
    "text/plain;charset=utf-8",
};

#define utf8_atom_count (sizeof(utf8_atom_names)/sizeof(utf8_atom_names[0]))

struct vdagent_x11_selection_request {
    XEvent event;
    struct vdagent_x11_selection_request *next;
};

struct vdagent_x11 {
    Display *display;
    Atom clipboard_atom;
    Atom targets_atom;
    Atom incr_atom;
    Atom multiple_atom;
    Atom utf8_atoms[utf8_atom_count];
    Window root_window;
    Window selection_window;
    struct udscs_connection *vdagentd;
    int verbose;
    int fd;
    int screen;
    int width;
    int height;
    int has_xrandr;
    int has_xfixes;
    int xfixes_event_base;
    int expected_targets_notifies;
    int clipboard_owner;
    int clipboard_type_count;
    /* Warning the size of these needs to be increased each time we add
       support for a new type!! */
    uint32_t clipboard_agent_types[1];
    Atom clipboard_x11_targets[1];
    struct vdagent_x11_selection_request *selection_request;
};

static void vdagent_x11_send_daemon_guest_xorg_res(struct vdagent_x11 *x11);
static void vdagent_x11_handle_selection_notify(struct vdagent_x11 *x11,
                                                XEvent *event);
static void vdagent_x11_handle_selection_request(struct vdagent_x11 *x11);
static void vdagent_x11_handle_targets_notify(struct vdagent_x11 *x11,
                                              XEvent *event);
static void vdagent_x11_send_selection_notify(struct vdagent_x11 *x11,
                                              Atom prop,
                                              int process_next_req);

struct vdagent_x11 *vdagent_x11_create(struct udscs_connection *vdagentd,
    int verbose)
{
    struct vdagent_x11 *x11;
    XWindowAttributes attrib;
    int i, major, minor;

    x11 = calloc(1, sizeof(*x11));
    if (!x11) {
        fprintf(stderr, "out of memory allocating vdagent_x11 struct\n");
        return NULL;
    }
    
    x11->vdagentd = vdagentd;
    x11->verbose = verbose;

    x11->display = XOpenDisplay(NULL);
    if (!x11->display) {
        fprintf(stderr, "could not connect to X-server\n");
        free(x11);
        return NULL;
    }

    x11->screen = DefaultScreen(x11->display);
    x11->root_window = RootWindow(x11->display, x11->screen);
    x11->fd = ConnectionNumber(x11->display);
    x11->clipboard_atom = XInternAtom(x11->display, "CLIPBOARD", False);
    x11->targets_atom = XInternAtom(x11->display, "TARGETS", False);
    x11->incr_atom = XInternAtom(x11->display, "INCR", False);
    x11->multiple_atom = XInternAtom(x11->display, "MULTIPLE", False);
    for(i = 0; i < utf8_atom_count; i++)
        x11->utf8_atoms[i] = XInternAtom(x11->display, utf8_atom_names[i],
                                         False);

    /* We should not store properties (for selections) on the root window */
    x11->selection_window = XCreateSimpleWindow(x11->display, x11->root_window,
                                                0, 0, 1, 1, 0, 0, 0);
    if (x11->verbose)
        fprintf(stderr, "Selection window: %u\n",
                (unsigned int)x11->selection_window);

    if (XRRQueryExtension(x11->display, &i, &i))
        x11->has_xrandr = 1;
    else
        fprintf(stderr, "no xrandr\n");

    if (XFixesQueryExtension(x11->display, &x11->xfixes_event_base, &i) &&
        XFixesQueryVersion(x11->display, &major, &minor) && major >= 1) {
        x11->has_xfixes = 1;
        XFixesSelectSelectionInput(x11->display, x11->root_window,
                                   x11->clipboard_atom,
                                   XFixesSetSelectionOwnerNotifyMask);
    } else
        fprintf(stderr, "no xfixes, no guest -> client copy paste support\n");

    /* Catch resolution changes */
    XSelectInput(x11->display, x11->root_window, StructureNotifyMask);

    /* Get the current resolution */
    XGetWindowAttributes(x11->display, x11->root_window, &attrib);
    x11->width = attrib.width;
    x11->height = attrib.height;
    vdagent_x11_send_daemon_guest_xorg_res(x11);

    /* No need for XFlush as XGetWindowAttributes does an implicit Xflush */

    return x11;
}

void vdagent_x11_destroy(struct vdagent_x11 *x11)
{
    if (!x11)
        return;

    XCloseDisplay(x11->display);
    free(x11);
}

int vdagent_x11_get_fd(struct vdagent_x11 *x11)
{
    return x11->fd;
}

static void vdagent_x11_set_clipboard_owner(struct vdagent_x11 *x11,
    int new_owner)
{
    if (x11->selection_request) {
         fprintf(stderr,
                 "selection requests pending on clipboard ownership change, "
                 "clearing");
         while (x11->selection_request)
             vdagent_x11_send_selection_notify(x11, None, 0);
    }

    if (new_owner == owner_none) {
        /* When going from owner_guest to owner_none we need to send a
           clipboard release message to the client */
        if (x11->clipboard_owner == owner_guest)
           udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_RELEASE, 0, NULL, 0);

        x11->clipboard_type_count = 0;
    }
    x11->clipboard_owner = new_owner;
}

static void vdagent_x11_handle_event(struct vdagent_x11 *x11)
{
    XEvent event;
    int handled = 0;

    XNextEvent(x11->display, &event);

    if (event.type == x11->xfixes_event_base) {
        union {
            XEvent ev;
            XFixesSelectionNotifyEvent xfev;
        } ev;

        ev.ev = event;
        if (ev.xfev.subtype != XFixesSetSelectionOwnerNotify) {
            if (x11->verbose)
                fprintf(stderr, "unexpected xfix event subtype %d window %d\n",
                        (int)ev.xfev.subtype, (int)event.xany.window);
            return;
        }

        if (x11->verbose)
            fprintf(stderr, "New selection owner: %u\n",
                    (unsigned int)ev.xfev.owner);

        /* Ignore becoming the owner ourselves */
        if (ev.xfev.owner == x11->selection_window)
            return;

        /* If the clipboard owner is changed we no longer own it */
        vdagent_x11_set_clipboard_owner(x11, owner_none);

        if (ev.xfev.owner == None)
            return;

        /* Request the supported targets from the new owner */
        XConvertSelection(x11->display, x11->clipboard_atom, x11->targets_atom,
                          x11->targets_atom, x11->selection_window,
                          CurrentTime);
        XFlush(x11->display);
        x11->expected_targets_notifies++;
        return;
    }

    switch (event.type) {
    case ConfigureNotify:
        if (event.xconfigure.window != x11->root_window)
            break;

        handled = 1;

        if (event.xconfigure.width  == x11->width &&
            event.xconfigure.height == x11->height)
            break;

        x11->width  = event.xconfigure.width;
        x11->height = event.xconfigure.height;

        vdagent_x11_send_daemon_guest_xorg_res(x11);
        break;
    case SelectionNotify:
        if (event.xselection.target == x11->targets_atom)
            vdagent_x11_handle_targets_notify(x11, &event);
        else
            vdagent_x11_handle_selection_notify(x11, &event);

        handled = 1;
        break;
    case SelectionClear:
        /* Do nothing the clipboard ownership will get updated through
           the XFixesSetSelectionOwnerNotify event */
        handled = 1;
        break;
    case SelectionRequest: {
        struct vdagent_x11_selection_request *req, *new_req;

        new_req = malloc(sizeof(*new_req));
        if (!new_req) {
            fprintf(stderr, "out of memory on SelectionRequest, ignoring.\n");
            break;
        }

        handled = 1;

        new_req->event = event;
        new_req->next = NULL;

        if (!x11->selection_request) {
            x11->selection_request = new_req;
            vdagent_x11_handle_selection_request(x11);
            break;
        }

        /* maybe we should limit the selection_request stack depth ? */
        req = x11->selection_request;
        while (req->next)
            req = req->next;

        req->next = new_req;
        break;
    }
    }
    if (!handled && x11->verbose)
        fprintf(stderr, "unhandled x11 event, type %d, window %d\n",
                (int)event.type, (int)event.xany.window);
}

void vdagent_x11_do_read(struct vdagent_x11 *x11)
{
    while(XPending(x11->display))
         vdagent_x11_handle_event(x11);
}

static void vdagent_x11_send_daemon_guest_xorg_res(struct vdagent_x11 *x11)
{
    struct vdagentd_guest_xorg_resolution res;

    res.width  = x11->width;
    res.height = x11->height;

    udscs_write(x11->vdagentd, VDAGENTD_GUEST_XORG_RESOLUTION, 0,
                (uint8_t *)&res, sizeof(res));
}

static const char *vdagent_x11_get_atom_name(struct vdagent_x11 *x11, Atom a)
{
    if (a == None)
        return "None";

    return XGetAtomName(x11->display, a);
}

static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event,
                                  Atom type, Atom prop, int format,
                                  unsigned char **data)
{
    Atom type_ret;
    int format_ret;
    unsigned long len, remain;

    if (event->xselection.property == None) {
        if (x11->verbose)
            fprintf(stderr, "XConvertSelection refused by clipboard owner\n");
        return -1;
    }

    if (event->xselection.requestor != x11->selection_window ||
        event->xselection.selection != x11->clipboard_atom ||
        event->xselection.property  != prop) {
        fprintf(stderr, "SelectionNotify parameters mismatch\n");
        return -1;
    }

    /* FIXME when we've incr support we should not immediately
       delete the property (as we need to first register for
       property change events) */
    if (XGetWindowProperty(x11->display, x11->selection_window, prop, 0,
                           LONG_MAX, True, type, &type_ret, &format_ret, &len,
                           &remain, data) != Success) {
        fprintf(stderr, "XGetWindowProperty failed\n");
        return -1;
    }

    if (type_ret == x11->incr_atom) {
        /* FIXME */
        fprintf(stderr, "incr properties are currently unsupported\n");
        return -1;
    }

    if (type_ret != type) {
        fprintf(stderr, "expected property type: %s, got: %s\n",
                vdagent_x11_get_atom_name(x11, type),
                vdagent_x11_get_atom_name(x11, type_ret));
        return -1;
    }

    if (format_ret != format) {
        fprintf(stderr, "expected %d bit format, got %d bits\n", format,
                format_ret);
        return -1;
    }

    if (len == 0) {
        fprintf(stderr, "property contains no data (zero length)\n");
        return -1;
    }

    return len;
}

static uint32_t vdagent_x11_target_to_type(struct vdagent_x11 *x11,
                                           Atom target)
{
    int i;

    for (i = 0; i < utf8_atom_count; i++)
        if (x11->utf8_atoms[i] == target)
            return VD_AGENT_CLIPBOARD_UTF8_TEXT;

    /* TODO Add support for more types here */
    
    fprintf(stderr, "unexpected selection type %s\n",
            vdagent_x11_get_atom_name(x11, target));
    return VD_AGENT_CLIPBOARD_NONE;
}

static Atom vdagent_x11_type_to_target(struct vdagent_x11 *x11, uint32_t type)
{
    int i;

    for (i = 0; i < x11->clipboard_type_count; i++)
        if (x11->clipboard_agent_types[i] == type)
            return x11->clipboard_x11_targets[i];

    fprintf(stderr, "client requested unavailable type %u\n", type);
    return None;
}

static void vdagent_x11_handle_selection_notify(struct vdagent_x11 *x11,
                                                XEvent *event)
{
    int len;
    unsigned char *data = NULL;
    uint32_t type;

    type = vdagent_x11_target_to_type(x11, event->xselection.target);
    if (type == VD_AGENT_CLIPBOARD_NONE) {
        udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_DATA,
                    VD_AGENT_CLIPBOARD_NONE, NULL, 0);
        return;
    }

    len = vdagent_x11_get_selection(x11, event, event->xselection.target,
                                    x11->clipboard_atom, 8, &data);
    if (len == -1) {
        if (data)
            XFree(data);
        udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_DATA,
                    VD_AGENT_CLIPBOARD_NONE, NULL, 0);
        return;
    }

    udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_DATA, type, data, len);
    XFree(data);
}

static Atom atom_lists_overlap(Atom *atoms1, Atom *atoms2, int l1, int l2)
{
    int i, j;

    for (i = 0; i < l1; i++)
        for (j = 0; j < l2; j++)
            if (atoms1[i] == atoms2[j])
                return atoms1[i];

    return 0;
}

static void vdagent_x11_print_targets(struct vdagent_x11 *x11,
                                      const char *action, Atom *atoms, int c)
{
    int i;

    if (!x11->verbose)
        return;

    fprintf(stderr, "%s %d targets:\n", action, c);
    for (i = 0; i < c; i++)
        fprintf(stderr, "%s\n", vdagent_x11_get_atom_name(x11, atoms[i]));
}

static void vdagent_x11_handle_targets_notify(struct vdagent_x11 *x11,
                                              XEvent *event)
{
    int len;
    Atom atom, *atoms = NULL;

    if (!x11->expected_targets_notifies) {
        fprintf(stderr, "unexpected selection notify TARGETS\n");
        return;
    }

    x11->expected_targets_notifies--;

    /* If we have more targets_notifies pending, ignore this one, we
       are only interested in the targets list of the current owner
       (which is the last one we've requested a targets list from */
    if (x11->expected_targets_notifies)
        return;

    len = vdagent_x11_get_selection(x11, event, XA_ATOM, x11->targets_atom, 32,
                                    (unsigned char **)&atoms);
    if (len == -1)
        return;

    vdagent_x11_print_targets(x11, "received", atoms, len);

    x11->clipboard_type_count = 0;
    atom = atom_lists_overlap(x11->utf8_atoms, atoms, utf8_atom_count, len);
    if (atom) {
        x11->clipboard_agent_types[x11->clipboard_type_count] =
            VD_AGENT_CLIPBOARD_UTF8_TEXT;
        x11->clipboard_x11_targets[x11->clipboard_type_count] = atom;
        x11->clipboard_type_count++;
    }

    /* TODO Add support for more types here */

    if (x11->clipboard_type_count) {
        udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_GRAB, 0,
                    (uint8_t *)x11->clipboard_agent_types,
                    x11->clipboard_type_count * sizeof(uint32_t));
        vdagent_x11_set_clipboard_owner(x11, owner_guest);
    }
}

static void vdagent_x11_send_selection_notify(struct vdagent_x11 *x11,
    Atom prop, int process_next_req)
{
    XEvent res, *event = &x11->selection_request->event;
    struct vdagent_x11_selection_request *selection_request;

    res.xselection.property = prop;
    res.xselection.type = SelectionNotify;
    res.xselection.display = event->xselectionrequest.display;
    res.xselection.requestor = event->xselectionrequest.requestor;
    res.xselection.selection = event->xselectionrequest.selection;
    res.xselection.target = event->xselectionrequest.target;
    res.xselection.time = event->xselectionrequest.time;
    XSendEvent(x11->display, event->xselectionrequest.requestor, 0, 0, &res);
    XFlush(x11->display);

    selection_request = x11->selection_request;
    x11->selection_request = selection_request->next;
    free(selection_request);
    if (process_next_req)
        vdagent_x11_handle_selection_request(x11);
}

static void vdagent_x11_send_targets(struct vdagent_x11 *x11, XEvent *event)
{
    /* Warning the size of this needs to be increased each time we add support
       for a new type, or the atom count of an existing type changes */
    Atom prop, targets[4] = { x11->targets_atom, };
    int i, j, target_count = 1;

    for (i = 0; i < x11->clipboard_type_count; i++) {
        switch (x11->clipboard_agent_types[i]) {
            case VD_AGENT_CLIPBOARD_UTF8_TEXT:
                for (j = 0; j < utf8_atom_count; j++) {
                    targets[target_count] = x11->utf8_atoms[j];
                    target_count++;
                }
                break;
            /* TODO Add support for more types here */
        }
    }

    prop = event->xselectionrequest.property;
    if (prop == None)
        prop = event->xselectionrequest.target;

    XChangeProperty(x11->display, event->xselectionrequest.requestor, prop,
                    XA_ATOM, 32, PropModeReplace, (unsigned char *)&targets,
                    target_count);
    vdagent_x11_send_selection_notify(x11, prop, 1);
    vdagent_x11_print_targets(x11, "sent", targets, target_count);
}

static void vdagent_x11_handle_selection_request(struct vdagent_x11 *x11)
{
    XEvent *event;
    uint32_t type = VD_AGENT_CLIPBOARD_NONE;

    if (!x11->selection_request)
        return;

    event = &x11->selection_request->event;

    if (x11->clipboard_owner != owner_client) {
        fprintf(stderr,
            "received selection request event for target %s, "
            "while not owning client clipboard\n",
            vdagent_x11_get_atom_name(x11, event->xselectionrequest.target));
        vdagent_x11_send_selection_notify(x11, None, 1);
        return;
    }

    if (event->xselectionrequest.target == x11->multiple_atom) {
        fprintf(stderr, "multiple target not supported\n");
        vdagent_x11_send_selection_notify(x11, None, 1);
        return;
    }

    if (event->xselectionrequest.target == x11->targets_atom) {
        vdagent_x11_send_targets(x11, event);
        return;
    }

    type = vdagent_x11_target_to_type(x11, event->xselectionrequest.target);
    if (type == VD_AGENT_CLIPBOARD_NONE) {
        vdagent_x11_send_selection_notify(x11, None, 1);
        return;
    }

    udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_REQUEST, type, NULL, 0);
}

void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11,
                                    VDAgentMonitorsConfig *mon_config)
{
    int i, num_sizes = 0;
    int best = -1;
    unsigned int closest_diff = -1;
    XRRScreenSize* sizes;
    XRRScreenConfiguration* config;
    Rotation rotation;

    if (!x11->has_xrandr)
        return;

    if (mon_config->num_of_monitors != 1) {
        fprintf(stderr, "Only 1 monitor supported, ignoring monitor config\n");
        return;
    }

    sizes = XRRSizes(x11->display, x11->screen, &num_sizes);
    if (!sizes || !num_sizes) {
        fprintf(stderr, "XRRSizes failed\n");
        return;
    }

    /* Find the closest size which will fit within the monitor */
    for (i = 0; i < num_sizes; i++) {
        if (sizes[i].width  > mon_config->monitors[0].width ||
            sizes[i].height > mon_config->monitors[0].height)
            continue; /* Too large for the monitor */

        unsigned int wdiff = mon_config->monitors[0].width  - sizes[i].width;
        unsigned int hdiff = mon_config->monitors[0].height - sizes[i].height;
        unsigned int diff = wdiff * wdiff + hdiff * hdiff;
        if (diff < closest_diff) {
            closest_diff = diff;
            best = i;
        }
    }

    if (best == -1) {
        fprintf(stderr, "no suitable resolution found for monitor\n");
        return;
    }

    config = XRRGetScreenInfo(x11->display, x11->root_window);
    if(!config) {
        fprintf(stderr, "get screen info failed\n");
        return;
    }
    XRRConfigCurrentConfiguration(config, &rotation);
    XRRSetScreenConfig(x11->display, config, x11->root_window, best,
                       rotation, CurrentTime);
    XRRFreeScreenConfigInfo(config);
    XFlush(x11->display);
}

void vdagent_x11_clipboard_request(struct vdagent_x11 *x11, uint32_t type)
{
    Atom target;

    if (x11->clipboard_owner != owner_guest) {
        fprintf(stderr,
                "received clipboard req while not owning guest clipboard\n");
        udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_DATA,
                    VD_AGENT_CLIPBOARD_NONE, NULL, 0);
        return;
    }
    
    target = vdagent_x11_type_to_target(x11, type);
    if (target == None) {
        udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_DATA,
                    VD_AGENT_CLIPBOARD_NONE, NULL, 0);
        return;
    }

    XConvertSelection(x11->display, x11->clipboard_atom, target,
                      x11->clipboard_atom, x11->selection_window, CurrentTime);
    XFlush(x11->display);
}

void vdagent_x11_clipboard_grab(struct vdagent_x11 *x11, uint32_t *types,
    uint32_t type_count)
{
    int i;

    x11->clipboard_type_count = 0;
    for (i = 0; i < type_count; i++) {
        /* TODO Add support for more types here */
        /* Check if we support the type */
        if (types[i] != VD_AGENT_CLIPBOARD_UTF8_TEXT)
            continue;

        x11->clipboard_agent_types[x11->clipboard_type_count] = types[i];
        x11->clipboard_type_count++;
    }

    if (!x11->clipboard_type_count)
        return;

    XSetSelectionOwner(x11->display, x11->clipboard_atom,
                       x11->selection_window, CurrentTime);
    XFlush(x11->display);
    vdagent_x11_set_clipboard_owner(x11, owner_client);
}

void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint32_t type,
    const uint8_t *data, uint32_t size)
{
    Atom prop;
    XEvent *event;
    uint32_t type_from_event;

    if (!x11->selection_request) {
        fprintf(stderr, "received clipboard data without an outstanding"
                        "selection request, ignoring\n");
        return;
    }

    event = &x11->selection_request->event;
    type_from_event = vdagent_x11_target_to_type(x11,
                                             event->xselectionrequest.target);
    if (type_from_event != type) {
        fprintf(stderr, "expecting type %u clipboard data got %u\n",
                type_from_event, type);
        vdagent_x11_send_selection_notify(x11, None, 1);
        return;
    }

    prop = event->xselectionrequest.property;
    if (prop == None)
        prop = event->xselectionrequest.target;

    XChangeProperty(x11->display, event->xselectionrequest.requestor, prop,
                    event->xselectionrequest.target, 8, PropModeReplace,
                    data, size);
    XFlush(x11->display);
    vdagent_x11_send_selection_notify(x11, prop, 1);
}