add focus/defocus messages to the GAM

this allows animation-heavy apps to be backgrounded and not
continue to run their animation while backgrounded.
This commit is contained in:
bunnie 2022-01-23 17:12:14 +08:00
parent d2a054ec33
commit a0c4304d29
9 changed files with 64 additions and 12 deletions

View file

@ -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");

View file

@ -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;

View file

@ -72,6 +72,8 @@ pub struct UxRegistration {
pub audioframe_id: Option<u32>,
/// optional opcode ID for raw keystrokes. They are passed on to the caller in real-time.
pub rawkeys_id: Option<u32>,
/// 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<u32>,
}

View file

@ -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.

View file

@ -84,6 +84,8 @@ pub(crate) struct UxContext {
pub rawkeys_id: Option<u32>,
/// opcode ID for AudioFrame
pub audioframe_id: Option<u32>,
/// opcode ID for focus change
pub focuschange_id: Option<u32>,
}
// 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::<SwitchToApp, _>().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;
}
}
}

View file

@ -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");

View file

@ -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");

View file

@ -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

View file

@ -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