diff --git a/Cargo.lock b/Cargo.lock index 47bb19765..8632b42e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -259,6 +259,8 @@ dependencies = [ "blitstr", "com", "graphics-server", + "hash32", + "hash32-derive", "heapless", "keyboard", "log", diff --git a/services/gam/Cargo.toml b/services/gam/Cargo.toml index f393437c9..00601950f 100644 --- a/services/gam/Cargo.toml +++ b/services/gam/Cargo.toml @@ -16,6 +16,8 @@ keyboard = { path = "../keyboard" } trng = { path = "../trng" } heapless = "0.5" log = "0.4" +hash32 = "0.1.0" +hash32-derive = "0.1.0" com = { path = "../com"} diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index 82a6b703a..c79afe2c9 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -1,6 +1,23 @@ use xous::{Message, ScalarMessage}; use graphics_server::{Point, Rectangle}; use blitstr::{GlyphStyle, Cursor}; +use hash32::{Hash, Hasher}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub struct Gid { + gid: [u32; 4], +} +impl Gid { + pub fn new(id: [u32; 4]) -> Self { Gid{gid: id} } +} +impl hash32::Hash for Gid { + fn hash(&self, state: &mut H) + where + H: Hasher, + { + Hash::hash(&self.gid[..], state); + } +} #[derive(Debug, Copy, Clone)] pub enum TextAlignment { @@ -21,8 +38,36 @@ pub enum TextBounds { GrowableFromBl(Point, u16), } +#[derive(Debug, Copy, Clone)] +// operations that may be requested of a TextView when sent to GAM +pub enum TextOp { + Nop, + Render, + ComputeBounds, +} +impl Into for TextOp { + fn into(self) -> usize { + match self { + TextOp::Nop => 0, + TextOp::Render => 1, + TextOp::ComputeBounds => 2, + } + } +} +impl From for TextOp { + fn from(code: usize) -> Self { + match code { + 1 => TextOp::Render, + 2 => TextOp::ComputeBounds, + _ => TextOp::Nop, + } + } +} + #[repr(C)] pub struct TextView<'a> { + operation: TextOp, + untrusted: bool, // render content with random stipples to indicate the strings within are untrusted token: Option<[u32; 4]>, // optional 128-bit token which is presented to prove a field's trustability invert: bool, // only trusted, token-validated TextViews will have the invert bit respected @@ -31,8 +76,8 @@ pub struct TextView<'a> { draw_order: usize, // offsets for text drawing -- exactly one of the following options should be specified - bounds: TextBounds, - computed_bounds: Option, // is Some(Rectangle) if bounds have been computed and text has not been modified + bounds_hint: TextBounds, + bounds_computed: Option, // is Some(Rectangle) if bounds have been computed and text has not been modified style: GlyphStyle, text: xous::String<'a>, @@ -47,7 +92,32 @@ pub struct TextView<'a> { // this field specifies the beginning and end of a "selected" region of text selected: Option<[usize; 2]>, - canvas: [u32; 4], // GID of the canvas to draw on + canvas: Gid, // GID of the canvas to draw on +} +impl<'a> TextView<'a> { + pub fn new(maxlen: usize, draw_order: usize, bounds_hint: TextBounds, canvas: Gid) -> Self { + TextView { + operation: TextOp::Nop, + untrusted: true, + token: None, + invert: false, + draw_order, + bounds_hint, + bounds_computed: None, + style: GlyphStyle::Regular, + text: xous::String::new(maxlen), + alignment: TextAlignment::Left, + draw_border: true, + border_width: 1, + rounded_border: false, + x_margin: 4, + y_margin: 4, + selected: None, + canvas, + } + } + pub fn set_op(&mut self, op: TextOp) { self.operation = op; } + pub fn get_op(&self) -> TextOp { self.operation } } impl<'a> core::fmt::Debug for TextView<'a> { @@ -61,16 +131,16 @@ impl<'a> core::fmt::Debug for TextView<'a> { #[derive(Debug)] pub enum Opcode<'a> { // clears a canvas with a given GID - ClearCanvas([u32; 4]), + ClearCanvas(Gid), // renders a TextView RenderTextView(TextView<'a>), // returns a GID to the "content" Canvas; requires an authentication token - RequestContentCanvas([u32; 4]), + RequestContentCanvas(Gid), // hides a canvas with a given GID - HideCanvas([u32; 4]), + HideCanvas(Gid), // requests the GID to the "input" Canvas; call only works once (for the IME server), then maps out RequestInputCanvas(), @@ -84,7 +154,7 @@ impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> { fn try_from(message: &'a Message) -> Result { match message { Message::Scalar(m) => match m.id { - 0 => Ok(Opcode::ClearCanvas([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _])), + 0 => Ok(Opcode::ClearCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))), _ => Err("GAM api: unknown Scalar ID"), }, _ => Err("GAM api: unhandled message type"), @@ -96,7 +166,7 @@ impl<'a> Into for Opcode<'a> { fn into(self) -> Message { match self { Opcode::ClearCanvas(gid) => Message::Scalar(ScalarMessage { - id: 0, arg1: gid[0] as _, arg2: gid[1] as _, arg3: gid[2] as _, arg4: gid[3] as _ + id: 0, arg1: gid.gid[0] as _, arg2: gid.gid[1] as _, arg3: gid.gid[2] as _, arg4: gid.gid[3] as _ }), _ => panic!("GAM api: Opcode type not handled by Into(), maybe you meant to use a helper method?"), } diff --git a/services/gam/src/canvas.rs b/services/gam/src/canvas.rs index e3cb4e77a..ad7d78330 100644 --- a/services/gam/src/canvas.rs +++ b/services/gam/src/canvas.rs @@ -5,6 +5,8 @@ use heapless::Vec; use heapless::consts::*; use graphics_server::{Rectangle, Point}; +use crate::api::*; + #[derive(Debug, Copy, Clone)] pub enum CanvasState { // the initial state of every Canvas. Not drawable. @@ -33,14 +35,14 @@ pub struct Canvas { state: CanvasState, // unique, random identifier for the Canvas - gid: [u32; 4], + gid: Gid, // enables scroll/pan of objects within a region pan_offset: Point, } // we need the "screen" parameter so we can turn off drawing to canvases that are off-screen -pub fn recompute_canvases(mut canvases: BinaryHeap, screen: Rectangle) -> (BinaryHeap, FnvIndexMap<[u32; 4], Canvas, U32>) { +pub fn recompute_canvases(mut canvases: BinaryHeap, screen: Rectangle) -> (BinaryHeap, FnvIndexMap) { let mut higher_clipregions: BinaryHeap = BinaryHeap::new(); let mut trust_level: u8 = 255; @@ -73,7 +75,7 @@ pub fn recompute_canvases(mut canvases: BinaryHeap, screen: Re higher_clipregions.push(canvas).unwrap(); } - let mut map: FnvIndexMap<[u32; 4], Canvas, U32> = FnvIndexMap::new(); + let mut map: FnvIndexMap = FnvIndexMap::new(); for &c in higher_clipregions.iter() { map.insert(c.gid(), c).unwrap(); } @@ -95,16 +97,16 @@ impl Canvas { Ok(if pan_offset.is_some() { Canvas { - clip_rect, trust_level, state: CanvasState::Created, gid, pan_offset: pan_offset.unwrap() + clip_rect, trust_level, state: CanvasState::Created, gid: Gid::new(gid), pan_offset: pan_offset.unwrap() } } else { Canvas { - clip_rect, trust_level, state: CanvasState::Created, gid, pan_offset: Point::new(0, 0) + clip_rect, trust_level, state: CanvasState::Created, gid: Gid::new(gid), pan_offset: Point::new(0, 0) } }) } pub fn clip_rect(&self) -> Rectangle { self.clip_rect } - pub fn gid(&self) -> [u32; 4] { self.gid } + pub fn gid(&self) -> Gid { self.gid } pub fn trust_level(&self) -> u8 { self.trust_level } pub fn state(&self) -> CanvasState { self.state } pub fn set_state(&mut self, state: CanvasState) { self.state = state; } diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs new file mode 100644 index 000000000..0653c1f2f --- /dev/null +++ b/services/gam/src/lib.rs @@ -0,0 +1,16 @@ +#![cfg_attr(target_os = "none", no_std)] + +pub mod api; +use api::*; + +use xous::ipc::*; +use core::fmt::Write; + +pub fn draw_textview(gam_cid: xous::CID, tv: &mut TextView) -> Result<(), xous::Error> { + let mut sendable_tv = Sendable::new(tv).expect("can't create sendable textview"); + sendable_tv.set_op(TextOp::Render); + sendable_tv.lend_mut(gam_cid, sendable_tv.get_op().into()).expect("draw_textview operation failure"); + + sendable_tv.set_op(TextOp::Nop); + Ok(()) +} \ No newline at end of file diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index b8f844226..03fc5951e 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -22,6 +22,92 @@ use heapless::binary_heap::{BinaryHeap, Max}; use heapless::FnvIndexMap; use heapless::consts::*; +// 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, +} + +// remember GIDs of the canvases for modal pop-up boxes +struct ModalCanvases { + pub password: Gid, + pub menu: Gid, + pub alert: Gid, +} + +fn add_modal_layout(trng_conn: xous::CID, canvases: &mut BinaryHeap) -> Result { + let password_canvas = Canvas::new( + Rectangle::new_coords(-1, -1, -1, -1), + 0, trng_conn, None + ).expect("GAM: couldn't create password canvas"); + canvases.push(password_canvas).expect("GAM: can't store password canvas"); + + let menu_canvas = Canvas::new( + Rectangle::new_coords(-1, -1, -1, -1), + 0, trng_conn, None + ).expect("GAM: couldn't create menu canvas"); + canvases.push(menu_canvas).expect("GAM: can't store menu canvas"); + + let alert_canvas = Canvas::new( + Rectangle::new_coords(-1, -1, -1, -1), + 0, trng_conn, None + ).expect("GAM: couldn't create alert canvas"); + canvases.push(alert_canvas).expect("GAM: can't store alert canvas"); + + Ok(ModalCanvases { + password: password_canvas.gid(), + menu: menu_canvas.gid(), + alert: alert_canvas.gid(), + }) +} + +fn add_chat_layout(gfx_conn: xous::CID, trng_conn: xous::CID, canvases: &mut BinaryHeap) -> Result { + let screensize = graphics_server::screen_size(gfx_conn).expect("GAM: Couldn't get screen size"); + // get the height of various text regions to compute the layout + graphics_server::set_glyph_style(gfx_conn, GlyphStyle::Small).expect("GAM: couldn't set glyph style"); + let (_, small_height_tuple) = graphics_server::query_glyph(gfx_conn).expect("GAM: couldn't get glyph height"); + let small_height: i16 = small_height_tuple as i16; + graphics_server::set_glyph_style(gfx_conn, GlyphStyle::Regular).expect("GAM: couldn't set glyph style"); + let (_, regular_height_tuple) = graphics_server::query_glyph(gfx_conn).expect("GAM: couldn't get glyph height"); + let regular_height: i16 = regular_height_tuple as i16; + + // 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), + 0, trng_conn, None + ).expect("GAM: couldn't create status canvas"); + canvases.push(status_canvas).expect("GAM: can't store status canvus"); + + let predictive_canvas = Canvas::new( + Rectangle::new_coords(0, screensize.y - regular_height, screensize.x, screensize.y), + 1, + trng_conn, None + ).expect("GAM: couldn't create predictive text canvas"); + canvases.push(predictive_canvas).expect("GAM: couldn't store predictive canvas"); + + let input_canvas = Canvas::new( + Rectangle::new_v_stack(predictive_canvas.clip_rect(), -regular_height), + 1, trng_conn, None + ).expect("GAM: couldn't create input text canvas"); + canvases.push(input_canvas).expect("GAM: couldn't store input canvas"); + + let content_canvas = Canvas::new( + Rectangle::new_v_span(status_canvas.clip_rect(), input_canvas.clip_rect()), + 2, trng_conn, None + ).expect("GAM: couldn't create content canvas"); + canvases.push(content_canvas).expect("GAM: can't store content canvas"); + + Ok(ChatLayout { + status: status_canvas.gid(), + content: content_canvas.gid(), + predictive: predictive_canvas.gid(), + input: input_canvas.gid(), + }) +} + #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); @@ -35,52 +121,21 @@ fn xmain() -> ! { let screensize = graphics_server::screen_size(gfx_conn).expect("GAM: Couldn't get screen size"); - graphics_server::set_glyph_style(gfx_conn, GlyphStyle::Small).expect("GAM: couldn't set glyph style"); - let (_, small_height_tuple) = graphics_server::query_glyph(gfx_conn).expect("GAM: couldn't get glyph height"); - let small_height: i16 = small_height_tuple as i16; - graphics_server::set_glyph_style(gfx_conn, GlyphStyle::Regular).expect("GAM: couldn't set glyph style"); - let (_, regular_height_tuple) = graphics_server::query_glyph(gfx_conn).expect("GAM: couldn't get glyph height"); - let regular_height: i16 = regular_height_tuple as i16; - let mut canvases: BinaryHeap = BinaryHeap::new(); - let mut status_canvas = Canvas::new( - Rectangle::new_coords(0, 0, screensize.x, small_height), - 0, trng_conn, None - ).expect("GAM: couldn't create status canvas"); - canvases.push(status_canvas).expect("GAM: can't store status canvus"); + let modallayout = add_modal_layout(trng_conn, &mut canvases).expect("GAM: can't add modal layouts"); + let chatlayout = add_chat_layout(gfx_conn, trng_conn, &mut canvases).expect("GAM: couldn't create chat layout"); - let mut password_canvas = Canvas::new( - Rectangle::new_coords(-1, -1, -1, -1), - 0, trng_conn, None - ).expect("GAM: couldn't create password canvas"); - canvases.push(password_canvas).expect("GAM: can't store password canvas"); + // canvas_map is a copy of the canvases, but searchable by GID + let canvas_map: FnvIndexMap; - let mut predictive_canvas = Canvas::new( - Rectangle::new_coords(0, screensize.y - regular_height, screensize.x, screensize.y), - 1, - trng_conn, None - ).expect("GAM: couldn't create predictive text canvas"); - canvases.push(predictive_canvas).expect("GAM: couldn't store predictive canvas"); - - let mut input_canvas = Canvas::new( - Rectangle::new_v_stack(predictive_canvas.clip_rect(), -regular_height), - 1, trng_conn, None - ).expect("GAM: couldn't create input text canvas"); - canvases.push(input_canvas).expect("GAM: couldn't store input canvas"); - - let mut content_canvas = Canvas::new( - Rectangle::new_v_span(status_canvas.clip_rect(), input_canvas.clip_rect()), - 2, trng_conn, None - ).expect("GAM: couldn't create content canvas"); - canvases.push(content_canvas).expect("GAM: can't store content canvas"); - - let mut canvas_map: FnvIndexMap<[u32;4], Canvas, U32>; + // 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 // this is broken into two steps because of https://github.com/rust-lang/rust/issues/71126 let (c_tuple, m_tuple) = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize)); canvases = c_tuple; canvas_map = m_tuple; // make a thread to manage the status bar - xous::create_thread_simple(status_thread, status_canvas.gid()).expect("GAM: couldn't create status thread"); + xous::create_thread_simple(status_thread, chatlayout.status).expect("GAM: couldn't create status thread"); let gam_sid = xous_names::register_name(xous::names::SERVER_NAME_GAM).expect("GAM: can't register server"); @@ -108,6 +163,16 @@ fn xmain() -> ! { }, _ => todo!("GAM: opcode not yet implemented"), } + } else if let xous::Message::MutableBorrow(m) = &envelope.body { + match TextOp::from(m.id) { + TextOp::Nop => (), + TextOp::Render => { + + }, + TextOp::ComputeBounds => { + + }, + } } } _ => xous::yield_slice(), diff --git a/services/gam/src/status.rs b/services/gam/src/status.rs index 8dd5d50e7..6271272ce 100644 --- a/services/gam/src/status.rs +++ b/services/gam/src/status.rs @@ -1,4 +1,5 @@ +use crate::api::*; -pub fn status_thread(status_gid: [u32; 4]) { +pub fn status_thread(status_gid: Gid) { } \ No newline at end of file