blob: 2000f90e8a5cc98485dfee10fe9dc6f0c1c656d1 [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
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <stdio.h>
27
28#include "compositor.h"
29
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040030struct weston_drag {
31 struct wl_client *client;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -070032 struct weston_data_source *data_source;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040033 struct wl_listener data_source_listener;
34 struct weston_surface *focus;
35 struct wl_resource *focus_resource;
36 struct wl_listener focus_listener;
37 struct weston_pointer_grab grab;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -040038 struct weston_surface *icon;
39 struct wl_listener icon_destroy_listener;
Kristian Høgsberg054c50a2013-05-08 15:27:47 -040040 int32_t dx, dy;
41};
42
Kristian Høgsberg2158a882013-04-18 15:07:39 -040043static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -040044empty_region(pixman_region32_t *region)
45{
46 pixman_region32_fini(region);
47 pixman_region32_init(region);
48}
49
50static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -040051data_offer_accept(struct wl_client *client, struct wl_resource *resource,
52 uint32_t serial, const char *mime_type)
53{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070054 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040055
56 /* FIXME: Check that client is currently focused by the input
57 * device that is currently dragging this data source. Should
58 * this be a wl_data_device request? */
59
60 if (offer->source)
61 offer->source->accept(offer->source, serial, mime_type);
62}
63
64static void
65data_offer_receive(struct wl_client *client, struct wl_resource *resource,
66 const char *mime_type, int32_t fd)
67{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070068 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040069
70 if (offer->source)
71 offer->source->send(offer->source, mime_type, fd);
72 else
73 close(fd);
74}
75
76static void
77data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
78{
79 wl_resource_destroy(resource);
80}
81
82static const struct wl_data_offer_interface data_offer_interface = {
83 data_offer_accept,
84 data_offer_receive,
85 data_offer_destroy,
86};
87
88static void
89destroy_data_offer(struct wl_resource *resource)
90{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -070091 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040092
93 if (offer->source)
94 wl_list_remove(&offer->source_destroy_listener.link);
95 free(offer);
96}
97
98static void
99destroy_offer_data_source(struct wl_listener *listener, void *data)
100{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700101 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400102
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700103 offer = container_of(listener, struct weston_data_offer,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400104 source_destroy_listener);
105
106 offer->source = NULL;
107}
108
109static struct wl_resource *
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700110weston_data_source_send_offer(struct weston_data_source *source,
111 struct wl_resource *target)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400112{
Kristian Høgsberg5e76a492013-07-25 16:09:37 -0700113 struct weston_data_offer *offer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400114 char **p;
115
116 offer = malloc(sizeof *offer);
117 if (offer == NULL)
118 return NULL;
119
Jason Ekstranda85118c2013-06-27 20:17:02 -0500120 offer->resource =
121 wl_resource_create(wl_resource_get_client(target),
122 &wl_data_offer_interface, 1, 0);
123 wl_resource_set_implementation(offer->resource, &data_offer_interface,
124 offer, destroy_data_offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400125
126 offer->source = source;
127 offer->source_destroy_listener.notify = destroy_offer_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500128 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400129 &offer->source_destroy_listener);
130
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500131 wl_data_device_send_data_offer(target, offer->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400132
133 wl_array_for_each(p, &source->mime_types)
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500134 wl_data_offer_send_offer(offer->resource, *p);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400135
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500136 return offer->resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400137}
138
139static void
140data_source_offer(struct wl_client *client,
141 struct wl_resource *resource,
142 const char *type)
143{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700144 struct weston_data_source *source =
145 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146 char **p;
147
148 p = wl_array_add(&source->mime_types, sizeof *p);
149 if (p)
150 *p = strdup(type);
151 if (!p || !*p)
152 wl_resource_post_no_memory(resource);
153}
154
155static void
156data_source_destroy(struct wl_client *client, struct wl_resource *resource)
157{
158 wl_resource_destroy(resource);
159}
160
161static struct wl_data_source_interface data_source_interface = {
162 data_source_offer,
163 data_source_destroy
164};
165
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400166static void
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400167drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
168{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400169 struct weston_drag *drag = es->configure_private;
170 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400171 struct wl_list *list;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400172 float fx, fy;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400173
174 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400175 if (pointer->sprite && weston_surface_is_mapped(pointer->sprite))
176 list = &pointer->sprite->layer_link;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400177 else
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400178 list = &es->compositor->cursor_layer.surface_list;
Kristian Høgsberg415f30c2013-05-07 22:42:28 -0400179
180 wl_list_insert(list, &es->layer_link);
181 weston_surface_update_transform(es);
182 empty_region(&es->pending.input);
183 }
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400184
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400185 drag->dx += sx;
186 drag->dy += sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400187
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400188 fx = wl_fixed_to_double(pointer->x) + drag->dx;
189 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400190 weston_surface_configure(es, fx, fy, width, height);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400191}
192
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400193static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400194destroy_drag_focus(struct wl_listener *listener, void *data)
195{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400196 struct weston_drag *drag =
197 container_of(listener, struct weston_drag, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400198
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400199 drag->focus_resource = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400200}
201
202static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400203weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface,
204 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205{
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400206 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400207 struct wl_resource *resource, *offer = NULL;
208 struct wl_display *display;
209 uint32_t serial;
210
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400211 if (drag->focus_resource) {
212 wl_data_device_send_leave(drag->focus_resource);
213 wl_list_remove(&drag->focus_listener.link);
214 drag->focus_resource = NULL;
215 drag->focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400216 }
217
218 if (!surface)
219 return;
220
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500221 if (!drag->data_source &&
222 wl_resource_get_client(surface->resource) != drag->client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400223 return;
224
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500225 resource = wl_resource_find_for_client(&pointer->seat->drag_resource_list,
226 wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400227 if (!resource)
228 return;
229
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500230 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400231 serial = wl_display_next_serial(display);
232
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400233 if (drag->data_source)
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700234 offer = weston_data_source_send_offer(drag->data_source,
235 resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400236
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500237 wl_data_device_send_enter(resource, serial, surface->resource,
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400238 sx, sy, offer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400239
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400240 drag->focus = surface;
241 drag->focus_listener.notify = destroy_drag_focus;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500242 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400243 drag->focus_resource = resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400244}
245
246static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400247drag_grab_focus(struct weston_pointer_grab *grab)
248{
249 struct weston_drag *drag =
250 container_of(grab, struct weston_drag, grab);
251 struct weston_pointer *pointer = grab->pointer;
252 struct weston_surface *surface;
253 wl_fixed_t sx, sy;
254
255 surface = weston_compositor_pick_surface(pointer->seat->compositor,
256 pointer->x, pointer->y,
257 &sx, &sy);
258 if (drag->focus != surface)
259 weston_drag_set_focus(drag, surface, sx, sy);
260}
261
262static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400263drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400264{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400265 struct weston_drag *drag =
266 container_of(grab, struct weston_drag, grab);
267 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400268 float fx, fy;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400269 wl_fixed_t sx, sy;
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400270
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400271 if (drag->icon) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400272 fx = wl_fixed_to_double(pointer->x) + drag->dx;
273 fy = wl_fixed_to_double(pointer->y) + drag->dy;
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400274 weston_surface_set_position(drag->icon, fx, fy);
275 weston_surface_schedule_repaint(drag->icon);
Kristian Høgsbergaad80992013-05-07 22:53:43 -0400276 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400277
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400278 if (drag->focus_resource) {
279 weston_surface_from_global_fixed(drag->focus,
280 pointer->x, pointer->y,
281 &sx, &sy);
282
283 wl_data_device_send_motion(drag->focus_resource, time, sx, sy);
284 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400285}
286
287static void
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400288data_device_end_drag_grab(struct weston_drag *drag)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400289{
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400290 if (drag->icon) {
291 if (weston_surface_is_mapped(drag->icon))
292 weston_surface_unmap(drag->icon);
293
294 drag->icon->configure = NULL;
295 empty_region(&drag->icon->pending.input);
296 wl_list_remove(&drag->icon_destroy_listener.link);
297 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400298
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400299 weston_drag_set_focus(drag, NULL, 0, 0);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400300
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400301 weston_pointer_end_grab(drag->grab.pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400302
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400303 free(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400304}
305
306static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400307drag_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308 uint32_t time, uint32_t button, uint32_t state_w)
309{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400310 struct weston_drag *drag =
311 container_of(grab, struct weston_drag, grab);
312 struct weston_pointer *pointer = drag->grab.pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400313 enum wl_pointer_button_state state = state_w;
314
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400315 if (drag->focus_resource &&
316 pointer->grab_button == button &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400317 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400318 wl_data_device_send_drop(drag->focus_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400319
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400320 if (pointer->button_count == 0 &&
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400322 if (drag->data_source)
323 wl_list_remove(&drag->data_source_listener.link);
324 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400325 }
326}
327
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400328static const struct weston_pointer_grab_interface drag_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400329 drag_grab_focus,
330 drag_grab_motion,
331 drag_grab_button,
332};
333
334static void
335destroy_data_device_source(struct wl_listener *listener, void *data)
336{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400337 struct weston_drag *drag = container_of(listener, struct weston_drag,
338 data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400339
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400340 data_device_end_drag_grab(drag);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341}
342
343static void
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400344handle_drag_icon_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400345{
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400346 struct weston_drag *drag = container_of(listener, struct weston_drag,
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400347 icon_destroy_listener);
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400348
Kristian Høgsbergc43aad12013-05-08 15:30:42 -0400349 drag->icon = NULL;
Kristian Høgsberg7848bb62013-05-07 11:18:46 -0400350}
351
352static void
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400353data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
354 struct wl_resource *source_resource,
355 struct wl_resource *origin_resource,
356 struct wl_resource *icon_resource, uint32_t serial)
357{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500358 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsberge2173b52013-06-25 11:28:18 -0400359 struct weston_drag *drag;
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400360 struct weston_surface *icon = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400361
Kristian Høgsbergdba25862013-05-08 15:53:42 -0400362 if (seat->pointer->button_count == 0 ||
363 seat->pointer->grab_serial != serial ||
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500364 seat->pointer->focus != wl_resource_get_user_data(origin_resource))
Kristian Høgsbergdba25862013-05-08 15:53:42 -0400365 return;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400366
367 /* FIXME: Check that the data source type array isn't empty. */
368
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400369 if (icon_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500370 icon = wl_resource_get_user_data(icon_resource);
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400371 if (icon && icon->configure) {
372 wl_resource_post_error(icon_resource,
373 WL_DISPLAY_ERROR_INVALID_OBJECT,
374 "surface->configure already set");
375 return;
376 }
377
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400378 drag = malloc(sizeof *drag);
379 if (drag == NULL) {
380 wl_resource_post_no_memory(resource);
381 return;
382 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400383
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400384 memset(drag, 0, sizeof *drag);
385 drag->grab.interface = &drag_grab_interface;
386 drag->client = client;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400387
388 if (source_resource) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500389 drag->data_source = wl_resource_get_user_data(source_resource);
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400390 drag->data_source_listener.notify = destroy_data_device_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500391 wl_resource_add_destroy_listener(source_resource,
392 &drag->data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400393 }
394
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400395 if (icon) {
396 drag->icon = icon;
397 drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500398 wl_signal_add(&icon->destroy_signal,
Kristian Høgsberg5a9fb352013-05-08 15:47:52 -0400399 &drag->icon_destroy_listener);
400
401 icon->configure = drag_surface_configure;
402 icon->configure_private = drag;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400403 }
404
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400405 weston_pointer_set_focus(seat->pointer, NULL,
406 wl_fixed_from_int(0), wl_fixed_from_int(0));
Kristian Høgsberg054c50a2013-05-08 15:27:47 -0400407 weston_pointer_start_grab(seat->pointer, &drag->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408}
409
410static void
411destroy_selection_data_source(struct wl_listener *listener, void *data)
412{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400413 struct weston_seat *seat = container_of(listener, struct weston_seat,
414 selection_data_source_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400415 struct wl_resource *data_device;
416 struct wl_resource *focus = NULL;
417
418 seat->selection_data_source = NULL;
419
420 if (seat->keyboard)
421 focus = seat->keyboard->focus_resource;
422 if (focus) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500423 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
424 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400425 if (data_device)
426 wl_data_device_send_selection(data_device, NULL);
427 }
428
429 wl_signal_emit(&seat->selection_signal, seat);
430}
431
432WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400433weston_seat_set_selection(struct weston_seat *seat,
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700434 struct weston_data_source *source, uint32_t serial)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400435{
436 struct wl_resource *data_device, *offer;
437 struct wl_resource *focus = NULL;
438
439 if (seat->selection_data_source &&
440 seat->selection_serial - serial < UINT32_MAX / 2)
441 return;
442
443 if (seat->selection_data_source) {
444 seat->selection_data_source->cancel(seat->selection_data_source);
445 wl_list_remove(&seat->selection_data_source_listener.link);
446 seat->selection_data_source = NULL;
447 }
448
449 seat->selection_data_source = source;
450 seat->selection_serial = serial;
451
452 if (seat->keyboard)
453 focus = seat->keyboard->focus_resource;
454 if (focus) {
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500455 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
456 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400457 if (data_device && source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700458 offer = weston_data_source_send_offer(seat->selection_data_source,
459 data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400460 wl_data_device_send_selection(data_device, offer);
461 } else if (data_device) {
462 wl_data_device_send_selection(data_device, NULL);
463 }
464 }
465
466 wl_signal_emit(&seat->selection_signal, seat);
467
468 if (source) {
469 seat->selection_data_source_listener.notify =
470 destroy_selection_data_source;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500471 wl_signal_add(&source->destroy_signal,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400472 &seat->selection_data_source_listener);
473 }
474}
475
476static void
477data_device_set_selection(struct wl_client *client,
478 struct wl_resource *resource,
479 struct wl_resource *source_resource, uint32_t serial)
480{
481 if (!source_resource)
482 return;
483
484 /* FIXME: Store serial and check against incoming serial here. */
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500485 weston_seat_set_selection(wl_resource_get_user_data(resource),
486 wl_resource_get_user_data(source_resource),
Kristian Høgsberge3148752013-05-06 23:19:49 -0400487 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400488}
489
490static const struct wl_data_device_interface data_device_interface = {
491 data_device_start_drag,
492 data_device_set_selection,
493};
494
495static void
496destroy_data_source(struct wl_resource *resource)
497{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700498 struct weston_data_source *source =
499 wl_resource_get_user_data(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400500 char **p;
501
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500502 wl_signal_emit(&source->destroy_signal, source);
503
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400504 wl_array_for_each(p, &source->mime_types)
505 free(*p);
506
507 wl_array_release(&source->mime_types);
508
Kristian Høgsberg489b2792013-06-25 11:26:31 -0400509 free(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400510}
511
512static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700513client_source_accept(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400514 uint32_t time, const char *mime_type)
515{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500516 wl_data_source_send_target(source->resource, mime_type);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400517}
518
519static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700520client_source_send(struct weston_data_source *source,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400521 const char *mime_type, int32_t fd)
522{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500523 wl_data_source_send_send(source->resource, mime_type, fd);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524 close(fd);
525}
526
527static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700528client_source_cancel(struct weston_data_source *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400529{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500530 wl_data_source_send_cancelled(source->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400531}
532
533static void
534create_data_source(struct wl_client *client,
535 struct wl_resource *resource, uint32_t id)
536{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700537 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400538
539 source = malloc(sizeof *source);
540 if (source == NULL) {
541 wl_resource_post_no_memory(resource);
542 return;
543 }
544
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500545 wl_signal_init(&source->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546 source->accept = client_source_accept;
547 source->send = client_source_send;
548 source->cancel = client_source_cancel;
549
550 wl_array_init(&source->mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500551
Jason Ekstranda85118c2013-06-27 20:17:02 -0500552 source->resource =
553 wl_resource_create(client, &wl_data_source_interface, 1, id);
554 wl_resource_set_implementation(source->resource, &data_source_interface,
555 source, destroy_data_source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400556}
557
558static void unbind_data_device(struct wl_resource *resource)
559{
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500560 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400561}
562
563static void
564get_data_device(struct wl_client *client,
565 struct wl_resource *manager_resource,
566 uint32_t id, struct wl_resource *seat_resource)
567{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500568 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400569 struct wl_resource *resource;
570
Jason Ekstranda85118c2013-06-27 20:17:02 -0500571 resource = wl_resource_create(client,
572 &wl_data_device_interface, 1, id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400573
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500574 wl_list_insert(&seat->drag_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -0500575 wl_resource_set_implementation(resource, &data_device_interface,
576 seat, unbind_data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400577}
578
579static const struct wl_data_device_manager_interface manager_interface = {
580 create_data_source,
581 get_data_device
582};
583
584static void
585bind_manager(struct wl_client *client,
586 void *data, uint32_t version, uint32_t id)
587{
Jason Ekstranda85118c2013-06-27 20:17:02 -0500588 struct wl_resource *resource;
589
590 resource =
591 wl_resource_create(client,
592 &wl_data_device_manager_interface, 1, id);
593 if (resource)
594 wl_resource_set_implementation(resource, &manager_interface,
595 NULL, NULL);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400596}
597
598WL_EXPORT void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400599wl_data_device_set_keyboard_focus(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400600{
601 struct wl_resource *data_device, *focus, *offer;
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700602 struct weston_data_source *source;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400603
604 if (!seat->keyboard)
605 return;
606
607 focus = seat->keyboard->focus_resource;
608 if (!focus)
609 return;
610
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500611 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
612 wl_resource_get_client(focus));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400613 if (!data_device)
614 return;
615
616 source = seat->selection_data_source;
617 if (source) {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700618 offer = weston_data_source_send_offer(source, data_device);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400619 wl_data_device_send_selection(data_device, offer);
620 }
621}
622
623WL_EXPORT int
624wl_data_device_manager_init(struct wl_display *display)
625{
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400626 if (wl_global_create(display,
627 &wl_data_device_manager_interface, 1,
628 NULL, bind_manager) == NULL)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400629 return -1;
630
631 return 0;
632}