blob: 3e22b5137d0c6542cfa8aefdf0d3d15f339b7bf4 [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
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400347static const struct weston_pointer_grab_interface drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400348 drag_grab_focus,
349 drag_grab_motion,
350 drag_grab_button,
351};
352
353static void
354destroy_data_device_source(struct wl_listener *listener, void *data)
355{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400356 struct weston_drag *drag = container_of(listener, struct weston_drag,
357 data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400358
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400359 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400360}
361
362static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400363handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400364{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400365 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400366 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400367
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400368 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400369}
370
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700371WL_EXPORT int
372weston_seat_start_drag(struct weston_seat *seat,
373 struct weston_data_source *source,
374 struct weston_surface *icon,
375 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400376{
Kristian Høgsberge2173b52013-06-25 11:28:18 -0400377 struct weston_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400378
Peter Huttererf3d62272013-08-08 11:57:05 +1000379 drag = zalloc(sizeof *drag);
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700380 if (drag == NULL)
381 return -1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400382
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400383 drag->grab.interface = &drag_grab_interface;
384 drag->client = client;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700385 drag->data_source = source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400386
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400387 if (icon) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500388 drag->icon = weston_view_create(icon);
389 if (drag->icon == NULL) {
390 free(drag);
391 return -1;
392 }
393
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400394 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500395 wl_signal_add(&icon->destroy_signal,
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400396 &drag->icon_destroy_listener);
397
398 icon->configure = drag_surface_configure;
399 icon->configure_private = drag;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400 } else {
401 drag->icon = NULL;
402 }
403
404 if (source) {
405 drag->data_source_listener.notify = destroy_data_device_source;
406 wl_signal_add(&source->destroy_signal,
407 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408 }
409
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400410 weston_pointer_set_focus(seat->pointer, NULL,
411 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400412 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg0abad072013-09-11 09:42:26 -0700413
414 return 0;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400415}
416
417static void
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700418data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
419 struct wl_resource *source_resource,
420 struct wl_resource *origin_resource,
421 struct wl_resource *icon_resource, uint32_t serial)
422{
423 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberg1702d4c2013-09-11 09:45:03 -0700424 struct weston_data_source *source = NULL;
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700425 struct weston_surface *icon = NULL;
426
427 if (seat->pointer->button_count == 0 ||
428 seat->pointer->grab_serial != serial ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500429 !seat->pointer->focus ||
430 seat->pointer->focus->surface != wl_resource_get_user_data(origin_resource))
Kristian Høgsberg85de9c22013-09-04 20:44:26 -0700431 return;
432
433 /* FIXME: Check that the data source type array isn't empty. */
434
435 if (source_resource)
436 source = wl_resource_get_user_data(source_resource);
437 if (icon_resource)
438 icon = wl_resource_get_user_data(icon_resource);
439 if (icon && icon->configure) {
440 wl_resource_post_error(icon_resource,
441 WL_DISPLAY_ERROR_INVALID_OBJECT,
442 "surface->configure already set");
443 return;
444 }
445
446 if (weston_seat_start_drag(seat, source, icon, client) < 0)
447 wl_resource_post_no_memory(resource);
448}
449
450static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400451destroy_selection_data_source(struct wl_listener *listener, void *data)
452{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400453 struct weston_seat *seat = container_of(listener, struct weston_seat,
454 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400455 struct wl_resource *data_device;
Neil Roberts96d790e2013-09-19 17:32:00 +0100456 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400457
458 seat->selection_data_source = NULL;
459
460 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100461 focus = seat->keyboard->focus;
462 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500463 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100464 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400465 if (data_device)
466 wl_data_device_send_selection(data_device, NULL);
467 }
468
469 wl_signal_emit(&seat->selection_signal, seat);
470}
471
472WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400473weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700474 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400475{
476 struct wl_resource *data_device, *offer;
Neil Roberts96d790e2013-09-19 17:32:00 +0100477 struct weston_surface *focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478
479 if (seat->selection_data_source &&
480 seat->selection_serial - serial < UINT32_MAX / 2)
481 return;
482
483 if (seat->selection_data_source) {
484 seat->selection_data_source->cancel(seat->selection_data_source);
485 wl_list_remove(&seat->selection_data_source_listener.link);
486 seat->selection_data_source = NULL;
487 }
488
489 seat->selection_data_source = source;
490 seat->selection_serial = serial;
491
492 if (seat->keyboard)
Neil Roberts96d790e2013-09-19 17:32:00 +0100493 focus = seat->keyboard->focus;
494 if (focus && focus->resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500495 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100496 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400497 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700498 offer = weston_data_source_send_offer(seat->selection_data_source,
499 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400500 wl_data_device_send_selection(data_device, offer);
501 } else if (data_device) {
502 wl_data_device_send_selection(data_device, NULL);
503 }
504 }
505
506 wl_signal_emit(&seat->selection_signal, seat);
507
508 if (source) {
509 seat->selection_data_source_listener.notify =
510 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500511 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400512 &seat->selection_data_source_listener);
513 }
514}
515
516static void
517data_device_set_selection(struct wl_client *client,
518 struct wl_resource *resource,
519 struct wl_resource *source_resource, uint32_t serial)
520{
521 if (!source_resource)
522 return;
523
524 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500525 weston_seat_set_selection(wl_resource_get_user_data(resource),
526 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400527 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400528}
529
530static const struct wl_data_device_interface data_device_interface = {
531 data_device_start_drag,
532 data_device_set_selection,
533};
534
535static void
536destroy_data_source(struct wl_resource *resource)
537{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700538 struct weston_data_source *source =
539 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540 char **p;
541
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500542 wl_signal_emit(&source->destroy_signal, source);
543
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400544 wl_array_for_each(p, &source->mime_types)
545 free(*p);
546
547 wl_array_release(&source->mime_types);
548
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400549 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400550}
551
552static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700553client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400554 uint32_t time, const char *mime_type)
555{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500556 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400557}
558
559static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700560client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400561 const char *mime_type, int32_t fd)
562{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500563 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400564 close(fd);
565}
566
567static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700568client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400569{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500570 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400571}
572
573static void
574create_data_source(struct wl_client *client,
575 struct wl_resource *resource, uint32_t id)
576{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700577 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400578
579 source = malloc(sizeof *source);
580 if (source == NULL) {
581 wl_resource_post_no_memory(resource);
582 return;
583 }
584
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500585 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400586 source->accept = client_source_accept;
587 source->send = client_source_send;
588 source->cancel = client_source_cancel;
589
590 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500591
Jason Ekstranda85118c2013-06-27 20:17:02 -0500592 source->resource =
593 wl_resource_create(client, &wl_data_source_interface, 1, id);
594 wl_resource_set_implementation(source->resource, &data_source_interface,
595 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400596}
597
598static void unbind_data_device(struct wl_resource *resource)
599{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500600 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400601}
602
603static void
604get_data_device(struct wl_client *client,
605 struct wl_resource *manager_resource,
606 uint32_t id, struct wl_resource *seat_resource)
607{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500608 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400609 struct wl_resource *resource;
610
Jason Ekstranda85118c2013-06-27 20:17:02 -0500611 resource = wl_resource_create(client,
612 &wl_data_device_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700613 if (resource == NULL) {
614 wl_resource_post_no_memory(manager_resource);
615 return;
616 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400617
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700618 wl_list_insert(&seat->drag_resource_list,
619 wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500620 wl_resource_set_implementation(resource, &data_device_interface,
621 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400622}
623
624static const struct wl_data_device_manager_interface manager_interface = {
625 create_data_source,
626 get_data_device
627};
628
629static void
630bind_manager(struct wl_client *client,
631 void *data, uint32_t version, uint32_t id)
632{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500633 struct wl_resource *resource;
634
635 resource =
636 wl_resource_create(client,
637 &wl_data_device_manager_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -0700638 if (resource == NULL) {
639 wl_client_post_no_memory(client);
640 return;
641 }
642
643 wl_resource_set_implementation(resource, &manager_interface,
644 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400645}
646
647WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400648wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400649{
Neil Roberts96d790e2013-09-19 17:32:00 +0100650 struct wl_resource *data_device, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700651 struct weston_data_source *source;
Neil Roberts96d790e2013-09-19 17:32:00 +0100652 struct weston_surface *focus;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400653
654 if (!seat->keyboard)
655 return;
656
Neil Roberts96d790e2013-09-19 17:32:00 +0100657 focus = seat->keyboard->focus;
658 if (!focus || !focus->resource)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400659 return;
660
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500661 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +0100662 wl_resource_get_client(focus->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400663 if (!data_device)
664 return;
665
666 source = seat->selection_data_source;
667 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700668 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400669 wl_data_device_send_selection(data_device, offer);
670 }
671}
672
673WL_EXPORT int
674wl_data_device_manager_init(struct wl_display *display)
675{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400676 if (wl_global_create(display,
677 &wl_data_device_manager_interface, 1,
678 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400679 return -1;
680
681 return 0;
682}