blob: 858235f6addfb11f70ccd61a75478c906ff613ed [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2011 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010023#include "config.h"
24
Kristian Høgsberg2158a882013-04-18 15:07:39 -040025#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdio.h>
29
30#include "compositor.h"
31
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040032struct weston_drag {
33 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070034 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040035 struct wl_listener data_source_listener;
36 struct weston_surface *focus;
37 struct wl_resource *focus_resource;
38 struct wl_listener focus_listener;
39 struct weston_pointer_grab grab;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040040 struct weston_surface *icon;
41 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040042 int32_t dx, dy;
43};
44
Kristian Høgsberg2158a882013-04-18 15:07:39 -040045static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -040046empty_region(pixman_region32_t *region)
47{
48 pixman_region32_fini(region);
49 pixman_region32_init(region);
50}
51
52static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -040053data_offer_accept(struct wl_client *client, struct wl_resource *resource,
54 uint32_t serial, const char *mime_type)
55{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070056 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040057
58 /* FIXME: Check that client is currently focused by the input
59 * device that is currently dragging this data source. Should
60 * this be a wl_data_device request? */
61
62 if (offer->source)
63 offer->source->accept(offer->source, serial, mime_type);
64}
65
66static void
67data_offer_receive(struct wl_client *client, struct wl_resource *resource,
68 const char *mime_type, int32_t fd)
69{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070070 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040071
72 if (offer->source)
73 offer->source->send(offer->source, mime_type, fd);
74 else
75 close(fd);
76}
77
78static void
79data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
80{
81 wl_resource_destroy(resource);
82}
83
84static const struct wl_data_offer_interface data_offer_interface = {
85 data_offer_accept,
86 data_offer_receive,
87 data_offer_destroy,
88};
89
90static void
91destroy_data_offer(struct wl_resource *resource)
92{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070093 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040094
95 if (offer->source)
96 wl_list_remove(&offer->source_destroy_listener.link);
97 free(offer);
98}
99
100static void
101destroy_offer_data_source(struct wl_listener *listener, void *data)
102{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700103 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400104
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700105 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400106 source_destroy_listener);
107
108 offer->source = NULL;
109}
110
111static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700112weston_data_source_send_offer(struct weston_data_source *source,
113 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400114{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700115 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400116 char **p;
117
118 offer = malloc(sizeof *offer);
119 if (offer == NULL)
120 return NULL;
121
Jason Ekstranda85118c2013-06-27 20:17:02 -0500122 offer->resource =
123 wl_resource_create(wl_resource_get_client(target),
124 &wl_data_offer_interface, 1, 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700125 if (offer->resource == NULL) {
126 free(offer);
127 return NULL;
128 }
129
Jason Ekstranda85118c2013-06-27 20:17:02 -0500130 wl_resource_set_implementation(offer->resource, &data_offer_interface,
131 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400132
133 offer->source = source;
134 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500135 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400136 &offer->source_destroy_listener);
137
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500138 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400139
140 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500141 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400142
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500143 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400144}
145
146static void
147data_source_offer(struct wl_client *client,
148 struct wl_resource *resource,
149 const char *type)
150{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700151 struct weston_data_source *source =
152 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400153 char **p;
154
155 p = wl_array_add(&source->mime_types, sizeof *p);
156 if (p)
157 *p = strdup(type);
158 if (!p || !*p)
159 wl_resource_post_no_memory(resource);
160}
161
162static void
163data_source_destroy(struct wl_client *client, struct wl_resource *resource)
164{
165 wl_resource_destroy(resource);
166}
167
168static struct wl_data_source_interface data_source_interface = {
169 data_source_offer,
170 data_source_destroy
171};
172
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400173static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400174drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
175{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400176 struct weston_drag *drag = es->configure_private;
177 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400178 struct wl_list *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400179 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400180
181 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400182 if (pointer->sprite && weston_surface_is_mapped(pointer->sprite))
183 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400184 else
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400185 list = &es->compositor->cursor_layer.surface_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400186
187 wl_list_insert(list, &es->layer_link);
188 weston_surface_update_transform(es);
189 empty_region(&es->pending.input);
190 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400191
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400192 drag->dx += sx;
193 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400194
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400195 fx = wl_fixed_to_double(pointer->x) + drag->dx;
196 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400197 weston_surface_configure(es, fx, fy, width, height);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400198}
199
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400200static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400201destroy_drag_focus(struct wl_listener *listener, void *data)
202{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400203 struct weston_drag *drag =
204 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400206 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400207}
208
209static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400210weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface,
211 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400212{
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400213 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400214 struct wl_resource *resource, *offer = NULL;
Rob Bradford880ebc72013-07-22 17:31:38 +0100215 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400216 uint32_t serial;
217
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400218 if (drag->focus_resource) {
219 wl_data_device_send_leave(drag->focus_resource);
220 wl_list_remove(&drag->focus_listener.link);
221 drag->focus_resource = NULL;
222 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400223 }
224
225 if (!surface)
226 return;
227
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500228 if (!drag->data_source &&
229 wl_resource_get_client(surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400230 return;
231
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500232 resource = wl_resource_find_for_client(&pointer->seat->drag_resource_list,
233 wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400234 if (!resource)
235 return;
236
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400237 serial = wl_display_next_serial(display);
238
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700239 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700240 offer = weston_data_source_send_offer(drag->data_source,
241 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700242 if (offer == NULL)
243 return;
244 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400245
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500246 wl_data_device_send_enter(resource, serial, surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400247 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400248
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400249 drag->focus = surface;
250 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500251 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400252 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400253}
254
255static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400256drag_grab_focus(struct weston_pointer_grab *grab)
257{
258 struct weston_drag *drag =
259 container_of(grab, struct weston_drag, grab);
260 struct weston_pointer *pointer = grab->pointer;
261 struct weston_surface *surface;
262 wl_fixed_t sx, sy;
263
264 surface = weston_compositor_pick_surface(pointer->seat->compositor,
265 pointer->x, pointer->y,
266 &sx, &sy);
267 if (drag->focus != surface)
268 weston_drag_set_focus(drag, surface, sx, sy);
269}
270
271static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400272drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400273{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400274 struct weston_drag *drag =
275 container_of(grab, struct weston_drag, grab);
276 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400277 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400278 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400279
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400280 if (drag->icon) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400281 fx = wl_fixed_to_double(pointer->x) + drag->dx;
282 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400283 weston_surface_set_position(drag->icon, fx, fy);
284 weston_surface_schedule_repaint(drag->icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400285 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400286
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400287 if (drag->focus_resource) {
288 weston_surface_from_global_fixed(drag->focus,
289 pointer->x, pointer->y,
290 &sx, &sy);
291
292 wl_data_device_send_motion(drag->focus_resource, time, sx, sy);
293 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400294}
295
296static void
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400297data_device_end_drag_grab(struct weston_drag *drag)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400298{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400299 if (drag->icon) {
300 if (weston_surface_is_mapped(drag->icon))
301 weston_surface_unmap(drag->icon);
302
303 drag->icon->configure = NULL;
304 empty_region(&drag->icon->pending.input);
305 wl_list_remove(&drag->icon_destroy_listener.link);
306 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400307
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400308 weston_drag_set_focus(drag, NULL, 0, 0);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400309
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400310 weston_pointer_end_grab(drag->grab.pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400311
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400312 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400313}
314
315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400317 uint32_t time, uint32_t button, uint32_t state_w)
318{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400319 struct weston_drag *drag =
320 container_of(grab, struct weston_drag, grab);
321 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400322 enum wl_pointer_button_state state = state_w;
323
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400324 if (drag->focus_resource &&
325 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400327 wl_data_device_send_drop(drag->focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400328
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400329 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400331 if (drag->data_source)
332 wl_list_remove(&drag->data_source_listener.link);
333 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400334 }
335}
336
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400337static const struct weston_pointer_grab_interface drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338 drag_grab_focus,
339 drag_grab_motion,
340 drag_grab_button,
341};
342
343static void
344destroy_data_device_source(struct wl_listener *listener, void *data)
345{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400346 struct weston_drag *drag = container_of(listener, struct weston_drag,
347 data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400348
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400349 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400350}
351
352static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400353handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400354{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400355 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400356 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400357
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400358 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400359}
360
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700361WL_EXPORT int
362weston_seat_start_drag(struct weston_seat *seat,
363 struct weston_data_source *source,
364 struct weston_surface *icon,
365 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400366{
Kristian Høgsberge2173b52013-06-25 11:28:18 -0400367 struct weston_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400368
Peter Huttererf3d62272013-08-08 11:57:05 +1000369 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700370 if (drag == NULL)
371 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400372
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400373 drag->grab.interface = &drag_grab_interface;
374 drag->client = client;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700375 drag->data_source = source;
376 drag->icon = icon;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400377
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700378 if (source) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400379 drag->data_source_listener.notify = destroy_data_device_source;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700380 wl_signal_add(&source->destroy_signal,
381 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400382 }
383
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400384 if (icon) {
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400385 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500386 wl_signal_add(&icon->destroy_signal,
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400387 &drag->icon_destroy_listener);
388
389 icon->configure = drag_surface_configure;
390 icon->configure_private = drag;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400391 }
392
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400393 weston_pointer_set_focus(seat->pointer, NULL,
394 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400395 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700396
397 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398}
399
400static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700401data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
402 struct wl_resource *source_resource,
403 struct wl_resource *origin_resource,
404 struct wl_resource *icon_resource, uint32_t serial)
405{
406 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700407 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700408 struct weston_surface *icon = NULL;
409
410 if (seat->pointer->button_count == 0 ||
411 seat->pointer->grab_serial != serial ||
412 seat->pointer->focus != wl_resource_get_user_data(origin_resource))
413 return;
414
415 /* FIXME: Check that the data source type array isn't empty. */
416
417 if (source_resource)
418 source = wl_resource_get_user_data(source_resource);
419 if (icon_resource)
420 icon = wl_resource_get_user_data(icon_resource);
421 if (icon && icon->configure) {
422 wl_resource_post_error(icon_resource,
423 WL_DISPLAY_ERROR_INVALID_OBJECT,
424 "surface->configure already set");
425 return;
426 }
427
428 if (weston_seat_start_drag(seat, source, icon, client) < 0)
429 wl_resource_post_no_memory(resource);
430}
431
432static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400433destroy_selection_data_source(struct wl_listener *listener, void *data)
434{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400435 struct weston_seat *seat = container_of(listener, struct weston_seat,
436 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400437 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100438 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400439
440 seat->selection_data_source = NULL;
441
442 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100443 focus = seat->keyboard->focus;
444 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500445 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100446 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400447 if (data_device)
448 wl_data_device_send_selection(data_device, NULL);
449 }
450
451 wl_signal_emit(&seat->selection_signal, seat);
452}
453
454WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400455weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700456 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400457{
458 struct wl_resource *data_device, *offer;
Neil Roberts96d790e2013-09-19 17:32:00 +0100459 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400460
461 if (seat->selection_data_source &&
462 seat->selection_serial - serial < UINT32_MAX / 2)
463 return;
464
465 if (seat->selection_data_source) {
466 seat->selection_data_source->cancel(seat->selection_data_source);
467 wl_list_remove(&seat->selection_data_source_listener.link);
468 seat->selection_data_source = NULL;
469 }
470
471 seat->selection_data_source = source;
472 seat->selection_serial = serial;
473
474 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100475 focus = seat->keyboard->focus;
476 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500477 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100478 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400479 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700480 offer = weston_data_source_send_offer(seat->selection_data_source,
481 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400482 wl_data_device_send_selection(data_device, offer);
483 } else if (data_device) {
484 wl_data_device_send_selection(data_device, NULL);
485 }
486 }
487
488 wl_signal_emit(&seat->selection_signal, seat);
489
490 if (source) {
491 seat->selection_data_source_listener.notify =
492 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500493 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400494 &seat->selection_data_source_listener);
495 }
496}
497
498static void
499data_device_set_selection(struct wl_client *client,
500 struct wl_resource *resource,
501 struct wl_resource *source_resource, uint32_t serial)
502{
503 if (!source_resource)
504 return;
505
506 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500507 weston_seat_set_selection(wl_resource_get_user_data(resource),
508 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400509 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400510}
511
512static const struct wl_data_device_interface data_device_interface = {
513 data_device_start_drag,
514 data_device_set_selection,
515};
516
517static void
518destroy_data_source(struct wl_resource *resource)
519{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700520 struct weston_data_source *source =
521 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400522 char **p;
523
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500524 wl_signal_emit(&source->destroy_signal, source);
525
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400526 wl_array_for_each(p, &source->mime_types)
527 free(*p);
528
529 wl_array_release(&source->mime_types);
530
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400531 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400532}
533
534static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700535client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400536 uint32_t time, const char *mime_type)
537{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500538 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400539}
540
541static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700542client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400543 const char *mime_type, int32_t fd)
544{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500545 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546 close(fd);
547}
548
549static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700550client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400551{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500552 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400553}
554
555static void
556create_data_source(struct wl_client *client,
557 struct wl_resource *resource, uint32_t id)
558{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700559 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400560
561 source = malloc(sizeof *source);
562 if (source == NULL) {
563 wl_resource_post_no_memory(resource);
564 return;
565 }
566
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500567 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400568 source->accept = client_source_accept;
569 source->send = client_source_send;
570 source->cancel = client_source_cancel;
571
572 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500573
Jason Ekstranda85118c2013-06-27 20:17:02 -0500574 source->resource =
575 wl_resource_create(client, &wl_data_source_interface, 1, id);
576 wl_resource_set_implementation(source->resource, &data_source_interface,
577 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400578}
579
580static void unbind_data_device(struct wl_resource *resource)
581{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500582 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400583}
584
585static void
586get_data_device(struct wl_client *client,
587 struct wl_resource *manager_resource,
588 uint32_t id, struct wl_resource *seat_resource)
589{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500590 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400591 struct wl_resource *resource;
592
Jason Ekstranda85118c2013-06-27 20:17:02 -0500593 resource = wl_resource_create(client,
594 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700595 if (resource == NULL) {
596 wl_resource_post_no_memory(manager_resource);
597 return;
598 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400599
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700600 wl_list_insert(&seat->drag_resource_list,
601 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500602 wl_resource_set_implementation(resource, &data_device_interface,
603 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400604}
605
606static const struct wl_data_device_manager_interface manager_interface = {
607 create_data_source,
608 get_data_device
609};
610
611static void
612bind_manager(struct wl_client *client,
613 void *data, uint32_t version, uint32_t id)
614{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500615 struct wl_resource *resource;
616
617 resource =
618 wl_resource_create(client,
619 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700620 if (resource == NULL) {
621 wl_client_post_no_memory(client);
622 return;
623 }
624
625 wl_resource_set_implementation(resource, &manager_interface,
626 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400627}
628
629WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400630wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400631{
Neil Roberts96d790e2013-09-19 17:32:00 +0100632 struct wl_resource *data_device, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700633 struct weston_data_source *source;
Neil Roberts96d790e2013-09-19 17:32:00 +0100634 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400635
636 if (!seat->keyboard)
637 return;
638
Neil Roberts96d790e2013-09-19 17:32:00 +0100639 focus = seat->keyboard->focus;
640 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400641 return;
642
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500643 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100644 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400645 if (!data_device)
646 return;
647
648 source = seat->selection_data_source;
649 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700650 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400651 wl_data_device_send_selection(data_device, offer);
652 }
653}
654
655WL_EXPORT int
656wl_data_device_manager_init(struct wl_display *display)
657{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400658 if (wl_global_create(display,
659 &wl_data_device_manager_interface, 1,
660 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400661 return -1;
662
663 return 0;
664}