blob: a8ab4e8c847eec8e473d86b11420884391315341 [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
56data_offer_accept(struct wl_client *client, struct wl_resource *resource,
57 uint32_t serial, const char *mime_type)
58{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070059 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040060
61 /* FIXME: Check that client is currently focused by the input
62 * device that is currently dragging this data source. Should
63 * this be a wl_data_device request? */
64
65 if (offer->source)
66 offer->source->accept(offer->source, serial, mime_type);
67}
68
69static void
70data_offer_receive(struct wl_client *client, struct wl_resource *resource,
71 const char *mime_type, int32_t fd)
72{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070073 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040074
75 if (offer->source)
76 offer->source->send(offer->source, mime_type, fd);
77 else
78 close(fd);
79}
80
81static void
82data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
83{
84 wl_resource_destroy(resource);
85}
86
87static const struct wl_data_offer_interface data_offer_interface = {
88 data_offer_accept,
89 data_offer_receive,
90 data_offer_destroy,
91};
92
93static void
94destroy_data_offer(struct wl_resource *resource)
95{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070096 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040097
98 if (offer->source)
99 wl_list_remove(&offer->source_destroy_listener.link);
100 free(offer);
101}
102
103static void
104destroy_offer_data_source(struct wl_listener *listener, void *data)
105{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700106 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400107
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700108 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400109 source_destroy_listener);
110
111 offer->source = NULL;
112}
113
114static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700115weston_data_source_send_offer(struct weston_data_source *source,
116 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400117{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700118 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400119 char **p;
120
121 offer = malloc(sizeof *offer);
122 if (offer == NULL)
123 return NULL;
124
Jason Ekstranda85118c2013-06-27 20:17:02 -0500125 offer->resource =
126 wl_resource_create(wl_resource_get_client(target),
127 &wl_data_offer_interface, 1, 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700128 if (offer->resource == NULL) {
129 free(offer);
130 return NULL;
131 }
132
Jason Ekstranda85118c2013-06-27 20:17:02 -0500133 wl_resource_set_implementation(offer->resource, &data_offer_interface,
134 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400135
136 offer->source = source;
137 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500138 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400139 &offer->source_destroy_listener);
140
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500141 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400142
143 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500144 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400145
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500146 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400147}
148
149static void
150data_source_offer(struct wl_client *client,
151 struct wl_resource *resource,
152 const char *type)
153{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700154 struct weston_data_source *source =
155 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400156 char **p;
157
158 p = wl_array_add(&source->mime_types, sizeof *p);
159 if (p)
160 *p = strdup(type);
161 if (!p || !*p)
162 wl_resource_post_no_memory(resource);
163}
164
165static void
166data_source_destroy(struct wl_client *client, struct wl_resource *resource)
167{
168 wl_resource_destroy(resource);
169}
170
171static struct wl_data_source_interface data_source_interface = {
172 data_source_offer,
173 data_source_destroy
174};
175
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400176static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800177drag_surface_configure(struct weston_drag *drag,
Jonas Ådahl767d8912013-12-03 22:30:17 +0100178 struct weston_pointer *pointer,
179 struct weston_touch *touch,
180 struct weston_surface *es,
181 int32_t sx, int32_t sy)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400182{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300183 struct weston_layer_entry *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400184 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400185
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800186 assert((pointer != NULL && touch == NULL) ||
187 (pointer == NULL && touch != NULL));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500188
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400189 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800190 if (pointer && pointer->sprite &&
191 weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400192 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400193 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500194 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400195
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300196 weston_layer_entry_remove(&drag->icon->layer_link);
197 weston_layer_entry_insert(list, &drag->icon->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500198 weston_view_update_transform(drag->icon);
Jason Ekstrandef540082014-06-26 10:37:36 -0700199 pixman_region32_clear(&es->pending.input);
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400200 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400201
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400202 drag->dx += sx;
203 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400204
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800205 /* init to 0 for avoiding a compile warning */
206 fx = fy = 0;
207 if (pointer) {
208 fx = wl_fixed_to_double(pointer->x) + drag->dx;
209 fy = wl_fixed_to_double(pointer->y) + drag->dy;
210 } else if (touch) {
211 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
212 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
213 }
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600214 weston_view_set_position(drag->icon, fx, fy);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400215}
216
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400217static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100218pointer_drag_surface_configure(struct weston_surface *es,
219 int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800220{
221 struct weston_pointer_drag *drag = es->configure_private;
222 struct weston_pointer *pointer = drag->grab.pointer;
223
224 assert(es->configure == pointer_drag_surface_configure);
225
Jonas Ådahl767d8912013-12-03 22:30:17 +0100226 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800227}
228
229static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100230touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800231{
232 struct weston_touch_drag *drag = es->configure_private;
233 struct weston_touch *touch = drag->grab.touch;
234
235 assert(es->configure == touch_drag_surface_configure);
236
Jonas Ådahl767d8912013-12-03 22:30:17 +0100237 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800238}
239
240static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400241destroy_drag_focus(struct wl_listener *listener, void *data)
242{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400243 struct weston_drag *drag =
244 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400245
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400246 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400247}
248
249static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800250weston_drag_set_focus(struct weston_drag *drag,
251 struct weston_seat *seat,
252 struct weston_view *view,
253 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400254{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400255 struct wl_resource *resource, *offer = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800256 struct wl_display *display = seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400257 uint32_t serial;
258
Jason Ekstranda7af7042013-10-12 22:38:11 -0500259 if (drag->focus && view && drag->focus->surface == view->surface) {
260 drag->focus = view;
261 return;
262 }
263
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400264 if (drag->focus_resource) {
265 wl_data_device_send_leave(drag->focus_resource);
266 wl_list_remove(&drag->focus_listener.link);
267 drag->focus_resource = NULL;
268 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400269 }
270
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400272 return;
273
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500274 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400276 return;
277
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800278 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400280 if (!resource)
281 return;
282
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400283 serial = wl_display_next_serial(display);
284
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700285 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700286 offer = weston_data_source_send_offer(drag->data_source,
287 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700288 if (offer == NULL)
289 return;
290 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400291
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 wl_data_device_send_enter(resource, serial, view->surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400293 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400294
Jason Ekstranda7af7042013-10-12 22:38:11 -0500295 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400296 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500297 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400298 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400299}
300
301static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400302drag_grab_focus(struct weston_pointer_grab *grab)
303{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800304 struct weston_pointer_drag *drag =
305 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400306 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500307 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400308 wl_fixed_t sx, sy;
309
Jason Ekstranda7af7042013-10-12 22:38:11 -0500310 view = weston_compositor_pick_view(pointer->seat->compositor,
311 pointer->x, pointer->y,
312 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800313 if (drag->base.focus != view)
314 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400315}
316
317static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100318drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
319 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400320{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800321 struct weston_pointer_drag *drag =
322 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400323 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400324 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400325 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400326
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100327 weston_pointer_move(pointer, x, y);
328
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800329 if (drag->base.icon) {
330 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
331 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
332 weston_view_set_position(drag->base.icon, fx, fy);
333 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400334 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400335
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800336 if (drag->base.focus_resource) {
337 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 pointer->x, pointer->y,
339 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400340
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800341 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400342 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400343}
344
345static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800346data_device_end_drag_grab(struct weston_drag *drag,
347 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400348{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400349 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500350 if (weston_view_is_mapped(drag->icon))
351 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400352
Jason Ekstranda7af7042013-10-12 22:38:11 -0500353 drag->icon->surface->configure = NULL;
Jason Ekstrandef540082014-06-26 10:37:36 -0700354 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400355 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500356 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400357 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400358
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800359 weston_drag_set_focus(drag, seat, NULL, 0, 0);
360}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400361
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800362static void
363data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
364{
365 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400366
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800367 data_device_end_drag_grab(&drag->base, pointer->seat);
368 weston_pointer_end_grab(pointer);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400369 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400370}
371
372static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400373drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400374 uint32_t time, uint32_t button, uint32_t state_w)
375{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800376 struct weston_pointer_drag *drag =
377 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400378 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400379 enum wl_pointer_button_state state = state_w;
380
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800381 if (drag->base.focus_resource &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400382 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400383 state == WL_POINTER_BUTTON_STATE_RELEASED)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800384 wl_data_device_send_drop(drag->base.focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400386 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400387 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800388 if (drag->base.data_source)
389 wl_list_remove(&drag->base.data_source_listener.link);
390 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400391 }
392}
393
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200394static void
395drag_grab_cancel(struct weston_pointer_grab *grab)
396{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800397 struct weston_pointer_drag *drag =
398 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200399
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800400 if (drag->base.data_source)
401 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200402
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800403 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200404}
405
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800406static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400407 drag_grab_focus,
408 drag_grab_motion,
409 drag_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200410 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400411};
412
413static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800414drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
415 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400416{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800417}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400418
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800419static void
420data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
421{
422 struct weston_touch *touch = drag->grab.touch;
423
424 data_device_end_drag_grab(&drag->base, touch->seat);
425 weston_touch_end_grab(touch);
426 free(drag);
427}
428
429static void
430drag_grab_touch_up(struct weston_touch_grab *grab,
431 uint32_t time, int touch_id)
432{
433 struct weston_touch_drag *touch_drag =
434 container_of(grab, struct weston_touch_drag, grab);
435 struct weston_touch *touch = grab->touch;
436
437 if (touch_id != touch->grab_touch_id)
438 return;
439
440 if (touch_drag->base.focus_resource)
441 wl_data_device_send_drop(touch_drag->base.focus_resource);
442 if (touch_drag->base.data_source)
443 wl_list_remove(&touch_drag->base.data_source_listener.link);
444 data_device_end_touch_drag_grab(touch_drag);
445}
446
447static void
448drag_grab_touch_focus(struct weston_touch_drag *drag)
449{
450 struct weston_touch *touch = drag->grab.touch;
451 struct weston_view *view;
452 wl_fixed_t view_x, view_y;
453
454 view = weston_compositor_pick_view(touch->seat->compositor,
455 touch->grab_x, touch->grab_y,
456 &view_x, &view_y);
457 if (drag->base.focus != view)
458 weston_drag_set_focus(&drag->base, touch->seat,
459 view, view_x, view_y);
460}
461
462static void
463drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
464 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
465{
466 struct weston_touch_drag *touch_drag =
467 container_of(grab, struct weston_touch_drag, grab);
468 struct weston_touch *touch = grab->touch;
469 wl_fixed_t view_x, view_y;
470 float fx, fy;
471
472 if (touch_id != touch->grab_touch_id)
473 return;
474
475 drag_grab_touch_focus(touch_drag);
476 if (touch_drag->base.icon) {
477 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
478 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
479 weston_view_set_position(touch_drag->base.icon, fx, fy);
480 weston_view_schedule_repaint(touch_drag->base.icon);
481 }
482
483 if (touch_drag->base.focus_resource) {
484 weston_view_from_global_fixed(touch_drag->base.focus,
485 touch->grab_x, touch->grab_y,
486 &view_x, &view_y);
487 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
488 view_x, view_y);
489 }
490}
491
492static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200493drag_grab_touch_frame(struct weston_touch_grab *grab)
494{
495}
496
497static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800498drag_grab_touch_cancel(struct weston_touch_grab *grab)
499{
500 struct weston_touch_drag *touch_drag =
501 container_of(grab, struct weston_touch_drag, grab);
502
503 if (touch_drag->base.data_source)
504 wl_list_remove(&touch_drag->base.data_source_listener.link);
505 data_device_end_touch_drag_grab(touch_drag);
506}
507
508static const struct weston_touch_grab_interface touch_drag_grab_interface = {
509 drag_grab_touch_down,
510 drag_grab_touch_up,
511 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200512 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800513 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);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700642 struct weston_surface *origin = wl_resource_get_user_data(origin_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;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700645 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800646 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700647
Jason Ekstrand8202d722014-06-24 21:19:24 -0700648 is_pointer_grab = seat->pointer &&
649 seat->pointer->button_count == 1 &&
650 seat->pointer->grab_serial == serial &&
651 seat->pointer->focus &&
652 seat->pointer->focus->surface == origin;
653
654 is_touch_grab = seat->touch &&
655 seat->touch->num_tp == 1 &&
656 seat->touch->grab_serial == serial &&
657 seat->touch->focus &&
658 seat->touch->focus->surface == origin;
659
660 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700661 return;
662
663 /* FIXME: Check that the data source type array isn't empty. */
664
665 if (source_resource)
666 source = wl_resource_get_user_data(source_resource);
667 if (icon_resource)
668 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +0300669
670 if (icon) {
671 if (weston_surface_set_role(icon, "wl_data_device-icon",
672 resource,
673 WL_DATA_DEVICE_ERROR_ROLE) < 0)
674 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700675 }
676
Jason Ekstrand8202d722014-06-24 21:19:24 -0700677 if (is_pointer_grab)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800678 ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700679 else if (is_touch_grab)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800680 ret = weston_touch_start_drag(seat->touch, source, icon, client);
681
682 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700683 wl_resource_post_no_memory(resource);
684}
685
686static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400687destroy_selection_data_source(struct wl_listener *listener, void *data)
688{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400689 struct weston_seat *seat = container_of(listener, struct weston_seat,
690 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400691 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100692 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400693
694 seat->selection_data_source = NULL;
695
696 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100697 focus = seat->keyboard->focus;
698 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500699 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100700 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400701 if (data_device)
702 wl_data_device_send_selection(data_device, NULL);
703 }
704
705 wl_signal_emit(&seat->selection_signal, seat);
706}
707
708WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400709weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700710 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400711{
712 struct wl_resource *data_device, *offer;
Neil Roberts96d790e2013-09-19 17:32:00 +0100713 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400714
715 if (seat->selection_data_source &&
716 seat->selection_serial - serial < UINT32_MAX / 2)
717 return;
718
719 if (seat->selection_data_source) {
720 seat->selection_data_source->cancel(seat->selection_data_source);
721 wl_list_remove(&seat->selection_data_source_listener.link);
722 seat->selection_data_source = NULL;
723 }
724
725 seat->selection_data_source = source;
726 seat->selection_serial = serial;
727
728 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100729 focus = seat->keyboard->focus;
730 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500731 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100732 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400733 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700734 offer = weston_data_source_send_offer(seat->selection_data_source,
735 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400736 wl_data_device_send_selection(data_device, offer);
737 } else if (data_device) {
738 wl_data_device_send_selection(data_device, NULL);
739 }
740 }
741
742 wl_signal_emit(&seat->selection_signal, seat);
743
744 if (source) {
745 seat->selection_data_source_listener.notify =
746 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500747 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400748 &seat->selection_data_source_listener);
749 }
750}
751
752static void
753data_device_set_selection(struct wl_client *client,
754 struct wl_resource *resource,
755 struct wl_resource *source_resource, uint32_t serial)
756{
757 if (!source_resource)
758 return;
759
760 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500761 weston_seat_set_selection(wl_resource_get_user_data(resource),
762 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400763 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400764}
765
766static const struct wl_data_device_interface data_device_interface = {
767 data_device_start_drag,
768 data_device_set_selection,
769};
770
771static void
772destroy_data_source(struct wl_resource *resource)
773{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700774 struct weston_data_source *source =
775 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400776 char **p;
777
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500778 wl_signal_emit(&source->destroy_signal, source);
779
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400780 wl_array_for_each(p, &source->mime_types)
781 free(*p);
782
783 wl_array_release(&source->mime_types);
784
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400785 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400786}
787
788static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700789client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400790 uint32_t time, const char *mime_type)
791{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500792 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400793}
794
795static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700796client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400797 const char *mime_type, int32_t fd)
798{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500799 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400800 close(fd);
801}
802
803static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700804client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400805{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500806 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400807}
808
809static void
810create_data_source(struct wl_client *client,
811 struct wl_resource *resource, uint32_t id)
812{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700813 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400814
815 source = malloc(sizeof *source);
816 if (source == NULL) {
817 wl_resource_post_no_memory(resource);
818 return;
819 }
820
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500821 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400822 source->accept = client_source_accept;
823 source->send = client_source_send;
824 source->cancel = client_source_cancel;
825
826 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500827
Jason Ekstranda85118c2013-06-27 20:17:02 -0500828 source->resource =
829 wl_resource_create(client, &wl_data_source_interface, 1, id);
830 wl_resource_set_implementation(source->resource, &data_source_interface,
831 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400832}
833
834static void unbind_data_device(struct wl_resource *resource)
835{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500836 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400837}
838
839static void
840get_data_device(struct wl_client *client,
841 struct wl_resource *manager_resource,
842 uint32_t id, struct wl_resource *seat_resource)
843{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500844 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400845 struct wl_resource *resource;
846
Jason Ekstranda85118c2013-06-27 20:17:02 -0500847 resource = wl_resource_create(client,
848 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700849 if (resource == NULL) {
850 wl_resource_post_no_memory(manager_resource);
851 return;
852 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400853
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700854 wl_list_insert(&seat->drag_resource_list,
855 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500856 wl_resource_set_implementation(resource, &data_device_interface,
857 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400858}
859
860static const struct wl_data_device_manager_interface manager_interface = {
861 create_data_source,
862 get_data_device
863};
864
865static void
866bind_manager(struct wl_client *client,
867 void *data, uint32_t version, uint32_t id)
868{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500869 struct wl_resource *resource;
870
871 resource =
872 wl_resource_create(client,
873 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700874 if (resource == NULL) {
875 wl_client_post_no_memory(client);
876 return;
877 }
878
879 wl_resource_set_implementation(resource, &manager_interface,
880 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400881}
882
883WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400884wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400885{
Neil Roberts96d790e2013-09-19 17:32:00 +0100886 struct wl_resource *data_device, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700887 struct weston_data_source *source;
Neil Roberts96d790e2013-09-19 17:32:00 +0100888 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400889
890 if (!seat->keyboard)
891 return;
892
Neil Roberts96d790e2013-09-19 17:32:00 +0100893 focus = seat->keyboard->focus;
894 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400895 return;
896
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500897 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100898 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400899 if (!data_device)
900 return;
901
902 source = seat->selection_data_source;
903 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700904 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400905 wl_data_device_send_selection(data_device, offer);
906 }
907}
908
909WL_EXPORT int
910wl_data_device_manager_init(struct wl_display *display)
911{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400912 if (wl_global_create(display,
913 &wl_data_device_manager_interface, 1,
914 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400915 return -1;
916
917 return 0;
918}