Skip to content

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

Stage 7b: client surfaces are own scene nodes

oxydizer186fb2c09d6e
diff --git a/AGENTS.md b/AGENTS.md
index 583e225..3377d46 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -34,15 +34,16 @@ panel painting. The key rules:
* **Scene nodes are ours** (`node.zig`/`node.c`, wlr_scene's method names). A
buffer or rect node keeps its pixels in a hidden wlr_scene *carrier* that the
output pass reads; every setter keeps the two in step, and every change asks
- for a frame through the scene's hook. Client surfaces are still wlr_scene
- trees, hosted under the scene's disabled host and placed by an own `Foreign`
- node: their `data` points back at it (`Foreign.of`, `scene_data.fromWlrNode`),
- and positions inside them go through `node.wlrCoords`. Hits come from
- `Projection.nodeAt` (own node, or `foreign` for a client surface). A sync
- resolves only the top-level subtrees (groups) whose nodes changed, and nothing
- when no group and not the camera did: a change the node setters cannot see (done
- to a hosted wlr_scene subtree) must call `Node.touch`, and a node whose geometry
- depends on another group's (a glass effect) is walked every sync. The
+ for a frame through the scene's hook. Client surfaces are own nodes too
+ (`node_surface.c`, ported from wlr_scene_surface/subsurface_tree/xdg_shell/
+ drag_icon): a surface is a buffer node whose `surface` binding follows its
+ wl_surface, an xdg surface a tree. The carrier's signals (`outputs_update`,
+ `output_sample`, `frame_done`) are where the binding listens, so the projection
+ raises them on the carrier. Hits come from `Projection.nodeAt` (a leaf node; a
+ client surface has `buffer.surface`). A sync resolves only the top-level subtrees
+ (groups) whose nodes changed, and nothing when no group and not the camera did:
+ a change the node setters cannot see must call `Node.touch`, and a node whose
+ geometry depends on another group's (a glass effect) is walked every sync. The
`-Ddamage-audit` build logs any item the gate left stale.
* **The world has no second tree:** its source tree stays disabled. Pick with
`rediwm_projection_node_at`, send callbacks through `Server.sendFrameDone`,
diff --git a/build.zig b/build.zig
index 9f8d883..967df74 100644
--- a/build.zig
+++ b/build.zig
@@ -817,6 +817,7 @@ fn addGlass(b: *std.Build, module: *std.Build.Module, damage_audit: bool) void {
module.linkSystemLibrary("glesv2", .{});
module.addCSourceFile(.{ .file = b.path("src/projection.c"), .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.addCSourceFile(.{ .file = b.path("src/scene_pass.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
module.addCSourceFile(.{ .file = b.path("src/scene_output.c"), .flags = if (damage_audit) &.{ "-std=c11", "-Wall", "-Wextra", "-Werror", "-DREDIWM_DAMAGE_AUDIT" } else &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
module.addCSourceFile(.{ .file = b.path("src/scene_damage.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
diff --git a/docs/plans/scene-graph-stage7.md b/docs/plans/scene-graph-stage7.md
index 8ef2e5e..04ccb2b 100644
--- a/docs/plans/scene-graph-stage7.md
+++ b/docs/plans/scene-graph-stage7.md
@@ -119,6 +119,17 @@ the same last error line (`scene_baseline` is a benchmark, not run).
of call sites, layout trees first, then the compositor-drawn UI; windows keep
`wlr.SceneTree` under a foreign node until 7b.
+**7b landed 2026-10-16.** `node_surface.c` ports wlr_scene_surface, subsurface_tree,
+xdg_shell and drag_icon onto own nodes; a client surface is an own buffer node whose
+`surface` binding listens to its carrier's `outputs_update`/`output_sample`/`frame_done`
+(the projection still raises them there until 7c). `Foreign`, the wlr walk in the
+projection and the `_wlr_` setters are gone; layer-surface placement is Zig
+(`LayerSurface.place`, wrapping arithmetic) because the C side would need a generated
+layer-shell header. Hosted-subtree `touch` calls are no longer needed. Gates: unit and C
+suites pass; integration batch: the same failures as HEAD plus flakes that also fail on
+the previous commit (`input_persistence`, `xwayland`, `xwayland_fractional`: xrdb sees no
+`Xft.dpi` about half the runs).
+
## Design
An own node module, C core with a thin Zig wrapper, living beside `scene_graph.h`:
diff --git a/src/Input.zig b/src/Input.zig
index 7a181b7..7ea8825 100644
--- a/src/Input.zig
+++ b/src/Input.zig
@@ -135,7 +135,7 @@ open_power_menu: ?*PowerMenu = null,
new_input: wl.Listener(*wlr.InputDevice) = .init(newInput),
request_set_cursor: wl.Listener(*wlr.Seat.event.RequestSetCursor) = .init(requestSetCursor),
request_set_shape: wl.Listener(*wlr.CursorShapeManagerV1.event.RequestSetShape) = .init(requestSetShape),
-drag_icon_tree: ?*sn.Foreign = null,
+drag_icon_tree: ?*sn.Tree = null,
start_drag: wl.Listener(*wlr.Drag) = .init(startDrag),
drag_icon_destroy: wl.Listener(void) = .init(dragIconDestroyed),
request_start_drag: wl.Listener(*wlr.Seat.event.RequestStartDrag) = .init(requestStartDrag),
diff --git a/src/KdeDecoration.zig b/src/KdeDecoration.zig
index 5a89853..a80a239 100644
--- a/src/KdeDecoration.zig
+++ b/src/KdeDecoration.zig
@@ -84,7 +84,7 @@ pub const Manager = struct {
// walk popup parents or subsurface roots here.
if (wlr.XdgSurface.tryFromWlrSurface(surface)) |xdg| {
if (scene_data.xdgSceneTree(xdg)) |tree| {
- if (scene_data.fromWlrNode(&tree.node)) |data| {
+ if (scene_data.SceneData.fromNodeOrParents(&tree.node)) |data| {
if (data.role == .toplevel and data.role.toplevel.surface() == surface) {
decoration.toplevel = data.role.toplevel;
data.role.toplevel.kde_decoration = decoration;
diff --git a/src/LayerSurface.zig b/src/LayerSurface.zig
index 6fd2533..01b9e95 100644
--- a/src/LayerSurface.zig
+++ b/src/LayerSurface.zig
@@ -1,4 +1,4 @@
-//! Layer-shell scene integration. SceneLayerSurfaceV1 owns mapping and subsurfaces.
+//! Layer-shell scene integration. The scene tree follows the surface and its subsurfaces.
const std = @import("std");
const wl = @import("wayland").server.wl;
const wlr = @import("wlroots");
@@ -10,10 +10,8 @@ const gpa = @import("main.zig").gpa;
const Self = @This();
server: *Server,
layer: *wlr.LayerSurfaceV1,
-scene: *wlr.SceneLayerSurfaceV1,
-/// Places the layer's wlr_scene subtree in our scene; the layer shell positions the
-/// subtree's own root.
-foreign: *sn.Foreign,
+/// The layer's root: placed by `place`, holding the surface and its subsurfaces.
+tree: *sn.Tree,
identity: SceneData,
link: wl.list.Link = undefined,
commit: wl.Listener(*wlr.Surface) = .init(committed),
@@ -29,9 +27,11 @@ pub fn create(server: *Server, layer: *wlr.LayerSurfaceV1) !void {
layer.output = output;
const self = try gpa.create(Self);
errdefer gpa.destroy(self);
- const created = try server.desktop_tree.createSceneLayerSurfaceV1(layer);
- self.* = .{ .server = server, .layer = layer, .scene = created.scene, .foreign = created.foreign, .identity = .{ .role = .{ .layer = self } } };
- self.identity.attach(&self.foreign.node);
+ const tree = try server.desktop_tree.createSceneTree();
+ errdefer tree.node.destroy();
+ _ = try tree.createSceneSubsurfaceTree(layer.surface);
+ self.* = .{ .server = server, .layer = layer, .tree = tree, .identity = .{ .role = .{ .layer = self } } };
+ self.identity.attach(&self.tree.node);
server.layer_surfaces.prepend(self);
layer.data = self;
layer.surface.events.commit.add(&self.commit);
@@ -57,7 +57,7 @@ fn defaultOutput(server: *Server) ?*wlr.Output {
pub fn localPoint(self: *Self, x: f64, y: f64) @import("geometry.zig").Vec2 {
var ox: i32 = 0;
var oy: i32 = 0;
- _ = sn.wlrCoords(&self.scene.tree.node, &ox, &oy);
+ _ = self.tree.node.coords(&ox, &oy);
return @import("geometry.zig").toLocal(x, y, ox, oy);
}
@@ -68,7 +68,7 @@ pub fn arrange(self: *Self) void {
.overlay => self.server.overlay_tree,
_ => self.server.desktop_tree,
};
- self.foreign.node.reparent(parent);
+ self.tree.node.reparent(parent);
var full: wlr.Box = undefined;
self.server.output_layout.getBox(self.layer.output, &full);
const output = Output.fromWlr(self.layer.output.?);
@@ -77,9 +77,68 @@ pub fn arrange(self: *Self) void {
};
var usable = full;
if (output) |out| usable = out.usableBox();
- self.scene.configure(&full, &usable);
- self.foreign.node.touch();
+ self.place(&full, &usable);
}
+/// Places the surface by its anchors and margins in `full` or `usable`, sends the
+/// configure, and takes its exclusive zone out of `usable` (wlr_scene_layer_surface_v1_configure).
+/// The values are the client's: all arithmetic wraps rather than traps.
+fn place(self: *Self, full: *const wlr.Box, usable: *wlr.Box) void {
+ const state = &self.layer.current;
+ // An exclusive zone of -1 takes the whole output, otherwise what is left.
+ const bounds: wlr.Box = if (state.exclusive_zone == -1) full.* else usable.*;
+ const margin = state.margin;
+ const anchor = state.anchor;
+ var box: wlr.Box = .{ .x = 0, .y = 0, .width = @bitCast(state.desired_width), .height = @bitCast(state.desired_height) };
+
+ if (box.width == 0) {
+ box.x = bounds.x +% margin.left;
+ box.width = bounds.width -% (margin.left +% margin.right);
+ } else if (anchor.left and anchor.right) {
+ box.x = bounds.x +% @divTrunc(bounds.width, 2) -% @divTrunc(box.width, 2);
+ } else if (anchor.left) {
+ box.x = bounds.x +% margin.left;
+ } else if (anchor.right) {
+ box.x = bounds.x +% bounds.width -% box.width -% margin.right;
+ } else {
+ box.x = bounds.x +% @divTrunc(bounds.width, 2) -% @divTrunc(box.width, 2);
+ }
+
+ if (box.height == 0) {
+ box.y = bounds.y +% margin.top;
+ box.height = bounds.height -% (margin.top +% margin.bottom);
+ } else if (anchor.top and anchor.bottom) {
+ box.y = bounds.y +% @divTrunc(bounds.height, 2) -% @divTrunc(box.height, 2);
+ } else if (anchor.top) {
+ box.y = bounds.y +% margin.top;
+ } else if (anchor.bottom) {
+ box.y = bounds.y +% bounds.height -% box.height -% margin.bottom;
+ } else {
+ box.y = bounds.y +% @divTrunc(bounds.height, 2) -% @divTrunc(box.height, 2);
+ }
+
+ self.tree.node.setPosition(box.x, box.y);
+ _ = self.layer.configure(@bitCast(box.width), @bitCast(box.height));
+
+ if (self.layer.surface.mapped and state.exclusive_zone > 0) {
+ const zone = state.exclusive_zone;
+ const edge = wlr_layer_surface_v1_get_exclusive_edge(self.layer);
+ if (edge.top) {
+ usable.y +%= zone +% margin.top;
+ usable.height -%= zone +% margin.top;
+ } else if (edge.bottom) {
+ usable.height -%= zone +% margin.bottom;
+ } else if (edge.left) {
+ usable.x +%= zone +% margin.left;
+ usable.width -%= zone +% margin.left;
+ } else if (edge.right) {
+ usable.width -%= zone +% margin.right;
+ }
+ usable.width = @max(usable.width, 0);
+ usable.height = @max(usable.height, 0);
+ }
+}
+extern fn wlr_layer_surface_v1_get_exclusive_edge(surface: *wlr.LayerSurfaceV1) wlr.Edges;
+
fn committed(listener: *wl.Listener(*wlr.Surface), _: *wlr.Surface) void {
const self: *Self = @fieldParentPtr("commit", listener);
if (self.layer.initial_commit or @as(u32, @bitCast(self.layer.current.committed)) != 0) self.arrange();
@@ -98,6 +157,7 @@ fn outputDestroyed(listener: *wl.Listener(*wlr.Output), _: *wlr.Output) void {
fn destroyed(listener: *wl.Listener(*wlr.LayerSurfaceV1), _: *wlr.LayerSurfaceV1) void {
const self: *Self = @fieldParentPtr("destroy", listener);
const server = self.server;
+ self.tree.node.destroy();
self.commit.link.remove();
self.destroy.link.remove();
self.output_destroy.link.remove();
diff --git a/src/Popup.zig b/src/Popup.zig
index 3fac536..d71458f 100644
--- a/src/Popup.zig
+++ b/src/Popup.zig
@@ -46,7 +46,7 @@ pub fn create(server: *Server, xdg_popup: *wlr.XdgPopup) !void {
errdefer scene_tree.node.destroy(); // gpa.create below must not leak the node
// The popup hangs under its parent's tree, which a window's rounded shape
// clips; a menu is no part of that window's shape and may leave it.
- _ = @import("projection.zig").setWlrTreeUnclipped(&scene_tree.node, true);
+ _ = @import("projection.zig").setTreeUnclipped(&scene_tree.node, true);
const popup = gpa.create(Popup) catch |err| {
log.err("Popup.create: could not allocate popup: {}", .{err});
@@ -88,7 +88,7 @@ fn unconstrainToOutput(server: *Server, xdg_popup: *wlr.XdgPopup) void {
const root_tree = scene_data.xdgSceneTree(root) orelse return;
var ox: i32 = 0;
var oy: i32 = 0;
- _ = sn.wlrCoords(&root_tree.node, &ox, &oy);
+ _ = root_tree.node.coords(&ox, &oy);
const box = (if (Toplevel.fromSurface(server, root.surface)) |toplevel|
windowConstraintBox(server, toplevel, ox, oy)
diff --git a/src/Shell.zig b/src/Shell.zig
index e455010..7b8d628 100644
--- a/src/Shell.zig
+++ b/src/Shell.zig
@@ -70,7 +70,7 @@ fn newXdgDecoration(
decoration: *wlr.XdgToplevelDecorationV1,
) void {
const tree = scene_data.xdgSceneTree(decoration.toplevel.base) orelse return;
- const data = scene_data.fromWlrNode(&tree.node) orelse return;
+ const data = scene_data.SceneData.fromNodeOrParents(&tree.node) orelse return;
const toplevel = switch (data.role) {
.toplevel => |t| t,
.chrome => |t| t,
diff --git a/src/Toplevel.zig b/src/Toplevel.zig
index 2a08d94..fdcffb3 100644
--- a/src/Toplevel.zig
+++ b/src/Toplevel.zig
@@ -161,7 +161,7 @@ frame_fills: [3]*sn.Rect,
// The outline of a window that draws its own decorations.
border_rects: [4]*sn.Rect,
content_tree: *sn.Tree,
-/// What the client draws into: the hosted surface tree of an xdg or X11 surface, or a
+/// What the client draws into: the surface tree of an xdg or X11 surface, or a
/// bare placeholder tree while there is none.
scene_tree: *sn.Node,
decoration: ?*Decoration = null,
@@ -447,7 +447,7 @@ pub fn create(server: *Server, xdg_toplevel: *wlr.XdgToplevel) !void {
};
server.next_toplevel_id += 1;
finishCreate(toplevel, nodes, scene_tree);
- scene_data.setXdgSceneTree(xdg_surface, hosted.tree());
+ scene_data.setXdgSceneTree(xdg_surface, hosted);
server.shell.kde_decorations.attach(toplevel);
switch (toplevel.backend) {
.xdg => |*adapter| adapter.listen(),
@@ -1847,9 +1847,9 @@ fn setSizeClip(toplevel: *Toplevel, clip: ?wlr.Box) void {
toplevel.last_clip_valid = true;
if (clip) |box| {
var copy = box;
- if (toplevel.scene_tree.foreign()) |hosted| hosted.subsurfaceTreeSetClip(&copy);
+ toplevel.scene_tree.subsurfaceTreeSetClip(&copy);
} else {
- if (toplevel.scene_tree.foreign()) |hosted| hosted.subsurfaceTreeSetClip(null);
+ toplevel.scene_tree.subsurfaceTreeSetClip(null);
}
}
@@ -1860,8 +1860,7 @@ fn applySizeClip(toplevel: *Toplevel) void {
// here through handleIdentityChanged -> syncChrome) before its X11
// window is associated with a wl_surface and attachXwaylandScene has run.
// scene_tree is then still the bare placeholder tree from createXwayland,
- // with none of the subsurface-tree bookkeeping wlr_scene_subsurface_tree_set_clip
- // requires — calling it anyway aborts the whole compositor.
+ // with no subsurface tree to clip.
if (!toplevel.isInitialized()) return;
if (toplevel.size_w.settle_ms == 0 and toplevel.size_h.settle_ms == 0) {
toplevel.setSizeClip(null);
@@ -1944,8 +1943,8 @@ const CloseSnapshot = struct {
ok: bool = true,
};
-fn copyCloseBuffer(buffer: *wlr.SceneBuffer, sx: c_int, sy: c_int, ctx: *CloseSnapshot) void {
- const src = buffer.buffer orelse return;
+fn copyCloseBuffer(buffer: *sn.Buffer, sx: c_int, sy: c_int, ctx: *CloseSnapshot) void {
+ const src = buffer.getBuffer() orelse return;
const dest = ctx.tree.createSceneBuffer(src) catch {
ctx.ok = false;
return;
@@ -3127,13 +3126,6 @@ pub fn applyOpacity(toplevel: *Toplevel, now_ms: i64) void {
buffer.setOpacity(value.*);
}
}.iter, &opacity);
- toplevel.frame_tree.node.forEachSurfaceBuffer(*f32, struct {
- fn iter(buffer: *wlr.SceneBuffer, _: c_int, _: c_int, value: *f32) void {
- buffer.setOpacity(value.*);
- }
- }.iter, &opacity);
- // The surfaces' opacity is set on wlr_scene nodes, which tell nobody.
- if (opacity != toplevel.shape_opacity) toplevel.frame_tree.node.touch();
const color = toplevel.borderColor().scale(opacity);
for (toplevel.border_rects) |rect| col.setRect(rect, color);
for (toplevel.frame_fills) |rect| col.setRect(rect, chrome.fillColor().scale(opacity));
diff --git a/src/capture/window_source.zig b/src/capture/window_source.zig
index 8e21b91..2a8fdee 100644
--- a/src/capture/window_source.zig
+++ b/src/capture/window_source.zig
@@ -252,9 +252,9 @@ pub const WindowCaptureSource = struct {
.off_y = geom.y,
.shape = self.frameShape(geom.x, geom.y),
};
- // The compositor's own buffers (a shell window's), then the clients' surfaces.
+ // Every buffer of the subtree, bottom to top: a shell window's own, then the
+ // clients' surfaces.
self.content_node.forEachBuffer(*RenderCtx, renderOwnBuffer, &ctx);
- self.content_node.forEachSurfaceBuffer(*RenderCtx, renderNodeBuffer, &ctx);
const submitted = pass.submit();
explicit_sync.ReadFence.end(server.read_fence, submitted);
@@ -281,16 +281,14 @@ pub const WindowCaptureSource = struct {
/// already relies on `setEnabled(false)` to hide nodes from real output
/// rendering elsewhere (e.g. `screenshot/capture.zig`'s isolation mode).
fn renderOwnBuffer(buffer: *sn.Buffer, sx: c_int, sy: c_int, ctx: *RenderCtx) void {
- renderNodeBuffer(buffer.carrier, sx, sy, ctx);
+ renderNodeBuffer(buffer.carrier, if (buffer.surface) |scene_surface| scene_surface.surface else null, sx, sy, ctx);
}
- fn renderNodeBuffer(scene_buffer: *wlr.SceneBuffer, sx: c_int, sy: c_int, ctx: *RenderCtx) void {
+ fn renderNodeBuffer(scene_buffer: *wlr.SceneBuffer, surface: ?*wlr.Surface, sx: c_int, sy: c_int, ctx: *RenderCtx) void {
const buf = scene_buffer.buffer orelse return;
const texture = wlr.Texture.fromBuffer(ctx.renderer, buf) orelse return;
defer texture.destroy();
- if (wlr.SceneSurface.tryFromBuffer(scene_buffer)) |scene_surface| {
- explicit_sync.ReadFence.note(ctx.read_fence, scene_surface.surface);
- }
+ if (surface) |client| explicit_sync.ReadFence.note(ctx.read_fence, client);
var alpha = scene_buffer.opacity;
var luminance_multiplier: f32 = 1.0;
diff --git a/src/input/text_input.zig b/src/input/text_input.zig
index 4604bf3..041eb0e 100644
--- a/src/input/text_input.zig
+++ b/src/input/text_input.zig
@@ -342,31 +342,16 @@ fn newPopup(listener: *wl.Listener(*wlr.InputPopupSurfaceV2), popup: *wlr.InputP
self.updatePopups();
}
-fn wlrSurfaceNode(tree: *wlr.SceneTree, surface: *wlr.Surface) ?*wlr.SceneNode {
- var it = tree.children.iterator(.forward);
- while (it.next()) |node| {
- if (node.type == .tree) {
- if (wlrSurfaceNode(wlr.SceneTree.fromNode(node), surface)) |found| return found;
- } else if (node.type == .buffer) {
- if (wlr.SceneSurface.tryFromBuffer(wlr.SceneBuffer.fromNode(node))) |scene_surface| {
- if (scene_surface.surface == surface) return node;
- }
- }
- }
- return null;
-}
-
-/// The node showing `surface` under `tree`: a client's surfaces are wlr_scene nodes
-/// inside the foreign nodes that host them.
-fn surfaceNode(tree: *sn.Tree, surface: *wlr.Surface) ?*wlr.SceneNode {
+/// The node showing `surface` under `tree`.
+fn surfaceNode(tree: *sn.Tree, surface: *wlr.Surface) ?*sn.Node {
var it = tree.children.iterator(.forward);
while (it.next()) |node| {
switch (node.type) {
.tree => if (surfaceNode(@fieldParentPtr("node", node), surface)) |found| return found,
- .foreign => if (node.foreign().?.wlr != null) {
- if (wlrSurfaceNode(node.foreign().?.tree(), surface)) |found| return found;
+ .buffer => if (node.buffer().?.surface) |scene_surface| {
+ if (scene_surface.surface == surface) return node;
},
- else => {},
+ .rect => {},
}
}
return null;
@@ -386,7 +371,7 @@ pub fn updatePopups(self: *Self) void {
const node = surfaceNode(&self.server.nodes.tree, surface) orelse break :visible false;
var sx: i32 = 0;
var sy: i32 = 0;
- _ = sn.wlrCoords(node, &sx, &sy);
+ _ = node.coords(&sx, &sy);
var scale: f64 = 1;
var x: f64 = @floatFromInt(sx);
var y: f64 = @floatFromInt(sy);
@@ -447,7 +432,7 @@ pub fn popupPoint(self: *Self, surface: *wlr.Surface, x: f64, y: f64) ?@import("
const node = surfaceNode(self.server.ime_tree, surface) orelse return null;
var sx: i32 = 0;
var sy: i32 = 0;
- _ = sn.wlrCoords(node, &sx, &sy);
+ _ = node.coords(&sx, &sy);
return .{ .x = x - @as(f64, @floatFromInt(sx)), .y = y - @as(f64, @floatFromInt(sy)) };
}
diff --git a/src/ipc/handlers.zig b/src/ipc/handlers.zig
index 0c11002..4027f73 100644
--- a/src/ipc/handlers.zig
+++ b/src/ipc/handlers.zig
@@ -529,8 +529,8 @@ pub fn handleRequest(
.keyboard_interactive = ls.layer.current.keyboard_interactive != .none,
.mapped = ls.layer.surface.mapped,
.box = .{
- .x = ls.scene.tree.node.x,
- .y = ls.scene.tree.node.y,
+ .x = ls.tree.node.x,
+ .y = ls.tree.node.y,
.width = ls.layer.surface.current.width,
.height = ls.layer.surface.current.height,
},
diff --git a/src/mini_map.zig b/src/mini_map.zig
index 8e01e65..6c45bbf 100644
--- a/src/mini_map.zig
+++ b/src/mini_map.zig
@@ -242,7 +242,7 @@ pub fn tick(self: *Self, output: *Output, now: i64) bool {
if (!self.dragging) {
const picked = server.world.projection.nodeAt(server.input.cursor.x, server.input.cursor.y);
self.hovered = server.input.cursor_mode == .passthrough and server.input.active_buttons == 0 and
- picked != null and picked.?.own != null and SceneData.fromNode(picked.?.own.?) == &self.scene_data;
+ picked != null and SceneData.fromNode(picked.?.own) == &self.scene_data;
}
const changed = self.last_camera == null or !std.meta.eql(self.last_camera.?, world.camera);
self.last_camera = world.camera;
diff --git a/src/node.c b/src/node.c
index d81c907..2894cc0 100644
--- a/src/node.c
+++ b/src/node.c
@@ -108,25 +108,6 @@ struct rediwm_buffer *rediwm_buffer_create(struct rediwm_tree *parent, struct wl
return b;
}
-static void foreign_wlr_destroyed(struct wl_listener *listener, void *data) {
- (void)data;
- struct rediwm_foreign *f = wl_container_of(listener, f, wlr_destroy);
- wl_list_remove(&f->wlr_destroy.link);
- f->wlr = NULL;
- rediwm_node_destroy(&f->node);
-}
-
-struct rediwm_foreign *rediwm_foreign_create(struct rediwm_tree *parent, struct wlr_scene_node *wlr) {
- struct rediwm_foreign *f = calloc(1, sizeof(*f));
- if (!f) return NULL;
- node_init(&f->node, REDIWM_NODE_FOREIGN, parent);
- f->wlr = wlr;
- f->wlr_destroy.notify = foreign_wlr_destroyed;
- wl_signal_add(&wlr->events.destroy, &f->wlr_destroy);
- structure(&f->node);
- return f;
-}
-
struct wlr_scene_tree *rediwm_tree_host(const struct rediwm_tree *tree) {
return tree->node.scene->host;
}
@@ -138,14 +119,6 @@ void rediwm_node_destroy(struct rediwm_node *node) {
struct rediwm_tree *tree = (struct rediwm_tree *)node;
struct rediwm_node *child, *tmp;
wl_list_for_each_safe(child, tmp, &tree->children, link) rediwm_node_destroy(child);
- } else if (node->type == REDIWM_NODE_FOREIGN) {
- struct rediwm_foreign *f = (struct rediwm_foreign *)node;
- if (f->wlr) {
- wl_list_remove(&f->wlr_destroy.link);
- struct wlr_scene_node *wlr = f->wlr;
- f->wlr = NULL;
- wlr_scene_node_destroy(wlr);
- }
} else if (node->type == REDIWM_NODE_RECT) {
wlr_scene_node_destroy(&((struct rediwm_rect *)node)->carrier->node);
} else if (node->type == REDIWM_NODE_BUFFER) {
@@ -271,6 +244,15 @@ void rediwm_buffer_set_buffer(struct rediwm_buffer *b, struct wlr_buffer *buffer
changed(&b->node);
}
+void rediwm_buffer_set_buffer_with_options(struct rediwm_buffer *b, struct wlr_buffer *buffer,
+ const struct wlr_scene_buffer_set_buffer_options *options) {
+ wlr_scene_buffer_set_buffer_with_options(b->carrier, buffer, options);
+ b->buffer_width = buffer ? buffer->width : 0;
+ b->buffer_height = buffer ? buffer->height : 0;
+ buffer_damaged(b, options ? options->damage : NULL);
+ changed(&b->node);
+}
+
void rediwm_buffer_set_buffer_with_damage(struct rediwm_buffer *b, struct wlr_buffer *buffer,
const pixman_region32_t *damage) {
wlr_scene_buffer_set_buffer_with_damage(b->carrier, buffer, damage);
@@ -376,8 +358,6 @@ struct rediwm_node *rediwm_node_at(struct rediwm_node *node, double x, double y,
if (b->point_accepts_input && !b->point_accepts_input(b, &x, &y)) return NULL;
break;
}
- case REDIWM_NODE_FOREIGN:
- return NULL;
}
if (nx) *nx = x;
if (ny) *ny = y;
@@ -408,38 +388,3 @@ void rediwm_node_for_each_buffer(struct rediwm_node *node, rediwm_buffer_iterato
fn((struct rediwm_buffer *)node, 0, 0, data);
}
}
-
-struct wlr_iteration {
- rediwm_wlr_buffer_iterator fn;
- void *data;
- int x, y;
-};
-static void wlr_buffer_shifted(struct wlr_scene_buffer *b, int sx, int sy, void *data) {
- struct wlr_iteration *it = data;
- it->fn(b, it->x + sx, it->y + sy, it->data);
-}
-static void for_each_wlr_buffer(struct rediwm_node *node, int x, int y, rediwm_wlr_buffer_iterator fn, void *data) {
- if (!node->enabled) return;
- x += node->x;
- y += node->y;
- if (node->type == REDIWM_NODE_TREE) {
- struct rediwm_node *child;
- wl_list_for_each(child, &((struct rediwm_tree *)node)->children, link)
- for_each_wlr_buffer(child, x, y, fn, data);
- } else if (node->type == REDIWM_NODE_FOREIGN) {
- struct rediwm_foreign *f = (struct rediwm_foreign *)node;
- struct wlr_iteration it = {.fn = fn, .data = data, .x = x, .y = y};
- if (f->wlr) wlr_scene_node_for_each_buffer(f->wlr, wlr_buffer_shifted, &it);
- }
-}
-
-void rediwm_node_for_each_wlr_buffer(struct rediwm_node *node, rediwm_wlr_buffer_iterator fn, void *data) {
- if (!node->enabled) return;
- if (node->type == REDIWM_NODE_TREE) {
- struct rediwm_node *child;
- wl_list_for_each(child, &((struct rediwm_tree *)node)->children, link)
- for_each_wlr_buffer(child, 0, 0, fn, data);
- } else if (node->type == REDIWM_NODE_FOREIGN) {
- for_each_wlr_buffer(node, -node->x, -node->y, fn, data);
- }
-}
diff --git a/src/node.h b/src/node.h
index 226fb34..55939eb 100644
--- a/src/node.h
+++ b/src/node.h
@@ -34,9 +34,6 @@ enum rediwm_node_type {
REDIWM_NODE_TREE,
REDIWM_NODE_RECT,
REDIWM_NODE_BUFFER,
- // A wlr_scene subtree hosted inside an own tree: the client surface trees
- // wlr_scene_xdg_surface and friends build. Stage 7b removes it.
- REDIWM_NODE_FOREIGN,
};
struct rediwm_node {
@@ -70,6 +67,7 @@ struct rediwm_rect {
};
struct rediwm_buffer;
+struct rediwm_surface;
typedef bool (*rediwm_buffer_point_accepts_input)(struct rediwm_buffer *, double *x, double *y);
// Geometry and content state live here; the pixels themselves (the locked buffer,
@@ -88,20 +86,14 @@ struct rediwm_buffer {
pixman_region32_t opaque_region;
// The size of the buffer as last set, kept for when the renderer releases it.
int buffer_width, buffer_height;
-};
-
-// A wlr_scene subtree held by an own tree. `wlr` is the subtree's root node; its
-// own position stays (0, 0) and the foreign node carries the position.
-struct rediwm_foreign {
- struct rediwm_node node;
- struct wlr_scene_node *wlr; // NULL once wlroots has destroyed it
- struct wl_listener wlr_destroy;
+ // The client surface this node shows, or NULL (node_surface.h).
+ struct rediwm_surface *surface;
};
struct rediwm_scene {
struct rediwm_tree tree;
- // A hidden, disabled wlr_scene tree that hosts every buffer's carrier and the
- // foreign subtrees, so wlroots computes nothing for them.
+ // A hidden, disabled wlr_scene tree that hosts every buffer's carrier, so wlroots
+ // computes nothing for them.
struct wlr_scene_tree *host;
// Something the tree shows may have changed: the owner schedules a frame.
// `node` is the one that changed (NULL when it is gone); `structural` when nodes
@@ -123,12 +115,8 @@ static inline struct rediwm_rect *rediwm_rect_from_node(struct rediwm_node *node
static inline struct rediwm_buffer *rediwm_buffer_from_node(struct rediwm_node *node) {
return node->type == REDIWM_NODE_BUFFER ? (struct rediwm_buffer *)node : NULL;
}
-static inline struct rediwm_foreign *rediwm_foreign_from_node(struct rediwm_node *node) {
- return node->type == REDIWM_NODE_FOREIGN ? (struct rediwm_foreign *)node : NULL;
-}
-// A scene is its root tree. `host` is where carriers and foreign subtrees live (the
-// caller disables it); `changed` may be NULL.
+// A scene is its root tree. `host` is where carriers live (the caller disables it); `changed` may be NULL.
struct rediwm_scene *rediwm_scene_create(struct wlr_scene_tree *host,
void (*changed)(void *, struct rediwm_node *, bool),
void *data);
@@ -140,16 +128,12 @@ struct rediwm_tree *rediwm_tree_create(struct rediwm_tree *parent);
struct rediwm_rect *rediwm_rect_create(struct rediwm_tree *parent, int width, int height,
const float color[4]);
struct rediwm_buffer *rediwm_buffer_create(struct rediwm_tree *parent, struct wlr_buffer *);
-// Takes over `wlr`, a node of a wlr_scene tree under the scene's host. It goes when
-// the foreign node does, and the foreign node goes when wlroots destroys it first.
-struct rediwm_foreign *rediwm_foreign_create(struct rediwm_tree *parent, struct wlr_scene_node *wlr);
-// Where the scene's carriers and foreign subtrees are made.
+// Where the scene's carriers are made.
struct wlr_scene_tree *rediwm_tree_host(const struct rediwm_tree *);
// Destroys the node and everything under it; each node emits events.destroy first.
void rediwm_node_destroy(struct rediwm_node *);
-// Reports a change the node itself cannot see (something done to the wlr_scene subtree
-// it hosts): asks for a frame.
+// Reports a change the node's setters cannot see: asks for a frame.
void rediwm_node_touch(struct rediwm_node *);
void rediwm_node_set_enabled(struct rediwm_node *, bool enabled);
void rediwm_node_set_position(struct rediwm_node *, int x, int y);
@@ -176,6 +160,10 @@ void rediwm_buffer_set_buffer(struct rediwm_buffer *, struct wlr_buffer *);
// As wlr_scene_buffer_set_buffer_with_damage: `damage` is buffer-local.
void rediwm_buffer_set_buffer_with_damage(struct rediwm_buffer *, struct wlr_buffer *,
const pixman_region32_t *damage);
+// As wlr_scene_buffer_set_buffer_with_options: damage plus an acquire point to wait for.
+struct wlr_scene_buffer_set_buffer_options;
+void rediwm_buffer_set_buffer_with_options(struct rediwm_buffer *, struct wlr_buffer *,
+ const struct wlr_scene_buffer_set_buffer_options *);
// The buffer the carrier holds; NULL once the renderer released it after upload.
struct wlr_buffer *rediwm_buffer_get_buffer(const struct rediwm_buffer *);
void rediwm_buffer_set_dest_size(struct rediwm_buffer *, int width, int height);
@@ -191,14 +179,9 @@ void rediwm_buffer_size(const struct rediwm_buffer *, int *width, int *height);
// The topmost enabled node of the subtree at (x, y) in the subtree's own
// coordinates, with the point in the node's, as wlr_scene_node_at. Buffers answer
-// through `point_accepts_input`. Foreign nodes are not looked into here: the
-// caller's pick of them is the projection's.
+// through `point_accepts_input`.
struct rediwm_node *rediwm_node_at(struct rediwm_node *, double x, double y, double *nx, double *ny);
typedef void (*rediwm_buffer_iterator)(struct rediwm_buffer *, int sx, int sy, void *data);
-typedef void (*rediwm_wlr_buffer_iterator)(struct wlr_scene_buffer *, int sx, int sy, void *data);
-// The same for the wlr_scene buffers of the foreign subtrees under the node (the
-// surfaces of clients), with their positions relative to the node passed in.
-void rediwm_node_for_each_wlr_buffer(struct rediwm_node *, rediwm_wlr_buffer_iterator, void *data);
// Every buffer of the subtree, bottom to top, with its position relative to the
// node passed in (as wlr_scene_node_for_each_buffer; disabled nodes are skipped).
void rediwm_node_for_each_buffer(struct rediwm_node *, rediwm_buffer_iterator, void *data);
diff --git a/src/node.zig b/src/node.zig
index e72c7d7..468eda1 100644
--- a/src/node.zig
+++ b/src/node.zig
@@ -1,8 +1,8 @@
//! The compositor's own scene nodes (node.h): what wlr.Scene* was as a data model.
//! Method names are wlr_scene's, so a call site changes its type and little else.
//!
-//! A client's surfaces are still wlr_scene trees (stage 7b replaces that): they live
-//! under the scene's hidden host and an own `Foreign` node places them.
+//! A client's surfaces are own nodes too (node_surface.h): an xdg surface is a tree, a
+//! surface an own buffer node that follows its wl_surface.
const std = @import("std");
const wayland = @import("wayland");
const wl = wayland.server.wl;
@@ -10,7 +10,7 @@ const pixman = @import("pixman");
const wlr = @import("wlroots");
pub const Node = extern struct {
- pub const Type = enum(c_int) { tree, rect, buffer, foreign };
+ pub const Type = enum(c_int) { tree, rect, buffer };
type: Type,
parent: ?*Tree,
@@ -30,8 +30,8 @@ pub const Node = extern struct {
extern fn rediwm_node_destroy(node: *Node) void;
pub const destroy = rediwm_node_destroy;
extern fn rediwm_node_touch(node: *Node) void;
- /// Reports a change the node cannot see itself (something done to the wlr_scene
- /// subtree it hosts): asks for a frame and a fresh resolve.
+ /// Reports a change the node's setters cannot see: asks for a frame and a fresh
+ /// resolve.
pub const touch = rediwm_node_touch;
extern fn rediwm_node_set_enabled(node: *Node, enabled: bool) void;
pub const setEnabled = rediwm_node_set_enabled;
@@ -76,37 +76,34 @@ pub const Node = extern struct {
);
}
- extern fn rediwm_node_for_each_wlr_buffer(
- node: *Node,
- iterator: *const fn (buffer: *wlr.SceneBuffer, sx: c_int, sy: c_int, data: ?*anyopaque) callconv(.c) void,
- user_data: ?*anyopaque,
- ) void;
- /// The wlr_scene buffers of the clients' surfaces under this node, positions
- /// relative to it.
+ /// The buffers of the clients' surfaces under this node, positions relative to it.
pub inline fn forEachSurfaceBuffer(
node: *Node,
comptime T: type,
- comptime iterator: fn (buffer: *wlr.SceneBuffer, sx: c_int, sy: c_int, data: T) void,
+ comptime iterator: fn (buffer: *Buffer, sx: c_int, sy: c_int, data: T) void,
data: T,
) void {
- rediwm_node_for_each_wlr_buffer(
- node,
- struct {
- fn wrapper(b: *wlr.SceneBuffer, sx: c_int, sy: c_int, d: ?*anyopaque) callconv(.c) void {
- iterator(b, sx, sy, @ptrCast(@alignCast(d)));
- }
- }.wrapper,
- data,
- );
+ const Filter = struct {
+ fn visit(b: *Buffer, sx: c_int, sy: c_int, d: T) void {
+ if (b.surface != null) iterator(b, sx, sy, d);
+ }
+ };
+ node.forEachBuffer(T, Filter.visit, data);
}
- /// The foreign node this is, if it is one.
- pub fn foreign(node: *Node) ?*Foreign {
- return if (node.type == .foreign) @fieldParentPtr("node", node) else null;
+ extern fn rediwm_subsurface_tree_set_clip(node: *Node, clip: ?*const wlr.Box) bool;
+ /// Clips every surface of the subsurface trees at or under this node to `clip`
+ /// (in the tree's coordinates), or lifts the clip when null.
+ pub fn subsurfaceTreeSetClip(node: *Node, clip: ?*const wlr.Box) void {
+ _ = rediwm_subsurface_tree_set_clip(node, clip);
}
+
pub fn tree(node: *Node) ?*Tree {
return if (node.type == .tree) @fieldParentPtr("node", node) else null;
}
+ pub fn buffer(node: *Node) ?*Buffer {
+ return if (node.type == .buffer) @fieldParentPtr("node", node) else null;
+ }
};
pub const Tree = extern struct {
@@ -128,39 +125,23 @@ pub const Tree = extern struct {
return rediwm_buffer_create(parent, buffer) orelse error.OutOfMemory;
}
- extern fn rediwm_foreign_create(parent: *Tree, wlr_node: *wlr.SceneNode) ?*Foreign;
extern fn rediwm_tree_host(tree: *const Tree) *wlr.SceneTree;
- /// The hidden wlr_scene tree that holds the clients' surface trees.
+ /// The hidden wlr_scene tree that holds the buffers' carriers.
pub const host = rediwm_tree_host;
- /// A hosted subtree's root keeps its foreign node in its `data`, so a hit inside
- /// it finds its way back to the own tree.
- fn adopt(parent: *Tree, tree: *wlr.SceneTree) !*Foreign {
- const foreign = rediwm_foreign_create(parent, &tree.node) orelse {
- tree.node.destroy();
- return error.OutOfMemory;
- };
- tree.node.data = foreign;
- return foreign;
- }
-
- pub fn createSceneXdgSurface(parent: *Tree, xdg_surface: *wlr.XdgSurface) !*Foreign {
- return adopt(parent, try rediwm_tree_host(parent).createSceneXdgSurface(xdg_surface));
- }
-
- pub fn createSceneSubsurfaceTree(parent: *Tree, surface: *wlr.Surface) !*Foreign {
- return adopt(parent, try rediwm_tree_host(parent).createSceneSubsurfaceTree(surface));
+ extern fn rediwm_xdg_surface_create(parent: *Tree, xdg_surface: *wlr.XdgSurface) ?*Tree;
+ pub fn createSceneXdgSurface(parent: *Tree, xdg_surface: *wlr.XdgSurface) !*Tree {
+ return rediwm_xdg_surface_create(parent, xdg_surface) orelse error.OutOfMemory;
}
- pub fn createSceneDragIcon(parent: *Tree, drag_icon: *wlr.Drag.Icon) !*Foreign {
- return adopt(parent, try rediwm_tree_host(parent).createSceneDragIcon(drag_icon));
+ extern fn rediwm_subsurface_tree_create(parent: *Tree, surface: *wlr.Surface) ?*Tree;
+ pub fn createSceneSubsurfaceTree(parent: *Tree, surface: *wlr.Surface) !*Tree {
+ return rediwm_subsurface_tree_create(parent, surface) orelse error.OutOfMemory;
}
- /// The layer surface's wlr_scene tree is hosted like any client's; the foreign
- /// node places it, and the wlr tree's own position is the layer shell's.
- pub fn createSceneLayerSurfaceV1(parent: *Tree, layer_surface: *wlr.LayerSurfaceV1) !struct { scene: *wlr.SceneLayerSurfaceV1, foreign: *Foreign } {
- const scene = try rediwm_tree_host(parent).createSceneLayerSurfaceV1(layer_surface);
- return .{ .scene = scene, .foreign = try adopt(parent, scene.tree) };
+ extern fn rediwm_drag_icon_create(parent: *Tree, drag_icon: *wlr.Drag.Icon) ?*Tree;
+ pub fn createSceneDragIcon(parent: *Tree, drag_icon: *wlr.Drag.Icon) !*Tree {
+ return rediwm_drag_icon_create(parent, drag_icon) orelse error.OutOfMemory;
}
pub fn fromNode(node: *Node) *Tree {
@@ -199,6 +180,8 @@ pub const Buffer = extern struct {
opaque_region: pixman.Region32,
buffer_width: c_int,
buffer_height: c_int,
+ /// The client surface this node shows, if it is one.
+ surface: ?*Surface,
extern fn rediwm_buffer_set_buffer(buffer: *Buffer, wlr_buffer: ?*wlr.Buffer) void;
pub const setBuffer = rediwm_buffer_set_buffer;
@@ -225,35 +208,11 @@ pub const Buffer = extern struct {
}
};
-/// A wlr_scene subtree hosted under an own tree.
-pub const Foreign = extern struct {
- node: Node,
- /// Null once wlroots has destroyed the subtree (this node goes with it).
- wlr: ?*wlr.SceneNode,
- wlr_destroy: wl.Listener(void),
-
- pub fn tree(foreign: *Foreign) *wlr.SceneTree {
- return wlr.SceneTree.fromNode(foreign.wlr.?);
- }
-
- /// The foreign node whose subtree holds this wlr node (a root's `data`).
- pub fn of(wlr_node: *wlr.SceneNode) ?*Foreign {
- var current: *wlr.SceneNode = wlr_node;
- while (true) {
- if (current.data) |data| return @ptrCast(@alignCast(data));
- current = if (current.parent) |parent| &parent.node else return null;
- }
- }
-
- /// Clips the hosted surface tree, as wlr_scene_subsurface_tree_set_clip.
- pub fn subsurfaceTreeSetClip(foreign: *Foreign, clip: ?*const wlr.Box) void {
- wlr.SceneNode.subsurfaceTreeSetClip(foreign.wlr.?, clip);
- foreign.node.touch();
- }
-
- pub fn fromNode(node: *Node) *Foreign {
- return @fieldParentPtr("node", node);
- }
+/// The surface an own buffer node follows (node_surface.h); the rest of the struct is
+/// the binding's own.
+pub const Surface = extern struct {
+ buffer: *Buffer,
+ surface: *wlr.Surface,
};
/// The own scene: its root tree's children are the layers, bottom to top.
@@ -265,8 +224,7 @@ pub const Scene = extern struct {
changed_data: ?*anyopaque,
extern fn rediwm_scene_create(host: *wlr.SceneTree, changed: ?*const fn (?*anyopaque, ?*Node, bool) callconv(.c) void, data: ?*anyopaque) ?*Scene;
- /// `host` is a hidden, disabled wlr_scene tree for the carriers and the
- /// clients' subtrees.
+ /// `host` is a hidden, disabled wlr_scene tree for the buffers' carriers.
pub fn create(host: *wlr.SceneTree) !*Scene {
return rediwm_scene_create(host, null, null) orelse error.OutOfMemory;
}
@@ -274,32 +232,6 @@ pub const Scene = extern struct {
pub const destroy = rediwm_scene_destroy;
};
-/// Where a node inside a client's subtree is in the scene's root, and whether the
-/// own nodes above it are enabled: the hosting node's place plus the wlr_scene offsets
-/// down to it.
-pub fn wlrCoords(wlr_node: *wlr.SceneNode, lx: *c_int, ly: *c_int) bool {
- var x: c_int = 0;
- var y: c_int = 0;
- var current: *wlr.SceneNode = wlr_node;
- while (true) {
- x += current.x;
- y += current.y;
- if (current.data != null) break;
- current = if (current.parent) |parent| &parent.node else {
- lx.* = x;
- ly.* = y;
- return false;
- };
- }
- const foreign: *Foreign = @ptrCast(@alignCast(current.data.?));
- var ox: c_int = 0;
- var oy: c_int = 0;
- const enabled = foreign.node.coords(&ox, &oy);
- lx.* = ox + x;
- ly.* = oy + y;
- return enabled;
-}
-
/// A scene for unit tests: a wlroots scene with the own scene's hidden host in it.
pub const TestScene = struct {
wlr_scene: *wlr.Scene,
diff --git a/src/node_surface.c b/src/node_surface.c
new file mode 100644
index 0000000..c3d7ca3
--- /dev/null
+++ b/src/node_surface.c
@@ -0,0 +1,869 @@
+// 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).
+#define WLR_USE_UNSTABLE
+#define WLR_PRIVATE private
+#include "node_surface.h"
+#include <assert.h>
+#include <math.h>
+#include <stdlib.h>
+#include <wlr/types/wlr_alpha_modifier_v1.h>
+#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 <wlr/types/wlr_scene.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 -----------------------------------------------------------------
+
+static double get_surface_preferred_buffer_scale(struct wlr_surface *surface) {
+ double scale = 1;
+ struct wlr_surface_output *surface_output;
+ wl_list_for_each(surface_output, &surface->current_outputs, link) {
+ if (surface_output->output->scale > scale) scale = surface_output->output->scale;
+ }
+ return scale;
+}
+
+// Output used for frame pacing (surface frame callbacks, presentation time feedback,
+// etc), may be NULL.
+static struct wlr_output *get_surface_frame_pacing_output(struct wlr_surface *surface) {
+ struct wlr_output *frame_pacing_output = NULL;
+ struct wlr_surface_output *surface_output;
+ wl_list_for_each(surface_output, &surface->current_outputs, link) {
+ if (!surface_output->private.suspended &&
+ (frame_pacing_output == NULL || surface_output->output->refresh > frame_pacing_output->refresh))
+ frame_pacing_output = surface_output->output;
+ }
+ return frame_pacing_output;
+}
+
+static int get_tf_preference(enum wlr_color_transfer_function tf) {
+ switch (tf) {
+ case WLR_COLOR_TRANSFER_FUNCTION_GAMMA22:
+ return 0;
+ case WLR_COLOR_TRANSFER_FUNCTION_ST2084_PQ:
+ return 1;
+ case WLR_COLOR_TRANSFER_FUNCTION_BT1886:
+ case WLR_COLOR_TRANSFER_FUNCTION_SRGB:
+ case WLR_COLOR_TRANSFER_FUNCTION_EXT_LINEAR:
+ return -1;
+ }
+ abort(); // unreachable
+}
+
+static int get_primaries_preference(enum wlr_color_named_primaries primaries) {
+ switch (primaries) {
+ case WLR_COLOR_NAMED_PRIMARIES_SRGB:
+ return 0;
+ case WLR_COLOR_NAMED_PRIMARIES_BT2020:
+ return 1;
+ }
+ abort(); // unreachable
+}
+
+static void get_surface_preferred_image_description(struct wlr_surface *surface,
+ struct wlr_image_description_v1_data *out) {
+ struct wlr_output_image_description preferred = {
+ .transfer_function = WLR_COLOR_TRANSFER_FUNCTION_GAMMA22,
+ .primaries = WLR_COLOR_NAMED_PRIMARIES_SRGB,
+ };
+
+ struct wlr_surface_output *surface_output;
+ wl_list_for_each(surface_output, &surface->current_outputs, link) {
+ const struct wlr_output_image_description *img_desc = surface_output->output->image_description;
+ if (img_desc == NULL) continue;
+ if (get_tf_preference(preferred.transfer_function) < get_tf_preference(img_desc->transfer_function))
+ preferred.transfer_function = img_desc->transfer_function;
+ if (get_primaries_preference(preferred.primaries) < get_primaries_preference(img_desc->primaries))
+ preferred.primaries = img_desc->primaries;
+ }
+
+ *out = (struct wlr_image_description_v1_data){
+ .tf_named = wlr_color_manager_v1_transfer_function_from_wlr(preferred.transfer_function),
+ .primaries_named = wlr_color_manager_v1_primaries_from_wlr(preferred.primaries),
+ };
+}
+
+// The wlr_scene whose host holds the node's carrier: it knows the outputs and the colour
+// manager (stage 7c replaces both with plain pointers).
+static struct wlr_scene *carrier_scene(struct wlr_scene_buffer *carrier) {
+ struct wlr_scene_tree *tree = carrier->node.parent;
+ while (tree->node.parent) tree = tree->node.parent;
+ return wl_container_of(tree, (struct wlr_scene *)NULL, tree);
+}
+
+static void handle_scene_buffer_outputs_update(struct wl_listener *listener, void *data) {
+ struct rediwm_surface *surface = wl_container_of(listener, surface, outputs_update);
+ struct wlr_scene_outputs_update_event *event = data;
+ struct wlr_scene *scene = carrier_scene(surface->buffer->carrier);
+
+ // If the surface is no longer visible on any output in the scene, keep the last sent
+ // preferred configuration to avoid unnecessary redraws.
+ bool suspend = event->size == 0;
+
+ // To avoid sending redundant leave/enter events when a surface is hidden and then
+ // shown without moving to a different output the following policy is implemented:
+ //
+ // 1. When a surface transitions from being visible on >0 outputs to being visible on 0
+ // outputs don't send any leave events.
+ //
+ // 2. When a surface transitions from being visible on 0 outputs to being visible on >0
+ // outputs send leave events for all entered outputs on which the surface is no
+ // longer visible as well as enter events for any outputs not already entered.
+ struct wlr_surface_output *entered_output, *tmp;
+ wl_list_for_each_safe(entered_output, tmp, &surface->surface->current_outputs, link) {
+ bool active = false;
+ for (size_t i = 0; i < event->size; i++) {
+ if (entered_output->output == event->active[i]->output) {
+ active = true;
+ break;
+ }
+ }
+
+ struct wlr_scene_output *scene_output;
+ wl_list_for_each(scene_output, &scene->outputs, link) {
+ if (scene_output->output == entered_output->output) {
+ entered_output->private.suspended = suspend;
+ if (!suspend && !active) wlr_surface_send_leave(surface->surface, entered_output->output);
+ break;
+ }
+ }
+ }
+
+ // No reason to update the preferred configuration if we aren't sending leave/enter
+ // events.
+ if (suspend) return;
+
+ for (size_t i = 0; i < event->size; i++) {
+ // This function internally checks if an enter event was already sent for the output
+ // to avoid sending redundant events.
+ wlr_surface_send_enter(surface->surface, event->active[i]->output);
+ }
+
+ double scale = get_surface_preferred_buffer_scale(surface->surface);
+ wlr_fractional_scale_v1_notify_scale(surface->surface, scale);
+ wlr_surface_set_preferred_buffer_scale(surface->surface, ceil(scale));
+
+ if (scene->color_manager_v1 != NULL) {
+ struct wlr_image_description_v1_data img_desc = {0};
+ get_surface_preferred_image_description(surface->surface, &img_desc);
+ wlr_color_manager_v1_set_surface_preferred_image_description(scene->color_manager_v1,
+ surface->surface, &img_desc);
+ }
+}
+
+static void handle_scene_buffer_output_sample(struct wl_listener *listener, void *data) {
+ struct rediwm_surface *surface = wl_container_of(listener, surface, output_sample);
+ const struct wlr_scene_output_sample_event *event = data;
+ struct wlr_output *output = event->output->output;
+ if (get_surface_frame_pacing_output(surface->surface) != output) return;
+
+ if (event->direct_scanout)
+ wlr_presentation_surface_scanned_out_on_output(surface->surface, output);
+ else
+ wlr_presentation_surface_textured_on_output(surface->surface, output);
+
+ struct wlr_linux_drm_syncobj_surface_v1_state *syncobj_surface_state =
+ wlr_linux_drm_syncobj_v1_get_surface_state(surface->surface);
+ if (syncobj_surface_state != NULL && event->release_timeline != NULL) {
+ wlr_linux_drm_syncobj_v1_state_add_release_point(syncobj_surface_state, event->release_timeline,
+ event->release_point, output->event_loop);
+ }
+}
+
+static void handle_scene_buffer_frame_done(struct wl_listener *listener, void *data) {
+ struct rediwm_surface *surface = wl_container_of(listener, surface, frame_done);
+ struct wlr_scene_frame_done_event *event = data;
+ if (get_surface_frame_pacing_output(surface->surface) != event->output->output) return;
+
+ wlr_surface_send_frame_done(surface->surface, &event->when);
+}
+
+static void scene_surface_handle_surface_destroy(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_surface *surface = wl_container_of(listener, surface, surface_destroy);
+ rediwm_node_destroy(&surface->buffer->node);
+}
+
+// This is used for wlr_scene where it unconditionally locks buffers preventing reuse of
+// the existing texture for shm clients. With the usage pattern of surface handling, we
+// can mark its locked buffer as safe for mutation.
+static void client_buffer_mark_next_can_damage(struct wlr_client_buffer *buffer) {
+ buffer->private.n_ignore_locks++;
+}
+
+static void carrier_unmark_client_buffer(struct wlr_scene_buffer *scene_buffer) {
+ if (!scene_buffer->buffer) return;
+
+ struct wlr_client_buffer *buffer = wlr_client_buffer_get(scene_buffer->buffer);
+ if (!buffer) return;
+
+ // If the buffer was a single-pixel buffer where we cached its color then it won't have
+ // been marked as damage-allowed.
+ if (buffer->private.n_ignore_locks > 0) buffer->private.n_ignore_locks--;
+}
+
+static int min(int a, int b) { return a < b ? a : b; }
+
+static void surface_reconfigure(struct rediwm_surface *scene_surface) {
+ struct rediwm_buffer *scene_buffer = scene_surface->buffer;
+ struct wlr_scene_buffer *carrier = scene_buffer->carrier;
+ struct wlr_surface *surface = scene_surface->surface;
+ struct wlr_surface_state *state = &surface->current;
+
+ struct wlr_fbox src_box;
+ wlr_surface_get_buffer_source_box(surface, &src_box);
+
+ pixman_region32_t opaque;
+ pixman_region32_init(&opaque);
+ pixman_region32_copy(&opaque, &surface->opaque_region);
+
+ int width = state->width;
+ int height = state->height;
+
+ if (!wlr_box_empty(&scene_surface->clip)) {
+ struct wlr_box *clip = &scene_surface->clip;
+
+ int buffer_width = state->buffer_width;
+ int buffer_height = state->buffer_height;
+ width = min(clip->width, width - clip->x);
+ height = min(clip->height, height - clip->y);
+
+ wlr_fbox_transform(&src_box, &src_box, state->transform, buffer_width, buffer_height);
+ wlr_output_transform_coords(state->transform, &buffer_width, &buffer_height);
+
+ src_box.x += (double)(clip->x * src_box.width) / state->width;
+ src_box.y += (double)(clip->y * src_box.height) / state->height;
+ src_box.width *= (double)width / state->width;
+ src_box.height *= (double)height / state->height;
+
+ wlr_fbox_transform(&src_box, &src_box, wlr_output_transform_invert(state->transform),
+ buffer_width, buffer_height);
+
+ pixman_region32_translate(&opaque, -clip->x, -clip->y);
+ pixman_region32_intersect_rect(&opaque, &opaque, 0, 0, width, height);
+ }
+
+ if (width <= 0 || height <= 0) {
+ rediwm_buffer_set_buffer(scene_buffer, NULL);
+ pixman_region32_fini(&opaque);
+ return;
+ }
+
+ float opacity = 1.0;
+ const struct wlr_alpha_modifier_surface_v1_state *alpha_modifier_state =
+ wlr_alpha_modifier_v1_get_surface_state(surface);
+ if (alpha_modifier_state != NULL) opacity = (float)alpha_modifier_state->multiplier;
+
+ enum wlr_color_transfer_function tf = WLR_COLOR_TRANSFER_FUNCTION_GAMMA22;
+ enum wlr_color_named_primaries primaries = WLR_COLOR_NAMED_PRIMARIES_SRGB;
+ const struct wlr_image_description_v1_data *img_desc =
+ wlr_surface_get_image_description_v1_data(surface);
+ if (img_desc != NULL) {
+ tf = wlr_color_manager_v1_transfer_function_to_wlr(img_desc->tf_named);
+ primaries = wlr_color_manager_v1_primaries_to_wlr(img_desc->primaries_named);
+ }
+
+ enum wlr_color_encoding color_encoding = WLR_COLOR_ENCODING_NONE;
+ enum wlr_color_range color_range = WLR_COLOR_RANGE_NONE;
+ const struct wlr_color_representation_v1_surface_state *color_repr =
+ wlr_color_representation_v1_get_surface_state(surface);
+ if (color_repr != NULL) {
+ if (color_repr->coefficients != 0)
+ color_encoding = wlr_color_representation_v1_color_encoding_to_wlr(color_repr->coefficients);
+ if (color_repr->range != 0)
+ color_range = wlr_color_representation_v1_color_range_to_wlr(color_repr->range);
+ }
+
+ rediwm_buffer_set_opaque_region(scene_buffer, &opaque);
+ rediwm_buffer_set_source_box(scene_buffer, &src_box);
+ rediwm_buffer_set_dest_size(scene_buffer, width, height);
+ rediwm_buffer_set_transform(scene_buffer, state->transform);
+ rediwm_buffer_set_opacity(scene_buffer, opacity);
+ // The colour description is the carrier's alone: the buffer set below reports it.
+ wlr_scene_buffer_set_transfer_function(carrier, tf);
+ wlr_scene_buffer_set_primaries(carrier, primaries);
+ wlr_scene_buffer_set_color_encoding(carrier, color_encoding);
+ wlr_scene_buffer_set_color_range(carrier, color_range);
+
+ carrier_unmark_client_buffer(carrier);
+
+ if (surface->buffer) {
+ // If we've cached the buffer's single-pixel buffer color then any in-place updates
+ // to the texture wouldn't be reflected in rendering. So only allow in-place texture
+ // updates if it's not a single pixel buffer. Note that we can't use the cached
+ // scene_buffer->is_single_pixel_buffer because that's only set later on.
+ bool is_single_pixel_buffer = false;
+ if (surface->buffer->source != NULL) {
+ struct wlr_single_pixel_buffer_v1 *spb =
+ wlr_single_pixel_buffer_v1_try_from_buffer(surface->buffer->source);
+ is_single_pixel_buffer = spb != NULL;
+ }
+ if (!is_single_pixel_buffer) client_buffer_mark_next_can_damage(surface->buffer);
+
+ struct wlr_linux_drm_syncobj_surface_v1_state *syncobj_surface_state =
+ wlr_linux_drm_syncobj_v1_get_surface_state(surface);
+
+ struct wlr_scene_buffer_set_buffer_options options = {
+ .damage = &surface->buffer_damage,
+ };
+ if (syncobj_surface_state != NULL) {
+ options.wait_timeline = syncobj_surface_state->acquire_timeline;
+ options.wait_point = syncobj_surface_state->acquire_point;
+ }
+ rediwm_buffer_set_buffer_with_options(scene_buffer, &surface->buffer->base, &options);
+ } else {
+ rediwm_buffer_set_buffer(scene_buffer, NULL);
+ }
+
+ pixman_region32_fini(&opaque);
+}
+
+static void handle_scene_surface_surface_commit(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_surface *surface = wl_container_of(listener, surface, surface_commit);
+
+ surface_reconfigure(surface);
+
+ // If the surface has requested a frame done event, honour that. The frame_callback_list
+ // will be populated in this case. We should only schedule the frame however if the node
+ // is enabled and there is an output intersecting, otherwise the frame done events would
+ // never reach the surface anyway.
+ bool enabled = rediwm_node_coords(&surface->buffer->node, NULL, NULL);
+ struct wlr_output *output = get_surface_frame_pacing_output(surface->surface);
+ if (!wl_list_empty(&surface->surface->current.frame_callback_list) && output && enabled)
+ wlr_output_schedule_frame(output);
+}
+
+static bool scene_buffer_point_accepts_input(struct rediwm_buffer *buffer, double *sx, double *sy) {
+ struct rediwm_surface *scene_surface = buffer->surface;
+
+ *sx += scene_surface->clip.x;
+ *sy += scene_surface->clip.y;
+
+ return wlr_surface_point_accepts_input(scene_surface->surface, *sx, *sy);
+}
+
+static void surface_node_destroyed(struct wl_listener *listener, void *data) {
+ (void)data;
+ struct rediwm_surface *surface = wl_container_of(listener, surface, node_destroy);
+
+ carrier_unmark_client_buffer(surface->buffer->carrier);
+ surface->buffer->surface = NULL;
+ surface->buffer->point_accepts_input = NULL;
+
+ wl_list_remove(&surface->node_destroy.link);
+ wl_list_remove(&surface->outputs_update.link);
+ wl_list_remove(&surface->output_sample.link);
+ wl_list_remove(&surface->frame_done.link);
+ wl_list_remove(&surface->surface_destroy.link);
+ wl_list_remove(&surface->surface_commit.link);
+
+ free(surface);
+}
+
+struct rediwm_surface *rediwm_surface_create(struct rediwm_tree *parent, struct wlr_surface *wlr_surface) {
+ struct rediwm_surface *surface = calloc(1, sizeof(*surface));
+ if (surface == NULL) return NULL;
+
+ struct rediwm_buffer *scene_buffer = rediwm_buffer_create(parent, NULL);
+ if (!scene_buffer) {
+ free(surface);
+ return NULL;
+ }
+
+ surface->buffer = scene_buffer;
+ surface->surface = wlr_surface;
+ scene_buffer->surface = surface;
+ scene_buffer->point_accepts_input = scene_buffer_point_accepts_input;
+ struct wlr_scene_buffer *carrier = scene_buffer->carrier;
+
+ surface->node_destroy.notify = surface_node_destroyed;
+ wl_signal_add(&scene_buffer->node.events.destroy, &surface->node_destroy);
+
+ surface->outputs_update.notify = handle_scene_buffer_outputs_update;
+ wl_signal_add(&carrier->events.outputs_update, &surface->outputs_update);
+
+ surface->output_sample.notify = handle_scene_buffer_output_sample;
+ wl_signal_add(&carrier->events.output_sample, &surface->output_sample);
+
+ surface->frame_done.notify = handle_scene_buffer_frame_done;
+ wl_signal_add(&carrier->events.frame_done, &surface->frame_done);
+
+ surface->surface_destroy.notify = scene_surface_handle_surface_destroy;
+ wl_signal_add(&wlr_surface->events.destroy, &surface->surface_destroy);
+
+ surface->surface_commit.notify = handle_scene_surface_surface_commit;
+ wl_signal_add(&wlr_surface->events.commit, &surface->surface_commit);
+
+ surface_reconfigure(surface);
+
+ return surface;
+}
+
+static void scene_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)
+ surface->clip = *clip;
+ else
+ surface->clip = (struct wlr_box){0};
+
+ 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
new file mode 100644
index 0000000..38f4f58
--- /dev/null
+++ b/src/node_surface.h
@@ -0,0 +1,41 @@
+// Client surfaces in the own scene (stage 7b of docs/plans/scene-graph-stage7.md).
+//
+// A port of wlroots' wlr_scene_surface, wlr_scene_subsurface_tree, wlr_scene_xdg_surface,
+// and wlr_scene_drag_icon onto own nodes (MIT licensed, as
+// wlroots; the logic follows types/scene/*.c of wlroots 0.20). A surface is an own buffer
+// node whose content follows the wl_surface; a surface with subsurfaces is a tree of
+// them, stacked as the protocol orders them. The pixels still ride on the buffer's
+// carrier (node.h), whose signals the projection raises where wlr_scene_output once did:
+// outputs_update, output_sample and frame_done.
+#pragma once
+#include "node.h"
+#include <wlr/util/box.h>
+
+struct wlr_surface;
+struct wlr_xdg_surface;
+struct wlr_drag_icon;
+
+// The surface a buffer node shows. Only `buffer` and `surface` are read outside.
+struct rediwm_surface {
+ struct rediwm_buffer *buffer;
+ struct wlr_surface *surface;
+ struct wlr_box clip;
+ struct wl_listener node_destroy, outputs_update, output_sample, frame_done, surface_destroy,
+ surface_commit;
+};
+
+// A node showing `surface` alone, no subsurfaces. NULL on allocation failure.
+struct rediwm_surface *rediwm_surface_create(struct rediwm_tree *parent, struct wlr_surface *);
+
+// 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 *);
+// Clips every surface of the subsurface trees at or under `node` to `clip` (in the
+// tree's coordinates), or lifts the clip when NULL. Returns false if none was found.
+bool rediwm_subsurface_tree_set_clip(struct rediwm_node *node, const struct wlr_box *clip);
+
+// An xdg surface's tree: the surface at the origin of its window geometry, and a popup
+// at its position. Popups are made by the compositor as children of this tree.
+struct rediwm_tree *rediwm_xdg_surface_create(struct rediwm_tree *parent, struct wlr_xdg_surface *);
+
+// A drag icon's tree, moved by the icon's own offset on every commit.
+struct rediwm_tree *rediwm_drag_icon_create(struct rediwm_tree *parent, struct wlr_drag_icon *);
diff --git a/src/projection.c b/src/projection.c
index 7be6160..7785e40 100644
--- a/src/projection.c
+++ b/src/projection.c
@@ -14,6 +14,7 @@
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_scene.h>
+#include "node_surface.h"
#include <wlr/util/addon.h>
#include <wlr/util/log.h>
@@ -103,20 +104,18 @@ struct leaf {
// In its group's `items` when the last resolve() reached it, else alone.
struct wl_list paint;
struct rediwm_projection *projection;
- // The wlr_scene node the pass reads the pixels from (`transport`): a buffer's or a
- // rect's carrier, or a client surface's own node.
+ // The wlr_scene node the pass reads the pixels from (`transport`): the carrier of
+ // the own node's buffer or rect.
struct wlr_scene_node *source;
- // The own node that places it, or NULL for a node inside a foreign subtree, whose
- // `foreign` is the own node hosting that subtree.
+ // The own node it is.
struct rediwm_node *own;
- struct rediwm_foreign *foreign;
// The group the last resolve() put it in.
struct group *group;
// A glass effect: decided when the leaf is made, as an effect is its own node.
bool effect;
struct wl_listener destroy, commit;
struct wlr_addon source_lookup;
- struct wlr_scene_surface *surface;
+ struct rediwm_surface *surface;
struct feedback *feedback;
bool dirty, seen;
// Under an inert tree at the last resolve(): drawn, never picked.
@@ -316,19 +315,12 @@ static bool set_tree_inert(struct wlr_addon_set *set, void **slot, bool inert) {
}
bool rediwm_projection_set_tree_zoom(struct rediwm_node *node, double scale) { props_changed = false; bool ok = set_tree_zoom(&node->addons, &node->props, scale); if (props_changed) rediwm_node_touch(node); return ok; }
-bool rediwm_projection_set_wlr_tree_zoom(struct wlr_scene_node *node, double scale) { props_changed = false; bool ok = set_tree_zoom(&node->addons, NULL, scale); if (props_changed && current) mark_all(current); return ok; }
bool rediwm_projection_set_tree_scale(struct rediwm_node *node, double factor) { props_changed = false; bool ok = set_tree_scale(&node->addons, &node->props, factor); if (props_changed) rediwm_node_touch(node); return ok; }
-bool rediwm_projection_set_wlr_tree_scale(struct wlr_scene_node *node, double factor) { props_changed = false; bool ok = set_tree_scale(&node->addons, NULL, factor); if (props_changed && current) mark_all(current); return ok; }
bool rediwm_projection_set_tree_clip(struct rediwm_node *node, const struct rediwm_clip_spec *spec) { props_changed = false; bool ok = set_tree_clip(&node->addons, &node->props, spec); if (props_changed) rediwm_node_touch(node); return ok; }
-bool rediwm_projection_set_wlr_tree_clip(struct wlr_scene_node *node, const struct rediwm_clip_spec *spec) { props_changed = false; bool ok = set_tree_clip(&node->addons, NULL, spec); if (props_changed && current) mark_all(current); return ok; }
bool rediwm_projection_set_tree_shadow(struct rediwm_node *node, const struct rediwm_shadow_spec *spec) { props_changed = false; bool ok = set_tree_shadow(&node->addons, &node->props, spec); if (props_changed) rediwm_node_touch(node); return ok; }
-bool rediwm_projection_set_wlr_tree_shadow(struct wlr_scene_node *node, const struct rediwm_shadow_spec *spec) { props_changed = false; bool ok = set_tree_shadow(&node->addons, NULL, spec); if (props_changed && current) mark_all(current); return ok; }
bool rediwm_projection_set_tree_dim(struct rediwm_node *node, float brightness, float saturation) { props_changed = false; bool ok = set_tree_dim(&node->addons, &node->props, brightness, saturation); if (props_changed) rediwm_node_touch(node); return ok; }
-bool rediwm_projection_set_wlr_tree_dim(struct wlr_scene_node *node, float brightness, float saturation) { props_changed = false; bool ok = set_tree_dim(&node->addons, NULL, brightness, saturation); if (props_changed && current) mark_all(current); return ok; }
bool rediwm_projection_set_tree_unclipped(struct rediwm_node *node, bool unclipped) { props_changed = false; bool ok = set_tree_unclipped(&node->addons, &node->props, unclipped); if (props_changed) rediwm_node_touch(node); return ok; }
-bool rediwm_projection_set_wlr_tree_unclipped(struct wlr_scene_node *node, bool unclipped) { props_changed = false; bool ok = set_tree_unclipped(&node->addons, NULL, unclipped); if (props_changed && current) mark_all(current); return ok; }
bool rediwm_projection_set_tree_inert(struct rediwm_node *node, bool inert) { props_changed = false; bool ok = set_tree_inert(&node->addons, &node->props, inert); if (props_changed) rediwm_node_touch(node); return ok; }
-bool rediwm_projection_set_wlr_tree_inert(struct wlr_scene_node *node, bool inert) { props_changed = false; bool ok = set_tree_inert(&node->addons, NULL, inert); if (props_changed && current) mark_all(current); return ok; }
static const struct wlr_addon_interface source_lookup_impl, group_source_impl;
struct watched_surface {
struct rediwm_projection *projection;
@@ -489,7 +481,7 @@ static void destroy_leaf(struct leaf *l) {
wl_list_remove(&l->destroy.link);
wl_list_remove(&l->link);
wl_list_remove(&l->paint);
- if (l->own && l->source->data == l) l->source->data = NULL;
+ if (l->source->data == l) l->source->data = NULL;
wlr_addon_finish(&l->source_lookup);
pixman_region32_fini(&l->damage);
pixman_region32_fini(&l->visible);
@@ -538,20 +530,15 @@ static void committed(struct wl_listener *listener, void *data) {
schedule(l->projection);
}
static struct leaf *get_leaf(struct rediwm_projection *p, struct wlr_scene_node *source,
- struct rediwm_node *own, struct rediwm_foreign *foreign) {
- struct leaf *l;
- // An own node's leaf is kept on its carrier; the rest are found by addon.
- if (own && source->data)
+ struct rediwm_node *own) {
+ // A node's leaf is kept on its carrier.
+ if (source->data)
return source->data;
- struct wlr_addon *addon = own ? NULL : wlr_addon_find(&source->addons, p, &source_lookup_impl);
- if (addon)
- return wl_container_of(addon, l, source_lookup);
- l = calloc(1, sizeof(*l));
+ struct leaf *l = calloc(1, sizeof(*l));
if (!l)
return NULL;
l->source = source;
l->own = own;
- l->foreign = foreign;
l->projection = p;
l->item.id = ++p->next_leaf_id;
l->dirty = true;
@@ -562,13 +549,12 @@ static struct leaf *get_leaf(struct rediwm_projection *p, struct wlr_scene_node
wl_list_init(&l->paint);
wl_list_insert(&p->leaves, &l->link);
wlr_addon_init(&l->source_lookup, &source->addons, p, &source_lookup_impl);
- if (own) source->data = l;
+ source->data = l;
l->effect = source->type == WLR_SCENE_NODE_BUFFER && rediwm_glass_is_effect_node(source);
l->destroy.notify = source_destroy;
wl_signal_add(&source->events.destroy, &l->destroy);
if (source->type == WLR_SCENE_NODE_BUFFER) {
- struct wlr_scene_buffer *buffer = wlr_scene_buffer_from_node(source);
- l->surface = wlr_scene_surface_try_from_buffer(buffer);
+ l->surface = rediwm_buffer_from_node(own)->surface;
if (l->surface) {
l->damage_width = l->surface->surface->current.buffer_width;
l->damage_height = l->surface->surface->current.buffer_height;
@@ -673,7 +659,6 @@ struct walk {
const struct rediwm_clip *clip;
const struct dim *dim;
const struct rediwm_shadow *shadow;
- struct rediwm_foreign *foreign;
};
// `here` and `cast` hold the tree's clip and shadow for as long as the walk is below
// it.
@@ -712,7 +697,7 @@ static void apply_tree(struct rediwm_projection *p, struct tree_props *props, st
static void resolve_leaf(struct rediwm_projection *p, struct group *g, struct wlr_scene_node *node,
struct rediwm_node *own, const struct walk *w, double x, double y,
bool enabled) {
- struct leaf *l = get_leaf(p, node, own, w->foreign);
+ struct leaf *l = get_leaf(p, node, own);
if (!l) {
wlr_log(WLR_ERROR, "zoom: cannot allocate presentation node");
return;
@@ -720,7 +705,6 @@ static void resolve_leaf(struct rediwm_projection *p, struct group *g, struct wl
l->seen = true;
if (l->inert != w->inert) l->inert = w->inert;
if (l->own != own) l->own = own;
- if (l->foreign != w->foreign) l->foreign = w->foreign;
l->group = g;
// Flattened leaves within each group retain the source's paint order.
wl_list_insert(g->items.prev, &l->paint);
@@ -789,27 +773,6 @@ static void resolve_leaf(struct rediwm_projection *p, struct group *g, struct wl
}
item->edges = rediwm_xform_rect(w->t, x, y, item->width, item->height);
}
-// A wlroots subtree (the surfaces of a client): its nodes are the pixel sources
-// themselves.
-static void resolve_wlr(struct rediwm_projection *p, struct group *g, struct wlr_scene_node *node,
- const struct walk *parent) {
- const double x = parent->x + node->x, y = parent->y + node->y;
- const bool enabled = parent->enabled && node->enabled;
- if (node->type != WLR_SCENE_NODE_TREE) {
- resolve_leaf(p, g, node, NULL, parent, x, y, enabled);
- return;
- }
- struct walk w = *parent;
- w.x = x;
- w.y = y;
- w.enabled = enabled;
- struct rediwm_clip here;
- struct rediwm_shadow cast;
- apply_tree(p, tree_props_find(&node->addons, NULL), &w, &here, &cast);
- struct wlr_scene_node *child;
- wl_list_for_each(child, &wlr_scene_tree_from_node(node)->children, link)
- resolve_wlr(p, g, child, &w);
-}
static void resolve_own(struct rediwm_projection *p, struct group *g, struct rediwm_node *node,
const struct walk *parent) {
const double x = parent->x + node->x, y = parent->y + node->y;
@@ -834,16 +797,6 @@ static void resolve_own(struct rediwm_projection *p, struct group *g, struct red
resolve_own(p, g, child, &w);
return;
}
- case REDIWM_NODE_FOREIGN: {
- // The hosted subtree's root stays at its own origin; this node carries the place.
- struct walk w = *parent;
- w.x = x;
- w.y = y;
- w.enabled = enabled;
- w.foreign = (struct rediwm_foreign *)node;
- resolve_wlr(p, g, ((struct rediwm_foreign *)node)->wlr, &w);
- return;
- }
}
}
@@ -1046,12 +999,10 @@ bool rediwm_projection_present_owned(struct rediwm_projection *p,
void rediwm_projection_damage_buffer(struct rediwm_projection *p,
struct wlr_scene_node *source,
const pixman_region32_t *damage) {
- struct wlr_addon *addon =
- wlr_addon_find(&source->addons, p, &source_lookup_impl);
+ struct leaf *l = source->data;
// A leaf's first projection applies its whole buffer anyway.
- if (!addon)
+ if (!l || l->projection != p)
return;
- struct leaf *l = wl_container_of(addon, l, source_lookup);
if (l->surface)
return;
if (!damage ||
@@ -1071,9 +1022,8 @@ static uint32_t region_area(const pixman_region32_t *region) {
return area;
}
static struct leaf *transport_leaf(struct rediwm_projection *p, struct wlr_scene_node *node) {
- struct wlr_addon *addon = wlr_addon_find(&node->addons, p, &source_lookup_impl);
- struct leaf *l;
- return addon ? wl_container_of(addon, l, source_lookup) : NULL;
+ struct leaf *l = node->data;
+ return l && l->projection == p ? l : NULL;
}
static struct leaf *feedback_leaf(struct rediwm_projection *p, struct wlr_scene_node *node) {
struct leaf *l = transport_leaf(p, node);
@@ -1509,15 +1459,10 @@ static bool pick_leaf(struct rediwm_projection *p, const struct group *only, dou
const double cy = e->y1 + y * (e->y2 - e->y1) / item->height;
if (rediwm_clip_distance(item->clip, cx, cy) > 0) continue;
}
- if (l->own) {
- struct rediwm_buffer *own = rediwm_buffer_from_node(l->own);
- if (own->point_accepts_input && !own->point_accepts_input(own, &x, &y)) continue;
- } else {
- struct wlr_scene_buffer *source = wlr_scene_buffer_from_node(l->source);
- if (source->point_accepts_input && !source->point_accepts_input(source, &x, &y)) continue;
- }
+ struct rediwm_buffer *own = rediwm_buffer_from_node(l->own);
+ if (own->point_accepts_input && !own->point_accepts_input(own, &x, &y)) continue;
}
- *hit = (struct rediwm_hit){.own = l->own, .wlr = l->source, .foreign = l->foreign, .x = x, .y = y};
+ *hit = (struct rediwm_hit){.own = l->own, .x = x, .y = y};
return true;
}
}
diff --git a/src/projection.h b/src/projection.h
index 62a6f44..bc6eb7e 100644
--- a/src/projection.h
+++ b/src/projection.h
@@ -6,7 +6,6 @@ struct wlr_scene_tree;
struct wlr_scene;
struct rediwm_node;
struct rediwm_tree;
-struct rediwm_foreign;
struct rediwm_scene;
struct wlr_scene_buffer;
struct wlr_surface_state;
@@ -52,14 +51,10 @@ uint64_t rediwm_projection_damage_cursor(struct rediwm_projection *);
// camera.
void rediwm_projection_sync_fixed(struct rediwm_projection *, double, double, double,
double, double, struct rediwm_tree *);
-// What a point lands on. An own node has `own`; a node inside a client's subtree has
-// only `wlr`, and `foreign` is the own node that hosts that subtree. (`wlr` is
-// always the pixel source.) (x, y) is the point in the node's own coordinates
+// What a point lands on: the leaf node, and (x, y) the point in its own coordinates
// (source units for a client surface).
struct rediwm_hit {
struct rediwm_node *own;
- struct wlr_scene_node *wlr;
- struct rediwm_foreign *foreign;
double x, y;
};
bool rediwm_projection_node_at(struct rediwm_projection *, double lx, double ly,
@@ -93,14 +88,7 @@ bool rediwm_projection_set_tree_unclipped(struct rediwm_node *, bool);
// Shows the subtree but lets the pick pass through it to what is below.
bool rediwm_projection_set_tree_inert(struct rediwm_node *, bool);
// The same for a node inside a wlroots subtree (a client's popup).
-bool rediwm_projection_set_wlr_tree_zoom(struct wlr_scene_node *, double);
-bool rediwm_projection_set_wlr_tree_scale(struct wlr_scene_node *, double);
-bool rediwm_projection_set_wlr_tree_clip(struct wlr_scene_node *, const struct rediwm_clip_spec *);
struct rediwm_shadow_spec;
-bool rediwm_projection_set_wlr_tree_shadow(struct wlr_scene_node *, const struct rediwm_shadow_spec *);
-bool rediwm_projection_set_wlr_tree_dim(struct wlr_scene_node *, float brightness, float saturation);
-bool rediwm_projection_set_wlr_tree_unclipped(struct wlr_scene_node *, bool);
-bool rediwm_projection_set_wlr_tree_inert(struct wlr_scene_node *, bool);
// Buffer-local damage for the next buffer set on a compositor-owned source
// buffer node; NULL damages all of it. Surface leaves track their own.
struct pixman_region32;
diff --git a/src/projection.zig b/src/projection.zig
index fd76621..6db87eb 100644
--- a/src/projection.zig
+++ b/src/projection.zig
@@ -29,12 +29,8 @@ pub const Projection = opaque {
return rediwm_projection_present_owned(projection, target.carrier, renderer, buffer, damage);
}
pub const Hit = extern struct {
- /// The own node that was hit; null inside a client's surface tree.
- own: ?*node.Node,
- /// The node the pixels come from (an own node's carrier).
- wlr: *wlr.SceneNode,
- /// The own node that hosts the surface tree a client hit lies in.
- foreign: ?*node.Foreign,
+ /// The leaf that was hit: a client surface's node, or a shell node.
+ own: *node.Node,
x: f64,
y: f64,
};
@@ -120,7 +116,3 @@ pub const setTreeUnclipped = rediwm_projection_set_tree_unclipped;
/// window sliding off the workspace it has left.
extern fn rediwm_projection_set_tree_inert(target: *node.Node, inert: bool) bool;
pub const setTreeInert = rediwm_projection_set_tree_inert;
-
-/// The same for a tree inside a client's wlr_scene subtree (a popup's).
-extern fn rediwm_projection_set_wlr_tree_unclipped(target: *wlr.SceneNode, unclipped: bool) bool;
-pub const setWlrTreeUnclipped = rediwm_projection_set_wlr_tree_unclipped;
diff --git a/src/projection_test.c b/src/projection_test.c
index bb69bc8..29fcb25 100644
--- a/src/projection_test.c
+++ b/src/projection_test.c
@@ -260,7 +260,7 @@ static void test_graph(struct wlr_scene *scene) {
assert(client_leaf->item.kind == REDIWM_ITEM_BUFFER);
assert(glass_leaf->item.kind == REDIWM_ITEM_EFFECT);
struct wlr_surface surface = {0};
- struct wlr_scene_surface scene_surface = {.surface = &surface};
+ struct rediwm_surface scene_surface = {.surface = &surface};
client_leaf->surface = &scene_surface;
mark_all(p); // the test changed a leaf behind the scene's back
rediwm_projection_sync(p, 1, 0, 0, 0, 0);
@@ -1408,7 +1408,7 @@ int main(void) {
struct leaf *l = leaf_of(p, &node->node);
struct wlr_surface surface = {.current = {.buffer_width = 200, .buffer_height = 120}};
pixman_region32_init(&surface.buffer_damage);
- struct wlr_scene_surface scene_surface = {.surface = &surface};
+ struct rediwm_surface scene_surface = {.surface = &surface};
l->surface = &scene_surface;
l->damage_width = 200;
l->damage_height = 120;
diff --git a/src/scene_data.zig b/src/scene_data.zig
index abe0fa5..90959f2 100644
--- a/src/scene_data.zig
+++ b/src/scene_data.zig
@@ -1,7 +1,7 @@
-// Typed identity stored in `wlr.SceneNode.data` (and the matching xdg-surface
+// Typed identity stored in `Node.data` (and the matching xdg-surface
// user pointer used to parent popups).
//
-// Invariant: every non-null `SceneNode.data` in this compositor is a
+// Invariant: every non-null `Node.data` of a window, shell panel or layer is a
// `*SceneData`. Hit-testing must go through `fromNode` / `hitTest` rather than
// casting the opaque pointer to a window or widget type.
const wlr = @import("wlroots");
@@ -63,13 +63,6 @@ pub const SceneData = struct {
}
};
-/// The role of a node inside a client's surface tree: up to the root of the hosted
-/// subtree, then on through the own tree that places it.
-pub fn fromWlrNode(node: *wlr.SceneNode) ?*SceneData {
- const hosted = sn.Foreign.of(node) orelse return null;
- return SceneData.fromNodeOrParents(&hosted.node);
-}
-
pub const Hit = union(enum) {
none,
input_popup: struct { surface: *wlr.Surface, sx: f64, sy: f64 },
@@ -216,7 +209,7 @@ pub fn hitTest(server: *@import("Server.zig"), lx: f64, ly: f64) Hit {
// leaves, so subsurfaces, popups, chrome and client input regions retain their
// normal semantics.
if (server.world.peekTarget()) |selected| {
- if (SceneData.fromNodeOrParents(hitNode(hit))) |data| {
+ if (SceneData.fromNodeOrParents(hit.own)) |data| {
const owner: ?*Toplevel = switch (data.role) {
.toplevel => |t| t,
.chrome => |t| t,
@@ -230,51 +223,33 @@ pub fn hitTest(server: *@import("Server.zig"), lx: f64, ly: f64) Hit {
const nx = hit.x;
const ny = hit.y;
- // An own node of the shell: its role says what it is.
- if (hit.own) |own| {
- const data = SceneData.fromNode(own) orelse return .none;
- return shellHit(data, lx, ly);
- }
-
- // A client's surface: found through the subtree that hosts it.
- if (hit.wlr.type != .buffer) return .none;
- const scene_surface = wlr.SceneSurface.tryFromBuffer(wlr.SceneBuffer.fromNode(hit.wlr)) orelse return .none;
- if (wlr.InputPopupSurfaceV2.tryFromWlrSurface(scene_surface.surface.getRootSurface()) != null) {
- return .{ .input_popup = .{ .surface = scene_surface.surface, .sx = nx, .sy = ny } };
- }
- const hosted = hit.foreign orelse return .none;
- const data = SceneData.fromNodeOrParents(&hosted.node) orelse return .none;
- return switch (data.role) {
- .toplevel => |toplevel| .{ .surface = .{
- .toplevel = toplevel,
- .surface = scene_surface.surface,
- .sx = nx,
- .sy = ny,
- } },
- .layer => |owner| .{ .layer = .{ .owner = owner, .surface = scene_surface.surface, .sx = nx, .sy = ny } },
- .xwayland_unmanaged => |owner| .{ .xwayland_unmanaged = .{
- .owner = owner,
- .surface = scene_surface.surface,
- .sx = nx,
- .sy = ny,
- } },
- else => .none,
+ // A client's surface: the role is the window, layer or X11 menu it is a part of.
+ if (hit.own.buffer()) |buffer| if (buffer.surface) |scene_surface| {
+ const surface = scene_surface.surface;
+ if (wlr.InputPopupSurfaceV2.tryFromWlrSurface(surface.getRootSurface()) != null) {
+ return .{ .input_popup = .{ .surface = surface, .sx = nx, .sy = ny } };
+ }
+ const data = SceneData.fromNodeOrParents(hit.own) orelse return .none;
+ return switch (data.role) {
+ .toplevel => |toplevel| .{ .surface = .{ .toplevel = toplevel, .surface = surface, .sx = nx, .sy = ny } },
+ .layer => |owner| .{ .layer = .{ .owner = owner, .surface = surface, .sx = nx, .sy = ny } },
+ .xwayland_unmanaged => |owner| .{ .xwayland_unmanaged = .{ .owner = owner, .surface = surface, .sx = nx, .sy = ny } },
+ else => .none,
+ };
};
+
+ // An own node of the shell: its role says what it is.
+ const data = SceneData.fromNodeOrParents(hit.own) orelse return .none;
+ return shellHit(data, lx, ly);
}
/// xdg_surface.data holds the scene tree created for that surface so popups
/// can parent themselves without walking the compositor's own objects.
-pub fn setXdgSceneTree(xdg_surface: *wlr.XdgSurface, tree: *wlr.SceneTree) void {
+pub fn setXdgSceneTree(xdg_surface: *wlr.XdgSurface, tree: *sn.Tree) void {
xdg_surface.data = tree;
}
-pub fn xdgSceneTree(xdg_surface: *wlr.XdgSurface) ?*wlr.SceneTree {
+pub fn xdgSceneTree(xdg_surface: *wlr.XdgSurface) ?*sn.Tree {
const ptr = xdg_surface.data orelse return null;
return @ptrCast(@alignCast(ptr));
}
-
-/// The own node a hit belongs to: the node itself, or the one that hosts the client
-/// surface it lies in.
-fn hitNode(hit: @import("projection.zig").Projection.Hit) *sn.Node {
- return hit.own orelse &hit.foreign.?.node;
-}
diff --git a/src/session/client_lock.zig b/src/session/client_lock.zig
index a304a84..8b3a229 100644
--- a/src/session/client_lock.zig
+++ b/src/session/client_lock.zig
@@ -263,7 +263,7 @@ const Surface = struct {
lock_surface: *wlr.SessionLockSurfaceV1,
/// Null once the output is gone; wlroots destroys the surface next.
output: ?*Output,
- tree: ?*sn.Foreign,
+ tree: ?*sn.Tree,
width: i32 = 0,
height: i32 = 0,
link: wl.list.Link = undefined,
diff --git a/src/xwayland_unmanaged.zig b/src/xwayland_unmanaged.zig
index 7ed72c2..0bbc2aa 100644
--- a/src/xwayland_unmanaged.zig
+++ b/src/xwayland_unmanaged.zig
@@ -18,7 +18,7 @@ const log = std.log.scoped(.xwayland);
server: *Server,
xsurface: *wlr.XwaylandSurface,
tree: *sn.Tree,
-scene_tree: ?*sn.Foreign = null,
+scene_tree: ?*sn.Tree = null,
node_data: scene_data.SceneData = undefined,
destroy: wl.Listener(void) = .init(handleDestroy),