blob: 313278e9c86d3877fc840a0bba0c3cd13683286c [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;
40 struct weston_pointer_grab grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -050041 struct weston_view *icon;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040042 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040043 int32_t dx, dy;
44};
45
Kristian Høgsberg2158a882013-04-18 15:07:39 -040046static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -040047empty_region(pixman_region32_t *region)
48{
49 pixman_region32_fini(region);
50 pixman_region32_init(region);
51}
52
53static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -040054data_offer_accept(struct wl_client *client, struct wl_resource *resource,
55 uint32_t serial, const char *mime_type)
56{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070057 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040058
59 /* FIXME: Check that client is currently focused by the input
60 * device that is currently dragging this data source. Should
61 * this be a wl_data_device request? */
62
63 if (offer->source)
64 offer->source->accept(offer->source, serial, mime_type);
65}
66
67static void
68data_offer_receive(struct wl_client *client, struct wl_resource *resource,
69 const char *mime_type, int32_t fd)
70{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070071 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040072
73 if (offer->source)
74 offer->source->send(offer->source, mime_type, fd);
75 else
76 close(fd);
77}
78
79static void
80data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
81{
82 wl_resource_destroy(resource);
83}
84
85static const struct wl_data_offer_interface data_offer_interface = {
86 data_offer_accept,
87 data_offer_receive,
88 data_offer_destroy,
89};
90
91static void
92destroy_data_offer(struct wl_resource *resource)
93{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070094 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040095
96 if (offer->source)
97 wl_list_remove(&offer->source_destroy_listener.link);
98 free(offer);
99}
100
101static void
102destroy_offer_data_source(struct wl_listener *listener, void *data)
103{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700104 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400105
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700106 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400107 source_destroy_listener);
108
109 offer->source = NULL;
110}
111
112static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700113weston_data_source_send_offer(struct weston_data_source *source,
114 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400115{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700116 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400117 char **p;
118
119 offer = malloc(sizeof *offer);
120 if (offer == NULL)
121 return NULL;
122
Jason Ekstranda85118c2013-06-27 20:17:02 -0500123 offer->resource =
124 wl_resource_create(wl_resource_get_client(target),
125 &wl_data_offer_interface, 1, 0);
Kristian Høgsberg3c30f0f2013-08-06 10:24:04 -0700126 if (offer->resource == NULL) {
127 free(offer);
128 return NULL;
129 }
130
Jason Ekstranda85118c2013-06-27 20:17:02 -0500131 wl_resource_set_implementation(offer->resource, &data_offer_interface,
132 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400133
134 offer->source = source;
135 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500136 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400137 &offer->source_destroy_listener);
138
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500139 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400140
141 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500142 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400143
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500144 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400145}
146
147static void
148data_source_offer(struct wl_client *client,
149 struct wl_resource *resource,
150 const char *type)
151{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700152 struct weston_data_source *source =
153 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400154 char **p;
155
156 p = wl_array_add(&source->mime_types, sizeof *p);
157 if (p)
158 *p = strdup(type);
159 if (!p || !*p)
160 wl_resource_post_no_memory(resource);
161}
162
163static void
164data_source_destroy(struct wl_client *client, struct wl_resource *resource)
165{
166 wl_resource_destroy(resource);
167}
168
169static struct wl_data_source_interface data_source_interface = {
170 data_source_offer,
171 data_source_destroy
172};
173
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400174static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400175drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
176{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400177 struct weston_drag *drag = es->configure_private;
178 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400179 struct wl_list *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400180 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400181
Jason Ekstranda7af7042013-10-12 22:38:11 -0500182 assert(es->configure == drag_surface_configure);
183
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400184 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500185 if (pointer->sprite && weston_view_is_mapped(pointer->sprite))
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400186 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400187 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500188 list = &es->compositor->cursor_layer.view_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400189
Jason Ekstranda7af7042013-10-12 22:38:11 -0500190 wl_list_remove(&drag->icon->layer_link);
191 wl_list_insert(list, &drag->icon->layer_link);
192 weston_view_update_transform(drag->icon);
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400193 empty_region(&es->pending.input);
194 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400195
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400196 drag->dx += sx;
197 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400198
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400199 fx = wl_fixed_to_double(pointer->x) + drag->dx;
200 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500201 weston_view_configure(drag->icon, fx, fy, width, height);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400202}
203
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400204static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205destroy_drag_focus(struct wl_listener *listener, void *data)
206{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400207 struct weston_drag *drag =
208 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400209
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400210 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400211}
212
213static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500214weston_drag_set_focus(struct weston_drag *drag, struct weston_view *view,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400215 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400216{
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400217 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400218 struct wl_resource *resource, *offer = NULL;
Rob Bradford880ebc72013-07-22 17:31:38 +0100219 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400220 uint32_t serial;
221
Jason Ekstranda7af7042013-10-12 22:38:11 -0500222 if (drag->focus && view && drag->focus->surface == view->surface) {
223 drag->focus = view;
224 return;
225 }
226
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400227 if (drag->focus_resource) {
228 wl_data_device_send_leave(drag->focus_resource);
229 wl_list_remove(&drag->focus_listener.link);
230 drag->focus_resource = NULL;
231 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400232 }
233
Jason Ekstranda7af7042013-10-12 22:38:11 -0500234 if (!view || !view->surface->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400235 return;
236
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500237 if (!drag->data_source &&
Jason Ekstranda7af7042013-10-12 22:38:11 -0500238 wl_resource_get_client(view->surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400239 return;
240
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500241 resource = wl_resource_find_for_client(&pointer->seat->drag_resource_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500242 wl_resource_get_client(view->surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400243 if (!resource)
244 return;
245
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400246 serial = wl_display_next_serial(display);
247
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700248 if (drag->data_source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700249 offer = weston_data_source_send_offer(drag->data_source,
250 resource);
Kristian Høgsberga34e2f22013-08-20 15:58:25 -0700251 if (offer == NULL)
252 return;
253 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400254
Jason Ekstranda7af7042013-10-12 22:38:11 -0500255 wl_data_device_send_enter(resource, serial, view->surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400256 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400257
Jason Ekstranda7af7042013-10-12 22:38:11 -0500258 drag->focus = view;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400259 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500260 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400261 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400262}
263
264static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400265drag_grab_focus(struct weston_pointer_grab *grab)
266{
267 struct weston_drag *drag =
268 container_of(grab, struct weston_drag, grab);
269 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500270 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400271 wl_fixed_t sx, sy;
272
Jason Ekstranda7af7042013-10-12 22:38:11 -0500273 view = weston_compositor_pick_view(pointer->seat->compositor,
274 pointer->x, pointer->y,
275 &sx, &sy);
276 if (drag->focus != view)
277 weston_drag_set_focus(drag, view, sx, sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400278}
279
280static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100281drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
282 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400283{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400284 struct weston_drag *drag =
285 container_of(grab, struct weston_drag, grab);
286 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400287 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400288 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400289
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100290 weston_pointer_move(pointer, x, y);
291
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400292 if (drag->icon) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400293 fx = wl_fixed_to_double(pointer->x) + drag->dx;
294 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500295 weston_view_set_position(drag->icon, fx, fy);
296 weston_view_schedule_repaint(drag->icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400297 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400298
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400299 if (drag->focus_resource) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500300 weston_view_from_global_fixed(drag->focus,
301 pointer->x, pointer->y,
302 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400303
304 wl_data_device_send_motion(drag->focus_resource, time, sx, sy);
305 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400306}
307
308static void
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400309data_device_end_drag_grab(struct weston_drag *drag)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400310{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400311 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 if (weston_view_is_mapped(drag->icon))
313 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400314
Jason Ekstranda7af7042013-10-12 22:38:11 -0500315 drag->icon->surface->configure = NULL;
316 empty_region(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400317 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400319 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400320
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400321 weston_drag_set_focus(drag, NULL, 0, 0);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400322
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400323 weston_pointer_end_grab(drag->grab.pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400324
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400325 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326}
327
328static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330 uint32_t time, uint32_t button, uint32_t state_w)
331{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400332 struct weston_drag *drag =
333 container_of(grab, struct weston_drag, grab);
334 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400335 enum wl_pointer_button_state state = state_w;
336
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400337 if (drag->focus_resource &&
338 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400339 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400340 wl_data_device_send_drop(drag->focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400342 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400343 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400344 if (drag->data_source)
345 wl_list_remove(&drag->data_source_listener.link);
346 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400347 }
348}
349
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200350static void
351drag_grab_cancel(struct weston_pointer_grab *grab)
352{
353 struct weston_drag *drag =
354 container_of(grab, struct weston_drag, grab);
355
356 if (drag->data_source)
357 wl_list_remove(&drag->data_source_listener.link);
358
359 data_device_end_drag_grab(drag);
360}
361
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400362static const struct weston_pointer_grab_interface drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400363 drag_grab_focus,
364 drag_grab_motion,
365 drag_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200366 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400367};
368
369static void
370destroy_data_device_source(struct wl_listener *listener, void *data)
371{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400372 struct weston_drag *drag = container_of(listener, struct weston_drag,
373 data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400374
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400375 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400376}
377
378static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400379handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400380{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400381 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400382 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400383
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400384 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400385}
386
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700387WL_EXPORT int
388weston_seat_start_drag(struct weston_seat *seat,
389 struct weston_data_source *source,
390 struct weston_surface *icon,
391 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392{
Kristian Høgsberge2173b52013-06-25 11:28:18 -0400393 struct weston_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400394
Peter Huttererf3d62272013-08-08 11:57:05 +1000395 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700396 if (drag == NULL)
397 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400399 drag->grab.interface = &drag_grab_interface;
400 drag->client = client;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700401 drag->data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400403 if (icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500404 drag->icon = weston_view_create(icon);
405 if (drag->icon == NULL) {
406 free(drag);
407 return -1;
408 }
409
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400410 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500411 wl_signal_add(&icon->destroy_signal,
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400412 &drag->icon_destroy_listener);
413
414 icon->configure = drag_surface_configure;
415 icon->configure_private = drag;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500416 } else {
417 drag->icon = NULL;
418 }
419
420 if (source) {
421 drag->data_source_listener.notify = destroy_data_device_source;
422 wl_signal_add(&source->destroy_signal,
423 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400424 }
425
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400426 weston_pointer_set_focus(seat->pointer, NULL,
427 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400428 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700429
430 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400431}
432
433static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700434data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
435 struct wl_resource *source_resource,
436 struct wl_resource *origin_resource,
437 struct wl_resource *icon_resource, uint32_t serial)
438{
439 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700440 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700441 struct weston_surface *icon = NULL;
442
443 if (seat->pointer->button_count == 0 ||
444 seat->pointer->grab_serial != serial ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 !seat->pointer->focus ||
446 seat->pointer->focus->surface != wl_resource_get_user_data(origin_resource))
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700447 return;
448
449 /* FIXME: Check that the data source type array isn't empty. */
450
451 if (source_resource)
452 source = wl_resource_get_user_data(source_resource);
453 if (icon_resource)
454 icon = wl_resource_get_user_data(icon_resource);
455 if (icon && icon->configure) {
456 wl_resource_post_error(icon_resource,
457 WL_DISPLAY_ERROR_INVALID_OBJECT,
458 "surface->configure already set");
459 return;
460 }
461
462 if (weston_seat_start_drag(seat, source, icon, client) < 0)
463 wl_resource_post_no_memory(resource);
464}
465
466static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400467destroy_selection_data_source(struct wl_listener *listener, void *data)
468{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400469 struct weston_seat *seat = container_of(listener, struct weston_seat,
470 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400471 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100472 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400473
474 seat->selection_data_source = NULL;
475
476 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100477 focus = seat->keyboard->focus;
478 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500479 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100480 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400481 if (data_device)
482 wl_data_device_send_selection(data_device, NULL);
483 }
484
485 wl_signal_emit(&seat->selection_signal, seat);
486}
487
488WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400489weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700490 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491{
492 struct wl_resource *data_device, *offer;
Neil Roberts96d790e2013-09-19 17:32:00 +0100493 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400494
495 if (seat->selection_data_source &&
496 seat->selection_serial - serial < UINT32_MAX / 2)
497 return;
498
499 if (seat->selection_data_source) {
500 seat->selection_data_source->cancel(seat->selection_data_source);
501 wl_list_remove(&seat->selection_data_source_listener.link);
502 seat->selection_data_source = NULL;
503 }
504
505 seat->selection_data_source = source;
506 seat->selection_serial = serial;
507
508 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100509 focus = seat->keyboard->focus;
510 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500511 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100512 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400513 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700514 offer = weston_data_source_send_offer(seat->selection_data_source,
515 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400516 wl_data_device_send_selection(data_device, offer);
517 } else if (data_device) {
518 wl_data_device_send_selection(data_device, NULL);
519 }
520 }
521
522 wl_signal_emit(&seat->selection_signal, seat);
523
524 if (source) {
525 seat->selection_data_source_listener.notify =
526 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500527 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400528 &seat->selection_data_source_listener);
529 }
530}
531
532static void
533data_device_set_selection(struct wl_client *client,
534 struct wl_resource *resource,
535 struct wl_resource *source_resource, uint32_t serial)
536{
537 if (!source_resource)
538 return;
539
540 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500541 weston_seat_set_selection(wl_resource_get_user_data(resource),
542 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400543 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400544}
545
546static const struct wl_data_device_interface data_device_interface = {
547 data_device_start_drag,
548 data_device_set_selection,
549};
550
551static void
552destroy_data_source(struct wl_resource *resource)
553{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700554 struct weston_data_source *source =
555 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400556 char **p;
557
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500558 wl_signal_emit(&source->destroy_signal, source);
559
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400560 wl_array_for_each(p, &source->mime_types)
561 free(*p);
562
563 wl_array_release(&source->mime_types);
564
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400565 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400566}
567
568static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700569client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400570 uint32_t time, const char *mime_type)
571{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500572 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400573}
574
575static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700576client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400577 const char *mime_type, int32_t fd)
578{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500579 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400580 close(fd);
581}
582
583static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700584client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400585{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500586 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400587}
588
589static void
590create_data_source(struct wl_client *client,
591 struct wl_resource *resource, uint32_t id)
592{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700593 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400594
595 source = malloc(sizeof *source);
596 if (source == NULL) {
597 wl_resource_post_no_memory(resource);
598 return;
599 }
600
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500601 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400602 source->accept = client_source_accept;
603 source->send = client_source_send;
604 source->cancel = client_source_cancel;
605
606 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500607
Jason Ekstranda85118c2013-06-27 20:17:02 -0500608 source->resource =
609 wl_resource_create(client, &wl_data_source_interface, 1, id);
610 wl_resource_set_implementation(source->resource, &data_source_interface,
611 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400612}
613
614static void unbind_data_device(struct wl_resource *resource)
615{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500616 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400617}
618
619static void
620get_data_device(struct wl_client *client,
621 struct wl_resource *manager_resource,
622 uint32_t id, struct wl_resource *seat_resource)
623{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500624 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400625 struct wl_resource *resource;
626
Jason Ekstranda85118c2013-06-27 20:17:02 -0500627 resource = wl_resource_create(client,
628 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700629 if (resource == NULL) {
630 wl_resource_post_no_memory(manager_resource);
631 return;
632 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700634 wl_list_insert(&seat->drag_resource_list,
635 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500636 wl_resource_set_implementation(resource, &data_device_interface,
637 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400638}
639
640static const struct wl_data_device_manager_interface manager_interface = {
641 create_data_source,
642 get_data_device
643};
644
645static void
646bind_manager(struct wl_client *client,
647 void *data, uint32_t version, uint32_t id)
648{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500649 struct wl_resource *resource;
650
651 resource =
652 wl_resource_create(client,
653 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700654 if (resource == NULL) {
655 wl_client_post_no_memory(client);
656 return;
657 }
658
659 wl_resource_set_implementation(resource, &manager_interface,
660 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400661}
662
663WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400664wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400665{
Neil Roberts96d790e2013-09-19 17:32:00 +0100666 struct wl_resource *data_device, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700667 struct weston_data_source *source;
Neil Roberts96d790e2013-09-19 17:32:00 +0100668 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400669
670 if (!seat->keyboard)
671 return;
672
Neil Roberts96d790e2013-09-19 17:32:00 +0100673 focus = seat->keyboard->focus;
674 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400675 return;
676
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500677 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100678 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400679 if (!data_device)
680 return;
681
682 source = seat->selection_data_source;
683 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700684 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400685 wl_data_device_send_selection(data_device, offer);
686 }
687}
688
689WL_EXPORT int
690wl_data_device_manager_init(struct wl_display *display)
691{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400692 if (wl_global_create(display,
693 &wl_data_device_manager_interface, 1,
694 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400695 return -1;
696
697 return 0;
698}