Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 1 | #include "qemu/osdep.h" |
| 2 | #include "ui/clipboard.h" |
Marc-André Lureau | 410840c | 2022-09-12 14:24:51 +0400 | [diff] [blame] | 3 | #include "trace.h" |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 4 | |
| 5 | static NotifierList clipboard_notifiers = |
| 6 | NOTIFIER_LIST_INITIALIZER(clipboard_notifiers); |
| 7 | |
Marc-André Lureau | 684e64d | 2021-08-05 17:57:02 +0400 | [diff] [blame] | 8 | static QemuClipboardInfo *cbinfo[QEMU_CLIPBOARD_SELECTION__COUNT]; |
| 9 | |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 10 | void qemu_clipboard_peer_register(QemuClipboardPeer *peer) |
| 11 | { |
Marc-André Lureau | 1b17f1e | 2021-07-19 19:42:15 +0400 | [diff] [blame] | 12 | notifier_list_add(&clipboard_notifiers, &peer->notifier); |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | void qemu_clipboard_peer_unregister(QemuClipboardPeer *peer) |
| 16 | { |
Marc-André Lureau | 7424bfa | 2021-08-05 17:57:05 +0400 | [diff] [blame] | 17 | int i; |
| 18 | |
| 19 | for (i = 0; i < QEMU_CLIPBOARD_SELECTION__COUNT; i++) { |
| 20 | qemu_clipboard_peer_release(peer, i); |
| 21 | } |
Marc-André Lureau | 1b17f1e | 2021-07-19 19:42:15 +0400 | [diff] [blame] | 22 | notifier_remove(&peer->notifier); |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 23 | } |
| 24 | |
Marc-André Lureau | 482bbaf | 2021-08-05 17:57:03 +0400 | [diff] [blame] | 25 | bool qemu_clipboard_peer_owns(QemuClipboardPeer *peer, |
| 26 | QemuClipboardSelection selection) |
| 27 | { |
| 28 | QemuClipboardInfo *info = qemu_clipboard_info(selection); |
| 29 | |
| 30 | return info && info->owner == peer; |
| 31 | } |
| 32 | |
Marc-André Lureau | 1387865 | 2021-08-05 17:57:04 +0400 | [diff] [blame] | 33 | void qemu_clipboard_peer_release(QemuClipboardPeer *peer, |
| 34 | QemuClipboardSelection selection) |
| 35 | { |
| 36 | g_autoptr(QemuClipboardInfo) info = NULL; |
| 37 | |
| 38 | if (qemu_clipboard_peer_owns(peer, selection)) { |
| 39 | /* set empty clipboard info */ |
| 40 | info = qemu_clipboard_info_new(NULL, selection); |
| 41 | qemu_clipboard_update(info); |
| 42 | } |
| 43 | } |
| 44 | |
Marc-André Lureau | 349504e | 2021-07-21 15:19:13 +0400 | [diff] [blame] | 45 | bool qemu_clipboard_check_serial(QemuClipboardInfo *info, bool client) |
| 46 | { |
Marc-André Lureau | 410840c | 2022-09-12 14:24:51 +0400 | [diff] [blame] | 47 | bool ok; |
| 48 | |
Marc-André Lureau | 349504e | 2021-07-21 15:19:13 +0400 | [diff] [blame] | 49 | if (!info->has_serial || |
| 50 | !cbinfo[info->selection] || |
| 51 | !cbinfo[info->selection]->has_serial) { |
Marc-André Lureau | 410840c | 2022-09-12 14:24:51 +0400 | [diff] [blame] | 52 | trace_clipboard_check_serial(-1, -1, true); |
Marc-André Lureau | 349504e | 2021-07-21 15:19:13 +0400 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
| 56 | if (client) { |
Marc-André Lureau | 0e23ae9 | 2022-09-12 14:24:52 +0400 | [diff] [blame] | 57 | ok = info->serial >= cbinfo[info->selection]->serial; |
Marc-André Lureau | 349504e | 2021-07-21 15:19:13 +0400 | [diff] [blame] | 58 | } else { |
Marc-André Lureau | 0e23ae9 | 2022-09-12 14:24:52 +0400 | [diff] [blame] | 59 | ok = info->serial > cbinfo[info->selection]->serial; |
Marc-André Lureau | 349504e | 2021-07-21 15:19:13 +0400 | [diff] [blame] | 60 | } |
Marc-André Lureau | 410840c | 2022-09-12 14:24:51 +0400 | [diff] [blame] | 61 | |
| 62 | trace_clipboard_check_serial(cbinfo[info->selection]->serial, info->serial, ok); |
| 63 | return ok; |
Marc-André Lureau | 349504e | 2021-07-21 15:19:13 +0400 | [diff] [blame] | 64 | } |
| 65 | |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 66 | void qemu_clipboard_update(QemuClipboardInfo *info) |
| 67 | { |
Fiona Ebner | 9c41658 | 2024-01-24 11:57:49 +0100 | [diff] [blame] | 68 | uint32_t type; |
Marc-André Lureau | 1b17f1e | 2021-07-19 19:42:15 +0400 | [diff] [blame] | 69 | QemuClipboardNotify notify = { |
| 70 | .type = QEMU_CLIPBOARD_UPDATE_INFO, |
| 71 | .info = info, |
| 72 | }; |
Marc-André Lureau | 684e64d | 2021-08-05 17:57:02 +0400 | [diff] [blame] | 73 | assert(info->selection < QEMU_CLIPBOARD_SELECTION__COUNT); |
| 74 | |
Fiona Ebner | 9c41658 | 2024-01-24 11:57:49 +0100 | [diff] [blame] | 75 | for (type = 0; type < QEMU_CLIPBOARD_TYPE__COUNT; type++) { |
| 76 | /* |
| 77 | * If data is missing, the clipboard owner's 'request' callback needs to |
| 78 | * be set. Otherwise, there is no way to get the clipboard data and |
| 79 | * qemu_clipboard_request() cannot be called. |
| 80 | */ |
| 81 | if (info->types[type].available && !info->types[type].data) { |
| 82 | assert(info->owner && info->owner->request); |
| 83 | } |
| 84 | } |
| 85 | |
Marc-André Lureau | 1b17f1e | 2021-07-19 19:42:15 +0400 | [diff] [blame] | 86 | notifier_list_notify(&clipboard_notifiers, ¬ify); |
Marc-André Lureau | 684e64d | 2021-08-05 17:57:02 +0400 | [diff] [blame] | 87 | |
Marc-André Lureau | 02a8ee2 | 2022-02-14 15:59:17 +0400 | [diff] [blame] | 88 | if (cbinfo[info->selection] != info) { |
| 89 | qemu_clipboard_info_unref(cbinfo[info->selection]); |
| 90 | cbinfo[info->selection] = qemu_clipboard_info_ref(info); |
| 91 | } |
Marc-André Lureau | 684e64d | 2021-08-05 17:57:02 +0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | QemuClipboardInfo *qemu_clipboard_info(QemuClipboardSelection selection) |
| 95 | { |
| 96 | assert(selection < QEMU_CLIPBOARD_SELECTION__COUNT); |
| 97 | |
| 98 | return cbinfo[selection]; |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | QemuClipboardInfo *qemu_clipboard_info_new(QemuClipboardPeer *owner, |
| 102 | QemuClipboardSelection selection) |
| 103 | { |
| 104 | QemuClipboardInfo *info = g_new0(QemuClipboardInfo, 1); |
| 105 | |
| 106 | info->owner = owner; |
| 107 | info->selection = selection; |
| 108 | info->refcount = 1; |
| 109 | |
| 110 | return info; |
| 111 | } |
| 112 | |
| 113 | QemuClipboardInfo *qemu_clipboard_info_ref(QemuClipboardInfo *info) |
| 114 | { |
| 115 | info->refcount++; |
| 116 | return info; |
| 117 | } |
| 118 | |
| 119 | void qemu_clipboard_info_unref(QemuClipboardInfo *info) |
| 120 | { |
| 121 | uint32_t type; |
| 122 | |
| 123 | if (!info) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | info->refcount--; |
| 128 | if (info->refcount > 0) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | for (type = 0; type < QEMU_CLIPBOARD_TYPE__COUNT; type++) { |
| 133 | g_free(info->types[type].data); |
| 134 | } |
| 135 | g_free(info); |
| 136 | } |
| 137 | |
| 138 | void qemu_clipboard_request(QemuClipboardInfo *info, |
| 139 | QemuClipboardType type) |
| 140 | { |
| 141 | if (info->types[type].data || |
| 142 | info->types[type].requested || |
| 143 | !info->types[type].available || |
| 144 | !info->owner) |
| 145 | return; |
| 146 | |
Fiona Ebner | 9c41658 | 2024-01-24 11:57:49 +0100 | [diff] [blame] | 147 | assert(info->owner->request); |
| 148 | |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 149 | info->types[type].requested = true; |
| 150 | info->owner->request(info, type); |
| 151 | } |
| 152 | |
Marc-André Lureau | 505dbf9 | 2021-07-19 19:49:56 +0400 | [diff] [blame] | 153 | void qemu_clipboard_reset_serial(void) |
| 154 | { |
| 155 | QemuClipboardNotify notify = { .type = QEMU_CLIPBOARD_RESET_SERIAL }; |
Marc-André Lureau | 72ce36f | 2022-09-12 14:24:54 +0400 | [diff] [blame] | 156 | int i; |
Marc-André Lureau | 505dbf9 | 2021-07-19 19:49:56 +0400 | [diff] [blame] | 157 | |
Marc-André Lureau | 63a5d4d | 2024-07-17 21:15:39 +0400 | [diff] [blame] | 158 | trace_clipboard_reset_serial(); |
| 159 | |
Marc-André Lureau | 72ce36f | 2022-09-12 14:24:54 +0400 | [diff] [blame] | 160 | for (i = 0; i < QEMU_CLIPBOARD_SELECTION__COUNT; i++) { |
| 161 | QemuClipboardInfo *info = qemu_clipboard_info(i); |
| 162 | if (info) { |
| 163 | info->serial = 0; |
| 164 | } |
| 165 | } |
Marc-André Lureau | 505dbf9 | 2021-07-19 19:49:56 +0400 | [diff] [blame] | 166 | notifier_list_notify(&clipboard_notifiers, ¬ify); |
| 167 | } |
| 168 | |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 169 | void qemu_clipboard_set_data(QemuClipboardPeer *peer, |
| 170 | QemuClipboardInfo *info, |
| 171 | QemuClipboardType type, |
| 172 | uint32_t size, |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 173 | const void *data, |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 174 | bool update) |
| 175 | { |
| 176 | if (!info || |
| 177 | info->owner != peer) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | g_free(info->types[type].data); |
Fiona Ebner | 405484b | 2024-01-24 11:57:48 +0100 | [diff] [blame] | 182 | if (size) { |
| 183 | info->types[type].data = g_memdup2(data, size); |
| 184 | info->types[type].size = size; |
| 185 | info->types[type].available = true; |
| 186 | } else { |
| 187 | info->types[type].data = NULL; |
| 188 | info->types[type].size = 0; |
| 189 | info->types[type].available = false; |
| 190 | } |
Gerd Hoffmann | 660e8d0 | 2021-05-19 07:39:33 +0200 | [diff] [blame] | 191 | |
| 192 | if (update) { |
| 193 | qemu_clipboard_update(info); |
| 194 | } |
| 195 | } |