Allow clock_gettime() monotonic clock to be utilized on more OS's
[qemu/ar7.git] / spice-qemu-char.c
blob535f955ca88abd2021697b4c7ff7645fd72f5524
1 #include "config-host.h"
2 #include "trace.h"
3 #include "ui/qemu-spice.h"
4 #include "char/char.h"
5 #include <spice.h>
6 #include <spice-experimental.h>
7 #include <spice/protocol.h>
9 #include "qemu/osdep.h"
11 typedef struct SpiceCharDriver {
12 CharDriverState* chr;
13 SpiceCharDeviceInstance sin;
14 char *subtype;
15 bool active;
16 uint8_t *buffer;
17 uint8_t *datapos;
18 ssize_t bufsize, datalen;
19 QLIST_ENTRY(SpiceCharDriver) next;
20 } SpiceCharDriver;
22 static QLIST_HEAD(, SpiceCharDriver) spice_chars =
23 QLIST_HEAD_INITIALIZER(spice_chars);
25 static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
27 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
28 ssize_t out = 0;
29 ssize_t last_out;
30 uint8_t* p = (uint8_t*)buf;
32 while (len > 0) {
33 last_out = MIN(len, qemu_chr_be_can_write(scd->chr));
34 if (last_out <= 0) {
35 break;
37 qemu_chr_be_write(scd->chr, p, last_out);
38 out += last_out;
39 len -= last_out;
40 p += last_out;
43 trace_spice_vmc_write(out, len + out);
44 return out;
47 static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
49 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
50 int bytes = MIN(len, scd->datalen);
52 if (bytes > 0) {
53 memcpy(buf, scd->datapos, bytes);
54 scd->datapos += bytes;
55 scd->datalen -= bytes;
56 assert(scd->datalen >= 0);
57 if (scd->datalen == 0) {
58 scd->datapos = 0;
61 trace_spice_vmc_read(bytes, len);
62 return bytes;
65 #if SPICE_SERVER_VERSION >= 0x000c02
66 static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
68 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
69 int chr_event;
71 switch (event) {
72 case SPICE_PORT_EVENT_BREAK:
73 chr_event = CHR_EVENT_BREAK;
74 break;
75 default:
76 return;
79 trace_spice_vmc_event(chr_event);
80 qemu_chr_be_event(scd->chr, chr_event);
82 #endif
84 static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
86 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
88 #if SPICE_SERVER_VERSION < 0x000901
90 * spice-server calls the state callback for the agent channel when the
91 * spice client connects / disconnects. Given that not the client but
92 * the server is doing the parsing of the messages this is wrong as the
93 * server is still listening. Worse, this causes the parser in the server
94 * to go out of sync, so we ignore state calls for subtype vdagent
95 * spicevmc chardevs. For the full story see:
96 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
98 if (strcmp(sin->subtype, "vdagent") == 0) {
99 return;
101 #endif
103 if ((scd->chr->be_open && connected) ||
104 (!scd->chr->be_open && !connected)) {
105 return;
108 qemu_chr_be_event(scd->chr,
109 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
112 static SpiceCharDeviceInterface vmc_interface = {
113 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
114 .base.description = "spice virtual channel char device",
115 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
116 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
117 .state = vmc_state,
118 .write = vmc_write,
119 .read = vmc_read,
120 #if SPICE_SERVER_VERSION >= 0x000c02
121 .event = vmc_event,
122 #endif
126 static void vmc_register_interface(SpiceCharDriver *scd)
128 if (scd->active) {
129 return;
131 scd->sin.base.sif = &vmc_interface.base;
132 qemu_spice_add_interface(&scd->sin.base);
133 scd->active = true;
134 trace_spice_vmc_register_interface(scd);
137 static void vmc_unregister_interface(SpiceCharDriver *scd)
139 if (!scd->active) {
140 return;
142 spice_server_remove_interface(&scd->sin.base);
143 scd->active = false;
144 trace_spice_vmc_unregister_interface(scd);
148 static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
150 SpiceCharDriver *s = chr->opaque;
152 assert(s->datalen == 0);
153 if (s->bufsize < len) {
154 s->bufsize = len;
155 s->buffer = g_realloc(s->buffer, s->bufsize);
157 memcpy(s->buffer, buf, len);
158 s->datapos = s->buffer;
159 s->datalen = len;
160 spice_server_char_device_wakeup(&s->sin);
161 return len;
164 static void spice_chr_close(struct CharDriverState *chr)
166 SpiceCharDriver *s = chr->opaque;
168 vmc_unregister_interface(s);
169 QLIST_REMOVE(s, next);
171 g_free((char *)s->sin.subtype);
172 #if SPICE_SERVER_VERSION >= 0x000c02
173 g_free((char *)s->sin.portname);
174 #endif
175 g_free(s);
178 static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open)
180 SpiceCharDriver *s = chr->opaque;
181 if (fe_open) {
182 vmc_register_interface(s);
183 } else {
184 vmc_unregister_interface(s);
188 static void print_allowed_subtypes(void)
190 const char** psubtype;
191 int i;
193 fprintf(stderr, "allowed names: ");
194 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
195 *psubtype != NULL; ++psubtype, ++i) {
196 if (i == 0) {
197 fprintf(stderr, "%s", *psubtype);
198 } else {
199 fprintf(stderr, ", %s", *psubtype);
202 fprintf(stderr, "\n");
205 static CharDriverState *chr_open(const char *subtype)
207 CharDriverState *chr;
208 SpiceCharDriver *s;
210 chr = g_malloc0(sizeof(CharDriverState));
211 s = g_malloc0(sizeof(SpiceCharDriver));
212 s->chr = chr;
213 s->active = false;
214 s->sin.subtype = g_strdup(subtype);
215 chr->opaque = s;
216 chr->chr_write = spice_chr_write;
217 chr->chr_close = spice_chr_close;
218 chr->chr_set_fe_open = spice_chr_set_fe_open;
220 QLIST_INSERT_HEAD(&spice_chars, s, next);
222 return chr;
225 CharDriverState *qemu_chr_open_spice_vmc(const char *type)
227 CharDriverState *chr;
228 const char **psubtype = spice_server_char_device_recognized_subtypes();
230 if (type == NULL) {
231 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
232 print_allowed_subtypes();
233 return NULL;
235 for (; *psubtype != NULL; ++psubtype) {
236 if (strcmp(type, *psubtype) == 0) {
237 break;
240 if (*psubtype == NULL) {
241 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
242 print_allowed_subtypes();
243 return NULL;
246 chr = chr_open(type);
248 #if SPICE_SERVER_VERSION < 0x000901
249 /* See comment in vmc_state() */
250 if (strcmp(type, "vdagent") == 0) {
251 qemu_chr_generic_open(chr);
253 #endif
255 return chr;
258 #if SPICE_SERVER_VERSION >= 0x000c02
259 CharDriverState *qemu_chr_open_spice_port(const char *name)
261 CharDriverState *chr;
262 SpiceCharDriver *s;
264 if (name == NULL) {
265 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
266 return NULL;
269 chr = chr_open("port");
270 s = chr->opaque;
271 s->sin.portname = g_strdup(name);
273 return chr;
276 void qemu_spice_register_ports(void)
278 SpiceCharDriver *s;
280 QLIST_FOREACH(s, &spice_chars, next) {
281 if (s->sin.portname == NULL) {
282 continue;
284 vmc_register_interface(s);
287 #endif
289 static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
290 Error **errp)
292 const char *name = qemu_opt_get(opts, "name");
294 if (name == NULL) {
295 error_setg(errp, "chardev: spice channel: no name given");
296 return;
298 backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
299 backend->spicevmc->type = g_strdup(name);
302 static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
303 Error **errp)
305 const char *name = qemu_opt_get(opts, "name");
307 if (name == NULL) {
308 error_setg(errp, "chardev: spice port: no name given");
309 return;
311 backend->spiceport = g_new0(ChardevSpicePort, 1);
312 backend->spiceport->fqdn = g_strdup(name);
315 static void register_types(void)
317 register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
318 qemu_chr_parse_spice_vmc);
319 register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
320 qemu_chr_parse_spice_port);
323 type_init(register_types);