handle container destroy in the seat

This commit is contained in:
Tony Crisci 2018-03-31 15:22:10 -04:00
parent e7ecb001d7
commit 6f7b33e691
3 changed files with 88 additions and 32 deletions

View file

@ -150,4 +150,7 @@ void container_for_each_descendant_dfs(struct sway_container *container,
bool container_has_anscestor(struct sway_container *descendant, bool container_has_anscestor(struct sway_container *descendant,
struct sway_container *anscestor); struct sway_container *anscestor);
bool container_has_child(struct sway_container *con,
struct sway_container *child);
#endif #endif

View file

@ -27,6 +27,7 @@ static void seat_device_destroy(struct sway_seat_device *seat_device) {
} }
void sway_seat_destroy(struct sway_seat *seat) { void sway_seat_destroy(struct sway_seat *seat) {
// TODO destroy seat containers
struct sway_seat_device *seat_device, *next; struct sway_seat_device *seat_device, *next;
wl_list_for_each_safe(seat_device, next, &seat->devices, link) { wl_list_for_each_safe(seat_device, next, &seat->devices, link) {
seat_device_destroy(seat_device); seat_device_destroy(seat_device);
@ -36,30 +37,57 @@ void sway_seat_destroy(struct sway_seat *seat) {
wlr_seat_destroy(seat->wlr_seat); wlr_seat_destroy(seat->wlr_seat);
} }
static struct sway_seat_container *seat_container_from_container(
struct sway_seat *seat, struct sway_container *con);
static void seat_container_destroy(struct sway_seat_container *seat_con) {
struct sway_container *con = seat_con->container;
struct sway_container *child = NULL;
if (con->children != NULL) {
for (int i = 0; i < con->children->length; ++i) {
child = con->children->items[i];
struct sway_seat_container *seat_child =
seat_container_from_container(seat_con->seat, child);
seat_container_destroy(seat_child);
}
}
wl_list_remove(&seat_con->destroy.link);
wl_list_remove(&seat_con->link);
free(seat_con);
}
static void handle_seat_container_destroy(struct wl_listener *listener, static void handle_seat_container_destroy(struct wl_listener *listener,
void *data) { void *data) {
struct sway_seat_container *seat_con = struct sway_seat_container *seat_con =
wl_container_of(listener, seat_con, destroy); wl_container_of(listener, seat_con, destroy);
struct sway_seat *seat = seat_con->seat; struct sway_seat *seat = seat_con->seat;
struct sway_container *con = seat_con->container; struct sway_container *con = seat_con->container;
struct sway_container *parent = con->parent;
struct sway_container *focus = sway_seat_get_focus(seat);
bool is_focus = (sway_seat_get_focus(seat) == con); // TODO handle workspace switch in the seat?
bool set_focus =
(focus == con || container_has_child(con, focus)) &&
con->type != C_WORKSPACE;
wl_list_remove(&seat_con->link); seat_container_destroy(seat_con);
if (is_focus) { if (set_focus && con->type != C_WORKSPACE) {
// pick next focus struct sway_container *next_focus = NULL;
sway_seat_set_focus(seat, NULL); while (next_focus == NULL) {
struct sway_container *next = sway_seat_get_focus_inactive(seat, con->parent); next_focus = sway_seat_get_focus_by_type(seat, parent, C_VIEW);
if (next == NULL) { parent = parent->parent;
next = con->parent;
if (next_focus == NULL && parent->type == C_WORKSPACE) {
next_focus = parent;
break;
} }
sway_seat_set_focus(seat, next);
} }
wl_list_remove(&seat_con->destroy.link); sway_seat_set_focus(seat, next_focus);
}
free(seat_con);
} }
static struct sway_seat_container *seat_container_from_container( static struct sway_seat_container *seat_container_from_container(
@ -293,11 +321,37 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) {
seat->cursor->cursor->y); seat->cursor->cursor->y);
} }
static void seat_send_focus(struct sway_seat *seat,
struct sway_container *con) {
if (con->type != C_VIEW) {
return;
}
struct sway_view *view = con->sway_view;
if (view->type == SWAY_XWAYLAND_VIEW) {
struct wlr_xwayland *xwayland =
seat->input->server->xwayland;
wlr_xwayland_set_seat(xwayland, seat->wlr_seat);
}
view_set_activated(view, true);
struct wlr_keyboard *keyboard =
wlr_seat_get_keyboard(seat->wlr_seat);
if (keyboard) {
wlr_seat_keyboard_notify_enter(seat->wlr_seat,
view->surface, keyboard->keycodes,
keyboard->num_keycodes, &keyboard->modifiers);
} else {
wlr_seat_keyboard_notify_enter(
seat->wlr_seat, view->surface, NULL, 0, NULL);
}
}
void sway_seat_set_focus_warp(struct sway_seat *seat, void sway_seat_set_focus_warp(struct sway_seat *seat,
struct sway_container *container, bool warp) { struct sway_container *container, bool warp) {
struct sway_container *last_focus = sway_seat_get_focus(seat); struct sway_container *last_focus = sway_seat_get_focus(seat);
if (container && last_focus == container) { if (container && last_focus == container) {
seat_send_focus(seat, container);
return; return;
} }
@ -312,23 +366,7 @@ void sway_seat_set_focus_warp(struct sway_seat *seat,
wl_list_insert(&seat->focus_stack, &seat_con->link); wl_list_insert(&seat->focus_stack, &seat_con->link);
if (container->type == C_VIEW) { if (container->type == C_VIEW) {
struct sway_view *view = container->sway_view; seat_send_focus(seat, container);
view_set_activated(view, true);
if (view->type == SWAY_XWAYLAND_VIEW) {
struct wlr_xwayland *xwayland =
seat->input->server->xwayland;
wlr_xwayland_set_seat(xwayland, seat->wlr_seat);
}
struct wlr_keyboard *keyboard =
wlr_seat_get_keyboard(seat->wlr_seat);
if (keyboard) {
wlr_seat_keyboard_notify_enter(seat->wlr_seat,
view->surface, keyboard->keycodes,
keyboard->num_keycodes, &keyboard->modifiers);
} else {
wlr_seat_keyboard_notify_enter(
seat->wlr_seat, view->surface, NULL, 0, NULL);
}
} }
} }
@ -378,7 +416,8 @@ void sway_seat_set_focus(struct sway_seat *seat,
sway_seat_set_focus_warp(seat, container, true); sway_seat_set_focus_warp(seat, container, true);
} }
struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) { struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat,
struct sway_container *container) {
return sway_seat_get_focus_by_type(seat, container, C_TYPES); return sway_seat_get_focus_by_type(seat, container, C_TYPES);
} }
@ -401,7 +440,8 @@ struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat,
} }
while (parent) { while (parent) {
if (parent == container && (type == C_TYPES || current->container->type == type)) { if (parent == container && (type == C_TYPES ||
current->container->type == type)) {
return current->container; return current->container;
} }
parent = parent->parent; parent = parent->parent;

View file

@ -401,3 +401,16 @@ bool container_has_anscestor(struct sway_container *descendant,
} }
return false; return false;
} }
bool find_child_func(struct sway_container *con, void *data) {
struct sway_container *child = data;
return con == child;
}
bool container_has_child(struct sway_container *con,
struct sway_container *child) {
if (child->type == C_VIEW || child->children->length == 0) {
return false;
}
return container_find(con, find_child_func, child);
}