summaryrefslogtreecommitdiffstats
path: root/src/vdagent-x11-priv.h
blob: c607850691450421c7c32f19068fa9a41175fffd (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
#ifndef VDAGENT_X11_PRIV
#define VDAGENT_X11_PRIV

#include <stdint.h>
#include <stdio.h>

#include <spice/vd_agent.h>

#include <X11/extensions/Xrandr.h>

/* Macros to print a message to the logfile prefixed by the selection */
#define SELPRINTF(format, ...) \
    syslog(LOG_ERR, "%s: " format, \
            vdagent_x11_sel_to_str(selection), ##__VA_ARGS__)

#define VSELPRINTF(format, ...) \
    do { \
        if (x11->debug) { \
            syslog(LOG_DEBUG, "%s: " format, \
                    vdagent_x11_sel_to_str(selection), ##__VA_ARGS__); \
        } \
    } while (0)

#define MAX_SCREENS 16
/* Same as qxl_dev.h client_monitors_config.heads count */
#define MONITOR_SIZE_COUNT 64

enum { owner_none, owner_guest, owner_client };

/* X11 terminology is confusing a selection request is a request from an
   app to get clipboard data from us, so iow from the spice client through
   the vdagent channel. We handle these one at a time and queue any which
   come in while we are still handling the current one. */
struct vdagent_x11_selection_request {
    XEvent event;
    uint8_t selection;
    struct vdagent_x11_selection_request *next;
};

/* A conversion request is X11 speak for asking an other app to give its
   clipboard data to us, we do these on behalf of the spice client to copy
   data from the guest to the client. Like selection requests we process
   these one at a time. */
struct vdagent_x11_conversion_request {
    Atom target;
    uint8_t selection;
    struct vdagent_x11_conversion_request *next;
};

struct clipboard_format_tmpl {
    uint32_t type;
    const char *atom_names[16];
};

struct clipboard_format_info {
    uint32_t type;
    Atom atoms[16];
    int atom_count;
};

struct monitor_size {
    int width;
    int height;
};

static const struct clipboard_format_tmpl clipboard_format_templates[] = {
    { VD_AGENT_CLIPBOARD_UTF8_TEXT, { "UTF8_STRING",
      "text/plain;charset=UTF-8", "text/plain;charset=utf-8", NULL }, },
    { VD_AGENT_CLIPBOARD_IMAGE_PNG, { "image/png", NULL }, },
    { VD_AGENT_CLIPBOARD_IMAGE_BMP, { "image/bmp", "image/x-bmp",
      "image/x-MS-bmp", "image/x-win-bitmap", NULL }, },
    { VD_AGENT_CLIPBOARD_IMAGE_TIFF, { "image/tiff", NULL }, },
    { VD_AGENT_CLIPBOARD_IMAGE_JPG, { "image/jpeg", NULL }, },
};

#define clipboard_format_count (sizeof(clipboard_format_templates)/sizeof(clipboard_format_templates[0]))

struct vdagent_x11 {
    struct clipboard_format_info clipboard_formats[clipboard_format_count];
    Display *display;
    Atom clipboard_atom;
    Atom clipboard_primary_atom;
    Atom targets_atom;
    Atom incr_atom;
    Atom multiple_atom;
    Window root_window[MAX_SCREENS];
    Window selection_window;
    struct udscs_connection *vdagentd;
    char *net_wm_name;
    int debug;
    int fd;
    int screen_count;
    int width[MAX_SCREENS];
    int height[MAX_SCREENS];
    int has_xfixes;
    int xfixes_event_base;
    int max_prop_size;
    int expected_targets_notifies[256];
    int clipboard_owner[256];
    int clipboard_type_count[256];
    uint32_t clipboard_agent_types[256][256];
    Atom clipboard_x11_targets[256][256];
    /* Data for conversion_req which is currently being processed */
    struct vdagent_x11_conversion_request *conversion_req;
    int expect_property_notify;
    uint8_t *clipboard_data;
    uint32_t clipboard_data_size;
    uint32_t clipboard_data_space;
    /* Data for selection_req which is currently being processed */
    struct vdagent_x11_selection_request *selection_req;
    uint8_t *selection_req_data;
    uint32_t selection_req_data_pos;
    uint32_t selection_req_data_size;
    Atom selection_req_atom;
    /* resolution change state */
    struct {
        XRRScreenResources *res;
        XRROutputInfo **outputs;
        XRRCrtcInfo **crtcs;
        int min_width;
        int max_width;
        int min_height;
        int max_height;
        int num_monitors;
        struct monitor_size monitor_sizes[MONITOR_SIZE_COUNT];
        VDAgentMonitorsConfig *failed_conf;
    } randr;

    /* NB: we cache this assuming the driver isn't changed under our feet */
    int set_crtc_config_not_functional;

    int has_xrandr;
    int xrandr_major;
    int xrandr_minor;
    int has_xinerama;
    int dont_send_guest_xorg_res;
};

extern int (*vdagent_x11_prev_error_handler)(Display *, XErrorEvent *);
extern int vdagent_x11_caught_error;

void vdagent_x11_randr_init(struct vdagent_x11 *x11);
void vdagent_x11_send_daemon_guest_xorg_res(struct vdagent_x11 *x11,
                                            int update);
void vdagent_x11_randr_handle_root_size_change(struct vdagent_x11 *x11,
                                            int screen, int width, int height);

void vdagent_x11_set_error_handler(struct vdagent_x11 *x11,
    int (*handler)(Display *, XErrorEvent *));
int vdagent_x11_restore_error_handler(struct vdagent_x11 *x11);

#endif // VDAGENT_X11_PRIV