asterinas/kernel/comps/input/src/lib.rs

77 lines
1.7 KiB
Rust
Raw Normal View History

2024-01-03 03:22:36 +00:00
// SPDX-License-Identifier: MPL-2.0
//! The input devices of Asterinas.
#![no_std]
#![deny(unsafe_code)]
#![feature(fn_traits)]
extern crate alloc;
2024-01-18 14:58:36 +00:00
pub mod key;
use alloc::{collections::BTreeMap, string::String, sync::Arc, vec::Vec};
use core::{any::Any, fmt::Debug};
2023-12-25 03:12:25 +00:00
use aster_frame::sync::SpinLock;
use component::{init_component, ComponentInitError};
use key::{Key, KeyStatus};
use spin::Once;
2024-01-18 14:58:36 +00:00
#[derive(Debug, Clone, Copy)]
pub enum InputEvent {
KeyBoard(Key, KeyStatus),
}
2023-08-28 07:03:28 +00:00
pub trait InputDevice: Send + Sync + Any + Debug {
2024-01-18 14:58:36 +00:00
fn register_callbacks(&self, function: &'static (dyn Fn(InputEvent) + Send + Sync));
}
2023-08-28 07:03:28 +00:00
pub fn register_device(name: String, device: Arc<dyn InputDevice>) {
2023-11-20 12:37:51 +00:00
COMPONENT
.get()
.unwrap()
.input_device_table
.lock()
.insert(name, device);
}
2023-11-20 12:37:51 +00:00
pub fn get_device(str: &str) -> Option<Arc<dyn InputDevice>> {
COMPONENT
.get()
.unwrap()
.input_device_table
.lock()
.get(str)
.cloned()
}
2023-08-28 07:03:28 +00:00
pub fn all_devices() -> Vec<(String, Arc<dyn InputDevice>)> {
2023-11-20 12:37:51 +00:00
let input_devs = COMPONENT.get().unwrap().input_device_table.lock();
input_devs
.iter()
.map(|(name, device)| (name.clone(), device.clone()))
.collect()
}
2023-08-28 07:03:28 +00:00
static COMPONENT: Once<Component> = Once::new();
2023-08-28 07:03:28 +00:00
#[init_component]
fn component_init() -> Result<(), ComponentInitError> {
let a = Component::init()?;
COMPONENT.call_once(|| a);
Ok(())
}
#[derive(Debug)]
2023-08-28 07:03:28 +00:00
struct Component {
2023-11-20 12:37:51 +00:00
input_device_table: SpinLock<BTreeMap<String, Arc<dyn InputDevice>>>,
2023-08-28 07:03:28 +00:00
}
impl Component {
pub fn init() -> Result<Self, ComponentInitError> {
Ok(Self {
2023-11-20 12:37:51 +00:00
input_device_table: SpinLock::new(BTreeMap::new()),
2023-08-28 07:03:28 +00:00
})
}
}