diff options
Diffstat (limited to 'src/ninep')
| -rw-r--r-- | src/ninep/ctl.zig | 112 | ||||
| -rw-r--r-- | src/ninep/tree.zig | 23 |
2 files changed, 128 insertions, 7 deletions
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); |
