blob: 483e22e9dbd30415fc6b522b4edfba15970a83ec [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>
Jason Ekstranda7af7042013-10-12 22:38:11 -050029#include <assert.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040030
31#include "compositor.h"
32
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040033struct weston_drag {
34 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070035 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040036 struct wl_listener data_source_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050037 struct weston_view *focus;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040038 struct wl_resource *focus_resource;
39 struct wl_listener focus_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -050040 struct weston_view *icon;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040041 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040042 int32_t dx, dy;
43};
44
Xiong Zhangfd51e7b2013-11-25 18:42:49 +080045struct weston_pointer_drag {
46 struct weston_drag base;
47 struct weston_pointer_grab grab;
48};
49
50struct weston_touch_drag {
51 struct weston_drag base;
52 struct weston_touch_grab grab;
53};
54
Kristian Høgsberg2158a882013-04-18 15:07:39 -040055static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -040056empty_region(pixman_region32_t *region)
57{
58 pixman_region32_fini(region);
59 pixman_region32_init(region);
60}
61
62static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -040063data_offer_accept(struct wl_client *client, struct wl_resource *resource,
64 uint32_t serial, const char *mime_type)
65{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070066 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040067
68 /* FIXME: Check that client is currently focused by the input
69 * device that is currently dragging this data source. Should
70 * this be a wl_data_device request? */
71
72 if (offer->source)
73 offer->source->accept(offer->source, serial, mime_type);
74}
75
76static void
77data_offer_receive(struct wl_client *client, struct wl_resource *resource,
78 const char *mime_type, int32_t fd)
79{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070080 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040081
82 if (offer->source)
83 offer->source->send(offer->source, mime_type, fd);
84 else
85 close(fd);
86}
87
88static void
89data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
90{
91 wl_resource_destroy(resource);
92}
93
94static const struct wl_data_offer_interface data_offer_interface = {
95 data_offer_accept,
96 data_offer_receive,
97 data_offer_destroy,
98};
99
100static void
101destroy_data_offer(struct wl_resource *resource)
102{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700103 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400104
105 if (offer->source)
106 wl_list_remove(&offer->source_destroy_listener.link);
107 free(offer);
108}
109
110static void
111destroy_offer_data_source(struct wl_listener *listener, void *data)
112{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700113 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400114
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700115 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400116 source_destroy_listener);
117
118 offer->source = NULL;
119}
120
121static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700122weston_data_source_send_offer(struct weston_data_source *source,
123 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400124{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700125 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400126 char **p;
127
128 offer = malloc(sizeof *offer);
129 if (offer == NULL)
130 return NULL;
131
Jason Ekstranda85118c2013-06-27 20:17:02 -0500132 offer->resource =
133 wl_resource_create(wl_resource_get_client(target),
134 &wl_data_offer_interface, 1, 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700135 if (offer->resource == NULL) {
136 free(offer);
137 return NULL;
138 }
139
Jason Ekstranda85118c2013-06-27 20:17:02 -0500140 wl_resource_set_implementation(offer->resource, &data_offer_interface,
141 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400142
143 offer->source = source;
144 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500145 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146 &offer->source_destroy_listener);
147
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500148 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400149
150 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500151 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400152
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500153 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400154}
155
156static void
157data_source_offer(struct wl_client *client,
158 struct wl_resource *resource,
159 const char *type)
160{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700161 struct weston_data_source *source =
162 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400163 char **p;
164
165 p = wl_array_add(&source->mime_types, sizeof *p);
166 if (p)
167 *p = strdup(type);
168 if (!p || !*p)
169 wl_resource_post_no_memory(resource);
170}
171
172static void
173data_source_destroy(struct wl_client *client, struct wl_resource *resource)
174{
175 wl_resource_destroy(resource);
176}
177
178static struct wl_data_source_interface data_source_interface = {
179 data_source_offer,
180 data_source_destroy
181};
182
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400183static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800184drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100185 struct weston_pointer *pointer,
186 struct weston_touch *touch,
187 struct weston_surface *es,
188 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400189{
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400190 struct wl_list *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400191 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400192
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800193 assert((pointer != NULL && touch == NULL) ||
194 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500195
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400196 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800197 if (pointer && pointer->sprite &&
198 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400199 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400200 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500201 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400202
Jason Ekstranda7af7042013-10-12 22:38:11 -0500203 wl_list_remove(&drag->icon->layer_link);
204 wl_list_insert(list, &drag->icon->layer_link);
205 weston_view_update_transform(drag->icon);
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400206 empty_region(&es->pending.input);
207 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400208
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400209 drag->dx += sx;
210 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400211
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800212 /* init to 0 for avoiding a compile warning */
213 fx = fy = 0;
214 if (pointer) {
215 fx = wl_fixed_to_double(pointer->x) + drag->dx;
216 fy = wl_fixed_to_double(pointer->y) + drag->dy;
217 } else if (touch) {
218 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
219 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
220 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600221 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400222}
223
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400224static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100225pointer_drag_surface_configure(struct weston_surface *es,
226 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800227{
228 struct weston_pointer_drag *drag = es->configure_private;
229 struct weston_pointer *pointer = drag->grab.pointer;
230
231 assert(es->configure == pointer_drag_surface_configure);
232
Jonas Ådahl767d8912013-12-03 22:30:17 +0100233 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800234}
235
236static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100237touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800238{
239 struct weston_touch_drag *drag = es->configure_private;
240 struct weston_touch *touch = drag->grab.touch;
241
242 assert(es->configure == touch_drag_surface_configure);
243
Jonas Ådahl767d8912013-12-03 22:30:17 +0100244 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800245}
246
247static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400248destroy_drag_focus(struct wl_listener *listener, void *data)
249{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400250 struct weston_drag *drag =
251 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400252
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400253 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400254}
255
256static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800257weston_drag_set_focus(struct weston_drag *drag,
258 struct weston_seat *seat,
259 struct weston_view *view,
260 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400261{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400262 struct wl_resource *resource, *offer = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800263 struct wl_display *display = seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400264 uint32_t serial;
265
Jason Ekstranda7af7042013-10-12 22:38:11 -0500266 if (drag->focus && view && drag->focus->surface == view->surface) {
267 drag->focus = view;
268 return;
269 }
270
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400271 if (drag->focus_resource) {
272 wl_data_device_send_leave(drag->focus_resource);
273 wl_list_remove(&drag->focus_listener.link);
274 drag->focus_resource = NULL;
275 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400276 }
277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400279 return;
280
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500281 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400283 return;
284
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800285 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500286 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400287 if (!resource)
288 return;
289
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400290 serial = wl_display_next_serial(display);
291
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700292 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700293 offer = weston_data_source_send_offer(drag->data_source,
294 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700295 if (offer == NULL)
296 return;
297 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400298
Jason Ekstranda7af7042013-10-12 22:38:11 -0500299 wl_data_device_send_enter(resource, serial, view->surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400300 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400301
Jason Ekstranda7af7042013-10-12 22:38:11 -0500302 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400303 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500304 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400305 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400306}
307
308static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400309drag_grab_focus(struct weston_pointer_grab *grab)
310{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800311 struct weston_pointer_drag *drag =
312 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400313 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500314 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400315 wl_fixed_t sx, sy;
316
Jason Ekstranda7af7042013-10-12 22:38:11 -0500317 view = weston_compositor_pick_view(pointer->seat->compositor,
318 pointer->x, pointer->y,
319 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800320 if (drag->base.focus != view)
321 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400322}
323
324static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100325drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
326 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800328 struct weston_pointer_drag *drag =
329 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400330 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400331 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400332 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400333
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100334 weston_pointer_move(pointer, x, y);
335
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800336 if (drag->base.icon) {
337 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
338 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
339 weston_view_set_position(drag->base.icon, fx, fy);
340 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400341 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400342
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800343 if (drag->base.focus_resource) {
344 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500345 pointer->x, pointer->y,
346 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400347
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800348 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400349 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400350}
351
352static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800353data_device_end_drag_grab(struct weston_drag *drag,
354 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400355{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400356 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500357 if (weston_view_is_mapped(drag->icon))
358 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400359
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360 drag->icon->surface->configure = NULL;
361 empty_region(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400362 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500363 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400364 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400365
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800366 weston_drag_set_focus(drag, seat, NULL, 0, 0);
367}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400368
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800369static void
370data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
371{
372 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400373
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800374 data_device_end_drag_grab(&drag->base, pointer->seat);
375 weston_pointer_end_grab(pointer);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400376 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400377}
378
379static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400380drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400381 uint32_t time, uint32_t button, uint32_t state_w)
382{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800383 struct weston_pointer_drag *drag =
384 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400385 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400386 enum wl_pointer_button_state state = state_w;
387
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800388 if (drag->base.focus_resource &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400389 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400390 state == WL_POINTER_BUTTON_STATE_RELEASED)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800391 wl_data_device_send_drop(drag->base.focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400393 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400394 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800395 if (drag->base.data_source)
396 wl_list_remove(&drag->base.data_source_listener.link);
397 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398 }
399}
400
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200401static void
402drag_grab_cancel(struct weston_pointer_grab *grab)
403{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800404 struct weston_pointer_drag *drag =
405 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200406
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800407 if (drag->base.data_source)
408 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200409
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800410 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200411}
412
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800413static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400414 drag_grab_focus,
415 drag_grab_motion,
416 drag_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200417 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400418};
419
420static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800421drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
422 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400423{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800424}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400425
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800426static void
427data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
428{
429 struct weston_touch *touch = drag->grab.touch;
430
431 data_device_end_drag_grab(&drag->base, touch->seat);
432 weston_touch_end_grab(touch);
433 free(drag);
434}
435
436static void
437drag_grab_touch_up(struct weston_touch_grab *grab,
438 uint32_t time, int touch_id)
439{
440 struct weston_touch_drag *touch_drag =
441 container_of(grab, struct weston_touch_drag, grab);
442 struct weston_touch *touch = grab->touch;
443
444 if (touch_id != touch->grab_touch_id)
445 return;
446
447 if (touch_drag->base.focus_resource)
448 wl_data_device_send_drop(touch_drag->base.focus_resource);
449 if (touch_drag->base.data_source)
450 wl_list_remove(&touch_drag->base.data_source_listener.link);
451 data_device_end_touch_drag_grab(touch_drag);
452}
453
454static void
455drag_grab_touch_focus(struct weston_touch_drag *drag)
456{
457 struct weston_touch *touch = drag->grab.touch;
458 struct weston_view *view;
459 wl_fixed_t view_x, view_y;
460
461 view = weston_compositor_pick_view(touch->seat->compositor,
462 touch->grab_x, touch->grab_y,
463 &view_x, &view_y);
464 if (drag->base.focus != view)
465 weston_drag_set_focus(&drag->base, touch->seat,
466 view, view_x, view_y);
467}
468
469static void
470drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
471 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
472{
473 struct weston_touch_drag *touch_drag =
474 container_of(grab, struct weston_touch_drag, grab);
475 struct weston_touch *touch = grab->touch;
476 wl_fixed_t view_x, view_y;
477 float fx, fy;
478
479 if (touch_id != touch->grab_touch_id)
480 return;
481
482 drag_grab_touch_focus(touch_drag);
483 if (touch_drag->base.icon) {
484 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
485 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
486 weston_view_set_position(touch_drag->base.icon, fx, fy);
487 weston_view_schedule_repaint(touch_drag->base.icon);
488 }
489
490 if (touch_drag->base.focus_resource) {
491 weston_view_from_global_fixed(touch_drag->base.focus,
492 touch->grab_x, touch->grab_y,
493 &view_x, &view_y);
494 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
495 view_x, view_y);
496 }
497}
498
499static void
500drag_grab_touch_cancel(struct weston_touch_grab *grab)
501{
502 struct weston_touch_drag *touch_drag =
503 container_of(grab, struct weston_touch_drag, grab);
504
505 if (touch_drag->base.data_source)
506 wl_list_remove(&touch_drag->base.data_source_listener.link);
507 data_device_end_touch_drag_grab(touch_drag);
508}
509
510static const struct weston_touch_grab_interface touch_drag_grab_interface = {
511 drag_grab_touch_down,
512 drag_grab_touch_up,
513 drag_grab_touch_motion,
514 drag_grab_touch_cancel
515};
516
517static void
518destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
519{
520 struct weston_pointer_drag *drag = container_of(listener,
521 struct weston_pointer_drag, base.data_source_listener);
522
523 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524}
525
526static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400527handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400528{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400529 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400530 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400531
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400532 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400533}
534
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700535WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800536weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700537 struct weston_data_source *source,
538 struct weston_surface *icon,
539 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800541 struct weston_pointer_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400542
Peter Huttererf3d62272013-08-08 11:57:05 +1000543 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700544 if (drag == NULL)
545 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800547 drag->grab.interface = &pointer_drag_grab_interface;
548 drag->base.client = client;
549 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400550
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400551 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800552 drag->base.icon = weston_view_create(icon);
553 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554 free(drag);
555 return -1;
556 }
557
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800558 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500559 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800560 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400561
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800562 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400563 icon->configure_private = drag;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500564 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800565 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500566 }
567
568 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800569 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500570 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800571 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400572 }
573
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800574 weston_pointer_set_focus(pointer, NULL,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400575 wl_fixed_from_int(0), wl_fixed_from_int(0));
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800576 weston_pointer_start_grab(pointer, &drag->grab);
577
578 return 0;
579}
580
581static void
582destroy_touch_data_device_source(struct wl_listener *listener, void *data)
583{
584 struct weston_touch_drag *drag = container_of(listener,
585 struct weston_touch_drag, base.data_source_listener);
586
587 data_device_end_touch_drag_grab(drag);
588}
589
590WL_EXPORT int
591weston_touch_start_drag(struct weston_touch *touch,
592 struct weston_data_source *source,
593 struct weston_surface *icon,
594 struct wl_client *client)
595{
596 struct weston_touch_drag *drag;
597
598 drag = zalloc(sizeof *drag);
599 if (drag == NULL)
600 return -1;
601
602 drag->grab.interface = &touch_drag_grab_interface;
603 drag->base.client = client;
604 drag->base.data_source = source;
605
606 if (icon) {
607 drag->base.icon = weston_view_create(icon);
608 if (drag->base.icon == NULL) {
609 free(drag);
610 return -1;
611 }
612
613 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
614 wl_signal_add(&icon->destroy_signal,
615 &drag->base.icon_destroy_listener);
616
617 icon->configure = touch_drag_surface_configure;
618 icon->configure_private = drag;
619 } else {
620 drag->base.icon = NULL;
621 }
622
623 if (source) {
624 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
625 wl_signal_add(&source->destroy_signal,
626 &drag->base.data_source_listener);
627 }
628
629 weston_touch_start_grab(touch, &drag->grab);
630
631 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700632
633 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400634}
635
636static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700637data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
638 struct wl_resource *source_resource,
639 struct wl_resource *origin_resource,
640 struct wl_resource *icon_resource, uint32_t serial)
641{
642 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700643 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700644 struct weston_surface *icon = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800645 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700646
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800647 if ((seat->pointer->button_count == 0 ||
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700648 seat->pointer->grab_serial != serial ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500649 !seat->pointer->focus ||
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800650 seat->pointer->focus->surface != wl_resource_get_user_data(origin_resource)) &&
651 (seat->touch->grab_serial != serial ||
652 !seat->touch->focus ||
653 seat->touch->focus->surface != wl_resource_get_user_data(origin_resource)))
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700654 return;
655
656 /* FIXME: Check that the data source type array isn't empty. */
657
658 if (source_resource)
659 source = wl_resource_get_user_data(source_resource);
660 if (icon_resource)
661 icon = wl_resource_get_user_data(icon_resource);
662 if (icon && icon->configure) {
663 wl_resource_post_error(icon_resource,
664 WL_DISPLAY_ERROR_INVALID_OBJECT,
665 "surface->configure already set");
666 return;
667 }
668
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800669 if (seat->pointer->button_count == 1 &&
670 seat->pointer->grab_serial == serial &&
671 seat->pointer->focus &&
672 seat->pointer->focus->surface == wl_resource_get_user_data(origin_resource))
673 ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
674 else if (seat->touch->grab_serial != serial ||
675 seat->touch->focus ||
676 seat->touch->focus->surface != wl_resource_get_user_data(origin_resource))
677 ret = weston_touch_start_drag(seat->touch, source, icon, client);
678
679 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700680 wl_resource_post_no_memory(resource);
681}
682
683static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400684destroy_selection_data_source(struct wl_listener *listener, void *data)
685{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400686 struct weston_seat *seat = container_of(listener, struct weston_seat,
687 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400688 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100689 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400690
691 seat->selection_data_source = NULL;
692
693 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100694 focus = seat->keyboard->focus;
695 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500696 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100697 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400698 if (data_device)
699 wl_data_device_send_selection(data_device, NULL);
700 }
701
702 wl_signal_emit(&seat->selection_signal, seat);
703}
704
705WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400706weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700707 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400708{
709 struct wl_resource *data_device, *offer;
Neil Roberts96d790e2013-09-19 17:32:00 +0100710 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400711
712 if (seat->selection_data_source &&
713 seat->selection_serial - serial < UINT32_MAX / 2)
714 return;
715
716 if (seat->selection_data_source) {
717 seat->selection_data_source->cancel(seat->selection_data_source);
718 wl_list_remove(&seat->selection_data_source_listener.link);
719 seat->selection_data_source = NULL;
720 }
721
722 seat->selection_data_source = source;
723 seat->selection_serial = serial;
724
725 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100726 focus = seat->keyboard->focus;
727 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500728 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100729 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400730 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700731 offer = weston_data_source_send_offer(seat->selection_data_source,
732 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400733 wl_data_device_send_selection(data_device, offer);
734 } else if (data_device) {
735 wl_data_device_send_selection(data_device, NULL);
736 }
737 }
738
739 wl_signal_emit(&seat->selection_signal, seat);
740
741 if (source) {
742 seat->selection_data_source_listener.notify =
743 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500744 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400745 &seat->selection_data_source_listener);
746 }
747}
748
749static void
750data_device_set_selection(struct wl_client *client,
751 struct wl_resource *resource,
752 struct wl_resource *source_resource, uint32_t serial)
753{
754 if (!source_resource)
755 return;
756
757 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500758 weston_seat_set_selection(wl_resource_get_user_data(resource),
759 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400760 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400761}
762
763static const struct wl_data_device_interface data_device_interface = {
764 data_device_start_drag,
765 data_device_set_selection,
766};
767
768static void
769destroy_data_source(struct wl_resource *resource)
770{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700771 struct weston_data_source *source =
772 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400773 char **p;
774
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500775 wl_signal_emit(&source->destroy_signal, source);
776
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400777 wl_array_for_each(p, &source->mime_types)
778 free(*p);
779
780 wl_array_release(&source->mime_types);
781
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400782 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400783}
784
785static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700786client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400787 uint32_t time, const char *mime_type)
788{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500789 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400790}
791
792static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700793client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400794 const char *mime_type, int32_t fd)
795{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500796 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400797 close(fd);
798}
799
800static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700801client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400802{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500803 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400804}
805
806static void
807create_data_source(struct wl_client *client,
808 struct wl_resource *resource, uint32_t id)
809{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700810 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400811
812 source = malloc(sizeof *source);
813 if (source == NULL) {
814 wl_resource_post_no_memory(resource);
815 return;
816 }
817
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500818 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400819 source->accept = client_source_accept;
820 source->send = client_source_send;
821 source->cancel = client_source_cancel;
822
823 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500824
Jason Ekstranda85118c2013-06-27 20:17:02 -0500825 source->resource =
826 wl_resource_create(client, &wl_data_source_interface, 1, id);
827 wl_resource_set_implementation(source->resource, &data_source_interface,
828 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400829}
830
831static void unbind_data_device(struct wl_resource *resource)
832{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500833 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400834}
835
836static void
837get_data_device(struct wl_client *client,
838 struct wl_resource *manager_resource,
839 uint32_t id, struct wl_resource *seat_resource)
840{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500841 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400842 struct wl_resource *resource;
843
Jason Ekstranda85118c2013-06-27 20:17:02 -0500844 resource = wl_resource_create(client,
845 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700846 if (resource == NULL) {
847 wl_resource_post_no_memory(manager_resource);
848 return;
849 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400850
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700851 wl_list_insert(&seat->drag_resource_list,
852 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500853 wl_resource_set_implementation(resource, &data_device_interface,
854 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400855}
856
857static const struct wl_data_device_manager_interface manager_interface = {
858 create_data_source,
859 get_data_device
860};
861
862static void
863bind_manager(struct wl_client *client,
864 void *data, uint32_t version, uint32_t id)
865{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500866 struct wl_resource *resource;
867
868 resource =
869 wl_resource_create(client,
870 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700871 if (resource == NULL) {
872 wl_client_post_no_memory(client);
873 return;
874 }
875
876 wl_resource_set_implementation(resource, &manager_interface,
877 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400878}
879
880WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400881wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400882{
Neil Roberts96d790e2013-09-19 17:32:00 +0100883 struct wl_resource *data_device, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700884 struct weston_data_source *source;
Neil Roberts96d790e2013-09-19 17:32:00 +0100885 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400886
887 if (!seat->keyboard)
888 return;
889
Neil Roberts96d790e2013-09-19 17:32:00 +0100890 focus = seat->keyboard->focus;
891 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400892 return;
893
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500894 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100895 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400896 if (!data_device)
897 return;
898
899 source = seat->selection_data_source;
900 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700901 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400902 wl_data_device_send_selection(data_device, offer);
903 }
904}
905
906WL_EXPORT int
907wl_data_device_manager_init(struct wl_display *display)
908{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400909 if (wl_global_create(display,
910 &wl_data_device_manager_interface, 1,
911 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400912 return -1;
913
914 return 0;
915}