2016-03-07 15:01:20 +00:00
|
|
|
use libc::{c_int, c_uint, c_uchar};
|
|
|
|
|
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-07 15:01:20 +00:00
|
|
|
pub fn tcgetattr(filedes: c_int, termptr: *mut Termios) -> c_int;
|
|
|
|
pub fn tcsetattr(filedes: c_int, opt: c_int, termptr: *mut Termios) -> c_int;
|
|
|
|
pub fn cfmakeraw(termptr: *mut Termios);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct Termios {
|
|
|
|
c_iflag: c_uint,
|
|
|
|
c_oflag: c_uint,
|
|
|
|
c_cflag: c_uint,
|
|
|
|
c_lflag: c_uint,
|
|
|
|
c_line: c_uchar,
|
|
|
|
c_cc: [c_uchar; 32],
|
|
|
|
c_ispeed: c_uint,
|
|
|
|
c_ospeed: c_uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_terminal_attr() -> (Termios, c_int) {
|
|
|
|
unsafe {
|
|
|
|
let mut ios = Termios {
|
|
|
|
c_iflag: 0,
|
|
|
|
c_oflag: 0,
|
|
|
|
c_cflag: 0,
|
|
|
|
c_lflag: 0,
|
|
|
|
c_line: 0,
|
|
|
|
c_cc: [0; 32],
|
|
|
|
c_ispeed: 0,
|
|
|
|
c_ospeed: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
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 _);
|
|
|
|
}
|
|
|
|
}
|