it helps to save the file before committing it

This commit is contained in:
bunnie 2021-06-15 17:46:10 +08:00
parent 8cd84d3be0
commit 13a8cf7417

View file

@ -27,7 +27,6 @@ use xous_ipc::{Buffer, String};
use api::Opcode;
use xous::{msg_scalar_unpack, msg_blocking_scalar_unpack};
<<<<<<< HEAD
//// todo:
// - fork current state out to a branch, so we can add auths to xous-names
// - change xous-names to count registrations and limit connections; provision for future authorization extension
@ -71,276 +70,6 @@ pub(crate) struct UxContext {
pub gotinput_id: Option<usize>,
/// opcode ID for AudioFrame
pub audioframe_id: Option<usize>,
=======
pub trait LayoutApi {
type Layout;
fn init(xns: &xous_names::XousNames, trng: &trng::Trng, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<Self::Layout, xous::Error>;
fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<(), xous::Error>;
fn resize(&mut self, gfx: &graphics_server::Gfx, new_height: i16, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<Point, xous::Error>;
}
#[derive(Debug)]
// GIDs of canvases that are used the "Chat" layout.
struct ChatLayout {
// a set of GIDs to track the elements of the chat layout
pub status: Gid,
pub content: Gid,
pub predictive: Gid,
pub input: Gid,
// my internal bookkeeping records. Allow input area to grow into content area
min_content_height: i16,
min_input_height: i16,
}
impl LayoutApi for ChatLayout {
type Layout = ChatLayout;
fn init(xns: &xous_names::XousNames, trng: &trng::Trng, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<ChatLayout, xous::Error> {
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 regular_height: i16 = gfx.glyph_height_hint(GlyphStyle::Regular).expect("couldn't get glyph height") as i16;
let margin = 4;
// allocate canvases in structures, and record their GID for future reference
let status_canvas = Canvas::new(
Rectangle::new_coords(0, 0, screensize.x, small_height * 2),
255, &trng, None
).expect("couldn't create status canvas");
canvases.insert(status_canvas.gid(), status_canvas).expect("can't store status canvus");
let predictive_canvas = Canvas::new(
Rectangle::new_coords(0, screensize.y - regular_height - margin*2, screensize.x, screensize.y),
254,
&trng, None
).expect("couldn't create predictive text canvas");
canvases.insert(predictive_canvas.gid(), predictive_canvas).expect("couldn't store predictive canvas");
let min_input_height = regular_height + margin*2;
let input_canvas = Canvas::new(
Rectangle::new_v_stack(predictive_canvas.clip_rect(), -min_input_height),
254, &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()),
128, &trng, None
).expect("couldn't create content canvas");
canvases.insert(content_canvas.gid(), content_canvas).expect("can't store content canvas");
Ok(ChatLayout {
status: status_canvas.gid(),
content: content_canvas.gid(),
predictive: predictive_canvas.gid(),
input: input_canvas.gid(),
min_content_height: 64,
min_input_height,
})
}
fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> 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");
let predictive_canvas = canvases.get(&self.predictive).expect("couldn't find predictive canvas");
let status_canvas = canvases.get(&self.status).expect("couldn't find status canvas");
let mut rect = status_canvas.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");
let mut rect = content_canvas.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");
let mut rect = predictive_canvas.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");
let mut rect = input_canvas.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");
Ok(())
}
fn resize(&mut self, gfx: &graphics_server::Gfx, new_height: i16, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<Point, xous::Error> {
let input_canvas = canvases.get(&self.input).expect("couldn't find input canvas");
let predictive_canvas = canvases.get(&self.predictive).expect("couldn't find predictive canvas");
let status_canvas = canvases.get(&self.status).expect("couldn't find status canvas");
let height: i16 = if new_height < self.min_input_height {
self.min_input_height
} else {
new_height
};
let mut new_input_rect = Rectangle::new_v_stack(predictive_canvas.clip_rect(), -height);
let mut new_content_rect = Rectangle::new_v_span(status_canvas.clip_rect(), new_input_rect);
if (new_content_rect.br.y - new_content_rect.tl.y) > self.min_content_height {
{
let input_canvas_mut = canvases.get_mut(&self.input).expect("couldn't find input canvas");
input_canvas_mut.set_clip(new_input_rect);
new_input_rect.style = DrawStyle {fill_color: Some(PixelColor::Light), stroke_color: None, stroke_width: 0,};
gfx.draw_rectangle(new_input_rect).expect("can't clear canvas");
}
{
let content_canvas_mut = canvases.get_mut(&self.content).expect("couldn't find content canvas");
content_canvas_mut.set_clip(new_content_rect);
new_content_rect.style = DrawStyle {fill_color: Some(PixelColor::Light), stroke_color: None, stroke_width: 0,};
gfx.draw_rectangle(new_content_rect).expect("can't clear canvas");
}
// we resized to this new height
Ok(new_content_rect.br)
} else {
// we didn't resize anything, height unchanged
Ok(input_canvas.clip_rect().br)
}
}
}
// remember GIDs of the canvases for menus
struct MenuLayout {
pub menu: Gid,
menu_y_pad: i16,
menu_x_pad: i16,
menu_min_height: i16,
}
impl LayoutApi for MenuLayout {
type Layout = MenuLayout;
fn init(xns: &xous_names::XousNames, trng: &trng::Trng, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<MenuLayout, xous::Error> {
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;
const MENU_Y_PAD: i16 = 100;
const MENU_X_PAD: i16 = 35;
// build for an initial size of 1 entry
let menu_canvas = Canvas::new(
Rectangle::new_coords(MENU_X_PAD, MENU_Y_PAD, screensize.x - MENU_X_PAD, MENU_Y_PAD + small_height),
255, &trng, None
).expect("couldn't create menu canvas");
canvases.insert(menu_canvas.gid(), menu_canvas).expect("can't store menu canvas");
Ok(MenuLayout {
menu: menu_canvas.gid(),
menu_y_pad: MENU_Y_PAD,
menu_x_pad: MENU_X_PAD,
menu_min_height: small_height,
})
}
fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<(), xous::Error> {
let menu_canvas = canvases.get(&self.menu).expect("couldn't find menu canvas");
let mut rect = menu_canvas.clip_rect();
rect.style = DrawStyle {fill_color: Some(PixelColor::Dark), stroke_color: None, stroke_width: 0,};
gfx.draw_rectangle(rect)
}
fn resize(&mut self, gfx: &graphics_server::Gfx, new_height: i16, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<Point, xous::Error> {
let mut menu_canvas = canvases.get_mut(&self.menu).expect("couldn't find menu canvas");
let screensize = gfx.screen_size().expect("Couldn't get screen size");
let mut height: i16 = if new_height < self.menu_min_height {
self.menu_min_height
} else {
new_height
};
if new_height > screensize.y - self.menu_y_pad {
height = screensize.y - self.menu_y_pad;
}
let mut menu_clip_rect = Rectangle::new_coords(self.menu_x_pad, self.menu_y_pad, screensize.x - self.menu_x_pad, height);
menu_clip_rect.style = DrawStyle {fill_color: Some(PixelColor::Dark), stroke_color: None, stroke_width: 0,};
menu_canvas.set_clip(menu_clip_rect);
gfx.draw_rectangle(menu_clip_rect).expect("can't clear menu");
Ok(menu_clip_rect.br)
}
}
/*
Authentication tokens to the GAM are created on a first-come, first-serve basis,
under the following assumptions:
- the boot set is fully trusted (signature checked, just-my-own-code running)
- the boot set will grab all the token slots availble before allowing any less-trusted code to run
This scheme thus effectively locks out less-trusted code, while simplifying the
registration of interprocess comms between trusted elements, only relying on ephemeral,
dynamically generated 128-bit tokens.
*/
const TOKEN_SLOTS: usize = 3;
#[derive(Copy, Clone, Debug)]
pub struct NamedToken {
token: [u32; 4],
name: String::<128>,
}
pub struct TokenManager {
tokens: [Option<NamedToken>; TOKEN_SLOTS],
slot_names: [&'static str; TOKEN_SLOTS],
trng: trng::Trng,
}
impl<'a> TokenManager {
pub fn new(xns: &xous_names::XousNames) -> TokenManager {
TokenManager {
tokens: [None; TOKEN_SLOTS],
slot_names: ["status", "menu", "passwords"],
trng: trng::Trng::new(&xns).unwrap(),
}
}
/// checks to see if all the slots have been occupied. We can't allow untrusted code to run until all slots have checked in
pub fn allow_untrusted_code(&self) -> bool {
let mut allow = true;
for t in self.tokens.iter() {
if t.is_none() {
allow = false
}
}
allow
}
pub fn claim_token(&mut self, name: &str) -> Option<[u32; 4]> {
// first check if the name is valid
let mut valid = false;
for &valid_name in self.slot_names.iter() {
if name.eq(valid_name) {
valid = true;
}
}
if !valid { return None }
// now check if it hasn't already been registered
let mut registered = false;
for maybe_token in self.tokens.iter() {
match maybe_token {
Some(token) => {
if name.eq(token.name.as_str().unwrap()) {
registered = true;
}
}
_ => ()
}
}
if registered { return None }
// now do the registration
let token = [self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap(),];
for maybe_token in self.tokens.iter_mut() {
if maybe_token.is_none() {
*maybe_token = Some(NamedToken {
token,
name: String::<128>::from_str(name),
});
}
return Some(token)
}
// somehow, we didn't have space -- but with all the previous checks, we really should have
None
}
pub fn is_token_valid(&self, token: [u32; 4]) -> bool {
for maybe_token in self.tokens.iter() {
match maybe_token {
Some(found_token) => {
if found_token.token == token {
return true
}
}
_ => ()
}
}
false
}
>>>>>>> bunnie-dev
}
const MAX_UX_CONTEXTS: usize = 4;
const DEFAULT_APP_TOKEN: &'static str = "shellchat";
@ -375,15 +104,9 @@ fn xmain() -> ! {
// a map of canvases accessable by Gid
let mut canvases: FnvIndexMap<Gid, Canvas, 32> = FnvIndexMap::new();
<<<<<<< HEAD
//let mut menulayout = MenuLayout::init(&xns, &trng, &mut canvases).expect("couldn't create menu layout");
//let mut chatlayout = ChatLayout::init(&xns, &trng, &status_canvas, &mut canvases).expect("couldn't create chat layout");
//chatlayout.clear(&mut canvases).expect("couldn't clear initial chatlayout");
=======
let mut menulayout = MenuLayout::init(&xns, &trng, &gfx, &mut canvases).expect("couldn't create menu layout");
let mut chatlayout = ChatLayout::init(&xns, &trng, &gfx, &mut canvases).expect("couldn't create chat layout");
chatlayout.clear(&gfx, &mut canvases).expect("couldn't clear initial chatlayout");
>>>>>>> bunnie-dev
// now that all the initial canvases have been allocated, compute what canvases are drawable
// this _replaces_ the original canvas structure, to avoid complications of tracking mutable references through compound data structures