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 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<Box<Thread>>,
handler: Rc<RefCell<dyn KeyboardHandler>>,
}
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()
.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<T>(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)
}

View File

@ -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()