master
c 252 lines 7.93 KB
Raw
1 /*
2 * GTK UI -- clipboard support
3 *
4 * Copyright (C) 2021 Gerd Hoffmann <kraxel@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "qemu/osdep.h"
22 #include "qemu/error-report.h"
23 #include "qemu/main-loop.h"
24
25 #include "ui/gtk.h"
26
27 static QemuClipboardSelection gd_find_selection(GtkDisplayState *gd,
28 GtkClipboard *clipboard)
29 {
30 QemuClipboardSelection s;
31
32 for (s = 0; s < QEMU_CLIPBOARD_SELECTION__COUNT; s++) {
33 if (gd->gtkcb[s] == clipboard) {
34 return s;
35 }
36 }
37 return QEMU_CLIPBOARD_SELECTION_CLIPBOARD;
38 }
39
40 static void gd_clipboard_get_data(GtkClipboard *clipboard,
41 GtkSelectionData *selection_data,
42 guint selection_info,
43 gpointer data)
44 {
45 GtkDisplayState *gd = data;
46 QemuClipboardSelection s = gd_find_selection(gd, clipboard);
47 QemuClipboardType type = QEMU_CLIPBOARD_TYPE_TEXT;
48 g_autoptr(QemuClipboardInfo) info = NULL;
49
50 info = qemu_clipboard_info_ref(qemu_clipboard_info(s));
51
52 qemu_clipboard_request(info, type);
53 while (info == qemu_clipboard_info(s) &&
54 info->types[type].available &&
55 info->types[type].data == NULL) {
56 main_loop_wait(false);
57 }
58
59 if (info == qemu_clipboard_info(s) && gd->cbowner[s]) {
60 gtk_selection_data_set_text(selection_data,
61 info->types[type].data,
62 info->types[type].size);
63 } else {
64 /* clipboard owner changed while waiting for the data */
65 }
66 }
67
68 static void gd_clipboard_clear(GtkClipboard *clipboard,
69 gpointer data)
70 {
71 GtkDisplayState *gd = data;
72 QemuClipboardSelection s = gd_find_selection(gd, clipboard);
73
74 gd->cbowner[s] = false;
75 }
76
77 static void gd_clipboard_update_info(GtkDisplayState *gd,
78 QemuClipboardInfo *info)
79 {
80 QemuClipboardSelection s = info->selection;
81 bool self_update = info->owner == &gd->cbpeer;
82
83 if (info != qemu_clipboard_info(s)) {
84 gd->cbpending[s] = 0;
85 if (!self_update) {
86 g_autoptr(GtkTargetList) list = NULL;
87 GtkTargetEntry *targets;
88 gint n_targets;
89
90 list = gtk_target_list_new(NULL, 0);
91 if (info->types[QEMU_CLIPBOARD_TYPE_TEXT].available) {
92 gtk_target_list_add_text_targets(list, 0);
93 }
94 targets = gtk_target_table_new_from_list(list, &n_targets);
95
96 gtk_clipboard_clear(gd->gtkcb[s]);
97 if (targets) {
98 gd->cbowner[s] = true;
99 if (!gtk_clipboard_set_with_data(gd->gtkcb[s],
100 targets, n_targets,
101 gd_clipboard_get_data,
102 gd_clipboard_clear,
103 gd)) {
104 warn_report("Failed to set GTK clipboard");
105 }
106
107 gtk_target_table_free(targets, n_targets);
108 }
109 }
110 return;
111 }
112
113 if (self_update) {
114 return;
115 }
116
117 /*
118 * Clipboard got updated, with data probably. No action here, we
119 * are waiting for updates in gd_clipboard_get_data().
120 */
121 }
122
123 static void gd_clipboard_notify(Notifier *notifier, void *data)
124 {
125 GtkDisplayState *gd =
126 container_of(notifier, GtkDisplayState, cbpeer.notifier);
127 QemuClipboardNotify *notify = data;
128
129 switch (notify->type) {
130 case QEMU_CLIPBOARD_UPDATE_INFO:
131 gd_clipboard_update_info(gd, notify->info);
132 return;
133 case QEMU_CLIPBOARD_RESET_SERIAL:
134 /* ignore */
135 return;
136 }
137 }
138
139 static void
140 gd_clipboard_request_text_received_callback(GtkClipboard *clipboard,
141 const gchar *text,
142 gpointer data)
143 {
144 QemuClipboardInfo *info = (QemuClipboardInfo *)data;
145
146 if (text) {
147 qemu_clipboard_set_data(info->owner, info, QEMU_CLIPBOARD_TYPE_TEXT,
148 strlen(text), text, true);
149 }
150 qemu_clipboard_info_unref(info);
151 }
152
153 static void gd_clipboard_request(QemuClipboardInfo *info,
154 QemuClipboardType type)
155 {
156 GtkDisplayState *gd = container_of(info->owner, GtkDisplayState, cbpeer);
157
158 switch (type) {
159 case QEMU_CLIPBOARD_TYPE_TEXT:
160 qemu_clipboard_info_ref(info);
161 gtk_clipboard_request_text(gd->gtkcb[info->selection],
162 gd_clipboard_request_text_received_callback,
163 info);
164 break;
165 default:
166 break;
167 }
168 }
169
170 static void gd_clipboard_owner_change_targets_received_callback(
171 GtkClipboard *clipboard,
172 GdkAtom *targets,
173 gint n_targets,
174 gpointer data)
175 {
176 QemuClipboardInfo *info = (QemuClipboardInfo *)data;
177
178 if (targets && n_targets > 0) {
179 if (gtk_targets_include_text(targets, n_targets)) {
180 info->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
181 }
182 }
183
184 qemu_clipboard_update(info);
185 qemu_clipboard_info_unref(info);
186 }
187
188 static void gd_owner_change(GtkClipboard *clipboard,
189 GdkEvent *event,
190 gpointer data)
191 {
192 GtkDisplayState *gd = data;
193 QemuClipboardSelection s = gd_find_selection(gd, clipboard);
194 QemuClipboardInfo *info;
195
196 if (gd->cbowner[s]) {
197 /* ignore notifications about our own grabs */
198 return;
199 }
200
201
202 switch (event->owner_change.reason) {
203 case GDK_OWNER_CHANGE_NEW_OWNER:
204 info = qemu_clipboard_info_new(&gd->cbpeer, s);
205 gtk_clipboard_request_targets(
206 clipboard,
207 gd_clipboard_owner_change_targets_received_callback,
208 info);
209 break;
210 default:
211 qemu_clipboard_peer_release(&gd->cbpeer, s);
212 gd->cbowner[s] = false;
213 break;
214 }
215 }
216
217 void gd_clipboard_init(GtkDisplayState *gd)
218 {
219 gd->cbpeer.name = "gtk";
220 gd->cbpeer.notifier.notify = gd_clipboard_notify;
221 gd->cbpeer.request = gd_clipboard_request;
222 qemu_clipboard_peer_register(&gd->cbpeer);
223
224 gd->gtkcb[QEMU_CLIPBOARD_SELECTION_CLIPBOARD] =
225 gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
226 gd->gtkcb[QEMU_CLIPBOARD_SELECTION_PRIMARY] =
227 gtk_clipboard_get(GDK_SELECTION_PRIMARY);
228 gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY] =
229 gtk_clipboard_get(GDK_SELECTION_SECONDARY);
230
231 g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_CLIPBOARD],
232 "owner-change", G_CALLBACK(gd_owner_change), gd);
233 g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_PRIMARY],
234 "owner-change", G_CALLBACK(gd_owner_change), gd);
235 g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY],
236 "owner-change", G_CALLBACK(gd_owner_change), gd);
237 }
238
239 void gd_clipboard_cleanup(GtkDisplayState *gd)
240 {
241 if (!gd->cbpeer.name) {
242 return;
243 }
244 qemu_clipboard_peer_unregister(&gd->cbpeer);
245 g_signal_handlers_disconnect_by_data(
246 gd->gtkcb[QEMU_CLIPBOARD_SELECTION_CLIPBOARD], gd);
247 g_signal_handlers_disconnect_by_data(
248 gd->gtkcb[QEMU_CLIPBOARD_SELECTION_PRIMARY], gd);
249 g_signal_handlers_disconnect_by_data(
250 gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY], gd);
251 gd->cbpeer.name = NULL;
252 }