Move teton to use new thread spawning.

This commit is contained in:
Drew Galbraith 2025-01-25 23:16:27 -08:00
parent 9da38d608a
commit 71c6003905
2 changed files with 34 additions and 75 deletions

View File

@ -1,12 +1,7 @@
use core::cell::RefCell;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::ToString; use alloc::string::ToString;
use mammoth::cap::Capability; use mammoth::cap::Capability;
use mammoth::port::PortServer; use mammoth::port::PortServer;
use mammoth::thread::Thread; use mammoth::thread;
use mammoth::zion::ZError;
#[repr(u8)] #[repr(u8)]
#[allow(dead_code)] #[allow(dead_code)]
@ -198,67 +193,34 @@ pub trait KeyboardHandler {
fn handle_char(&mut self, c: char); fn handle_char(&mut self, c: char);
} }
pub struct KeyboardListener { pub fn spawn_keyboard_listener<T>(mut handler: T) -> thread::JoinHandle
listen_port: PortServer, where
listen_thread: Option<Box<Thread>>, T: KeyboardHandler + Send + 'static,
handler: Rc<RefCell<dyn KeyboardHandler>>, {
} let listen_port = PortServer::new().unwrap();
impl KeyboardListener {
pub fn new(handler: Rc<RefCell<dyn KeyboardHandler>>) -> Result<Box<Self>, ZError> {
let mut listnr = Box::new(Self {
listen_port: PortServer::new()?,
listen_thread: None,
handler,
});
let voyageur_endpoint = yellowstone_yunq::from_init_endpoint() let voyageur_endpoint = yellowstone_yunq::from_init_endpoint()
.get_endpoint(&yellowstone_yunq::GetEndpointRequest { .get_endpoint(&yellowstone_yunq::GetEndpointRequest {
endpoint_name: "voyageurs".to_string(), endpoint_name: "voyageurs".to_string(),
})? })
.unwrap()
.endpoint; .endpoint;
let mut voyageur_client = crate::VoyageursClient::new(Capability::take(voyageur_endpoint)); let mut voyageur_client = crate::VoyageursClient::new(Capability::take(voyageur_endpoint));
voyageur_client.register_keyboard_listener(&crate::KeyboardListener { voyageur_client
port_capability: listnr.listen_port.create_client_cap()?, .register_keyboard_listener(&crate::KeyboardListener {
})?; port_capability: listen_port.create_client_cap().unwrap(),
})
.unwrap();
let thread_entry = |self_raw| { let listen_thread = move || loop {
let listener = unsafe { let scancode = listen_port.recv_u16().expect("Failed to recieve scancode");
(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 keycode = Keycode::from_scancode(scancode);
let modifiers = Modifiers::from_scancode(scancode); let modifiers = Modifiers::from_scancode(scancode);
self.handler handler.handle_char(into_char(keycode, modifiers))
.as_ref() };
.borrow_mut()
.handle_char(into_char(keycode, modifiers))
}
}
pub fn join(&self) -> Result<(), ZError> { thread::spawn(listen_thread)
self.listen_thread.as_ref().unwrap().join()
}
} }

View File

@ -8,11 +8,8 @@ mod framebuffer;
mod psf; mod psf;
mod terminal; mod terminal;
use core::cell::RefCell;
use alloc::rc::Rc;
use mammoth::{debug, define_entry, zion::z_err_t}; use mammoth::{debug, define_entry, zion::z_err_t};
use voyageurs::listener::KeyboardListener; use voyageurs::listener;
define_entry!(); 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 psf = psf::Psf::new("/default8x16.psfu").expect("Failed to open font file.");
let console = console::Console::new(framebuffer, psf); 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 kb_listener
.join() .join()