[PATCH wayland 3/4] Add accessor functions for wl_resource and deprecate wl_client_add_resource

Jason Ekstrand jason at jlekstrand.net
Sat Jun 1 15:40:54 PDT 2013


This is the first step towards making wl_resource an opaque pointer type.

Signed-off-by: Jason Ekstrand <jason at jlekstrand.net>
---
 src/wayland-server.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
 src/wayland-server.h | 29 +++++++++++++++++++++++++++--
 src/wayland-shm.c    | 35 +++++++++++++++++++----------------
 src/wayland-util.h   |  7 +++++++
 4 files changed, 95 insertions(+), 20 deletions(-)

diff --git a/src/wayland-server.c b/src/wayland-server.c
index 2d13d9d..13b9dc8 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -379,8 +379,9 @@ wl_client_add_resource(struct wl_client *client,
 {
 	if (resource->object.id == 0) {
 		resource->object.id =
-			wl_map_insert_new(&client->objects, 0, resource);
-	} else if (wl_map_insert_at(&client->objects, 0,
+			wl_map_insert_new(&client->objects,
+					  WL_MAP_ENTRY_LEGACY, resource);
+	} else if (wl_map_insert_at(&client->objects, WL_MAP_ENTRY_LEGACY,
 				  resource->object.id, resource) < 0) {
 		wl_resource_post_error(client->display_resource,
 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
@@ -439,6 +440,45 @@ wl_resource_destroy(struct wl_resource *resource)
 	}
 }
 
+WL_EXPORT struct wl_list *
+wl_resource_get_link(struct wl_resource *resource)
+{
+	return &resource->link;
+}
+
+WL_EXPORT struct wl_client *
+wl_resource_get_client(struct wl_resource *resource)
+{
+	return resource->client;
+}
+
+WL_EXPORT void *
+wl_resource_get_user_data(struct wl_resource *resource)
+{
+	return resource->data;
+}
+
+WL_EXPORT void
+wl_resource_set_destructor(struct wl_resource *resource,
+			   wl_resource_destroy_func_t destroy)
+{
+	resource->destroy = destroy;
+}
+
+WL_EXPORT void
+wl_resource_add_destroy_listener(struct wl_resource *resource,
+				 struct wl_listener * listener)
+{
+	wl_signal_add(&resource->destroy_signal, listener);
+}
+
+WL_EXPORT struct wl_listener *
+wl_resource_get_destroy_listener(struct wl_resource *resource,
+				 wl_notify_func_t notify)
+{
+	return wl_signal_get(&resource->destroy_signal, notify);
+}
+
 WL_EXPORT void
 wl_client_add_destroy_listener(struct wl_client *client,
 			       struct wl_listener *listener)
diff --git a/src/wayland-server.h b/src/wayland-server.h
index a9cf544..677f998 100644
--- a/src/wayland-server.h
+++ b/src/wayland-server.h
@@ -78,6 +78,8 @@ int wl_event_loop_get_fd(struct wl_event_loop *loop);
 struct wl_client;
 struct wl_display;
 struct wl_listener;
+struct wl_resource;
+struct wl_global;
 typedef void (*wl_notify_func_t)(struct wl_listener *listener, void *data);
 
 void wl_event_loop_add_destroy_listener(struct wl_event_loop *loop,
@@ -177,9 +179,17 @@ wl_signal_emit(struct wl_signal *signal, void *data)
 		l->notify(l, data);
 }
 
+typedef void (*wl_resource_destroy_func_t)(struct wl_resource *resource);
+
+/* The wl_resource structure has be deprecated as a transparent structure.
+ * While wl_resource will still exist, it will, in the future, be an opaque
+ * pointer.  Instead of accessing wl_resource directly, it should be created by
+ * wl_client_add_object and wl_client_new_object and only accessed by the
+ * accessor functions provided.
+ */
 struct wl_resource {
 	struct wl_object object;
-	void (*destroy)(struct wl_resource *resource);
+	wl_resource_destroy_func_t destroy;
 	struct wl_list link;
 	struct wl_signal destroy_signal;
 	struct wl_client *client;
@@ -239,13 +249,28 @@ void wl_resource_post_no_memory(struct wl_resource *resource);
 
 uint32_t
 wl_client_add_resource(struct wl_client *client,
-		       struct wl_resource *resource);
+		       struct wl_resource *resource) WL_DEPRECATED;
 
 struct wl_display *
 wl_client_get_display(struct wl_client *client);
 
 void
 wl_resource_destroy(struct wl_resource *resource);
+struct wl_list *
+wl_resource_get_link(struct wl_resource *resource);
+struct wl_client *
+wl_resource_get_client(struct wl_resource *resource);
+void *
+wl_resource_get_user_data(struct wl_resource *resource);
+void
+wl_resource_set_destructor(struct wl_resource *resource,
+			   wl_resource_destroy_func_t destroy);
+void
+wl_resource_add_destroy_listener(struct wl_resource *resource,
+				 struct wl_listener * listener);
+struct wl_listener *
+wl_resource_get_destroy_listener(struct wl_resource *resource,
+				 wl_notify_func_t notify);
 
 void *
 wl_shm_buffer_get_data(struct wl_buffer *buffer);
diff --git a/src/wayland-shm.c b/src/wayland-shm.c
index 776c0f2..dc169ae 100644
--- a/src/wayland-shm.c
+++ b/src/wayland-shm.c
@@ -37,7 +37,7 @@
 #include "wayland-server.h"
 
 struct wl_shm_pool {
-	struct wl_resource resource;
+	struct wl_resource *resource;
 	int refcount;
 	char *data;
 	int size;
@@ -65,8 +65,7 @@ shm_pool_unref(struct wl_shm_pool *pool)
 static void
 destroy_buffer(struct wl_resource *resource)
 {
-	struct wl_shm_buffer *buffer =
-		container_of(resource, struct wl_shm_buffer, buffer.resource);
+	struct wl_shm_buffer *buffer = wl_resource_get_user_data(resource);
 
 	if (buffer->pool)
 		shm_pool_unref(buffer->pool);
@@ -89,7 +88,7 @@ shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
 		       int32_t width, int32_t height,
 		       int32_t stride, uint32_t format)
 {
-	struct wl_shm_pool *pool = resource->data;
+	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
 	struct wl_shm_buffer *buffer;
 
 	switch (format) {
@@ -128,9 +127,11 @@ shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
 	buffer->pool = pool;
 	pool->refcount++;
 
-	wl_resource_init(&buffer->buffer.resource, &wl_buffer_interface,
-			 &shm_buffer_interface, id, buffer);
-	buffer->buffer.resource.client = resource->client;
+	buffer->buffer.resource.object.id = id;
+	buffer->buffer.resource.object.interface = &wl_buffer_interface;
+	buffer->buffer.resource.object.implementation = &shm_buffer_interface;
+	buffer->buffer.resource.data = buffer;
+	buffer->buffer.resource.client = client;
 	buffer->buffer.resource.destroy = destroy_buffer;
 
 	wl_client_add_resource(client, &buffer->buffer.resource);
@@ -139,7 +140,7 @@ shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
 static void
 destroy_pool(struct wl_resource *resource)
 {
-	struct wl_shm_pool *pool = resource->data;
+	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
 
 	shm_pool_unref(pool);
 }
@@ -154,7 +155,7 @@ static void
 shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
 		int32_t size)
 {
-	struct wl_shm_pool *pool = resource->data;
+	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
 	void *data;
 
 	data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE);
@@ -207,12 +208,12 @@ shm_create_pool(struct wl_client *client, struct wl_resource *resource,
 	}
 	close(fd);
 
-	wl_resource_init(&pool->resource, &wl_shm_pool_interface,
-			 &shm_pool_interface, id, pool);
-	pool->resource.client = client;
-	pool->resource.destroy = destroy_pool;
+	pool->resource = wl_client_add_object(client, &wl_shm_pool_interface,
+					      &shm_pool_interface, id, pool);
+	if (!pool->resource)
+		goto err_free;
 
-	wl_client_add_resource(client, &pool->resource);
+	wl_resource_set_destructor(pool->resource, destroy_pool);
 
 	return;
 
@@ -275,8 +276,10 @@ wl_shm_buffer_create(struct wl_client *client,
 	buffer->offset = 0;
 	buffer->pool = NULL;
 
-	wl_resource_init(&buffer->buffer.resource, &wl_buffer_interface,
-			 &shm_buffer_interface, id, buffer);
+	buffer->buffer.resource.object.id = id;
+	buffer->buffer.resource.object.interface = &wl_buffer_interface;
+	buffer->buffer.resource.object.implementation = &shm_buffer_interface;
+	buffer->buffer.resource.data = buffer;
 	buffer->buffer.resource.client = client;
 	buffer->buffer.resource.destroy = destroy_buffer;
 
diff --git a/src/wayland-util.h b/src/wayland-util.h
index dbe05a5..119fa33 100644
--- a/src/wayland-util.h
+++ b/src/wayland-util.h
@@ -39,6 +39,13 @@ extern "C" {
 #define WL_EXPORT
 #endif
 
+/* Deprecated attribute */
+#if defined(__GNUC__) && __GNUC__ >= 4
+#define WL_DEPRECATED __attribute__ ((deprecated))
+#else
+#define WL_DEPRECATED
+#endif
+
 struct wl_message {
 	const char *name;
 	const char *signature;
-- 
1.8.1.4



More information about the wayland-devel mailing list