From 21626e8153490bf155e812644454fe9610491ffd Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 10 Dec 2017 11:11:47 -0500 Subject: seat focus on button press --- include/sway/container.h | 4 ++++ include/sway/input/input-manager.h | 3 +++ include/sway/input/seat.h | 8 ++++++- sway/input/cursor.c | 15 +++++++++++++ sway/input/input-manager.c | 14 +++++++++++- sway/input/seat.c | 44 ++++++++++++++++++++++++++++++++++++-- sway/tree/container.c | 6 ++++++ 7 files changed, 90 insertions(+), 4 deletions(-) diff --git a/include/sway/container.h b/include/sway/container.h index 0e1cc8a3..f13745ee 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -124,6 +124,10 @@ struct sway_container { * Marks applied to the container, list_t of char*. */ list_t *marks; + + struct { + struct wl_signal destroy; + } events; }; void swayc_descendants_of_type(swayc_t *root, enum swayc_types type, diff --git a/include/sway/input/input-manager.h b/include/sway/input/input-manager.h index 5dc75ba7..9548c170 100644 --- a/include/sway/input/input-manager.h +++ b/include/sway/input/input-manager.h @@ -19,4 +19,7 @@ char* libinput_dev_unique_id(struct libinput_device *dev); struct sway_input_manager *sway_input_manager_create( struct sway_server *server); +bool sway_input_manager_swayc_has_focus(struct sway_input_manager *input, + swayc_t *container); + #endif diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index f7f8a1bb..964c0f7b 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -7,9 +7,13 @@ struct sway_seat { struct wlr_seat *seat; struct sway_cursor *cursor; + struct sway_input_manager *input; + swayc_t *focus; + + struct wl_listener focus_destroy; }; -struct sway_seat *sway_seat_create(struct wl_display *display, +struct sway_seat *sway_seat_create(struct sway_input_manager *input, const char *seat_name); void sway_seat_add_device(struct sway_seat *seat, @@ -20,4 +24,6 @@ void sway_seat_remove_device(struct sway_seat *seat, void sway_seat_configure_xcursor(struct sway_seat *seat); +void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container); + #endif diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 5f2d650e..217c2ddb 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -1,4 +1,9 @@ #define _XOPEN_SOURCE 700 +#ifdef __linux__ +#include +#elif __FreeBSD__ +#include +#endif #include #include #include "sway/input/cursor.h" @@ -57,6 +62,16 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, button); struct wlr_event_pointer_button *event = data; + + if (event->button == BTN_LEFT) { + struct wlr_surface *surface = NULL; + double sx, sy; + swayc_t *swayc = + swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); + + sway_seat_set_focus(cursor->seat, swayc); + } + wlr_seat_pointer_notify_button(cursor->seat->seat, event->time_msec, event->button, event->state); } diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index 4f52e59a..ca80f267 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -27,7 +27,7 @@ static struct sway_seat *input_manager_get_seat( } } - seat = sway_seat_create(input->server->wl_display, seat_name); + seat = sway_seat_create(input, seat_name); list_add(input->seats, seat); return seat; @@ -131,3 +131,15 @@ char *libinput_dev_unique_id(struct libinput_device *device) { free(name); return identifier; } + +bool sway_input_manager_swayc_has_focus(struct sway_input_manager *input, + swayc_t *container) { + for (int i = 0; i < input->seats->length; ++i) { + struct sway_seat *seat = input->seats->items[i]; + if (seat->focus == container) { + return true; + } + } + + return false; +} diff --git a/sway/input/seat.c b/sway/input/seat.c index 5aed1f68..94f547cc 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -5,16 +5,17 @@ #include "sway/input/cursor.h" #include "sway/input/input-manager.h" #include "sway/output.h" +#include "sway/view.h" #include "log.h" -struct sway_seat *sway_seat_create(struct wl_display *display, +struct sway_seat *sway_seat_create(struct sway_input_manager *input, const char *seat_name) { struct sway_seat *seat = calloc(1, sizeof(struct sway_seat)); if (!seat) { return NULL; } - seat->seat = wlr_seat_create(display, seat_name); + seat->seat = wlr_seat_create(input->server->wl_display, seat_name); if (!sway_assert(seat->seat, "could not allocate seat")) { return NULL; } @@ -26,6 +27,8 @@ struct sway_seat *sway_seat_create(struct wl_display *display, return NULL; } + seat->input = input; + wlr_seat_set_capabilities(seat->seat, WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_POINTER | @@ -110,3 +113,40 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) { wlr_cursor_warp(seat->cursor->cursor, NULL, seat->cursor->cursor->x, seat->cursor->cursor->y); } + +static void handle_focus_destroy(struct wl_listener *listener, void *data) { + struct sway_seat *seat = wl_container_of(listener, seat, focus_destroy); + //swayc_t *container = data; + + // TODO set new focus based on the state of the tree + sway_seat_set_focus(seat, NULL); +} + +void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { + swayc_t *last_focus = seat->focus; + + if (last_focus == container) { + return; + } + + if (last_focus) { + wl_list_remove(&seat->focus_destroy.link); + } + + if (container) { + struct sway_view *view = container->sway_view; + view->iface.set_activated(view, true); + wl_signal_add(&container->events.destroy, &seat->focus_destroy); + seat->focus_destroy.notify = handle_focus_destroy; + // TODO give keyboard focus + } + + seat->focus = container; + + if (last_focus && + !sway_input_manager_swayc_has_focus(seat->input, last_focus)) { + struct sway_view *view = last_focus->sway_view; + view->iface.set_activated(view, false); + + } +} diff --git a/sway/tree/container.c b/sway/tree/container.c index 321ef8b1..78c8625f 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -40,6 +40,9 @@ static swayc_t *new_swayc(enum swayc_types type) { if (type != C_VIEW) { c->children = create_list(); } + + wl_signal_init(&c->events.destroy); + return c; } @@ -119,6 +122,9 @@ static void free_swayc(swayc_t *cont) { if (!sway_assert(cont, "free_swayc passed NULL")) { return; } + + wl_signal_emit(&cont->events.destroy, cont); + if (cont->children) { // remove children until there are no more, free_swayc calls // remove_child, which removes child from this container -- cgit v1.2.3-54-g00ecf