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-01-18 04:52:33 +01:00
|
|
|
const os = std.os;
|
2024-02-11 19:59:33 +01:00
|
|
|
const Vaxis = @import("vaxis.zig").Vaxis;
|
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-01-18 04:52:33 +01:00
|
|
|
|
|
|
|
const log = std.log.scoped(.tty);
|
|
|
|
|
|
|
|
const Tty = @This();
|
|
|
|
|
2024-01-20 04:07:16 +01:00
|
|
|
const Writer = std.io.Writer(os.fd_t, os.WriteError, os.write);
|
|
|
|
|
|
|
|
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
|
|
|
|
termios: os.termios,
|
|
|
|
|
|
|
|
/// The file descriptor we are using for I/O
|
|
|
|
fd: os.fd_t,
|
|
|
|
|
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-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-02-18 19:20:47 +01:00
|
|
|
const fd = try os.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 {
|
|
|
|
os.tcsetattr(self.fd, .FLUSH, self.termios) catch |err| {
|
|
|
|
log.err("couldn't restore terminal: {}", .{err});
|
|
|
|
};
|
|
|
|
os.close(self.fd);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
_ = std.os.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,
|
|
|
|
vx: *Vaxis(Event),
|
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-01-19 19:24:02 +01:00
|
|
|
vx.postEvent(.{ .winsize = winsize });
|
|
|
|
}
|
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-02-11 19:59:33 +01:00
|
|
|
var vx_winch: *Vaxis(Event) = undefined;
|
2024-01-19 05:37:48 +01:00
|
|
|
var fd: os.fd_t = undefined;
|
|
|
|
|
2024-02-11 19:59:33 +01:00
|
|
|
fn init(vx_arg: *Vaxis(Event), fd_arg: os.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;
|
|
|
|
var act = os.Sigaction{
|
|
|
|
.handler = .{ .handler = Self.handleWinch },
|
|
|
|
.mask = switch (builtin.os.tag) {
|
|
|
|
.macos => 0,
|
|
|
|
.linux => std.os.empty_sigset,
|
|
|
|
else => @compileError("os not supported"),
|
|
|
|
},
|
|
|
|
.flags = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
try os.sigaction(os.SIG.WINCH, &act, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-19 17:13:32 +01:00
|
|
|
try WinchHandler.init(vx, 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-01-23 20:25:31 +01:00
|
|
|
var parser: Parser = .{};
|
|
|
|
|
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-01-18 05:34:40 +01:00
|
|
|
const n = try os.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);
|
|
|
|
}
|
|
|
|
vx.postEvent(.{ .key_press = mut_key });
|
2024-01-19 03:17:26 +01:00
|
|
|
}
|
2024-01-31 19:50:00 +01:00
|
|
|
},
|
|
|
|
.mouse => |mouse| {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "mouse")) {
|
2024-01-31 19:50:00 +01:00
|
|
|
vx.postEvent(.{ .mouse = mouse });
|
|
|
|
}
|
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-01-21 20:19:18 +01:00
|
|
|
vx.postEvent(.focus_in);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.focus_out => {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "focus_out")) {
|
2024-01-21 20:19:18 +01:00
|
|
|
vx.postEvent(.focus_out);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.paste_start => {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "paste_start")) {
|
2024-01-21 20:19:18 +01:00
|
|
|
vx.postEvent(.paste_start);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.paste_end => {
|
2024-02-11 19:59:33 +01:00
|
|
|
if (@hasField(Event, "paste_end")) {
|
2024-01-21 20:19:18 +01:00
|
|
|
vx.postEvent(.paste_end);
|
|
|
|
}
|
|
|
|
},
|
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-01-24 04:30:09 +01:00
|
|
|
vx.caps.kitty_keyboard = true;
|
2024-01-23 15:10:59 +01:00
|
|
|
},
|
2024-01-31 13:59:34 +01:00
|
|
|
.cap_kitty_graphics => {
|
|
|
|
if (!vx.caps.kitty_graphics) {
|
|
|
|
log.info("kitty graphics capability detected", .{});
|
|
|
|
vx.caps.kitty_graphics = true;
|
|
|
|
}
|
|
|
|
},
|
2024-01-23 20:25:31 +01:00
|
|
|
.cap_rgb => {
|
2024-01-24 13:12:39 +01:00
|
|
|
log.info("rgb capability detected", .{});
|
2024-01-24 04:30:09 +01:00
|
|
|
vx.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-01-24 04:30:09 +01:00
|
|
|
vx.caps.unicode = true;
|
2024-01-24 13:12:39 +01:00
|
|
|
vx.screen.unicode = true;
|
2024-01-24 04:30:09 +01:00
|
|
|
},
|
|
|
|
.cap_da1 => {
|
|
|
|
std.Thread.Futex.wake(&vx.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.
|
|
|
|
pub fn makeRaw(fd: os.fd_t) !os.termios {
|
|
|
|
const state = try os.tcgetattr(fd);
|
|
|
|
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;
|
|
|
|
|
|
|
|
raw.cc[@intFromEnum(std.posix.V.MIN)] = 1;
|
|
|
|
raw.cc[@intFromEnum(std.posix.V.TIME)] = 0;
|
2024-01-18 04:52:33 +01:00
|
|
|
try os.tcsetattr(fd, .FLUSH, raw);
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn getWinsize(fd: os.fd_t) !Winsize {
|
2024-02-18 19:20:47 +01:00
|
|
|
var winsize = os.winsize{
|
2024-01-19 05:37:48 +01:00
|
|
|
.ws_row = 0,
|
|
|
|
.ws_col = 0,
|
|
|
|
.ws_xpixel = 0,
|
|
|
|
.ws_ypixel = 0,
|
|
|
|
};
|
|
|
|
|
2024-02-18 19:20:47 +01:00
|
|
|
const err = os.system.ioctl(fd, os.T.IOCGWINSZ, @intFromPtr(&winsize));
|
2024-01-19 05:37:48 +01:00
|
|
|
if (os.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;
|
|
|
|
}
|