diff options
Diffstat (limited to 'src/RawMode.zig')
| -rw-r--r-- | src/RawMode.zig | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/RawMode.zig b/src/RawMode.zig new file mode 100644 index 0000000..ed71819 --- /dev/null +++ b/src/RawMode.zig | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | const linux = std.os.linux; | ||
| 2 | const std = @import("std"); | ||
| 3 | |||
| 4 | const STDIN_FILENO = std.os.STDIN_FILENO; | ||
| 5 | const RawMode = @This(); | ||
| 6 | const tcflag_t = linux.tcflag_t; | ||
| 7 | const tcgetattr = std.os.tcgetattr; | ||
| 8 | const tcsetattr = std.os.tcsetattr; | ||
| 9 | const termios = std.os.termios; | ||
| 10 | |||
| 11 | orig: termios, | ||
| 12 | |||
| 13 | pub fn init() !RawMode { | ||
| 14 | const orig = try tcgetattr(STDIN_FILENO); | ||
| 15 | const self = RawMode{ .orig = orig }; | ||
| 16 | errdefer self.deinit(); | ||
| 17 | |||
| 18 | var raw = orig; | ||
| 19 | |||
| 20 | raw.iflag &= ~@as(tcflag_t, linux.BRKINT | linux.ICRNL | linux.INPCK | linux.ISTRIP | linux.IXON); | ||
| 21 | raw.lflag &= ~@as(tcflag_t, linux.ECHO | linux.ICANON | linux.IEXTEN | linux.ISIG); | ||
| 22 | raw.oflag &= ~@as(tcflag_t, linux.OPOST); | ||
| 23 | |||
| 24 | raw.cflag |= linux.CS8; | ||
| 25 | |||
| 26 | raw.cc[linux.V.MIN] = 0; | ||
| 27 | raw.cc[linux.V.TIME] = 1; | ||
| 28 | |||
| 29 | try tcsetattr(STDIN_FILENO, .FLUSH, raw); | ||
| 30 | |||
| 31 | return self; | ||
| 32 | } | ||
| 33 | |||
| 34 | pub fn deinit(self: RawMode) void { | ||
| 35 | tcsetattr(STDIN_FILENO, .FLUSH, self.orig) catch |err| { | ||
| 36 | std.log.err("Failed to reset termios: {}", .{err}); | ||
| 37 | }; | ||
| 38 | } | ||