gtk-clipboard.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "qemu/osdep.h"
  21. #include "qemu/main-loop.h"
  22. #include "ui/gtk.h"
  23. static QemuClipboardSelection gd_find_selection(GtkDisplayState *gd,
  24. GtkClipboard *clipboard)
  25. {
  26. QemuClipboardSelection s;
  27. for (s = 0; s < QEMU_CLIPBOARD_SELECTION__COUNT; s++) {
  28. if (gd->gtkcb[s] == clipboard) {
  29. return s;
  30. }
  31. }
  32. return QEMU_CLIPBOARD_SELECTION_CLIPBOARD;
  33. }
  34. static void gd_clipboard_get_data(GtkClipboard *clipboard,
  35. GtkSelectionData *selection_data,
  36. guint selection_info,
  37. gpointer data)
  38. {
  39. GtkDisplayState *gd = data;
  40. QemuClipboardSelection s = gd_find_selection(gd, clipboard);
  41. QemuClipboardType type = QEMU_CLIPBOARD_TYPE_TEXT;
  42. g_autoptr(QemuClipboardInfo) info = NULL;
  43. info = qemu_clipboard_info_ref(qemu_clipboard_info(s));
  44. qemu_clipboard_request(info, type);
  45. while (info == qemu_clipboard_info(s) &&
  46. info->types[type].available &&
  47. info->types[type].data == NULL) {
  48. main_loop_wait(false);
  49. }
  50. if (info == qemu_clipboard_info(s) && gd->cbowner[s]) {
  51. gtk_selection_data_set_text(selection_data,
  52. info->types[type].data,
  53. info->types[type].size);
  54. } else {
  55. /* clipboard owner changed while waiting for the data */
  56. }
  57. }
  58. static void gd_clipboard_clear(GtkClipboard *clipboard,
  59. gpointer data)
  60. {
  61. GtkDisplayState *gd = data;
  62. QemuClipboardSelection s = gd_find_selection(gd, clipboard);
  63. gd->cbowner[s] = false;
  64. }
  65. static void gd_clipboard_update_info(GtkDisplayState *gd,
  66. QemuClipboardInfo *info)
  67. {
  68. QemuClipboardSelection s = info->selection;
  69. bool self_update = info->owner == &gd->cbpeer;
  70. if (info != qemu_clipboard_info(s)) {
  71. gd->cbpending[s] = 0;
  72. if (!self_update) {
  73. g_autoptr(GtkTargetList) list = NULL;
  74. GtkTargetEntry *targets;
  75. gint n_targets;
  76. list = gtk_target_list_new(NULL, 0);
  77. if (info->types[QEMU_CLIPBOARD_TYPE_TEXT].available) {
  78. gtk_target_list_add_text_targets(list, 0);
  79. }
  80. targets = gtk_target_table_new_from_list(list, &n_targets);
  81. gtk_clipboard_clear(gd->gtkcb[s]);
  82. if (targets) {
  83. gd->cbowner[s] = true;
  84. gtk_clipboard_set_with_data(gd->gtkcb[s],
  85. targets, n_targets,
  86. gd_clipboard_get_data,
  87. gd_clipboard_clear,
  88. gd);
  89. gtk_target_table_free(targets, n_targets);
  90. }
  91. }
  92. return;
  93. }
  94. if (self_update) {
  95. return;
  96. }
  97. /*
  98. * Clipboard got updated, with data probably. No action here, we
  99. * are waiting for updates in gd_clipboard_get_data().
  100. */
  101. }
  102. static void gd_clipboard_notify(Notifier *notifier, void *data)
  103. {
  104. GtkDisplayState *gd =
  105. container_of(notifier, GtkDisplayState, cbpeer.notifier);
  106. QemuClipboardNotify *notify = data;
  107. switch (notify->type) {
  108. case QEMU_CLIPBOARD_UPDATE_INFO:
  109. gd_clipboard_update_info(gd, notify->info);
  110. return;
  111. case QEMU_CLIPBOARD_RESET_SERIAL:
  112. /* ignore */
  113. return;
  114. }
  115. }
  116. static void gd_clipboard_request(QemuClipboardInfo *info,
  117. QemuClipboardType type)
  118. {
  119. GtkDisplayState *gd = container_of(info->owner, GtkDisplayState, cbpeer);
  120. char *text;
  121. switch (type) {
  122. case QEMU_CLIPBOARD_TYPE_TEXT:
  123. text = gtk_clipboard_wait_for_text(gd->gtkcb[info->selection]);
  124. if (text) {
  125. qemu_clipboard_set_data(&gd->cbpeer, info, type,
  126. strlen(text), text, true);
  127. g_free(text);
  128. }
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. static void gd_owner_change(GtkClipboard *clipboard,
  135. GdkEvent *event,
  136. gpointer data)
  137. {
  138. GtkDisplayState *gd = data;
  139. QemuClipboardSelection s = gd_find_selection(gd, clipboard);
  140. QemuClipboardInfo *info;
  141. if (gd->cbowner[s]) {
  142. /* ignore notifications about our own grabs */
  143. return;
  144. }
  145. switch (event->owner_change.reason) {
  146. case GDK_OWNER_CHANGE_NEW_OWNER:
  147. info = qemu_clipboard_info_new(&gd->cbpeer, s);
  148. if (gtk_clipboard_wait_is_text_available(clipboard)) {
  149. info->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
  150. }
  151. qemu_clipboard_update(info);
  152. qemu_clipboard_info_unref(info);
  153. break;
  154. default:
  155. qemu_clipboard_peer_release(&gd->cbpeer, s);
  156. gd->cbowner[s] = false;
  157. break;
  158. }
  159. }
  160. void gd_clipboard_init(GtkDisplayState *gd)
  161. {
  162. gd->cbpeer.name = "gtk";
  163. gd->cbpeer.notifier.notify = gd_clipboard_notify;
  164. gd->cbpeer.request = gd_clipboard_request;
  165. qemu_clipboard_peer_register(&gd->cbpeer);
  166. gd->gtkcb[QEMU_CLIPBOARD_SELECTION_CLIPBOARD] =
  167. gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
  168. gd->gtkcb[QEMU_CLIPBOARD_SELECTION_PRIMARY] =
  169. gtk_clipboard_get(GDK_SELECTION_PRIMARY);
  170. gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY] =
  171. gtk_clipboard_get(GDK_SELECTION_SECONDARY);
  172. g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_CLIPBOARD],
  173. "owner-change", G_CALLBACK(gd_owner_change), gd);
  174. g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_PRIMARY],
  175. "owner-change", G_CALLBACK(gd_owner_change), gd);
  176. g_signal_connect(gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY],
  177. "owner-change", G_CALLBACK(gd_owner_change), gd);
  178. }