From cf713edc1052943ceaf33fd3b41242b5775fd924 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 3 Nov 2017 14:49:15 +0100 Subject: [PATCH 1/7] Fix moving views when resizing below min size --- include/rootston/view.h | 8 +++- rootston/cursor.c | 23 ++++++----- rootston/desktop.c | 31 ++++++++++----- rootston/wl_shell.c | 2 +- rootston/xdg_shell_v6.c | 75 +++++++++++++++++++++++++++-------- rootston/xwayland.c | 86 ++++++++++++++++++++++++++++------------- 6 files changed, 159 insertions(+), 66 deletions(-) diff --git a/include/rootston/view.h b/include/rootston/view.h index 993ff654..0913b42e 100644 --- a/include/rootston/view.h +++ b/include/rootston/view.h @@ -73,15 +73,19 @@ struct roots_view { // elsewhere void (*get_size)(struct roots_view *view, struct wlr_box *box); void (*activate)(struct roots_view *view, bool active); + void (*move)(struct roots_view *view, double x, double y); void (*resize)(struct roots_view *view, uint32_t width, uint32_t height); - void (*set_position)(struct roots_view *view, double x, double y); + void (*move_resize)(struct roots_view *view, double x, double y, + uint32_t width, uint32_t height); void (*close)(struct roots_view *view); }; void view_get_size(struct roots_view *view, struct wlr_box *box); void view_activate(struct roots_view *view, bool active); +void view_move(struct roots_view *view, double x, double y); void view_resize(struct roots_view *view, uint32_t width, uint32_t height); -void view_set_position(struct roots_view *view, double x, double y); +void view_move_resize(struct roots_view *view, double x, double y, + uint32_t width, uint32_t height); void view_close(struct roots_view *view); bool view_center(struct roots_view *view); void view_setup(struct roots_view *view); diff --git a/rootston/cursor.c b/rootston/cursor.c index 31001a9f..b153e8c8 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -117,8 +117,8 @@ void cursor_update_position(struct roots_input *input, uint32_t time) { if (input->active_view) { double dx = input->cursor->x - input->offs_x; double dy = input->cursor->y - input->offs_y; - view_set_position(input->active_view, - input->view_x + dx, input->view_y + dy); + view_move(input->active_view, input->view_x + dx, + input->view_y + dy); } break; case ROOTS_CURSOR_RESIZE: @@ -132,15 +132,19 @@ void cursor_update_position(struct roots_input *input, uint32_t time) { if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_TOP) { active_y = input->view_y + dy; height -= dy; - } - if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_BOTTOM) { + if (height < 0) { + active_y += height; + } + } else if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_BOTTOM) { height += dy; } if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_LEFT) { active_x = input->view_x + dx; width -= dx; - } - if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_RIGHT) { + if (width < 0) { + active_x += width; + } + } else if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_RIGHT) { width += dx; } @@ -151,12 +155,13 @@ void cursor_update_position(struct roots_input *input, uint32_t time) { height = 0; } - // TODO we might need one configure event for this if (active_x != input->active_view->x || active_y != input->active_view->y) { - view_set_position(input->active_view, active_x, active_y); + view_move_resize(input->active_view, active_x, active_y, + width, height); + } else { + view_resize(input->active_view, width, height); } - view_resize(input->active_view, width, height); } break; case ROOTS_CURSOR_ROTATE: diff --git a/rootston/desktop.c b/rootston/desktop.c index f93d1df8..29f78ac7 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -45,9 +45,15 @@ void view_get_size(struct roots_view *view, struct wlr_box *box) { box->height = view->wlr_surface->current->height; } -void view_set_position(struct roots_view *view, double x, double y) { - if (view->set_position) { - view->set_position(view, x, y); +void view_activate(struct roots_view *view, bool activate) { + if (view->activate) { + view->activate(view, activate); + } +} + +void view_move(struct roots_view *view, double x, double y) { + if (view->move) { + view->move(view, x, y); return; } @@ -55,18 +61,23 @@ void view_set_position(struct roots_view *view, double x, double y) { view->y = y; } -void view_activate(struct roots_view *view, bool activate) { - if (view->activate) { - view->activate(view, activate); - } -} - void view_resize(struct roots_view *view, uint32_t width, uint32_t height) { if (view->resize) { view->resize(view, width, height); } } +void view_move_resize(struct roots_view *view, double x, double y, + uint32_t width, uint32_t height) { + if (view->move_resize) { + view->move_resize(view, x, y, width, height); + return; + } + + view_move(view, x, y); + view_resize(view, width, height); +} + void view_close(struct roots_view *view) { if (view->close) { view->close(view); @@ -101,7 +112,7 @@ bool view_center(struct roots_view *view) { double view_x = (double)(width - size.width) / 2 + l_output->x; double view_y = (double)(height - size.height) / 2 + l_output->y; - view_set_position(view, view_x, view_y); + view_move(view, view_x, view_y); return true; } diff --git a/rootston/wl_shell.c b/rootston/wl_shell.c index e5366672..e38eb697 100644 --- a/rootston/wl_shell.c +++ b/rootston/wl_shell.c @@ -120,7 +120,7 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { surface->parent); if (i != -1) { struct roots_view *parent = desktop->views->items[i]; - view_set_position(view, + view_move(view, parent->x + surface->transient_state->x, parent->y + surface->transient_state->y); } diff --git a/rootston/xdg_shell_v6.c b/rootston/xdg_shell_v6.c index 704ccb1e..ca33c582 100644 --- a/rootston/xdg_shell_v6.c +++ b/rootston/xdg_shell_v6.c @@ -25,27 +25,63 @@ static void activate(struct roots_view *view, bool active) { } } +static void apply_size_constraints(struct wlr_xdg_surface_v6 *surf, + uint32_t width, uint32_t height, uint32_t *dest_width, + uint32_t *dest_height) { + *dest_width = width; + *dest_height = height; + + struct wlr_xdg_toplevel_v6_state *state = + &surf->toplevel_state->current; + if (width < state->min_width) { + *dest_width = state->min_width; + } else if (state->max_width > 0 && + width > state->max_width) { + *dest_width = state->max_width; + } + if (height < state->min_height) { + *dest_height = state->min_height; + } else if (state->max_height > 0 && + height > state->max_height) { + *dest_height = state->max_height; + } +} + static void resize(struct roots_view *view, uint32_t width, uint32_t height) { assert(view->type == ROOTS_XDG_SHELL_V6_VIEW); struct wlr_xdg_surface_v6 *surf = view->xdg_surface_v6; - if (surf->role == WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) { - struct wlr_xdg_toplevel_v6_state *state = - &surf->toplevel_state->current; - if (width < state->min_width) { - width = state->min_width; - } else if (state->max_width > 0 && - width > state->max_width) { - width = state->max_width; - } - if (height < state->min_height) { - height = state->min_height; - } else if (state->max_height > 0 && - height > state->max_height) { - height = state->max_height; - } - - wlr_xdg_toplevel_v6_set_size(surf, width, height); + if (surf->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) { + return; } + + uint32_t contrained_width, contrained_height; + apply_size_constraints(surf, width, height, &contrained_width, + &contrained_height); + + wlr_xdg_toplevel_v6_set_size(surf, contrained_width, contrained_height); +} + +static void move_resize(struct roots_view *view, double x, double y, + uint32_t width, uint32_t height) { + assert(view->type == ROOTS_XDG_SHELL_V6_VIEW); + struct wlr_xdg_surface_v6 *surf = view->xdg_surface_v6; + if (surf->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) { + return; + } + + uint32_t contrained_width, contrained_height; + apply_size_constraints(surf, width, height, &contrained_width, + &contrained_height); + + x = x + width - contrained_width; + y = y + height - contrained_height; + + // TODO: we should wait for an ack_configure event before updating the + // position + view->x = x; + view->y = y; + + wlr_xdg_toplevel_v6_set_size(surf, contrained_width, contrained_height); } static void close(struct roots_view *view) { @@ -130,6 +166,10 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { &roots_surface->request_resize); struct roots_view *view = calloc(1, sizeof(struct roots_view)); + if (!view) { + free(roots_surface); + return; + } view->type = ROOTS_XDG_SHELL_V6_VIEW; view->xdg_surface_v6 = surface; view->roots_xdg_surface_v6 = roots_surface; @@ -137,6 +177,7 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { view->get_size = get_size; view->activate = activate; view->resize = resize; + view->move_resize = move_resize; view->close = close; view->desktop = desktop; roots_surface->view = view; diff --git a/rootston/xwayland.c b/rootston/xwayland.c index 2d7fe946..e3fc1c84 100644 --- a/rootston/xwayland.c +++ b/rootston/xwayland.c @@ -15,32 +15,7 @@ static void activate(struct roots_view *view, bool active) { wlr_xwayland_surface_activate(xwayland, view->xwayland_surface, active); } -static void resize(struct roots_view *view, uint32_t width, uint32_t height) { - assert(view->type == ROOTS_XWAYLAND_VIEW); - struct wlr_xwayland_surface *xwayland_surface = view->xwayland_surface; - - struct wlr_xwayland_surface_size_hints *size_hints = - xwayland_surface->size_hints; - if (size_hints != NULL) { - if (width < (uint32_t)size_hints->min_width) { - width = size_hints->min_width; - } else if (size_hints->max_width > 0 && - width > (uint32_t)size_hints->max_width) { - width = size_hints->max_width; - } - if (height < (uint32_t)size_hints->min_height) { - height = size_hints->min_height; - } else if (size_hints->max_height > 0 && - height > (uint32_t)size_hints->max_height) { - height = size_hints->max_height; - } - } - - wlr_xwayland_surface_configure(view->desktop->xwayland, xwayland_surface, - xwayland_surface->x, xwayland_surface->y, width, height); -} - -static void set_position(struct roots_view *view, double x, double y) { +static void move(struct roots_view *view, double x, double y) { assert(view->type == ROOTS_XWAYLAND_VIEW); struct wlr_xwayland_surface *xwayland_surface = view->xwayland_surface; view->x = x; @@ -49,6 +24,62 @@ static void set_position(struct roots_view *view, double x, double y) { x, y, xwayland_surface->width, xwayland_surface->height); } +static void apply_size_constraints( + struct wlr_xwayland_surface *xwayland_surface, uint32_t width, + uint32_t height, uint32_t *dest_width, uint32_t *dest_height) { + *dest_width = width; + *dest_height = height; + + struct wlr_xwayland_surface_size_hints *size_hints = + xwayland_surface->size_hints; + if (size_hints != NULL) { + if (width < (uint32_t)size_hints->min_width) { + *dest_width = size_hints->min_width; + } else if (size_hints->max_width > 0 && + width > (uint32_t)size_hints->max_width) { + *dest_width = size_hints->max_width; + } + if (height < (uint32_t)size_hints->min_height) { + *dest_height = size_hints->min_height; + } else if (size_hints->max_height > 0 && + height > (uint32_t)size_hints->max_height) { + *dest_height = size_hints->max_height; + } + } +} + +static void resize(struct roots_view *view, uint32_t width, uint32_t height) { + assert(view->type == ROOTS_XWAYLAND_VIEW); + struct wlr_xwayland_surface *xwayland_surface = view->xwayland_surface; + + uint32_t contrained_width, contrained_height; + apply_size_constraints(xwayland_surface, width, height, &contrained_width, + &contrained_height); + + wlr_xwayland_surface_configure(view->desktop->xwayland, xwayland_surface, + xwayland_surface->x, xwayland_surface->y, contrained_width, + contrained_height); +} + +static void move_resize(struct roots_view *view, double x, double y, + uint32_t width, uint32_t height) { + assert(view->type == ROOTS_XWAYLAND_VIEW); + struct wlr_xwayland_surface *xwayland_surface = view->xwayland_surface; + + uint32_t contrained_width, contrained_height; + apply_size_constraints(xwayland_surface, width, height, &contrained_width, + &contrained_height); + + x = x + width - contrained_width; + y = y + height - contrained_height; + + view->x = x; + view->y = y; + + wlr_xwayland_surface_configure(view->desktop->xwayland, xwayland_surface, + x, y, contrained_width, contrained_height); +} + static void close(struct roots_view *view) { assert(view->type == ROOTS_XWAYLAND_VIEW); wlr_xwayland_surface_close(view->desktop->xwayland, view->xwayland_surface); @@ -204,7 +235,8 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { view->desktop = desktop; view->activate = activate; view->resize = resize; - view->set_position = set_position; + view->move = move; + view->move_resize = move_resize; view->close = close; roots_surface->view = view; wlr_list_add(desktop->views, view); From 1cee782fec090a19a390aec96e88475303dacafc Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 5 Nov 2017 13:00:27 +0100 Subject: [PATCH 2/7] Fix pointer axis delta in Wayland backend --- backend/wayland/wl_seat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index deed215e..a2da8df5 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -90,7 +90,7 @@ static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, struct wlr_event_pointer_axis wlr_event; wlr_event.device = dev; - wlr_event.delta = value; + wlr_event.delta = wl_fixed_to_double(value); wlr_event.orientation = axis; wlr_event.time_msec = time; wlr_event.source = wlr_wl_pointer->axis_source; From c88990d2c2c66835f9098dc642b6d679529a84a8 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Tue, 7 Nov 2017 12:32:55 +0100 Subject: [PATCH 3/7] fix the keybind handling in rootston. As mentioned in https://github.com/swaywm/wlroots/issues/393 keybinds did't trigger / were checked with "odd" keys and modifiers. This commit sends the keycode through two paths, one to get the keycode and modifiers *after* xkb handles them, a secondary path to get a "raw" keysym without modifiers and then add the modifiers rootston knows about. This will result in the `[Alt]+[Shift]+2` combination I mention earlier going through the keybind detection twice. 1) `[Alt]+[at]` 2) `[Alt]+[Shift]+2` When either combination is found, the appropriate keybind is executed. The xkb handled version will be prefered over the "raw" version. --- rootston/keyboard.c | 66 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/rootston/keyboard.c b/rootston/keyboard.c index e174b731..6b1fde86 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -60,7 +60,7 @@ static void keyboard_binding_execute(struct roots_keyboard *keyboard, * should be propagated to clients. */ static bool keyboard_keysym_press(struct roots_keyboard *keyboard, - xkb_keysym_t keysym) { + xkb_keysym_t keysym, uint32_t modifiers) { ssize_t i = keyboard_pressed_keysym_index(keyboard, keysym); if (i < 0) { i = keyboard_pressed_keysym_index(keyboard, XKB_KEY_NoSymbol); @@ -88,7 +88,6 @@ static bool keyboard_keysym_press(struct roots_keyboard *keyboard, wlr_seat_keyboard_end_grab(keyboard->input->wl_seat); } - uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); struct wl_list *bindings = &keyboard->input->server->config->bindings; struct binding_config *bc; wl_list_for_each(bc, bindings, link) { @@ -122,25 +121,70 @@ static void keyboard_keysym_release(struct roots_keyboard *keyboard, } } -static void keyboard_key_notify(struct wl_listener *listener, void *data) { - struct wlr_event_keyboard_key *event = data; - struct roots_keyboard *keyboard = wl_container_of(listener, keyboard, key); - - uint32_t keycode = event->keycode + 8; +static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, + uint32_t keycode, enum wlr_key_state state) +{ + uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); const xkb_keysym_t *syms; - int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, - keycode, &syms); + xkb_layout_index_t layout_index = xkb_state_key_get_layout( + keyboard->device->keyboard->xkb_state, + keycode); + int syms_len = xkb_keymap_key_get_syms_by_level(keyboard->device->keyboard->keymap, + keycode, layout_index, 0, &syms); bool handled = false; for (int i = 0; i < syms_len; i++) { - if (event->state == WLR_KEY_PRESSED) { - bool keysym_handled = keyboard_keysym_press(keyboard, syms[i]); + if (state) { + bool keysym_handled = keyboard_keysym_press(keyboard, + syms[i], modifiers); handled = handled || keysym_handled; } else { // WLR_KEY_RELEASED keyboard_keysym_release(keyboard, syms[i]); } } + return handled; +} + +static bool keyboard_keysyms_xkb(struct roots_keyboard *keyboard, + uint32_t keycode, enum wlr_key_state state) +{ + uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); + const xkb_keysym_t *syms; + int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + uint32_t consumed = xkb_state_key_get_consumed_mods2( + keyboard->device->keyboard->xkb_state, + keycode, + XKB_CONSUMED_MODE_XKB); + + modifiers = modifiers & ~consumed; + + bool handled = false; + for (int i = 0; i < syms_len; i++) { + if (state) { + bool keysym_handled = keyboard_keysym_press(keyboard, + syms[i], modifiers); + handled = handled || keysym_handled; + } else { // WLR_KEY_RELEASED + keyboard_keysym_release(keyboard, syms[i]); + } + } + + return handled; +} + +static void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct wlr_event_keyboard_key *event = data; + struct roots_keyboard *keyboard = wl_container_of(listener, keyboard, key); + + uint32_t keycode = event->keycode + 8; + + bool handled = keyboard_keysyms_xkb(keyboard, keycode, event->state); + + if (!handled) + handled |= keyboard_keysyms_simple(keyboard, keycode, event->state); + if (!handled) { wlr_seat_set_keyboard(keyboard->input->wl_seat, keyboard->device); wlr_seat_keyboard_notify_key(keyboard->input->wl_seat, event->time_msec, From 8ca76ff7f27175e275cbebcc788a1e09ce831293 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Tue, 7 Nov 2017 12:55:19 +0100 Subject: [PATCH 4/7] explains 2 way keybind trying. --- rootston/keyboard.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 6b1fde86..86f10374 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -121,14 +121,21 @@ static void keyboard_keysym_release(struct roots_keyboard *keyboard, } } +/** + * Process keypresses from the keyboard as if modifiers didn't change keysyms. + * + * This avoids the xkb keysym translation based on modifiers considered pressed + * in the state and uses the list of modifiers saved on the rootston side. + * + * This will trigger the keybind: [Alt]+[Shift]+2 + */ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, uint32_t keycode, enum wlr_key_state state) { uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); const xkb_keysym_t *syms; xkb_layout_index_t layout_index = xkb_state_key_get_layout( - keyboard->device->keyboard->xkb_state, - keycode); + keyboard->device->keyboard->xkb_state, keycode); int syms_len = xkb_keymap_key_get_syms_by_level(keyboard->device->keyboard->keymap, keycode, layout_index, 0, &syms); @@ -146,6 +153,15 @@ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, return handled; } +/** + * Process keypresses from the keyboard as xkb sees them. + * + * This uses the xkb keysyms translation based on pressed modifiers and clears + * the consumed modifiers from the list of modifiers passed to keybind + * detection. + * + * (On US layout) this will trigger: [Alt]+[at] + */ static bool keyboard_keysyms_xkb(struct roots_keyboard *keyboard, uint32_t keycode, enum wlr_key_state state) { @@ -154,9 +170,7 @@ static bool keyboard_keysyms_xkb(struct roots_keyboard *keyboard, int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, keycode, &syms); uint32_t consumed = xkb_state_key_get_consumed_mods2( - keyboard->device->keyboard->xkb_state, - keycode, - XKB_CONSUMED_MODE_XKB); + keyboard->device->keyboard->xkb_state, keycode, XKB_CONSUMED_MODE_XKB); modifiers = modifiers & ~consumed; From 23991861a461acabafde3c8d63c4355320e67db3 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Tue, 7 Nov 2017 16:50:34 +0100 Subject: [PATCH 5/7] works in review comments --- rootston/keyboard.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 86f10374..6c419df8 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -121,7 +121,7 @@ static void keyboard_keysym_release(struct roots_keyboard *keyboard, } } -/** +/* * Process keypresses from the keyboard as if modifiers didn't change keysyms. * * This avoids the xkb keysym translation based on modifiers considered pressed @@ -130,8 +130,7 @@ static void keyboard_keysym_release(struct roots_keyboard *keyboard, * This will trigger the keybind: [Alt]+[Shift]+2 */ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, - uint32_t keycode, enum wlr_key_state state) -{ + uint32_t keycode, enum wlr_key_state state) { uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); const xkb_keysym_t *syms; xkb_layout_index_t layout_index = xkb_state_key_get_layout( @@ -153,7 +152,7 @@ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, return handled; } -/** +/* * Process keypresses from the keyboard as xkb sees them. * * This uses the xkb keysyms translation based on pressed modifiers and clears @@ -163,8 +162,7 @@ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, * (On US layout) this will trigger: [Alt]+[at] */ static bool keyboard_keysyms_xkb(struct roots_keyboard *keyboard, - uint32_t keycode, enum wlr_key_state state) -{ + uint32_t keycode, enum wlr_key_state state) { uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); const xkb_keysym_t *syms; int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, @@ -196,8 +194,10 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { bool handled = keyboard_keysyms_xkb(keyboard, keycode, event->state); - if (!handled) - handled |= keyboard_keysyms_simple(keyboard, keycode, event->state); + if (!handled) { + bool key_handled = keyboard_keysyms_simple(keyboard, keycode, event->state); + handled = handled || key_handled; + } if (!handled) { wlr_seat_set_keyboard(keyboard->input->wl_seat, keyboard->device); From 84d5e6bbb445c601d85653264f91d4faf333145d Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 5 Nov 2017 17:09:00 +0100 Subject: [PATCH 6/7] Send current keyboard state when entering a surface --- include/wlr/types/wlr_keyboard.h | 3 +++ types/wlr_keyboard.c | 25 +++++++++++++++++++++++++ types/wlr_seat.c | 17 +++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/include/wlr/types/wlr_keyboard.h b/include/wlr/types/wlr_keyboard.h index af837ff5..e2d50b03 100644 --- a/include/wlr/types/wlr_keyboard.h +++ b/include/wlr/types/wlr_keyboard.h @@ -28,6 +28,8 @@ enum wlr_keyboard_modifier { WLR_MODIFIER_MOD5 = 128, }; +#define WLR_KEYBOARD_KEYS_CAP 32 + struct wlr_keyboard_impl; struct wlr_keyboard { @@ -41,6 +43,7 @@ struct wlr_keyboard { xkb_led_index_t led_indexes[WLR_LED_COUNT]; xkb_mod_index_t mod_indexes[WLR_MODIFIER_COUNT]; + uint32_t keycodes[WLR_KEYBOARD_KEYS_CAP]; struct { xkb_mod_mask_t depressed; xkb_mod_mask_t latched; diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index c27264ef..98ebeed5 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -45,6 +45,30 @@ static void keyboard_modifier_update(struct wlr_keyboard *keyboard) { wl_signal_emit(&keyboard->events.modifiers, keyboard); } +static void keyboard_key_update(struct wlr_keyboard *keyboard, + struct wlr_event_keyboard_key *event) { + bool found = false; + size_t i = 0; + for (; i < WLR_KEYBOARD_KEYS_CAP; ++i) { + if (keyboard->keycodes[i] == event->keycode) { + found = true; + break; + } + } + + if (event->state == WLR_KEY_PRESSED && !found) { + for (size_t i = 0; i < WLR_KEYBOARD_KEYS_CAP; ++i) { + if (keyboard->keycodes[i] == 0) { + keyboard->keycodes[i] = event->keycode; + break; + } + } + } + if (event->state == WLR_KEY_RELEASED && found) { + keyboard->keycodes[i] = 0; + } +} + void wlr_keyboard_notify_modifiers(struct wlr_keyboard *keyboard, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) { @@ -68,6 +92,7 @@ void wlr_keyboard_notify_key(struct wlr_keyboard *keyboard, } keyboard_led_update(keyboard); keyboard_modifier_update(keyboard); + keyboard_key_update(keyboard, event); wl_signal_emit(&keyboard->events.key, event); } diff --git a/types/wlr_seat.c b/types/wlr_seat.c index 76b34d31..ae991383 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -751,13 +751,26 @@ void wlr_seat_keyboard_enter(struct wlr_seat *wlr_seat, // enter the current surface if (client && client->keyboard) { - // TODO: read the currently pressed keys out of the active keyboard and - // put them in this array + struct wlr_keyboard *keyboard = wlr_seat->keyboard_state.keyboard; + struct wl_array keys; wl_array_init(&keys); + size_t n = 0; + for (size_t i = 0; i < WLR_KEYBOARD_KEYS_CAP; ++i) { + if (keyboard->keycodes[i] != 0) { + wl_array_add(&keys, sizeof(uint32_t)); + ((uint32_t *)keys.data)[n] = keyboard->keycodes[i]; + n++; + } + } uint32_t serial = wl_display_next_serial(wlr_seat->display); wl_keyboard_send_enter(client->keyboard, serial, surface->resource, &keys); + wl_array_release(&keys); + + wlr_seat_keyboard_send_modifiers(wlr_seat, + keyboard->modifiers.depressed, keyboard->modifiers.latched, + keyboard->modifiers.locked, keyboard->modifiers.group); wlr_seat_client_send_selection(client); } From 882e6206393f1931fea3de8fd2b87b91314ed6d0 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 8 Nov 2017 19:02:56 +0100 Subject: [PATCH 7/7] Always read state from wlr_keyboard in the seat --- include/wlr/types/wlr_seat.h | 12 ++---- rootston/desktop.c | 1 - rootston/keyboard.c | 23 +++++------ types/wlr_data_device.c | 5 +-- types/wlr_seat.c | 78 +++++++++++++++++------------------- types/wlr_xdg_shell_v6.c | 7 +--- 6 files changed, 52 insertions(+), 74 deletions(-) diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index b8b467d2..09d3e282 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -45,9 +45,7 @@ struct wlr_keyboard_grab_interface { struct wlr_surface *surface); void (*key)(struct wlr_seat_keyboard_grab *grab, uint32_t time, uint32_t key, uint32_t state); - void (*modifiers)(struct wlr_seat_keyboard_grab *grab, - uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group); + void (*modifiers)(struct wlr_seat_keyboard_grab *grab); void (*cancel)(struct wlr_seat_keyboard_grab *grab); }; @@ -296,17 +294,13 @@ void wlr_seat_keyboard_notify_key(struct wlr_seat *seat, uint32_t time, * Send the modifier state to focused keyboard resources. Compositors should use * `wlr_seat_keyboard_notify_modifiers()` to respect any keyboard grabs. */ -void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat, - uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, - uint32_t group); +void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat); /** * Notify the seat that the modifiers for the keyboard have changed. Defers to * any keyboard grabs. */ -void wlr_seat_keyboard_notify_modifiers(struct wlr_seat *seat, - uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, - uint32_t group); +void wlr_seat_keyboard_notify_modifiers(struct wlr_seat *seat); /** * Notify the seat that the keyboard focus has changed and request it to be the diff --git a/rootston/desktop.c b/rootston/desktop.c index 29f78ac7..b36a6932 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -133,7 +133,6 @@ void view_teardown(struct roots_view *view) { struct roots_view *prev_view = views->items[views->length-2]; struct roots_input *input = prev_view->desktop->server->input; set_view_focus(input, prev_view->desktop, prev_view); - wlr_seat_keyboard_notify_enter(input->wl_seat, prev_view->wlr_surface); } struct roots_view *view_at(struct roots_desktop *desktop, double lx, double ly, diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 6c419df8..bde912aa 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -135,8 +135,8 @@ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, const xkb_keysym_t *syms; xkb_layout_index_t layout_index = xkb_state_key_get_layout( keyboard->device->keyboard->xkb_state, keycode); - int syms_len = xkb_keymap_key_get_syms_by_level(keyboard->device->keyboard->keymap, - keycode, layout_index, 0, &syms); + int syms_len = xkb_keymap_key_get_syms_by_level( + keyboard->device->keyboard->keymap, keycode, layout_index, 0, &syms); bool handled = false; for (int i = 0; i < syms_len; i++) { @@ -195,7 +195,8 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { bool handled = keyboard_keysyms_xkb(keyboard, keycode, event->state); if (!handled) { - bool key_handled = keyboard_keysyms_simple(keyboard, keycode, event->state); + bool key_handled = keyboard_keysyms_simple(keyboard, keycode, + event->state); handled = handled || key_handled; } @@ -207,17 +208,11 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { } static void keyboard_modifiers_notify(struct wl_listener *listener, void *data) { - struct roots_keyboard *r_keyboard = - wl_container_of(listener, r_keyboard, modifiers); - struct wlr_seat *seat = r_keyboard->input->wl_seat; - struct wlr_keyboard *keyboard = r_keyboard->device->keyboard; - wlr_seat_set_keyboard(seat, r_keyboard->device); - wlr_seat_keyboard_notify_modifiers(seat, - keyboard->modifiers.depressed, - keyboard->modifiers.latched, - keyboard->modifiers.locked, - keyboard->modifiers.group); - + struct roots_keyboard *keyboard = + wl_container_of(listener, keyboard, modifiers); + struct wlr_seat *seat = keyboard->input->wl_seat; + wlr_seat_set_keyboard(seat, keyboard->device); + wlr_seat_keyboard_notify_modifiers(seat); } static void keyboard_config_merge(struct keyboard_config *config, diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index ee83260e..ea28ef50 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -531,9 +531,8 @@ static void keyboard_drag_key(struct wlr_seat_keyboard_grab *grab, // no keyboard input during drags } -static void keyboard_drag_modifiers(struct wlr_seat_keyboard_grab *grab, - uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group) { +static void keyboard_drag_modifiers(struct wlr_seat_keyboard_grab *grab) { + //struct wlr_keyboard *keyboard = grab->seat->keyboard_state.keyboard; // TODO change the dnd action based on what modifier is pressed on the // keyboard } diff --git a/types/wlr_seat.c b/types/wlr_seat.c index ae991383..9de1b3a0 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -284,11 +284,8 @@ static void default_keyboard_key(struct wlr_seat_keyboard_grab *grab, wlr_seat_keyboard_send_key(grab->seat, time, key, state); } -static void default_keyboard_modifiers(struct wlr_seat_keyboard_grab *grab, - uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group) { - wlr_seat_keyboard_send_modifiers(grab->seat, mods_depressed, - mods_latched, mods_locked, group); +static void default_keyboard_modifiers(struct wlr_seat_keyboard_grab *grab) { + wlr_seat_keyboard_send_modifiers(grab->seat); } static void default_keyboard_cancel(struct wlr_seat_keyboard_grab *grab) { @@ -708,24 +705,26 @@ static void keyboard_resource_destroy_notify(struct wl_listener *listener, wlr_seat_keyboard_clear_focus(state->seat); } -void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat, - uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, - uint32_t group) { +void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat) { struct wlr_seat_client *client = seat->keyboard_state.focused_client; if (!client || !client->keyboard) { return; } - uint32_t serial = wl_display_next_serial(seat->display); + struct wlr_keyboard *keyboard = seat->keyboard_state.keyboard; + if (!keyboard) { + return; + } + uint32_t serial = wl_display_next_serial(seat->display); wl_keyboard_send_modifiers(client->keyboard, serial, - mods_depressed, mods_latched, - mods_locked, group); + keyboard->modifiers.depressed, keyboard->modifiers.latched, + keyboard->modifiers.locked, keyboard->modifiers.group); } -void wlr_seat_keyboard_enter(struct wlr_seat *wlr_seat, +void wlr_seat_keyboard_enter(struct wlr_seat *seat, struct wlr_surface *surface) { - if (wlr_seat->keyboard_state.focused_surface == surface) { + if (seat->keyboard_state.focused_surface == surface) { // this surface already got an enter notify return; } @@ -734,24 +733,24 @@ void wlr_seat_keyboard_enter(struct wlr_seat *wlr_seat, if (surface) { struct wl_client *wl_client = wl_resource_get_client(surface->resource); - client = wlr_seat_client_for_wl_client(wlr_seat, wl_client); + client = wlr_seat_client_for_wl_client(seat, wl_client); } struct wlr_seat_client *focused_client = - wlr_seat->keyboard_state.focused_client; + seat->keyboard_state.focused_client; struct wlr_surface *focused_surface = - wlr_seat->keyboard_state.focused_surface; + seat->keyboard_state.focused_surface; // leave the previously entered surface if (focused_client && focused_client->keyboard && focused_surface) { - uint32_t serial = wl_display_next_serial(wlr_seat->display); + uint32_t serial = wl_display_next_serial(seat->display); wl_keyboard_send_leave(focused_client->keyboard, serial, focused_surface->resource); } // enter the current surface - if (client && client->keyboard) { - struct wlr_keyboard *keyboard = wlr_seat->keyboard_state.keyboard; + if (client && client->keyboard && seat->keyboard_state.keyboard) { + struct wlr_keyboard *keyboard = seat->keyboard_state.keyboard; struct wl_array keys; wl_array_init(&keys); @@ -763,55 +762,50 @@ void wlr_seat_keyboard_enter(struct wlr_seat *wlr_seat, n++; } } - uint32_t serial = wl_display_next_serial(wlr_seat->display); + uint32_t serial = wl_display_next_serial(seat->display); wl_keyboard_send_enter(client->keyboard, serial, surface->resource, &keys); wl_array_release(&keys); - wlr_seat_keyboard_send_modifiers(wlr_seat, - keyboard->modifiers.depressed, keyboard->modifiers.latched, - keyboard->modifiers.locked, keyboard->modifiers.group); + wlr_seat_keyboard_send_modifiers(seat); wlr_seat_client_send_selection(client); } // reinitialize the focus destroy events - wl_list_remove(&wlr_seat->keyboard_state.surface_destroy.link); - wl_list_init(&wlr_seat->keyboard_state.surface_destroy.link); - wl_list_remove(&wlr_seat->keyboard_state.resource_destroy.link); - wl_list_init(&wlr_seat->keyboard_state.resource_destroy.link); + wl_list_remove(&seat->keyboard_state.surface_destroy.link); + wl_list_init(&seat->keyboard_state.surface_destroy.link); + wl_list_remove(&seat->keyboard_state.resource_destroy.link); + wl_list_init(&seat->keyboard_state.resource_destroy.link); if (surface) { wl_signal_add(&surface->events.destroy, - &wlr_seat->keyboard_state.surface_destroy); + &seat->keyboard_state.surface_destroy); wl_resource_add_destroy_listener(surface->resource, - &wlr_seat->keyboard_state.resource_destroy); - wlr_seat->keyboard_state.resource_destroy.notify = + &seat->keyboard_state.resource_destroy); + seat->keyboard_state.resource_destroy.notify = keyboard_resource_destroy_notify; - wlr_seat->keyboard_state.surface_destroy.notify = + seat->keyboard_state.surface_destroy.notify = keyboard_surface_destroy_notify; } - wlr_seat->keyboard_state.focused_client = client; - wlr_seat->keyboard_state.focused_surface = surface; + seat->keyboard_state.focused_client = client; + seat->keyboard_state.focused_surface = surface; } -void wlr_seat_keyboard_notify_enter(struct wlr_seat *wlr_seat, struct +void wlr_seat_keyboard_notify_enter(struct wlr_seat *seat, struct wlr_surface *surface) { - struct wlr_seat_keyboard_grab *grab = wlr_seat->keyboard_state.grab; + struct wlr_seat_keyboard_grab *grab = seat->keyboard_state.grab; grab->interface->enter(grab, surface); } -void wlr_seat_keyboard_clear_focus(struct wlr_seat *wlr_seat) { +void wlr_seat_keyboard_clear_focus(struct wlr_seat *seat) { struct wl_array keys; wl_array_init(&keys); - wlr_seat_keyboard_enter(wlr_seat, NULL); + wlr_seat_keyboard_enter(seat, NULL); } -void wlr_seat_keyboard_notify_modifiers(struct wlr_seat *seat, - uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, - uint32_t group) { +void wlr_seat_keyboard_notify_modifiers(struct wlr_seat *seat) { struct wlr_seat_keyboard_grab *grab = seat->keyboard_state.grab; - grab->interface->modifiers(grab, - mods_depressed, mods_latched, mods_locked, group); + grab->interface->modifiers(grab); } void wlr_seat_keyboard_notify_key(struct wlr_seat *seat, uint32_t time, diff --git a/types/wlr_xdg_shell_v6.c b/types/wlr_xdg_shell_v6.c index fc45bc17..ef4ae42e 100644 --- a/types/wlr_xdg_shell_v6.c +++ b/types/wlr_xdg_shell_v6.c @@ -111,11 +111,8 @@ static void xdg_keyboard_grab_key(struct wlr_seat_keyboard_grab *grab, uint32_t wlr_seat_keyboard_send_key(grab->seat, time, key, state); } -static void xdg_keyboard_grab_modifiers(struct wlr_seat_keyboard_grab *grab, - uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group) { - wlr_seat_keyboard_send_modifiers(grab->seat, mods_depressed, mods_latched, - mods_locked, group); +static void xdg_keyboard_grab_modifiers(struct wlr_seat_keyboard_grab *grab) { + wlr_seat_keyboard_send_modifiers(grab->seat); } static void xdg_keyboard_grab_cancel(struct wlr_seat_keyboard_grab *grab) {