blob: f04f0308852388b48b407b2744c21d053024774f [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);
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400424 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400425
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400426 drag->dx += sx;
427 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400428
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800429 /* init to 0 for avoiding a compile warning */
430 fx = fy = 0;
431 if (pointer) {
432 fx = wl_fixed_to_double(pointer->x) + drag->dx;
433 fy = wl_fixed_to_double(pointer->y) + drag->dy;
434 } else if (touch) {
435 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
436 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
437 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600438 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400439}
440
Pekka Paalanen8274d902014-08-06 19:36:51 +0300441static int
442pointer_drag_surface_get_label(struct weston_surface *surface,
443 char *buf, size_t len)
444{
445 return snprintf(buf, len, "pointer drag icon");
446}
447
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400448static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100449pointer_drag_surface_configure(struct weston_surface *es,
450 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800451{
452 struct weston_pointer_drag *drag = es->configure_private;
453 struct weston_pointer *pointer = drag->grab.pointer;
454
455 assert(es->configure == pointer_drag_surface_configure);
456
Jonas Ådahl767d8912013-12-03 22:30:17 +0100457 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800458}
459
Pekka Paalanen8274d902014-08-06 19:36:51 +0300460static int
461touch_drag_surface_get_label(struct weston_surface *surface,
462 char *buf, size_t len)
463{
464 return snprintf(buf, len, "touch drag icon");
465}
466
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800467static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100468touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800469{
470 struct weston_touch_drag *drag = es->configure_private;
471 struct weston_touch *touch = drag->grab.touch;
472
473 assert(es->configure == touch_drag_surface_configure);
474
Jonas Ådahl767d8912013-12-03 22:30:17 +0100475 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800476}
477
478static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400479destroy_drag_focus(struct wl_listener *listener, void *data)
480{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400481 struct weston_drag *drag =
482 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400484 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400485}
486
487static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800488weston_drag_set_focus(struct weston_drag *drag,
489 struct weston_seat *seat,
490 struct weston_view *view,
491 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400492{
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100493 struct wl_resource *resource, *offer_resource = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800494 struct wl_display *display = seat->compositor->wl_display;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100495 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400496 uint32_t serial;
497
Jason Ekstranda7af7042013-10-12 22:38:11 -0500498 if (drag->focus && view && drag->focus->surface == view->surface) {
499 drag->focus = view;
500 return;
501 }
502
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400503 if (drag->focus_resource) {
504 wl_data_device_send_leave(drag->focus_resource);
505 wl_list_remove(&drag->focus_listener.link);
506 drag->focus_resource = NULL;
507 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400508 }
509
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400511 return;
512
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500513 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400515 return;
516
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100517 if (drag->data_source &&
518 drag->data_source->offer) {
519 /* Unlink the offer from the source */
520 offer = drag->data_source->offer;
521 offer->source = NULL;
522 drag->data_source->offer = NULL;
523 wl_list_remove(&offer->source_destroy_listener.link);
524 }
525
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800526 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500527 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400528 if (!resource)
529 return;
530
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400531 serial = wl_display_next_serial(display);
532
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700533 if (drag->data_source) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100534 drag->data_source->accepted = false;
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800535 offer = weston_data_source_send_offer(drag->data_source, resource);
536 if (offer == NULL)
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700537 return;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100538
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +0800539 data_offer_update_action(offer);
540
541 offer_resource = offer->resource;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100542 if (wl_resource_get_version (offer_resource) >=
543 WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
544 wl_data_offer_send_source_actions (offer_resource,
545 drag->data_source->dnd_actions);
546 }
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700547 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400548
Jason Ekstranda7af7042013-10-12 22:38:11 -0500549 wl_data_device_send_enter(resource, serial, view->surface->resource,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100550 sx, sy, offer_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400551
Jason Ekstranda7af7042013-10-12 22:38:11 -0500552 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400553 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500554 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400555 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400556}
557
558static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400559drag_grab_focus(struct weston_pointer_grab *grab)
560{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800561 struct weston_pointer_drag *drag =
562 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400563 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500564 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400565 wl_fixed_t sx, sy;
566
Jason Ekstranda7af7042013-10-12 22:38:11 -0500567 view = weston_compositor_pick_view(pointer->seat->compositor,
568 pointer->x, pointer->y,
569 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800570 if (drag->base.focus != view)
571 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400572}
573
574static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100575drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200576 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400577{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800578 struct weston_pointer_drag *drag =
579 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400580 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400581 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400582 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400583
Jonas Ådahld2510102014-10-05 21:39:14 +0200584 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100585
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800586 if (drag->base.icon) {
587 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
588 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
589 weston_view_set_position(drag->base.icon, fx, fy);
590 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400591 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400592
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800593 if (drag->base.focus_resource) {
594 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500595 pointer->x, pointer->y,
596 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400597
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800598 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400599 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400600}
601
602static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800603data_device_end_drag_grab(struct weston_drag *drag,
604 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400605{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400606 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500607 if (weston_view_is_mapped(drag->icon))
608 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400609
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 drag->icon->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300611 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700612 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400613 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500614 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400615 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400616
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800617 weston_drag_set_focus(drag, seat, NULL, 0, 0);
618}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400619
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800620static void
621data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
622{
623 struct weston_pointer *pointer = drag->grab.pointer;
Carlos Garnachob2889882016-01-15 21:14:26 +0100624 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400625
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800626 data_device_end_drag_grab(&drag->base, pointer->seat);
627 weston_pointer_end_grab(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100628 weston_keyboard_end_grab(keyboard);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400629 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400630}
631
632static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400633drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400634 uint32_t time, uint32_t button, uint32_t state_w)
635{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800636 struct weston_pointer_drag *drag =
637 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400638 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400639 enum wl_pointer_button_state state = state_w;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100640 struct weston_data_source *data_source = drag->base.data_source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400641
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100642 if (data_source &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400643 pointer->grab_button == button &&
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100644 state == WL_POINTER_BUTTON_STATE_RELEASED) {
645 if (drag->base.focus_resource &&
Carlos Garnacho9c931792016-01-18 23:52:12 +0100646 data_source->accepted &&
647 data_source->current_dnd_action) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100648 wl_data_device_send_drop(drag->base.focus_resource);
649
650 if (wl_resource_get_version(data_source->resource) >=
651 WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION)
652 wl_data_source_send_dnd_drop_performed(data_source->resource);
653
Carlos Garnacho9c931792016-01-18 23:52:12 +0100654 data_source->offer->in_ask =
655 data_source->current_dnd_action ==
656 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
657
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100658 data_source->seat = NULL;
659 } else if (wl_resource_get_version(data_source->resource) >=
660 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
661 wl_data_source_send_cancelled(data_source->resource);
662 }
663 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400664
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400665 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400666 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800667 if (drag->base.data_source)
668 wl_list_remove(&drag->base.data_source_listener.link);
669 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400670 }
671}
672
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200673static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200674drag_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000675 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200676{
677}
678
679static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000680drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
681{
682}
683
684static void
685drag_grab_frame(struct weston_pointer_grab *grab)
686{
687}
688
689static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200690drag_grab_cancel(struct weston_pointer_grab *grab)
691{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800692 struct weston_pointer_drag *drag =
693 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200694
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800695 if (drag->base.data_source)
696 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200697
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800698 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200699}
700
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800701static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400702 drag_grab_focus,
703 drag_grab_motion,
704 drag_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200705 drag_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000706 drag_grab_axis_source,
707 drag_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200708 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400709};
710
711static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800712drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
713 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400714{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800715}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400716
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800717static void
718data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
719{
720 struct weston_touch *touch = drag->grab.touch;
Carlos Garnachob2889882016-01-15 21:14:26 +0100721 struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800722
723 data_device_end_drag_grab(&drag->base, touch->seat);
724 weston_touch_end_grab(touch);
Carlos Garnachob2889882016-01-15 21:14:26 +0100725 weston_keyboard_end_grab(keyboard);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800726 free(drag);
727}
728
729static void
730drag_grab_touch_up(struct weston_touch_grab *grab,
731 uint32_t time, int touch_id)
732{
733 struct weston_touch_drag *touch_drag =
734 container_of(grab, struct weston_touch_drag, grab);
735 struct weston_touch *touch = grab->touch;
736
737 if (touch_id != touch->grab_touch_id)
738 return;
739
740 if (touch_drag->base.focus_resource)
741 wl_data_device_send_drop(touch_drag->base.focus_resource);
742 if (touch_drag->base.data_source)
743 wl_list_remove(&touch_drag->base.data_source_listener.link);
744 data_device_end_touch_drag_grab(touch_drag);
745}
746
747static void
748drag_grab_touch_focus(struct weston_touch_drag *drag)
749{
750 struct weston_touch *touch = drag->grab.touch;
751 struct weston_view *view;
752 wl_fixed_t view_x, view_y;
753
754 view = weston_compositor_pick_view(touch->seat->compositor,
755 touch->grab_x, touch->grab_y,
756 &view_x, &view_y);
757 if (drag->base.focus != view)
758 weston_drag_set_focus(&drag->base, touch->seat,
759 view, view_x, view_y);
760}
761
762static void
763drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300764 int touch_id, wl_fixed_t x, wl_fixed_t y)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800765{
766 struct weston_touch_drag *touch_drag =
767 container_of(grab, struct weston_touch_drag, grab);
768 struct weston_touch *touch = grab->touch;
769 wl_fixed_t view_x, view_y;
770 float fx, fy;
771
772 if (touch_id != touch->grab_touch_id)
773 return;
774
775 drag_grab_touch_focus(touch_drag);
776 if (touch_drag->base.icon) {
777 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
778 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
779 weston_view_set_position(touch_drag->base.icon, fx, fy);
780 weston_view_schedule_repaint(touch_drag->base.icon);
781 }
782
783 if (touch_drag->base.focus_resource) {
784 weston_view_from_global_fixed(touch_drag->base.focus,
785 touch->grab_x, touch->grab_y,
786 &view_x, &view_y);
787 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
788 view_x, view_y);
789 }
790}
791
792static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200793drag_grab_touch_frame(struct weston_touch_grab *grab)
794{
795}
796
797static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800798drag_grab_touch_cancel(struct weston_touch_grab *grab)
799{
800 struct weston_touch_drag *touch_drag =
801 container_of(grab, struct weston_touch_drag, grab);
802
803 if (touch_drag->base.data_source)
804 wl_list_remove(&touch_drag->base.data_source_listener.link);
805 data_device_end_touch_drag_grab(touch_drag);
806}
807
808static const struct weston_touch_grab_interface touch_drag_grab_interface = {
809 drag_grab_touch_down,
810 drag_grab_touch_up,
811 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200812 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800813 drag_grab_touch_cancel
814};
815
816static void
Carlos Garnachob2889882016-01-15 21:14:26 +0100817drag_grab_keyboard_key(struct weston_keyboard_grab *grab,
818 uint32_t time, uint32_t key, uint32_t state)
819{
820}
821
822static void
823drag_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
824 uint32_t serial, uint32_t mods_depressed,
825 uint32_t mods_latched,
826 uint32_t mods_locked, uint32_t group)
827{
828 struct weston_keyboard *keyboard = grab->keyboard;
829 struct weston_drag *drag =
830 container_of(grab, struct weston_drag, keyboard_grab);
831 uint32_t compositor_action;
832
833 if (mods_depressed & (1 << keyboard->xkb_info->shift_mod))
834 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
835 else if (mods_depressed & (1 << keyboard->xkb_info->ctrl_mod))
836 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
837 else
838 compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
839
840 drag->data_source->compositor_action = compositor_action;
841
842 if (drag->data_source->offer)
843 data_offer_update_action(drag->data_source->offer);
844}
845
846static void
847drag_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
848{
849 struct weston_drag *drag =
850 container_of(grab, struct weston_drag, keyboard_grab);
851 struct weston_pointer *pointer = grab->keyboard->seat->pointer_state;
852 struct weston_touch *touch = grab->keyboard->seat->touch_state;
853
854 if (pointer && pointer->grab->interface == &pointer_drag_grab_interface) {
855 struct weston_touch_drag *touch_drag =
856 (struct weston_touch_drag *) drag;
857 drag_grab_touch_cancel(&touch_drag->grab);
858 } else if (touch && touch->grab->interface == &touch_drag_grab_interface) {
859 struct weston_pointer_drag *pointer_drag =
860 (struct weston_pointer_drag *) drag;
861 drag_grab_cancel(&pointer_drag->grab);
862 }
863}
864
865static const struct weston_keyboard_grab_interface keyboard_drag_grab_interface = {
866 drag_grab_keyboard_key,
867 drag_grab_keyboard_modifiers,
868 drag_grab_keyboard_cancel
869};
870
871static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800872destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
873{
874 struct weston_pointer_drag *drag = container_of(listener,
875 struct weston_pointer_drag, base.data_source_listener);
876
877 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400878}
879
880static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400881handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400882{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400883 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400884 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400885
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400886 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400887}
888
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700889WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800890weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700891 struct weston_data_source *source,
892 struct weston_surface *icon,
893 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400894{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800895 struct weston_pointer_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100896 struct weston_keyboard *keyboard =
897 weston_seat_get_keyboard(pointer->seat);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400898
Peter Huttererf3d62272013-08-08 11:57:05 +1000899 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700900 if (drag == NULL)
901 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400902
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800903 drag->grab.interface = &pointer_drag_grab_interface;
Carlos Garnachob2889882016-01-15 21:14:26 +0100904 drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800905 drag->base.client = client;
906 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400907
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400908 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800909 drag->base.icon = weston_view_create(icon);
910 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 free(drag);
912 return -1;
913 }
914
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800915 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500916 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800917 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400918
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800919 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400920 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300921 weston_surface_set_label_func(icon,
922 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800924 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 }
926
927 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800928 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800930 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400931 }
932
Derek Foremanf9318d12015-05-11 15:40:11 -0500933 weston_pointer_clear_focus(pointer);
Carlos Garnachob2889882016-01-15 21:14:26 +0100934 weston_keyboard_set_focus(keyboard, NULL);
935
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800936 weston_pointer_start_grab(pointer, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100937 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800938
939 return 0;
940}
941
942static void
943destroy_touch_data_device_source(struct wl_listener *listener, void *data)
944{
945 struct weston_touch_drag *drag = container_of(listener,
946 struct weston_touch_drag, base.data_source_listener);
947
948 data_device_end_touch_drag_grab(drag);
949}
950
951WL_EXPORT int
952weston_touch_start_drag(struct weston_touch *touch,
953 struct weston_data_source *source,
954 struct weston_surface *icon,
955 struct wl_client *client)
956{
957 struct weston_touch_drag *drag;
Carlos Garnachob2889882016-01-15 21:14:26 +0100958 struct weston_keyboard *keyboard =
959 weston_seat_get_keyboard(touch->seat);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800960
961 drag = zalloc(sizeof *drag);
962 if (drag == NULL)
963 return -1;
964
965 drag->grab.interface = &touch_drag_grab_interface;
966 drag->base.client = client;
967 drag->base.data_source = source;
968
969 if (icon) {
970 drag->base.icon = weston_view_create(icon);
971 if (drag->base.icon == NULL) {
972 free(drag);
973 return -1;
974 }
975
976 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
977 wl_signal_add(&icon->destroy_signal,
978 &drag->base.icon_destroy_listener);
979
980 icon->configure = touch_drag_surface_configure;
981 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300982 weston_surface_set_label_func(icon,
983 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800984 } else {
985 drag->base.icon = NULL;
986 }
987
988 if (source) {
989 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
990 wl_signal_add(&source->destroy_signal,
991 &drag->base.data_source_listener);
992 }
993
Carlos Garnachob2889882016-01-15 21:14:26 +0100994 weston_keyboard_set_focus(keyboard, NULL);
995
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800996 weston_touch_start_grab(touch, &drag->grab);
Carlos Garnachob2889882016-01-15 21:14:26 +0100997 weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800998
999 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -07001000
1001 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001002}
1003
1004static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001005data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
1006 struct wl_resource *source_resource,
1007 struct wl_resource *origin_resource,
1008 struct wl_resource *icon_resource, uint32_t serial)
1009{
1010 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001011 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1012 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001013 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -07001014 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001015 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001016 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001017 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001018
Derek Foreman1281a362015-07-31 16:55:32 -05001019 is_pointer_grab = pointer &&
1020 pointer->button_count == 1 &&
1021 pointer->grab_serial == serial &&
1022 pointer->focus &&
1023 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001024
Derek Foreman1281a362015-07-31 16:55:32 -05001025 is_touch_grab = touch &&
1026 touch->num_tp == 1 &&
1027 touch->grab_serial == serial &&
1028 touch->focus &&
1029 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -07001030
1031 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001032 return;
1033
1034 /* FIXME: Check that the data source type array isn't empty. */
1035
1036 if (source_resource)
1037 source = wl_resource_get_user_data(source_resource);
1038 if (icon_resource)
1039 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +03001040
1041 if (icon) {
1042 if (weston_surface_set_role(icon, "wl_data_device-icon",
1043 resource,
1044 WL_DATA_DEVICE_ERROR_ROLE) < 0)
1045 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001046 }
1047
Jason Ekstrand8202d722014-06-24 21:19:24 -07001048 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001049 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -07001050 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -05001051 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +08001052
1053 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001054 wl_resource_post_no_memory(resource);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001055 else
1056 source->seat = seat;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -07001057}
1058
1059static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001060destroy_selection_data_source(struct wl_listener *listener, void *data)
1061{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001062 struct weston_seat *seat = container_of(listener, struct weston_seat,
1063 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05001064 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001065 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +01001066 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001067
1068 seat->selection_data_source = NULL;
1069
Derek Foreman1281a362015-07-31 16:55:32 -05001070 if (keyboard)
1071 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001072 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001073 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01001074 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001075 if (data_device)
1076 wl_data_device_send_selection(data_device, NULL);
1077 }
1078
1079 wl_signal_emit(&seat->selection_signal, seat);
1080}
1081
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001082/** \brief Send the selection to the specified client
1083 *
1084 * This function creates a new wl_data_offer if there is a wl_data_source
1085 * currently set as the selection and sends it to the specified client,
1086 * followed by the wl_data_device.selection() event.
1087 * If there is no current selection the wl_data_device.selection() event
1088 * will carry a NULL wl_data_offer.
1089 *
1090 * If the client does not have a wl_data_device for the specified seat
1091 * nothing will be done.
1092 *
1093 * \param seat The seat owning the wl_data_device used to send the events.
1094 * \param client The client to which to send the selection.
1095 */
1096WL_EXPORT void
1097weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
1098{
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001099 struct weston_data_offer *offer;
1100 struct wl_resource *data_device;
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001101
Giulio Camuffod46bb012015-05-01 12:59:36 +03001102 wl_resource_for_each(data_device, &seat->drag_resource_list) {
1103 if (wl_resource_get_client(data_device) != client)
1104 continue;
1105
1106 if (seat->selection_data_source) {
1107 offer = weston_data_source_send_offer(seat->selection_data_source,
Jonas Ådahl8b6c9fc2016-03-15 15:23:50 +08001108 data_device);
1109 wl_data_device_send_selection(data_device, offer->resource);
Giulio Camuffod46bb012015-05-01 12:59:36 +03001110 } else {
1111 wl_data_device_send_selection(data_device, NULL);
1112 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001113 }
1114}
1115
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001116WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001117weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001118 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001119{
Neil Roberts96d790e2013-09-19 17:32:00 +01001120 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -05001121 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001122
1123 if (seat->selection_data_source &&
1124 seat->selection_serial - serial < UINT32_MAX / 2)
1125 return;
1126
1127 if (seat->selection_data_source) {
1128 seat->selection_data_source->cancel(seat->selection_data_source);
1129 wl_list_remove(&seat->selection_data_source_listener.link);
1130 seat->selection_data_source = NULL;
1131 }
1132
1133 seat->selection_data_source = source;
1134 seat->selection_serial = serial;
1135
Derek Foreman1281a362015-07-31 16:55:32 -05001136 if (keyboard)
1137 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001138 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001139 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001140 }
1141
1142 wl_signal_emit(&seat->selection_signal, seat);
1143
1144 if (source) {
1145 seat->selection_data_source_listener.notify =
1146 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001147 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001148 &seat->selection_data_source_listener);
1149 }
1150}
1151
1152static void
1153data_device_set_selection(struct wl_client *client,
1154 struct wl_resource *resource,
1155 struct wl_resource *source_resource, uint32_t serial)
1156{
Carlos Garnacho9c931792016-01-18 23:52:12 +01001157 struct weston_data_source *source;
1158
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001159 if (!source_resource)
1160 return;
1161
Carlos Garnacho9c931792016-01-18 23:52:12 +01001162 source = wl_resource_get_user_data(source_resource);
1163
1164 if (source->actions_set) {
1165 wl_resource_post_error(source_resource,
1166 WL_DATA_SOURCE_ERROR_INVALID_SOURCE,
1167 "cannot set drag-and-drop source as selection");
1168 return;
1169 }
1170
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001171 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001172 weston_seat_set_selection(wl_resource_get_user_data(resource),
Carlos Garnacho9c931792016-01-18 23:52:12 +01001173 source, serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001174}
kabeer khan3a510d82014-10-20 11:47:15 +05301175static void
1176data_device_release(struct wl_client *client, struct wl_resource *resource)
1177{
1178 wl_resource_destroy(resource);
1179}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001180
1181static const struct wl_data_device_interface data_device_interface = {
1182 data_device_start_drag,
1183 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +05301184 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001185};
1186
1187static void
1188destroy_data_source(struct wl_resource *resource)
1189{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001190 struct weston_data_source *source =
1191 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001192 char **p;
1193
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001194 wl_signal_emit(&source->destroy_signal, source);
1195
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196 wl_array_for_each(p, &source->mime_types)
1197 free(*p);
1198
1199 wl_array_release(&source->mime_types);
1200
Kristian Høgsberg489b2792013-06-25 11:26:31 -04001201 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001202}
1203
1204static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001205client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001206 uint32_t time, const char *mime_type)
1207{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001208 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001209}
1210
1211static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001212client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001213 const char *mime_type, int32_t fd)
1214{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001215 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001216 close(fd);
1217}
1218
1219static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001220client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001221{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001222 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001223}
1224
1225static void
1226create_data_source(struct wl_client *client,
1227 struct wl_resource *resource, uint32_t id)
1228{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -07001229 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001230
1231 source = malloc(sizeof *source);
1232 if (source == NULL) {
1233 wl_resource_post_no_memory(resource);
1234 return;
1235 }
1236
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001237 source->resource =
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001238 wl_resource_create(client, &wl_data_source_interface,
1239 wl_resource_get_version(resource), id);
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -05001240 if (source->resource == NULL) {
1241 free(source);
1242 wl_resource_post_no_memory(resource);
1243 return;
1244 }
1245
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001246 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001247 source->accept = client_source_accept;
1248 source->send = client_source_send;
1249 source->cancel = client_source_cancel;
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001250 source->offer = NULL;
1251 source->accepted = false;
1252 source->seat = NULL;
Carlos Garnacho9c931792016-01-18 23:52:12 +01001253 source->actions_set = false;
1254 source->dnd_actions = 0;
1255 source->current_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Carlos Garnachob2889882016-01-15 21:14:26 +01001256 source->compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001257
1258 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001259
Jason Ekstranda85118c2013-06-27 20:17:02 -05001260 wl_resource_set_implementation(source->resource, &data_source_interface,
1261 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001262}
1263
1264static void unbind_data_device(struct wl_resource *resource)
1265{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -05001266 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001267}
1268
1269static void
1270get_data_device(struct wl_client *client,
1271 struct wl_resource *manager_resource,
1272 uint32_t id, struct wl_resource *seat_resource)
1273{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001274 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001275 struct wl_resource *resource;
1276
Jason Ekstranda85118c2013-06-27 20:17:02 -05001277 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +05301278 &wl_data_device_interface,
1279 wl_resource_get_version(manager_resource),
1280 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001281 if (resource == NULL) {
1282 wl_resource_post_no_memory(manager_resource);
1283 return;
1284 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001285
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001286 wl_list_insert(&seat->drag_resource_list,
1287 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001288 wl_resource_set_implementation(resource, &data_device_interface,
1289 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001290}
1291
1292static const struct wl_data_device_manager_interface manager_interface = {
1293 create_data_source,
1294 get_data_device
1295};
1296
1297static void
1298bind_manager(struct wl_client *client,
1299 void *data, uint32_t version, uint32_t id)
1300{
Jason Ekstranda85118c2013-06-27 20:17:02 -05001301 struct wl_resource *resource;
1302
kabeer khan3a510d82014-10-20 11:47:15 +05301303 resource = wl_resource_create(client,
1304 &wl_data_device_manager_interface,
1305 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001306 if (resource == NULL) {
1307 wl_client_post_no_memory(client);
1308 return;
1309 }
1310
1311 wl_resource_set_implementation(resource, &manager_interface,
1312 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001313}
1314
1315WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001316wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001317{
Neil Roberts96d790e2013-09-19 17:32:00 +01001318 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -05001319 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001320
Derek Foreman1281a362015-07-31 16:55:32 -05001321 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001322 return;
1323
Derek Foreman1281a362015-07-31 16:55:32 -05001324 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01001325 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001326 return;
1327
Giulio Camuffodddf9e62015-05-01 12:59:35 +03001328 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001329}
1330
1331WL_EXPORT int
1332wl_data_device_manager_init(struct wl_display *display)
1333{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001334 if (wl_global_create(display,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01001335 &wl_data_device_manager_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001336 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001337 return -1;
1338
1339 return 0;
1340}