blob: 8b0a282fa9281906d434236d409f66ee7a524542 [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
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020035#include <libweston/libweston.h>
Guillaume Champagnef1e8fc92020-01-27 20:14:29 -050036#include "libweston-internal.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070037#include "shared/helpers.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020038#include "shared/timespec-util.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040039
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040040struct weston_drag {
41 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070042 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040043 struct wl_listener data_source_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050044 struct weston_view *focus;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040045 struct wl_resource *focus_resource;
46 struct wl_listener focus_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050047 struct weston_view *icon;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040048 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040049 int32_t dx, dy;
Carlos Garnachob2889882016-01-15 21:14:26 +010050 struct weston_keyboard_grab keyboard_grab;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040051};
52
Xiong Zhangfd51e7b2013-11-25 18:42:49 +080053struct weston_pointer_drag {
54 struct weston_drag base;
55 struct weston_pointer_grab grab;
56};
57
58struct weston_touch_drag {
59 struct weston_drag base;
60 struct weston_touch_grab grab;
61};
62
Carlos Garnacho9c931792016-01-18 23:52:12 +010063#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
Kristian Høgsberg2158a882013-04-18 15:07:39 -040067static void
68data_offer_accept(struct wl_client *client, struct wl_resource *resource,
69 uint32_t serial, const char *mime_type)
70{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070071 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040072
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010073 /* Protect against untimely calls from older data offers */
74 if (!offer->source || offer != offer->source->offer)
75 return;
76
Kristian Høgsberg2158a882013-04-18 15:07:39 -040077 /* 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
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010081 offer->source->accept(offer->source, serial, mime_type);
82 offer->source->accepted = mime_type != NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -040083}
84
85static void
86data_offer_receive(struct wl_client *client, struct wl_resource *resource,
87 const char *mime_type, int32_t fd)
88{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070089 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040090
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010091 if (offer->source && offer == offer->source->offer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040092 offer->source->send(offer->source, mime_type, fd);
93 else
94 close(fd);
95}
96
97static void
98data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
99{
100 wl_resource_destroy(resource);
101}
102
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100103static void
104data_source_notify_finish(struct weston_data_source *source)
105{
Carlos Garnacho4061e2b2016-02-01 20:28:16 +0100106 if (!source->actions_set)
107 return;
108
Carlos Garnacho9c931792016-01-18 23:52:12 +0100109 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
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100116 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
Carlos Garnacho9c931792016-01-18 23:52:12 +0100124static uint32_t
125data_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
Carlos Garnachob2889882016-01-15 21:14:26 +0100149 if (offer->source->seat &&
150 offer->source->compositor_action & available_actions)
151 return offer->source->compositor_action;
152
Carlos Garnacho9c931792016-01-18 23:52:12 +0100153 /* 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
161static void
162data_offer_update_action(struct weston_data_offer *offer)
163{
164 uint32_t action;
165
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800166 if (!offer->source)
Carlos Garnacho9c931792016-01-18 23:52:12 +0100167 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
188static void
189data_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
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100216static void
217data_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
Harish Krupo737ac0d2019-04-19 22:06:37 +0530224 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
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100231 /* 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
Carlos Garnacho9c931792016-01-18 23:52:12 +0100242 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
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100253 data_source_notify_finish(offer->source);
254}
255
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400256static const struct wl_data_offer_interface data_offer_interface = {
257 data_offer_accept,
258 data_offer_receive,
259 data_offer_destroy,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100260 data_offer_finish,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100261 data_offer_set_actions,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400262};
263
264static void
265destroy_data_offer(struct wl_resource *resource)
266{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700267 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400268
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100269 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);
Carlos Garnacho4061e2b2016-02-01 20:28:16 +0100284 } else if (offer->source->resource &&
285 wl_resource_get_version(offer->source->resource) >=
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100286 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
287 wl_data_source_send_cancelled(offer->source->resource);
288 }
289
290 offer->source->offer = NULL;
291out:
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400292 free(offer);
293}
294
295static void
296destroy_offer_data_source(struct wl_listener *listener, void *data)
297{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700298 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400299
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700300 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400301 source_destroy_listener);
302
303 offer->source = NULL;
304}
305
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800306static struct weston_data_offer *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700307weston_data_source_send_offer(struct weston_data_source *source,
308 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400309{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700310 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400311 char **p;
312
313 offer = malloc(sizeof *offer);
314 if (offer == NULL)
315 return NULL;
316
Jason Ekstranda85118c2013-06-27 20:17:02 -0500317 offer->resource =
318 wl_resource_create(wl_resource_get_client(target),
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100319 &wl_data_offer_interface,
320 wl_resource_get_version(target), 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700321 if (offer->resource == NULL) {
322 free(offer);
323 return NULL;
324 }
325
Jason Ekstranda85118c2013-06-27 20:17:02 -0500326 wl_resource_set_implementation(offer->resource, &data_offer_interface,
327 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400328
Carlos Garnacho9c931792016-01-18 23:52:12 +0100329 offer->in_ask = false;
330 offer->dnd_actions = 0;
331 offer->preferred_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400332 offer->source = source;
333 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500334 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400335 &offer->source_destroy_listener);
336
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500337 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338
339 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500340 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100342 source->offer = offer;
343 source->accepted = false;
344
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800345 return offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400346}
347
348static void
349data_source_offer(struct wl_client *client,
350 struct wl_resource *resource,
351 const char *type)
352{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700353 struct weston_data_source *source =
354 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400355 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
364static void
365data_source_destroy(struct wl_client *client, struct wl_resource *resource)
366{
367 wl_resource_destroy(resource);
368}
369
Carlos Garnacho9c931792016-01-18 23:52:12 +0100370static void
371data_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
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404static struct wl_data_source_interface data_source_interface = {
405 data_source_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100406 data_source_destroy,
407 data_source_set_actions
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408};
409
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400410static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800411drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100412 struct weston_pointer *pointer,
413 struct weston_touch *touch,
414 struct weston_surface *es,
415 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400416{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300417 struct weston_layer_entry *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400418 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400419
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800420 assert((pointer != NULL && touch == NULL) ||
421 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500422
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400423 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800424 if (pointer && pointer->sprite &&
425 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400426 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400427 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500428 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400429
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300430 weston_layer_entry_remove(&drag->icon->layer_link);
431 weston_layer_entry_insert(list, &drag->icon->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500432 weston_view_update_transform(drag->icon);
Jason Ekstrandef540082014-06-26 10:37:36 -0700433 pixman_region32_clear(&es->pending.input);
Armin Krezovićf8486c32016-06-30 06:04:28 +0200434 es->is_mapped = true;
435 drag->icon->is_mapped = true;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400436 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400437
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400438 drag->dx += sx;
439 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400440
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800441 /* 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 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600450 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400451}
452
Pekka Paalanen8274d902014-08-06 19:36:51 +0300453static int
454pointer_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
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400460static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200461pointer_drag_surface_committed(struct weston_surface *es,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100462 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800463{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200464 struct weston_pointer_drag *drag = es->committed_private;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800465 struct weston_pointer *pointer = drag->grab.pointer;
466
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200467 assert(es->committed == pointer_drag_surface_committed);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800468
Jonas Ådahl767d8912013-12-03 22:30:17 +0100469 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800470}
471
Pekka Paalanen8274d902014-08-06 19:36:51 +0300472static int
473touch_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
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800479static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200480touch_drag_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800481{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200482 struct weston_touch_drag *drag = es->committed_private;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800483 struct weston_touch *touch = drag->grab.touch;
484
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200485 assert(es->committed == touch_drag_surface_committed);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800486
Jonas Ådahl767d8912013-12-03 22:30:17 +0100487 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800488}
489
490static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491destroy_drag_focus(struct wl_listener *listener, void *data)
492{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400493 struct weston_drag *drag =
494 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400495
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400496 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400497}
498
499static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800500weston_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)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400504{
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100505 struct wl_resource *resource, *offer_resource = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800506 struct wl_display *display = seat->compositor->wl_display;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100507 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400508 uint32_t serial;
509
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 if (drag->focus && view && drag->focus->surface == view->surface) {
511 drag->focus = view;
512 return;
513 }
514
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400515 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;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400520 }
521
Jason Ekstranda7af7042013-10-12 22:38:11 -0500522 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400523 return;
524
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500525 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500526 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400527 return;
528
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100529 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
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800538 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500539 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540 if (!resource)
541 return;
542
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400543 serial = wl_display_next_serial(display);
544
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700545 if (drag->data_source) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100546 drag->data_source->accepted = false;
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800547 offer = weston_data_source_send_offer(drag->data_source, resource);
548 if (offer == NULL)
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700549 return;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100550
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800551 data_offer_update_action(offer);
552
553 offer_resource = offer->resource;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100554 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 }
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700559 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400560
Jason Ekstranda7af7042013-10-12 22:38:11 -0500561 wl_data_device_send_enter(resource, serial, view->surface->resource,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100562 sx, sy, offer_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400563
Jason Ekstranda7af7042013-10-12 22:38:11 -0500564 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400565 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500566 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400567 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400568}
569
570static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400571drag_grab_focus(struct weston_pointer_grab *grab)
572{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800573 struct weston_pointer_drag *drag =
574 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400575 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500576 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400577 wl_fixed_t sx, sy;
578
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 view = weston_compositor_pick_view(pointer->seat->compositor,
580 pointer->x, pointer->y,
581 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800582 if (drag->base.focus != view)
583 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400584}
585
586static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200587drag_grab_motion(struct weston_pointer_grab *grab,
588 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200589 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400590{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800591 struct weston_pointer_drag *drag =
592 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400593 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400594 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400595 wl_fixed_t sx, sy;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200596 uint32_t msecs;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400597
Jonas Ådahld2510102014-10-05 21:39:14 +0200598 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100599
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800600 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);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400605 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400606
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800607 if (drag->base.focus_resource) {
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200608 msecs = timespec_to_msec(time);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800609 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 pointer->x, pointer->y,
611 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400612
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200613 wl_data_device_send_motion(drag->base.focus_resource, msecs, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400614 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400615}
616
617static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800618data_device_end_drag_grab(struct weston_drag *drag,
619 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400620{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400621 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500622 if (weston_view_is_mapped(drag->icon))
623 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400624
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200625 drag->icon->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300626 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700627 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400628 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500629 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400630 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400631
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800632 weston_drag_set_focus(drag, seat, NULL, 0, 0);
633}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400634
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800635static void
636data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
637{
638 struct weston_pointer *pointer = drag->grab.pointer;
Carlos Garnachob2889882016-01-15 21:14:26 +0100639 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400640
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800641 data_device_end_drag_grab(&drag->base, pointer->seat);
642 weston_pointer_end_grab(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100643 weston_keyboard_end_grab(keyboard);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400644 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400645}
646
647static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400648drag_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200649 const struct timespec *time,
650 uint32_t button, uint32_t state_w)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400651{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800652 struct weston_pointer_drag *drag =
653 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400654 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400655 enum wl_pointer_button_state state = state_w;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100656 struct weston_data_source *data_source = drag->base.data_source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400657
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100658 if (data_source &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400659 pointer->grab_button == button &&
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100660 state == WL_POINTER_BUTTON_STATE_RELEASED) {
661 if (drag->base.focus_resource &&
Carlos Garnacho9c931792016-01-18 23:52:12 +0100662 data_source->accepted &&
663 data_source->current_dnd_action) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100664 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
Carlos Garnacho9c931792016-01-18 23:52:12 +0100670 data_source->offer->in_ask =
671 data_source->current_dnd_action ==
672 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
673
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100674 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 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400680
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400681 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400682 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800683 if (drag->base.data_source)
684 wl_list_remove(&drag->base.data_source_listener.link);
685 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400686 }
687}
688
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200689static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200690drag_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200691 const struct timespec *time,
692 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200693{
694}
695
696static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000697drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
698{
699}
700
701static void
702drag_grab_frame(struct weston_pointer_grab *grab)
703{
704}
705
706static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200707drag_grab_cancel(struct weston_pointer_grab *grab)
708{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800709 struct weston_pointer_drag *drag =
710 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200711
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800712 if (drag->base.data_source)
713 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200714
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800715 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200716}
717
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800718static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400719 drag_grab_focus,
720 drag_grab_motion,
721 drag_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200722 drag_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000723 drag_grab_axis_source,
724 drag_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200725 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400726};
727
728static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200729drag_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)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400732{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800733}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400734
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800735static void
736data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
737{
738 struct weston_touch *touch = drag->grab.touch;
Carlos Garnachob2889882016-01-15 21:14:26 +0100739 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800740
741 data_device_end_drag_grab(&drag->base, touch->seat);
742 weston_touch_end_grab(touch);
Carlos Garnachob2889882016-01-15 21:14:26 +0100743 weston_keyboard_end_grab(keyboard);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800744 free(drag);
745}
746
747static void
748drag_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200749 const struct timespec *time, int touch_id)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800750{
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
765static void
766drag_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
780static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200781drag_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)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800784{
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;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200790 uint32_t msecs;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800791
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) {
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200804 msecs = timespec_to_msec(time);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800805 weston_view_from_global_fixed(touch_drag->base.focus,
806 touch->grab_x, touch->grab_y,
807 &view_x, &view_y);
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200808 wl_data_device_send_motion(touch_drag->base.focus_resource,
809 msecs, view_x, view_y);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800810 }
811}
812
813static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200814drag_grab_touch_frame(struct weston_touch_grab *grab)
815{
816}
817
818static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800819drag_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
829static 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,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200833 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800834 drag_grab_touch_cancel
835};
836
837static void
Carlos Garnachob2889882016-01-15 21:14:26 +0100838drag_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200839 const struct timespec *time, uint32_t key, uint32_t state)
Carlos Garnachob2889882016-01-15 21:14:26 +0100840{
841}
842
843static void
844drag_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
867static void
868drag_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
886static 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
892static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800893destroy_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);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400899}
900
901static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400902handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400903{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400904 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400905 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400906
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400907 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400908}
909
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700910WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800911weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700912 struct weston_data_source *source,
913 struct weston_surface *icon,
914 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400915{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800916 struct weston_pointer_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100917 struct weston_keyboard *keyboard =
918 weston_seat_get_keyboard(pointer->seat);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400919
Peter Huttererf3d62272013-08-08 11:57:05 +1000920 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700921 if (drag == NULL)
922 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400923
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800924 drag->grab.interface = &pointer_drag_grab_interface;
Carlos Garnachob2889882016-01-15 21:14:26 +0100925 drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800926 drag->base.client = client;
927 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400928
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400929 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800930 drag->base.icon = weston_view_create(icon);
931 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 free(drag);
933 return -1;
934 }
935
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800936 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500937 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800938 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400939
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200940 icon->committed = pointer_drag_surface_committed;
941 icon->committed_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300942 weston_surface_set_label_func(icon,
943 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800945 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 }
947
948 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800949 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500950 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800951 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400952 }
953
Derek Foremanf9318d12015-05-11 15:40:11 -0500954 weston_pointer_clear_focus(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100955 weston_keyboard_set_focus(keyboard, NULL);
956
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800957 weston_pointer_start_grab(pointer, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100958 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800959
960 return 0;
961}
962
963static void
964destroy_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
972WL_EXPORT int
973weston_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;
Carlos Garnachob2889882016-01-15 21:14:26 +0100979 struct weston_keyboard *keyboard =
980 weston_seat_get_keyboard(touch->seat);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800981
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
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001001 icon->committed = touch_drag_surface_committed;
1002 icon->committed_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001003 weston_surface_set_label_func(icon,
1004 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001005 } 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
Carlos Garnachob2889882016-01-15 21:14:26 +01001015 weston_keyboard_set_focus(keyboard, NULL);
1016
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001017 weston_touch_start_grab(touch, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +01001018 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001019
1020 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -07001021
1022 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001023}
1024
1025static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001026data_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);
Derek Foreman1281a362015-07-31 16:55:32 -05001032 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1033 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001034 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -07001035 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001036 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001037 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001038 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001039
Derek Foreman1281a362015-07-31 16:55:32 -05001040 is_pointer_grab = pointer &&
1041 pointer->button_count == 1 &&
1042 pointer->grab_serial == serial &&
1043 pointer->focus &&
1044 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001045
Derek Foreman1281a362015-07-31 16:55:32 -05001046 is_touch_grab = touch &&
1047 touch->num_tp == 1 &&
1048 touch->grab_serial == serial &&
1049 touch->focus &&
1050 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001051
1052 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001053 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);
Pekka Paalanen50b67472014-10-01 15:02:41 +03001061
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;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001067 }
1068
Jason Ekstrand8202d722014-06-24 21:19:24 -07001069 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001070 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001071 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001072 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001073
1074 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001075 wl_resource_post_no_memory(resource);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001076 else
1077 source->seat = seat;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001078}
1079
1080static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001081destroy_selection_data_source(struct wl_listener *listener, void *data)
1082{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001083 struct weston_seat *seat = container_of(listener, struct weston_seat,
1084 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05001085 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001086 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +01001087 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001088
1089 seat->selection_data_source = NULL;
1090
Derek Foreman1281a362015-07-31 16:55:32 -05001091 if (keyboard)
1092 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001093 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001094 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01001095 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001096 if (data_device)
1097 wl_data_device_send_selection(data_device, NULL);
1098 }
1099
1100 wl_signal_emit(&seat->selection_signal, seat);
1101}
1102
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001103/** \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 */
1117WL_EXPORT void
1118weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
1119{
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001120 struct weston_data_offer *offer;
1121 struct wl_resource *data_device;
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001122
Giulio Camuffod46bb012015-05-01 12:59:36 +03001123 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,
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001129 data_device);
1130 wl_data_device_send_selection(data_device, offer->resource);
Giulio Camuffod46bb012015-05-01 12:59:36 +03001131 } else {
1132 wl_data_device_send_selection(data_device, NULL);
1133 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001134 }
1135}
1136
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001137WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001138weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001139 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001140{
Neil Roberts96d790e2013-09-19 17:32:00 +01001141 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -05001142 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001143
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;
Emmanuel Gil Peyrot45f5e532019-08-15 14:02:22 +02001156
1157 if (source)
1158 source->set_selection = true;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001159
Derek Foreman1281a362015-07-31 16:55:32 -05001160 if (keyboard)
1161 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001162 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001163 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001164 }
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;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001171 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001172 &seat->selection_data_source_listener);
1173 }
1174}
1175
1176static void
1177data_device_set_selection(struct wl_client *client,
1178 struct wl_resource *resource,
1179 struct wl_resource *source_resource, uint32_t serial)
1180{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02001181 struct weston_seat *seat = wl_resource_get_user_data(resource);
Carlos Garnacho9c931792016-01-18 23:52:12 +01001182 struct weston_data_source *source;
1183
Alexandros Frantzis8480d132018-02-15 13:07:09 +02001184 if (!seat || !source_resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001185 return;
1186
Carlos Garnacho9c931792016-01-18 23:52:12 +01001187 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
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196 /* FIXME: Store serial and check against incoming serial here. */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02001197 weston_seat_set_selection(seat, source, serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001198}
kabeer khan3a510d82014-10-20 11:47:15 +05301199static void
1200data_device_release(struct wl_client *client, struct wl_resource *resource)
1201{
1202 wl_resource_destroy(resource);
1203}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001204
1205static const struct wl_data_device_interface data_device_interface = {
1206 data_device_start_drag,
1207 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +05301208 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001209};
1210
1211static void
1212destroy_data_source(struct wl_resource *resource)
1213{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001214 struct weston_data_source *source =
1215 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001216 char **p;
1217
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001218 wl_signal_emit(&source->destroy_signal, source);
1219
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001220 wl_array_for_each(p, &source->mime_types)
1221 free(*p);
1222
1223 wl_array_release(&source->mime_types);
1224
Kristian Høgsberg489b2792013-06-25 11:26:31 -04001225 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001226}
1227
1228static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001229client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001230 uint32_t time, const char *mime_type)
1231{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001232 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001233}
1234
1235static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001236client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001237 const char *mime_type, int32_t fd)
1238{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001239 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001240 close(fd);
1241}
1242
1243static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001244client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001245{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001246 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001247}
1248
1249static void
1250create_data_source(struct wl_client *client,
1251 struct wl_resource *resource, uint32_t id)
1252{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001253 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001254
1255 source = malloc(sizeof *source);
1256 if (source == NULL) {
1257 wl_resource_post_no_memory(resource);
1258 return;
1259 }
1260
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001261 source->resource =
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001262 wl_resource_create(client, &wl_data_source_interface,
1263 wl_resource_get_version(resource), id);
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001264 if (source->resource == NULL) {
1265 free(source);
1266 wl_resource_post_no_memory(resource);
1267 return;
1268 }
1269
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001270 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001271 source->accept = client_source_accept;
1272 source->send = client_source_send;
1273 source->cancel = client_source_cancel;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001274 source->offer = NULL;
1275 source->accepted = false;
1276 source->seat = NULL;
Carlos Garnacho9c931792016-01-18 23:52:12 +01001277 source->actions_set = false;
1278 source->dnd_actions = 0;
1279 source->current_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Carlos Garnachob2889882016-01-15 21:14:26 +01001280 source->compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Harish Krupo737ac0d2019-04-19 22:06:37 +05301281 source->set_selection = false;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001282
1283 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001284
Jason Ekstranda85118c2013-06-27 20:17:02 -05001285 wl_resource_set_implementation(source->resource, &data_source_interface,
1286 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001287}
1288
1289static void unbind_data_device(struct wl_resource *resource)
1290{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001291 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001292}
1293
1294static void
1295get_data_device(struct wl_client *client,
1296 struct wl_resource *manager_resource,
1297 uint32_t id, struct wl_resource *seat_resource)
1298{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001299 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001300 struct wl_resource *resource;
1301
Jason Ekstranda85118c2013-06-27 20:17:02 -05001302 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +05301303 &wl_data_device_interface,
1304 wl_resource_get_version(manager_resource),
1305 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001306 if (resource == NULL) {
1307 wl_resource_post_no_memory(manager_resource);
1308 return;
1309 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001310
Alexandros Frantzis8480d132018-02-15 13:07:09 +02001311 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
Jason Ekstranda85118c2013-06-27 20:17:02 -05001318 wl_resource_set_implementation(resource, &data_device_interface,
1319 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001320}
1321
1322static const struct wl_data_device_manager_interface manager_interface = {
1323 create_data_source,
1324 get_data_device
1325};
1326
1327static void
1328bind_manager(struct wl_client *client,
1329 void *data, uint32_t version, uint32_t id)
1330{
Jason Ekstranda85118c2013-06-27 20:17:02 -05001331 struct wl_resource *resource;
1332
kabeer khan3a510d82014-10-20 11:47:15 +05301333 resource = wl_resource_create(client,
1334 &wl_data_device_manager_interface,
1335 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001336 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);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001343}
1344
1345WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001346wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001347{
Neil Roberts96d790e2013-09-19 17:32:00 +01001348 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -05001349 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001350
Derek Foreman1281a362015-07-31 16:55:32 -05001351 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001352 return;
1353
Derek Foreman1281a362015-07-31 16:55:32 -05001354 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001355 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001356 return;
1357
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001358 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001359}
1360
1361WL_EXPORT int
1362wl_data_device_manager_init(struct wl_display *display)
1363{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001364 if (wl_global_create(display,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001365 &wl_data_device_manager_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001366 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001367 return -1;
1368
1369 return 0;
1370}