mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-22 01:16:26 +04:00
Decode input events in inputdev_event.rs
This commit is contained in:
parent
b77f2d6999
commit
f4b856741b
@ -13,10 +13,8 @@ buddy_system_allocator = "0.6"
|
||||
bitflags = "1.2.1"
|
||||
xmas-elf = "0.7.0"
|
||||
volatile = "0.3"
|
||||
#virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "4ee80e5" }
|
||||
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "70b5850" }
|
||||
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "4ee80e5" }
|
||||
easy-fs = { path = "../easy-fs" }
|
||||
#virtio-input-decoder = "0.1.4"
|
||||
embedded-graphics = "0.7.1"
|
||||
tinybmp = "0.3.1"
|
||||
|
||||
|
@ -16,41 +16,20 @@ struct VirtIOInputInner {
|
||||
|
||||
struct VirtIOInputWrapper {
|
||||
inner: UPIntrFreeCell<VirtIOInputInner>,
|
||||
//condvars: BTreeMap<u16, Condvar>,
|
||||
//condvar: Arc::<Condvar> ,
|
||||
condvar: Condvar,
|
||||
}
|
||||
|
||||
pub trait InputDevice: Send + Sync + Any {
|
||||
fn read_event(&self) -> u64;
|
||||
fn handle_irq(&self);
|
||||
// fn events(&self) -> &VecDeque<u64>;
|
||||
fn is_empty(&self) -> bool;
|
||||
}
|
||||
|
||||
lazy_static::lazy_static!(
|
||||
pub static ref KEYBOARD_DEVICE: Arc<dyn InputDevice> = Arc::new(VirtIOInputWrapper::new(VIRTIO5));
|
||||
pub static ref MOUSE_DEVICE: Arc<dyn InputDevice> = Arc::new(VirtIOInputWrapper::new(VIRTIO6));
|
||||
// pub static ref INPUT_CONDVAR: Arc::<Condvar> = Arc::new(Condvar::new());
|
||||
);
|
||||
|
||||
// from virtio-drivers/src/input.rs
|
||||
//const QUEUE_SIZE: u16 = 32;
|
||||
// pub fn read_input_event() -> u64 {
|
||||
// loop {
|
||||
|
||||
// //let mut inner = self.inner.exclusive_access();
|
||||
// let kb=KEYBOARD_DEVICE.clone();
|
||||
// let evs = kb.events();
|
||||
// if let Some(event) = evs.pop_front() {
|
||||
// return event;
|
||||
// } else {
|
||||
// let task_cx_ptr = INPUT_CONDVAR.clone().wait_no_sched();
|
||||
// drop(inner);
|
||||
// schedule(task_cx_ptr);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
impl VirtIOInputWrapper {
|
||||
pub fn new(addr: usize) -> Self {
|
||||
let inner = VirtIOInputInner {
|
||||
@ -59,17 +38,8 @@ impl VirtIOInputWrapper {
|
||||
},
|
||||
events: VecDeque::new(),
|
||||
};
|
||||
|
||||
// let mut condvars = BTreeMap::new();
|
||||
// let channels = QUEUE_SIZE;
|
||||
// for i in 0..channels {
|
||||
// let condvar = Condvar::new();
|
||||
// condvars.insert(i, condvar);
|
||||
// }
|
||||
|
||||
Self {
|
||||
inner: unsafe { UPIntrFreeCell::new(inner) },
|
||||
//condvar: INPUT_CONDVAR.clone(),
|
||||
condvar: Condvar::new(),
|
||||
}
|
||||
}
|
||||
@ -93,29 +63,20 @@ impl InputDevice for VirtIOInputWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
// fn events(&self) -> &VecDeque<u64> {
|
||||
// &self.inner.exclusive_access().events
|
||||
// }
|
||||
|
||||
fn handle_irq(&self) {
|
||||
let mut count = 0;
|
||||
let mut result = 0;
|
||||
let mut key = 0;
|
||||
self.inner.exclusive_session(|inner| {
|
||||
inner.virtio_input.ack_interrupt();
|
||||
while let Some((token, event)) = inner.virtio_input.pop_pending_event() {
|
||||
while let Some(event) = inner.virtio_input.pop_pending_event() {
|
||||
count += 1;
|
||||
key = token;
|
||||
result = (event.event_type as u64) << 48
|
||||
| (event.code as u64) << 32
|
||||
| (event.value) as u64;
|
||||
inner.events.push_back(result);
|
||||
// for test
|
||||
//println!("[KERN] inputdev_handle_irq: event: {:x}", result);
|
||||
}
|
||||
});
|
||||
if count > 0 {
|
||||
//self.condvars.get(&key).unwrap().signal();
|
||||
self.condvar.signal();
|
||||
};
|
||||
}
|
||||
|
@ -29,7 +29,6 @@ mod trap;
|
||||
|
||||
use crate::drivers::chardev::CharDevice;
|
||||
use crate::drivers::chardev::UART;
|
||||
//use syscall::create_desktop; //for test
|
||||
|
||||
core::arch::global_asm!(include_str!("entry.asm"));
|
||||
|
||||
@ -59,7 +58,6 @@ pub fn rust_main() -> ! {
|
||||
UART.init();
|
||||
println!("KERN: init gpu");
|
||||
let _gpu = GPU_DEVICE.clone();
|
||||
//let _input_condvar = INPUT_CONDVAR.clone();
|
||||
println!("KERN: init keyboard");
|
||||
let _keyboard = KEYBOARD_DEVICE.clone();
|
||||
println!("KERN: init mouse");
|
||||
|
@ -12,5 +12,7 @@ bitflags = "1.2.1"
|
||||
riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
|
||||
embedded-graphics = "0.7.1"
|
||||
oorandom ="11"
|
||||
virtio-input-decoder = "0.1.4"
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
@ -48,7 +48,6 @@ impl DrawingBoard {
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
// let fb_ptr = framebuffer() as *mut u8;
|
||||
let mut board = DrawingBoard::new();
|
||||
let _ = board.disp.clear(Rgb888::BLACK).unwrap();
|
||||
for i in 0..20 {
|
||||
|
@ -1,7 +1,6 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
use user_lib::{VIRTGPU_XRES, VIRTGPU_YRES, Display};
|
||||
|
@ -1,7 +1,6 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use user_lib::{event_get};
|
||||
use user_lib::{event_get, DecodeType, Key, KeyType};
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
@ -9,13 +9,17 @@ extern crate user_lib;
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
println!("Input device event test");
|
||||
let mut event=0;
|
||||
for _ in 0..3 {
|
||||
while event==0 {
|
||||
event = event_get();
|
||||
}
|
||||
println!("event: {:?}", event);
|
||||
loop {
|
||||
if let Some(event) = event_get() {
|
||||
if let Some(decoder_type) = event.decode() {
|
||||
println!("{:?}", decoder_type);
|
||||
if let DecodeType::Key(key, keytype) = decoder_type {
|
||||
if key == Key::Enter && keytype == KeyType::Press {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
0
|
||||
}
|
@ -2,6 +2,8 @@ use super::*;
|
||||
use embedded_graphics::prelude::{RgbColor, Size};
|
||||
use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions};
|
||||
use embedded_graphics::pixelcolor::Rgb888;
|
||||
use virtio_input_decoder::Decoder;
|
||||
pub use virtio_input_decoder::{DecodeType, Key, KeyType, Mouse};
|
||||
|
||||
pub const VIRTGPU_XRES: u32 = 1280;
|
||||
pub const VIRTGPU_YRES: u32 = 800;
|
||||
@ -14,17 +16,6 @@ pub fn framebuffer_flush() -> isize {
|
||||
sys_framebuffer_flush()
|
||||
}
|
||||
|
||||
pub fn event_get() -> isize {
|
||||
sys_event_get()
|
||||
}
|
||||
|
||||
pub fn key_pressed() -> bool {
|
||||
if sys_key_pressed() == 1 {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
pub struct Display {
|
||||
pub size: Size,
|
||||
pub fb: &'static mut [u8],
|
||||
@ -76,3 +67,52 @@ impl DrawTarget for Display {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn event_get() -> Option<InputEvent> {
|
||||
let raw_value = sys_event_get();
|
||||
if raw_value == 0 {
|
||||
None
|
||||
} else {
|
||||
Some((raw_value as u64).into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn key_pressed() -> bool {
|
||||
if sys_key_pressed() == 1 {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct InputEvent {
|
||||
pub event_type: u16,
|
||||
pub code: u16,
|
||||
pub value: u32,
|
||||
}
|
||||
|
||||
impl From<u64> for InputEvent {
|
||||
fn from(mut v: u64) -> Self {
|
||||
let value = v as u32;
|
||||
v >>= 32;
|
||||
let code = v as u16;
|
||||
v >>= 16;
|
||||
let event_type = v as u16;
|
||||
Self {
|
||||
event_type,
|
||||
code,
|
||||
value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl InputEvent {
|
||||
pub fn decode(&self) -> Option<DecodeType> {
|
||||
Decoder::decode(
|
||||
self.event_type as usize,
|
||||
self.code as usize,
|
||||
self.value as usize,
|
||||
).ok()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user