diff --git a/configure.ac b/configure.ac index c535b28f..239fb5b6 100644 --- a/configure.ac +++ b/configure.ac @@ -43,7 +43,7 @@ AC_CHECK_HEADERS([execinfo.h]) AC_CHECK_FUNCS([mkostemp strchrnul]) -COMPOSITOR_MODULES="wayland-server xkbcommon pixman-1" +COMPOSITOR_MODULES="wayland-server >= 1.1.90 xkbcommon pixman-1" AC_ARG_ENABLE(egl, [ --disable-egl],, enable_egl=yes) diff --git a/src/Makefile.am b/src/Makefile.am index d33ebc5d..ba08fe8e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,6 +18,8 @@ weston_SOURCES = \ log.c \ compositor.c \ compositor.h \ + input.c \ + data-device.c \ filter.c \ filter.h \ screenshooter.c \ diff --git a/src/compositor.h b/src/compositor.h index 93a82b2e..36aa44c9 100644 --- a/src/compositor.h +++ b/src/compositor.h @@ -208,6 +208,238 @@ struct weston_output { uint16_t *b); }; +struct wl_pointer_grab; +struct wl_pointer_grab_interface { + void (*focus)(struct wl_pointer_grab *grab, + struct wl_surface *surface, + wl_fixed_t x, + wl_fixed_t y); + void (*motion)(struct wl_pointer_grab *grab, + uint32_t time, + wl_fixed_t x, + wl_fixed_t y); + void (*button)(struct wl_pointer_grab *grab, + uint32_t time, uint32_t button, uint32_t state); +}; + +struct wl_pointer_grab { + const struct wl_pointer_grab_interface *interface; + struct wl_pointer *pointer; + struct wl_surface *focus; + wl_fixed_t x, y; +}; + +struct wl_keyboard_grab; +struct wl_keyboard_grab_interface { + void (*key)(struct wl_keyboard_grab *grab, uint32_t time, + uint32_t key, uint32_t state); + void (*modifiers)(struct wl_keyboard_grab *grab, uint32_t serial, + uint32_t mods_depressed, uint32_t mods_latched, + uint32_t mods_locked, uint32_t group); +}; + +struct wl_keyboard_grab { + const struct wl_keyboard_grab_interface *interface; + struct wl_keyboard *keyboard; + struct wl_surface *focus; + uint32_t key; +}; + +struct wl_touch_grab; +struct wl_touch_grab_interface { + void (*down)(struct wl_touch_grab *grab, + uint32_t time, + int touch_id, + wl_fixed_t sx, + wl_fixed_t sy); + void (*up)(struct wl_touch_grab *grab, + uint32_t time, + int touch_id); + void (*motion)(struct wl_touch_grab *grab, + uint32_t time, + int touch_id, + wl_fixed_t sx, + wl_fixed_t sy); +}; + +struct wl_touch_grab { + const struct wl_touch_grab_interface *interface; + struct wl_touch *touch; + struct wl_surface *focus; +}; + +struct wl_data_offer { + struct wl_resource resource; + struct wl_data_source *source; + struct wl_listener source_destroy_listener; +}; + +struct wl_data_source { + struct wl_resource resource; + struct wl_array mime_types; + + void (*accept)(struct wl_data_source *source, + uint32_t serial, const char *mime_type); + void (*send)(struct wl_data_source *source, + const char *mime_type, int32_t fd); + void (*cancel)(struct wl_data_source *source); +}; + +struct wl_pointer { + struct wl_seat *seat; + + struct wl_list resource_list; + struct wl_surface *focus; + struct wl_resource *focus_resource; + struct wl_listener focus_listener; + uint32_t focus_serial; + struct wl_signal focus_signal; + + struct wl_pointer_grab *grab; + struct wl_pointer_grab default_grab; + wl_fixed_t grab_x, grab_y; + uint32_t grab_button; + uint32_t grab_serial; + uint32_t grab_time; + + wl_fixed_t x, y; + struct wl_surface *current; + struct wl_listener current_listener; + wl_fixed_t current_x, current_y; + + uint32_t button_count; +}; + +struct wl_keyboard { + struct wl_seat *seat; + + struct wl_list resource_list; + struct wl_surface *focus; + struct wl_resource *focus_resource; + struct wl_listener focus_listener; + uint32_t focus_serial; + struct wl_signal focus_signal; + + struct wl_keyboard_grab *grab; + struct wl_keyboard_grab default_grab; + uint32_t grab_key; + uint32_t grab_serial; + uint32_t grab_time; + + struct wl_array keys; + + struct { + uint32_t mods_depressed; + uint32_t mods_latched; + uint32_t mods_locked; + uint32_t group; + } modifiers; +}; + +struct wl_touch { + struct wl_seat *seat; + + struct wl_list resource_list; + struct wl_surface *focus; + struct wl_resource *focus_resource; + struct wl_listener focus_listener; + uint32_t focus_serial; + struct wl_signal focus_signal; + + struct wl_touch_grab *grab; + struct wl_touch_grab default_grab; + wl_fixed_t grab_x, grab_y; + uint32_t grab_serial; + uint32_t grab_time; +}; + +struct wl_seat { + struct wl_list base_resource_list; + struct wl_signal destroy_signal; + + struct wl_pointer *pointer; + struct wl_keyboard *keyboard; + struct wl_touch *touch; + + uint32_t selection_serial; + struct wl_data_source *selection_data_source; + struct wl_listener selection_data_source_listener; + struct wl_signal selection_signal; + + struct wl_list drag_resource_list; + struct wl_client *drag_client; + struct wl_data_source *drag_data_source; + struct wl_listener drag_data_source_listener; + struct wl_surface *drag_focus; + struct wl_resource *drag_focus_resource; + struct wl_listener drag_focus_listener; + struct wl_pointer_grab drag_grab; + struct wl_surface *drag_surface; + struct wl_listener drag_icon_listener; + struct wl_signal drag_icon_signal; +}; + +void +wl_seat_init(struct wl_seat *seat); + +void +wl_seat_release(struct wl_seat *seat); + +void +wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer); +void +wl_seat_set_keyboard(struct wl_seat *seat, struct wl_keyboard *keyboard); +void +wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch); + +void +wl_pointer_init(struct wl_pointer *pointer); +void +wl_pointer_release(struct wl_pointer *pointer); +void +wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface, + wl_fixed_t sx, wl_fixed_t sy); +void +wl_pointer_start_grab(struct wl_pointer *pointer, + struct wl_pointer_grab *grab); +void +wl_pointer_end_grab(struct wl_pointer *pointer); +void +wl_pointer_set_current(struct wl_pointer *pointer, struct wl_surface *surface); + +void +wl_keyboard_init(struct wl_keyboard *keyboard); +void +wl_keyboard_release(struct wl_keyboard *keyboard); +void +wl_keyboard_set_focus(struct wl_keyboard *keyboard, struct wl_surface *surface); +void +wl_keyboard_start_grab(struct wl_keyboard *device, + struct wl_keyboard_grab *grab); +void +wl_keyboard_end_grab(struct wl_keyboard *keyboard); + +void +wl_touch_init(struct wl_touch *touch); +void +wl_touch_release(struct wl_touch *touch); +void +wl_touch_start_grab(struct wl_touch *device, + struct wl_touch_grab *grab); +void +wl_touch_end_grab(struct wl_touch *touch); + +void +wl_data_device_set_keyboard_focus(struct wl_seat *seat); + +int +wl_data_device_manager_init(struct wl_display *display); + + +void +wl_seat_set_selection(struct wl_seat *seat, + struct wl_data_source *source, uint32_t serial); + struct weston_xkb_info { struct xkb_keymap *keymap; int keymap_fd; diff --git a/src/data-device.c b/src/data-device.c new file mode 100644 index 00000000..a2856b6b --- /dev/null +++ b/src/data-device.c @@ -0,0 +1,538 @@ +/* + * Copyright © 2011 Kristian Høgsberg + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include "compositor.h" + +static void +data_offer_accept(struct wl_client *client, struct wl_resource *resource, + uint32_t serial, const char *mime_type) +{ + struct wl_data_offer *offer = resource->data; + + /* FIXME: Check that client is currently focused by the input + * device that is currently dragging this data source. Should + * this be a wl_data_device request? */ + + if (offer->source) + offer->source->accept(offer->source, serial, mime_type); +} + +static void +data_offer_receive(struct wl_client *client, struct wl_resource *resource, + const char *mime_type, int32_t fd) +{ + struct wl_data_offer *offer = resource->data; + + if (offer->source) + offer->source->send(offer->source, mime_type, fd); + else + close(fd); +} + +static void +data_offer_destroy(struct wl_client *client, struct wl_resource *resource) +{ + wl_resource_destroy(resource); +} + +static const struct wl_data_offer_interface data_offer_interface = { + data_offer_accept, + data_offer_receive, + data_offer_destroy, +}; + +static void +destroy_data_offer(struct wl_resource *resource) +{ + struct wl_data_offer *offer = resource->data; + + if (offer->source) + wl_list_remove(&offer->source_destroy_listener.link); + free(offer); +} + +static void +destroy_offer_data_source(struct wl_listener *listener, void *data) +{ + struct wl_data_offer *offer; + + offer = container_of(listener, struct wl_data_offer, + source_destroy_listener); + + offer->source = NULL; +} + +static struct wl_resource * +wl_data_source_send_offer(struct wl_data_source *source, + struct wl_resource *target) +{ + struct wl_data_offer *offer; + char **p; + + offer = malloc(sizeof *offer); + if (offer == NULL) + return NULL; + + wl_resource_init(&offer->resource, &wl_data_offer_interface, + &data_offer_interface, 0, offer); + offer->resource.destroy = destroy_data_offer; + + offer->source = source; + offer->source_destroy_listener.notify = destroy_offer_data_source; + wl_signal_add(&source->resource.destroy_signal, + &offer->source_destroy_listener); + + wl_client_add_resource(target->client, &offer->resource); + + wl_data_device_send_data_offer(target, &offer->resource); + + wl_array_for_each(p, &source->mime_types) + wl_data_offer_send_offer(&offer->resource, *p); + + return &offer->resource; +} + +static void +data_source_offer(struct wl_client *client, + struct wl_resource *resource, + const char *type) +{ + struct wl_data_source *source = resource->data; + char **p; + + p = wl_array_add(&source->mime_types, sizeof *p); + if (p) + *p = strdup(type); + if (!p || !*p) + wl_resource_post_no_memory(resource); +} + +static void +data_source_destroy(struct wl_client *client, struct wl_resource *resource) +{ + wl_resource_destroy(resource); +} + +static struct wl_data_source_interface data_source_interface = { + data_source_offer, + data_source_destroy +}; + +static struct wl_resource * +find_resource(struct wl_list *list, struct wl_client *client) +{ + struct wl_resource *r; + + wl_list_for_each(r, list, link) { + if (r->client == client) + return r; + } + + return NULL; +} + +static void +destroy_drag_focus(struct wl_listener *listener, void *data) +{ + struct wl_seat *seat = + container_of(listener, struct wl_seat, drag_focus_listener); + + seat->drag_focus_resource = NULL; +} + +static void +drag_grab_focus(struct wl_pointer_grab *grab, + struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) +{ + struct wl_seat *seat = container_of(grab, struct wl_seat, drag_grab); + struct wl_resource *resource, *offer = NULL; + struct wl_display *display; + uint32_t serial; + + if (seat->drag_focus_resource) { + wl_data_device_send_leave(seat->drag_focus_resource); + wl_list_remove(&seat->drag_focus_listener.link); + seat->drag_focus_resource = NULL; + seat->drag_focus = NULL; + } + + if (!surface) + return; + + if (!seat->drag_data_source && + surface->resource.client != seat->drag_client) + return; + + resource = find_resource(&seat->drag_resource_list, + surface->resource.client); + if (!resource) + return; + + display = wl_client_get_display(resource->client); + serial = wl_display_next_serial(display); + + if (seat->drag_data_source) + offer = wl_data_source_send_offer(seat->drag_data_source, + resource); + + wl_data_device_send_enter(resource, serial, &surface->resource, + x, y, offer); + + seat->drag_focus = surface; + seat->drag_focus_listener.notify = destroy_drag_focus; + wl_signal_add(&resource->destroy_signal, + &seat->drag_focus_listener); + seat->drag_focus_resource = resource; + grab->focus = surface; +} + +static void +drag_grab_motion(struct wl_pointer_grab *grab, + uint32_t time, wl_fixed_t x, wl_fixed_t y) +{ + struct wl_seat *seat = container_of(grab, struct wl_seat, drag_grab); + + if (seat->drag_focus_resource) + wl_data_device_send_motion(seat->drag_focus_resource, + time, x, y); +} + +static void +data_device_end_drag_grab(struct wl_seat *seat) +{ + if (seat->drag_surface) { + seat->drag_surface = NULL; + wl_signal_emit(&seat->drag_icon_signal, NULL); + wl_list_remove(&seat->drag_icon_listener.link); + } + + drag_grab_focus(&seat->drag_grab, NULL, + wl_fixed_from_int(0), wl_fixed_from_int(0)); + + wl_pointer_end_grab(seat->pointer); + + seat->drag_data_source = NULL; + seat->drag_client = NULL; +} + +static void +drag_grab_button(struct wl_pointer_grab *grab, + uint32_t time, uint32_t button, uint32_t state_w) +{ + struct wl_seat *seat = container_of(grab, struct wl_seat, drag_grab); + enum wl_pointer_button_state state = state_w; + + if (seat->drag_focus_resource && + seat->pointer->grab_button == button && + state == WL_POINTER_BUTTON_STATE_RELEASED) + wl_data_device_send_drop(seat->drag_focus_resource); + + if (seat->pointer->button_count == 0 && + state == WL_POINTER_BUTTON_STATE_RELEASED) { + if (seat->drag_data_source) + wl_list_remove(&seat->drag_data_source_listener.link); + data_device_end_drag_grab(seat); + } +} + +static const struct wl_pointer_grab_interface drag_grab_interface = { + drag_grab_focus, + drag_grab_motion, + drag_grab_button, +}; + +static void +destroy_data_device_source(struct wl_listener *listener, void *data) +{ + struct wl_seat *seat = container_of(listener, struct wl_seat, + drag_data_source_listener); + + data_device_end_drag_grab(seat); +} + +static void +destroy_data_device_icon(struct wl_listener *listener, void *data) +{ + struct wl_seat *seat = container_of(listener, struct wl_seat, + drag_icon_listener); + + seat->drag_surface = NULL; +} + +static void +data_device_start_drag(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *source_resource, + struct wl_resource *origin_resource, + struct wl_resource *icon_resource, uint32_t serial) +{ + struct wl_seat *seat = resource->data; + + /* FIXME: Check that client has implicit grab on the origin + * surface that matches the given time. */ + + /* FIXME: Check that the data source type array isn't empty. */ + + seat->drag_grab.interface = &drag_grab_interface; + + seat->drag_client = client; + seat->drag_data_source = NULL; + + if (source_resource) { + seat->drag_data_source = source_resource->data; + seat->drag_data_source_listener.notify = + destroy_data_device_source; + wl_signal_add(&source_resource->destroy_signal, + &seat->drag_data_source_listener); + } + + if (icon_resource) { + seat->drag_surface = icon_resource->data; + seat->drag_icon_listener.notify = destroy_data_device_icon; + wl_signal_add(&icon_resource->destroy_signal, + &seat->drag_icon_listener); + wl_signal_emit(&seat->drag_icon_signal, icon_resource); + } + + wl_pointer_set_focus(seat->pointer, NULL, + wl_fixed_from_int(0), wl_fixed_from_int(0)); + wl_pointer_start_grab(seat->pointer, &seat->drag_grab); +} + +static void +destroy_selection_data_source(struct wl_listener *listener, void *data) +{ + struct wl_seat *seat = container_of(listener, struct wl_seat, + selection_data_source_listener); + struct wl_resource *data_device; + struct wl_resource *focus = NULL; + + seat->selection_data_source = NULL; + + if (seat->keyboard) + focus = seat->keyboard->focus_resource; + if (focus) { + data_device = find_resource(&seat->drag_resource_list, + focus->client); + if (data_device) + wl_data_device_send_selection(data_device, NULL); + } + + wl_signal_emit(&seat->selection_signal, seat); +} + +WL_EXPORT void +wl_seat_set_selection(struct wl_seat *seat, struct wl_data_source *source, + uint32_t serial) +{ + struct wl_resource *data_device, *offer; + struct wl_resource *focus = NULL; + + if (seat->selection_data_source && + seat->selection_serial - serial < UINT32_MAX / 2) + return; + + if (seat->selection_data_source) { + seat->selection_data_source->cancel(seat->selection_data_source); + wl_list_remove(&seat->selection_data_source_listener.link); + seat->selection_data_source = NULL; + } + + seat->selection_data_source = source; + seat->selection_serial = serial; + + if (seat->keyboard) + focus = seat->keyboard->focus_resource; + if (focus) { + data_device = find_resource(&seat->drag_resource_list, + focus->client); + if (data_device && source) { + offer = wl_data_source_send_offer(seat->selection_data_source, + data_device); + wl_data_device_send_selection(data_device, offer); + } else if (data_device) { + wl_data_device_send_selection(data_device, NULL); + } + } + + wl_signal_emit(&seat->selection_signal, seat); + + if (source) { + seat->selection_data_source_listener.notify = + destroy_selection_data_source; + wl_signal_add(&source->resource.destroy_signal, + &seat->selection_data_source_listener); + } +} + +static void +data_device_set_selection(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *source_resource, uint32_t serial) +{ + if (!source_resource) + return; + + /* FIXME: Store serial and check against incoming serial here. */ + wl_seat_set_selection(resource->data, source_resource->data, + serial); +} + +static const struct wl_data_device_interface data_device_interface = { + data_device_start_drag, + data_device_set_selection, +}; + +static void +destroy_data_source(struct wl_resource *resource) +{ + struct wl_data_source *source = + container_of(resource, struct wl_data_source, resource); + char **p; + + wl_array_for_each(p, &source->mime_types) + free(*p); + + wl_array_release(&source->mime_types); + + source->resource.object.id = 0; +} + +static void +client_source_accept(struct wl_data_source *source, + uint32_t time, const char *mime_type) +{ + wl_data_source_send_target(&source->resource, mime_type); +} + +static void +client_source_send(struct wl_data_source *source, + const char *mime_type, int32_t fd) +{ + wl_data_source_send_send(&source->resource, mime_type, fd); + close(fd); +} + +static void +client_source_cancel(struct wl_data_source *source) +{ + wl_data_source_send_cancelled(&source->resource); +} + +static void +create_data_source(struct wl_client *client, + struct wl_resource *resource, uint32_t id) +{ + struct wl_data_source *source; + + source = malloc(sizeof *source); + if (source == NULL) { + wl_resource_post_no_memory(resource); + return; + } + + wl_resource_init(&source->resource, &wl_data_source_interface, + &data_source_interface, id, source); + source->resource.destroy = destroy_data_source; + + source->accept = client_source_accept; + source->send = client_source_send; + source->cancel = client_source_cancel; + + wl_array_init(&source->mime_types); + wl_client_add_resource(client, &source->resource); +} + +static void unbind_data_device(struct wl_resource *resource) +{ + wl_list_remove(&resource->link); + free(resource); +} + +static void +get_data_device(struct wl_client *client, + struct wl_resource *manager_resource, + uint32_t id, struct wl_resource *seat_resource) +{ + struct wl_seat *seat = seat_resource->data; + struct wl_resource *resource; + + resource = wl_client_add_object(client, &wl_data_device_interface, + &data_device_interface, id, + seat); + + wl_list_insert(&seat->drag_resource_list, &resource->link); + resource->destroy = unbind_data_device; +} + +static const struct wl_data_device_manager_interface manager_interface = { + create_data_source, + get_data_device +}; + +static void +bind_manager(struct wl_client *client, + void *data, uint32_t version, uint32_t id) +{ + wl_client_add_object(client, &wl_data_device_manager_interface, + &manager_interface, id, NULL); +} + +WL_EXPORT void +wl_data_device_set_keyboard_focus(struct wl_seat *seat) +{ + struct wl_resource *data_device, *focus, *offer; + struct wl_data_source *source; + + if (!seat->keyboard) + return; + + focus = seat->keyboard->focus_resource; + if (!focus) + return; + + data_device = find_resource(&seat->drag_resource_list, + focus->client); + if (!data_device) + return; + + source = seat->selection_data_source; + if (source) { + offer = wl_data_source_send_offer(source, data_device); + wl_data_device_send_selection(data_device, offer); + } +} + +WL_EXPORT int +wl_data_device_manager_init(struct wl_display *display) +{ + if (wl_display_add_global(display, + &wl_data_device_manager_interface, + NULL, bind_manager) == NULL) + return -1; + + return 0; +} diff --git a/src/input.c b/src/input.c new file mode 100644 index 00000000..8ef5b2a1 --- /dev/null +++ b/src/input.c @@ -0,0 +1,553 @@ +/* + * Copyright © 2013 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include "compositor.h" + +static void +lose_pointer_focus(struct wl_listener *listener, void *data) +{ + struct wl_pointer *pointer = + container_of(listener, struct wl_pointer, focus_listener); + + pointer->focus_resource = NULL; +} + +static void +lose_keyboard_focus(struct wl_listener *listener, void *data) +{ + struct wl_keyboard *keyboard = + container_of(listener, struct wl_keyboard, focus_listener); + + keyboard->focus_resource = NULL; +} + +static void +lose_touch_focus(struct wl_listener *listener, void *data) +{ + struct wl_touch *touch = + container_of(listener, struct wl_touch, focus_listener); + + touch->focus_resource = NULL; +} + +static void +default_grab_focus(struct wl_pointer_grab *grab, + struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) +{ + struct wl_pointer *pointer = grab->pointer; + + if (pointer->button_count > 0) + return; + + wl_pointer_set_focus(pointer, surface, x, y); +} + +static void +default_grab_motion(struct wl_pointer_grab *grab, + uint32_t time, wl_fixed_t x, wl_fixed_t y) +{ + struct wl_resource *resource; + + resource = grab->pointer->focus_resource; + if (resource) + wl_pointer_send_motion(resource, time, x, y); +} + +static void +default_grab_button(struct wl_pointer_grab *grab, + uint32_t time, uint32_t button, uint32_t state_w) +{ + struct wl_pointer *pointer = grab->pointer; + struct wl_resource *resource; + uint32_t serial; + enum wl_pointer_button_state state = state_w; + struct wl_display *display; + + resource = pointer->focus_resource; + if (resource) { + display = wl_client_get_display(resource->client); + serial = wl_display_next_serial(display); + wl_pointer_send_button(resource, serial, time, button, state_w); + } + + if (pointer->button_count == 0 && + state == WL_POINTER_BUTTON_STATE_RELEASED) + wl_pointer_set_focus(pointer, pointer->current, + pointer->current_x, pointer->current_y); +} + +static const struct wl_pointer_grab_interface + default_pointer_grab_interface = { + default_grab_focus, + default_grab_motion, + default_grab_button +}; + +static void default_grab_touch_down(struct wl_touch_grab *grab, + uint32_t time, + int touch_id, + wl_fixed_t sx, + wl_fixed_t sy) +{ + struct wl_touch *touch = grab->touch; + struct wl_display *display; + uint32_t serial; + + if (touch->focus_resource && touch->focus) { + display = wl_client_get_display(touch->focus_resource->client); + serial = wl_display_next_serial(display); + wl_touch_send_down(touch->focus_resource, serial, time, + &touch->focus->resource, touch_id, sx, sy); + } +} + +static void default_grab_touch_up(struct wl_touch_grab *grab, + uint32_t time, + int touch_id) +{ + struct wl_touch *touch = grab->touch; + struct wl_display *display; + uint32_t serial; + + if (touch->focus_resource) { + display = wl_client_get_display(touch->focus_resource->client); + serial = wl_display_next_serial(display); + wl_touch_send_up(touch->focus_resource, serial, time, touch_id); + } +} + +static void default_grab_touch_motion(struct wl_touch_grab *grab, + uint32_t time, + int touch_id, + wl_fixed_t sx, + wl_fixed_t sy) +{ + struct wl_touch *touch = grab->touch; + + if (touch->focus_resource) { + wl_touch_send_motion(touch->focus_resource, time, + touch_id, sx, sy); + } +} + +static const struct wl_touch_grab_interface default_touch_grab_interface = { + default_grab_touch_down, + default_grab_touch_up, + default_grab_touch_motion +}; + +static void +default_grab_key(struct wl_keyboard_grab *grab, + uint32_t time, uint32_t key, uint32_t state) +{ + struct wl_keyboard *keyboard = grab->keyboard; + struct wl_resource *resource; + struct wl_display *display; + uint32_t serial; + + resource = keyboard->focus_resource; + if (resource) { + display = wl_client_get_display(resource->client); + serial = wl_display_next_serial(display); + wl_keyboard_send_key(resource, serial, time, key, state); + } +} + +static struct wl_resource * +find_resource_for_surface(struct wl_list *list, struct wl_surface *surface) +{ + struct wl_resource *r; + + if (!surface) + return NULL; + + wl_list_for_each(r, list, link) { + if (r->client == surface->resource.client) + return r; + } + + return NULL; +} + +static void +default_grab_modifiers(struct wl_keyboard_grab *grab, uint32_t serial, + uint32_t mods_depressed, uint32_t mods_latched, + uint32_t mods_locked, uint32_t group) +{ + struct wl_keyboard *keyboard = grab->keyboard; + struct wl_pointer *pointer = keyboard->seat->pointer; + struct wl_resource *resource, *pr; + + resource = keyboard->focus_resource; + if (!resource) + return; + + wl_keyboard_send_modifiers(resource, serial, mods_depressed, + mods_latched, mods_locked, group); + + if (pointer && pointer->focus && pointer->focus != keyboard->focus) { + pr = find_resource_for_surface(&keyboard->resource_list, + pointer->focus); + if (pr) { + wl_keyboard_send_modifiers(pr, + serial, + keyboard->modifiers.mods_depressed, + keyboard->modifiers.mods_latched, + keyboard->modifiers.mods_locked, + keyboard->modifiers.group); + } + } +} + +static const struct wl_keyboard_grab_interface + default_keyboard_grab_interface = { + default_grab_key, + default_grab_modifiers, +}; + +WL_EXPORT void +wl_pointer_init(struct wl_pointer *pointer) +{ + memset(pointer, 0, sizeof *pointer); + wl_list_init(&pointer->resource_list); + pointer->focus_listener.notify = lose_pointer_focus; + pointer->default_grab.interface = &default_pointer_grab_interface; + pointer->default_grab.pointer = pointer; + pointer->grab = &pointer->default_grab; + wl_signal_init(&pointer->focus_signal); + + /* FIXME: Pick better co-ords. */ + pointer->x = wl_fixed_from_int(100); + pointer->y = wl_fixed_from_int(100); +} + +WL_EXPORT void +wl_pointer_release(struct wl_pointer *pointer) +{ + /* XXX: What about pointer->resource_list? */ + if (pointer->focus_resource) + wl_list_remove(&pointer->focus_listener.link); +} + +WL_EXPORT void +wl_keyboard_init(struct wl_keyboard *keyboard) +{ + memset(keyboard, 0, sizeof *keyboard); + wl_list_init(&keyboard->resource_list); + wl_array_init(&keyboard->keys); + keyboard->focus_listener.notify = lose_keyboard_focus; + keyboard->default_grab.interface = &default_keyboard_grab_interface; + keyboard->default_grab.keyboard = keyboard; + keyboard->grab = &keyboard->default_grab; + wl_signal_init(&keyboard->focus_signal); +} + +WL_EXPORT void +wl_keyboard_release(struct wl_keyboard *keyboard) +{ + /* XXX: What about keyboard->resource_list? */ + if (keyboard->focus_resource) + wl_list_remove(&keyboard->focus_listener.link); + wl_array_release(&keyboard->keys); +} + +WL_EXPORT void +wl_touch_init(struct wl_touch *touch) +{ + memset(touch, 0, sizeof *touch); + wl_list_init(&touch->resource_list); + touch->focus_listener.notify = lose_touch_focus; + touch->default_grab.interface = &default_touch_grab_interface; + touch->default_grab.touch = touch; + touch->grab = &touch->default_grab; + wl_signal_init(&touch->focus_signal); +} + +WL_EXPORT void +wl_touch_release(struct wl_touch *touch) +{ + /* XXX: What about touch->resource_list? */ + if (touch->focus_resource) + wl_list_remove(&touch->focus_listener.link); +} + +WL_EXPORT void +wl_seat_init(struct wl_seat *seat) +{ + memset(seat, 0, sizeof *seat); + + wl_signal_init(&seat->destroy_signal); + + seat->selection_data_source = NULL; + wl_list_init(&seat->base_resource_list); + wl_signal_init(&seat->selection_signal); + wl_list_init(&seat->drag_resource_list); + wl_signal_init(&seat->drag_icon_signal); +} + +WL_EXPORT void +wl_seat_release(struct wl_seat *seat) +{ + wl_signal_emit(&seat->destroy_signal, seat); + + if (seat->pointer) + wl_pointer_release(seat->pointer); + if (seat->keyboard) + wl_keyboard_release(seat->keyboard); + if (seat->touch) + wl_touch_release(seat->touch); +} + +static void +seat_send_updated_caps(struct wl_seat *seat) +{ + struct wl_resource *r; + enum wl_seat_capability caps = 0; + + if (seat->pointer) + caps |= WL_SEAT_CAPABILITY_POINTER; + if (seat->keyboard) + caps |= WL_SEAT_CAPABILITY_KEYBOARD; + if (seat->touch) + caps |= WL_SEAT_CAPABILITY_TOUCH; + + wl_list_for_each(r, &seat->base_resource_list, link) + wl_seat_send_capabilities(r, caps); +} + +WL_EXPORT void +wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer) +{ + if (pointer && (seat->pointer || pointer->seat)) + return; /* XXX: error? */ + if (!pointer && !seat->pointer) + return; + + seat->pointer = pointer; + if (pointer) + pointer->seat = seat; + + seat_send_updated_caps(seat); +} + +WL_EXPORT void +wl_seat_set_keyboard(struct wl_seat *seat, struct wl_keyboard *keyboard) +{ + if (keyboard && (seat->keyboard || keyboard->seat)) + return; /* XXX: error? */ + if (!keyboard && !seat->keyboard) + return; + + seat->keyboard = keyboard; + if (keyboard) + keyboard->seat = seat; + + seat_send_updated_caps(seat); +} + +WL_EXPORT void +wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch) +{ + if (touch && (seat->touch || touch->seat)) + return; /* XXX: error? */ + if (!touch && !seat->touch) + return; + + seat->touch = touch; + if (touch) + touch->seat = seat; + + seat_send_updated_caps(seat); +} + +WL_EXPORT void +wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface, + wl_fixed_t sx, wl_fixed_t sy) +{ + struct wl_keyboard *kbd = pointer->seat->keyboard; + struct wl_resource *resource, *kr; + struct wl_display *display; + uint32_t serial; + + resource = pointer->focus_resource; + if (resource && pointer->focus != surface) { + display = wl_client_get_display(resource->client); + serial = wl_display_next_serial(display); + wl_pointer_send_leave(resource, serial, + &pointer->focus->resource); + wl_list_remove(&pointer->focus_listener.link); + } + + resource = find_resource_for_surface(&pointer->resource_list, + surface); + if (resource && + (pointer->focus != surface || + pointer->focus_resource != resource)) { + display = wl_client_get_display(resource->client); + serial = wl_display_next_serial(display); + if (kbd) { + kr = find_resource_for_surface(&kbd->resource_list, + surface); + if (kr) { + wl_keyboard_send_modifiers(kr, + serial, + kbd->modifiers.mods_depressed, + kbd->modifiers.mods_latched, + kbd->modifiers.mods_locked, + kbd->modifiers.group); + } + } + wl_pointer_send_enter(resource, serial, &surface->resource, + sx, sy); + wl_signal_add(&resource->destroy_signal, + &pointer->focus_listener); + pointer->focus_serial = serial; + } + + pointer->focus_resource = resource; + pointer->focus = surface; + pointer->default_grab.focus = surface; + wl_signal_emit(&pointer->focus_signal, pointer); +} + +WL_EXPORT void +wl_keyboard_set_focus(struct wl_keyboard *keyboard, struct wl_surface *surface) +{ + struct wl_resource *resource; + struct wl_display *display; + uint32_t serial; + + if (keyboard->focus_resource && keyboard->focus != surface) { + resource = keyboard->focus_resource; + display = wl_client_get_display(resource->client); + serial = wl_display_next_serial(display); + wl_keyboard_send_leave(resource, serial, + &keyboard->focus->resource); + wl_list_remove(&keyboard->focus_listener.link); + } + + resource = find_resource_for_surface(&keyboard->resource_list, + surface); + if (resource && + (keyboard->focus != surface || + keyboard->focus_resource != resource)) { + display = wl_client_get_display(resource->client); + serial = wl_display_next_serial(display); + wl_keyboard_send_modifiers(resource, serial, + keyboard->modifiers.mods_depressed, + keyboard->modifiers.mods_latched, + keyboard->modifiers.mods_locked, + keyboard->modifiers.group); + wl_keyboard_send_enter(resource, serial, &surface->resource, + &keyboard->keys); + wl_signal_add(&resource->destroy_signal, + &keyboard->focus_listener); + keyboard->focus_serial = serial; + } + + keyboard->focus_resource = resource; + keyboard->focus = surface; + wl_signal_emit(&keyboard->focus_signal, keyboard); +} + +WL_EXPORT void +wl_keyboard_start_grab(struct wl_keyboard *keyboard, + struct wl_keyboard_grab *grab) +{ + keyboard->grab = grab; + grab->keyboard = keyboard; + + /* XXX focus? */ +} + +WL_EXPORT void +wl_keyboard_end_grab(struct wl_keyboard *keyboard) +{ + keyboard->grab = &keyboard->default_grab; +} + +WL_EXPORT void +wl_pointer_start_grab(struct wl_pointer *pointer, struct wl_pointer_grab *grab) +{ + const struct wl_pointer_grab_interface *interface; + + pointer->grab = grab; + interface = pointer->grab->interface; + grab->pointer = pointer; + + if (pointer->current) + interface->focus(pointer->grab, pointer->current, + pointer->current_x, pointer->current_y); +} + +WL_EXPORT void +wl_pointer_end_grab(struct wl_pointer *pointer) +{ + const struct wl_pointer_grab_interface *interface; + + pointer->grab = &pointer->default_grab; + interface = pointer->grab->interface; + interface->focus(pointer->grab, pointer->current, + pointer->current_x, pointer->current_y); +} + +static void +current_surface_destroy(struct wl_listener *listener, void *data) +{ + struct wl_pointer *pointer = + container_of(listener, struct wl_pointer, current_listener); + + pointer->current = NULL; +} + +WL_EXPORT void +wl_pointer_set_current(struct wl_pointer *pointer, struct wl_surface *surface) +{ + if (pointer->current) + wl_list_remove(&pointer->current_listener.link); + + pointer->current = surface; + + if (!surface) + return; + + wl_signal_add(&surface->resource.destroy_signal, + &pointer->current_listener); + pointer->current_listener.notify = current_surface_destroy; +} + +WL_EXPORT void +wl_touch_start_grab(struct wl_touch *touch, struct wl_touch_grab *grab) +{ + touch->grab = grab; + grab->touch = touch; +} + +WL_EXPORT void +wl_touch_end_grab(struct wl_touch *touch) +{ + touch->grab = &touch->default_grab; +}