From 0ebc5ab0ee0fc80c801487f534687c8bd236abc1 Mon Sep 17 00:00:00 2001 From: Tianhao Wang Date: Thu, 1 Feb 2024 18:12:24 +0100 Subject: keyctl: use bitflags for key modifiers Merge key modifier getters and setters --- Cargo.toml | 1 + src/machine/key.rs | 122 ++++++++++++------------------------------------- src/machine/keyctrl.rs | 16 +++---- 3 files changed, 38 insertions(+), 101 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 700e7ee..65a6dd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] spin = "0.9.8" +bitflags = "2.4.2" [dependencies.lazy_static] version = "1.4" diff --git a/src/machine/key.rs b/src/machine/key.rs index 250b38d..0a09781 100644 --- a/src/machine/key.rs +++ b/src/machine/key.rs @@ -1,10 +1,11 @@ use self::super::kbd_defs::*; +use bitflags::bitflags; use core::convert; pub struct Key { asc: u8, scan: u8, - modi: u8, + modi: Modifiers, rawcode: u8, // this field not necessary, remove after testing } @@ -24,17 +25,33 @@ impl convert::Into for Key { } } +bitflags! { + pub struct Modifiers:u8 { + const NONE = 0; + const SHIFT = 1 << 0; + const ALT_LEFT = 1 << 1; + const ALT_RIGHT = 1 << 2; + const CTRL_LEFT = 1 << 3; + const CTRL_RIGHT = 1 << 4; + const CAPSLOCK = 1 << 5; + const NUMLOCK = 1 << 6; + const SCROLL_LOCK = 1 << 7; + } +} + #[allow(dead_code)] impl Key { pub fn new() -> Self { Self { asc: 0, scan: 0, - modi: 0, + modi: Modifiers::NONE, rawcode: 0, } } - + pub fn decode(&mut self) { + // decode key + } pub fn valid(self) -> bool { self.scan != 0 } @@ -65,100 +82,19 @@ impl Key { self.scan } - // reading the state of SHIFT, ALT, CTRL etc. - pub fn shift(&self) -> bool { - self.modi & (Mbit::Shift as u8) != 0 - } - pub fn alt_left(&self) -> bool { - self.modi & (Mbit::AltLeft as u8) != 0 - } - pub fn alt_right(&self) -> bool { - self.modi & (Mbit::AltRight as u8) != 0 - } - pub fn ctrl_left(&self) -> bool { - self.modi & (Mbit::CtrlLeft as u8) != 0 - } - pub fn ctrl_right(&self) -> bool { - self.modi & (Mbit::CtrlRight as u8) != 0 - } - pub fn caps_lock(&self) -> bool { - self.modi & (Mbit::CapsLock as u8) != 0 - } - pub fn num_lock(&self) -> bool { - self.modi & (Mbit::NumLock as u8) != 0 - } - pub fn scroll_lock(&self) -> bool { - self.modi & (Mbit::ScrollLock as u8) != 0 - } - pub fn alt(&self) -> bool { - self.alt_left() | self.alt_right() - } - pub fn ctrl(&self) -> bool { - self.ctrl_left() | self.ctrl_right() - } - - // setting/clearing states of SHIFT, ALT, CTRL etc. - pub fn set_shift(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::Shift as u8 - } else { - self.modi & !(Mbit::Shift as u8) - } - } + // TODO the setters and getters should not be their own functions.... - pub fn set_alt_left(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::AltLeft as u8 - } else { - self.modi & !(Mbit::AltLeft as u8) - } - } - - pub fn set_alt_right(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::AltRight as u8 - } else { - self.modi & !(Mbit::AltRight as u8) - } - } - - pub fn set_ctrl_left(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::CtrlLeft as u8 - } else { - self.modi & !(Mbit::CtrlLeft as u8) - } - } - - pub fn set_ctrl_right(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::CtrlRight as u8 - } else { - self.modi & !(Mbit::CtrlRight as u8) - } - } - - pub fn set_caps_lock(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::CapsLock as u8 - } else { - self.modi & !(Mbit::CapsLock as u8) - } - } - - pub fn set_num_lock(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::NumLock as u8 - } else { - self.modi & !(Mbit::NumLock as u8) - } + #[inline(always)] + pub fn mod_contains(&self, modi: Modifiers) -> bool { + self.modi.contains(modi) } - pub fn set_scroll_lock(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::ScrollLock as u8 + #[inline(always)] + pub fn mod_set(&mut self, modi: Modifiers, pressed: bool) { + if pressed { + self.modi.insert(modi); } else { - self.modi & !(Mbit::ScrollLock as u8) + self.modi.remove(modi); } } } diff --git a/src/machine/keyctrl.rs b/src/machine/keyctrl.rs index 93bd419..43f92d1 100644 --- a/src/machine/keyctrl.rs +++ b/src/machine/keyctrl.rs @@ -15,27 +15,23 @@ use crate::machine::device_io::*; // reboot() // set_led(char led,bool on) // set_repeat_rate(int speed,int delay) + pub struct KeyboardController { code: u8, prefix: u8, gather: Key, leds: u8, - - // two ports for keyboard controller - ctrl_port: u16, - data_port: u16, - // status register bits } impl KeyboardController { + const CTRL_PORT:u16 = 0x64; + const DATA_PORT:u16 = 0x60; pub fn new() -> Self { Self { code: 0, prefix: 9, gather: Key::new(), leds: 0, - ctrl_port: 0x64, - data_port: 0x60, } } @@ -45,8 +41,12 @@ impl KeyboardController { let mut invalid: Key = Key::new(); invalid.set_raw(0xff); - let status = inb(self.ctrl_port); + let status = inb(Self::CTRL_PORT); return Key::new(); // TODO here } + + pub fn reboot(&mut self) { + todo!(); + } } -- cgit v1.2.3-70-g09d2