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
43
44
45
46
|
extern crate termios;
use std::io;
use std::io::{Stdout, Stdin, Read, Write};
use self::termios::{Termios, TCSANOW, ECHO, ICANON, tcsetattr};
pub struct Controller {
termios: Termios,
stdin: i32,
stdout: Stdout,
reader: Stdin,
buffer: [u8; 1]
}
impl Controller {
pub fn init() -> Controller {
let stdin = 0;
let termios = Termios::from_fd(stdin).unwrap();
// make a mutable copy of termios, and drop echo and canonical mode
let mut new_termios = termios;
new_termios.c_lflag &= !(ICANON | ECHO);
tcsetattr(stdin, TCSANOW, &new_termios).unwrap();
let stdout = io::stdout();
let reader = io::stdin();
let buffer = [0; 1];
Controller {
termios,
stdin,
stdout,
reader,
buffer
}
}
pub fn read(&mut self) -> u8 {
self.stdout.lock().flush().unwrap();
self.reader.read_exact(&mut self.buffer).unwrap();
self.buffer[0]
}
pub fn destroy(&self) {
tcsetattr(self.stdin, TCSANOW, & self.termios).unwrap(); // reset the stdin to original termios data
}
}
|