blob: 8cdf22380a81a06365d57d61b29a375454162077 [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"
35
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040036struct weston_drag {
37 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070038 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040039 struct wl_listener data_source_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050040 struct weston_view *focus;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040041 struct wl_resource *focus_resource;
42 struct wl_listener focus_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050043 struct weston_view *icon;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040044 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040045 int32_t dx, dy;
46};
47
Xiong Zhangfd51e7b2013-11-25 18:42:49 +080048struct weston_pointer_drag {
49 struct weston_drag base;
50 struct weston_pointer_grab grab;
51};
52
53struct weston_touch_drag {
54 struct weston_drag base;
55 struct weston_touch_grab grab;
56};
57
Kristian Høgsberg2158a882013-04-18 15:07:39 -040058static void
59data_offer_accept(struct wl_client *client, struct wl_resource *resource,
60 uint32_t serial, const char *mime_type)
61{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070062 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040063
64 /* FIXME: Check that client is currently focused by the input
65 * device that is currently dragging this data source. Should
66 * this be a wl_data_device request? */
67
68 if (offer->source)
69 offer->source->accept(offer->source, serial, mime_type);
70}
71
72static void
73data_offer_receive(struct wl_client *client, struct wl_resource *resource,
74 const char *mime_type, int32_t fd)
75{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070076 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040077
78 if (offer->source)
79 offer->source->send(offer->source, mime_type, fd);
80 else
81 close(fd);
82}
83
84static void
85data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
86{
87 wl_resource_destroy(resource);
88}
89
90static const struct wl_data_offer_interface data_offer_interface = {
91 data_offer_accept,
92 data_offer_receive,
93 data_offer_destroy,
94};
95
96static void
97destroy_data_offer(struct wl_resource *resource)
98{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070099 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400100
101 if (offer->source)
102 wl_list_remove(&offer->source_destroy_listener.link);
103 free(offer);
104}
105
106static void
107destroy_offer_data_source(struct wl_listener *listener, void *data)
108{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700109 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400110
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700111 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400112 source_destroy_listener);
113
114 offer->source = NULL;
115}
116
117static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700118weston_data_source_send_offer(struct weston_data_source *source,
119 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400120{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700121 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400122 char **p;
123
124 offer = malloc(sizeof *offer);
125 if (offer == NULL)
126 return NULL;
127
Jason Ekstranda85118c2013-06-27 20:17:02 -0500128 offer->resource =
129 wl_resource_create(wl_resource_get_client(target),
130 &wl_data_offer_interface, 1, 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700131 if (offer->resource == NULL) {
132 free(offer);
133 return NULL;
134 }
135
Jason Ekstranda85118c2013-06-27 20:17:02 -0500136 wl_resource_set_implementation(offer->resource, &data_offer_interface,
137 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400138
139 offer->source = source;
140 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500141 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400142 &offer->source_destroy_listener);
143
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500144 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400145
146 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500147 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400148
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500149 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400150}
151
152static void
153data_source_offer(struct wl_client *client,
154 struct wl_resource *resource,
155 const char *type)
156{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700157 struct weston_data_source *source =
158 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400159 char **p;
160
161 p = wl_array_add(&source->mime_types, sizeof *p);
162 if (p)
163 *p = strdup(type);
164 if (!p || !*p)
165 wl_resource_post_no_memory(resource);
166}
167
168static void
169data_source_destroy(struct wl_client *client, struct wl_resource *resource)
170{
171 wl_resource_destroy(resource);
172}
173
174static struct wl_data_source_interface data_source_interface = {
175 data_source_offer,
176 data_source_destroy
177};
178
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400179static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800180drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100181 struct weston_pointer *pointer,
182 struct weston_touch *touch,
183 struct weston_surface *es,
184 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400185{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300186 struct weston_layer_entry *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400187 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400188
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800189 assert((pointer != NULL && touch == NULL) ||
190 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500191
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400192 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800193 if (pointer && pointer->sprite &&
194 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400195 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400196 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500197 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400198
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300199 weston_layer_entry_remove(&drag->icon->layer_link);
200 weston_layer_entry_insert(list, &drag->icon->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500201 weston_view_update_transform(drag->icon);
Jason Ekstrandef540082014-06-26 10:37:36 -0700202 pixman_region32_clear(&es->pending.input);
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400203 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400204
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400205 drag->dx += sx;
206 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400207
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800208 /* init to 0 for avoiding a compile warning */
209 fx = fy = 0;
210 if (pointer) {
211 fx = wl_fixed_to_double(pointer->x) + drag->dx;
212 fy = wl_fixed_to_double(pointer->y) + drag->dy;
213 } else if (touch) {
214 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
215 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
216 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600217 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400218}
219
Pekka Paalanen8274d902014-08-06 19:36:51 +0300220static int
221pointer_drag_surface_get_label(struct weston_surface *surface,
222 char *buf, size_t len)
223{
224 return snprintf(buf, len, "pointer drag icon");
225}
226
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400227static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100228pointer_drag_surface_configure(struct weston_surface *es,
229 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800230{
231 struct weston_pointer_drag *drag = es->configure_private;
232 struct weston_pointer *pointer = drag->grab.pointer;
233
234 assert(es->configure == pointer_drag_surface_configure);
235
Jonas Ådahl767d8912013-12-03 22:30:17 +0100236 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800237}
238
Pekka Paalanen8274d902014-08-06 19:36:51 +0300239static int
240touch_drag_surface_get_label(struct weston_surface *surface,
241 char *buf, size_t len)
242{
243 return snprintf(buf, len, "touch drag icon");
244}
245
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800246static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100247touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800248{
249 struct weston_touch_drag *drag = es->configure_private;
250 struct weston_touch *touch = drag->grab.touch;
251
252 assert(es->configure == touch_drag_surface_configure);
253
Jonas Ådahl767d8912013-12-03 22:30:17 +0100254 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800255}
256
257static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400258destroy_drag_focus(struct wl_listener *listener, void *data)
259{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400260 struct weston_drag *drag =
261 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400262
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400263 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400264}
265
266static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800267weston_drag_set_focus(struct weston_drag *drag,
268 struct weston_seat *seat,
269 struct weston_view *view,
270 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400271{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400272 struct wl_resource *resource, *offer = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800273 struct wl_display *display = seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400274 uint32_t serial;
275
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276 if (drag->focus && view && drag->focus->surface == view->surface) {
277 drag->focus = view;
278 return;
279 }
280
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400281 if (drag->focus_resource) {
282 wl_data_device_send_leave(drag->focus_resource);
283 wl_list_remove(&drag->focus_listener.link);
284 drag->focus_resource = NULL;
285 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400286 }
287
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400289 return;
290
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500291 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400293 return;
294
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800295 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400297 if (!resource)
298 return;
299
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400300 serial = wl_display_next_serial(display);
301
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700302 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700303 offer = weston_data_source_send_offer(drag->data_source,
304 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700305 if (offer == NULL)
306 return;
307 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308
Jason Ekstranda7af7042013-10-12 22:38:11 -0500309 wl_data_device_send_enter(resource, serial, view->surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400310 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400311
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400313 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500314 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400315 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400316}
317
318static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400319drag_grab_focus(struct weston_pointer_grab *grab)
320{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800321 struct weston_pointer_drag *drag =
322 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400323 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500324 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400325 wl_fixed_t sx, sy;
326
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 view = weston_compositor_pick_view(pointer->seat->compositor,
328 pointer->x, pointer->y,
329 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800330 if (drag->base.focus != view)
331 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400332}
333
334static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100335drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
336 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400337{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800338 struct weston_pointer_drag *drag =
339 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400340 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400341 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400342 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400343
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100344 weston_pointer_move(pointer, x, y);
345
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800346 if (drag->base.icon) {
347 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
348 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
349 weston_view_set_position(drag->base.icon, fx, fy);
350 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400351 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400352
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800353 if (drag->base.focus_resource) {
354 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500355 pointer->x, pointer->y,
356 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400357
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800358 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400359 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400360}
361
362static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800363data_device_end_drag_grab(struct weston_drag *drag,
364 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400365{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400366 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500367 if (weston_view_is_mapped(drag->icon))
368 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400369
Jason Ekstranda7af7042013-10-12 22:38:11 -0500370 drag->icon->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300371 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700372 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400373 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500374 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400375 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400376
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800377 weston_drag_set_focus(drag, seat, NULL, 0, 0);
378}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400379
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800380static void
381data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
382{
383 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400384
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800385 data_device_end_drag_grab(&drag->base, pointer->seat);
386 weston_pointer_end_grab(pointer);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400387 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400388}
389
390static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400391drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392 uint32_t time, uint32_t button, uint32_t state_w)
393{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800394 struct weston_pointer_drag *drag =
395 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400396 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400397 enum wl_pointer_button_state state = state_w;
398
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800399 if (drag->base.focus_resource &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400400 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400401 state == WL_POINTER_BUTTON_STATE_RELEASED)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800402 wl_data_device_send_drop(drag->base.focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400403
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400404 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400405 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800406 if (drag->base.data_source)
407 wl_list_remove(&drag->base.data_source_listener.link);
408 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400409 }
410}
411
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200412static void
413drag_grab_cancel(struct weston_pointer_grab *grab)
414{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800415 struct weston_pointer_drag *drag =
416 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200417
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800418 if (drag->base.data_source)
419 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200420
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800421 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200422}
423
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800424static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400425 drag_grab_focus,
426 drag_grab_motion,
427 drag_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200428 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400429};
430
431static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800432drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
433 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400434{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800435}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400436
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800437static void
438data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
439{
440 struct weston_touch *touch = drag->grab.touch;
441
442 data_device_end_drag_grab(&drag->base, touch->seat);
443 weston_touch_end_grab(touch);
444 free(drag);
445}
446
447static void
448drag_grab_touch_up(struct weston_touch_grab *grab,
449 uint32_t time, int touch_id)
450{
451 struct weston_touch_drag *touch_drag =
452 container_of(grab, struct weston_touch_drag, grab);
453 struct weston_touch *touch = grab->touch;
454
455 if (touch_id != touch->grab_touch_id)
456 return;
457
458 if (touch_drag->base.focus_resource)
459 wl_data_device_send_drop(touch_drag->base.focus_resource);
460 if (touch_drag->base.data_source)
461 wl_list_remove(&touch_drag->base.data_source_listener.link);
462 data_device_end_touch_drag_grab(touch_drag);
463}
464
465static void
466drag_grab_touch_focus(struct weston_touch_drag *drag)
467{
468 struct weston_touch *touch = drag->grab.touch;
469 struct weston_view *view;
470 wl_fixed_t view_x, view_y;
471
472 view = weston_compositor_pick_view(touch->seat->compositor,
473 touch->grab_x, touch->grab_y,
474 &view_x, &view_y);
475 if (drag->base.focus != view)
476 weston_drag_set_focus(&drag->base, touch->seat,
477 view, view_x, view_y);
478}
479
480static void
481drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
482 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
483{
484 struct weston_touch_drag *touch_drag =
485 container_of(grab, struct weston_touch_drag, grab);
486 struct weston_touch *touch = grab->touch;
487 wl_fixed_t view_x, view_y;
488 float fx, fy;
489
490 if (touch_id != touch->grab_touch_id)
491 return;
492
493 drag_grab_touch_focus(touch_drag);
494 if (touch_drag->base.icon) {
495 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
496 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
497 weston_view_set_position(touch_drag->base.icon, fx, fy);
498 weston_view_schedule_repaint(touch_drag->base.icon);
499 }
500
501 if (touch_drag->base.focus_resource) {
502 weston_view_from_global_fixed(touch_drag->base.focus,
503 touch->grab_x, touch->grab_y,
504 &view_x, &view_y);
505 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
506 view_x, view_y);
507 }
508}
509
510static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200511drag_grab_touch_frame(struct weston_touch_grab *grab)
512{
513}
514
515static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800516drag_grab_touch_cancel(struct weston_touch_grab *grab)
517{
518 struct weston_touch_drag *touch_drag =
519 container_of(grab, struct weston_touch_drag, grab);
520
521 if (touch_drag->base.data_source)
522 wl_list_remove(&touch_drag->base.data_source_listener.link);
523 data_device_end_touch_drag_grab(touch_drag);
524}
525
526static const struct weston_touch_grab_interface touch_drag_grab_interface = {
527 drag_grab_touch_down,
528 drag_grab_touch_up,
529 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200530 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800531 drag_grab_touch_cancel
532};
533
534static void
535destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
536{
537 struct weston_pointer_drag *drag = container_of(listener,
538 struct weston_pointer_drag, base.data_source_listener);
539
540 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400541}
542
543static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400544handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400545{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400546 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400547 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400548
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400549 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400550}
551
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700552WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800553weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700554 struct weston_data_source *source,
555 struct weston_surface *icon,
556 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400557{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800558 struct weston_pointer_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400559
Peter Huttererf3d62272013-08-08 11:57:05 +1000560 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700561 if (drag == NULL)
562 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400563
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800564 drag->grab.interface = &pointer_drag_grab_interface;
565 drag->base.client = client;
566 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400567
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400568 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800569 drag->base.icon = weston_view_create(icon);
570 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500571 free(drag);
572 return -1;
573 }
574
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800575 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500576 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800577 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400578
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800579 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400580 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300581 weston_surface_set_label_func(icon,
582 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800584 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500585 }
586
587 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800588 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800590 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400591 }
592
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800593 weston_pointer_set_focus(pointer, NULL,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400594 wl_fixed_from_int(0), wl_fixed_from_int(0));
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);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700664 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700665 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700666 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700667 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800668 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700669
Jason Ekstrand8202d722014-06-24 21:19:24 -0700670 is_pointer_grab = seat->pointer &&
671 seat->pointer->button_count == 1 &&
672 seat->pointer->grab_serial == serial &&
673 seat->pointer->focus &&
674 seat->pointer->focus->surface == origin;
675
676 is_touch_grab = seat->touch &&
677 seat->touch->num_tp == 1 &&
678 seat->touch->grab_serial == serial &&
679 seat->touch->focus &&
680 seat->touch->focus->surface == origin;
681
682 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700683 return;
684
685 /* FIXME: Check that the data source type array isn't empty. */
686
687 if (source_resource)
688 source = wl_resource_get_user_data(source_resource);
689 if (icon_resource)
690 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +0300691
692 if (icon) {
693 if (weston_surface_set_role(icon, "wl_data_device-icon",
694 resource,
695 WL_DATA_DEVICE_ERROR_ROLE) < 0)
696 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700697 }
698
Jason Ekstrand8202d722014-06-24 21:19:24 -0700699 if (is_pointer_grab)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800700 ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700701 else if (is_touch_grab)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800702 ret = weston_touch_start_drag(seat->touch, source, icon, client);
703
704 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700705 wl_resource_post_no_memory(resource);
706}
707
708static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400709destroy_selection_data_source(struct wl_listener *listener, void *data)
710{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400711 struct weston_seat *seat = container_of(listener, struct weston_seat,
712 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400713 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100714 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400715
716 seat->selection_data_source = NULL;
717
718 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100719 focus = seat->keyboard->focus;
720 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500721 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100722 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400723 if (data_device)
724 wl_data_device_send_selection(data_device, NULL);
725 }
726
727 wl_signal_emit(&seat->selection_signal, seat);
728}
729
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300730/** \brief Send the selection to the specified client
731 *
732 * This function creates a new wl_data_offer if there is a wl_data_source
733 * currently set as the selection and sends it to the specified client,
734 * followed by the wl_data_device.selection() event.
735 * If there is no current selection the wl_data_device.selection() event
736 * will carry a NULL wl_data_offer.
737 *
738 * If the client does not have a wl_data_device for the specified seat
739 * nothing will be done.
740 *
741 * \param seat The seat owning the wl_data_device used to send the events.
742 * \param client The client to which to send the selection.
743 */
744WL_EXPORT void
745weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
746{
747 struct wl_resource *data_device, *offer;
748
Giulio Camuffod46bb012015-05-01 12:59:36 +0300749 wl_resource_for_each(data_device, &seat->drag_resource_list) {
750 if (wl_resource_get_client(data_device) != client)
751 continue;
752
753 if (seat->selection_data_source) {
754 offer = weston_data_source_send_offer(seat->selection_data_source,
755 data_device);
756 wl_data_device_send_selection(data_device, offer);
757 } else {
758 wl_data_device_send_selection(data_device, NULL);
759 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300760 }
761}
762
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400763WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400764weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700765 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400766{
Neil Roberts96d790e2013-09-19 17:32:00 +0100767 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400768
769 if (seat->selection_data_source &&
770 seat->selection_serial - serial < UINT32_MAX / 2)
771 return;
772
773 if (seat->selection_data_source) {
774 seat->selection_data_source->cancel(seat->selection_data_source);
775 wl_list_remove(&seat->selection_data_source_listener.link);
776 seat->selection_data_source = NULL;
777 }
778
779 seat->selection_data_source = source;
780 seat->selection_serial = serial;
781
782 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100783 focus = seat->keyboard->focus;
784 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300785 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400786 }
787
788 wl_signal_emit(&seat->selection_signal, seat);
789
790 if (source) {
791 seat->selection_data_source_listener.notify =
792 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500793 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400794 &seat->selection_data_source_listener);
795 }
796}
797
798static void
799data_device_set_selection(struct wl_client *client,
800 struct wl_resource *resource,
801 struct wl_resource *source_resource, uint32_t serial)
802{
803 if (!source_resource)
804 return;
805
806 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500807 weston_seat_set_selection(wl_resource_get_user_data(resource),
808 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400809 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400810}
kabeer khan3a510d82014-10-20 11:47:15 +0530811static void
812data_device_release(struct wl_client *client, struct wl_resource *resource)
813{
814 wl_resource_destroy(resource);
815}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400816
817static const struct wl_data_device_interface data_device_interface = {
818 data_device_start_drag,
819 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +0530820 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400821};
822
823static void
824destroy_data_source(struct wl_resource *resource)
825{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700826 struct weston_data_source *source =
827 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400828 char **p;
829
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500830 wl_signal_emit(&source->destroy_signal, source);
831
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400832 wl_array_for_each(p, &source->mime_types)
833 free(*p);
834
835 wl_array_release(&source->mime_types);
836
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400837 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400838}
839
840static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700841client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400842 uint32_t time, const char *mime_type)
843{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500844 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400845}
846
847static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700848client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400849 const char *mime_type, int32_t fd)
850{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500851 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400852 close(fd);
853}
854
855static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700856client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400857{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500858 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400859}
860
861static void
862create_data_source(struct wl_client *client,
863 struct wl_resource *resource, uint32_t id)
864{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700865 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400866
867 source = malloc(sizeof *source);
868 if (source == NULL) {
869 wl_resource_post_no_memory(resource);
870 return;
871 }
872
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500873 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400874 source->accept = client_source_accept;
875 source->send = client_source_send;
876 source->cancel = client_source_cancel;
877
878 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500879
Jason Ekstranda85118c2013-06-27 20:17:02 -0500880 source->resource =
881 wl_resource_create(client, &wl_data_source_interface, 1, id);
882 wl_resource_set_implementation(source->resource, &data_source_interface,
883 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400884}
885
886static void unbind_data_device(struct wl_resource *resource)
887{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500888 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400889}
890
891static void
892get_data_device(struct wl_client *client,
893 struct wl_resource *manager_resource,
894 uint32_t id, struct wl_resource *seat_resource)
895{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500896 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400897 struct wl_resource *resource;
898
Jason Ekstranda85118c2013-06-27 20:17:02 -0500899 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +0530900 &wl_data_device_interface,
901 wl_resource_get_version(manager_resource),
902 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700903 if (resource == NULL) {
904 wl_resource_post_no_memory(manager_resource);
905 return;
906 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400907
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700908 wl_list_insert(&seat->drag_resource_list,
909 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500910 wl_resource_set_implementation(resource, &data_device_interface,
911 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400912}
913
914static const struct wl_data_device_manager_interface manager_interface = {
915 create_data_source,
916 get_data_device
917};
918
919static void
920bind_manager(struct wl_client *client,
921 void *data, uint32_t version, uint32_t id)
922{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500923 struct wl_resource *resource;
924
kabeer khan3a510d82014-10-20 11:47:15 +0530925 resource = wl_resource_create(client,
926 &wl_data_device_manager_interface,
927 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700928 if (resource == NULL) {
929 wl_client_post_no_memory(client);
930 return;
931 }
932
933 wl_resource_set_implementation(resource, &manager_interface,
934 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400935}
936
937WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400938wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400939{
Neil Roberts96d790e2013-09-19 17:32:00 +0100940 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400941
942 if (!seat->keyboard)
943 return;
944
Neil Roberts96d790e2013-09-19 17:32:00 +0100945 focus = seat->keyboard->focus;
946 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400947 return;
948
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300949 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400950}
951
952WL_EXPORT int
953wl_data_device_manager_init(struct wl_display *display)
954{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400955 if (wl_global_create(display,
kabeer khan3a510d82014-10-20 11:47:15 +0530956 &wl_data_device_manager_interface, 2,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400957 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400958 return -1;
959
960 return 0;
961}