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_data_source_send_target(&offer->source->resource,
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_data_source_send_send(&offer->source->resource,
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_data_source_send_cancelled(&source->resource);
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_data_device_send_data_offer(target, &offer->resource);
125 end = source->mime_types.data + source->mime_types.size;
126 for (p = source->mime_types.data; p < end; p++)
127 wl_data_offer_send_offer(&offer->resource, *p);
129 return &offer->resource;
133 data_source_offer(struct wl_client *client,
134 struct wl_resource *resource,
137 struct wl_data_source *source = resource->data;
140 p = wl_array_add(&source->mime_types, sizeof *p);
144 wl_resource_post_no_memory(resource);
148 data_source_destroy(struct wl_client *client, struct wl_resource *resource)
150 wl_resource_destroy(resource, 0);
153 static struct wl_data_source_interface data_source_interface = {
158 static struct wl_resource *
159 find_resource(struct wl_list *list, struct wl_client *client)
161 struct wl_resource *r;
163 wl_list_for_each(r, list, link) {
164 if (r->client == client)
172 destroy_drag_focus(struct wl_listener *listener,
173 struct wl_resource *resource, uint32_t time)
175 struct wl_input_device *device =
176 container_of(listener, struct wl_input_device,
177 drag_focus_listener);
179 device->drag_focus_resource = NULL;
183 drag_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
184 struct wl_surface *surface, int32_t x, int32_t y)
186 struct wl_input_device *device =
187 container_of(grab, struct wl_input_device, drag_grab);
188 struct wl_resource *resource, *offer;
190 if (device->drag_focus_resource) {
191 wl_data_device_send_leave(device->drag_focus_resource);
192 wl_list_remove(&device->drag_focus_listener.link);
193 device->drag_focus_resource = NULL;
194 device->drag_focus = NULL;
198 resource = find_resource(&device->drag_resource_list,
199 surface->resource.client);
200 if (surface && resource) {
201 offer = wl_data_source_send_offer(device->drag_data_source,
204 wl_data_device_send_enter(resource, time, surface,
207 device->drag_focus = surface;
208 device->drag_focus_listener.func = destroy_drag_focus;
209 wl_list_insert(resource->destroy_listener_list.prev,
210 &device->drag_focus_listener.link);
211 device->drag_focus_resource = resource;
212 grab->focus = surface;
217 drag_grab_motion(struct wl_pointer_grab *grab,
218 uint32_t time, int32_t x, int32_t y)
220 struct wl_input_device *device =
221 container_of(grab, struct wl_input_device, drag_grab);
223 if (device->drag_focus_resource)
224 wl_data_device_send_motion(device->drag_focus_resource,
229 drag_grab_button(struct wl_pointer_grab *grab,
230 uint32_t time, int32_t button, int32_t state)
232 struct wl_input_device *device =
233 container_of(grab, struct wl_input_device, drag_grab);
235 if (device->drag_focus_resource &&
236 device->grab_button == button && state == 0)
237 wl_data_device_send_drop(device->drag_focus_resource);
239 if (device->button_count == 0 && state == 0) {
240 wl_input_device_end_pointer_grab(device, time);
242 if (device->drag_surface) {
243 struct wl_resource *surface_resource =
244 &device->drag_surface->resource;
245 struct wl_surface_interface *implementation =
246 (struct wl_surface_interface *)
247 surface_resource->object.implementation;
249 implementation->attach(surface_resource->client,
250 surface_resource, NULL, 0, 0);
251 wl_list_remove(&device->drag_icon_listener.link);
254 device->drag_data_source = NULL;
255 device->drag_surface = NULL;
259 static const struct wl_pointer_grab_interface drag_grab_interface = {
266 destroy_data_device_icon(struct wl_listener *listener,
267 struct wl_resource *resource, uint32_t time)
269 struct wl_input_device *device;
271 device = container_of(listener, struct wl_input_device,
274 device->drag_surface = NULL;
278 data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
279 struct wl_resource *source_resource,
280 struct wl_resource *origin_resource,
281 struct wl_resource *icon_resource, uint32_t time)
283 struct wl_input_device *device = resource->data;
285 /* FIXME: Check that client has implicit grab on the origin
286 * surface that matches the given time. */
288 /* FIXME: Check that the data source type array isn't empty. */
290 device->drag_grab.interface = &drag_grab_interface;
291 device->drag_data_source = source_resource->data;
294 device->drag_surface = icon_resource->data;
295 device->drag_icon_listener.func = destroy_data_device_icon;
296 wl_list_insert(icon_resource->destroy_listener_list.prev,
297 &device->drag_icon_listener.link);
300 wl_input_device_start_pointer_grab(device, &device->drag_grab, time);
304 destroy_selection_data_source(struct wl_listener *listener,
305 struct wl_resource *resource, uint32_t time)
307 struct wl_input_device *device =
308 container_of(listener, struct wl_input_device,
309 selection_data_source_listener);
310 struct wl_resource *data_device, *focus;
312 device->selection_data_source = NULL;
314 focus = device->keyboard_focus_resource;
316 data_device = find_resource(&device->drag_resource_list,
318 wl_data_device_send_selection(data_device, NULL);
323 wl_input_device_set_selection(struct wl_input_device *device,
324 struct wl_data_source *source, uint32_t time)
326 struct wl_resource *data_device, *focus, *offer;
327 struct wl_selection_listener *listener, *next;
329 if (device->selection_data_source) {
330 device->selection_data_source->cancel(device->selection_data_source);
331 wl_list_remove(&device->selection_data_source_listener.link);
332 device->selection_data_source = NULL;
335 device->selection_data_source = source;
337 focus = device->keyboard_focus_resource;
339 data_device = find_resource(&device->drag_resource_list,
342 offer = wl_data_source_send_offer(device->selection_data_source,
344 wl_data_device_send_selection(data_device, offer);
348 wl_list_for_each_safe(listener, next,
349 &device->selection_listener_list, link)
350 listener->func(listener, device);
352 device->selection_data_source_listener.func =
353 destroy_selection_data_source;
354 wl_list_insert(source->resource.destroy_listener_list.prev,
355 &device->selection_data_source_listener.link);
359 data_device_set_selection(struct wl_client *client,
360 struct wl_resource *resource,
361 struct wl_resource *source_resource, uint32_t time)
363 if (!source_resource)
366 wl_input_device_set_selection(resource->data,
367 source_resource->data, time);
370 static const struct wl_data_device_interface data_device_interface = {
371 data_device_start_drag,
372 data_device_set_selection,
376 destroy_data_source(struct wl_resource *resource)
378 struct wl_data_source *source =
379 container_of(resource, struct wl_data_source, resource);
382 end = source->mime_types.data + source->mime_types.size;
383 for (p = source->mime_types.data; p < end; p++)
386 wl_array_release(&source->mime_types);
388 source->resource.object.id = 0;
392 create_data_source(struct wl_client *client,
393 struct wl_resource *resource, uint32_t id)
395 struct wl_data_source *source;
397 source = malloc(sizeof *source);
398 if (source == NULL) {
399 wl_resource_post_no_memory(resource);
403 source->resource.destroy = destroy_data_source;
404 source->resource.object.id = id;
405 source->resource.object.interface = &wl_data_source_interface;
406 source->resource.object.implementation =
407 (void (**)(void)) &data_source_interface;
408 source->resource.data = source;
409 wl_list_init(&source->resource.destroy_listener_list);
411 source->offer_interface = &data_offer_interface;
412 source->cancel = data_source_cancel;
414 wl_array_init(&source->mime_types);
415 wl_client_add_resource(client, &source->resource);
418 static void unbind_data_device(struct wl_resource *resource)
420 wl_list_remove(&resource->link);
425 get_data_device(struct wl_client *client,
426 struct wl_resource *manager_resource,
427 uint32_t id, struct wl_resource *input_device)
429 struct wl_input_device *device = input_device->data;
430 struct wl_resource *resource;
433 wl_client_add_object(client, &wl_data_device_interface,
434 &data_device_interface, id, device);
436 wl_list_insert(&device->drag_resource_list, &resource->link);
437 resource->destroy = unbind_data_device;
440 static const struct wl_data_device_manager_interface manager_interface = {
446 bind_manager(struct wl_client *client,
447 void *data, uint32_t version, uint32_t id)
449 wl_client_add_object(client, &wl_data_device_manager_interface,
450 &manager_interface, id, NULL);
454 wl_data_device_set_keyboard_focus(struct wl_input_device *device)
456 struct wl_resource *data_device, *focus, *offer;
457 struct wl_data_source *source;
459 focus = device->keyboard_focus_resource;
463 data_device = find_resource(&device->drag_resource_list,
468 source = device->selection_data_source;
470 offer = wl_data_source_send_offer(source, data_device);
471 wl_data_device_send_selection(data_device, offer);
476 wl_data_device_manager_init(struct wl_display *display)
478 if (wl_display_add_global(display,
479 &wl_data_device_manager_interface,
480 NULL, bind_manager) == NULL)