From d026102d419a8cc8b37a859e10fd4320eabb5048 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 1 Apr 2022 22:54:33 +0800 Subject: [PATCH] Limit scope of Foreground/Background messages to just app changes Don't send FocusState::Foreground / FocusState::Background messages to Layouts that are non-App type. This allows this to be used more reliably as a signal of if the background is the running app or not, rather than if it is merely the drawable canvas. --- services/gam/src/api.rs | 1 + services/gam/src/contexts.rs | 31 ++++++++++++++++++++----------- services/gam/src/main.rs | 34 ++++++++++------------------------ services/gam/src/menu.rs | 2 +- services/gam/src/modal.rs | 2 +- 5 files changed, 33 insertions(+), 37 deletions(-) diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index 42dc1e403..6fc6496a8 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -97,6 +97,7 @@ pub struct UxRegistration { /// optional opcode ID for raw keystrokes. They are passed on to the caller in real-time. pub rawkeys_id: Option, /// optional opcode ID code for focus change notifications. Most applications will want to provide this to stop hogging resources when backgrounded + /// If the LayoutType is not an App, this field is ignored and does nothing pub focuschange_id: Option, } diff --git a/services/gam/src/contexts.rs b/services/gam/src/contexts.rs index e3f754450..0d71b6e72 100644 --- a/services/gam/src/contexts.rs +++ b/services/gam/src/contexts.rs @@ -366,10 +366,6 @@ impl ContextManager { // let all the previous operations go out of scope, so we can "check out" the old copy and modify it if self.focused_context.is_some() { // immutable borrow here can't be combined with mutable borrow below - if let Some(old_context) = self.get_context_by_token(self.focused_context.unwrap()) { - self.notify_focus_change_to(gam::FocusState::Background, old_context).unwrap(); - log::trace!("lowered focus to: {:?}", old_context); - } if let Some(old_context) = self.get_context_by_token_mut(self.focused_context.unwrap()) { old_context.layout.set_visibility_state(leaving_visibility, canvases); } @@ -430,7 +426,6 @@ impl ContextManager { // revert the keyboard vibe state self.kbd.set_vibe(context.vibe).expect("couldn't restore keyboard vibe"); - self.notify_focus_change_to(gam::FocusState::Foreground, context).unwrap(); log::trace!("raised focus to: {:?}", context); let last_token = context.app_token; self.last_context = self.focused_context; @@ -455,12 +450,26 @@ impl ContextManager { Err(xous::Error::UseBeforeInit) } } - fn notify_focus_change_to(&self, new_state: gam::FocusState, context: &UxContext) -> Result<(), xous::Error> { - if let Some(focuschange_id) = context.focuschange_id { - log::trace!("focus change {:?} msg to {}, id {}", new_state, context.listener, context.redraw_id); - return xous::send_message(context.listener, - xous::Message::new_scalar(focuschange_id as usize, new_state as usize, 0, 0, 0) - ).map(|_| ()) + pub(crate) fn notify_app_switch(&self, new_app_token: [u32; 4]) -> Result<(), xous::Error> { + if let Some(old_context) = self.get_context_by_token(self.focused_context.unwrap()) { + if let Some(focuschange_id) = old_context.focuschange_id { + log::trace!("Background focus change to {}, id {}", old_context.listener, old_context.redraw_id); + xous::send_message(old_context.listener, + xous::Message::new_scalar(focuschange_id as usize, gam::FocusState::Background as usize, 0, 0, 0) + ).map(|_| ())?; + } else { + return Err(xous::Error::ServerNotFound); + } + } + if let Some(new_context) = self.get_context_by_token(new_app_token) { + if let Some(focuschange_id) = new_context.focuschange_id { + log::trace!("Foreground focus change to {}, id {}", new_context.listener, new_context.redraw_id); + xous::send_message(new_context.listener, + xous::Message::new_scalar(focuschange_id as usize, gam::FocusState::Foreground as usize, 0, 0, 0) + ).map(|_| ())?; + } else { + return Err(xous::Error::ServerNotFound); + } } Ok(()) } diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index 50a4c24f8..c1e49e838 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -491,31 +491,17 @@ fn xmain() -> ! { log::debug!("trying to switch to {:?} with token {:?}", switchapp.app_name.as_str().unwrap(), switchapp.token); if let Some(new_app_token) = context_mgr.find_app_token_by_name(switchapp.app_name.as_str().unwrap()) { - if let Some(menu_token) = context_mgr.find_app_token_by_name(MAIN_MENU_NAME) { - if menu_token == switchapp.token { - match context_mgr.activate(&gfx, &mut canvases, new_app_token, false) { - Ok(_) => (), - Err(_) => log::warn!("failed to switch to {}, silent error!", switchapp.app_name.as_str().unwrap()), + let authorized_switchers = vec![MAIN_MENU_NAME, ROOTKEY_MODAL_NAME, gam::STATUS_BAR_NAME]; + for switchers in authorized_switchers { + if let Some(auth_token) = context_mgr.find_app_token_by_name(switchers) { + if auth_token == switchapp.token { + context_mgr.notify_app_switch(new_app_token).expect("couldn't send focus swap notifications!"); + match context_mgr.activate(&gfx, &mut canvases, new_app_token, false) { + Ok(_) => (), + Err(_) => log::warn!("failed to switch to {}, silent error!", switchapp.app_name.as_str().unwrap()), + } + continue; } - continue; - } - } - if let Some(modal_token) = context_mgr.find_app_token_by_name(ROOTKEY_MODAL_NAME) { - if modal_token == switchapp.token { - match context_mgr.activate(&gfx, &mut canvases, new_app_token, false) { - Ok(_) => (), - Err(_) => log::warn!("failed to switch to {}, silent error!", switchapp.app_name.as_str().unwrap()), - } - continue; - } - } - if let Some(token) = context_mgr.find_app_token_by_name(gam::STATUS_BAR_NAME) { - if token == switchapp.token { - match context_mgr.activate(&gfx, &mut canvases, new_app_token, true) { - Ok(_) => (), - Err(_) => log::warn!("failed to switch to {}, silent error!", switchapp.app_name.as_str().unwrap()), - } - continue; } } // this message came from ourselves diff --git a/services/gam/src/menu.rs b/services/gam/src/menu.rs index 1133c19f0..15476240a 100644 --- a/services/gam/src/menu.rs +++ b/services/gam/src/menu.rs @@ -53,7 +53,7 @@ impl<'a> Menu<'a> { redraw_id: MenuOpcode::Redraw.to_u32().unwrap(), gotinput_id: None, audioframe_id: None, - focuschange_id: None, + focuschange_id: None, // should always be None because we're not an app rawkeys_id: Some(MenuOpcode::Rawkeys.to_u32().unwrap()), } ).expect("couldn't register my Ux element with GAM"); diff --git a/services/gam/src/modal.rs b/services/gam/src/modal.rs index 4d5888858..ea687254f 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -279,7 +279,7 @@ impl<'a> Modal<'a> { redraw_id: ModalOpcode::Redraw.to_u32().unwrap(), gotinput_id: None, audioframe_id: None, - focuschange_id: None, + focuschange_id: None, // should always be none because we're not an app rawkeys_id: Some(ModalOpcode::Rawkeys.to_u32().unwrap()), } ).expect("couldn't register my Ux element with GAM");