summaryrefslogtreecommitdiff
path: root/src/RawMode.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/RawMode.zig')
-rw-r--r--src/RawMode.zig38
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 @@
1const linux = std.os.linux;
2const std = @import("std");
3
4const STDIN_FILENO = std.os.STDIN_FILENO;
5const RawMode = @This();
6const tcflag_t = linux.tcflag_t;
7const tcgetattr = std.os.tcgetattr;
8const tcsetattr = std.os.tcsetattr;
9const termios = std.os.termios;
10
11orig: termios,
12
13pub 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
34pub 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}