blob: 1eb192577287b2a06e194824ba1377f1ccbadc1c [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
361static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400362data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
363 struct wl_resource *source_resource,
364 struct wl_resource *origin_resource,
365 struct wl_resource *icon_resource, uint32_t serial)
366{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500367 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberge2173b52013-06-25 11:28:18 -0400368 struct weston_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400369 struct weston_surface *icon = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400370
Kristian Høgsbergdba25862013-05-08 15:53:42 -0400371 if (seat->pointer->button_count == 0 ||
372 seat->pointer->grab_serial != serial ||
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500373 seat->pointer->focus != wl_resource_get_user_data(origin_resource))
Kristian Høgsbergdba25862013-05-08 15:53:42 -0400374 return;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400375
376 /* FIXME: Check that the data source type array isn't empty. */
377
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400378 if (icon_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500379 icon = wl_resource_get_user_data(icon_resource);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400380 if (icon && icon->configure) {
381 wl_resource_post_error(icon_resource,
382 WL_DISPLAY_ERROR_INVALID_OBJECT,
383 "surface->configure already set");
384 return;
385 }
386
Peter Huttererf3d62272013-08-08 11:57:05 +1000387 drag = zalloc(sizeof *drag);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400388 if (drag == NULL) {
389 wl_resource_post_no_memory(resource);
390 return;
391 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400393 drag->grab.interface = &drag_grab_interface;
394 drag->client = client;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400395
396 if (source_resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500397 drag->data_source = wl_resource_get_user_data(source_resource);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400398 drag->data_source_listener.notify = destroy_data_device_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500399 wl_resource_add_destroy_listener(source_resource,
400 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400401 }
402
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400403 if (icon) {
404 drag->icon = icon;
405 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500406 wl_signal_add(&icon->destroy_signal,
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400407 &drag->icon_destroy_listener);
408
409 icon->configure = drag_surface_configure;
410 icon->configure_private = drag;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400411 }
412
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400413 weston_pointer_set_focus(seat->pointer, NULL,
414 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400415 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400416}
417
418static void
419destroy_selection_data_source(struct wl_listener *listener, void *data)
420{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400421 struct weston_seat *seat = container_of(listener, struct weston_seat,
422 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400423 struct wl_resource *data_device;
424 struct wl_resource *focus = NULL;
425
426 seat->selection_data_source = NULL;
427
428 if (seat->keyboard)
429 focus = seat->keyboard->focus_resource;
430 if (focus) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500431 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
432 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400433 if (data_device)
434 wl_data_device_send_selection(data_device, NULL);
435 }
436
437 wl_signal_emit(&seat->selection_signal, seat);
438}
439
440WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400441weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700442 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400443{
444 struct wl_resource *data_device, *offer;
445 struct wl_resource *focus = NULL;
446
447 if (seat->selection_data_source &&
448 seat->selection_serial - serial < UINT32_MAX / 2)
449 return;
450
451 if (seat->selection_data_source) {
452 seat->selection_data_source->cancel(seat->selection_data_source);
453 wl_list_remove(&seat->selection_data_source_listener.link);
454 seat->selection_data_source = NULL;
455 }
456
457 seat->selection_data_source = source;
458 seat->selection_serial = serial;
459
460 if (seat->keyboard)
461 focus = seat->keyboard->focus_resource;
462 if (focus) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500463 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
464 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400465 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700466 offer = weston_data_source_send_offer(seat->selection_data_source,
467 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400468 wl_data_device_send_selection(data_device, offer);
469 } else if (data_device) {
470 wl_data_device_send_selection(data_device, NULL);
471 }
472 }
473
474 wl_signal_emit(&seat->selection_signal, seat);
475
476 if (source) {
477 seat->selection_data_source_listener.notify =
478 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500479 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400480 &seat->selection_data_source_listener);
481 }
482}
483
484static void
485data_device_set_selection(struct wl_client *client,
486 struct wl_resource *resource,
487 struct wl_resource *source_resource, uint32_t serial)
488{
489 if (!source_resource)
490 return;
491
492 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500493 weston_seat_set_selection(wl_resource_get_user_data(resource),
494 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400495 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400496}
497
498static const struct wl_data_device_interface data_device_interface = {
499 data_device_start_drag,
500 data_device_set_selection,
501};
502
503static void
504destroy_data_source(struct wl_resource *resource)
505{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700506 struct weston_data_source *source =
507 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400508 char **p;
509
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500510 wl_signal_emit(&source->destroy_signal, source);
511
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400512 wl_array_for_each(p, &source->mime_types)
513 free(*p);
514
515 wl_array_release(&source->mime_types);
516
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400517 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400518}
519
520static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700521client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400522 uint32_t time, const char *mime_type)
523{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500524 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400525}
526
527static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700528client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400529 const char *mime_type, int32_t fd)
530{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500531 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400532 close(fd);
533}
534
535static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700536client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400537{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500538 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400539}
540
541static void
542create_data_source(struct wl_client *client,
543 struct wl_resource *resource, uint32_t id)
544{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700545 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546
547 source = malloc(sizeof *source);
548 if (source == NULL) {
549 wl_resource_post_no_memory(resource);
550 return;
551 }
552
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500553 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400554 source->accept = client_source_accept;
555 source->send = client_source_send;
556 source->cancel = client_source_cancel;
557
558 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500559
Jason Ekstranda85118c2013-06-27 20:17:02 -0500560 source->resource =
561 wl_resource_create(client, &wl_data_source_interface, 1, id);
562 wl_resource_set_implementation(source->resource, &data_source_interface,
563 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400564}
565
566static void unbind_data_device(struct wl_resource *resource)
567{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500568 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400569}
570
571static void
572get_data_device(struct wl_client *client,
573 struct wl_resource *manager_resource,
574 uint32_t id, struct wl_resource *seat_resource)
575{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500576 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400577 struct wl_resource *resource;
578
Jason Ekstranda85118c2013-06-27 20:17:02 -0500579 resource = wl_resource_create(client,
580 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700581 if (resource == NULL) {
582 wl_resource_post_no_memory(manager_resource);
583 return;
584 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400585
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700586 wl_list_insert(&seat->drag_resource_list,
587 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500588 wl_resource_set_implementation(resource, &data_device_interface,
589 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400590}
591
592static const struct wl_data_device_manager_interface manager_interface = {
593 create_data_source,
594 get_data_device
595};
596
597static void
598bind_manager(struct wl_client *client,
599 void *data, uint32_t version, uint32_t id)
600{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500601 struct wl_resource *resource;
602
603 resource =
604 wl_resource_create(client,
605 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700606 if (resource == NULL) {
607 wl_client_post_no_memory(client);
608 return;
609 }
610
611 wl_resource_set_implementation(resource, &manager_interface,
612 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400613}
614
615WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400616wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400617{
618 struct wl_resource *data_device, *focus, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700619 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400620
621 if (!seat->keyboard)
622 return;
623
624 focus = seat->keyboard->focus_resource;
625 if (!focus)
626 return;
627
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500628 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
629 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400630 if (!data_device)
631 return;
632
633 source = seat->selection_data_source;
634 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700635 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400636 wl_data_device_send_selection(data_device, offer);
637 }
638}
639
640WL_EXPORT int
641wl_data_device_manager_init(struct wl_display *display)
642{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400643 if (wl_global_create(display,
644 &wl_data_device_manager_interface, 1,
645 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400646 return -1;
647
648 return 0;
649}