input/seat: unset has_focus when focus_stack becomes empty

We currently track the focus of a seat in two ways: we use a list called
focus_stack to track the order in which nodes have been focused, with
the first node representing what's currently focused, and we use a
variable called has_focus to indicate whether anything has focus--i.e.
whether we should actually treat that first node as focused at any given
time.

In a number of places, we treat has_focus as implying that a focused
node exists. If it's true, we attempt to dereference the return value of
seat_get_focus(), our helper function for getting the first node in
focus_list, with no further checks. But this isn't quite correct with
the current implementation of seat_get_focus(): not only does it return
NULL when has_focus is false, it also returns NULL when focus_stack
contains no items.

In most cases, focus_stack never becomes empty and so this doesn't
matter at all. Since focus_stack stores a history of focused nodes, we
rarely remove nodes from it. The exception to this is when a node itself
goes away. In that case, we call seat_node_destroy() to remove it from
focus_stack and free it. But we don't unset has_focus if we've removed
the final node! This lets us get into a state where has_focus is true
but seat_get_focus() returns NULL, leading to a segfault when we try to
dereference it.

Fix the issue both by updating has_focus in seat_node_destroy() and by
adding an assertion in seat_get_focus() that ensures focus_stack and
has_focus are in sync, which will make it easier to track down similar
issues in the future.

Fixes #6395.

[1] There's some discussion in #1585 from when this was implemented
about whether has_focus is actually necessary; it's possible we could
remove it entirely, but for the moment this is the architecture we have.

(cherry picked from commit 921b0a8633)
This commit is contained in:
Thomas Hebb 2022-01-06 03:44:55 -08:00 committed by Simon Ser
parent 0e5dda3747
commit a235794a96
1 changed files with 12 additions and 3 deletions

View File

@ -51,6 +51,16 @@ static void seat_device_destroy(struct sway_seat_device *seat_device) {
static void seat_node_destroy(struct sway_seat_node *seat_node) {
wl_list_remove(&seat_node->destroy.link);
wl_list_remove(&seat_node->link);
/*
* This is the only time we remove items from the focus stack without
* immediately re-adding them. If we just removed the last thing,
* mark that nothing has focus anymore.
*/
if (wl_list_empty(&seat_node->seat->focus_stack)) {
seat_node->seat->has_focus = false;
}
free(seat_node);
}
@ -1415,9 +1425,8 @@ struct sway_node *seat_get_focus(struct sway_seat *seat) {
if (!seat->has_focus) {
return NULL;
}
if (wl_list_empty(&seat->focus_stack)) {
return NULL;
}
sway_assert(!wl_list_empty(&seat->focus_stack),
"focus_stack is empty, but has_focus is true");
struct sway_seat_node *current =
wl_container_of(seat->focus_stack.next, current, link);
return current->node;