xref: /StarryEngine/starry_applications/src/lib.rs (revision 2b942a51ce0651f9872b9bf803f3dfc6199e7b63)
1 extern crate starry_client;
2 extern crate starry_server;
3 extern crate starry_toolkit;
4 extern crate termios;
5 
6 pub mod asset_manager;
7 
8 use std::io;
9 use std::os::unix::io::AsRawFd;
10 use termios::{tcsetattr, Termios};
11 
12 static mut STORED_SETTINGS: Option<Termios> = None;
13 
14 /// 禁止tty回显 并设置为非阻塞模式
set_tty() -> io::Result<()>15 pub fn set_tty() -> io::Result<()> {
16     let stdin = io::stdin().as_raw_fd();
17     let cur_settings = Termios::from_fd(stdin)?;
18 
19     let mut new_settings = cur_settings.clone();
20     new_settings.c_lflag &= !(termios::ICANON) & !(termios::ECHO);
21     new_settings.c_cc[termios::VMIN] = 1;
22     new_settings.c_cc[termios::VTIME] = 0;
23 
24     tcsetattr(stdin, termios::TCSANOW, &new_settings)?;
25 
26     unsafe {
27         STORED_SETTINGS = Some(cur_settings);
28     }
29 
30     Ok(())
31 }
32 
33 /// 回退tty设置
reset_tty() -> io::Result<()>34 pub fn reset_tty() -> io::Result<()> {
35     let stdin = io::stdin().as_raw_fd();
36 
37     unsafe {
38         tcsetattr(stdin, termios::TCSANOW, &STORED_SETTINGS.unwrap())?;
39     }
40 
41     Ok(())
42 }
43