termios (doesn't work)

This commit is contained in:
Greg Shuflin 2023-07-26 00:27:07 -07:00
parent 02bddc908c
commit 398c36525f
2 changed files with 39 additions and 35 deletions

View File

@ -21,6 +21,44 @@ extern "C" {
static mut foregroundColor: c_int;
}
static mut SAVED_TERMIOS: Option<termios::Termios> = None;
/// Turn off terminal echo and line buffering when passed an integer value
/// that evaluates to true. Restore the original terminal values when passed
/// an integer value that evaluates to false.
#[no_mangle]
pub extern "C" fn nmstermio_set_terminal(s: c_int) {
use std::os::fd::AsRawFd;
use std::fs::File;
use termios::*;
let fd = if atty::is(atty::Stream::Stdin) {
println!("stdin");
std::io::stdin().as_raw_fd()
} else {
println!("tty");
File::open("/dev/tty").unwrap().as_raw_fd()
};
println!("{fd}");
let mut termios = Termios::from_fd(fd).unwrap();
if s == 0 {
unsafe {
SAVED_TERMIOS = Some(termios.clone());
}
termios.c_lflag &= !ICANON & !ECHO;
tcsetattr(fd, TCSANOW, &mut termios).unwrap();
} else {
unsafe {
if let Some(saved_termios) = SAVED_TERMIOS {
tcsetattr(fd, TCSANOW, &saved_termios).unwrap();
}
}
}
}
///Sleep for the number of milliseconds indicated by argument
#[no_mangle]
pub extern "C" fn nmseffect_sleep(t: c_int) {

View File

@ -53,7 +53,7 @@ static int clearScr = 0; // clearScr flag
int foregroundColor = COLOR_BLUE; // Foreground color setting
// Function prototypes
static void nmstermio_set_terminal(int);
extern void nmstermio_set_terminal(int);
/*
* Initialize and configure the terminal for output. This usually means
@ -257,37 +257,3 @@ int nmstermio_get_cursor_row(void) {
return row;
}
/*
* Turn off terminal echo and line buffering when passed an integer value
* that evaluates to true. Restore the original terminal values when passed
* an integer value that evaluates to false.
*/
static void nmstermio_set_terminal(int s) {
struct termios tp;
static struct termios save;
static int state = 1;
if (!isatty(STDIN_FILENO)) {
stdin = freopen("/dev/tty", "r", stdin);
}
if (s == 0) {
if (tcgetattr(STDIN_FILENO, &tp) == -1) {
return;
}
save = tp;
tp.c_lflag &=(~ICANON & ~ECHO);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tp) == -1) {
return;
}
} else {
if (state == 0 && tcsetattr(STDIN_FILENO, TCSANOW, &save) == -1)
return;
}
state = s;
}