diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 17:35:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 17:35:06 -0300 |
| commit | 6d8da8721141284cf33bb901168b26135893eb86 (patch) | |
| tree | 791d63401d4e85fbca1d597a8ce0326e70efd545 /src/detached/client.zig | |
| parent | efd43a3a2cf03bb741366331158b368abf9ac583 (diff) | |
| parent | bf6810a8f9d6cc82050e7d9c0cfb06358301b574 (diff) | |
| download | pardes-6d8da8721141284cf33bb901168b26135893eb86.tar.gz pardes-6d8da8721141284cf33bb901168b26135893eb86.zip | |
Merge latest upstream into the macOS branch for Zig 0.17
Retain the macOS branch history and local Acme selection color while adopting upstream's current core and platform changes. Pin Zig and ZLS to 0.17.0, and fetch the existing zstbi compatibility commit through upstream after its original fork was removed.
Diffstat (limited to 'src/detached/client.zig')
| -rw-r--r-- | src/detached/client.zig | 121 |
1 files changed, 99 insertions, 22 deletions
diff --git a/src/detached/client.zig b/src/detached/client.zig index 9481cd54..617ae342 100644 --- a/src/detached/client.zig +++ b/src/detached/client.zig @@ -59,6 +59,7 @@ const std = @import("std"); const libc = std.c; const pardes = @import("../pardes.zig"); +const dump = @import("../dump.zig"); const server = @import("server.zig"); const wire = @import("wire.zig"); @@ -102,8 +103,16 @@ pub const Client = struct { grid: std.ArrayListUnmanaged(pardes.Cell) = .empty, cursor: ?wire.Cursor = null, pointer_shape: pardes.Surface.PointerShape = .arrow, - body_layers: [pardes.MAX_PANES]pardes.BodyLayer = @splat(.{}), - tag_layers: [pardes.MAX_TAG_LAYERS]pardes.TagLayer = @splat(.{}), + body_layers: [pardes.MAX_PANES]pardes.Layer = @splat(.{}), + tag_layers: [pardes.MAX_TAG_LAYERS]pardes.Layer = @splat(.{}), + /// The session's placed regions, as its last frame gave them. + regions: std.ArrayListUnmanaged(pardes.Region) = .empty, + /// The palette its chrome is drawn in, as its last frame gave it. + chrome: pardes.Chrome = .{}, + /// The post chain its frames are drawn through, as the session last + /// sent it; its paths and SPIR-V owned here. `post_key` moves with it. + post: wire.Post = .{}, + post_key: u32 = 0, in: std.ArrayListUnmanaged(u8) = .empty, out: std.ArrayListUnmanaged(u8) = .empty, /// Bytes of `in` belonging to the message `next` returned last. Compacted at @@ -156,7 +165,7 @@ pub const Client = struct { _ = libc.close(fd); return error.NoSession; } - server.setNonblock(fd); + _ = server.setNonblock(fd); var c: Client = .{ .gpa = gpa, .fd = fd }; errdefer c.deinit(); try c.send(.{ .hello = .{ @@ -181,6 +190,8 @@ pub const Client = struct { layer.* = .{}; } for (&c.tag_layers) |*layer| c.gpa.free(layer.cells); + c.regions.deinit(c.gpa); + c.freePost(); c.in.deinit(c.gpa); c.out.deinit(c.gpa); } @@ -214,7 +225,7 @@ pub const Client = struct { // next flush: the session decodes it as a message, refuses it, and // drops a frontend whose only mistake was a message this protocol // cannot carry (an `Event.command` past 64 KiB is `Overlong`, and a - // caller that mis-sized the queue is `NoSpace`). + // caller that mis-sized the queue is `WriteFailed`). c.out.items.len = at; return err; }; @@ -318,7 +329,7 @@ pub const Client = struct { try c.reshape(f.cols, f.rows); } try f.apply(c.grid.items); - f.applyLayers(c.gpa, &c.body_layers, &c.tag_layers) catch |err| return switch (err) { + f.applyLayers(c.gpa, &c.body_layers, &c.tag_layers, &c.regions, &c.chrome) catch |err| return switch (err) { error.OutOfMemory => error.Closed, else => @as(wire.Error, @errorCast(err)), }; @@ -350,10 +361,30 @@ pub const Client = struct { // open a URI on this display or read this display's selection back // over the socket. `Ungreeted` is the same refusal a frame gets. .set_clipboard, .read_clipboard, .open_link => if (!c.attached()) return error.Ungreeted, + // Kept, like the chrome: the frames that follow are drawn + // through it. + .post => |chain| { + if (!c.attached()) return error.Ungreeted; + c.freePost(); + c.post = chain; + for (c.post.passes[0..c.post.len]) |*pass| { + pass.path = c.gpa.dupe(u8, pass.path) catch return error.Closed; + pass.spirv = c.gpa.dupe(u8, pass.spirv) catch return error.Closed; + } + c.post_key +%= 1; + }, } return msg; } + fn freePost(c: *Client) void { + for (c.post.passes[0..c.post.len]) |pass| { + c.gpa.free(pass.path); + c.gpa.free(pass.spirv); + } + c.post = .{}; + } + // ---- internals -------------------------------------------------------- fn reshape(c: *Client, cols: u16, rows: u16) (Error || wire.Error)!void { @@ -837,24 +868,25 @@ test "detached Restore keeps attached frontends and follows queued frames with a const fd = h.session.clients[c.slot].fd; try testing.expectError(error.BadDumpMagic, h.session.restore( ".{ .magic = \"not-a-pardes-dump\", .theme = \"dark\", .screen = .{ .cols = 60, .rows = 16 } }", + "test", )); try testing.expectEqual(before, h.session.core); try testing.expectEqual(fd, h.session.clients[c.slot].fd); try testing.expect(h.session.clients[c.slot].attached); - try h.core.dumpState(); + try dump.dumpState(h.core); const saved = try testing.allocator.dupe(u8, h.core.dump_out.?); defer testing.allocator.free(saved); while (h.core.nextEffect()) |_| {} _ = try h.core.setTestFile("changed after dump\n"); - try h.session.restore(saved); + try h.session.restore(saved, "test"); h.core = h.session.core; try testing.expect(h.core != before); try testing.expectEqual(fd, h.session.clients[c.slot].fd); try testing.expect(h.session.clients[c.slot].attached); try testing.expectEqualStrings("saved body\n", h.core.panes[0].?.file.?.content); try testing.expect(h.session.clients[c.slot].need_full); - var wake = [_]libc.pollfd{.{ .fd = h.session.mailbox.wake[0], .events = poll_in, .revents = 0 }}; + var wake = [_]libc.pollfd{.{ .fd = h.session.wake[0], .events = poll_in, .revents = 0 }}; try testing.expectEqual(@as(c_int, 1), libc.poll(&wake, wake.len, 0)); for (0..Harness.rounds) |_| { if ((try h.pumpUntil(&c, .frame)).frame.kind == .full) break; @@ -867,9 +899,9 @@ test "detached selection pipe runs off the loop and returns through the attached try h.init(60, 16); defer h.deinit(); const pane = try h.core.setTestFile("one\ntwo\n"); - pane.cur_row = 0; - pane.cur_col = 2; - pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + pane.body.cur_row = 0; + pane.body.cur_col = 2; + pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; var client = try h.attach(60, 16); defer client.deinit(); _ = try h.pumpUntil(&client, .frame); @@ -883,7 +915,7 @@ test "detached selection pipe runs off the loop and returns through the attached while (try client.next()) |_| {} if (std.mem.eql(u8, pane.file.?.content, "ONE\ntwo\n")) break; } else return error.PipeDidNotComplete; - try testing.expect(h.core.pipe_wait == null); + try testing.expect(h.core.pipe.wait == null); try testing.expectEqual(@as(usize, 0), h.session.pipe_tasks.len); try h.pumpUntilShowsCore(&client); } @@ -994,6 +1026,12 @@ test "detached session: a frontend that dies takes nothing with it" { } test "detached session: a frontend speaking another protocol is refused, loudly" { + // The protocol before this one (v7 carried one-row tags and no regions) + // and the one after. + for ([_]u16{ wire.version - 1, wire.version + 1 }) |claimed| try refusedHello(claimed); +} + +fn refusedHello(claimed: u16) !void { var h: Harness = undefined; try h.init(60, 16); defer h.deinit(); @@ -1005,7 +1043,7 @@ test "detached session: a frontend speaking another protocol is refused, loudly" defer _ = libc.close(fd); var buf: [64]u8 = undefined; const hello = try wire.encodeClient(&buf, .{ - .hello = .{ .version = wire.version + 1, .cols = 60, .rows = 16 }, + .hello = .{ .version = claimed, .cols = 60, .rows = 16 }, }); try testing.expectEqual(@as(isize, @intCast(hello.len)), libc.send(fd, hello.ptr, hello.len, nosignal)); @@ -1023,6 +1061,17 @@ test "detached session: a frontend speaking another protocol is refused, loudly" try testing.expect(!h.core.quit); } +test "detached frontend: a session speaking another protocol is not attached to" { + for ([_]u16{ wire.version - 1, wire.version + 1 }) |claimed| { + var c: Client = .{ .gpa = testing.allocator }; + defer c.deinit(); + var buf: [64]u8 = undefined; + try c.in.appendSlice(testing.allocator, try wire.encodeServer(&buf, .{ .welcome = .{ .version = claimed, .slot = 0, .cols = 60, .rows = 16 } })); + try testing.expectError(error.Ungreeted, c.next()); + try testing.expect(!c.attached()); + } +} + test "detached session: a peer that sends garbage is dropped, not obeyed" { var h: Harness = undefined; try h.init(60, 16); @@ -1107,7 +1156,7 @@ test "detached session: the seam's own routing rules, per surviving effect" { test "detached session: a frontend is never asked to fork, write, or watch" { // THE INVARIANT OF THE WHOLE DETACHED DESIGN, pinned as a property of the // protocol rather than of one code path: a frontend is input and screen, so - // the set of messages that can reach it is exactly the eight below. If a + // the set of messages that can reach it is exactly the nine below. If a // machine-local effect is ever put back on the wire, a frontend becomes the // process that owns a pane's shell again — and a pane whose shell belongs to // a frontend dies when that frontend leaves, which is the bug this replaced. @@ -1125,16 +1174,20 @@ test "detached session: a frontend is never asked to fork, write, or watch" { // The three that need THIS human's display and cannot be done by a // daemon nobody is looking at. "set_clipboard", "read_clipboard", "open_link", + // Display state, like a frame's chrome: the passes this display + // draws its frames through. A file's SPIR-V arrives compiled, so the + // frontend still reads no disk and runs no program. + "post", }; // One: the union a frontend decodes into has no other variant. Named // rather than counted, so re-adding `spawn` fails with the name in it. - const fields = @typeInfo(wire.ServerMsg).@"union".fields; - inline for (fields) |f| { + const fields = @typeInfo(wire.ServerMsg).@"union".field_names; + inline for (fields) |name| { for (allowed) |ok| { - if (std.mem.eql(u8, f.name, ok)) break; + if (std.mem.eql(u8, name, ok)) break; } else { - std.debug.print("ServerMsg.{s} is not an effect a frontend may perform\n", .{f.name}); + std.debug.print("ServerMsg.{s} is not an effect a frontend may perform\n", .{name}); return error.MachineLocalEffectOnTheWire; } } @@ -1148,7 +1201,7 @@ test "detached session: a frontend is never asked to fork, write, or watch" { for (0..256) |i| { const tag: u8 = @intCast(i); // Payloads are deliberately empty: what is asked is whether the TAG is - // known, and every known tag fails later (`Truncated`) or succeeds, but + // known, and every known tag fails later (`EndOfStream`) or succeeds, but // never with `BadTag`. if (wire.decodeServer(tag, &.{})) |_| accepted += 1 else |err| switch (err) { error.BadTag => continue, @@ -1156,10 +1209,10 @@ test "detached session: a frontend is never asked to fork, write, or watch" { } const named = for (allowed) |ok| { const want = std.meta.stringToEnum(wire.ServerTag, ok).?; - if (@intFromEnum(want) == tag) break true; + if (@backingInt(want) == tag) break true; } else false; if (!named) { - std.debug.print("tag 0x{x:0>2} is decodable by a frontend and is not one of the seven\n", .{tag}); + std.debug.print("tag 0x{x:0>2} is decodable by a frontend and is not one of the nine\n", .{tag}); return error.MachineLocalEffectOnTheWire; } } @@ -1266,8 +1319,32 @@ test "detached session: a frontend that stops reading is dropped, not waited for // for, which is the whole claim: one slow peer costs its own slot. try testing.expect(h.session.clients[0].attached); try testing.expect(!h.core.quit); + // A message is a tagline band now, not a grid run, so "still being drawn + // for" is checked on the layers the frontend decoded rather than on nruns. try good.send(.{ .event = .{ .command = "Msg still here" } }); - try testing.expect((try h.pumpUntilChange(&good)).nruns > 0); + try testing.expect(try pumpUntilNotice(&h, &good, "still here")); +} + +/// Pump until this client has a notice band carrying `want`. +fn pumpUntilNotice(h: *Harness, c: *Client, want: []const u8) !bool { + var line: [256]u8 = undefined; + for (0..Harness.rounds) |_| { + try h.pump(); + try c.wait(5); + while (try c.next()) |_| {} + for (&c.tag_layers) |*layer| { + if (layer.rows == 0 or layer.kind != .notice) continue; + var n: usize = 0; + for (layer.cells[0..layer.cols]) |cell| { + const g = cell.grapheme(); + if (n + g.len > line.len) break; + @memcpy(line[n..][0..g.len], g); + n += g.len; + } + if (std.mem.indexOf(u8, line[0..n], want) != null) return true; + } + } + return false; } /// A connected socket with nothing said on it yet, for the tests whose peer is |
