2 * QEMU VNC display driver -- clipboard support
4 * Copyright (C) 2021 Gerd Hoffmann <kraxel@redhat.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
30 static uint8_t *inflate_buffer(uint8_t *in
, uint32_t in_len
, uint32_t *size
)
39 uint8_t *out
= g_malloc(out_len
);
42 stream
.next_out
= out
+ stream
.total_out
;
43 stream
.avail_out
= out_len
- stream
.total_out
;
45 ret
= inflateInit(&stream
);
50 while (stream
.avail_in
) {
51 ret
= inflate(&stream
, Z_FINISH
);
58 if (out_len
> (1 << 20)) {
61 out
= g_realloc(out
, out_len
);
62 stream
.next_out
= out
+ stream
.total_out
;
63 stream
.avail_out
= out_len
- stream
.total_out
;
70 *size
= stream
.total_out
;
82 static uint8_t *deflate_buffer(uint8_t *in
, uint32_t in_len
, uint32_t *size
)
91 uint8_t *out
= g_malloc(out_len
);
94 stream
.next_out
= out
+ stream
.total_out
;
95 stream
.avail_out
= out_len
- stream
.total_out
;
97 ret
= deflateInit(&stream
, Z_DEFAULT_COMPRESSION
);
102 while (ret
!= Z_STREAM_END
) {
103 ret
= deflate(&stream
, Z_FINISH
);
110 if (out_len
> (1 << 20)) {
113 out
= g_realloc(out
, out_len
);
114 stream
.next_out
= out
+ stream
.total_out
;
115 stream
.avail_out
= out_len
- stream
.total_out
;
122 *size
= stream
.total_out
;
134 static void vnc_clipboard_send(VncState
*vs
, uint32_t count
, uint32_t *dwords
)
139 vnc_write_u8(vs
, VNC_MSG_SERVER_CUT_TEXT
);
143 vnc_write_s32(vs
, -(count
* sizeof(uint32_t))); /* -(message length) */
144 for (i
= 0; i
< count
; i
++) {
145 vnc_write_u32(vs
, dwords
[i
]);
147 vnc_unlock_output(vs
);
151 static void vnc_clipboard_provide(VncState
*vs
,
152 QemuClipboardInfo
*info
,
153 QemuClipboardType type
)
156 g_autofree
uint8_t *buf
= NULL
;
157 g_autofree
void *zbuf
= NULL
;
161 case QEMU_CLIPBOARD_TYPE_TEXT
:
162 flags
|= VNC_CLIPBOARD_TEXT
;
167 flags
|= VNC_CLIPBOARD_PROVIDE
;
169 buf
= g_malloc(info
->types
[type
].size
+ 4);
170 buf
[0] = (info
->types
[type
].size
>> 24) & 0xff;
171 buf
[1] = (info
->types
[type
].size
>> 16) & 0xff;
172 buf
[2] = (info
->types
[type
].size
>> 8) & 0xff;
173 buf
[3] = (info
->types
[type
].size
>> 0) & 0xff;
174 memcpy(buf
+ 4, info
->types
[type
].data
, info
->types
[type
].size
);
175 zbuf
= deflate_buffer(buf
, info
->types
[type
].size
+ 4, &zsize
);
181 vnc_write_u8(vs
, VNC_MSG_SERVER_CUT_TEXT
);
185 vnc_write_s32(vs
, -(sizeof(uint32_t) + zsize
)); /* -(message length) */
186 vnc_write_u32(vs
, flags
);
187 vnc_write(vs
, zbuf
, zsize
);
188 vnc_unlock_output(vs
);
192 static void vnc_clipboard_notify(Notifier
*notifier
, void *data
)
194 VncState
*vs
= container_of(notifier
, VncState
, cbpeer
.update
);
195 QemuClipboardInfo
*info
= data
;
196 QemuClipboardType type
;
197 bool self_update
= info
->owner
== &vs
->cbpeer
;
200 if (info
!= vs
->cbinfo
) {
201 qemu_clipboard_info_unref(vs
->cbinfo
);
202 vs
->cbinfo
= qemu_clipboard_info_ref(info
);
205 if (info
->types
[QEMU_CLIPBOARD_TYPE_TEXT
].available
) {
206 flags
|= VNC_CLIPBOARD_TEXT
;
208 flags
|= VNC_CLIPBOARD_NOTIFY
;
209 vnc_clipboard_send(vs
, 1, &flags
);
218 for (type
= 0; type
< QEMU_CLIPBOARD_TYPE__COUNT
; type
++) {
219 if (vs
->cbpending
& (1 << type
)) {
220 vs
->cbpending
&= ~(1 << type
);
221 vnc_clipboard_provide(vs
, info
, type
);
226 static void vnc_clipboard_request(QemuClipboardInfo
*info
,
227 QemuClipboardType type
)
229 VncState
*vs
= container_of(info
->owner
, VncState
, cbpeer
);
232 if (type
== QEMU_CLIPBOARD_TYPE_TEXT
) {
233 flags
|= VNC_CLIPBOARD_TEXT
;
238 flags
|= VNC_CLIPBOARD_REQUEST
;
240 vnc_clipboard_send(vs
, 1, &flags
);
243 void vnc_client_cut_text_ext(VncState
*vs
, int32_t len
, uint32_t flags
, uint8_t *data
)
245 if (flags
& VNC_CLIPBOARD_CAPS
) {
246 /* need store caps somewhere ? */
250 if (flags
& VNC_CLIPBOARD_NOTIFY
) {
251 QemuClipboardInfo
*info
=
252 qemu_clipboard_info_new(&vs
->cbpeer
, QEMU_CLIPBOARD_SELECTION_CLIPBOARD
);
253 if (flags
& VNC_CLIPBOARD_TEXT
) {
254 info
->types
[QEMU_CLIPBOARD_TYPE_TEXT
].available
= true;
256 qemu_clipboard_update(info
);
257 qemu_clipboard_info_unref(info
);
261 if (flags
& VNC_CLIPBOARD_PROVIDE
&&
263 vs
->cbinfo
->owner
== &vs
->cbpeer
) {
265 g_autofree
uint8_t *buf
= inflate_buffer(data
, len
- 4, &size
);
266 if ((flags
& VNC_CLIPBOARD_TEXT
) &&
268 uint32_t tsize
= read_u32(buf
, 0);
269 uint8_t *tbuf
= buf
+ 4;
271 qemu_clipboard_set_data(&vs
->cbpeer
, vs
->cbinfo
,
272 QEMU_CLIPBOARD_TYPE_TEXT
,
278 if (flags
& VNC_CLIPBOARD_REQUEST
&&
280 vs
->cbinfo
->owner
!= &vs
->cbpeer
) {
281 if ((flags
& VNC_CLIPBOARD_TEXT
) &&
282 vs
->cbinfo
->types
[QEMU_CLIPBOARD_TYPE_TEXT
].available
) {
283 if (vs
->cbinfo
->types
[QEMU_CLIPBOARD_TYPE_TEXT
].data
) {
284 vnc_clipboard_provide(vs
, vs
->cbinfo
, QEMU_CLIPBOARD_TYPE_TEXT
);
286 vs
->cbpending
|= (1 << QEMU_CLIPBOARD_TYPE_TEXT
);
287 qemu_clipboard_request(vs
->cbinfo
, QEMU_CLIPBOARD_TYPE_TEXT
);
293 void vnc_client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
295 QemuClipboardInfo
*info
=
296 qemu_clipboard_info_new(&vs
->cbpeer
, QEMU_CLIPBOARD_SELECTION_CLIPBOARD
);
298 qemu_clipboard_set_data(&vs
->cbpeer
, info
, QEMU_CLIPBOARD_TYPE_TEXT
,
300 qemu_clipboard_info_unref(info
);
303 void vnc_server_cut_text_caps(VncState
*vs
)
307 if (!vnc_has_feature(vs
, VNC_FEATURE_CLIPBOARD_EXT
)) {
311 caps
[0] = (VNC_CLIPBOARD_PROVIDE
|
312 VNC_CLIPBOARD_NOTIFY
|
313 VNC_CLIPBOARD_REQUEST
|
317 vnc_clipboard_send(vs
, 2, caps
);
319 vs
->cbpeer
.name
= "vnc";
320 vs
->cbpeer
.update
.notify
= vnc_clipboard_notify
;
321 vs
->cbpeer
.request
= vnc_clipboard_request
;
322 qemu_clipboard_peer_register(&vs
->cbpeer
);