blob: 888e60652321593c7c05bc07d49a7d12aa449d5f [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
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400281drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400282{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400283 struct weston_drag *drag =
284 container_of(grab, struct weston_drag, grab);
285 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400286 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400287 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400288
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400289 if (drag->icon) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400290 fx = wl_fixed_to_double(pointer->x) + drag->dx;
291 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 weston_view_set_position(drag->icon, fx, fy);
293 weston_view_schedule_repaint(drag->icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400294 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400295
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400296 if (drag->focus_resource) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297 weston_view_from_global_fixed(drag->focus,
298 pointer->x, pointer->y,
299 &sx, &sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400300
301 wl_data_device_send_motion(drag->focus_resource, time, sx, sy);
302 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400303}
304
305static void
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400306data_device_end_drag_grab(struct weston_drag *drag)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400307{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400308 if (drag->icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500309 if (weston_view_is_mapped(drag->icon))
310 weston_view_unmap(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400311
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 drag->icon->surface->configure = NULL;
313 empty_region(&drag->icon->surface->pending.input);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400314 wl_list_remove(&drag->icon_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500315 weston_view_destroy(drag->icon);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400316 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400317
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400318 weston_drag_set_focus(drag, NULL, 0, 0);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400319
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400320 weston_pointer_end_grab(drag->grab.pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400322 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400323}
324
325static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400326drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327 uint32_t time, uint32_t button, uint32_t state_w)
328{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400329 struct weston_drag *drag =
330 container_of(grab, struct weston_drag, grab);
331 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400332 enum wl_pointer_button_state state = state_w;
333
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400334 if (drag->focus_resource &&
335 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400336 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400337 wl_data_device_send_drop(drag->focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400339 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400340 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400341 if (drag->data_source)
342 wl_list_remove(&drag->data_source_listener.link);
343 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400344 }
345}
346
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200347static void
348drag_grab_cancel(struct weston_pointer_grab *grab)
349{
350 struct weston_drag *drag =
351 container_of(grab, struct weston_drag, grab);
352
353 if (drag->data_source)
354 wl_list_remove(&drag->data_source_listener.link);
355
356 data_device_end_drag_grab(drag);
357}
358
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400359static const struct weston_pointer_grab_interface drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400360 drag_grab_focus,
361 drag_grab_motion,
362 drag_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200363 drag_grab_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400364};
365
366static void
367destroy_data_device_source(struct wl_listener *listener, void *data)
368{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400369 struct weston_drag *drag = container_of(listener, struct weston_drag,
370 data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400371
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400372 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400373}
374
375static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400376handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400377{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400378 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400379 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400380
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400381 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400382}
383
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700384WL_EXPORT int
385weston_seat_start_drag(struct weston_seat *seat,
386 struct weston_data_source *source,
387 struct weston_surface *icon,
388 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400389{
Kristian Høgsberge2173b52013-06-25 11:28:18 -0400390 struct weston_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400391
Peter Huttererf3d62272013-08-08 11:57:05 +1000392 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700393 if (drag == NULL)
394 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400395
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400396 drag->grab.interface = &drag_grab_interface;
397 drag->client = client;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700398 drag->data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400399
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400400 if (icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500401 drag->icon = weston_view_create(icon);
402 if (drag->icon == NULL) {
403 free(drag);
404 return -1;
405 }
406
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400407 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500408 wl_signal_add(&icon->destroy_signal,
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400409 &drag->icon_destroy_listener);
410
411 icon->configure = drag_surface_configure;
412 icon->configure_private = drag;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413 } else {
414 drag->icon = NULL;
415 }
416
417 if (source) {
418 drag->data_source_listener.notify = destroy_data_device_source;
419 wl_signal_add(&source->destroy_signal,
420 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400421 }
422
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400423 weston_pointer_set_focus(seat->pointer, NULL,
424 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400425 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700426
427 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400428}
429
430static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700431data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
432 struct wl_resource *source_resource,
433 struct wl_resource *origin_resource,
434 struct wl_resource *icon_resource, uint32_t serial)
435{
436 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700437 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700438 struct weston_surface *icon = NULL;
439
440 if (seat->pointer->button_count == 0 ||
441 seat->pointer->grab_serial != serial ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442 !seat->pointer->focus ||
443 seat->pointer->focus->surface != wl_resource_get_user_data(origin_resource))
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700444 return;
445
446 /* FIXME: Check that the data source type array isn't empty. */
447
448 if (source_resource)
449 source = wl_resource_get_user_data(source_resource);
450 if (icon_resource)
451 icon = wl_resource_get_user_data(icon_resource);
452 if (icon && icon->configure) {
453 wl_resource_post_error(icon_resource,
454 WL_DISPLAY_ERROR_INVALID_OBJECT,
455 "surface->configure already set");
456 return;
457 }
458
459 if (weston_seat_start_drag(seat, source, icon, client) < 0)
460 wl_resource_post_no_memory(resource);
461}
462
463static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400464destroy_selection_data_source(struct wl_listener *listener, void *data)
465{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400466 struct weston_seat *seat = container_of(listener, struct weston_seat,
467 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400468 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100469 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470
471 seat->selection_data_source = NULL;
472
473 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100474 focus = seat->keyboard->focus;
475 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500476 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100477 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478 if (data_device)
479 wl_data_device_send_selection(data_device, NULL);
480 }
481
482 wl_signal_emit(&seat->selection_signal, seat);
483}
484
485WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400486weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700487 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400488{
489 struct wl_resource *data_device, *offer;
Neil Roberts96d790e2013-09-19 17:32:00 +0100490 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491
492 if (seat->selection_data_source &&
493 seat->selection_serial - serial < UINT32_MAX / 2)
494 return;
495
496 if (seat->selection_data_source) {
497 seat->selection_data_source->cancel(seat->selection_data_source);
498 wl_list_remove(&seat->selection_data_source_listener.link);
499 seat->selection_data_source = NULL;
500 }
501
502 seat->selection_data_source = source;
503 seat->selection_serial = serial;
504
505 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100506 focus = seat->keyboard->focus;
507 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500508 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100509 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400510 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700511 offer = weston_data_source_send_offer(seat->selection_data_source,
512 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400513 wl_data_device_send_selection(data_device, offer);
514 } else if (data_device) {
515 wl_data_device_send_selection(data_device, NULL);
516 }
517 }
518
519 wl_signal_emit(&seat->selection_signal, seat);
520
521 if (source) {
522 seat->selection_data_source_listener.notify =
523 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500524 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400525 &seat->selection_data_source_listener);
526 }
527}
528
529static void
530data_device_set_selection(struct wl_client *client,
531 struct wl_resource *resource,
532 struct wl_resource *source_resource, uint32_t serial)
533{
534 if (!source_resource)
535 return;
536
537 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500538 weston_seat_set_selection(wl_resource_get_user_data(resource),
539 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400540 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400541}
542
543static const struct wl_data_device_interface data_device_interface = {
544 data_device_start_drag,
545 data_device_set_selection,
546};
547
548static void
549destroy_data_source(struct wl_resource *resource)
550{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700551 struct weston_data_source *source =
552 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400553 char **p;
554
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500555 wl_signal_emit(&source->destroy_signal, source);
556
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400557 wl_array_for_each(p, &source->mime_types)
558 free(*p);
559
560 wl_array_release(&source->mime_types);
561
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400562 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400563}
564
565static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700566client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400567 uint32_t time, const char *mime_type)
568{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500569 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400570}
571
572static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700573client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400574 const char *mime_type, int32_t fd)
575{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500576 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400577 close(fd);
578}
579
580static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700581client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400582{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500583 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400584}
585
586static void
587create_data_source(struct wl_client *client,
588 struct wl_resource *resource, uint32_t id)
589{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700590 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400591
592 source = malloc(sizeof *source);
593 if (source == NULL) {
594 wl_resource_post_no_memory(resource);
595 return;
596 }
597
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500598 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400599 source->accept = client_source_accept;
600 source->send = client_source_send;
601 source->cancel = client_source_cancel;
602
603 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500604
Jason Ekstranda85118c2013-06-27 20:17:02 -0500605 source->resource =
606 wl_resource_create(client, &wl_data_source_interface, 1, id);
607 wl_resource_set_implementation(source->resource, &data_source_interface,
608 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400609}
610
611static void unbind_data_device(struct wl_resource *resource)
612{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500613 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400614}
615
616static void
617get_data_device(struct wl_client *client,
618 struct wl_resource *manager_resource,
619 uint32_t id, struct wl_resource *seat_resource)
620{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500621 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400622 struct wl_resource *resource;
623
Jason Ekstranda85118c2013-06-27 20:17:02 -0500624 resource = wl_resource_create(client,
625 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700626 if (resource == NULL) {
627 wl_resource_post_no_memory(manager_resource);
628 return;
629 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400630
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700631 wl_list_insert(&seat->drag_resource_list,
632 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500633 wl_resource_set_implementation(resource, &data_device_interface,
634 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400635}
636
637static const struct wl_data_device_manager_interface manager_interface = {
638 create_data_source,
639 get_data_device
640};
641
642static void
643bind_manager(struct wl_client *client,
644 void *data, uint32_t version, uint32_t id)
645{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500646 struct wl_resource *resource;
647
648 resource =
649 wl_resource_create(client,
650 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700651 if (resource == NULL) {
652 wl_client_post_no_memory(client);
653 return;
654 }
655
656 wl_resource_set_implementation(resource, &manager_interface,
657 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400658}
659
660WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400661wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400662{
Neil Roberts96d790e2013-09-19 17:32:00 +0100663 struct wl_resource *data_device, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700664 struct weston_data_source *source;
Neil Roberts96d790e2013-09-19 17:32:00 +0100665 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400666
667 if (!seat->keyboard)
668 return;
669
Neil Roberts96d790e2013-09-19 17:32:00 +0100670 focus = seat->keyboard->focus;
671 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400672 return;
673
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500674 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100675 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400676 if (!data_device)
677 return;
678
679 source = seat->selection_data_source;
680 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700681 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400682 wl_data_device_send_selection(data_device, offer);
683 }
684}
685
686WL_EXPORT int
687wl_data_device_manager_init(struct wl_display *display)
688{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400689 if (wl_global_create(display,
690 &wl_data_device_manager_interface, 1,
691 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400692 return -1;
693
694 return 0;
695}