From nobody Mon Feb 9 16:44:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1613741162253318.8081076342862; Fri, 19 Feb 2021 05:26:02 -0800 (PST) Received: from localhost ([::1]:52414 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lD5nF-0004wH-3O for importer@patchew.org; Fri, 19 Feb 2021 08:26:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:41532) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lD5bu-0004On-SV for qemu-devel@nongnu.org; Fri, 19 Feb 2021 08:14:18 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:60610) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lD5bp-0006VF-3e for qemu-devel@nongnu.org; Fri, 19 Feb 2021 08:14:18 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-425-M5sfoLuYNkaIRKBoajwT-w-1; Fri, 19 Feb 2021 08:14:10 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4D73A835E22 for ; Fri, 19 Feb 2021 13:14:09 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-114-184.ams2.redhat.com [10.36.114.184]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CA6B25D734; Fri, 19 Feb 2021 13:13:59 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 42CE71800864; Fri, 19 Feb 2021 14:13:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613740452; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vaiE+Xf/eoTkNQKfw4Lrn5Cuiui55zVMkTSPMXKhW+w=; b=MjmTR0AvBk7SbQJAeSxGlM8sDfHl8T69ZONEYVTuIoYe37lqaSdHMClSPjoQjecdIA5O2Q 9+oJ6eMt1oigZlU/2F0BRp6nupSLreVu6ryiSl0VyWqxcbmr5nZf713edgQ5azVDFISUkm vLQJD3vaCOU7I+EjrqDAtgmRy0TcKMg= X-MC-Unique: M5sfoLuYNkaIRKBoajwT-w-1 From: Gerd Hoffmann To: qemu-devel@nongnu.org Subject: [PATCH 5/7] ui/vnc: clipboard support Date: Fri, 19 Feb 2021 14:13:47 +0100 Message-Id: <20210219131349.3993192-6-kraxel@redhat.com> In-Reply-To: <20210219131349.3993192-1-kraxel@redhat.com> References: <20210219131349.3993192-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kraxel@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=kraxel@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Markus Armbruster Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch adds support for cut+paste to the qemu vnc server, which allows the vnc client exchange clipbaord data with qemu and other peers like the qemu vdagent implementation. Signed-off-by: Gerd Hoffmann --- ui/vnc.h | 24 ++++ ui/vnc-clipboard.c | 326 +++++++++++++++++++++++++++++++++++++++++++++ ui/vnc.c | 20 ++- ui/meson.build | 1 + 4 files changed, 365 insertions(+), 6 deletions(-) create mode 100644 ui/vnc-clipboard.c diff --git a/ui/vnc.h b/ui/vnc.h index 116463d5f099..f611223859ae 100644 --- a/ui/vnc.h +++ b/ui/vnc.h @@ -29,6 +29,7 @@ =20 #include "qemu/queue.h" #include "qemu/thread.h" +#include "ui/clipboard.h" #include "ui/console.h" #include "audio/audio.h" #include "qemu/bitmap.h" @@ -347,6 +348,10 @@ struct VncState =20 Notifier mouse_mode_notifier; =20 + QemuClipboardPeer cbpeer; + QemuClipboardInfo *cbinfo; + uint32_t cbpending; + QTAILQ_ENTRY(VncState) next; }; =20 @@ -416,6 +421,7 @@ enum { #define VNC_ENCODING_XVP 0XFFFFFECB /* -309 */ #define VNC_ENCODING_ALPHA_CURSOR 0XFFFFFEC6 /* -314 */ #define VNC_ENCODING_WMVi 0x574D5669 +#define VNC_ENCODING_CLIPBOARD_EXT 0xc0a1e5ce =20 /*************************************************************************= **** * @@ -457,6 +463,7 @@ enum VncFeatures { VNC_FEATURE_ZYWRLE, VNC_FEATURE_LED_STATE, VNC_FEATURE_XVP, + VNC_FEATURE_CLIPBOARD_EXT, }; =20 #define VNC_FEATURE_RESIZE_MASK (1 << VNC_FEATURE_RESIZE) @@ -473,6 +480,7 @@ enum VncFeatures { #define VNC_FEATURE_ZYWRLE_MASK (1 << VNC_FEATURE_ZYWRLE) #define VNC_FEATURE_LED_STATE_MASK (1 << VNC_FEATURE_LED_STATE) #define VNC_FEATURE_XVP_MASK (1 << VNC_FEATURE_XVP) +#define VNC_FEATURE_CLIPBOARD_EXT_MASK (1 << VNC_FEATURE_CLIPBOARD_= EXT) =20 =20 /* Client -> Server message IDs */ @@ -534,6 +542,17 @@ enum VncFeatures { #define VNC_XVP_ACTION_REBOOT 3 #define VNC_XVP_ACTION_RESET 4 =20 +/* extended clipboard flags */ +#define VNC_CLIPBOARD_TEXT (1 << 0) +#define VNC_CLIPBOARD_RTF (1 << 1) +#define VNC_CLIPBOARD_HTML (1 << 2) +#define VNC_CLIPBOARD_DIB (1 << 3) +#define VNC_CLIPBOARD_FILES (1 << 4) +#define VNC_CLIPBOARD_CAPS (1 << 24) +#define VNC_CLIPBOARD_REQUEST (1 << 25) +#define VNC_CLIPBOARD_PEEK (1 << 26) +#define VNC_CLIPBOARD_NOTIFY (1 << 27) +#define VNC_CLIPBOARD_PROVIDE (1 << 28) =20 /*************************************************************************= **** * @@ -617,4 +636,9 @@ int vnc_zrle_send_framebuffer_update(VncState *vs, int = x, int y, int w, int h); int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, = int h); void vnc_zrle_clear(VncState *vs); =20 +/* vnc-clipboard.c */ +void vnc_server_cut_text_caps(VncState *vs); +void vnc_client_cut_text(VncState *vs, size_t len, uint8_t *text); +void vnc_client_cut_text_ext(VncState *vs, int32_t len, uint32_t flags, ui= nt8_t *data); + #endif /* QEMU_VNC_H */ diff --git a/ui/vnc-clipboard.c b/ui/vnc-clipboard.c new file mode 100644 index 000000000000..e729120ba360 --- /dev/null +++ b/ui/vnc-clipboard.c @@ -0,0 +1,326 @@ +/* + * QEMU VNC display driver -- clipboard support + * + * Copyright (C) 2021 Gerd Hoffmann + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "vnc.h" +#include "vnc-jobs.h" + +static uint8_t *inflate_buffer(uint8_t *in, uint32_t in_len, uint32_t *siz= e) +{ + z_stream stream =3D { + .next_in =3D in, + .avail_in =3D in_len, + .zalloc =3D Z_NULL, + .zfree =3D Z_NULL, + }; + uint32_t out_len =3D 8; + uint8_t *out =3D g_malloc(out_len); + int ret; + + stream.next_out =3D out + stream.total_out; + stream.avail_out =3D out_len - stream.total_out; + + ret =3D inflateInit(&stream); + if (ret !=3D Z_OK) { + goto err; + } + + while (stream.avail_in) { + ret =3D inflate(&stream, Z_FINISH); + switch (ret) { + case Z_OK: + case Z_STREAM_END: + break; + case Z_BUF_ERROR: + out_len <<=3D 1; + if (out_len > (1 << 20)) { + goto err_end; + } + out =3D g_realloc(out, out_len); + stream.next_out =3D out + stream.total_out; + stream.avail_out =3D out_len - stream.total_out; + break; + default: + goto err_end; + } + } + + *size =3D stream.total_out; + inflateEnd(&stream); + + return out; + +err_end: + inflateEnd(&stream); +err: + g_free(out); + return NULL; +} + +static uint8_t *deflate_buffer(uint8_t *in, uint32_t in_len, uint32_t *siz= e) +{ + z_stream stream =3D { + .next_in =3D in, + .avail_in =3D in_len, + .zalloc =3D Z_NULL, + .zfree =3D Z_NULL, + }; + uint32_t out_len =3D 8; + uint8_t *out =3D g_malloc(out_len); + int ret; + + stream.next_out =3D out + stream.total_out; + stream.avail_out =3D out_len - stream.total_out; + + ret =3D deflateInit(&stream, Z_DEFAULT_COMPRESSION); + if (ret !=3D Z_OK) { + goto err; + } + + while (ret !=3D Z_STREAM_END) { + ret =3D deflate(&stream, Z_FINISH); + switch (ret) { + case Z_OK: + case Z_STREAM_END: + break; + case Z_BUF_ERROR: + out_len <<=3D 1; + if (out_len > (1 << 20)) { + goto err_end; + } + out =3D g_realloc(out, out_len); + stream.next_out =3D out + stream.total_out; + stream.avail_out =3D out_len - stream.total_out; + break; + default: + goto err_end; + } + } + + *size =3D stream.total_out; + deflateEnd(&stream); + + return out; + +err_end: + deflateEnd(&stream); +err: + g_free(out); + return NULL; +} + +static void vnc_clipboard_send(VncState *vs, uint32_t count, uint32_t *dwo= rds) +{ + int i; + + vnc_lock_output(vs); + vnc_write_u8(vs, VNC_MSG_SERVER_CUT_TEXT); + vnc_write_u8(vs, 0); + vnc_write_u8(vs, 0); + vnc_write_u8(vs, 0); + vnc_write_s32(vs, -(count * sizeof(uint32_t))); /* -(message length) = */ + for (i =3D 0; i < count; i++) { + vnc_write_u32(vs, dwords[i]); + } + vnc_unlock_output(vs); + vnc_flush(vs); +} + +static void vnc_clipboard_provide(VncState *vs, + QemuClipboardInfo *info, + QemuClipboardType type) +{ + uint32_t flags =3D 0; + uint8_t *buf; + void *zbuf; + uint32_t zsize; + + switch (type) { + case QEMU_CLIPBOARD_TYPE_TEXT: + flags |=3D VNC_CLIPBOARD_TEXT; + break; + default: + return; + } + flags |=3D VNC_CLIPBOARD_PROVIDE; + + buf =3D g_malloc(info->types[type].size + 4); + buf[0] =3D (info->types[type].size >> 24) & 0xff; + buf[1] =3D (info->types[type].size >> 16) & 0xff; + buf[2] =3D (info->types[type].size >> 8) & 0xff; + buf[3] =3D (info->types[type].size >> 0) & 0xff; + memcpy(buf + 4, info->types[type].data, info->types[type].size); + zbuf =3D deflate_buffer(buf, info->types[type].size + 4, &zsize); + g_free(buf); + + if (!zbuf) { + return; + } + + vnc_lock_output(vs); + vnc_write_u8(vs, VNC_MSG_SERVER_CUT_TEXT); + vnc_write_u8(vs, 0); + vnc_write_u8(vs, 0); + vnc_write_u8(vs, 0); + vnc_write_s32(vs, -(sizeof(uint32_t) + zsize)); /* -(message length) = */ + vnc_write_u32(vs, flags); + vnc_write(vs, zbuf, zsize); + vnc_unlock_output(vs); + vnc_flush(vs); +} + +static void vnc_clipboard_notify(Notifier *notifier, void *data) +{ + VncState *vs =3D container_of(notifier, VncState, cbpeer.update); + QemuClipboardInfo *info =3D data; + QemuClipboardType type; + bool self_update =3D info->owner =3D=3D &vs->cbpeer; + uint32_t flags =3D 0; + + if (info !=3D vs->cbinfo) { + qemu_clipboard_info_put(vs->cbinfo); + vs->cbinfo =3D qemu_clipboard_info_get(info); + vs->cbpending =3D 0; + if (!self_update) { + if (info->types[QEMU_CLIPBOARD_TYPE_TEXT].available) { + flags |=3D VNC_CLIPBOARD_TEXT; + } + flags |=3D VNC_CLIPBOARD_NOTIFY; + vnc_clipboard_send(vs, 1, &flags); + } + return; + } + + if (self_update) { + return; + } + + for (type =3D 0; type < QEMU_CLIPBOARD_TYPE__COUNT; type++) { + if (vs->cbpending & (1 << type)) { + vs->cbpending &=3D ~(1 << type); + vnc_clipboard_provide(vs, info, type); + } + } +} + +static void vnc_clipboard_request(QemuClipboardInfo *info, + QemuClipboardType type) +{ + VncState *vs =3D container_of(info->owner, VncState, cbpeer); + uint32_t flags =3D 0; + + if (type =3D=3D QEMU_CLIPBOARD_TYPE_TEXT) { + flags |=3D VNC_CLIPBOARD_TEXT; + } + if (!flags) { + return; + } + flags |=3D VNC_CLIPBOARD_REQUEST; + + vnc_clipboard_send(vs, 1, &flags); +} + +void vnc_client_cut_text_ext(VncState *vs, int32_t len, uint32_t flags, ui= nt8_t *data) +{ + if (flags & VNC_CLIPBOARD_CAPS) { + /* need store caps somewhere ? */ + return; + } + + if (flags & VNC_CLIPBOARD_NOTIFY) { + QemuClipboardInfo *info =3D + qemu_clipboard_info_new(&vs->cbpeer, QEMU_CLIPBOARD_SELECTION_= CLIPBOARD); + if (flags & VNC_CLIPBOARD_TEXT) { + info->types[QEMU_CLIPBOARD_TYPE_TEXT].available =3D true; + } + qemu_clipboard_update(info); + qemu_clipboard_info_put(info); + return; + } + + if (flags & VNC_CLIPBOARD_PROVIDE && + vs->cbinfo && + vs->cbinfo->owner =3D=3D &vs->cbpeer) { + uint32_t size =3D 0; + uint8_t *buf =3D inflate_buffer(data, len - 4, &size); + if ((flags & VNC_CLIPBOARD_TEXT) && + buf && size >=3D 4) { + uint32_t tsize =3D read_u32(buf, 0); + uint8_t *tbuf =3D buf + 4; + if (tsize < size) { + qemu_clipboard_set_data(&vs->cbpeer, vs->cbinfo, + QEMU_CLIPBOARD_TYPE_TEXT, + tsize, tbuf, true); + } + } + g_free(buf); + } + + if (flags & VNC_CLIPBOARD_REQUEST && + vs->cbinfo && + vs->cbinfo->owner !=3D &vs->cbpeer) { + if ((flags & VNC_CLIPBOARD_TEXT) && + vs->cbinfo->types[QEMU_CLIPBOARD_TYPE_TEXT].available) { + if (vs->cbinfo->types[QEMU_CLIPBOARD_TYPE_TEXT].data) { + vnc_clipboard_provide(vs, vs->cbinfo, QEMU_CLIPBOARD_TYPE_= TEXT); + } else { + vs->cbpending |=3D (1 << QEMU_CLIPBOARD_TYPE_TEXT); + qemu_clipboard_request(vs->cbinfo, QEMU_CLIPBOARD_TYPE_TEX= T); + } + } + } +} + +void vnc_client_cut_text(VncState *vs, size_t len, uint8_t *text) +{ + QemuClipboardInfo *info =3D + qemu_clipboard_info_new(&vs->cbpeer, QEMU_CLIPBOARD_SELECTION_CLIP= BOARD); + + qemu_clipboard_set_data(&vs->cbpeer, info, QEMU_CLIPBOARD_TYPE_TEXT, + len, text, true); + qemu_clipboard_info_put(info); +} + +void vnc_server_cut_text_caps(VncState *vs) +{ + uint32_t caps[2]; + + if (!vnc_has_feature(vs, VNC_FEATURE_CLIPBOARD_EXT)) { + return; + } + + caps[0] =3D (VNC_CLIPBOARD_PROVIDE | + VNC_CLIPBOARD_NOTIFY | + VNC_CLIPBOARD_REQUEST | + VNC_CLIPBOARD_CAPS | + VNC_CLIPBOARD_TEXT); + caps[1] =3D 0; + vnc_clipboard_send(vs, 2, caps); + + vs->cbpeer.name =3D "vnc"; + vs->cbpeer.update.notify =3D vnc_clipboard_notify; + vs->cbpeer.request =3D vnc_clipboard_request; + qemu_clipboard_peer_register(&vs->cbpeer); +} diff --git a/ui/vnc.c b/ui/vnc.c index 16bb3be770b2..91ec51c7c67d 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -25,6 +25,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu-common.h" #include "vnc.h" #include "vnc-jobs.h" #include "trace.h" @@ -1309,6 +1310,9 @@ void vnc_disconnect_finish(VncState *vs) /* last client gone */ vnc_update_server_surface(vs->vd); } + if (vs->cbpeer.update.notify) { + qemu_clipboard_peer_unregister(&vs->cbpeer); + } =20 vnc_unlock_output(vs); =20 @@ -1734,10 +1738,6 @@ uint32_t read_u32(uint8_t *data, size_t offset) (data[offset + 2] << 8) | data[offset + 3]); } =20 -static void client_cut_text(VncState *vs, size_t len, uint8_t *text) -{ -} - static void check_pointer_type_change(Notifier *notifier, void *data) { VncState *vs =3D container_of(notifier, VncState, mouse_mode_notifier); @@ -2179,6 +2179,10 @@ static void set_encodings(VncState *vs, int32_t *enc= odings, size_t n_encodings) send_xvp_message(vs, VNC_XVP_CODE_INIT); } break; + case VNC_ENCODING_CLIPBOARD_EXT: + vs->features |=3D VNC_FEATURE_CLIPBOARD_EXT_MASK; + vnc_server_cut_text_caps(vs); + break; case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 += 9: vs->tight->compression =3D (enc & 0x0F); break; @@ -2395,7 +2399,7 @@ static int protocol_client_msg(VncState *vs, uint8_t = *data, size_t len) return 8; } if (len =3D=3D 8) { - uint32_t dlen =3D read_u32(data, 4); + uint32_t dlen =3D abs(read_s32(data, 4)); if (dlen > (1 << 20)) { error_report("vnc: client_cut_text msg payload has %u byte= s" " which exceeds our limit of 1MB.", dlen); @@ -2407,7 +2411,11 @@ static int protocol_client_msg(VncState *vs, uint8_t= *data, size_t len) } } =20 - client_cut_text(vs, read_u32(data, 4), data + 8); + if (read_s32(data, 4) < 0) { + vnc_client_cut_text_ext(vs, abs(read_s32(data, 4)), read_u32(d= ata, 8), data + 12); + break; + } + vnc_client_cut_text(vs, read_u32(data, 4), data + 8); break; case VNC_MSG_CLIENT_XVP: if (!(vs->features & VNC_FEATURE_XVP)) { diff --git a/ui/meson.build b/ui/meson.build index 08447ac15c5e..a98f89b48978 100644 --- a/ui/meson.build +++ b/ui/meson.build @@ -30,6 +30,7 @@ vnc_ss.add(files( 'vnc-auth-vencrypt.c', 'vnc-ws.c', 'vnc-jobs.c', + 'vnc-clipboard.c', )) vnc_ss.add(zlib, png, jpeg, gnutls) vnc_ss.add(when: sasl, if_true: files('vnc-auth-sasl.c')) --=20 2.29.2