From eb3e8f08a8bb17f9b6260015b901cacbb9b0ce94 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 1 Feb 2023 20:14:39 +0100 Subject: [PATCH] subcompositor: convert to try_from References: https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/884 --- include/wlr/types/wlr_subcompositor.h | 13 +++------ types/wlr_compositor.c | 8 ++---- types/wlr_subcompositor.c | 41 +++++++++++---------------- 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/include/wlr/types/wlr_subcompositor.h b/include/wlr/types/wlr_subcompositor.h index afbff45b..c292ea0c 100644 --- a/include/wlr/types/wlr_subcompositor.h +++ b/include/wlr/types/wlr_subcompositor.h @@ -62,18 +62,13 @@ struct wlr_subcompositor { } events; }; -/** - * Returns true if the surface has the subsurface role. - */ -bool wlr_surface_is_subsurface(struct wlr_surface *surface); - /** * Get a struct wlr_subsurface from a struct wlr_surface. - * Asserts that the surface has the subsurface role. - * May return NULL even if the surface has the subsurface role if the - * corresponding subsurface has been destroyed. + * + * Returns NULL if the surface doesn't have the subsurface role or if + * the subsurface has been destroyed. */ -struct wlr_subsurface *wlr_subsurface_from_wlr_surface( +struct wlr_subsurface *wlr_subsurface_try_from_wlr_surface( struct wlr_surface *surface); struct wlr_subcompositor *wlr_subcompositor_create(struct wl_display *display); diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index 83c98ffc..f43641a2 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -812,12 +812,8 @@ void wlr_surface_unlock_cached(struct wlr_surface *surface, uint32_t seq) { } struct wlr_surface *wlr_surface_get_root_surface(struct wlr_surface *surface) { - while (wlr_surface_is_subsurface(surface)) { - struct wlr_subsurface *subsurface = - wlr_subsurface_from_wlr_surface(surface); - if (subsurface == NULL) { - break; - } + struct wlr_subsurface *subsurface; + while ((subsurface = wlr_subsurface_try_from_wlr_surface(surface))) { surface = subsurface->parent; } return surface; diff --git a/types/wlr_subcompositor.c b/types/wlr_subcompositor.c index 6e8a67a6..db4883e2 100644 --- a/types/wlr_subcompositor.c +++ b/types/wlr_subcompositor.c @@ -16,10 +16,7 @@ static bool subsurface_is_synchronized(struct wlr_subsurface *subsurface) { return true; } - if (!wlr_surface_is_subsurface(subsurface->parent)) { - break; - } - subsurface = wlr_subsurface_from_wlr_surface(subsurface->parent); + subsurface = wlr_subsurface_try_from_wlr_surface(subsurface->parent); } return false; @@ -197,12 +194,10 @@ static void subsurface_consider_map(struct wlr_subsurface *subsurface) { return; } - if (wlr_surface_is_subsurface(subsurface->parent)) { - struct wlr_subsurface *parent = - wlr_subsurface_from_wlr_surface(subsurface->parent); - if (parent == NULL || !parent->mapped) { - return; - } + struct wlr_subsurface *parent = + wlr_subsurface_try_from_wlr_surface(subsurface->parent); + if (parent == NULL || !parent->mapped) { + return; } // Now we can map the subsurface @@ -242,16 +237,16 @@ static void subsurface_unmap(struct wlr_subsurface *subsurface) { } static void subsurface_role_commit(struct wlr_surface *surface) { - struct wlr_subsurface *subsurface = - wlr_subsurface_from_wlr_surface(surface); + struct wlr_subsurface *subsurface = wlr_subsurface_try_from_wlr_surface(surface); + assert(subsurface != NULL); subsurface_consider_map(subsurface); } static void subsurface_role_precommit(struct wlr_surface *surface, const struct wlr_surface_state *state) { - struct wlr_subsurface *subsurface = - wlr_subsurface_from_wlr_surface(surface); + struct wlr_subsurface *subsurface = wlr_subsurface_try_from_wlr_surface(surface); + assert(subsurface != NULL); if (state->committed & WLR_SURFACE_STATE_BUFFER && state->buffer == NULL) { // This is a NULL commit @@ -260,8 +255,8 @@ static void subsurface_role_precommit(struct wlr_surface *surface, } static void subsurface_role_destroy(struct wlr_surface *surface) { - struct wlr_subsurface *subsurface = - wlr_subsurface_from_wlr_surface(surface); + struct wlr_subsurface *subsurface = wlr_subsurface_try_from_wlr_surface(surface); + assert(subsurface != NULL); if (subsurface->has_cache) { wlr_surface_unlock_cached(subsurface->surface, @@ -403,13 +398,10 @@ static struct wlr_subsurface *subsurface_create(struct wlr_surface *surface, return subsurface; } -bool wlr_surface_is_subsurface(struct wlr_surface *surface) { - return surface->role == &subsurface_role; -} - -struct wlr_subsurface *wlr_subsurface_from_wlr_surface( - struct wlr_surface *surface) { - assert(wlr_surface_is_subsurface(surface)); +struct wlr_subsurface *wlr_subsurface_try_from_wlr_surface(struct wlr_surface *surface) { + if (surface->role != &subsurface_role) { + return NULL; + } return (struct wlr_subsurface *)surface->role_data; } @@ -435,8 +427,7 @@ static void subcompositor_handle_get_subsurface(struct wl_client *client, return; } - if (wlr_surface_is_subsurface(surface) && - wlr_subsurface_from_wlr_surface(surface) != NULL) { + if (wlr_subsurface_try_from_wlr_surface(surface) != NULL) { wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE, "%s%" PRIu32 ": wl_surface@%" PRIu32 " is already a sub-surface",