From 4a0f062558a86404ee274fc77913286152976005 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 16 Dec 2021 02:28:37 +0800 Subject: [PATCH] add notification api --- services/modals/src/lib.rs | 10 ++++++++ services/modals/src/main.rs | 45 +++++++++++++++++++++++++++++++----- services/modals/src/tests.rs | 4 ++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 866ed013a..a78aa9e65 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -76,6 +76,16 @@ impl Modals { _ => Err(xous::Error::InternalError) } } + + /// this blocks until the notification has been acknowledged. + pub fn show_notification(&self, notification: &str) -> Result<(), xous::Error> { + let spec = ManagedNotification { + message: xous_ipc::String::from_str(notification), + }; + let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::Notification.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + Ok(()) + } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index d87b6ca47..2ffadbc3c 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -80,7 +80,7 @@ fn xmain() -> ! { renderer_cid, RendererOp::RadioReturn.to_u32().unwrap() ); - let mut notification = gam::modal::Notification::new( + let notification = gam::modal::Notification::new( renderer_cid, RendererOp::NotificationReturn.to_u32().unwrap() ); @@ -116,11 +116,19 @@ fn xmain() -> ! { renderer_modal.modify( Some(ActionType::TextEntry(text_action)), Some(config.prompt.as_str().unwrap()), false, - None, false, None + None, true, None ); renderer_modal.activate(); log::info!("should be active!"); }, + RendererState::RunNotification(config) => { + renderer_modal.modify( + Some(ActionType::Notification(notification)), + Some(config.message.as_str().unwrap()), false, + None, true, None + ); + renderer_modal.activate(); + }, RendererState::None => { log::error!("Operation initiated with no argument specified. Ignoring request."); continue; @@ -170,6 +178,16 @@ fn xmain() -> ! { } } } + Some(RendererOp::NotificationReturn) => { + let mut mutex_op = op.lock().unwrap(); + match *mutex_op { + RendererState::RunNotification(_) => *mutex_op = RendererState::None, + _ => { + log::error!("UX return opcode does not match our current operation in flight. This is a serious internal error."); + panic!("UX return opcode does not match our current operation in flight. This is a serious internal error."); + } + } + }, Some(RendererOp::ModalRedraw) => { renderer_modal.redraw(); }, @@ -219,9 +237,7 @@ fn xmain() -> ! { Some(Opcode::PromptWithTextResponse) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let spec = buffer.to_original::().unwrap(); - { - *op.lock().unwrap() = RendererState::RunText(spec); - } + *op.lock().unwrap() = RendererState::RunText(spec); send_message( renderer_cid, Message::new_scalar(RendererOp::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0) @@ -242,7 +258,24 @@ fn xmain() -> ! { } }, Some(Opcode::Notification) => { - + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let spec = buffer.to_original::().unwrap(); + *op.lock().unwrap() = RendererState::RunNotification(spec); + send_message( + renderer_cid, + Message::new_scalar(RendererOp::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0) + ).expect("couldn't initiate UX op"); + loop { + match *op.lock().unwrap() { + RendererState::RunNotification(_) => (), + RendererState::None => break, + _ => { + log::error!("Illegal state transition in renderer"); + panic!("Illegal state transition in renderer"); + } + } + tt.sleep_ms(100).unwrap(); // don't put the idle in the match/lock(), it'll prevent the other thread from running! + } }, Some(Opcode::StartProgress) => { diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index ba561a3c4..671ab77c6 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -21,6 +21,10 @@ pub(crate) fn spawn_test() { } } log::info!("text input test done"); + + log::info!("testing notification"); + modals.show_notification("This is a test!\n这是一个测验!").expect("notification failed"); + log::info!("notification test done"); } }); }