From 2f55bfdfa3831b9f1987cc332746d2c7f29502c9 Mon Sep 17 00:00:00 2001 From: pjht Date: Fri, 20 Sep 2024 14:41:55 -0500 Subject: [PATCH] Add poll support --- Cargo.lock | 14 +++++++++ Cargo.toml | 1 + src/controller.rs | 13 +++++---- src/main.rs | 72 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 84 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3573946..7454c23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,9 @@ name = "bitflags" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +dependencies = [ + "serde", +] [[package]] name = "byteorder" @@ -87,6 +90,7 @@ checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" name = "file_rpc" version = "0.1.0" dependencies = [ + "bitflags", "parking_lot", "postcard", "serde", @@ -204,6 +208,7 @@ dependencies = [ "file_rpc", "parking_lot", "pc-keyboard", + "slab", "syslog_rpc", "x86_64", ] @@ -273,6 +278,15 @@ dependencies = [ "syn 2.0.77", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.13.2" diff --git a/Cargo.toml b/Cargo.toml index 781215c..5fef4f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,6 @@ devfs_rpc = { version = "0.1.0", path = "../devfs/devfs_rpc" } file_rpc = { version = "0.1.0", path = "../file_rpc" } parking_lot = "0.12.3" pc-keyboard = "0.7.0" +slab = "0.4.9" syslog_rpc = { version = "0.1.0", path = "../syslog/syslog_rpc" } x86_64 = "0.15.1" diff --git a/src/controller.rs b/src/controller.rs index 5a39039..ab7d78f 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -191,8 +191,6 @@ impl Ps2Controller { } } - - pub fn initialize(&mut self) -> Result<(), Ps2InitFailure> { let mut ports = self.ports.lock(); @@ -276,18 +274,21 @@ impl Ps2Controller { } pub fn write_port1(&self, data: u8) { - self.ports.lock().send_command(Ps2ControllerCommand::WritePort1Input(data)); + self.ports + .lock() + .send_command(Ps2ControllerCommand::WritePort1Input(data)); } - pub fn write_port2(&self, data: u8) { - self.ports.lock().send_command(Ps2ControllerCommand::WritePort2Input(data)); + self.ports + .lock() + .send_command(Ps2ControllerCommand::WritePort2Input(data)); } pub fn handle_interrupt(&self) { let mut ports = self.ports.lock(); if ports.get_status().contains(Ps2Status::OutputBufFull) { - let data = unsafe {ports.data_port.read()}; + let data = unsafe { ports.data_port.read() }; self.port1_output_bytes.lock().push_back(data); } } diff --git a/src/main.rs b/src/main.rs index 7dc30c6..f30c4f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,19 @@ mod controller; -use std::{collections::VecDeque, os::mikros::{ipc, syscalls}, sync::OnceLock}; +use std::{ + collections::VecDeque, + os::mikros::{ipc, syscalls}, + sync::OnceLock, + usize, +}; use controller::Ps2Controller; +use file_rpc::PollEvents; use parking_lot::Mutex; -use pc_keyboard::{layouts::Us104Key, EventDecoder, HandleControl, KeyCode, KeyEvent, ScancodeSet, ScancodeSet2}; +use pc_keyboard::{ + layouts::Us104Key, EventDecoder, HandleControl, KeyCode, KeyEvent, ScancodeSet, ScancodeSet2, +}; +use slab::Slab; static CONTROLLER: OnceLock = OnceLock::new(); @@ -18,7 +27,9 @@ impl dev_driver_rpc::Server for DevServ { } } -struct FileServ; +struct FileServ { + polls: Mutex>, +} impl file_rpc::Server for FileServ { fn read( @@ -33,14 +44,14 @@ impl file_rpc::Server for FileServ { while remaining_len > 0 { let mut keypress_buf = KEYPRESS_BUF.lock(); let Some(c) = keypress_buf.pop_front() else { - break + break; }; let str = c.encode_utf8(&mut encode_buf); let bytes = str.as_bytes(); if bytes.len() > remaining_len { keypress_buf.push_front(c); break; - } + } buf.extend_from_slice(bytes); remaining_len -= bytes.len(); } @@ -56,14 +67,49 @@ impl file_rpc::Server for FileServ { fn size(&self, _fd: u64) -> Option { None } - + fn dup(&self, fd: u64) -> Option { Some(fd) } + + fn register_poll(&self, _fd: u64, events: PollEvents) -> u64 { + self.polls.lock().insert(events) as u64 + } + + fn poll(&self, poll_id: u64) -> Option { + let poll_id = poll_id as usize; + let req_events = *(self.polls.lock().get(poll_id)?); + if !req_events.contains(PollEvents::POLLIN) { + return None; + } + loop { + if !KEYPRESS_BUF.lock().is_empty() { + if !self.polls.lock().contains(poll_id) { + return None; + } else { + self.polls.lock().remove(poll_id); + return Some(PollEvents::POLLIN); + } + } else { + ipc::process_messages(); + process_keyboard_data(); + if !self.polls.lock().contains(poll_id) { + return None; + } + } + } + } + + fn cancel_poll(&self, poll_id: u64) { + self.polls.lock().remove(poll_id as usize); + } } static SCANDODE_DECODER: Mutex = Mutex::new(ScancodeSet2::new()); -static EVENT_DECODER: Mutex> = Mutex::new(EventDecoder::new(Us104Key, HandleControl::MapLettersToUnicode)); +static EVENT_DECODER: Mutex> = Mutex::new(EventDecoder::new( + Us104Key, + HandleControl::MapLettersToUnicode, +)); fn process_keyboard_data() { let mut scancode_decoder = SCANDODE_DECODER.lock(); @@ -150,7 +196,9 @@ fn main() { } }); - syslog_client.send_text_message("ps2", "Setting keyboard to scan code set 2").unwrap(); + syslog_client + .send_text_message("ps2", "Setting keyboard to scan code set 2") + .unwrap(); controller.write_port1(0xF5); loop { @@ -190,10 +238,14 @@ fn main() { break; } - syslog_client.send_text_message("ps2", "Set keyboard to scan code set 2").unwrap(); + syslog_client + .send_text_message("ps2", "Set keyboard to scan code set 2") + .unwrap(); dev_driver_rpc::register_server(Box::new(DevServ)); - file_rpc::register_server(Box::new(FileServ)); + file_rpc::register_server(Box::new(FileServ { + polls: Mutex::new(Slab::new()), + })); let devfs_pid; loop { if let Some(pid) = syscalls::try_get_registered(1) {