[RFC wayland 2/4] Add wl_resource_init and use it in libwayland implementations of data sharing and SHM

Jason Ekstrand jason at jlekstrand.net
Wed Feb 27 19:29:56 PST 2013


Signed-off-by: Jason Ekstrand <jason at jlekstrand.net>
---
 src/data-device.c    | 16 ++++------------
 src/wayland-server.c |  6 +-----
 src/wayland-server.h | 16 ++++++++++++++++
 src/wayland-shm.c    | 24 ++++++------------------
 4 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/src/data-device.c b/src/data-device.c
index 4255c13..7dedd4c 100644
--- a/src/data-device.c
+++ b/src/data-device.c
@@ -98,13 +98,9 @@ wl_data_source_send_offer(struct wl_data_source *source,
 	if (offer == NULL)
 		return NULL;
 
+	wl_resource_init(&offer->resource, &wl_data_offer_interface,
+			 &data_offer_interface, 0, offer);
 	offer->resource.destroy = destroy_data_offer;
-	offer->resource.object.id = 0;
-	offer->resource.object.interface = &wl_data_offer_interface;
-	offer->resource.object.implementation =
-		(void (**)(void)) &data_offer_interface;
-	offer->resource.data = offer;
-	wl_signal_init(&offer->resource.destroy_signal);
 
 	offer->source = source;
 	offer->source_destroy_listener.notify = destroy_offer_data_source;
@@ -459,13 +455,9 @@ create_data_source(struct wl_client *client,
 		return;
 	}
 
+	wl_resource_init(&source->resource, &wl_data_source_interface,
+			 &data_source_interface, 0, source);
 	source->resource.destroy = destroy_data_source;
-	source->resource.object.id = id;
-	source->resource.object.interface = &wl_data_source_interface;
-	source->resource.object.implementation =
-		(void (**)(void)) &data_source_interface;
-	source->resource.data = source;
-	wl_signal_init(&source->resource.destroy_signal);
 
 	source->accept = client_source_accept;
 	source->send = client_source_send;
diff --git a/src/wayland-server.c b/src/wayland-server.c
index e45b880..f14881b 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -1395,13 +1395,9 @@ wl_client_add_object(struct wl_client *client,
 		return NULL;
 	}
 
-	resource->object.interface = interface;
-	resource->object.implementation = implementation;
-	resource->object.id = id;
+	wl_resource_init(resource, interface, implementation, id, data);
 	resource->client = client;
-	resource->data = data;
 	resource->destroy = (void *) free;
-	wl_signal_init(&resource->destroy_signal);
 
 	if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
 		wl_resource_post_error(client->display_resource,
diff --git a/src/wayland-server.h b/src/wayland-server.h
index c7369eb..38b8303 100644
--- a/src/wayland-server.h
+++ b/src/wayland-server.h
@@ -190,6 +190,22 @@ struct wl_resource {
 	void *data;
 };
 
+static inline void
+wl_resource_init(struct wl_resource *resource,
+		 const struct wl_interface *interface,
+		 const void *implementation, uint32_t id, void *data)
+{
+	resource->object.id = id;
+	resource->object.interface = interface;
+	resource->object.implementation = implementation;
+
+	wl_signal_init(&resource->destroy_signal);
+
+	resource->destroy = NULL;
+	resource->client = NULL;
+	resource->data = data;
+}
+
 struct wl_buffer {
 	struct wl_resource resource;
 	int32_t width, height;
diff --git a/src/wayland-shm.c b/src/wayland-shm.c
index 47c126b..776c0f2 100644
--- a/src/wayland-shm.c
+++ b/src/wayland-shm.c
@@ -128,12 +128,8 @@ shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
 	buffer->pool = pool;
 	pool->refcount++;
 
-	buffer->buffer.resource.object.id = id;
-	buffer->buffer.resource.object.interface = &wl_buffer_interface;
-	buffer->buffer.resource.object.implementation = (void (**)(void))
-		&shm_buffer_interface;
-
-	buffer->buffer.resource.data = buffer;
+	wl_resource_init(&buffer->buffer.resource, &wl_buffer_interface,
+			 &shm_buffer_interface, id, buffer);
 	buffer->buffer.resource.client = resource->client;
 	buffer->buffer.resource.destroy = destroy_buffer;
 
@@ -211,12 +207,8 @@ shm_create_pool(struct wl_client *client, struct wl_resource *resource,
 	}
 	close(fd);
 
-	pool->resource.object.id = id;
-	pool->resource.object.interface = &wl_shm_pool_interface;
-	pool->resource.object.implementation =
-		(void (**)(void)) &shm_pool_interface;
-
-	pool->resource.data = pool;
+	wl_resource_init(&pool->resource, &wl_shm_pool_interface,
+			 &shm_pool_interface, id, pool);
 	pool->resource.client = client;
 	pool->resource.destroy = destroy_pool;
 
@@ -283,12 +275,8 @@ wl_shm_buffer_create(struct wl_client *client,
 	buffer->offset = 0;
 	buffer->pool = NULL;
 
-	buffer->buffer.resource.object.id = id;
-	buffer->buffer.resource.object.interface = &wl_buffer_interface;
-	buffer->buffer.resource.object.implementation = (void (**)(void))
-		&shm_buffer_interface;
-
-	buffer->buffer.resource.data = buffer;
+	wl_resource_init(&buffer->buffer.resource, &wl_buffer_interface,
+			 &shm_buffer_interface, id, buffer);
 	buffer->buffer.resource.client = client;
 	buffer->buffer.resource.destroy = destroy_buffer;
 
-- 
1.8.1.2



More information about the wayland-devel mailing list