From da06d95acdca5c566b95568b80526f15c5620838 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 1 Jun 2022 22:31:44 +0800 Subject: [PATCH] add a deferred listener for keyboard events from a dynamic notification --- services/modals/src/api.rs | 3 +++ services/modals/src/lib.rs | 30 ++++++++++++++++++++++++++++++ services/modals/src/main.rs | 23 +++++++++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/services/modals/src/api.rs b/services/modals/src/api.rs index 4289aead3..8ca853a71 100644 --- a/services/modals/src/api.rs +++ b/services/modals/src/api.rs @@ -74,6 +74,8 @@ pub(crate) enum Opcode { Notification, /// dynamic notification - a simple non-interactive notification that allows its text to be dynamically updated DynamicNotification, + /// listen to dynamic notification - a blocking call, meant to be called from a separate thread from the control loop + ListenToDynamicNotification, /// ask a question, get a free-form answer back PromptWithTextResponse, @@ -116,6 +118,7 @@ pub(crate) enum Opcode { DoUpdateDynamicNotification, DoCloseDynamicNotification, + HandleDynamicNotificationKeyhit, ModalRedraw, ModalKeypress, diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 5cc39844f..c00eb93df 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -410,6 +410,36 @@ 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) { diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index 0cc8ac532..b30ad51d2 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -139,6 +139,8 @@ fn main() -> ! { ]; let mut work_queue = Vec::<(xous::MessageSender, [u32; 4])>::new(); + let mut dynamic_notification_listener: Option = None; + loop { let mut msg = xous::receive_message(modals_sid).unwrap(); log::debug!("message: {:?}", msg); @@ -327,6 +329,10 @@ fn main() -> ! { log::warn!("Attempt to access modals without a mutex lock. Ignoring."); continue; } + if let Some(sender) = dynamic_notification_listener.take() { + // unblock the listener with no key hit response + xous::return_scalar2(sender, 0, 0,).unwrap(); + } send_message( renderer_cid, Message::new_scalar( @@ -373,6 +379,14 @@ fn main() -> ! { xous::yield_slice(); // give time for the GAM to redraw } }), + Some(Opcode::ListenToDynamicNotification) => msg_blocking_scalar_unpack!(msg, t0, t1, t2, t3, { + let incoming_token = [t0 as u32, t1 as u32, t2 as u32, t3 as u32]; + if incoming_token != token_lock.unwrap_or(default_nonce) { + log::warn!("Attempt to access modals without a mutex lock. Ignoring."); + xous::return_scalar2(msg.sender, 2, 0).unwrap(); + } + dynamic_notification_listener = Some(msg.sender); // this defers the response, blocking the caller, while we can proceed onwards. + }), // ------------------ INTERNAL APIS -------------------- Some(Opcode::InitiateOp) => { @@ -520,7 +534,7 @@ fn main() -> ! { } let mut gutter = gam::modal::Notification::new( renderer_cid, - Opcode::Gutter.to_u32().unwrap(), + Opcode::HandleDynamicNotificationKeyhit.to_u32().unwrap(), ); gutter.set_manual_dismiss(false); renderer_modal.modify( @@ -583,7 +597,12 @@ fn main() -> ! { renderer_modal.gam.relinquish_focus().unwrap(); op = RendererState::None; token_lock = next_lock(&mut work_queue); - } + }, + Some(Opcode::HandleDynamicNotificationKeyhit) => msg_scalar_unpack!(msg, k, _, _, _, { + if let Some(sender) = dynamic_notification_listener.take() { + xous::return_scalar2(sender, 1, k).unwrap(); + } + }), Some(Opcode::TextEntryReturn) => match op { RendererState::RunText(_config) => { log::trace!("validating text entry modal");