From bbec5a0a9353397e4d3c1ad67dbf4b5b9bc6da33 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 22 Mar 2021 22:21:38 +0100 Subject: [PATCH] transactions-v1: new protocol implementation This implements the transactions-unstable-v1 protocol [1]. The protocol allows clients to synchronize multiple surfaces together and ensure their commits are applied atomically. [1]: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/26 --- include/wlr/types/wlr_transactions_v1.h | 41 +++++++ protocol/meson.build | 1 + types/meson.build | 1 + types/wlr_transactions_v1.c | 156 ++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 include/wlr/types/wlr_transactions_v1.h create mode 100644 types/wlr_transactions_v1.c diff --git a/include/wlr/types/wlr_transactions_v1.h b/include/wlr/types/wlr_transactions_v1.h new file mode 100644 index 000000000..7d6ee46a6 --- /dev/null +++ b/include/wlr/types/wlr_transactions_v1.h @@ -0,0 +1,41 @@ +/* + * This an unstable interface of wlroots. No guarantees are made regarding the + * future consistency of this API. + */ +#ifndef WLR_USE_UNSTABLE +#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" +#endif + +#ifndef WLR_TYPES_WLR_TRANSACTIONS_V1_H +#define WLR_TYPES_WLR_TRANSACTIONS_V1_H + +#include + +struct wlr_transaction_v1_surface { + struct wlr_surface *surface; + struct wl_list link; // wlr_transactions_v1.surfaces + uint32_t cached_state_seq; + + struct wl_listener surface_destroy; +}; + +struct wlr_transaction_v1 { + struct wl_resource *resource; + + struct wl_list surfaces; // wlr_transaction_v1_surface.link +}; + +struct wlr_transaction_manager_v1 { + struct wl_global *global; + + struct { + struct wl_signal destroy; + } events; + + struct wl_listener display_destroy; +}; + +struct wlr_transaction_manager_v1 *wlr_transaction_manager_v1_create( + struct wl_display *display); + +#endif diff --git a/protocol/meson.build b/protocol/meson.build index a602cd1d8..54187377b 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -26,6 +26,7 @@ protocols = { 'relative-pointer-unstable-v1': wl_protocol_dir / 'unstable/relative-pointer/relative-pointer-unstable-v1.xml', 'tablet-unstable-v2': wl_protocol_dir / 'unstable/tablet/tablet-unstable-v2.xml', 'text-input-unstable-v3': wl_protocol_dir / 'unstable/text-input/text-input-unstable-v3.xml', + 'transactions-unstable-v1': wl_protocol_dir / 'unstable/transactions/transactions-unstable-v1.xml', 'xdg-decoration-unstable-v1': wl_protocol_dir / 'unstable/xdg-decoration/xdg-decoration-unstable-v1.xml', 'xdg-foreign-unstable-v1': wl_protocol_dir / 'unstable/xdg-foreign/xdg-foreign-unstable-v1.xml', 'xdg-foreign-unstable-v2': wl_protocol_dir / 'unstable/xdg-foreign/xdg-foreign-unstable-v2.xml', diff --git a/types/meson.build b/types/meson.build index 811d2521a..958fde162 100644 --- a/types/meson.build +++ b/types/meson.build @@ -59,6 +59,7 @@ wlr_files += files( 'wlr_tablet_tool.c', 'wlr_text_input_v3.c', 'wlr_touch.c', + 'wlr_transactions_v1.c', 'wlr_viewporter.c', 'wlr_virtual_keyboard_v1.c', 'wlr_virtual_pointer_v1.c', diff --git a/types/wlr_transactions_v1.c b/types/wlr_transactions_v1.c new file mode 100644 index 000000000..3693579d9 --- /dev/null +++ b/types/wlr_transactions_v1.c @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include "transactions-unstable-v1-protocol.h" + +#define TRANSACTION_MANAGER_V1_VERSION 1 + +static const struct zwp_transaction_v1_interface transaction_impl; + +static struct wlr_transaction_v1 *transaction_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &zwp_transaction_v1_interface, &transaction_impl)); + return wl_resource_get_user_data(resource); +} + +static void transaction_surface_destroy( + struct wlr_transaction_v1_surface *transaction_surface) { + if (transaction_surface == NULL) { + return; + } + + wl_list_remove(&transaction_surface->surface_destroy.link); + wl_list_remove(&transaction_surface->link); + free(transaction_surface); +} + +static void transaction_handle_resource_destroy(struct wl_resource *resource) { + struct wlr_transaction_v1 *transaction = + transaction_from_resource(resource); + + struct wlr_transaction_v1_surface *transaction_surface, *tmp; + wl_list_for_each_safe(transaction_surface, tmp, &transaction->surfaces, link) { + wlr_surface_unlock_cached(transaction_surface->surface, + transaction_surface->cached_state_seq); + transaction_surface_destroy(transaction_surface); + } + + free(transaction); +} + +static void transaction_surface_handle_surface_destroy( + struct wl_listener *listener, void *data) { + struct wlr_transaction_v1_surface *transaction_surface = + wl_container_of(listener, transaction_surface, surface_destroy); + transaction_surface_destroy(transaction_surface); +} + +static void transaction_handle_add_surface(struct wl_client *client, + struct wl_resource *transaction_resource, + struct wl_resource *surface_resource) { + struct wlr_transaction_v1 *transaction = + transaction_from_resource(transaction_resource); + struct wlr_surface *surface = wlr_surface_from_resource(surface_resource); + + struct wlr_transaction_v1_surface *transaction_surface = + calloc(1, sizeof(*transaction_surface)); + if (transaction_surface == NULL) { + wl_resource_post_no_memory(transaction_resource); + return; + } + + transaction_surface->surface = surface; + transaction_surface->cached_state_seq = wlr_surface_lock_pending(surface); + + transaction_surface->surface_destroy.notify = transaction_surface_handle_surface_destroy; + wl_signal_add(&surface->events.destroy, &transaction_surface->surface_destroy); + + wl_list_insert(&transaction->surfaces, &transaction_surface->link); +} + +static void transaction_handle_commit(struct wl_client *client, + struct wl_resource *transaction_resource) { + // The resource destroy handler will release surface locks + wl_resource_destroy(transaction_resource); +} + +static const struct zwp_transaction_v1_interface transaction_impl = { + .add_surface = transaction_handle_add_surface, + .commit = transaction_handle_commit, +}; + +static void manager_handle_create_transaction(struct wl_client *client, + struct wl_resource *manager_resource, uint32_t id) { + struct wlr_transaction_v1 *transaction = calloc(1, sizeof(*transaction)); + if (transaction == NULL) { + wl_resource_post_no_memory(manager_resource); + return; + } + + wl_list_init(&transaction->surfaces); + + uint32_t version = wl_resource_get_version(manager_resource); + transaction->resource = wl_resource_create(client, + &zwp_transaction_v1_interface, version, id); + if (transaction->resource == NULL) { + wl_resource_post_no_memory(manager_resource); + free(transaction); + return; + } + wl_resource_set_implementation(transaction->resource, &transaction_impl, + transaction, transaction_handle_resource_destroy); +} + +static void manager_handle_destroy(struct wl_client *client, + struct wl_resource *manager_resource) { + wl_resource_destroy(manager_resource); +} + +static const struct zwp_transaction_manager_v1_interface manager_impl = { + .destroy = manager_handle_destroy, + .create_transaction = manager_handle_create_transaction, +}; + +static void manager_bind(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { + struct wlr_transaction_manager_v1 *manager = data; + + struct wl_resource *resource = wl_resource_create(client, + &zwp_transaction_manager_v1_interface, version, id); + if (resource == NULL) { + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(resource, &manager_impl, manager, NULL); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_transaction_manager_v1 *manager = + wl_container_of(listener, manager, display_destroy); + wl_global_destroy(manager->global); + wl_list_remove(&manager->display_destroy.link); + free(manager); +} + +struct wlr_transaction_manager_v1 *wlr_transaction_manager_v1_create( + struct wl_display *display) { + struct wlr_transaction_manager_v1 *manager = calloc(1, sizeof(*manager)); + if (manager == NULL) { + return NULL; + } + + manager->global = wl_global_create(display, + &zwp_transaction_manager_v1_interface, TRANSACTION_MANAGER_V1_VERSION, + manager, manager_bind); + if (manager->global == NULL) { + free(manager); + return NULL; + } + + manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy); + + return manager; +}