summaryrefslogtreecommitdiff
path: root/src/RawMode.zig
blob: 67c52032d93f66b83b759a88ebf554f20e2d4c15 (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
38
39
40
41
42
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.BRKINT = false;
    raw.iflag.ICRNL = false;
    raw.iflag.INPCK = false;
    raw.iflag.ISTRIP = false;
    raw.iflag.IXON = false;

    raw.lflag.ECHO = false;
    raw.lflag.ICANON = false;
    raw.lflag.IEXTEN = false;
    raw.lflag.ISIG = false;

    raw.oflag.OPOST = false;

    raw.cflag.CSIZE = .CS8;

    raw.cc[@intFromEnum(std.posix.V.MIN)] = 0;
    raw.cc[@intFromEnum(std.posix.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});
    };
}