From ffa8385e7c797789ca7cc7a84e98a1923c60c940 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 6 Nov 2022 18:36:20 +0800 Subject: [PATCH] refactor: send response packet after context has been switched This refactor (at least partially) fixes a "mystery delay" sometimes required after a dialog box swaps out. The previous implementation would send the response, and then swap the contexts. This would often cause the recipient to start drawing its UX before the calling app was foregrounded, causing elements to go missing. This refactor puts the response after the context swap. The main downside is it requires every type of modal to have a GAM handle to issue the redraw call, and is especially complicated by the fact that GAM is not clone-friendly. However, the trade-offs of either creating a handle dynamically or storing one in the object is probably worth the benefit of removing an open-loop delay. --- services/gam/src/modal.rs | 11 ++--------- services/gam/src/modal/bip39entry.rs | 16 ++++++++++++---- services/gam/src/modal/checkboxes.rs | 12 +++++++++--- services/gam/src/modal/consoleinput.rs | 24 ++++++++++++++++++++---- services/gam/src/modal/image.rs | 12 +++++++++--- services/gam/src/modal/notification.rs | 12 +++++++++--- services/gam/src/modal/radiobuttons.rs | 12 +++++++++--- services/gam/src/modal/slider.rs | 24 +++++++++++++++++++----- services/gam/src/modal/textentry.rs | 14 ++++++++++---- 9 files changed, 99 insertions(+), 38 deletions(-) diff --git a/services/gam/src/modal.rs b/services/gam/src/modal.rs index 41aa6d15b..c5fef7a7a 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -56,7 +56,7 @@ pub trait ActionApi { fn close(&mut self) {} fn is_password(&self) -> bool { false } /// navigation is one of '∴' | '←' | '→' | '↑' | '↓' - fn key_action(&mut self, _key: char) -> (Option, bool) {(None, true)} + fn key_action(&mut self, _key: char) -> Option {None} fn set_action_opcode(&mut self, _op: u32) {} } @@ -478,16 +478,9 @@ impl<'a> Modal<'a> { for &k in keys.iter() { if k != '\u{0}' { log::debug!("got key '{}'", k); - let (err, close) = self.action.key_action(k); + let err = self.action.key_action(k); if let Some(err_msg) = err { self.modify(None, None, false, Some(err_msg.to_str()), false, None); - } else { - if close { - // if it's a "close" button, invoke the GAM to put our box away - self.gam.relinquish_focus().unwrap(); - xous::yield_slice(); - break; // don't process any more keys after a close message - } } } } diff --git a/services/gam/src/modal/bip39entry.rs b/services/gam/src/modal/bip39entry.rs index 61ce4a396..67ac22d11 100644 --- a/services/gam/src/modal/bip39entry.rs +++ b/services/gam/src/modal/bip39entry.rs @@ -298,7 +298,7 @@ impl ActionApi for Bip39Entry { modal.gam.post_textview(&mut tv).expect("couldn't post tv"); } - fn key_action(&mut self, k: char) -> (Option, bool) { + fn key_action(&mut self, k: char) -> Option { let can_move_downwards = !(self.suggestion_index.get() + 1 == NUM_RECCOS); let can_move_upwards = !(self.suggestion_index.get() - 1 < 0); @@ -310,9 +310,13 @@ impl ActionApi for Bip39Entry { let mut ret = Bip39EntryPayload::default(); ret.data[..data.len()].copy_from_slice(&data); ret.len = data.len() as u32; + + // relinquish focus before returning the result + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + let buf = Buffer::into_buf(ret).expect("couldn't convert message to payload"); buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - return (None, true) } } else { if self.suggested_words.len() > 0 { @@ -345,10 +349,14 @@ impl ActionApi for Bip39Entry { // ignore null messages } '\u{14}' => { // F4 + // relinquish focus before returning the result + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + let ret = Bip39EntryPayload::default(); // return a 0-length entry let buf = Buffer::into_buf(ret).expect("couldn't convert message to payload"); buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - return (None, true) + return None; } '\u{8}' => { // backspace #[cfg(feature="tts")] @@ -397,6 +405,6 @@ impl ActionApi for Bip39Entry { } } } - (None, false) + None } } \ No newline at end of file diff --git a/services/gam/src/modal/checkboxes.rs b/services/gam/src/modal/checkboxes.rs index b588f45da..3f3e1a26c 100644 --- a/services/gam/src/modal/checkboxes.rs +++ b/services/gam/src/modal/checkboxes.rs @@ -16,6 +16,7 @@ pub struct CheckBoxes { pub action_opcode: u32, pub action_payload: CheckBoxPayload, pub select_index: i16, + gam: crate::Gam, #[cfg(feature = "tts")] pub tts: TtsFrontend, } @@ -29,6 +30,7 @@ impl CheckBoxes { action_opcode, action_payload: CheckBoxPayload::new(), select_index: 0, + gam: crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(), #[cfg(feature="tts")] tts, } @@ -141,7 +143,7 @@ impl ActionApi for CheckBoxes { DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1)) ).expect("couldn't draw entry line"); } - fn key_action(&mut self, k: char) -> (Option, bool) { + fn key_action(&mut self, k: char) -> Option { log::trace!("key_action: {}", k); match k { '←' | '→' => { @@ -180,9 +182,13 @@ impl ActionApi for CheckBoxes { } } } else { // the OK button select + // relinquish focus before returning the result + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - return (None, true) + return None; } } '\u{0}' => { @@ -192,6 +198,6 @@ impl ActionApi for CheckBoxes { // ignore text entry } } - (None, false) + None } } diff --git a/services/gam/src/modal/consoleinput.rs b/services/gam/src/modal/consoleinput.rs index d44902769..54268ab03 100644 --- a/services/gam/src/modal/consoleinput.rs +++ b/services/gam/src/modal/consoleinput.rs @@ -1,11 +1,22 @@ use crate::*; /// This is a specialty structure that takes input from the serial console and records it to a string. -#[derive(Debug, Copy, Clone)] +#[derive(Debug)] pub struct ConsoleInput { pub action_conn: xous::CID, pub action_opcode: u32, pub action_payload: TextEntryPayload, + gam: Gam, +} +impl Clone for ConsoleInput { + fn clone(&self) -> Self { + ConsoleInput { + action_conn: self.action_conn, + action_opcode: self.action_opcode, + action_payload: self.action_payload, + gam: crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(), + } + } } impl ConsoleInput { pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { @@ -13,6 +24,7 @@ impl ConsoleInput { action_conn, action_opcode, action_payload: TextEntryPayload::new(), + gam: crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(), } } } @@ -24,22 +36,26 @@ impl ActionApi for ConsoleInput { fn redraw(&self, _at_height: i16, _modal: &Modal) { // has nothing } - fn key_action(&mut self, k: char) -> (Option, bool) { + fn key_action(&mut self, k: char) -> Option { log::trace!("key_action: {}", k); match k { '\u{0}' => { // ignore null messages } '∴' | '\u{d}' => { + // relinquish focus before returning the result + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - return (None, true) + return None; } _ => { // text entry self.action_payload.content.push(k).expect("ran out of space storing password"); log::trace!("****update payload: {}", self.action_payload.content); } } - (None, false) + None } } \ No newline at end of file diff --git a/services/gam/src/modal/image.rs b/services/gam/src/modal/image.rs index ecebe6393..fec4449af 100644 --- a/services/gam/src/modal/image.rs +++ b/services/gam/src/modal/image.rs @@ -7,6 +7,7 @@ pub struct Image { pub action_conn: xous::CID, pub action_opcode: u32, pub bitmap: Option, + gam: crate::Gam, } impl Image { @@ -15,6 +16,7 @@ impl Image { action_conn, action_opcode, bitmap: None, + gam: crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(), } } pub fn set_bitmap(&mut self, setting: Option) { @@ -48,21 +50,25 @@ impl ActionApi for Image { .expect("couldn't draw bitmap"); } } - fn key_action(&mut self, k: char) -> (Option, bool) { + fn key_action(&mut self, k: char) -> Option { log::trace!("key_action: {}", k); match k { '\u{0}' => { // ignore null messages } _ => { + // relinquish focus before returning the result + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + send_message( self.action_conn, xous::Message::new_scalar(self.action_opcode as usize, 0, 0, 0, 0), ) .expect("couldn't pass on dismissal"); - return (None, true); + None } } - (None, false) + None } } diff --git a/services/gam/src/modal/notification.rs b/services/gam/src/modal/notification.rs index 077ff1f3e..7f3865417 100644 --- a/services/gam/src/modal/notification.rs +++ b/services/gam/src/modal/notification.rs @@ -18,6 +18,7 @@ pub struct Notification { pub manual_dismiss: bool, pub qrcode: Vec, pub qrwidth: usize, + gam: crate::Gam, } impl Notification { pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { @@ -28,6 +29,7 @@ impl Notification { manual_dismiss: true, qrcode: Vec::new(), qrwidth: 0, + gam: crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(), } } pub fn set_is_password(&mut self, setting: bool) { @@ -184,23 +186,27 @@ impl ActionApi for Notification { ) .expect("couldn't draw entry line"); } - fn key_action(&mut self, k: char) -> (Option, bool) { + fn key_action(&mut self, k: char) -> Option { log::trace!("key_action: {}", k); match k { '\u{0}' => { // ignore null messages } _ => { + // relinquish focus before returning the result + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + send_message( self.action_conn, xous::Message::new_scalar(self.action_opcode as usize, k as u32 as usize, 0, 0, 0), ) .expect("couldn't pass on dismissal"); if self.manual_dismiss { - return (None, true); + return None; } } } - (None, false) + None } } diff --git a/services/gam/src/modal/radiobuttons.rs b/services/gam/src/modal/radiobuttons.rs index 81d16f1b0..68e4012a8 100644 --- a/services/gam/src/modal/radiobuttons.rs +++ b/services/gam/src/modal/radiobuttons.rs @@ -17,6 +17,7 @@ pub struct RadioButtons { pub action_payload: RadioButtonPayload, // the current "radio button" selection pub select_index: i16, // the current candidate to be selected pub is_password: bool, + gam: crate::Gam, #[cfg(feature = "tts")] pub tts: TtsFrontend, } @@ -31,6 +32,7 @@ impl RadioButtons { action_payload: RadioButtonPayload::new(""), select_index: 0, is_password: false, + gam: crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(), #[cfg(feature="tts")] tts, } @@ -152,7 +154,7 @@ impl ActionApi for RadioButtons { DrawStyle::new(color, color, 1)) ).expect("couldn't draw entry line"); } - fn key_action(&mut self, k: char) -> (Option, bool) { + fn key_action(&mut self, k: char) -> Option { log::trace!("key_action: {}", k); match k { '←' | '→' => { @@ -177,9 +179,13 @@ impl ActionApi for RadioButtons { self.tts.tts_simple(self.items[self.select_index as usize].as_str()).unwrap(); } } else { // the OK button select + // relinquish focus before returning the result + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - return (None, true) + return None; } } '\u{0}' => { @@ -189,6 +195,6 @@ impl ActionApi for RadioButtons { // ignore text entry } } - (None, false) + None } } \ No newline at end of file diff --git a/services/gam/src/modal/slider.rs b/services/gam/src/modal/slider.rs index c0f9f7e42..603c2231a 100644 --- a/services/gam/src/modal/slider.rs +++ b/services/gam/src/modal/slider.rs @@ -4,6 +4,10 @@ use graphics_server::api::*; use core::fmt::Write; +// This structure needs to be "shallow copy capable" so we can use it with +// the enum_actions API to update the progress state in an efficient manner. +// Thus it does not include its own GAM reference; instead we create one on +// the fly when needed. #[derive(Debug, Copy, Clone)] pub struct Slider { pub min: u32, @@ -163,7 +167,7 @@ impl ActionApi for Slider { draw_list.push(GamObjectType::Rect(inner_rect)).unwrap(); modal.gam.draw_list(draw_list).expect("couldn't execute draw list"); } - fn key_action(&mut self, k: char) -> (Option, bool) { + fn key_action(&mut self, k: char) -> Option { log::trace!("key_action: {}", k); if !self.is_progressbar { match k { @@ -185,20 +189,30 @@ impl ActionApi for Slider { // ignore null messages } '∴' | '\u{d}' => { + // relinquish focus before returning the result + let gam = crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(); + gam.relinquish_focus().unwrap(); + xous::yield_slice(); + send_message(self.action_conn, xous::Message::new_scalar(self.action_opcode as usize, self.action_payload as usize, 0, 0, 0)).expect("couldn't pass on action payload"); - return(None, true) + return None; } _ => { // ignore all other messages } } - (None, false) + None } else { if k == '🛑' { // use the "stop" emoji as a signal that we should close the progress bar - (None, true) + // relinquish focus on stop + let gam = crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(); + gam.relinquish_focus().unwrap(); + xous::yield_slice(); + + None } else { - (None, false) + None } } } diff --git a/services/gam/src/modal/textentry.rs b/services/gam/src/modal/textentry.rs index 5b0b1f7e4..27bedec02 100644 --- a/services/gam/src/modal/textentry.rs +++ b/services/gam/src/modal/textentry.rs @@ -55,6 +55,7 @@ pub struct TextEntry { /// track if keys were hit since initialized: this allows us to clear the default text, /// instead of having it re-appear every time the text area is cleared keys_hit: [bool; MAX_FIELDS as usize], + // gam: crate::Gam, // no GAM field because this needs to be a clone-capable structure. We create a GAM handle when we need it. } impl Default for TextEntry { @@ -358,7 +359,7 @@ impl ActionApi for TextEntry { current_height += self.field_height.get(); } } - fn key_action(&mut self, k: char) -> (Option, bool) { + fn key_action(&mut self, k: char) -> Option { // needs to be a reference, otherwise we're operating on a copy of the payload! let payload = &mut self.action_payloads[self.selected_field as usize]; @@ -415,7 +416,7 @@ impl ActionApi for TextEntry { if let Some(validator) = self.validator { if let Some(err_msg) = validator(*payload, self.action_opcode) { payload.content.clear(); // reset the input field - return (Some(err_msg), false); + return Some(err_msg); } } @@ -427,6 +428,11 @@ impl ActionApi for TextEntry { } } } + // relinquish focus before returning the result + let gam = crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(); + gam.relinquish_focus().unwrap(); + xous::yield_slice(); + let mut payloads: TextEntryPayloads = Default::default(); payloads.1 = self.max_field_amount as usize; payloads.0[..self.max_field_amount as usize].copy_from_slice(&self.action_payloads[..self.max_field_amount as usize]); @@ -438,7 +444,7 @@ impl ActionApi for TextEntry { } self.keys_hit[self.selected_field as usize] = false; - return (None, true) + return None; } '↑' => { if can_move_upwards { @@ -492,6 +498,6 @@ impl ActionApi for TextEntry { } } - (None, false) + None } } \ No newline at end of file