]> git.mdlowis.com Git - proto/labwc.git/commitdiff
session-lock: make session_lock_output_create() safe to call twice
authorJohn Lindgren <john@jlindgren.net>
Sun, 20 Oct 2024 00:02:08 +0000 (20:02 -0400)
committerJohan Malm <johanmalm@users.noreply.github.com>
Tue, 29 Oct 2024 21:41:48 +0000 (21:41 +0000)
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

index bdf3f05310336e515351da8140ff194b44f83f72..edad8f649abb9bac940002fd7418c7f7e871f4f3 100644 (file)
@@ -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);