2 * GTK UI -- clipboard support
4 * Copyright (C) 2021 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "qemu-common.h"
23 #include "qemu/main-loop.h"
27 static QemuClipboardSelection
gd_find_selection(GtkDisplayState
*gd
,
28 GtkClipboard
*clipboard
)
30 QemuClipboardSelection s
;
32 for (s
= 0; s
< QEMU_CLIPBOARD_SELECTION__COUNT
; s
++) {
33 if (gd
->gtkcb
[s
] == clipboard
) {
37 return QEMU_CLIPBOARD_SELECTION_CLIPBOARD
;
40 static void gd_clipboard_get_data(GtkClipboard
*clipboard
,
41 GtkSelectionData
*selection_data
,
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
;
50 info
= qemu_clipboard_info_ref(qemu_clipboard_info(s
));
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);
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
);
64 /* clipboard owner changed while waiting for the data */
68 static void gd_clipboard_clear(GtkClipboard
*clipboard
,
71 GtkDisplayState
*gd
= data
;
72 QemuClipboardSelection s
= gd_find_selection(gd
, clipboard
);
74 gd
->cbowner
[s
] = false;
77 static void gd_clipboard_update_info(GtkDisplayState
*gd
,
78 QemuClipboardInfo
*info
)
80 QemuClipboardSelection s
= info
->selection
;
81 bool self_update
= info
->owner
== &gd
->cbpeer
;
83 if (info
!= qemu_clipboard_info(s
)) {
86 g_autoptr(GtkTargetList
) list
= NULL
;
87 GtkTargetEntry
*targets
;
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);
94 targets
= gtk_target_table_new_from_list(list
, &n_targets
);
96 gtk_clipboard_clear(gd
->gtkcb
[s
]);
98 gd
->cbowner
[s
] = true;
99 gtk_clipboard_set_with_data(gd
->gtkcb
[s
],
101 gd_clipboard_get_data
,
105 gtk_target_table_free(targets
, n_targets
);
116 * Clipboard got updated, with data probably. No action here, we
117 * are waiting for updates in gd_clipboard_get_data().
121 static void gd_clipboard_notify(Notifier
*notifier
, void *data
)
123 GtkDisplayState
*gd
=
124 container_of(notifier
, GtkDisplayState
, cbpeer
.notifier
);
125 QemuClipboardNotify
*notify
= data
;
127 switch (notify
->type
) {
128 case QEMU_CLIPBOARD_UPDATE_INFO
:
129 gd_clipboard_update_info(gd
, notify
->info
);
131 case QEMU_CLIPBOARD_RESET_SERIAL
:
137 static void gd_clipboard_request(QemuClipboardInfo
*info
,
138 QemuClipboardType type
)
140 GtkDisplayState
*gd
= container_of(info
->owner
, GtkDisplayState
, cbpeer
);
144 case QEMU_CLIPBOARD_TYPE_TEXT
:
145 text
= gtk_clipboard_wait_for_text(gd
->gtkcb
[info
->selection
]);
147 qemu_clipboard_set_data(&gd
->cbpeer
, info
, type
,
148 strlen(text
), text
, true);
157 static void gd_owner_change(GtkClipboard
*clipboard
,
161 GtkDisplayState
*gd
= data
;
162 QemuClipboardSelection s
= gd_find_selection(gd
, clipboard
);
163 QemuClipboardInfo
*info
;
165 if (gd
->cbowner
[s
]) {
166 /* ignore notifications about our own grabs */
171 switch (event
->owner_change
.reason
) {
172 case GDK_OWNER_CHANGE_NEW_OWNER
:
173 info
= qemu_clipboard_info_new(&gd
->cbpeer
, s
);
174 if (gtk_clipboard_wait_is_text_available(clipboard
)) {
175 info
->types
[QEMU_CLIPBOARD_TYPE_TEXT
].available
= true;
178 qemu_clipboard_update(info
);
179 qemu_clipboard_info_unref(info
);
182 qemu_clipboard_peer_release(&gd
->cbpeer
, s
);
183 gd
->cbowner
[s
] = false;
188 void gd_clipboard_init(GtkDisplayState
*gd
)
190 gd
->cbpeer
.name
= "gtk";
191 gd
->cbpeer
.notifier
.notify
= gd_clipboard_notify
;
192 gd
->cbpeer
.request
= gd_clipboard_request
;
193 qemu_clipboard_peer_register(&gd
->cbpeer
);
195 gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_CLIPBOARD
] =
196 gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
197 gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_PRIMARY
] =
198 gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
199 gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_SECONDARY
] =
200 gtk_clipboard_get(GDK_SELECTION_SECONDARY
);
202 g_signal_connect(gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_CLIPBOARD
],
203 "owner-change", G_CALLBACK(gd_owner_change
), gd
);
204 g_signal_connect(gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_PRIMARY
],
205 "owner-change", G_CALLBACK(gd_owner_change
), gd
);
206 g_signal_connect(gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_SECONDARY
],
207 "owner-change", G_CALLBACK(gd_owner_change
), gd
);