feat(libkos): add fs, display, input service client modules with newtypes
This commit is contained in:
63
crates/libkos/src/input.rs
Normal file
63
crates/libkos/src/input.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use types::bounded::CapSlot;
|
||||
use crate::syscall;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Key {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Enter,
|
||||
Escape,
|
||||
Space,
|
||||
Backspace,
|
||||
Tab,
|
||||
Char(u8),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum InputEvent {
|
||||
KeyDown(Key),
|
||||
KeyUp(Key),
|
||||
}
|
||||
|
||||
fn decode_key(kind: usize, extra: usize) -> Option<Key> {
|
||||
match kind {
|
||||
0 => Some(Key::Up),
|
||||
1 => Some(Key::Down),
|
||||
2 => Some(Key::Left),
|
||||
3 => Some(Key::Right),
|
||||
4 => Some(Key::Enter),
|
||||
5 => Some(Key::Escape),
|
||||
6 => Some(Key::Space),
|
||||
7 => Some(Key::Backspace),
|
||||
8 => Some(Key::Tab),
|
||||
9 => Some(Key::Char(extra as u8)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_event(ep: CapSlot) -> Option<InputEvent> {
|
||||
syscall::send(ep, 0, 0, 0, 0);
|
||||
let (tag, kind, extra, _) = syscall::receive(ep);
|
||||
match tag {
|
||||
0 => None,
|
||||
1 => decode_key(kind, extra).map(InputEvent::KeyDown),
|
||||
2 => decode_key(kind, extra).map(InputEvent::KeyUp),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn drain_events(ep: CapSlot) -> Option<InputEvent> {
|
||||
let mut last = None;
|
||||
loop {
|
||||
match poll_event(ep) {
|
||||
None => return last,
|
||||
Some(ev) => {
|
||||
if matches!(ev, InputEvent::KeyDown(_)) {
|
||||
last = Some(ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user