blob: 56c25077b71d6ffd5b435ef3bd3078b32cf89232 [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
Pekka Paalanen8274d902014-08-06 19:36:51 +0300217static int
218pointer_drag_surface_get_label(struct weston_surface *surface,
219 char *buf, size_t len)
220{
221 return snprintf(buf, len, "pointer drag icon");
222}
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
Pekka Paalanen8274d902014-08-06 19:36:51 +0300236static int
237touch_drag_surface_get_label(struct weston_surface *surface,
238 char *buf, size_t len)
239{
240 return snprintf(buf, len, "touch drag icon");
241}
242
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800243static void
Jonas Ådahl767d8912013-12-03 22:30:17 +0100244touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800245{
246 struct weston_touch_drag *drag = es->configure_private;
247 struct weston_touch *touch = drag->grab.touch;
248
249 assert(es->configure == touch_drag_surface_configure);
250
Jonas Ådahl767d8912013-12-03 22:30:17 +0100251 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800252}
253
254static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400255destroy_drag_focus(struct wl_listener *listener, void *data)
256{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400257 struct weston_drag *drag =
258 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400259
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400260 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400261}
262
263static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800264weston_drag_set_focus(struct weston_drag *drag,
265 struct weston_seat *seat,
266 struct weston_view *view,
267 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400268{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400269 struct wl_resource *resource, *offer = NULL;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800270 struct wl_display *display = seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400271 uint32_t serial;
272
Jason Ekstranda7af7042013-10-12 22:38:11 -0500273 if (drag->focus && view && drag->focus->surface == view->surface) {
274 drag->focus = view;
275 return;
276 }
277
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400278 if (drag->focus_resource) {
279 wl_data_device_send_leave(drag->focus_resource);
280 wl_list_remove(&drag->focus_listener.link);
281 drag->focus_resource = NULL;
282 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400283 }
284
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400286 return;
287
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500288 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400290 return;
291
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800292 resource = wl_resource_find_for_client(&seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400294 if (!resource)
295 return;
296
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400297 serial = wl_display_next_serial(display);
298
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700299 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700300 offer = weston_data_source_send_offer(drag->data_source,
301 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700302 if (offer == NULL)
303 return;
304 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400305
Jason Ekstranda7af7042013-10-12 22:38:11 -0500306 wl_data_device_send_enter(resource, serial, view->surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400307 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308
Jason Ekstranda7af7042013-10-12 22:38:11 -0500309 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400310 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500311 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400312 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400313}
314
315static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400316drag_grab_focus(struct weston_pointer_grab *grab)
317{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800318 struct weston_pointer_drag *drag =
319 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400320 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400322 wl_fixed_t sx, sy;
323
Jason Ekstranda7af7042013-10-12 22:38:11 -0500324 view = weston_compositor_pick_view(pointer->seat->compositor,
325 pointer->x, pointer->y,
326 &sx, &sy);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800327 if (drag->base.focus != view)
328 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400329}
330
331static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100332drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
333 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400334{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800335 struct weston_pointer_drag *drag =
336 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400337 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400338 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400339 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400340
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100341 weston_pointer_move(pointer, x, y);
342
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800343 if (drag->base.icon) {
344 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
345 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
346 weston_view_set_position(drag->base.icon, fx, fy);
347 weston_view_schedule_repaint(drag->base.icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400348 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400349
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800350 if (drag->base.focus_resource) {
351 weston_view_from_global_fixed(drag->base.focus,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500352 pointer->x, pointer->y,
353 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400354
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800355 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400356 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400357}
358
359static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800360data_device_end_drag_grab(struct weston_drag *drag,
361 struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400362{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400363 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500364 if (weston_view_is_mapped(drag->icon))
365 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400366
Jason Ekstranda7af7042013-10-12 22:38:11 -0500367 drag->icon->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300368 weston_surface_set_label_func(drag->icon->surface, NULL);
Jason Ekstrandef540082014-06-26 10:37:36 -0700369 pixman_region32_clear(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400370 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500371 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400372 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400373
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800374 weston_drag_set_focus(drag, seat, NULL, 0, 0);
375}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400376
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800377static void
378data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
379{
380 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400381
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800382 data_device_end_drag_grab(&drag->base, pointer->seat);
383 weston_pointer_end_grab(pointer);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400384 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385}
386
387static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400388drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400389 uint32_t time, uint32_t button, uint32_t state_w)
390{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800391 struct weston_pointer_drag *drag =
392 container_of(grab, struct weston_pointer_drag, grab);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400393 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400394 enum wl_pointer_button_state state = state_w;
395
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800396 if (drag->base.focus_resource &&
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400397 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398 state == WL_POINTER_BUTTON_STATE_RELEASED)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800399 wl_data_device_send_drop(drag->base.focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400401 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800403 if (drag->base.data_source)
404 wl_list_remove(&drag->base.data_source_listener.link);
405 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406 }
407}
408
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200409static void
410drag_grab_cancel(struct weston_pointer_grab *grab)
411{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800412 struct weston_pointer_drag *drag =
413 container_of(grab, struct weston_pointer_drag, grab);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200414
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800415 if (drag->base.data_source)
416 wl_list_remove(&drag->base.data_source_listener.link);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200417
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800418 data_device_end_pointer_drag_grab(drag);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200419}
420
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800421static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400422 drag_grab_focus,
423 drag_grab_motion,
424 drag_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200425 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400426};
427
428static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800429drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
430 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400431{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800432}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400433
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800434static void
435data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
436{
437 struct weston_touch *touch = drag->grab.touch;
438
439 data_device_end_drag_grab(&drag->base, touch->seat);
440 weston_touch_end_grab(touch);
441 free(drag);
442}
443
444static void
445drag_grab_touch_up(struct weston_touch_grab *grab,
446 uint32_t time, int touch_id)
447{
448 struct weston_touch_drag *touch_drag =
449 container_of(grab, struct weston_touch_drag, grab);
450 struct weston_touch *touch = grab->touch;
451
452 if (touch_id != touch->grab_touch_id)
453 return;
454
455 if (touch_drag->base.focus_resource)
456 wl_data_device_send_drop(touch_drag->base.focus_resource);
457 if (touch_drag->base.data_source)
458 wl_list_remove(&touch_drag->base.data_source_listener.link);
459 data_device_end_touch_drag_grab(touch_drag);
460}
461
462static void
463drag_grab_touch_focus(struct weston_touch_drag *drag)
464{
465 struct weston_touch *touch = drag->grab.touch;
466 struct weston_view *view;
467 wl_fixed_t view_x, view_y;
468
469 view = weston_compositor_pick_view(touch->seat->compositor,
470 touch->grab_x, touch->grab_y,
471 &view_x, &view_y);
472 if (drag->base.focus != view)
473 weston_drag_set_focus(&drag->base, touch->seat,
474 view, view_x, view_y);
475}
476
477static void
478drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
479 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
480{
481 struct weston_touch_drag *touch_drag =
482 container_of(grab, struct weston_touch_drag, grab);
483 struct weston_touch *touch = grab->touch;
484 wl_fixed_t view_x, view_y;
485 float fx, fy;
486
487 if (touch_id != touch->grab_touch_id)
488 return;
489
490 drag_grab_touch_focus(touch_drag);
491 if (touch_drag->base.icon) {
492 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
493 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
494 weston_view_set_position(touch_drag->base.icon, fx, fy);
495 weston_view_schedule_repaint(touch_drag->base.icon);
496 }
497
498 if (touch_drag->base.focus_resource) {
499 weston_view_from_global_fixed(touch_drag->base.focus,
500 touch->grab_x, touch->grab_y,
501 &view_x, &view_y);
502 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
503 view_x, view_y);
504 }
505}
506
507static void
Jonas Ådahl1679f232014-04-12 09:39:51 +0200508drag_grab_touch_frame(struct weston_touch_grab *grab)
509{
510}
511
512static void
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800513drag_grab_touch_cancel(struct weston_touch_grab *grab)
514{
515 struct weston_touch_drag *touch_drag =
516 container_of(grab, struct weston_touch_drag, grab);
517
518 if (touch_drag->base.data_source)
519 wl_list_remove(&touch_drag->base.data_source_listener.link);
520 data_device_end_touch_drag_grab(touch_drag);
521}
522
523static const struct weston_touch_grab_interface touch_drag_grab_interface = {
524 drag_grab_touch_down,
525 drag_grab_touch_up,
526 drag_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200527 drag_grab_touch_frame,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800528 drag_grab_touch_cancel
529};
530
531static void
532destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
533{
534 struct weston_pointer_drag *drag = container_of(listener,
535 struct weston_pointer_drag, base.data_source_listener);
536
537 data_device_end_pointer_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400538}
539
540static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400541handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400542{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400543 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400544 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400545
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400546 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400547}
548
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700549WL_EXPORT int
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800550weston_pointer_start_drag(struct weston_pointer *pointer,
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700551 struct weston_data_source *source,
552 struct weston_surface *icon,
553 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400554{
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800555 struct weston_pointer_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400556
Peter Huttererf3d62272013-08-08 11:57:05 +1000557 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700558 if (drag == NULL)
559 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400560
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800561 drag->grab.interface = &pointer_drag_grab_interface;
562 drag->base.client = client;
563 drag->base.data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400564
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400565 if (icon) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800566 drag->base.icon = weston_view_create(icon);
567 if (drag->base.icon == NULL) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500568 free(drag);
569 return -1;
570 }
571
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800572 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500573 wl_signal_add(&icon->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800574 &drag->base.icon_destroy_listener);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400575
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800576 icon->configure = pointer_drag_surface_configure;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400577 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300578 weston_surface_set_label_func(icon,
579 pointer_drag_surface_get_label);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500580 } else {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800581 drag->base.icon = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500582 }
583
584 if (source) {
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800585 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 wl_signal_add(&source->destroy_signal,
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800587 &drag->base.data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400588 }
589
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800590 weston_pointer_set_focus(pointer, NULL,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400591 wl_fixed_from_int(0), wl_fixed_from_int(0));
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800592 weston_pointer_start_grab(pointer, &drag->grab);
593
594 return 0;
595}
596
597static void
598destroy_touch_data_device_source(struct wl_listener *listener, void *data)
599{
600 struct weston_touch_drag *drag = container_of(listener,
601 struct weston_touch_drag, base.data_source_listener);
602
603 data_device_end_touch_drag_grab(drag);
604}
605
606WL_EXPORT int
607weston_touch_start_drag(struct weston_touch *touch,
608 struct weston_data_source *source,
609 struct weston_surface *icon,
610 struct wl_client *client)
611{
612 struct weston_touch_drag *drag;
613
614 drag = zalloc(sizeof *drag);
615 if (drag == NULL)
616 return -1;
617
618 drag->grab.interface = &touch_drag_grab_interface;
619 drag->base.client = client;
620 drag->base.data_source = source;
621
622 if (icon) {
623 drag->base.icon = weston_view_create(icon);
624 if (drag->base.icon == NULL) {
625 free(drag);
626 return -1;
627 }
628
629 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
630 wl_signal_add(&icon->destroy_signal,
631 &drag->base.icon_destroy_listener);
632
633 icon->configure = touch_drag_surface_configure;
634 icon->configure_private = drag;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300635 weston_surface_set_label_func(icon,
636 touch_drag_surface_get_label);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800637 } else {
638 drag->base.icon = NULL;
639 }
640
641 if (source) {
642 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
643 wl_signal_add(&source->destroy_signal,
644 &drag->base.data_source_listener);
645 }
646
647 weston_touch_start_grab(touch, &drag->grab);
648
649 drag_grab_touch_focus(drag);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700650
651 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400652}
653
654static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700655data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
656 struct wl_resource *source_resource,
657 struct wl_resource *origin_resource,
658 struct wl_resource *icon_resource, uint32_t serial)
659{
660 struct weston_seat *seat = wl_resource_get_user_data(resource);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700661 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700662 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700663 struct weston_surface *icon = NULL;
Jason Ekstrand8202d722014-06-24 21:19:24 -0700664 int is_pointer_grab, is_touch_grab;
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800665 int32_t ret = 0;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700666
Jason Ekstrand8202d722014-06-24 21:19:24 -0700667 is_pointer_grab = seat->pointer &&
668 seat->pointer->button_count == 1 &&
669 seat->pointer->grab_serial == serial &&
670 seat->pointer->focus &&
671 seat->pointer->focus->surface == origin;
672
673 is_touch_grab = seat->touch &&
674 seat->touch->num_tp == 1 &&
675 seat->touch->grab_serial == serial &&
676 seat->touch->focus &&
677 seat->touch->focus->surface == origin;
678
679 if (!is_pointer_grab && !is_touch_grab)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700680 return;
681
682 /* FIXME: Check that the data source type array isn't empty. */
683
684 if (source_resource)
685 source = wl_resource_get_user_data(source_resource);
686 if (icon_resource)
687 icon = wl_resource_get_user_data(icon_resource);
Pekka Paalanen50b67472014-10-01 15:02:41 +0300688
689 if (icon) {
690 if (weston_surface_set_role(icon, "wl_data_device-icon",
691 resource,
692 WL_DATA_DEVICE_ERROR_ROLE) < 0)
693 return;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700694 }
695
Jason Ekstrand8202d722014-06-24 21:19:24 -0700696 if (is_pointer_grab)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800697 ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
Jason Ekstrand8202d722014-06-24 21:19:24 -0700698 else if (is_touch_grab)
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800699 ret = weston_touch_start_drag(seat->touch, source, icon, client);
700
701 if (ret < 0)
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700702 wl_resource_post_no_memory(resource);
703}
704
705static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400706destroy_selection_data_source(struct wl_listener *listener, void *data)
707{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400708 struct weston_seat *seat = container_of(listener, struct weston_seat,
709 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400710 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100711 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400712
713 seat->selection_data_source = NULL;
714
715 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100716 focus = seat->keyboard->focus;
717 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500718 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100719 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400720 if (data_device)
721 wl_data_device_send_selection(data_device, NULL);
722 }
723
724 wl_signal_emit(&seat->selection_signal, seat);
725}
726
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300727/** \brief Send the selection to the specified client
728 *
729 * This function creates a new wl_data_offer if there is a wl_data_source
730 * currently set as the selection and sends it to the specified client,
731 * followed by the wl_data_device.selection() event.
732 * If there is no current selection the wl_data_device.selection() event
733 * will carry a NULL wl_data_offer.
734 *
735 * If the client does not have a wl_data_device for the specified seat
736 * nothing will be done.
737 *
738 * \param seat The seat owning the wl_data_device used to send the events.
739 * \param client The client to which to send the selection.
740 */
741WL_EXPORT void
742weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
743{
744 struct wl_resource *data_device, *offer;
745
746 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
747 client);
748 if (data_device && seat->selection_data_source) {
749 offer = weston_data_source_send_offer(seat->selection_data_source,
750 data_device);
751 wl_data_device_send_selection(data_device, offer);
752 } else if (data_device) {
753 wl_data_device_send_selection(data_device, NULL);
754 }
755}
756
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400757WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400758weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700759 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400760{
Neil Roberts96d790e2013-09-19 17:32:00 +0100761 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400762
763 if (seat->selection_data_source &&
764 seat->selection_serial - serial < UINT32_MAX / 2)
765 return;
766
767 if (seat->selection_data_source) {
768 seat->selection_data_source->cancel(seat->selection_data_source);
769 wl_list_remove(&seat->selection_data_source_listener.link);
770 seat->selection_data_source = NULL;
771 }
772
773 seat->selection_data_source = source;
774 seat->selection_serial = serial;
775
776 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100777 focus = seat->keyboard->focus;
778 if (focus && focus->resource) {
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300779 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400780 }
781
782 wl_signal_emit(&seat->selection_signal, seat);
783
784 if (source) {
785 seat->selection_data_source_listener.notify =
786 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500787 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400788 &seat->selection_data_source_listener);
789 }
790}
791
792static void
793data_device_set_selection(struct wl_client *client,
794 struct wl_resource *resource,
795 struct wl_resource *source_resource, uint32_t serial)
796{
797 if (!source_resource)
798 return;
799
800 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500801 weston_seat_set_selection(wl_resource_get_user_data(resource),
802 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400803 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400804}
kabeer khan3a510d82014-10-20 11:47:15 +0530805static void
806data_device_release(struct wl_client *client, struct wl_resource *resource)
807{
808 wl_resource_destroy(resource);
809}
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400810
811static const struct wl_data_device_interface data_device_interface = {
812 data_device_start_drag,
813 data_device_set_selection,
kabeer khan3a510d82014-10-20 11:47:15 +0530814 data_device_release
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400815};
816
817static void
818destroy_data_source(struct wl_resource *resource)
819{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700820 struct weston_data_source *source =
821 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400822 char **p;
823
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500824 wl_signal_emit(&source->destroy_signal, source);
825
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400826 wl_array_for_each(p, &source->mime_types)
827 free(*p);
828
829 wl_array_release(&source->mime_types);
830
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400831 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400832}
833
834static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700835client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400836 uint32_t time, const char *mime_type)
837{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500838 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400839}
840
841static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700842client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400843 const char *mime_type, int32_t fd)
844{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500845 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400846 close(fd);
847}
848
849static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700850client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400851{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500852 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400853}
854
855static void
856create_data_source(struct wl_client *client,
857 struct wl_resource *resource, uint32_t id)
858{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700859 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400860
861 source = malloc(sizeof *source);
862 if (source == NULL) {
863 wl_resource_post_no_memory(resource);
864 return;
865 }
866
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500867 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400868 source->accept = client_source_accept;
869 source->send = client_source_send;
870 source->cancel = client_source_cancel;
871
872 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500873
Jason Ekstranda85118c2013-06-27 20:17:02 -0500874 source->resource =
875 wl_resource_create(client, &wl_data_source_interface, 1, id);
876 wl_resource_set_implementation(source->resource, &data_source_interface,
877 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400878}
879
880static void unbind_data_device(struct wl_resource *resource)
881{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500882 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400883}
884
885static void
886get_data_device(struct wl_client *client,
887 struct wl_resource *manager_resource,
888 uint32_t id, struct wl_resource *seat_resource)
889{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500890 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400891 struct wl_resource *resource;
892
Jason Ekstranda85118c2013-06-27 20:17:02 -0500893 resource = wl_resource_create(client,
kabeer khan3a510d82014-10-20 11:47:15 +0530894 &wl_data_device_interface,
895 wl_resource_get_version(manager_resource),
896 id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700897 if (resource == NULL) {
898 wl_resource_post_no_memory(manager_resource);
899 return;
900 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400901
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700902 wl_list_insert(&seat->drag_resource_list,
903 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500904 wl_resource_set_implementation(resource, &data_device_interface,
905 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400906}
907
908static const struct wl_data_device_manager_interface manager_interface = {
909 create_data_source,
910 get_data_device
911};
912
913static void
914bind_manager(struct wl_client *client,
915 void *data, uint32_t version, uint32_t id)
916{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500917 struct wl_resource *resource;
918
kabeer khan3a510d82014-10-20 11:47:15 +0530919 resource = wl_resource_create(client,
920 &wl_data_device_manager_interface,
921 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700922 if (resource == NULL) {
923 wl_client_post_no_memory(client);
924 return;
925 }
926
927 wl_resource_set_implementation(resource, &manager_interface,
928 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400929}
930
931WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400932wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400933{
Neil Roberts96d790e2013-09-19 17:32:00 +0100934 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400935
936 if (!seat->keyboard)
937 return;
938
Neil Roberts96d790e2013-09-19 17:32:00 +0100939 focus = seat->keyboard->focus;
940 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400941 return;
942
Giulio Camuffodddf9e62015-05-01 12:59:35 +0300943 weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400944}
945
946WL_EXPORT int
947wl_data_device_manager_init(struct wl_display *display)
948{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400949 if (wl_global_create(display,
kabeer khan3a510d82014-10-20 11:47:15 +0530950 &wl_data_device_manager_interface, 2,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400951 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400952 return -1;
953
954 return 0;
955}