blob: fc4e662cacf7733dde38bbf9dbe5c21b669e44dc [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
Pekka Paalanen82850052020-10-21 14:18:30 +0300404static const struct wl_data_source_interface data_source_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400405 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);
Jonathan Marlerd3223cc2021-04-22 21:25:18 -0600760 if (touch_drag->base.data_source) {
761 touch_drag->base.data_source->seat = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800762 wl_list_remove(&touch_drag->base.data_source_listener.link);
Jonathan Marlerd3223cc2021-04-22 21:25:18 -0600763 }
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800764 data_device_end_touch_drag_grab(touch_drag);
765}
766
767static void
768drag_grab_touch_focus(struct weston_touch_drag *drag)
769{
770 struct weston_touch *touch = drag->grab.touch;
771 struct weston_view *view;
772 wl_fixed_t view_x, view_y;
773
774 view = weston_compositor_pick_view(touch->seat->compositor,
775 touch->grab_x, touch->grab_y,
776 &view_x, &view_y);
777 if (drag->base.focus != view)
778 weston_drag_set_focus(&drag->base, touch->seat,
779 view, view_x, view_y);
780}
781
782static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200783drag_grab_touch_motion(struct weston_touch_grab *grab,
784 const struct timespec *time,
785 int touch_id, wl_fixed_t x, wl_fixed_t y)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800786{
787 struct weston_touch_drag *touch_drag =
788 container_of(grab, struct weston_touch_drag, grab);
789 struct weston_touch *touch = grab->touch;
790 wl_fixed_t view_x, view_y;
791 float fx, fy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200792 uint32_t msecs;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800793
794 if (touch_id != touch->grab_touch_id)
795 return;
796
797 drag_grab_touch_focus(touch_drag);
798 if (touch_drag->base.icon) {
799 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
800 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
801 weston_view_set_position(touch_drag->base.icon, fx, fy);
802 weston_view_schedule_repaint(touch_drag->base.icon);
803 }
804
805 if (touch_drag->base.focus_resource) {
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200806 msecs = timespec_to_msec(time);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800807 weston_view_from_global_fixed(touch_drag->base.focus,
808 touch->grab_x, touch->grab_y,
809 &view_x, &view_y);
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200810 wl_data_device_send_motion(touch_drag->base.focus_resource,
811 msecs, view_x, view_y);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800812 }
813}
814
815static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200816drag_grab_touch_frame(struct weston_touch_grab *grab)
817{
818}
819
820static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800821drag_grab_touch_cancel(struct weston_touch_grab *grab)
822{
823 struct weston_touch_drag *touch_drag =
824 container_of(grab, struct weston_touch_drag, grab);
825
826 if (touch_drag->base.data_source)
827 wl_list_remove(&touch_drag->base.data_source_listener.link);
828 data_device_end_touch_drag_grab(touch_drag);
829}
830
831static const struct weston_touch_grab_interface touch_drag_grab_interface = {
832 drag_grab_touch_down,
833 drag_grab_touch_up,
834 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200835 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800836 drag_grab_touch_cancel
837};
838
839static void
Carlos Garnachob2889882016-01-15 21:14:26 +0100840drag_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200841 const struct timespec *time, uint32_t key, uint32_t state)
Carlos Garnachob2889882016-01-15 21:14:26 +0100842{
843}
844
845static void
846drag_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
847 uint32_t serial, uint32_t mods_depressed,
848 uint32_t mods_latched,
849 uint32_t mods_locked, uint32_t group)
850{
851 struct weston_keyboard *keyboard = grab->keyboard;
852 struct weston_drag *drag =
853 container_of(grab, struct weston_drag, keyboard_grab);
854 uint32_t compositor_action;
855
856 if (mods_depressed & (1 << keyboard->xkb_info->shift_mod))
857 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
858 else if (mods_depressed & (1 << keyboard->xkb_info->ctrl_mod))
859 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
860 else
861 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
862
863 drag->data_source->compositor_action = compositor_action;
864
865 if (drag->data_source->offer)
866 data_offer_update_action(drag->data_source->offer);
867}
868
869static void
870drag_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
871{
872 struct weston_drag *drag =
873 container_of(grab, struct weston_drag, keyboard_grab);
874 struct weston_pointer *pointer = grab->keyboard->seat->pointer_state;
875 struct weston_touch *touch = grab->keyboard->seat->touch_state;
876
877 if (pointer && pointer->grab->interface == &pointer_drag_grab_interface) {
878 struct weston_touch_drag *touch_drag =
879 (struct weston_touch_drag *) drag;
880 drag_grab_touch_cancel(&touch_drag->grab);
881 } else if (touch && touch->grab->interface == &touch_drag_grab_interface) {
882 struct weston_pointer_drag *pointer_drag =
883 (struct weston_pointer_drag *) drag;
884 drag_grab_cancel(&pointer_drag->grab);
885 }
886}
887
888static const struct weston_keyboard_grab_interface keyboard_drag_grab_interface = {
889 drag_grab_keyboard_key,
890 drag_grab_keyboard_modifiers,
891 drag_grab_keyboard_cancel
892};
893
894static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800895destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
896{
897 struct weston_pointer_drag *drag = container_of(listener,
898 struct weston_pointer_drag, base.data_source_listener);
899
900 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400901}
902
903static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400904handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400905{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400906 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400907 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400908
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400909 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400910}
911
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700912WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800913weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700914 struct weston_data_source *source,
915 struct weston_surface *icon,
916 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400917{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800918 struct weston_pointer_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100919 struct weston_keyboard *keyboard =
920 weston_seat_get_keyboard(pointer->seat);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400921
Peter Huttererf3d62272013-08-08 11:57:05 +1000922 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700923 if (drag == NULL)
924 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400925
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800926 drag->grab.interface = &pointer_drag_grab_interface;
Carlos Garnachob2889882016-01-15 21:14:26 +0100927 drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800928 drag->base.client = client;
929 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400930
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400931 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800932 drag->base.icon = weston_view_create(icon);
933 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 free(drag);
935 return -1;
936 }
937
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800938 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500939 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800940 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400941
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200942 icon->committed = pointer_drag_surface_committed;
943 icon->committed_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300944 weston_surface_set_label_func(icon,
945 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800947 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500948 }
949
950 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800951 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800953 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400954 }
955
Derek Foremanf9318d12015-05-11 15:40:11 -0500956 weston_pointer_clear_focus(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100957 weston_keyboard_set_focus(keyboard, NULL);
958
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800959 weston_pointer_start_grab(pointer, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100960 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800961
962 return 0;
963}
964
965static void
966destroy_touch_data_device_source(struct wl_listener *listener, void *data)
967{
968 struct weston_touch_drag *drag = container_of(listener,
969 struct weston_touch_drag, base.data_source_listener);
970
971 data_device_end_touch_drag_grab(drag);
972}
973
974WL_EXPORT int
975weston_touch_start_drag(struct weston_touch *touch,
976 struct weston_data_source *source,
977 struct weston_surface *icon,
978 struct wl_client *client)
979{
980 struct weston_touch_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100981 struct weston_keyboard *keyboard =
982 weston_seat_get_keyboard(touch->seat);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800983
984 drag = zalloc(sizeof *drag);
985 if (drag == NULL)
986 return -1;
987
988 drag->grab.interface = &touch_drag_grab_interface;
989 drag->base.client = client;
990 drag->base.data_source = source;
991
992 if (icon) {
993 drag->base.icon = weston_view_create(icon);
994 if (drag->base.icon == NULL) {
995 free(drag);
996 return -1;
997 }
998
999 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
1000 wl_signal_add(&icon->destroy_signal,
1001 &drag->base.icon_destroy_listener);
1002
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001003 icon->committed = touch_drag_surface_committed;
1004 icon->committed_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001005 weston_surface_set_label_func(icon,
1006 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001007 } else {
1008 drag->base.icon = NULL;
1009 }
1010
1011 if (source) {
1012 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
1013 wl_signal_add(&source->destroy_signal,
1014 &drag->base.data_source_listener);
1015 }
1016
Carlos Garnachob2889882016-01-15 21:14:26 +01001017 weston_keyboard_set_focus(keyboard, NULL);
1018
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001019 weston_touch_start_grab(touch, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +01001020 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001021
1022 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -07001023
1024 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001025}
1026
1027static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001028data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
1029 struct wl_resource *source_resource,
1030 struct wl_resource *origin_resource,
1031 struct wl_resource *icon_resource, uint32_t serial)
1032{
1033 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001034 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1035 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001036 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -07001037 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001038 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001039 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001040 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001041
Derek Foreman1281a362015-07-31 16:55:32 -05001042 is_pointer_grab = pointer &&
1043 pointer->button_count == 1 &&
1044 pointer->grab_serial == serial &&
1045 pointer->focus &&
1046 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001047
Derek Foreman1281a362015-07-31 16:55:32 -05001048 is_touch_grab = touch &&
1049 touch->num_tp == 1 &&
1050 touch->grab_serial == serial &&
1051 touch->focus &&
1052 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001053
1054 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001055 return;
1056
1057 /* FIXME: Check that the data source type array isn't empty. */
1058
1059 if (source_resource)
1060 source = wl_resource_get_user_data(source_resource);
1061 if (icon_resource)
1062 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +03001063
1064 if (icon) {
1065 if (weston_surface_set_role(icon, "wl_data_device-icon",
1066 resource,
1067 WL_DATA_DEVICE_ERROR_ROLE) < 0)
1068 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001069 }
1070
Jason Ekstrand8202d722014-06-24 21:19:24 -07001071 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001072 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001073 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001074 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001075
1076 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001077 wl_resource_post_no_memory(resource);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001078 else
1079 source->seat = seat;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001080}
1081
1082static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001083destroy_selection_data_source(struct wl_listener *listener, void *data)
1084{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001085 struct weston_seat *seat = container_of(listener, struct weston_seat,
1086 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05001087 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001088 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +01001089 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001090
1091 seat->selection_data_source = NULL;
1092
Derek Foreman1281a362015-07-31 16:55:32 -05001093 if (keyboard)
1094 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001095 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001096 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01001097 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001098 if (data_device)
1099 wl_data_device_send_selection(data_device, NULL);
1100 }
1101
1102 wl_signal_emit(&seat->selection_signal, seat);
1103}
1104
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001105/** \brief Send the selection to the specified client
1106 *
1107 * This function creates a new wl_data_offer if there is a wl_data_source
1108 * currently set as the selection and sends it to the specified client,
1109 * followed by the wl_data_device.selection() event.
1110 * If there is no current selection the wl_data_device.selection() event
1111 * will carry a NULL wl_data_offer.
1112 *
1113 * If the client does not have a wl_data_device for the specified seat
1114 * nothing will be done.
1115 *
1116 * \param seat The seat owning the wl_data_device used to send the events.
1117 * \param client The client to which to send the selection.
1118 */
1119WL_EXPORT void
1120weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
1121{
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001122 struct weston_data_offer *offer;
1123 struct wl_resource *data_device;
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001124
Giulio Camuffod46bb012015-05-01 12:59:36 +03001125 wl_resource_for_each(data_device, &seat->drag_resource_list) {
1126 if (wl_resource_get_client(data_device) != client)
1127 continue;
1128
1129 if (seat->selection_data_source) {
1130 offer = weston_data_source_send_offer(seat->selection_data_source,
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001131 data_device);
1132 wl_data_device_send_selection(data_device, offer->resource);
Giulio Camuffod46bb012015-05-01 12:59:36 +03001133 } else {
1134 wl_data_device_send_selection(data_device, NULL);
1135 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001136 }
1137}
1138
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001139WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001140weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001141 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001142{
Neil Roberts96d790e2013-09-19 17:32:00 +01001143 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -05001144 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001145
1146 if (seat->selection_data_source &&
1147 seat->selection_serial - serial < UINT32_MAX / 2)
1148 return;
1149
1150 if (seat->selection_data_source) {
1151 seat->selection_data_source->cancel(seat->selection_data_source);
1152 wl_list_remove(&seat->selection_data_source_listener.link);
1153 seat->selection_data_source = NULL;
1154 }
1155
1156 seat->selection_data_source = source;
1157 seat->selection_serial = serial;
Emmanuel Gil Peyrot45f5e532019-08-15 14:02:22 +02001158
1159 if (source)
1160 source->set_selection = true;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001161
Derek Foreman1281a362015-07-31 16:55:32 -05001162 if (keyboard)
1163 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001164 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001165 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001166 }
1167
1168 wl_signal_emit(&seat->selection_signal, seat);
1169
1170 if (source) {
1171 seat->selection_data_source_listener.notify =
1172 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001173 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001174 &seat->selection_data_source_listener);
1175 }
1176}
1177
1178static void
1179data_device_set_selection(struct wl_client *client,
1180 struct wl_resource *resource,
1181 struct wl_resource *source_resource, uint32_t serial)
1182{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02001183 struct weston_seat *seat = wl_resource_get_user_data(resource);
Carlos Garnacho9c931792016-01-18 23:52:12 +01001184 struct weston_data_source *source;
1185
Alexandros Frantzis8480d132018-02-15 13:07:09 +02001186 if (!seat || !source_resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001187 return;
1188
Carlos Garnacho9c931792016-01-18 23:52:12 +01001189 source = wl_resource_get_user_data(source_resource);
1190
1191 if (source->actions_set) {
1192 wl_resource_post_error(source_resource,
1193 WL_DATA_SOURCE_ERROR_INVALID_SOURCE,
1194 "cannot set drag-and-drop source as selection");
1195 return;
1196 }
1197
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001198 /* FIXME: Store serial and check against incoming serial here. */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02001199 weston_seat_set_selection(seat, source, serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001200}
kabeer khan3a510d82014-10-20 11:47:15 +05301201static void
1202data_device_release(struct wl_client *client, struct wl_resource *resource)
1203{
1204 wl_resource_destroy(resource);
1205}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001206
1207static const struct wl_data_device_interface data_device_interface = {
1208 data_device_start_drag,
1209 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +05301210 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001211};
1212
1213static void
1214destroy_data_source(struct wl_resource *resource)
1215{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001216 struct weston_data_source *source =
1217 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001218 char **p;
1219
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001220 wl_signal_emit(&source->destroy_signal, source);
1221
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001222 wl_array_for_each(p, &source->mime_types)
1223 free(*p);
1224
1225 wl_array_release(&source->mime_types);
1226
Kristian Høgsberg489b2792013-06-25 11:26:31 -04001227 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001228}
1229
1230static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001231client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001232 uint32_t time, const char *mime_type)
1233{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001234 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001235}
1236
1237static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001238client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001239 const char *mime_type, int32_t fd)
1240{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001241 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001242 close(fd);
1243}
1244
1245static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001246client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001247{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001248 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001249}
1250
1251static void
1252create_data_source(struct wl_client *client,
1253 struct wl_resource *resource, uint32_t id)
1254{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001255 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001256
1257 source = malloc(sizeof *source);
1258 if (source == NULL) {
1259 wl_resource_post_no_memory(resource);
1260 return;
1261 }
1262
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001263 source->resource =
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001264 wl_resource_create(client, &wl_data_source_interface,
1265 wl_resource_get_version(resource), id);
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001266 if (source->resource == NULL) {
1267 free(source);
1268 wl_resource_post_no_memory(resource);
1269 return;
1270 }
1271
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001272 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001273 source->accept = client_source_accept;
1274 source->send = client_source_send;
1275 source->cancel = client_source_cancel;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001276 source->offer = NULL;
1277 source->accepted = false;
1278 source->seat = NULL;
Carlos Garnacho9c931792016-01-18 23:52:12 +01001279 source->actions_set = false;
1280 source->dnd_actions = 0;
1281 source->current_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Carlos Garnachob2889882016-01-15 21:14:26 +01001282 source->compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Harish Krupo737ac0d2019-04-19 22:06:37 +05301283 source->set_selection = false;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001284
1285 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001286
Jason Ekstranda85118c2013-06-27 20:17:02 -05001287 wl_resource_set_implementation(source->resource, &data_source_interface,
1288 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001289}
1290
1291static void unbind_data_device(struct wl_resource *resource)
1292{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001293 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001294}
1295
1296static void
1297get_data_device(struct wl_client *client,
1298 struct wl_resource *manager_resource,
1299 uint32_t id, struct wl_resource *seat_resource)
1300{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001301 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001302 struct wl_resource *resource;
1303
Jason Ekstranda85118c2013-06-27 20:17:02 -05001304 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +05301305 &wl_data_device_interface,
1306 wl_resource_get_version(manager_resource),
1307 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001308 if (resource == NULL) {
1309 wl_resource_post_no_memory(manager_resource);
1310 return;
1311 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001312
Alexandros Frantzis8480d132018-02-15 13:07:09 +02001313 if (seat) {
1314 wl_list_insert(&seat->drag_resource_list,
1315 wl_resource_get_link(resource));
1316 } else {
1317 wl_list_init(wl_resource_get_link(resource));
1318 }
1319
Jason Ekstranda85118c2013-06-27 20:17:02 -05001320 wl_resource_set_implementation(resource, &data_device_interface,
1321 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001322}
1323
1324static const struct wl_data_device_manager_interface manager_interface = {
1325 create_data_source,
1326 get_data_device
1327};
1328
1329static void
1330bind_manager(struct wl_client *client,
1331 void *data, uint32_t version, uint32_t id)
1332{
Jason Ekstranda85118c2013-06-27 20:17:02 -05001333 struct wl_resource *resource;
1334
kabeer khan3a510d82014-10-20 11:47:15 +05301335 resource = wl_resource_create(client,
1336 &wl_data_device_manager_interface,
1337 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001338 if (resource == NULL) {
1339 wl_client_post_no_memory(client);
1340 return;
1341 }
1342
1343 wl_resource_set_implementation(resource, &manager_interface,
1344 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001345}
1346
1347WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001348wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001349{
Neil Roberts96d790e2013-09-19 17:32:00 +01001350 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -05001351 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001352
Derek Foreman1281a362015-07-31 16:55:32 -05001353 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001354 return;
1355
Derek Foreman1281a362015-07-31 16:55:32 -05001356 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001357 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001358 return;
1359
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001360 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001361}
1362
1363WL_EXPORT int
1364wl_data_device_manager_init(struct wl_display *display)
1365{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001366 if (wl_global_create(display,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001367 &wl_data_device_manager_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001368 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001369 return -1;
1370
1371 return 0;
1372}