diff --git a/os/Cargo.toml b/os/Cargo.toml index ded2765..d8f4216 100644 --- a/os/Cargo.toml +++ b/os/Cargo.toml @@ -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" diff --git a/os/src/drivers/input/mod.rs b/os/src/drivers/input/mod.rs index d7c40fc..a9f5f0b 100644 --- a/os/src/drivers/input/mod.rs +++ b/os/src/drivers/input/mod.rs @@ -16,41 +16,20 @@ struct VirtIOInputInner { struct VirtIOInputWrapper { inner: UPIntrFreeCell, - //condvars: BTreeMap, - //condvar: Arc:: , condvar: Condvar, } pub trait InputDevice: Send + Sync + Any { fn read_event(&self) -> u64; fn handle_irq(&self); - // fn events(&self) -> &VecDeque; fn is_empty(&self) -> bool; } lazy_static::lazy_static!( pub static ref KEYBOARD_DEVICE: Arc = Arc::new(VirtIOInputWrapper::new(VIRTIO5)); pub static ref MOUSE_DEVICE: Arc = Arc::new(VirtIOInputWrapper::new(VIRTIO6)); - // pub static ref INPUT_CONDVAR: Arc:: = 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 { - // &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(); }; } diff --git a/os/src/main.rs b/os/src/main.rs index 4819ea6..1103c57 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -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"); diff --git a/user/Cargo.toml b/user/Cargo.toml index 35e06d5..7b1233e 100644 --- a/user/Cargo.toml +++ b/user/Cargo.toml @@ -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 \ No newline at end of file diff --git a/user/src/bin/gui_rect.rs b/user/src/bin/gui_rect.rs index 39f8173..c8696be 100644 --- a/user/src/bin/gui_rect.rs +++ b/user/src/bin/gui_rect.rs @@ -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 { diff --git a/user/src/bin/gui_simple.rs b/user/src/bin/gui_simple.rs index e55232d..ccf5b89 100644 --- a/user/src/bin/gui_simple.rs +++ b/user/src/bin/gui_simple.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] -#[macro_use] extern crate user_lib; use user_lib::{VIRTGPU_XRES, VIRTGPU_YRES, Display}; diff --git a/user/src/bin/gui_snake.rs b/user/src/bin/gui_snake.rs index b28fd9c..080c472 100644 --- a/user/src/bin/gui_snake.rs +++ b/user/src/bin/gui_snake.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] -#[macro_use] extern crate user_lib; extern crate alloc; diff --git a/user/src/bin/inputdev_event.rs b/user/src/bin/inputdev_event.rs index 1e5b5a1..5b820d8 100644 --- a/user/src/bin/inputdev_event.rs +++ b/user/src/bin/inputdev_event.rs @@ -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 } \ No newline at end of file diff --git a/user/src/io.rs b/user/src/io.rs index 6cc2e66..baaa338 100644 --- a/user/src/io.rs +++ b/user/src/io.rs @@ -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 { + 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 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 { + Decoder::decode( + self.event_type as usize, + self.code as usize, + self.value as usize, + ).ok() + } +} \ No newline at end of file