{"id":1571489,"url":"http://patchwork.ozlabs.org/api/patches/1571489/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20211221065855.142578-31-marcandre.lureau@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20211221065855.142578-31-marcandre.lureau@redhat.com>","list_archive_url":null,"date":"2021-12-21T06:58:49","name":"[PULL,v2,30/36] ui/dbus: add clipboard interface","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"3964601dd134964dc4e0423f1ab037dde43c9658","submitter":{"id":66774,"url":"http://patchwork.ozlabs.org/api/people/66774/?format=json","name":"Marc-André Lureau","email":"marcandre.lureau@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20211221065855.142578-31-marcandre.lureau@redhat.com/mbox/","series":[{"id":277865,"url":"http://patchwork.ozlabs.org/api/series/277865/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=277865","date":"2021-12-21T06:58:19","name":"[PULL,v2,01/36] ui/vdagent: add CHECK_SPICE_PROTOCOL_VERSION","version":2,"mbox":"http://patchwork.ozlabs.org/series/277865/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/1571489/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/1571489/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["bilbo.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=ZtTR68bR;\n\tdkim-atps=neutral","ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=<UNKNOWN>)","relay.mimecast.com;\n auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=marcandre.lureau@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby bilbo.ozlabs.org (Postfix) with ESMTPS id 4JJ7f130Wxz9s3q\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 21 Dec 2021 18:40:09 +1100 (AEDT)","from localhost ([::1]:47182 helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1mzZkl-0003vh-3J\n\tfor incoming@patchwork.ozlabs.org; Tue, 21 Dec 2021 02:40:07 -0500","from eggs.gnu.org ([209.51.188.92]:59222)\n by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <marcandre.lureau@redhat.com>)\n id 1mzZFg-0002oO-JP\n for qemu-devel@nongnu.org; Tue, 21 Dec 2021 02:08:04 -0500","from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:43278)\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <marcandre.lureau@redhat.com>)\n id 1mzZFc-0002Wd-SR\n for qemu-devel@nongnu.org; Tue, 21 Dec 2021 02:07:59 -0500","from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com\n [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id\n us-mta-135-UyBT3bfuOX-P4wl-Xm3bWg-1; Tue, 21 Dec 2021 02:07:42 -0500","from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com\n [10.5.11.13])\n (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n (No client certificate requested)\n by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A00681006AA6;\n Tue, 21 Dec 2021 07:07:41 +0000 (UTC)","from localhost (unknown [10.39.208.37])\n by smtp.corp.redhat.com (Postfix) with ESMTP id 8143E87945;\n Tue, 21 Dec 2021 07:07:32 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1640070469;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=5hiUfx+gBCUbUp8y/68Tfn2aLA/jeDrE7SScd4mmGms=;\n b=ZtTR68bRcGlB5PJQmp7ntVsPHA0F+05/VHv0wmC9TkH3ABCt2eYYOkVCQ482NL5WObIKe0\n XsAirbo3h+rlZs7S3/IZVx88f4wvO6FZ/24uBzo/KiUTaN7+5YDiypuFf8rkLq/xFV9FwI\n sSn4e/Zo3WRptG4qu9JlaB2+eaejxjE=","X-MC-Unique":"UyBT3bfuOX-P4wl-Xm3bWg-1","From":"marcandre.lureau@redhat.com","To":"qemu-devel@nongnu.org","Subject":"[PULL v2 30/36] ui/dbus: add clipboard interface","Date":"Tue, 21 Dec 2021 10:58:49 +0400","Message-Id":"<20211221065855.142578-31-marcandre.lureau@redhat.com>","In-Reply-To":"<20211221065855.142578-1-marcandre.lureau@redhat.com>","References":"<20211221065855.142578-1-marcandre.lureau@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.13","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Received-SPF":"pass client-ip=170.10.129.124;\n envelope-from=marcandre.lureau@redhat.com;\n helo=us-smtp-delivery-124.mimecast.com","X-Spam_score_int":"-29","X-Spam_score":"-3.0","X-Spam_bar":"---","X-Spam_report":"(-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.203,\n DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001,\n 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.29","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"peter.maydell@linaro.org, richard.henderson@linaro.org, =?utf-8?q?Marc-A?=\n\t=?utf-8?q?ndr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n <qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"From: Marc-André Lureau <marcandre.lureau@redhat.com>\n\nExpose the clipboard API over D-Bus. See the interface documentation for\nfurther details.\n\nSigned-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>\nAcked-by: Gerd Hoffmann <kraxel@redhat.com>\n---\n ui/dbus.h            |  14 ++\n ui/dbus-clipboard.c  | 457 +++++++++++++++++++++++++++++++++++++++++++\n ui/dbus.c            |   7 +\n ui/dbus-display1.xml |  97 +++++++++\n ui/meson.build       |   1 +\n ui/trace-events      |   3 +\n 6 files changed, 579 insertions(+)\n create mode 100644 ui/dbus-clipboard.c","diff":"diff --git a/ui/dbus.h b/ui/dbus.h\nindex ca1f0f4ab94f..3e89eafcab6e 100644\n--- a/ui/dbus.h\n+++ b/ui/dbus.h\n@@ -27,9 +27,16 @@\n #include \"qemu/dbus.h\"\n #include \"qom/object.h\"\n #include \"ui/console.h\"\n+#include \"ui/clipboard.h\"\n \n #include \"dbus-display1.h\"\n \n+typedef struct DBusClipboardRequest {\n+    GDBusMethodInvocation *invocation;\n+    QemuClipboardType type;\n+    guint timeout_id;\n+} DBusClipboardRequest;\n+\n struct DBusDisplay {\n     Object parent;\n \n@@ -44,6 +51,11 @@ struct DBusDisplay {\n     QemuDBusDisplay1VM *iface;\n     GPtrArray *consoles;\n     GCancellable *add_client_cancellable;\n+\n+    QemuClipboardPeer clipboard_peer;\n+    QemuDBusDisplay1Clipboard *clipboard;\n+    QemuDBusDisplay1Clipboard *clipboard_proxy;\n+    DBusClipboardRequest clipboard_request[QEMU_CLIPBOARD_SELECTION__COUNT];\n };\n \n #define TYPE_DBUS_DISPLAY \"dbus-display\"\n@@ -83,4 +95,6 @@ dbus_display_listener_get_bus_name(DBusDisplayListener *ddl);\n extern const DisplayChangeListenerOps dbus_gl_dcl_ops;\n extern const DisplayChangeListenerOps dbus_dcl_ops;\n \n+void dbus_clipboard_init(DBusDisplay *dpy);\n+\n #endif /* UI_DBUS_H_ */\ndiff --git a/ui/dbus-clipboard.c b/ui/dbus-clipboard.c\nnew file mode 100644\nindex 000000000000..5843d26cd2cb\n--- /dev/null\n+++ b/ui/dbus-clipboard.c\n@@ -0,0 +1,457 @@\n+/*\n+ * QEMU DBus display\n+ *\n+ * Copyright (c) 2021 Marc-André Lureau <marcandre.lureau@redhat.com>\n+ *\n+ * Permission is hereby granted, free of charge, to any person obtaining a copy\n+ * of this software and associated documentation files (the \"Software\"), to deal\n+ * in the Software without restriction, including without limitation the rights\n+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n+ * copies of the Software, and to permit persons to whom the Software is\n+ * furnished to do so, subject to the following conditions:\n+ *\n+ * The above copyright notice and this permission notice shall be included in\n+ * all copies or substantial portions of the Software.\n+ *\n+ * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\n+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n+ * THE SOFTWARE.\n+ */\n+#include \"qemu/osdep.h\"\n+#include \"qemu/dbus.h\"\n+#include \"qemu/main-loop.h\"\n+#include \"qom/object_interfaces.h\"\n+#include \"sysemu/sysemu.h\"\n+#include \"qapi/error.h\"\n+#include \"trace.h\"\n+\n+#include \"dbus.h\"\n+\n+#define MIME_TEXT_PLAIN_UTF8 \"text/plain;charset=utf-8\"\n+\n+static void\n+dbus_clipboard_complete_request(\n+    DBusDisplay *dpy,\n+    GDBusMethodInvocation *invocation,\n+    QemuClipboardInfo *info,\n+    QemuClipboardType type)\n+{\n+    GVariant *v_data = g_variant_new_from_data(\n+        G_VARIANT_TYPE(\"ay\"),\n+        info->types[type].data,\n+        info->types[type].size,\n+        TRUE,\n+        (GDestroyNotify)qemu_clipboard_info_unref,\n+        qemu_clipboard_info_ref(info));\n+\n+    qemu_dbus_display1_clipboard_complete_request(\n+        dpy->clipboard, invocation,\n+        MIME_TEXT_PLAIN_UTF8, v_data);\n+}\n+\n+static void\n+dbus_clipboard_update_info(DBusDisplay *dpy, QemuClipboardInfo *info)\n+{\n+    bool self_update = info->owner == &dpy->clipboard_peer;\n+    const char *mime[QEMU_CLIPBOARD_TYPE__COUNT + 1] = { 0, };\n+    DBusClipboardRequest *req;\n+    int i = 0;\n+\n+    if (info->owner == NULL) {\n+        if (dpy->clipboard_proxy) {\n+            qemu_dbus_display1_clipboard_call_release(\n+                dpy->clipboard_proxy,\n+                info->selection,\n+                G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);\n+        }\n+        return;\n+    }\n+\n+    if (self_update || !info->has_serial) {\n+        return;\n+    }\n+\n+    req = &dpy->clipboard_request[info->selection];\n+    if (req->invocation && info->types[req->type].data) {\n+        dbus_clipboard_complete_request(dpy, req->invocation, info, req->type);\n+        g_clear_object(&req->invocation);\n+        g_source_remove(req->timeout_id);\n+        req->timeout_id = 0;\n+        return;\n+    }\n+\n+    if (info->types[QEMU_CLIPBOARD_TYPE_TEXT].available) {\n+        mime[i++] = MIME_TEXT_PLAIN_UTF8;\n+    }\n+\n+    if (i > 0) {\n+        if (dpy->clipboard_proxy) {\n+            qemu_dbus_display1_clipboard_call_grab(\n+                dpy->clipboard_proxy,\n+                info->selection,\n+                info->serial,\n+                mime,\n+                G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);\n+        }\n+    }\n+}\n+\n+static void\n+dbus_clipboard_reset_serial(DBusDisplay *dpy)\n+{\n+    if (dpy->clipboard_proxy) {\n+        qemu_dbus_display1_clipboard_call_register(\n+            dpy->clipboard_proxy,\n+            G_DBUS_CALL_FLAGS_NONE,\n+            -1, NULL, NULL, NULL);\n+    }\n+}\n+\n+static void\n+dbus_clipboard_notify(Notifier *notifier, void *data)\n+{\n+    DBusDisplay *dpy =\n+        container_of(notifier, DBusDisplay, clipboard_peer.notifier);\n+    QemuClipboardNotify *notify = data;\n+\n+    switch (notify->type) {\n+    case QEMU_CLIPBOARD_UPDATE_INFO:\n+        dbus_clipboard_update_info(dpy, notify->info);\n+        return;\n+    case QEMU_CLIPBOARD_RESET_SERIAL:\n+        dbus_clipboard_reset_serial(dpy);\n+        return;\n+    }\n+}\n+\n+static void\n+dbus_clipboard_qemu_request(QemuClipboardInfo *info,\n+                            QemuClipboardType type)\n+{\n+    DBusDisplay *dpy = container_of(info->owner, DBusDisplay, clipboard_peer);\n+    g_autofree char *mime = NULL;\n+    g_autoptr(GVariant) v_data = NULL;\n+    g_autoptr(GError) err = NULL;\n+    const char *data = NULL;\n+    const char *mimes[] = { MIME_TEXT_PLAIN_UTF8, NULL };\n+    size_t n;\n+\n+    if (type != QEMU_CLIPBOARD_TYPE_TEXT) {\n+        /* unsupported atm */\n+        return;\n+    }\n+\n+    if (dpy->clipboard_proxy) {\n+        if (!qemu_dbus_display1_clipboard_call_request_sync(\n+                dpy->clipboard_proxy,\n+                info->selection,\n+                mimes,\n+                G_DBUS_CALL_FLAGS_NONE, -1, &mime, &v_data, NULL, &err)) {\n+            error_report(\"Failed to request clipboard: %s\", err->message);\n+            return;\n+        }\n+\n+        if (g_strcmp0(mime, MIME_TEXT_PLAIN_UTF8)) {\n+            error_report(\"Unsupported returned MIME: %s\", mime);\n+            return;\n+        }\n+\n+        data = g_variant_get_fixed_array(v_data, &n, 1);\n+        qemu_clipboard_set_data(&dpy->clipboard_peer, info, type,\n+                                n, data, true);\n+    }\n+}\n+\n+static void\n+dbus_clipboard_request_cancelled(DBusClipboardRequest *req)\n+{\n+    if (!req->invocation) {\n+        return;\n+    }\n+\n+    g_dbus_method_invocation_return_error(\n+        req->invocation,\n+        DBUS_DISPLAY_ERROR,\n+        DBUS_DISPLAY_ERROR_FAILED,\n+        \"Cancelled clipboard request\");\n+\n+    g_clear_object(&req->invocation);\n+    g_source_remove(req->timeout_id);\n+    req->timeout_id = 0;\n+}\n+\n+static void\n+dbus_clipboard_unregister_proxy(DBusDisplay *dpy)\n+{\n+    const char *name = NULL;\n+    int i;\n+\n+    for (i = 0; i < G_N_ELEMENTS(dpy->clipboard_request); ++i) {\n+        dbus_clipboard_request_cancelled(&dpy->clipboard_request[i]);\n+    }\n+\n+    if (!dpy->clipboard_proxy) {\n+        return;\n+    }\n+\n+    name = g_dbus_proxy_get_name(G_DBUS_PROXY(dpy->clipboard_proxy));\n+    trace_dbus_clipboard_unregister(name);\n+    g_clear_object(&dpy->clipboard_proxy);\n+}\n+\n+static void\n+dbus_on_clipboard_proxy_name_owner_changed(\n+    DBusDisplay *dpy,\n+    GObject *object,\n+    GParamSpec *pspec)\n+{\n+    dbus_clipboard_unregister_proxy(dpy);\n+}\n+\n+static gboolean\n+dbus_clipboard_register(\n+    DBusDisplay *dpy,\n+    GDBusMethodInvocation *invocation)\n+{\n+    g_autoptr(GError) err = NULL;\n+    const char *name = NULL;\n+\n+    if (dpy->clipboard_proxy) {\n+        g_dbus_method_invocation_return_error(\n+            invocation,\n+            DBUS_DISPLAY_ERROR,\n+            DBUS_DISPLAY_ERROR_FAILED,\n+            \"Clipboard peer already registered!\");\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    dpy->clipboard_proxy =\n+        qemu_dbus_display1_clipboard_proxy_new_sync(\n+            g_dbus_method_invocation_get_connection(invocation),\n+            G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,\n+            g_dbus_method_invocation_get_sender(invocation),\n+            \"/org/qemu/Display1/Clipboard\",\n+            NULL,\n+            &err);\n+    if (!dpy->clipboard_proxy) {\n+        g_dbus_method_invocation_return_error(\n+            invocation,\n+            DBUS_DISPLAY_ERROR,\n+            DBUS_DISPLAY_ERROR_FAILED,\n+            \"Failed to setup proxy: %s\", err->message);\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    name = g_dbus_proxy_get_name(G_DBUS_PROXY(dpy->clipboard_proxy));\n+    trace_dbus_clipboard_register(name);\n+\n+    g_object_connect(dpy->clipboard_proxy,\n+                     \"swapped-signal::notify::g-name-owner\",\n+                     dbus_on_clipboard_proxy_name_owner_changed, dpy,\n+                     NULL);\n+    qemu_clipboard_reset_serial();\n+\n+    qemu_dbus_display1_clipboard_complete_register(dpy->clipboard, invocation);\n+    return DBUS_METHOD_INVOCATION_HANDLED;\n+}\n+\n+static gboolean\n+dbus_clipboard_check_caller(DBusDisplay *dpy, GDBusMethodInvocation *invocation)\n+{\n+    if (!dpy->clipboard_proxy ||\n+        g_strcmp0(g_dbus_proxy_get_name(G_DBUS_PROXY(dpy->clipboard_proxy)),\n+                  g_dbus_method_invocation_get_sender(invocation))) {\n+        g_dbus_method_invocation_return_error(\n+            invocation,\n+            DBUS_DISPLAY_ERROR,\n+            DBUS_DISPLAY_ERROR_FAILED,\n+            \"Unregistered caller\");\n+        return FALSE;\n+    }\n+\n+    return TRUE;\n+}\n+\n+static gboolean\n+dbus_clipboard_unregister(\n+    DBusDisplay *dpy,\n+    GDBusMethodInvocation *invocation)\n+{\n+    if (!dbus_clipboard_check_caller(dpy, invocation)) {\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    dbus_clipboard_unregister_proxy(dpy);\n+\n+    qemu_dbus_display1_clipboard_complete_unregister(\n+        dpy->clipboard, invocation);\n+\n+    return DBUS_METHOD_INVOCATION_HANDLED;\n+}\n+\n+static gboolean\n+dbus_clipboard_grab(\n+    DBusDisplay *dpy,\n+    GDBusMethodInvocation *invocation,\n+    gint arg_selection,\n+    guint arg_serial,\n+    const gchar *const *arg_mimes)\n+{\n+    QemuClipboardSelection s = arg_selection;\n+    g_autoptr(QemuClipboardInfo) info = NULL;\n+\n+    if (!dbus_clipboard_check_caller(dpy, invocation)) {\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    if (s >= QEMU_CLIPBOARD_SELECTION__COUNT) {\n+        g_dbus_method_invocation_return_error(\n+            invocation,\n+            DBUS_DISPLAY_ERROR,\n+            DBUS_DISPLAY_ERROR_FAILED,\n+            \"Invalid clipboard selection: %d\", arg_selection);\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    info = qemu_clipboard_info_new(&dpy->clipboard_peer, s);\n+    if (g_strv_contains(arg_mimes, MIME_TEXT_PLAIN_UTF8)) {\n+        info->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;\n+    }\n+    info->serial = arg_serial;\n+    info->has_serial = true;\n+    if (qemu_clipboard_check_serial(info, true)) {\n+        qemu_clipboard_update(info);\n+    } else {\n+        trace_dbus_clipboard_grab_failed();\n+    }\n+\n+    qemu_dbus_display1_clipboard_complete_grab(dpy->clipboard, invocation);\n+    return DBUS_METHOD_INVOCATION_HANDLED;\n+}\n+\n+static gboolean\n+dbus_clipboard_release(\n+    DBusDisplay *dpy,\n+    GDBusMethodInvocation *invocation,\n+    gint arg_selection)\n+{\n+    if (!dbus_clipboard_check_caller(dpy, invocation)) {\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    qemu_clipboard_peer_release(&dpy->clipboard_peer, arg_selection);\n+\n+    qemu_dbus_display1_clipboard_complete_release(dpy->clipboard, invocation);\n+    return DBUS_METHOD_INVOCATION_HANDLED;\n+}\n+\n+static gboolean\n+dbus_clipboard_request_timeout(gpointer user_data)\n+{\n+    dbus_clipboard_request_cancelled(user_data);\n+    return G_SOURCE_REMOVE;\n+}\n+\n+static gboolean\n+dbus_clipboard_request(\n+    DBusDisplay *dpy,\n+    GDBusMethodInvocation *invocation,\n+    gint arg_selection,\n+    const gchar *const *arg_mimes)\n+{\n+    QemuClipboardSelection s = arg_selection;\n+    QemuClipboardType type = QEMU_CLIPBOARD_TYPE_TEXT;\n+    QemuClipboardInfo *info = NULL;\n+\n+    if (!dbus_clipboard_check_caller(dpy, invocation)) {\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    if (s >= QEMU_CLIPBOARD_SELECTION__COUNT) {\n+        g_dbus_method_invocation_return_error(\n+            invocation,\n+            DBUS_DISPLAY_ERROR,\n+            DBUS_DISPLAY_ERROR_FAILED,\n+            \"Invalid clipboard selection: %d\", arg_selection);\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    if (dpy->clipboard_request[s].invocation) {\n+        g_dbus_method_invocation_return_error(\n+            invocation,\n+            DBUS_DISPLAY_ERROR,\n+            DBUS_DISPLAY_ERROR_FAILED,\n+            \"Pending request\");\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    info = qemu_clipboard_info(s);\n+    if (!info || !info->owner || info->owner == &dpy->clipboard_peer) {\n+        g_dbus_method_invocation_return_error(\n+            invocation,\n+            DBUS_DISPLAY_ERROR,\n+            DBUS_DISPLAY_ERROR_FAILED,\n+            \"Empty clipboard\");\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    if (!g_strv_contains(arg_mimes, MIME_TEXT_PLAIN_UTF8) ||\n+        !info->types[type].available) {\n+        g_dbus_method_invocation_return_error(\n+            invocation,\n+            DBUS_DISPLAY_ERROR,\n+            DBUS_DISPLAY_ERROR_FAILED,\n+            \"Unhandled MIME types requested\");\n+        return DBUS_METHOD_INVOCATION_HANDLED;\n+    }\n+\n+    if (info->types[type].data) {\n+        dbus_clipboard_complete_request(dpy, invocation, info, type);\n+    } else {\n+        qemu_clipboard_request(info, type);\n+\n+        dpy->clipboard_request[s].invocation = g_object_ref(invocation);\n+        dpy->clipboard_request[s].type = type;\n+        dpy->clipboard_request[s].timeout_id =\n+            g_timeout_add_seconds(5, dbus_clipboard_request_timeout,\n+                                  &dpy->clipboard_request[s]);\n+    }\n+\n+    return DBUS_METHOD_INVOCATION_HANDLED;\n+}\n+\n+void\n+dbus_clipboard_init(DBusDisplay *dpy)\n+{\n+    g_autoptr(GDBusObjectSkeleton) clipboard = NULL;\n+\n+    assert(!dpy->clipboard);\n+\n+    clipboard = g_dbus_object_skeleton_new(DBUS_DISPLAY1_ROOT \"/Clipboard\");\n+    dpy->clipboard = qemu_dbus_display1_clipboard_skeleton_new();\n+    g_object_connect(dpy->clipboard,\n+                     \"swapped-signal::handle-register\",\n+                     dbus_clipboard_register, dpy,\n+                     \"swapped-signal::handle-unregister\",\n+                     dbus_clipboard_unregister, dpy,\n+                     \"swapped-signal::handle-grab\",\n+                     dbus_clipboard_grab, dpy,\n+                     \"swapped-signal::handle-release\",\n+                     dbus_clipboard_release, dpy,\n+                     \"swapped-signal::handle-request\",\n+                     dbus_clipboard_request, dpy,\n+                     NULL);\n+\n+    g_dbus_object_skeleton_add_interface(\n+        G_DBUS_OBJECT_SKELETON(clipboard),\n+        G_DBUS_INTERFACE_SKELETON(dpy->clipboard));\n+    g_dbus_object_manager_server_export(dpy->server, clipboard);\n+    dpy->clipboard_peer.name = \"dbus\";\n+    dpy->clipboard_peer.notifier.notify = dbus_clipboard_notify;\n+    dpy->clipboard_peer.request = dbus_clipboard_qemu_request;\n+    qemu_clipboard_peer_register(&dpy->clipboard_peer);\n+}\ndiff --git a/ui/dbus.c b/ui/dbus.c\nindex d24f704d4662..4f0bc293aaa0 100644\n--- a/ui/dbus.c\n+++ b/ui/dbus.c\n@@ -24,6 +24,7 @@\n #include \"qemu/osdep.h\"\n #include \"qemu/cutils.h\"\n #include \"qemu/dbus.h\"\n+#include \"qemu/main-loop.h\"\n #include \"qemu/option.h\"\n #include \"qom/object_interfaces.h\"\n #include \"sysemu/sysemu.h\"\n@@ -70,6 +71,8 @@ dbus_display_init(Object *o)\n     g_dbus_object_skeleton_add_interface(\n         vm, G_DBUS_INTERFACE_SKELETON(dd->iface));\n     g_dbus_object_manager_server_export(dd->server, vm);\n+\n+    dbus_clipboard_init(dd);\n }\n \n static void\n@@ -77,6 +80,9 @@ dbus_display_finalize(Object *o)\n {\n     DBusDisplay *dd = DBUS_DISPLAY(o);\n \n+    qemu_clipboard_peer_unregister(&dd->clipboard_peer);\n+    g_clear_object(&dd->clipboard);\n+\n     g_clear_object(&dd->server);\n     g_clear_pointer(&dd->consoles, g_ptr_array_unref);\n     if (dd->add_client_cancellable) {\n@@ -294,6 +300,7 @@ set_audiodev(Object *o, const char *str, Error **errp)\n     dd->audiodev = g_strdup(str);\n }\n \n+\n static int\n get_gl_mode(Object *o, Error **errp)\n {\ndiff --git a/ui/dbus-display1.xml b/ui/dbus-display1.xml\nindex aff645220c88..767562ad1ea3 100644\n--- a/ui/dbus-display1.xml\n+++ b/ui/dbus-display1.xml\n@@ -376,6 +376,103 @@\n     </method>\n   </interface>\n \n+  <!--\n+      org.qemu.Display1.Clipboard:\n+\n+      This interface must be implemented by both the client and the server on\n+      ``/org/qemu/Display1/Clipboard`` to support clipboard sharing between\n+      the client and the guest.\n+\n+      Once :dbus:meth:`Register`'ed, method calls may be sent and received in both\n+      directions. Unregistered callers will get error replies.\n+\n+      .. _dbus-clipboard-selection:\n+\n+      **Selection values**::\n+\n+        Clipboard   = 0\n+        Primary     = 1\n+        Secondary   = 2\n+\n+      .. _dbus-clipboard-serial:\n+\n+      **Serial counter**\n+\n+      To solve potential clipboard races, clipboard grabs have an associated\n+      serial counter. It is set to 0 on registration, and incremented by 1 for\n+      each grab. The peer with the highest serial is the clipboard grab owner.\n+\n+      When a grab with a lower serial is received, it should be discarded.\n+\n+      When a grab is attempted with the same serial number as the current grab,\n+      the one coming from the client should have higher priority, and the client\n+      should gain clipboard grab ownership.\n+  -->\n+  <interface name=\"org.qemu.Display1.Clipboard\">\n+    <!--\n+        Register:\n+\n+        Register a clipboard session and reinitialize the serial counter.\n+\n+        The client must register itself, and is granted an exclusive\n+        access for handling the clipboard.\n+\n+        The server can reinitialize the session as well (to reset the counter).\n+    -->\n+    <method name=\"Register\"/>\n+\n+    <!--\n+        Unregister:\n+\n+        Unregister the clipboard session.\n+    -->\n+    <method name=\"Unregister\"/>\n+    <!--\n+        Grab:\n+        @selection: a :ref:`selection value<dbus-clipboard-selection>`.\n+        @serial: the current grab :ref:`serial<dbus-clipboard-serial>`.\n+        @mimes: the list of available content MIME types.\n+\n+        Grab the clipboard, claiming current clipboard content.\n+    -->\n+    <method name=\"Grab\">\n+      <arg type=\"u\" name=\"selection\"/>\n+      <arg type=\"u\" name=\"serial\"/>\n+      <arg type=\"as\" name=\"mimes\"/>\n+    </method>\n+\n+    <!--\n+        Release:\n+        @selection: a :ref:`selection value<dbus-clipboard-selection>`.\n+\n+        Release the clipboard (does nothing if not the current owner).\n+    -->\n+    <method name=\"Release\">\n+      <arg type=\"u\" name=\"selection\"/>\n+    </method>\n+\n+    <!--\n+        Request:\n+        @selection: a :ref:`selection value<dbus-clipboard-selection>`\n+        @mimes: requested MIME types (by order of preference).\n+        @reply_mime: the returned data MIME type.\n+        @data: the clipboard data.\n+\n+        Request the clipboard content.\n+\n+        Return an error if the clipboard is empty, or the requested MIME types\n+        are unavailable.\n+    -->\n+    <method name=\"Request\">\n+      <arg type=\"u\" name=\"selection\"/>\n+      <arg type=\"as\" name=\"mimes\"/>\n+      <arg type=\"s\" name=\"reply_mime\" direction=\"out\"/>\n+      <arg type=\"ay\" name=\"data\" direction=\"out\">\n+        <annotation name=\"org.gtk.GDBus.C.ForceGVariant\" value=\"true\"/>\n+      </arg>\n+    </method>\n+  </interface>\n+\n   <!--\n       org.qemu.Display1.Audio:\n \ndiff --git a/ui/meson.build b/ui/meson.build\nindex 80f21704ada6..8982ab63c4df 100644\n--- a/ui/meson.build\n+++ b/ui/meson.build\n@@ -82,6 +82,7 @@ if dbus_display\n                                           '--generate-c-code', '@BASENAME@'])\n   dbus_ss.add(when: [gio, pixman, opengl, 'CONFIG_GIO'],\n               if_true: [files(\n+                'dbus-clipboard.c',\n                 'dbus-console.c',\n                 'dbus-error.c',\n                 'dbus-listener.c',\ndiff --git a/ui/trace-events b/ui/trace-events\nindex b1ae30159a53..f78b5e66061f 100644\n--- a/ui/trace-events\n+++ b/ui/trace-events\n@@ -147,3 +147,6 @@ dbus_mouse_release(unsigned int button) \"button %u\"\n dbus_mouse_set_pos(unsigned int x, unsigned int y) \"x=%u, y=%u\"\n dbus_mouse_rel_motion(int dx, int dy) \"dx=%d, dy=%d\"\n dbus_update(int x, int y, int w, int h) \"x=%d, y=%d, w=%d, h=%d\"\n+dbus_clipboard_grab_failed(void) \"\"\n+dbus_clipboard_register(const char *bus_name) \"peer %s\"\n+dbus_clipboard_unregister(const char *bus_name) \"peer %s\"\n","prefixes":["PULL","v2","30/36"]}