2024-01-18 04:52:33 +01:00
|
|
|
const std = @import("std");
|
2024-01-19 05:37:48 +01:00
|
|
|
const builtin = @import("builtin");
|
2024-03-22 01:29:26 +01:00
|
|
|
const posix = std.posix;
|
2024-04-29 19:26:50 +02:00
|
|
|
const Loop = @import("Loop.zig").Loop;
|
2024-01-23 20:25:31 +01:00
|
|
|
const Parser = @import("Parser.zig");
|
2024-01-22 17:40:30 +01:00
|
|
|
const GraphemeCache = @import("GraphemeCache.zig");
|
2024-03-19 19:26:08 +01:00
|
|
|
const ctlseqs = @import("ctlseqs.zig");
|
2024-04-29 21:00:08 +02:00
|
|
|
const grapheme = @import("grapheme");
|
2024-01-18 04:52:33 +01:00
|
|
|
|
|
|
|
const log = std.log.scoped(.tty);
|
|
|
|
|
|
|
|
const Tty = @This();
|
|
|
|
|
2024-03-22 01:29:26 +01:00
|
|
|
const Writer = std.io.Writer(posix.fd_t, posix.WriteError, posix.write);
|
2024-01-20 04:07:16 +01:00
|
|
|
|
|
|
|
const BufferedWriter = std.io.BufferedWriter(4096, Writer);
|
|
|
|
|
2024-01-18 04:52:33 +01:00
|
|
|
/// the original state of the terminal, prior to calling makeRaw
|
2024-03-22 01:29:26 +01:00
|
|
|
termios: posix.termios,
|
2024-01-18 04:52:33 +01:00
|
|
|
|
|
|
|
/// The file descriptor we are using for I/O
|
2024-03-22 01:29:26 +01:00
|
|
|
fd: posix.fd_t,
|
2024-01-18 04:52:33 +01:00
|
|
|
|
2024-03-19 19:26:08 +01:00
|
|
|
should_quit: bool = false,
|
2024-01-18 23:16:21 +01:00
|
|
|
|
2024-01-20 04:07:16 +01:00
|
|
|
buffered_writer: BufferedWriter,
|
|
|
|
|
2024-04-30 23:42:10 +02:00
|
|
|
state: struct {
|
|
|
|
/// if we are in the alt screen
|
|
|
|
alt_screen: bool = false,
|
|
|
|
/// if we have entered kitty keyboard
|
|
|
|
kitty_keyboard: bool = false,
|
|
|
|
bracketed_paste: bool = false,
|
|
|
|
mouse: bool = false,
|
|
|
|
} = .{},
|
|
|
|
|
2024-01-18 04:52:33 +01:00
|
|
|
/// initializes a Tty instance by opening /dev/tty and "making it raw"
|
|
|
|
pub fn init() !Tty {
|
|
|
|
// Open our tty
|
2024-03-22 01:29:26 +01:00
|
|
|
const fd = try posix.open("/dev/tty", .{ .ACCMODE = .RDWR }, 0);
|
2024-01-18 04:52:33 +01:00
|
|
|
|
|
|
|
// Set the termios of the tty
|
|
|
|
const termios = try makeRaw(fd);
|
|
|
|
|
|
|
|
return Tty{
|
|
|
|
.fd = fd,
|
|
|
|
.termios = termios,
|
2024-01-20 04:07:16 +01:00
|
|
|
.buffered_writer = std.io.bufferedWriter(Writer{ .context = fd }),
|
2024-01-18 04:52:33 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
fix: use `select` on macOS
the man page for macOS `poll(2)` says, under "bugs", that polling
devices does not work, and indeed, although the current implementation
based on poll(2) does produce output, it does not appear to actually
be polling for this reason; instead the wait is blocking on the `read`
call, causing the macOS user to need to input another character to
exit.
this change introduces a wrapper over macOS's implementation of
`select`, modeled after `std.io.poll` as `select.zig`. it is a compile
error to use `select.zig` when `builtin.os.tag.isDarwin()` is false. a
lightly altered version of `Tty.zig` accompanies this change. it's
certainly possible to incorporate the two into one file; i didn't just
to leave the other one in its original state.
with this change, writing to the pipe correctly exits.
additionally, on macOS, `Thread.setName` must be called from the
thread whose name you wish to set, so I have just commented out that
line.
2024-03-09 06:23:25 +01:00
|
|
|
/// release resources associated with the Tty return it to its original state
|
2024-01-18 04:52:33 +01:00
|
|
|
pub fn deinit(self: *Tty) void {
|
2024-04-30 23:42:10 +02:00
|
|
|
if (self.state.kitty_keyboard) {
|
|
|
|
_ = self.write(ctlseqs.csi_u_pop) catch {};
|
|
|
|
}
|
|
|
|
if (self.state.mouse) {
|
|
|
|
_ = self.write(ctlseqs.mouse_reset) catch {};
|
|
|
|
}
|
|
|
|
if (self.state.bracketed_paste) {
|
|
|
|
_ = self.write(ctlseqs.bp_reset) catch {};
|
|
|
|
}
|
|
|
|
if (self.state.alt_screen) {
|
|
|
|
_ = self.write(ctlseqs.rmcup) catch {};
|
|
|
|
}
|
|
|
|
// always show the cursor on exit
|
|
|
|
_ = self.write(ctlseqs.show_cursor) catch {};
|
|
|
|
self.flush() catch {};
|
2024-03-22 01:29:26 +01:00
|
|
|
posix.tcsetattr(self.fd, .FLUSH, self.termios) catch |err| {
|
2024-01-18 04:52:33 +01:00
|
|
|
log.err("couldn't restore terminal: {}", .{err});
|
|
|
|
};
|
2024-03-22 01:29:26 +01:00
|
|
|
posix.close(self.fd);
|
2024-01-18 04:52:33 +01:00
|
|
|
}
|
|
|
|
|
2024-01-18 23:16:21 +01:00
|
|
|
/// stops the run loop
|
|
|
|
pub fn stop(self: *Tty) void {
|
2024-03-19 19:26:08 +01:00
|
|
|
self.should_quit = true;
|
2024-03-22 01:29:26 +01:00
|
|
|
_ = posix.write(self.fd, ctlseqs.device_status_report) catch {};
|
2024-01-18 23:16:21 +01:00
|
|
|
}
|
|
|
|
|
2024-01-18 04:52:33 +01:00
|
|
|
/// read input from the tty
|
2024-01-19 02:02:59 +01:00
|
|
|
pub fn run(
|
|
|
|
self: *Tty,
|
2024-02-11 19:59:33 +01:00
|
|
|
comptime Event: type,
|
2024-04-29 19:26:50 +02:00
|
|
|
loop: *Loop(Event),
|
2024-04-29 21:00:08 +02:00
|
|
|
grapheme_data: *const grapheme.GraphemeData,
|
2024-01-19 02:02:59 +01:00
|
|
|
) !void {
|
2024-01-18 23:16:21 +01:00
|
|
|
|
2024-01-19 05:37:48 +01:00
|
|
|
// get our initial winsize
|
|
|
|
const winsize = try getWinsize(self.fd);
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "winsize")) {
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.postEvent(.{ .winsize = winsize });
|
2024-01-19 19:24:02 +01:00
|
|
|
}
|
2024-01-19 05:37:48 +01:00
|
|
|
|
|
|
|
// Build a winch handler. We need build this struct to get an anonymous
|
|
|
|
// function which can post the winsize event
|
|
|
|
// TODO: more signals, move this outside of this function?
|
|
|
|
const WinchHandler = struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
2024-04-29 19:26:50 +02:00
|
|
|
var vx_winch: *Loop(Event) = undefined;
|
2024-03-22 01:29:26 +01:00
|
|
|
var fd: posix.fd_t = undefined;
|
2024-01-19 05:37:48 +01:00
|
|
|
|
2024-04-29 19:26:50 +02:00
|
|
|
fn init(vx_arg: *Loop(Event), fd_arg: posix.fd_t) !void {
|
2024-01-19 17:13:32 +01:00
|
|
|
vx_winch = vx_arg;
|
2024-01-19 05:37:48 +01:00
|
|
|
fd = fd_arg;
|
2024-03-22 01:29:26 +01:00
|
|
|
var act = posix.Sigaction{
|
2024-01-19 05:37:48 +01:00
|
|
|
.handler = .{ .handler = Self.handleWinch },
|
|
|
|
.mask = switch (builtin.os.tag) {
|
|
|
|
.macos => 0,
|
2024-03-22 01:29:26 +01:00
|
|
|
.linux => posix.empty_sigset,
|
2024-01-19 05:37:48 +01:00
|
|
|
else => @compileError("os not supported"),
|
|
|
|
},
|
|
|
|
.flags = 0,
|
|
|
|
};
|
|
|
|
|
2024-03-22 01:29:26 +01:00
|
|
|
try posix.sigaction(posix.SIG.WINCH, &act, null);
|
2024-01-19 05:37:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handleWinch(_: c_int) callconv(.C) void {
|
|
|
|
const ws = getWinsize(fd) catch {
|
|
|
|
return;
|
|
|
|
};
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "winsize")) {
|
2024-01-19 19:24:02 +01:00
|
|
|
vx_winch.postEvent(.{ .winsize = ws });
|
|
|
|
}
|
2024-01-19 05:37:48 +01:00
|
|
|
}
|
|
|
|
};
|
2024-04-29 19:26:50 +02:00
|
|
|
try WinchHandler.init(loop, self.fd);
|
2024-01-19 05:37:48 +01:00
|
|
|
|
2024-01-22 17:40:30 +01:00
|
|
|
// initialize a grapheme cache
|
|
|
|
var cache: GraphemeCache = .{};
|
|
|
|
|
2024-04-29 21:00:08 +02:00
|
|
|
var parser: Parser = .{
|
|
|
|
.grapheme_data = grapheme_data,
|
|
|
|
};
|
2024-01-23 20:25:31 +01:00
|
|
|
|
2024-01-19 02:02:59 +01:00
|
|
|
// initialize the read buffer
|
|
|
|
var buf: [1024]u8 = undefined;
|
2024-01-21 18:53:25 +01:00
|
|
|
// read loop
|
2024-03-19 19:26:08 +01:00
|
|
|
while (!self.should_quit) {
|
2024-03-22 01:29:26 +01:00
|
|
|
const n = try posix.read(self.fd, &buf);
|
2024-01-20 15:05:22 +01:00
|
|
|
var start: usize = 0;
|
2024-01-21 19:47:34 +01:00
|
|
|
while (start < n) {
|
|
|
|
const result = try parser.parse(buf[start..n]);
|
2024-01-22 17:40:30 +01:00
|
|
|
start += result.n;
|
2024-01-21 20:16:52 +01:00
|
|
|
// TODO: if we get 0 byte read, copy the remaining bytes to the
|
2024-01-21 20:19:18 +01:00
|
|
|
// beginning of the buffer and read mmore? this should only happen
|
|
|
|
// if we are in the middle of a grapheme at and filled our
|
|
|
|
// buffer. Probably can happen on large pastes so needs to be
|
|
|
|
// implemented but low priority
|
2024-01-21 20:16:52 +01:00
|
|
|
|
2024-01-21 19:47:34 +01:00
|
|
|
const event = result.event orelse continue;
|
|
|
|
switch (event) {
|
|
|
|
.key_press => |key| {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "key_press")) {
|
2024-01-22 17:40:30 +01:00
|
|
|
// HACK: yuck. there has to be a better way
|
|
|
|
var mut_key = key;
|
|
|
|
if (key.text) |text| {
|
|
|
|
mut_key.text = cache.put(text);
|
|
|
|
}
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.postEvent(.{ .key_press = mut_key });
|
2024-01-19 03:17:26 +01:00
|
|
|
}
|
2024-01-31 19:50:00 +01:00
|
|
|
},
|
2024-04-30 15:55:26 +02:00
|
|
|
.key_release => |*key| {
|
|
|
|
if (@hasField(Event, "key_release")) {
|
|
|
|
// HACK: yuck. there has to be a better way
|
|
|
|
var mut_key = key;
|
|
|
|
if (key.text) |text| {
|
|
|
|
mut_key.text = cache.put(text);
|
|
|
|
}
|
|
|
|
loop.postEvent(.{ .key_release = mut_key });
|
|
|
|
}
|
|
|
|
},
|
2024-01-31 19:50:00 +01:00
|
|
|
.mouse => |mouse| {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "mouse")) {
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.postEvent(.{ .mouse = mouse });
|
2024-01-31 19:50:00 +01:00
|
|
|
}
|
2024-01-19 02:02:59 +01:00
|
|
|
},
|
2024-01-21 20:19:18 +01:00
|
|
|
.focus_in => {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "focus_in")) {
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.postEvent(.focus_in);
|
2024-01-21 20:19:18 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.focus_out => {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "focus_out")) {
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.postEvent(.focus_out);
|
2024-01-21 20:19:18 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.paste_start => {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "paste_start")) {
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.postEvent(.paste_start);
|
2024-01-21 20:19:18 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.paste_end => {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "paste_end")) {
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.postEvent(.paste_end);
|
2024-01-21 20:19:18 +01:00
|
|
|
}
|
|
|
|
},
|
2024-01-23 15:10:59 +01:00
|
|
|
.cap_kitty_keyboard => {
|
2024-01-31 13:59:34 +01:00
|
|
|
log.info("kitty keyboard capability detected", .{});
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.vaxis.caps.kitty_keyboard = true;
|
2024-01-23 15:10:59 +01:00
|
|
|
},
|
2024-01-31 13:59:34 +01:00
|
|
|
.cap_kitty_graphics => {
|
2024-04-29 19:26:50 +02:00
|
|
|
if (!loop.vaxis.caps.kitty_graphics) {
|
2024-01-31 13:59:34 +01:00
|
|
|
log.info("kitty graphics capability detected", .{});
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.vaxis.caps.kitty_graphics = true;
|
2024-01-31 13:59:34 +01:00
|
|
|
}
|
|
|
|
},
|
2024-01-23 20:25:31 +01:00
|
|
|
.cap_rgb => {
|
2024-01-24 13:12:39 +01:00
|
|
|
log.info("rgb capability detected", .{});
|
2024-04-29 19:26:50 +02:00
|
|
|
loop.vaxis.caps.rgb = true;
|
2024-01-23 20:25:31 +01:00
|
|
|
},
|
2024-01-24 04:06:02 +01:00
|
|
|
.cap_unicode => {
|
2024-01-24 13:12:39 +01:00
|
|
|
log.info("unicode capability detected", .{});
|
2024-04-29 20:01:04 +02:00
|
|
|
loop.vaxis.caps.unicode = .unicode;
|
|
|
|
loop.vaxis.screen.width_method = .unicode;
|
2024-01-24 04:30:09 +01:00
|
|
|
},
|
|
|
|
.cap_da1 => {
|
2024-04-29 19:26:50 +02:00
|
|
|
std.Thread.Futex.wake(&loop.vaxis.query_futex, 10);
|
2024-01-24 04:06:02 +01:00
|
|
|
},
|
2024-01-19 02:02:59 +01:00
|
|
|
}
|
|
|
|
}
|
2024-01-18 05:34:40 +01:00
|
|
|
}
|
2024-01-18 04:52:33 +01:00
|
|
|
}
|
|
|
|
|
2024-01-20 04:07:16 +01:00
|
|
|
/// write to the tty. These writes are buffered and require calling flush to
|
|
|
|
/// flush writes to the tty
|
2024-01-19 17:58:14 +01:00
|
|
|
pub fn write(self: *Tty, bytes: []const u8) !usize {
|
2024-01-20 04:07:16 +01:00
|
|
|
return self.buffered_writer.write(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// flushes the write buffer to the tty
|
|
|
|
pub fn flush(self: *Tty) !void {
|
|
|
|
try self.buffered_writer.flush();
|
2024-01-19 17:58:14 +01:00
|
|
|
}
|
|
|
|
|
2024-01-18 04:52:33 +01:00
|
|
|
/// makeRaw enters the raw state for the terminal.
|
2024-03-22 01:29:26 +01:00
|
|
|
pub fn makeRaw(fd: posix.fd_t) !posix.termios {
|
|
|
|
const state = try posix.tcgetattr(fd);
|
2024-01-18 04:52:33 +01:00
|
|
|
var raw = state;
|
|
|
|
// see termios(3)
|
2024-02-18 19:20:47 +01:00
|
|
|
raw.iflag.IGNBRK = false;
|
|
|
|
raw.iflag.BRKINT = false;
|
2024-02-27 01:26:12 +01:00
|
|
|
raw.iflag.PARMRK = false;
|
2024-02-18 19:20:47 +01:00
|
|
|
raw.iflag.ISTRIP = false;
|
|
|
|
raw.iflag.INLCR = false;
|
|
|
|
raw.iflag.IGNCR = false;
|
|
|
|
raw.iflag.ICRNL = false;
|
|
|
|
raw.iflag.IXON = false;
|
|
|
|
|
|
|
|
raw.oflag.OPOST = false;
|
|
|
|
|
|
|
|
raw.lflag.ECHO = false;
|
|
|
|
raw.lflag.ECHONL = false;
|
|
|
|
raw.lflag.ICANON = false;
|
2024-02-27 01:26:12 +01:00
|
|
|
raw.lflag.ISIG = false;
|
2024-02-18 19:20:47 +01:00
|
|
|
raw.lflag.IEXTEN = false;
|
|
|
|
|
|
|
|
raw.cflag.CSIZE = .CS8;
|
|
|
|
raw.cflag.PARENB = false;
|
|
|
|
|
2024-03-22 01:29:26 +01:00
|
|
|
raw.cc[@intFromEnum(posix.V.MIN)] = 1;
|
|
|
|
raw.cc[@intFromEnum(posix.V.TIME)] = 0;
|
|
|
|
try posix.tcsetattr(fd, .FLUSH, raw);
|
2024-01-18 04:52:33 +01:00
|
|
|
return state;
|
|
|
|
}
|
2024-01-19 05:37:48 +01:00
|
|
|
|
2024-01-19 17:58:14 +01:00
|
|
|
/// The size of the terminal screen
|
2024-01-19 06:17:57 +01:00
|
|
|
pub const Winsize = struct {
|
|
|
|
rows: usize,
|
|
|
|
cols: usize,
|
|
|
|
x_pixel: usize,
|
|
|
|
y_pixel: usize,
|
|
|
|
};
|
|
|
|
|
2024-03-22 01:29:26 +01:00
|
|
|
fn getWinsize(fd: posix.fd_t) !Winsize {
|
|
|
|
var winsize = posix.winsize{
|
2024-01-19 05:37:48 +01:00
|
|
|
.ws_row = 0,
|
|
|
|
.ws_col = 0,
|
|
|
|
.ws_xpixel = 0,
|
|
|
|
.ws_ypixel = 0,
|
|
|
|
};
|
|
|
|
|
2024-03-22 01:29:26 +01:00
|
|
|
const err = posix.system.ioctl(fd, posix.T.IOCGWINSZ, @intFromPtr(&winsize));
|
|
|
|
if (posix.errno(err) == .SUCCESS)
|
2024-01-19 06:17:57 +01:00
|
|
|
return Winsize{
|
|
|
|
.rows = winsize.ws_row,
|
|
|
|
.cols = winsize.ws_col,
|
|
|
|
.x_pixel = winsize.ws_xpixel,
|
|
|
|
.y_pixel = winsize.ws_ypixel,
|
|
|
|
};
|
2024-01-19 05:37:48 +01:00
|
|
|
return error.IoctlError;
|
|
|
|
}
|