summaryrefslogtreecommitdiff
path: root/src/ninep/ctl.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/ctl.zig')
-rw-r--r--src/ninep/ctl.zig112
1 files changed, 112 insertions, 0 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());
+}