diff options
Diffstat (limited to 'src/host/control.zig')
| -rw-r--r-- | src/host/control.zig | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/host/control.zig b/src/host/control.zig index bf1e12d..6fb997b 100644 --- a/src/host/control.zig +++ b/src/host/control.zig @@ -24,7 +24,7 @@ const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const .ctl = .{ .mode = 0o600, .doc = "write a verb a line, of those below: ctl's own, the input a script plays (live, no frame of its own), the console's; any other line goes to the sketch (commands lists its words)" }, .status = .{ .doc = "name, canvas size, playback mode, gpu, and the screenshot's state" }, .log = .{ .doc = "the console's last five lines" }, - .@"screen.png" = .{ .doc = "the frame screenshot captured, as PNG; each open pins its own copy" }, + .@"screen.png" = .{ .doc = "the next frame, as PNG: an open waits for it to be drawn, and pins its own copy" }, .profile = .{ .doc = "with --profile-gpu: label, count, mean, worst, p95 and p99 ms, tab-separated" }, .@"profile-trace" = .{ .doc = "with --profile-gpu: label,sample,ns, the samples kept, as CSV" }, .@"timing-trace" = .{ .doc = "index,frame_ns,work_ns,outside_work_ns, the frames kept, as CSV" }, @@ -37,7 +37,7 @@ const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const /// The verbs `ctl` takes beside the console's lines. const Verb = enum { screenshot, @"profile-reset" }; const verbs = std.enums.EnumArray(Verb, []const u8).init(.{ - .screenshot = "captures the next frame for screen.png", + .screenshot = "captures the next frame (status says when); screen.png's open does it itself", .@"profile-reset" = "clears profile's counters and frame count, keeping its labels", }); @@ -124,7 +124,8 @@ pub const Control = struct { conn.used -= consumed; progressed = consumed != 0; } - while (conn.server.next()) |req| self.answer(host, conn, req); + while (conn.server.retry()) |req| self.answer(host, conn, req, true); + while (conn.server.next()) |req| self.answer(host, conn, req, false); if (conn.server.protocol.dead) return error.Closed; if (conn.server.output().len > 0) { if (try transport.write(conn.fd, conn.server.output())) |n| { @@ -144,11 +145,18 @@ pub const Control = struct { } } - fn answer(self: *Control, host: *Host, conn: *Connection, req: fs.Req) void { + /// Answers a request, or parks it: an open of screen.png asks for the + /// next frame and waits for it, asked again (`retried`) each frame + /// until the capture has landed. + fn answer(self: *Control, host: *Host, conn: *Connection, req: fs.Req, retried: bool) void { var reply: fs.Reply = .{ .tag = req.tag }; var buffer: [msize]u8 = undefined; + if (!retried and req.op == .open and req.node == @backingInt(File.@"screen.png")) { + self.capture_pending = true; + self.capture_error = null; + } const bytes = self.respond(host, conn, req, &reply, &buffer) catch |err| blk: { - reply = .{ .tag = req.tag, .status = .err, .errno = fs.E.INVAL, .ename = @errorName(err) }; + reply = if (err == error.CapturePending) .{ .tag = req.tag, .status = .again } else .{ .tag = req.tag, .status = .err, .errno = fs.E.INVAL, .ename = @errorName(err) }; break :blk &.{}; }; conn.server.reply(&reply, bytes); @@ -594,7 +602,7 @@ test "control writes take input verbs live, and the README and commands read the try std.testing.expectEqual(abi.Event.Kind.key_down, host.pending.items[3].kind); try std.testing.expectError(error.ExpectedPoint, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "click 10" }, &reply, &buf)); for ([_]File{ .README, .commands }) |f| { - _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = @intFromEnum(f) }, &reply, &buf); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = @backingInt(f) }, &reply, &buf); const handle = reply.handle; const text = conn.snapshots[handle - 1].?; if (f == .README) { @@ -608,6 +616,6 @@ test "control writes take input verbs live, and the README and commands read the try std.testing.expect(std.mem.startsWith(u8, text, "screenshot captures")); try std.testing.expect(std.mem.endsWith(u8, text, "\nscene room\nscene street\n")); } - _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = @intFromEnum(f), .handle = handle, .opened = true }, &reply, &buf); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = @backingInt(f), .handle = handle, .opened = true }, &reply, &buf); } } |
