diff --git a/include/wlr/types/wlr_alpha_modifier_v1.h b/include/wlr/types/wlr_alpha_modifier_v1.h new file mode 100644 index 00000000..e400f4e5 --- /dev/null +++ b/include/wlr/types/wlr_alpha_modifier_v1.h @@ -0,0 +1,33 @@ +/* + * 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_ALPHA_MODIFIER_V1_H +#define WLR_TYPES_WLR_ALPHA_MODIFIER_V1_H + +#include + +struct wlr_surface; + +struct wlr_alpha_modifier_surface_v1_state { + double multiplier; // between 0 and 1 +}; + +struct wlr_alpha_modifier_v1 { + struct wl_global *global; + + // private state + + struct wl_listener display_destroy; +}; + +struct wlr_alpha_modifier_v1 *wlr_alpha_modifier_v1_create(struct wl_display *display); + +const struct wlr_alpha_modifier_surface_v1_state *wlr_alpha_modifier_v1_get_surface_state( + struct wlr_surface *surface); + +#endif diff --git a/protocol/meson.build b/protocol/meson.build index 6a8d82b4..c60f2b9a 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -1,5 +1,5 @@ wayland_protos = dependency('wayland-protocols', - version: '>=1.33', + version: '>=1.35', fallback: 'wayland-protocols', default_options: ['tests=false'], ) @@ -19,6 +19,7 @@ protocols = { 'xdg-shell': wl_protocol_dir / 'stable/xdg-shell/xdg-shell.xml', # Staging upstream protocols + 'alpha-modifier-v1': wl_protocol_dir / 'staging/alpha-modifier/alpha-modifier-v1.xml', 'content-type-v1': wl_protocol_dir / 'staging/content-type/content-type-v1.xml', 'cursor-shape-v1': wl_protocol_dir / 'staging/cursor-shape/cursor-shape-v1.xml', 'drm-lease-v1': wl_protocol_dir / 'staging/drm-lease/drm-lease-v1.xml', diff --git a/types/meson.build b/types/meson.build index e6c08244..9ea0d540 100644 --- a/types/meson.build +++ b/types/meson.build @@ -33,6 +33,7 @@ wlr_files += files( 'buffer/dmabuf.c', 'buffer/readonly_data.c', 'buffer/resource.c', + 'wlr_alpha_modifier_v1.c', 'wlr_compositor.c', 'wlr_content_type_v1.c', 'wlr_cursor_shape_v1.c', diff --git a/types/wlr_alpha_modifier_v1.c b/types/wlr_alpha_modifier_v1.c new file mode 100644 index 00000000..921b9412 --- /dev/null +++ b/types/wlr_alpha_modifier_v1.c @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include +#include "alpha-modifier-v1-protocol.h" + +#define ALPHA_MODIFIER_VERSION 1 + +struct wlr_alpha_modifier_surface_v1 { + struct wl_resource *resource; + struct wlr_surface *surface; + struct wlr_addon addon; + struct wlr_surface_synced synced; + struct wlr_alpha_modifier_surface_v1_state pending, current; +}; + +static const struct wp_alpha_modifier_surface_v1_interface surface_impl; + +// Returns NULL if the wl_surface was destroyed +static struct wlr_alpha_modifier_surface_v1 *surface_from_resource(struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &wp_alpha_modifier_surface_v1_interface, + &surface_impl)); + return wl_resource_get_user_data(resource); +} + +static void surface_destroy(struct wlr_alpha_modifier_surface_v1 *surface) { + if (surface == NULL) { + return; + } + wlr_surface_synced_finish(&surface->synced); + wlr_addon_finish(&surface->addon); + wl_resource_set_user_data(surface->resource, NULL); + free(surface); +} + +static void surface_handle_resource_destroy(struct wl_resource *resource) { + struct wlr_alpha_modifier_surface_v1 *surface = surface_from_resource(resource); + surface_destroy(surface); +} + +static void surface_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void surface_handle_set_multiplier(struct wl_client *client, + struct wl_resource *resource, uint32_t multiplier) { + struct wlr_alpha_modifier_surface_v1 *surface = surface_from_resource(resource); + if (surface == NULL) { + wl_resource_post_error(resource, + WP_ALPHA_MODIFIER_SURFACE_V1_ERROR_NO_SURFACE, + "The wl_surface object has been destroyed"); + return; + } + surface->pending.multiplier = (double)multiplier / UINT32_MAX; +} + +static const struct wp_alpha_modifier_surface_v1_interface surface_impl = { + .destroy = surface_handle_destroy, + .set_multiplier = surface_handle_set_multiplier, +}; + +static void surface_synced_init_state(void *_state) { + struct wlr_alpha_modifier_surface_v1_state *state = _state; + state->multiplier = 1; +} + +static const struct wlr_surface_synced_impl surface_synced_impl = { + .state_size = sizeof(struct wlr_alpha_modifier_surface_v1_state), + .init_state = surface_synced_init_state, +}; + +static void surface_addon_destroy(struct wlr_addon *addon) { + struct wlr_alpha_modifier_surface_v1 *surface = wl_container_of(addon, surface, addon); + surface_destroy(surface); +} + +static const struct wlr_addon_interface surface_addon_impl = { + .name = "wp_alpha_modifier_surface_v1", + .destroy = surface_addon_destroy, +}; + +static struct wlr_alpha_modifier_surface_v1 *surface_from_wlr_surface( + struct wlr_surface *wlr_surface) { + struct wlr_addon *addon = wlr_addon_find(&wlr_surface->addons, NULL, &surface_addon_impl); + if (addon == NULL) { + return NULL; + } + struct wlr_alpha_modifier_surface_v1 *surface = wl_container_of(addon, surface, addon); + return surface; +} + +static void manager_handle_destroy(struct wl_client *client, struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void manager_handle_get_surface(struct wl_client *client, + struct wl_resource *manager_resource, uint32_t id, + struct wl_resource *surface_resource) { + struct wlr_surface *wlr_surface = wlr_surface_from_resource(surface_resource); + + if (surface_from_wlr_surface(wlr_surface) != NULL) { + wl_resource_post_error(manager_resource, + WP_ALPHA_MODIFIER_V1_ERROR_ALREADY_CONSTRUCTED, + "The wl_surface object already has a wp_alpha_modifier_surface_v1 object"); + return; + } + + struct wlr_alpha_modifier_surface_v1 *surface = calloc(1, sizeof(*surface)); + if (surface == NULL) { + wl_resource_post_no_memory(manager_resource); + return; + } + + if (!wlr_surface_synced_init(&surface->synced, wlr_surface, + &surface_synced_impl, &surface->pending, &surface->current)) { + free(surface); + wl_resource_post_no_memory(manager_resource); + return; + } + + uint32_t version = wl_resource_get_version(manager_resource); + surface->resource = wl_resource_create(client, + &wp_alpha_modifier_surface_v1_interface, version, id); + if (surface->resource == NULL) { + wlr_surface_synced_finish(&surface->synced); + free(surface); + wl_resource_post_no_memory(manager_resource); + return; + } + wl_resource_set_implementation(surface->resource, &surface_impl, + surface, surface_handle_resource_destroy); + + surface->surface = wlr_surface; + wlr_addon_init(&surface->addon, &wlr_surface->addons, NULL, &surface_addon_impl); +} + +static const struct wp_alpha_modifier_v1_interface manager_impl = { + .destroy = manager_handle_destroy, + .get_surface = manager_handle_get_surface, +}; + +static void manager_bind(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { + struct wl_resource *resource = wl_resource_create(client, + &wp_alpha_modifier_v1_interface, version, id); + if (resource == NULL) { + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_alpha_modifier_v1 *manager = wl_container_of(listener, manager, display_destroy); + wl_list_remove(&manager->display_destroy.link); + wl_global_destroy(manager->global); + free(manager); +} + +struct wlr_alpha_modifier_v1 *wlr_alpha_modifier_v1_create(struct wl_display *display) { + struct wlr_alpha_modifier_v1 *manager = calloc(1, sizeof(*manager)); + if (manager == NULL) { + return NULL; + } + + manager->global = wl_global_create(display, &wp_alpha_modifier_v1_interface, + ALPHA_MODIFIER_VERSION, NULL, 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; +} + +const struct wlr_alpha_modifier_surface_v1_state *wlr_alpha_modifier_v1_get_surface_state( + struct wlr_surface *wlr_surface) { + struct wlr_alpha_modifier_surface_v1 *surface = surface_from_wlr_surface(wlr_surface); + return &surface->current; +}