From 2161771b093e7bb489b1e9f32bdf78dccc782989 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 5 Jun 2022 01:45:25 +0800 Subject: [PATCH] fix keyboard listener on dynamic notifications --- services/modals/src/lib.rs | 68 +++++++++++++++++++++---------------- services/modals/src/main.rs | 3 +- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index c00eb93df..e5ba75d1a 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -410,36 +410,6 @@ impl Modals { self.unlock(); Ok(()) } - /// If a dynamic notification is active, this will block and return only if one of two - /// conditions are met: - /// 1. a key is pressed, in which case, the `Some(char)` is the key pressed. If there is a - /// "fat finger" event, only the first character is reported. - /// 2. the dynamic notification is closed, in which case a `None` is reported. - pub fn dynamic_notification_blocking_listener(&self) -> Result, xous::Error> { - match send_message( - self.conn, - Message::new_blocking_scalar( - Opcode::ListenToDynamicNotification.to_usize().unwrap(), - self.token[0] as usize, - self.token[1] as usize, - self.token[2] as usize, - self.token[3] as usize, - ), - ).expect("couldn't listen") { - xous::Result::Scalar2(is_some, code) => { - if is_some == 1 { - let c = char::from_u32(code as u32).unwrap_or('\u{0000}'); - Ok(Some(c)) - } else if is_some == 2 { - log::warn!("Attempt to listen, but did not have the mutex. Aborted."); - Ok(None) - } else { - Ok(None) - } - } - _ => Err(xous::Error::InternalError) - } - } /// Blocks until we have a lock on the modals server fn lock(&self) { @@ -471,6 +441,10 @@ impl Modals { fn unlock(&self) { self.have_lock.set(false); } + pub fn conn(&self) -> CID {self.conn} + /// Don't leak this token outside of your server, otherwise, another server can pretend to be you and + /// steal your modal information! + pub fn token(&self) -> [u32; 4] {self.token} } use core::sync::atomic::{AtomicU32, Ordering}; @@ -489,3 +463,37 @@ impl Drop for Modals { // de-allocate those items here. They don't need a reference count because they are object-specific } } + +/// If a dynamic notification is active, this will block and return only if one of two +/// conditions are met: +/// 1. a key is pressed, in which case, the `Some(char)` is the key pressed. If there is a +/// "fat finger" event, only the first character is reported. +/// 2. the dynamic notification is closed, in which case a `None` is reported. +/// +/// This function is "broken out" so that it can be called from a thread without having +/// to wrap a mutex around the primary Modals structure. +pub fn dynamic_notification_blocking_listener(token: [u32; 4], conn: CID) -> Result, xous::Error> { + match send_message( + conn, + Message::new_blocking_scalar( + Opcode::ListenToDynamicNotification.to_usize().unwrap(), + token[0] as usize, + token[1] as usize, + token[2] as usize, + token[3] as usize, + ), + ).expect("couldn't listen") { + xous::Result::Scalar2(is_some, code) => { + if is_some == 1 { + let c = char::from_u32(code as u32).unwrap_or('\u{0000}'); + Ok(Some(c)) + } else if is_some == 2 { + log::warn!("Attempt to listen, but did not have the mutex. Aborted."); + Ok(None) + } else { + Ok(None) + } + } + _ => Err(xous::Error::InternalError) + } +} diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index b30ad51d2..5fdf4d151 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -536,7 +536,7 @@ fn main() -> ! { renderer_cid, Opcode::HandleDynamicNotificationKeyhit.to_u32().unwrap(), ); - gutter.set_manual_dismiss(false); + gutter.set_manual_dismiss(true); renderer_modal.modify( Some(ActionType::Notification(gutter)), Some(&top_text), @@ -599,6 +599,7 @@ fn main() -> ! { token_lock = next_lock(&mut work_queue); }, Some(Opcode::HandleDynamicNotificationKeyhit) => msg_scalar_unpack!(msg, k, _, _, _, { + log::info!("Dynamic kbd hit: {}({})", k, char::from_u32(k as u32).unwrap_or(' ')); if let Some(sender) = dynamic_notification_listener.take() { xous::return_scalar2(sender, 1, k).unwrap(); }