xdg-shell: convert to try_from

References: https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/884
This commit is contained in:
Simon Ser 2023-02-01 20:12:49 +01:00
parent 03412e9aab
commit 711a1a3ed4
6 changed files with 33 additions and 55 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -5,14 +5,12 @@
#include <wlr/util/log.h>
#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 -