Debian Codegraph @

gdk/wayland/gdkdnd-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 "gdkdndprivate.h"
21
22 #include "gdkmain.h"
23 #include "gdkinternals.h"
24 #include "gdkproperty.h"
25 #include "gdkprivate-wayland.h"
26 #include "gdkdisplay-wayland.h"
27 #include "gdkseat-wayland.h"
28
29 #include "gdkdeviceprivate.h"
30
31 #include <string.h>
32
33 #define GDK_TYPE_WAYLAND_DRAG_CONTEXT (gdk_wayland_drag_context_get_type ())
34 #define GDK_WAYLAND_DRAG_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_WAYLAND_DRAG_CONTEXT, GdkWaylandDragContext))
35 #define GDK_WAYLAND_DRAG_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_WAYLAND_DRAG_CONTEXT, GdkWaylandDragContextClass))
36 #define GDK_IS_WAYLAND_DRAG_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_WAYLAND_DRAG_CONTEXT))
37 #define GDK_IS_WAYLAND_DRAG_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_WAYLAND_DRAG_CONTEXT))
38 #define GDK_WAYLAND_DRAG_CONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_WAYLAND_DRAG_CONTEXT, GdkWaylandDragContextClass))
39
40 typedef struct _GdkWaylandDragContext GdkWaylandDragContext;
41 typedef struct _GdkWaylandDragContextClass GdkWaylandDragContextClass;
42
43 struct _GdkWaylandDragContext
44 {
45 GdkDragContext context;
46 GdkWindow *dnd_window;
47 struct wl_surface *dnd_surface;
48 struct wl_data_source *data_source;
49 GdkDragAction selected_action;
50 uint32_t serial;
51 gdouble x;
52 gdouble y;
53 gint hot_x;
54 gint hot_y;
55 };
56
57 struct _GdkWaylandDragContextClass
58 {
59 GdkDragContextClass parent_class;
60 };
61
62 static GList *contexts;
63
64 GType gdk_wayland_drag_context_get_type (void);
65
66 G_DEFINE_TYPE (GdkWaylandDragContext, gdk_wayland_drag_context, GDK_TYPE_DRAG_CONTEXT)
67
68 static void
69 gdk_wayland_drag_context_finalize (GObject *object)
70 {
71 GdkWaylandDragContext *wayland_context = GDK_WAYLAND_DRAG_CONTEXT (object);
72 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
73 GdkWindow *dnd_window;
74
75 contexts = g_list_remove (contexts, context);
76
77 if (context->is_source)
78 {
79 GdkDisplay *display = gdk_window_get_display (context->source_window);
80 GdkAtom selection;
81 GdkWindow *selection_owner;
82
83 selection = gdk_drag_get_selection (context);
84 selection_owner = gdk_selection_owner_get_for_display (display, selection);
85 if (selection_owner == context->source_window)
86 gdk_wayland_selection_unset_data_source (display, selection);
87
88 gdk_drag_context_set_cursor (context, NULL);
89 }
90
91 if (wayland_context->data_source)
92 wl_data_source_destroy (wayland_context->data_source);
93
94 dnd_window = wayland_context->dnd_window;
95
96 G_OBJECT_CLASS (gdk_wayland_drag_context_parent_class)->finalize (object);
97
98 if (dnd_window)
99 gdk_window_destroy (dnd_window);
100 }
101
102 void
103 _gdk_wayland_drag_context_emit_event (GdkDragContext *context,
104 GdkEventType type,
105 guint32 time_)
106 {
107 GdkWindow *window;
108 GdkEvent *event;
109
110 switch (type)
111 {
112 case GDK_DRAG_ENTER:
113 case GDK_DRAG_LEAVE:
114 case GDK_DRAG_MOTION:
115 case GDK_DRAG_STATUS:
116 case GDK_DROP_START:
117 case GDK_DROP_FINISHED:
118 break;
119 default:
120 return;
121 }
122
123 if (context->is_source)
124 window = gdk_drag_context_get_source_window (context);
125 else
126 window = gdk_drag_context_get_dest_window (context);
127
128 event = gdk_event_new (type);
129 event->dnd.window = g_object_ref (window);
130 event->dnd.context = g_object_ref (context);
131 event->dnd.time = time_;
132 event->dnd.x_root = GDK_WAYLAND_DRAG_CONTEXT (context)->x;
133 event->dnd.y_root = GDK_WAYLAND_DRAG_CONTEXT (context)->y;
134 gdk_event_set_device (event, gdk_drag_context_get_device (context));
135
136 gdk_event_put (event);
137 gdk_event_free (event);
138 }
139
140 static GdkWindow *
141 gdk_wayland_drag_context_find_window (GdkDragContext *context,
142 GdkWindow *drag_window,
143 GdkScreen *screen,
144 gint x_root,
145 gint y_root,
146 GdkDragProtocol *protocol)
147 {
148 GdkDevice *device;
149 GdkWindow *window;
150
151 device = gdk_drag_context_get_device (context);
152 window = gdk_device_get_window_at_position (device, NULL, NULL);
153
154 if (window)
155 {
156 window = gdk_window_get_toplevel (window);
157 *protocol = GDK_DRAG_PROTO_WAYLAND;
158 return g_object_ref (window);
159 }
160
161 return NULL;
162 }
163
164 static inline uint32_t
165 gdk_to_wl_actions (GdkDragAction action)
166 {
167 uint32_t dnd_actions = 0;
168
169 if (action & (GDK_ACTION_COPY | GDK_ACTION_LINK | GDK_ACTION_PRIVATE))
170 dnd_actions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
171 if (action & GDK_ACTION_MOVE)
172 dnd_actions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
173 if (action & GDK_ACTION_ASK)
174 dnd_actions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
175
176 return dnd_actions;
177 }
178
179 void
180 gdk_wayland_drag_context_set_action (GdkDragContext *context,
181 GdkDragAction action)
182 {
183 context->suggested_action = context->action = action;
184 }
185
186 static gboolean
187 gdk_wayland_drag_context_drag_motion (GdkDragContext *context,
188 GdkWindow *dest_window,
189 GdkDragProtocol protocol,
190 gint x_root,
191 gint y_root,
192 GdkDragAction suggested_action,
193 GdkDragAction possible_actions,
194 guint32 time)
195 {
196 if (context->dest_window != dest_window)
197 {
198 context->dest_window = dest_window ? g_object_ref (dest_window) : NULL;
199 _gdk_wayland_drag_context_set_coords (context, x_root, y_root);
200 _gdk_wayland_drag_context_emit_event (context, GDK_DRAG_STATUS, time);
201 }
202
203 gdk_wayland_drag_context_set_action (context, suggested_action);
204
205 return context->dest_window != NULL;
206 }
207
208 static void
209 gdk_wayland_drag_context_drag_abort (GdkDragContext *context,
210 guint32 time)
211 {
212 }
213
214 static void
215 gdk_wayland_drag_context_drag_drop (GdkDragContext *context,
216 guint32 time)
217 {
218 }
219
220 /* Destination side */
221
222 static void
223 gdk_wayland_drop_context_set_status (GdkDragContext *context,
224 gboolean accepted)
225 {
226 GdkWaylandDragContext *context_wayland;
227 GdkDisplay *display;
228 struct wl_data_offer *wl_offer;
229
230 if (!context->dest_window)
231 return;
232
233 context_wayland = GDK_WAYLAND_DRAG_CONTEXT (context);
234
235 display = gdk_device_get_display (gdk_drag_context_get_device (context));
236 wl_offer = gdk_wayland_selection_get_offer (display,
237 gdk_drag_get_selection (context));
238
239 if (!wl_offer)
240 return;
241
242 if (accepted)
243 {
244 GList *l;
245
246 for (l = context->targets; l; l = l->next)
247 {
248 if (l->data != gdk_atom_intern_static_string ("DELETE"))
249 break;
250 }
251
252 if (l)
253 {
254 gchar *mimetype = gdk_atom_name (l->data);
255
256 wl_data_offer_accept (wl_offer, context_wayland->serial, mimetype);
257 g_free (mimetype);
258 return;
259 }
260 }
261
262 wl_data_offer_accept (wl_offer, context_wayland->serial, NULL);
263 }
264
265 static void
266 gdk_wayland_drag_context_commit_status (GdkDragContext *context)
267 {
268 GdkWaylandDragContext *wayland_context;
269 GdkDisplay *display;
270 uint32_t dnd_actions;
271
272 wayland_context = GDK_WAYLAND_DRAG_CONTEXT (context);
273 display = gdk_device_get_display (gdk_drag_context_get_device (context));
274
275 dnd_actions = gdk_to_wl_actions (wayland_context->selected_action);
276 gdk_wayland_selection_set_current_offer_actions (display, dnd_actions);
277
278 gdk_wayland_drop_context_set_status (context, wayland_context->selected_action != 0);
279 }
280
281 static void
282 gdk_wayland_drag_context_drag_status (GdkDragContext *context,
283 GdkDragAction action,
284 guint32 time_)
285 {
286 GdkWaylandDragContext *wayland_context;
287
288 wayland_context = GDK_WAYLAND_DRAG_CONTEXT (context);
289 wayland_context->selected_action = action;
290 }
291
292 static void
293 gdk_wayland_drag_context_drop_reply (GdkDragContext *context,
294 gboolean accepted,
295 guint32 time_)
296 {
297 if (!accepted)
298 gdk_wayland_drop_context_set_status (context, accepted);
299 }
300
301 static void
302 gdk_wayland_drag_context_drop_finish (GdkDragContext *context,
303 gboolean success,
304 guint32 time)
305 {
306 GdkDisplay *display = gdk_device_get_display (gdk_drag_context_get_device (context));
307 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
308 GdkWaylandDragContext *wayland_context;
309 struct wl_data_offer *wl_offer;
310 GdkAtom selection;
311
312 wayland_context = GDK_WAYLAND_DRAG_CONTEXT (context);
313 selection = gdk_drag_get_selection (context);
314 wl_offer = gdk_wayland_selection_get_offer (display, selection);
315
316 if (wl_offer && success && wayland_context->selected_action &&
317 wayland_context->selected_action != GDK_ACTION_ASK)
318 {
319 gdk_wayland_drag_context_commit_status (context);
320
321 if (display_wayland->data_device_manager_version >=
322 WL_DATA_OFFER_FINISH_SINCE_VERSION)
323 wl_data_offer_finish (wl_offer);
324 }
325
326 gdk_wayland_selection_set_offer (display, selection, NULL);
327 }
328
329 static gboolean
330 gdk_wayland_drag_context_drop_status (GdkDragContext *context)
331 {
332 return FALSE;
333 }
334
335 static GdkAtom
336 gdk_wayland_drag_context_get_selection (GdkDragContext *context)
337 {
338 return gdk_atom_intern_static_string ("GdkWaylandSelection");
339 }
340
341 static void
342 gdk_wayland_drag_context_init (GdkWaylandDragContext *context_wayland)
343 {
344 GdkDragContext *context;
345
346 context = GDK_DRAG_CONTEXT (context_wayland);
347 contexts = g_list_prepend (contexts, context);
348
349 context->action = GDK_ACTION_COPY;
350 context->suggested_action = GDK_ACTION_COPY;
351 context->actions = GDK_ACTION_COPY | GDK_ACTION_MOVE;
352 }
353
354 static GdkWindow *
355 gdk_wayland_drag_context_get_drag_window (GdkDragContext *context)
356 {
357 return GDK_WAYLAND_DRAG_CONTEXT (context)->dnd_window;
358 }
359
360 static void
361 gdk_wayland_drag_context_set_hotspot (GdkDragContext *context,
362 gint hot_x,
363 gint hot_y)
364 {
365 GdkWaylandDragContext *context_wayland = GDK_WAYLAND_DRAG_CONTEXT (context);
366 gint prev_hot_x = context_wayland->hot_x;
367 gint prev_hot_y = context_wayland->hot_y;
368 const GdkRectangle damage_rect = { .width = 1, .height = 1 };
369
370 context_wayland->hot_x = hot_x;
371 context_wayland->hot_y = hot_y;
372
373 if (prev_hot_x == hot_x && prev_hot_y == hot_y)
374 return;
375
376 _gdk_wayland_window_offset_next_wl_buffer (context_wayland->dnd_window,
377 prev_hot_x - hot_x, prev_hot_y - hot_y);
378 gdk_window_invalidate_rect (context_wayland->dnd_window, &damage_rect, FALSE);
379 }
380
381 static gboolean
382 gdk_wayland_drag_context_manage_dnd (GdkDragContext *context,
383 GdkWindow *ipc_window,
384 GdkDragAction actions)
385 {
386 GdkWaylandDragContext *context_wayland;
387 GdkWaylandDisplay *display_wayland;
388 GdkDevice *device;
389 GdkWindow *toplevel;
390
391 device = gdk_drag_context_get_device (context);
392 display_wayland = GDK_WAYLAND_DISPLAY (gdk_device_get_display (device));
393 toplevel = _gdk_device_window_at_position (device, NULL, NULL, NULL, TRUE);
394
395 context_wayland = GDK_WAYLAND_DRAG_CONTEXT (context);
396
397 if (!gdk_wayland_window_get_wl_surface (toplevel))
398 return FALSE;
399
400 if (display_wayland->data_device_manager_version >=
401 WL_DATA_SOURCE_SET_ACTIONS_SINCE_VERSION)
402 {
403 wl_data_source_set_actions (context_wayland->data_source,
404 gdk_to_wl_actions (actions));
405 }
406
407 wl_data_device_start_drag (gdk_wayland_device_get_data_device (device),
408 context_wayland->data_source,
409 gdk_wayland_window_get_wl_surface (toplevel),
410 context_wayland->dnd_surface,
411 _gdk_wayland_display_get_serial (display_wayland));
412
413 gdk_seat_ungrab (gdk_device_get_seat (device));
414
415 return TRUE;
416 }
417
418 static void
419 gdk_wayland_drag_context_set_cursor (GdkDragContext *context,
420 GdkCursor *cursor)
421 {
422 GdkDevice *device = gdk_drag_context_get_device (context);
423
424 gdk_wayland_seat_set_global_cursor (gdk_device_get_seat (device), cursor);
425 }
426
427 static void
428 gdk_wayland_drag_context_action_changed (GdkDragContext *context,
429 GdkDragAction action)
430 {
431 GdkCursor *cursor;
432
433 cursor = gdk_drag_get_cursor (context, action);
434 gdk_drag_context_set_cursor (context, cursor);
435 }
436
437 static void
438 gdk_wayland_drag_context_drop_performed (GdkDragContext *context,
439 guint32 time_)
440 {
441 gdk_drag_context_set_cursor (context, NULL);
442 }
443
444 static void
445 gdk_wayland_drag_context_cancel (GdkDragContext *context,
446 GdkDragCancelReason reason)
447 {
448 gdk_drag_context_set_cursor (context, NULL);
449 }
450
451 static void
452 gdk_wayland_drag_context_drop_done (GdkDragContext *context,
453 gboolean success)
454 {
455 GdkWaylandDragContext *context_wayland = GDK_WAYLAND_DRAG_CONTEXT (context);
456
457 if (success)
458 {
459 if (context_wayland->dnd_window)
460 gdk_window_hide (context_wayland->dnd_window);
461 }
462 }
463
464 static void
465 gdk_wayland_drag_context_class_init (GdkWaylandDragContextClass *klass)
466 {
467 GObjectClass *object_class = G_OBJECT_CLASS (klass);
468 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
469
470 object_class->finalize = gdk_wayland_drag_context_finalize;
471
472 context_class->find_window = gdk_wayland_drag_context_find_window;
473 context_class->drag_status = gdk_wayland_drag_context_drag_status;
474 context_class->drag_motion = gdk_wayland_drag_context_drag_motion;
475 context_class->drag_abort = gdk_wayland_drag_context_drag_abort;
476 context_class->drag_drop = gdk_wayland_drag_context_drag_drop;
477 context_class->drop_reply = gdk_wayland_drag_context_drop_reply;
478 context_class->drop_finish = gdk_wayland_drag_context_drop_finish;
479 context_class->drop_status = gdk_wayland_drag_context_drop_status;
480 context_class->get_selection = gdk_wayland_drag_context_get_selection;
481 context_class->get_drag_window = gdk_wayland_drag_context_get_drag_window;
482 context_class->set_hotspot = gdk_wayland_drag_context_set_hotspot;
483 context_class->drop_done = gdk_wayland_drag_context_drop_done;
484 context_class->manage_dnd = gdk_wayland_drag_context_manage_dnd;
485 context_class->set_cursor = gdk_wayland_drag_context_set_cursor;
486 context_class->action_changed = gdk_wayland_drag_context_action_changed;
487 context_class->drop_performed = gdk_wayland_drag_context_drop_performed;
488 context_class->cancel = gdk_wayland_drag_context_cancel;
489 context_class->commit_drag_status = gdk_wayland_drag_context_commit_status;
490 }
491
492 GdkDragProtocol
493 _gdk_wayland_window_get_drag_protocol (GdkWindow *window, GdkWindow **target)
494 {
495 return GDK_DRAG_PROTO_WAYLAND;
496 }
497
498 void
499 _gdk_wayland_window_register_dnd (GdkWindow *window)
500 {
501 }
502
503 static GdkWindow *
504 create_dnd_window (GdkScreen *screen)
505 {
506 GdkWindowAttr attrs;
507 guint mask;
508
509 attrs.x = attrs.y = 0;
510 attrs.width = attrs.height = 100;
511 attrs.wclass = GDK_INPUT_OUTPUT;
512 attrs.window_type = GDK_WINDOW_TEMP;
513 attrs.type_hint = GDK_WINDOW_TYPE_HINT_DND;
514 attrs.visual = gdk_screen_get_system_visual (screen);
515
516 mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_TYPE_HINT;
517
518 return gdk_window_new (gdk_screen_get_root_window (screen), &attrs, mask);
519 }
520
521 GdkDragContext *
522 _gdk_wayland_window_drag_begin (GdkWindow *window,
523 GdkDevice *device,
524 GList *targets,
525 gint x_root,
526 gint y_root)
527 {
528 GdkWaylandDragContext *context_wayland;
529 GdkDragContext *context;
530 GList *l;
531
532 context_wayland = g_object_new (GDK_TYPE_WAYLAND_DRAG_CONTEXT, NULL);
533 context = GDK_DRAG_CONTEXT (context_wayland);
534 context->display = gdk_window_get_display (window);
535 context->source_window = g_object_ref (window);
536 context->is_source = TRUE;
537 context->targets = g_list_copy (targets);
538
539 gdk_drag_context_set_device (context, device);
540
541 context_wayland->dnd_window = create_dnd_window (gdk_window_get_screen (window));
542 context_wayland->dnd_surface = gdk_wayland_window_get_wl_surface (context_wayland->dnd_window);
543 context_wayland->data_source =
544 gdk_wayland_selection_get_data_source (window,
545 gdk_wayland_drag_context_get_selection (context));
546
547 for (l = context->targets; l; l = l->next)
548 {
549 gchar *mimetype = gdk_atom_name (l->data);
550
551 wl_data_source_offer (context_wayland->data_source, mimetype);
552 g_free (mimetype);
553 }
554
555 /* If there's no targets this is local DnD, ensure we create a target for it */
556 if (!context->targets)
557 {
558 gchar *local_dnd_mime;
559 local_dnd_mime = g_strdup_printf ("application/gtk+-local-dnd-%x", getpid());
560 wl_data_source_offer (context_wayland->data_source, local_dnd_mime);
561 g_free (local_dnd_mime);
562 }
563
564 return context;
565 }
566
567 GdkDragContext *
568 _gdk_wayland_drop_context_new (GdkDisplay *display,
569 struct wl_data_device *data_device)
570 {
571 GdkWaylandDragContext *context_wayland;
572 GdkDragContext *context;
573
574 context_wayland = g_object_new (GDK_TYPE_WAYLAND_DRAG_CONTEXT, NULL);
575 context = GDK_DRAG_CONTEXT (context_wayland);
576 context->display = display;
577 context->is_source = FALSE;
578
579 return context;
580 }
581
582 void
583 gdk_wayland_drop_context_update_targets (GdkDragContext *context)
584 {
585 GdkDisplay *display;
586 GdkDevice *device;
587
588 device = gdk_drag_context_get_device (context);
589 display = gdk_device_get_display (device);
590 g_list_free (context->targets);
591 context->targets = g_list_copy (gdk_wayland_selection_get_targets (display,
592 gdk_drag_get_selection (context)));
593 }
594
595 void
596 _gdk_wayland_drag_context_set_coords (GdkDragContext *context,
597 gdouble x,
598 gdouble y)
599 {
600 GdkWaylandDragContext *context_wayland;
601
602 context_wayland = GDK_WAYLAND_DRAG_CONTEXT (context);
603 context_wayland->x = x;
604 context_wayland->y = y;
605 }
606
607 void
608 _gdk_wayland_drag_context_set_source_window (GdkDragContext *context,
609 GdkWindow *window)
610 {
611 if (context->source_window)
612 g_object_unref (context->source_window);
613
614 context->source_window = window ? g_object_ref (window) : NULL;
615 }
616
617 void
618 _gdk_wayland_drag_context_set_dest_window (GdkDragContext *context,
619 GdkWindow *dest_window,
620 uint32_t serial)
621 {
622 if (context->dest_window)
623 g_object_unref (context->dest_window);
624
625 context->dest_window = dest_window ? g_object_ref (dest_window) : NULL;
626 GDK_WAYLAND_DRAG_CONTEXT (context)->serial = serial;
627 gdk_wayland_drop_context_update_targets (context);
628 }
629
630 GdkDragContext *
631 gdk_wayland_drag_context_lookup_by_data_source (struct wl_data_source *source)
632 {
633 GList *l;
634
635 for (l = contexts; l; l = l->next)
636 {
637 GdkWaylandDragContext *wayland_context = l->data;
638
639 if (wayland_context->data_source == source)
640 return l->data;
641 }
642
643 return NULL;
644 }
645
646 GdkDragContext *
647 gdk_wayland_drag_context_lookup_by_source_window (GdkWindow *window)
648 {
649 GList *l;
650
651 for (l = contexts; l; l = l->next)
652 {
653 if (window == gdk_drag_context_get_source_window (l->data))
654 return l->data;
655 }
656
657 return NULL;
658 }
659
660 struct wl_data_source *
661 gdk_wayland_drag_context_get_data_source (GdkDragContext *context)
662 {
663 return GDK_WAYLAND_DRAG_CONTEXT (context)->data_source;
664 }