blob: 4a1c1b85bcb23c71aa4a1372bc8f80a13ed747b6 [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;
47};
48
Xiong Zhangfd51e7b2013-11-25 18:42:49 +080049struct weston_pointer_drag {
50 struct weston_drag base;
51 struct weston_pointer_grab grab;
52};
53
54struct weston_touch_drag {
55 struct weston_drag base;
56 struct weston_touch_grab grab;
57};
58
Kristian Høgsberg2158a882013-04-18 15:07:39 -040059static void
60data_offer_accept(struct wl_client *client, struct wl_resource *resource,
61 uint32_t serial, const char *mime_type)
62{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070063 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040064
65 /* FIXME: Check that client is currently focused by the input
66 * device that is currently dragging this data source. Should
67 * this be a wl_data_device request? */
68
69 if (offer->source)
70 offer->source->accept(offer->source, serial, mime_type);
71}
72
73static void
74data_offer_receive(struct wl_client *client, struct wl_resource *resource,
75 const char *mime_type, int32_t fd)
76{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070077 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040078
79 if (offer->source)
80 offer->source->send(offer->source, mime_type, fd);
81 else
82 close(fd);
83}
84
85static void
86data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
87{
88 wl_resource_destroy(resource);
89}
90
91static const struct wl_data_offer_interface data_offer_interface = {
92 data_offer_accept,
93 data_offer_receive,
94 data_offer_destroy,
95};
96
97static void
98destroy_data_offer(struct wl_resource *resource)
99{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700100 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400101
102 if (offer->source)
103 wl_list_remove(&offer->source_destroy_listener.link);
104 free(offer);
105}
106
107static void
108destroy_offer_data_source(struct wl_listener *listener, void *data)
109{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700110 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400111
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700112 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400113 source_destroy_listener);
114
115 offer->source = NULL;
116}
117
118static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700119weston_data_source_send_offer(struct weston_data_source *source,
120 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400121{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700122 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400123 char **p;
124
125 offer = malloc(sizeof *offer);
126 if (offer == NULL)
127 return NULL;
128
Jason Ekstranda85118c2013-06-27 20:17:02 -0500129 offer->resource =
130 wl_resource_create(wl_resource_get_client(target),
131 &wl_data_offer_interface, 1, 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700132 if (offer->resource == NULL) {
133 free(offer);
134 return NULL;
135 }
136
Jason Ekstranda85118c2013-06-27 20:17:02 -0500137 wl_resource_set_implementation(offer->resource, &data_offer_interface,
138 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400139
140 offer->source = source;
141 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500142 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400143 &offer->source_destroy_listener);
144
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500145 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146
147 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500148 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400149
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500150 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400151}
152
153static void
154data_source_offer(struct wl_client *client,
155 struct wl_resource *resource,
156 const char *type)
157{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700158 struct weston_data_source *source =
159 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400160 char **p;
161
162 p = wl_array_add(&source->mime_types, sizeof *p);
163 if (p)
164 *p = strdup(type);
165 if (!p || !*p)
166 wl_resource_post_no_memory(resource);
167}
168
169static void
170data_source_destroy(struct wl_client *client, struct wl_resource *resource)
171{
172 wl_resource_destroy(resource);
173}
174
175static struct wl_data_source_interface data_source_interface = {
176 data_source_offer,
177 data_source_destroy
178};
179
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400180static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800181drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100182 struct weston_pointer *pointer,
183 struct weston_touch *touch,
184 struct weston_surface *es,
185 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400186{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300187 struct weston_layer_entry *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400188 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400189
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800190 assert((pointer != NULL && touch == NULL) ||
191 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500192
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400193 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800194 if (pointer && pointer->sprite &&
195 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400196 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400197 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500198 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400199
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300200 weston_layer_entry_remove(&drag->icon->layer_link);
201 weston_layer_entry_insert(list, &drag->icon->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500202 weston_view_update_transform(drag->icon);
Jason Ekstrandef540082014-06-26 10:37:36 -0700203 pixman_region32_clear(&es->pending.input);
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400204 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400205
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400206 drag->dx += sx;
207 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400208
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800209 /* init to 0 for avoiding a compile warning */
210 fx = fy = 0;
211 if (pointer) {
212 fx = wl_fixed_to_double(pointer->x) + drag->dx;
213 fy = wl_fixed_to_double(pointer->y) + drag->dy;
214 } else if (touch) {
215 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
216 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
217 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600218 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400219}
220
Pekka Paalanen8274d902014-08-06 19:36:51 +0300221static int
222pointer_drag_surface_get_label(struct weston_surface *surface,
223 char *buf, size_t len)
224{
225 return snprintf(buf, len, "pointer drag icon");
226}
227
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400228static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100229pointer_drag_surface_configure(struct weston_surface *es,
230 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800231{
232 struct weston_pointer_drag *drag = es->configure_private;
233 struct weston_pointer *pointer = drag->grab.pointer;
234
235 assert(es->configure == pointer_drag_surface_configure);
236
Jonas Ådahl767d8912013-12-03 22:30:17 +0100237 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800238}
239
Pekka Paalanen8274d902014-08-06 19:36:51 +0300240static int
241touch_drag_surface_get_label(struct weston_surface *surface,
242 char *buf, size_t len)
243{
244 return snprintf(buf, len, "touch drag icon");
245}
246
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800247static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100248touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800249{
250 struct weston_touch_drag *drag = es->configure_private;
251 struct weston_touch *touch = drag->grab.touch;
252
253 assert(es->configure == touch_drag_surface_configure);
254
Jonas Ådahl767d8912013-12-03 22:30:17 +0100255 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800256}
257
258static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400259destroy_drag_focus(struct wl_listener *listener, void *data)
260{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400261 struct weston_drag *drag =
262 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400263
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400264 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400265}
266
267static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800268weston_drag_set_focus(struct weston_drag *drag,
269 struct weston_seat *seat,
270 struct weston_view *view,
271 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400272{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400273 struct wl_resource *resource, *offer = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800274 struct wl_display *display = seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400275 uint32_t serial;
276
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277 if (drag->focus && view && drag->focus->surface == view->surface) {
278 drag->focus = view;
279 return;
280 }
281
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400282 if (drag->focus_resource) {
283 wl_data_device_send_leave(drag->focus_resource);
284 wl_list_remove(&drag->focus_listener.link);
285 drag->focus_resource = NULL;
286 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400287 }
288
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400290 return;
291
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500292 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400294 return;
295
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800296 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400298 if (!resource)
299 return;
300
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400301 serial = wl_display_next_serial(display);
302
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700303 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700304 offer = weston_data_source_send_offer(drag->data_source,
305 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700306 if (offer == NULL)
307 return;
308 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400309
Jason Ekstranda7af7042013-10-12 22:38:11 -0500310 wl_data_device_send_enter(resource, serial, view->surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400311 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400312
Jason Ekstranda7af7042013-10-12 22:38:11 -0500313 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400314 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500315 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400316 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400317}
318
319static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400320drag_grab_focus(struct weston_pointer_grab *grab)
321{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800322 struct weston_pointer_drag *drag =
323 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400324 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500325 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400326 wl_fixed_t sx, sy;
327
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 view = weston_compositor_pick_view(pointer->seat->compositor,
329 pointer->x, pointer->y,
330 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800331 if (drag->base.focus != view)
332 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400333}
334
335static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100336drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200337 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800339 struct weston_pointer_drag *drag =
340 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400341 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400342 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400343 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400344
Jonas Ådahld2510102014-10-05 21:39:14 +0200345 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100346
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800347 if (drag->base.icon) {
348 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
349 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
350 weston_view_set_position(drag->base.icon, fx, fy);
351 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400352 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400353
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800354 if (drag->base.focus_resource) {
355 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500356 pointer->x, pointer->y,
357 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400358
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800359 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400360 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400361}
362
363static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800364data_device_end_drag_grab(struct weston_drag *drag,
365 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400366{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400367 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368 if (weston_view_is_mapped(drag->icon))
369 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400370
Jason Ekstranda7af7042013-10-12 22:38:11 -0500371 drag->icon->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300372 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700373 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400374 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400376 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400377
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800378 weston_drag_set_focus(drag, seat, NULL, 0, 0);
379}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400380
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800381static void
382data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
383{
384 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800386 data_device_end_drag_grab(&drag->base, pointer->seat);
387 weston_pointer_end_grab(pointer);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400388 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400389}
390
391static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400392drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400393 uint32_t time, uint32_t button, uint32_t state_w)
394{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800395 struct weston_pointer_drag *drag =
396 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400397 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398 enum wl_pointer_button_state state = state_w;
399
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800400 if (drag->base.focus_resource &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400401 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402 state == WL_POINTER_BUTTON_STATE_RELEASED)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800403 wl_data_device_send_drop(drag->base.focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400405 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800407 if (drag->base.data_source)
408 wl_list_remove(&drag->base.data_source_listener.link);
409 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400410 }
411}
412
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200413static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200414drag_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000415 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200416{
417}
418
419static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200420drag_grab_cancel(struct weston_pointer_grab *grab)
421{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800422 struct weston_pointer_drag *drag =
423 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200424
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800425 if (drag->base.data_source)
426 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200427
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800428 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200429}
430
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800431static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400432 drag_grab_focus,
433 drag_grab_motion,
434 drag_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200435 drag_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200436 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400437};
438
439static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800440drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
441 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800443}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400444
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800445static void
446data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
447{
448 struct weston_touch *touch = drag->grab.touch;
449
450 data_device_end_drag_grab(&drag->base, touch->seat);
451 weston_touch_end_grab(touch);
452 free(drag);
453}
454
455static void
456drag_grab_touch_up(struct weston_touch_grab *grab,
457 uint32_t time, int touch_id)
458{
459 struct weston_touch_drag *touch_drag =
460 container_of(grab, struct weston_touch_drag, grab);
461 struct weston_touch *touch = grab->touch;
462
463 if (touch_id != touch->grab_touch_id)
464 return;
465
466 if (touch_drag->base.focus_resource)
467 wl_data_device_send_drop(touch_drag->base.focus_resource);
468 if (touch_drag->base.data_source)
469 wl_list_remove(&touch_drag->base.data_source_listener.link);
470 data_device_end_touch_drag_grab(touch_drag);
471}
472
473static void
474drag_grab_touch_focus(struct weston_touch_drag *drag)
475{
476 struct weston_touch *touch = drag->grab.touch;
477 struct weston_view *view;
478 wl_fixed_t view_x, view_y;
479
480 view = weston_compositor_pick_view(touch->seat->compositor,
481 touch->grab_x, touch->grab_y,
482 &view_x, &view_y);
483 if (drag->base.focus != view)
484 weston_drag_set_focus(&drag->base, touch->seat,
485 view, view_x, view_y);
486}
487
488static void
489drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300490 int touch_id, wl_fixed_t x, wl_fixed_t y)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800491{
492 struct weston_touch_drag *touch_drag =
493 container_of(grab, struct weston_touch_drag, grab);
494 struct weston_touch *touch = grab->touch;
495 wl_fixed_t view_x, view_y;
496 float fx, fy;
497
498 if (touch_id != touch->grab_touch_id)
499 return;
500
501 drag_grab_touch_focus(touch_drag);
502 if (touch_drag->base.icon) {
503 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
504 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
505 weston_view_set_position(touch_drag->base.icon, fx, fy);
506 weston_view_schedule_repaint(touch_drag->base.icon);
507 }
508
509 if (touch_drag->base.focus_resource) {
510 weston_view_from_global_fixed(touch_drag->base.focus,
511 touch->grab_x, touch->grab_y,
512 &view_x, &view_y);
513 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
514 view_x, view_y);
515 }
516}
517
518static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200519drag_grab_touch_frame(struct weston_touch_grab *grab)
520{
521}
522
523static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800524drag_grab_touch_cancel(struct weston_touch_grab *grab)
525{
526 struct weston_touch_drag *touch_drag =
527 container_of(grab, struct weston_touch_drag, grab);
528
529 if (touch_drag->base.data_source)
530 wl_list_remove(&touch_drag->base.data_source_listener.link);
531 data_device_end_touch_drag_grab(touch_drag);
532}
533
534static const struct weston_touch_grab_interface touch_drag_grab_interface = {
535 drag_grab_touch_down,
536 drag_grab_touch_up,
537 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200538 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800539 drag_grab_touch_cancel
540};
541
542static void
543destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
544{
545 struct weston_pointer_drag *drag = container_of(listener,
546 struct weston_pointer_drag, base.data_source_listener);
547
548 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400549}
550
551static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400552handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400553{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400554 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400555 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400556
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400557 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400558}
559
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700560WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800561weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700562 struct weston_data_source *source,
563 struct weston_surface *icon,
564 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400565{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800566 struct weston_pointer_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400567
Peter Huttererf3d62272013-08-08 11:57:05 +1000568 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700569 if (drag == NULL)
570 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400571
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800572 drag->grab.interface = &pointer_drag_grab_interface;
573 drag->base.client = client;
574 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400575
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400576 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800577 drag->base.icon = weston_view_create(icon);
578 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 free(drag);
580 return -1;
581 }
582
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800583 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500584 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800585 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400586
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800587 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400588 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300589 weston_surface_set_label_func(icon,
590 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500591 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800592 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500593 }
594
595 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800596 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500597 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800598 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400599 }
600
Derek Foremanf9318d12015-05-11 15:40:11 -0500601 weston_pointer_clear_focus(pointer);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800602 weston_pointer_start_grab(pointer, &drag->grab);
603
604 return 0;
605}
606
607static void
608destroy_touch_data_device_source(struct wl_listener *listener, void *data)
609{
610 struct weston_touch_drag *drag = container_of(listener,
611 struct weston_touch_drag, base.data_source_listener);
612
613 data_device_end_touch_drag_grab(drag);
614}
615
616WL_EXPORT int
617weston_touch_start_drag(struct weston_touch *touch,
618 struct weston_data_source *source,
619 struct weston_surface *icon,
620 struct wl_client *client)
621{
622 struct weston_touch_drag *drag;
623
624 drag = zalloc(sizeof *drag);
625 if (drag == NULL)
626 return -1;
627
628 drag->grab.interface = &touch_drag_grab_interface;
629 drag->base.client = client;
630 drag->base.data_source = source;
631
632 if (icon) {
633 drag->base.icon = weston_view_create(icon);
634 if (drag->base.icon == NULL) {
635 free(drag);
636 return -1;
637 }
638
639 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
640 wl_signal_add(&icon->destroy_signal,
641 &drag->base.icon_destroy_listener);
642
643 icon->configure = touch_drag_surface_configure;
644 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300645 weston_surface_set_label_func(icon,
646 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800647 } else {
648 drag->base.icon = NULL;
649 }
650
651 if (source) {
652 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
653 wl_signal_add(&source->destroy_signal,
654 &drag->base.data_source_listener);
655 }
656
657 weston_touch_start_grab(touch, &drag->grab);
658
659 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700660
661 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400662}
663
664static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700665data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
666 struct wl_resource *source_resource,
667 struct wl_resource *origin_resource,
668 struct wl_resource *icon_resource, uint32_t serial)
669{
670 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -0500671 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
672 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700673 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700674 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700675 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700676 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800677 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700678
Derek Foreman1281a362015-07-31 16:55:32 -0500679 is_pointer_grab = pointer &&
680 pointer->button_count == 1 &&
681 pointer->grab_serial == serial &&
682 pointer->focus &&
683 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700684
Derek Foreman1281a362015-07-31 16:55:32 -0500685 is_touch_grab = touch &&
686 touch->num_tp == 1 &&
687 touch->grab_serial == serial &&
688 touch->focus &&
689 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700690
691 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700692 return;
693
694 /* FIXME: Check that the data source type array isn't empty. */
695
696 if (source_resource)
697 source = wl_resource_get_user_data(source_resource);
698 if (icon_resource)
699 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +0300700
701 if (icon) {
702 if (weston_surface_set_role(icon, "wl_data_device-icon",
703 resource,
704 WL_DATA_DEVICE_ERROR_ROLE) < 0)
705 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700706 }
707
Jason Ekstrand8202d722014-06-24 21:19:24 -0700708 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -0500709 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700710 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -0500711 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800712
713 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700714 wl_resource_post_no_memory(resource);
715}
716
717static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400718destroy_selection_data_source(struct wl_listener *listener, void *data)
719{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400720 struct weston_seat *seat = container_of(listener, struct weston_seat,
721 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -0500722 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400723 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100724 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400725
726 seat->selection_data_source = NULL;
727
Derek Foreman1281a362015-07-31 16:55:32 -0500728 if (keyboard)
729 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100730 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500731 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100732 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400733 if (data_device)
734 wl_data_device_send_selection(data_device, NULL);
735 }
736
737 wl_signal_emit(&seat->selection_signal, seat);
738}
739
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300740/** \brief Send the selection to the specified client
741 *
742 * This function creates a new wl_data_offer if there is a wl_data_source
743 * currently set as the selection and sends it to the specified client,
744 * followed by the wl_data_device.selection() event.
745 * If there is no current selection the wl_data_device.selection() event
746 * will carry a NULL wl_data_offer.
747 *
748 * If the client does not have a wl_data_device for the specified seat
749 * nothing will be done.
750 *
751 * \param seat The seat owning the wl_data_device used to send the events.
752 * \param client The client to which to send the selection.
753 */
754WL_EXPORT void
755weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
756{
757 struct wl_resource *data_device, *offer;
758
Giulio Camuffod46bb012015-05-01 12:59:36 +0300759 wl_resource_for_each(data_device, &seat->drag_resource_list) {
760 if (wl_resource_get_client(data_device) != client)
761 continue;
762
763 if (seat->selection_data_source) {
764 offer = weston_data_source_send_offer(seat->selection_data_source,
765 data_device);
766 wl_data_device_send_selection(data_device, offer);
767 } else {
768 wl_data_device_send_selection(data_device, NULL);
769 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300770 }
771}
772
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400773WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400774weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700775 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400776{
Neil Roberts96d790e2013-09-19 17:32:00 +0100777 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -0500778 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400779
780 if (seat->selection_data_source &&
781 seat->selection_serial - serial < UINT32_MAX / 2)
782 return;
783
784 if (seat->selection_data_source) {
785 seat->selection_data_source->cancel(seat->selection_data_source);
786 wl_list_remove(&seat->selection_data_source_listener.link);
787 seat->selection_data_source = NULL;
788 }
789
790 seat->selection_data_source = source;
791 seat->selection_serial = serial;
792
Derek Foreman1281a362015-07-31 16:55:32 -0500793 if (keyboard)
794 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100795 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300796 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400797 }
798
799 wl_signal_emit(&seat->selection_signal, seat);
800
801 if (source) {
802 seat->selection_data_source_listener.notify =
803 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500804 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400805 &seat->selection_data_source_listener);
806 }
807}
808
809static void
810data_device_set_selection(struct wl_client *client,
811 struct wl_resource *resource,
812 struct wl_resource *source_resource, uint32_t serial)
813{
814 if (!source_resource)
815 return;
816
817 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500818 weston_seat_set_selection(wl_resource_get_user_data(resource),
819 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400820 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400821}
kabeer khan3a510d82014-10-20 11:47:15 +0530822static void
823data_device_release(struct wl_client *client, struct wl_resource *resource)
824{
825 wl_resource_destroy(resource);
826}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400827
828static const struct wl_data_device_interface data_device_interface = {
829 data_device_start_drag,
830 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +0530831 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400832};
833
834static void
835destroy_data_source(struct wl_resource *resource)
836{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700837 struct weston_data_source *source =
838 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400839 char **p;
840
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500841 wl_signal_emit(&source->destroy_signal, source);
842
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400843 wl_array_for_each(p, &source->mime_types)
844 free(*p);
845
846 wl_array_release(&source->mime_types);
847
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400848 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400849}
850
851static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700852client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400853 uint32_t time, const char *mime_type)
854{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500855 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400856}
857
858static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700859client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400860 const char *mime_type, int32_t fd)
861{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500862 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400863 close(fd);
864}
865
866static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700867client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400868{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500869 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400870}
871
872static void
873create_data_source(struct wl_client *client,
874 struct wl_resource *resource, uint32_t id)
875{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700876 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400877
878 source = malloc(sizeof *source);
879 if (source == NULL) {
880 wl_resource_post_no_memory(resource);
881 return;
882 }
883
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -0500884 source->resource =
885 wl_resource_create(client, &wl_data_source_interface, 1, id);
886 if (source->resource == NULL) {
887 free(source);
888 wl_resource_post_no_memory(resource);
889 return;
890 }
891
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500892 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400893 source->accept = client_source_accept;
894 source->send = client_source_send;
895 source->cancel = client_source_cancel;
896
897 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500898
Jason Ekstranda85118c2013-06-27 20:17:02 -0500899 wl_resource_set_implementation(source->resource, &data_source_interface,
900 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400901}
902
903static void unbind_data_device(struct wl_resource *resource)
904{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500905 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400906}
907
908static void
909get_data_device(struct wl_client *client,
910 struct wl_resource *manager_resource,
911 uint32_t id, struct wl_resource *seat_resource)
912{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500913 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400914 struct wl_resource *resource;
915
Jason Ekstranda85118c2013-06-27 20:17:02 -0500916 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +0530917 &wl_data_device_interface,
918 wl_resource_get_version(manager_resource),
919 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700920 if (resource == NULL) {
921 wl_resource_post_no_memory(manager_resource);
922 return;
923 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400924
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700925 wl_list_insert(&seat->drag_resource_list,
926 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500927 wl_resource_set_implementation(resource, &data_device_interface,
928 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400929}
930
931static const struct wl_data_device_manager_interface manager_interface = {
932 create_data_source,
933 get_data_device
934};
935
936static void
937bind_manager(struct wl_client *client,
938 void *data, uint32_t version, uint32_t id)
939{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500940 struct wl_resource *resource;
941
kabeer khan3a510d82014-10-20 11:47:15 +0530942 resource = wl_resource_create(client,
943 &wl_data_device_manager_interface,
944 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700945 if (resource == NULL) {
946 wl_client_post_no_memory(client);
947 return;
948 }
949
950 wl_resource_set_implementation(resource, &manager_interface,
951 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400952}
953
954WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400955wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400956{
Neil Roberts96d790e2013-09-19 17:32:00 +0100957 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -0500958 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400959
Derek Foreman1281a362015-07-31 16:55:32 -0500960 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400961 return;
962
Derek Foreman1281a362015-07-31 16:55:32 -0500963 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100964 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400965 return;
966
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300967 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400968}
969
970WL_EXPORT int
971wl_data_device_manager_init(struct wl_display *display)
972{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400973 if (wl_global_create(display,
kabeer khan3a510d82014-10-20 11:47:15 +0530974 &wl_data_device_manager_interface, 2,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400975 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400976 return -1;
977
978 return 0;
979}