blob: 5ef69d19d13c6fe5c0b2b106c3bb8024cae3b5f1 [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,
185 struct weston_pointer *pointer,
186 struct weston_touch *touch,
187 struct weston_surface *es, int32_t sx, int32_t sy,
188 int32_t width, int32_t height)
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 Ekstranda7af7042013-10-12 22:38:11 -0500221 weston_view_configure(drag->icon, fx, fy, width, height);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400222}
223
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400224static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800225pointer_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
226{
227 struct weston_pointer_drag *drag = es->configure_private;
228 struct weston_pointer *pointer = drag->grab.pointer;
229
230 assert(es->configure == pointer_drag_surface_configure);
231
232 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy, width, height);
233}
234
235static void
236touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
237{
238 struct weston_touch_drag *drag = es->configure_private;
239 struct weston_touch *touch = drag->grab.touch;
240
241 assert(es->configure == touch_drag_surface_configure);
242
243 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy, width, height);
244}
245
246static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400247destroy_drag_focus(struct wl_listener *listener, void *data)
248{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400249 struct weston_drag *drag =
250 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400251
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400252 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400253}
254
255static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800256weston_drag_set_focus(struct weston_drag *drag,
257 struct weston_seat *seat,
258 struct weston_view *view,
259 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400260{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400261 struct wl_resource *resource, *offer = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800262 struct wl_display *display = seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400263 uint32_t serial;
264
Jason Ekstranda7af7042013-10-12 22:38:11 -0500265 if (drag->focus && view && drag->focus->surface == view->surface) {
266 drag->focus = view;
267 return;
268 }
269
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400270 if (drag->focus_resource) {
271 wl_data_device_send_leave(drag->focus_resource);
272 wl_list_remove(&drag->focus_listener.link);
273 drag->focus_resource = NULL;
274 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400275 }
276
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400278 return;
279
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500280 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400282 return;
283
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800284 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400286 if (!resource)
287 return;
288
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400289 serial = wl_display_next_serial(display);
290
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700291 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700292 offer = weston_data_source_send_offer(drag->data_source,
293 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700294 if (offer == NULL)
295 return;
296 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400297
Jason Ekstranda7af7042013-10-12 22:38:11 -0500298 wl_data_device_send_enter(resource, serial, view->surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400299 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400300
Jason Ekstranda7af7042013-10-12 22:38:11 -0500301 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400302 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500303 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400304 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400305}
306
307static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400308drag_grab_focus(struct weston_pointer_grab *grab)
309{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800310 struct weston_pointer_drag *drag =
311 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400312 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500313 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400314 wl_fixed_t sx, sy;
315
Jason Ekstranda7af7042013-10-12 22:38:11 -0500316 view = weston_compositor_pick_view(pointer->seat->compositor,
317 pointer->x, pointer->y,
318 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800319 if (drag->base.focus != view)
320 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400321}
322
323static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100324drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
325 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800327 struct weston_pointer_drag *drag =
328 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400329 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400330 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400331 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400332
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100333 weston_pointer_move(pointer, x, y);
334
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800335 if (drag->base.icon) {
336 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
337 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
338 weston_view_set_position(drag->base.icon, fx, fy);
339 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400340 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800342 if (drag->base.focus_resource) {
343 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500344 pointer->x, pointer->y,
345 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400346
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800347 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400348 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400349}
350
351static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800352data_device_end_drag_grab(struct weston_drag *drag,
353 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400354{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400355 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500356 if (weston_view_is_mapped(drag->icon))
357 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400358
Jason Ekstranda7af7042013-10-12 22:38:11 -0500359 drag->icon->surface->configure = NULL;
360 empty_region(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400361 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500362 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400363 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400364
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800365 weston_drag_set_focus(drag, seat, NULL, 0, 0);
366}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400367
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800368static void
369data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
370{
371 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400372
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800373 data_device_end_drag_grab(&drag->base, pointer->seat);
374 weston_pointer_end_grab(pointer);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400375 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400376}
377
378static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400379drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400380 uint32_t time, uint32_t button, uint32_t state_w)
381{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800382 struct weston_pointer_drag *drag =
383 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400384 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385 enum wl_pointer_button_state state = state_w;
386
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800387 if (drag->base.focus_resource &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400388 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400389 state == WL_POINTER_BUTTON_STATE_RELEASED)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800390 wl_data_device_send_drop(drag->base.focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400391
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400392 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400393 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800394 if (drag->base.data_source)
395 wl_list_remove(&drag->base.data_source_listener.link);
396 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400397 }
398}
399
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200400static void
401drag_grab_cancel(struct weston_pointer_grab *grab)
402{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800403 struct weston_pointer_drag *drag =
404 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200405
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800406 if (drag->base.data_source)
407 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200408
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800409 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200410}
411
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800412static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400413 drag_grab_focus,
414 drag_grab_motion,
415 drag_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200416 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400417};
418
419static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800420drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
421 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400422{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800423}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400424
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800425static void
426data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
427{
428 struct weston_touch *touch = drag->grab.touch;
429
430 data_device_end_drag_grab(&drag->base, touch->seat);
431 weston_touch_end_grab(touch);
432 free(drag);
433}
434
435static void
436drag_grab_touch_up(struct weston_touch_grab *grab,
437 uint32_t time, int touch_id)
438{
439 struct weston_touch_drag *touch_drag =
440 container_of(grab, struct weston_touch_drag, grab);
441 struct weston_touch *touch = grab->touch;
442
443 if (touch_id != touch->grab_touch_id)
444 return;
445
446 if (touch_drag->base.focus_resource)
447 wl_data_device_send_drop(touch_drag->base.focus_resource);
448 if (touch_drag->base.data_source)
449 wl_list_remove(&touch_drag->base.data_source_listener.link);
450 data_device_end_touch_drag_grab(touch_drag);
451}
452
453static void
454drag_grab_touch_focus(struct weston_touch_drag *drag)
455{
456 struct weston_touch *touch = drag->grab.touch;
457 struct weston_view *view;
458 wl_fixed_t view_x, view_y;
459
460 view = weston_compositor_pick_view(touch->seat->compositor,
461 touch->grab_x, touch->grab_y,
462 &view_x, &view_y);
463 if (drag->base.focus != view)
464 weston_drag_set_focus(&drag->base, touch->seat,
465 view, view_x, view_y);
466}
467
468static void
469drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
470 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
471{
472 struct weston_touch_drag *touch_drag =
473 container_of(grab, struct weston_touch_drag, grab);
474 struct weston_touch *touch = grab->touch;
475 wl_fixed_t view_x, view_y;
476 float fx, fy;
477
478 if (touch_id != touch->grab_touch_id)
479 return;
480
481 drag_grab_touch_focus(touch_drag);
482 if (touch_drag->base.icon) {
483 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
484 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
485 weston_view_set_position(touch_drag->base.icon, fx, fy);
486 weston_view_schedule_repaint(touch_drag->base.icon);
487 }
488
489 if (touch_drag->base.focus_resource) {
490 weston_view_from_global_fixed(touch_drag->base.focus,
491 touch->grab_x, touch->grab_y,
492 &view_x, &view_y);
493 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
494 view_x, view_y);
495 }
496}
497
498static void
499drag_grab_touch_cancel(struct weston_touch_grab *grab)
500{
501 struct weston_touch_drag *touch_drag =
502 container_of(grab, struct weston_touch_drag, grab);
503
504 if (touch_drag->base.data_source)
505 wl_list_remove(&touch_drag->base.data_source_listener.link);
506 data_device_end_touch_drag_grab(touch_drag);
507}
508
509static const struct weston_touch_grab_interface touch_drag_grab_interface = {
510 drag_grab_touch_down,
511 drag_grab_touch_up,
512 drag_grab_touch_motion,
513 drag_grab_touch_cancel
514};
515
516static void
517destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
518{
519 struct weston_pointer_drag *drag = container_of(listener,
520 struct weston_pointer_drag, base.data_source_listener);
521
522 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400523}
524
525static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400526handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400527{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400528 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400529 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400530
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400531 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400532}
533
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700534WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800535weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700536 struct weston_data_source *source,
537 struct weston_surface *icon,
538 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400539{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800540 struct weston_pointer_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400541
Peter Huttererf3d62272013-08-08 11:57:05 +1000542 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700543 if (drag == NULL)
544 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400545
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800546 drag->grab.interface = &pointer_drag_grab_interface;
547 drag->base.client = client;
548 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400549
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400550 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800551 drag->base.icon = weston_view_create(icon);
552 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500553 free(drag);
554 return -1;
555 }
556
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800557 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500558 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800559 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400560
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800561 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400562 icon->configure_private = drag;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800564 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500565 }
566
567 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800568 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500569 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800570 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400571 }
572
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800573 weston_pointer_set_focus(pointer, NULL,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400574 wl_fixed_from_int(0), wl_fixed_from_int(0));
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800575 weston_pointer_start_grab(pointer, &drag->grab);
576
577 return 0;
578}
579
580static void
581destroy_touch_data_device_source(struct wl_listener *listener, void *data)
582{
583 struct weston_touch_drag *drag = container_of(listener,
584 struct weston_touch_drag, base.data_source_listener);
585
586 data_device_end_touch_drag_grab(drag);
587}
588
589WL_EXPORT int
590weston_touch_start_drag(struct weston_touch *touch,
591 struct weston_data_source *source,
592 struct weston_surface *icon,
593 struct wl_client *client)
594{
595 struct weston_touch_drag *drag;
596
597 drag = zalloc(sizeof *drag);
598 if (drag == NULL)
599 return -1;
600
601 drag->grab.interface = &touch_drag_grab_interface;
602 drag->base.client = client;
603 drag->base.data_source = source;
604
605 if (icon) {
606 drag->base.icon = weston_view_create(icon);
607 if (drag->base.icon == NULL) {
608 free(drag);
609 return -1;
610 }
611
612 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
613 wl_signal_add(&icon->destroy_signal,
614 &drag->base.icon_destroy_listener);
615
616 icon->configure = touch_drag_surface_configure;
617 icon->configure_private = drag;
618 } else {
619 drag->base.icon = NULL;
620 }
621
622 if (source) {
623 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
624 wl_signal_add(&source->destroy_signal,
625 &drag->base.data_source_listener);
626 }
627
628 weston_touch_start_grab(touch, &drag->grab);
629
630 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700631
632 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633}
634
635static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700636data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
637 struct wl_resource *source_resource,
638 struct wl_resource *origin_resource,
639 struct wl_resource *icon_resource, uint32_t serial)
640{
641 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700642 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700643 struct weston_surface *icon = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800644 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700645
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800646 if ((seat->pointer->button_count == 0 ||
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700647 seat->pointer->grab_serial != serial ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500648 !seat->pointer->focus ||
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800649 seat->pointer->focus->surface != wl_resource_get_user_data(origin_resource)) &&
650 (seat->touch->grab_serial != serial ||
651 !seat->touch->focus ||
652 seat->touch->focus->surface != wl_resource_get_user_data(origin_resource)))
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700653 return;
654
655 /* FIXME: Check that the data source type array isn't empty. */
656
657 if (source_resource)
658 source = wl_resource_get_user_data(source_resource);
659 if (icon_resource)
660 icon = wl_resource_get_user_data(icon_resource);
661 if (icon && icon->configure) {
662 wl_resource_post_error(icon_resource,
663 WL_DISPLAY_ERROR_INVALID_OBJECT,
664 "surface->configure already set");
665 return;
666 }
667
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800668 if (seat->pointer->button_count == 1 &&
669 seat->pointer->grab_serial == serial &&
670 seat->pointer->focus &&
671 seat->pointer->focus->surface == wl_resource_get_user_data(origin_resource))
672 ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
673 else if (seat->touch->grab_serial != serial ||
674 seat->touch->focus ||
675 seat->touch->focus->surface != wl_resource_get_user_data(origin_resource))
676 ret = weston_touch_start_drag(seat->touch, source, icon, client);
677
678 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700679 wl_resource_post_no_memory(resource);
680}
681
682static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400683destroy_selection_data_source(struct wl_listener *listener, void *data)
684{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400685 struct weston_seat *seat = container_of(listener, struct weston_seat,
686 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400687 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100688 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400689
690 seat->selection_data_source = NULL;
691
692 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100693 focus = seat->keyboard->focus;
694 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500695 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100696 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400697 if (data_device)
698 wl_data_device_send_selection(data_device, NULL);
699 }
700
701 wl_signal_emit(&seat->selection_signal, seat);
702}
703
704WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400705weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700706 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400707{
708 struct wl_resource *data_device, *offer;
Neil Roberts96d790e2013-09-19 17:32:00 +0100709 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400710
711 if (seat->selection_data_source &&
712 seat->selection_serial - serial < UINT32_MAX / 2)
713 return;
714
715 if (seat->selection_data_source) {
716 seat->selection_data_source->cancel(seat->selection_data_source);
717 wl_list_remove(&seat->selection_data_source_listener.link);
718 seat->selection_data_source = NULL;
719 }
720
721 seat->selection_data_source = source;
722 seat->selection_serial = serial;
723
724 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100725 focus = seat->keyboard->focus;
726 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500727 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100728 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400729 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700730 offer = weston_data_source_send_offer(seat->selection_data_source,
731 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400732 wl_data_device_send_selection(data_device, offer);
733 } else if (data_device) {
734 wl_data_device_send_selection(data_device, NULL);
735 }
736 }
737
738 wl_signal_emit(&seat->selection_signal, seat);
739
740 if (source) {
741 seat->selection_data_source_listener.notify =
742 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500743 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400744 &seat->selection_data_source_listener);
745 }
746}
747
748static void
749data_device_set_selection(struct wl_client *client,
750 struct wl_resource *resource,
751 struct wl_resource *source_resource, uint32_t serial)
752{
753 if (!source_resource)
754 return;
755
756 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500757 weston_seat_set_selection(wl_resource_get_user_data(resource),
758 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400759 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400760}
761
762static const struct wl_data_device_interface data_device_interface = {
763 data_device_start_drag,
764 data_device_set_selection,
765};
766
767static void
768destroy_data_source(struct wl_resource *resource)
769{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700770 struct weston_data_source *source =
771 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400772 char **p;
773
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500774 wl_signal_emit(&source->destroy_signal, source);
775
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400776 wl_array_for_each(p, &source->mime_types)
777 free(*p);
778
779 wl_array_release(&source->mime_types);
780
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400781 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400782}
783
784static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700785client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400786 uint32_t time, const char *mime_type)
787{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500788 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400789}
790
791static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700792client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400793 const char *mime_type, int32_t fd)
794{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500795 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400796 close(fd);
797}
798
799static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700800client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400801{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500802 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400803}
804
805static void
806create_data_source(struct wl_client *client,
807 struct wl_resource *resource, uint32_t id)
808{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700809 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400810
811 source = malloc(sizeof *source);
812 if (source == NULL) {
813 wl_resource_post_no_memory(resource);
814 return;
815 }
816
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500817 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400818 source->accept = client_source_accept;
819 source->send = client_source_send;
820 source->cancel = client_source_cancel;
821
822 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500823
Jason Ekstranda85118c2013-06-27 20:17:02 -0500824 source->resource =
825 wl_resource_create(client, &wl_data_source_interface, 1, id);
826 wl_resource_set_implementation(source->resource, &data_source_interface,
827 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400828}
829
830static void unbind_data_device(struct wl_resource *resource)
831{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500832 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400833}
834
835static void
836get_data_device(struct wl_client *client,
837 struct wl_resource *manager_resource,
838 uint32_t id, struct wl_resource *seat_resource)
839{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500840 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400841 struct wl_resource *resource;
842
Jason Ekstranda85118c2013-06-27 20:17:02 -0500843 resource = wl_resource_create(client,
844 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700845 if (resource == NULL) {
846 wl_resource_post_no_memory(manager_resource);
847 return;
848 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400849
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700850 wl_list_insert(&seat->drag_resource_list,
851 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500852 wl_resource_set_implementation(resource, &data_device_interface,
853 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400854}
855
856static const struct wl_data_device_manager_interface manager_interface = {
857 create_data_source,
858 get_data_device
859};
860
861static void
862bind_manager(struct wl_client *client,
863 void *data, uint32_t version, uint32_t id)
864{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500865 struct wl_resource *resource;
866
867 resource =
868 wl_resource_create(client,
869 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700870 if (resource == NULL) {
871 wl_client_post_no_memory(client);
872 return;
873 }
874
875 wl_resource_set_implementation(resource, &manager_interface,
876 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400877}
878
879WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400880wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400881{
Neil Roberts96d790e2013-09-19 17:32:00 +0100882 struct wl_resource *data_device, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700883 struct weston_data_source *source;
Neil Roberts96d790e2013-09-19 17:32:00 +0100884 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400885
886 if (!seat->keyboard)
887 return;
888
Neil Roberts96d790e2013-09-19 17:32:00 +0100889 focus = seat->keyboard->focus;
890 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400891 return;
892
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500893 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100894 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400895 if (!data_device)
896 return;
897
898 source = seat->selection_data_source;
899 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700900 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400901 wl_data_device_send_selection(data_device, offer);
902 }
903}
904
905WL_EXPORT int
906wl_data_device_manager_init(struct wl_display *display)
907{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400908 if (wl_global_create(display,
909 &wl_data_device_manager_interface, 1,
910 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400911 return -1;
912
913 return 0;
914}