|
|
@ -145,8 +145,20 @@ impl Reader {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn next(&mut self) -> Result<Console::INPUT_RECORD> {
|
|
|
|
pub fn next(&mut self) -> Result<Event> {
|
|
|
|
// All buffered items have been processed, ask the OS for new event records
|
|
|
|
let rec = self.next_rec()?;
|
|
|
|
|
|
|
|
if rec.EventType as u32 == Console::KEY_EVENT {
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
|
|
|
let event = rec.Event.KeyEvent;
|
|
|
|
|
|
|
|
if event.wVirtualKeyCode == 0 && event.uChar.UnicodeChar == 27 {
|
|
|
|
|
|
|
|
return Ok(self.next_escape_sequence()?);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(rec.into())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn next_rec(&mut self) -> Result<Console::INPUT_RECORD> {
|
|
|
|
if self.buf_idx as u32 >= self.buf_len {
|
|
|
|
if self.buf_idx as u32 >= self.buf_len {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
Error::check(Console::ReadConsoleInputA(
|
|
|
|
Error::check(Console::ReadConsoleInputA(
|
|
|
@ -162,6 +174,49 @@ impl Reader {
|
|
|
|
self.buf_idx += 1;
|
|
|
|
self.buf_idx += 1;
|
|
|
|
return Ok(rec);
|
|
|
|
return Ok(rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn next_escape_sequence(&mut self) -> Result<Event> {
|
|
|
|
|
|
|
|
self.take_bracket()?;
|
|
|
|
|
|
|
|
match self.next_escape_char()? {
|
|
|
|
|
|
|
|
'D' => Ok(Event::Left),
|
|
|
|
|
|
|
|
e => Err(Error::input_error(format!("unexpected escape char: {}", e)).into()),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn take_bracket(&mut self) -> Result<()> {
|
|
|
|
|
|
|
|
let rec = self.next_rec()?;
|
|
|
|
|
|
|
|
if rec.EventType as u32 != Console::KEY_EVENT {
|
|
|
|
|
|
|
|
Err(Error::input_error("failed to read escape sequence: not a key event").into())
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
|
|
|
let event = rec.Event.KeyEvent;
|
|
|
|
|
|
|
|
if event.wVirtualKeyCode == 0 && event.uChar.UnicodeChar == 91 {
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Err(Error::input_error("failed to read escape sequence: not a [").into())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn next_escape_char(&mut self) -> Result<char> {
|
|
|
|
|
|
|
|
let rec = self.next_rec()?;
|
|
|
|
|
|
|
|
if rec.EventType as u32 != Console::KEY_EVENT {
|
|
|
|
|
|
|
|
Err(
|
|
|
|
|
|
|
|
Error::input_error("failed to read char in escape sequence: not a key event")
|
|
|
|
|
|
|
|
.into(),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
|
|
|
let n = rec.Event.KeyEvent.uChar.UnicodeChar as u32;
|
|
|
|
|
|
|
|
let c = char::from_u32(n);
|
|
|
|
|
|
|
|
c.ok_or_else(|| {
|
|
|
|
|
|
|
|
let msg = format!("escape key value is not a valid unicode character: {}", n);
|
|
|
|
|
|
|
|
Error::input_error(msg).into()
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
@ -169,11 +224,9 @@ pub enum Event {
|
|
|
|
Focus(bool),
|
|
|
|
Focus(bool),
|
|
|
|
Menu(u32),
|
|
|
|
Menu(u32),
|
|
|
|
Key(key::Event),
|
|
|
|
Key(key::Event),
|
|
|
|
Mouse{
|
|
|
|
Mouse { x: i16, y: i16 },
|
|
|
|
x: i16,
|
|
|
|
|
|
|
|
y: i16,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Size,
|
|
|
|
Size,
|
|
|
|
|
|
|
|
Left,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ALT_KEYS: u32 = 0x0002 | 0x0001;
|
|
|
|
const ALT_KEYS: u32 = 0x0002 | 0x0001;
|
|
|
|