package term

import "core:sys/posix"

original_termios: posix.termios

enable_raw_mode :: proc() {
	posix.tcgetattr(posix.STDIN_FILENO, &original_termios)

	raw := original_termios
	// Strip the flags that fight you
	raw.c_lflag &~= {.ECHO, .ICANON, .ISIG, .IEXTEN}
	raw.c_iflag &~= {.IXON, .ICRNL, .BRKINT, .INPCK, .ISTRIP}
	raw.c_oflag &~= {.OPOST}

	// VMIN=0, VTIME=1 -> read() returns after 100ms even with no input
	// (good for a game loop: check input, redraw, repeat)
	raw.c_cc[.VMIN] = 0
	raw.c_cc[.VTIME] = 1

	posix.tcsetattr(posix.STDIN_FILENO, .TCSAFLUSH, &raw)
}

disable_raw_mode :: proc() {
	posix.tcsetattr(posix.STDIN_FILENO, .TCSAFLUSH, &original_termios)
}