blob: 25587b26bac3ccfdde14f4272ee8b3b2c768d177 [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øgsberg054c50a2013-05-08 15:27:47 -0400239 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øgsbergb10b44b2013-08-06 10:31:12 -0700242 if (offer == NULL)
243 return;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400244
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500245 wl_data_device_send_enter(resource, serial, surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400246 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400247
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400248 drag->focus = surface;
249 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500250 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400251 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400252}
253
254static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400255drag_grab_focus(struct weston_pointer_grab *grab)
256{
257 struct weston_drag *drag =
258 container_of(grab, struct weston_drag, grab);
259 struct weston_pointer *pointer = grab->pointer;
260 struct weston_surface *surface;
261 wl_fixed_t sx, sy;
262
263 surface = weston_compositor_pick_surface(pointer->seat->compositor,
264 pointer->x, pointer->y,
265 &sx, &sy);
266 if (drag->focus != surface)
267 weston_drag_set_focus(drag, surface, sx, sy);
268}
269
270static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400271drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400272{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400273 struct weston_drag *drag =
274 container_of(grab, struct weston_drag, grab);
275 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400276 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400277 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400278
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400279 if (drag->icon) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400280 fx = wl_fixed_to_double(pointer->x) + drag->dx;
281 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400282 weston_surface_set_position(drag->icon, fx, fy);
283 weston_surface_schedule_repaint(drag->icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400284 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400285
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400286 if (drag->focus_resource) {
287 weston_surface_from_global_fixed(drag->focus,
288 pointer->x, pointer->y,
289 &sx, &sy);
290
291 wl_data_device_send_motion(drag->focus_resource, time, sx, sy);
292 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400293}
294
295static void
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400296data_device_end_drag_grab(struct weston_drag *drag)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400297{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400298 if (drag->icon) {
299 if (weston_surface_is_mapped(drag->icon))
300 weston_surface_unmap(drag->icon);
301
302 drag->icon->configure = NULL;
303 empty_region(&drag->icon->pending.input);
304 wl_list_remove(&drag->icon_destroy_listener.link);
305 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400306
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400307 weston_drag_set_focus(drag, NULL, 0, 0);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400309 weston_pointer_end_grab(drag->grab.pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400310
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400311 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400312}
313
314static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400315drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400316 uint32_t time, uint32_t button, uint32_t state_w)
317{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400318 struct weston_drag *drag =
319 container_of(grab, struct weston_drag, grab);
320 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321 enum wl_pointer_button_state state = state_w;
322
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400323 if (drag->focus_resource &&
324 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400325 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400326 wl_data_device_send_drop(drag->focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400328 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400329 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400330 if (drag->data_source)
331 wl_list_remove(&drag->data_source_listener.link);
332 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400333 }
334}
335
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400336static const struct weston_pointer_grab_interface drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400337 drag_grab_focus,
338 drag_grab_motion,
339 drag_grab_button,
340};
341
342static void
343destroy_data_device_source(struct wl_listener *listener, void *data)
344{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400345 struct weston_drag *drag = container_of(listener, struct weston_drag,
346 data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400347
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400348 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400349}
350
351static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400352handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400353{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400354 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400355 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400356
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400357 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400358}
359
360static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400361data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
362 struct wl_resource *source_resource,
363 struct wl_resource *origin_resource,
364 struct wl_resource *icon_resource, uint32_t serial)
365{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500366 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberge2173b52013-06-25 11:28:18 -0400367 struct weston_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400368 struct weston_surface *icon = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400369
Kristian Høgsbergdba25862013-05-08 15:53:42 -0400370 if (seat->pointer->button_count == 0 ||
371 seat->pointer->grab_serial != serial ||
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500372 seat->pointer->focus != wl_resource_get_user_data(origin_resource))
Kristian Høgsbergdba25862013-05-08 15:53:42 -0400373 return;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400374
375 /* FIXME: Check that the data source type array isn't empty. */
376
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400377 if (icon_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500378 icon = wl_resource_get_user_data(icon_resource);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400379 if (icon && icon->configure) {
380 wl_resource_post_error(icon_resource,
381 WL_DISPLAY_ERROR_INVALID_OBJECT,
382 "surface->configure already set");
383 return;
384 }
385
Peter Huttererf3d62272013-08-08 11:57:05 +1000386 drag = zalloc(sizeof *drag);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400387 if (drag == NULL) {
388 wl_resource_post_no_memory(resource);
389 return;
390 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400391
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400392 drag->grab.interface = &drag_grab_interface;
393 drag->client = client;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400394
395 if (source_resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500396 drag->data_source = wl_resource_get_user_data(source_resource);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400397 drag->data_source_listener.notify = destroy_data_device_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500398 wl_resource_add_destroy_listener(source_resource,
399 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400 }
401
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400402 if (icon) {
403 drag->icon = icon;
404 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500405 wl_signal_add(&icon->destroy_signal,
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400406 &drag->icon_destroy_listener);
407
408 icon->configure = drag_surface_configure;
409 icon->configure_private = drag;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400410 }
411
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400412 weston_pointer_set_focus(seat->pointer, NULL,
413 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400414 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400415}
416
417static void
418destroy_selection_data_source(struct wl_listener *listener, void *data)
419{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400420 struct weston_seat *seat = container_of(listener, struct weston_seat,
421 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400422 struct wl_resource *data_device;
423 struct wl_resource *focus = NULL;
424
425 seat->selection_data_source = NULL;
426
427 if (seat->keyboard)
428 focus = seat->keyboard->focus_resource;
429 if (focus) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500430 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
431 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400432 if (data_device)
433 wl_data_device_send_selection(data_device, NULL);
434 }
435
436 wl_signal_emit(&seat->selection_signal, seat);
437}
438
439WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400440weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700441 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442{
443 struct wl_resource *data_device, *offer;
444 struct wl_resource *focus = NULL;
445
446 if (seat->selection_data_source &&
447 seat->selection_serial - serial < UINT32_MAX / 2)
448 return;
449
450 if (seat->selection_data_source) {
451 seat->selection_data_source->cancel(seat->selection_data_source);
452 wl_list_remove(&seat->selection_data_source_listener.link);
453 seat->selection_data_source = NULL;
454 }
455
456 seat->selection_data_source = source;
457 seat->selection_serial = serial;
458
459 if (seat->keyboard)
460 focus = seat->keyboard->focus_resource;
461 if (focus) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500462 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
463 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400464 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700465 offer = weston_data_source_send_offer(seat->selection_data_source,
466 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400467 wl_data_device_send_selection(data_device, offer);
468 } else if (data_device) {
469 wl_data_device_send_selection(data_device, NULL);
470 }
471 }
472
473 wl_signal_emit(&seat->selection_signal, seat);
474
475 if (source) {
476 seat->selection_data_source_listener.notify =
477 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500478 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400479 &seat->selection_data_source_listener);
480 }
481}
482
483static void
484data_device_set_selection(struct wl_client *client,
485 struct wl_resource *resource,
486 struct wl_resource *source_resource, uint32_t serial)
487{
488 if (!source_resource)
489 return;
490
491 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500492 weston_seat_set_selection(wl_resource_get_user_data(resource),
493 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400494 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400495}
496
497static const struct wl_data_device_interface data_device_interface = {
498 data_device_start_drag,
499 data_device_set_selection,
500};
501
502static void
503destroy_data_source(struct wl_resource *resource)
504{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700505 struct weston_data_source *source =
506 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400507 char **p;
508
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500509 wl_signal_emit(&source->destroy_signal, source);
510
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400511 wl_array_for_each(p, &source->mime_types)
512 free(*p);
513
514 wl_array_release(&source->mime_types);
515
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400516 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400517}
518
519static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700520client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400521 uint32_t time, const char *mime_type)
522{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500523 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524}
525
526static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700527client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400528 const char *mime_type, int32_t fd)
529{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500530 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400531 close(fd);
532}
533
534static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700535client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400536{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500537 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400538}
539
540static void
541create_data_source(struct wl_client *client,
542 struct wl_resource *resource, uint32_t id)
543{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700544 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400545
546 source = malloc(sizeof *source);
547 if (source == NULL) {
548 wl_resource_post_no_memory(resource);
549 return;
550 }
551
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500552 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400553 source->accept = client_source_accept;
554 source->send = client_source_send;
555 source->cancel = client_source_cancel;
556
557 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500558
Jason Ekstranda85118c2013-06-27 20:17:02 -0500559 source->resource =
560 wl_resource_create(client, &wl_data_source_interface, 1, id);
561 wl_resource_set_implementation(source->resource, &data_source_interface,
562 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400563}
564
565static void unbind_data_device(struct wl_resource *resource)
566{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500567 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400568}
569
570static void
571get_data_device(struct wl_client *client,
572 struct wl_resource *manager_resource,
573 uint32_t id, struct wl_resource *seat_resource)
574{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500575 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400576 struct wl_resource *resource;
577
Jason Ekstranda85118c2013-06-27 20:17:02 -0500578 resource = wl_resource_create(client,
579 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700580 if (resource == NULL) {
581 wl_resource_post_no_memory(manager_resource);
582 return;
583 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400584
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700585 wl_list_insert(&seat->drag_resource_list,
586 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500587 wl_resource_set_implementation(resource, &data_device_interface,
588 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400589}
590
591static const struct wl_data_device_manager_interface manager_interface = {
592 create_data_source,
593 get_data_device
594};
595
596static void
597bind_manager(struct wl_client *client,
598 void *data, uint32_t version, uint32_t id)
599{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500600 struct wl_resource *resource;
601
602 resource =
603 wl_resource_create(client,
604 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700605 if (resource == NULL) {
606 wl_client_post_no_memory(client);
607 return;
608 }
609
610 wl_resource_set_implementation(resource, &manager_interface,
611 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400612}
613
614WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400615wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400616{
617 struct wl_resource *data_device, *focus, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700618 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400619
620 if (!seat->keyboard)
621 return;
622
623 focus = seat->keyboard->focus_resource;
624 if (!focus)
625 return;
626
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500627 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
628 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400629 if (!data_device)
630 return;
631
632 source = seat->selection_data_source;
633 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700634 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400635 wl_data_device_send_selection(data_device, offer);
636 }
637}
638
639WL_EXPORT int
640wl_data_device_manager_init(struct wl_display *display)
641{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400642 if (wl_global_create(display,
643 &wl_data_device_manager_interface, 1,
644 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400645 return -1;
646
647 return 0;
648}