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
31 #include "compositor.h"
34 struct wl_client *client;
35 struct weston_data_source *data_source;
36 struct wl_listener data_source_listener;
37 struct weston_view *focus;
38 struct wl_resource *focus_resource;
39 struct wl_listener focus_listener;
40 struct weston_view *icon;
41 struct wl_listener icon_destroy_listener;
45 struct weston_pointer_drag {
46 struct weston_drag base;
47 struct weston_pointer_grab grab;
50 struct weston_touch_drag {
51 struct weston_drag base;
52 struct weston_touch_grab grab;
56 data_offer_accept(struct wl_client *client, struct wl_resource *resource,
57 uint32_t serial, const char *mime_type)
59 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
61 /* FIXME: Check that client is currently focused by the input
62 * device that is currently dragging this data source. Should
63 * this be a wl_data_device request? */
66 offer->source->accept(offer->source, serial, mime_type);
70 data_offer_receive(struct wl_client *client, struct wl_resource *resource,
71 const char *mime_type, int32_t fd)
73 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
76 offer->source->send(offer->source, mime_type, fd);
82 data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
84 wl_resource_destroy(resource);
87 static const struct wl_data_offer_interface data_offer_interface = {
94 destroy_data_offer(struct wl_resource *resource)
96 struct weston_data_offer *offer = wl_resource_get_user_data(resource);
99 wl_list_remove(&offer->source_destroy_listener.link);
104 destroy_offer_data_source(struct wl_listener *listener, void *data)
106 struct weston_data_offer *offer;
108 offer = container_of(listener, struct weston_data_offer,
109 source_destroy_listener);
111 offer->source = NULL;
114 static struct wl_resource *
115 weston_data_source_send_offer(struct weston_data_source *source,
116 struct wl_resource *target)
118 struct weston_data_offer *offer;
121 offer = malloc(sizeof *offer);
126 wl_resource_create(wl_resource_get_client(target),
127 &wl_data_offer_interface, 1, 0);
128 if (offer->resource == NULL) {
133 wl_resource_set_implementation(offer->resource, &data_offer_interface,
134 offer, destroy_data_offer);
136 offer->source = source;
137 offer->source_destroy_listener.notify = destroy_offer_data_source;
138 wl_signal_add(&source->destroy_signal,
139 &offer->source_destroy_listener);
141 wl_data_device_send_data_offer(target, offer->resource);
143 wl_array_for_each(p, &source->mime_types)
144 wl_data_offer_send_offer(offer->resource, *p);
146 return offer->resource;
150 data_source_offer(struct wl_client *client,
151 struct wl_resource *resource,
154 struct weston_data_source *source =
155 wl_resource_get_user_data(resource);
158 p = wl_array_add(&source->mime_types, sizeof *p);
162 wl_resource_post_no_memory(resource);
166 data_source_destroy(struct wl_client *client, struct wl_resource *resource)
168 wl_resource_destroy(resource);
171 static struct wl_data_source_interface data_source_interface = {
177 drag_surface_configure(struct weston_drag *drag,
178 struct weston_pointer *pointer,
179 struct weston_touch *touch,
180 struct weston_surface *es,
181 int32_t sx, int32_t sy)
183 struct weston_layer_entry *list;
186 assert((pointer != NULL && touch == NULL) ||
187 (pointer == NULL && touch != NULL));
189 if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
190 if (pointer && pointer->sprite &&
191 weston_view_is_mapped(pointer->sprite))
192 list = &pointer->sprite->layer_link;
194 list = &es->compositor->cursor_layer.view_list;
196 weston_layer_entry_remove(&drag->icon->layer_link);
197 weston_layer_entry_insert(list, &drag->icon->layer_link);
198 weston_view_update_transform(drag->icon);
199 pixman_region32_clear(&es->pending.input);
205 /* init to 0 for avoiding a compile warning */
208 fx = wl_fixed_to_double(pointer->x) + drag->dx;
209 fy = wl_fixed_to_double(pointer->y) + drag->dy;
211 fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
212 fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
214 weston_view_set_position(drag->icon, fx, fy);
218 pointer_drag_surface_configure(struct weston_surface *es,
219 int32_t sx, int32_t sy)
221 struct weston_pointer_drag *drag = es->configure_private;
222 struct weston_pointer *pointer = drag->grab.pointer;
224 assert(es->configure == pointer_drag_surface_configure);
226 drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
230 touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
232 struct weston_touch_drag *drag = es->configure_private;
233 struct weston_touch *touch = drag->grab.touch;
235 assert(es->configure == touch_drag_surface_configure);
237 drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
241 destroy_drag_focus(struct wl_listener *listener, void *data)
243 struct weston_drag *drag =
244 container_of(listener, struct weston_drag, focus_listener);
246 drag->focus_resource = NULL;
250 weston_drag_set_focus(struct weston_drag *drag,
251 struct weston_seat *seat,
252 struct weston_view *view,
253 wl_fixed_t sx, wl_fixed_t sy)
255 struct wl_resource *resource, *offer = NULL;
256 struct wl_display *display = seat->compositor->wl_display;
259 if (drag->focus && view && drag->focus->surface == view->surface) {
264 if (drag->focus_resource) {
265 wl_data_device_send_leave(drag->focus_resource);
266 wl_list_remove(&drag->focus_listener.link);
267 drag->focus_resource = NULL;
271 if (!view || !view->surface->resource)
274 if (!drag->data_source &&
275 wl_resource_get_client(view->surface->resource) != drag->client)
278 resource = wl_resource_find_for_client(&seat->drag_resource_list,
279 wl_resource_get_client(view->surface->resource));
283 serial = wl_display_next_serial(display);
285 if (drag->data_source) {
286 offer = weston_data_source_send_offer(drag->data_source,
292 wl_data_device_send_enter(resource, serial, view->surface->resource,
296 drag->focus_listener.notify = destroy_drag_focus;
297 wl_resource_add_destroy_listener(resource, &drag->focus_listener);
298 drag->focus_resource = resource;
302 drag_grab_focus(struct weston_pointer_grab *grab)
304 struct weston_pointer_drag *drag =
305 container_of(grab, struct weston_pointer_drag, grab);
306 struct weston_pointer *pointer = grab->pointer;
307 struct weston_view *view;
310 view = weston_compositor_pick_view(pointer->seat->compositor,
311 pointer->x, pointer->y,
313 if (drag->base.focus != view)
314 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
318 drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
319 wl_fixed_t x, wl_fixed_t y)
321 struct weston_pointer_drag *drag =
322 container_of(grab, struct weston_pointer_drag, grab);
323 struct weston_pointer *pointer = drag->grab.pointer;
327 weston_pointer_move(pointer, x, y);
329 if (drag->base.icon) {
330 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
331 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
332 weston_view_set_position(drag->base.icon, fx, fy);
333 weston_view_schedule_repaint(drag->base.icon);
336 if (drag->base.focus_resource) {
337 weston_view_from_global_fixed(drag->base.focus,
338 pointer->x, pointer->y,
341 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
346 data_device_end_drag_grab(struct weston_drag *drag,
347 struct weston_seat *seat)
350 if (weston_view_is_mapped(drag->icon))
351 weston_view_unmap(drag->icon);
353 drag->icon->surface->configure = NULL;
354 pixman_region32_clear(&drag->icon->surface->pending.input);
355 wl_list_remove(&drag->icon_destroy_listener.link);
356 weston_view_destroy(drag->icon);
359 weston_drag_set_focus(drag, seat, NULL, 0, 0);
363 data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
365 struct weston_pointer *pointer = drag->grab.pointer;
367 data_device_end_drag_grab(&drag->base, pointer->seat);
368 weston_pointer_end_grab(pointer);
373 drag_grab_button(struct weston_pointer_grab *grab,
374 uint32_t time, uint32_t button, uint32_t state_w)
376 struct weston_pointer_drag *drag =
377 container_of(grab, struct weston_pointer_drag, grab);
378 struct weston_pointer *pointer = drag->grab.pointer;
379 enum wl_pointer_button_state state = state_w;
381 if (drag->base.focus_resource &&
382 pointer->grab_button == button &&
383 state == WL_POINTER_BUTTON_STATE_RELEASED)
384 wl_data_device_send_drop(drag->base.focus_resource);
386 if (pointer->button_count == 0 &&
387 state == WL_POINTER_BUTTON_STATE_RELEASED) {
388 if (drag->base.data_source)
389 wl_list_remove(&drag->base.data_source_listener.link);
390 data_device_end_pointer_drag_grab(drag);
395 drag_grab_cancel(struct weston_pointer_grab *grab)
397 struct weston_pointer_drag *drag =
398 container_of(grab, struct weston_pointer_drag, grab);
400 if (drag->base.data_source)
401 wl_list_remove(&drag->base.data_source_listener.link);
403 data_device_end_pointer_drag_grab(drag);
406 static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
414 drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
415 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
420 data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
422 struct weston_touch *touch = drag->grab.touch;
424 data_device_end_drag_grab(&drag->base, touch->seat);
425 weston_touch_end_grab(touch);
430 drag_grab_touch_up(struct weston_touch_grab *grab,
431 uint32_t time, int touch_id)
433 struct weston_touch_drag *touch_drag =
434 container_of(grab, struct weston_touch_drag, grab);
435 struct weston_touch *touch = grab->touch;
437 if (touch_id != touch->grab_touch_id)
440 if (touch_drag->base.focus_resource)
441 wl_data_device_send_drop(touch_drag->base.focus_resource);
442 if (touch_drag->base.data_source)
443 wl_list_remove(&touch_drag->base.data_source_listener.link);
444 data_device_end_touch_drag_grab(touch_drag);
448 drag_grab_touch_focus(struct weston_touch_drag *drag)
450 struct weston_touch *touch = drag->grab.touch;
451 struct weston_view *view;
452 wl_fixed_t view_x, view_y;
454 view = weston_compositor_pick_view(touch->seat->compositor,
455 touch->grab_x, touch->grab_y,
457 if (drag->base.focus != view)
458 weston_drag_set_focus(&drag->base, touch->seat,
459 view, view_x, view_y);
463 drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
464 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
466 struct weston_touch_drag *touch_drag =
467 container_of(grab, struct weston_touch_drag, grab);
468 struct weston_touch *touch = grab->touch;
469 wl_fixed_t view_x, view_y;
472 if (touch_id != touch->grab_touch_id)
475 drag_grab_touch_focus(touch_drag);
476 if (touch_drag->base.icon) {
477 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
478 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
479 weston_view_set_position(touch_drag->base.icon, fx, fy);
480 weston_view_schedule_repaint(touch_drag->base.icon);
483 if (touch_drag->base.focus_resource) {
484 weston_view_from_global_fixed(touch_drag->base.focus,
485 touch->grab_x, touch->grab_y,
487 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
493 drag_grab_touch_frame(struct weston_touch_grab *grab)
498 drag_grab_touch_cancel(struct weston_touch_grab *grab)
500 struct weston_touch_drag *touch_drag =
501 container_of(grab, struct weston_touch_drag, grab);
503 if (touch_drag->base.data_source)
504 wl_list_remove(&touch_drag->base.data_source_listener.link);
505 data_device_end_touch_drag_grab(touch_drag);
508 static const struct weston_touch_grab_interface touch_drag_grab_interface = {
509 drag_grab_touch_down,
511 drag_grab_touch_motion,
512 drag_grab_touch_frame,
513 drag_grab_touch_cancel
517 destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
519 struct weston_pointer_drag *drag = container_of(listener,
520 struct weston_pointer_drag, base.data_source_listener);
522 data_device_end_pointer_drag_grab(drag);
526 handle_drag_icon_destroy(struct wl_listener *listener, void *data)
528 struct weston_drag *drag = container_of(listener, struct weston_drag,
529 icon_destroy_listener);
535 weston_pointer_start_drag(struct weston_pointer *pointer,
536 struct weston_data_source *source,
537 struct weston_surface *icon,
538 struct wl_client *client)
540 struct weston_pointer_drag *drag;
542 drag = zalloc(sizeof *drag);
546 drag->grab.interface = &pointer_drag_grab_interface;
547 drag->base.client = client;
548 drag->base.data_source = source;
551 drag->base.icon = weston_view_create(icon);
552 if (drag->base.icon == NULL) {
557 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
558 wl_signal_add(&icon->destroy_signal,
559 &drag->base.icon_destroy_listener);
561 icon->configure = pointer_drag_surface_configure;
562 icon->configure_private = drag;
564 drag->base.icon = NULL;
568 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
569 wl_signal_add(&source->destroy_signal,
570 &drag->base.data_source_listener);
573 weston_pointer_set_focus(pointer, NULL,
574 wl_fixed_from_int(0), wl_fixed_from_int(0));
575 weston_pointer_start_grab(pointer, &drag->grab);
581 destroy_touch_data_device_source(struct wl_listener *listener, void *data)
583 struct weston_touch_drag *drag = container_of(listener,
584 struct weston_touch_drag, base.data_source_listener);
586 data_device_end_touch_drag_grab(drag);
590 weston_touch_start_drag(struct weston_touch *touch,
591 struct weston_data_source *source,
592 struct weston_surface *icon,
593 struct wl_client *client)
595 struct weston_touch_drag *drag;
597 drag = zalloc(sizeof *drag);
601 drag->grab.interface = &touch_drag_grab_interface;
602 drag->base.client = client;
603 drag->base.data_source = source;
606 drag->base.icon = weston_view_create(icon);
607 if (drag->base.icon == NULL) {
612 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
613 wl_signal_add(&icon->destroy_signal,
614 &drag->base.icon_destroy_listener);
616 icon->configure = touch_drag_surface_configure;
617 icon->configure_private = drag;
619 drag->base.icon = NULL;
623 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
624 wl_signal_add(&source->destroy_signal,
625 &drag->base.data_source_listener);
628 weston_touch_start_grab(touch, &drag->grab);
630 drag_grab_touch_focus(drag);
636 data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
637 struct wl_resource *source_resource,
638 struct wl_resource *origin_resource,
639 struct wl_resource *icon_resource, uint32_t serial)
641 struct weston_seat *seat = wl_resource_get_user_data(resource);
642 struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
643 struct weston_data_source *source = NULL;
644 struct weston_surface *icon = NULL;
645 int is_pointer_grab, is_touch_grab;
648 is_pointer_grab = seat->pointer &&
649 seat->pointer->button_count == 1 &&
650 seat->pointer->grab_serial == serial &&
651 seat->pointer->focus &&
652 seat->pointer->focus->surface == origin;
654 is_touch_grab = seat->touch &&
655 seat->touch->num_tp == 1 &&
656 seat->touch->grab_serial == serial &&
657 seat->touch->focus &&
658 seat->touch->focus->surface == origin;
660 if (!is_pointer_grab && !is_touch_grab)
663 /* FIXME: Check that the data source type array isn't empty. */
666 source = wl_resource_get_user_data(source_resource);
668 icon = wl_resource_get_user_data(icon_resource);
669 if (icon && icon->configure) {
670 wl_resource_post_error(icon_resource,
671 WL_DISPLAY_ERROR_INVALID_OBJECT,
672 "surface->configure already set");
677 ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
678 else if (is_touch_grab)
679 ret = weston_touch_start_drag(seat->touch, source, icon, client);
682 wl_resource_post_no_memory(resource);
686 destroy_selection_data_source(struct wl_listener *listener, void *data)
688 struct weston_seat *seat = container_of(listener, struct weston_seat,
689 selection_data_source_listener);
690 struct wl_resource *data_device;
691 struct weston_surface *focus = NULL;
693 seat->selection_data_source = NULL;
696 focus = seat->keyboard->focus;
697 if (focus && focus->resource) {
698 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
699 wl_resource_get_client(focus->resource));
701 wl_data_device_send_selection(data_device, NULL);
704 wl_signal_emit(&seat->selection_signal, seat);
708 weston_seat_set_selection(struct weston_seat *seat,
709 struct weston_data_source *source, uint32_t serial)
711 struct wl_resource *data_device, *offer;
712 struct weston_surface *focus = NULL;
714 if (seat->selection_data_source &&
715 seat->selection_serial - serial < UINT32_MAX / 2)
718 if (seat->selection_data_source) {
719 seat->selection_data_source->cancel(seat->selection_data_source);
720 wl_list_remove(&seat->selection_data_source_listener.link);
721 seat->selection_data_source = NULL;
724 seat->selection_data_source = source;
725 seat->selection_serial = serial;
728 focus = seat->keyboard->focus;
729 if (focus && focus->resource) {
730 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
731 wl_resource_get_client(focus->resource));
732 if (data_device && source) {
733 offer = weston_data_source_send_offer(seat->selection_data_source,
735 wl_data_device_send_selection(data_device, offer);
736 } else if (data_device) {
737 wl_data_device_send_selection(data_device, NULL);
741 wl_signal_emit(&seat->selection_signal, seat);
744 seat->selection_data_source_listener.notify =
745 destroy_selection_data_source;
746 wl_signal_add(&source->destroy_signal,
747 &seat->selection_data_source_listener);
752 data_device_set_selection(struct wl_client *client,
753 struct wl_resource *resource,
754 struct wl_resource *source_resource, uint32_t serial)
756 if (!source_resource)
759 /* FIXME: Store serial and check against incoming serial here. */
760 weston_seat_set_selection(wl_resource_get_user_data(resource),
761 wl_resource_get_user_data(source_resource),
765 static const struct wl_data_device_interface data_device_interface = {
766 data_device_start_drag,
767 data_device_set_selection,
771 destroy_data_source(struct wl_resource *resource)
773 struct weston_data_source *source =
774 wl_resource_get_user_data(resource);
777 wl_signal_emit(&source->destroy_signal, source);
779 wl_array_for_each(p, &source->mime_types)
782 wl_array_release(&source->mime_types);
788 client_source_accept(struct weston_data_source *source,
789 uint32_t time, const char *mime_type)
791 wl_data_source_send_target(source->resource, mime_type);
795 client_source_send(struct weston_data_source *source,
796 const char *mime_type, int32_t fd)
798 wl_data_source_send_send(source->resource, mime_type, fd);
803 client_source_cancel(struct weston_data_source *source)
805 wl_data_source_send_cancelled(source->resource);
809 create_data_source(struct wl_client *client,
810 struct wl_resource *resource, uint32_t id)
812 struct weston_data_source *source;
814 source = malloc(sizeof *source);
815 if (source == NULL) {
816 wl_resource_post_no_memory(resource);
820 wl_signal_init(&source->destroy_signal);
821 source->accept = client_source_accept;
822 source->send = client_source_send;
823 source->cancel = client_source_cancel;
825 wl_array_init(&source->mime_types);
828 wl_resource_create(client, &wl_data_source_interface, 1, id);
829 wl_resource_set_implementation(source->resource, &data_source_interface,
830 source, destroy_data_source);
833 static void unbind_data_device(struct wl_resource *resource)
835 wl_list_remove(wl_resource_get_link(resource));
839 get_data_device(struct wl_client *client,
840 struct wl_resource *manager_resource,
841 uint32_t id, struct wl_resource *seat_resource)
843 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
844 struct wl_resource *resource;
846 resource = wl_resource_create(client,
847 &wl_data_device_interface, 1, id);
848 if (resource == NULL) {
849 wl_resource_post_no_memory(manager_resource);
853 wl_list_insert(&seat->drag_resource_list,
854 wl_resource_get_link(resource));
855 wl_resource_set_implementation(resource, &data_device_interface,
856 seat, unbind_data_device);
859 static const struct wl_data_device_manager_interface manager_interface = {
865 bind_manager(struct wl_client *client,
866 void *data, uint32_t version, uint32_t id)
868 struct wl_resource *resource;
871 wl_resource_create(client,
872 &wl_data_device_manager_interface, 1, id);
873 if (resource == NULL) {
874 wl_client_post_no_memory(client);
878 wl_resource_set_implementation(resource, &manager_interface,
883 wl_data_device_set_keyboard_focus(struct weston_seat *seat)
885 struct wl_resource *data_device, *offer;
886 struct weston_data_source *source;
887 struct weston_surface *focus;
892 focus = seat->keyboard->focus;
893 if (!focus || !focus->resource)
896 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
897 wl_resource_get_client(focus->resource));
901 source = seat->selection_data_source;
903 offer = weston_data_source_send_offer(source, data_device);
904 wl_data_device_send_selection(data_device, offer);
909 wl_data_device_manager_init(struct wl_display *display)
911 if (wl_global_create(display,
912 &wl_data_device_manager_interface, 1,
913 NULL, bind_manager) == NULL)