blob: 1612091aa75b5ff23b484163fabb7605a1b8b0b0 [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,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300483 int touch_id, wl_fixed_t x, wl_fixed_t y)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800484{
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
Derek Foremanf9318d12015-05-11 15:40:11 -0500594 weston_pointer_clear_focus(pointer);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800595 weston_pointer_start_grab(pointer, &drag->grab);
596
597 return 0;
598}
599
600static void
601destroy_touch_data_device_source(struct wl_listener *listener, void *data)
602{
603 struct weston_touch_drag *drag = container_of(listener,
604 struct weston_touch_drag, base.data_source_listener);
605
606 data_device_end_touch_drag_grab(drag);
607}
608
609WL_EXPORT int
610weston_touch_start_drag(struct weston_touch *touch,
611 struct weston_data_source *source,
612 struct weston_surface *icon,
613 struct wl_client *client)
614{
615 struct weston_touch_drag *drag;
616
617 drag = zalloc(sizeof *drag);
618 if (drag == NULL)
619 return -1;
620
621 drag->grab.interface = &touch_drag_grab_interface;
622 drag->base.client = client;
623 drag->base.data_source = source;
624
625 if (icon) {
626 drag->base.icon = weston_view_create(icon);
627 if (drag->base.icon == NULL) {
628 free(drag);
629 return -1;
630 }
631
632 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
633 wl_signal_add(&icon->destroy_signal,
634 &drag->base.icon_destroy_listener);
635
636 icon->configure = touch_drag_surface_configure;
637 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300638 weston_surface_set_label_func(icon,
639 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800640 } else {
641 drag->base.icon = NULL;
642 }
643
644 if (source) {
645 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
646 wl_signal_add(&source->destroy_signal,
647 &drag->base.data_source_listener);
648 }
649
650 weston_touch_start_grab(touch, &drag->grab);
651
652 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700653
654 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400655}
656
657static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700658data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
659 struct wl_resource *source_resource,
660 struct wl_resource *origin_resource,
661 struct wl_resource *icon_resource, uint32_t serial)
662{
663 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -0500664 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
665 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700666 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700667 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700668 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700669 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800670 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700671
Derek Foreman1281a362015-07-31 16:55:32 -0500672 is_pointer_grab = pointer &&
673 pointer->button_count == 1 &&
674 pointer->grab_serial == serial &&
675 pointer->focus &&
676 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700677
Derek Foreman1281a362015-07-31 16:55:32 -0500678 is_touch_grab = touch &&
679 touch->num_tp == 1 &&
680 touch->grab_serial == serial &&
681 touch->focus &&
682 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700683
684 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700685 return;
686
687 /* FIXME: Check that the data source type array isn't empty. */
688
689 if (source_resource)
690 source = wl_resource_get_user_data(source_resource);
691 if (icon_resource)
692 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +0300693
694 if (icon) {
695 if (weston_surface_set_role(icon, "wl_data_device-icon",
696 resource,
697 WL_DATA_DEVICE_ERROR_ROLE) < 0)
698 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700699 }
700
Jason Ekstrand8202d722014-06-24 21:19:24 -0700701 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -0500702 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700703 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -0500704 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800705
706 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700707 wl_resource_post_no_memory(resource);
708}
709
710static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400711destroy_selection_data_source(struct wl_listener *listener, void *data)
712{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400713 struct weston_seat *seat = container_of(listener, struct weston_seat,
714 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -0500715 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400716 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100717 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400718
719 seat->selection_data_source = NULL;
720
Derek Foreman1281a362015-07-31 16:55:32 -0500721 if (keyboard)
722 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100723 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500724 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100725 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400726 if (data_device)
727 wl_data_device_send_selection(data_device, NULL);
728 }
729
730 wl_signal_emit(&seat->selection_signal, seat);
731}
732
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300733/** \brief Send the selection to the specified client
734 *
735 * This function creates a new wl_data_offer if there is a wl_data_source
736 * currently set as the selection and sends it to the specified client,
737 * followed by the wl_data_device.selection() event.
738 * If there is no current selection the wl_data_device.selection() event
739 * will carry a NULL wl_data_offer.
740 *
741 * If the client does not have a wl_data_device for the specified seat
742 * nothing will be done.
743 *
744 * \param seat The seat owning the wl_data_device used to send the events.
745 * \param client The client to which to send the selection.
746 */
747WL_EXPORT void
748weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
749{
750 struct wl_resource *data_device, *offer;
751
Giulio Camuffod46bb012015-05-01 12:59:36 +0300752 wl_resource_for_each(data_device, &seat->drag_resource_list) {
753 if (wl_resource_get_client(data_device) != client)
754 continue;
755
756 if (seat->selection_data_source) {
757 offer = weston_data_source_send_offer(seat->selection_data_source,
758 data_device);
759 wl_data_device_send_selection(data_device, offer);
760 } else {
761 wl_data_device_send_selection(data_device, NULL);
762 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300763 }
764}
765
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400766WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400767weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700768 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400769{
Neil Roberts96d790e2013-09-19 17:32:00 +0100770 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -0500771 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400772
773 if (seat->selection_data_source &&
774 seat->selection_serial - serial < UINT32_MAX / 2)
775 return;
776
777 if (seat->selection_data_source) {
778 seat->selection_data_source->cancel(seat->selection_data_source);
779 wl_list_remove(&seat->selection_data_source_listener.link);
780 seat->selection_data_source = NULL;
781 }
782
783 seat->selection_data_source = source;
784 seat->selection_serial = serial;
785
Derek Foreman1281a362015-07-31 16:55:32 -0500786 if (keyboard)
787 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100788 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300789 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400790 }
791
792 wl_signal_emit(&seat->selection_signal, seat);
793
794 if (source) {
795 seat->selection_data_source_listener.notify =
796 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500797 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400798 &seat->selection_data_source_listener);
799 }
800}
801
802static void
803data_device_set_selection(struct wl_client *client,
804 struct wl_resource *resource,
805 struct wl_resource *source_resource, uint32_t serial)
806{
807 if (!source_resource)
808 return;
809
810 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500811 weston_seat_set_selection(wl_resource_get_user_data(resource),
812 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400813 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400814}
kabeer khan3a510d82014-10-20 11:47:15 +0530815static void
816data_device_release(struct wl_client *client, struct wl_resource *resource)
817{
818 wl_resource_destroy(resource);
819}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400820
821static const struct wl_data_device_interface data_device_interface = {
822 data_device_start_drag,
823 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +0530824 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400825};
826
827static void
828destroy_data_source(struct wl_resource *resource)
829{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700830 struct weston_data_source *source =
831 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400832 char **p;
833
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500834 wl_signal_emit(&source->destroy_signal, source);
835
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400836 wl_array_for_each(p, &source->mime_types)
837 free(*p);
838
839 wl_array_release(&source->mime_types);
840
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400841 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400842}
843
844static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700845client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400846 uint32_t time, const char *mime_type)
847{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500848 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400849}
850
851static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700852client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400853 const char *mime_type, int32_t fd)
854{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500855 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400856 close(fd);
857}
858
859static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700860client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400861{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500862 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400863}
864
865static void
866create_data_source(struct wl_client *client,
867 struct wl_resource *resource, uint32_t id)
868{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700869 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400870
871 source = malloc(sizeof *source);
872 if (source == NULL) {
873 wl_resource_post_no_memory(resource);
874 return;
875 }
876
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500877 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400878 source->accept = client_source_accept;
879 source->send = client_source_send;
880 source->cancel = client_source_cancel;
881
882 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500883
Jason Ekstranda85118c2013-06-27 20:17:02 -0500884 source->resource =
885 wl_resource_create(client, &wl_data_source_interface, 1, id);
886 wl_resource_set_implementation(source->resource, &data_source_interface,
887 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400888}
889
890static void unbind_data_device(struct wl_resource *resource)
891{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500892 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400893}
894
895static void
896get_data_device(struct wl_client *client,
897 struct wl_resource *manager_resource,
898 uint32_t id, struct wl_resource *seat_resource)
899{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500900 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400901 struct wl_resource *resource;
902
Jason Ekstranda85118c2013-06-27 20:17:02 -0500903 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +0530904 &wl_data_device_interface,
905 wl_resource_get_version(manager_resource),
906 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700907 if (resource == NULL) {
908 wl_resource_post_no_memory(manager_resource);
909 return;
910 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400911
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700912 wl_list_insert(&seat->drag_resource_list,
913 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500914 wl_resource_set_implementation(resource, &data_device_interface,
915 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400916}
917
918static const struct wl_data_device_manager_interface manager_interface = {
919 create_data_source,
920 get_data_device
921};
922
923static void
924bind_manager(struct wl_client *client,
925 void *data, uint32_t version, uint32_t id)
926{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500927 struct wl_resource *resource;
928
kabeer khan3a510d82014-10-20 11:47:15 +0530929 resource = wl_resource_create(client,
930 &wl_data_device_manager_interface,
931 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700932 if (resource == NULL) {
933 wl_client_post_no_memory(client);
934 return;
935 }
936
937 wl_resource_set_implementation(resource, &manager_interface,
938 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400939}
940
941WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400942wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400943{
Neil Roberts96d790e2013-09-19 17:32:00 +0100944 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -0500945 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400946
Derek Foreman1281a362015-07-31 16:55:32 -0500947 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400948 return;
949
Derek Foreman1281a362015-07-31 16:55:32 -0500950 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100951 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400952 return;
953
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300954 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400955}
956
957WL_EXPORT int
958wl_data_device_manager_init(struct wl_display *display)
959{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400960 if (wl_global_create(display,
kabeer khan3a510d82014-10-20 11:47:15 +0530961 &wl_data_device_manager_interface, 2,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400962 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400963 return -1;
964
965 return 0;
966}