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 QemuClipboardInfo
*info
= qemu_clipboard_info_ref(gd
->cbinfo
[s
]);
50 qemu_clipboard_request(info
, type
);
51 while (info
== gd
->cbinfo
[s
] &&
52 info
->types
[type
].available
&&
53 info
->types
[type
].data
== NULL
) {
54 main_loop_wait(false);
57 if (info
== gd
->cbinfo
[s
] && gd
->cbowner
[s
]) {
58 gtk_selection_data_set_text(selection_data
,
59 info
->types
[type
].data
,
60 info
->types
[type
].size
);
62 /* clipboard owner changed while waiting for the data */
65 qemu_clipboard_info_unref(info
);
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_notify(Notifier
*notifier
, void *data
)
79 GtkDisplayState
*gd
= container_of(notifier
, GtkDisplayState
, cbpeer
.update
);
80 QemuClipboardInfo
*info
= data
;
81 QemuClipboardSelection s
= info
->selection
;
82 bool self_update
= info
->owner
== &gd
->cbpeer
;
84 if (info
!= gd
->cbinfo
[s
]) {
85 qemu_clipboard_info_unref(gd
->cbinfo
[s
]);
86 gd
->cbinfo
[s
] = qemu_clipboard_info_ref(info
);
90 GtkTargetEntry
*targets
;
93 list
= gtk_target_list_new(NULL
, 0);
94 if (info
->types
[QEMU_CLIPBOARD_TYPE_TEXT
].available
) {
95 gtk_target_list_add_text_targets(list
, 0);
97 targets
= gtk_target_table_new_from_list(list
, &n_targets
);
99 gtk_clipboard_clear(gd
->gtkcb
[s
]);
100 gd
->cbowner
[s
] = true;
101 gtk_clipboard_set_with_data(gd
->gtkcb
[s
],
103 gd_clipboard_get_data
,
107 gtk_target_table_free(targets
, n_targets
);
108 gtk_target_list_unref(list
);
118 * Clipboard got updated, with data probably. No action here, we
119 * are waiting for updates in gd_clipboard_get_data().
123 static void gd_clipboard_request(QemuClipboardInfo
*info
,
124 QemuClipboardType type
)
126 GtkDisplayState
*gd
= container_of(info
->owner
, GtkDisplayState
, cbpeer
);
130 case QEMU_CLIPBOARD_TYPE_TEXT
:
131 text
= gtk_clipboard_wait_for_text(gd
->gtkcb
[info
->selection
]);
133 qemu_clipboard_set_data(&gd
->cbpeer
, info
, type
,
134 strlen(text
), text
, true);
143 static void gd_owner_change(GtkClipboard
*clipboard
,
147 GtkDisplayState
*gd
= data
;
148 QemuClipboardSelection s
= gd_find_selection(gd
, clipboard
);
149 QemuClipboardInfo
*info
;
151 if (gd
->cbowner
[s
]) {
152 /* ignore notifications about our own grabs */
157 switch (event
->owner_change
.reason
) {
158 case GDK_SETTING_ACTION_NEW
:
159 info
= qemu_clipboard_info_new(&gd
->cbpeer
, s
);
160 if (gtk_clipboard_wait_is_text_available(clipboard
)) {
161 info
->types
[QEMU_CLIPBOARD_TYPE_TEXT
].available
= true;
164 qemu_clipboard_update(info
);
165 qemu_clipboard_info_unref(info
);
172 void gd_clipboard_init(GtkDisplayState
*gd
)
174 gd
->cbpeer
.name
= "gtk";
175 gd
->cbpeer
.update
.notify
= gd_clipboard_notify
;
176 gd
->cbpeer
.request
= gd_clipboard_request
;
177 qemu_clipboard_peer_register(&gd
->cbpeer
);
179 gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_CLIPBOARD
] =
180 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE
));
181 gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_PRIMARY
] =
182 gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE
));
183 gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_SECONDARY
] =
184 gtk_clipboard_get(gdk_atom_intern("SECONDARY", FALSE
));
186 g_signal_connect(gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_CLIPBOARD
],
187 "owner-change", G_CALLBACK(gd_owner_change
), gd
);
188 g_signal_connect(gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_PRIMARY
],
189 "owner-change", G_CALLBACK(gd_owner_change
), gd
);
190 g_signal_connect(gd
->gtkcb
[QEMU_CLIPBOARD_SELECTION_SECONDARY
],
191 "owner-change", G_CALLBACK(gd_owner_change
), gd
);