making the keyboard module more flexible, still needs work
This commit is contained in:
@@ -5,8 +5,11 @@ use core::{
|
||||
|
||||
use crossbeam::queue::ArrayQueue;
|
||||
use futures_util::{Stream, StreamExt, task::AtomicWaker};
|
||||
use pc_keyboard::{DecodedKey, HandleControl, Keyboard, ScancodeSet1, layouts};
|
||||
use spin::Once;
|
||||
use pc_keyboard::{
|
||||
DecodedKey, HandleControl, KeyCode, Keyboard, ScancodeSet1,
|
||||
layouts::{self, Uk105Key},
|
||||
};
|
||||
use spin::{Lazy, Mutex, Once};
|
||||
|
||||
use crate::println;
|
||||
|
||||
@@ -15,6 +18,17 @@ use super::print;
|
||||
static KBD_QUEUE: Once<ArrayQueue<u8>> = Once::new();
|
||||
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||
|
||||
pub static KEYBOARD: Lazy<Mutex<Keyboard<Uk105Key, ScancodeSet1>>> = Lazy::new(|| {
|
||||
Mutex::new(Keyboard::new(
|
||||
ScancodeSet1::new(),
|
||||
layouts::Uk105Key,
|
||||
HandleControl::Ignore,
|
||||
))
|
||||
});
|
||||
|
||||
pub static KEYBOARD_HANDLER: Lazy<Mutex<KeyboardHandler>> =
|
||||
Lazy::new(|| Mutex::new(KeyboardHandler::new()));
|
||||
|
||||
pub fn add_scancode(scancode: u8) {
|
||||
if let Some(queue) = KBD_QUEUE.get() {
|
||||
if queue.push(scancode).is_err() {
|
||||
@@ -83,3 +97,116 @@ pub async fn print_keypresses() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct KeyboardHandler {
|
||||
_scancodes: ScancodeStream,
|
||||
}
|
||||
|
||||
impl KeyboardHandler {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
_scancodes: ScancodeStream::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for KeyboardHandler {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum KeyStroke {
|
||||
Char(char),
|
||||
Ctrl,
|
||||
RCtrl,
|
||||
Alt,
|
||||
RAlt,
|
||||
Shift,
|
||||
RShift,
|
||||
Meta,
|
||||
RMeta,
|
||||
Backspace,
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
Down,
|
||||
None,
|
||||
Enter,
|
||||
Escape,
|
||||
Del,
|
||||
}
|
||||
|
||||
impl KeyStroke {
|
||||
pub const fn from_keycode(key: KeyCode) -> Self {
|
||||
match key {
|
||||
KeyCode::LControl => Self::Ctrl,
|
||||
KeyCode::RControl => Self::RCtrl,
|
||||
KeyCode::LAlt => Self::Alt,
|
||||
KeyCode::RAlt2 => Self::RAlt,
|
||||
KeyCode::LShift => Self::Shift,
|
||||
KeyCode::RShift => Self::RShift,
|
||||
KeyCode::LWin => Self::Meta,
|
||||
KeyCode::RWin => Self::RMeta,
|
||||
KeyCode::Backspace => Self::Backspace,
|
||||
KeyCode::ArrowLeft => Self::Left,
|
||||
KeyCode::ArrowRight => Self::Right,
|
||||
KeyCode::ArrowUp => Self::Up,
|
||||
KeyCode::ArrowDown => Self::Down,
|
||||
KeyCode::Return => Self::Enter,
|
||||
KeyCode::Escape => Self::Escape,
|
||||
KeyCode::Delete => Self::Del,
|
||||
_ => Self::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for KeyStroke {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(code: u8) -> Result<Self, Self::Error> {
|
||||
let mut keyboard = KEYBOARD.lock();
|
||||
|
||||
let key = match keyboard.add_byte(code) {
|
||||
Ok(Some(event)) => match keyboard.process_keyevent(event) {
|
||||
Some(key) => key,
|
||||
_ => return Err(()),
|
||||
},
|
||||
_ => return Err(()),
|
||||
};
|
||||
|
||||
match key {
|
||||
DecodedKey::Unicode(ch) => Ok(Self::Char(ch)),
|
||||
DecodedKey::RawKey(key) => match Self::from_keycode(key) {
|
||||
Self::None => Err(()),
|
||||
key => Ok(key),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Display for KeyStroke {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
Self::Char(c) => write!(f, "{}", c),
|
||||
Self::Ctrl => write!(f, "CTRL"),
|
||||
Self::RCtrl => write!(f, "RCtrl"),
|
||||
Self::Alt => write!(f, "ALT"),
|
||||
Self::RAlt => write!(f, "RAlt"),
|
||||
Self::Shift => write!(f, "SHIFT"),
|
||||
Self::RShift => write!(f, "RShift"),
|
||||
Self::Meta => write!(f, "META"),
|
||||
Self::RMeta => write!(f, "RMeta"),
|
||||
Self::Backspace => write!(f, "BACKSPACE"),
|
||||
Self::Left => write!(f, "LEFT"),
|
||||
Self::Right => write!(f, "RIGHT"),
|
||||
Self::Up => write!(f, "UP"),
|
||||
Self::Down => write!(f, "DOWN"),
|
||||
Self::Enter => write!(f, "ENTER"),
|
||||
Self::Escape => write!(f, "ESCAPE"),
|
||||
Self::None => write!(f, "NONE"),
|
||||
Self::Del => write!(f, "DEL"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user