1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "include/qemu-common.h"
4 #include "chardev/char.h"
5 #include "qemu/buffer.h"
6 #include "qemu/option.h"
7 #include "qemu/units.h"
8 #include "hw/qdev-core.h"
9 #include "migration/blocker.h"
10 #include "ui/clipboard.h"
11 #include "ui/console.h"
15 #include "qapi/qapi-types-char.h"
16 #include "qapi/qapi-types-ui.h"
18 #include "spice/vd_agent.h"
20 #define VDAGENT_BUFFER_LIMIT (1 * MiB)
21 #define VDAGENT_MOUSE_DEFAULT true
22 #define VDAGENT_CLIPBOARD_DEFAULT false
24 struct VDAgentChardev
{
27 /* TODO: migration isn't yet supported */
28 Error
*migration_blocker
;
45 DeviceState mouse_dev
;
49 uint32_t mouse_display
;
50 QemuInputHandlerState
*mouse_hs
;
53 QemuClipboardPeer cbpeer
;
54 uint32_t cbpending
[QEMU_CLIPBOARD_SELECTION__COUNT
];
56 typedef struct VDAgentChardev VDAgentChardev
;
58 #define TYPE_CHARDEV_QEMU_VDAGENT "chardev-qemu-vdagent"
60 DECLARE_INSTANCE_CHECKER(VDAgentChardev
, QEMU_VDAGENT_CHARDEV
,
61 TYPE_CHARDEV_QEMU_VDAGENT
);
63 /* ------------------------------------------------------------------ */
64 /* names, for debug logging */
66 static const char *cap_name
[] = {
67 [VD_AGENT_CAP_MOUSE_STATE
] = "mouse-state",
68 [VD_AGENT_CAP_MONITORS_CONFIG
] = "monitors-config",
69 [VD_AGENT_CAP_REPLY
] = "reply",
70 [VD_AGENT_CAP_CLIPBOARD
] = "clipboard",
71 [VD_AGENT_CAP_DISPLAY_CONFIG
] = "display-config",
72 [VD_AGENT_CAP_CLIPBOARD_BY_DEMAND
] = "clipboard-by-demand",
73 [VD_AGENT_CAP_CLIPBOARD_SELECTION
] = "clipboard-selection",
74 [VD_AGENT_CAP_SPARSE_MONITORS_CONFIG
] = "sparse-monitors-config",
75 [VD_AGENT_CAP_GUEST_LINEEND_LF
] = "guest-lineend-lf",
76 [VD_AGENT_CAP_GUEST_LINEEND_CRLF
] = "guest-lineend-crlf",
77 [VD_AGENT_CAP_MAX_CLIPBOARD
] = "max-clipboard",
78 [VD_AGENT_CAP_AUDIO_VOLUME_SYNC
] = "audio-volume-sync",
79 [VD_AGENT_CAP_MONITORS_CONFIG_POSITION
] = "monitors-config-position",
80 [VD_AGENT_CAP_FILE_XFER_DISABLED
] = "file-xfer-disabled",
81 [VD_AGENT_CAP_FILE_XFER_DETAILED_ERRORS
] = "file-xfer-detailed-errors",
83 [VD_AGENT_CAP_GRAPHICS_DEVICE_INFO
] = "graphics-device-info",
84 [VD_AGENT_CAP_CLIPBOARD_NO_RELEASE_ON_REGRAB
] = "clipboard-no-release-on-regrab",
85 [VD_AGENT_CAP_CLIPBOARD_GRAB_SERIAL
] = "clipboard-grab-serial",
89 static const char *msg_name
[] = {
90 [VD_AGENT_MOUSE_STATE
] = "mouse-state",
91 [VD_AGENT_MONITORS_CONFIG
] = "monitors-config",
92 [VD_AGENT_REPLY
] = "reply",
93 [VD_AGENT_CLIPBOARD
] = "clipboard",
94 [VD_AGENT_DISPLAY_CONFIG
] = "display-config",
95 [VD_AGENT_ANNOUNCE_CAPABILITIES
] = "announce-capabilities",
96 [VD_AGENT_CLIPBOARD_GRAB
] = "clipboard-grab",
97 [VD_AGENT_CLIPBOARD_REQUEST
] = "clipboard-request",
98 [VD_AGENT_CLIPBOARD_RELEASE
] = "clipboard-release",
99 [VD_AGENT_FILE_XFER_START
] = "file-xfer-start",
100 [VD_AGENT_FILE_XFER_STATUS
] = "file-xfer-status",
101 [VD_AGENT_FILE_XFER_DATA
] = "file-xfer-data",
102 [VD_AGENT_CLIENT_DISCONNECTED
] = "client-disconnected",
103 [VD_AGENT_MAX_CLIPBOARD
] = "max-clipboard",
104 [VD_AGENT_AUDIO_VOLUME_SYNC
] = "audio-volume-sync",
106 [VD_AGENT_GRAPHICS_DEVICE_INFO
] = "graphics-device-info",
110 static const char *sel_name
[] = {
111 [VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD
] = "clipboard",
112 [VD_AGENT_CLIPBOARD_SELECTION_PRIMARY
] = "primary",
113 [VD_AGENT_CLIPBOARD_SELECTION_SECONDARY
] = "secondary",
116 static const char *type_name
[] = {
117 [VD_AGENT_CLIPBOARD_NONE
] = "none",
118 [VD_AGENT_CLIPBOARD_UTF8_TEXT
] = "text",
119 [VD_AGENT_CLIPBOARD_IMAGE_PNG
] = "png",
120 [VD_AGENT_CLIPBOARD_IMAGE_BMP
] = "bmp",
121 [VD_AGENT_CLIPBOARD_IMAGE_TIFF
] = "tiff",
122 [VD_AGENT_CLIPBOARD_IMAGE_JPG
] = "jpg",
124 [VD_AGENT_CLIPBOARD_FILE_LIST
] = "files",
128 #define GET_NAME(_m, _v) \
129 (((_v) < ARRAY_SIZE(_m) && (_m[_v])) ? (_m[_v]) : "???")
131 /* ------------------------------------------------------------------ */
134 static void vdagent_send_buf(VDAgentChardev
*vd
)
138 while (!buffer_empty(&vd
->outbuf
)) {
139 len
= qemu_chr_be_can_write(CHARDEV(vd
));
143 if (len
> vd
->outbuf
.offset
) {
144 len
= vd
->outbuf
.offset
;
146 qemu_chr_be_write(CHARDEV(vd
), vd
->outbuf
.buffer
, len
);
147 buffer_advance(&vd
->outbuf
, len
);
151 static void vdagent_send_msg(VDAgentChardev
*vd
, VDAgentMessage
*msg
)
153 uint8_t *msgbuf
= (void *)msg
;
154 uint32_t msgsize
= sizeof(VDAgentMessage
) + msg
->size
;
156 VDIChunkHeader chunk
;
158 trace_vdagent_send(GET_NAME(msg_name
, msg
->type
));
160 msg
->protocol
= VD_AGENT_PROTOCOL
;
162 if (vd
->outbuf
.offset
+ msgsize
> VDAGENT_BUFFER_LIMIT
) {
163 error_report("buffer full, dropping message");
167 while (msgoff
< msgsize
) {
168 chunk
.port
= VDP_CLIENT_PORT
;
169 chunk
.size
= msgsize
- msgoff
;
170 if (chunk
.size
> 1024) {
173 buffer_reserve(&vd
->outbuf
, sizeof(chunk
) + chunk
.size
);
174 buffer_append(&vd
->outbuf
, &chunk
, sizeof(chunk
));
175 buffer_append(&vd
->outbuf
, msgbuf
+ msgoff
, chunk
.size
);
176 msgoff
+= chunk
.size
;
178 vdagent_send_buf(vd
);
181 static void vdagent_send_caps(VDAgentChardev
*vd
)
183 g_autofree VDAgentMessage
*msg
= g_malloc0(sizeof(VDAgentMessage
) +
184 sizeof(VDAgentAnnounceCapabilities
) +
186 VDAgentAnnounceCapabilities
*caps
= (void *)msg
->data
;
188 msg
->type
= VD_AGENT_ANNOUNCE_CAPABILITIES
;
189 msg
->size
= sizeof(VDAgentAnnounceCapabilities
) + sizeof(uint32_t);
191 caps
->caps
[0] |= (1 << VD_AGENT_CAP_MOUSE_STATE
);
194 caps
->caps
[0] |= (1 << VD_AGENT_CAP_CLIPBOARD_BY_DEMAND
);
195 caps
->caps
[0] |= (1 << VD_AGENT_CAP_CLIPBOARD_SELECTION
);
198 vdagent_send_msg(vd
, msg
);
201 /* ------------------------------------------------------------------ */
204 static bool have_mouse(VDAgentChardev
*vd
)
207 (vd
->caps
& (1 << VD_AGENT_CAP_MOUSE_STATE
));
210 static void vdagent_send_mouse(VDAgentChardev
*vd
)
212 g_autofree VDAgentMessage
*msg
= g_malloc0(sizeof(VDAgentMessage
) +
213 sizeof(VDAgentMouseState
));
214 VDAgentMouseState
*mouse
= (void *)msg
->data
;
216 msg
->type
= VD_AGENT_MOUSE_STATE
;
217 msg
->size
= sizeof(VDAgentMouseState
);
219 mouse
->x
= vd
->mouse_x
;
220 mouse
->y
= vd
->mouse_y
;
221 mouse
->buttons
= vd
->mouse_btn
;
222 mouse
->display_id
= vd
->mouse_display
;
224 vdagent_send_msg(vd
, msg
);
227 static void vdagent_pointer_event(DeviceState
*dev
, QemuConsole
*src
,
230 static const int bmap
[INPUT_BUTTON__MAX
] = {
231 [INPUT_BUTTON_LEFT
] = VD_AGENT_LBUTTON_MASK
,
232 [INPUT_BUTTON_RIGHT
] = VD_AGENT_RBUTTON_MASK
,
233 [INPUT_BUTTON_MIDDLE
] = VD_AGENT_MBUTTON_MASK
,
234 [INPUT_BUTTON_WHEEL_UP
] = VD_AGENT_UBUTTON_MASK
,
235 [INPUT_BUTTON_WHEEL_DOWN
] = VD_AGENT_DBUTTON_MASK
,
236 #ifdef VD_AGENT_EBUTTON_MASK
237 [INPUT_BUTTON_SIDE
] = VD_AGENT_SBUTTON_MASK
,
238 [INPUT_BUTTON_EXTRA
] = VD_AGENT_EBUTTON_MASK
,
242 VDAgentChardev
*vd
= container_of(dev
, struct VDAgentChardev
, mouse_dev
);
243 InputMoveEvent
*move
;
248 case INPUT_EVENT_KIND_ABS
:
249 move
= evt
->u
.abs
.data
;
250 xres
= qemu_console_get_width(src
, 1024);
251 yres
= qemu_console_get_height(src
, 768);
252 if (move
->axis
== INPUT_AXIS_X
) {
253 vd
->mouse_x
= qemu_input_scale_axis(move
->value
,
257 } else if (move
->axis
== INPUT_AXIS_Y
) {
258 vd
->mouse_y
= qemu_input_scale_axis(move
->value
,
263 vd
->mouse_display
= qemu_console_get_index(src
);
266 case INPUT_EVENT_KIND_BTN
:
267 btn
= evt
->u
.btn
.data
;
269 vd
->mouse_btn
|= bmap
[btn
->button
];
271 vd
->mouse_btn
&= ~bmap
[btn
->button
];
281 static void vdagent_pointer_sync(DeviceState
*dev
)
283 VDAgentChardev
*vd
= container_of(dev
, struct VDAgentChardev
, mouse_dev
);
285 if (vd
->caps
& (1 << VD_AGENT_CAP_MOUSE_STATE
)) {
286 vdagent_send_mouse(vd
);
290 static QemuInputHandler vdagent_mouse_handler
= {
291 .name
= "vdagent mouse",
292 .mask
= INPUT_EVENT_MASK_BTN
| INPUT_EVENT_MASK_ABS
,
293 .event
= vdagent_pointer_event
,
294 .sync
= vdagent_pointer_sync
,
297 /* ------------------------------------------------------------------ */
300 static bool have_clipboard(VDAgentChardev
*vd
)
302 return vd
->clipboard
&&
303 (vd
->caps
& (1 << VD_AGENT_CAP_CLIPBOARD_BY_DEMAND
));
306 static bool have_selection(VDAgentChardev
*vd
)
308 return vd
->caps
& (1 << VD_AGENT_CAP_CLIPBOARD_SELECTION
);
311 static uint32_t type_qemu_to_vdagent(enum QemuClipboardType type
)
314 case QEMU_CLIPBOARD_TYPE_TEXT
:
315 return VD_AGENT_CLIPBOARD_UTF8_TEXT
;
317 return VD_AGENT_CLIPBOARD_NONE
;
321 static void vdagent_send_clipboard_grab(VDAgentChardev
*vd
,
322 QemuClipboardInfo
*info
)
324 g_autofree VDAgentMessage
*msg
=
325 g_malloc0(sizeof(VDAgentMessage
) +
326 sizeof(uint32_t) * (QEMU_CLIPBOARD_TYPE__COUNT
+ 1));
327 uint8_t *s
= msg
->data
;
328 uint32_t *data
= (uint32_t *)msg
->data
;
331 if (have_selection(vd
)) {
332 *s
= info
->selection
;
334 msg
->size
+= sizeof(uint32_t);
335 } else if (info
->selection
!= QEMU_CLIPBOARD_SELECTION_CLIPBOARD
) {
339 for (q
= 0; q
< QEMU_CLIPBOARD_TYPE__COUNT
; q
++) {
340 type
= type_qemu_to_vdagent(q
);
341 if (type
!= VD_AGENT_CLIPBOARD_NONE
&& info
->types
[q
].available
) {
344 msg
->size
+= sizeof(uint32_t);
348 msg
->type
= VD_AGENT_CLIPBOARD_GRAB
;
349 vdagent_send_msg(vd
, msg
);
352 static void vdagent_send_clipboard_release(VDAgentChardev
*vd
,
353 QemuClipboardInfo
*info
)
355 g_autofree VDAgentMessage
*msg
= g_malloc0(sizeof(VDAgentMessage
) +
358 if (have_selection(vd
)) {
359 uint8_t *s
= msg
->data
;
360 *s
= info
->selection
;
361 msg
->size
+= sizeof(uint32_t);
362 } else if (info
->selection
!= QEMU_CLIPBOARD_SELECTION_CLIPBOARD
) {
366 msg
->type
= VD_AGENT_CLIPBOARD_RELEASE
;
367 vdagent_send_msg(vd
, msg
);
370 static void vdagent_send_clipboard_data(VDAgentChardev
*vd
,
371 QemuClipboardInfo
*info
,
372 QemuClipboardType type
)
374 g_autofree VDAgentMessage
*msg
= g_malloc0(sizeof(VDAgentMessage
) +
375 sizeof(uint32_t) * 2 +
376 info
->types
[type
].size
);
378 uint8_t *s
= msg
->data
;
379 uint32_t *data
= (uint32_t *)msg
->data
;
381 if (have_selection(vd
)) {
382 *s
= info
->selection
;
384 msg
->size
+= sizeof(uint32_t);
385 } else if (info
->selection
!= QEMU_CLIPBOARD_SELECTION_CLIPBOARD
) {
389 *data
= type_qemu_to_vdagent(type
);
391 msg
->size
+= sizeof(uint32_t);
393 memcpy(data
, info
->types
[type
].data
, info
->types
[type
].size
);
394 msg
->size
+= info
->types
[type
].size
;
396 msg
->type
= VD_AGENT_CLIPBOARD
;
397 vdagent_send_msg(vd
, msg
);
400 static void vdagent_send_empty_clipboard_data(VDAgentChardev
*vd
,
401 QemuClipboardSelection selection
,
402 QemuClipboardType type
)
404 g_autoptr(QemuClipboardInfo
) info
= qemu_clipboard_info_new(&vd
->cbpeer
, selection
);
406 trace_vdagent_send_empty_clipboard();
407 vdagent_send_clipboard_data(vd
, info
, type
);
410 static void vdagent_clipboard_notify(Notifier
*notifier
, void *data
)
412 VDAgentChardev
*vd
= container_of(notifier
, VDAgentChardev
, cbpeer
.update
);
413 QemuClipboardInfo
*info
= data
;
414 QemuClipboardSelection s
= info
->selection
;
415 QemuClipboardType type
;
416 bool self_update
= info
->owner
== &vd
->cbpeer
;
418 if (info
!= qemu_clipboard_info(s
)) {
419 vd
->cbpending
[s
] = 0;
422 vdagent_send_clipboard_grab(vd
, info
);
424 vdagent_send_clipboard_release(vd
, info
);
434 for (type
= 0; type
< QEMU_CLIPBOARD_TYPE__COUNT
; type
++) {
435 if (vd
->cbpending
[s
] & (1 << type
)) {
436 vd
->cbpending
[s
] &= ~(1 << type
);
437 vdagent_send_clipboard_data(vd
, info
, type
);
442 static void vdagent_clipboard_request(QemuClipboardInfo
*info
,
443 QemuClipboardType qtype
)
445 VDAgentChardev
*vd
= container_of(info
->owner
, VDAgentChardev
, cbpeer
);
446 g_autofree VDAgentMessage
*msg
= g_malloc0(sizeof(VDAgentMessage
) +
447 sizeof(uint32_t) * 2);
448 uint32_t type
= type_qemu_to_vdagent(qtype
);
449 uint8_t *s
= msg
->data
;
450 uint32_t *data
= (uint32_t *)msg
->data
;
452 if (type
== VD_AGENT_CLIPBOARD_NONE
) {
456 if (have_selection(vd
)) {
457 *s
= info
->selection
;
459 msg
->size
+= sizeof(uint32_t);
463 msg
->size
+= sizeof(uint32_t);
465 msg
->type
= VD_AGENT_CLIPBOARD_REQUEST
;
466 vdagent_send_msg(vd
, msg
);
469 static void vdagent_clipboard_recv_grab(VDAgentChardev
*vd
, uint8_t s
, uint32_t size
, void *data
)
471 g_autoptr(QemuClipboardInfo
) info
= NULL
;
473 trace_vdagent_cb_grab_selection(GET_NAME(sel_name
, s
));
474 info
= qemu_clipboard_info_new(&vd
->cbpeer
, s
);
475 if (size
> sizeof(uint32_t) * 10) {
477 * spice has 6 types as of 2021. Limiting to 10 entries
478 * so we we have some wiggle room.
482 while (size
>= sizeof(uint32_t)) {
483 trace_vdagent_cb_grab_type(GET_NAME(type_name
, *(uint32_t *)data
));
484 switch (*(uint32_t *)data
) {
485 case VD_AGENT_CLIPBOARD_UTF8_TEXT
:
486 info
->types
[QEMU_CLIPBOARD_TYPE_TEXT
].available
= true;
491 data
+= sizeof(uint32_t);
492 size
-= sizeof(uint32_t);
494 qemu_clipboard_update(info
);
497 static void vdagent_clipboard_recv_request(VDAgentChardev
*vd
, uint8_t s
, uint32_t size
, void *data
)
499 QemuClipboardType type
;
500 QemuClipboardInfo
*info
;
502 if (size
< sizeof(uint32_t)) {
505 switch (*(uint32_t *)data
) {
506 case VD_AGENT_CLIPBOARD_UTF8_TEXT
:
507 type
= QEMU_CLIPBOARD_TYPE_TEXT
;
513 info
= qemu_clipboard_info(s
);
514 if (info
&& info
->types
[type
].available
&& info
->owner
!= &vd
->cbpeer
) {
515 if (info
->types
[type
].data
) {
516 vdagent_send_clipboard_data(vd
, info
, type
);
518 vd
->cbpending
[s
] |= (1 << type
);
519 qemu_clipboard_request(info
, type
);
522 vdagent_send_empty_clipboard_data(vd
, s
, type
);
526 static void vdagent_clipboard_recv_data(VDAgentChardev
*vd
, uint8_t s
, uint32_t size
, void *data
)
528 QemuClipboardType type
;
530 if (size
< sizeof(uint32_t)) {
533 switch (*(uint32_t *)data
) {
534 case VD_AGENT_CLIPBOARD_UTF8_TEXT
:
535 type
= QEMU_CLIPBOARD_TYPE_TEXT
;
543 if (qemu_clipboard_peer_owns(&vd
->cbpeer
, s
)) {
544 qemu_clipboard_set_data(&vd
->cbpeer
, qemu_clipboard_info(s
),
545 type
, size
, data
, true);
549 static void vdagent_clipboard_recv_release(VDAgentChardev
*vd
, uint8_t s
)
551 qemu_clipboard_peer_release(&vd
->cbpeer
, s
);
554 static void vdagent_chr_recv_clipboard(VDAgentChardev
*vd
, VDAgentMessage
*msg
)
556 uint8_t s
= VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD
;
557 uint32_t size
= msg
->size
;
558 void *data
= msg
->data
;
560 if (have_selection(vd
)) {
564 s
= *(uint8_t *)data
;
565 if (s
>= QEMU_CLIPBOARD_SELECTION__COUNT
) {
573 case VD_AGENT_CLIPBOARD_GRAB
:
574 return vdagent_clipboard_recv_grab(vd
, s
, size
, data
);
575 case VD_AGENT_CLIPBOARD_REQUEST
:
576 return vdagent_clipboard_recv_request(vd
, s
, size
, data
);
577 case VD_AGENT_CLIPBOARD
: /* data */
578 return vdagent_clipboard_recv_data(vd
, s
, size
, data
);
579 case VD_AGENT_CLIPBOARD_RELEASE
:
580 return vdagent_clipboard_recv_release(vd
, s
);
582 g_assert_not_reached();
586 /* ------------------------------------------------------------------ */
587 /* chardev backend */
589 static void vdagent_chr_open(Chardev
*chr
,
590 ChardevBackend
*backend
,
594 VDAgentChardev
*vd
= QEMU_VDAGENT_CHARDEV(chr
);
595 ChardevQemuVDAgent
*cfg
= backend
->u
.qemu_vdagent
.data
;
597 #if defined(HOST_WORDS_BIGENDIAN)
599 * TODO: vdagent protocol is defined to be LE,
600 * so we have to byteswap everything on BE hosts.
602 error_setg(errp
, "vdagent is not supported on bigendian hosts");
606 if (migrate_add_blocker(vd
->migration_blocker
, errp
) != 0) {
610 vd
->mouse
= VDAGENT_MOUSE_DEFAULT
;
611 if (cfg
->has_mouse
) {
612 vd
->mouse
= cfg
->mouse
;
615 vd
->clipboard
= VDAGENT_CLIPBOARD_DEFAULT
;
616 if (cfg
->has_clipboard
) {
617 vd
->clipboard
= cfg
->clipboard
;
621 vd
->mouse_hs
= qemu_input_handler_register(&vd
->mouse_dev
,
622 &vdagent_mouse_handler
);
628 static void vdagent_chr_recv_caps(VDAgentChardev
*vd
, VDAgentMessage
*msg
)
630 VDAgentAnnounceCapabilities
*caps
= (void *)msg
->data
;
633 if (msg
->size
< (sizeof(VDAgentAnnounceCapabilities
) +
638 for (i
= 0; i
< ARRAY_SIZE(cap_name
); i
++) {
639 if (caps
->caps
[0] & (1 << i
)) {
640 trace_vdagent_peer_cap(GET_NAME(cap_name
, i
));
644 vd
->caps
= caps
->caps
[0];
646 vdagent_send_caps(vd
);
648 if (have_mouse(vd
) && vd
->mouse_hs
) {
649 qemu_input_handler_activate(vd
->mouse_hs
);
651 if (have_clipboard(vd
) && vd
->cbpeer
.update
.notify
== NULL
) {
652 vd
->cbpeer
.name
= "vdagent";
653 vd
->cbpeer
.update
.notify
= vdagent_clipboard_notify
;
654 vd
->cbpeer
.request
= vdagent_clipboard_request
;
655 qemu_clipboard_peer_register(&vd
->cbpeer
);
659 static void vdagent_chr_recv_msg(VDAgentChardev
*vd
, VDAgentMessage
*msg
)
661 trace_vdagent_recv_msg(GET_NAME(msg_name
, msg
->type
), msg
->size
);
664 case VD_AGENT_ANNOUNCE_CAPABILITIES
:
665 vdagent_chr_recv_caps(vd
, msg
);
667 case VD_AGENT_CLIPBOARD
:
668 case VD_AGENT_CLIPBOARD_GRAB
:
669 case VD_AGENT_CLIPBOARD_REQUEST
:
670 case VD_AGENT_CLIPBOARD_RELEASE
:
671 if (have_clipboard(vd
)) {
672 vdagent_chr_recv_clipboard(vd
, msg
);
680 static void vdagent_reset_xbuf(VDAgentChardev
*vd
)
682 g_clear_pointer(&vd
->xbuf
, g_free
);
687 static void vdagent_chr_recv_chunk(VDAgentChardev
*vd
)
689 VDAgentMessage
*msg
= (void *)vd
->msgbuf
;
692 if (vd
->msgsize
< sizeof(*msg
)) {
693 error_report("%s: message too small: %d < %zd", __func__
,
694 vd
->msgsize
, sizeof(*msg
));
697 if (vd
->msgsize
== msg
->size
+ sizeof(*msg
)) {
698 vdagent_chr_recv_msg(vd
, msg
);
704 vd
->xsize
= msg
->size
+ sizeof(*msg
);
705 vd
->xbuf
= g_malloc0(vd
->xsize
);
708 if (vd
->xoff
+ vd
->msgsize
> vd
->xsize
) {
709 error_report("%s: Oops: %d+%d > %d", __func__
,
710 vd
->xoff
, vd
->msgsize
, vd
->xsize
);
711 vdagent_reset_xbuf(vd
);
715 memcpy(vd
->xbuf
+ vd
->xoff
, vd
->msgbuf
, vd
->msgsize
);
716 vd
->xoff
+= vd
->msgsize
;
717 if (vd
->xoff
< vd
->xsize
) {
721 msg
= (void *)vd
->xbuf
;
722 vdagent_chr_recv_msg(vd
, msg
);
723 vdagent_reset_xbuf(vd
);
726 static void vdagent_reset_bufs(VDAgentChardev
*vd
)
728 memset(&vd
->chunk
, 0, sizeof(vd
->chunk
));
735 static int vdagent_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
737 VDAgentChardev
*vd
= QEMU_VDAGENT_CHARDEV(chr
);
738 uint32_t copy
, ret
= len
;
741 if (vd
->chunksize
< sizeof(vd
->chunk
)) {
742 copy
= sizeof(vd
->chunk
) - vd
->chunksize
;
746 memcpy((void *)(&vd
->chunk
) + vd
->chunksize
, buf
, copy
);
747 vd
->chunksize
+= copy
;
750 if (vd
->chunksize
< sizeof(vd
->chunk
)) {
754 assert(vd
->msgbuf
== NULL
);
755 vd
->msgbuf
= g_malloc0(vd
->chunk
.size
);
758 copy
= vd
->chunk
.size
- vd
->msgsize
;
762 memcpy(vd
->msgbuf
+ vd
->msgsize
, buf
, copy
);
767 if (vd
->msgsize
== vd
->chunk
.size
) {
768 trace_vdagent_recv_chunk(vd
->chunk
.size
);
769 vdagent_chr_recv_chunk(vd
);
770 vdagent_reset_bufs(vd
);
777 static void vdagent_chr_accept_input(Chardev
*chr
)
779 VDAgentChardev
*vd
= QEMU_VDAGENT_CHARDEV(chr
);
781 vdagent_send_buf(vd
);
784 static void vdagent_disconnect(VDAgentChardev
*vd
)
786 buffer_reset(&vd
->outbuf
);
787 vdagent_reset_bufs(vd
);
790 qemu_input_handler_deactivate(vd
->mouse_hs
);
792 if (vd
->cbpeer
.update
.notify
) {
793 qemu_clipboard_peer_unregister(&vd
->cbpeer
);
794 memset(&vd
->cbpeer
, 0, sizeof(vd
->cbpeer
));
798 static void vdagent_chr_set_fe_open(struct Chardev
*chr
, int fe_open
)
800 VDAgentChardev
*vd
= QEMU_VDAGENT_CHARDEV(chr
);
803 trace_vdagent_close();
804 vdagent_disconnect(vd
);
808 trace_vdagent_open();
811 static void vdagent_chr_parse(QemuOpts
*opts
, ChardevBackend
*backend
,
814 ChardevQemuVDAgent
*cfg
;
816 backend
->type
= CHARDEV_BACKEND_KIND_QEMU_VDAGENT
;
817 cfg
= backend
->u
.qemu_vdagent
.data
= g_new0(ChardevQemuVDAgent
, 1);
818 qemu_chr_parse_common(opts
, qapi_ChardevQemuVDAgent_base(cfg
));
819 cfg
->has_mouse
= true;
820 cfg
->mouse
= qemu_opt_get_bool(opts
, "mouse", VDAGENT_MOUSE_DEFAULT
);
821 cfg
->has_clipboard
= true;
822 cfg
->clipboard
= qemu_opt_get_bool(opts
, "clipboard", VDAGENT_CLIPBOARD_DEFAULT
);
825 /* ------------------------------------------------------------------ */
827 static void vdagent_chr_class_init(ObjectClass
*oc
, void *data
)
829 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
831 cc
->parse
= vdagent_chr_parse
;
832 cc
->open
= vdagent_chr_open
;
833 cc
->chr_write
= vdagent_chr_write
;
834 cc
->chr_set_fe_open
= vdagent_chr_set_fe_open
;
835 cc
->chr_accept_input
= vdagent_chr_accept_input
;
838 static void vdagent_chr_init(Object
*obj
)
840 VDAgentChardev
*vd
= QEMU_VDAGENT_CHARDEV(obj
);
842 buffer_init(&vd
->outbuf
, "vdagent-outbuf");
843 error_setg(&vd
->migration_blocker
,
844 "The vdagent chardev doesn't yet support migration");
847 static void vdagent_chr_fini(Object
*obj
)
849 VDAgentChardev
*vd
= QEMU_VDAGENT_CHARDEV(obj
);
851 migrate_del_blocker(vd
->migration_blocker
);
852 vdagent_disconnect(vd
);
853 buffer_free(&vd
->outbuf
);
854 error_free(vd
->migration_blocker
);
857 static const TypeInfo vdagent_chr_type_info
= {
858 .name
= TYPE_CHARDEV_QEMU_VDAGENT
,
859 .parent
= TYPE_CHARDEV
,
860 .instance_size
= sizeof(VDAgentChardev
),
861 .instance_init
= vdagent_chr_init
,
862 .instance_finalize
= vdagent_chr_fini
,
863 .class_init
= vdagent_chr_class_init
,
866 static void register_types(void)
868 type_register_static(&vdagent_chr_type_info
);
871 type_init(register_types
);