text_input: Properly handle map/unmap events

The last implementation would ignore these and get it could get into
a bad state where it would start crashing sway.
This commit is contained in:
Alexander Orzechowski 2024-08-04 13:03:59 -04:00 committed by Simon Ser
parent 023f6b0a50
commit 74e507962e
2 changed files with 57 additions and 9 deletions

View file

@ -15,6 +15,8 @@ struct sway_input_popup {
struct wl_listener popup_destroy;
struct wl_listener popup_surface_commit;
struct wl_listener popup_surface_map;
struct wl_listener popup_surface_unmap;
struct wl_listener focused_surface_unmap;
};

View file

@ -132,6 +132,9 @@ static void constrain_popup(struct sway_input_popup *popup) {
struct sway_text_input *text_input =
relay_get_focused_text_input(popup->relay);
if (!popup->desc.relative) {
return;
}
struct wlr_box parent = {0};
wlr_scene_node_coords(&popup->desc.relative->parent->node, &parent.x, &parent.y);
@ -199,7 +202,10 @@ static void constrain_popup(struct sway_input_popup *popup) {
wlr_input_popup_surface_v2_send_text_input_rectangle(
popup->popup_surface, &box);
}
if (popup->scene_tree) {
wlr_scene_node_set_position(&popup->scene_tree->node, x - geo.x, y - geo.y);
}
}
static void relay_send_im_state(struct sway_input_method_relay *relay,
@ -376,7 +382,6 @@ static void input_popup_update(struct sway_input_popup *popup) {
wlr_layer_surface_v1_try_from_wlr_surface(focused_surface);
struct wlr_scene_tree *relative_parent;
popup->scene_tree = wlr_scene_subsurface_tree_create(root->layers.popup, popup->popup_surface->surface);
if (layer_surface != NULL) {
struct sway_layer_surface *layer = layer_surface->data;
if (layer == NULL) {
@ -435,19 +440,43 @@ static void input_popup_set_focus(struct sway_input_popup *popup,
static void handle_im_popup_destroy(struct wl_listener *listener, void *data) {
struct sway_input_popup *popup =
wl_container_of(listener, popup, popup_destroy);
wlr_scene_node_destroy(&popup->scene_tree->node);
wl_list_remove(&popup->focused_surface_unmap.link);
wl_list_remove(&popup->popup_surface_commit.link);
wl_list_remove(&popup->popup_surface_map.link);
wl_list_remove(&popup->popup_surface_unmap.link);
wl_list_remove(&popup->popup_destroy.link);
wl_list_remove(&popup->link);
free(popup);
}
static void handle_im_popup_surface_commit(struct wl_listener *listener,
void *data) {
static void handle_im_popup_surface_map(struct wl_listener *listener, void *data) {
struct sway_input_popup *popup =
wl_container_of(listener, popup, popup_surface_map);
struct sway_text_input *text_input = relay_get_focused_text_input(popup->relay);
if (text_input != NULL) {
input_popup_set_focus(popup, text_input->input->focused_surface);
} else {
input_popup_set_focus(popup, NULL);
}
}
static void handle_im_popup_surface_unmap(struct wl_listener *listener, void *data) {
struct sway_input_popup *popup =
wl_container_of(listener, popup, popup_surface_unmap);
// relative should already be freed as it should be a child of the just unmapped scene
popup->desc.relative = NULL;
input_popup_set_focus(popup, NULL);
}
static void handle_im_popup_surface_commit(struct wl_listener *listener, void *data) {
struct sway_input_popup *popup =
wl_container_of(listener, popup, popup_surface_commit);
input_popup_update(popup);
constrain_popup(popup);
}
static void handle_im_focused_surface_unmap(
@ -471,12 +500,29 @@ static void handle_im_new_popup_surface(struct wl_listener *listener,
popup->popup_surface = data;
popup->popup_surface->data = popup;
wl_signal_add(
&popup->popup_surface->events.destroy, &popup->popup_destroy);
popup->scene_tree = wlr_scene_tree_create(root->layers.popup);
if (!popup->scene_tree) {
sway_log(SWAY_ERROR, "Failed to allocate scene tree");
free(popup);
return;
}
if (!wlr_scene_subsurface_tree_create(popup->scene_tree,
popup->popup_surface->surface)) {
sway_log(SWAY_ERROR, "Failed to allocate subsurface tree");
wlr_scene_node_destroy(&popup->scene_tree->node);
free(popup);
return;
}
wl_signal_add(&popup->popup_surface->events.destroy, &popup->popup_destroy);
popup->popup_destroy.notify = handle_im_popup_destroy;
wl_signal_add(&popup->popup_surface->surface->events.commit,
&popup->popup_surface_commit);
wl_signal_add(&popup->popup_surface->surface->events.commit, &popup->popup_surface_commit);
popup->popup_surface_commit.notify = handle_im_popup_surface_commit;
wl_signal_add(&popup->popup_surface->surface->events.map, &popup->popup_surface_map);
popup->popup_surface_map.notify = handle_im_popup_surface_map;
wl_signal_add(&popup->popup_surface->surface->events.unmap, &popup->popup_surface_unmap);
popup->popup_surface_unmap.notify = handle_im_popup_surface_unmap;
wl_list_init(&popup->focused_surface_unmap.link);
popup->focused_surface_unmap.notify = handle_im_focused_surface_unmap;