mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2025-10-29 05:40:16 -04:00
server: add wl_client_get_user_data/wl_client_set_user_data
The only way to attach some data to a wl_client seems to be setting up a destroy listener and use wl_container_of. Let's make it straight forward to attach some data. Having an explicit destroy callback for the user data makes managing the user data lifetime much more convenient. All other callbacks, be they wl_resource request listeners, destroy listeners or destructors, or wl_client destroy listeners, can assume that the wl_client user data still exists if it was set. Otherwise making that guarantee would be complicated. Co-authored-by: Pekka Paalanen <pekka.paalanen@collabora.com> Signed-off-by: Sebastian Wick <sebastian@sebastianwick.net>
This commit is contained in:
parent
6a7284c632
commit
9c4213ed3e
3 changed files with 71 additions and 0 deletions
|
|
@ -365,6 +365,16 @@ wl_client_for_each_resource(struct wl_client *client,
|
|||
wl_client_for_each_resource_iterator_func_t iterator,
|
||||
void *user_data);
|
||||
|
||||
typedef void (*wl_user_data_destroy_func_t)(void *data);
|
||||
|
||||
void
|
||||
wl_client_set_user_data(struct wl_client *client,
|
||||
void *data,
|
||||
wl_user_data_destroy_func_t dtor);
|
||||
|
||||
void *
|
||||
wl_client_get_user_data(struct wl_client *client);
|
||||
|
||||
/** \class wl_listener
|
||||
*
|
||||
* \brief A single listener for Wayland signals
|
||||
|
|
|
|||
|
|
@ -85,6 +85,8 @@ struct wl_client {
|
|||
gid_t gid;
|
||||
bool error;
|
||||
struct wl_priv_signal resource_created_signal;
|
||||
void *data;
|
||||
wl_user_data_destroy_func_t data_dtor;
|
||||
};
|
||||
|
||||
struct wl_display {
|
||||
|
|
@ -943,6 +945,10 @@ wl_client_destroy(struct wl_client *client)
|
|||
|
||||
wl_list_remove(&client->link);
|
||||
wl_list_remove(&client->resource_created_signal.listener_list);
|
||||
|
||||
if (client->data_dtor)
|
||||
client->data_dtor(client->data);
|
||||
|
||||
free(client);
|
||||
}
|
||||
|
||||
|
|
@ -2469,6 +2475,46 @@ wl_client_new_object(struct wl_client *client,
|
|||
return resource;
|
||||
}
|
||||
|
||||
/** Set the client's user data
|
||||
*
|
||||
* User data is whatever the caller wants to store. Use dtor if
|
||||
* the user data needs freeing as the very last step of destroying
|
||||
* the client.
|
||||
*
|
||||
* \param client The client object
|
||||
* \param data The user data pointer
|
||||
* \param dtor Destroy function to be called after all resources have been
|
||||
* destroyed and all destroy listeners have been called. Can be NULL.
|
||||
*
|
||||
* The argument to the destroy function is the user data pointer. If the
|
||||
* destroy function is not NULL, it will be called even if user data is NULL.
|
||||
*
|
||||
* \since 1.22.90
|
||||
* \sa wl_client_get_user_data
|
||||
*/
|
||||
WL_EXPORT void
|
||||
wl_client_set_user_data(struct wl_client *client,
|
||||
void *data,
|
||||
wl_user_data_destroy_func_t dtor)
|
||||
{
|
||||
client->data = data;
|
||||
client->data_dtor = dtor;
|
||||
}
|
||||
|
||||
/** Get the client's user data
|
||||
*
|
||||
* \param client The client object
|
||||
* \return The user data pointer
|
||||
*
|
||||
* \since 1.22.90
|
||||
* \sa wl_client_set_user_data
|
||||
*/
|
||||
WL_EXPORT void *
|
||||
wl_client_get_user_data(struct wl_client *client)
|
||||
{
|
||||
return client->data;
|
||||
}
|
||||
|
||||
struct wl_global *
|
||||
wl_display_add_global(struct wl_display *display,
|
||||
const struct wl_interface *interface,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue