1 /*
2 * Copyright © 2011 Kristian Høgsberg
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <assert.h>
34
35 #include <libweston/libweston.h>
36 #include "libweston-internal.h"
37 #include "shared/helpers.h"
38 #include "shared/timespec-util.h"
39
40 struct weston_drag {
41 struct wl_client *client;
42 struct weston_data_source *data_source;
43 struct wl_listener data_source_listener;
44 struct weston_view *focus;
45 struct wl_resource *focus_resource;
46 struct wl_listener focus_listener;
47 struct weston_view *icon;
48 struct wl_listener icon_destroy_listener;
49 int32_t dx, dy;
50 struct weston_keyboard_grab keyboard_grab;
51 };
52
53 struct weston_pointer_drag {
54 struct weston_drag base;
55 struct weston_pointer_grab grab;
56 };
57
58 struct weston_touch_drag {
59 struct weston_drag base;
60 struct weston_touch_grab grab;
61 };
62
63 #define ALL_ACTIONS (WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | \
64 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | \
65 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
66
67 static void
data_offer_accept(struct wl_client * client,struct wl_resource * resource,uint32_t serial,const char * mime_type)68 data_offer_accept(struct wl_client *client, struct wl_resource *resource,
69 uint32_t serial, const char *mime_type)
70 {
71 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
72
73 /* Protect against untimely calls from older data offers */
74 if (!offer->source || offer != offer->source->offer)
75 return;
76
77 /* FIXME: Check that client is currently focused by the input
78 * device that is currently dragging this data source. Should
79 * this be a wl_data_device request? */
80
81 offer->source->accept(offer->source, serial, mime_type);
82 offer->source->accepted = mime_type != NULL;
83 }
84
85 static void
data_offer_receive(struct wl_client * client,struct wl_resource * resource,const char * mime_type,int32_t fd)86 data_offer_receive(struct wl_client *client, struct wl_resource *resource,
87 const char *mime_type, int32_t fd)
88 {
89 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
90
91 if (offer->source && offer == offer->source->offer)
92 offer->source->send(offer->source, mime_type, fd);
93 else
94 close(fd);
95 }
96
97 static void
data_offer_destroy(struct wl_client * client,struct wl_resource * resource)98 data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
99 {
100 wl_resource_destroy(resource);
101 }
102
103 static void
data_source_notify_finish(struct weston_data_source * source)104 data_source_notify_finish(struct weston_data_source *source)
105 {
106 if (!source->actions_set)
107 return;
108
109 if (source->offer->in_ask &&
110 wl_resource_get_version(source->resource) >=
111 WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
112 wl_data_source_send_action(source->resource,
113 source->current_dnd_action);
114 }
115
116 if (wl_resource_get_version(source->resource) >=
117 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
118 wl_data_source_send_dnd_finished(source->resource);
119 }
120
121 source->offer = NULL;
122 }
123
124 static uint32_t
data_offer_choose_action(struct weston_data_offer * offer)125 data_offer_choose_action(struct weston_data_offer *offer)
126 {
127 uint32_t available_actions, preferred_action = 0;
128 uint32_t source_actions, offer_actions;
129
130 if (wl_resource_get_version(offer->resource) >=
131 WL_DATA_OFFER_ACTION_SINCE_VERSION) {
132 offer_actions = offer->dnd_actions;
133 preferred_action = offer->preferred_dnd_action;
134 } else {
135 offer_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
136 }
137
138 if (wl_resource_get_version(offer->source->resource) >=
139 WL_DATA_SOURCE_ACTION_SINCE_VERSION)
140 source_actions = offer->source->dnd_actions;
141 else
142 source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
143
144 available_actions = offer_actions & source_actions;
145
146 if (!available_actions)
147 return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
148
149 if (offer->source->seat &&
150 offer->source->compositor_action & available_actions)
151 return offer->source->compositor_action;
152
153 /* If the dest side has a preferred DnD action, use it */
154 if ((preferred_action & available_actions) != 0)
155 return preferred_action;
156
157 /* Use the first found action, in bit order */
158 return 1 << (ffs(available_actions) - 1);
159 }
160
161 static void
data_offer_update_action(struct weston_data_offer * offer)162 data_offer_update_action(struct weston_data_offer *offer)
163 {
164 uint32_t action;
165
166 if (!offer->source)
167 return;
168
169 action = data_offer_choose_action(offer);
170
171 if (offer->source->current_dnd_action == action)
172 return;
173
174 offer->source->current_dnd_action = action;
175
176 if (offer->in_ask)
177 return;
178
179 if (wl_resource_get_version(offer->source->resource) >=
180 WL_DATA_SOURCE_ACTION_SINCE_VERSION)
181 wl_data_source_send_action(offer->source->resource, action);
182
183 if (wl_resource_get_version(offer->resource) >=
184 WL_DATA_OFFER_ACTION_SINCE_VERSION)
185 wl_data_offer_send_action(offer->resource, action);
186 }
187
188 static void
data_offer_set_actions(struct wl_client * client,struct wl_resource * resource,uint32_t dnd_actions,uint32_t preferred_action)189 data_offer_set_actions(struct wl_client *client,
190 struct wl_resource *resource,
191 uint32_t dnd_actions, uint32_t preferred_action)
192 {
193 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
194
195 if (dnd_actions & ~ALL_ACTIONS) {
196 wl_resource_post_error(offer->resource,
197 WL_DATA_OFFER_ERROR_INVALID_ACTION_MASK,
198 "invalid action mask %x", dnd_actions);
199 return;
200 }
201
202 if (preferred_action &&
203 (!(preferred_action & dnd_actions) ||
204 __builtin_popcount(preferred_action) > 1)) {
205 wl_resource_post_error(offer->resource,
206 WL_DATA_OFFER_ERROR_INVALID_ACTION,
207 "invalid action %x", preferred_action);
208 return;
209 }
210
211 offer->dnd_actions = dnd_actions;
212 offer->preferred_dnd_action = preferred_action;
213 data_offer_update_action(offer);
214 }
215
216 static void
data_offer_finish(struct wl_client * client,struct wl_resource * resource)217 data_offer_finish(struct wl_client *client, struct wl_resource *resource)
218 {
219 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
220
221 if (!offer->source || offer->source->offer != offer)
222 return;
223
224 if (offer->source->set_selection) {
225 wl_resource_post_error(offer->resource,
226 WL_DATA_OFFER_ERROR_INVALID_FINISH,
227 "finish only valid for drag n drop");
228 return;
229 }
230
231 /* Disallow finish while we have a grab driving drag-and-drop, or
232 * if the negotiation is not at the right stage
233 */
234 if (offer->source->seat ||
235 !offer->source->accepted) {
236 wl_resource_post_error(offer->resource,
237 WL_DATA_OFFER_ERROR_INVALID_FINISH,
238 "premature finish request");
239 return;
240 }
241
242 switch (offer->source->current_dnd_action) {
243 case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE:
244 case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK:
245 wl_resource_post_error(offer->resource,
246 WL_DATA_OFFER_ERROR_INVALID_OFFER,
247 "offer finished with an invalid action");
248 return;
249 default:
250 break;
251 }
252
253 data_source_notify_finish(offer->source);
254 }
255
256 static const struct wl_data_offer_interface data_offer_interface = {
257 data_offer_accept,
258 data_offer_receive,
259 data_offer_destroy,
260 data_offer_finish,
261 data_offer_set_actions,
262 };
263
264 static void
destroy_data_offer(struct wl_resource * resource)265 destroy_data_offer(struct wl_resource *resource)
266 {
267 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
268
269 if (!offer->source)
270 goto out;
271
272 wl_list_remove(&offer->source_destroy_listener.link);
273
274 if (offer->source->offer != offer)
275 goto out;
276
277 /* If the drag destination has version < 3, wl_data_offer.finish
278 * won't be called, so do this here as a safety net, because
279 * we still want the version >=3 drag source to be happy.
280 */
281 if (wl_resource_get_version(offer->resource) <
282 WL_DATA_OFFER_ACTION_SINCE_VERSION) {
283 data_source_notify_finish(offer->source);
284 } else if (offer->source->resource &&
285 wl_resource_get_version(offer->source->resource) >=
286 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
287 wl_data_source_send_cancelled(offer->source->resource);
288 }
289
290 offer->source->offer = NULL;
291 out:
292 free(offer);
293 }
294
295 static void
destroy_offer_data_source(struct wl_listener * listener,void * data)296 destroy_offer_data_source(struct wl_listener *listener, void *data)
297 {
298 struct weston_data_offer *offer;
299
300 offer = container_of(listener, struct weston_data_offer,
301 source_destroy_listener);
302
303 offer->source = NULL;
304 }
305
306 static struct weston_data_offer *
weston_data_source_send_offer(struct weston_data_source * source,struct wl_resource * target)307 weston_data_source_send_offer(struct weston_data_source *source,
308 struct wl_resource *target)
309 {
310 struct weston_data_offer *offer;
311 char **p;
312
313 offer = malloc(sizeof *offer);
314 if (offer == NULL)
315 return NULL;
316
317 offer->resource =
318 wl_resource_create(wl_resource_get_client(target),
319 &wl_data_offer_interface,
320 wl_resource_get_version(target), 0);
321 if (offer->resource == NULL) {
322 free(offer);
323 return NULL;
324 }
325
326 wl_resource_set_implementation(offer->resource, &data_offer_interface,
327 offer, destroy_data_offer);
328
329 offer->in_ask = false;
330 offer->dnd_actions = 0;
331 offer->preferred_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
332 offer->source = source;
333 offer->source_destroy_listener.notify = destroy_offer_data_source;
334 wl_signal_add(&source->destroy_signal,
335 &offer->source_destroy_listener);
336
337 wl_data_device_send_data_offer(target, offer->resource);
338
339 wl_array_for_each(p, &source->mime_types)
340 wl_data_offer_send_offer(offer->resource, *p);
341
342 source->offer = offer;
343 source->accepted = false;
344
345 return offer;
346 }
347
348 static void
data_source_offer(struct wl_client * client,struct wl_resource * resource,const char * type)349 data_source_offer(struct wl_client *client,
350 struct wl_resource *resource,
351 const char *type)
352 {
353 struct weston_data_source *source =
354 wl_resource_get_user_data(resource);
355 char **p;
356
357 p = wl_array_add(&source->mime_types, sizeof *p);
358 if (p)
359 *p = strdup(type);
360 if (!p || !*p)
361 wl_resource_post_no_memory(resource);
362 }
363
364 static void
data_source_destroy(struct wl_client * client,struct wl_resource * resource)365 data_source_destroy(struct wl_client *client, struct wl_resource *resource)
366 {
367 wl_resource_destroy(resource);
368 }
369
370 static void
data_source_set_actions(struct wl_client * client,struct wl_resource * resource,uint32_t dnd_actions)371 data_source_set_actions(struct wl_client *client,
372 struct wl_resource *resource,
373 uint32_t dnd_actions)
374 {
375 struct weston_data_source *source =
376 wl_resource_get_user_data(resource);
377
378 if (source->actions_set) {
379 wl_resource_post_error(source->resource,
380 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
381 "cannot set actions more than once");
382 return;
383 }
384
385 if (dnd_actions & ~ALL_ACTIONS) {
386 wl_resource_post_error(source->resource,
387 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
388 "invalid action mask %x", dnd_actions);
389 return;
390 }
391
392 if (source->seat) {
393 wl_resource_post_error(source->resource,
394 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
395 "invalid action change after "
396 "wl_data_device.start_drag");
397 return;
398 }
399
400 source->dnd_actions = dnd_actions;
401 source->actions_set = true;
402 }
403
404 static struct wl_data_source_interface data_source_interface = {
405 data_source_offer,
406 data_source_destroy,
407 data_source_set_actions
408 };
409
410 static void
drag_surface_configure(struct weston_drag * drag,struct weston_pointer * pointer,struct weston_touch * touch,struct weston_surface * es,int32_t sx,int32_t sy)411 drag_surface_configure(struct weston_drag *drag,
412 struct weston_pointer *pointer,
413 struct weston_touch *touch,
414 struct weston_surface *es,
415 int32_t sx, int32_t sy)
416 {
417 struct weston_layer_entry *list;
418 float fx, fy;
419
420 assert((pointer != NULL && touch == NULL) ||
421 (pointer == NULL && touch != NULL));
422
423 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
424 if (pointer && pointer->sprite &&
425 weston_view_is_mapped(pointer->sprite))
426 list = &pointer->sprite->layer_link;
427 else
428 list = &es->compositor->cursor_layer.view_list;
429
430 weston_layer_entry_remove(&drag->icon->layer_link);
431 weston_layer_entry_insert(list, &drag->icon->layer_link);
432 weston_view_update_transform(drag->icon);
433 pixman_region32_clear(&es->pending.input);
434 es->is_mapped = true;
435 drag->icon->is_mapped = true;
436 }
437
438 drag->dx += sx;
439 drag->dy += sy;
440
441 /* init to 0 for avoiding a compile warning */
442 fx = fy = 0;
443 if (pointer) {
444 fx = wl_fixed_to_double(pointer->x) + drag->dx;
445 fy = wl_fixed_to_double(pointer->y) + drag->dy;
446 } else if (touch) {
447 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
448 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
449 }
450 weston_view_set_position(drag->icon, fx, fy);
451 }
452
453 static int
pointer_drag_surface_get_label(struct weston_surface * surface,char * buf,size_t len)454 pointer_drag_surface_get_label(struct weston_surface *surface,
455 char *buf, size_t len)
456 {
457 return snprintf(buf, len, "pointer drag icon");
458 }
459
460 static void
pointer_drag_surface_committed(struct weston_surface * es,int32_t sx,int32_t sy)461 pointer_drag_surface_committed(struct weston_surface *es,
462 int32_t sx, int32_t sy)
463 {
464 struct weston_pointer_drag *drag = es->committed_private;
465 struct weston_pointer *pointer = drag->grab.pointer;
466
467 assert(es->committed == pointer_drag_surface_committed);
468
469 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
470 }
471
472 static int
touch_drag_surface_get_label(struct weston_surface * surface,char * buf,size_t len)473 touch_drag_surface_get_label(struct weston_surface *surface,
474 char *buf, size_t len)
475 {
476 return snprintf(buf, len, "touch drag icon");
477 }
478
479 static void
touch_drag_surface_committed(struct weston_surface * es,int32_t sx,int32_t sy)480 touch_drag_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
481 {
482 struct weston_touch_drag *drag = es->committed_private;
483 struct weston_touch *touch = drag->grab.touch;
484
485 assert(es->committed == touch_drag_surface_committed);
486
487 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
488 }
489
490 static void
destroy_drag_focus(struct wl_listener * listener,void * data)491 destroy_drag_focus(struct wl_listener *listener, void *data)
492 {
493 struct weston_drag *drag =
494 container_of(listener, struct weston_drag, focus_listener);
495
496 drag->focus_resource = NULL;
497 }
498
499 static void
weston_drag_set_focus(struct weston_drag * drag,struct weston_seat * seat,struct weston_view * view,wl_fixed_t sx,wl_fixed_t sy)500 weston_drag_set_focus(struct weston_drag *drag,
501 struct weston_seat *seat,
502 struct weston_view *view,
503 wl_fixed_t sx, wl_fixed_t sy)
504 {
505 struct wl_resource *resource, *offer_resource = NULL;
506 struct wl_display *display = seat->compositor->wl_display;
507 struct weston_data_offer *offer;
508 uint32_t serial;
509
510 if (drag->focus && view && drag->focus->surface == view->surface) {
511 drag->focus = view;
512 return;
513 }
514
515 if (drag->focus_resource) {
516 wl_data_device_send_leave(drag->focus_resource);
517 wl_list_remove(&drag->focus_listener.link);
518 drag->focus_resource = NULL;
519 drag->focus = NULL;
520 }
521
522 if (!view || !view->surface->resource)
523 return;
524
525 if (!drag->data_source &&
526 wl_resource_get_client(view->surface->resource) != drag->client)
527 return;
528
529 if (drag->data_source &&
530 drag->data_source->offer) {
531 /* Unlink the offer from the source */
532 offer = drag->data_source->offer;
533 offer->source = NULL;
534 drag->data_source->offer = NULL;
535 wl_list_remove(&offer->source_destroy_listener.link);
536 }
537
538 resource = wl_resource_find_for_client(&seat->drag_resource_list,
539 wl_resource_get_client(view->surface->resource));
540 if (!resource)
541 return;
542
543 serial = wl_display_next_serial(display);
544
545 if (drag->data_source) {
546 drag->data_source->accepted = false;
547 offer = weston_data_source_send_offer(drag->data_source, resource);
548 if (offer == NULL)
549 return;
550
551 data_offer_update_action(offer);
552
553 offer_resource = offer->resource;
554 if (wl_resource_get_version (offer_resource) >=
555 WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
556 wl_data_offer_send_source_actions (offer_resource,
557 drag->data_source->dnd_actions);
558 }
559 }
560
561 wl_data_device_send_enter(resource, serial, view->surface->resource,
562 sx, sy, offer_resource);
563
564 drag->focus = view;
565 drag->focus_listener.notify = destroy_drag_focus;
566 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
567 drag->focus_resource = resource;
568 }
569
570 static void
drag_grab_focus(struct weston_pointer_grab * grab)571 drag_grab_focus(struct weston_pointer_grab *grab)
572 {
573 struct weston_pointer_drag *drag =
574 container_of(grab, struct weston_pointer_drag, grab);
575 struct weston_pointer *pointer = grab->pointer;
576 struct weston_view *view;
577 wl_fixed_t sx, sy;
578
579 view = weston_compositor_pick_view(pointer->seat->compositor,
580 pointer->x, pointer->y,
581 &sx, &sy);
582 if (drag->base.focus != view)
583 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
584 }
585
586 static void
drag_grab_motion(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_motion_event * event)587 drag_grab_motion(struct weston_pointer_grab *grab,
588 const struct timespec *time,
589 struct weston_pointer_motion_event *event)
590 {
591 struct weston_pointer_drag *drag =
592 container_of(grab, struct weston_pointer_drag, grab);
593 struct weston_pointer *pointer = drag->grab.pointer;
594 float fx, fy;
595 wl_fixed_t sx, sy;
596 uint32_t msecs;
597
598 weston_pointer_move(pointer, event);
599
600 if (drag->base.icon) {
601 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
602 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
603 weston_view_set_position(drag->base.icon, fx, fy);
604 weston_view_schedule_repaint(drag->base.icon);
605 }
606
607 if (drag->base.focus_resource) {
608 msecs = timespec_to_msec(time);
609 weston_view_from_global_fixed(drag->base.focus,
610 pointer->x, pointer->y,
611 &sx, &sy);
612
613 wl_data_device_send_motion(drag->base.focus_resource, msecs, sx, sy);
614 }
615 }
616
617 static void
data_device_end_drag_grab(struct weston_drag * drag,struct weston_seat * seat)618 data_device_end_drag_grab(struct weston_drag *drag,
619 struct weston_seat *seat)
620 {
621 if (drag->icon) {
622 if (weston_view_is_mapped(drag->icon))
623 weston_view_unmap(drag->icon);
624
625 drag->icon->surface->committed = NULL;
626 weston_surface_set_label_func(drag->icon->surface, NULL);
627 pixman_region32_clear(&drag->icon->surface->pending.input);
628 wl_list_remove(&drag->icon_destroy_listener.link);
629 weston_view_destroy(drag->icon);
630 }
631
632 weston_drag_set_focus(drag, seat, NULL, 0, 0);
633 }
634
635 static void
data_device_end_pointer_drag_grab(struct weston_pointer_drag * drag)636 data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
637 {
638 struct weston_pointer *pointer = drag->grab.pointer;
639 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
640
641 data_device_end_drag_grab(&drag->base, pointer->seat);
642 weston_pointer_end_grab(pointer);
643 weston_keyboard_end_grab(keyboard);
644 free(drag);
645 }
646
647 static void
drag_grab_button(struct weston_pointer_grab * grab,const struct timespec * time,uint32_t button,uint32_t state_w)648 drag_grab_button(struct weston_pointer_grab *grab,
649 const struct timespec *time,
650 uint32_t button, uint32_t state_w)
651 {
652 struct weston_pointer_drag *drag =
653 container_of(grab, struct weston_pointer_drag, grab);
654 struct weston_pointer *pointer = drag->grab.pointer;
655 enum wl_pointer_button_state state = state_w;
656 struct weston_data_source *data_source = drag->base.data_source;
657
658 if (data_source &&
659 pointer->grab_button == button &&
660 state == WL_POINTER_BUTTON_STATE_RELEASED) {
661 if (drag->base.focus_resource &&
662 data_source->accepted &&
663 data_source->current_dnd_action) {
664 wl_data_device_send_drop(drag->base.focus_resource);
665
666 if (wl_resource_get_version(data_source->resource) >=
667 WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION)
668 wl_data_source_send_dnd_drop_performed(data_source->resource);
669
670 data_source->offer->in_ask =
671 data_source->current_dnd_action ==
672 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
673
674 data_source->seat = NULL;
675 } else if (wl_resource_get_version(data_source->resource) >=
676 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
677 wl_data_source_send_cancelled(data_source->resource);
678 }
679 }
680
681 if (pointer->button_count == 0 &&
682 state == WL_POINTER_BUTTON_STATE_RELEASED) {
683 if (drag->base.data_source)
684 wl_list_remove(&drag->base.data_source_listener.link);
685 data_device_end_pointer_drag_grab(drag);
686 }
687 }
688
689 static void
drag_grab_axis(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_axis_event * event)690 drag_grab_axis(struct weston_pointer_grab *grab,
691 const struct timespec *time,
692 struct weston_pointer_axis_event *event)
693 {
694 }
695
696 static void
drag_grab_axis_source(struct weston_pointer_grab * grab,uint32_t source)697 drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
698 {
699 }
700
701 static void
drag_grab_frame(struct weston_pointer_grab * grab)702 drag_grab_frame(struct weston_pointer_grab *grab)
703 {
704 }
705
706 static void
drag_grab_cancel(struct weston_pointer_grab * grab)707 drag_grab_cancel(struct weston_pointer_grab *grab)
708 {
709 struct weston_pointer_drag *drag =
710 container_of(grab, struct weston_pointer_drag, grab);
711
712 if (drag->base.data_source)
713 wl_list_remove(&drag->base.data_source_listener.link);
714
715 data_device_end_pointer_drag_grab(drag);
716 }
717
718 static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
719 drag_grab_focus,
720 drag_grab_motion,
721 drag_grab_button,
722 drag_grab_axis,
723 drag_grab_axis_source,
724 drag_grab_frame,
725 drag_grab_cancel,
726 };
727
728 static void
drag_grab_touch_down(struct weston_touch_grab * grab,const struct timespec * time,int touch_id,wl_fixed_t sx,wl_fixed_t sy)729 drag_grab_touch_down(struct weston_touch_grab *grab,
730 const struct timespec *time, int touch_id,
731 wl_fixed_t sx, wl_fixed_t sy)
732 {
733 }
734
735 static void
data_device_end_touch_drag_grab(struct weston_touch_drag * drag)736 data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
737 {
738 struct weston_touch *touch = drag->grab.touch;
739 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
740
741 data_device_end_drag_grab(&drag->base, touch->seat);
742 weston_touch_end_grab(touch);
743 weston_keyboard_end_grab(keyboard);
744 free(drag);
745 }
746
747 static void
drag_grab_touch_up(struct weston_touch_grab * grab,const struct timespec * time,int touch_id)748 drag_grab_touch_up(struct weston_touch_grab *grab,
749 const struct timespec *time, int touch_id)
750 {
751 struct weston_touch_drag *touch_drag =
752 container_of(grab, struct weston_touch_drag, grab);
753 struct weston_touch *touch = grab->touch;
754
755 if (touch_id != touch->grab_touch_id)
756 return;
757
758 if (touch_drag->base.focus_resource)
759 wl_data_device_send_drop(touch_drag->base.focus_resource);
760 if (touch_drag->base.data_source)
761 wl_list_remove(&touch_drag->base.data_source_listener.link);
762 data_device_end_touch_drag_grab(touch_drag);
763 }
764
765 static void
drag_grab_touch_focus(struct weston_touch_drag * drag)766 drag_grab_touch_focus(struct weston_touch_drag *drag)
767 {
768 struct weston_touch *touch = drag->grab.touch;
769 struct weston_view *view;
770 wl_fixed_t view_x, view_y;
771
772 view = weston_compositor_pick_view(touch->seat->compositor,
773 touch->grab_x, touch->grab_y,
774 &view_x, &view_y);
775 if (drag->base.focus != view)
776 weston_drag_set_focus(&drag->base, touch->seat,
777 view, view_x, view_y);
778 }
779
780 static void
drag_grab_touch_motion(struct weston_touch_grab * grab,const struct timespec * time,int touch_id,wl_fixed_t x,wl_fixed_t y)781 drag_grab_touch_motion(struct weston_touch_grab *grab,
782 const struct timespec *time,
783 int touch_id, wl_fixed_t x, wl_fixed_t y)
784 {
785 struct weston_touch_drag *touch_drag =
786 container_of(grab, struct weston_touch_drag, grab);
787 struct weston_touch *touch = grab->touch;
788 wl_fixed_t view_x, view_y;
789 float fx, fy;
790 uint32_t msecs;
791
792 if (touch_id != touch->grab_touch_id)
793 return;
794
795 drag_grab_touch_focus(touch_drag);
796 if (touch_drag->base.icon) {
797 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
798 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
799 weston_view_set_position(touch_drag->base.icon, fx, fy);
800 weston_view_schedule_repaint(touch_drag->base.icon);
801 }
802
803 if (touch_drag->base.focus_resource) {
804 msecs = timespec_to_msec(time);
805 weston_view_from_global_fixed(touch_drag->base.focus,
806 touch->grab_x, touch->grab_y,
807 &view_x, &view_y);
808 wl_data_device_send_motion(touch_drag->base.focus_resource,
809 msecs, view_x, view_y);
810 }
811 }
812
813 static void
drag_grab_touch_frame(struct weston_touch_grab * grab)814 drag_grab_touch_frame(struct weston_touch_grab *grab)
815 {
816 }
817
818 static void
drag_grab_touch_cancel(struct weston_touch_grab * grab)819 drag_grab_touch_cancel(struct weston_touch_grab *grab)
820 {
821 struct weston_touch_drag *touch_drag =
822 container_of(grab, struct weston_touch_drag, grab);
823
824 if (touch_drag->base.data_source)
825 wl_list_remove(&touch_drag->base.data_source_listener.link);
826 data_device_end_touch_drag_grab(touch_drag);
827 }
828
829 static const struct weston_touch_grab_interface touch_drag_grab_interface = {
830 drag_grab_touch_down,
831 drag_grab_touch_up,
832 drag_grab_touch_motion,
833 drag_grab_touch_frame,
834 drag_grab_touch_cancel
835 };
836
837 static void
drag_grab_keyboard_key(struct weston_keyboard_grab * grab,const struct timespec * time,uint32_t key,uint32_t state)838 drag_grab_keyboard_key(struct weston_keyboard_grab *grab,
839 const struct timespec *time, uint32_t key, uint32_t state)
840 {
841 }
842
843 static void
drag_grab_keyboard_modifiers(struct weston_keyboard_grab * grab,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)844 drag_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
845 uint32_t serial, uint32_t mods_depressed,
846 uint32_t mods_latched,
847 uint32_t mods_locked, uint32_t group)
848 {
849 struct weston_keyboard *keyboard = grab->keyboard;
850 struct weston_drag *drag =
851 container_of(grab, struct weston_drag, keyboard_grab);
852 uint32_t compositor_action;
853
854 if (mods_depressed & (1 << keyboard->xkb_info->shift_mod))
855 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
856 else if (mods_depressed & (1 << keyboard->xkb_info->ctrl_mod))
857 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
858 else
859 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
860
861 drag->data_source->compositor_action = compositor_action;
862
863 if (drag->data_source->offer)
864 data_offer_update_action(drag->data_source->offer);
865 }
866
867 static void
drag_grab_keyboard_cancel(struct weston_keyboard_grab * grab)868 drag_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
869 {
870 struct weston_drag *drag =
871 container_of(grab, struct weston_drag, keyboard_grab);
872 struct weston_pointer *pointer = grab->keyboard->seat->pointer_state;
873 struct weston_touch *touch = grab->keyboard->seat->touch_state;
874
875 if (pointer && pointer->grab->interface == &pointer_drag_grab_interface) {
876 struct weston_touch_drag *touch_drag =
877 (struct weston_touch_drag *) drag;
878 drag_grab_touch_cancel(&touch_drag->grab);
879 } else if (touch && touch->grab->interface == &touch_drag_grab_interface) {
880 struct weston_pointer_drag *pointer_drag =
881 (struct weston_pointer_drag *) drag;
882 drag_grab_cancel(&pointer_drag->grab);
883 }
884 }
885
886 static const struct weston_keyboard_grab_interface keyboard_drag_grab_interface = {
887 drag_grab_keyboard_key,
888 drag_grab_keyboard_modifiers,
889 drag_grab_keyboard_cancel
890 };
891
892 static void
destroy_pointer_data_device_source(struct wl_listener * listener,void * data)893 destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
894 {
895 struct weston_pointer_drag *drag = container_of(listener,
896 struct weston_pointer_drag, base.data_source_listener);
897
898 data_device_end_pointer_drag_grab(drag);
899 }
900
901 static void
handle_drag_icon_destroy(struct wl_listener * listener,void * data)902 handle_drag_icon_destroy(struct wl_listener *listener, void *data)
903 {
904 struct weston_drag *drag = container_of(listener, struct weston_drag,
905 icon_destroy_listener);
906
907 drag->icon = NULL;
908 }
909
910 WL_EXPORT int
weston_pointer_start_drag(struct weston_pointer * pointer,struct weston_data_source * source,struct weston_surface * icon,struct wl_client * client)911 weston_pointer_start_drag(struct weston_pointer *pointer,
912 struct weston_data_source *source,
913 struct weston_surface *icon,
914 struct wl_client *client)
915 {
916 struct weston_pointer_drag *drag;
917 struct weston_keyboard *keyboard =
918 weston_seat_get_keyboard(pointer->seat);
919
920 drag = zalloc(sizeof *drag);
921 if (drag == NULL)
922 return -1;
923
924 drag->grab.interface = &pointer_drag_grab_interface;
925 drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface;
926 drag->base.client = client;
927 drag->base.data_source = source;
928
929 if (icon) {
930 drag->base.icon = weston_view_create(icon);
931 if (drag->base.icon == NULL) {
932 free(drag);
933 return -1;
934 }
935
936 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
937 wl_signal_add(&icon->destroy_signal,
938 &drag->base.icon_destroy_listener);
939
940 icon->committed = pointer_drag_surface_committed;
941 icon->committed_private = drag;
942 weston_surface_set_label_func(icon,
943 pointer_drag_surface_get_label);
944 } else {
945 drag->base.icon = NULL;
946 }
947
948 if (source) {
949 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
950 wl_signal_add(&source->destroy_signal,
951 &drag->base.data_source_listener);
952 }
953
954 weston_pointer_clear_focus(pointer);
955 weston_keyboard_set_focus(keyboard, NULL);
956
957 weston_pointer_start_grab(pointer, &drag->grab);
958 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
959
960 return 0;
961 }
962
963 static void
destroy_touch_data_device_source(struct wl_listener * listener,void * data)964 destroy_touch_data_device_source(struct wl_listener *listener, void *data)
965 {
966 struct weston_touch_drag *drag = container_of(listener,
967 struct weston_touch_drag, base.data_source_listener);
968
969 data_device_end_touch_drag_grab(drag);
970 }
971
972 WL_EXPORT int
weston_touch_start_drag(struct weston_touch * touch,struct weston_data_source * source,struct weston_surface * icon,struct wl_client * client)973 weston_touch_start_drag(struct weston_touch *touch,
974 struct weston_data_source *source,
975 struct weston_surface *icon,
976 struct wl_client *client)
977 {
978 struct weston_touch_drag *drag;
979 struct weston_keyboard *keyboard =
980 weston_seat_get_keyboard(touch->seat);
981
982 drag = zalloc(sizeof *drag);
983 if (drag == NULL)
984 return -1;
985
986 drag->grab.interface = &touch_drag_grab_interface;
987 drag->base.client = client;
988 drag->base.data_source = source;
989
990 if (icon) {
991 drag->base.icon = weston_view_create(icon);
992 if (drag->base.icon == NULL) {
993 free(drag);
994 return -1;
995 }
996
997 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
998 wl_signal_add(&icon->destroy_signal,
999 &drag->base.icon_destroy_listener);
1000
1001 icon->committed = touch_drag_surface_committed;
1002 icon->committed_private = drag;
1003 weston_surface_set_label_func(icon,
1004 touch_drag_surface_get_label);
1005 } else {
1006 drag->base.icon = NULL;
1007 }
1008
1009 if (source) {
1010 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
1011 wl_signal_add(&source->destroy_signal,
1012 &drag->base.data_source_listener);
1013 }
1014
1015 weston_keyboard_set_focus(keyboard, NULL);
1016
1017 weston_touch_start_grab(touch, &drag->grab);
1018 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
1019
1020 drag_grab_touch_focus(drag);
1021
1022 return 0;
1023 }
1024
1025 static void
data_device_start_drag(struct wl_client * client,struct wl_resource * resource,struct wl_resource * source_resource,struct wl_resource * origin_resource,struct wl_resource * icon_resource,uint32_t serial)1026 data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
1027 struct wl_resource *source_resource,
1028 struct wl_resource *origin_resource,
1029 struct wl_resource *icon_resource, uint32_t serial)
1030 {
1031 struct weston_seat *seat = wl_resource_get_user_data(resource);
1032 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1033 struct weston_touch *touch = weston_seat_get_touch(seat);
1034 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
1035 struct weston_data_source *source = NULL;
1036 struct weston_surface *icon = NULL;
1037 int is_pointer_grab, is_touch_grab;
1038 int32_t ret = 0;
1039
1040 is_pointer_grab = pointer &&
1041 pointer->button_count == 1 &&
1042 pointer->grab_serial == serial &&
1043 pointer->focus &&
1044 pointer->focus->surface == origin;
1045
1046 is_touch_grab = touch &&
1047 touch->num_tp == 1 &&
1048 touch->grab_serial == serial &&
1049 touch->focus &&
1050 touch->focus->surface == origin;
1051
1052 if (!is_pointer_grab && !is_touch_grab)
1053 return;
1054
1055 /* FIXME: Check that the data source type array isn't empty. */
1056
1057 if (source_resource)
1058 source = wl_resource_get_user_data(source_resource);
1059 if (icon_resource)
1060 icon = wl_resource_get_user_data(icon_resource);
1061
1062 if (icon) {
1063 if (weston_surface_set_role(icon, "wl_data_device-icon",
1064 resource,
1065 WL_DATA_DEVICE_ERROR_ROLE) < 0)
1066 return;
1067 }
1068
1069 if (is_pointer_grab)
1070 ret = weston_pointer_start_drag(pointer, source, icon, client);
1071 else if (is_touch_grab)
1072 ret = weston_touch_start_drag(touch, source, icon, client);
1073
1074 if (ret < 0)
1075 wl_resource_post_no_memory(resource);
1076 else
1077 source->seat = seat;
1078 }
1079
1080 static void
destroy_selection_data_source(struct wl_listener * listener,void * data)1081 destroy_selection_data_source(struct wl_listener *listener, void *data)
1082 {
1083 struct weston_seat *seat = container_of(listener, struct weston_seat,
1084 selection_data_source_listener);
1085 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1086 struct wl_resource *data_device;
1087 struct weston_surface *focus = NULL;
1088
1089 seat->selection_data_source = NULL;
1090
1091 if (keyboard)
1092 focus = keyboard->focus;
1093 if (focus && focus->resource) {
1094 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
1095 wl_resource_get_client(focus->resource));
1096 if (data_device)
1097 wl_data_device_send_selection(data_device, NULL);
1098 }
1099
1100 wl_signal_emit(&seat->selection_signal, seat);
1101 }
1102
1103 /** \brief Send the selection to the specified client
1104 *
1105 * This function creates a new wl_data_offer if there is a wl_data_source
1106 * currently set as the selection and sends it to the specified client,
1107 * followed by the wl_data_device.selection() event.
1108 * If there is no current selection the wl_data_device.selection() event
1109 * will carry a NULL wl_data_offer.
1110 *
1111 * If the client does not have a wl_data_device for the specified seat
1112 * nothing will be done.
1113 *
1114 * \param seat The seat owning the wl_data_device used to send the events.
1115 * \param client The client to which to send the selection.
1116 */
1117 WL_EXPORT void
weston_seat_send_selection(struct weston_seat * seat,struct wl_client * client)1118 weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
1119 {
1120 struct weston_data_offer *offer;
1121 struct wl_resource *data_device;
1122
1123 wl_resource_for_each(data_device, &seat->drag_resource_list) {
1124 if (wl_resource_get_client(data_device) != client)
1125 continue;
1126
1127 if (seat->selection_data_source) {
1128 offer = weston_data_source_send_offer(seat->selection_data_source,
1129 data_device);
1130 wl_data_device_send_selection(data_device, offer->resource);
1131 } else {
1132 wl_data_device_send_selection(data_device, NULL);
1133 }
1134 }
1135 }
1136
1137 WL_EXPORT void
weston_seat_set_selection(struct weston_seat * seat,struct weston_data_source * source,uint32_t serial)1138 weston_seat_set_selection(struct weston_seat *seat,
1139 struct weston_data_source *source, uint32_t serial)
1140 {
1141 struct weston_surface *focus = NULL;
1142 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1143
1144 if (seat->selection_data_source &&
1145 seat->selection_serial - serial < UINT32_MAX / 2)
1146 return;
1147
1148 if (seat->selection_data_source) {
1149 seat->selection_data_source->cancel(seat->selection_data_source);
1150 wl_list_remove(&seat->selection_data_source_listener.link);
1151 seat->selection_data_source = NULL;
1152 }
1153
1154 seat->selection_data_source = source;
1155 seat->selection_serial = serial;
1156
1157 if (source)
1158 source->set_selection = true;
1159
1160 if (keyboard)
1161 focus = keyboard->focus;
1162 if (focus && focus->resource) {
1163 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
1164 }
1165
1166 wl_signal_emit(&seat->selection_signal, seat);
1167
1168 if (source) {
1169 seat->selection_data_source_listener.notify =
1170 destroy_selection_data_source;
1171 wl_signal_add(&source->destroy_signal,
1172 &seat->selection_data_source_listener);
1173 }
1174 }
1175
1176 static void
data_device_set_selection(struct wl_client * client,struct wl_resource * resource,struct wl_resource * source_resource,uint32_t serial)1177 data_device_set_selection(struct wl_client *client,
1178 struct wl_resource *resource,
1179 struct wl_resource *source_resource, uint32_t serial)
1180 {
1181 struct weston_seat *seat = wl_resource_get_user_data(resource);
1182 struct weston_data_source *source;
1183
1184 if (!seat || !source_resource)
1185 return;
1186
1187 source = wl_resource_get_user_data(source_resource);
1188
1189 if (source->actions_set) {
1190 wl_resource_post_error(source_resource,
1191 WL_DATA_SOURCE_ERROR_INVALID_SOURCE,
1192 "cannot set drag-and-drop source as selection");
1193 return;
1194 }
1195
1196 /* FIXME: Store serial and check against incoming serial here. */
1197 weston_seat_set_selection(seat, source, serial);
1198 }
1199 static void
data_device_release(struct wl_client * client,struct wl_resource * resource)1200 data_device_release(struct wl_client *client, struct wl_resource *resource)
1201 {
1202 wl_resource_destroy(resource);
1203 }
1204
1205 static const struct wl_data_device_interface data_device_interface = {
1206 data_device_start_drag,
1207 data_device_set_selection,
1208 data_device_release
1209 };
1210
1211 static void
destroy_data_source(struct wl_resource * resource)1212 destroy_data_source(struct wl_resource *resource)
1213 {
1214 struct weston_data_source *source =
1215 wl_resource_get_user_data(resource);
1216 char **p;
1217
1218 wl_signal_emit(&source->destroy_signal, source);
1219
1220 wl_array_for_each(p, &source->mime_types)
1221 free(*p);
1222
1223 wl_array_release(&source->mime_types);
1224
1225 free(source);
1226 }
1227
1228 static void
client_source_accept(struct weston_data_source * source,uint32_t time,const char * mime_type)1229 client_source_accept(struct weston_data_source *source,
1230 uint32_t time, const char *mime_type)
1231 {
1232 wl_data_source_send_target(source->resource, mime_type);
1233 }
1234
1235 static void
client_source_send(struct weston_data_source * source,const char * mime_type,int32_t fd)1236 client_source_send(struct weston_data_source *source,
1237 const char *mime_type, int32_t fd)
1238 {
1239 wl_data_source_send_send(source->resource, mime_type, fd);
1240 close(fd);
1241 }
1242
1243 static void
client_source_cancel(struct weston_data_source * source)1244 client_source_cancel(struct weston_data_source *source)
1245 {
1246 wl_data_source_send_cancelled(source->resource);
1247 }
1248
1249 static void
create_data_source(struct wl_client * client,struct wl_resource * resource,uint32_t id)1250 create_data_source(struct wl_client *client,
1251 struct wl_resource *resource, uint32_t id)
1252 {
1253 struct weston_data_source *source;
1254
1255 source = malloc(sizeof *source);
1256 if (source == NULL) {
1257 wl_resource_post_no_memory(resource);
1258 return;
1259 }
1260
1261 source->resource =
1262 wl_resource_create(client, &wl_data_source_interface,
1263 wl_resource_get_version(resource), id);
1264 if (source->resource == NULL) {
1265 free(source);
1266 wl_resource_post_no_memory(resource);
1267 return;
1268 }
1269
1270 wl_signal_init(&source->destroy_signal);
1271 source->accept = client_source_accept;
1272 source->send = client_source_send;
1273 source->cancel = client_source_cancel;
1274 source->offer = NULL;
1275 source->accepted = false;
1276 source->seat = NULL;
1277 source->actions_set = false;
1278 source->dnd_actions = 0;
1279 source->current_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
1280 source->compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
1281 source->set_selection = false;
1282
1283 wl_array_init(&source->mime_types);
1284
1285 wl_resource_set_implementation(source->resource, &data_source_interface,
1286 source, destroy_data_source);
1287 }
1288
unbind_data_device(struct wl_resource * resource)1289 static void unbind_data_device(struct wl_resource *resource)
1290 {
1291 wl_list_remove(wl_resource_get_link(resource));
1292 }
1293
1294 static void
get_data_device(struct wl_client * client,struct wl_resource * manager_resource,uint32_t id,struct wl_resource * seat_resource)1295 get_data_device(struct wl_client *client,
1296 struct wl_resource *manager_resource,
1297 uint32_t id, struct wl_resource *seat_resource)
1298 {
1299 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1300 struct wl_resource *resource;
1301
1302 resource = wl_resource_create(client,
1303 &wl_data_device_interface,
1304 wl_resource_get_version(manager_resource),
1305 id);
1306 if (resource == NULL) {
1307 wl_resource_post_no_memory(manager_resource);
1308 return;
1309 }
1310
1311 if (seat) {
1312 wl_list_insert(&seat->drag_resource_list,
1313 wl_resource_get_link(resource));
1314 } else {
1315 wl_list_init(wl_resource_get_link(resource));
1316 }
1317
1318 wl_resource_set_implementation(resource, &data_device_interface,
1319 seat, unbind_data_device);
1320 }
1321
1322 static const struct wl_data_device_manager_interface manager_interface = {
1323 create_data_source,
1324 get_data_device
1325 };
1326
1327 static void
bind_manager(struct wl_client * client,void * data,uint32_t version,uint32_t id)1328 bind_manager(struct wl_client *client,
1329 void *data, uint32_t version, uint32_t id)
1330 {
1331 struct wl_resource *resource;
1332
1333 resource = wl_resource_create(client,
1334 &wl_data_device_manager_interface,
1335 version, id);
1336 if (resource == NULL) {
1337 wl_client_post_no_memory(client);
1338 return;
1339 }
1340
1341 wl_resource_set_implementation(resource, &manager_interface,
1342 NULL, NULL);
1343 }
1344
1345 WL_EXPORT void
wl_data_device_set_keyboard_focus(struct weston_seat * seat)1346 wl_data_device_set_keyboard_focus(struct weston_seat *seat)
1347 {
1348 struct weston_surface *focus;
1349 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1350
1351 if (!keyboard)
1352 return;
1353
1354 focus = keyboard->focus;
1355 if (!focus || !focus->resource)
1356 return;
1357
1358 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
1359 }
1360
1361 WL_EXPORT int
wl_data_device_manager_init(struct wl_display * display)1362 wl_data_device_manager_init(struct wl_display *display)
1363 {
1364 if (wl_global_create(display,
1365 &wl_data_device_manager_interface, 3,
1366 NULL, bind_manager) == NULL)
1367 return -1;
1368
1369 return 0;
1370 }
1371