diff --git a/include/wlr/types/wlr_xdg_shell.h b/include/wlr/types/wlr_xdg_shell.h index d017a2ae4..d1072d0d1 100644 --- a/include/wlr/types/wlr_xdg_shell.h +++ b/include/wlr/types/wlr_xdg_shell.h @@ -480,19 +480,13 @@ struct wlr_surface *wlr_xdg_surface_popup_surface_at( struct wlr_xdg_surface *surface, double sx, double sy, double *sub_x, double *sub_y); -/** - * Returns true if the surface has the xdg surface role. - */ -bool wlr_surface_is_xdg_surface(struct wlr_surface *surface); - /** * Get a struct wlr_xdg_surface from a struct wlr_surface. - * Asserts that the surface has the xdg surface role. - * May return NULL even if the surface has the xdg surface role if the - * corresponding xdg surface has been destroyed. + * + * Returns NULL if the surface doesn't have the xdg_surface role or + * if the xdg_surface has been destroyed. */ -struct wlr_xdg_surface *wlr_xdg_surface_from_wlr_surface( - struct wlr_surface *surface); +struct wlr_xdg_surface *wlr_xdg_surface_try_from_wlr_surface(struct wlr_surface *surface); /** * Get the surface geometry. diff --git a/tinywl/tinywl.c b/tinywl/tinywl.c index d3ff2d091..45b418775 100644 --- a/tinywl/tinywl.c +++ b/tinywl/tinywl.c @@ -119,9 +119,9 @@ static void focus_view(struct tinywl_view *view, struct wlr_surface *surface) { * it no longer has focus and the client will repaint accordingly, e.g. * stop displaying a caret. */ - struct wlr_xdg_surface *previous = wlr_xdg_surface_from_wlr_surface( - seat->keyboard_state.focused_surface); - assert(previous->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL); + struct wlr_xdg_surface *previous = + wlr_xdg_surface_try_from_wlr_surface(seat->keyboard_state.focused_surface); + assert(previous != NULL && previous->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL); wlr_xdg_toplevel_set_activated(previous->toplevel, false); } struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat); @@ -773,8 +773,9 @@ static void server_new_xdg_surface(struct wl_listener *listener, void *data) { * we always set the user data field of xdg_surfaces to the corresponding * scene node. */ if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) { - struct wlr_xdg_surface *parent = wlr_xdg_surface_from_wlr_surface( - xdg_surface->popup->parent); + struct wlr_xdg_surface *parent = + wlr_xdg_surface_try_from_wlr_surface(xdg_surface->popup->parent); + assert(parent != NULL); struct wlr_scene_tree *parent_tree = parent->data; xdg_surface->data = wlr_scene_xdg_surface_create( parent_tree, xdg_surface); diff --git a/types/wlr_xdg_foreign_v1.c b/types/wlr_xdg_foreign_v1.c index 9cca77149..a2d5c7927 100644 --- a/types/wlr_xdg_foreign_v1.c +++ b/types/wlr_xdg_foreign_v1.c @@ -29,13 +29,7 @@ static void xdg_imported_handle_destroy(struct wl_client *client, static struct wlr_xdg_toplevel *verify_is_toplevel(struct wl_resource *resource, struct wlr_surface *surface) { - if (!wlr_surface_is_xdg_surface(surface)) { - wl_resource_post_error(resource, -1, "surface must be an xdg_surface"); - return NULL; - } - - struct wlr_xdg_surface *xdg_surface = - wlr_xdg_surface_from_wlr_surface(surface); + struct wlr_xdg_surface *xdg_surface = wlr_xdg_surface_try_from_wlr_surface(surface); if (xdg_surface == NULL || xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) { wl_resource_post_error(resource, -1, "surface must be an xdg_toplevel"); return NULL; @@ -84,7 +78,7 @@ static void xdg_imported_handle_set_parent_of(struct wl_client *client, } struct wlr_xdg_surface *surface = - wlr_xdg_surface_from_wlr_surface(wlr_surface); + wlr_xdg_surface_try_from_wlr_surface(wlr_surface); if (!surface->mapped) { wlr_xdg_toplevel_set_parent(child_toplevel, NULL); @@ -162,7 +156,8 @@ static void destroy_imported(struct wlr_xdg_imported_v1 *imported) { struct wlr_xdg_imported_child_v1 *child, *child_tmp; wl_list_for_each_safe(child, child_tmp, &imported->children, link) { struct wlr_xdg_surface *xdg_child = - wlr_xdg_surface_from_wlr_surface(child->surface); + wlr_xdg_surface_try_from_wlr_surface(child->surface); + assert(xdg_child != NULL); wlr_xdg_toplevel_set_parent(xdg_child->toplevel, NULL); } diff --git a/types/wlr_xdg_foreign_v2.c b/types/wlr_xdg_foreign_v2.c index 43e65e0ef..11cc03f79 100644 --- a/types/wlr_xdg_foreign_v2.c +++ b/types/wlr_xdg_foreign_v2.c @@ -32,15 +32,7 @@ static struct wlr_xdg_toplevel *verify_is_toplevel(struct wl_resource *resource, // Note: the error codes are the same for zxdg_exporter_v2 and // zxdg_importer_v2 - if (!wlr_surface_is_xdg_surface(surface)) { - wl_resource_post_error(resource, - ZXDG_EXPORTER_V2_ERROR_INVALID_SURFACE, - "surface must be an xdg_surface"); - return NULL; - } - - struct wlr_xdg_surface *xdg_surface = - wlr_xdg_surface_from_wlr_surface(surface); + struct wlr_xdg_surface *xdg_surface = wlr_xdg_surface_try_from_wlr_surface(surface); if (xdg_surface == NULL || xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) { wl_resource_post_error(resource, ZXDG_EXPORTER_V2_ERROR_INVALID_SURFACE, @@ -84,8 +76,7 @@ static void xdg_imported_handle_set_parent_of(struct wl_client *client, struct wlr_surface *wlr_surface_child = wlr_surface_from_resource(child_resource); - struct wlr_xdg_surface *surface = - wlr_xdg_surface_from_wlr_surface(wlr_surface); + struct wlr_xdg_surface *surface = wlr_xdg_surface_try_from_wlr_surface(wlr_surface); struct wlr_xdg_toplevel *child_toplevel = verify_is_toplevel(resource, wlr_surface_child); if (!child_toplevel) { @@ -168,7 +159,8 @@ static void destroy_imported(struct wlr_xdg_imported_v2 *imported) { struct wlr_xdg_imported_child_v2 *child, *child_tmp; wl_list_for_each_safe(child, child_tmp, &imported->children, link) { struct wlr_xdg_surface *xdg_child = - wlr_xdg_surface_from_wlr_surface(child->surface); + wlr_xdg_surface_try_from_wlr_surface(child->surface); + assert(xdg_child != NULL); wlr_xdg_toplevel_set_parent(xdg_child->toplevel, NULL); } diff --git a/types/xdg_shell/wlr_xdg_popup.c b/types/xdg_shell/wlr_xdg_popup.c index d9c598812..33eba859a 100644 --- a/types/xdg_shell/wlr_xdg_popup.c +++ b/types/xdg_shell/wlr_xdg_popup.c @@ -477,10 +477,8 @@ void wlr_xdg_popup_destroy(struct wlr_xdg_popup *popup) { void wlr_xdg_popup_get_toplevel_coords(struct wlr_xdg_popup *popup, int popup_sx, int popup_sy, int *toplevel_sx, int *toplevel_sy) { struct wlr_surface *parent = popup->parent; - while (wlr_surface_is_xdg_surface(parent)) { - struct wlr_xdg_surface *xdg_surface = - wlr_xdg_surface_from_wlr_surface(parent); - + struct wlr_xdg_surface *xdg_surface; + while ((xdg_surface = wlr_xdg_surface_try_from_wlr_surface(parent))) { if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) { popup_sx += xdg_surface->popup->current.geometry.x; popup_sy += xdg_surface->popup->current.geometry.y; diff --git a/types/xdg_shell/wlr_xdg_surface.c b/types/xdg_shell/wlr_xdg_surface.c index f554eb6e2..9f6968680 100644 --- a/types/xdg_shell/wlr_xdg_surface.c +++ b/types/xdg_shell/wlr_xdg_surface.c @@ -5,14 +5,12 @@ #include #include "types/wlr_xdg_shell.h" -bool wlr_surface_is_xdg_surface(struct wlr_surface *surface) { - return surface->role == &xdg_toplevel_surface_role || - surface->role == &xdg_popup_surface_role; -} - -struct wlr_xdg_surface *wlr_xdg_surface_from_wlr_surface( +struct wlr_xdg_surface *wlr_xdg_surface_try_from_wlr_surface( struct wlr_surface *surface) { - assert(wlr_surface_is_xdg_surface(surface)); + if (surface->role != &xdg_toplevel_surface_role && + surface->role != &xdg_popup_surface_role) { + return NULL; + } return (struct wlr_xdg_surface *)surface->role_data; } @@ -284,8 +282,8 @@ static void xdg_surface_handle_surface_commit(struct wl_listener *listener, } void xdg_surface_role_commit(struct wlr_surface *wlr_surface) { - struct wlr_xdg_surface *surface = - wlr_xdg_surface_from_wlr_surface(wlr_surface); + struct wlr_xdg_surface *surface = wlr_xdg_surface_try_from_wlr_surface(wlr_surface); + assert(surface != NULL); surface->current = surface->pending; @@ -315,8 +313,8 @@ void xdg_surface_role_commit(struct wlr_surface *wlr_surface) { void xdg_surface_role_precommit(struct wlr_surface *wlr_surface, const struct wlr_surface_state *state) { - struct wlr_xdg_surface *surface = - wlr_xdg_surface_from_wlr_surface(wlr_surface); + struct wlr_xdg_surface *surface = wlr_xdg_surface_try_from_wlr_surface(wlr_surface); + assert(surface != NULL); if (state->committed & WLR_SURFACE_STATE_BUFFER && state->buffer == NULL) { // This is a NULL commit @@ -327,8 +325,8 @@ void xdg_surface_role_precommit(struct wlr_surface *wlr_surface, } void xdg_surface_role_destroy(struct wlr_surface *wlr_surface) { - struct wlr_xdg_surface *surface = - wlr_xdg_surface_from_wlr_surface(wlr_surface); + struct wlr_xdg_surface *surface = wlr_xdg_surface_try_from_wlr_surface(wlr_surface); + assert(surface != NULL); reset_xdg_surface(surface); @@ -445,8 +443,8 @@ void wlr_xdg_surface_ping(struct wlr_xdg_surface *surface) { void wlr_xdg_popup_get_position(struct wlr_xdg_popup *popup, double *popup_sx, double *popup_sy) { - struct wlr_xdg_surface *parent = - wlr_xdg_surface_from_wlr_surface(popup->parent); + struct wlr_xdg_surface *parent = wlr_xdg_surface_try_from_wlr_surface(popup->parent); + assert(parent != NULL); struct wlr_box parent_geo; wlr_xdg_surface_get_geometry(parent, &parent_geo); *popup_sx = parent_geo.x + popup->current.geometry.x -