Add accessor functions for wl_resource and deprecate wl_client_add_resource

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

Signed-off-by: Jason Ekstrand <jason@jlekstrand.net>
This commit is contained in:
Jason Ekstrand 2013-06-01 17:40:54 -05:00 committed by Kristian Høgsberg
parent 2c7468b868
commit 1488c96a5d
4 changed files with 95 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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