blob: 9825bda69aa781bc9875ebca7bb3ad0b9a52e789 [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,
337 wl_fixed_t x, wl_fixed_t y)
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
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100345 weston_pointer_move(pointer, x, y);
346
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
414drag_grab_cancel(struct weston_pointer_grab *grab)
415{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800416 struct weston_pointer_drag *drag =
417 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200418
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800419 if (drag->base.data_source)
420 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200421
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800422 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200423}
424
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800425static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400426 drag_grab_focus,
427 drag_grab_motion,
428 drag_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200429 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400430};
431
432static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800433drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
434 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400435{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800436}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400437
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800438static void
439data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
440{
441 struct weston_touch *touch = drag->grab.touch;
442
443 data_device_end_drag_grab(&drag->base, touch->seat);
444 weston_touch_end_grab(touch);
445 free(drag);
446}
447
448static void
449drag_grab_touch_up(struct weston_touch_grab *grab,
450 uint32_t time, int touch_id)
451{
452 struct weston_touch_drag *touch_drag =
453 container_of(grab, struct weston_touch_drag, grab);
454 struct weston_touch *touch = grab->touch;
455
456 if (touch_id != touch->grab_touch_id)
457 return;
458
459 if (touch_drag->base.focus_resource)
460 wl_data_device_send_drop(touch_drag->base.focus_resource);
461 if (touch_drag->base.data_source)
462 wl_list_remove(&touch_drag->base.data_source_listener.link);
463 data_device_end_touch_drag_grab(touch_drag);
464}
465
466static void
467drag_grab_touch_focus(struct weston_touch_drag *drag)
468{
469 struct weston_touch *touch = drag->grab.touch;
470 struct weston_view *view;
471 wl_fixed_t view_x, view_y;
472
473 view = weston_compositor_pick_view(touch->seat->compositor,
474 touch->grab_x, touch->grab_y,
475 &view_x, &view_y);
476 if (drag->base.focus != view)
477 weston_drag_set_focus(&drag->base, touch->seat,
478 view, view_x, view_y);
479}
480
481static void
482drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
483 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
484{
485 struct weston_touch_drag *touch_drag =
486 container_of(grab, struct weston_touch_drag, grab);
487 struct weston_touch *touch = grab->touch;
488 wl_fixed_t view_x, view_y;
489 float fx, fy;
490
491 if (touch_id != touch->grab_touch_id)
492 return;
493
494 drag_grab_touch_focus(touch_drag);
495 if (touch_drag->base.icon) {
496 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
497 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
498 weston_view_set_position(touch_drag->base.icon, fx, fy);
499 weston_view_schedule_repaint(touch_drag->base.icon);
500 }
501
502 if (touch_drag->base.focus_resource) {
503 weston_view_from_global_fixed(touch_drag->base.focus,
504 touch->grab_x, touch->grab_y,
505 &view_x, &view_y);
506 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
507 view_x, view_y);
508 }
509}
510
511static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200512drag_grab_touch_frame(struct weston_touch_grab *grab)
513{
514}
515
516static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800517drag_grab_touch_cancel(struct weston_touch_grab *grab)
518{
519 struct weston_touch_drag *touch_drag =
520 container_of(grab, struct weston_touch_drag, grab);
521
522 if (touch_drag->base.data_source)
523 wl_list_remove(&touch_drag->base.data_source_listener.link);
524 data_device_end_touch_drag_grab(touch_drag);
525}
526
527static const struct weston_touch_grab_interface touch_drag_grab_interface = {
528 drag_grab_touch_down,
529 drag_grab_touch_up,
530 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200531 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800532 drag_grab_touch_cancel
533};
534
535static void
536destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
537{
538 struct weston_pointer_drag *drag = container_of(listener,
539 struct weston_pointer_drag, base.data_source_listener);
540
541 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400542}
543
544static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400545handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400546{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400547 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400548 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400549
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400550 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400551}
552
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700553WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800554weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700555 struct weston_data_source *source,
556 struct weston_surface *icon,
557 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400558{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800559 struct weston_pointer_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400560
Peter Huttererf3d62272013-08-08 11:57:05 +1000561 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700562 if (drag == NULL)
563 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400564
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800565 drag->grab.interface = &pointer_drag_grab_interface;
566 drag->base.client = client;
567 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400568
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400569 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800570 drag->base.icon = weston_view_create(icon);
571 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500572 free(drag);
573 return -1;
574 }
575
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800576 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500577 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800578 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400579
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800580 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400581 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300582 weston_surface_set_label_func(icon,
583 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500584 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800585 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 }
587
588 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800589 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500590 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800591 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400592 }
593
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800594 weston_pointer_set_focus(pointer, NULL,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400595 wl_fixed_from_int(0), wl_fixed_from_int(0));
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800596 weston_pointer_start_grab(pointer, &drag->grab);
597
598 return 0;
599}
600
601static void
602destroy_touch_data_device_source(struct wl_listener *listener, void *data)
603{
604 struct weston_touch_drag *drag = container_of(listener,
605 struct weston_touch_drag, base.data_source_listener);
606
607 data_device_end_touch_drag_grab(drag);
608}
609
610WL_EXPORT int
611weston_touch_start_drag(struct weston_touch *touch,
612 struct weston_data_source *source,
613 struct weston_surface *icon,
614 struct wl_client *client)
615{
616 struct weston_touch_drag *drag;
617
618 drag = zalloc(sizeof *drag);
619 if (drag == NULL)
620 return -1;
621
622 drag->grab.interface = &touch_drag_grab_interface;
623 drag->base.client = client;
624 drag->base.data_source = source;
625
626 if (icon) {
627 drag->base.icon = weston_view_create(icon);
628 if (drag->base.icon == NULL) {
629 free(drag);
630 return -1;
631 }
632
633 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
634 wl_signal_add(&icon->destroy_signal,
635 &drag->base.icon_destroy_listener);
636
637 icon->configure = touch_drag_surface_configure;
638 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300639 weston_surface_set_label_func(icon,
640 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800641 } else {
642 drag->base.icon = NULL;
643 }
644
645 if (source) {
646 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
647 wl_signal_add(&source->destroy_signal,
648 &drag->base.data_source_listener);
649 }
650
651 weston_touch_start_grab(touch, &drag->grab);
652
653 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700654
655 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400656}
657
658static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700659data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
660 struct wl_resource *source_resource,
661 struct wl_resource *origin_resource,
662 struct wl_resource *icon_resource, uint32_t serial)
663{
664 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -0500665 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
666 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700667 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700668 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700669 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700670 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800671 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700672
Derek Foreman1281a362015-07-31 16:55:32 -0500673 is_pointer_grab = pointer &&
674 pointer->button_count == 1 &&
675 pointer->grab_serial == serial &&
676 pointer->focus &&
677 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700678
Derek Foreman1281a362015-07-31 16:55:32 -0500679 is_touch_grab = touch &&
680 touch->num_tp == 1 &&
681 touch->grab_serial == serial &&
682 touch->focus &&
683 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700684
685 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700686 return;
687
688 /* FIXME: Check that the data source type array isn't empty. */
689
690 if (source_resource)
691 source = wl_resource_get_user_data(source_resource);
692 if (icon_resource)
693 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +0300694
695 if (icon) {
696 if (weston_surface_set_role(icon, "wl_data_device-icon",
697 resource,
698 WL_DATA_DEVICE_ERROR_ROLE) < 0)
699 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700700 }
701
Jason Ekstrand8202d722014-06-24 21:19:24 -0700702 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -0500703 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700704 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -0500705 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800706
707 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700708 wl_resource_post_no_memory(resource);
709}
710
711static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400712destroy_selection_data_source(struct wl_listener *listener, void *data)
713{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400714 struct weston_seat *seat = container_of(listener, struct weston_seat,
715 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -0500716 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400717 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100718 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400719
720 seat->selection_data_source = NULL;
721
Derek Foreman1281a362015-07-31 16:55:32 -0500722 if (keyboard)
723 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100724 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500725 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100726 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400727 if (data_device)
728 wl_data_device_send_selection(data_device, NULL);
729 }
730
731 wl_signal_emit(&seat->selection_signal, seat);
732}
733
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300734/** \brief Send the selection to the specified client
735 *
736 * This function creates a new wl_data_offer if there is a wl_data_source
737 * currently set as the selection and sends it to the specified client,
738 * followed by the wl_data_device.selection() event.
739 * If there is no current selection the wl_data_device.selection() event
740 * will carry a NULL wl_data_offer.
741 *
742 * If the client does not have a wl_data_device for the specified seat
743 * nothing will be done.
744 *
745 * \param seat The seat owning the wl_data_device used to send the events.
746 * \param client The client to which to send the selection.
747 */
748WL_EXPORT void
749weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
750{
751 struct wl_resource *data_device, *offer;
752
Giulio Camuffod46bb012015-05-01 12:59:36 +0300753 wl_resource_for_each(data_device, &seat->drag_resource_list) {
754 if (wl_resource_get_client(data_device) != client)
755 continue;
756
757 if (seat->selection_data_source) {
758 offer = weston_data_source_send_offer(seat->selection_data_source,
759 data_device);
760 wl_data_device_send_selection(data_device, offer);
761 } else {
762 wl_data_device_send_selection(data_device, NULL);
763 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300764 }
765}
766
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400767WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400768weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700769 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400770{
Neil Roberts96d790e2013-09-19 17:32:00 +0100771 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -0500772 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400773
774 if (seat->selection_data_source &&
775 seat->selection_serial - serial < UINT32_MAX / 2)
776 return;
777
778 if (seat->selection_data_source) {
779 seat->selection_data_source->cancel(seat->selection_data_source);
780 wl_list_remove(&seat->selection_data_source_listener.link);
781 seat->selection_data_source = NULL;
782 }
783
784 seat->selection_data_source = source;
785 seat->selection_serial = serial;
786
Derek Foreman1281a362015-07-31 16:55:32 -0500787 if (keyboard)
788 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100789 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300790 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400791 }
792
793 wl_signal_emit(&seat->selection_signal, seat);
794
795 if (source) {
796 seat->selection_data_source_listener.notify =
797 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500798 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400799 &seat->selection_data_source_listener);
800 }
801}
802
803static void
804data_device_set_selection(struct wl_client *client,
805 struct wl_resource *resource,
806 struct wl_resource *source_resource, uint32_t serial)
807{
808 if (!source_resource)
809 return;
810
811 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500812 weston_seat_set_selection(wl_resource_get_user_data(resource),
813 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400814 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400815}
kabeer khan3a510d82014-10-20 11:47:15 +0530816static void
817data_device_release(struct wl_client *client, struct wl_resource *resource)
818{
819 wl_resource_destroy(resource);
820}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400821
822static const struct wl_data_device_interface data_device_interface = {
823 data_device_start_drag,
824 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +0530825 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400826};
827
828static void
829destroy_data_source(struct wl_resource *resource)
830{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700831 struct weston_data_source *source =
832 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400833 char **p;
834
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500835 wl_signal_emit(&source->destroy_signal, source);
836
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400837 wl_array_for_each(p, &source->mime_types)
838 free(*p);
839
840 wl_array_release(&source->mime_types);
841
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400842 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400843}
844
845static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700846client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400847 uint32_t time, const char *mime_type)
848{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500849 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400850}
851
852static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700853client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400854 const char *mime_type, int32_t fd)
855{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500856 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400857 close(fd);
858}
859
860static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700861client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400862{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500863 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400864}
865
866static void
867create_data_source(struct wl_client *client,
868 struct wl_resource *resource, uint32_t id)
869{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700870 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400871
872 source = malloc(sizeof *source);
873 if (source == NULL) {
874 wl_resource_post_no_memory(resource);
875 return;
876 }
877
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500878 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400879 source->accept = client_source_accept;
880 source->send = client_source_send;
881 source->cancel = client_source_cancel;
882
883 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500884
Jason Ekstranda85118c2013-06-27 20:17:02 -0500885 source->resource =
886 wl_resource_create(client, &wl_data_source_interface, 1, id);
887 wl_resource_set_implementation(source->resource, &data_source_interface,
888 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400889}
890
891static void unbind_data_device(struct wl_resource *resource)
892{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500893 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400894}
895
896static void
897get_data_device(struct wl_client *client,
898 struct wl_resource *manager_resource,
899 uint32_t id, struct wl_resource *seat_resource)
900{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500901 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400902 struct wl_resource *resource;
903
Jason Ekstranda85118c2013-06-27 20:17:02 -0500904 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +0530905 &wl_data_device_interface,
906 wl_resource_get_version(manager_resource),
907 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700908 if (resource == NULL) {
909 wl_resource_post_no_memory(manager_resource);
910 return;
911 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400912
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700913 wl_list_insert(&seat->drag_resource_list,
914 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500915 wl_resource_set_implementation(resource, &data_device_interface,
916 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400917}
918
919static const struct wl_data_device_manager_interface manager_interface = {
920 create_data_source,
921 get_data_device
922};
923
924static void
925bind_manager(struct wl_client *client,
926 void *data, uint32_t version, uint32_t id)
927{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500928 struct wl_resource *resource;
929
kabeer khan3a510d82014-10-20 11:47:15 +0530930 resource = wl_resource_create(client,
931 &wl_data_device_manager_interface,
932 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700933 if (resource == NULL) {
934 wl_client_post_no_memory(client);
935 return;
936 }
937
938 wl_resource_set_implementation(resource, &manager_interface,
939 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400940}
941
942WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400943wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400944{
Neil Roberts96d790e2013-09-19 17:32:00 +0100945 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -0500946 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400947
Derek Foreman1281a362015-07-31 16:55:32 -0500948 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400949 return;
950
Derek Foreman1281a362015-07-31 16:55:32 -0500951 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100952 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400953 return;
954
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300955 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400956}
957
958WL_EXPORT int
959wl_data_device_manager_init(struct wl_display *display)
960{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400961 if (wl_global_create(display,
kabeer khan3a510d82014-10-20 11:47:15 +0530962 &wl_data_device_manager_interface, 2,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400963 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400964 return -1;
965
966 return 0;
967}