From cf347f91ee838759dc44ed34ec6ff320186e90ce Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 01:44:36 -0300 Subject: One verb table: the flags, the console's, the script's and ctl's words are rows Six hand-kept lists said one vocabulary: root.zig's parser, its usage and its header; the console's completion list, its help strings and the execute chain; Host.zig's list of lines that do not move State; the script's header comment and its step chain; control.zig's names and its magic node numbers. Now each is a table, and the parser, the help, the completion and the dispatch read it. - root.zig: `Flag`, an enum, and `flags`, a row each (arg, doc): the loop looks the word up, takes its argument when the row names one, and a switch sets what it means; `usage` is made from the rows at compile time (the same text, and a line for --help). Errors say the flag and what it needs. - Console.zig: `Verb` and `verbs`, a row each (args, doc, moves_state): execute is a switch on the verb, a line whose first word is none goes to the sketch as before; Tab completes from the rows (a verb, and with each lowercase word its first argument may be: `seek start`); `help` prints the rows wrapped to the log's width, `help VERB` its line. - Host.zig: movesState asks the row; a sketch's command moves State. - script.zig: `Verb` and `verbs` (args, doc, frame, input): step is a switch, a row with `frame` is followed by one; the input verbs are `input`, which a 9P ctl will call live. - control.zig: `File` and `files` (mode, doc), a node being its number (the same numbers); `Verb` and `verbs` for ctl's own two words. Behaviour is unchanged but for help's text, completion's list (the same words, and `timing`, `play`, `seek`... alone) and errors' words. The diff adds more than it deletes (+630 -521): the rows carry a line of doc for every verb, which nothing had before. Tests: test-control, test-timing, test-setup, test-same, test-kill, test-past, test-import, test-paths: 70/70 steps, 29/29 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/control.zig | 100 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 37 deletions(-) (limited to 'src/host/control.zig') diff --git a/src/host/control.zig b/src/host/control.zig index 8cc8296..33b186b 100644 --- a/src/host/control.zig +++ b/src/host/control.zig @@ -12,7 +12,27 @@ const Backend = struct { }; const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4 }); const msize = 16384; -const names = [_][]const u8{ "/", "ctl", "status", "log", "screen.png", "profile", "profile-trace", "timing-trace", "presentation-trace" }; +/// The files, a row each: what lookup, readdir and getattr answer. A +/// file's node is its number here. +const File = enum(u64) { @"/" = 1, ctl, status, log, @"screen.png", profile, @"profile-trace", @"timing-trace", @"presentation-trace" }; +const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const u8 }).init(.{ + .@"/" = .{ .mode = 0o700, .doc = "" }, + .ctl = .{ .mode = 0o600, .doc = "write console lines, a line each; screenshot captures the next frame, profile-reset clears profile" }, + .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" }, + .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" }, + .@"presentation-trace" = .{ .doc = "on Wayland: presentation feedback per frame, as CSV" }, +}); + +/// 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", + .@"profile-reset" = "clears profile's counters and frame count, keeping its labels", +}); const Connection = struct { fd: c_int = -1, @@ -128,31 +148,28 @@ pub const Control = struct { } fn respond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { - if (req.node < 1 or req.node > names.len) return error.UnknownFile; + const file = std.enums.fromInt(File, req.node) orelse return error.UnknownFile; switch (req.op) { .lookup => { - if (req.node != 1) return error.NotDirectory; - for (names[1..], 2..) |name, node| if (std.mem.eql(u8, name, req.data)) { - reply.attr = self.attr(node); - return &.{}; - }; - return error.UnknownFile; + if (file != .@"/") return error.NotDirectory; + const found = std.meta.stringToEnum(File, req.data) orelse return error.UnknownFile; + if (found == .@"/") return error.UnknownFile; + reply.attr = self.attr(@backingInt(found)); }, .getattr => reply.attr = self.attr(req.node), .open => { - if (req.node == 5 or req.node == 7 or req.node == 8 or req.node == 9) { - if (req.node == 5) { + if (file == .@"screen.png" or file == .@"profile-trace" or file == .@"timing-trace" or file == .@"presentation-trace") { + if (file == .@"screen.png") { if (self.capture_pending) return error.CapturePending; if (self.capture_error) |err| return err; if (self.png.len == 0) return error.CaptureScreenshotFirst; } for (&conn.snapshots, 1..) |*slot, handle| if (slot.* == null) { - slot.* = switch (req.node) { - 5 => try self.allocator.dupe(u8, self.png), - 7 => try self.trace(host), - 8 => try self.timingTrace(host), - 9 => try self.presentationTrace(host), - else => unreachable, + slot.* = switch (file) { + .@"screen.png" => try self.allocator.dupe(u8, self.png), + .@"profile-trace" => try self.trace(host), + .@"timing-trace" => try self.timingTrace(host), + else => try self.presentationTrace(host), }; reply.handle = @intCast(handle); return &.{}; @@ -166,10 +183,11 @@ pub const Control = struct { slot.* = null; }, .readdir => { - if (req.node != 1) return error.NotDirectory; + if (file != .@"/") return error.NotDirectory; var used: usize = 0; - for (names[1..], 0..) |name, index| { + for (std.enums.values(File)[1..], 0..) |f, index| { if (index < req.off) continue; + const name = @tagName(f); std.mem.writeInt(u64, buffer[used..][0..8], index + 2, .little); buffer[used + 8] = 0; buffer[used + 9] = @intCast(name.len); @@ -180,21 +198,21 @@ pub const Control = struct { }, .read => { var writer: std.Io.Writer = .fixed(buffer); - const bytes: []const u8 = switch (req.node) { - 2 => "Write one console command per line. screenshot captures the next frame.\n", - 3 => blk: { + const bytes: []const u8 = switch (file) { + .ctl => "Write one console command per line. screenshot captures the next frame.\n", + .status => blk: { try writer.print("name {s}\nsize {d} {d}\nmode {t}\ngpu {s}\nscreenshot_pending {}\nscreenshot_generation {d}\nscreenshot_error {s}\n", .{ host.name, host.canvas[0], host.canvas[1], host.timeline.mode, if (host.gpu != null) "yes" else "no", self.capture_pending, self.generation, if (self.capture_error) |err| @errorName(err) else "none" }); break :blk writer.buffered(); }, - 4 => blk: { + .log => blk: { for (host.console.lines) |line| { try writer.writeAll(line.bytes[0..line.len]); try writer.writeByte('\n'); } break :blk writer.buffered(); }, - 5, 7, 8, 9 => if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle, - 6 => blk: { + .@"screen.png", .@"profile-trace", .@"timing-trace", .@"presentation-trace" => if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle, + .profile => blk: { const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; try writer.print("# CPU wall milliseconds including GPU submission and fence wait; not hardware timestamps\n# frames {d}\nlabel\tcount\tmean_ms\tworst_ms\tp95_ms\tp99_ms\n", .{gpu.frame - profile.start_frame}); @@ -207,30 +225,37 @@ pub const Control = struct { } break :blk writer.buffered(); }, - else => return error.NotFile, + .@"/" => return error.NotFile, }; return window(bytes, req.off, req.size); }, .write => { - if (req.node != 2) return error.ReadOnly; + if (file != .ctl) return error.ReadOnly; if (req.data.len > 4096 or std.mem.indexOfScalar(u8, req.data, 0) != null) return error.InvalidCommand; var lines = std.mem.tokenizeScalar(u8, req.data, '\n'); while (lines.next()) |raw| { const line = std.mem.trim(u8, raw, " \t\r"); - if (std.mem.eql(u8, line, "screenshot")) { - self.capture_pending = true; - self.capture_error = null; - } else if (std.mem.eql(u8, line, "profile-reset")) { - const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; - const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; - for (profile.entries.values()) |*entry| entry.* = .{}; - profile.start_frame = gpu.frame; - } else try host.runLine(line, c.SDL_GetTicksNS()); + const verb = std.meta.stringToEnum(Verb, line) orelse { + try host.runLine(line, c.SDL_GetTicksNS()); + continue; + }; + switch (verb) { + .screenshot => { + self.capture_pending = true; + self.capture_error = null; + }, + .@"profile-reset" => { + const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; + const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; + for (profile.entries.values()) |*entry| entry.* = .{}; + profile.start_frame = gpu.frame; + }, + } } reply.written = @intCast(req.data.len); }, .setattr => { - if (req.node != 2 or !req.truncate) return error.ReadOnly; + if (file != .ctl or !req.truncate) return error.ReadOnly; reply.attr = self.attr(req.node); }, } @@ -279,7 +304,8 @@ pub const Control = struct { } fn attr(self: *Control, node: u64) fs.Attr { - return .{ .name = names[@intCast(node - 1)], .node = node, .dir = node == 1, .mode = if (node == 1) 0o700 else if (node == 2) 0o600 else 0o400, .size = if (node == 5) self.png.len else 0, .version = if (node == 5) @truncate(self.generation) else 0 }; + const file: File = @fromBackingInt(@intCast(node)); + return .{ .name = @tagName(file), .node = node, .dir = file == .@"/", .mode = files.get(file).mode, .size = if (file == .@"screen.png") self.png.len else 0, .version = if (file == .@"screen.png") @truncate(self.generation) else 0 }; } pub fn capture(self: *Control, surface: *c.SDL_Surface) void { -- cgit v1.3