From a86fd86b5e4b67c2debd900dbbdd0fc92d1c6e27 Mon Sep 17 00:00:00 2001 From: Thomas Lukaszewicz Date: Wed, 3 Jan 2024 23:47:46 +0000 Subject: [PATCH] wayland-server: Add wl_client_add_event_dispatch_listener The patch adds a listener called after an event has been dispatched by the server. Wayland servers that incorporate the Wayland display into their own event loops may dispatch multiple Wayland events to clients as they work through their task queues. For e.g. if system display state changes the Wayland server may emit several output / surface Wayland events to connected clients. Currently an explicit flush is performd immediately after each individual Wayland event is dispatched to make sure these are propagated to clients in a deterministic / reliable manner. This results in hard to maintain / easy to break code. Contributors must make sure to explicitly flush when dispatching Wayland events to clients - or otherwise wait for some other server event to flush the client buffer later on. In the latter case behavior is not deterministic and makes testing the server code much harder. The client event_dispatch_listener added in this patch allows the server to track if the processing of a server event has resulted in the dispatch of one or more Wayland events to clients - and ensure the buffer is appropriately flushed. This eliminates the need to flush in many different areas of the codebase, making the server easier to maintain and test. Signed-off-by: Thomas Lukaszewicz --- src/wayland-server-core.h | 4 ++++ src/wayland-server.c | 37 +++++++++++++++++++++++++++----- tests/client-test.c | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/wayland-server-core.h b/src/wayland-server-core.h index df958211..579fb9f1 100644 --- a/src/wayland-server-core.h +++ b/src/wayland-server-core.h @@ -342,6 +342,10 @@ struct wl_listener * wl_client_get_destroy_late_listener(struct wl_client *client, wl_notify_func_t notify); +void +wl_client_add_event_dispatch_listener(struct wl_client *client, + struct wl_listener *listener); + struct wl_resource * wl_client_get_object(struct wl_client *client, uint32_t id); diff --git a/src/wayland-server.c b/src/wayland-server.c index e784ef6b..06879e5d 100644 --- a/src/wayland-server.c +++ b/src/wayland-server.c @@ -80,6 +80,7 @@ struct wl_client { struct wl_map objects; struct wl_priv_signal destroy_signal; struct wl_priv_signal destroy_late_signal; + struct wl_priv_signal event_dispatch_signal; pid_t pid; uid_t uid; gid_t gid; @@ -212,12 +213,13 @@ handle_array(struct wl_resource *resource, uint32_t opcode, { struct wl_closure *closure; struct wl_object *object = &resource->object; + struct wl_client *client = resource->client; - if (resource->client->error) + if (client->error) return; if (!verify_objects(resource, opcode, args)) { - resource->client->error = true; + client->error = true; return; } @@ -225,14 +227,17 @@ handle_array(struct wl_resource *resource, uint32_t opcode, &object->interface->events[opcode]); if (closure == NULL) { - resource->client->error = true; + client->error = true; return; } log_closure(resource, closure, true); - if (send_func(closure, resource->client->connection)) - resource->client->error = true; + if (send_func(closure, client->connection)) { + client->error = true; + } else { + wl_priv_signal_final_emit(&client->event_dispatch_signal, client); + } wl_closure_destroy(closure); } @@ -549,6 +554,7 @@ wl_client_create(struct wl_display *display, int fd) wl_priv_signal_init(&client->destroy_signal); wl_priv_signal_init(&client->destroy_late_signal); + wl_priv_signal_init(&client->event_dispatch_signal); if (bind_display(client, display) < 0) goto err_map; @@ -917,6 +923,27 @@ wl_client_get_destroy_late_listener(struct wl_client *client, return wl_priv_signal_get(&client->destroy_late_signal, notify); } +/** Add a listener to be called after successful event dispatch. + * + * \param client The client object. + * \param listener The listener to be added. + * + * After an event has been dispatched to this client, the listener will + * be notified. + * + * There is no requirement to remove the link of the wl_listener when the + * signal is emitted. + * + * \memberof wl_client + * \since 1.22.0 + */ +WL_EXPORT void +wl_client_add_event_dispatch_listener(struct wl_client *client, + struct wl_listener *listener) +{ + wl_priv_signal_add(&client->event_dispatch_signal, listener); +} + WL_EXPORT void wl_client_destroy(struct wl_client *client) { diff --git a/tests/client-test.c b/tests/client-test.c index 47be83fc..cd81fb18 100644 --- a/tests/client-test.c +++ b/tests/client-test.c @@ -143,3 +143,47 @@ TEST(client_destroy_listener) wl_display_destroy(display); } +struct event_dispatch_listener { + struct wl_listener listener; + bool done; +}; + +static void +event_dispatch_notify(struct wl_listener *l, void *data) +{ + struct event_dispatch_listener *listener = + wl_container_of(l, listener, listener); + listener->done = true; +} + +TEST(event_dispatch_listener) +{ + struct wl_display *display; + struct wl_client *client; + struct wl_resource *resource; + struct event_dispatch_listener dispatch_listener; + int s[2]; + + assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0); + display = wl_display_create(); + assert(display); + client = wl_client_create(display, s[0]); + assert(client); + + dispatch_listener.listener.notify = event_dispatch_notify; + dispatch_listener.done = false; + wl_client_add_event_dispatch_listener(client, &dispatch_listener.listener); + + resource = wl_resource_create(client, &wl_seat_interface, 4, 0); + assert(resource); + wl_seat_send_name(resource, "default"); + + wl_event_loop_dispatch(wl_display_get_event_loop(display), 0); + + assert(dispatch_listener.done); + + close(s[1]); + wl_client_destroy(client); + wl_display_destroy(display); +} +