blob: 545a895d8f05ec2f4f4496956abe27c38cb58b88 [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2011 Kristian Høgsberg
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg2158a882013-04-18 15:07:39 -040011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg2158a882013-04-18 15:07:39 -040024 */
25
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010026#include "config.h"
27
Kristian Høgsberg2158a882013-04-18 15:07:39 -040028#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31#include <stdio.h>
Jason Ekstranda7af7042013-10-12 22:38:11 -050032#include <assert.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040033
34#include "compositor.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070035#include "shared/helpers.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040036
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040037struct weston_drag {
38 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070039 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040040 struct wl_listener data_source_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050041 struct weston_view *focus;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040042 struct wl_resource *focus_resource;
43 struct wl_listener focus_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050044 struct weston_view *icon;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040045 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040046 int32_t dx, dy;
47};
48
Xiong Zhangfd51e7b2013-11-25 18:42:49 +080049struct weston_pointer_drag {
50 struct weston_drag base;
51 struct weston_pointer_grab grab;
52};
53
54struct weston_touch_drag {
55 struct weston_drag base;
56 struct weston_touch_grab grab;
57};
58
Kristian Høgsberg2158a882013-04-18 15:07:39 -040059static void
60data_offer_accept(struct wl_client *client, struct wl_resource *resource,
61 uint32_t serial, const char *mime_type)
62{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070063 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040064
65 /* FIXME: Check that client is currently focused by the input
66 * device that is currently dragging this data source. Should
67 * this be a wl_data_device request? */
68
69 if (offer->source)
70 offer->source->accept(offer->source, serial, mime_type);
71}
72
73static void
74data_offer_receive(struct wl_client *client, struct wl_resource *resource,
75 const char *mime_type, int32_t fd)
76{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070077 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040078
79 if (offer->source)
80 offer->source->send(offer->source, mime_type, fd);
81 else
82 close(fd);
83}
84
85static void
86data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
87{
88 wl_resource_destroy(resource);
89}
90
91static const struct wl_data_offer_interface data_offer_interface = {
92 data_offer_accept,
93 data_offer_receive,
94 data_offer_destroy,
95};
96
97static void
98destroy_data_offer(struct wl_resource *resource)
99{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700100 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400101
102 if (offer->source)
103 wl_list_remove(&offer->source_destroy_listener.link);
104 free(offer);
105}
106
107static void
108destroy_offer_data_source(struct wl_listener *listener, void *data)
109{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700110 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400111
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700112 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400113 source_destroy_listener);
114
115 offer->source = NULL;
116}
117
118static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700119weston_data_source_send_offer(struct weston_data_source *source,
120 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400121{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700122 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400123 char **p;
124
125 offer = malloc(sizeof *offer);
126 if (offer == NULL)
127 return NULL;
128
Jason Ekstranda85118c2013-06-27 20:17:02 -0500129 offer->resource =
130 wl_resource_create(wl_resource_get_client(target),
131 &wl_data_offer_interface, 1, 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700132 if (offer->resource == NULL) {
133 free(offer);
134 return NULL;
135 }
136
Jason Ekstranda85118c2013-06-27 20:17:02 -0500137 wl_resource_set_implementation(offer->resource, &data_offer_interface,
138 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400139
140 offer->source = source;
141 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500142 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400143 &offer->source_destroy_listener);
144
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500145 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146
147 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500148 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400149
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500150 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400151}
152
153static void
154data_source_offer(struct wl_client *client,
155 struct wl_resource *resource,
156 const char *type)
157{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700158 struct weston_data_source *source =
159 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400160 char **p;
161
162 p = wl_array_add(&source->mime_types, sizeof *p);
163 if (p)
164 *p = strdup(type);
165 if (!p || !*p)
166 wl_resource_post_no_memory(resource);
167}
168
169static void
170data_source_destroy(struct wl_client *client, struct wl_resource *resource)
171{
172 wl_resource_destroy(resource);
173}
174
175static struct wl_data_source_interface data_source_interface = {
176 data_source_offer,
177 data_source_destroy
178};
179
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400180static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800181drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100182 struct weston_pointer *pointer,
183 struct weston_touch *touch,
184 struct weston_surface *es,
185 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400186{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300187 struct weston_layer_entry *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400188 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400189
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800190 assert((pointer != NULL && touch == NULL) ||
191 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500192
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400193 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800194 if (pointer && pointer->sprite &&
195 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400196 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400197 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500198 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400199
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300200 weston_layer_entry_remove(&drag->icon->layer_link);
201 weston_layer_entry_insert(list, &drag->icon->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500202 weston_view_update_transform(drag->icon);
Jason Ekstrandef540082014-06-26 10:37:36 -0700203 pixman_region32_clear(&es->pending.input);
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400204 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400205
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400206 drag->dx += sx;
207 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400208
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800209 /* init to 0 for avoiding a compile warning */
210 fx = fy = 0;
211 if (pointer) {
212 fx = wl_fixed_to_double(pointer->x) + drag->dx;
213 fy = wl_fixed_to_double(pointer->y) + drag->dy;
214 } else if (touch) {
215 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
216 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
217 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600218 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400219}
220
Pekka Paalanen8274d902014-08-06 19:36:51 +0300221static int
222pointer_drag_surface_get_label(struct weston_surface *surface,
223 char *buf, size_t len)
224{
225 return snprintf(buf, len, "pointer drag icon");
226}
227
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400228static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100229pointer_drag_surface_configure(struct weston_surface *es,
230 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800231{
232 struct weston_pointer_drag *drag = es->configure_private;
233 struct weston_pointer *pointer = drag->grab.pointer;
234
235 assert(es->configure == pointer_drag_surface_configure);
236
Jonas Ådahl767d8912013-12-03 22:30:17 +0100237 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800238}
239
Pekka Paalanen8274d902014-08-06 19:36:51 +0300240static int
241touch_drag_surface_get_label(struct weston_surface *surface,
242 char *buf, size_t len)
243{
244 return snprintf(buf, len, "touch drag icon");
245}
246
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800247static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100248touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800249{
250 struct weston_touch_drag *drag = es->configure_private;
251 struct weston_touch *touch = drag->grab.touch;
252
253 assert(es->configure == touch_drag_surface_configure);
254
Jonas Ådahl767d8912013-12-03 22:30:17 +0100255 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800256}
257
258static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400259destroy_drag_focus(struct wl_listener *listener, void *data)
260{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400261 struct weston_drag *drag =
262 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400263
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400264 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400265}
266
267static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800268weston_drag_set_focus(struct weston_drag *drag,
269 struct weston_seat *seat,
270 struct weston_view *view,
271 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400272{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400273 struct wl_resource *resource, *offer = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800274 struct wl_display *display = seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400275 uint32_t serial;
276
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277 if (drag->focus && view && drag->focus->surface == view->surface) {
278 drag->focus = view;
279 return;
280 }
281
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400282 if (drag->focus_resource) {
283 wl_data_device_send_leave(drag->focus_resource);
284 wl_list_remove(&drag->focus_listener.link);
285 drag->focus_resource = NULL;
286 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400287 }
288
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400290 return;
291
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500292 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400294 return;
295
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800296 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400298 if (!resource)
299 return;
300
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400301 serial = wl_display_next_serial(display);
302
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700303 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700304 offer = weston_data_source_send_offer(drag->data_source,
305 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700306 if (offer == NULL)
307 return;
308 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400309
Jason Ekstranda7af7042013-10-12 22:38:11 -0500310 wl_data_device_send_enter(resource, serial, view->surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400311 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400312
Jason Ekstranda7af7042013-10-12 22:38:11 -0500313 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400314 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500315 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400316 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400317}
318
319static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400320drag_grab_focus(struct weston_pointer_grab *grab)
321{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800322 struct weston_pointer_drag *drag =
323 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400324 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500325 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400326 wl_fixed_t sx, sy;
327
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 view = weston_compositor_pick_view(pointer->seat->compositor,
329 pointer->x, pointer->y,
330 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800331 if (drag->base.focus != view)
332 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400333}
334
335static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100336drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200337 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800339 struct weston_pointer_drag *drag =
340 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400341 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400342 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400343 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400344
Jonas Ådahld2510102014-10-05 21:39:14 +0200345 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100346
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800347 if (drag->base.icon) {
348 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
349 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
350 weston_view_set_position(drag->base.icon, fx, fy);
351 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400352 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400353
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800354 if (drag->base.focus_resource) {
355 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500356 pointer->x, pointer->y,
357 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400358
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800359 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400360 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400361}
362
363static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800364data_device_end_drag_grab(struct weston_drag *drag,
365 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400366{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400367 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368 if (weston_view_is_mapped(drag->icon))
369 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400370
Jason Ekstranda7af7042013-10-12 22:38:11 -0500371 drag->icon->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300372 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700373 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400374 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400376 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400377
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800378 weston_drag_set_focus(drag, seat, NULL, 0, 0);
379}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400380
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800381static void
382data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
383{
384 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800386 data_device_end_drag_grab(&drag->base, pointer->seat);
387 weston_pointer_end_grab(pointer);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400388 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400389}
390
391static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400392drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400393 uint32_t time, uint32_t button, uint32_t state_w)
394{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800395 struct weston_pointer_drag *drag =
396 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400397 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398 enum wl_pointer_button_state state = state_w;
399
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800400 if (drag->base.focus_resource &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400401 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402 state == WL_POINTER_BUTTON_STATE_RELEASED)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800403 wl_data_device_send_drop(drag->base.focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400405 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800407 if (drag->base.data_source)
408 wl_list_remove(&drag->base.data_source_listener.link);
409 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400410 }
411}
412
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200413static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200414drag_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000415 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200416{
417}
418
419static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000420drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
421{
422}
423
424static void
425drag_grab_frame(struct weston_pointer_grab *grab)
426{
427}
428
429static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200430drag_grab_cancel(struct weston_pointer_grab *grab)
431{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800432 struct weston_pointer_drag *drag =
433 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200434
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800435 if (drag->base.data_source)
436 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200437
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800438 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200439}
440
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800441static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442 drag_grab_focus,
443 drag_grab_motion,
444 drag_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200445 drag_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000446 drag_grab_axis_source,
447 drag_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200448 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400449};
450
451static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800452drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
453 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400454{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800455}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400456
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800457static void
458data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
459{
460 struct weston_touch *touch = drag->grab.touch;
461
462 data_device_end_drag_grab(&drag->base, touch->seat);
463 weston_touch_end_grab(touch);
464 free(drag);
465}
466
467static void
468drag_grab_touch_up(struct weston_touch_grab *grab,
469 uint32_t time, int touch_id)
470{
471 struct weston_touch_drag *touch_drag =
472 container_of(grab, struct weston_touch_drag, grab);
473 struct weston_touch *touch = grab->touch;
474
475 if (touch_id != touch->grab_touch_id)
476 return;
477
478 if (touch_drag->base.focus_resource)
479 wl_data_device_send_drop(touch_drag->base.focus_resource);
480 if (touch_drag->base.data_source)
481 wl_list_remove(&touch_drag->base.data_source_listener.link);
482 data_device_end_touch_drag_grab(touch_drag);
483}
484
485static void
486drag_grab_touch_focus(struct weston_touch_drag *drag)
487{
488 struct weston_touch *touch = drag->grab.touch;
489 struct weston_view *view;
490 wl_fixed_t view_x, view_y;
491
492 view = weston_compositor_pick_view(touch->seat->compositor,
493 touch->grab_x, touch->grab_y,
494 &view_x, &view_y);
495 if (drag->base.focus != view)
496 weston_drag_set_focus(&drag->base, touch->seat,
497 view, view_x, view_y);
498}
499
500static void
501drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300502 int touch_id, wl_fixed_t x, wl_fixed_t y)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800503{
504 struct weston_touch_drag *touch_drag =
505 container_of(grab, struct weston_touch_drag, grab);
506 struct weston_touch *touch = grab->touch;
507 wl_fixed_t view_x, view_y;
508 float fx, fy;
509
510 if (touch_id != touch->grab_touch_id)
511 return;
512
513 drag_grab_touch_focus(touch_drag);
514 if (touch_drag->base.icon) {
515 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
516 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
517 weston_view_set_position(touch_drag->base.icon, fx, fy);
518 weston_view_schedule_repaint(touch_drag->base.icon);
519 }
520
521 if (touch_drag->base.focus_resource) {
522 weston_view_from_global_fixed(touch_drag->base.focus,
523 touch->grab_x, touch->grab_y,
524 &view_x, &view_y);
525 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
526 view_x, view_y);
527 }
528}
529
530static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200531drag_grab_touch_frame(struct weston_touch_grab *grab)
532{
533}
534
535static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800536drag_grab_touch_cancel(struct weston_touch_grab *grab)
537{
538 struct weston_touch_drag *touch_drag =
539 container_of(grab, struct weston_touch_drag, grab);
540
541 if (touch_drag->base.data_source)
542 wl_list_remove(&touch_drag->base.data_source_listener.link);
543 data_device_end_touch_drag_grab(touch_drag);
544}
545
546static const struct weston_touch_grab_interface touch_drag_grab_interface = {
547 drag_grab_touch_down,
548 drag_grab_touch_up,
549 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200550 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800551 drag_grab_touch_cancel
552};
553
554static void
555destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
556{
557 struct weston_pointer_drag *drag = container_of(listener,
558 struct weston_pointer_drag, base.data_source_listener);
559
560 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400561}
562
563static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400564handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400565{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400566 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400567 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400568
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400569 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400570}
571
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700572WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800573weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700574 struct weston_data_source *source,
575 struct weston_surface *icon,
576 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400577{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800578 struct weston_pointer_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400579
Peter Huttererf3d62272013-08-08 11:57:05 +1000580 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700581 if (drag == NULL)
582 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400583
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800584 drag->grab.interface = &pointer_drag_grab_interface;
585 drag->base.client = client;
586 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400587
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400588 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800589 drag->base.icon = weston_view_create(icon);
590 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500591 free(drag);
592 return -1;
593 }
594
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800595 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500596 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800597 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400598
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800599 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400600 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300601 weston_surface_set_label_func(icon,
602 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500603 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800604 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500605 }
606
607 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800608 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500609 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800610 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400611 }
612
Derek Foremanf9318d12015-05-11 15:40:11 -0500613 weston_pointer_clear_focus(pointer);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800614 weston_pointer_start_grab(pointer, &drag->grab);
615
616 return 0;
617}
618
619static void
620destroy_touch_data_device_source(struct wl_listener *listener, void *data)
621{
622 struct weston_touch_drag *drag = container_of(listener,
623 struct weston_touch_drag, base.data_source_listener);
624
625 data_device_end_touch_drag_grab(drag);
626}
627
628WL_EXPORT int
629weston_touch_start_drag(struct weston_touch *touch,
630 struct weston_data_source *source,
631 struct weston_surface *icon,
632 struct wl_client *client)
633{
634 struct weston_touch_drag *drag;
635
636 drag = zalloc(sizeof *drag);
637 if (drag == NULL)
638 return -1;
639
640 drag->grab.interface = &touch_drag_grab_interface;
641 drag->base.client = client;
642 drag->base.data_source = source;
643
644 if (icon) {
645 drag->base.icon = weston_view_create(icon);
646 if (drag->base.icon == NULL) {
647 free(drag);
648 return -1;
649 }
650
651 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
652 wl_signal_add(&icon->destroy_signal,
653 &drag->base.icon_destroy_listener);
654
655 icon->configure = touch_drag_surface_configure;
656 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300657 weston_surface_set_label_func(icon,
658 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800659 } else {
660 drag->base.icon = NULL;
661 }
662
663 if (source) {
664 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
665 wl_signal_add(&source->destroy_signal,
666 &drag->base.data_source_listener);
667 }
668
669 weston_touch_start_grab(touch, &drag->grab);
670
671 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700672
673 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400674}
675
676static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700677data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
678 struct wl_resource *source_resource,
679 struct wl_resource *origin_resource,
680 struct wl_resource *icon_resource, uint32_t serial)
681{
682 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -0500683 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
684 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700685 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700686 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700687 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700688 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800689 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700690
Derek Foreman1281a362015-07-31 16:55:32 -0500691 is_pointer_grab = pointer &&
692 pointer->button_count == 1 &&
693 pointer->grab_serial == serial &&
694 pointer->focus &&
695 pointer->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700696
Derek Foreman1281a362015-07-31 16:55:32 -0500697 is_touch_grab = touch &&
698 touch->num_tp == 1 &&
699 touch->grab_serial == serial &&
700 touch->focus &&
701 touch->focus->surface == origin;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700702
703 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700704 return;
705
706 /* FIXME: Check that the data source type array isn't empty. */
707
708 if (source_resource)
709 source = wl_resource_get_user_data(source_resource);
710 if (icon_resource)
711 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +0300712
713 if (icon) {
714 if (weston_surface_set_role(icon, "wl_data_device-icon",
715 resource,
716 WL_DATA_DEVICE_ERROR_ROLE) < 0)
717 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700718 }
719
Jason Ekstrand8202d722014-06-24 21:19:24 -0700720 if (is_pointer_grab)
Derek Foreman1281a362015-07-31 16:55:32 -0500721 ret = weston_pointer_start_drag(pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700722 else if (is_touch_grab)
Derek Foreman1281a362015-07-31 16:55:32 -0500723 ret = weston_touch_start_drag(touch, source, icon, client);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800724
725 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700726 wl_resource_post_no_memory(resource);
727}
728
729static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400730destroy_selection_data_source(struct wl_listener *listener, void *data)
731{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400732 struct weston_seat *seat = container_of(listener, struct weston_seat,
733 selection_data_source_listener);
Derek Foreman1281a362015-07-31 16:55:32 -0500734 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400735 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100736 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400737
738 seat->selection_data_source = NULL;
739
Derek Foreman1281a362015-07-31 16:55:32 -0500740 if (keyboard)
741 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100742 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500743 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100744 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400745 if (data_device)
746 wl_data_device_send_selection(data_device, NULL);
747 }
748
749 wl_signal_emit(&seat->selection_signal, seat);
750}
751
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300752/** \brief Send the selection to the specified client
753 *
754 * This function creates a new wl_data_offer if there is a wl_data_source
755 * currently set as the selection and sends it to the specified client,
756 * followed by the wl_data_device.selection() event.
757 * If there is no current selection the wl_data_device.selection() event
758 * will carry a NULL wl_data_offer.
759 *
760 * If the client does not have a wl_data_device for the specified seat
761 * nothing will be done.
762 *
763 * \param seat The seat owning the wl_data_device used to send the events.
764 * \param client The client to which to send the selection.
765 */
766WL_EXPORT void
767weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
768{
769 struct wl_resource *data_device, *offer;
770
Giulio Camuffod46bb012015-05-01 12:59:36 +0300771 wl_resource_for_each(data_device, &seat->drag_resource_list) {
772 if (wl_resource_get_client(data_device) != client)
773 continue;
774
775 if (seat->selection_data_source) {
776 offer = weston_data_source_send_offer(seat->selection_data_source,
777 data_device);
778 wl_data_device_send_selection(data_device, offer);
779 } else {
780 wl_data_device_send_selection(data_device, NULL);
781 }
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300782 }
783}
784
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400785WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400786weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700787 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400788{
Neil Roberts96d790e2013-09-19 17:32:00 +0100789 struct weston_surface *focus = NULL;
Derek Foreman1281a362015-07-31 16:55:32 -0500790 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400791
792 if (seat->selection_data_source &&
793 seat->selection_serial - serial < UINT32_MAX / 2)
794 return;
795
796 if (seat->selection_data_source) {
797 seat->selection_data_source->cancel(seat->selection_data_source);
798 wl_list_remove(&seat->selection_data_source_listener.link);
799 seat->selection_data_source = NULL;
800 }
801
802 seat->selection_data_source = source;
803 seat->selection_serial = serial;
804
Derek Foreman1281a362015-07-31 16:55:32 -0500805 if (keyboard)
806 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100807 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300808 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400809 }
810
811 wl_signal_emit(&seat->selection_signal, seat);
812
813 if (source) {
814 seat->selection_data_source_listener.notify =
815 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500816 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400817 &seat->selection_data_source_listener);
818 }
819}
820
821static void
822data_device_set_selection(struct wl_client *client,
823 struct wl_resource *resource,
824 struct wl_resource *source_resource, uint32_t serial)
825{
826 if (!source_resource)
827 return;
828
829 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500830 weston_seat_set_selection(wl_resource_get_user_data(resource),
831 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400832 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400833}
kabeer khan3a510d82014-10-20 11:47:15 +0530834static void
835data_device_release(struct wl_client *client, struct wl_resource *resource)
836{
837 wl_resource_destroy(resource);
838}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400839
840static const struct wl_data_device_interface data_device_interface = {
841 data_device_start_drag,
842 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +0530843 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400844};
845
846static void
847destroy_data_source(struct wl_resource *resource)
848{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700849 struct weston_data_source *source =
850 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400851 char **p;
852
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500853 wl_signal_emit(&source->destroy_signal, source);
854
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400855 wl_array_for_each(p, &source->mime_types)
856 free(*p);
857
858 wl_array_release(&source->mime_types);
859
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400860 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400861}
862
863static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700864client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400865 uint32_t time, const char *mime_type)
866{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500867 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400868}
869
870static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700871client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400872 const char *mime_type, int32_t fd)
873{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500874 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400875 close(fd);
876}
877
878static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700879client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400880{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500881 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400882}
883
884static void
885create_data_source(struct wl_client *client,
886 struct wl_resource *resource, uint32_t id)
887{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700888 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400889
890 source = malloc(sizeof *source);
891 if (source == NULL) {
892 wl_resource_post_no_memory(resource);
893 return;
894 }
895
cpaul@redhat.comc9f8f8a2016-01-05 11:18:30 -0500896 source->resource =
897 wl_resource_create(client, &wl_data_source_interface, 1, id);
898 if (source->resource == NULL) {
899 free(source);
900 wl_resource_post_no_memory(resource);
901 return;
902 }
903
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500904 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400905 source->accept = client_source_accept;
906 source->send = client_source_send;
907 source->cancel = client_source_cancel;
908
909 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500910
Jason Ekstranda85118c2013-06-27 20:17:02 -0500911 wl_resource_set_implementation(source->resource, &data_source_interface,
912 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400913}
914
915static void unbind_data_device(struct wl_resource *resource)
916{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500917 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400918}
919
920static void
921get_data_device(struct wl_client *client,
922 struct wl_resource *manager_resource,
923 uint32_t id, struct wl_resource *seat_resource)
924{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500925 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400926 struct wl_resource *resource;
927
Jason Ekstranda85118c2013-06-27 20:17:02 -0500928 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +0530929 &wl_data_device_interface,
930 wl_resource_get_version(manager_resource),
931 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700932 if (resource == NULL) {
933 wl_resource_post_no_memory(manager_resource);
934 return;
935 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400936
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700937 wl_list_insert(&seat->drag_resource_list,
938 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500939 wl_resource_set_implementation(resource, &data_device_interface,
940 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400941}
942
943static const struct wl_data_device_manager_interface manager_interface = {
944 create_data_source,
945 get_data_device
946};
947
948static void
949bind_manager(struct wl_client *client,
950 void *data, uint32_t version, uint32_t id)
951{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500952 struct wl_resource *resource;
953
kabeer khan3a510d82014-10-20 11:47:15 +0530954 resource = wl_resource_create(client,
955 &wl_data_device_manager_interface,
956 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700957 if (resource == NULL) {
958 wl_client_post_no_memory(client);
959 return;
960 }
961
962 wl_resource_set_implementation(resource, &manager_interface,
963 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400964}
965
966WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400967wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400968{
Neil Roberts96d790e2013-09-19 17:32:00 +0100969 struct weston_surface *focus;
Derek Foreman1281a362015-07-31 16:55:32 -0500970 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400971
Derek Foreman1281a362015-07-31 16:55:32 -0500972 if (!keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400973 return;
974
Derek Foreman1281a362015-07-31 16:55:32 -0500975 focus = keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +0100976 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400977 return;
978
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300979 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400980}
981
982WL_EXPORT int
983wl_data_device_manager_init(struct wl_display *display)
984{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400985 if (wl_global_create(display,
kabeer khan3a510d82014-10-20 11:47:15 +0530986 &wl_data_device_manager_interface, 2,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400987 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400988 return -1;
989
990 return 0;
991}