From 79dbd2968a5c8e42031fa934ac80c65f2e8400b5 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 27 Mar 2020 15:46:26 +0100 Subject: [PATCH] Create a ScancodeStream based on the SCANCODE_QUEUE --- Cargo.lock | 30 ++++++++++++++++++++++++++++++ Cargo.toml | 5 +++++ src/task/keyboard.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 23c1e596..c4225ccf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,6 +25,7 @@ dependencies = [ "bootloader", "conquer-once", "crossbeam-queue", + "futures-util", "lazy_static", "linked_list_allocator", "pc-keyboard", @@ -88,6 +89,29 @@ dependencies = [ "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]] name = "lazy_static" version = "1.4.0" @@ -130,6 +154,12 @@ dependencies = [ "cpuio", ] +[[package]] +name = "pin-utils" +version = "0.1.0-alpha.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" + [[package]] name = "scopeguard" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 39198cd9..fa23667f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,11 @@ features = ["alloc"] version = "0.2.0" default-features = false +[dependencies.futures-util] +version = "0.3.4" +default-features = false +features = ["alloc"] + [package.metadata.bootimage] test-args = [ "-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio", diff --git a/src/task/keyboard.rs b/src/task/keyboard.rs index f5910454..946acf35 100644 --- a/src/task/keyboard.rs +++ b/src/task/keyboard.rs @@ -1,6 +1,11 @@ use crate::println; use conquer_once::spin::OnceCell; +use core::{ + pin::Pin, + task::{Context, Poll}, +}; use crossbeam_queue::ArrayQueue; +use futures_util::stream::Stream; static SCANCODE_QUEUE: OnceCell> = OnceCell::uninit(); @@ -16,3 +21,28 @@ pub(crate) fn add_scancode(scancode: u8) { println!("WARNING: scancode queue uninitialized"); } } + +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>, cx: &mut Context) -> Poll> { + let queue = SCANCODE_QUEUE.try_get().expect("not initialized"); + match queue.pop() { + Ok(scancode) => Poll::Ready(Some(scancode)), + Err(crossbeam_queue::PopError) => Poll::Pending, + } + } +}