xous-core/services/gam/src/main.rs

553 lines
31 KiB
Rust
Raw Normal View History

#![cfg_attr(baremetal, no_main)]
#![cfg_attr(baremetal, no_std)]
mod api;
use api::*;
mod canvas;
use canvas::*;
2021-06-15 04:50:29 +00:00
mod tokens;
use tokens::*;
mod layouts;
use layouts::*;
mod contexts;
use contexts::*;
2021-06-15 04:50:29 +00:00
2021-01-18 20:54:54 +00:00
use graphics_server::*;
use xous_ipc::{Buffer, String};
2021-04-04 21:47:17 +00:00
use api::Opcode;
use xous::{msg_scalar_unpack, msg_blocking_scalar_unpack};
use gam::{MAIN_MENU_NAME, ROOTKEY_MODAL_NAME};
2021-02-18 20:02:29 +00:00
use log::info;
use std::collections::HashMap;
use num_traits::*;
use core::sync::atomic::{AtomicU32, Ordering};
/// This sets the initial app focus on boot
const INITIAL_APP_FOCUS: &'static str = gam::APP_NAME_SHELLCHAT;
static CB_TO_MAIN_CONN: AtomicU32 = AtomicU32::new(0);
fn imef_cb(s: String::<4000>) {
if CB_TO_MAIN_CONN.load(Ordering::Relaxed) != 0 {
let cb_to_main_conn = CB_TO_MAIN_CONN.load(Ordering::Relaxed);
let buf = xous_ipc::Buffer::into_buf(s).or(Err(xous::Error::InternalError)).unwrap();
buf.lend(cb_to_main_conn, Opcode::InputLine.to_u32().unwrap()).unwrap();
}
}
2021-01-17 13:58:02 +00:00
#[xous::xous_main]
fn xmain() -> ! {
log_server::init_wait().unwrap();
2022-01-24 17:57:16 +00:00
log::set_max_level(log::LevelFilter::Info);
2021-04-04 21:47:17 +00:00
info!("my PID is {}", xous::process::id());
2021-01-17 13:58:02 +00:00
2021-03-29 18:13:23 +00:00
let xns = xous_names::XousNames::new().unwrap();
// unlimited connections allowed; this is a gateway server
let gam_sid = xns.register_name(api::SERVER_NAME_GAM, None).expect("can't register server");
CB_TO_MAIN_CONN.store(xous::connect(gam_sid).unwrap(), Ordering::Relaxed);
2021-04-04 21:47:17 +00:00
log::trace!("starting up...");
2021-03-28 10:14:41 +00:00
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
2021-01-17 13:58:02 +00:00
2021-04-04 21:47:17 +00:00
let gfx = graphics_server::Gfx::new(&xns).expect("can't connect to GFX");
let trng = trng::Trng::new(&xns).expect("can't connect to TRNG");
2021-01-17 13:58:02 +00:00
let mut context_mgr = ContextManager::new(&xns);
// a map of canvases accessable by Gid
let mut canvases: HashMap<Gid, Canvas> = HashMap::new();
2021-04-04 21:47:17 +00:00
let screensize = gfx.screen_size().expect("Couldn't get screen size");
2021-06-15 04:50:29 +00:00
// the status canvas is special -- there can only be one, and it is ultimately trusted
let mut status_canvas = Canvas::new(
Rectangle::new_coords(
0, 0, screensize.x,
gfx.glyph_height_hint(GlyphStyle::Cjk).expect("couldn't get glyph height") as i16 * 2),
255, &trng, None, crate::api::CanvasType::Status
2021-06-15 04:50:29 +00:00
).expect("couldn't create status canvas");
status_canvas.set_onscreen(true);
status_canvas.set_drawable(true);
canvases.insert(status_canvas.gid(), status_canvas);
canvases = recompute_canvases(&canvases, Rectangle::new(Point::new(0, 0), screensize));
// initialize the status bar -- this needs to start late, after the IMEF and most other things are initialized
// this used to be domiciled in the GAM, but we split it out because this started to pull too much functionality
// into the GAM and was causing circular crate conflicts with sub-functions that the status bar relies upon.
// we do a hack to try and push a GID to the status bar "securely": we introduce a race condition where we hope
// that the GAM is the first thing to talk to the status bar, and the first message is its GID to render on.
// generally should be OK, because during boot, all processes are trusted...
let status_gid = status_canvas.gid().gid();
log::trace!("initializing status bar with gid {:?}", status_gid);
let status_conn = xns.request_connection_blocking("_Status bar GID receiver_").expect("couldn't connect to status bar GID receiver");
xous::send_message(status_conn,
xous::Message::new_scalar(0, // message type doesn't matter because there is only one message it should ever receive
status_gid[0] as usize, status_gid[1] as usize, status_gid[2] as usize, status_gid[3] as usize
)
).expect("couldn't set status GID");
2021-03-10 20:43:58 +00:00
// a random number we can use to identify ourselves between API calls
let gam_token = [trng.get_u32().unwrap(), trng.get_u32().unwrap(), trng.get_u32().unwrap(), trng.get_u32().unwrap()];
let mut powerdown_requested = false;
2021-03-28 20:35:11 +00:00
let mut last_time: u64 = ticktimer.elapsed_ms();
let mut did_test = false; // allow one go at the test pattern
2021-04-04 21:47:17 +00:00
log::trace!("entering main loop");
#[cfg(not(any(target_os = "none", target_os = "xous")))]
{
log::info!("********************************************************************************");
log::info!("USAGE:");
log::info!(" `Home` key to bring up menu; arrow keys to go up/down; `Home` to select again");
log::info!(" `F1`-`F4` to pick predictions; `F5` and `F6` generate test unicode characters");
log::info!(" Otherwise type in the GUI window; `help` for the current command list");
log::info!(" ^C in the console window (this window) to quit");
log::info!("********************************************************************************");
}
loop {
let mut msg = xous::receive_message(gam_sid).unwrap();
2021-04-04 21:47:17 +00:00
log::trace!("Message: {:?}", msg);
match FromPrimitive::from_usize(msg.body.id()) {
Some(Opcode::ClearCanvas) => {
msg_scalar_unpack!(msg, g0, g1, g2, g3, {
let gid = Gid::new([g0 as _, g1 as _, g2 as _, g3 as _]);
match canvases.get(&gid) {
Some(c) => {
let mut rect = c.clip_rect();
rect.style = DrawStyle {fill_color: Some(PixelColor::Light), stroke_color: None, stroke_width: 0,};
gfx.draw_rectangle(rect).expect("can't clear canvas");
},
2021-04-04 21:47:17 +00:00
None => info!("attempt to clear bogus canvas, ignored."),
}
2021-04-04 21:47:17 +00:00
});
}
Some(Opcode::GetCanvasBounds) => {
msg_blocking_scalar_unpack!(msg, g0, g1, g2, g3, {
let gid = Gid::new([g0 as _, g1 as _, g2 as _, g3 as _]);
match canvases.get(&gid) {
Some(c) => {
let mut rect = c.clip_rect();
rect.normalize(); // normalize to 0,0 coordinates
2021-06-18 16:25:18 +00:00
log::trace!("getcanvasbounds: {:?}", rect);
2021-04-04 21:47:17 +00:00
xous::return_scalar2(msg.sender,
rect.tl.into(),
rect.br.into(),
2021-04-04 21:47:17 +00:00
).expect("couldn't return canvas bounds");
2021-03-01 06:40:39 +00:00
},
2021-04-04 21:47:17 +00:00
None => info!("attempt to get bounds on bogus canvas gid {:?}, {:?} ignored.", gid, msg),
}
2021-04-04 21:47:17 +00:00
})
}
Some(Opcode::PowerDownRequest) => {
msg_blocking_scalar_unpack!(msg, _, _, _, _, {
powerdown_requested = true;
2021-04-04 21:47:17 +00:00
gfx.draw_sleepscreen().expect("couldn't draw sleep screen");
// a screen flush is part of the draw_sleepscreen abstraction
2021-04-04 21:47:17 +00:00
xous::return_scalar(msg.sender, 1).expect("couldn't confirm power down UI request");
})
}
2021-04-22 10:07:41 +00:00
Some(Opcode::ShipModeBlankRequest) => {
msg_blocking_scalar_unpack!(msg, _, _, _, _, {
powerdown_requested = true;
gfx.draw_rectangle(Rectangle::new_with_style(Point::new(0,0), screensize, DrawStyle::new(PixelColor::Light, PixelColor::Light, 0))).expect("couldn't clear screen");
gfx.flush().expect("couldn't refresh blank screen");
xous::return_scalar(msg.sender, 1).expect("couldn't confirm power down UI request");
})
}
2021-04-04 21:47:17 +00:00
Some(Opcode::Redraw) => {
msg_scalar_unpack!(msg, _, _, _, _, {
log::trace!("redraw message received");
if powerdown_requested {
continue; // don't allow any redraws if a powerdown is requested
}
let elapsed_time = ticktimer.elapsed_ms();
if elapsed_time - last_time > 33 { // rate limit updates, no point in going faster than the eye can see
last_time = elapsed_time;
2021-06-18 09:40:00 +00:00
if deface(&gfx, &trng, &mut canvases) {
2021-08-01 07:06:28 +00:00
// we keep this here because it's a fail-safe in case prior routines missed an edge case. shoot out a warning noting the issue.
log::warn!("canvases were not defaced in order. running a defacement, but this could result in drawing optimizations failing.");
// try to redraw the trusted foreground apps after a defacement
light refactor to the GAM to solve a deadlock issue This is a "no good choice" situation but there is a problem with progress bars. You want progress bars to update quickly, and you don't want to have to context switch out for the entire graphics stack to come back to you to approve the drawing of your progress bar update. On the other hand, you don't want to short-circuit all the checks on drawing stuff to the screen for the sake of progress bars -- right now, there are a number of checks in place to make sure you can't draw stuff on parts of the screen you shouldn't, to avoid fake messages and dialog boxes. I had managed to work out a way to get a dialog box to redraw without violating the security rules -- each redraw is checked by the defacing routine -- however, due to the way I was computing the height of the modal I was causing the dialog box to be resized to its largest size and then shrunk again to fit the text every iteration. This in essence also caused the defacement routine to run repeatedly, which caused the GAM to also issue repeated redraw requests to the main loop. These redraw requests can't be serviced until the task being tracked by the progress bar is completed. As the progress bar gets redrawn almost a hundred times as part of the task, roughly a hundred messages arrive in the server's queue before it can handle a single one. This results in a deadlock situation on completion of the progress bar, because we want to fire off a message to ourselves to go to the next state in the Ux routine. There are a few possible solutions to this: 1. Multi thread the heck out of the routine doing the progress bar, so the dialog box and the slider are each in their own threads with their own responders and then the thing doing the computation is doing some Mutex-guarded-Arc thing. 2. Do some very unsafe bodge to allow sliders to do direct draws that are unchecked 3. Add a check so that if the size of the dialog box doesn't change, defacement isn't recomputed and thus redraw messages aren't sent back. (1) doesn't work for a number of reasons -- in a general case, it's probably OK, but it does cause a lot more memory usage (and we are lean on memory); but more importantly, for this use case, our routine is interacting with CSRs and those don't implement Send so you can't pass them into a thread via a closure. You could maybe put the dialog boxes themselves into an Arc Mutex thing but because this routine was heavily coded to re-use the heavyweight Ux resources this becomes a bit cumersome to code. (2) is terrible (3) ended up working and what is in this commit. I think it's OK because a progress bar generally doesn't require changing the size of a window to update its state; therefore the redraw is deface-safe (since you aren't changing the bounds of what you were already approved to draw in). Of course, if you have some sort of a progress bar implementation that changes the size of the messages reported up top so that the dialog box has to change size over and over again, you still end up in the deadlock situation!
2021-08-11 17:25:35 +00:00
log::trace!("deface redraw");
2021-06-18 09:40:00 +00:00
context_mgr.redraw().expect("couldn't redraw after defacement");
}
log::trace!("flushing...");
gfx.flush().expect("couldn't flush buffer to screen");
for (_, c) in canvases.iter_mut() {
2021-06-18 09:40:00 +00:00
c.do_flushed().expect("couldn't update flushed state");
}
}
2021-04-04 21:47:17 +00:00
})
}
2021-04-04 21:47:17 +00:00
Some(Opcode::RenderTextView) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut tv = buffer.to_original::<TextView, _>().unwrap();
log::trace!("rendertextview {:?}", tv);
match tv.get_op() {
TextOp::Nop => (),
light refactor to the GAM to solve a deadlock issue This is a "no good choice" situation but there is a problem with progress bars. You want progress bars to update quickly, and you don't want to have to context switch out for the entire graphics stack to come back to you to approve the drawing of your progress bar update. On the other hand, you don't want to short-circuit all the checks on drawing stuff to the screen for the sake of progress bars -- right now, there are a number of checks in place to make sure you can't draw stuff on parts of the screen you shouldn't, to avoid fake messages and dialog boxes. I had managed to work out a way to get a dialog box to redraw without violating the security rules -- each redraw is checked by the defacing routine -- however, due to the way I was computing the height of the modal I was causing the dialog box to be resized to its largest size and then shrunk again to fit the text every iteration. This in essence also caused the defacement routine to run repeatedly, which caused the GAM to also issue repeated redraw requests to the main loop. These redraw requests can't be serviced until the task being tracked by the progress bar is completed. As the progress bar gets redrawn almost a hundred times as part of the task, roughly a hundred messages arrive in the server's queue before it can handle a single one. This results in a deadlock situation on completion of the progress bar, because we want to fire off a message to ourselves to go to the next state in the Ux routine. There are a few possible solutions to this: 1. Multi thread the heck out of the routine doing the progress bar, so the dialog box and the slider are each in their own threads with their own responders and then the thing doing the computation is doing some Mutex-guarded-Arc thing. 2. Do some very unsafe bodge to allow sliders to do direct draws that are unchecked 3. Add a check so that if the size of the dialog box doesn't change, defacement isn't recomputed and thus redraw messages aren't sent back. (1) doesn't work for a number of reasons -- in a general case, it's probably OK, but it does cause a lot more memory usage (and we are lean on memory); but more importantly, for this use case, our routine is interacting with CSRs and those don't implement Send so you can't pass them into a thread via a closure. You could maybe put the dialog boxes themselves into an Arc Mutex thing but because this routine was heavily coded to re-use the heavyweight Ux resources this becomes a bit cumersome to code. (2) is terrible (3) ended up working and what is in this commit. I think it's OK because a progress bar generally doesn't require changing the size of a window to update its state; therefore the redraw is deface-safe (since you aren't changing the bounds of what you were already approved to draw in). Of course, if you have some sort of a progress bar implementation that changes the size of the messages reported up top so that the dialog box has to change size over and over again, you still end up in the deadlock situation!
2021-08-11 17:25:35 +00:00
TextOp::Render => {
if tv.invert & tv.token.is_some() {
// an inverted text can only be made by secure processes. check that it has a valid token.
if !context_mgr.is_token_valid(tv.token.unwrap()) {
log::error!("Attempt to draw inverted text without valid credentials. Aborting.");
continue;
}
}
2021-04-04 21:47:17 +00:00
log::trace!("render request for {:?}", tv);
2021-04-04 21:47:17 +00:00
if let Some(canvas) = canvases.get_mut(&tv.get_canvas_gid()) {
tv.set_dry_run(!canvas.is_onscreen());
// if we're requesting inverted text, this better be a "trusted canvas"
// BOOT_CONTEXT_TRUSTLEVEL is reserved for the "status bar"
// BOOT_CONTEXT_TRUSTLEVEL - 1 is where e.g. password modal dialog boxes end up
if tv.invert & (canvas.trust_level() < BOOT_CONTEXT_TRUSTLEVEL - 1) {
log::error!("Attempt to draw inverted text without sufficient trust level: {}. Aborting.", canvas.trust_level());
continue;
}
2021-04-04 21:47:17 +00:00
// first, figure out if we should even be drawing to this canvas.
if canvas.is_drawable() {
2021-04-04 21:47:17 +00:00
// set the clip rectangle according to the canvas' location
light refactor to the GAM to solve a deadlock issue This is a "no good choice" situation but there is a problem with progress bars. You want progress bars to update quickly, and you don't want to have to context switch out for the entire graphics stack to come back to you to approve the drawing of your progress bar update. On the other hand, you don't want to short-circuit all the checks on drawing stuff to the screen for the sake of progress bars -- right now, there are a number of checks in place to make sure you can't draw stuff on parts of the screen you shouldn't, to avoid fake messages and dialog boxes. I had managed to work out a way to get a dialog box to redraw without violating the security rules -- each redraw is checked by the defacing routine -- however, due to the way I was computing the height of the modal I was causing the dialog box to be resized to its largest size and then shrunk again to fit the text every iteration. This in essence also caused the defacement routine to run repeatedly, which caused the GAM to also issue repeated redraw requests to the main loop. These redraw requests can't be serviced until the task being tracked by the progress bar is completed. As the progress bar gets redrawn almost a hundred times as part of the task, roughly a hundred messages arrive in the server's queue before it can handle a single one. This results in a deadlock situation on completion of the progress bar, because we want to fire off a message to ourselves to go to the next state in the Ux routine. There are a few possible solutions to this: 1. Multi thread the heck out of the routine doing the progress bar, so the dialog box and the slider are each in their own threads with their own responders and then the thing doing the computation is doing some Mutex-guarded-Arc thing. 2. Do some very unsafe bodge to allow sliders to do direct draws that are unchecked 3. Add a check so that if the size of the dialog box doesn't change, defacement isn't recomputed and thus redraw messages aren't sent back. (1) doesn't work for a number of reasons -- in a general case, it's probably OK, but it does cause a lot more memory usage (and we are lean on memory); but more importantly, for this use case, our routine is interacting with CSRs and those don't implement Send so you can't pass them into a thread via a closure. You could maybe put the dialog boxes themselves into an Arc Mutex thing but because this routine was heavily coded to re-use the heavyweight Ux resources this becomes a bit cumersome to code. (2) is terrible (3) ended up working and what is in this commit. I think it's OK because a progress bar generally doesn't require changing the size of a window to update its state; therefore the redraw is deface-safe (since you aren't changing the bounds of what you were already approved to draw in). Of course, if you have some sort of a progress bar implementation that changes the size of the messages reported up top so that the dialog box has to change size over and over again, you still end up in the deadlock situation!
2021-08-11 17:25:35 +00:00
let base_clip_rect = canvas.clip_rect();
tv.clip_rect = Some(base_clip_rect.into());
2021-04-04 21:47:17 +00:00
// you have to clone the tv object, because if you don't the same block of
// memory gets passed on to the graphics_server(). Which is efficient, but,
// the call will automatically Drop() the memory, which causes a panic when
// this routine returns.
let mut tv_clone = tv.clone();
// issue the draw command
gfx.draw_textview(&mut tv_clone).expect("text view draw could not complete.");
// copy back the fields that we want to be mutable
tv.cursor = tv_clone.cursor;
tv.bounds_computed = tv_clone.bounds_computed;
let ret = api::Return::RenderReturn(tv);
buffer.replace(ret).unwrap();
if canvas.is_onscreen() {
canvas.do_drawn().expect("couldn't set canvas to drawn");
}
} else {
2021-07-28 15:15:53 +00:00
log::debug!("attempt to draw TextView on non-drawable canvas. Not fatal, but request ignored. {:?}", tv);
2021-07-27 09:30:37 +00:00
let ret = api::Return::NotCurrentlyDrawable;
buffer.replace(ret).unwrap();
}
2021-04-04 21:47:17 +00:00
} else {
info!("bogus GID {:?} in TextView {}, not doing anything in response to draw request.", tv.get_canvas_gid(), tv.text);
// silently fail if a bogus Gid is given???
}
2021-04-04 21:47:17 +00:00
},
light refactor to the GAM to solve a deadlock issue This is a "no good choice" situation but there is a problem with progress bars. You want progress bars to update quickly, and you don't want to have to context switch out for the entire graphics stack to come back to you to approve the drawing of your progress bar update. On the other hand, you don't want to short-circuit all the checks on drawing stuff to the screen for the sake of progress bars -- right now, there are a number of checks in place to make sure you can't draw stuff on parts of the screen you shouldn't, to avoid fake messages and dialog boxes. I had managed to work out a way to get a dialog box to redraw without violating the security rules -- each redraw is checked by the defacing routine -- however, due to the way I was computing the height of the modal I was causing the dialog box to be resized to its largest size and then shrunk again to fit the text every iteration. This in essence also caused the defacement routine to run repeatedly, which caused the GAM to also issue repeated redraw requests to the main loop. These redraw requests can't be serviced until the task being tracked by the progress bar is completed. As the progress bar gets redrawn almost a hundred times as part of the task, roughly a hundred messages arrive in the server's queue before it can handle a single one. This results in a deadlock situation on completion of the progress bar, because we want to fire off a message to ourselves to go to the next state in the Ux routine. There are a few possible solutions to this: 1. Multi thread the heck out of the routine doing the progress bar, so the dialog box and the slider are each in their own threads with their own responders and then the thing doing the computation is doing some Mutex-guarded-Arc thing. 2. Do some very unsafe bodge to allow sliders to do direct draws that are unchecked 3. Add a check so that if the size of the dialog box doesn't change, defacement isn't recomputed and thus redraw messages aren't sent back. (1) doesn't work for a number of reasons -- in a general case, it's probably OK, but it does cause a lot more memory usage (and we are lean on memory); but more importantly, for this use case, our routine is interacting with CSRs and those don't implement Send so you can't pass them into a thread via a closure. You could maybe put the dialog boxes themselves into an Arc Mutex thing but because this routine was heavily coded to re-use the heavyweight Ux resources this becomes a bit cumersome to code. (2) is terrible (3) ended up working and what is in this commit. I think it's OK because a progress bar generally doesn't require changing the size of a window to update its state; therefore the redraw is deface-safe (since you aren't changing the bounds of what you were already approved to draw in). Of course, if you have some sort of a progress bar implementation that changes the size of the messages reported up top so that the dialog box has to change size over and over again, you still end up in the deadlock situation!
2021-08-11 17:25:35 +00:00
TextOp::ComputeBounds => {
log::trace!("render request for {:?}", tv);
tv.set_dry_run(true);
if tv.clip_rect.is_none() {
// fill in the clip rect from the canvas
if let Some(canvas) = canvases.get_mut(&tv.get_canvas_gid()) {
// set the clip rectangle according to the canvas' location
let mut base_clip_rect = canvas.clip_rect();
base_clip_rect.normalize();
tv.clip_rect = Some(base_clip_rect.into());
} else {
info!("bogus GID {:?} in TextView {}, not doing anything in response to draw request.", tv.get_canvas_gid(), tv.text);
// silently fail if a bogus Gid is given???
continue;
}
}
let mut tv_clone = tv.clone();
// issue the draw command
gfx.draw_textview(&mut tv_clone).expect("text view draw could not complete.");
// copy back the fields that we want to be mutable
log::trace!("got computed cursor of {:?}, bounds {:?}", tv_clone.cursor, tv_clone.bounds_computed);
tv.cursor = tv_clone.cursor;
tv.bounds_computed = tv_clone.bounds_computed;
let ret = api::Return::RenderReturn(tv);
buffer.replace(ret).unwrap();
}
2021-04-04 21:47:17 +00:00
};
}
Some(Opcode::SetCanvasBounds) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut cb = buffer.to_original::<SetCanvasBoundsRequest, _>().unwrap();
2021-06-18 16:25:18 +00:00
log::trace!("SetCanvasBoundsRequest {:?}", cb);
2021-04-04 21:47:17 +00:00
let granted = if cb.token_type == TokenType::Gam {
context_mgr.set_canvas_height(&gfx, cb.token, cb.requested.y, &status_canvas, &mut canvases)
} else {
context_mgr.set_canvas_height_app_token(&gfx, cb.token, cb.requested.y, &status_canvas, &mut canvases)
};
if granted.is_some() {
// recompute the canvas orders based on the new layout
let recomp_canvases = recompute_canvases(&canvases, Rectangle::new(Point::new(0, 0), screensize));
2021-06-18 09:40:00 +00:00
canvases = recomp_canvases;
light refactor to the GAM to solve a deadlock issue This is a "no good choice" situation but there is a problem with progress bars. You want progress bars to update quickly, and you don't want to have to context switch out for the entire graphics stack to come back to you to approve the drawing of your progress bar update. On the other hand, you don't want to short-circuit all the checks on drawing stuff to the screen for the sake of progress bars -- right now, there are a number of checks in place to make sure you can't draw stuff on parts of the screen you shouldn't, to avoid fake messages and dialog boxes. I had managed to work out a way to get a dialog box to redraw without violating the security rules -- each redraw is checked by the defacing routine -- however, due to the way I was computing the height of the modal I was causing the dialog box to be resized to its largest size and then shrunk again to fit the text every iteration. This in essence also caused the defacement routine to run repeatedly, which caused the GAM to also issue repeated redraw requests to the main loop. These redraw requests can't be serviced until the task being tracked by the progress bar is completed. As the progress bar gets redrawn almost a hundred times as part of the task, roughly a hundred messages arrive in the server's queue before it can handle a single one. This results in a deadlock situation on completion of the progress bar, because we want to fire off a message to ourselves to go to the next state in the Ux routine. There are a few possible solutions to this: 1. Multi thread the heck out of the routine doing the progress bar, so the dialog box and the slider are each in their own threads with their own responders and then the thing doing the computation is doing some Mutex-guarded-Arc thing. 2. Do some very unsafe bodge to allow sliders to do direct draws that are unchecked 3. Add a check so that if the size of the dialog box doesn't change, defacement isn't recomputed and thus redraw messages aren't sent back. (1) doesn't work for a number of reasons -- in a general case, it's probably OK, but it does cause a lot more memory usage (and we are lean on memory); but more importantly, for this use case, our routine is interacting with CSRs and those don't implement Send so you can't pass them into a thread via a closure. You could maybe put the dialog boxes themselves into an Arc Mutex thing but because this routine was heavily coded to re-use the heavyweight Ux resources this becomes a bit cumersome to code. (2) is terrible (3) ended up working and what is in this commit. I think it's OK because a progress bar generally doesn't require changing the size of a window to update its state; therefore the redraw is deface-safe (since you aren't changing the bounds of what you were already approved to draw in). Of course, if you have some sort of a progress bar implementation that changes the size of the messages reported up top so that the dialog box has to change size over and over again, you still end up in the deadlock situation!
2021-08-11 17:25:35 +00:00
log::trace!("canvas bounds redraw");
context_mgr.redraw().expect("can't redraw after new canvas bounds");
2021-04-04 21:47:17 +00:00
}
cb.granted = granted;
2021-04-04 21:47:17 +00:00
let ret = api::Return::SetCanvasBoundsReturn(cb);
2021-06-18 16:25:18 +00:00
log::trace!("returning {:?}", cb);
2021-04-04 21:47:17 +00:00
buffer.replace(ret).unwrap();
}
Some(Opcode::RequestContentCanvas) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let req = buffer.to_original::<[u32; 4], _>().unwrap();
2021-06-18 16:25:18 +00:00
log::trace!("RequestContentCanvas {:?}", req);
2021-04-04 21:47:17 +00:00
let ret = api::Return::ContentCanvasReturn(context_mgr.get_content_canvas(req));
2021-04-04 21:47:17 +00:00
buffer.replace(ret).unwrap();
}
Some(Opcode::RenderObject) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let obj = buffer.to_original::<GamObject, _>().unwrap();
2021-06-18 16:25:18 +00:00
log::trace!("renderobject {:?}", obj);
2021-04-04 21:47:17 +00:00
if let Some(canvas) = canvases.get_mut(&obj.canvas) {
// first, figure out if we should even be drawing to this canvas.
if canvas.is_drawable() && canvas.is_onscreen() {
2021-04-04 21:47:17 +00:00
match obj.obj {
GamObjectType::Line(mut line) => {
line.translate(canvas.clip_rect().tl);
line.translate(canvas.pan_offset());
gfx.draw_line_clipped(
line,
canvas.clip_rect(),
).expect("couldn't draw line");
},
GamObjectType::Circ(mut circ) => {
circ.translate(canvas.clip_rect().tl);
circ.translate(canvas.pan_offset());
gfx.draw_circle_clipped(
circ,
canvas.clip_rect(),
).expect("couldn't draw circle");
},
GamObjectType::Rect(mut rect) => {
rect.translate(canvas.clip_rect().tl);
rect.translate(canvas.pan_offset());
gfx.draw_rectangle_clipped(
rect,
canvas.clip_rect(),
).expect("couldn't draw rectangle");
},
GamObjectType::RoundRect(mut rr) => {
rr.translate(canvas.clip_rect().tl);
rr.translate(canvas.pan_offset());
gfx.draw_rounded_rectangle_clipped(
rr,
canvas.clip_rect(),
).expect("couldn't draw rounded rectangle");
}
}
2021-04-04 21:47:17 +00:00
canvas.do_drawn().expect("couldn't set canvas to drawn");
} else {
2021-07-28 15:15:53 +00:00
log::debug!("attempt to draw Object on non-drawable canvas. Not fatal, but request ignored: {:?}", obj);
}
2021-04-04 21:47:17 +00:00
} else {
info!("bogus GID in Object, not doing anything in response to draw request.");
}
log::trace!("leaving RenderObject");
}
Some(Opcode::RenderObjectList) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let obj_ipc = buffer.to_original::<GamObjectList, _>().unwrap();
if let Some(canvas) = canvases.get_mut(&obj_ipc.canvas) {
// first, figure out if we should even be drawing to this canvas.
if canvas.is_drawable() && canvas.is_onscreen() {
let mut obj_list = ClipObjectList::default();
for item in obj_ipc.list.iter() {
if let Some(obj) = item {
match obj {
GamObjectType::Line(mut line) => {
line.translate(canvas.clip_rect().tl);
line.translate(canvas.pan_offset());
obj_list.push(ClipObjectType::Line(line), canvas.clip_rect()).unwrap();
},
GamObjectType::Circ(mut circ) => {
circ.translate(canvas.clip_rect().tl);
circ.translate(canvas.pan_offset());
obj_list.push(ClipObjectType::Circ(circ), canvas.clip_rect()).unwrap();
},
GamObjectType::Rect(mut rect) => {
rect.translate(canvas.clip_rect().tl);
rect.translate(canvas.pan_offset());
obj_list.push(ClipObjectType::Rect(rect), canvas.clip_rect()).unwrap();
},
GamObjectType::RoundRect(mut rr) => {
rr.translate(canvas.clip_rect().tl);
rr.translate(canvas.pan_offset());
obj_list.push(ClipObjectType::RoundRect(rr), canvas.clip_rect()).unwrap();
}
}
} else {
break;
}
}
gfx.draw_object_list_clipped(obj_list).expect("couldn't draw object list");
canvas.do_drawn().expect("couldn't set canvas to drawn");
} else {
log::debug!("attempt to draw Object on non-drawable canvas. Not fatal, but request ignored: {:?}", obj_ipc);
}
} else {
info!("bogus GID in Object, not doing anything in response to draw request.");
}
}
Some(Opcode::ClaimToken) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut tokenclaim = buffer.to_original::<TokenClaim, _>().unwrap();
tokenclaim.token = context_mgr.claim_token(tokenclaim.name.as_str().unwrap());
buffer.replace(tokenclaim).unwrap();
},
Some(Opcode::TrustedInitDone) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
if context_mgr.allow_untrusted_code() {
xous::return_scalar(msg.sender, 1).unwrap();
} else {
xous::return_scalar(msg.sender, 0).unwrap();
}
}),
Some(Opcode::RegisterUx) => {
let mut buffer = unsafe{ Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let registration = buffer.to_original::<UxRegistration, _>().unwrap();
let init_focus_found = if registration.app_name.as_str().unwrap_or("UTF-8 error") == INITIAL_APP_FOCUS {
true
} else {
false
};
// 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,
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));
buffer.replace(Return::UxToken(token)).unwrap();
// fire off a thread that deals with activating the initial boot context. You need this because this call has to complete before the context can respond to activation events.
if token.is_some() & init_focus_found {
std::thread::spawn({
let gam_token = gam_token.clone();
let conn = CB_TO_MAIN_CONN.load(Ordering::SeqCst);
move || {
let switchapp = SwitchToApp {
token: gam_token,
app_name: String::<128>::from_str(INITIAL_APP_FOCUS),
};
let buf = Buffer::into_buf(switchapp).or(Err(xous::Error::InternalError))?;
buf.send(conn, Opcode::SwitchToApp.to_u32().unwrap()).or(Err(xous::Error::InternalError)).map(|_|())
}
});
}
},
Some(Opcode::SetAudioOpcode) => {
let buffer = unsafe{ Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let audio_op = buffer.to_original::<SetAudioOpcode, _>().unwrap();
context_mgr.set_audio_op(audio_op);
},
Some(Opcode::InputLine) => {
// receive the keyboard input and pass it on to the context with focus
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let inputline = buffer.to_original::<String::<4000>, _>().unwrap();
log::debug!("received input line, forwarding on... {}", inputline);
context_mgr.forward_input(inputline).expect("couldn't forward input line to focused app");
log::debug!("returned from forward_input");
},
Some(Opcode::KeyboardEvent) => msg_scalar_unpack!(msg, k1, k2, k3, k4, {
let keys = [
core::char::from_u32(k1 as u32).unwrap_or('\u{0000}'),
core::char::from_u32(k2 as u32).unwrap_or('\u{0000}'),
core::char::from_u32(k3 as u32).unwrap_or('\u{0000}'),
core::char::from_u32(k4 as u32).unwrap_or('\u{0000}'),
];
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);
xous::return_scalar(msg.sender, 0).expect("couldn't unblock caller");
},
Some(Opcode::RevertFocusNb) => {
context_mgr.revert_focus(&gfx, &mut canvases);
},
Some(Opcode::QueryGlyphProps) => msg_blocking_scalar_unpack!(msg, style, _, _, _, {
let height = gfx.glyph_height_hint(GlyphStyle::from(style)).expect("couldn't query glyph height from gfx");
xous::return_scalar(msg.sender, height).expect("could not return QueryGlyphProps request");
}),
2021-06-18 16:25:18 +00:00
Some(Opcode::RedrawIme) => {
context_mgr.redraw_imef().expect("couldn't redraw the IMEF");
},
Some(Opcode::SwitchToApp) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let switchapp = buffer.to_original::<SwitchToApp, _>().unwrap();
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 {
context_mgr.activate(&gfx, &mut canvases, new_app_token, false);
continue;
}
}
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;
}
}
if let Some(token) = context_mgr.find_app_token_by_name(gam::STATUS_BAR_NAME) {
if token == switchapp.token {
context_mgr.activate(&gfx, &mut canvases, new_app_token, true);
continue;
}
}
// this message came from ourselves
if gam_token == switchapp.token {
context_mgr.activate(&gfx, &mut canvases, new_app_token, true);
}
}
},
Some(Opcode::RaiseMenu) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut activation = buffer.to_original::<GamActivation, _>().unwrap();
log::debug!("got request to raise context {}", activation.name);
let result = context_mgr.raise_menu(activation.name.as_str().unwrap(), &gfx, &mut canvases);
activation.result = Some(result);
buffer.replace(activation).unwrap();
2021-07-03 13:08:39 +00:00
},
Some(Opcode::Devboot) => msg_scalar_unpack!(msg, ena, _, _, _, {
if ena != 0 { gfx.set_devboot(true).expect("couldn't send devboot message"); }
else { gfx.set_devboot(false).expect("couldn't send devboot message"); }
}),
Some(Opcode::TestPattern) => msg_blocking_scalar_unpack!(msg, duration_ms, _, _, _, {
if !did_test {
did_test = true;
let checked_duration = if duration_ms > 60_000 {
60_000
} else {
duration_ms
};
gfx.selftest(checked_duration);
}
xous::return_scalar(msg.sender, 1).expect("couldn't ack self test");
}),
Some(Opcode::Quit) => break,
None => {log::error!("unhandled message {:?}", msg);}
}
// we don't currently have a mechanism to guarantee that the default app has focus
// right now, it just depends upon it requesting focus, and none others taking it.
// probably something should be inserted around here to take care of that?
}
// clean up our program
log::trace!("main loop exit, destroying servers");
xns.unregister_server(gam_sid).unwrap();
xous::destroy_server(gam_sid).unwrap();
log::trace!("quitting");
xous::terminate_process(0)
}