Debian Codegraph @

gdk/wayland/gdkselection-wayland.c

raw
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include <gio/gunixinputstream.h>
24 #include <gio/gunixoutputstream.h>
25 #include <glib-unix.h>
26
27 #include "gdkwayland.h"
28 #include "gdkprivate-wayland.h"
29 #include "gdkdisplay-wayland.h"
30 #include "gdkdndprivate.h"
31 #include "gdkselection.h"
32 #include "gdkproperty.h"
33 #include "gdkprivate.h"
34
35 #include <string.h>
36
37 #define STRING_MIMETYPE "text/plain"
38 #define UTF8_STRING_MIMETYPE "text/plain;charset=utf-8"
39
40 typedef struct _SelectionBuffer SelectionBuffer;
41 typedef struct _SelectionData SelectionData;
42 typedef struct _StoredSelection StoredSelection;
43 typedef struct _AsyncWriteData AsyncWriteData;
44 typedef struct _DataOfferData DataOfferData;
45
46 struct _SelectionBuffer
47 {
48 GInputStream *stream;
49 GCancellable *cancellable;
50 GByteArray *data;
51 GList *requestors;
52 GdkAtom selection;
53 GdkAtom target;
54 gint ref_count;
55 };
56
57 struct _StoredSelection
58 {
59 GdkWaylandSelection *selection;
60 GdkWindow *source;
61 GCancellable *cancellable;
62 guchar *data;
63 gsize data_len;
64 GdkAtom type;
65 GdkAtom selection_atom;
66 GPtrArray *pending_writes; /* Array of AsyncWriteData */
67 };
68
69 struct _DataOfferData
70 {
71 GDestroyNotify destroy_notify;
72 gpointer offer_data;
73 GList *targets; /* List of GdkAtom */
74 };
75
76 struct _AsyncWriteData
77 {
78 GOutputStream *stream;
79 StoredSelection *stored_selection;
80 gsize index;
81 };
82
83 struct _SelectionData
84 {
85 DataOfferData *offer;
86 GHashTable *buffers; /* Hashtable of target_atom->SelectionBuffer */
87 };
88
89 enum {
90 ATOM_PRIMARY,
91 ATOM_CLIPBOARD,
92 ATOM_DND,
93 N_ATOMS
94 };
95
96 static GdkAtom atoms[N_ATOMS] = { 0 };
97
98 struct _GdkWaylandSelection
99 {
100 /* Destination-side data */
101 SelectionData selections[N_ATOMS];
102 GHashTable *offers; /* Currently alive offers, Hashtable of wl_data_offer->DataOfferData */
103
104 /* Source-side data */
105 GPtrArray *stored_selections; /* Array of StoredSelection */
106 StoredSelection *current_request_selection;
107 GArray *source_targets;
108 GdkAtom requested_target;
109
110 gpointer primary_source;
111 GdkWindow *primary_owner;
112
113 struct wl_data_source *clipboard_source;
114 GdkWindow *clipboard_owner;
115
116 struct wl_data_source *dnd_source; /* Owned by the GdkDragContext */
117 GdkWindow *dnd_owner;
118 };
119
120 static void selection_buffer_read (SelectionBuffer *buffer);
121 static void async_write_data_write (AsyncWriteData *write_data);
122 static void async_write_data_free (AsyncWriteData *write_data);
123 static void emit_selection_clear (GdkDisplay *display, GdkAtom selection);
124 static void emit_empty_selection_notify (GdkWindow *requestor,
125 GdkAtom selection,
126 GdkAtom target);
127 static void gdk_wayland_selection_handle_next_request (GdkWaylandSelection *wayland_selection);
128
129 static void
130 selection_buffer_notify (SelectionBuffer *buffer)
131 {
132 GdkEvent *event;
133 GList *l;
134
135 for (l = buffer->requestors; l; l = l->next)
136 {
137 event = gdk_event_new (GDK_SELECTION_NOTIFY);
138 event->selection.window = g_object_ref (l->data);
139 event->selection.send_event = FALSE;
140 event->selection.selection = buffer->selection;
141 event->selection.target = buffer->target;
142 event->selection.property = gdk_atom_intern_static_string ("GDK_SELECTION");
143 event->selection.time = GDK_CURRENT_TIME;
144 event->selection.requestor = g_object_ref (l->data);
145
146 gdk_event_put (event);
147 gdk_event_free (event);
148 }
149 }
150
151 static SelectionBuffer *
152 selection_buffer_new (GInputStream *stream,
153 GdkAtom selection,
154 GdkAtom target)
155 {
156 SelectionBuffer *buffer;
157
158 buffer = g_new0 (SelectionBuffer, 1);
159 buffer->stream = (stream) ? g_object_ref (stream) : NULL;
160 buffer->cancellable = g_cancellable_new ();
161 buffer->data = g_byte_array_new ();
162 buffer->selection = selection;
163 buffer->target = target;
164 buffer->ref_count = 1;
165
166 if (stream)
167 selection_buffer_read (buffer);
168
169 return buffer;
170 }
171
172 static SelectionBuffer *
173 selection_buffer_ref (SelectionBuffer *buffer)
174 {
175 buffer->ref_count++;
176 return buffer;
177 }
178
179 static void
180 selection_buffer_unref (SelectionBuffer *buffer_data)
181 {
182 GList *l;
183
184 buffer_data->ref_count--;
185
186 if (buffer_data->ref_count != 0)
187 return;
188
189 for (l = buffer_data->requestors; l; l = l->next)
190 {
191 emit_empty_selection_notify (l->data, buffer_data->selection,
192 buffer_data->target);
193 }
194
195 g_list_free (buffer_data->requestors);
196 buffer_data->requestors = NULL;
197
198 if (buffer_data->cancellable)
199 g_object_unref (buffer_data->cancellable);
200
201 if (buffer_data->stream)
202 g_object_unref (buffer_data->stream);
203
204 if (buffer_data->data)
205 g_byte_array_unref (buffer_data->data);
206
207 g_free (buffer_data);
208 }
209
210 static void
211 selection_buffer_append_data (SelectionBuffer *buffer,
212 gconstpointer data,
213 gsize len)
214 {
215 g_byte_array_append (buffer->data, data, len);
216 }
217
218 static void
219 selection_buffer_cancel_and_unref (SelectionBuffer *buffer_data)
220 {
221 if (buffer_data->cancellable)
222 g_cancellable_cancel (buffer_data->cancellable);
223
224 selection_buffer_unref (buffer_data);
225 }
226
227 static void
228 selection_buffer_add_requestor (SelectionBuffer *buffer,
229 GdkWindow *requestor)
230 {
231 if (!g_list_find (buffer->requestors, requestor))
232 buffer->requestors = g_list_prepend (buffer->requestors,
233 g_object_ref (requestor));
234 }
235
236 static gboolean
237 selection_buffer_remove_requestor (SelectionBuffer *buffer,
238 GdkWindow *requestor)
239 {
240 GList *link = g_list_find (buffer->requestors, requestor);
241
242 if (!link)
243 return FALSE;
244
245 g_object_unref (link->data);
246 buffer->requestors = g_list_delete_link (buffer->requestors, link);
247 return TRUE;
248 }
249
250 static inline glong
251 get_buffer_size (void)
252 {
253 return sysconf (_SC_PAGESIZE);
254 }
255
256 static void
257 selection_buffer_read_cb (GObject *object,
258 GAsyncResult *result,
259 gpointer user_data)
260 {
261 SelectionBuffer *buffer = user_data;
262 gboolean finished = TRUE;
263 GError *error = NULL;
264 GBytes *bytes;
265
266 bytes = g_input_stream_read_bytes_finish (buffer->stream, result, &error);
267
268 if (bytes)
269 {
270 finished = g_bytes_get_size (bytes) == 0;
271 selection_buffer_append_data (buffer,
272 g_bytes_get_data (bytes, NULL),
273 g_bytes_get_size (bytes));
274 g_bytes_unref (bytes);
275 }
276
277 if (!finished)
278 selection_buffer_read (buffer);
279 else
280 {
281 if (error)
282 {
283 g_warning (G_STRLOC ": error reading selection buffer: %s", error->message);
284 g_error_free (error);
285 }
286 else
287 selection_buffer_notify (buffer);
288
289 g_input_stream_close (buffer->stream, NULL, NULL);
290 g_clear_object (&buffer->stream);
291 g_clear_object (&buffer->cancellable);
292 }
293
294 selection_buffer_unref (buffer);
295 }
296
297 static void
298 selection_buffer_read (SelectionBuffer *buffer)
299 {
300 selection_buffer_ref (buffer);
301 g_input_stream_read_bytes_async (buffer->stream, get_buffer_size(),
302 G_PRIORITY_DEFAULT,
303 buffer->cancellable, selection_buffer_read_cb,
304 buffer);
305 }
306
307 static DataOfferData *
308 data_offer_data_new (gpointer offer,
309 GDestroyNotify destroy_notify)
310 {
311 DataOfferData *info;
312
313 info = g_slice_new0 (DataOfferData);
314 info->offer_data = offer;
315 info->destroy_notify = destroy_notify;
316
317 return info;
318 }
319
320 static void
321 data_offer_data_free (DataOfferData *info)
322 {
323 info->destroy_notify (info->offer_data);
324 g_list_free (info->targets);
325 g_slice_free (DataOfferData, info);
326 }
327
328 static StoredSelection *
329 stored_selection_new (GdkWaylandSelection *wayland_selection,
330 GdkWindow *source,
331 GdkAtom selection,
332 GdkAtom type)
333 {
334 StoredSelection *stored_selection;
335
336 stored_selection = g_new0 (StoredSelection, 1);
337 stored_selection->source = source;
338 stored_selection->type = type;
339 stored_selection->selection_atom = selection;
340 stored_selection->selection = wayland_selection;
341 stored_selection->cancellable = g_cancellable_new ();
342 stored_selection->pending_writes =
343 g_ptr_array_new_with_free_func ((GDestroyNotify) async_write_data_free);
344
345 return stored_selection;
346 }
347
348 static void
349 stored_selection_add_data (StoredSelection *stored_selection,
350 GdkPropMode mode,
351 const guchar *data,
352 gsize data_len)
353 {
354 if (mode == GDK_PROP_MODE_REPLACE)
355 {
356 g_free (stored_selection->data);
357 stored_selection->data = g_memdup (data, data_len);
358 stored_selection->data_len = data_len;
359 }
360 else
361 {
362 GArray *array;
363
364 array = g_array_new (TRUE, TRUE, sizeof (guchar));
365 g_array_append_vals (array, stored_selection->data, stored_selection->data_len);
366
367 if (mode == GDK_PROP_MODE_APPEND)
368 g_array_append_vals (array, data, data_len);
369 else if (mode == GDK_PROP_MODE_PREPEND)
370 g_array_prepend_vals (array, data, data_len);
371
372 g_free (stored_selection->data);
373 stored_selection->data_len = array->len;
374 stored_selection->data = (guchar *) g_array_free (array, FALSE);
375 }
376 }
377
378 static void
379 stored_selection_free (StoredSelection *stored_selection)
380 {
381 g_cancellable_cancel (stored_selection->cancellable);
382 g_object_unref (stored_selection->cancellable);
383 g_ptr_array_unref (stored_selection->pending_writes);
384 g_free (stored_selection->data);
385 g_free (stored_selection);
386 }
387
388 static void
389 stored_selection_notify_write (StoredSelection *stored_selection)
390 {
391 gint i;
392
393 for (i = 0; i < stored_selection->pending_writes->len; i++)
394 {
395 AsyncWriteData *write_data;
396
397 write_data = g_ptr_array_index (stored_selection->pending_writes, i);
398 async_write_data_write (write_data);
399 }
400 }
401
402 static void
403 stored_selection_cancel_write (StoredSelection *stored_selection)
404 {
405 g_cancellable_cancel (stored_selection->cancellable);
406 g_object_unref (stored_selection->cancellable);
407 stored_selection->cancellable = g_cancellable_new ();
408 g_ptr_array_set_size (stored_selection->pending_writes, 0);
409 }
410
411 GdkWaylandSelection *
412 gdk_wayland_selection_new (void)
413 {
414 GdkWaylandSelection *selection;
415 gint i;
416
417 /* init atoms */
418 atoms[ATOM_PRIMARY] = gdk_atom_intern_static_string ("PRIMARY");
419 atoms[ATOM_CLIPBOARD] = gdk_atom_intern_static_string ("CLIPBOARD");
420 atoms[ATOM_DND] = gdk_atom_intern_static_string ("GdkWaylandSelection");
421
422 selection = g_new0 (GdkWaylandSelection, 1);
423 for (i = 0; i < G_N_ELEMENTS (selection->selections); i++)
424 {
425 selection->selections[i].buffers =
426 g_hash_table_new_full (NULL, NULL, NULL,
427 (GDestroyNotify) selection_buffer_cancel_and_unref);
428 }
429
430 selection->offers =
431 g_hash_table_new_full (NULL, NULL, NULL,
432 (GDestroyNotify) data_offer_data_free);
433 selection->stored_selections =
434 g_ptr_array_new_with_free_func ((GDestroyNotify) stored_selection_free);
435
436 selection->source_targets = g_array_new (FALSE, FALSE, sizeof (GdkAtom));
437 return selection;
438 }
439
440 static void
441 primary_selection_source_destroy (gpointer primary_source)
442 {
443 GdkDisplay *display = gdk_display_get_default ();
444 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
445
446 if (display_wayland->zwp_primary_selection_manager_v1)
447 zwp_primary_selection_source_v1_destroy (primary_source);
448 else if (display_wayland->gtk_primary_selection_manager)
449 gtk_primary_selection_source_destroy (primary_source);
450 }
451
452 void
453 gdk_wayland_selection_free (GdkWaylandSelection *selection)
454 {
455 gint i;
456
457 for (i = 0; i < G_N_ELEMENTS (selection->selections); i++)
458 g_hash_table_destroy (selection->selections[i].buffers);
459
460 g_array_unref (selection->source_targets);
461
462 g_hash_table_destroy (selection->offers);
463 g_ptr_array_unref (selection->stored_selections);
464
465 if (selection->primary_source)
466 primary_selection_source_destroy (selection->primary_source);
467 if (selection->clipboard_source)
468 wl_data_source_destroy (selection->clipboard_source);
469 if (selection->dnd_source)
470 wl_data_source_destroy (selection->dnd_source);
471
472 g_free (selection);
473 }
474
475 static void
476 data_offer_offer (void *data,
477 struct wl_data_offer *wl_data_offer,
478 const char *type)
479 {
480 GdkWaylandSelection *selection = data;
481 DataOfferData *info;
482 GdkAtom atom = gdk_atom_intern (type, FALSE);
483
484 info = g_hash_table_lookup (selection->offers, wl_data_offer);
485
486 if (!info || g_list_find (info->targets, atom))
487 return;
488
489 GDK_NOTE (EVENTS,
490 g_message ("data offer offer, offer %p, type = %s", wl_data_offer, type));
491
492 info->targets = g_list_prepend (info->targets, atom);
493 }
494
495 static inline GdkDragAction
496 _wl_to_gdk_actions (uint32_t dnd_actions)
497 {
498 GdkDragAction actions = 0;
499
500 if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY)
501 actions |= GDK_ACTION_COPY;
502 if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE)
503 actions |= GDK_ACTION_MOVE;
504 if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
505 actions |= GDK_ACTION_ASK;
506
507 return actions;
508 }
509
510 static void
511 data_offer_source_actions (void *data,
512 struct wl_data_offer *wl_data_offer,
513 uint32_t source_actions)
514 {
515 GdkDragContext *drop_context;
516 GdkDisplay *display;
517 GdkDevice *device;
518 GdkSeat *seat;
519
520 display = gdk_display_get_default ();
521 seat = gdk_display_get_default_seat (display);
522 device = gdk_seat_get_pointer (seat);
523 drop_context = gdk_wayland_device_get_drop_context (device);
524
525 drop_context->actions = _wl_to_gdk_actions (source_actions);
526
527 GDK_NOTE (EVENTS,
528 g_message ("data offer source actions, offer %p, actions %d", wl_data_offer, source_actions));
529
530 if (gdk_drag_context_get_dest_window (drop_context))
531 _gdk_wayland_drag_context_emit_event (drop_context, GDK_DRAG_MOTION,
532 GDK_CURRENT_TIME);
533 }
534
535 static void
536 data_offer_action (void *data,
537 struct wl_data_offer *wl_data_offer,
538 uint32_t action)
539 {
540 GdkDragContext *drop_context;
541 GdkDisplay *display;
542 GdkDevice *device;
543 GdkSeat *seat;
544
545 display = gdk_display_get_default ();
546 seat = gdk_display_get_default_seat (display);
547 device = gdk_seat_get_pointer (seat);
548 drop_context = gdk_wayland_device_get_drop_context (device);
549
550 drop_context->action = _wl_to_gdk_actions (action);
551
552 if (gdk_drag_context_get_dest_window (drop_context))
553 _gdk_wayland_drag_context_emit_event (drop_context, GDK_DRAG_MOTION,
554 GDK_CURRENT_TIME);
555 }
556
557 static const struct wl_data_offer_listener data_offer_listener = {
558 data_offer_offer,
559 data_offer_source_actions,
560 data_offer_action
561 };
562
563 static void
564 primary_offer_offer (void *data,
565 gpointer offer,
566 const char *type)
567 {
568 GdkWaylandSelection *selection = data;
569 DataOfferData *info;
570 GdkAtom atom = gdk_atom_intern (type, FALSE);
571
572 info = g_hash_table_lookup (selection->offers, offer);
573
574 if (!info || g_list_find (info->targets, atom))
575 return;
576
577 GDK_NOTE (EVENTS,
578 g_message ("primary offer offer, offer %p, type = %s", offer, type));
579
580 info->targets = g_list_prepend (info->targets, atom);
581 }
582
583 static void
584 gtk_primary_offer_offer (void *data,
585 struct gtk_primary_selection_offer *offer,
586 const char *type)
587 {
588 primary_offer_offer (data, (gpointer) offer, type);
589 }
590
591 static void
592 zwp_primary_offer_v1_offer (void *data,
593 struct zwp_primary_selection_offer_v1 *offer,
594 const char *type)
595 {
596 primary_offer_offer (data, (gpointer) offer, type);
597 }
598
599 static const struct gtk_primary_selection_offer_listener gtk_primary_offer_listener = {
600 gtk_primary_offer_offer,
601 };
602
603 static const struct zwp_primary_selection_offer_v1_listener zwp_primary_offer_listener_v1 = {
604 zwp_primary_offer_v1_offer,
605 };
606
607 SelectionData *
608 selection_lookup_offer_by_atom (GdkWaylandSelection *selection,
609 GdkAtom selection_atom)
610 {
611 if (selection_atom == atoms[ATOM_PRIMARY])
612 return &selection->selections[ATOM_PRIMARY];
613 else if (selection_atom == atoms[ATOM_CLIPBOARD])
614 return &selection->selections[ATOM_CLIPBOARD];
615 else if (selection_atom == atoms[ATOM_DND])
616 return &selection->selections[ATOM_DND];
617 else
618 return NULL;
619 }
620
621 void
622 gdk_wayland_selection_ensure_offer (GdkDisplay *display,
623 struct wl_data_offer *wl_offer)
624 {
625 GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
626 DataOfferData *info;
627
628 info = g_hash_table_lookup (selection->offers, wl_offer);
629
630 if (!info)
631 {
632 info = data_offer_data_new (wl_offer,
633 (GDestroyNotify) wl_data_offer_destroy);
634 g_hash_table_insert (selection->offers, wl_offer, info);
635 wl_data_offer_add_listener (wl_offer,
636 &data_offer_listener,
637 selection);
638 }
639 }
640
641 void
642 gdk_wayland_selection_ensure_primary_offer (GdkDisplay *display,
643 gpointer gtk_offer)
644 {
645 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
646 GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
647 DataOfferData *info;
648
649 info = g_hash_table_lookup (selection->offers, gtk_offer);
650
651 if (!info)
652 {
653 if (display_wayland->zwp_primary_selection_manager_v1)
654 {
655 info = data_offer_data_new (gtk_offer,
656 (GDestroyNotify) zwp_primary_selection_offer_v1_destroy);
657 g_hash_table_insert (selection->offers, gtk_offer, info);
658 zwp_primary_selection_offer_v1_add_listener (gtk_offer,
659 &zwp_primary_offer_listener_v1,
660 selection);
661 }
662 else if (display_wayland->gtk_primary_selection_manager)
663 {
664 info = data_offer_data_new (gtk_offer,
665 (GDestroyNotify) gtk_primary_selection_offer_destroy);
666 g_hash_table_insert (selection->offers, gtk_offer, info);
667 gtk_primary_selection_offer_add_listener (gtk_offer,
668 &gtk_primary_offer_listener,
669 selection);
670 }
671 }
672 }
673
674 void
675 gdk_wayland_selection_set_offer (GdkDisplay *display,
676 GdkAtom selection_atom,
677 gpointer wl_offer)
678 {
679 GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
680 struct wl_data_offer *prev_offer;
681 SelectionData *selection_data;
682 DataOfferData *info;
683
684 info = g_hash_table_lookup (selection->offers, wl_offer);
685
686 prev_offer = gdk_wayland_selection_get_offer (display, selection_atom);
687
688 if (prev_offer)
689 g_hash_table_remove (selection->offers, prev_offer);
690
691 selection_data = selection_lookup_offer_by_atom (selection, selection_atom);
692
693 if (selection_data)
694 {
695 selection_data->offer = info;
696 /* Clear all buffers */
697 g_hash_table_remove_all (selection_data->buffers);
698 }
699 }
700
701 gpointer
702 gdk_wayland_selection_get_offer (GdkDisplay *display,
703 GdkAtom selection_atom)
704 {
705 GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
706 const SelectionData *data;
707
708 data = selection_lookup_offer_by_atom (selection, selection_atom);
709
710 if (data && data->offer)
711 return data->offer->offer_data;
712
713 return NULL;
714 }
715
716 GList *
717 gdk_wayland_selection_get_targets (GdkDisplay *display,
718 GdkAtom selection_atom)
719 {
720 GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
721 const SelectionData *data;
722
723 data = selection_lookup_offer_by_atom (selection, selection_atom);
724
725 if (data && data->offer)
726 return data->offer->targets;
727
728 return NULL;
729 }
730
731 static void
732 gdk_wayland_selection_emit_request (GdkWindow *window,
733 GdkAtom selection,
734 GdkAtom target)
735 {
736 GdkEvent *event;
737
738 event = gdk_event_new (GDK_SELECTION_REQUEST);
739 event->selection.window = g_object_ref (window);
740 event->selection.send_event = FALSE;
741 event->selection.selection = selection;
742 event->selection.target = target;
743 event->selection.property = gdk_atom_intern_static_string ("GDK_SELECTION");
744 event->selection.time = GDK_CURRENT_TIME;
745 event->selection.requestor = g_object_ref (window);
746
747 gdk_event_put (event);
748 gdk_event_free (event);
749 }
750
751 static AsyncWriteData *
752 async_write_data_new (StoredSelection *stored_selection,
753 gint fd)
754 {
755 AsyncWriteData *write_data;
756
757 write_data = g_slice_new0 (AsyncWriteData);
758 write_data->stored_selection = stored_selection;
759 write_data->stream = g_unix_output_stream_new (fd, TRUE);
760 g_ptr_array_add (stored_selection->pending_writes, write_data);
761
762 return write_data;
763 }
764
765 static void
766 async_write_data_free (AsyncWriteData *write_data)
767 {
768 g_object_unref (write_data->stream);
769 g_slice_free (AsyncWriteData, write_data);
770 }
771
772 static void
773 async_write_data_cb (GObject *object,
774 GAsyncResult *res,
775 gpointer user_data)
776 {
777 AsyncWriteData *write_data = user_data;
778 GError *error = NULL;
779 gsize bytes_written;
780
781 bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (object),
782 res, &error);
783 if (error)
784 {
785 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
786 {
787 g_warning ("Error writing selection data: %s", error->message);
788 g_ptr_array_remove_fast (write_data->stored_selection->pending_writes,
789 write_data);
790 }
791
792 g_error_free (error);
793 return;
794 }
795
796 write_data->index += bytes_written;
797
798 if (write_data->index < write_data->stored_selection->data_len)
799 {
800 /* Write the next chunk */
801 async_write_data_write (write_data);
802 }
803 else
804 {
805 g_ptr_array_remove_fast (write_data->stored_selection->pending_writes,
806 write_data);
807 }
808 }
809
810 static void
811 async_write_data_write (AsyncWriteData *write_data)
812 {
813 gsize buf_len;
814 guchar *buf;
815
816 buf = write_data->stored_selection->data;
817 buf_len = write_data->stored_selection->data_len;
818
819 g_output_stream_write_async (write_data->stream,
820 &buf[write_data->index],
821 buf_len - write_data->index,
822 G_PRIORITY_DEFAULT,
823 write_data->stored_selection->cancellable,
824 async_write_data_cb,
825 write_data);
826 }
827
828 static StoredSelection *
829 gdk_wayland_selection_find_stored_selection (GdkWaylandSelection *wayland_selection,
830 GdkWindow *window,
831 GdkAtom selection,
832 GdkAtom type)
833 {
834 gint i;
835
836 for (i = 0; i < wayland_selection->stored_selections->len; i++)
837 {
838 StoredSelection *stored_selection;
839
840 stored_selection = g_ptr_array_index (wayland_selection->stored_selections, i);
841
842 if (stored_selection->source == window &&
843 stored_selection->selection_atom == selection &&
844 stored_selection->type == type)
845 return stored_selection;
846 }
847
848 return NULL;
849 }
850
851 static void
852 gdk_wayland_selection_reset_selection (GdkWaylandSelection *wayland_selection,
853 GdkAtom selection)
854 {
855 gint i = 0;
856
857 while (i < wayland_selection->stored_selections->len)
858 {
859 StoredSelection *stored_selection;
860
861 stored_selection = g_ptr_array_index (wayland_selection->stored_selections, i);
862
863 if (stored_selection->selection_atom == selection)
864 {
865 if (wayland_selection->current_request_selection == stored_selection)
866 wayland_selection->current_request_selection = NULL;
867
868 g_ptr_array_remove_index_fast (wayland_selection->stored_selections, i);
869 }
870 else
871 i++;
872 }
873 }
874
875 void
876 gdk_wayland_selection_store (GdkWindow *window,
877 GdkAtom type,
878 GdkPropMode mode,
879 const guchar *data,
880 gint len)
881 {
882 GdkDisplay *display = gdk_window_get_display (window);
883 GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
884 StoredSelection *stored_selection;
885
886 if (type == gdk_atom_intern_static_string ("NULL"))
887 return;
888 if (!selection->current_request_selection)
889 return;
890
891 stored_selection = selection->current_request_selection;
892
893 if ((mode == GDK_PROP_MODE_PREPEND ||
894 mode == GDK_PROP_MODE_REPLACE) &&
895 stored_selection->data &&
896 stored_selection->pending_writes->len > 0)
897 {
898 /* If a prepend/replace action happens, all current readers are
899 * pretty much stale.
900 */
901 stored_selection_cancel_write (stored_selection);
902 }
903
904 stored_selection_add_data (stored_selection, mode, data, len);
905
906 if (stored_selection->data)
907 stored_selection_notify_write (stored_selection);
908 else
909 {
910 g_ptr_array_remove_fast (selection->stored_selections,
911 stored_selection);
912 }
913
914 /* Handle the next GDK_SELECTION_REQUEST / store, if any */
915 selection->current_request_selection = NULL;
916 gdk_wayland_selection_handle_next_request (selection);
917 }
918
919 static SelectionBuffer *
920 gdk_wayland_selection_lookup_requestor_buffer (GdkWindow *requestor)
921 {
922 GdkDisplay *display = gdk_window_get_display (requestor);
923 GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
924 SelectionBuffer *buffer_data;
925 GHashTableIter iter;
926 gint i;
927
928 for (i = 0; i < G_N_ELEMENTS (selection->selections); i++)
929 {
930 g_hash_table_iter_init (&iter, selection->selections[i].buffers);
931
932 while (g_hash_table_iter_next (&iter, NULL, (gpointer*) &buffer_data))
933 {
934 if (g_list_find (buffer_data->requestors, requestor))
935 return buffer_data;
936 }
937 }
938
939 return NULL;
940 }
941
942 static gboolean
943 gdk_wayland_selection_source_handles_target (GdkWaylandSelection *wayland_selection,
944 GdkAtom *target)
945 {
946 GdkAtom atom;
947 GdkAtom string_atom, utf8_string_atom;
948 GdkAtom string_mimetype, utf8_string_mimetype;
949 guint i;
950
951 if (*target == GDK_NONE)
952 return FALSE;
953
954 string_atom = gdk_atom_intern ("STRING", FALSE);
955 utf8_string_atom = gdk_atom_intern ("UTF8_STRING", FALSE);
956 string_mimetype = gdk_atom_intern (STRING_MIMETYPE, FALSE);
957 utf8_string_mimetype = gdk_atom_intern (UTF8_STRING_MIMETYPE, FALSE);
958
959 for (i = 0; i < wayland_selection->source_targets->len; i++)
960 {
961 atom = g_array_index (wayland_selection->source_targets, GdkAtom, i);
962
963 if (atom == *target)
964 return TRUE;
965
966 /* We might have converted (UTF8_)STRING to mimetypes when issuing
967 * the source.target requests, convert them back if needed.
968 */
969 if (atom == string_atom && *target == string_mimetype)
970 {
971 *target = string_atom;
972 return TRUE;
973 }
974 else if (atom == utf8_string_atom && *target == utf8_string_mimetype)
975 {
976 *target = utf8_string_atom;
977 return TRUE;
978 }
979 }
980
981 return FALSE;
982 }
983
984 static void
985 gdk_wayland_selection_handle_next_request (GdkWaylandSelection *wayland_selection)
986 {
987 gint i;
988
989 for (i = 0; i < wayland_selection->stored_selections->len; i++)
990 {
991 StoredSelection *stored_selection;
992
993 stored_selection = g_ptr_array_index (wayland_selection->stored_selections, i);
994
995 if (!stored_selection->data)
996 {
997 gdk_wayland_selection_emit_request (stored_selection->source,
998 stored_selection->selection_atom,
999 stored_selection->type);
1000 wayland_selection->current_request_selection = stored_selection;
1001 break;
1002 }
1003 }
1004 }
1005
1006 static gboolean
1007 gdk_wayland_selection_request_target (GdkWaylandSelection *wayland_selection,
1008 GdkWindow *window,
1009 GdkAtom selection,
1010 GdkAtom target,
1011 gint fd)
1012 {
1013 StoredSelection *stored_selection;
1014 AsyncWriteData *write_data;
1015
1016 if (!window ||
1017 !gdk_wayland_selection_source_handles_target (wayland_selection, &target))
1018 {
1019 close (fd);
1020 return FALSE;
1021 }
1022
1023 stored_selection =
1024 gdk_wayland_selection_find_stored_selection (wayland_selection, window,
1025 selection, target);
1026
1027 if (stored_selection && stored_selection->data)
1028 {
1029 /* Fast path, we already have the type cached */
1030 write_data = async_write_data_new (stored_selection, fd);
1031 async_write_data_write (write_data);
1032 return TRUE;
1033 }
1034
1035 if (!stored_selection)
1036 {
1037 stored_selection = stored_selection_new (wayland_selection, window,
1038 selection, target);
1039 g_ptr_array_add (wayland_selection->stored_selections, stored_selection);
1040 }
1041
1042 write_data = async_write_data_new (stored_selection, fd);
1043
1044 if (!wayland_selection->current_request_selection)
1045 gdk_wayland_selection_handle_next_request (wayland_selection);
1046
1047 return TRUE;
1048 }
1049
1050 static void
1051 data_source_target (void *data,
1052 struct wl_data_source *source,
1053 const char *mime_type)
1054 {
1055 GDK_NOTE (EVENTS,
1056 g_message ("data source target, source = %p, mime_type = %s",
1057 source, mime_type));
1058 }
1059
1060 static void
1061 data_source_send (void *data,
1062 struct wl_data_source *source,
1063 const char *mime_type,
1064 int32_t fd)
1065 {
1066 GdkWaylandSelection *wayland_selection = data;
1067 GdkWindow *window;
1068 GdkAtom selection;
1069
1070 GDK_NOTE (EVENTS,
1071 g_message ("data source send, source = %p, mime_type = %s, fd = %d",
1072 source, mime_type, fd));
1073
1074 if (!mime_type)
1075 {
1076 close (fd);
1077 return;
1078 }
1079
1080 if (source == wayland_selection->dnd_source)
1081 {
1082 window = wayland_selection->dnd_owner;
1083 selection = atoms[ATOM_DND];
1084 }
1085 else if (source == wayland_selection->clipboard_source)
1086 {
1087 window = wayland_selection->clipboard_owner;
1088 selection = atoms[ATOM_CLIPBOARD];
1089 }
1090 else
1091 {
1092 close (fd);
1093 return;
1094 }
1095
1096 if (!window)
1097 return;
1098
1099 gdk_wayland_selection_request_target (wayland_selection, window,
1100 selection,
1101 gdk_atom_intern (mime_type, FALSE),
1102 fd);
1103 }
1104
1105 static void
1106 data_source_cancelled (void *data,
1107 struct wl_data_source *source)
1108 {
1109 GdkWaylandSelection *wayland_selection = data;
1110 GdkDragContext *context;
1111 GdkDisplay *display;
1112 GdkAtom atom;
1113
1114 GDK_NOTE (EVENTS,
1115 g_message ("data source cancelled, source = %p", source));
1116
1117 display = gdk_display_get_default ();
1118
1119 if (source == wayland_selection->dnd_source)
1120 atom = atoms[ATOM_DND];
1121 else if (source == wayland_selection->clipboard_source)
1122 atom = atoms[ATOM_CLIPBOARD];
1123 else
1124 return;
1125
1126 context = gdk_wayland_drag_context_lookup_by_data_source (source);
1127
1128 if (context)
1129 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_ERROR);
1130
1131 emit_selection_clear (display, atom);
1132 gdk_selection_owner_set (NULL, atom, GDK_CURRENT_TIME, FALSE);
1133 gdk_wayland_selection_unset_data_source (display, atom);
1134 }
1135
1136 static void
1137 data_source_dnd_drop_performed (void *data,
1138 struct wl_data_source *source)
1139 {
1140 GdkDragContext *context;
1141
1142 context = gdk_wayland_drag_context_lookup_by_data_source (source);
1143
1144 if (!context)
1145 return;
1146
1147 g_signal_emit_by_name (context, "drop-performed", GDK_CURRENT_TIME);
1148 }
1149
1150 static void
1151 data_source_dnd_finished (void *data,
1152 struct wl_data_source *source)
1153 {
1154 GdkDisplay *display = gdk_display_get_default ();
1155 GdkDragContext *context;
1156
1157 context = gdk_wayland_drag_context_lookup_by_data_source (source);
1158
1159 if (!context)
1160 return;
1161
1162 if (context->action == GDK_ACTION_MOVE)
1163 {
1164 gdk_wayland_selection_emit_request (context->source_window,
1165 atoms[ATOM_DND],
1166 gdk_atom_intern_static_string ("DELETE"));
1167 }
1168
1169 g_signal_emit_by_name (context, "dnd-finished");
1170 gdk_selection_owner_set (NULL, atoms[ATOM_DND], GDK_CURRENT_TIME, TRUE);
1171 gdk_wayland_selection_clear_targets (display, atoms[ATOM_DND]);
1172 }
1173
1174 static void
1175 data_source_action (void *data,
1176 struct wl_data_source *source,
1177 uint32_t action)
1178 {
1179 GdkDragContext *context;
1180
1181 GDK_NOTE (EVENTS,
1182 g_message ("data source action, source = %p action=%x",
1183 source, action));
1184
1185 context = gdk_wayland_drag_context_lookup_by_data_source (source);
1186
1187 if (!context)
1188 return;
1189
1190 context->action = _wl_to_gdk_actions (action);
1191 g_signal_emit_by_name (context, "action-changed", context->action);
1192 }
1193
1194 static const struct wl_data_source_listener data_source_listener = {
1195 data_source_target,
1196 data_source_send,
1197 data_source_cancelled,
1198 data_source_dnd_drop_performed,
1199 data_source_dnd_finished,
1200 data_source_action,
1201 };
1202
1203 static void
1204 primary_source_send (void *data,
1205 gpointer source,
1206 const char *mime_type,
1207 int32_t fd)
1208 {
1209 GdkWaylandSelection *wayland_selection = data;
1210
1211 GDK_NOTE (EVENTS,
1212 g_message ("primary source send, source = %p, mime_type = %s, fd = %d",
1213 source, mime_type, fd));
1214
1215 if (!mime_type || !wayland_selection->primary_owner)
1216 {
1217 close (fd);
1218 return;
1219 }
1220
1221 gdk_wayland_selection_request_target (wayland_selection,
1222 wayland_selection->primary_owner,
1223 atoms[ATOM_PRIMARY],
1224 gdk_atom_intern (mime_type, FALSE),
1225 fd);
1226 }
1227
1228 static void
1229 gtk_primary_source_send (void *data,
1230 struct gtk_primary_selection_source *source,
1231 const char *mime_type,
1232 int32_t fd)
1233 {
1234 primary_source_send (data, (gpointer) source, mime_type, fd);
1235 }
1236
1237 static void
1238 zwp_primary_source_v1_send (void *data,
1239 struct zwp_primary_selection_source_v1 *source,
1240 const char *mime_type,
1241 int32_t fd)
1242 {
1243 primary_source_send (data, (gpointer) source, mime_type, fd);
1244 }
1245
1246 static void
1247 primary_source_cancelled (void *data,
1248 gpointer source)
1249 {
1250 GdkDisplay *display;
1251 GdkAtom atom;
1252
1253 GDK_NOTE (EVENTS,
1254 g_message ("primary source cancelled, source = %p", source));
1255
1256 display = gdk_display_get_default ();
1257
1258 atom = atoms[ATOM_PRIMARY];
1259 emit_selection_clear (display, atom);
1260 gdk_selection_owner_set (NULL, atom, GDK_CURRENT_TIME, FALSE);
1261 gdk_wayland_selection_unset_data_source (display, atom);
1262 }
1263
1264 static void
1265 gtk_primary_source_cancelled (void *data,
1266 struct gtk_primary_selection_source *source)
1267 {
1268 primary_source_cancelled (data, source);
1269 }
1270
1271 static void
1272 zwp_primary_source_v1_cancelled (void *data,
1273 struct zwp_primary_selection_source_v1 *source)
1274 {
1275 primary_source_cancelled (data, source);
1276 }
1277
1278 static const struct gtk_primary_selection_source_listener gtk_primary_source_listener = {
1279 gtk_primary_source_send,
1280 gtk_primary_source_cancelled,
1281 };
1282
1283 static const struct zwp_primary_selection_source_v1_listener zwp_primary_source_v1_listener = {
1284 zwp_primary_source_v1_send,
1285 zwp_primary_source_v1_cancelled,
1286 };
1287
1288 struct wl_data_source *
1289 gdk_wayland_selection_get_data_source (GdkWindow *owner,
1290 GdkAtom selection)
1291 {
1292 GdkDisplay *display = gdk_window_get_display (owner);
1293 GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
1294 gpointer source = NULL;
1295 GdkWaylandDisplay *display_wayland;
1296
1297 if (selection == atoms[ATOM_DND])
1298 {
1299 if (wayland_selection->dnd_source &&
1300 (!owner || owner == wayland_selection->dnd_owner))
1301 return wayland_selection->dnd_source;
1302 }
1303 else if (selection == atoms[ATOM_PRIMARY])
1304 {
1305 if (wayland_selection->primary_source &&
1306 (!owner || owner == wayland_selection->primary_owner))
1307 return wayland_selection->primary_source;
1308
1309 if (wayland_selection->primary_source)
1310 {
1311 primary_selection_source_destroy (wayland_selection->primary_source);
1312 wayland_selection->primary_source = NULL;
1313 }
1314 }
1315 else if (selection == atoms[ATOM_CLIPBOARD])
1316 {
1317 if (wayland_selection->clipboard_source &&
1318 (!owner || owner == wayland_selection->clipboard_owner))
1319 return wayland_selection->clipboard_source;
1320
1321 if (wayland_selection->clipboard_source)
1322 {
1323 wl_data_source_destroy (wayland_selection->clipboard_source);
1324 wayland_selection->clipboard_source = NULL;
1325 }
1326 }
1327 else
1328 return NULL;
1329
1330 if (!owner)
1331 return NULL;
1332
1333 display_wayland = GDK_WAYLAND_DISPLAY (gdk_window_get_display (owner));
1334
1335 if (selection == atoms[ATOM_PRIMARY])
1336 {
1337 if (display_wayland->zwp_primary_selection_manager_v1)
1338 {
1339 source = zwp_primary_selection_device_manager_v1_create_source (display_wayland->zwp_primary_selection_manager_v1);
1340 zwp_primary_selection_source_v1_add_listener (source,
1341 &zwp_primary_source_v1_listener,
1342 wayland_selection);
1343 }
1344 else if (display_wayland->gtk_primary_selection_manager)
1345 {
1346 source = gtk_primary_selection_device_manager_create_source (display_wayland->gtk_primary_selection_manager);
1347 gtk_primary_selection_source_add_listener (source,
1348 &gtk_primary_source_listener,
1349 wayland_selection);
1350 }
1351 }
1352 else
1353 {
1354 source = wl_data_device_manager_create_data_source (display_wayland->data_device_manager);
1355 wl_data_source_add_listener (source,
1356 &data_source_listener,
1357 wayland_selection);
1358 }
1359
1360 if (selection == atoms[ATOM_DND])
1361 wayland_selection->dnd_source = source;
1362 else if (selection == atoms[ATOM_PRIMARY])
1363 wayland_selection->primary_source = source;
1364 else if (selection == atoms[ATOM_CLIPBOARD])
1365 wayland_selection->clipboard_source = source;
1366
1367 return source;
1368 }
1369
1370 void
1371 gdk_wayland_selection_unset_data_source (GdkDisplay *display,
1372 GdkAtom selection)
1373 {
1374 GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
1375
1376 if (selection == atoms[ATOM_CLIPBOARD])
1377 {
1378 if (wayland_selection->clipboard_source)
1379 {
1380 wl_data_source_destroy (wayland_selection->clipboard_source);
1381 wayland_selection->clipboard_source = NULL;
1382 }
1383 }
1384 else if (selection == atoms[ATOM_PRIMARY])
1385 {
1386 if (wayland_selection->primary_source)
1387 {
1388 primary_selection_source_destroy (wayland_selection->primary_source);
1389 wayland_selection->primary_source = NULL;
1390 }
1391 }
1392 else if (selection == atoms[ATOM_DND])
1393 {
1394 wayland_selection->dnd_source = NULL;
1395 }
1396 }
1397
1398 GdkWindow *
1399 _gdk_wayland_display_get_selection_owner (GdkDisplay *display,
1400 GdkAtom selection)
1401 {
1402 GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
1403
1404 if (selection == atoms[ATOM_CLIPBOARD])
1405 return wayland_selection->clipboard_owner;
1406 else if (selection == atoms[ATOM_PRIMARY])
1407 return wayland_selection->primary_owner;
1408 else if (selection == atoms[ATOM_DND])
1409 return wayland_selection->dnd_owner;
1410
1411 return NULL;
1412 }
1413
1414 gboolean
1415 _gdk_wayland_display_set_selection_owner (GdkDisplay *display,
1416 GdkWindow *owner,
1417 GdkAtom selection,
1418 guint32 time,
1419 gboolean send_event)
1420 {
1421 GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
1422 GdkSeat *seat = gdk_display_get_default_seat (display);
1423
1424 gdk_wayland_selection_reset_selection (wayland_selection, selection);
1425
1426 if (selection == atoms[ATOM_CLIPBOARD])
1427 {
1428 wayland_selection->clipboard_owner = owner;
1429 if (send_event && !owner)
1430 {
1431 gdk_wayland_seat_set_selection (seat, NULL);
1432 gdk_wayland_selection_unset_data_source (display, selection);
1433 }
1434 return TRUE;
1435 }
1436 else if (selection == atoms[ATOM_PRIMARY])
1437 {
1438 wayland_selection->primary_owner = owner;
1439 if (send_event && !owner)
1440 {
1441 gdk_wayland_seat_set_primary (seat, NULL);
1442 gdk_wayland_selection_unset_data_source (display, selection);
1443 }
1444 return TRUE;
1445 }
1446 else if (selection == atoms[ATOM_DND])
1447 {
1448 wayland_selection->dnd_owner = owner;
1449 return TRUE;
1450 }
1451
1452 return FALSE;
1453 }
1454
1455 void
1456 _gdk_wayland_display_send_selection_notify (GdkDisplay *display,
1457 GdkWindow *requestor,
1458 GdkAtom selection,
1459 GdkAtom target,
1460 GdkAtom property,
1461 guint32 time)
1462 {
1463 GdkWaylandSelection *wayland_selection;
1464
1465 if (property != GDK_NONE)
1466 return;
1467
1468 wayland_selection = gdk_wayland_display_get_selection (display);
1469
1470 if (!wayland_selection->current_request_selection)
1471 return;
1472
1473 g_ptr_array_remove_fast (wayland_selection->stored_selections,
1474 wayland_selection->current_request_selection);
1475
1476 /* Handle the next GDK_SELECTION_REQUEST / store, if any */
1477 wayland_selection->current_request_selection = NULL;
1478 gdk_wayland_selection_handle_next_request (wayland_selection);
1479 }
1480
1481 gint
1482 _gdk_wayland_display_get_selection_property (GdkDisplay *display,
1483 GdkWindow *requestor,
1484 guchar **data,
1485 GdkAtom *ret_type,
1486 gint *ret_format)
1487 {
1488 SelectionBuffer *buffer_data;
1489 gsize len;
1490
1491 buffer_data = gdk_wayland_selection_lookup_requestor_buffer (requestor);
1492
1493 if (!buffer_data)
1494 return 0;
1495
1496 selection_buffer_remove_requestor (buffer_data, requestor);
1497 len = buffer_data->data->len;
1498
1499 if (data)
1500 {
1501 guchar *buffer;
1502
1503 buffer = g_new0 (guchar, len + 1);
1504 memcpy (buffer, buffer_data->data->data, len);
1505 *data = buffer;
1506 }
1507
1508 if (buffer_data->target == gdk_atom_intern_static_string ("TARGETS"))
1509 {
1510 if (ret_type)
1511 *ret_type = GDK_SELECTION_TYPE_ATOM;
1512 if (ret_format)
1513 *ret_format = 32;
1514 }
1515 else
1516 {
1517 if (ret_type)
1518 *ret_type = buffer_data->target;
1519 if (ret_format)
1520 *ret_format = 8;
1521 }
1522
1523 return len;
1524 }
1525
1526 static void
1527 emit_empty_selection_notify (GdkWindow *requestor,
1528 GdkAtom selection,
1529 GdkAtom target)
1530 {
1531 GdkEvent *event;
1532
1533 event = gdk_event_new (GDK_SELECTION_NOTIFY);
1534 event->selection.window = g_object_ref (requestor);
1535 event->selection.send_event = FALSE;
1536 event->selection.selection = selection;
1537 event->selection.target = target;
1538 event->selection.property = GDK_NONE;
1539 event->selection.time = GDK_CURRENT_TIME;
1540 event->selection.requestor = g_object_ref (requestor);
1541
1542 gdk_event_put (event);
1543 gdk_event_free (event);
1544 }
1545
1546 static void
1547 emit_selection_clear (GdkDisplay *display,
1548 GdkAtom selection)
1549 {
1550 GdkEvent *event;
1551 GdkWindow *window;
1552
1553 event = gdk_event_new (GDK_SELECTION_CLEAR);
1554 event->selection.selection = selection;
1555 event->selection.time = GDK_CURRENT_TIME;
1556
1557 window = _gdk_wayland_display_get_selection_owner (display, selection);
1558 if (window != NULL)
1559 {
1560 event->selection.window = g_object_ref (window);
1561 event->selection.requestor = g_object_ref (window);
1562 }
1563
1564 gdk_event_put (event);
1565 gdk_event_free (event);
1566 }
1567
1568 void
1569 _gdk_wayland_display_convert_selection (GdkDisplay *display,
1570 GdkWindow *requestor,
1571 GdkAtom selection,
1572 GdkAtom target,
1573 guint32 time)
1574 {
1575 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
1576 GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
1577 const SelectionData *selection_data;
1578 SelectionBuffer *buffer_data;
1579 gpointer offer;
1580 gchar *mimetype;
1581 GList *target_list;
1582
1583 selection_data = selection_lookup_offer_by_atom (wayland_selection, selection);
1584 if (!selection_data)
1585 {
1586 emit_empty_selection_notify (requestor, selection, target);
1587 return;
1588 }
1589
1590 offer = gdk_wayland_selection_get_offer (display, selection);
1591 target_list = gdk_wayland_selection_get_targets (display, selection);
1592
1593 if (!offer || target == gdk_atom_intern_static_string ("DELETE"))
1594 {
1595 emit_empty_selection_notify (requestor, selection, target);
1596 return;
1597 }
1598
1599 mimetype = gdk_atom_name (target);
1600
1601 if (target != gdk_atom_intern_static_string ("TARGETS"))
1602 {
1603 if (!g_list_find (target_list, GDK_ATOM_TO_POINTER (target)))
1604 {
1605 emit_empty_selection_notify (requestor, selection, target);
1606 return;
1607 }
1608
1609 if (selection != atoms[ATOM_PRIMARY])
1610 wl_data_offer_accept (offer,
1611 _gdk_wayland_display_get_serial (GDK_WAYLAND_DISPLAY (display)),
1612 mimetype);
1613 }
1614
1615 buffer_data = g_hash_table_lookup (selection_data->buffers, target);
1616
1617 if (buffer_data)
1618 selection_buffer_add_requestor (buffer_data, requestor);
1619 else
1620 {
1621 GInputStream *stream = NULL;
1622 int pipe_fd[2], natoms = 0;
1623 GdkAtom *targets = NULL;
1624
1625 if (target == gdk_atom_intern_static_string ("TARGETS"))
1626 {
1627 gint i = 0;
1628 GList *l;
1629
1630 natoms = g_list_length (target_list);
1631 targets = g_new0 (GdkAtom, natoms);
1632
1633 for (l = target_list; l; l = l->next)
1634 targets[i++] = l->data;
1635 }
1636 else
1637 {
1638 g_unix_open_pipe (pipe_fd, FD_CLOEXEC, NULL);
1639
1640 if (selection == atoms[ATOM_PRIMARY])
1641 {
1642 if (display_wayland->zwp_primary_selection_manager_v1)
1643 zwp_primary_selection_offer_v1_receive (offer, mimetype, pipe_fd[1]);
1644 else if (display_wayland->gtk_primary_selection_manager)
1645 gtk_primary_selection_offer_receive (offer, mimetype, pipe_fd[1]);
1646 }
1647 else
1648 {
1649 wl_data_offer_receive (offer, mimetype, pipe_fd[1]);
1650 }
1651
1652 stream = g_unix_input_stream_new (pipe_fd[0], TRUE);
1653 close (pipe_fd[1]);
1654 }
1655
1656 buffer_data = selection_buffer_new (stream, selection, target);
1657 selection_buffer_add_requestor (buffer_data, requestor);
1658
1659 if (stream)
1660 g_object_unref (stream);
1661
1662 if (targets)
1663 {
1664 /* Store directly the local atoms */
1665 selection_buffer_append_data (buffer_data, targets, natoms * sizeof (GdkAtom));
1666 g_free (targets);
1667 }
1668
1669 g_hash_table_insert (selection_data->buffers,
1670 GDK_ATOM_TO_POINTER (target),
1671 buffer_data);
1672 }
1673
1674 if (!buffer_data->stream)
1675 selection_buffer_notify (buffer_data);
1676
1677 g_free (mimetype);
1678 }
1679
1680 gint
1681 _gdk_wayland_display_text_property_to_utf8_list (GdkDisplay *display,
1682 GdkAtom encoding,
1683 gint format,
1684 const guchar *text,
1685 gint length,
1686 gchar ***list)
1687 {
1688 GPtrArray *array;
1689 const gchar *ptr;
1690 gsize chunk_len;
1691 gchar *copy;
1692 guint nitems;
1693
1694 ptr = (const gchar *) text;
1695 array = g_ptr_array_new ();
1696
1697 while (ptr < (const gchar *) &text[length])
1698 {
1699 chunk_len = strlen (ptr);
1700
1701 if (g_utf8_validate (ptr, chunk_len, NULL))
1702 {
1703 copy = g_strndup (ptr, chunk_len);
1704 g_ptr_array_add (array, copy);
1705 }
1706
1707 ptr = &ptr[chunk_len + 1];
1708 }
1709
1710 nitems = array->len;
1711 g_ptr_array_add (array, NULL);
1712
1713 if (list)
1714 *list = (gchar **) g_ptr_array_free (array, FALSE);
1715 else
1716 g_ptr_array_free (array, TRUE);
1717
1718 return nitems;
1719 }
1720
1721 /* This function has been copied straight from the x11 backend */
1722 static gchar *
1723 sanitize_utf8 (const gchar *src,
1724 gboolean return_latin1)
1725 {
1726 gint len = strlen (src);
1727 GString *result = g_string_sized_new (len);
1728 const gchar *p = src;
1729
1730 while (*p)
1731 {
1732 if (*p == '\r')
1733 {
1734 p++;
1735 if (*p == '\n')
1736 p++;
1737
1738 g_string_append_c (result, '\n');
1739 }
1740 else
1741 {
1742 gunichar ch = g_utf8_get_char (p);
1743
1744 if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
1745 {
1746 if (return_latin1)
1747 {
1748 if (ch <= 0xff)
1749 g_string_append_c (result, ch);
1750 else
1751 g_string_append_printf (result,
1752 ch < 0x10000 ? "\\u%04x" : "\\U%08x",
1753 ch);
1754 }
1755 else
1756 {
1757 char buf[7];
1758 gint buflen;
1759
1760 buflen = g_unichar_to_utf8 (ch, buf);
1761 g_string_append_len (result, buf, buflen);
1762 }
1763 }
1764
1765 p = g_utf8_next_char (p);
1766 }
1767 }
1768
1769 return g_string_free (result, FALSE);
1770 }
1771
1772 gchar *
1773 _gdk_wayland_display_utf8_to_string_target (GdkDisplay *display,
1774 const gchar *str)
1775 {
1776 /* This is mainly needed when interfacing with old clients through
1777 * Xwayland, the STRING target could be used, and passed as-is
1778 * by the compositor.
1779 *
1780 * There's already some handling of this atom (aka "mimetype" in
1781 * this backend) in common code, so we end up in this vfunc.
1782 */
1783 return sanitize_utf8 (str, TRUE);
1784 }
1785
1786 void
1787 gdk_wayland_selection_add_targets (GdkWindow *window,
1788 GdkAtom selection,
1789 guint ntargets,
1790 GdkAtom *targets)
1791 {
1792 GdkDisplay *display = gdk_window_get_display (window);
1793 GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
1794 gpointer data_source;
1795 guint i;
1796
1797 g_return_if_fail (GDK_IS_WINDOW (window));
1798
1799 data_source = gdk_wayland_selection_get_data_source (window, selection);
1800
1801 if (!data_source)
1802 return;
1803
1804 g_array_append_vals (wayland_selection->source_targets, targets, ntargets);
1805
1806 for (i = 0; i < ntargets; i++)
1807 {
1808 gchar *mimetype = gdk_atom_name (targets[i]);
1809
1810 if (selection == atoms[ATOM_PRIMARY])
1811 {
1812 if (g_strcmp0 (mimetype, "STRING") == 0)
1813 zwp_primary_selection_source_v1_offer (data_source, STRING_MIMETYPE);
1814 else if (g_strcmp0 (mimetype, "UTF8_STRING") == 0)
1815 zwp_primary_selection_source_v1_offer (data_source, UTF8_STRING_MIMETYPE);
1816
1817 zwp_primary_selection_source_v1_offer (data_source, mimetype);
1818 }
1819 else
1820 {
1821 if (g_strcmp0 (mimetype, "STRING") == 0)
1822 wl_data_source_offer (data_source, STRING_MIMETYPE);
1823 else if (g_strcmp0 (mimetype, "UTF8_STRING") == 0)
1824 wl_data_source_offer (data_source, UTF8_STRING_MIMETYPE);
1825
1826 wl_data_source_offer (data_source, mimetype);
1827 }
1828
1829 g_free (mimetype);
1830 }
1831
1832 if (selection == atoms[ATOM_CLIPBOARD])
1833 {
1834 GdkSeat *seat;
1835
1836 seat = gdk_display_get_default_seat (display);
1837 gdk_wayland_seat_set_selection (seat, data_source);
1838 }
1839 else if (selection == atoms[ATOM_PRIMARY])
1840 {
1841 GdkSeat *seat;
1842
1843 seat = gdk_display_get_default_seat (display);
1844 gdk_wayland_seat_set_primary (seat, data_source);
1845 }
1846 }
1847
1848 void
1849 gdk_wayland_selection_clear_targets (GdkDisplay *display,
1850 GdkAtom selection)
1851 {
1852 GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
1853
1854 wayland_selection->requested_target = GDK_NONE;
1855 g_array_set_size (wayland_selection->source_targets, 0);
1856 gdk_wayland_selection_unset_data_source (display, selection);
1857 }
1858
1859 gboolean
1860 gdk_wayland_selection_set_current_offer_actions (GdkDisplay *display,
1861 uint32_t action)
1862 {
1863 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
1864 struct wl_data_offer *offer;
1865 uint32_t all_actions = 0;
1866
1867 offer = gdk_wayland_selection_get_offer (display, atoms[ATOM_DND]);
1868
1869 if (!offer)
1870 return FALSE;
1871
1872 if (action != 0)
1873 all_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
1874 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE |
1875 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
1876
1877 if (display_wayland->data_device_manager_version >=
1878 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION)
1879 wl_data_offer_set_actions (offer, all_actions, action);
1880 return TRUE;
1881 }