blob: 2cfdcfe2196c73356ac51653e97b4216206ce6b9 [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 Garnacho9c931792016-01-18 23:52:12 +0100103 if (source->offer->in_ask &&
104 wl_resource_get_version(source->resource) >=
105 WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
106 wl_data_source_send_action(source->resource,
107 source->current_dnd_action);
108 }
109
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100110 if (wl_resource_get_version(source->resource) >=
111 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
112 wl_data_source_send_dnd_finished(source->resource);
113 }
114
115 source->offer = NULL;
116}
117
Carlos Garnacho9c931792016-01-18 23:52:12 +0100118static uint32_t
119data_offer_choose_action(struct weston_data_offer *offer)
120{
121 uint32_t available_actions, preferred_action = 0;
122 uint32_t source_actions, offer_actions;
123
124 if (wl_resource_get_version(offer->resource) >=
125 WL_DATA_OFFER_ACTION_SINCE_VERSION) {
126 offer_actions = offer->dnd_actions;
127 preferred_action = offer->preferred_dnd_action;
128 } else {
129 offer_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
130 }
131
132 if (wl_resource_get_version(offer->source->resource) >=
133 WL_DATA_SOURCE_ACTION_SINCE_VERSION)
134 source_actions = offer->source->dnd_actions;
135 else
136 source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
137
138 available_actions = offer_actions & source_actions;
139
140 if (!available_actions)
141 return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
142
Carlos Garnachob2889882016-01-15 21:14:26 +0100143 if (offer->source->seat &&
144 offer->source->compositor_action & available_actions)
145 return offer->source->compositor_action;
146
Carlos Garnacho9c931792016-01-18 23:52:12 +0100147 /* If the dest side has a preferred DnD action, use it */
148 if ((preferred_action & available_actions) != 0)
149 return preferred_action;
150
151 /* Use the first found action, in bit order */
152 return 1 << (ffs(available_actions) - 1);
153}
154
155static void
156data_offer_update_action(struct weston_data_offer *offer)
157{
158 uint32_t action;
159
160 if (!offer->source)
161 return;
162
163 action = data_offer_choose_action(offer);
164
165 if (offer->source->current_dnd_action == action)
166 return;
167
168 offer->source->current_dnd_action = action;
169
170 if (offer->in_ask)
171 return;
172
173 if (wl_resource_get_version(offer->source->resource) >=
174 WL_DATA_SOURCE_ACTION_SINCE_VERSION)
175 wl_data_source_send_action(offer->source->resource, action);
176
177 if (wl_resource_get_version(offer->resource) >=
178 WL_DATA_OFFER_ACTION_SINCE_VERSION)
179 wl_data_offer_send_action(offer->resource, action);
180}
181
182static void
183data_offer_set_actions(struct wl_client *client,
184 struct wl_resource *resource,
185 uint32_t dnd_actions, uint32_t preferred_action)
186{
187 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
188
189 if (dnd_actions & ~ALL_ACTIONS) {
190 wl_resource_post_error(offer->resource,
191 WL_DATA_OFFER_ERROR_INVALID_ACTION_MASK,
192 "invalid action mask %x", dnd_actions);
193 return;
194 }
195
196 if (preferred_action &&
197 (!(preferred_action & dnd_actions) ||
198 __builtin_popcount(preferred_action) > 1)) {
199 wl_resource_post_error(offer->resource,
200 WL_DATA_OFFER_ERROR_INVALID_ACTION,
201 "invalid action %x", preferred_action);
202 return;
203 }
204
205 offer->dnd_actions = dnd_actions;
206 offer->preferred_dnd_action = preferred_action;
207 data_offer_update_action(offer);
208}
209
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100210static void
211data_offer_finish(struct wl_client *client, struct wl_resource *resource)
212{
213 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
214
215 if (!offer->source || offer->source->offer != offer)
216 return;
217
218 /* Disallow finish while we have a grab driving drag-and-drop, or
219 * if the negotiation is not at the right stage
220 */
221 if (offer->source->seat ||
222 !offer->source->accepted) {
223 wl_resource_post_error(offer->resource,
224 WL_DATA_OFFER_ERROR_INVALID_FINISH,
225 "premature finish request");
226 return;
227 }
228
Carlos Garnacho9c931792016-01-18 23:52:12 +0100229 switch (offer->source->current_dnd_action) {
230 case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE:
231 case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK:
232 wl_resource_post_error(offer->resource,
233 WL_DATA_OFFER_ERROR_INVALID_OFFER,
234 "offer finished with an invalid action");
235 return;
236 default:
237 break;
238 }
239
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100240 data_source_notify_finish(offer->source);
241}
242
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400243static const struct wl_data_offer_interface data_offer_interface = {
244 data_offer_accept,
245 data_offer_receive,
246 data_offer_destroy,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100247 data_offer_finish,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100248 data_offer_set_actions,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400249};
250
251static void
252destroy_data_offer(struct wl_resource *resource)
253{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700254 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400255
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100256 if (!offer->source)
257 goto out;
258
259 wl_list_remove(&offer->source_destroy_listener.link);
260
261 if (offer->source->offer != offer)
262 goto out;
263
264 /* If the drag destination has version < 3, wl_data_offer.finish
265 * won't be called, so do this here as a safety net, because
266 * we still want the version >=3 drag source to be happy.
267 */
268 if (wl_resource_get_version(offer->resource) <
269 WL_DATA_OFFER_ACTION_SINCE_VERSION) {
270 data_source_notify_finish(offer->source);
271 } else if (wl_resource_get_version(offer->source->resource) >=
272 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
273 wl_data_source_send_cancelled(offer->source->resource);
274 }
275
276 offer->source->offer = NULL;
277out:
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400278 free(offer);
279}
280
281static void
282destroy_offer_data_source(struct wl_listener *listener, void *data)
283{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700284 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400285
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700286 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400287 source_destroy_listener);
288
289 offer->source = NULL;
290}
291
292static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700293weston_data_source_send_offer(struct weston_data_source *source,
294 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400295{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700296 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400297 char **p;
298
299 offer = malloc(sizeof *offer);
300 if (offer == NULL)
301 return NULL;
302
Jason Ekstranda85118c2013-06-27 20:17:02 -0500303 offer->resource =
304 wl_resource_create(wl_resource_get_client(target),
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100305 &wl_data_offer_interface,
306 wl_resource_get_version(target), 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700307 if (offer->resource == NULL) {
308 free(offer);
309 return NULL;
310 }
311
Jason Ekstranda85118c2013-06-27 20:17:02 -0500312 wl_resource_set_implementation(offer->resource, &data_offer_interface,
313 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400314
Carlos Garnacho9c931792016-01-18 23:52:12 +0100315 offer->in_ask = false;
316 offer->dnd_actions = 0;
317 offer->preferred_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400318 offer->source = source;
319 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500320 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321 &offer->source_destroy_listener);
322
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500323 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400324
325 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500326 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100328 source->offer = offer;
329 source->accepted = false;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100330 data_offer_update_action(offer);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100331
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500332 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400333}
334
335static void
336data_source_offer(struct wl_client *client,
337 struct wl_resource *resource,
338 const char *type)
339{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700340 struct weston_data_source *source =
341 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400342 char **p;
343
344 p = wl_array_add(&source->mime_types, sizeof *p);
345 if (p)
346 *p = strdup(type);
347 if (!p || !*p)
348 wl_resource_post_no_memory(resource);
349}
350
351static void
352data_source_destroy(struct wl_client *client, struct wl_resource *resource)
353{
354 wl_resource_destroy(resource);
355}
356
Carlos Garnacho9c931792016-01-18 23:52:12 +0100357static void
358data_source_set_actions(struct wl_client *client,
359 struct wl_resource *resource,
360 uint32_t dnd_actions)
361{
362 struct weston_data_source *source =
363 wl_resource_get_user_data(resource);
364
365 if (source->actions_set) {
366 wl_resource_post_error(source->resource,
367 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
368 "cannot set actions more than once");
369 return;
370 }
371
372 if (dnd_actions & ~ALL_ACTIONS) {
373 wl_resource_post_error(source->resource,
374 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
375 "invalid action mask %x", dnd_actions);
376 return;
377 }
378
379 if (source->seat) {
380 wl_resource_post_error(source->resource,
381 WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
382 "invalid action change after "
383 "wl_data_device.start_drag");
384 return;
385 }
386
387 source->dnd_actions = dnd_actions;
388 source->actions_set = true;
389}
390
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400391static struct wl_data_source_interface data_source_interface = {
392 data_source_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100393 data_source_destroy,
394 data_source_set_actions
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400395};
396
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400397static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800398drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100399 struct weston_pointer *pointer,
400 struct weston_touch *touch,
401 struct weston_surface *es,
402 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400403{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300404 struct weston_layer_entry *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400405 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400406
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800407 assert((pointer != NULL && touch == NULL) ||
408 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500409
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400410 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800411 if (pointer && pointer->sprite &&
412 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400413 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400414 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500415 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400416
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300417 weston_layer_entry_remove(&drag->icon->layer_link);
418 weston_layer_entry_insert(list, &drag->icon->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500419 weston_view_update_transform(drag->icon);
Jason Ekstrandef540082014-06-26 10:37:36 -0700420 pixman_region32_clear(&es->pending.input);
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400421 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400422
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400423 drag->dx += sx;
424 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400425
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800426 /* init to 0 for avoiding a compile warning */
427 fx = fy = 0;
428 if (pointer) {
429 fx = wl_fixed_to_double(pointer->x) + drag->dx;
430 fy = wl_fixed_to_double(pointer->y) + drag->dy;
431 } else if (touch) {
432 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
433 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
434 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600435 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400436}
437
Pekka Paalanen8274d902014-08-06 19:36:51 +0300438static int
439pointer_drag_surface_get_label(struct weston_surface *surface,
440 char *buf, size_t len)
441{
442 return snprintf(buf, len, "pointer drag icon");
443}
444
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400445static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100446pointer_drag_surface_configure(struct weston_surface *es,
447 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800448{
449 struct weston_pointer_drag *drag = es->configure_private;
450 struct weston_pointer *pointer = drag->grab.pointer;
451
452 assert(es->configure == pointer_drag_surface_configure);
453
Jonas Ådahl767d8912013-12-03 22:30:17 +0100454 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800455}
456
Pekka Paalanen8274d902014-08-06 19:36:51 +0300457static int
458touch_drag_surface_get_label(struct weston_surface *surface,
459 char *buf, size_t len)
460{
461 return snprintf(buf, len, "touch drag icon");
462}
463
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800464static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100465touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800466{
467 struct weston_touch_drag *drag = es->configure_private;
468 struct weston_touch *touch = drag->grab.touch;
469
470 assert(es->configure == touch_drag_surface_configure);
471
Jonas Ådahl767d8912013-12-03 22:30:17 +0100472 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800473}
474
475static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400476destroy_drag_focus(struct wl_listener *listener, void *data)
477{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400478 struct weston_drag *drag =
479 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400480
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400481 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400482}
483
484static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800485weston_drag_set_focus(struct weston_drag *drag,
486 struct weston_seat *seat,
487 struct weston_view *view,
488 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400489{
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100490 struct wl_resource *resource, *offer_resource = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800491 struct wl_display *display = seat->compositor->wl_display;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100492 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493 uint32_t serial;
494
Jason Ekstranda7af7042013-10-12 22:38:11 -0500495 if (drag->focus && view && drag->focus->surface == view->surface) {
496 drag->focus = view;
497 return;
498 }
499
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400500 if (drag->focus_resource) {
501 wl_data_device_send_leave(drag->focus_resource);
502 wl_list_remove(&drag->focus_listener.link);
503 drag->focus_resource = NULL;
504 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400505 }
506
Jason Ekstranda7af7042013-10-12 22:38:11 -0500507 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400508 return;
509
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500510 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500511 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400512 return;
513
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100514 if (drag->data_source &&
515 drag->data_source->offer) {
516 /* Unlink the offer from the source */
517 offer = drag->data_source->offer;
518 offer->source = NULL;
519 drag->data_source->offer = NULL;
520 wl_list_remove(&offer->source_destroy_listener.link);
521 }
522
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800523 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400525 if (!resource)
526 return;
527
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400528 serial = wl_display_next_serial(display);
529
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700530 if (drag->data_source) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100531 drag->data_source->accepted = false;
532 offer_resource = weston_data_source_send_offer(drag->data_source,
533 resource);
534 if (offer_resource == NULL)
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700535 return;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100536
537 if (wl_resource_get_version (offer_resource) >=
538 WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
539 wl_data_offer_send_source_actions (offer_resource,
540 drag->data_source->dnd_actions);
541 }
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700542 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400543
Jason Ekstranda7af7042013-10-12 22:38:11 -0500544 wl_data_device_send_enter(resource, serial, view->surface->resource,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100545 sx, sy, offer_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546
Jason Ekstranda7af7042013-10-12 22:38:11 -0500547 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400548 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500549 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400550 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400551}
552
553static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400554drag_grab_focus(struct weston_pointer_grab *grab)
555{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800556 struct weston_pointer_drag *drag =
557 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400558 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500559 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400560 wl_fixed_t sx, sy;
561
Jason Ekstranda7af7042013-10-12 22:38:11 -0500562 view = weston_compositor_pick_view(pointer->seat->compositor,
563 pointer->x, pointer->y,
564 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800565 if (drag->base.focus != view)
566 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400567}
568
569static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100570drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200571 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400572{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800573 struct weston_pointer_drag *drag =
574 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400575 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400576 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400577 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400578
Jonas Ådahld2510102014-10-05 21:39:14 +0200579 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100580
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800581 if (drag->base.icon) {
582 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
583 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
584 weston_view_set_position(drag->base.icon, fx, fy);
585 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400586 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400587
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800588 if (drag->base.focus_resource) {
589 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500590 pointer->x, pointer->y,
591 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400592
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800593 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400594 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400595}
596
597static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800598data_device_end_drag_grab(struct weston_drag *drag,
599 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400600{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400601 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500602 if (weston_view_is_mapped(drag->icon))
603 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400604
Jason Ekstranda7af7042013-10-12 22:38:11 -0500605 drag->icon->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300606 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700607 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400608 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500609 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400610 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400611
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800612 weston_drag_set_focus(drag, seat, NULL, 0, 0);
613}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400614
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800615static void
616data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
617{
618 struct weston_pointer *pointer = drag->grab.pointer;
Carlos Garnachob2889882016-01-15 21:14:26 +0100619 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400620
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800621 data_device_end_drag_grab(&drag->base, pointer->seat);
622 weston_pointer_end_grab(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100623 weston_keyboard_end_grab(keyboard);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400624 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400625}
626
627static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400628drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400629 uint32_t time, uint32_t button, uint32_t state_w)
630{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800631 struct weston_pointer_drag *drag =
632 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400633 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400634 enum wl_pointer_button_state state = state_w;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100635 struct weston_data_source *data_source = drag->base.data_source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400636
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100637 if (data_source &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400638 pointer->grab_button == button &&
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100639 state == WL_POINTER_BUTTON_STATE_RELEASED) {
640 if (drag->base.focus_resource &&
Carlos Garnacho9c931792016-01-18 23:52:12 +0100641 data_source->accepted &&
642 data_source->current_dnd_action) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100643 wl_data_device_send_drop(drag->base.focus_resource);
644
645 if (wl_resource_get_version(data_source->resource) >=
646 WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION)
647 wl_data_source_send_dnd_drop_performed(data_source->resource);
648
Carlos Garnacho9c931792016-01-18 23:52:12 +0100649 data_source->offer->in_ask =
650 data_source->current_dnd_action ==
651 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
652
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100653 data_source->seat = NULL;
654 } else if (wl_resource_get_version(data_source->resource) >=
655 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
656 wl_data_source_send_cancelled(data_source->resource);
657 }
658 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400659
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400660 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400661 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800662 if (drag->base.data_source)
663 wl_list_remove(&drag->base.data_source_listener.link);
664 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400665 }
666}
667
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200668static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200669drag_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000670 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200671{
672}
673
674static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000675drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
676{
677}
678
679static void
680drag_grab_frame(struct weston_pointer_grab *grab)
681{
682}
683
684static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200685drag_grab_cancel(struct weston_pointer_grab *grab)
686{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800687 struct weston_pointer_drag *drag =
688 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200689
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800690 if (drag->base.data_source)
691 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200692
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800693 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200694}
695
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800696static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400697 drag_grab_focus,
698 drag_grab_motion,
699 drag_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200700 drag_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000701 drag_grab_axis_source,
702 drag_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200703 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400704};
705
706static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800707drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
708 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400709{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800710}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400711
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800712static void
713data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
714{
715 struct weston_touch *touch = drag->grab.touch;
Carlos Garnachob2889882016-01-15 21:14:26 +0100716 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800717
718 data_device_end_drag_grab(&drag->base, touch->seat);
719 weston_touch_end_grab(touch);
Carlos Garnachob2889882016-01-15 21:14:26 +0100720 weston_keyboard_end_grab(keyboard);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800721 free(drag);
722}
723
724static void
725drag_grab_touch_up(struct weston_touch_grab *grab,
726 uint32_t time, int touch_id)
727{
728 struct weston_touch_drag *touch_drag =
729 container_of(grab, struct weston_touch_drag, grab);
730 struct weston_touch *touch = grab->touch;
731
732 if (touch_id != touch->grab_touch_id)
733 return;
734
735 if (touch_drag->base.focus_resource)
736 wl_data_device_send_drop(touch_drag->base.focus_resource);
737 if (touch_drag->base.data_source)
738 wl_list_remove(&touch_drag->base.data_source_listener.link);
739 data_device_end_touch_drag_grab(touch_drag);
740}
741
742static void
743drag_grab_touch_focus(struct weston_touch_drag *drag)
744{
745 struct weston_touch *touch = drag->grab.touch;
746 struct weston_view *view;
747 wl_fixed_t view_x, view_y;
748
749 view = weston_compositor_pick_view(touch->seat->compositor,
750 touch->grab_x, touch->grab_y,
751 &view_x, &view_y);
752 if (drag->base.focus != view)
753 weston_drag_set_focus(&drag->base, touch->seat,
754 view, view_x, view_y);
755}
756
757static void
758drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300759 int touch_id, wl_fixed_t x, wl_fixed_t y)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800760{
761 struct weston_touch_drag *touch_drag =
762 container_of(grab, struct weston_touch_drag, grab);
763 struct weston_touch *touch = grab->touch;
764 wl_fixed_t view_x, view_y;
765 float fx, fy;
766
767 if (touch_id != touch->grab_touch_id)
768 return;
769
770 drag_grab_touch_focus(touch_drag);
771 if (touch_drag->base.icon) {
772 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
773 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
774 weston_view_set_position(touch_drag->base.icon, fx, fy);
775 weston_view_schedule_repaint(touch_drag->base.icon);
776 }
777
778 if (touch_drag->base.focus_resource) {
779 weston_view_from_global_fixed(touch_drag->base.focus,
780 touch->grab_x, touch->grab_y,
781 &view_x, &view_y);
782 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
783 view_x, view_y);
784 }
785}
786
787static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200788drag_grab_touch_frame(struct weston_touch_grab *grab)
789{
790}
791
792static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800793drag_grab_touch_cancel(struct weston_touch_grab *grab)
794{
795 struct weston_touch_drag *touch_drag =
796 container_of(grab, struct weston_touch_drag, grab);
797
798 if (touch_drag->base.data_source)
799 wl_list_remove(&touch_drag->base.data_source_listener.link);
800 data_device_end_touch_drag_grab(touch_drag);
801}
802
803static const struct weston_touch_grab_interface touch_drag_grab_interface = {
804 drag_grab_touch_down,
805 drag_grab_touch_up,
806 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200807 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800808 drag_grab_touch_cancel
809};
810
811static void
Carlos Garnachob2889882016-01-15 21:14:26 +0100812drag_grab_keyboard_key(struct weston_keyboard_grab *grab,
813 uint32_t time, uint32_t key, uint32_t state)
814{
815}
816
817static void
818drag_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
819 uint32_t serial, uint32_t mods_depressed,
820 uint32_t mods_latched,
821 uint32_t mods_locked, uint32_t group)
822{
823 struct weston_keyboard *keyboard = grab->keyboard;
824 struct weston_drag *drag =
825 container_of(grab, struct weston_drag, keyboard_grab);
826 uint32_t compositor_action;
827
828 if (mods_depressed & (1 << keyboard->xkb_info->shift_mod))
829 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
830 else if (mods_depressed & (1 << keyboard->xkb_info->ctrl_mod))
831 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
832 else
833 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
834
835 drag->data_source->compositor_action = compositor_action;
836
837 if (drag->data_source->offer)
838 data_offer_update_action(drag->data_source->offer);
839}
840
841static void
842drag_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
843{
844 struct weston_drag *drag =
845 container_of(grab, struct weston_drag, keyboard_grab);
846 struct weston_pointer *pointer = grab->keyboard->seat->pointer_state;
847 struct weston_touch *touch = grab->keyboard->seat->touch_state;
848
849 if (pointer && pointer->grab->interface == &pointer_drag_grab_interface) {
850 struct weston_touch_drag *touch_drag =
851 (struct weston_touch_drag *) drag;
852 drag_grab_touch_cancel(&touch_drag->grab);
853 } else if (touch && touch->grab->interface == &touch_drag_grab_interface) {
854 struct weston_pointer_drag *pointer_drag =
855 (struct weston_pointer_drag *) drag;
856 drag_grab_cancel(&pointer_drag->grab);
857 }
858}
859
860static const struct weston_keyboard_grab_interface keyboard_drag_grab_interface = {
861 drag_grab_keyboard_key,
862 drag_grab_keyboard_modifiers,
863 drag_grab_keyboard_cancel
864};
865
866static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800867destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
868{
869 struct weston_pointer_drag *drag = container_of(listener,
870 struct weston_pointer_drag, base.data_source_listener);
871
872 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400873}
874
875static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400876handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400877{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400878 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400879 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400880
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400881 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400882}
883
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700884WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800885weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700886 struct weston_data_source *source,
887 struct weston_surface *icon,
888 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400889{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800890 struct weston_pointer_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100891 struct weston_keyboard *keyboard =
892 weston_seat_get_keyboard(pointer->seat);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400893
Peter Huttererf3d62272013-08-08 11:57:05 +1000894 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700895 if (drag == NULL)
896 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400897
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800898 drag->grab.interface = &pointer_drag_grab_interface;
Carlos Garnachob2889882016-01-15 21:14:26 +0100899 drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800900 drag->base.client = client;
901 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400902
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400903 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800904 drag->base.icon = weston_view_create(icon);
905 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 free(drag);
907 return -1;
908 }
909
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800910 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500911 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800912 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400913
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800914 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400915 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300916 weston_surface_set_label_func(icon,
917 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800919 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500920 }
921
922 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800923 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800925 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400926 }
927
Derek Foremanf9318d12015-05-11 15:40:11 -0500928 weston_pointer_clear_focus(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100929 weston_keyboard_set_focus(keyboard, NULL);
930
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800931 weston_pointer_start_grab(pointer, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100932 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800933
934 return 0;
935}
936
937static void
938destroy_touch_data_device_source(struct wl_listener *listener, void *data)
939{
940 struct weston_touch_drag *drag = container_of(listener,
941 struct weston_touch_drag, base.data_source_listener);
942
943 data_device_end_touch_drag_grab(drag);
944}
945
946WL_EXPORT int
947weston_touch_start_drag(struct weston_touch *touch,
948 struct weston_data_source *source,
949 struct weston_surface *icon,
950 struct wl_client *client)
951{
952 struct weston_touch_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100953 struct weston_keyboard *keyboard =
954 weston_seat_get_keyboard(touch->seat);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800955
956 drag = zalloc(sizeof *drag);
957 if (drag == NULL)
958 return -1;
959
960 drag->grab.interface = &touch_drag_grab_interface;
961 drag->base.client = client;
962 drag->base.data_source = source;
963
964 if (icon) {
965 drag->base.icon = weston_view_create(icon);
966 if (drag->base.icon == NULL) {
967 free(drag);
968 return -1;
969 }
970
971 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
972 wl_signal_add(&icon->destroy_signal,
973 &drag->base.icon_destroy_listener);
974
975 icon->configure = touch_drag_surface_configure;
976 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300977 weston_surface_set_label_func(icon,
978 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800979 } else {
980 drag->base.icon = NULL;
981 }
982
983 if (source) {
984 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
985 wl_signal_add(&source->destroy_signal,
986 &drag->base.data_source_listener);
987 }
988
Carlos Garnachob2889882016-01-15 21:14:26 +0100989 weston_keyboard_set_focus(keyboard, NULL);
990
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800991 weston_touch_start_grab(touch, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100992 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800993
994 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700995
996 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400997}
998
999static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001000data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
1001 struct wl_resource *source_resource,
1002 struct wl_resource *origin_resource,
1003 struct wl_resource *icon_resource, uint32_t serial)
1004{
1005 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001006 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1007 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001008 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -07001009 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001010 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001011 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001012 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001013
Derek Foreman1281a362015-07-31 16:55:32 -05001014 is_pointer_grab = pointer &&
1015 pointer->button_count == 1 &&
1016 pointer->grab_serial == serial &&
1017 pointer->focus &&
1018 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001019
Derek Foreman1281a362015-07-31 16:55:32 -05001020 is_touch_grab = touch &&
1021 touch->num_tp == 1 &&
1022 touch->grab_serial == serial &&
1023 touch->focus &&
1024 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001025
1026 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001027 return;
1028
1029 /* FIXME: Check that the data source type array isn't empty. */
1030
1031 if (source_resource)
1032 source = wl_resource_get_user_data(source_resource);
1033 if (icon_resource)
1034 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +03001035
1036 if (icon) {
1037 if (weston_surface_set_role(icon, "wl_data_device-icon",
1038 resource,
1039 WL_DATA_DEVICE_ERROR_ROLE) < 0)
1040 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001041 }
1042
Jason Ekstrand8202d722014-06-24 21:19:24 -07001043 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001044 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001045 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001046 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001047
1048 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001049 wl_resource_post_no_memory(resource);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001050 else
1051 source->seat = seat;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001052}
1053
1054static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001055destroy_selection_data_source(struct wl_listener *listener, void *data)
1056{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001057 struct weston_seat *seat = container_of(listener, struct weston_seat,
1058 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05001059 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001060 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +01001061 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001062
1063 seat->selection_data_source = NULL;
1064
Derek Foreman1281a362015-07-31 16:55:32 -05001065 if (keyboard)
1066 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001067 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001068 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01001069 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001070 if (data_device)
1071 wl_data_device_send_selection(data_device, NULL);
1072 }
1073
1074 wl_signal_emit(&seat->selection_signal, seat);
1075}
1076
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001077/** \brief Send the selection to the specified client
1078 *
1079 * This function creates a new wl_data_offer if there is a wl_data_source
1080 * currently set as the selection and sends it to the specified client,
1081 * followed by the wl_data_device.selection() event.
1082 * If there is no current selection the wl_data_device.selection() event
1083 * will carry a NULL wl_data_offer.
1084 *
1085 * If the client does not have a wl_data_device for the specified seat
1086 * nothing will be done.
1087 *
1088 * \param seat The seat owning the wl_data_device used to send the events.
1089 * \param client The client to which to send the selection.
1090 */
1091WL_EXPORT void
1092weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
1093{
1094 struct wl_resource *data_device, *offer;
1095
Giulio Camuffod46bb012015-05-01 12:59:36 +03001096 wl_resource_for_each(data_device, &seat->drag_resource_list) {
1097 if (wl_resource_get_client(data_device) != client)
1098 continue;
1099
1100 if (seat->selection_data_source) {
1101 offer = weston_data_source_send_offer(seat->selection_data_source,
1102 data_device);
1103 wl_data_device_send_selection(data_device, offer);
1104 } else {
1105 wl_data_device_send_selection(data_device, NULL);
1106 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001107 }
1108}
1109
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001110WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001111weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001112 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001113{
Neil Roberts96d790e2013-09-19 17:32:00 +01001114 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -05001115 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001116
1117 if (seat->selection_data_source &&
1118 seat->selection_serial - serial < UINT32_MAX / 2)
1119 return;
1120
1121 if (seat->selection_data_source) {
1122 seat->selection_data_source->cancel(seat->selection_data_source);
1123 wl_list_remove(&seat->selection_data_source_listener.link);
1124 seat->selection_data_source = NULL;
1125 }
1126
1127 seat->selection_data_source = source;
1128 seat->selection_serial = serial;
1129
Derek Foreman1281a362015-07-31 16:55:32 -05001130 if (keyboard)
1131 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001132 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001133 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001134 }
1135
1136 wl_signal_emit(&seat->selection_signal, seat);
1137
1138 if (source) {
1139 seat->selection_data_source_listener.notify =
1140 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001141 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001142 &seat->selection_data_source_listener);
1143 }
1144}
1145
1146static void
1147data_device_set_selection(struct wl_client *client,
1148 struct wl_resource *resource,
1149 struct wl_resource *source_resource, uint32_t serial)
1150{
Carlos Garnacho9c931792016-01-18 23:52:12 +01001151 struct weston_data_source *source;
1152
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001153 if (!source_resource)
1154 return;
1155
Carlos Garnacho9c931792016-01-18 23:52:12 +01001156 source = wl_resource_get_user_data(source_resource);
1157
1158 if (source->actions_set) {
1159 wl_resource_post_error(source_resource,
1160 WL_DATA_SOURCE_ERROR_INVALID_SOURCE,
1161 "cannot set drag-and-drop source as selection");
1162 return;
1163 }
1164
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001165 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001166 weston_seat_set_selection(wl_resource_get_user_data(resource),
Carlos Garnacho9c931792016-01-18 23:52:12 +01001167 source, serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001168}
kabeer khan3a510d82014-10-20 11:47:15 +05301169static void
1170data_device_release(struct wl_client *client, struct wl_resource *resource)
1171{
1172 wl_resource_destroy(resource);
1173}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001174
1175static const struct wl_data_device_interface data_device_interface = {
1176 data_device_start_drag,
1177 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +05301178 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001179};
1180
1181static void
1182destroy_data_source(struct wl_resource *resource)
1183{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001184 struct weston_data_source *source =
1185 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001186 char **p;
1187
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001188 wl_signal_emit(&source->destroy_signal, source);
1189
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001190 wl_array_for_each(p, &source->mime_types)
1191 free(*p);
1192
1193 wl_array_release(&source->mime_types);
1194
Kristian Høgsberg489b2792013-06-25 11:26:31 -04001195 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196}
1197
1198static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001199client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001200 uint32_t time, const char *mime_type)
1201{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001202 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001203}
1204
1205static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001206client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001207 const char *mime_type, int32_t fd)
1208{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001209 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001210 close(fd);
1211}
1212
1213static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001214client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001215{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001216 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001217}
1218
1219static void
1220create_data_source(struct wl_client *client,
1221 struct wl_resource *resource, uint32_t id)
1222{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001223 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001224
1225 source = malloc(sizeof *source);
1226 if (source == NULL) {
1227 wl_resource_post_no_memory(resource);
1228 return;
1229 }
1230
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001231 source->resource =
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001232 wl_resource_create(client, &wl_data_source_interface,
1233 wl_resource_get_version(resource), id);
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001234 if (source->resource == NULL) {
1235 free(source);
1236 wl_resource_post_no_memory(resource);
1237 return;
1238 }
1239
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001240 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001241 source->accept = client_source_accept;
1242 source->send = client_source_send;
1243 source->cancel = client_source_cancel;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001244 source->offer = NULL;
1245 source->accepted = false;
1246 source->seat = NULL;
Carlos Garnacho9c931792016-01-18 23:52:12 +01001247 source->actions_set = false;
1248 source->dnd_actions = 0;
1249 source->current_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Carlos Garnachob2889882016-01-15 21:14:26 +01001250 source->compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001251
1252 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001253
Jason Ekstranda85118c2013-06-27 20:17:02 -05001254 wl_resource_set_implementation(source->resource, &data_source_interface,
1255 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001256}
1257
1258static void unbind_data_device(struct wl_resource *resource)
1259{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001260 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001261}
1262
1263static void
1264get_data_device(struct wl_client *client,
1265 struct wl_resource *manager_resource,
1266 uint32_t id, struct wl_resource *seat_resource)
1267{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001268 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001269 struct wl_resource *resource;
1270
Jason Ekstranda85118c2013-06-27 20:17:02 -05001271 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +05301272 &wl_data_device_interface,
1273 wl_resource_get_version(manager_resource),
1274 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001275 if (resource == NULL) {
1276 wl_resource_post_no_memory(manager_resource);
1277 return;
1278 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001279
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001280 wl_list_insert(&seat->drag_resource_list,
1281 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001282 wl_resource_set_implementation(resource, &data_device_interface,
1283 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001284}
1285
1286static const struct wl_data_device_manager_interface manager_interface = {
1287 create_data_source,
1288 get_data_device
1289};
1290
1291static void
1292bind_manager(struct wl_client *client,
1293 void *data, uint32_t version, uint32_t id)
1294{
Jason Ekstranda85118c2013-06-27 20:17:02 -05001295 struct wl_resource *resource;
1296
kabeer khan3a510d82014-10-20 11:47:15 +05301297 resource = wl_resource_create(client,
1298 &wl_data_device_manager_interface,
1299 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001300 if (resource == NULL) {
1301 wl_client_post_no_memory(client);
1302 return;
1303 }
1304
1305 wl_resource_set_implementation(resource, &manager_interface,
1306 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001307}
1308
1309WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001310wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001311{
Neil Roberts96d790e2013-09-19 17:32:00 +01001312 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -05001313 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001314
Derek Foreman1281a362015-07-31 16:55:32 -05001315 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001316 return;
1317
Derek Foreman1281a362015-07-31 16:55:32 -05001318 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001319 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001320 return;
1321
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001322 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001323}
1324
1325WL_EXPORT int
1326wl_data_device_manager_init(struct wl_display *display)
1327{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001328 if (wl_global_create(display,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001329 &wl_data_device_manager_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001330 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001331 return -1;
1332
1333 return 0;
1334}