blob: 88c6de36ea58afd2a3bb9a7d32985fc614538338 [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
Jonas Ådahl1679f232014-04-12 09:39:51 +0200500drag_grab_touch_frame(struct weston_touch_grab *grab)
501{
502}
503
504static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800505drag_grab_touch_cancel(struct weston_touch_grab *grab)
506{
507 struct weston_touch_drag *touch_drag =
508 container_of(grab, struct weston_touch_drag, grab);
509
510 if (touch_drag->base.data_source)
511 wl_list_remove(&touch_drag->base.data_source_listener.link);
512 data_device_end_touch_drag_grab(touch_drag);
513}
514
515static const struct weston_touch_grab_interface touch_drag_grab_interface = {
516 drag_grab_touch_down,
517 drag_grab_touch_up,
518 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200519 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800520 drag_grab_touch_cancel
521};
522
523static void
524destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
525{
526 struct weston_pointer_drag *drag = container_of(listener,
527 struct weston_pointer_drag, base.data_source_listener);
528
529 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400530}
531
532static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400533handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400534{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400535 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400536 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400537
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400538 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400539}
540
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700541WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800542weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700543 struct weston_data_source *source,
544 struct weston_surface *icon,
545 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800547 struct weston_pointer_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400548
Peter Huttererf3d62272013-08-08 11:57:05 +1000549 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700550 if (drag == NULL)
551 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400552
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800553 drag->grab.interface = &pointer_drag_grab_interface;
554 drag->base.client = client;
555 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400556
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400557 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800558 drag->base.icon = weston_view_create(icon);
559 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560 free(drag);
561 return -1;
562 }
563
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800564 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500565 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800566 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400567
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800568 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400569 icon->configure_private = drag;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500570 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800571 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500572 }
573
574 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800575 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500576 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800577 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400578 }
579
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800580 weston_pointer_set_focus(pointer, NULL,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400581 wl_fixed_from_int(0), wl_fixed_from_int(0));
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800582 weston_pointer_start_grab(pointer, &drag->grab);
583
584 return 0;
585}
586
587static void
588destroy_touch_data_device_source(struct wl_listener *listener, void *data)
589{
590 struct weston_touch_drag *drag = container_of(listener,
591 struct weston_touch_drag, base.data_source_listener);
592
593 data_device_end_touch_drag_grab(drag);
594}
595
596WL_EXPORT int
597weston_touch_start_drag(struct weston_touch *touch,
598 struct weston_data_source *source,
599 struct weston_surface *icon,
600 struct wl_client *client)
601{
602 struct weston_touch_drag *drag;
603
604 drag = zalloc(sizeof *drag);
605 if (drag == NULL)
606 return -1;
607
608 drag->grab.interface = &touch_drag_grab_interface;
609 drag->base.client = client;
610 drag->base.data_source = source;
611
612 if (icon) {
613 drag->base.icon = weston_view_create(icon);
614 if (drag->base.icon == NULL) {
615 free(drag);
616 return -1;
617 }
618
619 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
620 wl_signal_add(&icon->destroy_signal,
621 &drag->base.icon_destroy_listener);
622
623 icon->configure = touch_drag_surface_configure;
624 icon->configure_private = drag;
625 } else {
626 drag->base.icon = NULL;
627 }
628
629 if (source) {
630 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
631 wl_signal_add(&source->destroy_signal,
632 &drag->base.data_source_listener);
633 }
634
635 weston_touch_start_grab(touch, &drag->grab);
636
637 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700638
639 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400640}
641
642static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700643data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
644 struct wl_resource *source_resource,
645 struct wl_resource *origin_resource,
646 struct wl_resource *icon_resource, uint32_t serial)
647{
648 struct weston_seat *seat = wl_resource_get_user_data(resource);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700649 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700650 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700651 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700652 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800653 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700654
Jason Ekstrand8202d722014-06-24 21:19:24 -0700655 is_pointer_grab = seat->pointer &&
656 seat->pointer->button_count == 1 &&
657 seat->pointer->grab_serial == serial &&
658 seat->pointer->focus &&
659 seat->pointer->focus->surface == origin;
660
661 is_touch_grab = seat->touch &&
662 seat->touch->num_tp == 1 &&
663 seat->touch->grab_serial == serial &&
664 seat->touch->focus &&
665 seat->touch->focus->surface == origin;
666
667 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700668 return;
669
670 /* FIXME: Check that the data source type array isn't empty. */
671
672 if (source_resource)
673 source = wl_resource_get_user_data(source_resource);
674 if (icon_resource)
675 icon = wl_resource_get_user_data(icon_resource);
676 if (icon && icon->configure) {
677 wl_resource_post_error(icon_resource,
678 WL_DISPLAY_ERROR_INVALID_OBJECT,
679 "surface->configure already set");
680 return;
681 }
682
Jason Ekstrand8202d722014-06-24 21:19:24 -0700683 if (is_pointer_grab)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800684 ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700685 else if (is_touch_grab)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800686 ret = weston_touch_start_drag(seat->touch, source, icon, client);
687
688 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700689 wl_resource_post_no_memory(resource);
690}
691
692static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400693destroy_selection_data_source(struct wl_listener *listener, void *data)
694{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400695 struct weston_seat *seat = container_of(listener, struct weston_seat,
696 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400697 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100698 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400699
700 seat->selection_data_source = NULL;
701
702 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100703 focus = seat->keyboard->focus;
704 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500705 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100706 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400707 if (data_device)
708 wl_data_device_send_selection(data_device, NULL);
709 }
710
711 wl_signal_emit(&seat->selection_signal, seat);
712}
713
714WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400715weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700716 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400717{
718 struct wl_resource *data_device, *offer;
Neil Roberts96d790e2013-09-19 17:32:00 +0100719 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400720
721 if (seat->selection_data_source &&
722 seat->selection_serial - serial < UINT32_MAX / 2)
723 return;
724
725 if (seat->selection_data_source) {
726 seat->selection_data_source->cancel(seat->selection_data_source);
727 wl_list_remove(&seat->selection_data_source_listener.link);
728 seat->selection_data_source = NULL;
729 }
730
731 seat->selection_data_source = source;
732 seat->selection_serial = serial;
733
734 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100735 focus = seat->keyboard->focus;
736 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500737 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100738 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400739 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700740 offer = weston_data_source_send_offer(seat->selection_data_source,
741 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400742 wl_data_device_send_selection(data_device, offer);
743 } else if (data_device) {
744 wl_data_device_send_selection(data_device, NULL);
745 }
746 }
747
748 wl_signal_emit(&seat->selection_signal, seat);
749
750 if (source) {
751 seat->selection_data_source_listener.notify =
752 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500753 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400754 &seat->selection_data_source_listener);
755 }
756}
757
758static void
759data_device_set_selection(struct wl_client *client,
760 struct wl_resource *resource,
761 struct wl_resource *source_resource, uint32_t serial)
762{
763 if (!source_resource)
764 return;
765
766 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500767 weston_seat_set_selection(wl_resource_get_user_data(resource),
768 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400769 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400770}
771
772static const struct wl_data_device_interface data_device_interface = {
773 data_device_start_drag,
774 data_device_set_selection,
775};
776
777static void
778destroy_data_source(struct wl_resource *resource)
779{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700780 struct weston_data_source *source =
781 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400782 char **p;
783
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500784 wl_signal_emit(&source->destroy_signal, source);
785
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400786 wl_array_for_each(p, &source->mime_types)
787 free(*p);
788
789 wl_array_release(&source->mime_types);
790
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400791 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400792}
793
794static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700795client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400796 uint32_t time, const char *mime_type)
797{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500798 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400799}
800
801static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700802client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400803 const char *mime_type, int32_t fd)
804{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500805 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400806 close(fd);
807}
808
809static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700810client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400811{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500812 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400813}
814
815static void
816create_data_source(struct wl_client *client,
817 struct wl_resource *resource, uint32_t id)
818{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700819 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400820
821 source = malloc(sizeof *source);
822 if (source == NULL) {
823 wl_resource_post_no_memory(resource);
824 return;
825 }
826
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500827 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400828 source->accept = client_source_accept;
829 source->send = client_source_send;
830 source->cancel = client_source_cancel;
831
832 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500833
Jason Ekstranda85118c2013-06-27 20:17:02 -0500834 source->resource =
835 wl_resource_create(client, &wl_data_source_interface, 1, id);
836 wl_resource_set_implementation(source->resource, &data_source_interface,
837 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400838}
839
840static void unbind_data_device(struct wl_resource *resource)
841{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500842 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400843}
844
845static void
846get_data_device(struct wl_client *client,
847 struct wl_resource *manager_resource,
848 uint32_t id, struct wl_resource *seat_resource)
849{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500850 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400851 struct wl_resource *resource;
852
Jason Ekstranda85118c2013-06-27 20:17:02 -0500853 resource = wl_resource_create(client,
854 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700855 if (resource == NULL) {
856 wl_resource_post_no_memory(manager_resource);
857 return;
858 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400859
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700860 wl_list_insert(&seat->drag_resource_list,
861 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500862 wl_resource_set_implementation(resource, &data_device_interface,
863 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400864}
865
866static const struct wl_data_device_manager_interface manager_interface = {
867 create_data_source,
868 get_data_device
869};
870
871static void
872bind_manager(struct wl_client *client,
873 void *data, uint32_t version, uint32_t id)
874{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500875 struct wl_resource *resource;
876
877 resource =
878 wl_resource_create(client,
879 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700880 if (resource == NULL) {
881 wl_client_post_no_memory(client);
882 return;
883 }
884
885 wl_resource_set_implementation(resource, &manager_interface,
886 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400887}
888
889WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400890wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400891{
Neil Roberts96d790e2013-09-19 17:32:00 +0100892 struct wl_resource *data_device, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700893 struct weston_data_source *source;
Neil Roberts96d790e2013-09-19 17:32:00 +0100894 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400895
896 if (!seat->keyboard)
897 return;
898
Neil Roberts96d790e2013-09-19 17:32:00 +0100899 focus = seat->keyboard->focus;
900 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400901 return;
902
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500903 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100904 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400905 if (!data_device)
906 return;
907
908 source = seat->selection_data_source;
909 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700910 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400911 wl_data_device_send_selection(data_device, offer);
912 }
913}
914
915WL_EXPORT int
916wl_data_device_manager_init(struct wl_display *display)
917{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400918 if (wl_global_create(display,
919 &wl_data_device_manager_interface, 1,
920 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400921 return -1;
922
923 return 0;
924}