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 serial, 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 offer->source->accept(offer->source, serial, mime_type);
45 data_offer_receive(struct wl_client *client, struct wl_resource *resource,
46 const char *mime_type, int32_t fd)
48 struct wl_data_offer *offer = resource->data;
51 offer->source->send(offer->source, mime_type, fd);
57 data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
59 wl_resource_destroy(resource);
62 static const struct wl_data_offer_interface data_offer_interface = {
69 destroy_data_offer(struct wl_resource *resource)
71 struct wl_data_offer *offer = resource->data;
74 wl_list_remove(&offer->source_destroy_listener.link);
79 destroy_offer_data_source(struct wl_listener *listener, void *data)
81 struct wl_data_offer *offer;
83 offer = container_of(listener, struct wl_data_offer,
84 source_destroy_listener);
89 static struct wl_resource *
90 wl_data_source_send_offer(struct wl_data_source *source,
91 struct wl_resource *target)
93 struct wl_data_offer *offer;
96 offer = malloc(sizeof *offer);
100 offer->resource.destroy = destroy_data_offer;
101 offer->resource.object.id = 0;
102 offer->resource.object.interface = &wl_data_offer_interface;
103 offer->resource.object.implementation =
104 (void (**)(void)) &data_offer_interface;
105 offer->resource.data = offer;
106 wl_signal_init(&offer->resource.destroy_signal);
108 offer->source = source;
109 offer->source_destroy_listener.notify = destroy_offer_data_source;
110 wl_signal_add(&source->resource.destroy_signal,
111 &offer->source_destroy_listener);
113 wl_client_add_resource(target->client, &offer->resource);
115 wl_data_device_send_data_offer(target, &offer->resource);
117 wl_array_for_each(p, &source->mime_types)
118 wl_data_offer_send_offer(&offer->resource, *p);
120 return &offer->resource;
124 data_source_offer(struct wl_client *client,
125 struct wl_resource *resource,
128 struct wl_data_source *source = resource->data;
131 p = wl_array_add(&source->mime_types, sizeof *p);
135 wl_resource_post_no_memory(resource);
139 data_source_destroy(struct wl_client *client, struct wl_resource *resource)
141 wl_resource_destroy(resource);
144 static struct wl_data_source_interface data_source_interface = {
149 static struct wl_resource *
150 find_resource(struct wl_list *list, struct wl_client *client)
152 struct wl_resource *r;
154 wl_list_for_each(r, list, link) {
155 if (r->client == client)
163 destroy_drag_focus(struct wl_listener *listener, void *data)
165 struct wl_seat *seat =
166 container_of(listener, struct wl_seat, drag_focus_listener);
168 seat->drag_focus_resource = NULL;
172 drag_grab_focus(struct wl_pointer_grab *grab,
173 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
175 struct wl_seat *seat = container_of(grab, struct wl_seat, drag_grab);
176 struct wl_resource *resource, *offer = NULL;
177 struct wl_display *display;
180 if (seat->drag_focus_resource) {
181 wl_data_device_send_leave(seat->drag_focus_resource);
182 wl_list_remove(&seat->drag_focus_listener.link);
183 seat->drag_focus_resource = NULL;
184 seat->drag_focus = NULL;
190 if (!seat->drag_data_source &&
191 surface->resource.client != seat->drag_client)
194 resource = find_resource(&seat->drag_resource_list,
195 surface->resource.client);
199 display = wl_client_get_display(resource->client);
200 serial = wl_display_next_serial(display);
202 if (seat->drag_data_source)
203 offer = wl_data_source_send_offer(seat->drag_data_source,
206 wl_data_device_send_enter(resource, serial, &surface->resource,
209 seat->drag_focus = surface;
210 seat->drag_focus_listener.notify = destroy_drag_focus;
211 wl_signal_add(&resource->destroy_signal,
212 &seat->drag_focus_listener);
213 seat->drag_focus_resource = resource;
214 grab->focus = surface;
218 drag_grab_motion(struct wl_pointer_grab *grab,
219 uint32_t time, wl_fixed_t x, wl_fixed_t y)
221 struct wl_seat *seat = container_of(grab, struct wl_seat, drag_grab);
223 if (seat->drag_focus_resource)
224 wl_data_device_send_motion(seat->drag_focus_resource,
229 data_device_end_drag_grab(struct wl_seat *seat)
231 if (seat->drag_surface) {
232 seat->drag_surface = NULL;
233 wl_signal_emit(&seat->drag_icon_signal, NULL);
234 wl_list_remove(&seat->drag_icon_listener.link);
237 drag_grab_focus(&seat->drag_grab, NULL,
238 wl_fixed_from_int(0), wl_fixed_from_int(0));
240 wl_pointer_end_grab(seat->pointer);
242 seat->drag_data_source = NULL;
243 seat->drag_client = NULL;
247 drag_grab_button(struct wl_pointer_grab *grab,
248 uint32_t time, uint32_t button, uint32_t state_w)
250 struct wl_seat *seat = container_of(grab, struct wl_seat, drag_grab);
251 enum wl_pointer_button_state state = state_w;
253 if (seat->drag_focus_resource &&
254 seat->pointer->grab_button == button &&
255 state == WL_POINTER_BUTTON_STATE_RELEASED)
256 wl_data_device_send_drop(seat->drag_focus_resource);
258 if (seat->pointer->button_count == 0 &&
259 state == WL_POINTER_BUTTON_STATE_RELEASED) {
260 if (seat->drag_data_source)
261 wl_list_remove(&seat->drag_data_source_listener.link);
262 data_device_end_drag_grab(seat);
266 static const struct wl_pointer_grab_interface drag_grab_interface = {
273 destroy_data_device_source(struct wl_listener *listener, void *data)
275 struct wl_seat *seat = container_of(listener, struct wl_seat,
276 drag_data_source_listener);
278 data_device_end_drag_grab(seat);
282 destroy_data_device_icon(struct wl_listener *listener, void *data)
284 struct wl_seat *seat = container_of(listener, struct wl_seat,
287 seat->drag_surface = NULL;
291 data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
292 struct wl_resource *source_resource,
293 struct wl_resource *origin_resource,
294 struct wl_resource *icon_resource, uint32_t serial)
296 struct wl_seat *seat = resource->data;
298 /* FIXME: Check that client has implicit grab on the origin
299 * surface that matches the given time. */
301 /* FIXME: Check that the data source type array isn't empty. */
303 seat->drag_grab.interface = &drag_grab_interface;
305 seat->drag_client = client;
306 seat->drag_data_source = NULL;
308 if (source_resource) {
309 seat->drag_data_source = source_resource->data;
310 seat->drag_data_source_listener.notify =
311 destroy_data_device_source;
312 wl_signal_add(&source_resource->destroy_signal,
313 &seat->drag_data_source_listener);
317 seat->drag_surface = icon_resource->data;
318 seat->drag_icon_listener.notify = destroy_data_device_icon;
319 wl_signal_add(&icon_resource->destroy_signal,
320 &seat->drag_icon_listener);
321 wl_signal_emit(&seat->drag_icon_signal, icon_resource);
324 wl_pointer_set_focus(seat->pointer, NULL,
325 wl_fixed_from_int(0), wl_fixed_from_int(0));
326 wl_pointer_start_grab(seat->pointer, &seat->drag_grab);
330 destroy_selection_data_source(struct wl_listener *listener, void *data)
332 struct wl_seat *seat = container_of(listener, struct wl_seat,
333 selection_data_source_listener);
334 struct wl_resource *data_device;
335 struct wl_resource *focus = NULL;
337 seat->selection_data_source = NULL;
340 focus = seat->keyboard->focus_resource;
342 data_device = find_resource(&seat->drag_resource_list,
345 wl_data_device_send_selection(data_device, NULL);
348 wl_signal_emit(&seat->selection_signal, seat);
352 wl_seat_set_selection(struct wl_seat *seat, struct wl_data_source *source,
355 struct wl_resource *data_device, *offer;
356 struct wl_resource *focus = NULL;
358 if (seat->selection_data_source &&
359 seat->selection_serial - serial < UINT32_MAX / 2)
362 if (seat->selection_data_source) {
363 seat->selection_data_source->cancel(seat->selection_data_source);
364 wl_list_remove(&seat->selection_data_source_listener.link);
365 seat->selection_data_source = NULL;
368 seat->selection_data_source = source;
369 seat->selection_serial = serial;
372 focus = seat->keyboard->focus_resource;
374 data_device = find_resource(&seat->drag_resource_list,
376 if (data_device && source) {
377 offer = wl_data_source_send_offer(seat->selection_data_source,
379 wl_data_device_send_selection(data_device, offer);
380 } else if (data_device) {
381 wl_data_device_send_selection(data_device, NULL);
385 wl_signal_emit(&seat->selection_signal, seat);
388 seat->selection_data_source_listener.notify =
389 destroy_selection_data_source;
390 wl_signal_add(&source->resource.destroy_signal,
391 &seat->selection_data_source_listener);
396 data_device_set_selection(struct wl_client *client,
397 struct wl_resource *resource,
398 struct wl_resource *source_resource, uint32_t serial)
400 if (!source_resource)
403 /* FIXME: Store serial and check against incoming serial here. */
404 wl_seat_set_selection(resource->data, source_resource->data,
408 static const struct wl_data_device_interface data_device_interface = {
409 data_device_start_drag,
410 data_device_set_selection,
414 destroy_data_source(struct wl_resource *resource)
416 struct wl_data_source *source =
417 container_of(resource, struct wl_data_source, resource);
420 wl_array_for_each(p, &source->mime_types)
423 wl_array_release(&source->mime_types);
425 source->resource.object.id = 0;
429 client_source_accept(struct wl_data_source *source,
430 uint32_t time, const char *mime_type)
432 wl_data_source_send_target(&source->resource, mime_type);
436 client_source_send(struct wl_data_source *source,
437 const char *mime_type, int32_t fd)
439 wl_data_source_send_send(&source->resource, mime_type, fd);
444 client_source_cancel(struct wl_data_source *source)
446 wl_data_source_send_cancelled(&source->resource);
450 create_data_source(struct wl_client *client,
451 struct wl_resource *resource, uint32_t id)
453 struct wl_data_source *source;
455 source = malloc(sizeof *source);
456 if (source == NULL) {
457 wl_resource_post_no_memory(resource);
461 source->resource.destroy = destroy_data_source;
462 source->resource.object.id = id;
463 source->resource.object.interface = &wl_data_source_interface;
464 source->resource.object.implementation =
465 (void (**)(void)) &data_source_interface;
466 source->resource.data = source;
467 wl_signal_init(&source->resource.destroy_signal);
469 source->accept = client_source_accept;
470 source->send = client_source_send;
471 source->cancel = client_source_cancel;
473 wl_array_init(&source->mime_types);
474 wl_client_add_resource(client, &source->resource);
477 static void unbind_data_device(struct wl_resource *resource)
479 wl_list_remove(&resource->link);
484 get_data_device(struct wl_client *client,
485 struct wl_resource *manager_resource,
486 uint32_t id, struct wl_resource *seat_resource)
488 struct wl_seat *seat = seat_resource->data;
489 struct wl_resource *resource;
491 resource = wl_client_add_object(client, &wl_data_device_interface,
492 &data_device_interface, id,
495 wl_list_insert(&seat->drag_resource_list, &resource->link);
496 resource->destroy = unbind_data_device;
499 static const struct wl_data_device_manager_interface manager_interface = {
505 bind_manager(struct wl_client *client,
506 void *data, uint32_t version, uint32_t id)
508 wl_client_add_object(client, &wl_data_device_manager_interface,
509 &manager_interface, id, NULL);
513 wl_data_device_set_keyboard_focus(struct wl_seat *seat)
515 struct wl_resource *data_device, *focus, *offer;
516 struct wl_data_source *source;
521 focus = seat->keyboard->focus_resource;
525 data_device = find_resource(&seat->drag_resource_list,
530 source = seat->selection_data_source;
532 offer = wl_data_source_send_offer(source, data_device);
533 wl_data_device_send_selection(data_device, offer);
538 wl_data_device_manager_init(struct wl_display *display)
540 if (wl_display_add_global(display,
541 &wl_data_device_manager_interface,
542 NULL, bind_manager) == NULL)