Add a keyboard task that prints keypresses

This commit is contained in:
Philipp Oppermann
2020-03-22 12:45:24 +01:00
parent a6273614e4
commit 90abd5c8c5
2 changed files with 21 additions and 1 deletions

View File

@@ -15,7 +15,7 @@ entry_point!(kernel_main);
fn kernel_main(boot_info: &'static BootInfo) -> ! { fn kernel_main(boot_info: &'static BootInfo) -> ! {
use blog_os::allocator; use blog_os::allocator;
use blog_os::memory::{self, BootInfoFrameAllocator}; use blog_os::memory::{self, BootInfoFrameAllocator};
use blog_os::task::{simple_executor::SimpleExecutor, Task}; use blog_os::task::{keyboard, simple_executor::SimpleExecutor, Task};
use x86_64::VirtAddr; use x86_64::VirtAddr;
println!("Hello World{}", "!"); println!("Hello World{}", "!");
@@ -29,6 +29,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
let mut executor = SimpleExecutor::new(); let mut executor = SimpleExecutor::new();
executor.spawn(Task::new(example_task())); executor.spawn(Task::new(example_task()));
executor.spawn(Task::new(keyboard::print_keypresses()));
executor.run(); executor.run();
#[cfg(test)] #[cfg(test)]

View File

@@ -1,3 +1,4 @@
use crate::print;
use crate::println; use crate::println;
use conquer_once::spin::OnceCell; use conquer_once::spin::OnceCell;
use core::{ use core::{
@@ -5,7 +6,9 @@ use core::{
task::{Context, Poll}, task::{Context, Poll},
}; };
use crossbeam_queue::ArrayQueue; use crossbeam_queue::ArrayQueue;
use futures_util::stream::StreamExt;
use futures_util::{stream::Stream, task::AtomicWaker}; use futures_util::{stream::Stream, task::AtomicWaker};
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit(); static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
static WAKER: AtomicWaker = AtomicWaker::new(); static WAKER: AtomicWaker = AtomicWaker::new();
@@ -58,3 +61,19 @@ impl Stream for ScancodeStream {
} }
} }
} }
pub async fn print_keypresses() {
let mut scancodes = ScancodeStream::new();
let mut keyboard = Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore);
while let Some(scancode) = scancodes.next().await {
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
if let Some(key) = keyboard.process_keyevent(key_event) {
match key {
DecodedKey::Unicode(character) => print!("{}", character),
DecodedKey::RawKey(key) => print!("{:?}", key),
}
}
}
}
}