Compare commits

...

2 Commits

Author SHA1 Message Date
Philipp Oppermann
ca8cd46863 Update 2020-03-22 11:58:16 +01:00
Philipp Oppermann
816f8746fb Implement a ScancodeStream type 2020-03-20 14:14:45 +01:00
5 changed files with 86 additions and 1 deletions

30
Cargo.lock generated
View File

@@ -25,6 +25,7 @@ dependencies = [
"bootloader", "bootloader",
"conquer-once", "conquer-once",
"crossbeam-queue", "crossbeam-queue",
"futures-util",
"lazy_static", "lazy_static",
"linked_list_allocator", "linked_list_allocator",
"pc-keyboard", "pc-keyboard",
@@ -86,6 +87,29 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "futures-core"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f25592f769825e89b92358db00d26f965761e094951ac44d3663ef25b7ac464a"
[[package]]
name = "futures-task"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b0a34e53cf6cdcd0178aa573aed466b646eb3db769570841fda0c7ede375a27"
[[package]]
name = "futures-util"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22766cf25d64306bedf0384da004d05c9974ab104fcc4528f1236181c18004c5"
dependencies = [
"futures-core",
"futures-task",
"pin-utils",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@@ -128,6 +152,12 @@ dependencies = [
"cpuio", "cpuio",
] ]
[[package]]
name = "pin-utils"
version = "0.1.0-alpha.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"

View File

@@ -35,6 +35,11 @@ features = ["alloc"]
version = "0.2.0" version = "0.2.0"
default-features = false default-features = false
[dependencies.futures-util]
version = "0.3.4"
default-features = false
features = ["alloc", "async-await"]
[package.metadata.bootimage] [package.metadata.bootimage]
test-args = [ test-args = [
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio", "-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",

View File

@@ -7,6 +7,7 @@
#![feature(alloc_layout_extra)] #![feature(alloc_layout_extra)]
#![feature(const_in_array_repeat_expressions)] #![feature(const_in_array_repeat_expressions)]
#![feature(wake_trait)] #![feature(wake_trait)]
#![feature(async_closure)]
#![test_runner(crate::test_runner)] #![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"] #![reexport_test_harness_main = "test_main"]

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::{simple_executor::SimpleExecutor, Task, keyboard};
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,4 +1,52 @@
use conquer_once::spin::OnceCell; use conquer_once::spin::OnceCell;
use crossbeam_queue::ArrayQueue; use crossbeam_queue::ArrayQueue;
use futures_util::stream::{Stream, StreamExt};
use core::{pin::Pin, task::{Context, Poll}};
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
use crate::print;
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit(); static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
pub struct ScancodeStream {
_private: (),
}
impl ScancodeStream {
pub fn new() -> Self {
SCANCODE_QUEUE.try_init_once(|| ArrayQueue::new(100))
.expect("ScancodeStream::new should only be called once");
ScancodeStream {
_private: (),
}
}
}
impl Stream for ScancodeStream {
type Item = u8;
fn poll_next(self: Pin<&mut Self>, context: &mut Context) -> Poll<Option<u8>> {
let queue = SCANCODE_QUEUE
.try_get()
.expect("scancode queue not initialized");
match queue.pop() {
Ok(scancode) => Poll::Ready(Some(scancode)),
Err(crossbeam_queue::PopError) => Poll::Pending,
}
}
}
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),
}
}
}
}
}