blob: 44a08f914bb128706bba64b68a251b6e9c415b72 [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>
31#include <stdio.h>
Jason Ekstranda7af7042013-10-12 22:38:11 -050032#include <assert.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040033
34#include "compositor.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070035#include "shared/helpers.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040036
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040037struct weston_drag {
38 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070039 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040040 struct wl_listener data_source_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050041 struct weston_view *focus;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040042 struct wl_resource *focus_resource;
43 struct wl_listener focus_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050044 struct weston_view *icon;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040045 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040046 int32_t dx, dy;
Carlos Garnachob2889882016-01-15 21:14:26 +010047 struct weston_keyboard_grab keyboard_grab;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040048};
49
Xiong Zhangfd51e7b2013-11-25 18:42:49 +080050struct weston_pointer_drag {
51 struct weston_drag base;
52 struct weston_pointer_grab grab;
53};
54
55struct weston_touch_drag {
56 struct weston_drag base;
57 struct weston_touch_grab grab;
58};
59
Carlos Garnacho9c931792016-01-18 23:52:12 +010060#define ALL_ACTIONS (WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | \
61 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | \
62 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
63
Kristian Høgsberg2158a882013-04-18 15:07:39 -040064static void
65data_offer_accept(struct wl_client *client, struct wl_resource *resource,
66 uint32_t serial, const char *mime_type)
67{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070068 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040069
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010070 /* Protect against untimely calls from older data offers */
71 if (!offer->source || offer != offer->source->offer)
72 return;
73
Kristian Høgsberg2158a882013-04-18 15:07:39 -040074 /* FIXME: Check that client is currently focused by the input
75 * device that is currently dragging this data source. Should
76 * this be a wl_data_device request? */
77
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010078 offer->source->accept(offer->source, serial, mime_type);
79 offer->source->accepted = mime_type != NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -040080}
81
82static void
83data_offer_receive(struct wl_client *client, struct wl_resource *resource,
84 const char *mime_type, int32_t fd)
85{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070086 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040087
Carlos Garnacho78d4bf92016-01-15 21:14:23 +010088 if (offer->source && offer == offer->source->offer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040089 offer->source->send(offer->source, mime_type, fd);
90 else
91 close(fd);
92}
93
94static void
95data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
96{
97 wl_resource_destroy(resource);
98}
99
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100100static void
101data_source_notify_finish(struct weston_data_source *source)
102{
Carlos Garnacho4061e2b2016-02-01 20:28:16 +0100103 if (!source->actions_set)
104 return;
105
Carlos Garnacho9c931792016-01-18 23:52:12 +0100106 if (source->offer->in_ask &&
107 wl_resource_get_version(source->resource) >=
108 WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
109 wl_data_source_send_action(source->resource,
110 source->current_dnd_action);
111 }
112
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100113 if (wl_resource_get_version(source->resource) >=
114 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
115 wl_data_source_send_dnd_finished(source->resource);
116 }
117
118 source->offer = NULL;
119}
120
Carlos Garnacho9c931792016-01-18 23:52:12 +0100121static uint32_t
122data_offer_choose_action(struct weston_data_offer *offer)
123{
124 uint32_t available_actions, preferred_action = 0;
125 uint32_t source_actions, offer_actions;
126
127 if (wl_resource_get_version(offer->resource) >=
128 WL_DATA_OFFER_ACTION_SINCE_VERSION) {
129 offer_actions = offer->dnd_actions;
130 preferred_action = offer->preferred_dnd_action;
131 } else {
132 offer_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
133 }
134
135 if (wl_resource_get_version(offer->source->resource) >=
136 WL_DATA_SOURCE_ACTION_SINCE_VERSION)
137 source_actions = offer->source->dnd_actions;
138 else
139 source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
140
141 available_actions = offer_actions & source_actions;
142
143 if (!available_actions)
144 return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
145
Carlos Garnachob2889882016-01-15 21:14:26 +0100146 if (offer->source->seat &&
147 offer->source->compositor_action & available_actions)
148 return offer->source->compositor_action;
149
Carlos Garnacho9c931792016-01-18 23:52:12 +0100150 /* If the dest side has a preferred DnD action, use it */
151 if ((preferred_action & available_actions) != 0)
152 return preferred_action;
153
154 /* Use the first found action, in bit order */
155 return 1 << (ffs(available_actions) - 1);
156}
157
158static void
159data_offer_update_action(struct weston_data_offer *offer)
160{
161 uint32_t action;
162
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800163 if (!offer->source)
Carlos Garnacho9c931792016-01-18 23:52:12 +0100164 return;
165
166 action = data_offer_choose_action(offer);
167
168 if (offer->source->current_dnd_action == action)
169 return;
170
171 offer->source->current_dnd_action = action;
172
173 if (offer->in_ask)
174 return;
175
176 if (wl_resource_get_version(offer->source->resource) >=
177 WL_DATA_SOURCE_ACTION_SINCE_VERSION)
178 wl_data_source_send_action(offer->source->resource, action);
179
180 if (wl_resource_get_version(offer->resource) >=
181 WL_DATA_OFFER_ACTION_SINCE_VERSION)
182 wl_data_offer_send_action(offer->resource, action);
183}
184
185static void
186data_offer_set_actions(struct wl_client *client,
187 struct wl_resource *resource,
188 uint32_t dnd_actions, uint32_t preferred_action)
189{
190 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
191
192 if (dnd_actions & ~ALL_ACTIONS) {
193 wl_resource_post_error(offer->resource,
194 WL_DATA_OFFER_ERROR_INVALID_ACTION_MASK,
195 "invalid action mask %x", dnd_actions);
196 return;
197 }
198
199 if (preferred_action &&
200 (!(preferred_action & dnd_actions) ||
201 __builtin_popcount(preferred_action) > 1)) {
202 wl_resource_post_error(offer->resource,
203 WL_DATA_OFFER_ERROR_INVALID_ACTION,
204 "invalid action %x", preferred_action);
205 return;
206 }
207
208 offer->dnd_actions = dnd_actions;
209 offer->preferred_dnd_action = preferred_action;
210 data_offer_update_action(offer);
211}
212
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100213static void
214data_offer_finish(struct wl_client *client, struct wl_resource *resource)
215{
216 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
217
218 if (!offer->source || offer->source->offer != offer)
219 return;
220
221 /* Disallow finish while we have a grab driving drag-and-drop, or
222 * if the negotiation is not at the right stage
223 */
224 if (offer->source->seat ||
225 !offer->source->accepted) {
226 wl_resource_post_error(offer->resource,
227 WL_DATA_OFFER_ERROR_INVALID_FINISH,
228 "premature finish request");
229 return;
230 }
231
Carlos Garnacho9c931792016-01-18 23:52:12 +0100232 switch (offer->source->current_dnd_action) {
233 case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE:
234 case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK:
235 wl_resource_post_error(offer->resource,
236 WL_DATA_OFFER_ERROR_INVALID_OFFER,
237 "offer finished with an invalid action");
238 return;
239 default:
240 break;
241 }
242
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100243 data_source_notify_finish(offer->source);
244}
245
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400246static const struct wl_data_offer_interface data_offer_interface = {
247 data_offer_accept,
248 data_offer_receive,
249 data_offer_destroy,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100250 data_offer_finish,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100251 data_offer_set_actions,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400252};
253
254static void
255destroy_data_offer(struct wl_resource *resource)
256{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700257 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400258
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100259 if (!offer->source)
260 goto out;
261
262 wl_list_remove(&offer->source_destroy_listener.link);
263
264 if (offer->source->offer != offer)
265 goto out;
266
267 /* If the drag destination has version < 3, wl_data_offer.finish
268 * won't be called, so do this here as a safety net, because
269 * we still want the version >=3 drag source to be happy.
270 */
271 if (wl_resource_get_version(offer->resource) <
272 WL_DATA_OFFER_ACTION_SINCE_VERSION) {
273 data_source_notify_finish(offer->source);
Carlos Garnacho4061e2b2016-02-01 20:28:16 +0100274 } else if (offer->source->resource &&
275 wl_resource_get_version(offer->source->resource) >=
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100276 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
277 wl_data_source_send_cancelled(offer->source->resource);
278 }
279
280 offer->source->offer = NULL;
281out:
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400282 free(offer);
283}
284
285static void
286destroy_offer_data_source(struct wl_listener *listener, void *data)
287{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700288 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400289
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700290 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400291 source_destroy_listener);
292
293 offer->source = NULL;
294}
295
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800296static struct weston_data_offer *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700297weston_data_source_send_offer(struct weston_data_source *source,
298 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400299{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700300 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400301 char **p;
302
303 offer = malloc(sizeof *offer);
304 if (offer == NULL)
305 return NULL;
306
Jason Ekstranda85118c2013-06-27 20:17:02 -0500307 offer->resource =
308 wl_resource_create(wl_resource_get_client(target),
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100309 &wl_data_offer_interface,
310 wl_resource_get_version(target), 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700311 if (offer->resource == NULL) {
312 free(offer);
313 return NULL;
314 }
315
Jason Ekstranda85118c2013-06-27 20:17:02 -0500316 wl_resource_set_implementation(offer->resource, &data_offer_interface,
317 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400318
Carlos Garnacho9c931792016-01-18 23:52:12 +0100319 offer->in_ask = false;
320 offer->dnd_actions = 0;
321 offer->preferred_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400322 offer->source = source;
323 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500324 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400325 &offer->source_destroy_listener);
326
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500327 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400328
329 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500330 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400331
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100332 source->offer = offer;
333 source->accepted = false;
334
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800335 return offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400336}
337
338static void
339data_source_offer(struct wl_client *client,
340 struct wl_resource *resource,
341 const char *type)
342{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700343 struct weston_data_source *source =
344 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400345 char **p;
346
347 p = wl_array_add(&source->mime_types, sizeof *p);
348 if (p)
349 *p = strdup(type);
350 if (!p || !*p)
351 wl_resource_post_no_memory(resource);
352}
353
354static void
355data_source_destroy(struct wl_client *client, struct wl_resource *resource)
356{
357 wl_resource_destroy(resource);
358}
359
Carlos Garnacho9c931792016-01-18 23:52:12 +0100360static void
361data_source_set_actions(struct wl_client *client,
362 struct wl_resource *resource,
363 uint32_t dnd_actions)
364{
365 struct weston_data_source *source =
366 wl_resource_get_user_data(resource);
367
368 if (source->actions_set) {
369 wl_resource_post_error(source->resource,
370 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
371 "cannot set actions more than once");
372 return;
373 }
374
375 if (dnd_actions & ~ALL_ACTIONS) {
376 wl_resource_post_error(source->resource,
377 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
378 "invalid action mask %x", dnd_actions);
379 return;
380 }
381
382 if (source->seat) {
383 wl_resource_post_error(source->resource,
384 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
385 "invalid action change after "
386 "wl_data_device.start_drag");
387 return;
388 }
389
390 source->dnd_actions = dnd_actions;
391 source->actions_set = true;
392}
393
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400394static struct wl_data_source_interface data_source_interface = {
395 data_source_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100396 data_source_destroy,
397 data_source_set_actions
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398};
399
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800401drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100402 struct weston_pointer *pointer,
403 struct weston_touch *touch,
404 struct weston_surface *es,
405 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400406{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300407 struct weston_layer_entry *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400408 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400409
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800410 assert((pointer != NULL && touch == NULL) ||
411 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500412
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400413 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800414 if (pointer && pointer->sprite &&
415 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400416 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400417 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400419
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300420 weston_layer_entry_remove(&drag->icon->layer_link);
421 weston_layer_entry_insert(list, &drag->icon->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500422 weston_view_update_transform(drag->icon);
Jason Ekstrandef540082014-06-26 10:37:36 -0700423 pixman_region32_clear(&es->pending.input);
Armin Krezovićf8486c32016-06-30 06:04:28 +0200424 es->is_mapped = true;
425 drag->icon->is_mapped = true;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400426 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400427
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400428 drag->dx += sx;
429 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400430
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800431 /* init to 0 for avoiding a compile warning */
432 fx = fy = 0;
433 if (pointer) {
434 fx = wl_fixed_to_double(pointer->x) + drag->dx;
435 fy = wl_fixed_to_double(pointer->y) + drag->dy;
436 } else if (touch) {
437 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
438 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
439 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600440 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400441}
442
Pekka Paalanen8274d902014-08-06 19:36:51 +0300443static int
444pointer_drag_surface_get_label(struct weston_surface *surface,
445 char *buf, size_t len)
446{
447 return snprintf(buf, len, "pointer drag icon");
448}
449
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400450static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100451pointer_drag_surface_configure(struct weston_surface *es,
452 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800453{
454 struct weston_pointer_drag *drag = es->configure_private;
455 struct weston_pointer *pointer = drag->grab.pointer;
456
457 assert(es->configure == pointer_drag_surface_configure);
458
Jonas Ådahl767d8912013-12-03 22:30:17 +0100459 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800460}
461
Pekka Paalanen8274d902014-08-06 19:36:51 +0300462static int
463touch_drag_surface_get_label(struct weston_surface *surface,
464 char *buf, size_t len)
465{
466 return snprintf(buf, len, "touch drag icon");
467}
468
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800469static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100470touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800471{
472 struct weston_touch_drag *drag = es->configure_private;
473 struct weston_touch *touch = drag->grab.touch;
474
475 assert(es->configure == touch_drag_surface_configure);
476
Jonas Ådahl767d8912013-12-03 22:30:17 +0100477 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800478}
479
480static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400481destroy_drag_focus(struct wl_listener *listener, void *data)
482{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400483 struct weston_drag *drag =
484 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400485
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400486 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400487}
488
489static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800490weston_drag_set_focus(struct weston_drag *drag,
491 struct weston_seat *seat,
492 struct weston_view *view,
493 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400494{
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100495 struct wl_resource *resource, *offer_resource = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800496 struct wl_display *display = seat->compositor->wl_display;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100497 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400498 uint32_t serial;
499
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 if (drag->focus && view && drag->focus->surface == view->surface) {
501 drag->focus = view;
502 return;
503 }
504
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400505 if (drag->focus_resource) {
506 wl_data_device_send_leave(drag->focus_resource);
507 wl_list_remove(&drag->focus_listener.link);
508 drag->focus_resource = NULL;
509 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400510 }
511
Jason Ekstranda7af7042013-10-12 22:38:11 -0500512 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400513 return;
514
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500515 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500516 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400517 return;
518
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100519 if (drag->data_source &&
520 drag->data_source->offer) {
521 /* Unlink the offer from the source */
522 offer = drag->data_source->offer;
523 offer->source = NULL;
524 drag->data_source->offer = NULL;
525 wl_list_remove(&offer->source_destroy_listener.link);
526 }
527
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800528 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500529 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400530 if (!resource)
531 return;
532
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533 serial = wl_display_next_serial(display);
534
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700535 if (drag->data_source) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100536 drag->data_source->accepted = false;
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800537 offer = weston_data_source_send_offer(drag->data_source, resource);
538 if (offer == NULL)
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700539 return;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100540
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800541 data_offer_update_action(offer);
542
543 offer_resource = offer->resource;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100544 if (wl_resource_get_version (offer_resource) >=
545 WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
546 wl_data_offer_send_source_actions (offer_resource,
547 drag->data_source->dnd_actions);
548 }
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700549 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400550
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551 wl_data_device_send_enter(resource, serial, view->surface->resource,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100552 sx, sy, offer_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400553
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400555 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500556 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400557 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400558}
559
560static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400561drag_grab_focus(struct weston_pointer_grab *grab)
562{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800563 struct weston_pointer_drag *drag =
564 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400565 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500566 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400567 wl_fixed_t sx, sy;
568
Jason Ekstranda7af7042013-10-12 22:38:11 -0500569 view = weston_compositor_pick_view(pointer->seat->compositor,
570 pointer->x, pointer->y,
571 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800572 if (drag->base.focus != view)
573 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400574}
575
576static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100577drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200578 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400579{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800580 struct weston_pointer_drag *drag =
581 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400582 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400583 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400584 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400585
Jonas Ådahld2510102014-10-05 21:39:14 +0200586 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100587
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800588 if (drag->base.icon) {
589 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
590 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
591 weston_view_set_position(drag->base.icon, fx, fy);
592 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400593 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400594
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800595 if (drag->base.focus_resource) {
596 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500597 pointer->x, pointer->y,
598 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400599
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800600 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400601 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400602}
603
604static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800605data_device_end_drag_grab(struct weston_drag *drag,
606 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400607{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400608 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500609 if (weston_view_is_mapped(drag->icon))
610 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400611
Jason Ekstranda7af7042013-10-12 22:38:11 -0500612 drag->icon->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300613 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700614 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400615 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500616 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400617 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400618
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800619 weston_drag_set_focus(drag, seat, NULL, 0, 0);
620}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400621
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800622static void
623data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
624{
625 struct weston_pointer *pointer = drag->grab.pointer;
Carlos Garnachob2889882016-01-15 21:14:26 +0100626 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400627
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800628 data_device_end_drag_grab(&drag->base, pointer->seat);
629 weston_pointer_end_grab(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100630 weston_keyboard_end_grab(keyboard);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400631 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400632}
633
634static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400635drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400636 uint32_t time, uint32_t button, uint32_t state_w)
637{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800638 struct weston_pointer_drag *drag =
639 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400640 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400641 enum wl_pointer_button_state state = state_w;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100642 struct weston_data_source *data_source = drag->base.data_source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400643
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100644 if (data_source &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400645 pointer->grab_button == button &&
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100646 state == WL_POINTER_BUTTON_STATE_RELEASED) {
647 if (drag->base.focus_resource &&
Carlos Garnacho9c931792016-01-18 23:52:12 +0100648 data_source->accepted &&
649 data_source->current_dnd_action) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100650 wl_data_device_send_drop(drag->base.focus_resource);
651
652 if (wl_resource_get_version(data_source->resource) >=
653 WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION)
654 wl_data_source_send_dnd_drop_performed(data_source->resource);
655
Carlos Garnacho9c931792016-01-18 23:52:12 +0100656 data_source->offer->in_ask =
657 data_source->current_dnd_action ==
658 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
659
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100660 data_source->seat = NULL;
661 } else if (wl_resource_get_version(data_source->resource) >=
662 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
663 wl_data_source_send_cancelled(data_source->resource);
664 }
665 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400666
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400667 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400668 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800669 if (drag->base.data_source)
670 wl_list_remove(&drag->base.data_source_listener.link);
671 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400672 }
673}
674
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200675static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200676drag_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000677 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200678{
679}
680
681static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000682drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
683{
684}
685
686static void
687drag_grab_frame(struct weston_pointer_grab *grab)
688{
689}
690
691static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200692drag_grab_cancel(struct weston_pointer_grab *grab)
693{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800694 struct weston_pointer_drag *drag =
695 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200696
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800697 if (drag->base.data_source)
698 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200699
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800700 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200701}
702
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800703static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400704 drag_grab_focus,
705 drag_grab_motion,
706 drag_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200707 drag_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000708 drag_grab_axis_source,
709 drag_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200710 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400711};
712
713static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800714drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
715 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400716{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800717}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400718
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800719static void
720data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
721{
722 struct weston_touch *touch = drag->grab.touch;
Carlos Garnachob2889882016-01-15 21:14:26 +0100723 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800724
725 data_device_end_drag_grab(&drag->base, touch->seat);
726 weston_touch_end_grab(touch);
Carlos Garnachob2889882016-01-15 21:14:26 +0100727 weston_keyboard_end_grab(keyboard);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800728 free(drag);
729}
730
731static void
732drag_grab_touch_up(struct weston_touch_grab *grab,
733 uint32_t time, int touch_id)
734{
735 struct weston_touch_drag *touch_drag =
736 container_of(grab, struct weston_touch_drag, grab);
737 struct weston_touch *touch = grab->touch;
738
739 if (touch_id != touch->grab_touch_id)
740 return;
741
742 if (touch_drag->base.focus_resource)
743 wl_data_device_send_drop(touch_drag->base.focus_resource);
744 if (touch_drag->base.data_source)
745 wl_list_remove(&touch_drag->base.data_source_listener.link);
746 data_device_end_touch_drag_grab(touch_drag);
747}
748
749static void
750drag_grab_touch_focus(struct weston_touch_drag *drag)
751{
752 struct weston_touch *touch = drag->grab.touch;
753 struct weston_view *view;
754 wl_fixed_t view_x, view_y;
755
756 view = weston_compositor_pick_view(touch->seat->compositor,
757 touch->grab_x, touch->grab_y,
758 &view_x, &view_y);
759 if (drag->base.focus != view)
760 weston_drag_set_focus(&drag->base, touch->seat,
761 view, view_x, view_y);
762}
763
764static void
765drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300766 int touch_id, wl_fixed_t x, wl_fixed_t y)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800767{
768 struct weston_touch_drag *touch_drag =
769 container_of(grab, struct weston_touch_drag, grab);
770 struct weston_touch *touch = grab->touch;
771 wl_fixed_t view_x, view_y;
772 float fx, fy;
773
774 if (touch_id != touch->grab_touch_id)
775 return;
776
777 drag_grab_touch_focus(touch_drag);
778 if (touch_drag->base.icon) {
779 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
780 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
781 weston_view_set_position(touch_drag->base.icon, fx, fy);
782 weston_view_schedule_repaint(touch_drag->base.icon);
783 }
784
785 if (touch_drag->base.focus_resource) {
786 weston_view_from_global_fixed(touch_drag->base.focus,
787 touch->grab_x, touch->grab_y,
788 &view_x, &view_y);
789 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
790 view_x, view_y);
791 }
792}
793
794static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200795drag_grab_touch_frame(struct weston_touch_grab *grab)
796{
797}
798
799static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800800drag_grab_touch_cancel(struct weston_touch_grab *grab)
801{
802 struct weston_touch_drag *touch_drag =
803 container_of(grab, struct weston_touch_drag, grab);
804
805 if (touch_drag->base.data_source)
806 wl_list_remove(&touch_drag->base.data_source_listener.link);
807 data_device_end_touch_drag_grab(touch_drag);
808}
809
810static const struct weston_touch_grab_interface touch_drag_grab_interface = {
811 drag_grab_touch_down,
812 drag_grab_touch_up,
813 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200814 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800815 drag_grab_touch_cancel
816};
817
818static void
Carlos Garnachob2889882016-01-15 21:14:26 +0100819drag_grab_keyboard_key(struct weston_keyboard_grab *grab,
820 uint32_t time, uint32_t key, uint32_t state)
821{
822}
823
824static void
825drag_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
826 uint32_t serial, uint32_t mods_depressed,
827 uint32_t mods_latched,
828 uint32_t mods_locked, uint32_t group)
829{
830 struct weston_keyboard *keyboard = grab->keyboard;
831 struct weston_drag *drag =
832 container_of(grab, struct weston_drag, keyboard_grab);
833 uint32_t compositor_action;
834
835 if (mods_depressed & (1 << keyboard->xkb_info->shift_mod))
836 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
837 else if (mods_depressed & (1 << keyboard->xkb_info->ctrl_mod))
838 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
839 else
840 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
841
842 drag->data_source->compositor_action = compositor_action;
843
844 if (drag->data_source->offer)
845 data_offer_update_action(drag->data_source->offer);
846}
847
848static void
849drag_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
850{
851 struct weston_drag *drag =
852 container_of(grab, struct weston_drag, keyboard_grab);
853 struct weston_pointer *pointer = grab->keyboard->seat->pointer_state;
854 struct weston_touch *touch = grab->keyboard->seat->touch_state;
855
856 if (pointer && pointer->grab->interface == &pointer_drag_grab_interface) {
857 struct weston_touch_drag *touch_drag =
858 (struct weston_touch_drag *) drag;
859 drag_grab_touch_cancel(&touch_drag->grab);
860 } else if (touch && touch->grab->interface == &touch_drag_grab_interface) {
861 struct weston_pointer_drag *pointer_drag =
862 (struct weston_pointer_drag *) drag;
863 drag_grab_cancel(&pointer_drag->grab);
864 }
865}
866
867static const struct weston_keyboard_grab_interface keyboard_drag_grab_interface = {
868 drag_grab_keyboard_key,
869 drag_grab_keyboard_modifiers,
870 drag_grab_keyboard_cancel
871};
872
873static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800874destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
875{
876 struct weston_pointer_drag *drag = container_of(listener,
877 struct weston_pointer_drag, base.data_source_listener);
878
879 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400880}
881
882static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400883handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400884{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400885 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400886 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400887
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400888 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400889}
890
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700891WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800892weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700893 struct weston_data_source *source,
894 struct weston_surface *icon,
895 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400896{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800897 struct weston_pointer_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100898 struct weston_keyboard *keyboard =
899 weston_seat_get_keyboard(pointer->seat);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400900
Peter Huttererf3d62272013-08-08 11:57:05 +1000901 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700902 if (drag == NULL)
903 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400904
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800905 drag->grab.interface = &pointer_drag_grab_interface;
Carlos Garnachob2889882016-01-15 21:14:26 +0100906 drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800907 drag->base.client = client;
908 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400909
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400910 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800911 drag->base.icon = weston_view_create(icon);
912 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 free(drag);
914 return -1;
915 }
916
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800917 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500918 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800919 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400920
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800921 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400922 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300923 weston_surface_set_label_func(icon,
924 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800926 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927 }
928
929 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800930 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800932 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400933 }
934
Derek Foremanf9318d12015-05-11 15:40:11 -0500935 weston_pointer_clear_focus(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100936 weston_keyboard_set_focus(keyboard, NULL);
937
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800938 weston_pointer_start_grab(pointer, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100939 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800940
941 return 0;
942}
943
944static void
945destroy_touch_data_device_source(struct wl_listener *listener, void *data)
946{
947 struct weston_touch_drag *drag = container_of(listener,
948 struct weston_touch_drag, base.data_source_listener);
949
950 data_device_end_touch_drag_grab(drag);
951}
952
953WL_EXPORT int
954weston_touch_start_drag(struct weston_touch *touch,
955 struct weston_data_source *source,
956 struct weston_surface *icon,
957 struct wl_client *client)
958{
959 struct weston_touch_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100960 struct weston_keyboard *keyboard =
961 weston_seat_get_keyboard(touch->seat);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800962
963 drag = zalloc(sizeof *drag);
964 if (drag == NULL)
965 return -1;
966
967 drag->grab.interface = &touch_drag_grab_interface;
968 drag->base.client = client;
969 drag->base.data_source = source;
970
971 if (icon) {
972 drag->base.icon = weston_view_create(icon);
973 if (drag->base.icon == NULL) {
974 free(drag);
975 return -1;
976 }
977
978 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
979 wl_signal_add(&icon->destroy_signal,
980 &drag->base.icon_destroy_listener);
981
982 icon->configure = touch_drag_surface_configure;
983 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300984 weston_surface_set_label_func(icon,
985 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800986 } else {
987 drag->base.icon = NULL;
988 }
989
990 if (source) {
991 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
992 wl_signal_add(&source->destroy_signal,
993 &drag->base.data_source_listener);
994 }
995
Carlos Garnachob2889882016-01-15 21:14:26 +0100996 weston_keyboard_set_focus(keyboard, NULL);
997
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800998 weston_touch_start_grab(touch, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100999 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001000
1001 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -07001002
1003 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001004}
1005
1006static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001007data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
1008 struct wl_resource *source_resource,
1009 struct wl_resource *origin_resource,
1010 struct wl_resource *icon_resource, uint32_t serial)
1011{
1012 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001013 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1014 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001015 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -07001016 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001017 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001018 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001019 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001020
Derek Foreman1281a362015-07-31 16:55:32 -05001021 is_pointer_grab = pointer &&
1022 pointer->button_count == 1 &&
1023 pointer->grab_serial == serial &&
1024 pointer->focus &&
1025 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001026
Derek Foreman1281a362015-07-31 16:55:32 -05001027 is_touch_grab = touch &&
1028 touch->num_tp == 1 &&
1029 touch->grab_serial == serial &&
1030 touch->focus &&
1031 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001032
1033 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001034 return;
1035
1036 /* FIXME: Check that the data source type array isn't empty. */
1037
1038 if (source_resource)
1039 source = wl_resource_get_user_data(source_resource);
1040 if (icon_resource)
1041 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +03001042
1043 if (icon) {
1044 if (weston_surface_set_role(icon, "wl_data_device-icon",
1045 resource,
1046 WL_DATA_DEVICE_ERROR_ROLE) < 0)
1047 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001048 }
1049
Jason Ekstrand8202d722014-06-24 21:19:24 -07001050 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001051 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001052 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001053 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001054
1055 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001056 wl_resource_post_no_memory(resource);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001057 else
1058 source->seat = seat;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001059}
1060
1061static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001062destroy_selection_data_source(struct wl_listener *listener, void *data)
1063{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001064 struct weston_seat *seat = container_of(listener, struct weston_seat,
1065 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05001066 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001067 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +01001068 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001069
1070 seat->selection_data_source = NULL;
1071
Derek Foreman1281a362015-07-31 16:55:32 -05001072 if (keyboard)
1073 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001074 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001075 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01001076 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001077 if (data_device)
1078 wl_data_device_send_selection(data_device, NULL);
1079 }
1080
1081 wl_signal_emit(&seat->selection_signal, seat);
1082}
1083
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001084/** \brief Send the selection to the specified client
1085 *
1086 * This function creates a new wl_data_offer if there is a wl_data_source
1087 * currently set as the selection and sends it to the specified client,
1088 * followed by the wl_data_device.selection() event.
1089 * If there is no current selection the wl_data_device.selection() event
1090 * will carry a NULL wl_data_offer.
1091 *
1092 * If the client does not have a wl_data_device for the specified seat
1093 * nothing will be done.
1094 *
1095 * \param seat The seat owning the wl_data_device used to send the events.
1096 * \param client The client to which to send the selection.
1097 */
1098WL_EXPORT void
1099weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
1100{
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001101 struct weston_data_offer *offer;
1102 struct wl_resource *data_device;
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001103
Giulio Camuffod46bb012015-05-01 12:59:36 +03001104 wl_resource_for_each(data_device, &seat->drag_resource_list) {
1105 if (wl_resource_get_client(data_device) != client)
1106 continue;
1107
1108 if (seat->selection_data_source) {
1109 offer = weston_data_source_send_offer(seat->selection_data_source,
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001110 data_device);
1111 wl_data_device_send_selection(data_device, offer->resource);
Giulio Camuffod46bb012015-05-01 12:59:36 +03001112 } else {
1113 wl_data_device_send_selection(data_device, NULL);
1114 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001115 }
1116}
1117
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001118WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001119weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001120 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001121{
Neil Roberts96d790e2013-09-19 17:32:00 +01001122 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -05001123 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001124
1125 if (seat->selection_data_source &&
1126 seat->selection_serial - serial < UINT32_MAX / 2)
1127 return;
1128
1129 if (seat->selection_data_source) {
1130 seat->selection_data_source->cancel(seat->selection_data_source);
1131 wl_list_remove(&seat->selection_data_source_listener.link);
1132 seat->selection_data_source = NULL;
1133 }
1134
1135 seat->selection_data_source = source;
1136 seat->selection_serial = serial;
1137
Derek Foreman1281a362015-07-31 16:55:32 -05001138 if (keyboard)
1139 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001140 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001141 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001142 }
1143
1144 wl_signal_emit(&seat->selection_signal, seat);
1145
1146 if (source) {
1147 seat->selection_data_source_listener.notify =
1148 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001149 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001150 &seat->selection_data_source_listener);
1151 }
1152}
1153
1154static void
1155data_device_set_selection(struct wl_client *client,
1156 struct wl_resource *resource,
1157 struct wl_resource *source_resource, uint32_t serial)
1158{
Carlos Garnacho9c931792016-01-18 23:52:12 +01001159 struct weston_data_source *source;
1160
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001161 if (!source_resource)
1162 return;
1163
Carlos Garnacho9c931792016-01-18 23:52:12 +01001164 source = wl_resource_get_user_data(source_resource);
1165
1166 if (source->actions_set) {
1167 wl_resource_post_error(source_resource,
1168 WL_DATA_SOURCE_ERROR_INVALID_SOURCE,
1169 "cannot set drag-and-drop source as selection");
1170 return;
1171 }
1172
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001173 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001174 weston_seat_set_selection(wl_resource_get_user_data(resource),
Carlos Garnacho9c931792016-01-18 23:52:12 +01001175 source, serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001176}
kabeer khan3a510d82014-10-20 11:47:15 +05301177static void
1178data_device_release(struct wl_client *client, struct wl_resource *resource)
1179{
1180 wl_resource_destroy(resource);
1181}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001182
1183static const struct wl_data_device_interface data_device_interface = {
1184 data_device_start_drag,
1185 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +05301186 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001187};
1188
1189static void
1190destroy_data_source(struct wl_resource *resource)
1191{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001192 struct weston_data_source *source =
1193 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001194 char **p;
1195
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001196 wl_signal_emit(&source->destroy_signal, source);
1197
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001198 wl_array_for_each(p, &source->mime_types)
1199 free(*p);
1200
1201 wl_array_release(&source->mime_types);
1202
Kristian Høgsberg489b2792013-06-25 11:26:31 -04001203 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001204}
1205
1206static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001207client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001208 uint32_t time, const char *mime_type)
1209{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001210 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001211}
1212
1213static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001214client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001215 const char *mime_type, int32_t fd)
1216{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001217 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001218 close(fd);
1219}
1220
1221static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001222client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001223{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001224 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001225}
1226
1227static void
1228create_data_source(struct wl_client *client,
1229 struct wl_resource *resource, uint32_t id)
1230{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001231 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001232
1233 source = malloc(sizeof *source);
1234 if (source == NULL) {
1235 wl_resource_post_no_memory(resource);
1236 return;
1237 }
1238
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001239 source->resource =
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001240 wl_resource_create(client, &wl_data_source_interface,
1241 wl_resource_get_version(resource), id);
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001242 if (source->resource == NULL) {
1243 free(source);
1244 wl_resource_post_no_memory(resource);
1245 return;
1246 }
1247
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001248 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001249 source->accept = client_source_accept;
1250 source->send = client_source_send;
1251 source->cancel = client_source_cancel;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001252 source->offer = NULL;
1253 source->accepted = false;
1254 source->seat = NULL;
Carlos Garnacho9c931792016-01-18 23:52:12 +01001255 source->actions_set = false;
1256 source->dnd_actions = 0;
1257 source->current_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Carlos Garnachob2889882016-01-15 21:14:26 +01001258 source->compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001259
1260 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001261
Jason Ekstranda85118c2013-06-27 20:17:02 -05001262 wl_resource_set_implementation(source->resource, &data_source_interface,
1263 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001264}
1265
1266static void unbind_data_device(struct wl_resource *resource)
1267{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001268 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001269}
1270
1271static void
1272get_data_device(struct wl_client *client,
1273 struct wl_resource *manager_resource,
1274 uint32_t id, struct wl_resource *seat_resource)
1275{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001276 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001277 struct wl_resource *resource;
1278
Jason Ekstranda85118c2013-06-27 20:17:02 -05001279 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +05301280 &wl_data_device_interface,
1281 wl_resource_get_version(manager_resource),
1282 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001283 if (resource == NULL) {
1284 wl_resource_post_no_memory(manager_resource);
1285 return;
1286 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001287
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001288 wl_list_insert(&seat->drag_resource_list,
1289 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001290 wl_resource_set_implementation(resource, &data_device_interface,
1291 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001292}
1293
1294static const struct wl_data_device_manager_interface manager_interface = {
1295 create_data_source,
1296 get_data_device
1297};
1298
1299static void
1300bind_manager(struct wl_client *client,
1301 void *data, uint32_t version, uint32_t id)
1302{
Jason Ekstranda85118c2013-06-27 20:17:02 -05001303 struct wl_resource *resource;
1304
kabeer khan3a510d82014-10-20 11:47:15 +05301305 resource = wl_resource_create(client,
1306 &wl_data_device_manager_interface,
1307 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001308 if (resource == NULL) {
1309 wl_client_post_no_memory(client);
1310 return;
1311 }
1312
1313 wl_resource_set_implementation(resource, &manager_interface,
1314 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001315}
1316
1317WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001318wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001319{
Neil Roberts96d790e2013-09-19 17:32:00 +01001320 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -05001321 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001322
Derek Foreman1281a362015-07-31 16:55:32 -05001323 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001324 return;
1325
Derek Foreman1281a362015-07-31 16:55:32 -05001326 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001327 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001328 return;
1329
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001330 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001331}
1332
1333WL_EXPORT int
1334wl_data_device_manager_init(struct wl_display *display)
1335{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001336 if (wl_global_create(display,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001337 &wl_data_device_manager_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001338 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001339 return -1;
1340
1341 return 0;
1342}