From f394d03600799e442c241f3235556b3502690a93 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Sat, 19 Oct 2024 20:02:08 -0400 Subject: [PATCH] session-lock: make session_lock_output_create() safe to call twice session_lock_output_create() can safely no-op if the lock output has already been created for the specified output. This scenario doesn't happen currently, and the change is in preparation for some other output-related changes I am working on. But I think it's a nice code improvement worth merging separately. --- src/session-lock.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/session-lock.c b/src/session-lock.c index bdf3f053..edad8f64 100644 --- a/src/session-lock.c +++ b/src/session-lock.c @@ -101,6 +101,19 @@ lock_output_reconfigure(struct session_lock_output *output) } } +static struct session_lock_output * +lock_output_for_output(struct session_lock_manager *manager, + struct output *output) +{ + struct session_lock_output *lock_output; + wl_list_for_each(lock_output, &manager->lock_outputs, link) { + if (lock_output->output == output) { + return lock_output; + } + } + return NULL; +} + static void handle_new_surface(struct wl_listener *listener, void *data) { @@ -108,17 +121,14 @@ handle_new_surface(struct wl_listener *listener, void *data) wl_container_of(listener, manager, lock_new_surface); struct wlr_session_lock_surface_v1 *lock_surface = data; struct output *output = lock_surface->output->data; - struct session_lock_output *lock_output; - wl_list_for_each(lock_output, &manager->lock_outputs, link) { - if (lock_output->output == output) { - goto found_lock_output; - } + struct session_lock_output *lock_output = + lock_output_for_output(manager, output); + if (!lock_output) { + wlr_log(WLR_ERROR, "new lock surface, but no output"); + /* TODO: Consider improving security by handling this better */ + return; } - wlr_log(WLR_ERROR, "new lock surface, but no output"); - /* TODO: Consider improving security by handling this better */ - return; -found_lock_output: lock_output->surface = lock_surface; struct wlr_scene_tree *surface_tree = wlr_scene_subsurface_tree_create(lock_output->tree, lock_surface->surface); @@ -188,6 +198,10 @@ handle_output_blank_timeout(void *data) void session_lock_output_create(struct session_lock_manager *manager, struct output *output) { + if (lock_output_for_output(manager, output)) { + return; /* already created */ + } + struct session_lock_output *lock_output = znew(*lock_output); struct wlr_scene_tree *tree = wlr_scene_tree_create(output->session_lock_tree); -- 2.52.0