blob: 4bf625677f745f32a37dcd58a9720041ac4a9f73 [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
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <stdio.h>
27
28#include "compositor.h"
29
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040030struct weston_drag {
31 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070032 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040033 struct wl_listener data_source_listener;
34 struct weston_surface *focus;
35 struct wl_resource *focus_resource;
36 struct wl_listener focus_listener;
37 struct weston_pointer_grab grab;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040038 struct weston_surface *icon;
39 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040040 int32_t dx, dy;
41};
42
Kristian Høgsberg2158a882013-04-18 15:07:39 -040043static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -040044empty_region(pixman_region32_t *region)
45{
46 pixman_region32_fini(region);
47 pixman_region32_init(region);
48}
49
50static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -040051data_offer_accept(struct wl_client *client, struct wl_resource *resource,
52 uint32_t serial, const char *mime_type)
53{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070054 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040055
56 /* FIXME: Check that client is currently focused by the input
57 * device that is currently dragging this data source. Should
58 * this be a wl_data_device request? */
59
60 if (offer->source)
61 offer->source->accept(offer->source, serial, mime_type);
62}
63
64static void
65data_offer_receive(struct wl_client *client, struct wl_resource *resource,
66 const char *mime_type, int32_t fd)
67{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070068 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040069
70 if (offer->source)
71 offer->source->send(offer->source, mime_type, fd);
72 else
73 close(fd);
74}
75
76static void
77data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
78{
79 wl_resource_destroy(resource);
80}
81
82static const struct wl_data_offer_interface data_offer_interface = {
83 data_offer_accept,
84 data_offer_receive,
85 data_offer_destroy,
86};
87
88static void
89destroy_data_offer(struct wl_resource *resource)
90{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070091 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040092
93 if (offer->source)
94 wl_list_remove(&offer->source_destroy_listener.link);
95 free(offer);
96}
97
98static void
99destroy_offer_data_source(struct wl_listener *listener, void *data)
100{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700101 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400102
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700103 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400104 source_destroy_listener);
105
106 offer->source = NULL;
107}
108
109static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700110weston_data_source_send_offer(struct weston_data_source *source,
111 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400112{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700113 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400114 char **p;
115
116 offer = malloc(sizeof *offer);
117 if (offer == NULL)
118 return NULL;
119
Jason Ekstranda85118c2013-06-27 20:17:02 -0500120 offer->resource =
121 wl_resource_create(wl_resource_get_client(target),
122 &wl_data_offer_interface, 1, 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700123 if (offer->resource == NULL) {
124 free(offer);
125 return NULL;
126 }
127
Jason Ekstranda85118c2013-06-27 20:17:02 -0500128 wl_resource_set_implementation(offer->resource, &data_offer_interface,
129 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400130
131 offer->source = source;
132 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500133 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400134 &offer->source_destroy_listener);
135
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500136 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400137
138 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500139 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400140
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500141 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400142}
143
144static void
145data_source_offer(struct wl_client *client,
146 struct wl_resource *resource,
147 const char *type)
148{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700149 struct weston_data_source *source =
150 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400151 char **p;
152
153 p = wl_array_add(&source->mime_types, sizeof *p);
154 if (p)
155 *p = strdup(type);
156 if (!p || !*p)
157 wl_resource_post_no_memory(resource);
158}
159
160static void
161data_source_destroy(struct wl_client *client, struct wl_resource *resource)
162{
163 wl_resource_destroy(resource);
164}
165
166static struct wl_data_source_interface data_source_interface = {
167 data_source_offer,
168 data_source_destroy
169};
170
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400171static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400172drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
173{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400174 struct weston_drag *drag = es->configure_private;
175 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400176 struct wl_list *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400177 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400178
179 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400180 if (pointer->sprite && weston_surface_is_mapped(pointer->sprite))
181 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400182 else
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400183 list = &es->compositor->cursor_layer.surface_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400184
185 wl_list_insert(list, &es->layer_link);
186 weston_surface_update_transform(es);
187 empty_region(&es->pending.input);
188 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400189
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400190 drag->dx += sx;
191 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400192
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400193 fx = wl_fixed_to_double(pointer->x) + drag->dx;
194 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400195 weston_surface_configure(es, fx, fy, width, height);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400196}
197
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400198static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400199destroy_drag_focus(struct wl_listener *listener, void *data)
200{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400201 struct weston_drag *drag =
202 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400203
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400204 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205}
206
207static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400208weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface,
209 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400210{
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400211 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400212 struct wl_resource *resource, *offer = NULL;
Rob Bradford880ebc72013-07-22 17:31:38 +0100213 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400214 uint32_t serial;
215
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400216 if (drag->focus_resource) {
217 wl_data_device_send_leave(drag->focus_resource);
218 wl_list_remove(&drag->focus_listener.link);
219 drag->focus_resource = NULL;
220 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400221 }
222
223 if (!surface)
224 return;
225
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500226 if (!drag->data_source &&
227 wl_resource_get_client(surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400228 return;
229
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500230 resource = wl_resource_find_for_client(&pointer->seat->drag_resource_list,
231 wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400232 if (!resource)
233 return;
234
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400235 serial = wl_display_next_serial(display);
236
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400237 if (drag->data_source)
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700238 offer = weston_data_source_send_offer(drag->data_source,
239 resource);
Kristian Høgsbergb10b44b2013-08-06 10:31:12 -0700240 if (offer == NULL)
241 return;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400242
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500243 wl_data_device_send_enter(resource, serial, surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400244 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400245
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400246 drag->focus = surface;
247 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500248 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400249 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400250}
251
252static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400253drag_grab_focus(struct weston_pointer_grab *grab)
254{
255 struct weston_drag *drag =
256 container_of(grab, struct weston_drag, grab);
257 struct weston_pointer *pointer = grab->pointer;
258 struct weston_surface *surface;
259 wl_fixed_t sx, sy;
260
261 surface = weston_compositor_pick_surface(pointer->seat->compositor,
262 pointer->x, pointer->y,
263 &sx, &sy);
264 if (drag->focus != surface)
265 weston_drag_set_focus(drag, surface, sx, sy);
266}
267
268static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400269drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400270{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400271 struct weston_drag *drag =
272 container_of(grab, struct weston_drag, grab);
273 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400274 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400275 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400276
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400277 if (drag->icon) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400278 fx = wl_fixed_to_double(pointer->x) + drag->dx;
279 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400280 weston_surface_set_position(drag->icon, fx, fy);
281 weston_surface_schedule_repaint(drag->icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400282 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400283
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400284 if (drag->focus_resource) {
285 weston_surface_from_global_fixed(drag->focus,
286 pointer->x, pointer->y,
287 &sx, &sy);
288
289 wl_data_device_send_motion(drag->focus_resource, time, sx, sy);
290 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400291}
292
293static void
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400294data_device_end_drag_grab(struct weston_drag *drag)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400295{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400296 if (drag->icon) {
297 if (weston_surface_is_mapped(drag->icon))
298 weston_surface_unmap(drag->icon);
299
300 drag->icon->configure = NULL;
301 empty_region(&drag->icon->pending.input);
302 wl_list_remove(&drag->icon_destroy_listener.link);
303 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400304
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400305 weston_drag_set_focus(drag, NULL, 0, 0);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400306
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400307 weston_pointer_end_grab(drag->grab.pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400309 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400310}
311
312static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400313drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400314 uint32_t time, uint32_t button, uint32_t state_w)
315{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400316 struct weston_drag *drag =
317 container_of(grab, struct weston_drag, grab);
318 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400319 enum wl_pointer_button_state state = state_w;
320
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400321 if (drag->focus_resource &&
322 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400323 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400324 wl_data_device_send_drop(drag->focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400325
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400326 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400328 if (drag->data_source)
329 wl_list_remove(&drag->data_source_listener.link);
330 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400331 }
332}
333
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400334static const struct weston_pointer_grab_interface drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400335 drag_grab_focus,
336 drag_grab_motion,
337 drag_grab_button,
338};
339
340static void
341destroy_data_device_source(struct wl_listener *listener, void *data)
342{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400343 struct weston_drag *drag = container_of(listener, struct weston_drag,
344 data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400345
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400346 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400347}
348
349static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400350handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400351{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400352 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400353 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400354
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400355 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400356}
357
358static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400359data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
360 struct wl_resource *source_resource,
361 struct wl_resource *origin_resource,
362 struct wl_resource *icon_resource, uint32_t serial)
363{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500364 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberge2173b52013-06-25 11:28:18 -0400365 struct weston_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400366 struct weston_surface *icon = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400367
Kristian Høgsbergdba25862013-05-08 15:53:42 -0400368 if (seat->pointer->button_count == 0 ||
369 seat->pointer->grab_serial != serial ||
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500370 seat->pointer->focus != wl_resource_get_user_data(origin_resource))
Kristian Høgsbergdba25862013-05-08 15:53:42 -0400371 return;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400372
373 /* FIXME: Check that the data source type array isn't empty. */
374
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400375 if (icon_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500376 icon = wl_resource_get_user_data(icon_resource);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400377 if (icon && icon->configure) {
378 wl_resource_post_error(icon_resource,
379 WL_DISPLAY_ERROR_INVALID_OBJECT,
380 "surface->configure already set");
381 return;
382 }
383
Peter Huttererf3d62272013-08-08 11:57:05 +1000384 drag = zalloc(sizeof *drag);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400385 if (drag == NULL) {
386 wl_resource_post_no_memory(resource);
387 return;
388 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400389
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400390 drag->grab.interface = &drag_grab_interface;
391 drag->client = client;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392
393 if (source_resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500394 drag->data_source = wl_resource_get_user_data(source_resource);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400395 drag->data_source_listener.notify = destroy_data_device_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500396 wl_resource_add_destroy_listener(source_resource,
397 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398 }
399
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400400 if (icon) {
401 drag->icon = icon;
402 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500403 wl_signal_add(&icon->destroy_signal,
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400404 &drag->icon_destroy_listener);
405
406 icon->configure = drag_surface_configure;
407 icon->configure_private = drag;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408 }
409
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400410 weston_pointer_set_focus(seat->pointer, NULL,
411 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400412 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400413}
414
415static void
416destroy_selection_data_source(struct wl_listener *listener, void *data)
417{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400418 struct weston_seat *seat = container_of(listener, struct weston_seat,
419 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400420 struct wl_resource *data_device;
421 struct wl_resource *focus = NULL;
422
423 seat->selection_data_source = NULL;
424
425 if (seat->keyboard)
426 focus = seat->keyboard->focus_resource;
427 if (focus) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500428 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
429 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400430 if (data_device)
431 wl_data_device_send_selection(data_device, NULL);
432 }
433
434 wl_signal_emit(&seat->selection_signal, seat);
435}
436
437WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400438weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700439 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400440{
441 struct wl_resource *data_device, *offer;
442 struct wl_resource *focus = NULL;
443
444 if (seat->selection_data_source &&
445 seat->selection_serial - serial < UINT32_MAX / 2)
446 return;
447
448 if (seat->selection_data_source) {
449 seat->selection_data_source->cancel(seat->selection_data_source);
450 wl_list_remove(&seat->selection_data_source_listener.link);
451 seat->selection_data_source = NULL;
452 }
453
454 seat->selection_data_source = source;
455 seat->selection_serial = serial;
456
457 if (seat->keyboard)
458 focus = seat->keyboard->focus_resource;
459 if (focus) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500460 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
461 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400462 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700463 offer = weston_data_source_send_offer(seat->selection_data_source,
464 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400465 wl_data_device_send_selection(data_device, offer);
466 } else if (data_device) {
467 wl_data_device_send_selection(data_device, NULL);
468 }
469 }
470
471 wl_signal_emit(&seat->selection_signal, seat);
472
473 if (source) {
474 seat->selection_data_source_listener.notify =
475 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500476 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400477 &seat->selection_data_source_listener);
478 }
479}
480
481static void
482data_device_set_selection(struct wl_client *client,
483 struct wl_resource *resource,
484 struct wl_resource *source_resource, uint32_t serial)
485{
486 if (!source_resource)
487 return;
488
489 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500490 weston_seat_set_selection(wl_resource_get_user_data(resource),
491 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400492 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493}
494
495static const struct wl_data_device_interface data_device_interface = {
496 data_device_start_drag,
497 data_device_set_selection,
498};
499
500static void
501destroy_data_source(struct wl_resource *resource)
502{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700503 struct weston_data_source *source =
504 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400505 char **p;
506
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500507 wl_signal_emit(&source->destroy_signal, source);
508
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400509 wl_array_for_each(p, &source->mime_types)
510 free(*p);
511
512 wl_array_release(&source->mime_types);
513
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400514 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400515}
516
517static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700518client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400519 uint32_t time, const char *mime_type)
520{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500521 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400522}
523
524static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700525client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400526 const char *mime_type, int32_t fd)
527{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500528 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400529 close(fd);
530}
531
532static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700533client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400534{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500535 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400536}
537
538static void
539create_data_source(struct wl_client *client,
540 struct wl_resource *resource, uint32_t id)
541{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700542 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400543
544 source = malloc(sizeof *source);
545 if (source == NULL) {
546 wl_resource_post_no_memory(resource);
547 return;
548 }
549
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500550 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400551 source->accept = client_source_accept;
552 source->send = client_source_send;
553 source->cancel = client_source_cancel;
554
555 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500556
Jason Ekstranda85118c2013-06-27 20:17:02 -0500557 source->resource =
558 wl_resource_create(client, &wl_data_source_interface, 1, id);
559 wl_resource_set_implementation(source->resource, &data_source_interface,
560 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400561}
562
563static void unbind_data_device(struct wl_resource *resource)
564{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500565 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400566}
567
568static void
569get_data_device(struct wl_client *client,
570 struct wl_resource *manager_resource,
571 uint32_t id, struct wl_resource *seat_resource)
572{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500573 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400574 struct wl_resource *resource;
575
Jason Ekstranda85118c2013-06-27 20:17:02 -0500576 resource = wl_resource_create(client,
577 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700578 if (resource == NULL) {
579 wl_resource_post_no_memory(manager_resource);
580 return;
581 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400582
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700583 wl_list_insert(&seat->drag_resource_list,
584 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500585 wl_resource_set_implementation(resource, &data_device_interface,
586 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400587}
588
589static const struct wl_data_device_manager_interface manager_interface = {
590 create_data_source,
591 get_data_device
592};
593
594static void
595bind_manager(struct wl_client *client,
596 void *data, uint32_t version, uint32_t id)
597{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500598 struct wl_resource *resource;
599
600 resource =
601 wl_resource_create(client,
602 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700603 if (resource == NULL) {
604 wl_client_post_no_memory(client);
605 return;
606 }
607
608 wl_resource_set_implementation(resource, &manager_interface,
609 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400610}
611
612WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400613wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400614{
615 struct wl_resource *data_device, *focus, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700616 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400617
618 if (!seat->keyboard)
619 return;
620
621 focus = seat->keyboard->focus_resource;
622 if (!focus)
623 return;
624
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500625 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
626 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400627 if (!data_device)
628 return;
629
630 source = seat->selection_data_source;
631 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700632 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633 wl_data_device_send_selection(data_device, offer);
634 }
635}
636
637WL_EXPORT int
638wl_data_device_manager_init(struct wl_display *display)
639{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400640 if (wl_global_create(display,
641 &wl_data_device_manager_interface, 1,
642 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400643 return -1;
644
645 return 0;
646}