Remove virtio-input-decoder dependence

This commit is contained in:
Yuke Peng 2024-01-18 22:58:36 +08:00 committed by Tate, Hongliang Tian
parent 2437fba0d9
commit 2d605e04d1
7 changed files with 172 additions and 33 deletions

4
Cargo.lock generated
View File

@ -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]]

View File

@ -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]

View File

@ -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,
}

View File

@ -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<dyn InputDevice>) {

View File

@ -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" }

View File

@ -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<VirtioInputConfig, IoMem>,
event_queue: SpinLock<VirtQueue>,
status_queue: VirtQueue,
event_buf: SpinLock<Box<[InputEvent; QUEUE_SIZE as usize]>>,
event_buf: SpinLock<Box<[VirtioInputEvent; QUEUE_SIZE as usize]>>,
#[allow(clippy::type_complexity)]
callbacks: SpinLock<Vec<Arc<dyn Fn(DecodeType) + Send + Sync + 'static>>>,
callbacks: SpinLock<Vec<Arc<dyn Fn(InputEvent) + Send + Sync + 'static>>>,
transport: Box<dyn VirtioTransport>,
}
@ -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<dyn VirtioTransport>) -> 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<InputEvent> {
pub fn pop_pending_event(&self) -> Option<VirtioInputEvent> {
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))
}
}

View File

@ -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.