blob: f522eb51f16263992601865ce19985e1b53eaea6 [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;
32 struct wl_data_source *data_source;
33 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{
54 struct wl_data_offer *offer = resource->data;
55
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{
68 struct wl_data_offer *offer = resource->data;
69
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{
91 struct wl_data_offer *offer = resource->data;
92
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{
101 struct wl_data_offer *offer;
102
103 offer = container_of(listener, struct wl_data_offer,
104 source_destroy_listener);
105
106 offer->source = NULL;
107}
108
109static struct wl_resource *
110wl_data_source_send_offer(struct wl_data_source *source,
111 struct wl_resource *target)
112{
113 struct wl_data_offer *offer;
114 char **p;
115
116 offer = malloc(sizeof *offer);
117 if (offer == NULL)
118 return NULL;
119
120 wl_resource_init(&offer->resource, &wl_data_offer_interface,
121 &data_offer_interface, 0, offer);
122 offer->resource.destroy = destroy_data_offer;
123
124 offer->source = source;
125 offer->source_destroy_listener.notify = destroy_offer_data_source;
126 wl_signal_add(&source->resource.destroy_signal,
127 &offer->source_destroy_listener);
128
129 wl_client_add_resource(target->client, &offer->resource);
130
131 wl_data_device_send_data_offer(target, &offer->resource);
132
133 wl_array_for_each(p, &source->mime_types)
134 wl_data_offer_send_offer(&offer->resource, *p);
135
136 return &offer->resource;
137}
138
139static void
140data_source_offer(struct wl_client *client,
141 struct wl_resource *resource,
142 const char *type)
143{
144 struct wl_data_source *source = resource->data;
145 char **p;
146
147 p = wl_array_add(&source->mime_types, sizeof *p);
148 if (p)
149 *p = strdup(type);
150 if (!p || !*p)
151 wl_resource_post_no_memory(resource);
152}
153
154static void
155data_source_destroy(struct wl_client *client, struct wl_resource *resource)
156{
157 wl_resource_destroy(resource);
158}
159
160static struct wl_data_source_interface data_source_interface = {
161 data_source_offer,
162 data_source_destroy
163};
164
165static struct wl_resource *
166find_resource(struct wl_list *list, struct wl_client *client)
167{
168 struct wl_resource *r;
169
170 wl_list_for_each(r, list, link) {
171 if (r->client == client)
172 return r;
173 }
174
175 return NULL;
176}
177
178static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400179drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
180{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400181 struct weston_drag *drag = es->configure_private;
182 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400183 struct wl_list *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400184 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400185
186 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400187 if (pointer->sprite && weston_surface_is_mapped(pointer->sprite))
188 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400189 else
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400190 list = &es->compositor->cursor_layer.surface_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400191
192 wl_list_insert(list, &es->layer_link);
193 weston_surface_update_transform(es);
194 empty_region(&es->pending.input);
195 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400196
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400197 drag->dx += sx;
198 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400199
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400200 fx = wl_fixed_to_double(pointer->x) + drag->dx;
201 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400202 weston_surface_configure(es, fx, fy, width, height);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400203}
204
205static int
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400206device_setup_new_drag_surface(struct weston_drag *drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400207 struct weston_surface *icon)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400208{
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400209 if (icon->configure) {
210 wl_resource_post_error(&icon->resource,
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400211 WL_DISPLAY_ERROR_INVALID_OBJECT,
212 "surface->configure already set");
213 return 0;
214 }
215
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400216 drag->icon = icon;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400217 drag->dx = 0;
218 drag->dy = 0;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400219
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400220 icon->configure = drag_surface_configure;
221 icon->configure_private = drag;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400222
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400223 wl_signal_add(&icon->resource.destroy_signal,
224 &drag->icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400225
226 return 1;
227}
228
229static void
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400230device_release_drag_surface(struct weston_drag *drag)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400231{
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400232 if (weston_surface_is_mapped(drag->icon))
233 weston_surface_unmap(drag->icon);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400234
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400235 drag->icon->configure = NULL;
236 empty_region(&drag->icon->pending.input);
237 wl_list_remove(&drag->icon_destroy_listener.link);
238 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400239}
240
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400241static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400242destroy_drag_focus(struct wl_listener *listener, void *data)
243{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400244 struct weston_drag *drag =
245 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400246
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400247 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400248}
249
250static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400251drag_grab_focus(struct weston_pointer_grab *grab,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400252 struct weston_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400253{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400254 struct weston_drag *drag =
255 container_of(grab, struct weston_drag, grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400256 struct wl_resource *resource, *offer = NULL;
257 struct wl_display *display;
258 uint32_t serial;
259
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400260 if (drag->focus_resource) {
261 wl_data_device_send_leave(drag->focus_resource);
262 wl_list_remove(&drag->focus_listener.link);
263 drag->focus_resource = NULL;
264 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400265 }
266
267 if (!surface)
268 return;
269
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400270 if (!drag->data_source && surface->resource.client != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400271 return;
272
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400273 resource = find_resource(&drag->grab.pointer->seat->drag_resource_list,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400274 surface->resource.client);
275 if (!resource)
276 return;
277
278 display = wl_client_get_display(resource->client);
279 serial = wl_display_next_serial(display);
280
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400281 if (drag->data_source)
282 offer = wl_data_source_send_offer(drag->data_source, resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400283
284 wl_data_device_send_enter(resource, serial, &surface->resource,
285 x, y, offer);
286
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400287 drag->focus = surface;
288 drag->focus_listener.notify = destroy_drag_focus;
289 wl_signal_add(&resource->destroy_signal, &drag->focus_listener);
290 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400291 grab->focus = surface;
292}
293
294static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400295drag_grab_motion(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400296 uint32_t time, wl_fixed_t x, wl_fixed_t y)
297{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400298 struct weston_drag *drag =
299 container_of(grab, struct weston_drag, grab);
300 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400301 float fx, fy;
302
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400303 if (drag->icon) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400304 fx = wl_fixed_to_double(pointer->x) + drag->dx;
305 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400306 weston_surface_set_position(drag->icon, fx, fy);
307 weston_surface_schedule_repaint(drag->icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400308 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400309
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400310 if (drag->focus_resource)
311 wl_data_device_send_motion(drag->focus_resource, time, x, y);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400312}
313
314static void
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400315data_device_end_drag_grab(struct weston_drag *drag)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400316{
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400317 if (drag->icon)
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400318 device_release_drag_surface(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400319
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400320 drag_grab_focus(&drag->grab, NULL,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321 wl_fixed_from_int(0), wl_fixed_from_int(0));
322
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400323 weston_pointer_end_grab(drag->grab.pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400324
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400325 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326}
327
328static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330 uint32_t time, uint32_t button, uint32_t state_w)
331{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400332 struct weston_drag *drag =
333 container_of(grab, struct weston_drag, grab);
334 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400335 enum wl_pointer_button_state state = state_w;
336
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400337 if (drag->focus_resource &&
338 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400339 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400340 wl_data_device_send_drop(drag->focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400342 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400343 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400344 if (drag->data_source)
345 wl_list_remove(&drag->data_source_listener.link);
346 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400347 }
348}
349
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400350static const struct weston_pointer_grab_interface drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400351 drag_grab_focus,
352 drag_grab_motion,
353 drag_grab_button,
354};
355
356static void
357destroy_data_device_source(struct wl_listener *listener, void *data)
358{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400359 struct weston_drag *drag = container_of(listener, struct weston_drag,
360 data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400361
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400362 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400363}
364
365static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400366handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400367{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400368 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400369 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400370
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400371 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400372}
373
374static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400375data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
376 struct wl_resource *source_resource,
377 struct wl_resource *origin_resource,
378 struct wl_resource *icon_resource, uint32_t serial)
379{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400380 struct weston_seat *seat = resource->data;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400381 struct weston_drag *drag = resource->data;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400382
383 /* FIXME: Check that client has implicit grab on the origin
384 * surface that matches the given time. */
385
386 /* FIXME: Check that the data source type array isn't empty. */
387
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400388 drag = malloc(sizeof *drag);
389 if (drag == NULL) {
390 wl_resource_post_no_memory(resource);
391 return;
392 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400393
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400394 memset(drag, 0, sizeof *drag);
395 drag->grab.interface = &drag_grab_interface;
396 drag->client = client;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400397 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398
399 if (source_resource) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400400 drag->data_source = source_resource->data;
401 drag->data_source_listener.notify = destroy_data_device_source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402 wl_signal_add(&source_resource->destroy_signal,
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400403 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404 }
405
406 if (icon_resource) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400407 if (!device_setup_new_drag_surface(drag, icon_resource->data))
Kristian Høgsberg5f955572013-05-07 21:06:38 -0400408 return;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400409 }
410
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400411 weston_pointer_set_focus(seat->pointer, NULL,
412 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400413 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400414}
415
416static void
417destroy_selection_data_source(struct wl_listener *listener, void *data)
418{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400419 struct weston_seat *seat = container_of(listener, struct weston_seat,
420 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400421 struct wl_resource *data_device;
422 struct wl_resource *focus = NULL;
423
424 seat->selection_data_source = NULL;
425
426 if (seat->keyboard)
427 focus = seat->keyboard->focus_resource;
428 if (focus) {
429 data_device = find_resource(&seat->drag_resource_list,
430 focus->client);
431 if (data_device)
432 wl_data_device_send_selection(data_device, NULL);
433 }
434
435 wl_signal_emit(&seat->selection_signal, seat);
436}
437
438WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400439weston_seat_set_selection(struct weston_seat *seat,
440 struct wl_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400441{
442 struct wl_resource *data_device, *offer;
443 struct wl_resource *focus = NULL;
444
445 if (seat->selection_data_source &&
446 seat->selection_serial - serial < UINT32_MAX / 2)
447 return;
448
449 if (seat->selection_data_source) {
450 seat->selection_data_source->cancel(seat->selection_data_source);
451 wl_list_remove(&seat->selection_data_source_listener.link);
452 seat->selection_data_source = NULL;
453 }
454
455 seat->selection_data_source = source;
456 seat->selection_serial = serial;
457
458 if (seat->keyboard)
459 focus = seat->keyboard->focus_resource;
460 if (focus) {
461 data_device = find_resource(&seat->drag_resource_list,
462 focus->client);
463 if (data_device && source) {
464 offer = wl_data_source_send_offer(seat->selection_data_source,
465 data_device);
466 wl_data_device_send_selection(data_device, offer);
467 } else if (data_device) {
468 wl_data_device_send_selection(data_device, NULL);
469 }
470 }
471
472 wl_signal_emit(&seat->selection_signal, seat);
473
474 if (source) {
475 seat->selection_data_source_listener.notify =
476 destroy_selection_data_source;
477 wl_signal_add(&source->resource.destroy_signal,
478 &seat->selection_data_source_listener);
479 }
480}
481
482static void
483data_device_set_selection(struct wl_client *client,
484 struct wl_resource *resource,
485 struct wl_resource *source_resource, uint32_t serial)
486{
487 if (!source_resource)
488 return;
489
490 /* FIXME: Store serial and check against incoming serial here. */
Kristian Høgsberge3148752013-05-06 23:19:49 -0400491 weston_seat_set_selection(resource->data, source_resource->data,
492 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{
503 struct wl_data_source *source =
504 container_of(resource, struct wl_data_source, resource);
505 char **p;
506
507 wl_array_for_each(p, &source->mime_types)
508 free(*p);
509
510 wl_array_release(&source->mime_types);
511
512 source->resource.object.id = 0;
513}
514
515static void
516client_source_accept(struct wl_data_source *source,
517 uint32_t time, const char *mime_type)
518{
519 wl_data_source_send_target(&source->resource, mime_type);
520}
521
522static void
523client_source_send(struct wl_data_source *source,
524 const char *mime_type, int32_t fd)
525{
526 wl_data_source_send_send(&source->resource, mime_type, fd);
527 close(fd);
528}
529
530static void
531client_source_cancel(struct wl_data_source *source)
532{
533 wl_data_source_send_cancelled(&source->resource);
534}
535
536static void
537create_data_source(struct wl_client *client,
538 struct wl_resource *resource, uint32_t id)
539{
540 struct wl_data_source *source;
541
542 source = malloc(sizeof *source);
543 if (source == NULL) {
544 wl_resource_post_no_memory(resource);
545 return;
546 }
547
548 wl_resource_init(&source->resource, &wl_data_source_interface,
549 &data_source_interface, id, source);
550 source->resource.destroy = destroy_data_source;
551
552 source->accept = client_source_accept;
553 source->send = client_source_send;
554 source->cancel = client_source_cancel;
555
556 wl_array_init(&source->mime_types);
557 wl_client_add_resource(client, &source->resource);
558}
559
560static void unbind_data_device(struct wl_resource *resource)
561{
562 wl_list_remove(&resource->link);
563 free(resource);
564}
565
566static void
567get_data_device(struct wl_client *client,
568 struct wl_resource *manager_resource,
569 uint32_t id, struct wl_resource *seat_resource)
570{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400571 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400572 struct wl_resource *resource;
573
574 resource = wl_client_add_object(client, &wl_data_device_interface,
575 &data_device_interface, id,
576 seat);
577
578 wl_list_insert(&seat->drag_resource_list, &resource->link);
579 resource->destroy = unbind_data_device;
580}
581
582static const struct wl_data_device_manager_interface manager_interface = {
583 create_data_source,
584 get_data_device
585};
586
587static void
588bind_manager(struct wl_client *client,
589 void *data, uint32_t version, uint32_t id)
590{
591 wl_client_add_object(client, &wl_data_device_manager_interface,
592 &manager_interface, id, NULL);
593}
594
595WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400596wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400597{
598 struct wl_resource *data_device, *focus, *offer;
599 struct wl_data_source *source;
600
601 if (!seat->keyboard)
602 return;
603
604 focus = seat->keyboard->focus_resource;
605 if (!focus)
606 return;
607
608 data_device = find_resource(&seat->drag_resource_list,
609 focus->client);
610 if (!data_device)
611 return;
612
613 source = seat->selection_data_source;
614 if (source) {
615 offer = wl_data_source_send_offer(source, data_device);
616 wl_data_device_send_selection(data_device, offer);
617 }
618}
619
620WL_EXPORT int
621wl_data_device_manager_init(struct wl_display *display)
622{
623 if (wl_display_add_global(display,
624 &wl_data_device_manager_interface,
625 NULL, bind_manager) == NULL)
626 return -1;
627
628 return 0;
629}