2021-05-19 08:39:40 +03:00
|
|
|
/*
|
|
|
|
* GTK UI -- clipboard support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Gerd Hoffmann <kraxel@redhat.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/main-loop.h"
|
|
|
|
|
|
|
|
#include "ui/gtk.h"
|
|
|
|
|
|
|
|
static QemuClipboardSelection gd_find_selection(GtkDisplayState *gd,
|
|
|
|
GtkClipboard *clipboard)
|
|
|
|
{
|
|
|
|
QemuClipboardSelection s;
|
|
|
|
|
|
|
|
for (s = 0; s < QEMU_CLIPBOARD_SELECTION__COUNT; s++) {
|
|
|
|
if (gd->gtkcb[s] == clipboard) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QEMU_CLIPBOARD_SELECTION_CLIPBOARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gd_clipboard_get_data(GtkClipboard *clipboard,
|
|
|
|
GtkSelectionData *selection_data,
|
|
|
|
guint selection_info,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDisplayState *gd = data;
|
|
|
|
QemuClipboardSelection s = gd_find_selection(gd, clipboard);
|
|
|
|
QemuClipboardType type = QEMU_CLIPBOARD_TYPE_TEXT;
|
2021-08-05 16:57:12 +03:00
|
|
|
g_autoptr(QemuClipboardInfo) info = NULL;
|
|
|
|
|
|
|
|
info = qemu_clipboard_info_ref(qemu_clipboard_info(s));
|
2021-05-19 08:39:40 +03:00
|
|
|
|
|
|
|
qemu_clipboard_request(info, type);
|
2021-08-05 16:57:12 +03:00
|
|
|
while (info == qemu_clipboard_info(s) &&
|
2021-05-19 08:39:40 +03:00
|
|
|
info->types[type].available &&
|
|
|
|
info->types[type].data == NULL) {
|
|
|
|
main_loop_wait(false);
|
|
|
|
}
|
|
|
|
|
2021-08-05 16:57:12 +03:00
|
|
|
if (info == qemu_clipboard_info(s) && gd->cbowner[s]) {
|
2021-05-19 08:39:40 +03:00
|
|
|
gtk_selection_data_set_text(selection_data,
|
|
|
|
info->types[type].data,
|
|
|
|
info->types[type].size);
|
|
|
|
} else {
|
|
|
|
/* clipboard owner changed while waiting for the data */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gd_clipboard_clear(GtkClipboard *clipboard,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDisplayState *gd = data;
|
|
|
|
QemuClipboardSelection s = gd_find_selection(gd, clipboard);
|
|
|
|
|
|
|
|
gd->cbowner[s] = false;
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:42:15 +03:00
|
|
|
static void gd_clipboard_update_info(GtkDisplayState *gd,
|
|
|
|
QemuClipboardInfo *info)
|
2021-05-19 08:39:40 +03:00
|
|
|
{
|
|
|
|
QemuClipboardSelection s = info->selection;
|
|
|
|
bool self_update = info->owner == &gd->cbpeer;
|
|
|
|
|
2021-08-05 16:57:12 +03:00
|
|
|
if (info != qemu_clipboard_info(s)) {
|
2021-05-19 08:39:40 +03:00
|
|
|
gd->cbpending[s] = 0;
|
|
|
|
if (!self_update) {
|
ui: fix gtk clipboard clear assertion
When closing the QEMU Gtk display window, it can occasionaly warn:
qemu-system-x86_64: Gtk: gtk_clipboard_set_with_data: assertion 'targets != NULL' failed
#3 0x00007ffff4f02f22 in gtk_clipboard_set_with_data (clipboard=<optimized out>, targets=<optimized out>, n_targets=<optimized out>, get_func=<optimized out>, clear_func=<optimized out>, user_data=<optimized out>) at /usr/src/debug/gtk3-3.24.30-4.fc35.x86_64/gtk/gtkclipboard.c:672
#4 0x00007ffff552cd75 in gd_clipboard_update_info (gd=0x5555579a9e00, info=0x555557ba4b50) at ../ui/gtk-clipboard.c:98
#5 0x00007ffff552ce00 in gd_clipboard_notify (notifier=0x5555579aaba8, data=0x7fffffffd720) at ../ui/gtk-clipboard.c:128
#6 0x000055555603e0ff in notifier_list_notify (list=0x555556657470 <clipboard_notifiers>, data=0x7fffffffd720) at ../util/notify.c:39
#7 0x000055555594e8e0 in qemu_clipboard_update (info=0x555557ba4b50) at ../ui/clipboard.c:54
#8 0x000055555594e840 in qemu_clipboard_peer_release (peer=0x55555684a5b0, selection=QEMU_CLIPBOARD_SELECTION_PRIMARY) at ../ui/clipboard.c:40
#9 0x000055555594e786 in qemu_clipboard_peer_unregister (peer=0x55555684a5b0) at ../ui/clipboard.c:19
#10 0x000055555595f044 in vdagent_disconnect (vd=0x55555684a400) at ../ui/vdagent.c:852
#11 0x000055555595f262 in vdagent_chr_fini (obj=0x55555684a400) at ../ui/vdagent.c:908
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20211216083233.1166504-1-marcandre.lureau@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2021-12-16 11:32:33 +03:00
|
|
|
g_autoptr(GtkTargetList) list = NULL;
|
2021-05-19 08:39:40 +03:00
|
|
|
GtkTargetEntry *targets;
|
|
|
|
gint n_targets;
|
|
|
|
|
|
|
|
list = gtk_target_list_new(NULL, 0);
|
|
|
|
if (info->types[QEMU_CLIPBOARD_TYPE_TEXT].available) {
|
|
|
|
gtk_target_list_add_text_targets(list, 0);
|
|
|
|
}
|
|
|
|
targets = gtk_target_table_new_from_list(list, &n_targets);
|
|
|
|
|
|
|
|
gtk_clipboard_clear(gd->gtkcb[s]);
|
ui: fix gtk clipboard clear assertion
When closing the QEMU Gtk display window, it can occasionaly warn:
qemu-system-x86_64: Gtk: gtk_clipboard_set_with_data: assertion 'targets != NULL' failed
#3 0x00007ffff4f02f22 in gtk_clipboard_set_with_data (clipboard=<optimized out>, targets=<optimized out>, n_targets=<optimized out>, get_func=<optimized out>, clear_func=<optimized out>, user_data=<optimized out>) at /usr/src/debug/gtk3-3.24.30-4.fc35.x86_64/gtk/gtkclipboard.c:672
#4 0x00007ffff552cd75 in gd_clipboard_update_info (gd=0x5555579a9e00, info=0x555557ba4b50) at ../ui/gtk-clipboard.c:98
#5 0x00007ffff552ce00 in gd_clipboard_notify (notifier=0x5555579aaba8, data=0x7fffffffd720) at ../ui/gtk-clipboard.c:128
#6 0x000055555603e0ff in notifier_list_notify (list=0x555556657470 <clipboard_notifiers>, data=0x7fffffffd720) at ../util/notify.c:39
#7 0x000055555594e8e0 in qemu_clipboard_update (info=0x555557ba4b50) at ../ui/clipboard.c:54
#8 0x000055555594e840 in qemu_clipboard_peer_release (peer=0x55555684a5b0, selection=QEMU_CLIPBOARD_SELECTION_PRIMARY) at ../ui/clipboard.c:40
#9 0x000055555594e786 in qemu_clipboard_peer_unregister (peer=0x55555684a5b0) at ../ui/clipboard.c:19
#10 0x000055555595f044 in vdagent_disconnect (vd=0x55555684a400) at ../ui/vdagent.c:852
#11 0x000055555595f262 in vdagent_chr_fini (obj=0x55555684a400) at ../ui/vdagent.c:908
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20211216083233.1166504-1-marcandre.lureau@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2021-12-16 11:32:33 +03:00
|
|
|
if (targets) {
|
|
|
|
gd->cbowner[s] = true;
|
|
|
|
gtk_clipboard_set_with_data(gd->gtkcb[s],
|
|
|
|
targets, n_targets,
|
|
|
|
gd_clipboard_get_data,
|
|
|
|
gd_clipboard_clear,
|
|
|
|
gd);
|
|
|
|
|
|
|
|
gtk_target_table_free(targets, n_targets);
|
|
|
|
}
|
2021-05-19 08:39:40 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self_update) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clipboard got updated, with data probably. No action here, we
|
|
|
|
* are waiting for updates in gd_clipboard_get_data().
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:42:15 +03:00
|
|
|
static void gd_clipboard_notify(Notifier *notifier, void *data)
|
|
|
|
{
|
|
|
|
GtkDisplayState *gd =
|
|
|
|
container_of(notifier, GtkDisplayState, cbpeer.notifier);
|
|
|
|
QemuClipboardNotify *notify = data;
|
|
|
|
|
|
|
|
switch (notify->type) {
|
|
|
|
case QEMU_CLIPBOARD_UPDATE_INFO:
|
|
|
|
gd_clipboard_update_info(gd, notify->info);
|
|
|
|
return;
|
2021-07-19 18:49:56 +03:00
|
|
|
case QEMU_CLIPBOARD_RESET_SERIAL:
|
|
|
|
/* ignore */
|
|
|
|
return;
|
2021-07-19 18:42:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 08:39:40 +03:00
|
|
|
static void gd_clipboard_request(QemuClipboardInfo *info,
|
|
|
|
QemuClipboardType type)
|
|
|
|
{
|
|
|
|
GtkDisplayState *gd = container_of(info->owner, GtkDisplayState, cbpeer);
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case QEMU_CLIPBOARD_TYPE_TEXT:
|
|
|
|
text = gtk_clipboard_wait_for_text(gd->gtkcb[info->selection]);
|
|
|
|
if (text) {
|
|
|
|
qemu_clipboard_set_data(&gd->cbpeer, info, type,
|
|
|
|
strlen(text), text, true);
|
|
|
|
g_free(text);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gd_owner_change(GtkClipboard *clipboard,
|
|
|
|
GdkEvent *event,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDisplayState *gd = data;
|
|
|
|
QemuClipboardSelection s = gd_find_selection(gd, clipboard);
|
|
|
|
QemuClipboardInfo *info;
|
|
|
|
|
|
|
|
if (gd->cbowner[s]) {
|
|
|
|
/* ignore notifications about our own grabs */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (event->owner_change.reason) {
|
2021-08-05 16:57:01 +03:00
|
|
|
case GDK_OWNER_CHANGE_NEW_OWNER:
|
2021-05-19 08:39:40 +03:00
|
|
|
info = qemu_clipboard_info_new(&gd->cbpeer, s);
|
|
|
|
if (gtk_clipboard_wait_is_text_available(clipboard)) {
|
|
|
|
info->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_clipboard_update(info);
|
|
|
|
qemu_clipboard_info_unref(info);
|
|
|
|
break;
|
|
|
|
default:
|
2021-08-05 16:57:14 +03:00
|
|
|
qemu_clipboard_peer_release(&gd->cbpeer, s);
|
|
|
|
gd->cbowner[s] = false;
|
2021-05-19 08:39:40 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gd_clipboard_init(GtkDisplayState *gd)
|
|
|
|
{
|
|
|
|
gd->cbpeer.name = "gtk";
|
2021-07-19 18:42:15 +03:00
|
|
|
gd->cbpeer.notifier.notify = gd_clipboard_notify;
|
2021-05-19 08:39:40 +03:00
|
|
|
gd->cbpeer.request = gd_clipboard_request;
|
|
|
|
qemu_clipboard_peer_register(&gd->cbpeer);
|
|
|
|
|
|
|
|
gd->gtkcb[QEMU_CLIPBOARD_SELECTION_CLIPBOARD] =
|
2021-08-05 16:57:00 +03:00
|
|
|
gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
|
2021-05-19 08:39:40 +03:00
|
|
|
gd->gtkcb[QEMU_CLIPBOARD_SELECTION_PRIMARY] =
|
2021-08-05 16:57:00 +03:00
|
|
|
gtk_clipboard_get(GDK_SELECTION_PRIMARY);
|
2021-05-19 08:39:40 +03:00
|
|
|
gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY] =
|
2021-08-05 16:57:00 +03:00
|
|
|
gtk_clipboard_get(GDK_SELECTION_SECONDARY);
|
2021-05-19 08:39:40 +03:00
|
|
|
|
|
|
|
g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_CLIPBOARD],
|
|
|
|
"owner-change", G_CALLBACK(gd_owner_change), gd);
|
|
|
|
g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_PRIMARY],
|
|
|
|
"owner-change", G_CALLBACK(gd_owner_change), gd);
|
|
|
|
g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY],
|
|
|
|
"owner-change", G_CALLBACK(gd_owner_change), gd);
|
|
|
|
}
|