diff options
| -rw-r--r-- | docs/typ/reference.typ | 17 | ||||
| -rw-r--r-- | docs/typ/scripting.typ | 5 | ||||
| -rw-r--r-- | src/CHANGELOG.md | 6 | ||||
| -rw-r--r-- | src/fs-help.txt | 2 | ||||
| -rw-r--r-- | src/ninep/ctl.zig | 112 | ||||
| -rw-r--r-- | src/ninep/tree.zig | 23 | ||||
| -rw-r--r-- | src/pardes.zig | 4 | ||||
| -rw-r--r-- | test/fs.py | 37 |
8 files changed, 197 insertions, 9 deletions
diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index e60964cc..75b33151 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -28,6 +28,8 @@ screen the rendered screen as JSON @on listeners dial addresses @other-files sessions the pardes daemon's sessions and shells (pardes --ps) @other-files focus the serial of the pane with the keyboard; write one to move it @columns-and-tags +kbdin write runes: keys typed at the pane with the keyboard @other-files +mousein write m col row buttons: the mouse as it now is @other-files ctl settings and session builtins @root-ctl commands every builtin, one a line @other-files recent files opened lately: open|closed <path> @other-files @@ -906,6 +908,21 @@ at 200, ending in `…`. Control characters become spaces. = Other files <other-files> +- #file("/kbdin") (write only), rio's: each rune written is a key typed at + the pane with the keyboard, done before the write is answered, so a + script that writes keys then reads a file sees what they did. Control + runes are Ctrl and a letter, but for Plan 9's keys + (`keyboard.h`): `\n` or `\r` #key("Enter"), `\t` #key("Tab"), `\b` + Backspace, `0x7f` Delete, `0x1b` #key("Esc"), and the + arrows, Home, End, Page Up and Down, Insert and F1-F12 at their + private-use runes (`U+F00E` up, `U+F800` down, `U+F011` left, `U+F012` + right, `U+F00D` Home, `U+F018` End). No Alt, Shift or Super. +- #file("/mousein") (write only), devmouse's, in cells: a line `m col row + buttons` is the mouse as it now is, the buttons Plan 9's bits (1 left, 2 + middle, 4 right; 8 and 16 a wheel notch up and down). What changed since + the last line is released, moved or pressed there, in that order: `m 10 + 5 4` then `m 10 5 0` is a right click (a #word("Look")) at column 10, + row 5. Cells are #file("/screen")'s. - #file("/screen"): JSON `cols`, `rows`, `cursor`, `styles`, and row-major `cells` of `[grapheme, style_index]`; one frame per open. Compare a cell's style through `styles`, not the index. diff --git a/docs/typ/scripting.typ b/docs/typ/scripting.typ index d90f68ac..1116063d 100644 --- a/docs/typ/scripting.typ +++ b/docs/typ/scripting.typ @@ -9,8 +9,9 @@ files is an extension: ``` $ ls $m -README col commands ctl exec focus index layout listeners log -look os pager pane recent screen sessions status tag tagexec +README col commands ctl exec focus index kbdin layout listeners +log look mousein os pager pane recent screen sessions status tag +tagexec $ cat $m/index 1 term 0 /home/me/src 1 2 text 0 /home/me/src/+New 1 diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 86cc8733..aa304597 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -14,6 +14,12 @@ build is refused in words, and an accepted connection is handed to its session, which so far only welcomes it and answers pings. `pardes --version` prints that build's semantics hash on a second line. +- `kbdin` and `mousein`, rio's and devmouse's, at the root of every + session's file tree: a script types keys (`printf 'ihello\033' > + $m/kbdin`) and clicks (`m col row buttons`, Plan 9's bits) as the user + does, each write done before it is answered, so the read that checks + it needs no sleep. test/fs.py's keys_and_mouse finds where to click on + /screen. - One `pardes --daemon` per user keeps sessions, started by the first session and gone once it keeps nothing. `pardes` in a terminal forks its session off and stays as its stub: `Detach` (`SPC s D`), or a diff --git a/src/fs-help.txt b/src/fs-help.txt index ee63dd0e..b62a13b8 100644 --- a/src/fs-help.txt +++ b/src/fs-help.txt @@ -11,6 +11,8 @@ screen the rendered screen as JSON, frozen from open to close listeners the session's dial addresses sessions the pardes daemon's, as pardes --ps: daemon <pid>; session <pid> tty|gui attached|detached <name>; shell <pid>; orphan <pid> focus the serial of the pane with the keyboard (empty on a column tag); write one to give it the keyboard +kbdin write runes: keys typed at the pane with the keyboard, done before the write answers (\x1b Esc, \n Enter) +mousein write `m col row buttons` (1 left, 2 middle, 4 right): the mouse as it now is; m 10 5 4, m 10 5 0 is a right click ctl settings a line; or Newcol, Dump, Restore [f], Exit (refuses once over unsaved panes), Kill [w], size C R commands every builtin: word, arg, root|pane|both (the ctl that takes it), -- what it does recent files opened lately, closed ones too: open|closed <path> diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 2859304a..37f27dac 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -992,6 +992,86 @@ pub fn writeFocus(p: *Pardes, req: Req) Reply { const e_no_pane = "no such pane"; +/// rio's kbdin (windows/rio/xfid.c:515): each rune written is a key typed +/// at the pane with the keyboard, the user's own, each done before the next +/// and all before the write is answered. Control runes are Ctrl and a +/// letter, but for Plan 9's keys (include/ALL/keyboard.h): \n or \r Enter, +/// \t Tab, \b Backspace, 0x7f Delete, 0x1b Esc, and the arrows, Home, End, +/// Page Up and Down, Insert and F1-F12 at its private-use runes. +pub fn writeKbdin(p: *Pardes, req: Req) Reply { + const view = std.unicode.Utf8View.init(req.data) catch return tree.failText(req.tag, E.INVAL, "invalid UTF-8"); + var it = view.iterator(); + while (it.nextCodepointSlice()) |rune| p.update(.{ .key = kbdKey(rune) }); + return .{ .tag = req.tag, .written = @intCast(req.data.len) }; +} + +fn kbdKey(rune: []const u8) pardes.Key { + const K = pardes.Key; + const c = std.unicode.utf8Decode(rune) catch unreachable; + const KF: u21 = 0xF000; + return switch (c) { + '\n', '\r' => .{ .cp = K.enter }, + '\t' => .{ .cp = K.tab }, + 0x08 => .{ .cp = K.backspace }, + 0x7f => .{ .cp = K.delete }, + 0x1b => .{ .cp = K.escape }, + 0x00 => .{ .cp = ' ', .ctrl = true }, + 0x01...0x07, 0x0b, 0x0c, 0x0e...0x1a => .{ .cp = 'a' + c - 1, .ctrl = true }, + KF | 0x0D => .{ .cp = K.home }, + KF | 0x0E => .{ .cp = K.up }, + 0xF800 => .{ .cp = K.down }, + KF | 0x0F => .{ .cp = K.page_up }, + KF | 0x11 => .{ .cp = K.left }, + KF | 0x12 => .{ .cp = K.right }, + KF | 0x13 => .{ .cp = K.page_down }, + KF | 0x14 => .{ .cp = K.insert }, + KF | 0x18 => .{ .cp = K.end }, + KF | 0x01...KF | 0x0C => .{ .cp = K.f1 + (c - (KF | 0x01)) }, + else => .{ .cp = c, .text = rune }, + }; +} + +/// devmouse's mousein (kernel/devices/mouse/devmouse.c:445), in cells: a +/// line `m col row buttons` is the mouse as it now is, Plan 9's bits (1 +/// left, 2 middle, 4 right; 8 and 16 a wheel notch up and down). What +/// changed since the last line is pressed or released there, in that order +/// of buttons; a move with one held drags, with none moves. +pub fn writeMousein(p: *Pardes, req: Req) Reply { + var lines = std.mem.tokenizeScalar(u8, req.data, '\n'); + while (lines.next()) |line| { + var words = std.mem.tokenizeAny(u8, line, " \t\r"); + const m = words.next() orelse continue; + if (!std.mem.eql(u8, m, "m")) return tree.failText(req.tag, E.INVAL, "invalid mousein: m col row buttons"); + var n: [3]u16 = undefined; + for (&n) |*v| v.* = std.fmt.parseInt(u16, words.next() orelse "", 10) catch + return tree.failText(req.tag, E.INVAL, "invalid mousein: m col row buttons"); + const col, const row, const b = n; + if (b > 31) return tree.failText(req.tag, E.INVAL, "invalid mousein: buttons are 1 2 4 8 16"); + const was = p.mousein_buttons; + const now: u8 = @intCast(b & 7); + const Button = pardes.Mouse.Button; + const buttons = [_]Button{ .left, .middle, .right }; + for (buttons, 0..) |button, i| { + const bit = @as(u8, 1) << @intCast(i); + if (was & bit != 0 and now & bit == 0) p.update(.{ .mouse = .{ .button = button, .kind = .release, .col = col, .row = row } }); + } + if (was == now and b & 24 == 0) { + const held = for (buttons, 0..) |button, i| { + if (now & (@as(u8, 1) << @intCast(i)) != 0) break button; + } else Button.none; + p.update(.{ .mouse = .{ .button = held, .kind = if (held == .none) .motion else .drag, .col = col, .row = row } }); + } + for (buttons, 0..) |button, i| { + const bit = @as(u8, 1) << @intCast(i); + if (was & bit == 0 and now & bit != 0) p.update(.{ .mouse = .{ .button = button, .kind = .press, .col = col, .row = row } }); + } + p.mousein_buttons = now; + if (b & 8 != 0) p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = col, .row = row } }); + if (b & 16 != 0) p.update(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = col, .row = row } }); + } + return .{ .tag = req.tag, .written = @intCast(req.data.len) }; +} + pub fn readPane(p: *Pardes, req: Req, pane: *Pane) Reply { var buf: [pane_text_cap]u8 = undefined; const out = p.fs.stage(p.gpa); @@ -5100,3 +5180,35 @@ test "the init file saved in a pane applies its settings again, and runs none of try testing.expectEqualStrings("acme", p.theme().name); try testing.expectEqual(cols, p.ncol); } + +test "kbdin types at the pane with the keyboard and mousein clicks, as a user would, each done before its write is answered" { + const p = try withFile(testing.allocator, "x\n"); + defer p.deinit(); + const s = serialOf(p); + const kbdin = @backingInt(tree.TopFile.kbdin); + const mousein = @backingInt(tree.TopFile.mousein); + const body = Node.of(s, .body); + try testing.expectEqual(@as(u16, 0), wr(p, kbdin, "ihello \x1b").errno()); + try testing.expectEqualStrings("hello x\n", rd(p, body, 0, 64).bytes); + // Plan 9's Khome rune, then text before it. + try testing.expectEqual(@as(u16, 0), wr(p, kbdin, "A!\x1b\u{F00D}i>\x1b").errno()); + try testing.expectEqualStrings(">hello x!\n", rd(p, body, 0, 64).bytes); + // A left click on the second l, then text there; where it is, as a + // client reads it off /screen, from a frame drawn. + var frame: std.heap.ArenaAllocator = .init(p.gpa); + defer frame.deinit(); + _ = try p.render(frame.allocator()); + const id = p.paneBySerial(s).?; + const pane = p.panes[id].?; + const r = p.rects[id]; + var line: [64]u8 = undefined; + const at = .{ r.x + config.GUTTER + panes.File.gutterWidth(pane) + 4, p.bodyTop(pane, r) }; + try testing.expectEqual(@as(u16, 0), wr(p, mousein, try std.fmt.bufPrint(&line, "m {d} {d} 1\nm {d} {d} 0\n", at ++ at)).errno()); + try testing.expectEqual(@as(u16, 0), wr(p, kbdin, "i#\x1b").errno()); + try testing.expectEqualStrings(">hel#lo x!\n", rd(p, body, 0, 64).bytes); + // Refused, in words, as rio and devmouse refuse. + try testing.expectEqual(E.INVAL, wr(p, mousein, "m 1 2\n").errno()); + try testing.expectEqual(E.INVAL, wr(p, mousein, "x 1 2 0\n").errno()); + try testing.expectEqual(E.INVAL, wr(p, kbdin, "\xff").errno()); + try testing.expectEqual(E.PERM, rd(p, kbdin, 0, 64).errno()); +} diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index c3eb3636..6bf14243 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -266,6 +266,11 @@ pub const TopFile = enum(u5) { /// them: `daemon <pid>`, `session <pid> tty|gui attached|detached /// <name>`, `shell <pid>` (the session above's), `orphan <pid>`. sessions, + /// rio's kbdin (windows/rio/xfid.c:515): each rune written is a key + /// typed at the pane with the keyboard, done before the write answers. + kbdin, + /// devmouse's mousein: `m col row buttons`, the mouse as it now is. + mousein, pub fn fileName(f: TopFile) []const u8 { return if (f == .root) "/" else @tagName(f); @@ -276,6 +281,7 @@ pub const TopFile = enum(u5) { .root, .pane, .col => 0o755, .look, .exec, .log, .focus, .ctl, .tag, .tagexec, .pager => 0o666, .README, .index, .status, .screen, .listeners, .new, .commands, .layout, .recent, .sessions => 0o444, + .kbdin, .mousein => 0o222, }; } @@ -681,7 +687,7 @@ fn topSize(p: *Pardes, f: TopFile) u64 { // frame, /pane/new until an open has a pane to name, and the index, // layout, recent list and listeners, which a stat would otherwise // have to build to count. - .root, .pane, .col, .screen, .new, .layout, .index, .recent, .listeners, .sessions => 0, + .root, .pane, .col, .screen, .new, .layout, .index, .recent, .listeners, .sessions, .kbdin, .mousein => 0, .tag => cols.headerText(p, null).?.len, .README => fs.help.len, .status => ctl.statusLen(p), @@ -789,7 +795,7 @@ fn readdir(p: *Pardes, req: Req, target: Target) Reply { .root => { // The guide, then what describes the session, then the // files that act, then the directories. - inline for (.{ TopFile.README, TopFile.index, TopFile.status, TopFile.focus, TopFile.ctl, TopFile.commands, TopFile.recent, TopFile.look, TopFile.exec, TopFile.pager, TopFile.log, TopFile.screen, TopFile.listeners, TopFile.sessions, TopFile.layout, TopFile.tag, TopFile.tagexec, TopFile.pane, TopFile.col }) |t| { + inline for (.{ TopFile.README, TopFile.index, TopFile.status, TopFile.focus, TopFile.ctl, TopFile.commands, TopFile.recent, TopFile.look, TopFile.exec, TopFile.pager, TopFile.log, TopFile.screen, TopFile.listeners, TopFile.sessions, TopFile.layout, TopFile.tag, TopFile.tagexec, TopFile.kbdin, TopFile.mousein, TopFile.pane, TopFile.col }) |t| { if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, @backingInt(t), t.dir(), t.fileName()); } if (skip > 0) skip -= 1 else stageDirent(out, p.gpa, fs.os_root, true, "os"); @@ -1092,6 +1098,7 @@ fn read(p: *Pardes, req: Req, target: Target) Reply { fn readFile(p: *Pardes, req: Req, target: Target) Reply { switch (target) { .top => |f| return switch (f) { + .kbdin, .mousein => Reply.fail(req.tag, E.PERM), .README => help: { const off = @min(req.off, fs.help.len); const bytes = fs.help[off..][0..@min(req.size, fs.help.len - off)]; @@ -1360,6 +1367,8 @@ fn writeNow(p: *Pardes, req: Req, target: Target) Reply { .look, .exec => ctl.command(p, req, null, f == .exec, false), .log => events.writeLog(p, req), .focus => ctl.writeFocus(p, req), + .kbdin => ctl.writeKbdin(p, req), + .mousein => ctl.writeMousein(p, req), .ctl => ctl.writeRoot(p, req), .pager => ctl.writePager(p, req), .tag => cols.write(p, req, null), @@ -1557,13 +1566,13 @@ test "readdir lists the root and a pane directory without creating anything" { @memcpy(top_bytes[0..top_reply.bytes.len], top_reply.bytes); const top = th.dirents(top_bytes[0..top_reply.bytes.len], &buf); try testing.expect(top.len >= 10); - for ([_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "pane", "col", "os" }, 0..) |name, i| { + for ([_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" }, 0..) |name, i| { try testing.expectEqualStrings(name, top[i].name); - try testing.expectEqual(i >= 17, top[i].dir); + try testing.expectEqual(i >= 19, top[i].dir); } - try testing.expectEqual(@backingInt(TopFile.pane), top[17].node); - try testing.expectEqual(@backingInt(TopFile.col), top[18].node); - try testing.expectEqual(fs.os_root, top[19].node); + try testing.expectEqual(@backingInt(TopFile.pane), top[19].node); + try testing.expectEqual(@backingInt(TopFile.col), top[20].node); + try testing.expectEqual(fs.os_root, top[21].node); var idbuf: [16]u8 = undefined; try testing.expect(th.nameAt(top, try std.fmt.bufPrint(&idbuf, "{d}", .{serial})) == null); try testing.expect(th.nameAt(top, "self") == null); diff --git a/src/pardes.zig b/src/pardes.zig index edab4a58..fb3dc36f 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -4464,6 +4464,10 @@ pub const Pardes = struct { /// Zerox's groups of panes on one buffer (File.syncTwins), and the id /// the next takes. twin_groups: std.ArrayList(panes.File.TwinGroup) = .empty, + /// The buttons held as the 9P mousein file last said (Plan 9's bits: + /// 1 left, 2 middle, 4 right): its next line presses and releases + /// what changed. + mousein_buttons: u8 = 0, next_twin: u32 = 1, jumps: [MAX_JUMPS]Loc = undefined, njumps: usize = 0, @@ -1095,6 +1095,42 @@ def cut_writes(binary): assert (screen['cols'], screen['rows']) == (132, 40), (screen['cols'], screen['rows']) +def find_on_screen(client, word): + """Where `word` is drawn, as (col, row), off /screen: what a mousein + line clicks.""" + screen = client.screen() + cols = screen['cols'] + for row in range(screen['rows']): + line = ''.join(cell[0] or ' ' for cell in screen['cells'][row * cols:(row + 1) * cols]) + at = line.find(word) + if at >= 0: + return at, row + raise AssertionError((word, 'not on the screen')) + + +def keys_and_mouse(binary): + """kbdin and mousein: a script types and clicks as the user does, each + write done before it is answered, so no sleep or poll between a write + and the read that checks it; where to click is read off /screen.""" + with tempfile.TemporaryDirectory(prefix='pardes-kbd-') as directory: + root = Path(directory) + (root / 'target.txt').write_bytes(b'hello-target\n') + with session(binary, root, 'kbd') as (client, _): + serial = new_pane(client, b'see \n') + client.write('/focus', f'{serial}\n'.encode()) + client.write('/kbdin', 'Atarget.txt\x1b'.encode()) + assert client.read(f'/pane/{serial}/body') == b'see target.txt\n' + col, row = find_on_screen(client, 'target.txt') + client.write('/mousein', f'm {col + 2} {row} 4\nm {col + 2} {row} 0\n'.encode()) + opened = newest(client) + assert opened != serial and client.read('/index').splitlines()[-1].endswith(b'target.txt 1'), client.read('/index') + assert client.read(f'/pane/{opened}/body') == b'hello-target\n' + # Undone from the keyboard, as the user would: u in normal mode. + client.write('/focus', f'{serial}\n'.encode()) + client.write('/kbdin', b'u') + assert client.read(f'/pane/{serial}/body') == b'see \n' + + def edit_commands(binary): """Edit's `< | >` run off the loop and answer the write that ran them once applied; a failing one changes nothing and fails the write in its @@ -1207,6 +1243,7 @@ def edit_commands(binary): def test(binary, quic=False): started = time.monotonic() + keys_and_mouse(binary) cut_writes(binary) edit_commands(binary) odd_paths(binary) |
