From 62c9737bbc05d95f5a83bf8bfac8842f5ab84e38 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 18 Jun 2021 04:10:41 +0800 Subject: [PATCH] tenuous addition of a menu object things don't crash, but...haven't really validated if they work, either! --- services/gam/src/api.rs | 6 + services/gam/src/canvas.rs | 2 +- services/gam/src/layouts.rs | 87 ++++++- services/gam/src/lib.rs | 14 ++ services/gam/src/main.rs | 276 +++++++++++++++++---- services/gam/src/menu.rs | 163 ++++++++++++ services/gam/src/status.rs | 1 - services/gam/src/tokens.rs | 2 +- services/graphics-server/src/api/shapes.rs | 7 + services/shellchat/src/main.rs | 7 +- 10 files changed, 509 insertions(+), 56 deletions(-) create mode 100755 services/gam/src/menu.rs diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index 2a0a9d439..2d0265f63 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -112,6 +112,12 @@ pub(crate) enum Opcode { /// used to turn keyboard vibrate on and off Vibe, + /// called by a context when it's done with taking the screen; requests the GAM to revert focus to the last-focused app + RevertFocus, + + /// request an app to take the focus + RequestFocus, + /////// planned // hides a canvas with a given GID diff --git a/services/gam/src/canvas.rs b/services/gam/src/canvas.rs index 5dcb1459b..5bad60bb5 100644 --- a/services/gam/src/canvas.rs +++ b/services/gam/src/canvas.rs @@ -178,7 +178,7 @@ pub fn deface(gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap, screen: Rectangle) -> FnvIndexMap { +pub fn recompute_canvases(canvases: &FnvIndexMap, screen: Rectangle) -> FnvIndexMap { let debug = false; // first, sort canvases by trust_level. Canvas implements ord/eq based on the trust_level attribute // so jush pushing it into a max binary heap does the trick. diff --git a/services/gam/src/layouts.rs b/services/gam/src/layouts.rs index 4516a1aa5..549e0a2b2 100755 --- a/services/gam/src/layouts.rs +++ b/services/gam/src/layouts.rs @@ -5,7 +5,7 @@ use blitstr_ref as blitstr; use blitstr::GlyphStyle; use graphics_server::*; -use crate::LayoutApi; +use crate::{LayoutApi, LayoutBehavior}; #[derive(Debug, Copy, Clone)] // GIDs of canvases that are used the "Chat" layout. @@ -21,6 +21,7 @@ pub(crate) struct ChatLayout { screensize: Point, small_height: i16, regular_height: i16, + visible: bool, } impl ChatLayout { // pass in the status canvas so we can size around it, but we can't draw on it @@ -32,10 +33,17 @@ impl ChatLayout { let regular_height: i16 = gfx.glyph_height_hint(GlyphStyle::Regular).expect("couldn't get glyph height") as i16; let margin = 4; + let checked_base_trust = if base_trust < 4 { + 4 + } else { + base_trust + }; + // allocate canvases in structures, and record their GID for future reference + // base trust - 2 so that main menu + status bar always ride on top let predictive_canvas = Canvas::new( Rectangle::new_coords(0, screensize.y - regular_height - margin*2, screensize.x, screensize.y), - base_trust, + checked_base_trust - 2, &trng, None ).expect("couldn't create predictive text canvas"); canvases.insert(predictive_canvas.gid(), predictive_canvas).expect("couldn't store predictive canvas"); @@ -43,13 +51,13 @@ impl ChatLayout { let min_input_height = regular_height + margin*2; let input_canvas = Canvas::new( Rectangle::new_v_stack(predictive_canvas.clip_rect(), -min_input_height), - base_trust, &trng, None + checked_base_trust - 2, &trng, None ).expect("couldn't create input text canvas"); canvases.insert(input_canvas.gid(), input_canvas).expect("couldn't store input canvas"); let content_canvas = Canvas::new( Rectangle::new_v_span(status_canvas.clip_rect(), input_canvas.clip_rect()), - base_trust / 2, &trng, None + checked_base_trust / 2, &trng, None ).expect("couldn't create content canvas"); canvases.insert(content_canvas.gid(), content_canvas).expect("can't store content canvas"); @@ -62,10 +70,14 @@ impl ChatLayout { screensize, small_height, regular_height, + visible: true, }) } } impl LayoutApi for ChatLayout { + fn behavior(&self) -> LayoutBehavior { + LayoutBehavior::App + } fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap) -> Result<(), xous::Error> { let input_canvas = canvases.get(&self.input).expect("couldn't find input canvas"); let content_canvas = canvases.get(&self.content).expect("couldn't find content canvas"); @@ -124,6 +136,35 @@ impl LayoutApi for ChatLayout { fn get_content_canvas(&self) -> Gid { self.content } + fn set_visibility_state(&mut self, onscreen: bool, canvases: &mut FnvIndexMap) { + log::debug!("chatlayout: set_visibility_state onscreen {}, self.visible {}", onscreen, self.visible); + if onscreen == self.visible { + log::trace!("chatlayout: no change to visibility, moving on"); + // nothing to do + return + } + let offscreen = if !onscreen && self.visible { + // move canvases off-screen + Point::new(self.screensize.x*2, 0) + } else if onscreen && !self.visible { + // undo the off-screen move + Point::new(-self.screensize.x*2, 0) + } else { + // should actually never reach this because of the identity check at the very top + Point::new(0, 0) + }; + log::debug!("chatlayout: shifting canvas for input by {:?}", offscreen); + let input_canvas = canvases.get_mut(&self.input).expect("couldn't find input canvas"); + input_canvas.set_clip(input_canvas.clip_rect().translate_chain(offscreen)); + + let content_canvas = canvases.get_mut(&self.content).expect("couldn't find content canvas"); + content_canvas.set_clip(content_canvas.clip_rect().translate_chain(offscreen)); + + let predictive_canvas = canvases.get_mut(&self.predictive).expect("couldn't find predictive canvas"); + predictive_canvas.set_clip(predictive_canvas.clip_rect().translate_chain(offscreen)); + + self.visible = onscreen; + } } // remember GIDs of the canvases for menus @@ -135,19 +176,27 @@ pub(crate) struct MenuLayout { menu_min_height: i16, screensize: Point, small_height: i16, + visible: bool, } impl MenuLayout { - pub fn init(gfx: &graphics_server::Gfx, trng: &trng::Trng, base_trust: u8, _status_canvas: &Canvas, canvases: &mut FnvIndexMap) -> Result { + pub fn init(gfx: &graphics_server::Gfx, trng: &trng::Trng, base_trust: u8, canvases: &mut FnvIndexMap) -> Result { let screensize = gfx.screen_size().expect("Couldn't get screen size"); // get the height of various text regions to compute the layout let small_height: i16 = gfx.glyph_height_hint(GlyphStyle::Small).expect("couldn't get glyph height") as i16; + let checked_base_trust = if base_trust < 4 { + 4 + } else { + base_trust + }; + const MENU_Y_PAD: i16 = 100; const MENU_X_PAD: i16 = 35; // build for an initial size of 1 entry + // base trust - 1 so that status bar can always ride on top let menu_canvas = Canvas::new( Rectangle::new_coords(MENU_X_PAD, MENU_Y_PAD, screensize.x - MENU_X_PAD, MENU_Y_PAD + small_height), - base_trust, &trng, None + checked_base_trust - 1, &trng, None ).expect("couldn't create menu canvas"); canvases.insert(menu_canvas.gid(), menu_canvas).expect("can't store menu canvas"); @@ -158,10 +207,14 @@ impl MenuLayout { menu_min_height: small_height, screensize, small_height, + visible: true, }) } } impl LayoutApi for MenuLayout { + fn behavior(&self) -> LayoutBehavior { + LayoutBehavior::Alert + } fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap) -> Result<(), xous::Error> { let menu_canvas = canvases.get(&self.menu).expect("couldn't find menu canvas"); @@ -189,4 +242,26 @@ impl LayoutApi for MenuLayout { fn get_content_canvas(&self) -> Gid { self.menu } + fn set_visibility_state(&mut self, onscreen: bool, canvases: &mut FnvIndexMap) { + log::debug!("menu entering set_visibilty_state, self.visible {}, onscreen {}", self.visible, onscreen); + if onscreen == self.visible { + // nothing to do + return + } + let menu_canvas = canvases.get_mut(&self.menu).expect("couldn't find menu canvas"); + + let offscreen = if !onscreen && self.visible { + // move canvases off-screen + Point::new(self.screensize.x*2, 0) + } else if onscreen && !self.visible { + // undo the off-screen move + Point::new(-self.screensize.x*2, 0) + } else { + // should actually never reach this because of the identity check at the very top + Point::new(0, 0) + }; + log::debug!("moving menu by {:?}", offscreen); + menu_canvas.set_clip(menu_canvas.clip_rect().translate_chain(offscreen)); + self.visible = onscreen; + } } \ No newline at end of file diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index b4d66d6e2..e49ab2a5a 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -220,6 +220,20 @@ impl Gam { ena, 0, 0, 0,) ).map(|_| ()) } + /// this indicates to the GAM that the currently running app no longer wants to be the focus of attention + /// we might respect that. or maybe not. depends on the GAM's policies. + pub fn relinquish_focus(&self) -> Result<(), xous::Error> { + send_message(self.conn, + Message::new_scalar(Opcode::RevertFocus.to_usize().unwrap(), + 0, 0, 0, 0,) + ).map(|_| ()) + } + pub fn request_focus(&self, token: [u32; 4]) -> Result<(), xous::Error> { + send_message(self.conn, + Message::new_blocking_scalar(Opcode::RequestFocus.to_usize().unwrap(), + token[0] as usize, token[1] as usize, token[2] as usize, token[3] as usize,) + ).map(|_| ()) + } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index 43f263573..b588b422b 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -7,6 +7,8 @@ mod status; use status::*; mod canvas; use canvas::*; +mod menu; +use menu::*; mod tokens; use tokens::*; @@ -33,6 +35,14 @@ use enum_dispatch::enum_dispatch; // - create menu server // - add auth tokens to audio streams, so less trusted processes can make direct connections to the codec and reduce latency +#[derive(PartialEq, Eq)] +pub(crate) enum LayoutBehavior { + /// a layout that can render over others, takes focus, and only dismissed if explicitly dismissed + Alert, + /// a layout that assumes it has the full screen and is the primary content when visible + App, +} + #[enum_dispatch] pub(crate) trait LayoutApi { fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap) -> Result<(), xous::Error>; @@ -41,6 +51,11 @@ pub(crate) trait LayoutApi { fn get_input_canvas(&self) -> Option { None } fn get_prediction_canvas(&self) -> Option { None } fn get_content_canvas(&self) -> Gid; // layouts always have a content canvas + // when the argument is true, the context is moved "onscreen" by moving the canvases into the screen clipping rectangle + // when false, the context is moved "offscreen" by moving the canvases outside the screen clipping rectangle + // note that this visibility state is an independent variable from the trust level draw-ability + fn set_visibility_state(&mut self, onscreen: bool, canvases: &mut FnvIndexMap); + fn behavior(&self) -> LayoutBehavior; } #[enum_dispatch(LayoutApi)] @@ -80,13 +95,23 @@ pub(crate) struct UxContext { const MAX_UX_CONTEXTS: usize = 4; pub(crate) const MAX_CANVASES: usize = 32; const BOOT_APP_NAME: &'static str = "shellchat"; // this is the app to display on boot +pub const MAIN_MENU_NAME: &'static str = "main menu"; const BOOT_CONTEXT_TRUSTLEVEL: u8 = 254; +/* + For now, app focus from menus is cooperative (menu items must relinquish focus). + However, later on, I think it would be good to implement a press-hold to feature to + swap focus in case of an app hang failure. This feature would probably be best done + by adding a hook to the keyboard manager to look for a press-hold on the "select" key + and then sending a message to the registered listener about the issue. +*/ struct ContextManager { tm: TokenManager, contexts: [Option; MAX_UX_CONTEXTS], focused_context: Option<[u32; 4]>, // app_token of the app that has I/O focus, if any + last_context: Option<[u32; 4]>, // previously focused context, if any imef: ime_plugin_api::ImeFrontEnd, + imef_active: bool, kbd: keyboard::Keyboard, } impl ContextManager { @@ -102,7 +127,9 @@ impl ContextManager { tm: TokenManager::new(&xns), contexts: [None; MAX_UX_CONTEXTS], focused_context: None, + last_context: None, imef, + imef_active: false, kbd, } } @@ -120,6 +147,7 @@ impl ContextManager { trng: &trng::Trng, status_canvas: &Canvas, canvases: &mut FnvIndexMap, + trust_level: u8, registration: UxRegistration) -> Option<[u32; 4]> { let maybe_token = self.tm.claim_token(registration.app_name.as_str().unwrap()); @@ -127,14 +155,16 @@ impl ContextManager { if let Some(token) = maybe_token { match registration.ux_type { UxType::Chat => { - let chatlayout = ChatLayout::init(&gfx, &trng, - BOOT_CONTEXT_TRUSTLEVEL, &status_canvas, canvases).expect("couldn't create chat layout"); - let ux_context = UxContext { + let mut chatlayout = ChatLayout::init(&gfx, &trng, + trust_level, &status_canvas, canvases).expect("couldn't create chat layout"); + // default to off-screen for all layouts + chatlayout.set_visibility_state(false, canvases); + let ux_context = UxContext { layout: UxLayout::ChatLayout(chatlayout), predictor: registration.predictor, app_token: token, gam_token: [trng.get_u32().unwrap(), trng.get_u32().unwrap(), trng.get_u32().unwrap(), trng.get_u32().unwrap(), ], - trust_level: BOOT_CONTEXT_TRUSTLEVEL, + trust_level, listener: xous::connect(xous::SID::from_array(registration.listener)).unwrap(), redraw_id: registration.redraw_id, gotinput_id: registration.gotinput_id, @@ -151,14 +181,17 @@ impl ContextManager { } }, UxType::Menu => { - let menulayout = MenuLayout::init(&gfx, &trng, - BOOT_CONTEXT_TRUSTLEVEL, &status_canvas, canvases).expect("couldn't create menu layout"); + let mut menulayout = MenuLayout::init(&gfx, &trng, + trust_level, canvases).expect("couldn't create menu layout"); + // default to off-screen for all layouts + menulayout.set_visibility_state(false, canvases); + log::debug!("debug layout: {:?}", menulayout); let ux_context = UxContext { layout: UxLayout::MenuLayout(menulayout), predictor: None, app_token: token, gam_token: [trng.get_u32().unwrap(), trng.get_u32().unwrap(), trng.get_u32().unwrap(), trng.get_u32().unwrap(), ], - trust_level: BOOT_CONTEXT_TRUSTLEVEL, + trust_level, listener: xous::connect(xous::SID::from_array(registration.listener)).unwrap(), redraw_id: registration.redraw_id, gotinput_id: None, @@ -215,15 +248,91 @@ impl ContextManager { } None } - pub(crate) fn activate(&mut self, - gfx: &graphics_server::Gfx, - canvases: &mut FnvIndexMap, - token: [u32; 4], - clear: bool, - ) { + fn get_context_by_token_mut(&'_ mut self, token: [u32; 4]) -> Option<&'_ mut UxContext> { for maybe_context in self.contexts.iter_mut() { if let Some(context) = maybe_context { if context.app_token == token { + return Some(context) + } + } + } + None + } + fn get_context_by_token(&'_ self, token: [u32; 4]) -> Option<&'_ UxContext> { + for maybe_context in self.contexts.iter() { + if let Some(context) = maybe_context { + if context.app_token == token { + return Some(context) + } + } + } + None + } + pub(crate) fn activate(&mut self, + gfx: &graphics_server::Gfx, + canvases: &mut FnvIndexMap, + token: [u32; 4], + clear: bool, + ) { + let mut leaving_visibility: bool = false; + { + // using a temp copy of the old focus, check if we need to update any visibility state + let maybe_leaving_focused_context = if self.focused_context.is_some() { + if let Some(old_context) = self.get_context_by_token(self.focused_context.unwrap()) { + Some(old_context.clone()) + } else { + None + } + } else { + None + }; + let maybe_new_focus = self.get_context_by_token_mut(token); + if let Some(context) = maybe_new_focus { + if let Some(leaving_focused_context) = maybe_leaving_focused_context { + if token != leaving_focused_context.app_token { + if (context.layout.behavior() == LayoutBehavior::Alert) && + (leaving_focused_context.layout.behavior() == LayoutBehavior::Alert) { + context.layout.set_visibility_state(true, canvases); + //leaving_focused_context.layout.set_visibility_state(false, canvases); + leaving_visibility = false; + } else if (context.layout.behavior() == LayoutBehavior::App) && + (leaving_focused_context.layout.behavior() == LayoutBehavior::App) { + context.layout.set_visibility_state(true, canvases); + //leaving_focused_context.layout.set_visibility_state(false, canvases); + leaving_visibility = false; + } else if (context.layout.behavior() == LayoutBehavior::Alert) && + (leaving_focused_context.layout.behavior() == LayoutBehavior::App) { + context.layout.set_visibility_state(true, canvases); + //leaving_focused_context.layout.set_visibility_state(true, canvases); + leaving_visibility = true; + } else if (context.layout.behavior() == LayoutBehavior::App) && + (leaving_focused_context.layout.behavior() == LayoutBehavior::Alert) { + context.layout.set_visibility_state(true, canvases); + //leaving_focused_context.layout.set_visibility_state(false, canvases); + leaving_visibility = false; + } + } + } else { + // there was no current focus, just make the activation visible + log::debug!("setting first-time visibility to context {:?}", token); + context.layout.set_visibility_state(true, canvases); + } + } + } + { + // 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() { + if let Some(old_context) = self.get_context_by_token_mut(self.focused_context.unwrap()) { + old_context.layout.set_visibility_state(leaving_visibility, canvases); + } + } + } + { + // 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 { + if context.predictor.is_some() { + // only hook up the IMEF if a predictor is selected for this context let descriptor = ImefDescriptor { input_canvas: context.layout.get_input_canvas(), prediction_canvas: context.layout.get_prediction_canvas(), @@ -231,20 +340,46 @@ impl ContextManager { token: context.gam_token, }; self.imef.connect_backend(descriptor).expect("couldn't connect IMEF to the current app"); - if clear { - context.layout.clear(gfx, canvases).expect("can't clear on context activation"); - } - // now update the IMEF area, since we're initialized - // note: we may need to skip this call if the context does not utilize a predictor... - self.imef.redraw().unwrap(); - - // revert the keyboard vibe state - self.kbd.set_vibe(context.vibe).expect("couldn't restore keyboard vibe"); - - self.focused_context = Some(context.app_token); + self.imef_active = true; + } else { + self.imef_active = false; } + + // now recompute the drawability of canvases, based on on-screen visibility and trust state + let screensize = gfx.screen_size().expect("Couldn't get screen size"); + *canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize)); } } + { + // 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 { + if clear { + context.layout.clear(gfx, canvases).expect("can't clear on context activation"); + } + // now update the IMEF area, since we're initialized + // note: we may need to skip this call if the context does not utilize a predictor... + if context.predictor.is_some() { + self.imef.redraw().unwrap(); + } + + // revert the keyboard vibe state + self.kbd.set_vibe(context.vibe).expect("couldn't restore keyboard vibe"); + + let last_token = context.app_token; + self.last_context = self.focused_context; + self.focused_context = Some(last_token); + self.redraw().expect("couldn't redraw the currently focused app"); + } + } + } + pub(crate) fn revert_focus(&mut self, + gfx: &graphics_server::Gfx, + canvases: &mut FnvIndexMap, + ) { + if let Some(last) = self.last_context { + self.activate(gfx, canvases, last, false); + } } pub(crate) fn redraw(&self) -> Result<(), xous::Error> { // redraws the currently focused context if let Some(token) = self.focused_app() { @@ -285,29 +420,60 @@ impl ContextManager { } Err(xous::Error::ServerNotFound) } - pub(crate) fn key_event(&self, keys: [char; 4]) { - self.imef.send_keyevent(keys).expect("couldn't send keys to the IMEF"); - } - fn focused_context(&'_ mut self) -> Option<&'_ mut UxContext> { - if let Some(focus) = self.focused_app() { - for maybe_context in self.contexts.iter_mut() { - if let Some(context) = maybe_context { - if context.app_token == focus { - return Some(context) - } + pub(crate) fn key_event(&mut self, keys: [char; 4], + gfx: &graphics_server::Gfx, + canvases: &mut FnvIndexMap, + ) { + // only pop up the menu if the primary key hit is the menu key (search just the first entry of keys); reject multi-key hits + if keys[0] == '∴' { + if let Some(menu_token) = self.find_app_token_by_name(MAIN_MENU_NAME) { + // set the menu to the active context + self.activate(gfx, canvases, menu_token, false); + } + } + + if self.imef_active { + // use the IMEF + self.imef.send_keyevent(keys).expect("couldn't send keys to the IMEF"); + } else { + // forward the keyboard hits without any IME to the current context + log::debug!("forwarding raw key event"); + if let Some(context) = self.focused_context() { + if let Some(rawkeys_id) = context.rawkeys_id { + xous::send_message(context.listener, + xous::Message::new_scalar(rawkeys_id as usize, + keys[0] as u32 as usize, + keys[1] as u32 as usize, + keys[2] as u32 as usize, + keys[3] as u32 as usize, + )).expect("couldn't forward raw keys onto context listener"); } } } - None + } + + fn focused_context(&'_ self) -> Option<&'_ UxContext> { + if let Some(focus) = self.focused_app() { + self.get_context_by_token(focus) + } else { + None + } + } + fn focused_context_mut(&'_ mut self) -> Option<&'_ mut UxContext> { + if let Some(focus) = self.focused_app() { + self.get_context_by_token_mut(focus) + } else { + None + } } pub(crate) fn set_audio_op(&mut self, audio_op: SetAudioOpcode) { - if let Some(context) = self.focused_context() { + if let Some(context) = self.focused_context_mut() { (*context).audioframe_id = Some(audio_op.opcode); } } pub(crate) fn vibe(&mut self, set_vibe: bool) { self.kbd.set_vibe(set_vibe).expect("couldn't set vibe on keyboard"); - if let Some(context) = self.focused_context() { + if let Some(context) = self.focused_context_mut() { (*context).vibe = set_vibe; } } @@ -354,7 +520,7 @@ fn xmain() -> ! { 255, &trng, None ).expect("couldn't create status canvas"); canvases.insert(status_canvas.gid(), status_canvas).expect("can't store status canvus"); - canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize)); + canvases = recompute_canvases(&canvases, Rectangle::new(Point::new(0, 0), screensize)); // make a thread to manage the status bar -- this needs to start late, after the IMEF and most other things are initialized // the status bar is a trusted element managed by the OS, and we are chosing to domicile this in the GAM process for now @@ -362,6 +528,10 @@ fn xmain() -> ! { log::trace!("starting status thread with gid {:?}", status_gid); xous::create_thread_4(status_thread, status_gid[0] as _, status_gid[1] as _, status_gid[2] as _, status_gid[3] as _).expect("couldn't create status thread"); + // make a thread to manage the main menu. + log::trace!("starting menu thread"); + xous::create_thread_0(main_menu_thread).expect("couldn't create menu thread"); + let mut powerdown_requested = false; let mut last_time: u64 = ticktimer.elapsed_ms(); log::trace!("entering main loop"); @@ -389,6 +559,7 @@ fn xmain() -> ! { Some(c) => { let mut rect = c.clip_rect(); rect.normalize(); // normalize to 0,0 coordinates + log::debug!("getcanvasbounds: {:?}", rect); xous::return_scalar2(msg.sender, rect.tl.into(), rect.br.into(), @@ -483,7 +654,7 @@ fn xmain() -> ! { buffer.replace(ret).unwrap(); canvas.do_drawn().expect("couldn't set canvas to drawn"); } else { - info!("attempt to draw TextView on non-drawable canvas. Not fatal, but request ignored."); + info!("attempt to draw TextView on non-drawable canvas. Not fatal, but request ignored. {:?}", tv); } } else { info!("bogus GID {:?} in TextView {}, not doing anything in response to draw request.", tv.get_canvas_gid(), tv.text); @@ -500,7 +671,7 @@ fn xmain() -> ! { let granted = context_mgr.set_canvas_height(&gfx, cb.token, cb.requested.y, &&status_canvas, &mut canvases); if granted.is_some() { // recompute the canvas orders based on the new layout - canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize)); + canvases = recompute_canvases(&canvases, Rectangle::new(Point::new(0, 0), screensize)); context_mgr.redraw().expect("can't redraw after new canvas bounds"); } cb.granted = granted; @@ -514,7 +685,6 @@ fn xmain() -> ! { log::debug!("RequestContentCanvas {:?}", req); let ret = api::Return::ContentCanvasReturn(context_mgr.get_content_canvas(req)); - log::debug!("returning {:?}", ret); buffer.replace(ret).unwrap(); } Some(Opcode::RenderObject) => { @@ -583,12 +753,16 @@ fn xmain() -> ! { Some(Opcode::RegisterUx) => { let mut buffer = unsafe{ Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let registration = buffer.to_original::().unwrap(); - let token = context_mgr.register(&gfx, &trng, &status_canvas, &mut canvases, registration); + + // note that we are currently assigning all Ux registrations a trust level consistent with a boot context (ultimately trusted) + // this needs to be modified later on once we allow post-boot apps to be created + let token = context_mgr.register(&gfx, &trng, &status_canvas, &mut canvases, + BOOT_CONTEXT_TRUSTLEVEL, registration); // compute what canvases are drawable // this _replaces_ the original canvas structure, to avoid complications of tracking mutable references through compound data structures // this is broken into two steps because of https://github.com/rust-lang/rust/issues/71126 - canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize)); + canvases = recompute_canvases(&canvases, Rectangle::new(Point::new(0, 0), screensize)); buffer.replace(Return::UxToken(token)).unwrap(); }, @@ -628,21 +802,33 @@ fn xmain() -> ! { '\u{0000}' }, ]; - context_mgr.key_event(keys); + context_mgr.key_event(keys, &gfx, &mut canvases); }), Some(Opcode::Vibe) => msg_scalar_unpack!(msg, ena, _, _, _, { if ena != 0 { context_mgr.vibe(true) } else { context_mgr.vibe(false) } }), + Some(Opcode::RevertFocus) => { + context_mgr.revert_focus(&gfx, &mut canvases); + }, + Some(Opcode::RequestFocus) => msg_blocking_scalar_unpack!(msg, t0, t1, t2, t3, { + // TODO: add some limitations around who can request focus + // for now, it's the boot set so we just trust the requestor + context_mgr.activate(&gfx, &mut canvases, [t0 as u32, t1 as u32, t2 as u32, t3 as u32], true); + + // this is a blocking scalar, so return /something/ so we know to move on + xous::return_scalar(msg.sender, 1).expect("couldn't confirm focus activation"); + }), Some(Opcode::Quit) => break, None => {log::error!("unhandled message {:?}", msg);} } + /* // if we don't have a focused app, try and find the default boot app and bring it to focus. if context_mgr.focused_app().is_none() { if let Some(shellchat_token) = context_mgr.find_app_token_by_name(BOOT_APP_NAME) { context_mgr.activate(&gfx, &mut canvases, shellchat_token, true); } - } + }*/ } // clean up our program log::trace!("main loop exit, destroying servers"); diff --git a/services/gam/src/menu.rs b/services/gam/src/menu.rs new file mode 100755 index 000000000..df962988c --- /dev/null +++ b/services/gam/src/menu.rs @@ -0,0 +1,163 @@ +use xous_ipc::*; +use graphics_server::*; +use num_traits::*; +use xous::msg_scalar_unpack; + +const MAX_ITEMS: usize = 16; + +#[derive(Debug, Copy, Clone)] +pub enum MenuPayload { + /// memorized scalar payload + Scalar([u32; 4]), + /// this a nebulous-but-TBD maybe way of bodging in a more complicated record, which would involve + /// casting this memorized, static payload into a Buffer and passing it on. Let's not worry too much about it for now, it's mostly apirational... + Memory(([u8; 256], usize)), +} +#[derive(Debug, Copy, Clone)] +pub struct MenuItem { + name: String::<64>, + action_conn: xous::CID, + action_opcode: u32, + action_payload: MenuPayload, +} + +#[derive(Debug)] +pub struct Menu { + pub sid: xous::SID, + pub gam: gam::Gam, + pub xns: xous_names::XousNames, + pub items: [Option; MAX_ITEMS], + pub index: usize, // currently selected item + pub canvas: Gid, + pub authtoken: [u32; 4], +} + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum MenuOpcode { + Redraw, + Rawkeys, + Quit, +} + +impl Menu { + pub fn new(name: &str) -> Menu { + let xns = xous_names::XousNames::new().unwrap(); + let sid = xous::create_server().expect("can't create private menu message server"); + let gam = gam::Gam::new(&xns).expect("can't connect to GAM"); + let authtoken = gam.register_ux( + gam::UxRegistration { + app_name: String::<128>::from_str(name), + ux_type: gam::UxType::Menu, + predictor: None, + listener: sid.to_array(), + redraw_id: MenuOpcode::Redraw.to_u32().unwrap(), + gotinput_id: None, + audioframe_id: None, + rawkeys_id: Some(MenuOpcode::Rawkeys.to_u32().unwrap()), + } + ).expect("couldn't register my Ux element with GAM"); + log::debug!("requesting content canvas for menu"); + let canvas = gam.request_content_canvas(authtoken.unwrap()).expect("couldn't get my content canvas from GAM"); + Menu { + sid, + gam, + xns, + items: [None; MAX_ITEMS], + index: 0, + canvas, + authtoken: authtoken.unwrap(), + } + } + // if successful, returns None, otherwise, the menu item + pub fn add_item(&mut self, new_item: MenuItem) -> Option { + for item in self.items.iter_mut() { + if item.is_none() { + *item = Some(new_item); + return None; + } + } + Some(new_item) + } + pub fn redraw(&mut self) { + // for now, just draw a black rectangle + log::trace!("menu redraw"); + let canvas_size = self.gam.get_canvas_bounds(self.canvas).unwrap(); + self.gam.draw_rounded_rectangle(self.canvas, + RoundedRectangle::new( + Rectangle::new_with_style(Point::new(0, 0), canvas_size, + DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1) + ), 10 + )).unwrap(); + } + pub fn key_event(&mut self, keys: [char; 4]) { + for &k in keys.iter() { + log::trace!("got key '{}'", k); + match k { + '∴' => { + self.gam.relinquish_focus().expect("couldn't relinquish focus of the menu!"); + }, + '←' => { + log::info!("got left arrow"); + } + '→' => { + log::info!("got right arrow"); + } + '↑' => { + log::info!("got up arrow"); + } + '↓' => { + log::info!("got down arrow"); + } + _ => {} + } + } + } +} + +/////// strictly speaking this doesn't have to be in this file, but we make it part of this server so we are guaranteed to have a main menu at all times +pub fn main_menu_thread() { + let mut menu = Menu::new(crate::MAIN_MENU_NAME); + loop { + let msg = xous::receive_message(menu.sid).unwrap(); + log::trace!("|status: Message: {:?}", msg); + match FromPrimitive::from_usize(msg.body.id()) { + Some(MenuOpcode::Redraw) => { + menu.redraw(); + }, + Some(MenuOpcode::Rawkeys) => msg_scalar_unpack!(msg, k1, k2, k3, k4, { + let keys = [ + if let Some(a) = core::char::from_u32(k1 as u32) { + a + } else { + '\u{0000}' + }, + if let Some(a) = core::char::from_u32(k2 as u32) { + a + } else { + '\u{0000}' + }, + if let Some(a) = core::char::from_u32(k3 as u32) { + a + } else { + '\u{0000}' + }, + if let Some(a) = core::char::from_u32(k4 as u32) { + a + } else { + '\u{0000}' + }, + ]; + menu.key_event(keys); + }), + Some(MenuOpcode::Quit) => { + break; + }, + None => { + log::error!("unknown opcode {:?}", msg.body.id()); + } + } + } + log::trace!("menu thread exit, destroying servers"); + // do we want to add a deregister_ux call to the system? + xous::destroy_server(menu.sid).unwrap(); +} \ No newline at end of file diff --git a/services/gam/src/status.rs b/services/gam/src/status.rs index 6302eaede..85333a746 100644 --- a/services/gam/src/status.rs +++ b/services/gam/src/status.rs @@ -162,7 +162,6 @@ pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usi info!("|status: starting main loop"); loop { let msg = xous::receive_message(status_sid).unwrap(); - //let msg = xous::receive_message(status_sid).unwrap(); log::trace!("|status: Message: {:?}", msg); match FromPrimitive::from_usize(msg.body.id()) { Some(StatusOpcode::BattStats) => msg_scalar_unpack!(msg, lo, hi, _, _, { diff --git a/services/gam/src/tokens.rs b/services/gam/src/tokens.rs index 26c508503..08d27f7d3 100755 --- a/services/gam/src/tokens.rs +++ b/services/gam/src/tokens.rs @@ -13,7 +13,7 @@ use xous_ipc::String; // if you add more UxContexts, and you want them authorized by the GAM, add their names here. const TOKEN_SLOTS: usize = 3; -const EXPECTED_BOOT_CONTEXTS: [&'static str; TOKEN_SLOTS] = ["shellchat", "menu", "status"]; +const EXPECTED_BOOT_CONTEXTS: [&'static str; TOKEN_SLOTS] = ["shellchat", "main menu", "status"]; #[derive(Copy, Clone, Debug)] pub(crate) struct NamedToken { diff --git a/services/graphics-server/src/api/shapes.rs b/services/graphics-server/src/api/shapes.rs index b6d658c96..35933d4b9 100644 --- a/services/graphics-server/src/api/shapes.rs +++ b/services/graphics-server/src/api/shapes.rs @@ -151,6 +151,13 @@ impl Rectangle { self.tl.y += offset.y; self.br.y += offset.y; } + pub fn translate_chain(self, offset: Point) -> Rectangle { + Rectangle { + tl: Point::new(self.tl.x + offset.x, self.tl.y + offset.y), + br: Point::new(self.br.x + offset.x, self.br.y + offset.y), + style: self.style, + } + } pub fn normalize(&mut self) { self.br.x -= self.tl.x; self.br.y -= self.tl.y; diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index 810a9b069..f8fc6c07e 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -65,10 +65,13 @@ impl Repl{ rawkeys_id: None, }).expect("couldn't register Ux context for shellchat"); + // we should be the first app running, so get the focus + gam.request_focus(token.unwrap()); + let content = gam.request_content_canvas(token.unwrap()).expect("couldn't get content canvas"); - log::trace!("content canvas {:?}", content); + log::debug!("content canvas {:?}", content); let screensize = gam.get_canvas_bounds(content).expect("couldn't get dimensions of content canvas"); - log::trace!("size {:?}", screensize); + log::debug!("size {:?}", screensize); Repl { input: None, msg: None,