From 3056435e77d70c76dea5d69e03aa00c58bc40dfa Mon Sep 17 00:00:00 2001 From: Michael Webster Date: Thu, 15 Jan 2026 23:39:19 -0500 Subject: [PATCH] Implement wlr-layer-shell. Very WIP, experimenting mainly with nemo-desktop for now. --- src/compositor/compositor.c | 9 +- src/compositor/meta-surface-actor.c | 4 +- src/meson.build | 3 + src/wayland/meta-wayland-layer-shell.c | 801 ++++++++++++++++++ src/wayland/meta-wayland-layer-shell.h | 48 ++ src/wayland/meta-wayland-surface.c | 5 +- src/wayland/meta-wayland-versions.h | 1 + .../protocol/wlr-layer-shell-unstable-v1.xml | 406 +++++++++ 8 files changed, 1272 insertions(+), 5 deletions(-) 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 66af08546..11d606c38 100644 --- a/src/compositor/compositor.c +++ b/src/compositor/compositor.c @@ -998,8 +998,13 @@ sync_actor_stacking (MetaCompositor *compositor) children = clutter_actor_get_children (priv->bottom_window_group); for (tmp = children; tmp != NULL; tmp = tmp->next) { - MetaWindowActor *child = tmp->data; - MetaWindow *mw = meta_window_actor_get_meta_window (child); + ClutterActor *child = tmp->data; + MetaWindow *mw; + + if (!META_IS_WINDOW_ACTOR (child)) + continue; + + mw = meta_window_actor_get_meta_window (META_WINDOW_ACTOR (child)); if (mw != NULL) { diff --git a/src/compositor/meta-surface-actor.c b/src/compositor/meta-surface-actor.c index 6cc3a6795..5ee141ad1 100644 --- a/src/compositor/meta-surface-actor.c +++ b/src/compositor/meta-surface-actor.c @@ -94,7 +94,7 @@ get_scaled_region (MetaSurfaceActor *surface_actor, float x, y; window_actor = meta_window_actor_from_actor (CLUTTER_ACTOR (surface_actor)); - geometry_scale = meta_window_actor_get_geometry_scale (window_actor); + geometry_scale = window_actor ? meta_window_actor_get_geometry_scale (window_actor) : 1; clutter_actor_get_position (CLUTTER_ACTOR (surface_actor), &x, &y); cairo_region_translate (region, x, y); @@ -331,7 +331,7 @@ meta_surface_actor_is_untransformed (MetaCullable *cullable) clutter_actor_get_abs_allocation_vertices (actor, verts); window_actor = meta_window_actor_from_actor (actor); - geometry_scale = meta_window_actor_get_geometry_scale (window_actor); + geometry_scale = window_actor ? meta_window_actor_get_geometry_scale (window_actor) : 1; return meta_actor_vertices_are_untransformed (verts, width * geometry_scale, diff --git a/src/meson.build b/src/meson.build index fa0e5c043..6d441ac90 100644 --- a/src/meson.build +++ b/src/meson.build @@ -518,6 +518,8 @@ if have_wayland 'wayland/meta-wayland-dnd-surface.h', 'wayland/meta-wayland-gtk-shell.c', 'wayland/meta-wayland-gtk-shell.h', + 'wayland/meta-wayland-layer-shell.c', + 'wayland/meta-wayland-layer-shell.h', 'wayland/meta-wayland.h', 'wayland/meta-wayland-idle-inhibit.c', 'wayland/meta-wayland-idle-inhibit.h', @@ -805,6 +807,7 @@ if have_wayland # - protocol version (if stability is 'unstable') wayland_protocols = [ ['gtk-shell', 'private', ], + ['wlr-layer-shell-unstable-v1', 'private', ], ['idle-inhibit', 'unstable', 'v1', ], ['keyboard-shortcuts-inhibit', 'unstable', 'v1', ], ['linux-dmabuf', 'unstable', 'v1', ], diff --git a/src/wayland/meta-wayland-layer-shell.c b/src/wayland/meta-wayland-layer-shell.c new file mode 100644 index 000000000..b3424290c --- /dev/null +++ b/src/wayland/meta-wayland-layer-shell.c @@ -0,0 +1,801 @@ +/* + * Copyright (C) 2024 Linux Mint + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +#include "config.h" + +#include "wayland/meta-wayland-layer-shell.h" + +#include "backends/meta-logical-monitor.h" +#include "compositor/compositor-private.h" +#include "compositor/meta-surface-actor-wayland.h" +#include "wayland/meta-wayland-outputs.h" +#include "wayland/meta-wayland-private.h" +#include "wayland/meta-wayland-surface.h" +#include "wayland/meta-wayland-versions.h" + +#include "wlr-layer-shell-unstable-v1-server-protocol.h" + +/* Layer shell global */ +struct _MetaWaylandLayerShell +{ + GObject parent; + GList *shell_resources; +}; + +G_DEFINE_TYPE (MetaWaylandLayerShell, meta_wayland_layer_shell, G_TYPE_OBJECT) + +/* Layer surface state (pending/current) */ +typedef struct +{ + uint32_t anchor; + int32_t exclusive_zone; + struct { + int32_t top; + int32_t right; + int32_t bottom; + int32_t left; + } margin; + uint32_t desired_width; + uint32_t desired_height; + MetaLayerShellLayer layer; + uint32_t keyboard_interactivity; +} MetaWaylandLayerSurfaceState; + +/* Layer surface */ +struct _MetaWaylandLayerSurface +{ + MetaWaylandActorSurface parent; + + struct wl_resource *resource; + MetaWaylandOutput *output; + char *namespace; + MetaLayerShellLayer initial_layer; + + MetaWaylandLayerSurfaceState current; + MetaWaylandLayerSurfaceState pending; + + uint32_t configure_serial; + gboolean configured; + gboolean mapped; +}; + +G_DEFINE_TYPE (MetaWaylandLayerSurface, meta_wayland_layer_surface, + META_TYPE_WAYLAND_ACTOR_SURFACE) + +enum +{ + PROP_0, + PROP_OUTPUT, + PROP_NAMESPACE, + PROP_INITIAL_LAYER, + N_PROPS +}; + +static GParamSpec *layer_surface_props[N_PROPS] = { NULL, }; + +static void meta_wayland_layer_surface_send_configure (MetaWaylandLayerSurface *layer_surface); + +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.desired_width = width; + layer_surface->pending.desired_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 value"); + return; + } + + layer_surface->pending.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->pending.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->pending.margin.top = top; + layer_surface->pending.margin.right = right; + layer_surface->pending.margin.bottom = bottom; + layer_surface->pending.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 value"); + return; + } + + layer_surface->pending.keyboard_interactivity = keyboard_interactivity; +} + +static void +layer_surface_get_popup (struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *popup_resource) +{ + /* TODO: Implement popup 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); + + layer_surface->configure_serial = serial; + layer_surface->configured = TRUE; +} + +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 > ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY) + { + wl_resource_post_error (resource, + ZWLR_LAYER_SHELL_V1_ERROR_INVALID_LAYER, + "Invalid layer value"); + return; + } + + layer_surface->pending.layer = layer; +} + +static void +layer_surface_set_exclusive_edge (struct wl_client *client, + struct wl_resource *resource, + uint32_t edge) +{ + /* TODO: Implement exclusive edge */ +} + +static const struct zwlr_layer_surface_v1_interface layer_surface_interface = { + layer_surface_set_size, + layer_surface_set_anchor, + layer_surface_set_exclusive_zone, + layer_surface_set_margin, + layer_surface_set_keyboard_interactivity, + layer_surface_get_popup, + layer_surface_ack_configure, + layer_surface_destroy, + layer_surface_set_layer, + layer_surface_set_exclusive_edge, +}; + +static void +layer_surface_resource_destroyed (struct wl_resource *resource) +{ + MetaWaylandLayerSurface *layer_surface = wl_resource_get_user_data (resource); + + if (layer_surface) + { + layer_surface->resource = NULL; + g_free (layer_surface->namespace); + layer_surface->namespace = NULL; + } +} + +/* Calculate surface position based on anchor, margin, and output geometry */ +static void +calculate_surface_position (MetaWaylandLayerSurface *layer_surface, + int *out_x, + int *out_y) +{ + MetaRectangle output_rect = { 0, 0, 0, 0 }; + MetaWaylandSurface *surface; + int width, height; + int x = 0, y = 0; + uint32_t anchor; + + surface = meta_wayland_surface_role_get_surface (META_WAYLAND_SURFACE_ROLE (layer_surface)); + if (!surface || !surface->buffer_ref.buffer) + { + *out_x = 0; + *out_y = 0; + return; + } + + /* Get output geometry */ + if (layer_surface->output && layer_surface->output->logical_monitor) + { + output_rect = layer_surface->output->logical_monitor->rect; + } + else + { + /* Fallback to primary monitor */ + MetaBackend *backend = meta_get_backend (); + MetaMonitorManager *monitor_manager = meta_backend_get_monitor_manager (backend); + MetaLogicalMonitor *primary = meta_monitor_manager_get_primary_logical_monitor (monitor_manager); + + if (primary) + output_rect = primary->rect; + else + { + output_rect.width = 1920; + output_rect.height = 1080; + } + } + + width = meta_wayland_surface_get_width (surface); + height = meta_wayland_surface_get_height (surface); + anchor = layer_surface->current.anchor; + + /* Calculate X position */ + if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) && + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) + { + /* Horizontally centered, stretched */ + x = output_rect.x + layer_surface->current.margin.left; + } + else if (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) + { + x = output_rect.x + layer_surface->current.margin.left; + } + else if (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) + { + x = output_rect.x + output_rect.width - width - layer_surface->current.margin.right; + } + else + { + /* Horizontally centered */ + x = output_rect.x + (output_rect.width - width) / 2; + } + + /* Calculate Y position */ + if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) && + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) + { + /* Vertically centered, stretched */ + y = output_rect.y + layer_surface->current.margin.top; + } + else if (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) + { + y = output_rect.y + layer_surface->current.margin.top; + } + else if (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM) + { + y = output_rect.y + output_rect.height - height - layer_surface->current.margin.bottom; + } + else + { + /* Vertically centered */ + y = output_rect.y + (output_rect.height - height) / 2; + } + + *out_x = x; + *out_y = y; +} + +static ClutterActor * +get_layer_container_for_layer (MetaWaylandLayerSurface *layer_surface) +{ + MetaDisplay *display; + ClutterActor *layer_container = NULL; + + display = meta_get_display (); + + switch (layer_surface->current.layer) + { + case META_LAYER_SHELL_LAYER_BACKGROUND: + case META_LAYER_SHELL_LAYER_BOTTOM: + /* Use bottom_window_group for background and bottom layers */ + layer_container = meta_get_bottom_window_group_for_display (display); + break; + case META_LAYER_SHELL_LAYER_TOP: + /* Use top_window_group for top layer */ + layer_container = meta_get_top_window_group_for_display (display); + break; + case META_LAYER_SHELL_LAYER_OVERLAY: + /* Use feedback_group for overlay (topmost) */ + layer_container = meta_get_feedback_group_for_display (display); + break; + } + + return layer_container; +} + +static void +meta_wayland_layer_surface_apply_state (MetaWaylandSurfaceRole *surface_role, + MetaWaylandSurfaceState *pending) +{ + MetaWaylandLayerSurface *layer_surface = META_WAYLAND_LAYER_SURFACE (surface_role); + MetaWaylandSurface *surface = + meta_wayland_surface_role_get_surface (surface_role); + MetaWaylandActorSurface *actor_surface = META_WAYLAND_ACTOR_SURFACE (surface_role); + MetaSurfaceActor *surface_actor; + ClutterActor *layer_container; + gboolean had_buffer; + gboolean has_buffer; + int x, y; + + had_buffer = layer_surface->mapped; + has_buffer = surface->buffer_ref.buffer != NULL; + + /* Copy pending state to current */ + layer_surface->current = layer_surface->pending; + + /* Chain up to handle frame callbacks */ + meta_wayland_actor_surface_queue_frame_callbacks (actor_surface, pending); + + /* If client committed without a buffer and hasn't been properly configured, + * send a configure with the calculated size based on their anchors. */ + if (!has_buffer && !layer_surface->configured) + { + meta_wayland_layer_surface_send_configure (layer_surface); + } + + surface_actor = meta_wayland_actor_surface_get_actor (actor_surface); + if (!surface_actor) + return; + + layer_container = get_layer_container_for_layer (layer_surface); + if (!layer_container) + return; + + if (has_buffer) + { + if (!had_buffer) + { + /* Surface is being mapped */ + ClutterActor *actor = CLUTTER_ACTOR (surface_actor); + + clutter_actor_add_child (layer_container, actor); + layer_surface->mapped = TRUE; + + g_debug ("Layer surface mapped: namespace=%s layer=%d", + layer_surface->namespace ? layer_surface->namespace : "(null)", + layer_surface->current.layer); + } + + /* Sync actor state */ + meta_wayland_actor_surface_sync_actor_state (actor_surface); + + /* Update position */ + calculate_surface_position (layer_surface, &x, &y); + clutter_actor_set_position (CLUTTER_ACTOR (surface_actor), x, y); + } + else if (had_buffer && !has_buffer) + { + /* Surface is being unmapped */ + ClutterActor *actor = CLUTTER_ACTOR (surface_actor); + + if (clutter_actor_get_parent (actor)) + clutter_actor_remove_child (layer_container, actor); + + layer_surface->mapped = FALSE; + + g_debug ("Layer surface unmapped: namespace=%s", + layer_surface->namespace ? layer_surface->namespace : "(null)"); + } +} + +static void +meta_wayland_layer_surface_send_configure (MetaWaylandLayerSurface *layer_surface) +{ + MetaWaylandSurface *surface = + meta_wayland_surface_role_get_surface (META_WAYLAND_SURFACE_ROLE (layer_surface)); + MetaRectangle output_rect = { 0, 0, 0, 0 }; + uint32_t width, height; + uint32_t serial; + + if (!layer_surface->resource) + return; + + /* Get output geometry for configure */ + if (layer_surface->output && layer_surface->output->logical_monitor) + { + output_rect = layer_surface->output->logical_monitor->rect; + } + else + { + MetaBackend *backend = meta_get_backend (); + MetaMonitorManager *monitor_manager = meta_backend_get_monitor_manager (backend); + MetaLogicalMonitor *primary = meta_monitor_manager_get_primary_logical_monitor (monitor_manager); + + if (primary) + output_rect = primary->rect; + else + { + output_rect.width = 1920; + output_rect.height = 1080; + } + } + + /* Calculate configure size based on anchors and desired size */ + if (layer_surface->pending.desired_width != 0) + width = layer_surface->pending.desired_width; + else if ((layer_surface->pending.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) && + (layer_surface->pending.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) + width = output_rect.width - + layer_surface->pending.margin.left - + layer_surface->pending.margin.right; + else + width = 0; + + if (layer_surface->pending.desired_height != 0) + height = layer_surface->pending.desired_height; + else if ((layer_surface->pending.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) && + (layer_surface->pending.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) + height = output_rect.height - + layer_surface->pending.margin.top - + layer_surface->pending.margin.bottom; + else + height = 0; + + serial = wl_display_next_serial (surface->compositor->wayland_display); + zwlr_layer_surface_v1_send_configure (layer_surface->resource, serial, width, height); + + g_debug ("Layer surface configured: serial=%u size=%ux%u", serial, width, height); +} + +static void +meta_wayland_layer_surface_dispose (GObject *object) +{ + MetaWaylandLayerSurface *layer_surface = META_WAYLAND_LAYER_SURFACE (object); + MetaWaylandActorSurface *actor_surface = META_WAYLAND_ACTOR_SURFACE (object); + MetaSurfaceActor *surface_actor; + + /* Remove from layer container */ + surface_actor = meta_wayland_actor_surface_get_actor (actor_surface); + if (surface_actor) + { + ClutterActor *actor = CLUTTER_ACTOR (surface_actor); + ClutterActor *parent = clutter_actor_get_parent (actor); + + if (parent) + clutter_actor_remove_child (parent, actor); + } + + g_clear_pointer (&layer_surface->namespace, g_free); + + G_OBJECT_CLASS (meta_wayland_layer_surface_parent_class)->dispose (object); +} + +static void +meta_wayland_layer_surface_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + MetaWaylandLayerSurface *layer_surface = META_WAYLAND_LAYER_SURFACE (object); + + switch (prop_id) + { + case PROP_OUTPUT: + layer_surface->output = g_value_get_pointer (value); + break; + case PROP_NAMESPACE: + layer_surface->namespace = g_value_dup_string (value); + break; + case PROP_INITIAL_LAYER: + layer_surface->initial_layer = g_value_get_uint (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +meta_wayland_layer_surface_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + MetaWaylandLayerSurface *layer_surface = META_WAYLAND_LAYER_SURFACE (object); + + switch (prop_id) + { + case PROP_OUTPUT: + g_value_set_pointer (value, layer_surface->output); + break; + case PROP_NAMESPACE: + g_value_set_string (value, layer_surface->namespace); + break; + case PROP_INITIAL_LAYER: + g_value_set_uint (value, layer_surface->initial_layer); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +meta_wayland_layer_surface_constructed (GObject *object) +{ + MetaWaylandLayerSurface *layer_surface = META_WAYLAND_LAYER_SURFACE (object); + + G_OBJECT_CLASS (meta_wayland_layer_surface_parent_class)->constructed (object); + + /* Apply the initial layer from construction property */ + layer_surface->pending.layer = layer_surface->initial_layer; +} + +static void +meta_wayland_layer_surface_init (MetaWaylandLayerSurface *layer_surface) +{ + /* Default state */ + layer_surface->pending.layer = META_LAYER_SHELL_LAYER_BACKGROUND; + layer_surface->pending.keyboard_interactivity = + ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE; + layer_surface->pending.exclusive_zone = 0; + layer_surface->configured = FALSE; + layer_surface->mapped = FALSE; +} + +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); + + object_class->constructed = meta_wayland_layer_surface_constructed; + object_class->dispose = meta_wayland_layer_surface_dispose; + object_class->set_property = meta_wayland_layer_surface_set_property; + object_class->get_property = meta_wayland_layer_surface_get_property; + + surface_role_class->apply_state = meta_wayland_layer_surface_apply_state; + + layer_surface_props[PROP_OUTPUT] = + g_param_spec_pointer ("output", NULL, NULL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + + layer_surface_props[PROP_NAMESPACE] = + g_param_spec_string ("namespace", NULL, NULL, NULL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + + layer_surface_props[PROP_INITIAL_LAYER] = + g_param_spec_uint ("initial-layer", NULL, NULL, + 0, ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, + META_LAYER_SHELL_LAYER_BACKGROUND, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + + g_object_class_install_properties (object_class, N_PROPS, layer_surface_props); +} + +MetaLayerShellLayer +meta_wayland_layer_surface_get_layer (MetaWaylandLayerSurface *layer_surface) +{ + return layer_surface->current.layer; +} + +MetaWaylandOutput * +meta_wayland_layer_surface_get_output (MetaWaylandLayerSurface *layer_surface) +{ + return layer_surface->output; +} + +/* Layer shell protocol implementation */ +static void +layer_shell_get_layer_surface (struct wl_client *client, + struct wl_resource *resource, + uint32_t id, + struct wl_resource *surface_resource, + struct wl_resource *output_resource, + uint32_t layer, + const char *namespace) +{ + MetaWaylandSurface *surface = wl_resource_get_user_data (surface_resource); + MetaWaylandOutput *output = NULL; + MetaWaylandLayerSurface *layer_surface; + + /* Check if surface already has a role */ + if (surface->role) + { + wl_resource_post_error (resource, + ZWLR_LAYER_SHELL_V1_ERROR_ROLE, + "Surface already has a role"); + return; + } + + /* Validate layer value */ + if (layer > ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY) + { + wl_resource_post_error (resource, + ZWLR_LAYER_SHELL_V1_ERROR_INVALID_LAYER, + "Invalid layer value"); + return; + } + + /* Check if surface already has a buffer */ + if (surface->buffer_ref.buffer) + { + wl_resource_post_error (resource, + ZWLR_LAYER_SHELL_V1_ERROR_ALREADY_CONSTRUCTED, + "Surface has a buffer attached"); + return; + } + + if (output_resource) + output = wl_resource_get_user_data (output_resource); + + /* Create layer surface role via meta_wayland_surface_assign_role. + * This ensures the surface is associated with the role during construction, + * which is required by MetaWaylandActorSurface's constructed() handler. */ + if (!meta_wayland_surface_assign_role (surface, + META_TYPE_WAYLAND_LAYER_SURFACE, + "output", output, + "namespace", namespace, + "initial-layer", layer, + NULL)) + { + wl_resource_post_error (resource, + ZWLR_LAYER_SHELL_V1_ERROR_ROLE, + "wl_surface@%d already has a different role", + wl_resource_get_id (surface_resource)); + return; + } + + layer_surface = META_WAYLAND_LAYER_SURFACE (surface->role); + + layer_surface->resource = + wl_resource_create (client, + &zwlr_layer_surface_v1_interface, + wl_resource_get_version (resource), + id); + + wl_resource_set_implementation (layer_surface->resource, + &layer_surface_interface, + layer_surface, + layer_surface_resource_destroyed); + + g_debug ("Layer surface created: namespace=%s layer=%d output=%p", + namespace ? namespace : "(null)", layer, output); + + /* Send initial configure now that resource is ready */ + meta_wayland_layer_surface_send_configure (layer_surface); +} + +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 layer_shell_interface = { + layer_shell_get_layer_surface, + layer_shell_destroy, +}; + +static void +layer_shell_destructor (struct wl_resource *resource) +{ + MetaWaylandLayerShell *layer_shell = wl_resource_get_user_data (resource); + + layer_shell->shell_resources = g_list_remove (layer_shell->shell_resources, + resource); +} + +static void +bind_layer_shell (struct wl_client *client, + void *data, + uint32_t version, + uint32_t id) +{ + MetaWaylandLayerShell *layer_shell = data; + struct wl_resource *resource; + + resource = wl_resource_create (client, + &zwlr_layer_shell_v1_interface, + version, + id); + wl_resource_set_implementation (resource, + &layer_shell_interface, + layer_shell, + layer_shell_destructor); + + layer_shell->shell_resources = g_list_prepend (layer_shell->shell_resources, + resource); +} + +static void +meta_wayland_layer_shell_init (MetaWaylandLayerShell *layer_shell) +{ +} + +static void +meta_wayland_layer_shell_class_init (MetaWaylandLayerShellClass *klass) +{ +} + +static MetaWaylandLayerShell * +meta_wayland_layer_shell_new (MetaWaylandCompositor *compositor) +{ + MetaWaylandLayerShell *layer_shell; + + layer_shell = g_object_new (META_TYPE_WAYLAND_LAYER_SHELL, NULL); + + if (wl_global_create (compositor->wayland_display, + &zwlr_layer_shell_v1_interface, + META_ZWLR_LAYER_SHELL_V1_VERSION, + layer_shell, bind_layer_shell) == NULL) + { + g_warning ("Failed to register wlr_layer_shell_v1 global"); + g_object_unref (layer_shell); + return NULL; + } + + g_debug ("Layer shell protocol initialized (version %d)", META_ZWLR_LAYER_SHELL_V1_VERSION); + + return layer_shell; +} + +void +meta_wayland_init_layer_shell (MetaWaylandCompositor *compositor) +{ + g_object_set_data_full (G_OBJECT (compositor), "-meta-wayland-layer-shell", + meta_wayland_layer_shell_new (compositor), + g_object_unref); +} diff --git a/src/wayland/meta-wayland-layer-shell.h b/src/wayland/meta-wayland-layer-shell.h new file mode 100644 index 000000000..6945f083a --- /dev/null +++ b/src/wayland/meta-wayland-layer-shell.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2024 Linux Mint + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +#ifndef META_WAYLAND_LAYER_SHELL_H +#define META_WAYLAND_LAYER_SHELL_H + +#include "wayland/meta-wayland.h" +#include "wayland/meta-wayland-actor-surface.h" + +/* Layer shell layer values - matches protocol enum */ +typedef enum +{ + META_LAYER_SHELL_LAYER_BACKGROUND = 0, + META_LAYER_SHELL_LAYER_BOTTOM = 1, + META_LAYER_SHELL_LAYER_TOP = 2, + META_LAYER_SHELL_LAYER_OVERLAY = 3, +} MetaLayerShellLayer; + +#define META_TYPE_WAYLAND_LAYER_SHELL (meta_wayland_layer_shell_get_type ()) +G_DECLARE_FINAL_TYPE (MetaWaylandLayerShell, meta_wayland_layer_shell, + META, WAYLAND_LAYER_SHELL, GObject) + +#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_init_layer_shell (MetaWaylandCompositor *compositor); + +MetaLayerShellLayer meta_wayland_layer_surface_get_layer (MetaWaylandLayerSurface *layer_surface); +MetaWaylandOutput * meta_wayland_layer_surface_get_output (MetaWaylandLayerSurface *layer_surface); + +#endif /* META_WAYLAND_LAYER_SHELL_H */ diff --git a/src/wayland/meta-wayland-surface.c b/src/wayland/meta-wayland-surface.c index 13d0f9184..069a8f90b 100644 --- a/src/wayland/meta-wayland-surface.c +++ b/src/wayland/meta-wayland-surface.c @@ -42,6 +42,7 @@ #include "wayland/meta-wayland-buffer.h" #include "wayland/meta-wayland-data-device.h" #include "wayland/meta-wayland-gtk-shell.h" +#include "wayland/meta-wayland-layer-shell.h" #include "wayland/meta-wayland-keyboard.h" #include "wayland/meta-wayland-outputs.h" #include "wayland/meta-wayland-pointer.h" @@ -1279,7 +1280,8 @@ update_surface_output_state (gpointer key, gpointer value, gpointer user_data) MetaLogicalMonitor *logical_monitor; gboolean is_on_logical_monitor; - g_assert (surface->role); + if (!surface->role) + return; logical_monitor = wayland_output->logical_monitor; if (!logical_monitor) @@ -1455,6 +1457,7 @@ meta_wayland_shell_init (MetaWaylandCompositor *compositor) { meta_wayland_xdg_shell_init (compositor); meta_wayland_init_gtk_shell (compositor); + meta_wayland_init_layer_shell (compositor); meta_wayland_init_viewporter (compositor); } diff --git a/src/wayland/meta-wayland-versions.h b/src/wayland/meta-wayland-versions.h index 3673369a5..3be65aaa2 100644 --- a/src/wayland/meta-wayland-versions.h +++ b/src/wayland/meta-wayland-versions.h @@ -59,5 +59,6 @@ #define META_XDG_DIALOG_VERSION 1 #define META_ZWP_PRIMARY_SELECTION_V1_VERSION 1 #define META_XDG_TOPLEVEL_TAG_V1_VERSION 1 +#define META_ZWLR_LAYER_SHELL_V1_VERSION 4 #endif 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 000000000..6998081ec --- /dev/null +++ b/src/wayland/protocol/wlr-layer-shell-unstable-v1.xml @@ -0,0 +1,406 @@ + + + + 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. + + + + +