2 * Copyright © 2011 Kristian Høgsberg
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.
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
28 #include "wayland-server.h"
31 data_offer_accept(struct wl_client *client, struct wl_resource *resource,
32 uint32_t time, const char *mime_type)
34 struct wl_data_offer *offer = resource->data;
36 /* FIXME: Check that client is currently focused by the input
37 * device that is currently dragging this data source. Should
38 * this be a wl_data_device request? */
41 wl_resource_post_event(&offer->source->resource,
42 WL_DATA_SOURCE_TARGET, mime_type);
46 data_offer_receive(struct wl_client *client, struct wl_resource *resource,
47 const char *mime_type, int32_t fd)
49 struct wl_data_offer *offer = resource->data;
52 wl_resource_post_event(&offer->source->resource,
53 WL_DATA_SOURCE_SEND, mime_type, fd);
59 data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
61 wl_resource_destroy(resource, 0);
65 destroy_data_offer(struct wl_resource *resource)
67 struct wl_data_offer *offer = resource->data;
69 wl_list_remove(&offer->source_destroy_listener.link);
73 static const struct wl_data_offer_interface data_offer_interface = {
80 destroy_offer_data_source(struct wl_listener *listener,
81 struct wl_resource *resource, uint32_t time)
83 struct wl_data_offer *offer;
85 offer = container_of(listener, struct wl_data_offer,
86 source_destroy_listener);
92 data_source_cancel(struct wl_data_source *source)
94 wl_resource_post_event(&source->resource, WL_DATA_SOURCE_CANCELLED);
97 static struct wl_resource *
98 wl_data_source_send_offer(struct wl_data_source *source,
99 struct wl_resource *target)
101 struct wl_data_offer *offer;
104 offer = malloc(sizeof *offer);
108 offer->resource.destroy = destroy_data_offer;
109 offer->resource.object.id = 0;
110 offer->resource.object.interface = &wl_data_offer_interface;
111 offer->resource.object.implementation =
112 (void (**)(void)) source->offer_interface;
113 offer->resource.data = offer;
114 wl_list_init(&offer->resource.destroy_listener_list);
116 offer->source = source;
117 offer->source_destroy_listener.func = destroy_offer_data_source;
118 wl_list_insert(&source->resource.destroy_listener_list,
119 &offer->source_destroy_listener.link);
121 wl_client_add_resource(target->client, &offer->resource);
123 wl_resource_post_event(target,
124 WL_DATA_DEVICE_DATA_OFFER, &offer->resource);
126 end = source->mime_types.data + source->mime_types.size;
127 for (p = source->mime_types.data; p < end; p++)
128 wl_resource_post_event(&offer->resource,
129 WL_DATA_OFFER_OFFER, *p);
131 return &offer->resource;
135 data_source_offer(struct wl_client *client,
136 struct wl_resource *resource,
139 struct wl_data_source *source = resource->data;
142 p = wl_array_add(&source->mime_types, sizeof *p);
146 wl_resource_post_no_memory(resource);
150 data_source_destroy(struct wl_client *client, struct wl_resource *resource)
152 wl_resource_destroy(resource, 0);
155 static struct wl_data_source_interface data_source_interface = {
160 static struct wl_resource *
161 find_resource(struct wl_list *list, struct wl_client *client)
163 struct wl_resource *r;
165 wl_list_for_each(r, list, link) {
166 if (r->client == client)
174 destroy_drag_focus(struct wl_listener *listener,
175 struct wl_resource *resource, uint32_t time)
177 struct wl_input_device *device =
178 container_of(listener, struct wl_input_device,
179 drag_focus_listener);
181 device->drag_focus_resource = NULL;
185 drag_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
186 struct wl_surface *surface, int32_t x, int32_t y)
188 struct wl_input_device *device =
189 container_of(grab, struct wl_input_device, drag_grab);
190 struct wl_resource *resource, *offer;
192 if (device->drag_focus_resource) {
193 wl_resource_post_event(device->drag_focus_resource,
194 WL_DATA_DEVICE_LEAVE);
195 wl_list_remove(&device->drag_focus_listener.link);
196 device->drag_focus_resource = NULL;
197 device->drag_focus = NULL;
201 resource = find_resource(&device->drag_resource_list,
202 surface->resource.client);
203 if (surface && resource) {
204 offer = wl_data_source_send_offer(device->drag_data_source,
207 wl_resource_post_event(resource,
208 WL_DATA_DEVICE_ENTER,
209 time, surface, x, y, offer);
211 device->drag_focus = surface;
212 device->drag_focus_listener.func = destroy_drag_focus;
213 wl_list_insert(resource->destroy_listener_list.prev,
214 &device->drag_focus_listener.link);
215 device->drag_focus_resource = resource;
216 grab->focus = surface;
221 drag_grab_motion(struct wl_pointer_grab *grab,
222 uint32_t time, int32_t x, int32_t y)
224 struct wl_input_device *device =
225 container_of(grab, struct wl_input_device, drag_grab);
227 if (device->drag_focus_resource)
228 wl_resource_post_event(device->drag_focus_resource,
229 WL_DATA_DEVICE_MOTION, time, x, y);
233 drag_grab_button(struct wl_pointer_grab *grab,
234 uint32_t time, int32_t button, int32_t state)
236 struct wl_input_device *device =
237 container_of(grab, struct wl_input_device, drag_grab);
239 if (device->drag_focus_resource &&
240 device->grab_button == button && state == 0)
241 wl_resource_post_event(device->drag_focus_resource,
242 WL_DATA_DEVICE_DROP);
244 if (device->button_count == 0 && state == 0) {
245 wl_input_device_end_pointer_grab(device, time);
247 if (device->drag_surface) {
248 struct wl_resource *surface_resource =
249 &device->drag_surface->resource;
250 struct wl_surface_interface *implementation =
251 (struct wl_surface_interface *)
252 surface_resource->object.implementation;
254 implementation->attach(surface_resource->client,
255 surface_resource, NULL, 0, 0);
256 wl_list_remove(&device->drag_icon_listener.link);
259 device->drag_data_source = NULL;
260 device->drag_surface = NULL;
264 static const struct wl_pointer_grab_interface drag_grab_interface = {
271 destroy_data_device_icon(struct wl_listener *listener,
272 struct wl_resource *resource, uint32_t time)
274 struct wl_input_device *device;
276 device = container_of(listener, struct wl_input_device,
279 device->drag_surface = NULL;
283 data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
284 struct wl_resource *source_resource,
285 struct wl_resource *origin_resource,
286 struct wl_resource *icon_resource, uint32_t time)
288 struct wl_input_device *device = resource->data;
290 /* FIXME: Check that client has implicit grab on the origin
291 * surface that matches the given time. */
293 /* FIXME: Check that the data source type array isn't empty. */
295 device->drag_grab.interface = &drag_grab_interface;
296 device->drag_data_source = source_resource->data;
299 device->drag_surface = icon_resource->data;
300 device->drag_icon_listener.func = destroy_data_device_icon;
301 wl_list_insert(icon_resource->destroy_listener_list.prev,
302 &device->drag_icon_listener.link);
305 wl_input_device_start_pointer_grab(device, &device->drag_grab, time);
309 destroy_selection_data_source(struct wl_listener *listener,
310 struct wl_resource *resource, uint32_t time)
312 struct wl_input_device *device =
313 container_of(listener, struct wl_input_device,
314 selection_data_source_listener);
315 struct wl_resource *data_device, *focus;
317 device->selection_data_source = NULL;
319 focus = device->keyboard_focus_resource;
321 data_device = find_resource(&device->drag_resource_list,
323 wl_resource_post_event(data_device,
324 WL_DATA_DEVICE_SELECTION, NULL);
329 wl_input_device_set_selection(struct wl_input_device *device,
330 struct wl_data_source *source, uint32_t time)
332 struct wl_resource *data_device, *focus, *offer;
333 struct wl_selection_listener *listener, *next;
335 if (device->selection_data_source) {
336 device->selection_data_source->cancel(device->selection_data_source);
337 wl_list_remove(&device->selection_data_source_listener.link);
338 device->selection_data_source = NULL;
341 device->selection_data_source = source;
343 focus = device->keyboard_focus_resource;
345 data_device = find_resource(&device->drag_resource_list,
348 offer = wl_data_source_send_offer(device->selection_data_source,
350 wl_resource_post_event(data_device,
351 WL_DATA_DEVICE_SELECTION,
356 wl_list_for_each_safe(listener, next,
357 &device->selection_listener_list, link)
358 listener->func(listener, device);
360 device->selection_data_source_listener.func =
361 destroy_selection_data_source;
362 wl_list_insert(source->resource.destroy_listener_list.prev,
363 &device->selection_data_source_listener.link);
367 data_device_set_selection(struct wl_client *client,
368 struct wl_resource *resource,
369 struct wl_resource *source_resource, uint32_t time)
371 if (!source_resource)
374 wl_input_device_set_selection(resource->data,
375 source_resource->data, time);
378 static const struct wl_data_device_interface data_device_interface = {
379 data_device_start_drag,
380 data_device_set_selection,
384 destroy_data_source(struct wl_resource *resource)
386 struct wl_data_source *source =
387 container_of(resource, struct wl_data_source, resource);
390 end = source->mime_types.data + source->mime_types.size;
391 for (p = source->mime_types.data; p < end; p++)
394 wl_array_release(&source->mime_types);
396 source->resource.object.id = 0;
400 create_data_source(struct wl_client *client,
401 struct wl_resource *resource, uint32_t id)
403 struct wl_data_source *source;
405 source = malloc(sizeof *source);
406 if (source == NULL) {
407 wl_resource_post_no_memory(resource);
411 source->resource.destroy = destroy_data_source;
412 source->resource.object.id = id;
413 source->resource.object.interface = &wl_data_source_interface;
414 source->resource.object.implementation =
415 (void (**)(void)) &data_source_interface;
416 source->resource.data = source;
417 wl_list_init(&source->resource.destroy_listener_list);
419 source->offer_interface = &data_offer_interface;
420 source->cancel = data_source_cancel;
422 wl_array_init(&source->mime_types);
423 wl_client_add_resource(client, &source->resource);
426 static void unbind_data_device(struct wl_resource *resource)
428 wl_list_remove(&resource->link);
433 get_data_device(struct wl_client *client,
434 struct wl_resource *manager_resource,
435 uint32_t id, struct wl_resource *input_device)
437 struct wl_input_device *device = input_device->data;
438 struct wl_resource *resource;
441 wl_client_add_object(client, &wl_data_device_interface,
442 &data_device_interface, id, device);
444 wl_list_insert(&device->drag_resource_list, &resource->link);
445 resource->destroy = unbind_data_device;
448 static const struct wl_data_device_manager_interface manager_interface = {
454 bind_manager(struct wl_client *client,
455 void *data, uint32_t version, uint32_t id)
457 wl_client_add_object(client, &wl_data_device_manager_interface,
458 &manager_interface, id, NULL);
462 wl_data_device_set_keyboard_focus(struct wl_input_device *device)
464 struct wl_resource *data_device, *focus, *offer;
465 struct wl_data_source *source;
467 focus = device->keyboard_focus_resource;
471 data_device = find_resource(&device->drag_resource_list,
476 source = device->selection_data_source;
478 offer = wl_data_source_send_offer(source, data_device);
479 wl_resource_post_event(data_device,
480 WL_DATA_DEVICE_SELECTION, offer);
485 wl_data_device_manager_init(struct wl_display *display)
487 if (wl_display_add_global(display,
488 &wl_data_device_manager_interface,
489 NULL, bind_manager) == NULL)