termion/src/termios.rs

48 lines
1018 B
Rust
Raw Normal View History

2016-03-10 06:05:08 +00:00
use libc::c_int;
use std::mem;
pub use libc::termios as Termios;
2016-03-07 15:01:20 +00:00
2016-03-08 09:08:50 +00:00
#[cfg(not(target_os = "macos"))]
2016-03-08 10:27:53 +00:00
pub const TIOCGWINSZ: usize = 0x00005413;
2016-03-08 09:08:50 +00:00
#[cfg(target_os = "macos")]
2016-03-08 10:27:53 +00:00
pub const TIOCGWINSZ: usize = 0x40087468;
2016-03-07 15:01:20 +00:00
2016-03-08 09:08:50 +00:00
extern {
2016-03-09 18:17:00 +00:00
pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int;
pub fn tcsetattr(fd: c_int, opt: c_int, termptr: *mut Termios) -> c_int;
2016-03-07 15:01:20 +00:00
pub fn cfmakeraw(termptr: *mut Termios);
}
pub fn get_terminal_attr() -> (Termios, c_int) {
unsafe {
2016-03-10 06:05:08 +00:00
let mut ios = mem::zeroed();
2016-03-07 15:01:20 +00:00
let attr = tcgetattr(0, &mut ios);
(ios, attr)
}
}
pub fn set_terminal_attr(ios: *mut Termios) -> c_int {
unsafe {
tcsetattr(0, 0, ios)
}
}
2016-03-09 16:18:31 +00:00
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_get_terminal_attr() {
get_terminal_attr();
get_terminal_attr();
get_terminal_attr();
}
#[test]
fn test_set_terminal_attr() {
let mut ios = get_terminal_attr().0;
set_terminal_attr(&mut ios as *mut _);
}
}