From 64674e09e84ff36211d66d466079c8c465873132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20=C5=A0vagelj?= Date: Sat, 2 May 2026 23:36:08 +0200 Subject: [PATCH] Add support for wlr-layer-shell MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tin Švagelj --- src/compositor/compositor.c | 114 +++ src/meson.build | 3 + src/meta/compositor.h | 12 + src/wayland/meta-wayland-layer-shell.c | 863 ++++++++++++++++++ src/wayland/meta-wayland-layer-shell.h | 28 + src/wayland/meta-wayland-surface.c | 2 + src/wayland/meta-wayland-types.h | 3 + src/wayland/meta-wayland-versions.h | 1 + .../protocol/wlr-layer-shell-unstable-v1.xml | 407 +++++++++ 9 files changed, 1433 insertions(+) create mode 100644 src/wayland/meta-wayland-layer-shell.c create mode 100644 src/wayland/meta-wayland-layer-shell.h create mode 100644 src/wayland/protocol/wlr-layer-shell-unstable-v1.xml diff --git a/src/compositor/compositor.c b/src/compositor/compositor.c index 4065c51473..3c4d9fa2ff 100644 --- a/src/compositor/compositor.c +++ b/src/compositor/compositor.c @@ -117,6 +117,11 @@ typedef struct _MetaCompositorPrivate ClutterActor *top_window_group; ClutterActor *feedback_group; + ClutterActor *layer_background_group; + ClutterActor *layer_bottom_group; + ClutterActor *layer_top_group; + ClutterActor *layer_overlay_group; + GList *windows; CoglContext *context; @@ -248,6 +253,74 @@ meta_compositor_get_feedback_group (MetaCompositor *compositor) return priv->feedback_group; } +/** + * meta_compositor_get_layer_background_group: + * @compositor: a #MetaCompositor + * + * Returns: (transfer none): The layer shell background group + */ +ClutterActor * +meta_compositor_get_layer_background_group (MetaCompositor *compositor) +{ + MetaCompositorPrivate *priv; + + g_return_val_if_fail (compositor, NULL); + priv = meta_compositor_get_instance_private (compositor); + + return priv->layer_background_group; +} + +/** + * meta_compositor_get_layer_bottom_group: + * @compositor: a #MetaCompositor + * + * Returns: (transfer none): The layer shell bottom group + */ +ClutterActor * +meta_compositor_get_layer_bottom_group (MetaCompositor *compositor) +{ + MetaCompositorPrivate *priv; + + g_return_val_if_fail (compositor, NULL); + priv = meta_compositor_get_instance_private (compositor); + + return priv->layer_bottom_group; +} + +/** + * meta_compositor_get_layer_top_group: + * @compositor: a #MetaCompositor + * + * Returns: (transfer none): The layer shell top group + */ +ClutterActor * +meta_compositor_get_layer_top_group (MetaCompositor *compositor) +{ + MetaCompositorPrivate *priv; + + g_return_val_if_fail (compositor, NULL); + priv = meta_compositor_get_instance_private (compositor); + + return priv->layer_top_group; +} + +/** + * meta_compositor_get_layer_overlay_group: + * @compositor: a #MetaCompositor + * + * Returns: (transfer none): The layer shell overlay group + */ +ClutterActor * +meta_compositor_get_layer_overlay_group (MetaCompositor *compositor) +{ + MetaCompositorPrivate *priv; + + g_return_val_if_fail (compositor, NULL); + priv = meta_compositor_get_instance_private (compositor); + + return priv->layer_overlay_group; +} + /** * meta_compositor_get_window_actors: * @compositor: a #MetaCompositor @@ -300,6 +373,24 @@ meta_compositor_manage (MetaCompositor *compositor, clutter_actor_add_child (stage, priv->top_window_group); clutter_actor_add_child (stage, priv->feedback_group); + priv->layer_background_group = clutter_actor_new (); + clutter_actor_set_accessible_name (priv->layer_background_group, + "Layer shell background"); + priv->layer_bottom_group = clutter_actor_new (); + clutter_actor_set_accessible_name (priv->layer_bottom_group, + "Layer shell bottom"); + priv->layer_top_group = clutter_actor_new (); + clutter_actor_set_accessible_name (priv->layer_top_group, + "Layer shell top"); + priv->layer_overlay_group = clutter_actor_new (); + clutter_actor_set_accessible_name (priv->layer_overlay_group, + "Layer shell overlay"); + + clutter_actor_add_child (priv->window_group, priv->layer_background_group); + clutter_actor_add_child (priv->window_group, priv->layer_bottom_group); + clutter_actor_add_child (priv->window_group, priv->layer_top_group); + clutter_actor_add_child (priv->window_group, priv->layer_overlay_group); + if (!META_COMPOSITOR_GET_CLASS (compositor)->manage (compositor, error)) return FALSE; @@ -318,6 +409,10 @@ meta_compositor_real_unmanage (MetaCompositor *compositor) g_clear_signal_handler (&priv->top_window_actor_destroy_id, priv->top_window_actor); + g_clear_pointer (&priv->layer_overlay_group, clutter_actor_destroy); + g_clear_pointer (&priv->layer_top_group, clutter_actor_destroy); + g_clear_pointer (&priv->layer_bottom_group, clutter_actor_destroy); + g_clear_pointer (&priv->layer_background_group, clutter_actor_destroy); g_clear_pointer (&priv->window_group, clutter_actor_destroy); g_clear_pointer (&priv->top_window_group, clutter_actor_destroy); g_clear_pointer (&priv->feedback_group, clutter_actor_destroy); @@ -587,6 +682,12 @@ sync_actor_stacking (MetaCompositor *compositor) { ClutterActor *actor = old->data; + if (actor == priv->layer_background_group || + actor == priv->layer_bottom_group || + actor == priv->layer_top_group || + actor == priv->layer_overlay_group) + continue; + if (META_IS_BACKGROUND_GROUP (actor) || META_IS_BACKGROUND_ACTOR (actor)) { @@ -620,6 +721,13 @@ sync_actor_stacking (MetaCompositor *compositor) * We reorder the actors even if they're not parented to the window group, * to allow stacking to work with intermediate actors (eg during effects) */ + + clutter_actor_set_child_above_sibling (priv->window_group, + priv->layer_overlay_group, NULL); + clutter_actor_set_child_below_sibling (priv->window_group, + priv->layer_top_group, + priv->layer_overlay_group); + for (tmp = g_list_last (priv->windows); tmp != NULL; tmp = tmp->prev) { ClutterActor *actor = tmp->data, *parent; @@ -631,6 +739,12 @@ sync_actor_stacking (MetaCompositor *compositor) /* we prepended the backgrounds above so the last actor in the list * should get lowered to the bottom last. */ + clutter_actor_set_child_below_sibling (priv->window_group, + priv->layer_bottom_group, NULL); + + clutter_actor_set_child_below_sibling (priv->window_group, + priv->layer_background_group, NULL); + for (tmp = backgrounds; tmp != NULL; tmp = tmp->next) { ClutterActor *actor = tmp->data, *parent; diff --git a/src/meson.build b/src/meson.build index 22d0188f8c..faa1b3de3d 100644 --- a/src/meson.build +++ b/src/meson.build @@ -661,6 +661,8 @@ mutter_sources += [ 'wayland/meta-wayland-xdg-session-state.h', 'wayland/meta-wayland-xdg-session.c', 'wayland/meta-wayland-xdg-session.h', + 'wayland/meta-wayland-layer-shell.c', + 'wayland/meta-wayland-layer-shell.h', 'wayland/meta-wayland-xdg-shell.c', 'wayland/meta-wayland-xdg-shell.h', 'wayland/meta-wayland-xdg-dialog.c', @@ -1086,6 +1088,7 @@ wayland_protocols = [ ['xdg-system-bell', 'staging', 1, ], ['xdg-toplevel-drag', 'staging', 1, ], ['xdg-toplevel-tag', 'staging', 1, ], + ['wlr-layer-shell-unstable-v1', 'private', ], ] if have_xwayland diff --git a/src/meta/compositor.h b/src/meta/compositor.h index 745700a03f..17d5d68b5c 100644 --- a/src/meta/compositor.h +++ b/src/meta/compositor.h @@ -87,6 +87,18 @@ GList * meta_compositor_get_window_actors (MetaCompositor *compositor); META_EXPORT ClutterActor * meta_compositor_get_top_window_group (MetaCompositor *compositor); +META_EXPORT +ClutterActor * meta_compositor_get_layer_background_group (MetaCompositor *compositor); + +META_EXPORT +ClutterActor * meta_compositor_get_layer_bottom_group (MetaCompositor *compositor); + +META_EXPORT +ClutterActor * meta_compositor_get_layer_top_group (MetaCompositor *compositor); + +META_EXPORT +ClutterActor * meta_compositor_get_layer_overlay_group (MetaCompositor *compositor); + META_EXPORT void meta_compositor_disable_unredirect (MetaCompositor *compositor); diff --git a/src/wayland/meta-wayland-layer-shell.c b/src/wayland/meta-wayland-layer-shell.c new file mode 100644 index 0000000000..fa456c345b --- /dev/null +++ b/src/wayland/meta-wayland-layer-shell.c @@ -0,0 +1,863 @@ +/* + * Copyright (C) 2024-2026 Tin Švagelj (Caellian) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "config.h" + +#include "wayland/meta-wayland-layer-shell.h" + +#include "backends/meta-backend-private.h" +#include "backends/meta-logical-monitor-private.h" +#include "backends/meta-monitor-manager-private.h" +#include "backends/meta-monitor-private.h" +#include "compositor/meta-surface-actor-wayland.h" +#include "meta/compositor.h" +#include "wayland/meta-wayland-outputs.h" +#include "wayland/meta-wayland-private.h" +#include "wayland/meta-wayland-surface-private.h" +#include "wayland/meta-wayland-versions.h" + +#include "wlr-layer-shell-unstable-v1-server-protocol.h" + +enum +{ + LAYER_BACKGROUND = 0, + LAYER_BOTTOM = 1, + LAYER_TOP = 2, + LAYER_OVERLAY = 3, +}; + +typedef struct +{ + uint32_t serial; + uint32_t width; + uint32_t height; +} MetaWaylandLayerSurfaceConfigure; + +struct _MetaWaylandLayerSurface +{ + MetaWaylandActorSurface parent; + + struct wl_resource *resource; + MetaWaylandCompositor *compositor; + MetaWaylandOutput *output; + + uint32_t layer; + uint32_t anchor; + int32_t exclusive_zone; + uint32_t keyboard_interactivity; + uint32_t exclusive_edge; + + struct + { + int32_t top; + int32_t right; + int32_t bottom; + int32_t left; + } margin; + + uint32_t pending_width; + uint32_t pending_height; + + GList *configure_list; + uint32_t configure_serial; + uint32_t acked_serial; + + gboolean mapped; + gboolean closed; + + gulong output_destroyed_handler_id; +}; + +G_DEFINE_TYPE (MetaWaylandLayerSurface, + meta_wayland_layer_surface, + META_TYPE_WAYLAND_ACTOR_SURFACE) + +static MetaDisplay * +display_from_layer_surface (MetaWaylandLayerSurface *layer_surface) +{ + MetaContext *context = + meta_wayland_compositor_get_context (layer_surface->compositor); + + return meta_context_get_display (context); +} + +static ClutterActor * +get_layer_group (MetaWaylandLayerSurface *layer_surface) +{ + MetaDisplay *display = display_from_layer_surface (layer_surface); + MetaCompositor *compositor = meta_display_get_compositor (display); + + switch (layer_surface->layer) + { + case LAYER_BACKGROUND: + return meta_compositor_get_layer_background_group (compositor); + case LAYER_BOTTOM: + return meta_compositor_get_layer_bottom_group (compositor); + case LAYER_TOP: + return meta_compositor_get_layer_top_group (compositor); + case LAYER_OVERLAY: + return meta_compositor_get_layer_overlay_group (compositor); + default: + g_assert_not_reached (); + } +} + +static MtkRectangle +get_output_rect (MetaWaylandLayerSurface *layer_surface) +{ + MetaContext *context = + meta_wayland_compositor_get_context (layer_surface->compositor); + MetaBackend *backend = meta_context_get_backend (context); + MetaMonitorManager *monitor_manager = + meta_backend_get_monitor_manager (backend); + MetaLogicalMonitor *logical_monitor = NULL; + MtkRectangle rect = { 0, 0, 0, 0 }; + + if (layer_surface->output) + { + MetaMonitor *monitor = + meta_wayland_output_get_monitor (layer_surface->output); + + if (monitor) + logical_monitor = meta_monitor_get_logical_monitor (monitor); + } + + if (!logical_monitor) + logical_monitor = + meta_monitor_manager_get_primary_logical_monitor (monitor_manager); + + if (logical_monitor) + rect = meta_logical_monitor_get_layout (logical_monitor); + + return rect; +} + +static void +compute_size (MetaWaylandLayerSurface *layer_surface, + uint32_t *out_width, + uint32_t *out_height) +{ + MtkRectangle output_rect = get_output_rect (layer_surface); + uint32_t width = layer_surface->pending_width; + uint32_t height = layer_surface->pending_height; + uint32_t anchor = layer_surface->anchor; + + if (width == 0) + { + gboolean anchored_left = + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) != 0; + gboolean anchored_right = + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) != 0; + + if (anchored_left && anchored_right) + width = output_rect.width + - layer_surface->margin.left + - layer_surface->margin.right; + } + + if (height == 0) + { + gboolean anchored_top = + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) != 0; + gboolean anchored_bottom = + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM) != 0; + + if (anchored_top && anchored_bottom) + height = output_rect.height + - layer_surface->margin.top + - layer_surface->margin.bottom; + } + + *out_width = width; + *out_height = height; +} + +static void +compute_position (MetaWaylandLayerSurface *layer_surface, + uint32_t width, + uint32_t height, + int *out_x, + int *out_y) +{ + MtkRectangle output_rect = get_output_rect (layer_surface); + uint32_t anchor = layer_surface->anchor; + int x, y; + + gboolean anchored_left = + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) != 0; + gboolean anchored_right = + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) != 0; + gboolean anchored_top = + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) != 0; + gboolean anchored_bottom = + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM) != 0; + + if (anchored_left && anchored_right) + x = output_rect.x + (output_rect.width - (int) width) / 2 + + layer_surface->margin.left - layer_surface->margin.right; + else if (anchored_left) + x = output_rect.x + layer_surface->margin.left; + else if (anchored_right) + x = output_rect.x + output_rect.width - (int) width + - layer_surface->margin.right; + else + x = output_rect.x + (output_rect.width - (int) width) / 2; + + if (anchored_top && anchored_bottom) + y = output_rect.y + (output_rect.height - (int) height) / 2 + + layer_surface->margin.top - layer_surface->margin.bottom; + else if (anchored_top) + y = output_rect.y + layer_surface->margin.top; + else if (anchored_bottom) + y = output_rect.y + output_rect.height - (int) height + - layer_surface->margin.bottom; + else + y = output_rect.y + (output_rect.height - (int) height) / 2; + + *out_x = x; + *out_y = y; +} + +static void +send_configure (MetaWaylandLayerSurface *layer_surface) +{ + MetaWaylandLayerSurfaceConfigure *configure; + uint32_t width, height; + + compute_size (layer_surface, &width, &height); + + layer_surface->configure_serial++; + + configure = g_new0 (MetaWaylandLayerSurfaceConfigure, 1); + configure->serial = layer_surface->configure_serial; + configure->width = width; + configure->height = height; + + layer_surface->configure_list = + g_list_append (layer_surface->configure_list, configure); + + zwlr_layer_surface_v1_send_configure (layer_surface->resource, + configure->serial, + configure->width, + configure->height); +} + +static void +unmap_layer_surface (MetaWaylandLayerSurface *layer_surface) +{ + MetaWaylandActorSurface *actor_surface; + MetaSurfaceActor *surface_actor; + ClutterActor *actor; + ClutterActor *parent; + + if (!layer_surface->mapped) + return; + + layer_surface->mapped = FALSE; + + actor_surface = META_WAYLAND_ACTOR_SURFACE (layer_surface); + surface_actor = meta_wayland_actor_surface_get_actor (actor_surface); + if (!surface_actor) + return; + + actor = CLUTTER_ACTOR (surface_actor); + parent = clutter_actor_get_parent (actor); + if (parent) + clutter_actor_remove_child (parent, actor); +} + +static void +map_layer_surface (MetaWaylandLayerSurface *layer_surface) +{ + MetaWaylandActorSurface *actor_surface; + MetaSurfaceActor *surface_actor; + ClutterActor *actor; + ClutterActor *layer_group; + uint32_t width, height; + int x, y; + + if (layer_surface->mapped) + return; + + actor_surface = META_WAYLAND_ACTOR_SURFACE (layer_surface); + surface_actor = meta_wayland_actor_surface_get_actor (actor_surface); + if (!surface_actor) + return; + + actor = CLUTTER_ACTOR (surface_actor); + layer_group = get_layer_group (layer_surface); + + /* Remove from current parent if any */ + if (clutter_actor_get_parent (actor)) + clutter_actor_remove_child (clutter_actor_get_parent (actor), actor); + + clutter_actor_add_child (layer_group, actor); + + compute_size (layer_surface, &width, &height); + compute_position (layer_surface, width, height, &x, &y); + + clutter_actor_set_position (actor, x, y); + clutter_actor_show (actor); + + layer_surface->mapped = TRUE; +} + +static void +update_position (MetaWaylandLayerSurface *layer_surface) +{ + MetaWaylandActorSurface *actor_surface; + MetaSurfaceActor *surface_actor; + uint32_t width, height; + int x, y; + + if (!layer_surface->mapped) + return; + + actor_surface = META_WAYLAND_ACTOR_SURFACE (layer_surface); + surface_actor = meta_wayland_actor_surface_get_actor (actor_surface); + if (!surface_actor) + return; + + compute_size (layer_surface, &width, &height); + compute_position (layer_surface, width, height, &x, &y); + + clutter_actor_set_position (CLUTTER_ACTOR (surface_actor), x, y); +} + +static void +update_layer (MetaWaylandLayerSurface *layer_surface) +{ + MetaWaylandActorSurface *actor_surface; + MetaSurfaceActor *surface_actor; + ClutterActor *actor; + ClutterActor *layer_group; + ClutterActor *current_parent; + + if (!layer_surface->mapped) + return; + + actor_surface = META_WAYLAND_ACTOR_SURFACE (layer_surface); + surface_actor = meta_wayland_actor_surface_get_actor (actor_surface); + if (!surface_actor) + return; + + actor = CLUTTER_ACTOR (surface_actor); + layer_group = get_layer_group (layer_surface); + current_parent = clutter_actor_get_parent (actor); + + if (current_parent != layer_group) + { + g_object_ref (actor); + if (current_parent) + clutter_actor_remove_child (current_parent, actor); + clutter_actor_add_child (layer_group, actor); + g_object_unref (actor); + } +} + +/* --- zwlr_layer_surface_v1 interface --- */ + +static void +layer_surface_set_size (struct wl_client *client, + struct wl_resource *resource, + uint32_t width, + uint32_t height) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + + layer_surface->pending_width = width; + layer_surface->pending_height = height; +} + +static void +layer_surface_set_anchor (struct wl_client *client, + struct wl_resource *resource, + uint32_t anchor) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + + if (anchor > (ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | + ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | + ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) + { + wl_resource_post_error (resource, + ZWLR_LAYER_SURFACE_V1_ERROR_INVALID_ANCHOR, + "Invalid anchor %u", anchor); + return; + } + + layer_surface->anchor = anchor; +} + +static void +layer_surface_set_exclusive_zone (struct wl_client *client, + struct wl_resource *resource, + int32_t zone) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + + layer_surface->exclusive_zone = zone; +} + +static void +layer_surface_set_margin (struct wl_client *client, + struct wl_resource *resource, + int32_t top, + int32_t right, + int32_t bottom, + int32_t left) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + + layer_surface->margin.top = top; + layer_surface->margin.right = right; + layer_surface->margin.bottom = bottom; + layer_surface->margin.left = left; +} + +static void +layer_surface_set_keyboard_interactivity (struct wl_client *client, + struct wl_resource *resource, + uint32_t keyboard_interactivity) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + + if (keyboard_interactivity > + ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_ON_DEMAND) + { + wl_resource_post_error ( + resource, + ZWLR_LAYER_SURFACE_V1_ERROR_INVALID_KEYBOARD_INTERACTIVITY, + "Invalid keyboard interactivity %u", keyboard_interactivity); + return; + } + + layer_surface->keyboard_interactivity = keyboard_interactivity; +} + +static void +layer_surface_get_popup (struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *popup_resource) +{ + /* TODO: proper popup parenting support */ +} + +static void +layer_surface_ack_configure (struct wl_client *client, + struct wl_resource *resource, + uint32_t serial) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + GList *l; + + layer_surface->acked_serial = serial; + + /* Remove all configure events up to and including this serial */ + while (layer_surface->configure_list) + { + MetaWaylandLayerSurfaceConfigure *configure = + layer_surface->configure_list->data; + gboolean is_match = (configure->serial == serial); + + layer_surface->configure_list = + g_list_delete_link (layer_surface->configure_list, + layer_surface->configure_list); + g_free (configure); + + if (is_match) + break; + } +} + +static void +layer_surface_destroy (struct wl_client *client, + struct wl_resource *resource) +{ + wl_resource_destroy (resource); +} + +static void +layer_surface_set_layer (struct wl_client *client, + struct wl_resource *resource, + uint32_t layer) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + + if (layer > LAYER_OVERLAY) + { + wl_resource_post_error ( + resource, + ZWLR_LAYER_SHELL_V1_ERROR_INVALID_LAYER, + "Invalid layer %u", layer); + return; + } + + layer_surface->layer = layer; + update_layer (layer_surface); + update_position (layer_surface); +} + +static void +layer_surface_set_exclusive_edge (struct wl_client *client, + struct wl_resource *resource, + uint32_t edge) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + + layer_surface->exclusive_edge = edge; +} + +static const struct zwlr_layer_surface_v1_interface +meta_wayland_layer_surface_interface = +{ + .set_size = layer_surface_set_size, + .set_anchor = layer_surface_set_anchor, + .set_exclusive_zone = layer_surface_set_exclusive_zone, + .set_margin = layer_surface_set_margin, + .set_keyboard_interactivity = layer_surface_set_keyboard_interactivity, + .get_popup = layer_surface_get_popup, + .ack_configure = layer_surface_ack_configure, + .destroy = layer_surface_destroy, + .set_layer = layer_surface_set_layer, + .set_exclusive_edge = layer_surface_set_exclusive_edge, +}; + +/* --- MetaWaylandActorSurface role vtable --- */ + +static void +layer_surface_role_assigned (MetaWaylandSurfaceRole *surface_role) +{ + MetaWaylandSurfaceRoleClass *surface_role_class = + META_WAYLAND_SURFACE_ROLE_CLASS (meta_wayland_layer_surface_parent_class); + + surface_role_class->assigned (surface_role); +} + +static void +layer_surface_role_commit_state (MetaWaylandSurfaceRole *surface_role, + MetaWaylandTransaction *transaction, + MetaWaylandSurfaceState *pending) +{ + MetaWaylandLayerSurface *layer_surface = + META_WAYLAND_LAYER_SURFACE (surface_role); + + if (layer_surface->closed) + return; + + /* Initial commit (no buffer): send first configure */ + if (!layer_surface->configure_list && !layer_surface->mapped) + { + send_configure (layer_surface); + return; + } +} + +static void +layer_surface_role_apply_state (MetaWaylandSurfaceRole *surface_role, + MetaWaylandSurfaceState *pending) +{ + MetaWaylandSurfaceRoleClass *surface_role_class = + META_WAYLAND_SURFACE_ROLE_CLASS (meta_wayland_layer_surface_parent_class); + MetaWaylandLayerSurface *layer_surface = + META_WAYLAND_LAYER_SURFACE (surface_role); + MetaWaylandSurface *surface = + meta_wayland_surface_role_get_surface (surface_role); + + if (layer_surface->closed) + return; + + surface_role_class->apply_state (surface_role, pending); + + /* Null buffer -> unmap */ + if (!meta_wayland_surface_get_buffer (surface)) + { + unmap_layer_surface (layer_surface); + return; + } + + /* Map if we have a buffer and an acked configure */ + if (!layer_surface->mapped && layer_surface->acked_serial > 0) + map_layer_surface (layer_surface); + + if (layer_surface->mapped) + { + update_layer (layer_surface); + update_position (layer_surface); + } +} + +static int +layer_surface_get_geometry_scale (MetaWaylandActorSurface *actor_surface) +{ + MetaWaylandSurfaceRole *role = META_WAYLAND_SURFACE_ROLE (actor_surface); + MetaWaylandSurface *surface = meta_wayland_surface_role_get_surface (role); + MetaContext *context = + meta_wayland_compositor_get_context (surface->compositor); + MetaBackend *backend = meta_context_get_backend (context); + + if (!meta_backend_is_stage_views_scaled (backend)) + { + MetaWaylandLayerSurface *layer_surface = + META_WAYLAND_LAYER_SURFACE (actor_surface); + MetaMonitorManager *monitor_manager = + meta_backend_get_monitor_manager (backend); + MetaLogicalMonitor *logical_monitor = NULL; + + if (layer_surface->output) + { + MetaMonitor *monitor = + meta_wayland_output_get_monitor (layer_surface->output); + if (monitor) + logical_monitor = meta_monitor_get_logical_monitor (monitor); + } + + if (!logical_monitor) + logical_monitor = + meta_monitor_manager_get_primary_logical_monitor (monitor_manager); + + if (logical_monitor) + return (int) roundf (meta_logical_monitor_get_scale (logical_monitor)); + } + + return 1; +} + +static void +layer_surface_sync_actor_state (MetaWaylandActorSurface *actor_surface) +{ + MetaWaylandActorSurfaceClass *actor_surface_class = + META_WAYLAND_ACTOR_SURFACE_CLASS (meta_wayland_layer_surface_parent_class); + + actor_surface_class->sync_actor_state (actor_surface); +} + +/* --- resource destructor --- */ + +static void +layer_surface_resource_destroy (struct wl_resource *resource) +{ + MetaWaylandLayerSurface *layer_surface = + wl_resource_get_user_data (resource); + + if (!layer_surface) + return; + + unmap_layer_surface (layer_surface); + + g_list_free_full (layer_surface->configure_list, + (GDestroyNotify) g_free); + layer_surface->configure_list = NULL; + + layer_surface->resource = NULL; +} + +/* --- output destroyed --- */ + +static void +on_output_destroyed (MetaWaylandOutput *output, + MetaWaylandLayerSurface *layer_surface) +{ + layer_surface->output = NULL; + layer_surface->output_destroyed_handler_id = 0; + + if (layer_surface->resource && !layer_surface->closed) + { + layer_surface->closed = TRUE; + zwlr_layer_surface_v1_send_closed (layer_surface->resource); + } +} + +/* Signal name for MetaWaylandOutput destruction */ +#define OUTPUT_DESTROYED_SIGNAL "output-destroyed" + +/* --- GObject boilerplate --- */ + +static void +meta_wayland_layer_surface_dispose (GObject *object) +{ + MetaWaylandLayerSurface *layer_surface = + META_WAYLAND_LAYER_SURFACE (object); + + unmap_layer_surface (layer_surface); + + if (layer_surface->output && layer_surface->output_destroyed_handler_id) + { + g_signal_handler_disconnect (layer_surface->output, + layer_surface->output_destroyed_handler_id); + layer_surface->output_destroyed_handler_id = 0; + } + + g_list_free_full (layer_surface->configure_list, + (GDestroyNotify) g_free); + layer_surface->configure_list = NULL; + + G_OBJECT_CLASS (meta_wayland_layer_surface_parent_class)->dispose (object); +} + +static void +meta_wayland_layer_surface_init (MetaWaylandLayerSurface *layer_surface) +{ +} + +static void +meta_wayland_layer_surface_class_init (MetaWaylandLayerSurfaceClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + MetaWaylandSurfaceRoleClass *surface_role_class = + META_WAYLAND_SURFACE_ROLE_CLASS (klass); + MetaWaylandActorSurfaceClass *actor_surface_class = + META_WAYLAND_ACTOR_SURFACE_CLASS (klass); + + object_class->dispose = meta_wayland_layer_surface_dispose; + + surface_role_class->assigned = layer_surface_role_assigned; + surface_role_class->commit_state = layer_surface_role_commit_state; + surface_role_class->apply_state = layer_surface_role_apply_state; + + actor_surface_class->get_geometry_scale = layer_surface_get_geometry_scale; + actor_surface_class->sync_actor_state = layer_surface_sync_actor_state; +} + +/* --- zwlr_layer_shell_v1 interface --- */ + +static void +layer_shell_get_layer_surface (struct wl_client *client, + struct wl_resource *shell_resource, + uint32_t id, + struct wl_resource *surface_resource, + struct wl_resource *output_resource, + uint32_t layer, + const char *namespace) +{ + MetaWaylandCompositor *compositor = + wl_resource_get_user_data (shell_resource); + MetaWaylandSurface *surface = + wl_resource_get_user_data (surface_resource); + MetaWaylandLayerSurface *layer_surface; + struct wl_resource *layer_surface_resource; + MetaWaylandOutput *output = NULL; + + if (layer > LAYER_OVERLAY) + { + wl_resource_post_error (shell_resource, + ZWLR_LAYER_SHELL_V1_ERROR_INVALID_LAYER, + "Invalid layer %u", layer); + return; + } + + if (meta_wayland_surface_get_buffer (surface)) + { + wl_resource_post_error ( + shell_resource, + ZWLR_LAYER_SHELL_V1_ERROR_ALREADY_CONSTRUCTED, + "wl_surface@%d already has a buffer attached", + wl_resource_get_id (surface_resource)); + return; + } + + if (!meta_wayland_surface_assign_role (surface, + META_TYPE_WAYLAND_LAYER_SURFACE, + NULL)) + { + wl_resource_post_error (shell_resource, + ZWLR_LAYER_SHELL_V1_ERROR_ROLE, + "wl_surface@%d already has a different role", + wl_resource_get_id (surface_resource)); + return; + } + + layer_surface_resource = + wl_resource_create (client, + &zwlr_layer_surface_v1_interface, + wl_resource_get_version (shell_resource), + id); + + layer_surface = META_WAYLAND_LAYER_SURFACE (surface->role); + layer_surface->resource = layer_surface_resource; + layer_surface->compositor = compositor; + layer_surface->layer = layer; + + if (output_resource) + { + output = wl_resource_get_user_data (output_resource); + layer_surface->output = output; + + if (output) + { + layer_surface->output_destroyed_handler_id = + g_signal_connect (output, OUTPUT_DESTROYED_SIGNAL, + G_CALLBACK (on_output_destroyed), + layer_surface); + } + } + + wl_resource_set_implementation (layer_surface_resource, + &meta_wayland_layer_surface_interface, + layer_surface, + layer_surface_resource_destroy); +} + +static void +layer_shell_destroy (struct wl_client *client, + struct wl_resource *resource) +{ + wl_resource_destroy (resource); +} + +static const struct zwlr_layer_shell_v1_interface +meta_wayland_layer_shell_interface = +{ + .get_layer_surface = layer_shell_get_layer_surface, + .destroy = layer_shell_destroy, +}; + +static void +layer_shell_bind (struct wl_client *client, + void *data, + uint32_t version, + uint32_t id) +{ + MetaWaylandCompositor *compositor = data; + struct wl_resource *resource; + + resource = wl_resource_create (client, + &zwlr_layer_shell_v1_interface, + version, id); + wl_resource_set_implementation (resource, + &meta_wayland_layer_shell_interface, + compositor, NULL); +} + +void +meta_wayland_layer_shell_init (MetaWaylandCompositor *compositor) +{ + if (wl_global_create (compositor->wayland_display, + &zwlr_layer_shell_v1_interface, + META_ZWLR_LAYER_SHELL_V1_VERSION, + compositor, layer_shell_bind) == NULL) + g_error ("Failed to register a global wlr-layer-shell object"); +} diff --git a/src/wayland/meta-wayland-layer-shell.h b/src/wayland/meta-wayland-layer-shell.h new file mode 100644 index 0000000000..b5fbbf7629 --- /dev/null +++ b/src/wayland/meta-wayland-layer-shell.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2024-2026 The Mutter Layer Shell Authors + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#pragma once + +#include "wayland/meta-wayland-actor-surface.h" + +#define META_TYPE_WAYLAND_LAYER_SURFACE (meta_wayland_layer_surface_get_type ()) +G_DECLARE_FINAL_TYPE (MetaWaylandLayerSurface, + meta_wayland_layer_surface, + META, WAYLAND_LAYER_SURFACE, + MetaWaylandActorSurface) + +void meta_wayland_layer_shell_init (MetaWaylandCompositor *compositor); diff --git a/src/wayland/meta-wayland-surface.c b/src/wayland/meta-wayland-surface.c index b52375afc6..32f85fc743 100644 --- a/src/wayland/meta-wayland-surface.c +++ b/src/wayland/meta-wayland-surface.c @@ -49,6 +49,7 @@ #include "wayland/meta-wayland-subsurface.h" #include "wayland/meta-wayland-transaction.h" #include "wayland/meta-wayland-viewporter.h" +#include "wayland/meta-wayland-layer-shell.h" #include "wayland/meta-wayland-xdg-shell.h" #include "wayland/meta-window-wayland.h" #include "wayland/meta-wayland-linux-drm-syncobj.h" @@ -1901,6 +1902,7 @@ meta_wayland_shell_init (MetaWaylandCompositor *compositor) meta_wayland_init_gtk_shell (compositor); meta_wayland_init_viewporter (compositor); meta_wayland_init_fractional_scale (compositor); + meta_wayland_layer_shell_init (compositor); } void diff --git a/src/wayland/meta-wayland-types.h b/src/wayland/meta-wayland-types.h index 9c712453df..95805fd02f 100644 --- a/src/wayland/meta-wayland-types.h +++ b/src/wayland/meta-wayland-types.h @@ -78,3 +78,6 @@ typedef struct _MetaWaylandDrmLeaseManager MetaWaylandDrmLeaseManager; typedef struct _MetaWaylandXdgSessionManager MetaWaylandXdgSessionManager; typedef struct _MetaWaylandToplevelDrag MetaWaylandToplevelDrag; + +typedef struct _MetaWaylandLayerShell MetaWaylandLayerShell; +typedef struct _MetaWaylandLayerSurface MetaWaylandLayerSurface; diff --git a/src/wayland/meta-wayland-versions.h b/src/wayland/meta-wayland-versions.h index bf87d1feff..6ecc2e53d3 100644 --- a/src/wayland/meta-wayland-versions.h +++ b/src/wayland/meta-wayland-versions.h @@ -70,3 +70,4 @@ #define META_WP_COLOR_REPRESENTATION_VERSION 1 #define META_WL_FIXES_VERSION 1 #define META_WP_POINTER_WARP_VERSION 1 +#define META_ZWLR_LAYER_SHELL_V1_VERSION 5 diff --git a/src/wayland/protocol/wlr-layer-shell-unstable-v1.xml b/src/wayland/protocol/wlr-layer-shell-unstable-v1.xml new file mode 100644 index 0000000000..e9f27e4fdb --- /dev/null +++ b/src/wayland/protocol/wlr-layer-shell-unstable-v1.xml @@ -0,0 +1,407 @@ + + + + Copyright © 2017 Drew DeVault + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + + + + + Clients can use this interface to assign the surface_layer role to + wl_surfaces. Such surfaces are assigned to a "layer" of the output and + rendered with a defined z-depth respective to each other. They may also be + anchored to the edges and corners of a screen and specify input handling + semantics. This interface should be suitable for the implementation of + many desktop shell components, and a broad number of other applications + that interact with the desktop. + + + + + Create a layer surface for an existing surface. This assigns the role of + layer_surface, or raises a protocol error if another role is already + assigned. + + Creating a layer surface from a wl_surface which has a buffer attached + or committed is a client error, and any attempts by a client to attach + or manipulate a buffer prior to the first layer_surface.configure call + must also be treated as errors. + + After creating a layer_surface object and setting it up, the client + must perform an initial commit without any buffer attached. + The compositor will reply with a layer_surface.configure event. + The client must acknowledge it and is then allowed to attach a buffer + to map the surface. + + You may pass NULL for output to allow the compositor to decide which + output to use. Generally this will be the one that the user most + recently interacted with. + + Clients can specify a namespace that defines the purpose of the layer + surface. + + + + + + + + + + + + + + + + + These values indicate which layers a surface can be rendered in. They + are ordered by z depth, bottom-most first. Traditional shell surfaces + will typically be rendered between the bottom and top layers. + Fullscreen shell surfaces are typically rendered at the top layer. + Multiple surfaces can share a single layer, and ordering within a + single layer is undefined. + + + + + + + + + + + + + This request indicates that the client will not use the layer_shell + object any more. Objects that have been created through this instance + are not affected. + + + + + + + An interface that may be implemented by a wl_surface, for surfaces that + are designed to be rendered as a layer of a stacked desktop-like + environment. + + Layer surface state (layer, size, anchor, exclusive zone, + margin, interactivity) is double-buffered, and will be applied at the + time wl_surface.commit of the corresponding wl_surface is called. + + Attaching a null buffer to a layer surface unmaps it. + + Unmapping a layer_surface means that the surface cannot be shown by the + compositor until it is explicitly mapped again. The layer_surface + returns to the state it had right after layer_shell.get_layer_surface. + The client can re-map the surface by performing a commit without any + buffer attached, waiting for a configure event and handling it as usual. + + + + + Sets the size of the surface in surface-local coordinates. The + compositor will display the surface centered with respect to its + anchors. + + If you pass 0 for either value, the compositor will assign it and + inform you of the assignment in the configure event. You must set your + anchor to opposite edges in the dimensions you omit; not doing so is a + protocol error. Both values are 0 by default. + + Size is double-buffered, see wl_surface.commit. + + + + + + + + Requests that the compositor anchor the surface to the specified edges + and corners. If two orthogonal edges are specified (e.g. 'top' and + 'left'), then the anchor point will be the intersection of the edges + (e.g. the top left corner of the output); otherwise the anchor point + will be centered on that edge, or in the center if none is specified. + + Anchor is double-buffered, see wl_surface.commit. + + + + + + + Requests that the compositor avoids occluding an area with other + surfaces. The compositor's use of this information is + implementation-dependent - do not assume that this region will not + actually be occluded. + + A positive value is only meaningful if the surface is anchored to one + edge or an edge and both perpendicular edges. If the surface is not + anchored, anchored to only two perpendicular edges (a corner), anchored + to only two parallel edges or anchored to all edges, a positive value + will be treated the same as zero. + + A positive zone is the distance from the edge in surface-local + coordinates to consider exclusive. + + Surfaces that do not wish to have an exclusive zone may instead specify + how they should interact with surfaces that do. If set to zero, the + surface indicates that it would like to be moved to avoid occluding + surfaces with a positive exclusive zone. If set to -1, the surface + indicates that it would not like to be moved to accommodate for other + surfaces, and the compositor should extend it all the way to the edges + it is anchored to. + + For example, a panel might set its exclusive zone to 10, so that + maximized shell surfaces are not shown on top of it. A notification + might set its exclusive zone to 0, so that it is moved to avoid + occluding the panel, but shell surfaces are shown underneath it. A + wallpaper or lock screen might set their exclusive zone to -1, so that + they stretch below or over the panel. + + The default value is 0. + + Exclusive zone is double-buffered, see wl_surface.commit. + + + + + + + Requests that the surface be placed some distance away from the anchor + point on the output, in surface-local coordinates. Setting this value + for edges you are not anchored to has no effect. + + The exclusive zone includes the margin. + + Margin is double-buffered, see wl_surface.commit. + + + + + + + + + + Types of keyboard interaction possible for layer shell surfaces. The + rationale for this is twofold: (1) some applications are not interested + in keyboard events and not allowing them to be focused can improve the + desktop experience; (2) some applications will want to take exclusive + keyboard focus. + + + + + This value indicates that this surface is not interested in keyboard + events and the compositor should never assign it the keyboard focus. + + This is the default value, set for newly created layer shell surfaces. + + This is useful for e.g. desktop widgets that display information or + only have interaction with non-keyboard input devices. + + + + + Request exclusive keyboard focus if this surface is above the shell surface layer. + + For the top and overlay layers, the seat will always give + exclusive keyboard focus to the top-most layer which has keyboard + interactivity set to exclusive. If this layer contains multiple + surfaces with keyboard interactivity set to exclusive, the compositor + determines the one receiving keyboard events in an implementation- + defined manner. In this case, no guarantee is made when this surface + will receive keyboard focus (if ever). + + For the bottom and background layers, the compositor is allowed to use + normal focus semantics. + + This setting is mainly intended for applications that need to ensure + they receive all keyboard events, such as a lock screen or a password + prompt. + + + + + This requests the compositor to allow this surface to be focused and + unfocused by the user in an implementation-defined manner. The user + should be able to unfocus this surface even regardless of the layer + it is on. + + Typically, the compositor will want to use its normal mechanism to + manage keyboard focus between layer shell surfaces with this setting + and regular toplevels on the desktop layer (e.g. click to focus). + Nevertheless, it is possible for a compositor to require a special + interaction to focus or unfocus layer shell surfaces (e.g. requiring + a click even if focus follows the mouse normally, or providing a + keybinding to switch focus between layers). + + This setting is mainly intended for desktop shell components (e.g. + panels) that allow keyboard interaction. Using this option can allow + implementing a desktop shell that can be fully usable without the + mouse. + + + + + + + Set how keyboard events are delivered to this surface. By default, + layer shell surfaces do not receive keyboard events; this request can + be used to change this. + + This setting is inherited by child surfaces set by the get_popup + request. + + Layer surfaces receive pointer, touch, and tablet events normally. If + you do not want to receive them, set the input region on your surface + to an empty region. + + Keyboard interactivity is double-buffered, see wl_surface.commit. + + + + + + + This assigns an xdg_popup's parent to this layer_surface. This popup + should have been created via xdg_surface::get_popup with the parent set + to NULL, and this request must be invoked before committing the popup's + initial state. + + See the documentation of xdg_popup for more details about what an + xdg_popup is and how it is used. + + + + + + + When a configure event is received, if a client commits the + surface in response to the configure event, then the client + must make an ack_configure request sometime before the commit + request, passing along the serial of the configure event. + + If the client receives multiple configure events before it + can respond to one, it only has to ack the last configure event. + + A client is not required to commit immediately after sending + an ack_configure request - it may even ack_configure several times + before its next surface commit. + + A client may send multiple ack_configure requests before committing, but + only the last request sent before a commit indicates which configure + event the client really is responding to. + + + + + + + This request destroys the layer surface. + + + + + + The configure event asks the client to resize its surface. + + Clients should arrange their surface for the new states, and then send + an ack_configure request with the serial sent in this configure event at + some point before committing the new surface. + + The client is free to dismiss all but the last configure event it + received. + + The width and height arguments specify the size of the window in + surface-local coordinates. + + The size is a hint, in the sense that the client is free to ignore it if + it doesn't resize, pick a smaller size (to satisfy aspect ratio or + resize in steps of NxM pixels). If the client picks a smaller size and + is anchored to two opposite anchors (e.g. 'top' and 'bottom'), the + surface will be centered on this axis. + + If the width or height arguments are zero, it means the client should + decide its own window dimension. + + + + + + + + + The closed event is sent by the compositor when the surface will no + longer be shown. The output may have been destroyed or the user may + have asked for it to be removed. Further changes to the surface will be + ignored. The client should destroy the resource after receiving this + event, and create a new surface if they so choose. + + + + + + + + + + + + + + + + + + + + + + + Change the layer that the surface is rendered on. + + Layer is double-buffered, see wl_surface.commit. + + + + + + + + + Requests an edge for the exclusive zone to apply. The exclusive + edge will be automatically deduced from anchor points when possible, + but when the surface is anchored to a corner, it will be necessary + to set it explicitly to disambiguate, as it is not possible to deduce + which one of the two corner edges should be used. + + The edge must be one the surface is anchored to, otherwise the + invalid_exclusive_edge protocol error will be raised. + + + + + -- 2.53.0