blob: 674d3a24ed21f78886523538211555d06d97f621 [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2011 Kristian Høgsberg
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * 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:
Kristian Høgsberg2158a882013-04-18 15:07:39 -040011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * 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.
Kristian Høgsberg2158a882013-04-18 15:07:39 -040024 */
25
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010026#include "config.h"
27
Kristian Høgsberg2158a882013-04-18 15:07:39 -040028#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030031#include <stdint.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040032#include <stdio.h>
Jason Ekstranda7af7042013-10-12 22:38:11 -050033#include <assert.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040034
35#include "compositor.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070036#include "shared/helpers.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020037#include "shared/timespec-util.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040038
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040039struct weston_drag {
40 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070041 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040042 struct wl_listener data_source_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050043 struct weston_view *focus;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040044 struct wl_resource *focus_resource;
45 struct wl_listener focus_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050046 struct weston_view *icon;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040047 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040048 int32_t dx, dy;
Carlos Garnachob2889882016-01-15 21:14:26 +010049 struct weston_keyboard_grab keyboard_grab;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040050};
51
Xiong Zhangfd51e7b2013-11-25 18:42:49 +080052struct weston_pointer_drag {
53 struct weston_drag base;
54 struct weston_pointer_grab grab;
55};
56
57struct weston_touch_drag {
58 struct weston_drag base;
59 struct weston_touch_grab grab;
60};
61
Carlos Garnacho9c931792016-01-18 23:52:12 +010062#define ALL_ACTIONS (WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | \
63 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | \
64 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
65
Kristian Høgsberg2158a882013-04-18 15:07:39 -040066static void
67data_offer_accept(struct wl_client *client, struct wl_resource *resource,
68 uint32_t serial, const char *mime_type)
69{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070070 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040071
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010072 /* Protect against untimely calls from older data offers */
73 if (!offer->source || offer != offer->source->offer)
74 return;
75
Kristian Høgsberg2158a882013-04-18 15:07:39 -040076 /* FIXME: Check that client is currently focused by the input
77 * device that is currently dragging this data source. Should
78 * this be a wl_data_device request? */
79
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010080 offer->source->accept(offer->source, serial, mime_type);
81 offer->source->accepted = mime_type != NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -040082}
83
84static void
85data_offer_receive(struct wl_client *client, struct wl_resource *resource,
86 const char *mime_type, int32_t fd)
87{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070088 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040089
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010090 if (offer->source && offer == offer->source->offer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040091 offer->source->send(offer->source, mime_type, fd);
92 else
93 close(fd);
94}
95
96static void
97data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
98{
99 wl_resource_destroy(resource);
100}
101
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100102static void
103data_source_notify_finish(struct weston_data_source *source)
104{
Carlos Garnacho4061e2b2016-02-01 20:28:16 +0100105 if (!source->actions_set)
106 return;
107
Carlos Garnacho9c931792016-01-18 23:52:12 +0100108 if (source->offer->in_ask &&
109 wl_resource_get_version(source->resource) >=
110 WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
111 wl_data_source_send_action(source->resource,
112 source->current_dnd_action);
113 }
114
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100115 if (wl_resource_get_version(source->resource) >=
116 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
117 wl_data_source_send_dnd_finished(source->resource);
118 }
119
120 source->offer = NULL;
121}
122
Carlos Garnacho9c931792016-01-18 23:52:12 +0100123static uint32_t
124data_offer_choose_action(struct weston_data_offer *offer)
125{
126 uint32_t available_actions, preferred_action = 0;
127 uint32_t source_actions, offer_actions;
128
129 if (wl_resource_get_version(offer->resource) >=
130 WL_DATA_OFFER_ACTION_SINCE_VERSION) {
131 offer_actions = offer->dnd_actions;
132 preferred_action = offer->preferred_dnd_action;
133 } else {
134 offer_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
135 }
136
137 if (wl_resource_get_version(offer->source->resource) >=
138 WL_DATA_SOURCE_ACTION_SINCE_VERSION)
139 source_actions = offer->source->dnd_actions;
140 else
141 source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
142
143 available_actions = offer_actions & source_actions;
144
145 if (!available_actions)
146 return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
147
Carlos Garnachob2889882016-01-15 21:14:26 +0100148 if (offer->source->seat &&
149 offer->source->compositor_action & available_actions)
150 return offer->source->compositor_action;
151
Carlos Garnacho9c931792016-01-18 23:52:12 +0100152 /* If the dest side has a preferred DnD action, use it */
153 if ((preferred_action & available_actions) != 0)
154 return preferred_action;
155
156 /* Use the first found action, in bit order */
157 return 1 << (ffs(available_actions) - 1);
158}
159
160static void
161data_offer_update_action(struct weston_data_offer *offer)
162{
163 uint32_t action;
164
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800165 if (!offer->source)
Carlos Garnacho9c931792016-01-18 23:52:12 +0100166 return;
167
168 action = data_offer_choose_action(offer);
169
170 if (offer->source->current_dnd_action == action)
171 return;
172
173 offer->source->current_dnd_action = action;
174
175 if (offer->in_ask)
176 return;
177
178 if (wl_resource_get_version(offer->source->resource) >=
179 WL_DATA_SOURCE_ACTION_SINCE_VERSION)
180 wl_data_source_send_action(offer->source->resource, action);
181
182 if (wl_resource_get_version(offer->resource) >=
183 WL_DATA_OFFER_ACTION_SINCE_VERSION)
184 wl_data_offer_send_action(offer->resource, action);
185}
186
187static void
188data_offer_set_actions(struct wl_client *client,
189 struct wl_resource *resource,
190 uint32_t dnd_actions, uint32_t preferred_action)
191{
192 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
193
194 if (dnd_actions & ~ALL_ACTIONS) {
195 wl_resource_post_error(offer->resource,
196 WL_DATA_OFFER_ERROR_INVALID_ACTION_MASK,
197 "invalid action mask %x", dnd_actions);
198 return;
199 }
200
201 if (preferred_action &&
202 (!(preferred_action & dnd_actions) ||
203 __builtin_popcount(preferred_action) > 1)) {
204 wl_resource_post_error(offer->resource,
205 WL_DATA_OFFER_ERROR_INVALID_ACTION,
206 "invalid action %x", preferred_action);
207 return;
208 }
209
210 offer->dnd_actions = dnd_actions;
211 offer->preferred_dnd_action = preferred_action;
212 data_offer_update_action(offer);
213}
214
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100215static void
216data_offer_finish(struct wl_client *client, struct wl_resource *resource)
217{
218 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
219
220 if (!offer->source || offer->source->offer != offer)
221 return;
222
223 /* Disallow finish while we have a grab driving drag-and-drop, or
224 * if the negotiation is not at the right stage
225 */
226 if (offer->source->seat ||
227 !offer->source->accepted) {
228 wl_resource_post_error(offer->resource,
229 WL_DATA_OFFER_ERROR_INVALID_FINISH,
230 "premature finish request");
231 return;
232 }
233
Carlos Garnacho9c931792016-01-18 23:52:12 +0100234 switch (offer->source->current_dnd_action) {
235 case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE:
236 case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK:
237 wl_resource_post_error(offer->resource,
238 WL_DATA_OFFER_ERROR_INVALID_OFFER,
239 "offer finished with an invalid action");
240 return;
241 default:
242 break;
243 }
244
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100245 data_source_notify_finish(offer->source);
246}
247
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400248static const struct wl_data_offer_interface data_offer_interface = {
249 data_offer_accept,
250 data_offer_receive,
251 data_offer_destroy,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100252 data_offer_finish,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100253 data_offer_set_actions,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400254};
255
256static void
257destroy_data_offer(struct wl_resource *resource)
258{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700259 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400260
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100261 if (!offer->source)
262 goto out;
263
264 wl_list_remove(&offer->source_destroy_listener.link);
265
266 if (offer->source->offer != offer)
267 goto out;
268
269 /* If the drag destination has version < 3, wl_data_offer.finish
270 * won't be called, so do this here as a safety net, because
271 * we still want the version >=3 drag source to be happy.
272 */
273 if (wl_resource_get_version(offer->resource) <
274 WL_DATA_OFFER_ACTION_SINCE_VERSION) {
275 data_source_notify_finish(offer->source);
Carlos Garnacho4061e2b2016-02-01 20:28:16 +0100276 } else if (offer->source->resource &&
277 wl_resource_get_version(offer->source->resource) >=
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100278 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
279 wl_data_source_send_cancelled(offer->source->resource);
280 }
281
282 offer->source->offer = NULL;
283out:
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400284 free(offer);
285}
286
287static void
288destroy_offer_data_source(struct wl_listener *listener, void *data)
289{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700290 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400291
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700292 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400293 source_destroy_listener);
294
295 offer->source = NULL;
296}
297
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800298static struct weston_data_offer *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700299weston_data_source_send_offer(struct weston_data_source *source,
300 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400301{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700302 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400303 char **p;
304
305 offer = malloc(sizeof *offer);
306 if (offer == NULL)
307 return NULL;
308
Jason Ekstranda85118c2013-06-27 20:17:02 -0500309 offer->resource =
310 wl_resource_create(wl_resource_get_client(target),
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100311 &wl_data_offer_interface,
312 wl_resource_get_version(target), 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700313 if (offer->resource == NULL) {
314 free(offer);
315 return NULL;
316 }
317
Jason Ekstranda85118c2013-06-27 20:17:02 -0500318 wl_resource_set_implementation(offer->resource, &data_offer_interface,
319 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400320
Carlos Garnacho9c931792016-01-18 23:52:12 +0100321 offer->in_ask = false;
322 offer->dnd_actions = 0;
323 offer->preferred_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400324 offer->source = source;
325 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500326 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327 &offer->source_destroy_listener);
328
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500329 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330
331 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500332 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400333
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100334 source->offer = offer;
335 source->accepted = false;
336
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800337 return offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338}
339
340static void
341data_source_offer(struct wl_client *client,
342 struct wl_resource *resource,
343 const char *type)
344{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700345 struct weston_data_source *source =
346 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400347 char **p;
348
349 p = wl_array_add(&source->mime_types, sizeof *p);
350 if (p)
351 *p = strdup(type);
352 if (!p || !*p)
353 wl_resource_post_no_memory(resource);
354}
355
356static void
357data_source_destroy(struct wl_client *client, struct wl_resource *resource)
358{
359 wl_resource_destroy(resource);
360}
361
Carlos Garnacho9c931792016-01-18 23:52:12 +0100362static void
363data_source_set_actions(struct wl_client *client,
364 struct wl_resource *resource,
365 uint32_t dnd_actions)
366{
367 struct weston_data_source *source =
368 wl_resource_get_user_data(resource);
369
370 if (source->actions_set) {
371 wl_resource_post_error(source->resource,
372 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
373 "cannot set actions more than once");
374 return;
375 }
376
377 if (dnd_actions & ~ALL_ACTIONS) {
378 wl_resource_post_error(source->resource,
379 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
380 "invalid action mask %x", dnd_actions);
381 return;
382 }
383
384 if (source->seat) {
385 wl_resource_post_error(source->resource,
386 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
387 "invalid action change after "
388 "wl_data_device.start_drag");
389 return;
390 }
391
392 source->dnd_actions = dnd_actions;
393 source->actions_set = true;
394}
395
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400396static struct wl_data_source_interface data_source_interface = {
397 data_source_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100398 data_source_destroy,
399 data_source_set_actions
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400};
401
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800403drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100404 struct weston_pointer *pointer,
405 struct weston_touch *touch,
406 struct weston_surface *es,
407 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400408{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300409 struct weston_layer_entry *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400410 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400411
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800412 assert((pointer != NULL && touch == NULL) ||
413 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500414
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400415 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800416 if (pointer && pointer->sprite &&
417 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400418 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400419 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400421
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300422 weston_layer_entry_remove(&drag->icon->layer_link);
423 weston_layer_entry_insert(list, &drag->icon->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 weston_view_update_transform(drag->icon);
Jason Ekstrandef540082014-06-26 10:37:36 -0700425 pixman_region32_clear(&es->pending.input);
Armin Krezovićf8486c32016-06-30 06:04:28 +0200426 es->is_mapped = true;
427 drag->icon->is_mapped = true;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400428 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400429
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400430 drag->dx += sx;
431 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400432
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800433 /* init to 0 for avoiding a compile warning */
434 fx = fy = 0;
435 if (pointer) {
436 fx = wl_fixed_to_double(pointer->x) + drag->dx;
437 fy = wl_fixed_to_double(pointer->y) + drag->dy;
438 } else if (touch) {
439 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
440 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
441 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600442 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400443}
444
Pekka Paalanen8274d902014-08-06 19:36:51 +0300445static int
446pointer_drag_surface_get_label(struct weston_surface *surface,
447 char *buf, size_t len)
448{
449 return snprintf(buf, len, "pointer drag icon");
450}
451
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400452static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200453pointer_drag_surface_committed(struct weston_surface *es,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100454 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800455{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200456 struct weston_pointer_drag *drag = es->committed_private;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800457 struct weston_pointer *pointer = drag->grab.pointer;
458
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200459 assert(es->committed == pointer_drag_surface_committed);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800460
Jonas Ådahl767d8912013-12-03 22:30:17 +0100461 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800462}
463
Pekka Paalanen8274d902014-08-06 19:36:51 +0300464static int
465touch_drag_surface_get_label(struct weston_surface *surface,
466 char *buf, size_t len)
467{
468 return snprintf(buf, len, "touch drag icon");
469}
470
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800471static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200472touch_drag_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800473{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200474 struct weston_touch_drag *drag = es->committed_private;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800475 struct weston_touch *touch = drag->grab.touch;
476
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200477 assert(es->committed == touch_drag_surface_committed);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800478
Jonas Ådahl767d8912013-12-03 22:30:17 +0100479 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800480}
481
482static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483destroy_drag_focus(struct wl_listener *listener, void *data)
484{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400485 struct weston_drag *drag =
486 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400487
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400488 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400489}
490
491static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800492weston_drag_set_focus(struct weston_drag *drag,
493 struct weston_seat *seat,
494 struct weston_view *view,
495 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400496{
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100497 struct wl_resource *resource, *offer_resource = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800498 struct wl_display *display = seat->compositor->wl_display;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100499 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400500 uint32_t serial;
501
Jason Ekstranda7af7042013-10-12 22:38:11 -0500502 if (drag->focus && view && drag->focus->surface == view->surface) {
503 drag->focus = view;
504 return;
505 }
506
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400507 if (drag->focus_resource) {
508 wl_data_device_send_leave(drag->focus_resource);
509 wl_list_remove(&drag->focus_listener.link);
510 drag->focus_resource = NULL;
511 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400512 }
513
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400515 return;
516
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500517 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400519 return;
520
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100521 if (drag->data_source &&
522 drag->data_source->offer) {
523 /* Unlink the offer from the source */
524 offer = drag->data_source->offer;
525 offer->source = NULL;
526 drag->data_source->offer = NULL;
527 wl_list_remove(&offer->source_destroy_listener.link);
528 }
529
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800530 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500531 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400532 if (!resource)
533 return;
534
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400535 serial = wl_display_next_serial(display);
536
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700537 if (drag->data_source) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100538 drag->data_source->accepted = false;
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800539 offer = weston_data_source_send_offer(drag->data_source, resource);
540 if (offer == NULL)
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700541 return;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100542
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800543 data_offer_update_action(offer);
544
545 offer_resource = offer->resource;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100546 if (wl_resource_get_version (offer_resource) >=
547 WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
548 wl_data_offer_send_source_actions (offer_resource,
549 drag->data_source->dnd_actions);
550 }
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700551 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400552
Jason Ekstranda7af7042013-10-12 22:38:11 -0500553 wl_data_device_send_enter(resource, serial, view->surface->resource,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100554 sx, sy, offer_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400555
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400557 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500558 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400559 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400560}
561
562static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400563drag_grab_focus(struct weston_pointer_grab *grab)
564{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800565 struct weston_pointer_drag *drag =
566 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400567 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500568 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400569 wl_fixed_t sx, sy;
570
Jason Ekstranda7af7042013-10-12 22:38:11 -0500571 view = weston_compositor_pick_view(pointer->seat->compositor,
572 pointer->x, pointer->y,
573 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800574 if (drag->base.focus != view)
575 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400576}
577
578static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200579drag_grab_motion(struct weston_pointer_grab *grab,
580 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200581 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400582{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800583 struct weston_pointer_drag *drag =
584 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400585 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400586 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400587 wl_fixed_t sx, sy;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200588 uint32_t msecs;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400589
Jonas Ådahld2510102014-10-05 21:39:14 +0200590 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100591
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800592 if (drag->base.icon) {
593 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
594 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
595 weston_view_set_position(drag->base.icon, fx, fy);
596 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400597 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400598
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800599 if (drag->base.focus_resource) {
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200600 msecs = timespec_to_msec(time);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800601 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500602 pointer->x, pointer->y,
603 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400604
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200605 wl_data_device_send_motion(drag->base.focus_resource, msecs, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400606 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400607}
608
609static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800610data_device_end_drag_grab(struct weston_drag *drag,
611 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400612{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400613 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500614 if (weston_view_is_mapped(drag->icon))
615 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400616
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200617 drag->icon->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300618 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700619 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400620 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500621 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400622 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400623
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800624 weston_drag_set_focus(drag, seat, NULL, 0, 0);
625}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400626
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800627static void
628data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
629{
630 struct weston_pointer *pointer = drag->grab.pointer;
Carlos Garnachob2889882016-01-15 21:14:26 +0100631 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400632
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800633 data_device_end_drag_grab(&drag->base, pointer->seat);
634 weston_pointer_end_grab(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100635 weston_keyboard_end_grab(keyboard);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400636 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400637}
638
639static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400640drag_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200641 const struct timespec *time,
642 uint32_t button, uint32_t state_w)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400643{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800644 struct weston_pointer_drag *drag =
645 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400646 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400647 enum wl_pointer_button_state state = state_w;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100648 struct weston_data_source *data_source = drag->base.data_source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400649
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100650 if (data_source &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400651 pointer->grab_button == button &&
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100652 state == WL_POINTER_BUTTON_STATE_RELEASED) {
653 if (drag->base.focus_resource &&
Carlos Garnacho9c931792016-01-18 23:52:12 +0100654 data_source->accepted &&
655 data_source->current_dnd_action) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100656 wl_data_device_send_drop(drag->base.focus_resource);
657
658 if (wl_resource_get_version(data_source->resource) >=
659 WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION)
660 wl_data_source_send_dnd_drop_performed(data_source->resource);
661
Carlos Garnacho9c931792016-01-18 23:52:12 +0100662 data_source->offer->in_ask =
663 data_source->current_dnd_action ==
664 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
665
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100666 data_source->seat = NULL;
667 } else if (wl_resource_get_version(data_source->resource) >=
668 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
669 wl_data_source_send_cancelled(data_source->resource);
670 }
671 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400672
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400673 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400674 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800675 if (drag->base.data_source)
676 wl_list_remove(&drag->base.data_source_listener.link);
677 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400678 }
679}
680
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200681static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200682drag_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200683 const struct timespec *time,
684 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200685{
686}
687
688static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000689drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
690{
691}
692
693static void
694drag_grab_frame(struct weston_pointer_grab *grab)
695{
696}
697
698static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200699drag_grab_cancel(struct weston_pointer_grab *grab)
700{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800701 struct weston_pointer_drag *drag =
702 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200703
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800704 if (drag->base.data_source)
705 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200706
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800707 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200708}
709
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800710static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400711 drag_grab_focus,
712 drag_grab_motion,
713 drag_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200714 drag_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000715 drag_grab_axis_source,
716 drag_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200717 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400718};
719
720static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800721drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
722 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400723{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800724}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400725
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800726static void
727data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
728{
729 struct weston_touch *touch = drag->grab.touch;
Carlos Garnachob2889882016-01-15 21:14:26 +0100730 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800731
732 data_device_end_drag_grab(&drag->base, touch->seat);
733 weston_touch_end_grab(touch);
Carlos Garnachob2889882016-01-15 21:14:26 +0100734 weston_keyboard_end_grab(keyboard);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800735 free(drag);
736}
737
738static void
739drag_grab_touch_up(struct weston_touch_grab *grab,
740 uint32_t time, int touch_id)
741{
742 struct weston_touch_drag *touch_drag =
743 container_of(grab, struct weston_touch_drag, grab);
744 struct weston_touch *touch = grab->touch;
745
746 if (touch_id != touch->grab_touch_id)
747 return;
748
749 if (touch_drag->base.focus_resource)
750 wl_data_device_send_drop(touch_drag->base.focus_resource);
751 if (touch_drag->base.data_source)
752 wl_list_remove(&touch_drag->base.data_source_listener.link);
753 data_device_end_touch_drag_grab(touch_drag);
754}
755
756static void
757drag_grab_touch_focus(struct weston_touch_drag *drag)
758{
759 struct weston_touch *touch = drag->grab.touch;
760 struct weston_view *view;
761 wl_fixed_t view_x, view_y;
762
763 view = weston_compositor_pick_view(touch->seat->compositor,
764 touch->grab_x, touch->grab_y,
765 &view_x, &view_y);
766 if (drag->base.focus != view)
767 weston_drag_set_focus(&drag->base, touch->seat,
768 view, view_x, view_y);
769}
770
771static void
772drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300773 int touch_id, wl_fixed_t x, wl_fixed_t y)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800774{
775 struct weston_touch_drag *touch_drag =
776 container_of(grab, struct weston_touch_drag, grab);
777 struct weston_touch *touch = grab->touch;
778 wl_fixed_t view_x, view_y;
779 float fx, fy;
780
781 if (touch_id != touch->grab_touch_id)
782 return;
783
784 drag_grab_touch_focus(touch_drag);
785 if (touch_drag->base.icon) {
786 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
787 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
788 weston_view_set_position(touch_drag->base.icon, fx, fy);
789 weston_view_schedule_repaint(touch_drag->base.icon);
790 }
791
792 if (touch_drag->base.focus_resource) {
793 weston_view_from_global_fixed(touch_drag->base.focus,
794 touch->grab_x, touch->grab_y,
795 &view_x, &view_y);
796 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
797 view_x, view_y);
798 }
799}
800
801static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200802drag_grab_touch_frame(struct weston_touch_grab *grab)
803{
804}
805
806static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800807drag_grab_touch_cancel(struct weston_touch_grab *grab)
808{
809 struct weston_touch_drag *touch_drag =
810 container_of(grab, struct weston_touch_drag, grab);
811
812 if (touch_drag->base.data_source)
813 wl_list_remove(&touch_drag->base.data_source_listener.link);
814 data_device_end_touch_drag_grab(touch_drag);
815}
816
817static const struct weston_touch_grab_interface touch_drag_grab_interface = {
818 drag_grab_touch_down,
819 drag_grab_touch_up,
820 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200821 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800822 drag_grab_touch_cancel
823};
824
825static void
Carlos Garnachob2889882016-01-15 21:14:26 +0100826drag_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200827 const struct timespec *time, uint32_t key, uint32_t state)
Carlos Garnachob2889882016-01-15 21:14:26 +0100828{
829}
830
831static void
832drag_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
833 uint32_t serial, uint32_t mods_depressed,
834 uint32_t mods_latched,
835 uint32_t mods_locked, uint32_t group)
836{
837 struct weston_keyboard *keyboard = grab->keyboard;
838 struct weston_drag *drag =
839 container_of(grab, struct weston_drag, keyboard_grab);
840 uint32_t compositor_action;
841
842 if (mods_depressed & (1 << keyboard->xkb_info->shift_mod))
843 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
844 else if (mods_depressed & (1 << keyboard->xkb_info->ctrl_mod))
845 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
846 else
847 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
848
849 drag->data_source->compositor_action = compositor_action;
850
851 if (drag->data_source->offer)
852 data_offer_update_action(drag->data_source->offer);
853}
854
855static void
856drag_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
857{
858 struct weston_drag *drag =
859 container_of(grab, struct weston_drag, keyboard_grab);
860 struct weston_pointer *pointer = grab->keyboard->seat->pointer_state;
861 struct weston_touch *touch = grab->keyboard->seat->touch_state;
862
863 if (pointer && pointer->grab->interface == &pointer_drag_grab_interface) {
864 struct weston_touch_drag *touch_drag =
865 (struct weston_touch_drag *) drag;
866 drag_grab_touch_cancel(&touch_drag->grab);
867 } else if (touch && touch->grab->interface == &touch_drag_grab_interface) {
868 struct weston_pointer_drag *pointer_drag =
869 (struct weston_pointer_drag *) drag;
870 drag_grab_cancel(&pointer_drag->grab);
871 }
872}
873
874static const struct weston_keyboard_grab_interface keyboard_drag_grab_interface = {
875 drag_grab_keyboard_key,
876 drag_grab_keyboard_modifiers,
877 drag_grab_keyboard_cancel
878};
879
880static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800881destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
882{
883 struct weston_pointer_drag *drag = container_of(listener,
884 struct weston_pointer_drag, base.data_source_listener);
885
886 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400887}
888
889static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400890handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400891{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400892 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400893 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400894
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400895 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400896}
897
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700898WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800899weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700900 struct weston_data_source *source,
901 struct weston_surface *icon,
902 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400903{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800904 struct weston_pointer_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100905 struct weston_keyboard *keyboard =
906 weston_seat_get_keyboard(pointer->seat);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400907
Peter Huttererf3d62272013-08-08 11:57:05 +1000908 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700909 if (drag == NULL)
910 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400911
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800912 drag->grab.interface = &pointer_drag_grab_interface;
Carlos Garnachob2889882016-01-15 21:14:26 +0100913 drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800914 drag->base.client = client;
915 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400916
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400917 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800918 drag->base.icon = weston_view_create(icon);
919 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500920 free(drag);
921 return -1;
922 }
923
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800924 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500925 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800926 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400927
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200928 icon->committed = pointer_drag_surface_committed;
929 icon->committed_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300930 weston_surface_set_label_func(icon,
931 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800933 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 }
935
936 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800937 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800939 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400940 }
941
Derek Foremanf9318d12015-05-11 15:40:11 -0500942 weston_pointer_clear_focus(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100943 weston_keyboard_set_focus(keyboard, NULL);
944
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800945 weston_pointer_start_grab(pointer, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100946 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800947
948 return 0;
949}
950
951static void
952destroy_touch_data_device_source(struct wl_listener *listener, void *data)
953{
954 struct weston_touch_drag *drag = container_of(listener,
955 struct weston_touch_drag, base.data_source_listener);
956
957 data_device_end_touch_drag_grab(drag);
958}
959
960WL_EXPORT int
961weston_touch_start_drag(struct weston_touch *touch,
962 struct weston_data_source *source,
963 struct weston_surface *icon,
964 struct wl_client *client)
965{
966 struct weston_touch_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100967 struct weston_keyboard *keyboard =
968 weston_seat_get_keyboard(touch->seat);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800969
970 drag = zalloc(sizeof *drag);
971 if (drag == NULL)
972 return -1;
973
974 drag->grab.interface = &touch_drag_grab_interface;
975 drag->base.client = client;
976 drag->base.data_source = source;
977
978 if (icon) {
979 drag->base.icon = weston_view_create(icon);
980 if (drag->base.icon == NULL) {
981 free(drag);
982 return -1;
983 }
984
985 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
986 wl_signal_add(&icon->destroy_signal,
987 &drag->base.icon_destroy_listener);
988
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200989 icon->committed = touch_drag_surface_committed;
990 icon->committed_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300991 weston_surface_set_label_func(icon,
992 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800993 } else {
994 drag->base.icon = NULL;
995 }
996
997 if (source) {
998 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
999 wl_signal_add(&source->destroy_signal,
1000 &drag->base.data_source_listener);
1001 }
1002
Carlos Garnachob2889882016-01-15 21:14:26 +01001003 weston_keyboard_set_focus(keyboard, NULL);
1004
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001005 weston_touch_start_grab(touch, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +01001006 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001007
1008 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -07001009
1010 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001011}
1012
1013static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001014data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
1015 struct wl_resource *source_resource,
1016 struct wl_resource *origin_resource,
1017 struct wl_resource *icon_resource, uint32_t serial)
1018{
1019 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001020 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1021 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001022 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -07001023 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001024 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001025 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001026 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001027
Derek Foreman1281a362015-07-31 16:55:32 -05001028 is_pointer_grab = pointer &&
1029 pointer->button_count == 1 &&
1030 pointer->grab_serial == serial &&
1031 pointer->focus &&
1032 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001033
Derek Foreman1281a362015-07-31 16:55:32 -05001034 is_touch_grab = touch &&
1035 touch->num_tp == 1 &&
1036 touch->grab_serial == serial &&
1037 touch->focus &&
1038 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001039
1040 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001041 return;
1042
1043 /* FIXME: Check that the data source type array isn't empty. */
1044
1045 if (source_resource)
1046 source = wl_resource_get_user_data(source_resource);
1047 if (icon_resource)
1048 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +03001049
1050 if (icon) {
1051 if (weston_surface_set_role(icon, "wl_data_device-icon",
1052 resource,
1053 WL_DATA_DEVICE_ERROR_ROLE) < 0)
1054 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001055 }
1056
Jason Ekstrand8202d722014-06-24 21:19:24 -07001057 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001058 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001059 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001060 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001061
1062 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001063 wl_resource_post_no_memory(resource);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001064 else
1065 source->seat = seat;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001066}
1067
1068static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001069destroy_selection_data_source(struct wl_listener *listener, void *data)
1070{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001071 struct weston_seat *seat = container_of(listener, struct weston_seat,
1072 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05001073 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001074 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +01001075 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001076
1077 seat->selection_data_source = NULL;
1078
Derek Foreman1281a362015-07-31 16:55:32 -05001079 if (keyboard)
1080 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001081 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001082 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01001083 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001084 if (data_device)
1085 wl_data_device_send_selection(data_device, NULL);
1086 }
1087
1088 wl_signal_emit(&seat->selection_signal, seat);
1089}
1090
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001091/** \brief Send the selection to the specified client
1092 *
1093 * This function creates a new wl_data_offer if there is a wl_data_source
1094 * currently set as the selection and sends it to the specified client,
1095 * followed by the wl_data_device.selection() event.
1096 * If there is no current selection the wl_data_device.selection() event
1097 * will carry a NULL wl_data_offer.
1098 *
1099 * If the client does not have a wl_data_device for the specified seat
1100 * nothing will be done.
1101 *
1102 * \param seat The seat owning the wl_data_device used to send the events.
1103 * \param client The client to which to send the selection.
1104 */
1105WL_EXPORT void
1106weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
1107{
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001108 struct weston_data_offer *offer;
1109 struct wl_resource *data_device;
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001110
Giulio Camuffod46bb012015-05-01 12:59:36 +03001111 wl_resource_for_each(data_device, &seat->drag_resource_list) {
1112 if (wl_resource_get_client(data_device) != client)
1113 continue;
1114
1115 if (seat->selection_data_source) {
1116 offer = weston_data_source_send_offer(seat->selection_data_source,
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001117 data_device);
1118 wl_data_device_send_selection(data_device, offer->resource);
Giulio Camuffod46bb012015-05-01 12:59:36 +03001119 } else {
1120 wl_data_device_send_selection(data_device, NULL);
1121 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001122 }
1123}
1124
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001125WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001126weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001127 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001128{
Neil Roberts96d790e2013-09-19 17:32:00 +01001129 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -05001130 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001131
1132 if (seat->selection_data_source &&
1133 seat->selection_serial - serial < UINT32_MAX / 2)
1134 return;
1135
1136 if (seat->selection_data_source) {
1137 seat->selection_data_source->cancel(seat->selection_data_source);
1138 wl_list_remove(&seat->selection_data_source_listener.link);
1139 seat->selection_data_source = NULL;
1140 }
1141
1142 seat->selection_data_source = source;
1143 seat->selection_serial = serial;
1144
Derek Foreman1281a362015-07-31 16:55:32 -05001145 if (keyboard)
1146 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001147 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001148 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001149 }
1150
1151 wl_signal_emit(&seat->selection_signal, seat);
1152
1153 if (source) {
1154 seat->selection_data_source_listener.notify =
1155 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001156 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001157 &seat->selection_data_source_listener);
1158 }
1159}
1160
1161static void
1162data_device_set_selection(struct wl_client *client,
1163 struct wl_resource *resource,
1164 struct wl_resource *source_resource, uint32_t serial)
1165{
Carlos Garnacho9c931792016-01-18 23:52:12 +01001166 struct weston_data_source *source;
1167
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001168 if (!source_resource)
1169 return;
1170
Carlos Garnacho9c931792016-01-18 23:52:12 +01001171 source = wl_resource_get_user_data(source_resource);
1172
1173 if (source->actions_set) {
1174 wl_resource_post_error(source_resource,
1175 WL_DATA_SOURCE_ERROR_INVALID_SOURCE,
1176 "cannot set drag-and-drop source as selection");
1177 return;
1178 }
1179
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001180 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001181 weston_seat_set_selection(wl_resource_get_user_data(resource),
Carlos Garnacho9c931792016-01-18 23:52:12 +01001182 source, serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001183}
kabeer khan3a510d82014-10-20 11:47:15 +05301184static void
1185data_device_release(struct wl_client *client, struct wl_resource *resource)
1186{
1187 wl_resource_destroy(resource);
1188}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001189
1190static const struct wl_data_device_interface data_device_interface = {
1191 data_device_start_drag,
1192 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +05301193 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001194};
1195
1196static void
1197destroy_data_source(struct wl_resource *resource)
1198{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001199 struct weston_data_source *source =
1200 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001201 char **p;
1202
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001203 wl_signal_emit(&source->destroy_signal, source);
1204
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001205 wl_array_for_each(p, &source->mime_types)
1206 free(*p);
1207
1208 wl_array_release(&source->mime_types);
1209
Kristian Høgsberg489b2792013-06-25 11:26:31 -04001210 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001211}
1212
1213static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001214client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001215 uint32_t time, const char *mime_type)
1216{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001217 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001218}
1219
1220static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001221client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001222 const char *mime_type, int32_t fd)
1223{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001224 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001225 close(fd);
1226}
1227
1228static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001229client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001230{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001231 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001232}
1233
1234static void
1235create_data_source(struct wl_client *client,
1236 struct wl_resource *resource, uint32_t id)
1237{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001238 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001239
1240 source = malloc(sizeof *source);
1241 if (source == NULL) {
1242 wl_resource_post_no_memory(resource);
1243 return;
1244 }
1245
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001246 source->resource =
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001247 wl_resource_create(client, &wl_data_source_interface,
1248 wl_resource_get_version(resource), id);
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001249 if (source->resource == NULL) {
1250 free(source);
1251 wl_resource_post_no_memory(resource);
1252 return;
1253 }
1254
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001255 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001256 source->accept = client_source_accept;
1257 source->send = client_source_send;
1258 source->cancel = client_source_cancel;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001259 source->offer = NULL;
1260 source->accepted = false;
1261 source->seat = NULL;
Carlos Garnacho9c931792016-01-18 23:52:12 +01001262 source->actions_set = false;
1263 source->dnd_actions = 0;
1264 source->current_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Carlos Garnachob2889882016-01-15 21:14:26 +01001265 source->compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001266
1267 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001268
Jason Ekstranda85118c2013-06-27 20:17:02 -05001269 wl_resource_set_implementation(source->resource, &data_source_interface,
1270 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001271}
1272
1273static void unbind_data_device(struct wl_resource *resource)
1274{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001275 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001276}
1277
1278static void
1279get_data_device(struct wl_client *client,
1280 struct wl_resource *manager_resource,
1281 uint32_t id, struct wl_resource *seat_resource)
1282{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001283 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001284 struct wl_resource *resource;
1285
Jason Ekstranda85118c2013-06-27 20:17:02 -05001286 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +05301287 &wl_data_device_interface,
1288 wl_resource_get_version(manager_resource),
1289 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001290 if (resource == NULL) {
1291 wl_resource_post_no_memory(manager_resource);
1292 return;
1293 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001294
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001295 wl_list_insert(&seat->drag_resource_list,
1296 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001297 wl_resource_set_implementation(resource, &data_device_interface,
1298 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001299}
1300
1301static const struct wl_data_device_manager_interface manager_interface = {
1302 create_data_source,
1303 get_data_device
1304};
1305
1306static void
1307bind_manager(struct wl_client *client,
1308 void *data, uint32_t version, uint32_t id)
1309{
Jason Ekstranda85118c2013-06-27 20:17:02 -05001310 struct wl_resource *resource;
1311
kabeer khan3a510d82014-10-20 11:47:15 +05301312 resource = wl_resource_create(client,
1313 &wl_data_device_manager_interface,
1314 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001315 if (resource == NULL) {
1316 wl_client_post_no_memory(client);
1317 return;
1318 }
1319
1320 wl_resource_set_implementation(resource, &manager_interface,
1321 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001322}
1323
1324WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001325wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001326{
Neil Roberts96d790e2013-09-19 17:32:00 +01001327 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -05001328 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001329
Derek Foreman1281a362015-07-31 16:55:32 -05001330 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001331 return;
1332
Derek Foreman1281a362015-07-31 16:55:32 -05001333 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001334 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001335 return;
1336
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001337 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001338}
1339
1340WL_EXPORT int
1341wl_data_device_manager_init(struct wl_display *display)
1342{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001343 if (wl_global_create(display,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001344 &wl_data_device_manager_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001345 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001346 return -1;
1347
1348 return 0;
1349}