Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Split node_surface.c along wlroots' own files

Claude2d71f06ac000
diff --git a/build/support.zig b/build/support.zig
index c3b894f..145589b 100644
--- a/build/support.zig
+++ b/build/support.zig
@@ -53,7 +53,7 @@ pub fn addGlass(b: *std.Build, module: *std.Build.Module, damage_audit: bool) vo
module.linkSystemLibrary("glesv2", .{});
module.addCSourceFiles(.{ .files = &projection_sources, .flags = if (damage_audit) &.{ "-std=c11", "-Wall", "-Wextra", "-DREDIWM_DAMAGE_AUDIT" } else &.{ "-std=c11", "-Wall", "-Wextra" } });
module.addCSourceFile(.{ .file = b.path("src/node.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
- module.addCSourceFile(.{ .file = b.path("src/node_surface.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
+ module.addCSourceFiles(.{ .files = &.{ "src/node_surface.c", "src/node_subsurface_tree.c", "src/node_xdg_shell.c" }, .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
module.addCSourceFile(.{ .file = b.path("src/scene_outputs.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
module.addCSourceFile(.{ .file = b.path("src/scene_pass.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
module.addCSourceFiles(.{ .files = &scene_output_sources, .flags = if (damage_audit) &.{ "-std=c11", "-Wall", "-Wextra", "-Werror", "-DREDIWM_DAMAGE_AUDIT" } else &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
diff --git a/src/node_subsurface_tree.c b/src/node_subsurface_tree.c
new file mode 100644
index 0000000..f0297e3
--- /dev/null
+++ b/src/node_subsurface_tree.c
@@ -0,0 +1,308 @@
+// A surface and its subsurfaces in the own scene: see node_surface.h. wlroots'
+// types/scene/subsurface_tree.c (wlroots 0.20, MIT licensed) on own nodes.
+#define WLR_USE_UNSTABLE
+#define WLR_PRIVATE private
+#include "node_surface.h"
+#include <assert.h>
+#include <stdlib.h>
+#include <wlr/types/wlr_compositor.h>
+#include <wlr/types/wlr_subcompositor.h>
+
+// A tree for a surface and all of its child sub-surfaces. `tree` contains `scene_surface`
+// and one node per sub-surface.
+struct rediwm_subsurface_tree {
+ struct rediwm_tree *tree;
+ struct wlr_surface *surface;
+ struct rediwm_surface *scene_surface;
+
+ struct wl_listener surface_destroy;
+ struct wl_listener surface_commit;
+ struct wl_listener surface_map;
+ struct wl_listener surface_unmap;
+ struct wl_listener surface_new_subsurface;
+
+ struct rediwm_subsurface_tree *parent; // NULL for the top-level surface
+
+ struct wlr_addon scene_addon;
+
+ struct wlr_box clip;
+
+ // Only valid if the surface is a sub-surface
+
+ struct wlr_addon surface_addon;
+
+ struct wl_listener subsurface_destroy;
+};
+
+static void subsurface_tree_addon_destroy(struct wlr_addon *addon) {
+ struct rediwm_subsurface_tree *subsurface_tree = wl_container_of(addon, subsurface_tree, scene_addon);
+ // tree and scene_surface are cleaned up with the node
+ if (subsurface_tree->parent) {
+ wlr_addon_finish(&subsurface_tree->surface_addon);
+ wl_list_remove(&subsurface_tree->subsurface_destroy.link);
+ }
+ wlr_addon_finish(&subsurface_tree->scene_addon);
+ wl_list_remove(&subsurface_tree->surface_destroy.link);
+ wl_list_remove(&subsurface_tree->surface_commit.link);
+ wl_list_remove(&subsurface_tree->surface_map.link);
+ wl_list_remove(&subsurface_tree->surface_unmap.link);
+ wl_list_remove(&subsurface_tree->surface_new_subsurface.link);
+ free(subsurface_tree);
+}
+
+static const struct wlr_addon_interface subsurface_tree_surface_addon_impl;
+
+static struct rediwm_subsurface_tree *subsurface_tree_from_subsurface(
+ struct rediwm_subsurface_tree *parent, struct wlr_subsurface *subsurface) {
+ struct wlr_addon *addon =
+ wlr_addon_find(&subsurface->surface->addons, parent, &subsurface_tree_surface_addon_impl);
+ assert(addon != NULL);
+ struct rediwm_subsurface_tree *subsurface_tree = wl_container_of(addon, subsurface_tree, surface_addon);
+ return subsurface_tree;
+}
+
+static bool subsurface_tree_reconfigure_clip(struct rediwm_subsurface_tree *subsurface_tree) {
+ struct rediwm_node *surface_node = &subsurface_tree->scene_surface->buffer->node;
+ if (subsurface_tree->parent) {
+ subsurface_tree->clip = (struct wlr_box){
+ .x = subsurface_tree->parent->clip.x - subsurface_tree->tree->node.x,
+ .y = subsurface_tree->parent->clip.y - subsurface_tree->tree->node.y,
+ .width = subsurface_tree->parent->clip.width,
+ .height = subsurface_tree->parent->clip.height,
+ };
+ }
+
+ if (wlr_box_empty(&subsurface_tree->clip)) {
+ rediwm_surface_set_clip(subsurface_tree->scene_surface, NULL);
+ rediwm_node_set_enabled(surface_node, true);
+ rediwm_node_set_position(surface_node, 0, 0);
+
+ return false;
+ } else {
+ struct wlr_box clip = subsurface_tree->clip;
+ struct wlr_box surface_box = {
+ .width = subsurface_tree->surface->current.width,
+ .height = subsurface_tree->surface->current.height,
+ };
+
+ bool intersects = wlr_box_intersection(&clip, &clip, &surface_box);
+ rediwm_node_set_enabled(surface_node, intersects);
+
+ if (intersects) {
+ rediwm_node_set_position(surface_node, clip.x, clip.y);
+ rediwm_surface_set_clip(subsurface_tree->scene_surface, &clip);
+ }
+
+ return true;
+ }
+}
+
+static void subsurface_tree_reconfigure(struct rediwm_subsurface_tree *subsurface_tree) {
+ bool has_clip = subsurface_tree_reconfigure_clip(subsurface_tree);
+
+ struct wlr_surface *surface = subsurface_tree->surface;
+
+ struct rediwm_node *prev = NULL;
+ struct wlr_subsurface *subsurface;
+ wl_list_for_each(subsurface, &surface->current.subsurfaces_below, current.link) {
+ struct rediwm_subsurface_tree *child = subsurface_tree_from_subsurface(subsurface_tree, subsurface);
+ if (prev != NULL) rediwm_node_place_above(&child->tree->node, prev);
+ prev = &child->tree->node;
+
+ rediwm_node_set_position(&child->tree->node, subsurface->current.x, subsurface->current.y);
+
+ if (has_clip) subsurface_tree_reconfigure_clip(child);
+ }
+
+ if (prev != NULL) rediwm_node_place_above(&subsurface_tree->scene_surface->buffer->node, prev);
+ prev = &subsurface_tree->scene_surface->buffer->node;
+
+ wl_list_for_each(subsurface, &surface->current.subsurfaces_above, current.link) {
+ struct rediwm_subsurface_tree *child = subsurface_tree_from_subsurface(subsurface_tree, subsurface);
+ rediwm_node_place_above(&child->tree->node, prev);
+ prev = &child->tree->node;
+
+ rediwm_node_set_position(&child->tree->node, subsurface->current.x, subsurface->current.y);
+
+ if (has_clip) subsurface_tree_reconfigure_clip(child);
+ }
+}
+
+static void subsurface_tree_handle_surface_destroy(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_subsurface_tree *subsurface_tree =
+ wl_container_of(listener, subsurface_tree, surface_destroy);
+ rediwm_node_destroy(&subsurface_tree->tree->node);
+}
+
+static void subsurface_tree_handle_surface_commit(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_subsurface_tree *subsurface_tree =
+ wl_container_of(listener, subsurface_tree, surface_commit);
+
+ // TODO: only do this on subsurface order or position change
+ subsurface_tree_reconfigure(subsurface_tree);
+}
+
+static void subsurface_tree_handle_subsurface_destroy(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_subsurface_tree *subsurface_tree =
+ wl_container_of(listener, subsurface_tree, subsurface_destroy);
+ rediwm_node_destroy(&subsurface_tree->tree->node);
+}
+
+static void subsurface_tree_handle_surface_map(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_subsurface_tree *subsurface_tree =
+ wl_container_of(listener, subsurface_tree, surface_map);
+
+ rediwm_node_set_enabled(&subsurface_tree->tree->node, true);
+}
+
+static void subsurface_tree_handle_surface_unmap(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_subsurface_tree *subsurface_tree =
+ wl_container_of(listener, subsurface_tree, surface_unmap);
+
+ rediwm_node_set_enabled(&subsurface_tree->tree->node, false);
+}
+
+static void subsurface_tree_surface_addon_destroy(struct wlr_addon *addon) {
+ struct rediwm_subsurface_tree *subsurface_tree = wl_container_of(addon, subsurface_tree, surface_addon);
+ rediwm_node_destroy(&subsurface_tree->tree->node);
+}
+
+static const struct wlr_addon_interface subsurface_tree_surface_addon_impl = {
+ .name = "rediwm_subsurface_tree",
+ .destroy = subsurface_tree_surface_addon_destroy,
+};
+
+static struct rediwm_subsurface_tree *scene_surface_tree_create(struct rediwm_tree *parent,
+ struct wlr_surface *surface);
+
+static bool subsurface_tree_create_subsurface(struct rediwm_subsurface_tree *parent,
+ struct wlr_subsurface *subsurface) {
+ struct rediwm_subsurface_tree *child = scene_surface_tree_create(parent->tree, subsurface->surface);
+ if (child == NULL) return false;
+
+ child->parent = parent;
+
+ wlr_addon_init(&child->surface_addon, &subsurface->surface->addons, parent,
+ &subsurface_tree_surface_addon_impl);
+
+ child->subsurface_destroy.notify = subsurface_tree_handle_subsurface_destroy;
+ wl_signal_add(&subsurface->events.destroy, &child->subsurface_destroy);
+
+ return true;
+}
+
+static void subsurface_tree_handle_surface_new_subsurface(struct wl_listener *listener, void *data) {
+ struct rediwm_subsurface_tree *subsurface_tree =
+ wl_container_of(listener, subsurface_tree, surface_new_subsurface);
+ struct wlr_subsurface *subsurface = data;
+ if (!subsurface_tree_create_subsurface(subsurface_tree, subsurface))
+ wl_resource_post_no_memory(subsurface->resource);
+}
+
+static const struct wlr_addon_interface subsurface_tree_addon_impl = {
+ .name = "rediwm_subsurface_tree",
+ .destroy = subsurface_tree_addon_destroy,
+};
+
+static struct rediwm_subsurface_tree *scene_surface_tree_create(struct rediwm_tree *parent,
+ struct wlr_surface *surface) {
+ struct rediwm_subsurface_tree *subsurface_tree = calloc(1, sizeof(*subsurface_tree));
+ if (subsurface_tree == NULL) return NULL;
+
+ subsurface_tree->tree = rediwm_tree_create(parent);
+ if (subsurface_tree->tree == NULL) goto error_surface_tree;
+
+ subsurface_tree->scene_surface = rediwm_surface_create(subsurface_tree->tree, surface);
+ if (subsurface_tree->scene_surface == NULL) goto error_scene_surface;
+
+ subsurface_tree->surface = surface;
+
+ struct wlr_subsurface *subsurface;
+ wl_list_for_each(subsurface, &surface->current.subsurfaces_below, current.link) {
+ if (!subsurface_tree_create_subsurface(subsurface_tree, subsurface)) goto error_scene_surface;
+ }
+ wl_list_for_each(subsurface, &surface->current.subsurfaces_above, current.link) {
+ if (!subsurface_tree_create_subsurface(subsurface_tree, subsurface)) goto error_scene_surface;
+ }
+
+ subsurface_tree_reconfigure(subsurface_tree);
+
+ wlr_addon_init(&subsurface_tree->scene_addon, &subsurface_tree->tree->node.addons, NULL,
+ &subsurface_tree_addon_impl);
+
+ subsurface_tree->surface_destroy.notify = subsurface_tree_handle_surface_destroy;
+ wl_signal_add(&surface->events.destroy, &subsurface_tree->surface_destroy);
+
+ subsurface_tree->surface_commit.notify = subsurface_tree_handle_surface_commit;
+ wl_signal_add(&surface->events.commit, &subsurface_tree->surface_commit);
+
+ subsurface_tree->surface_map.notify = subsurface_tree_handle_surface_map;
+ wl_signal_add(&surface->events.map, &subsurface_tree->surface_map);
+
+ subsurface_tree->surface_unmap.notify = subsurface_tree_handle_surface_unmap;
+ wl_signal_add(&surface->events.unmap, &subsurface_tree->surface_unmap);
+
+ subsurface_tree->surface_new_subsurface.notify = subsurface_tree_handle_surface_new_subsurface;
+ wl_signal_add(&surface->events.new_subsurface, &subsurface_tree->surface_new_subsurface);
+
+ rediwm_node_set_enabled(&subsurface_tree->tree->node, surface->mapped);
+
+ return subsurface_tree;
+
+error_scene_surface:
+ rediwm_node_destroy(&subsurface_tree->tree->node);
+error_surface_tree:
+ free(subsurface_tree);
+ return NULL;
+}
+
+struct rediwm_tree *rediwm_subsurface_tree_create(struct rediwm_tree *parent, struct wlr_surface *surface) {
+ struct rediwm_subsurface_tree *subsurface_tree = scene_surface_tree_create(parent, surface);
+ if (subsurface_tree == NULL) return NULL;
+ return subsurface_tree->tree;
+}
+
+static struct rediwm_subsurface_tree *get_subsurface_tree_from_node(struct rediwm_node *node) {
+ struct wlr_addon *addon = wlr_addon_find(&node->addons, NULL, &subsurface_tree_addon_impl);
+ if (!addon) return NULL;
+
+ struct rediwm_subsurface_tree *tree = wl_container_of(addon, tree, scene_addon);
+ return tree;
+}
+
+static bool subsurface_tree_set_clip(struct rediwm_node *node, const struct wlr_box *clip) {
+ if (node->type != REDIWM_NODE_TREE) return false;
+
+ bool discovered_subsurface_tree = false;
+ struct rediwm_subsurface_tree *tree = get_subsurface_tree_from_node(node);
+ if (tree) {
+ if (tree->parent == NULL) {
+ if (clip ? wlr_box_equal(&tree->clip, clip) : wlr_box_empty(&tree->clip)) return true;
+
+ if (clip)
+ tree->clip = *clip;
+ else
+ tree->clip = (struct wlr_box){0};
+ }
+
+ discovered_subsurface_tree = true;
+ subsurface_tree_reconfigure_clip(tree);
+ }
+
+ struct rediwm_tree *scene_tree = (struct rediwm_tree *)node;
+ struct rediwm_node *child;
+ wl_list_for_each(child, &scene_tree->children, link) {
+ discovered_subsurface_tree |= subsurface_tree_set_clip(child, clip);
+ }
+
+ return discovered_subsurface_tree;
+}
+
+bool rediwm_subsurface_tree_set_clip(struct rediwm_node *node, const struct wlr_box *clip) {
+ return subsurface_tree_set_clip(node, clip);
+}
diff --git a/src/node_surface.c b/src/node_surface.c
index a7c0f4f..4ef106c 100644
--- a/src/node_surface.c
+++ b/src/node_surface.c
@@ -1,7 +1,7 @@
// Client surfaces in the own scene: see node_surface.h. The logic is wlroots' own
-// (types/scene/surface.c, subsurface_tree.c, xdg_shell.c, drag_icon.c, wlroots 0.20, MIT
-// licensed), moved onto own nodes. wlroots 0.20 ABI, pinned by build.zig.zon (as in
-// projection.c).
+// (types/scene/surface.c, wlroots 0.20, MIT licensed), moved onto own nodes;
+// node_subsurface_tree.c and node_xdg_shell.c port the rest of types/scene.
+// wlroots 0.20 ABI, pinned by build.zig.zon (as in projection.c).
#define WLR_USE_UNSTABLE
#define WLR_PRIVATE private
#include "node_surface.h"
@@ -12,15 +12,12 @@
#include <wlr/types/wlr_color_management_v1.h>
#include <wlr/types/wlr_color_representation_v1.h>
#include <wlr/types/wlr_compositor.h>
-#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_fractional_scale_v1.h>
#include <wlr/types/wlr_linux_drm_syncobj_v1.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_presentation_time.h>
#include "scene_outputs.h"
#include <wlr/types/wlr_single_pixel_buffer_v1.h>
-#include <wlr/types/wlr_subcompositor.h>
-#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/transform.h>
// ---- The surface -----------------------------------------------------------------
@@ -416,7 +413,7 @@ struct rediwm_surface *rediwm_surface_create(struct rediwm_tree *parent, struct
return surface;
}
-static void scene_surface_set_clip(struct rediwm_surface *surface, const struct wlr_box *clip) {
+void rediwm_surface_set_clip(struct rediwm_surface *surface, const struct wlr_box *clip) {
if (clip ? wlr_box_equal(clip, &surface->clip) : wlr_box_empty(&surface->clip)) return;
if (clip)
@@ -426,449 +423,3 @@ static void scene_surface_set_clip(struct rediwm_surface *surface, const struct
surface_reconfigure(surface);
}
-
-// ---- The surface and its subsurfaces -----------------------------------------------
-
-// A tree for a surface and all of its child sub-surfaces. `tree` contains `scene_surface`
-// and one node per sub-surface.
-struct rediwm_subsurface_tree {
- struct rediwm_tree *tree;
- struct wlr_surface *surface;
- struct rediwm_surface *scene_surface;
-
- struct wl_listener surface_destroy;
- struct wl_listener surface_commit;
- struct wl_listener surface_map;
- struct wl_listener surface_unmap;
- struct wl_listener surface_new_subsurface;
-
- struct rediwm_subsurface_tree *parent; // NULL for the top-level surface
-
- struct wlr_addon scene_addon;
-
- struct wlr_box clip;
-
- // Only valid if the surface is a sub-surface
-
- struct wlr_addon surface_addon;
-
- struct wl_listener subsurface_destroy;
-};
-
-static void subsurface_tree_addon_destroy(struct wlr_addon *addon) {
- struct rediwm_subsurface_tree *subsurface_tree = wl_container_of(addon, subsurface_tree, scene_addon);
- // tree and scene_surface are cleaned up with the node
- if (subsurface_tree->parent) {
- wlr_addon_finish(&subsurface_tree->surface_addon);
- wl_list_remove(&subsurface_tree->subsurface_destroy.link);
- }
- wlr_addon_finish(&subsurface_tree->scene_addon);
- wl_list_remove(&subsurface_tree->surface_destroy.link);
- wl_list_remove(&subsurface_tree->surface_commit.link);
- wl_list_remove(&subsurface_tree->surface_map.link);
- wl_list_remove(&subsurface_tree->surface_unmap.link);
- wl_list_remove(&subsurface_tree->surface_new_subsurface.link);
- free(subsurface_tree);
-}
-
-static const struct wlr_addon_interface subsurface_tree_surface_addon_impl;
-
-static struct rediwm_subsurface_tree *subsurface_tree_from_subsurface(
- struct rediwm_subsurface_tree *parent, struct wlr_subsurface *subsurface) {
- struct wlr_addon *addon =
- wlr_addon_find(&subsurface->surface->addons, parent, &subsurface_tree_surface_addon_impl);
- assert(addon != NULL);
- struct rediwm_subsurface_tree *subsurface_tree = wl_container_of(addon, subsurface_tree, surface_addon);
- return subsurface_tree;
-}
-
-static bool subsurface_tree_reconfigure_clip(struct rediwm_subsurface_tree *subsurface_tree) {
- struct rediwm_node *surface_node = &subsurface_tree->scene_surface->buffer->node;
- if (subsurface_tree->parent) {
- subsurface_tree->clip = (struct wlr_box){
- .x = subsurface_tree->parent->clip.x - subsurface_tree->tree->node.x,
- .y = subsurface_tree->parent->clip.y - subsurface_tree->tree->node.y,
- .width = subsurface_tree->parent->clip.width,
- .height = subsurface_tree->parent->clip.height,
- };
- }
-
- if (wlr_box_empty(&subsurface_tree->clip)) {
- scene_surface_set_clip(subsurface_tree->scene_surface, NULL);
- rediwm_node_set_enabled(surface_node, true);
- rediwm_node_set_position(surface_node, 0, 0);
-
- return false;
- } else {
- struct wlr_box clip = subsurface_tree->clip;
- struct wlr_box surface_box = {
- .width = subsurface_tree->surface->current.width,
- .height = subsurface_tree->surface->current.height,
- };
-
- bool intersects = wlr_box_intersection(&clip, &clip, &surface_box);
- rediwm_node_set_enabled(surface_node, intersects);
-
- if (intersects) {
- rediwm_node_set_position(surface_node, clip.x, clip.y);
- scene_surface_set_clip(subsurface_tree->scene_surface, &clip);
- }
-
- return true;
- }
-}
-
-static void subsurface_tree_reconfigure(struct rediwm_subsurface_tree *subsurface_tree) {
- bool has_clip = subsurface_tree_reconfigure_clip(subsurface_tree);
-
- struct wlr_surface *surface = subsurface_tree->surface;
-
- struct rediwm_node *prev = NULL;
- struct wlr_subsurface *subsurface;
- wl_list_for_each(subsurface, &surface->current.subsurfaces_below, current.link) {
- struct rediwm_subsurface_tree *child = subsurface_tree_from_subsurface(subsurface_tree, subsurface);
- if (prev != NULL) rediwm_node_place_above(&child->tree->node, prev);
- prev = &child->tree->node;
-
- rediwm_node_set_position(&child->tree->node, subsurface->current.x, subsurface->current.y);
-
- if (has_clip) subsurface_tree_reconfigure_clip(child);
- }
-
- if (prev != NULL) rediwm_node_place_above(&subsurface_tree->scene_surface->buffer->node, prev);
- prev = &subsurface_tree->scene_surface->buffer->node;
-
- wl_list_for_each(subsurface, &surface->current.subsurfaces_above, current.link) {
- struct rediwm_subsurface_tree *child = subsurface_tree_from_subsurface(subsurface_tree, subsurface);
- rediwm_node_place_above(&child->tree->node, prev);
- prev = &child->tree->node;
-
- rediwm_node_set_position(&child->tree->node, subsurface->current.x, subsurface->current.y);
-
- if (has_clip) subsurface_tree_reconfigure_clip(child);
- }
-}
-
-static void subsurface_tree_handle_surface_destroy(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_subsurface_tree *subsurface_tree =
- wl_container_of(listener, subsurface_tree, surface_destroy);
- rediwm_node_destroy(&subsurface_tree->tree->node);
-}
-
-static void subsurface_tree_handle_surface_commit(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_subsurface_tree *subsurface_tree =
- wl_container_of(listener, subsurface_tree, surface_commit);
-
- // TODO: only do this on subsurface order or position change
- subsurface_tree_reconfigure(subsurface_tree);
-}
-
-static void subsurface_tree_handle_subsurface_destroy(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_subsurface_tree *subsurface_tree =
- wl_container_of(listener, subsurface_tree, subsurface_destroy);
- rediwm_node_destroy(&subsurface_tree->tree->node);
-}
-
-static void subsurface_tree_handle_surface_map(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_subsurface_tree *subsurface_tree =
- wl_container_of(listener, subsurface_tree, surface_map);
-
- rediwm_node_set_enabled(&subsurface_tree->tree->node, true);
-}
-
-static void subsurface_tree_handle_surface_unmap(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_subsurface_tree *subsurface_tree =
- wl_container_of(listener, subsurface_tree, surface_unmap);
-
- rediwm_node_set_enabled(&subsurface_tree->tree->node, false);
-}
-
-static void subsurface_tree_surface_addon_destroy(struct wlr_addon *addon) {
- struct rediwm_subsurface_tree *subsurface_tree = wl_container_of(addon, subsurface_tree, surface_addon);
- rediwm_node_destroy(&subsurface_tree->tree->node);
-}
-
-static const struct wlr_addon_interface subsurface_tree_surface_addon_impl = {
- .name = "rediwm_subsurface_tree",
- .destroy = subsurface_tree_surface_addon_destroy,
-};
-
-static struct rediwm_subsurface_tree *scene_surface_tree_create(struct rediwm_tree *parent,
- struct wlr_surface *surface);
-
-static bool subsurface_tree_create_subsurface(struct rediwm_subsurface_tree *parent,
- struct wlr_subsurface *subsurface) {
- struct rediwm_subsurface_tree *child = scene_surface_tree_create(parent->tree, subsurface->surface);
- if (child == NULL) return false;
-
- child->parent = parent;
-
- wlr_addon_init(&child->surface_addon, &subsurface->surface->addons, parent,
- &subsurface_tree_surface_addon_impl);
-
- child->subsurface_destroy.notify = subsurface_tree_handle_subsurface_destroy;
- wl_signal_add(&subsurface->events.destroy, &child->subsurface_destroy);
-
- return true;
-}
-
-static void subsurface_tree_handle_surface_new_subsurface(struct wl_listener *listener, void *data) {
- struct rediwm_subsurface_tree *subsurface_tree =
- wl_container_of(listener, subsurface_tree, surface_new_subsurface);
- struct wlr_subsurface *subsurface = data;
- if (!subsurface_tree_create_subsurface(subsurface_tree, subsurface))
- wl_resource_post_no_memory(subsurface->resource);
-}
-
-static const struct wlr_addon_interface subsurface_tree_addon_impl = {
- .name = "rediwm_subsurface_tree",
- .destroy = subsurface_tree_addon_destroy,
-};
-
-static struct rediwm_subsurface_tree *scene_surface_tree_create(struct rediwm_tree *parent,
- struct wlr_surface *surface) {
- struct rediwm_subsurface_tree *subsurface_tree = calloc(1, sizeof(*subsurface_tree));
- if (subsurface_tree == NULL) return NULL;
-
- subsurface_tree->tree = rediwm_tree_create(parent);
- if (subsurface_tree->tree == NULL) goto error_surface_tree;
-
- subsurface_tree->scene_surface = rediwm_surface_create(subsurface_tree->tree, surface);
- if (subsurface_tree->scene_surface == NULL) goto error_scene_surface;
-
- subsurface_tree->surface = surface;
-
- struct wlr_subsurface *subsurface;
- wl_list_for_each(subsurface, &surface->current.subsurfaces_below, current.link) {
- if (!subsurface_tree_create_subsurface(subsurface_tree, subsurface)) goto error_scene_surface;
- }
- wl_list_for_each(subsurface, &surface->current.subsurfaces_above, current.link) {
- if (!subsurface_tree_create_subsurface(subsurface_tree, subsurface)) goto error_scene_surface;
- }
-
- subsurface_tree_reconfigure(subsurface_tree);
-
- wlr_addon_init(&subsurface_tree->scene_addon, &subsurface_tree->tree->node.addons, NULL,
- &subsurface_tree_addon_impl);
-
- subsurface_tree->surface_destroy.notify = subsurface_tree_handle_surface_destroy;
- wl_signal_add(&surface->events.destroy, &subsurface_tree->surface_destroy);
-
- subsurface_tree->surface_commit.notify = subsurface_tree_handle_surface_commit;
- wl_signal_add(&surface->events.commit, &subsurface_tree->surface_commit);
-
- subsurface_tree->surface_map.notify = subsurface_tree_handle_surface_map;
- wl_signal_add(&surface->events.map, &subsurface_tree->surface_map);
-
- subsurface_tree->surface_unmap.notify = subsurface_tree_handle_surface_unmap;
- wl_signal_add(&surface->events.unmap, &subsurface_tree->surface_unmap);
-
- subsurface_tree->surface_new_subsurface.notify = subsurface_tree_handle_surface_new_subsurface;
- wl_signal_add(&surface->events.new_subsurface, &subsurface_tree->surface_new_subsurface);
-
- rediwm_node_set_enabled(&subsurface_tree->tree->node, surface->mapped);
-
- return subsurface_tree;
-
-error_scene_surface:
- rediwm_node_destroy(&subsurface_tree->tree->node);
-error_surface_tree:
- free(subsurface_tree);
- return NULL;
-}
-
-struct rediwm_tree *rediwm_subsurface_tree_create(struct rediwm_tree *parent, struct wlr_surface *surface) {
- struct rediwm_subsurface_tree *subsurface_tree = scene_surface_tree_create(parent, surface);
- if (subsurface_tree == NULL) return NULL;
- return subsurface_tree->tree;
-}
-
-static struct rediwm_subsurface_tree *get_subsurface_tree_from_node(struct rediwm_node *node) {
- struct wlr_addon *addon = wlr_addon_find(&node->addons, NULL, &subsurface_tree_addon_impl);
- if (!addon) return NULL;
-
- struct rediwm_subsurface_tree *tree = wl_container_of(addon, tree, scene_addon);
- return tree;
-}
-
-static bool subsurface_tree_set_clip(struct rediwm_node *node, const struct wlr_box *clip) {
- if (node->type != REDIWM_NODE_TREE) return false;
-
- bool discovered_subsurface_tree = false;
- struct rediwm_subsurface_tree *tree = get_subsurface_tree_from_node(node);
- if (tree) {
- if (tree->parent == NULL) {
- if (clip ? wlr_box_equal(&tree->clip, clip) : wlr_box_empty(&tree->clip)) return true;
-
- if (clip)
- tree->clip = *clip;
- else
- tree->clip = (struct wlr_box){0};
- }
-
- discovered_subsurface_tree = true;
- subsurface_tree_reconfigure_clip(tree);
- }
-
- struct rediwm_tree *scene_tree = (struct rediwm_tree *)node;
- struct rediwm_node *child;
- wl_list_for_each(child, &scene_tree->children, link) {
- discovered_subsurface_tree |= subsurface_tree_set_clip(child, clip);
- }
-
- return discovered_subsurface_tree;
-}
-
-bool rediwm_subsurface_tree_set_clip(struct rediwm_node *node, const struct wlr_box *clip) {
- return subsurface_tree_set_clip(node, clip);
-}
-
-// ---- xdg surfaces and drag icons -----------------------------------------
-
-struct rediwm_xdg_surface {
- struct rediwm_tree *tree;
- struct wlr_xdg_surface *xdg_surface;
- struct rediwm_tree *surface_tree;
-
- struct wl_listener tree_destroy;
- struct wl_listener xdg_surface_destroy;
- struct wl_listener xdg_surface_commit;
-};
-
-static void scene_xdg_surface_handle_tree_destroy(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_xdg_surface *scene_xdg_surface = wl_container_of(listener, scene_xdg_surface, tree_destroy);
- // tree and surface_node are cleaned up with the node
- wl_list_remove(&scene_xdg_surface->tree_destroy.link);
- wl_list_remove(&scene_xdg_surface->xdg_surface_destroy.link);
- wl_list_remove(&scene_xdg_surface->xdg_surface_commit.link);
- free(scene_xdg_surface);
-}
-
-static void scene_xdg_surface_handle_xdg_surface_destroy(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_xdg_surface *scene_xdg_surface =
- wl_container_of(listener, scene_xdg_surface, xdg_surface_destroy);
- rediwm_node_destroy(&scene_xdg_surface->tree->node);
-}
-
-static void scene_xdg_surface_update_position(struct rediwm_xdg_surface *scene_xdg_surface) {
- struct wlr_xdg_surface *xdg_surface = scene_xdg_surface->xdg_surface;
-
- rediwm_node_set_position(&scene_xdg_surface->surface_tree->node, -xdg_surface->geometry.x,
- -xdg_surface->geometry.y);
-
- if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) {
- struct wlr_xdg_popup *popup = xdg_surface->popup;
- if (popup != NULL)
- rediwm_node_set_position(&scene_xdg_surface->tree->node, popup->current.geometry.x,
- popup->current.geometry.y);
- }
-}
-
-static void scene_xdg_surface_handle_xdg_surface_commit(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_xdg_surface *scene_xdg_surface =
- wl_container_of(listener, scene_xdg_surface, xdg_surface_commit);
- scene_xdg_surface_update_position(scene_xdg_surface);
-}
-
-struct rediwm_tree *rediwm_xdg_surface_create(struct rediwm_tree *parent, struct wlr_xdg_surface *xdg_surface) {
- struct rediwm_xdg_surface *scene_xdg_surface = calloc(1, sizeof(*scene_xdg_surface));
- if (scene_xdg_surface == NULL) return NULL;
-
- scene_xdg_surface->xdg_surface = xdg_surface;
-
- scene_xdg_surface->tree = rediwm_tree_create(parent);
- if (scene_xdg_surface->tree == NULL) {
- free(scene_xdg_surface);
- return NULL;
- }
-
- scene_xdg_surface->surface_tree = rediwm_subsurface_tree_create(scene_xdg_surface->tree, xdg_surface->surface);
- if (scene_xdg_surface->surface_tree == NULL) {
- rediwm_node_destroy(&scene_xdg_surface->tree->node);
- free(scene_xdg_surface);
- return NULL;
- }
-
- scene_xdg_surface->tree_destroy.notify = scene_xdg_surface_handle_tree_destroy;
- wl_signal_add(&scene_xdg_surface->tree->node.events.destroy, &scene_xdg_surface->tree_destroy);
-
- scene_xdg_surface->xdg_surface_destroy.notify = scene_xdg_surface_handle_xdg_surface_destroy;
- wl_signal_add(&xdg_surface->events.destroy, &scene_xdg_surface->xdg_surface_destroy);
-
- scene_xdg_surface->xdg_surface_commit.notify = scene_xdg_surface_handle_xdg_surface_commit;
- wl_signal_add(&xdg_surface->surface->events.commit, &scene_xdg_surface->xdg_surface_commit);
-
- scene_xdg_surface_update_position(scene_xdg_surface);
-
- return scene_xdg_surface->tree;
-}
-
-struct rediwm_drag_icon {
- struct rediwm_tree *tree;
- struct rediwm_tree *surface_tree;
- struct wlr_drag_icon *drag_icon;
-
- struct wl_listener tree_destroy;
- struct wl_listener drag_icon_surface_commit;
- struct wl_listener drag_icon_destroy;
-};
-
-static void drag_icon_handle_surface_commit(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_drag_icon *icon = wl_container_of(listener, icon, drag_icon_surface_commit);
- struct wlr_surface *surface = icon->drag_icon->surface;
- struct rediwm_node *node = &icon->surface_tree->node;
- rediwm_node_set_position(node, node->x + surface->current.dx, node->y + surface->current.dy);
-}
-
-static void drag_icon_handle_tree_destroy(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_drag_icon *icon = wl_container_of(listener, icon, tree_destroy);
- wl_list_remove(&icon->tree_destroy.link);
- wl_list_remove(&icon->drag_icon_surface_commit.link);
- wl_list_remove(&icon->drag_icon_destroy.link);
- free(icon);
-}
-
-static void drag_icon_handle_destroy(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_drag_icon *icon = wl_container_of(listener, icon, drag_icon_destroy);
- rediwm_node_destroy(&icon->tree->node);
-}
-
-struct rediwm_tree *rediwm_drag_icon_create(struct rediwm_tree *parent, struct wlr_drag_icon *drag_icon) {
- struct rediwm_drag_icon *icon = calloc(1, sizeof(*icon));
- if (!icon) return NULL;
-
- icon->drag_icon = drag_icon;
-
- icon->tree = rediwm_tree_create(parent);
- if (!icon->tree) {
- free(icon);
- return NULL;
- }
-
- icon->surface_tree = rediwm_subsurface_tree_create(icon->tree, drag_icon->surface);
- if (!icon->surface_tree) {
- rediwm_node_destroy(&icon->tree->node);
- free(icon);
- return NULL;
- }
-
- icon->tree_destroy.notify = drag_icon_handle_tree_destroy;
- wl_signal_add(&icon->tree->node.events.destroy, &icon->tree_destroy);
- icon->drag_icon_surface_commit.notify = drag_icon_handle_surface_commit;
- wl_signal_add(&drag_icon->surface->events.commit, &icon->drag_icon_surface_commit);
- icon->drag_icon_destroy.notify = drag_icon_handle_destroy;
- wl_signal_add(&drag_icon->events.destroy, &icon->drag_icon_destroy);
-
- return icon->tree;
-}
diff --git a/src/node_surface.h b/src/node_surface.h
index 777f590..a762005 100644
--- a/src/node_surface.h
+++ b/src/node_surface.h
@@ -26,6 +26,9 @@ struct rediwm_surface {
// A node showing `surface` alone, no subsurfaces. NULL on allocation failure.
struct rediwm_surface *rediwm_surface_create(struct rediwm_tree *parent, struct wlr_surface *);
+// Clips what the node shows of the surface to `clip` (surface coordinates), or lifts the
+// clip when NULL. The subsurface tree sets it; others use rediwm_subsurface_tree_set_clip.
+void rediwm_surface_set_clip(struct rediwm_surface *surface, const struct wlr_box *clip);
// The surface and its subsurfaces, in a tree that follows them, enabled while mapped.
struct rediwm_tree *rediwm_subsurface_tree_create(struct rediwm_tree *parent, struct wlr_surface *);
diff --git a/src/node_xdg_shell.c b/src/node_xdg_shell.c
new file mode 100644
index 0000000..94be0b3
--- /dev/null
+++ b/src/node_xdg_shell.c
@@ -0,0 +1,152 @@
+// xdg surfaces and drag icons in the own scene: see node_surface.h. wlroots'
+// types/scene/xdg_shell.c and drag_icon.c (wlroots 0.20, MIT licensed) on own nodes.
+#define WLR_USE_UNSTABLE
+#define WLR_PRIVATE private
+#include "node_surface.h"
+#include <stdlib.h>
+#include <wlr/types/wlr_compositor.h>
+#include <wlr/types/wlr_data_device.h>
+#include <wlr/types/wlr_xdg_shell.h>
+
+struct rediwm_xdg_surface {
+ struct rediwm_tree *tree;
+ struct wlr_xdg_surface *xdg_surface;
+ struct rediwm_tree *surface_tree;
+
+ struct wl_listener tree_destroy;
+ struct wl_listener xdg_surface_destroy;
+ struct wl_listener xdg_surface_commit;
+};
+
+static void scene_xdg_surface_handle_tree_destroy(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_xdg_surface *scene_xdg_surface = wl_container_of(listener, scene_xdg_surface, tree_destroy);
+ // tree and surface_node are cleaned up with the node
+ wl_list_remove(&scene_xdg_surface->tree_destroy.link);
+ wl_list_remove(&scene_xdg_surface->xdg_surface_destroy.link);
+ wl_list_remove(&scene_xdg_surface->xdg_surface_commit.link);
+ free(scene_xdg_surface);
+}
+
+static void scene_xdg_surface_handle_xdg_surface_destroy(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_xdg_surface *scene_xdg_surface =
+ wl_container_of(listener, scene_xdg_surface, xdg_surface_destroy);
+ rediwm_node_destroy(&scene_xdg_surface->tree->node);
+}
+
+static void scene_xdg_surface_update_position(struct rediwm_xdg_surface *scene_xdg_surface) {
+ struct wlr_xdg_surface *xdg_surface = scene_xdg_surface->xdg_surface;
+
+ rediwm_node_set_position(&scene_xdg_surface->surface_tree->node, -xdg_surface->geometry.x,
+ -xdg_surface->geometry.y);
+
+ if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) {
+ struct wlr_xdg_popup *popup = xdg_surface->popup;
+ if (popup != NULL)
+ rediwm_node_set_position(&scene_xdg_surface->tree->node, popup->current.geometry.x,
+ popup->current.geometry.y);
+ }
+}
+
+static void scene_xdg_surface_handle_xdg_surface_commit(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_xdg_surface *scene_xdg_surface =
+ wl_container_of(listener, scene_xdg_surface, xdg_surface_commit);
+ scene_xdg_surface_update_position(scene_xdg_surface);
+}
+
+struct rediwm_tree *rediwm_xdg_surface_create(struct rediwm_tree *parent, struct wlr_xdg_surface *xdg_surface) {
+ struct rediwm_xdg_surface *scene_xdg_surface = calloc(1, sizeof(*scene_xdg_surface));
+ if (scene_xdg_surface == NULL) return NULL;
+
+ scene_xdg_surface->xdg_surface = xdg_surface;
+
+ scene_xdg_surface->tree = rediwm_tree_create(parent);
+ if (scene_xdg_surface->tree == NULL) {
+ free(scene_xdg_surface);
+ return NULL;
+ }
+
+ scene_xdg_surface->surface_tree = rediwm_subsurface_tree_create(scene_xdg_surface->tree, xdg_surface->surface);
+ if (scene_xdg_surface->surface_tree == NULL) {
+ rediwm_node_destroy(&scene_xdg_surface->tree->node);
+ free(scene_xdg_surface);
+ return NULL;
+ }
+
+ scene_xdg_surface->tree_destroy.notify = scene_xdg_surface_handle_tree_destroy;
+ wl_signal_add(&scene_xdg_surface->tree->node.events.destroy, &scene_xdg_surface->tree_destroy);
+
+ scene_xdg_surface->xdg_surface_destroy.notify = scene_xdg_surface_handle_xdg_surface_destroy;
+ wl_signal_add(&xdg_surface->events.destroy, &scene_xdg_surface->xdg_surface_destroy);
+
+ scene_xdg_surface->xdg_surface_commit.notify = scene_xdg_surface_handle_xdg_surface_commit;
+ wl_signal_add(&xdg_surface->surface->events.commit, &scene_xdg_surface->xdg_surface_commit);
+
+ scene_xdg_surface_update_position(scene_xdg_surface);
+
+ return scene_xdg_surface->tree;
+}
+
+struct rediwm_drag_icon {
+ struct rediwm_tree *tree;
+ struct rediwm_tree *surface_tree;
+ struct wlr_drag_icon *drag_icon;
+
+ struct wl_listener tree_destroy;
+ struct wl_listener drag_icon_surface_commit;
+ struct wl_listener drag_icon_destroy;
+};
+
+static void drag_icon_handle_surface_commit(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_drag_icon *icon = wl_container_of(listener, icon, drag_icon_surface_commit);
+ struct wlr_surface *surface = icon->drag_icon->surface;
+ struct rediwm_node *node = &icon->surface_tree->node;
+ rediwm_node_set_position(node, node->x + surface->current.dx, node->y + surface->current.dy);
+}
+
+static void drag_icon_handle_tree_destroy(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_drag_icon *icon = wl_container_of(listener, icon, tree_destroy);
+ wl_list_remove(&icon->tree_destroy.link);
+ wl_list_remove(&icon->drag_icon_surface_commit.link);
+ wl_list_remove(&icon->drag_icon_destroy.link);
+ free(icon);
+}
+
+static void drag_icon_handle_destroy(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_drag_icon *icon = wl_container_of(listener, icon, drag_icon_destroy);
+ rediwm_node_destroy(&icon->tree->node);
+}
+
+struct rediwm_tree *rediwm_drag_icon_create(struct rediwm_tree *parent, struct wlr_drag_icon *drag_icon) {
+ struct rediwm_drag_icon *icon = calloc(1, sizeof(*icon));
+ if (!icon) return NULL;
+
+ icon->drag_icon = drag_icon;
+
+ icon->tree = rediwm_tree_create(parent);
+ if (!icon->tree) {
+ free(icon);
+ return NULL;
+ }
+
+ icon->surface_tree = rediwm_subsurface_tree_create(icon->tree, drag_icon->surface);
+ if (!icon->surface_tree) {
+ rediwm_node_destroy(&icon->tree->node);
+ free(icon);
+ return NULL;
+ }
+
+ icon->tree_destroy.notify = drag_icon_handle_tree_destroy;
+ wl_signal_add(&icon->tree->node.events.destroy, &icon->tree_destroy);
+ icon->drag_icon_surface_commit.notify = drag_icon_handle_surface_commit;
+ wl_signal_add(&drag_icon->surface->events.commit, &icon->drag_icon_surface_commit);
+ icon->drag_icon_destroy.notify = drag_icon_handle_destroy;
+ wl_signal_add(&drag_icon->events.destroy, &icon->drag_icon_destroy);
+
+ return icon->tree;
+}