From 2d605e04d12658cd98728ee79de3e426d2ea4343 Mon Sep 17 00:00:00 2001 From: Yuke Peng Date: Thu, 18 Jan 2024 22:58:36 +0800 Subject: [PATCH] Remove virtio-input-decoder dependence --- Cargo.lock | 4 +- services/comps/input/Cargo.toml | 3 +- services/comps/input/src/key.rs | 128 ++++++++++++++++++ services/comps/input/src/lib.rs | 14 +- services/comps/virtio/Cargo.toml | 1 - .../comps/virtio/src/device/input/device.rs | 53 ++++---- services/comps/virtio/src/device/input/mod.rs | 2 +- 7 files changed, 172 insertions(+), 33 deletions(-) create mode 100644 services/comps/input/src/key.rs diff --git a/Cargo.lock b/Cargo.lock index 54c72b82..d2af0117 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,15 +189,16 @@ dependencies = [ name = "aster-input" version = "0.1.0" dependencies = [ + "ascii", "aster-frame", "aster-rights", "aster-util", "bitflags 1.3.2", "component", + "int-to-c-enum", "lazy_static", "log", "spin 0.9.8", - "virtio-input-decoder", ] [[package]] @@ -342,7 +343,6 @@ dependencies = [ "smoltcp", "spin 0.9.8", "typeflags-util", - "virtio-input-decoder", ] [[package]] diff --git a/services/comps/input/Cargo.toml b/services/comps/input/Cargo.toml index 7db8afd7..3a629304 100644 --- a/services/comps/input/Cargo.toml +++ b/services/comps/input/Cargo.toml @@ -12,8 +12,9 @@ aster-frame = { path = "../../../framework/aster-frame" } aster-util = { path = "../../libs/aster-util" } aster-rights = { path = "../../libs/aster-rights" } component = { path = "../../libs/comp-sys/component" } -virtio-input-decoder = "0.1.4" +int-to-c-enum = { path = "../../libs/int-to-c-enum" } log = "0.4" +ascii = { version = "1.1", default-features = false, features = ["alloc"] } [features] diff --git a/services/comps/input/src/key.rs b/services/comps/input/src/key.rs new file mode 100644 index 00000000..9f394c10 --- /dev/null +++ b/services/comps/input/src/key.rs @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: MPL-2.0 + +use int_to_c_enum::TryFromInt; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, TryFromInt)] +#[repr(u16)] +pub enum Key { + Reserved = 0, + ESC = 1, + One = 2, + Two = 3, + Three = 4, + Four = 5, + Five = 6, + Six = 7, + Seven = 8, + Eight = 9, + Nine = 10, + Zero = 11, + Minus = 12, + Equal = 13, + BackSpace = 14, + Tab = 15, + Q = 16, + W = 17, + E = 18, + R = 19, + T = 20, + Y = 21, + U = 22, + I = 23, + O = 24, + P = 25, + /// Symbol: [ + LeftBrace = 26, + /// Symbol: ] + RightBrace = 27, + Enter = 28, + LeftCtrl = 29, + A = 30, + S = 31, + D = 32, + F = 33, + G = 34, + H = 35, + J = 36, + K = 37, + L = 38, + /// Symbol: ; + SemiColon = 39, + /// Symbol: ' + Apostrophe = 40, + /// Symbol: ` + Grave = 41, + LeftShift = 42, + /// Symbol: \ + BackSlash = 43, + Z = 44, + X = 45, + C = 46, + V = 47, + B = 48, + N = 49, + M = 50, + Comma = 51, + Dot = 52, + // Symbol: / + Slash = 53, + RightShift = 54, + /// Keypad asterisk, Symbol: * + KpAsterisk = 55, + LeftAlt = 56, + Space = 57, + Capslock = 58, + F1 = 59, + F2 = 60, + F3 = 61, + F4 = 62, + F5 = 63, + F6 = 64, + F7 = 65, + F8 = 66, + F9 = 67, + F10 = 68, + NumLock = 69, + ScrollLock = 70, + Kp7 = 71, + Kp8 = 72, + Kp9 = 73, + KpMinus = 74, + Kp4 = 75, + Kp5 = 76, + Kp6 = 77, + KpPlus = 78, + Kp1 = 79, + Kp2 = 80, + Kp3 = 81, + Kp0 = 82, + KpDot = 83, + + F11 = 87, + F12 = 88, + + KpEnter = 96, + RightCtrl = 97, + KpSlash = 98, + + RightAlt = 100, + LineFeed = 101, + Home = 102, + Up = 103, + PageUp = 104, + Left = 105, + Right = 106, + End = 107, + Down = 108, + PageDown = 109, + Insert = 110, + Delete = 111, + + LeftMeta = 125, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum KeyStatus { + Pressed, + Released, +} diff --git a/services/comps/input/src/lib.rs b/services/comps/input/src/lib.rs index 815a1e82..41769408 100644 --- a/services/comps/input/src/lib.rs +++ b/services/comps/input/src/lib.rs @@ -6,6 +6,9 @@ #![feature(fn_traits)] extern crate alloc; + +pub mod key; + use core::any::Any; use core::fmt::Debug; @@ -18,11 +21,18 @@ use component::ComponentInitError; use aster_frame::sync::SpinLock; use spin::Once; -use virtio_input_decoder::DecodeType; + +use key::Key; +use key::KeyStatus; + +#[derive(Debug, Clone, Copy)] +pub enum InputEvent { + KeyBoard(Key, KeyStatus), +} pub trait InputDevice: Send + Sync + Any + Debug { fn handle_irq(&self) -> Option<()>; - fn register_callbacks(&self, function: &'static (dyn Fn(DecodeType) + Send + Sync)); + fn register_callbacks(&self, function: &'static (dyn Fn(InputEvent) + Send + Sync)); } pub fn register_device(name: String, device: Arc) { diff --git a/services/comps/virtio/Cargo.toml b/services/comps/virtio/Cargo.toml index e5570c99..cece300d 100644 --- a/services/comps/virtio/Cargo.toml +++ b/services/comps/virtio/Cargo.toml @@ -8,7 +8,6 @@ edition = "2021" [dependencies] bitflags = "1.3" spin = "0.9.4" -virtio-input-decoder = "0.1.4" bytes = { version = "1.4.0", default-features = false } align_ext = { path = "../../../framework/libs/align_ext" } aster-input = { path = "../input" } diff --git a/services/comps/virtio/src/device/input/device.rs b/services/comps/virtio/src/device/input/device.rs index e5a345fc..678ebe89 100644 --- a/services/comps/virtio/src/device/input/device.rs +++ b/services/comps/virtio/src/device/input/device.rs @@ -10,13 +10,16 @@ use alloc::{ vec::Vec, }; use aster_frame::{io_mem::IoMem, offset_of, sync::SpinLock, trap::TrapFrame}; +use aster_input::{ + key::{Key, KeyStatus}, + InputEvent, +}; use aster_util::{field_ptr, safe_ptr::SafePtr}; use bitflags::bitflags; use log::{debug, info}; use pod::Pod; -use virtio_input_decoder::{DecodeType, Decoder}; -use super::{InputConfigSelect, InputEvent, VirtioInputConfig, QUEUE_EVENT, QUEUE_STATUS}; +use super::{InputConfigSelect, VirtioInputConfig, VirtioInputEvent, QUEUE_EVENT, QUEUE_STATUS}; bitflags! { /// The properties of input device. @@ -64,9 +67,9 @@ pub struct InputDevice { config: SafePtr, event_queue: SpinLock, status_queue: VirtQueue, - event_buf: SpinLock>, + event_buf: SpinLock>, #[allow(clippy::type_complexity)] - callbacks: SpinLock>>, + callbacks: SpinLock>>, transport: Box, } @@ -74,7 +77,7 @@ impl InputDevice { /// Create a new VirtIO-Input driver. /// msix_vector_left should at least have one element or n elements where n is the virtqueue amount pub fn init(mut transport: Box) -> Result<(), VirtioDeviceError> { - let mut event_buf = Box::new([InputEvent::default(); QUEUE_SIZE as usize]); + let mut event_buf = Box::new([VirtioInputEvent::default(); QUEUE_SIZE as usize]); let mut event_queue = VirtQueue::new(QUEUE_EVENT, QUEUE_SIZE, transport.as_mut()) .expect("create event virtqueue failed"); let status_queue = VirtQueue::new(QUEUE_STATUS, QUEUE_SIZE, transport.as_mut()) @@ -139,7 +142,7 @@ impl InputDevice { } /// Pop the pending event. - pub fn pop_pending_event(&self) -> Option { + pub fn pop_pending_event(&self) -> Option { let mut lock = self.event_queue.lock(); if let Ok((token, _)) = lock.pop_used() { if token >= QUEUE_SIZE { @@ -192,31 +195,29 @@ impl aster_input::InputDevice for InputDevice { let Some(event) = self.pop_pending_event() else { return Some(()); }; - let dtype = match Decoder::decode( - event.event_type as usize, - event.code as usize, - event.value as usize, - ) { - Ok(dtype) => dtype, - Err(_) => return Some(()), - }; - let lock = self.callbacks.lock(); - for callback in lock.iter() { - callback.call((dtype,)); + match event.event_type { + 0 => return Some(()), + // Keyboard + 1 => {} + // TODO: Support mouse device. + _ => continue, } - match dtype { - virtio_input_decoder::DecodeType::Key(key, r#type) => { - info!("{:?} {:?}", key, r#type); - } - virtio_input_decoder::DecodeType::Mouse(mouse) => info!("{:?}", mouse), + let status = match event.value { + 1 => KeyStatus::Pressed, + 0 => KeyStatus::Released, + _ => return Some(()), + }; + let event = InputEvent::KeyBoard(Key::try_from(event.code).unwrap(), status); + info!("Input Event:{:?}", event); + + let callbacks = self.callbacks.lock(); + for callback in callbacks.iter() { + callback.call((event,)); } } } - fn register_callbacks( - &self, - function: &'static (dyn Fn(virtio_input_decoder::DecodeType) + Send + Sync), - ) { + fn register_callbacks(&self, function: &'static (dyn Fn(InputEvent) + Send + Sync)) { self.callbacks.lock().push(Arc::new(function)) } } diff --git a/services/comps/virtio/src/device/input/mod.rs b/services/comps/virtio/src/device/input/mod.rs index 011acd00..2f4760b8 100644 --- a/services/comps/virtio/src/device/input/mod.rs +++ b/services/comps/virtio/src/device/input/mod.rs @@ -103,7 +103,7 @@ struct DevIDs { /// are filled according to the Linux input layer (evdev) interface. #[repr(C)] #[derive(Clone, Copy, Debug, Default, Pod)] -pub struct InputEvent { +pub struct VirtioInputEvent { /// Event type. pub event_type: u16, /// Event code.