summaryrefslogtreecommitdiff
path: root/src/RawMode.zig
blob: 7298922d5a1c8d2b11933f4c75e952f6f575dd34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const linux = std.os.linux;
const std = @import("std");

const RawMode = @This();
const termios = std.os.termios;

orig: termios,

pub fn init() !RawMode {
    const orig = try std.os.tcgetattr(std.os.STDIN_FILENO);
    const self = RawMode{ .orig = orig };
    errdefer self.deinit();

    var raw = orig;

    raw.iflag &= ~@as(
        linux.tcflag_t,
        linux.BRKINT | linux.ICRNL | linux.INPCK | linux.ISTRIP | linux.IXON,
    );
    raw.lflag &= ~@as(linux.tcflag_t, linux.ECHO | linux.ICANON | linux.IEXTEN | linux.ISIG);
    raw.oflag &= ~@as(linux.tcflag_t, linux.OPOST);

    raw.cflag |= linux.CS8;

    raw.cc[linux.V.MIN] = 0;
    raw.cc[linux.V.TIME] = 1;

    try std.os.tcsetattr(std.os.STDIN_FILENO, .FLUSH, raw);

    return self;
}

pub fn deinit(self: RawMode) void {
    std.os.tcsetattr(std.os.STDIN_FILENO, .FLUSH, self.orig) catch |err| {
        std.log.err("Failed to reset termios: {}", .{err});
    };
}