diff --git a/libk/src/drivers/io/keyboard.rs b/libk/src/drivers/io/keyboard.rs index 0d7738e..87ce346 100644 --- a/libk/src/drivers/io/keyboard.rs +++ b/libk/src/drivers/io/keyboard.rs @@ -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> = Once::new(); static WAKER: AtomicWaker = AtomicWaker::new(); +pub static KEYBOARD: Lazy>> = Lazy::new(|| { + Mutex::new(Keyboard::new( + ScancodeSet1::new(), + layouts::Uk105Key, + HandleControl::Ignore, + )) +}); + +pub static KEYBOARD_HANDLER: Lazy> = + 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 for KeyStroke { + type Error = (); + + fn try_from(code: u8) -> Result { + 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"), + } + } +}