move input handling into a separate input module
parent
0d64ff47c7
commit
5a74f9308b
@ -0,0 +1,161 @@
|
|||||||
|
use anyhow::{Context, Result};
|
||||||
|
use crate::{error::Error, log::*};
|
||||||
|
use windows::Win32::System::Console;
|
||||||
|
use windows::Win32::Foundation::HANDLE;
|
||||||
|
|
||||||
|
fn log_input_mode(mode: Console::CONSOLE_MODE) {
|
||||||
|
// Characters read by the ReadFile or ReadConsole function are written to the active screen
|
||||||
|
// buffer as they are typed into the console. This mode can be used only if the
|
||||||
|
// ENABLE_LINE_INPUT mode is also enabled.
|
||||||
|
if (mode & Console::ENABLE_ECHO_INPUT).0 > 0 {
|
||||||
|
debug!("Echo Input: Enabled");
|
||||||
|
} else {
|
||||||
|
debug!("Echo Input: Disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
// When enabled, text entered in a console window will be inserted at the current cursor
|
||||||
|
// location and all text following that location will not be overwritten. When disabled, all
|
||||||
|
// following text will be overwritten.
|
||||||
|
if (mode & Console::ENABLE_INSERT_MODE).0 > 0 {
|
||||||
|
debug!("Insert Mode: Enabled");
|
||||||
|
} else {
|
||||||
|
debug!("Insert Mode: Disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The ReadFile or ReadConsole function returns only when a carriage return character is read.
|
||||||
|
// If this mode is disabled, the functions return when one or more characters are available.
|
||||||
|
if (mode & Console::ENABLE_LINE_INPUT).0 > 0 {
|
||||||
|
debug!("Line Input Mode: Enabled");
|
||||||
|
} else {
|
||||||
|
debug!("Line Input Mode: Disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the mouse pointer is within the borders of the console window and the window has the
|
||||||
|
// keyboard focus, mouse events generated by mouse movement and button presses are placed in
|
||||||
|
// the input buffer. These events are discarded by ReadFile or ReadConsole, even when this mode
|
||||||
|
// is enabled. The ReadConsoleInput function can be used to read MOUSE_EVENT input records from
|
||||||
|
// the input buffer.
|
||||||
|
if (mode & Console::ENABLE_MOUSE_INPUT).0 > 0 {
|
||||||
|
debug!("Mouse Input: Enabled");
|
||||||
|
} else {
|
||||||
|
debug!("Mouse Input: Disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
// CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer
|
||||||
|
// is being read by ReadFile or ReadConsole, other control keys are processed by the system and
|
||||||
|
// are not returned in the ReadFile or ReadConsole buffer. If the ENABLE_LINE_INPUT mode is
|
||||||
|
// also enabled, backspace, carriage return, and line feed characters are handled by the
|
||||||
|
// system.
|
||||||
|
if (mode & Console::ENABLE_PROCESSED_INPUT).0 > 0 {
|
||||||
|
debug!("Processed Input: Enabled");
|
||||||
|
} else {
|
||||||
|
debug!("Processed Input: Disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
// This flag enables the user to use the mouse to select and edit text. To enable this mode,
|
||||||
|
// use ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS. To disable this mode, use
|
||||||
|
// ENABLE_EXTENDED_FLAGS without this flag.
|
||||||
|
if (mode & Console::ENABLE_QUICK_EDIT_MODE).0 > 0 {
|
||||||
|
debug!("Quick Edit Mode: Enabled");
|
||||||
|
} else {
|
||||||
|
debug!("Quick Edit Mode: Disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
// User interactions that change the size of the console screen buffer are reported in the
|
||||||
|
// console's input buffer. Information about these events can be read from the input buffer by
|
||||||
|
// applications using the ReadConsoleInput function, but not by those using ReadFile or
|
||||||
|
// ReadConsole.
|
||||||
|
if (mode & Console::ENABLE_WINDOW_INPUT).0 > 0 {
|
||||||
|
debug!("Window Input: Enabled");
|
||||||
|
} else {
|
||||||
|
debug!("Window Input: Disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setting this flag directs the Virtual Terminal processing engine to convert user input
|
||||||
|
// received by the console window into Console Virtual Terminal Sequences that can be retrieved
|
||||||
|
// by a supporting application through ReadFile or ReadConsole functions.
|
||||||
|
//
|
||||||
|
// The typical usage of this flag is intended in conjunction with
|
||||||
|
// ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output handle to connect to an application that
|
||||||
|
// communicates exclusively via virtual terminal sequences.
|
||||||
|
if (mode & Console::ENABLE_VIRTUAL_TERMINAL_INPUT).0 > 0 {
|
||||||
|
debug!("Virtual Terminal Input: Enabled");
|
||||||
|
} else {
|
||||||
|
debug!("Virtual Terminal Input: Disabled");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stdin_handle() -> Result<HANDLE> {
|
||||||
|
unsafe {
|
||||||
|
let handle = Console::GetStdHandle(Console::STD_INPUT_HANDLE)
|
||||||
|
.context("unable to get stdin handle")?;
|
||||||
|
Ok(handle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_stdin() -> Result<()> {
|
||||||
|
let mut mode = Console::CONSOLE_MODE(0);
|
||||||
|
unsafe {
|
||||||
|
Console::SetConsoleCP(65001);
|
||||||
|
|
||||||
|
let handle = stdin_handle()?;
|
||||||
|
Error::check(Console::GetConsoleMode(handle, &mut mode))?;
|
||||||
|
|
||||||
|
// allow terminal input characters
|
||||||
|
mode |= Console::ENABLE_VIRTUAL_TERMINAL_INPUT;
|
||||||
|
|
||||||
|
// disable automatic processing of CTRL+C, we'll handle it ourselves
|
||||||
|
mode &= !Console::ENABLE_PROCESSED_INPUT;
|
||||||
|
|
||||||
|
// disable line mode to get every input as its pressed
|
||||||
|
mode &= !Console::ENABLE_LINE_INPUT;
|
||||||
|
|
||||||
|
// disable automatic echoing of inputs
|
||||||
|
mode &= !Console::ENABLE_ECHO_INPUT;
|
||||||
|
|
||||||
|
// enable mouse input
|
||||||
|
mode |= Console::ENABLE_MOUSE_INPUT;
|
||||||
|
|
||||||
|
Error::check(Console::SetConsoleMode(handle, mode))?;
|
||||||
|
Error::check(Console::GetConsoleMode(handle, &mut mode))?;
|
||||||
|
debug!("Stdin details:");
|
||||||
|
log_input_mode(mode);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Reader {
|
||||||
|
input: HANDLE,
|
||||||
|
// this area in memory is where the Windows API will write events read off of the keyboard.
|
||||||
|
buf: [Console::INPUT_RECORD; 100],
|
||||||
|
// the number of valid input record items in the buf since last read
|
||||||
|
buf_len: u32,
|
||||||
|
// the position of our current indexer into the input record buffer
|
||||||
|
buf_idx: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Reader {
|
||||||
|
pub fn new() -> Result<Self> {
|
||||||
|
setup_stdin()?;
|
||||||
|
Ok(Self {
|
||||||
|
buf: [Console::INPUT_RECORD::default(); 100],
|
||||||
|
buf_len: 0,
|
||||||
|
buf_idx: 0,
|
||||||
|
input: stdin_handle()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next(&mut self) -> Result<Console::INPUT_RECORD> {
|
||||||
|
// All buffered items have been processed, ask the OS for new event records
|
||||||
|
if self.buf_idx as u32 >= self.buf_len {
|
||||||
|
unsafe {
|
||||||
|
Error::check(Console::ReadConsoleInputW(self.input, &mut self.buf, &mut self.buf_len))?;
|
||||||
|
}
|
||||||
|
self.buf_idx = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let rec = self.buf[self.buf_idx];
|
||||||
|
self.buf_idx += 1;
|
||||||
|
return Ok(rec);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue