From a0c4304d29394f05b7dc45a4fba29deafc09b36e Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 23 Jan 2022 17:12:14 +0800 Subject: [PATCH] add focus/defocus messages to the GAM this allows animation-heavy apps to be backgrounded and not continue to run their animation while backgrounded. --- apps/ball/src/ball.rs | 1 + apps/ball/src/main.rs | 6 +++++ services/gam/src/api.rs | 2 ++ services/gam/src/lib.rs | 17 +++++++++++++ services/gam/src/main.rs | 44 ++++++++++++++++++++++++++-------- services/gam/src/menu.rs | 1 + services/gam/src/modal.rs | 1 + services/gam/src/tokens.rs | 3 +-- services/shellchat/src/main.rs | 1 + 9 files changed, 64 insertions(+), 12 deletions(-) diff --git a/apps/ball/src/ball.rs b/apps/ball/src/ball.rs index a4c3f54ce..205c1d252 100644 --- a/apps/ball/src/ball.rs +++ b/apps/ball/src/ball.rs @@ -25,6 +25,7 @@ impl Ball { redraw_id: AppOp::Redraw.to_u32().unwrap(), gotinput_id: None, audioframe_id: None, + focuschange_id: Some(AppOp::FocusChange.to_u32().unwrap()), rawkeys_id: Some(AppOp::Rawkeys.to_u32().unwrap()), }).expect("couldn't register Ux context for shellchat"); diff --git a/apps/ball/src/main.rs b/apps/ball/src/main.rs index 5d5a266cc..d3c91dff5 100644 --- a/apps/ball/src/main.rs +++ b/apps/ball/src/main.rs @@ -13,6 +13,8 @@ pub(crate) enum AppOp { Redraw, /// handle raw key input Rawkeys, + /// handle focus change + FocusChange, /// exit the application Quit, } @@ -43,6 +45,10 @@ fn xmain() -> ! { ]; ball.rawkeys(keys); }), + Some(AppOp::FocusChange) => xous::msg_scalar_unpack!(msg, new_state_code, _, _, _, { + let new_state = gam::FocusState::convert_focus_change(new_state_code); + log::info!("got focus change with new state: {:?}", new_state); + }), Some(AppOp::Quit) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, { xous::return_scalar(msg.sender, 1).expect("couldn't acknowledge quit message"); break; diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index ed74a721e..9294dcc49 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -72,6 +72,8 @@ pub struct UxRegistration { pub audioframe_id: Option, /// 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 + pub focuschange_id: Option, } diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index adcc74b6d..1250f7589 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -48,6 +48,23 @@ pub const EXPECTED_BOOT_CONTEXTS: &[&'static str] = &[ APP_NAME_BALL, ]; +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[repr(u32)] +pub enum FocusState { + Background = 0, + Foreground = 1, +} +impl FocusState { + pub fn convert_focus_change(code: usize) -> FocusState { + if code != 0 { + FocusState::Foreground + } else { + FocusState::Background + } + } +} + + #[derive(Debug)] pub struct Gam { /// The Gam structure exists on the client-side. This is the connection ID to the GAM server, local to this client. diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index d4ad081f5..d558d5d56 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -84,6 +84,8 @@ pub(crate) struct UxContext { pub rawkeys_id: Option, /// opcode ID for AudioFrame pub audioframe_id: Option, + /// opcode ID for focus change + pub focuschange_id: Option, } // const BOOT_APP_NAME: &'static str = "shellchat"; // this is the app to display on boot -- we will eventually need this once we have more than one app? pub const ROOTKEY_MODAL_NAME: &'static str = "rootkeys modal"; @@ -164,6 +166,7 @@ impl ContextManager { redraw_id: registration.redraw_id, gotinput_id: registration.gotinput_id, audioframe_id: registration.audioframe_id, + focuschange_id: registration.focuschange_id, rawkeys_id: None, vibe: false, }; @@ -185,6 +188,7 @@ impl ContextManager { redraw_id: registration.redraw_id, gotinput_id: None, audioframe_id: None, + focuschange_id: registration.focuschange_id, rawkeys_id: registration.rawkeys_id, vibe: false, }; @@ -212,6 +216,7 @@ impl ContextManager { redraw_id: registration.redraw_id, gotinput_id: None, audioframe_id: None, + focuschange_id: registration.focuschange_id, rawkeys_id: registration.rawkeys_id, vibe: false, }; @@ -232,6 +237,7 @@ impl ContextManager { redraw_id: registration.redraw_id, gotinput_id: None, audioframe_id: None, + focuschange_id: registration.focuschange_id, rawkeys_id: registration.rawkeys_id, vibe: false, }; @@ -344,6 +350,10 @@ 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(); + } if let Some(old_context) = self.get_context_by_token_mut(self.focused_context.unwrap()) { old_context.layout.set_visibility_state(leaving_visibility, canvases); } @@ -376,6 +386,7 @@ impl ContextManager { // now re-check-out the new context and finalize things let maybe_new_focus = self.get_context_by_token(token); if let Some(context) = maybe_new_focus { + self.notify_focus_change_to(gam::FocusState::Foreground, context).unwrap(); if clear { context.layout.clear(gfx, canvases).expect("can't clear on context activation"); } @@ -408,6 +419,15 @@ impl ContextManager { self.activate(gfx, canvases, last, false); } } + 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(|_| ()) + } + Ok(()) + } pub(crate) fn redraw(&self) -> Result<(), xous::Error> { // redraws the currently focused context if let Some(token) = self.focused_app() { if let Some(context) = self.contexts.get(&token) { @@ -943,23 +963,27 @@ fn xmain() -> ! { Some(Opcode::SwitchToApp) => { let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let switchapp = buffer.to_original::().unwrap(); + log::info!("trying to switch to {:?} with token {:?}", switchapp.app_name.as_str().unwrap(), switchapp.token); - if let Some(menu_token) = context_mgr.find_app_token_by_name(MAIN_MENU_NAME) { - if menu_token == switchapp.token { - if let Some(new_app_token) = context_mgr.find_app_token_by_name(switchapp.app_name.as_str().unwrap()) { + 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 { context_mgr.activate(&gfx, &mut canvases, new_app_token, false); + continue; } } - } else if let Some(modal_token) = context_mgr.find_app_token_by_name(ROOTKEY_MODAL_NAME) { - if modal_token == switchapp.token { - if let Some(new_app_token) = context_mgr.find_app_token_by_name(switchapp.app_name.as_str().unwrap()) { + if let Some(modal_token) = context_mgr.find_app_token_by_name(ROOTKEY_MODAL_NAME) { + if modal_token == switchapp.token { context_mgr.activate(&gfx, &mut canvases, new_app_token, false); + continue; } } - } else if let Some(token) = context_mgr.find_app_token_by_name(gam::STATUS_BAR_NAME) { - if token == switchapp.token { - if let Some(new_app_token) = context_mgr.find_app_token_by_name(switchapp.app_name.as_str().unwrap()) { - context_mgr.activate(&gfx, &mut canvases, new_app_token, false); + if let Some(token) = context_mgr.find_app_token_by_name(gam::STATUS_BAR_NAME) { + log::info!("status token: {:?}", token); + if token == switchapp.token { + log::info!("matched status token"); + context_mgr.activate(&gfx, &mut canvases, new_app_token, true); + continue; } } } diff --git a/services/gam/src/menu.rs b/services/gam/src/menu.rs index c6d4d12c4..04559f9c9 100644 --- a/services/gam/src/menu.rs +++ b/services/gam/src/menu.rs @@ -49,6 +49,7 @@ impl<'a> Menu<'a> { redraw_id: MenuOpcode::Redraw.to_u32().unwrap(), gotinput_id: None, audioframe_id: None, + focuschange_id: None, 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 136e7b9a8..e522b0864 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -279,6 +279,7 @@ impl<'a> Modal<'a> { redraw_id: ModalOpcode::Redraw.to_u32().unwrap(), gotinput_id: None, audioframe_id: None, + focuschange_id: None, rawkeys_id: Some(ModalOpcode::Rawkeys.to_u32().unwrap()), } ).expect("couldn't register my Ux element with GAM"); diff --git a/services/gam/src/tokens.rs b/services/gam/src/tokens.rs index 0a09ddda1..e96222d51 100644 --- a/services/gam/src/tokens.rs +++ b/services/gam/src/tokens.rs @@ -65,12 +65,11 @@ impl<'a> TokenManager { return Some(token) } pub(crate) fn is_token_valid(&self, token: [u32; 4]) -> bool { - log::trace!("checking for validity of token {:x?}", token); - log::trace!("token table is {:x?}", self.tokens); self.tokens.iter().find(|&namedtoken| namedtoken.token == token).is_some() } pub(crate) fn find_token(&self, name: &str) -> Option<[u32; 4]> { if let Some(i) = self.tokens.iter().position(|namedtoken| namedtoken.name == name) { + log::info!("found {}:{:?}", name, self.tokens[i].token); Some(self.tokens[i].token) } else { None diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index 4de82b998..7a2d91200 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -94,6 +94,7 @@ impl Repl{ gotinput_id: Some(ShellOpcode::Line.to_u32().unwrap()), audioframe_id: None, rawkeys_id: None, + focuschange_id: None, // shellchat will keep running in the background even if it has lost focus }).expect("couldn't register Ux context for shellchat"); // we should be the first app running, so get the focus