diff --git a/src/wayland-server-core.h b/src/wayland-server-core.h index b1baa25f..1fbb0da5 100644 --- a/src/wayland-server-core.h +++ b/src/wayland-server-core.h @@ -628,6 +628,9 @@ struct wl_listener * wl_resource_get_destroy_listener(struct wl_resource *resource, wl_notify_func_t notify); +struct wl_resource * +wl_resource_from_object(struct wl_object *object); + #define wl_resource_for_each(resource, list) \ for (resource = 0, resource = wl_resource_from_link((list)->next); \ wl_resource_get_link(resource) != (list); \ diff --git a/src/wayland-server.c b/src/wayland-server.c index 01fe87f4..f8cdea95 100644 --- a/src/wayland-server.c +++ b/src/wayland-server.c @@ -905,6 +905,28 @@ wl_resource_get_class(struct wl_resource *resource) return resource->object.interface->name; } +/** Safely converts an object into its corresponding resource + * + * \param object object to get the resource for + * \return A corresponding resource, or NULL on failure + * + * Safely converts an object into its corresponding resource. + * + * This is useful for implementing functions that are given a \c wl_argument + * array, and that need to do further introspection on the ".o" field, as it + * is otherwise an opaque type. + * + * \memberof wl_resource + */ +WL_EXPORT struct wl_resource * +wl_resource_from_object(struct wl_object *object) +{ + struct wl_resource *resource; + if (object == NULL) + return NULL; + return wl_container_of(object, resource, object); +} + /** * Add a listener to be called at the beginning of wl_client destruction *