a73x

aba50517

Bump xdg_wm_base to v6 and honor toplevel suspended state

a73x   2026-04-16 10:53

The scanner already generates v6 bindings (build.zig:31); this commit
bumps the runtime bind version and hooks xdg_toplevel.configure.states
into SurfaceState.suspended. Compositors that don't send .suspended
(pre-v6 impls) see no behavior change.

diff --git a/src/wayland.zig b/src/wayland.zig
index 951a92b..4b2680e 100644
--- a/src/wayland.zig
+++ b/src/wayland.zig
@@ -1078,11 +1078,27 @@ fn surfaceListener(_: *wl.Surface, event: wl.Surface.Event, window: *Window) voi
    }
}

fn applyToplevelStates(state: *SurfaceState, state_bytes: []const u8) void {
    // xdg_toplevel.configure delivers the state array as a wl_array of u32.
    // Re-interpret to u32 slice and scan for `.suspended`.
    const u32_count = state_bytes.len / @sizeOf(u32);
    const states = std.mem.bytesAsSlice(u32, state_bytes[0 .. u32_count * @sizeOf(u32)]);
    var suspended = false;
    for (states) |raw| {
        if (raw == @intFromEnum(xdg.Toplevel.State.suspended)) {
            suspended = true;
            break;
        }
    }
    state.suspended = suspended;
}

fn xdgToplevelListener(_: *xdg.Toplevel, event: xdg.Toplevel.Event, window: *Window) void {
    switch (event) {
        .configure => |cfg| {
            if (cfg.width > 0) window.width = @intCast(cfg.width);
            if (cfg.height > 0) window.height = @intCast(cfg.height);
            applyToplevelStates(&window.state, std.mem.sliceAsBytes(cfg.states.slice(u32)));
        },
        .close => window.should_close = true,
        .configure_bounds => {},
@@ -1103,7 +1119,7 @@ fn registryListener(
            } else if (std.mem.eql(u8, iface, std.mem.span(wl.DataDeviceManager.interface.name))) {
                conn.globals.data_device_manager = registry.bind(g.name, wl.DataDeviceManager, 3) catch return;
            } else if (std.mem.eql(u8, iface, std.mem.span(xdg.WmBase.interface.name))) {
                conn.globals.wm_base = registry.bind(g.name, xdg.WmBase, 5) catch return;
                conn.globals.wm_base = registry.bind(g.name, xdg.WmBase, 6) catch return;
            } else if (std.mem.eql(u8, iface, std.mem.span(wl.Seat.interface.name))) {
                conn.globals.seat = registry.bind(g.name, wl.Seat, 9) catch return;
            } else if (std.mem.eql(u8, iface, std.mem.span(wl.Output.interface.name))) {
@@ -1257,3 +1273,24 @@ test "SurfaceState.visible requires configured && !suspended && enteredCount > 0
    tracker.leaveOutput(1);
    try std.testing.expect(!state.visible());
}

test "SurfaceState.suspended toggles from xdg_toplevel.configure.states" {
    var tracker = ScaleTracker.init(std.testing.allocator);
    defer tracker.deinit();
    try tracker.addOutput(1);
    try tracker.enterOutput(1);

    var state = SurfaceState{ .tracker = &tracker, .configured = true };
    try std.testing.expect(state.visible());

    // Helper function under test — applies xdg_toplevel state array to SurfaceState.
    const states_suspended = [_]u32{@intFromEnum(xdg.Toplevel.State.suspended)};
    applyToplevelStates(&state, std.mem.sliceAsBytes(&states_suspended));
    try std.testing.expect(state.suspended);
    try std.testing.expect(!state.visible());

    const states_none = [_]u32{};
    applyToplevelStates(&state, std.mem.sliceAsBytes(&states_none));
    try std.testing.expect(!state.suspended);
    try std.testing.expect(state.visible());
}