From 71c6003905e9c0f04e95a0a3f8092146f684fb35 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Sat, 25 Jan 2025 23:16:27 -0800 Subject: [PATCH] Move teton to use new thread spawning. --- rust/lib/voyageurs/src/listener.rs | 100 +++++++++-------------------- rust/sys/teton/src/main.rs | 9 +-- 2 files changed, 34 insertions(+), 75 deletions(-) diff --git a/rust/lib/voyageurs/src/listener.rs b/rust/lib/voyageurs/src/listener.rs index 3f82095..1912b88 100644 --- a/rust/lib/voyageurs/src/listener.rs +++ b/rust/lib/voyageurs/src/listener.rs @@ -1,12 +1,7 @@ -use core::cell::RefCell; - -use alloc::boxed::Box; -use alloc::rc::Rc; use alloc::string::ToString; use mammoth::cap::Capability; use mammoth::port::PortServer; -use mammoth::thread::Thread; -use mammoth::zion::ZError; +use mammoth::thread; #[repr(u8)] #[allow(dead_code)] @@ -198,67 +193,34 @@ pub trait KeyboardHandler { fn handle_char(&mut self, c: char); } -pub struct KeyboardListener { - listen_port: PortServer, - listen_thread: Option>, - handler: Rc>, -} - -impl KeyboardListener { - pub fn new(handler: Rc>) -> Result, ZError> { - let mut listnr = Box::new(Self { - listen_port: PortServer::new()?, - listen_thread: None, - handler, - }); - - let voyageur_endpoint = yellowstone_yunq::from_init_endpoint() - .get_endpoint(&yellowstone_yunq::GetEndpointRequest { - endpoint_name: "voyageurs".to_string(), - })? - .endpoint; - - let mut voyageur_client = crate::VoyageursClient::new(Capability::take(voyageur_endpoint)); - - voyageur_client.register_keyboard_listener(&crate::KeyboardListener { - port_capability: listnr.listen_port.create_client_cap()?, - })?; - - let thread_entry = |self_raw| { - let listener = unsafe { - (self_raw as *mut KeyboardListener) - .as_mut() - .expect("Failed to convert to keyboard listener") - }; - listener.listen_loop(); - }; - - listnr.listen_thread = Some(Thread::spawn( - thread_entry, - &*listnr as *const Self as *const core::ffi::c_void, - )?); - - Ok(listnr) - } - - fn listen_loop(&mut self) { - loop { - let scancode = self - .listen_port - .recv_u16() - .expect("Failed to recieve scancode"); - - let keycode = Keycode::from_scancode(scancode); - let modifiers = Modifiers::from_scancode(scancode); - - self.handler - .as_ref() - .borrow_mut() - .handle_char(into_char(keycode, modifiers)) - } - } - - pub fn join(&self) -> Result<(), ZError> { - self.listen_thread.as_ref().unwrap().join() - } +pub fn spawn_keyboard_listener(mut handler: T) -> thread::JoinHandle +where + T: KeyboardHandler + Send + 'static, +{ + let listen_port = PortServer::new().unwrap(); + let voyageur_endpoint = yellowstone_yunq::from_init_endpoint() + .get_endpoint(&yellowstone_yunq::GetEndpointRequest { + endpoint_name: "voyageurs".to_string(), + }) + .unwrap() + .endpoint; + + let mut voyageur_client = crate::VoyageursClient::new(Capability::take(voyageur_endpoint)); + + voyageur_client + .register_keyboard_listener(&crate::KeyboardListener { + port_capability: listen_port.create_client_cap().unwrap(), + }) + .unwrap(); + + let listen_thread = move || loop { + let scancode = listen_port.recv_u16().expect("Failed to recieve scancode"); + + let keycode = Keycode::from_scancode(scancode); + let modifiers = Modifiers::from_scancode(scancode); + + handler.handle_char(into_char(keycode, modifiers)) + }; + + thread::spawn(listen_thread) } diff --git a/rust/sys/teton/src/main.rs b/rust/sys/teton/src/main.rs index 5cec4fb..982beb7 100644 --- a/rust/sys/teton/src/main.rs +++ b/rust/sys/teton/src/main.rs @@ -8,11 +8,8 @@ mod framebuffer; mod psf; mod terminal; -use core::cell::RefCell; - -use alloc::rc::Rc; use mammoth::{debug, define_entry, zion::z_err_t}; -use voyageurs::listener::KeyboardListener; +use voyageurs::listener; define_entry!(); @@ -39,9 +36,9 @@ extern "C" fn main() -> z_err_t { let psf = psf::Psf::new("/default8x16.psfu").expect("Failed to open font file."); let console = console::Console::new(framebuffer, psf); - let terminal = Rc::new(RefCell::new(terminal::Terminal::new(console))); + let terminal = terminal::Terminal::new(console); - let kb_listener = KeyboardListener::new(terminal).expect("Failed to create keyboard listener"); + let kb_listener = listener::spawn_keyboard_listener(terminal); kb_listener .join()