diff options
Diffstat (limited to 'src/host/control.zig')
| -rw-r--r-- | src/host/control.zig | 60 |
1 files changed, 54 insertions, 6 deletions
diff --git a/src/host/control.zig b/src/host/control.zig index 33b186b..1a8da92 100644 --- a/src/host/control.zig +++ b/src/host/control.zig @@ -4,17 +4,18 @@ const c9 = @import("cloud9"); const c = @import("c"); const Host = @import("Host.zig"); const Presentation = @import("Presentation.zig"); +const state_files = @import("state_files.zig"); const fs = c9.fs; const transport = c9.transport; const Backend = struct { pub const Req = fs.Req; pub const Reply = fs.Reply; }; -const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4 }); +const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4, .name_capacity = 64 }); const msize = 16384; /// 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 File = enum(u64) { @"/" = 1, ctl, status, log, @"screen.png", profile, @"profile-trace", @"timing-trace", @"presentation-trace", state = @bitCast(state_files.root) }; 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" }, @@ -25,6 +26,7 @@ const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const .@"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" }, + .state = .{ .mode = 0o700, .doc = "the sketch's State: a directory per struct and array, a file per value; a read is the value, a write what set does (a jump); an array of numbers is one file of its values, blank-separated" }, }); /// The verbs `ctl` takes beside the console's lines. @@ -148,6 +150,7 @@ 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 & @as(u64, @bitCast(state_files.root)) != 0) return stateRespond(host, req, reply, buffer); const file = std.enums.fromInt(File, req.node) orelse return error.UnknownFile; switch (req.op) { .lookup => { @@ -188,8 +191,8 @@ pub const Control = struct { 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; + std.mem.writeInt(u64, buffer[used..][0..8], @backingInt(f), .little); + buffer[used + 8] = @intFromBool(f == .state); buffer[used + 9] = @intCast(name.len); @memcpy(buffer[used + 10 ..][0..name.len], name); used += 10 + name.len; @@ -225,7 +228,7 @@ pub const Control = struct { } break :blk writer.buffered(); }, - .@"/" => return error.NotFile, + .@"/", .state => return error.NotFile, }; return window(bytes, req.off, req.size); }, @@ -305,7 +308,7 @@ pub const Control = struct { fn attr(self: *Control, node: u64) fs.Attr { 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 }; + return .{ .name = @tagName(file), .node = node, .dir = file == .@"/" or file == .state, .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 { @@ -329,6 +332,51 @@ pub const Control = struct { } }; +/// Under `state/`: the live State as files (state_files.zig), a write +/// run as the console's `set`. +fn stateRespond(host: *Host, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { + const layout = host.state.layout; + const node: state_files.Node = @bitCast(req.node); + if (!state_files.valid(layout, node)) return error.StateLayoutChangedWalkAgain; + const dir = state_files.isDir(layout, node); + const mode: u16 = if (dir) 0o700 else if (state_files.writable(layout, node)) 0o600 else 0o400; + switch (req.op) { + .lookup => { + if (!dir) return error.NotDirectory; + const child = state_files.lookup(layout, node, req.data) orelse return error.UnknownField; + const child_dir = state_files.isDir(layout, child); + reply.attr = .{ .node = @bitCast(child), .dir = child_dir, .mode = if (child_dir) 0o700 else if (state_files.writable(layout, child)) 0o600 else 0o400 }; + }, + .getattr => reply.attr = .{ .node = req.node, .dir = dir, .mode = mode }, + .setattr => { + if (mode != 0o600 or !req.truncate) return error.ReadOnly; + reply.attr = .{ .node = req.node, .mode = mode }; + }, + .open, .release => {}, + .readdir => { + if (!dir) return error.NotDirectory; + return buffer[0..state_files.readdir(layout, node, req.off, buffer)]; + }, + .read => { + if (dir) return error.NotFile; + var w: std.Io.Writer = .fixed(buffer); + state_files.read(layout, host.live(), node, &w) catch return error.ValueTooLong; + return window(w.buffered(), req.off, req.size); + }, + .write => { + if (mode != 0o600) return error.ReadOnly; + var w: std.Io.Writer = .fixed(buffer); + w.writeAll("set ") catch unreachable; + state_files.path(layout, node, &w) catch return error.ValueTooLong; + w.writeByte(' ') catch return error.ValueTooLong; + for (std.mem.trim(u8, req.data, " \t\r\n")) |byte| w.writeByte(if (byte == '\n') ' ' else byte) catch return error.ValueTooLong; + try host.runLine(w.buffered(), c.SDL_GetTicksNS()); + reply.written = @intCast(req.data.len); + }, + } + return &.{}; +} + fn window(bytes: []const u8, offset: u64, count: u32) []const u8 { if (offset >= bytes.len) return &.{}; const start: usize = @intCast(offset); |
