mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
light refactor to use HashMap instead of linearly searched array
GAM predates `std` so we had a linear array for storing UX contexts, that had to be manually grown every time we added an element. Convert that to a Heap-allocated structure.
This commit is contained in:
parent
de4e84abf5
commit
c852f5fbbc
1 changed files with 35 additions and 96 deletions
|
|
@ -88,7 +88,6 @@ pub(crate) struct UxContext {
|
||||||
/// opcode ID for AudioFrame
|
/// opcode ID for AudioFrame
|
||||||
pub audioframe_id: Option<u32>,
|
pub audioframe_id: Option<u32>,
|
||||||
}
|
}
|
||||||
const MAX_UX_CONTEXTS: usize = 8;
|
|
||||||
// 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?
|
// 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";
|
pub const ROOTKEY_MODAL_NAME: &'static str = "rootkeys modal";
|
||||||
const BOOT_CONTEXT_TRUSTLEVEL: u8 = 254;
|
const BOOT_CONTEXT_TRUSTLEVEL: u8 = 254;
|
||||||
|
|
@ -102,7 +101,7 @@ const BOOT_CONTEXT_TRUSTLEVEL: u8 = 254;
|
||||||
*/
|
*/
|
||||||
struct ContextManager {
|
struct ContextManager {
|
||||||
tm: TokenManager,
|
tm: TokenManager,
|
||||||
contexts: [Option<UxContext>; MAX_UX_CONTEXTS],
|
contexts: HashMap::<[u32; 4], UxContext>, // historical note: we used to limit the number of contexts to prevent rogue UX elements, but now we track valid ones in 'tokens.rs', allowing context storage allocations to grow on-demand.
|
||||||
focused_context: Option<[u32; 4]>, // app_token of the app that has I/O focus, if any
|
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
|
last_context: Option<[u32; 4]>, // previously focused context, if any
|
||||||
imef: ime_plugin_api::ImeFrontEnd,
|
imef: ime_plugin_api::ImeFrontEnd,
|
||||||
|
|
@ -123,7 +122,7 @@ impl ContextManager {
|
||||||
imef.hook_listener_callback(imef_cb).expect("couldn't request events from IMEF");
|
imef.hook_listener_callback(imef_cb).expect("couldn't request events from IMEF");
|
||||||
ContextManager {
|
ContextManager {
|
||||||
tm: TokenManager::new(&xns),
|
tm: TokenManager::new(&xns),
|
||||||
contexts: [None; MAX_UX_CONTEXTS],
|
contexts: HashMap::new(),
|
||||||
focused_context: None,
|
focused_context: None,
|
||||||
last_context: None,
|
last_context: None,
|
||||||
imef,
|
imef,
|
||||||
|
|
@ -151,7 +150,6 @@ impl ContextManager {
|
||||||
registration: UxRegistration)
|
registration: UxRegistration)
|
||||||
-> Option<[u32; 4]> {
|
-> Option<[u32; 4]> {
|
||||||
let maybe_token = self.tm.claim_token(registration.app_name.as_str().unwrap());
|
let maybe_token = self.tm.claim_token(registration.app_name.as_str().unwrap());
|
||||||
let mut found_slot = false;
|
|
||||||
if let Some(token) = maybe_token {
|
if let Some(token) = maybe_token {
|
||||||
match registration.ux_type {
|
match registration.ux_type {
|
||||||
UxType::Chat => {
|
UxType::Chat => {
|
||||||
|
|
@ -172,13 +170,7 @@ impl ContextManager {
|
||||||
rawkeys_id: None,
|
rawkeys_id: None,
|
||||||
vibe: false,
|
vibe: false,
|
||||||
};
|
};
|
||||||
for maybe_context in self.contexts.iter_mut() {
|
self.contexts.insert(token, ux_context);
|
||||||
if maybe_context.is_none() {
|
|
||||||
*maybe_context = Some(ux_context);
|
|
||||||
found_slot = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
UxType::Menu => {
|
UxType::Menu => {
|
||||||
let mut menulayout = MenuLayout::init(&gfx, &trng,
|
let mut menulayout = MenuLayout::init(&gfx, &trng,
|
||||||
|
|
@ -199,19 +191,13 @@ impl ContextManager {
|
||||||
rawkeys_id: registration.rawkeys_id,
|
rawkeys_id: registration.rawkeys_id,
|
||||||
vibe: false,
|
vibe: false,
|
||||||
};
|
};
|
||||||
for maybe_context in self.contexts.iter_mut() {
|
|
||||||
if maybe_context.is_none() {
|
|
||||||
*maybe_context = Some(ux_context);
|
|
||||||
found_slot = true;
|
|
||||||
// if this is the main menu, note its app token, as we have to conjure it later on
|
|
||||||
if registration.app_name.as_str().unwrap() == MAIN_MENU_NAME {
|
if registration.app_name.as_str().unwrap() == MAIN_MENU_NAME {
|
||||||
log::debug!("main menu found and registered!");
|
log::debug!("main menu found and registered!");
|
||||||
assert!(self.main_menu_app_token == None, "attempt to double-register main menu handler, this should never happen.");
|
assert!(self.main_menu_app_token == None, "attempt to double-register main menu handler, this should never happen.");
|
||||||
self.main_menu_app_token = Some(token);
|
self.main_menu_app_token = Some(token);
|
||||||
}
|
}
|
||||||
break;
|
self.contexts.insert(token, ux_context);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
UxType::Modal => {
|
UxType::Modal => {
|
||||||
let mut modallayout = ModalLayout::init(&gfx, &trng,
|
let mut modallayout = ModalLayout::init(&gfx, &trng,
|
||||||
|
|
@ -232,13 +218,7 @@ impl ContextManager {
|
||||||
rawkeys_id: registration.rawkeys_id,
|
rawkeys_id: registration.rawkeys_id,
|
||||||
vibe: false,
|
vibe: false,
|
||||||
};
|
};
|
||||||
for maybe_context in self.contexts.iter_mut() {
|
self.contexts.insert(token, ux_context);
|
||||||
if maybe_context.is_none() {
|
|
||||||
*maybe_context = Some(ux_context);
|
|
||||||
found_slot = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -248,29 +228,15 @@ impl ContextManager {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if found_slot {
|
|
||||||
maybe_token
|
maybe_token
|
||||||
} else {
|
|
||||||
// Note: for the most paranoid modes, you probably want exactly as many UX contexts
|
|
||||||
// as you expect to use. This cap helps prevent rouge processes from registering
|
|
||||||
// UX contexts that it shouldn't.
|
|
||||||
//
|
|
||||||
// That being said, if you are to run "generic apps from the internet" you can't
|
|
||||||
// put a proper bound on this number.
|
|
||||||
log::error!("Ran out of UX contexts: try increasing MAX_UX_CONTEXTS in gam/main.rs");
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub(crate) fn get_content_canvas(&self, token: [u32; 4]) -> Option<Gid> {
|
pub(crate) fn get_content_canvas(&self, token: [u32; 4]) -> Option<Gid> {
|
||||||
for maybe_context in self.contexts.iter() {
|
if let Some(context) = self.contexts.get(&token) {
|
||||||
if let Some(context) = maybe_context {
|
|
||||||
if context.app_token == token {
|
|
||||||
return Some(context.layout.get_content_canvas());
|
return Some(context.layout.get_content_canvas());
|
||||||
}
|
} else {
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
}
|
||||||
pub(crate) fn set_canvas_height(&mut self,
|
pub(crate) fn set_canvas_height(&mut self,
|
||||||
gfx: &graphics_server::Gfx,
|
gfx: &graphics_server::Gfx,
|
||||||
gam_token: [u32; 4],
|
gam_token: [u32; 4],
|
||||||
|
|
@ -278,14 +244,12 @@ impl ContextManager {
|
||||||
status_canvas: &Canvas,
|
status_canvas: &Canvas,
|
||||||
canvases: &mut HashMap<Gid, Canvas>) -> Option<Point> {
|
canvases: &mut HashMap<Gid, Canvas>) -> Option<Point> {
|
||||||
|
|
||||||
for maybe_context in self.contexts.iter_mut() {
|
for context in self.contexts.values_mut() {
|
||||||
if let Some(context) = maybe_context {
|
|
||||||
if context.gam_token == gam_token {
|
if context.gam_token == gam_token {
|
||||||
let result = context.layout.resize_height(gfx, new_height, status_canvas, canvases).expect("couldn't adjust height of active Ux context");
|
let result = context.layout.resize_height(gfx, new_height, status_canvas, canvases).expect("couldn't adjust height of active Ux context");
|
||||||
return Some(result)
|
return Some(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
// hmmm...feels wrong to have basically a dupe of the above. Maybe this abstraction needs to be cleaned up a bit.
|
// hmmm...feels wrong to have basically a dupe of the above. Maybe this abstraction needs to be cleaned up a bit.
|
||||||
|
|
@ -296,35 +260,18 @@ impl ContextManager {
|
||||||
status_canvas: &Canvas,
|
status_canvas: &Canvas,
|
||||||
canvases: &mut HashMap<Gid, Canvas>) -> Option<Point> {
|
canvases: &mut HashMap<Gid, Canvas>) -> Option<Point> {
|
||||||
|
|
||||||
for maybe_context in self.contexts.iter_mut() {
|
if let Some(context) = self.contexts.get_mut(&app_token) {
|
||||||
if let Some(context) = maybe_context {
|
|
||||||
if context.app_token == app_token {
|
|
||||||
let result = context.layout.resize_height(gfx, new_height, status_canvas, canvases).expect("couldn't adjust height of active Ux context");
|
let result = context.layout.resize_height(gfx, new_height, status_canvas, canvases).expect("couldn't adjust height of active Ux context");
|
||||||
return Some(result)
|
Some(result)
|
||||||
}
|
} else {
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
}
|
||||||
fn get_context_by_token_mut(&'_ mut self, token: [u32; 4]) -> Option<&'_ mut UxContext> {
|
fn get_context_by_token_mut(&'_ mut self, token: [u32; 4]) -> Option<&'_ mut UxContext> {
|
||||||
for maybe_context in self.contexts.iter_mut() {
|
self.contexts.get_mut(&token)
|
||||||
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> {
|
fn get_context_by_token(&'_ self, token: [u32; 4]) -> Option<&'_ UxContext> {
|
||||||
for maybe_context in self.contexts.iter() {
|
self.contexts.get(&token)
|
||||||
if let Some(context) = maybe_context {
|
|
||||||
if context.app_token == token {
|
|
||||||
return Some(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
pub(crate) fn activate(&mut self,
|
pub(crate) fn activate(&mut self,
|
||||||
gfx: &graphics_server::Gfx,
|
gfx: &graphics_server::Gfx,
|
||||||
|
|
@ -446,16 +393,12 @@ impl ContextManager {
|
||||||
}
|
}
|
||||||
pub(crate) fn redraw(&self) -> Result<(), xous::Error> { // redraws the currently focused context
|
pub(crate) fn redraw(&self) -> Result<(), xous::Error> { // redraws the currently focused context
|
||||||
if let Some(token) = self.focused_app() {
|
if let Some(token) = self.focused_app() {
|
||||||
for maybe_context in self.contexts.iter() {
|
if let Some(context) = self.contexts.get(&token) {
|
||||||
if let Some(context) = maybe_context {
|
|
||||||
if token == context.app_token {
|
|
||||||
log::trace!("redraw msg to {}, id {}", context.listener, context.redraw_id);
|
log::trace!("redraw msg to {}, id {}", context.listener, context.redraw_id);
|
||||||
return xous::send_message(context.listener,
|
return xous::send_message(context.listener,
|
||||||
xous::Message::new_scalar(context.redraw_id as usize, 0, 0, 0, 0)
|
xous::Message::new_scalar(context.redraw_id as usize, 0, 0, 0, 0)
|
||||||
).map(|_| ())
|
).map(|_| ())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return Err(xous::Error::UseBeforeInit)
|
return Err(xous::Error::UseBeforeInit)
|
||||||
}
|
}
|
||||||
|
|
@ -478,16 +421,12 @@ impl ContextManager {
|
||||||
}
|
}
|
||||||
pub(crate) fn forward_input(&self, input: String::<4000>) -> Result<(), xous::Error> {
|
pub(crate) fn forward_input(&self, input: String::<4000>) -> Result<(), xous::Error> {
|
||||||
if let Some(token) = self.focused_app() {
|
if let Some(token) = self.focused_app() {
|
||||||
for maybe_context in self.contexts.iter() {
|
if let Some(context) = self.contexts.get(&token) {
|
||||||
if let Some(context) = maybe_context {
|
|
||||||
if token == context.app_token {
|
|
||||||
if let Some(input_op) = context.gotinput_id {
|
if let Some(input_op) = context.gotinput_id {
|
||||||
let buf = Buffer::into_buf(input).or(Err(xous::Error::InternalError)).unwrap();
|
let buf = Buffer::into_buf(input).or(Err(xous::Error::InternalError)).unwrap();
|
||||||
return buf.send(context.listener, input_op).map(|_| ())
|
return buf.send(context.listener, input_op).map(|_| ())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return Err(xous::Error::UseBeforeInit)
|
return Err(xous::Error::UseBeforeInit)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue