major refactor of the Ux -- can create, redirect I/O contexts

getting ready for Precursor to do more than just one thing.
This commit is contained in:
bunnie 2021-06-16 04:50:26 +08:00
parent 13a8cf7417
commit 5301f4af20
15 changed files with 546 additions and 340 deletions

2
Cargo.lock generated
View file

@ -610,6 +610,7 @@ dependencies = [
"graphics-server",
"heapless 0.7.0",
"ime-plugin-api",
"ime-plugin-shell",
"keyboard",
"llio",
"log",
@ -1527,6 +1528,7 @@ dependencies = [
"graphics-server",
"heapless 0.7.0",
"ime-plugin-api",
"ime-plugin-shell",
"keyboard",
"llio",
"log",

View file

@ -13,6 +13,7 @@ content-plugin-api = {path = "../content-plugin-api"}
graphics-server = {path = "../graphics-server"}
heapless = "0.7"
ime-plugin-api = {path = "../ime-plugin-api"}
ime-plugin-shell = {path = "../ime-plugin-shell"}
keyboard = {path = "../keyboard"}
log = "0.4"
log-server = {path = "../log-server"}

View file

@ -19,21 +19,44 @@ pub struct GamObject {
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct SetCanvasBoundsRequest {
pub canvas: Gid,
pub token: [u32; 4],
pub requested: Point,
pub granted: Option<Point>,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct ContentCanvasRequest {
// return value of the canvas Gid
pub canvas: Gid,
// name of the server requesting the content canvas
pub servername: String<256>,
// redraw message scalar ID - to be sent back to the requestor in case a redraw is required
pub redraw_scalar_id: usize,
pub struct SetAudioOpcode {
pub token: [u32; 4],
pub opcode: u32,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub enum UxType {
Chat,
Menu,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct UxRegistration {
// request specification
pub app_name: String::<128>, // the putative name of our application - GAM may modify this if a spoof attempt is detected
pub ux_type: UxType,
pub predictor: Option<String::<64>>, // optional specification for an IME prediction engine to use. This can be updated later on, or None and a default engine will be provided.
// Callbacks:
/// SID ofserver for callbacks from the GAM. Note this is a disclosure of the SID, which is normally a secret in the kernel services.
/// however, for apps, we allow disclosure of this to the kernel services, because we trust them.
pub listener: [u32; 4],
/// opcode ID for redraw messages. This is mandatory.
pub redraw_id: u32,
/// optional opcode ID for inputs. If presented, input Strings are sent to this Ux
pub gotinput_id: Option<u32>,
/// optional opcode ID for audio frames. If presented, audio callbacks requests for more play/rec data will be sent directly to this opcode
pub audioframe_id: Option<u32>,
/// optional opcode ID for raw keystrokes. They are passed on to the caller in real-time.
pub rawkeys_id: Option<u32>,
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub(crate) enum Opcode {
// clears a canvas with a given GID
@ -55,33 +78,34 @@ pub(crate) enum Opcode {
// forces a redraw (which also does defacement, etc.)
Redraw,
// returns a GID to the "content" Canvas; currently, anyone can request it and draw to it, but maybe that policy should be stricter.
// the Gid argument is the rkyv return value.
//RequestContentCanvas, //(ContentCanvasRequest),
// registers the requester as an input focus object. keyboard input is passed to the graphical element with the current focus.
//RegisterInputFocus,
// returns a GID to the "content" Canvas of the token holder
RequestContentCanvas,
// registers a Ux of a requested type
// takes in the LayoutType, default PredictorType, a SID for UxEvents, a human-readable identifier; returns a content canvas GID
// takes in the LayoutType, default PredictorType, a SID for UxEvents, a human-readable identifier token; returns a content canvas GID
// also takes a bunch of optional ID codes for the various callbacks
// internally assigns a trust level, based on a first-come first-serve basis for known services, and then a much lower trust for rando ones
RegisterUx,
// updates the audio connection ID post-registration
SetAudioOpcode,
// Requests setting the UI to the power down screen
PowerDownRequest,
// Request blank screen for ship mode
ShipModeBlankRequest,
/// claims an authentication token out of a fixed name space
/// this is first-come, first-serve basis. Once the system is initialized, no more tokens can be handed out.
// used to claim a GAM registration token (should be used only by status.rs)
ClaimToken,
/// system-level API that can be called by the Xous process launcher to check if we're at a state where less trusted code could be run
/// it basically checks that all tokens have been claimed by trusted OS procesess, thus blocking any further token creation
TrustedInitDone,
/// this is used internally to route input lines from the IMEF
InputLine,
/////// planned
// hides a canvas with a given GID
@ -95,8 +119,9 @@ pub(crate) enum Opcode {
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) enum Return {
UxToken(Option<[u32; 4]>),
RenderReturn(TextView),
SetCanvasBoundsReturn(SetCanvasBoundsRequest),
ContentCanvasReturn(ContentCanvasRequest),
ContentCanvasReturn(Option<Gid>),
Failure,
}

View file

@ -8,12 +8,18 @@ use graphics_server::*;
pub(crate) trait LayoutApi {
type Layout;
fn init(xns: &xous_names::XousNames, trng: &trng::Trng, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<Self::Layout, xous::Error>;
fn clear(&self, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<(), xous::Error>;
fn resize(&mut self, new_height: i16, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<Point, xous::Error>;
fn init(gfx: &graphics_server::Gfx, trng: &trng::Trng,
base_trust: u8, status_canvas: &Canvas,
canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> Result<Self::Layout, xous::Error>;
fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> Result<(), xous::Error>;
// for Chats, this resizes the height of the input area; for menus, it resizes the overall height
fn resize_height(&mut self, gfx: &graphics_server::Gfx, new_height: i16, status_canvas: &Canvas, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> Result<Point, xous::Error>;
fn get_input_canvas(&self) -> Option<Gid> { None }
fn get_prediction_canvas(&self) -> Option<Gid> { None }
fn get_content_canvas(&self) -> Gid; // layouts always have a content canvas
}
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
// GIDs of canvases that are used the "Chat" layout.
pub(crate) struct ChatLayout {
// a set of GIDs to track the elements of the chat layout
@ -24,13 +30,15 @@ pub(crate) struct ChatLayout {
// my internal bookkeeping records. Allow input area to grow into content area
min_content_height: i16,
min_input_height: i16,
gfx: graphics_server::Gfx,
screensize: Point,
small_height: i16,
regular_height: i16,
}
impl LayoutApi for ChatLayout {
type Layout = ChatLayout;
// pass in the status canvas so we can size around it, but we can't draw on it
fn init(xns: &xous_names::XousNames, trng: &trng::Trng, status_canvas: &Canvas, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<ChatLayout, xous::Error> {
let gfx = graphics_server::Gfx::new(&xns).unwrap();
fn init(gfx: &graphics_server::Gfx, trng: &trng::Trng, base_trust: u8,
status_canvas: &Canvas, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> 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;
@ -40,7 +48,7 @@ impl LayoutApi for ChatLayout {
// allocate canvases in structures, and record their GID for future reference
let predictive_canvas = Canvas::new(
Rectangle::new_coords(0, screensize.y - regular_height - margin*2, screensize.x, screensize.y),
254,
base_trust,
&trng, None
).expect("couldn't create predictive text canvas");
canvases.insert(predictive_canvas.gid(), predictive_canvas).expect("couldn't store predictive canvas");
@ -48,13 +56,13 @@ impl LayoutApi for 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),
254, &trng, None
base_trust, &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
base_trust / 2, &trng, None
).expect("couldn't create content canvas");
canvases.insert(content_canvas.gid(), content_canvas).expect("can't store content canvas");
@ -64,36 +72,32 @@ impl LayoutApi for ChatLayout {
input: input_canvas.gid(),
min_content_height: 64,
min_input_height,
gfx,
screensize,
small_height,
regular_height,
})
}
fn clear(&self, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<(), xous::Error> {
fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> 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,};
self.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,};
self.gfx.draw_rectangle(rect).expect("can't clear canvas");
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,};
self.gfx.draw_rectangle(rect).expect("can't clear canvas");
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,};
self.gfx.draw_rectangle(rect).expect("can't clear canvas");
gfx.draw_rectangle(rect).expect("can't clear canvas");
Ok(())
}
fn resize(&mut self, new_height: i16, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<Point, xous::Error> {
fn resize_height(&mut self, gfx: &graphics_server::Gfx, new_height: i16, status_canvas: &Canvas, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> 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
@ -107,13 +111,13 @@ impl LayoutApi for ChatLayout {
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,};
self.gfx.draw_rectangle(new_input_rect).expect("can't clear canvas");
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,};
self.gfx.draw_rectangle(new_content_rect).expect("can't clear canvas");
gfx.draw_rectangle(new_content_rect).expect("can't clear canvas");
}
// we resized to this new height
Ok(new_content_rect.br)
@ -122,20 +126,30 @@ impl LayoutApi for ChatLayout {
Ok(input_canvas.clip_rect().br)
}
}
fn get_input_canvas(&self) -> Option<Gid> {
Some(self.input)
}
fn get_prediction_canvas(&self) -> Option<Gid> {
Some(self.predictive)
}
fn get_content_canvas(&self) -> Gid {
self.content
}
}
// remember GIDs of the canvases for menus
#[derive(Debug, Copy, Clone)]
pub(crate) struct MenuLayout {
pub menu: Gid,
gfx: graphics_server::Gfx,
menu_y_pad: i16,
menu_x_pad: i16,
menu_min_height: i16,
screensize: Point,
small_height: i16,
}
impl LayoutApi for MenuLayout {
type Layout = MenuLayout;
fn init(xns: &xous_names::XousNames, trng: &trng::Trng, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<MenuLayout, xous::Error> {
let gfx = graphics_server::Gfx::new(&xns).unwrap();
fn init(gfx: &graphics_server::Gfx, trng: &trng::Trng, base_trust: u8, _status_canvas: &Canvas, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> 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;
@ -145,41 +159,44 @@ impl LayoutApi for MenuLayout {
// 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
base_trust, &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(),
gfx,
menu_y_pad: MENU_Y_PAD,
menu_x_pad: MENU_X_PAD,
menu_min_height: small_height,
screensize,
small_height,
})
}
fn clear(&self, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<(), xous::Error> {
fn clear(&self, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> 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,};
self.gfx.draw_rectangle(rect)
gfx.draw_rectangle(rect)
}
fn resize(&mut self, 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 = self.gfx.screen_size().expect("Couldn't get screen size");
fn resize_height(&mut self, gfx: &graphics_server::Gfx, new_height: i16, _status_canvas: &Canvas, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> Result<Point, xous::Error> {
let menu_canvas = canvases.get_mut(&self.menu).expect("couldn't find menu canvas");
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;
if new_height > self.screensize.y - self.menu_y_pad {
height = self.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);
let mut menu_clip_rect = Rectangle::new_coords(self.menu_x_pad, self.menu_y_pad, self.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);
self.gfx.draw_rectangle(menu_clip_rect).expect("can't clear menu");
gfx.draw_rectangle(menu_clip_rect).expect("can't clear menu");
Ok(menu_clip_rect.br)
}
fn get_content_canvas(&self) -> Gid {
self.menu
}
}

View file

@ -1,7 +1,7 @@
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use api::*;
pub use api::*;
use graphics_server::api::{TextOp, TextView};
@ -12,8 +12,7 @@ use xous::{send_message, CID, Message};
use xous_ipc::{String, Buffer};
use num_traits::ToPrimitive;
use ime_plugin_api::{ImefCallback, ImefOpcode};
static mut INPUT_CB: Option<fn(String::<4000>)> = None;
use ime_plugin_api::ImefCallback;
#[derive(Debug)]
pub struct Gam {
@ -143,21 +142,17 @@ impl Gam {
Ok(())
}
pub fn request_content_canvas(&self, requestor_name: &str, redraw_id: usize) -> Result<Gid, xous::Error> {
let mut server = String::<256>::new();
use core::fmt::Write;
write!(server, "{}", requestor_name).expect("GAM_API: couldn't write request_content_canvas server name");
let req = ContentCanvasRequest {
canvas: Gid::new([0,0,0,0]),
servername: server,
redraw_scalar_id: redraw_id,
};
let mut buf = Buffer::into_buf(req).or(Err(xous::Error::InternalError))?;
pub fn request_content_canvas(&self, token: [u32; 4]) -> Result<Gid, xous::Error> {
let mut buf = Buffer::into_buf(token).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, Opcode::RequestContentCanvas.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() {
api::Return::ContentCanvasReturn(ret) => {
Ok(ret.canvas)
if let Some(gid) = ret {
Ok(gid)
} else {
Err(xous::Error::InternalError)
}
}
_ => {
log::error!("GAM_API: request_content_canvas got a return value from the server that isn't expected or handled");
@ -191,22 +186,28 @@ impl Gam {
Err(xous::Error::InternalError)
}
}
pub fn register_input_focus_listener(&self, cb: fn(String::<4000>)) -> Result<(), xous::Error> {
if unsafe{INPUT_CB}.is_some() {
return Err(xous::Error::MemoryInUse) // can't hook it twice
pub fn register_ux(&self, registration: UxRegistration) -> Result<Option<[u32; 4]>, xous::Error> {
let mut buf = Buffer::into_buf(registration).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, Opcode::RegisterUx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() {
api::Return::UxToken(token) => {
Ok(token)
}
_ => {
Err(xous::Error::InternalError)
}
}
unsafe{INPUT_CB = Some(cb)};
if self.callback_sid.is_none() {
let sid = xous::create_server().unwrap();
self.callback_sid = Some(sid);
let sid_tuple = sid.to_u32();
xous::create_thread_4(callback_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
xous::send_message(self.cid,
Message::new_scalar(Opcode::RegisterInputFocus.to_usize().unwrap(),
sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize
)).unwrap();
}
Ok(())
}
pub fn set_audio_opcode(&self, opcode: u32, token: [u32; 4]) -> Result<(), xous::Error> {
let audio_op = SetAudioOpcode {
token,
opcode,
};
let buf = Buffer::into_buf(audio_op).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::SetAudioOpcode.to_u32().unwrap()).or(Err(xous::Error::InternalError)).map(|_| ())
}
}
@ -230,26 +231,3 @@ impl Drop for Gam {
}
}
}
/// handles callback messages from server, in the library user's process space.
fn callback_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
loop {
let msg = xous::receive_message(sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(ImefCallback::GotInputLine) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let inputline = buffer.to_original::<String::<4000>, _>().unwrap();
unsafe {
if let Some(cb) = INPUT_CB {
cb(inputline)
}
}
},
Some(ImefCallback::Drop) => {
break; // this exits the loop and kills the thread
}
None => (),
}
}
}

View file

@ -14,69 +14,302 @@ mod layouts;
use layouts::*;
use graphics_server::*;
use ime_plugin_api::ImeFrontEndApi;
use content_plugin_api::{ContentCanvasConnection, ContentCanvasApi};
use ime_plugin_api::{ImeFrontEndApi, ImefDescriptor};
use log::info;
use heapless::FnvIndexMap;
use num_traits::FromPrimitive;
use num_traits::*;
use xous_ipc::{Buffer, String};
use api::Opcode;
use xous::{msg_scalar_unpack, msg_blocking_scalar_unpack};
use core::{sync::atomic::{AtomicU32, Ordering}};
//// 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
// - add auth tokens to audio streams, so less trusted processes can make direct connections to the codec and reduce latency
// - create the UxRegistration struct
// - convert shellchat to register a Ux
// - move vibe call to the GAM, reduce keyboard connections to 1
// - add auth tokens to audio streams, so less trusted processes can make direct connections to the codec and reduce latency
// - macro-ize the unwrapping of the UxLayout enum
pub(crate) enum UxType {
Chat,
Menu,
#[derive(Debug, Copy, Clone)]
pub(crate) enum UxLayout {
Chat(ChatLayout),
Menu(MenuLayout),
}
/// operations sent to a UxContext owner
pub(crate) enum UxEvents {
Redraw,
GotInputLine,
AudioFrame, // ScalarHooked
pub(crate) fn uxlayout_get_content_canvas(ul: UxLayout) -> Gid {
match ul {
UxLayout::Chat(layout) => layout.get_content_canvas(),
UxLayout::Menu(layout) => layout.get_content_canvas(),
}
}
pub(crate) fn uxlayout_resize_height(ul: &mut UxLayout, gfx: &graphics_server::Gfx, new_height: i16, status_canvas: &Canvas, canvases: &mut FnvIndexMap<Gid, Canvas, MAX_CANVASES>) -> Result<Point, xous::Error> {
match ul {
UxLayout::Chat(layout) => layout.resize_height(gfx, new_height, status_canvas, canvases),
UxLayout::Menu(layout) => layout.resize_height(gfx, new_height, status_canvas, canvases),
}
}
pub(crate) fn uxlayout_clear(ul: UxLayout, gfx: &graphics_server::Gfx, canvases: &mut FnvIndexMap<Gid, Canvas, {crate::MAX_CANVASES}>) -> Result<(), xous::Error> {
match ul {
UxLayout::Chat(layout) => layout.clear(gfx, canvases),
UxLayout::Menu(layout) => layout.clear(gfx, canvases),
}
}
pub(crate) fn uxlayout_get_input_canvas(ul: UxLayout) -> Option<Gid> {
match ul {
UxLayout::Chat(layout) => layout.get_input_canvas(),
UxLayout::Menu(layout) => layout.get_input_canvas(),
}
}
pub(crate) fn uxlayout_get_prediction_canvas(ul: UxLayout) -> Option<Gid> {
match ul {
UxLayout::Chat(layout) => layout.get_prediction_canvas(),
UxLayout::Menu(layout) => layout.get_prediction_canvas(),
}
}
#[derive(Debug, Copy, Clone)]
pub(crate) struct UxContext {
/// where the current interaction line should be rendered
pub input_canvas: Option<Gid>,
/// where, if any, should predictions be rendered
pub prediction_canvas: Option<Gid>,
/// what prediction engine to use
pub predictor: Option<&'static str>,
/// the type of the Ux defined here
pub ux_type: UxType,
/// a putative human-readable name given to the context. Passed to the TokenManager to compute a trust level
pub token: String::<128>,
/// the Ux is free to draw anything on this canvas
pub output_canvas: Option<Gid>,
/// trust level, 255 is most trusted
pub layout: UxLayout,
/// what prediction engine is being used
pub predictor: Option<String::<64>>,
/// a putative human-readable name given to the context.
/// Passed to the TokenManager to compute a trust level; add the app's name to tokens.rs EXPECTED_BOOT_CONTEXTS if you want this to succeed.
pub app_token: [u32; 4], // shared with the app, can be used for other auths to other servers (e.g. audio codec)
/// a token associated with the UxContext, but private to the GAM (not shared with the app). [currently no use for this, just seems like a good idea...]
pub gam_token: [u32; 4],
/// sets a trust level, 255 is the highest (status bar); 254 is a boot-validated context. Less trusted content canvases default to 127.
pub trust_level: u8,
/// audio playback auth token
pub audio_auth: Option<[u32; 4]>,
/// CID to send ContextEvents
pub listener: Option<CID>,
pub listener: xous::CID,
/// opcode ID for redraw
pub redraw_id: Option<usize>,
pub redraw_id: u32,
/// opcode ID for GotInput Line
pub gotinput_id: Option<usize>,
pub gotinput_id: Option<u32>,
/// opcode ID for raw keystroke data
pub rawkeys_id: Option<u32>,
/// opcode ID for AudioFrame
pub audioframe_id: Option<usize>,
pub audioframe_id: Option<u32>,
}
const MAX_UX_CONTEXTS: usize = 4;
const DEFAULT_APP_TOKEN: &'static str = "shellchat";
pub(crate) const MAX_CANVASES: usize = 32;
const BOOT_APP_NAME: &'static str = "shellchat"; // this is the app to display on boot
const BOOT_CONTEXT_TRUSTLEVEL: u8 = 254;
struct ContextManager {
tm: TokenManager,
contexts: [Option<UxContext>; MAX_UX_CONTEXTS],
focused_context: Option<[u32; 4]>, // app_token of the app that has I/O focus, if any
}
impl ContextManager {
pub fn new(xns: &xous_names::XousNames) -> Self {
ContextManager {
tm: TokenManager::new(&xns),
contexts: [None; MAX_UX_CONTEXTS],
focused_context: None,
}
}
pub(crate) fn claim_token(&mut self, name: &str) -> Option<[u32; 4]> {
self.tm.claim_token(name)
}
pub(crate) fn allow_untrusted_code(&self) -> bool {
self.tm.allow_untrusted_code()
}
pub(crate) fn is_token_valid(&self, token: [u32; 4]) -> bool {
self.tm.is_token_valid(token)
}
pub(crate) fn register(&mut self,
gfx: &graphics_server::Gfx,
trng: &trng::Trng,
status_canvas: &Canvas,
canvases: &mut FnvIndexMap<Gid, Canvas, MAX_CANVASES>,
registration: UxRegistration)
-> Option<[u32; 4]> {
let maybe_token = self.tm.claim_token(registration.app_name.as_str().unwrap());
let mut found_slot = false;
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 {
layout: UxLayout::Chat(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,
listener: xous::connect(xous::SID::from_array(registration.listener)).unwrap(),
redraw_id: registration.redraw_id,
gotinput_id: registration.gotinput_id,
audioframe_id: registration.audioframe_id,
rawkeys_id: None,
};
for maybe_context in self.contexts.iter_mut() {
if maybe_context.is_none() {
*maybe_context = Some(ux_context);
found_slot = true;
break;
}
}
},
UxType::Menu => {
let menulayout = MenuLayout::init(&gfx, &trng,
BOOT_CONTEXT_TRUSTLEVEL, &status_canvas, canvases).expect("couldn't create menu layout");
let ux_context = UxContext {
layout: UxLayout::Menu(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,
listener: xous::connect(xous::SID::from_array(registration.listener)).unwrap(),
redraw_id: registration.redraw_id,
gotinput_id: None,
audioframe_id: None,
rawkeys_id: registration.rawkeys_id,
};
for maybe_context in self.contexts.iter_mut() {
if maybe_context.is_none() {
*maybe_context = Some(ux_context);
found_slot = true;
break;
}
}
}
}
} else {
// at the moment, we don't allow contexts that are not part of the boot set.
// however, if later on we want to allow those, here is where we would then allocate these
// contexts and assign them a lower trust level
return None;
}
if found_slot {
maybe_token
} else {
None
}
}
pub(crate) fn get_content_canvas(&self, token: [u32; 4]) -> Option<Gid> {
for maybe_context in self.contexts.iter() {
if let Some(context) = maybe_context {
if context.app_token == token {
return Some(uxlayout_get_content_canvas(context.layout));
}
}
}
None
}
pub(crate) fn set_canvas_height(&mut self,
gfx: &graphics_server::Gfx,
gam_token: [u32; 4],
new_height: i16,
status_canvas: &Canvas,
canvases: &mut FnvIndexMap<Gid, Canvas, MAX_CANVASES>) -> Option<Point> {
for maybe_context in self.contexts.iter_mut() {
if let Some(context) = maybe_context {
if context.gam_token == gam_token {
let result = uxlayout_resize_height(&mut context.layout, gfx, new_height, status_canvas, canvases).expect("couldn't adjust height of active Ux context");
return Some(result)
}
}
}
None
}
pub(crate) fn activate(&mut self,
gfx: &graphics_server::Gfx,
imef: &mut ime_plugin_api::ImeFrontEnd,
canvases: &mut FnvIndexMap<Gid, Canvas, MAX_CANVASES>,
token: [u32; 4],
clear: bool,
) {
for maybe_context in self.contexts.iter_mut() {
if let Some(context) = maybe_context {
if context.app_token == token {
let descriptor = ImefDescriptor {
input_canvas: uxlayout_get_input_canvas(context.layout),
prediction_canvas: uxlayout_get_prediction_canvas(context.layout),
predictor: context.predictor,
token: context.gam_token,
};
imef.connect_backend(descriptor).expect("couldn't connect IMEF to the current app");
if clear {
uxlayout_clear(context.layout, 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...
imef.redraw().unwrap();
self.focused_context = Some(context.app_token);
}
}
}
}
pub(crate) fn redraw(&self) -> Result<(), xous::Error> { // redraws the currently focused context
if let Some(token) = self.focused_app() {
for maybe_context in self.contexts.iter() {
if let Some(context) = maybe_context {
if token == context.app_token {
return xous::send_message(context.listener,
xous::Message::new_scalar(context.redraw_id as usize, 0, 0, 0, 0)
).map(|_| ())
}
}
}
} else {
return Err(xous::Error::UseBeforeInit)
}
Err(xous::Error::ServerNotFound)
}
pub(crate) fn find_app_token_by_name(&self, name: &str) -> Option<[u32; 4]> {
self.tm.find_token(name)
}
pub(crate) fn focused_app(&self) -> Option<[u32; 4]> {
self.focused_context
}
pub(crate) fn set_audio_op(&mut self, audio_op: SetAudioOpcode) {
for maybe_context in self.contexts.iter_mut() {
if let Some(context) = maybe_context {
if context.app_token == audio_op.token {
(*context).audioframe_id = Some(audio_op.opcode);
}
}
}
}
pub(crate) fn forward_input(&self, input: String::<4000>) -> Result<(), xous::Error> {
if let Some(token) = self.focused_app() {
for maybe_context in self.contexts.iter() {
if let Some(context) = maybe_context {
if token == context.app_token {
if let Some(input_op) = context.gotinput_id {
let buf = Buffer::into_buf(input).or(Err(xous::Error::InternalError)).unwrap();
return buf.lend(context.listener, input_op).map(|_| ())
}
}
}
}
} else {
return Err(xous::Error::UseBeforeInit)
}
Err(xous::Error::ServerNotFound)
}
}
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();
}
}
#[xous::xous_main]
fn xmain() -> ! {
let debugc = true;
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
info!("my PID is {}", xous::process::id());
@ -84,6 +317,7 @@ fn xmain() -> ! {
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);
log::trace!("starting up...");
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
@ -91,48 +325,30 @@ fn xmain() -> ! {
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");
let mut tm = TokenManager::new(&xns);
let contexts: [Option<UxContext>; MAX_UX_CONTEXTS] = [None; MAX_UX_CONTEXTS];
let mut context_mgr = ContextManager::new(&xns);
// a map of canvases accessable by Gid
let mut canvases: FnvIndexMap<Gid, Canvas, MAX_CANVASES> = FnvIndexMap::new();
let screensize = gfx.screen_size().expect("Couldn't get screen size");
let small_height: i16 = gfx.glyph_height_hint(GlyphStyle::Small).expect("couldn't get glyph height") as i16;
// the status canvas is special -- there can only be one, and it is ultimately trusted
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");
// a map of canvases accessable by Gid
let mut canvases: FnvIndexMap<Gid, Canvas, 32> = FnvIndexMap::new();
//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");
// 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
//canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize));
canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize));
// connect to the IME front end, and set its canvas
info!("acquiring connection to IMEF...");
let imef = ime_plugin_api::ImeFrontEnd::new(&xns).expect("Couldn't connect to IME front end");
//imef.set_input_canvas(chatlayout.input).expect("couldn't set IMEF input canvas");
//imef.set_prediction_canvas(chatlayout.predictive).expect("couldn't set IMEF prediction canvas");
let mut imef = ime_plugin_api::ImeFrontEnd::new(&xns).expect("Couldn't connect to IME front end");
imef.hook_listener_callback(imef_cb).expect("couldn't request events from IMEF");
// ASSUME: shell is our default application, so set a default predictor of Shell
log::trace!("acquiring connection to the default 'shell' predictor...");
//imef.set_predictor(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("couldn't set IMEF prediction to shell");
// NOTE: all three API calls (set_input_canvas, set_prediction_canvas, set_predictor) are mandatory for IMEF initialization
// now update the IMEF area, since we're initialized
//imef.redraw().unwrap();
// no content canvas initially, but keep a placeholder for one
//let mut ccc: ContentCanvasConnection = ContentCanvasConnection{connection: None, redraw_id: None};
//if debugc{info!("chatlayout made st {:?} co {:?} pr {:?} in {:?}", chatlayout.status, chatlayout.content, chatlayout.predictive, chatlayout.input);}
// make a thread to manage the status bar -- this needs to start after the IMEF is 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
let status_gid = chatlayout.status.gid();
let status_gid = status_canvas.gid().gid();
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");
@ -217,7 +433,7 @@ fn xmain() -> ! {
TextOp::Render | TextOp::ComputeBounds => {
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 !tm.is_token_valid(tv.token.unwrap()) {
if !context_mgr.is_token_valid(tv.token.unwrap()) {
log::error!("Attempt to draw inverted text without valid credentials. Aborting.");
continue;
}
@ -231,6 +447,11 @@ fn xmain() -> ! {
}
if let Some(canvas) = canvases.get_mut(&tv.get_canvas_gid()) {
// if we're requesting inverted text, this better be a "trusted canvas"
if tv.invert & (canvas.trust_level() < BOOT_CONTEXT_TRUSTLEVEL) {
log::error!("Attempt to draw inverted text without sufficient trust level. Aborting.");
continue;
}
// first, figure out if we should even be drawing to this canvas.
if canvas.is_drawable() {
// set the clip rectangle according to the canvas' location
@ -265,40 +486,23 @@ fn xmain() -> ! {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut cb = buffer.to_original::<SetCanvasBoundsRequest, _>().unwrap();
log::trace!("SetCanvasBoundsRequest {:?}", cb);
// ASSUME:
// very few canvases allow dynamic resizing, so we special case these
if cb.canvas == chatlayout.input {
let newheight = chatlayout.resize(&gfx, cb.requested.y, &mut canvases).expect("SetCanvasBoundsRequest couldn't recompute input canvas height");
cb.granted = Some(newheight);
canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize));
if ccc.connection.is_some() {
ccc.redraw_canvas().expect("couldn't issue redraw to content canvas");
}
} else {
cb.granted = None;
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));
context_mgr.redraw().expect("can't redraw after new canvas bounds");
}
cb.granted = granted;
let ret = api::Return::SetCanvasBoundsReturn(cb);
buffer.replace(ret).unwrap();
}
Some(Opcode::RequestContentCanvas) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut req = buffer.to_original::<ContentCanvasRequest, _>().unwrap();
let req = buffer.to_original::<[u32; 4], _>().unwrap();
log::trace!("RequestContentCanvas {:?}", req);
// for now, we do nothing with the incoming gid value; but, in the future, we can use it
// as an authentication token perhaps to control access
//// here make a connection back to the requesting server, so that we can tell it to redraw if the layout has changed, etc.
if let Ok(cc) = xns.request_connection_blocking(req.servername.as_str().expect("malformed server name in content canvas request")) {
ccc.connection = Some(cc);
ccc.redraw_id = Some(req.redraw_scalar_id);
} else {
log::error!("content requestor gave us a bogus canvas result, aborting");
continue;
};
req.canvas = chatlayout.content;
let ret = api::Return::ContentCanvasReturn(req);
let ret = api::Return::ContentCanvasReturn(context_mgr.get_content_canvas(req));
buffer.replace(ret).unwrap();
}
Some(Opcode::RenderObject) => {
@ -354,37 +558,48 @@ fn xmain() -> ! {
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 = tm.claim_token(tokenclaim.name.as_str().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 tm.allow_untrusted_code() {
if context_mgr.allow_untrusted_code() {
xous::return_scalar(msg.sender, 1).unwrap();
} else {
xous::return_scalar(msg.sender, 0).unwrap();
}
}),
Some(Opcode::RegisterInputFocus) => msg_scalar_unpack!(msg, sid0, sid1, sid2, sid3, {
let sid = xous::SID::from_u32(sid0 as _, sid1 as _, sid2 as _, sid3 as _);
let cid = Some(xous::connect(sid).unwrap());
log::trace!("input focus registered: {:?}", sid);
let mut found = false;
////// ok this is where things diverge: we need to be able to record who, exactly, is registering this, instead of anonymously
////// sending input data to anyone who asks!
for entry in listeners.iter_mut() {
if *entry == None {
*entry = cid;
found = true;
break;
}
}
if !found {
log::error!("RegisterInputFocus listener ran out of space registering callback");
}
}),
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 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();
},
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();
context_mgr.forward_input(inputline).expect("couldn't forward input line to focused app");
},
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 imef, &mut canvases, shellchat_token, true);
}
}
}
// clean up our program
log::trace!("main loop exit, destroying servers");

View file

@ -10,7 +10,11 @@ use xous_ipc::String;
registration of interprocess comms between trusted elements, only relying on ephemeral,
dynamically generated 128-bit tokens.
*/
// 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"];
#[derive(Copy, Clone, Debug)]
pub(crate) struct NamedToken {
token: [u32; 4],
@ -18,14 +22,12 @@ pub(crate) struct NamedToken {
}
pub(crate) struct TokenManager {
tokens: [Option<NamedToken>; TOKEN_SLOTS],
slot_names: [&'static str; TOKEN_SLOTS],
trng: trng::Trng,
}
impl<'a> TokenManager {
pub(crate) fn new(xns: &xous_names::XousNames) -> TokenManager {
TokenManager {
tokens: [None; TOKEN_SLOTS],
slot_names: ["status", "menu", "passwords"],
trng: trng::Trng::new(&xns).unwrap(),
}
}
@ -42,7 +44,7 @@ impl<'a> TokenManager {
pub(crate) 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() {
for &valid_name in EXPECTED_BOOT_CONTEXTS.iter() {
if name.eq(valid_name) {
valid = true;
}
@ -88,4 +90,14 @@ impl<'a> TokenManager {
}
false
}
pub(crate) fn find_token(&self, name: &str) -> Option<[u32; 4]> {
for maybe_token in self.tokens.iter() {
if let Some(token) = maybe_token {
if token.name == String::<128>::from_str(name) {
return Some(token.token)
}
}
}
None
}
}

View file

@ -3,7 +3,7 @@
use gam::api::SetCanvasBoundsRequest;
use ime_plugin_api::{ImefOpcode, ImefCallback};
use ime_plugin_api::{ImefCallback, ImefDescriptor, ImefOpcode};
use log::{error, info};
@ -29,6 +29,8 @@ struct InputTracker {
input_canvas: Option<Gid>,
/// prediction display area, as given by the GAM
pred_canvas: Option<Gid>,
/// gam token, so the GAM can find the application that requested us
gam_token: Option<[u32; 4]>,
/// our current prediction engine
predictor: Option<PredictionPlugin>,
@ -65,6 +67,7 @@ impl InputTracker {
pred_canvas: None,
predictor: None,
pred_triggers: None,
gam_token: None,
can_unpick: false,
pred_phrase: String::<4000>::new(),
last_trigger_char: Some(0),
@ -76,6 +79,9 @@ impl InputTracker {
pred_options: [None; MAX_PREDICTION_OPTIONS],
}
}
pub fn set_gam_token(&mut self, token: [u32; 4]) {
self.gam_token = Some(token);
}
pub fn set_predictor(&mut self, predictor: PredictionPlugin) {
self.predictor = Some(predictor);
self.pred_triggers = Some(predictor.get_prediction_triggers()
@ -84,9 +90,11 @@ impl InputTracker {
pub fn set_input_canvas(&mut self, input: Gid) {
self.input_canvas = Some(input);
}
pub fn clear_input_canvas(&mut self) { self.input_canvas = None }
pub fn set_pred_canvas(&mut self, pred: Gid) {
self.pred_canvas = Some(pred);
}
pub fn clear_pred_canvas(&mut self) { self.pred_canvas = None }
pub fn is_init(&self) -> bool {
self.input_canvas.is_some() && self.pred_canvas.is_some() && self.predictor.is_some()
}
@ -356,9 +364,9 @@ impl InputTracker {
self.insertion = 0;
if self.was_grown {
let mut req = SetCanvasBoundsRequest {
canvas: ic,
requested: Point::new(0, 0), // size 0 will snap to the original smallest default size
granted: None,
token: self.gam_token.unwrap(),
};
if debug1{info!("attempting resize to {:?}", req.requested);}
self.gam.set_canvas_bounds_request(&mut req).expect("couldn't call set_bounds_request on input area overflow");
@ -462,9 +470,9 @@ impl InputTracker {
31 // a default value to grow in case we don't have a valid last height
};
let mut req = SetCanvasBoundsRequest {
canvas: ic,
requested: Point::new(0, ic_bounds.y + delta as i16),
granted: None,
token: self.gam_token.unwrap(),
};
if debug1{info!("attempting resize to {:?}", req.requested);}
self.gam.set_canvas_bounds_request(&mut req).expect("couldn't call set_bounds_request on input area overflow");
@ -606,8 +614,7 @@ fn xmain() -> ! {
let xns = xous_names::XousNames::new().unwrap();
// only one connection allowed: GAM
// however, currently have to refactor out the shellchat connection. revert this to 1 when that's done!!
let imef_sid = xns.register_name(ime_plugin_api::SERVER_NAME_IME_FRONT, Some(2)).expect("can't register server");
let imef_sid = xns.register_name(ime_plugin_api::SERVER_NAME_IME_FRONT, Some(1)).expect("can't register server");
log::trace!("registered with NS -- {:?}", imef_sid);
// hook the keyboard event server and have it forward keys to our local main loop
@ -624,28 +631,30 @@ fn xmain() -> ! {
let msg = xous::receive_message(imef_sid).unwrap();
log::trace!("Message: {:?}", msg);
match FromPrimitive::from_usize(msg.body.id()) {
Some(ImefOpcode::SetInputCanvas) => {
msg_scalar_unpack!(msg, g0, g1, g2, g3, {
let g = Gid::new([g0 as _, g1 as _, g2 as _, g3 as _]);
if debug1 || dbgcanvas {info!("got input canvas {:?}", g);}
tracker.set_input_canvas(g);
});
}
Some(ImefOpcode::SetPredictionCanvas) => {
msg_scalar_unpack!(msg, g0, g1, g2, g3, {
let g = Gid::new([g0 as _, g1 as _, g2 as _, g3 as _]);
if debug1 || dbgcanvas {info!("got prediction canvas {:?}", g);}
tracker.set_pred_canvas(g);
});
}
Some(ImefOpcode::SetPredictionServer) => {
Some(ImefOpcode::ConnectBackend) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let s = buffer.as_flat::<String::<64>, _>().unwrap();
log::trace!("got prediction server: {}", s.as_str());
match xns.request_connection(s.as_str()) {
Ok(pc) => tracker.set_predictor(ime_plugin_api::PredictionPlugin {connection: Some(pc)}),
_ => error!("can't find predictive engine {}, retaining existing one.", s.as_str()),
let descriptor = buffer.to_original::<ImefDescriptor, _>().unwrap();
if let Some(input) = descriptor.input_canvas {
if debug1 || dbgcanvas {info!("got input canvas {:?}", input);}
tracker.set_input_canvas(input);
} else {
tracker.clear_input_canvas();
}
if let Some(pred) = descriptor.prediction_canvas {
if debug1 || dbgcanvas {info!("got prediction canvas {:?}", pred);}
tracker.set_pred_canvas(pred);
} else {
tracker.clear_pred_canvas();
}
if let Some(s) = descriptor.predictor {
log::trace!("got prediction server: {}", s.as_str().unwrap());
match xns.request_connection(s.as_str().unwrap()) {
Ok(pc) => tracker.set_predictor(ime_plugin_api::PredictionPlugin {connection: Some(pc)}),
_ => error!("can't find predictive engine {}, retaining existing one.", s.as_str().unwrap()),
}
}
tracker.set_gam_token(descriptor.token);
}
Some(ImefOpcode::RegisterListener) => msg_scalar_unpack!(msg, sid0, sid1, sid2, sid3, {
let sid = xous::SID::from_u32(sid0 as _, sid1 as _, sid2 as _, sid3 as _);

View file

@ -176,14 +176,8 @@ impl PredictionApi for PredictionPlugin {
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum ImefOpcode {
/// informs me where my input canvas is
SetInputCanvas, //(Gid),
/// informs me where my prediction canvas is
SetPredictionCanvas, //(Gid),
/// set prediction server. Must be a String of the name of a server that is loaded in the system.
SetPredictionServer, //(String<64>),
/// connect an backend configuration
ConnectBackend,
/// register a listener for finalized inputs
RegisterListener, //(String<64>),
@ -203,10 +197,16 @@ pub enum ImefCallback {
Drop,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct ImefDescriptor {
pub input_canvas: Option<graphics_server::Gid>,
pub prediction_canvas: Option<graphics_server::Gid>,
pub predictor: Option<String::<64>>,
pub token: [u32; 4], // token used to lookup our connected app inside the GAM
}
pub trait ImeFrontEndApi {
fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error>;
fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error>;
fn set_predictor(&self, servername: &str) -> Result<(), xous::Error>;
fn connect_backend(&self, descriptor: ImefDescriptor) -> Result<(), xous::Error>;
fn hook_listener_callback(&mut self, cb: fn(String::<4000>)) -> Result<(), xous::Error>;
fn redraw(&self) -> Result<(), xous::Error>;
}
@ -250,28 +250,9 @@ impl Drop for ImeFrontEnd {
}
impl ImeFrontEndApi for ImeFrontEnd {
fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
send_message(self.cid,
Message::new_scalar(ImefOpcode::SetInputCanvas.to_usize().unwrap(),
g.gid()[0] as _, g.gid()[1] as _, g.gid()[2] as _, g.gid()[3] as _
))?;
Ok(())
}
fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
send_message(self.cid,
Message::new_scalar(ImefOpcode::SetPredictionCanvas.to_usize().unwrap(),
g.gid()[0] as _, g.gid()[1] as _, g.gid()[2] as _, g.gid()[3] as _
))?;
Ok(())
}
fn set_predictor(&self, servername: &str) -> Result<(), xous::Error> {
let mut server = String::<64>::new();
use core::fmt::Write;
write!(server, "{}", servername).expect("couldn't write set_predictor server name");
let buf = Buffer::into_buf(server).or(Err(xous::Error::InternalError))?;
buf.lend(self.cid, ImefOpcode::SetPredictionServer.to_u32().unwrap()).map(|_|())
fn connect_backend(&self, descriptor: ImefDescriptor) -> Result<(), xous::Error> {
let buf = Buffer::into_buf(descriptor).or(Err(xous::Error::InternalError))?;
buf.lend(self.cid, ImefOpcode::ConnectBackend.to_u32().unwrap()).or(Err(xous::Error::InternalError)).map(|_| ())
}
fn hook_listener_callback(&mut self, cb: fn(String::<4000>)) -> Result<(), xous::Error> {

View file

@ -1,4 +1,6 @@
#![cfg_attr(target_os = "none", no_std)]
pub const SERVER_NAME_IME_PLUGIN_SHELL: &str = "_IME shell plugin_";
// just inherit all the default from the ime_plugin_api
pub use ime_plugin_api::*;

View file

@ -18,7 +18,7 @@ fn xmain() -> ! {
let xns = xous_names::XousNames::new().unwrap();
// one connection only, should be the GAM
let ime_sh_sid = xns.register_name(xous::names::SERVER_NAME_IME_PLUGIN_SHELL, Some(1)).expect("can't register server");
let ime_sh_sid = xns.register_name(ime_plugin_shell::SERVER_NAME_IME_PLUGIN_SHELL, Some(1)).expect("can't register server");
log::trace!("registered with NS -- {:?}", ime_sh_sid);
let mut history: Queue<String<64>, U4> = Queue::new(); // this has 2^4 elements = 16??? or does it just have 4 elements.

View file

@ -11,6 +11,7 @@ content-plugin-api = {path = "../content-plugin-api"}# all content canvas provid
gam = {path = "../gam"}
graphics-server = {path = "../graphics-server"}
ime-plugin-api = {path = "../ime-plugin-api"}
ime-plugin-shell = {path = "../ime-plugin-shell"}
llio = {path = "../llio"}
rtc = {path = "../rtc"}
log = "0.4"

View file

@ -5,7 +5,7 @@ use log::info;
use core::fmt::Write;
use ime_plugin_api::{ImeFrontEndApi, ImeFrontEnd};
use gam::UxRegistration;
use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, GlyphStyle, PixelColor};
use xous::MessageEnvelope;
use xous_ipc::{String, Buffer};
@ -46,14 +46,26 @@ struct Repl {
// command environment
env: CmdEnv,
// our security token for making changes to our record on the GAM
token: [u32; 4],
}
impl Repl{
fn new(xns: &xous_names::XousNames, my_server_name: &str) -> Self {
fn new(xns: &xous_names::XousNames, sid: xous::SID) -> Self {
let gam = gam::Gam::new(xns).expect("can't connect to GAM");
let content = gam.request_content_canvas(
my_server_name,
ShellOpcode::Redraw.to_usize().unwrap()
).expect("couldn't get content canvas");
let token = gam.register_ux(UxRegistration {
app_name: String::<128>::from_str(APP_NAME_SHELLCHAT),
ux_type: gam::UxType::Chat,
predictor: Some(String::<64>::from_str(ime_plugin_shell::SERVER_NAME_IME_PLUGIN_SHELL)),
listener: sid.to_array(), // note disclosure of our SID to the GAM -- the secret is now shared with the GAM!
redraw_id: ShellOpcode::Redraw.to_u32().unwrap(),
gotinput_id: Some(ShellOpcode::Line.to_u32().unwrap()),
audioframe_id: None,
rawkeys_id: None,
}).expect("couldn't register Ux context for shellchat");
let content = gam.request_content_canvas(token.unwrap()).expect("couldn't get content canvas");
let screensize = gam.get_canvas_bounds(content).expect("couldn't get dimensions of content canvas");
Repl {
input: None,
@ -68,6 +80,7 @@ impl Repl{
bubble_radius: 4,
bubble_space: 4,
env: CmdEnv::new(xns),
token: token.unwrap(),
}
}
@ -220,7 +233,7 @@ impl Repl{
}
}
////////////////// local message passing from callback
////////////////// local message passing from Ux Callback
use num_traits::{ToPrimitive, FromPrimitive};
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
@ -232,17 +245,11 @@ enum ShellOpcode {
/// exit the application
Quit,
}
static mut CB_TO_MAIN_CONN: Option<xous::CID> = None;
fn imef_cb(s: String::<4000>) {
if let Some(cb_to_main_conn) = unsafe{CB_TO_MAIN_CONN} {
let buf = xous_ipc::Buffer::into_buf(s).or(Err(xous::Error::InternalError)).unwrap();
buf.lend(cb_to_main_conn, ShellOpcode::Line.to_u32().unwrap()).unwrap();
}
}
//////////////////
pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_";
// nothing prevents the two from being the same, other than naming conventions
pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_"; // used internally by xous-names
pub(crate) const APP_NAME_SHELLCHAT: &str = "shellchat"; // the user-facing name
#[xous::xous_main]
fn xmain() -> ! {
@ -255,12 +262,7 @@ fn xmain() -> ! {
let shch_sid = xns.register_name(SERVER_NAME_SHELLCHAT, None).expect("can't register server");
log::trace!("registered with NS -- {:?}", shch_sid);
unsafe{CB_TO_MAIN_CONN = Some(xous::connect(shch_sid).unwrap())};
// decrement imef allowlist count when this is refactored!
let mut imef = ImeFrontEnd::new(&xns).expect("can't connect to IMEF");
imef.hook_listener_callback(imef_cb).expect("couldn't request events from IMEF");
let mut repl = Repl::new(&xns, SERVER_NAME_SHELLCHAT);
let mut repl = Repl::new(&xns, shch_sid);
let mut update_repl = false;
let mut was_callback = false;
@ -300,11 +302,6 @@ fn xmain() -> ! {
}
// clean up our program
log::trace!("main loop exit, destroying servers");
unsafe{
if let Some(cb) = CB_TO_MAIN_CONN {
xous::disconnect(cb).unwrap();
}
}
xns.unregister_server(shch_sid).unwrap();
xous::destroy_server(shch_sid).unwrap();
log::trace!("quitting");

View file

@ -20,8 +20,6 @@ pub mod string;
pub mod stringbuffer;
pub mod syscall;
pub mod names;
pub use arch::{ProcessArgs, ProcessInit, ProcessKey, ThreadInit};
pub use definitions::*;
pub use messages::*;

View file

@ -1,32 +0,0 @@
#![allow(dead_code)]
/*
A list of descriptions of servers ("names") that are used as keys by the xous-names server
to refer to servers that are not statically bound into the kernel, or have a special debug purpose.
*/
pub const SERVER_NAME_IME_PLUGIN_SHELL: &str = "_IME shell plugin_";
/*
Global message IDs
The top 8 bits of message ID fields are reserved for Xous.
Thus IDs are structured as follows:
global ID message ID
| |
0xGG_MMMMMMM
Here is the allocation so far:
0x00 - private use by individual servers, allocated in api.rs for each server
0x01 - keyboard responses
0x02 - COM responses
*/
pub const GID_KEYBOARD_KEYSTATE_EVENT: usize = 0x01_000000;
pub const GID_KEYBOARD_RAW_KEYSTATE_EVENT: usize = 0x01_000001;
pub const GID_COM_BATTSTATS_EVENT: usize = 0x02_000000;