tighten up APIs, implement a very 0th-order input loop

this is where the debugging begins of the beginning of
the API. it's like finally finding the start of a roll
of clear tape.
This commit is contained in:
bunnie 2021-03-04 19:02:56 +08:00
parent fff7c024ad
commit 5ea2a21d2d
10 changed files with 212 additions and 62 deletions

5
Cargo.lock generated
View file

@ -390,7 +390,7 @@ dependencies = [
"com",
"graphics-server",
"heapless",
"ime-frontend",
"ime-plugin-api",
"keyboard",
"log",
"log-server",
@ -530,6 +530,8 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
name = "ime-frontend"
version = "0.1.0"
dependencies = [
"blitstr-ref",
"gam",
"graphics-server",
"heapless",
"ime-plugin-api",
@ -547,6 +549,7 @@ dependencies = [
name = "ime-plugin-api"
version = "0.1.0"
dependencies = [
"graphics-server",
"log",
"rkyv",
"xous",

View file

@ -19,7 +19,7 @@ heapless = "0.5"
log = "0.4"
com = { path = "../com"}
blitstr-ref = { path = "../../imports/blitstr-ref" }
ime-frontend = {path="../ime-frontend"}
ime-plugin-api = {path="../ime-plugin-api"}
[target.'cfg(not(any(windows,unix)))'.dependencies]

View file

@ -11,6 +11,7 @@ use canvas::*;
use blitstr_ref as blitstr;
use blitstr::GlyphStyle;
use graphics_server::*;
use ime_plugin_api::ImeFrontEndApi;
use log::info;
@ -138,11 +139,12 @@ fn xmain() -> ! {
// connect to the IME front end, and set its canvas
info!("GAM: acquiring connection to IMEF...");
let imef_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_FRONT).expect("GAM: can't connect to the IME front end");
ime_frontend::set_input_canvas(imef_conn, chatlayout.input).expect("GAM: couldn't set IMEF input canvas");
ime_frontend::set_prediction_canvas(imef_conn, chatlayout.predictive).expect("GAM: couldn't set IMEF prediction canvas");
let imef = ime_plugin_api::ImeFrontEnd{ connection: Some(imef_conn), };
imef.set_input_canvas(chatlayout.input).expect("GAM: couldn't set IMEF input canvas");
imef.set_prediction_canvas(chatlayout.predictive).expect("GAM: couldn't set IMEF prediction canvas");
// ASSUME: shell is our default application, so set a default predictor of Shell
ime_frontend::set_predictor(imef_conn, xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("GAM: couldn't set IMEF prediction to shell");
imef.set_predictor(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("GAM: couldn't set IMEF prediction to shell");
let mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap();
info!("GAM: entering main loop");

View file

@ -12,6 +12,8 @@ log-server = { path = "../log-server" }
ticktimer-server = { path = "../ticktimer-server" }
xous-names = { path = "../xous-names" }
graphics-server = {path = "../graphics-server"}
blitstr-ref = {path = "../../imports/blitstr-ref"}
gam = {path="../gam"}
log = "0.4"
rkyv = { version = "0.3.1", default_features = false }
keyboard = {path="../keyboard"}

View file

@ -1,27 +1,3 @@
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use xous::{CID, send_message};
pub fn set_input_canvas(cid: CID, g: graphics_server::Gid) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::SetInputCanvas(g).into())?;
Ok(())
}
pub fn set_prediction_canvas(cid: CID, g: graphics_server::Gid) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::SetPredictionCanvas(g).into())?;
Ok(())
}
pub fn set_predictor(cid: CID, servername: &str) -> Result<(), xous::Error> {
let mut server = xous::String::<256>::new();
use core::fmt::Write;
write!(server, "{}", servername).expect("IMEF: couldn't write set_predictor server name");
let ime_op = api::Opcode::SetPredictionServer(server);
use rkyv::Write as ArchiveWrite;
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&ime_op).expect("IMEF: couldn't archive SetPredictionServer");
writer.into_inner().lend(cid, pos as u32).expect("IMEF: SetPredicitonServer request failure");
Ok(())
}
pub use ime_plugin_api::{ImeFrontEnd, ImeFrontEndApi};

View file

@ -1,14 +1,17 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
mod api;
use api::*;
use ime_plugin_api::ImefOpcode;
use core::convert::TryFrom;
use core::fmt::Write;
use log::{error, info};
use graphics_server::{Gid, TextView};
use graphics_server::{Gid, TextView, Point, TextBounds, Rectangle, Line};
use blitstr_ref as blitstr;
use blitstr::Cursor;
use heapless::Vec;
use heapless::consts::U32;
use core::pin::Pin;
@ -37,12 +40,9 @@ struct InputTracker {
/// we need to track character widths so we can draw a cursor
charwidths: [u8; 4096],
/// what position the insertion cursor is at in the string. 0 is inserting elements into the front of the string
insertion: u8,
predictions: [Option<TextView>; 4],
insertion: Cursor,
}
impl InputTracker {
pub fn new(gam_conn: xous::CID)-> InputTracker {
InputTracker {
@ -53,8 +53,7 @@ impl InputTracker {
pred_triggers: None,
line: xous::String::<4096>::new(),
charwidths: [0; 4096],
insertion: 0,
predictions: [None; 4],
insertion: Cursor::new(0, 0, 0), // canvases always have (0,0) as the top left
}
}
pub fn set_predictor(&mut self, predictor: PredictionPlugin) {
@ -72,23 +71,63 @@ impl InputTracker {
self.input_canvas.is_some() && self.pred_canvas.is_some()
}
pub fn update(&mut self, newkeys: [char; 4]) {
pub fn update(&mut self, newkeys: [char; 4]) -> Result<(), xous::Error> {
let debug1= true;
// just draw a rectangle for the prediction area for now
if let Some(pc) = self.pred_canvas {
let pc_bounds: Point = gam::get_canvas_bounds(self.gam_conn, pc).expect("IMEF: Couldn't get prediction canvas bounds");
let mut starting_tv = TextView::new(pc, 255,
TextBounds::BoundingBox(Rectangle::new(Point::new(0, 0), pc_bounds)));
starting_tv.draw_border = true;
starting_tv.border_width = 1;
starting_tv.clear_area = true;
gam::post_textview(self.gam_conn, &mut starting_tv).expect("IMEF: can't draw prediction TextView");
}
if let Some(ic) = self.input_canvas {
let ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, ic).expect("IMEF: Couldn't get input canvas bounds");
let mut input_tv = TextView::new(ic, 255,
TextBounds::BoundingBox(Rectangle::new(Point::new(0,0), ic_bounds)));
input_tv.draw_border = true;
input_tv.border_width = 1;
input_tv.clear_area = true;
for &k in newkeys.iter() {
if debug1{info!("IMEF: got key '{}'", k);}
match k {
'\u{0000}' => (),
'\u{000d}' => {
// carriage return case
write!(self.line, "").expect("IMEF: can't clear line after carriage return");
},
_ => {
self.line.push(k).expect("IMEF: ran out of space pushing character into input line");
write!(input_tv.text, "{}", self.line.as_str().expect("IMEF: couldn't convert str")).expect("IMEF: couldn't update TextView string in input canvas");
gam::post_textview(self.gam_conn, &mut input_tv).expect("IMEF: can't draw input TextView");
if self.insertion.pt.y == input_tv.cursor.pt.y {
self.charwidths[self.line.len()] = (input_tv.cursor.pt.x - self.insertion.pt.x) as u8;
} else {
// line wrapped, assume we wrapped to x = 0
self.charwidths[self.line.len()] = (input_tv.cursor.pt.x - 0) as u8;
}
self.insertion.pt.y = input_tv.cursor.pt.y;
},
}
}
// draw the insertion point
// do a manual type conversion, because external crate etc. etc.
let ins_pt: Point = Point::new(self.insertion.pt.x as i16, self.insertion.pt.y as i16);
gam::draw_line(self.gam_conn, ic,
Line::new(ins_pt, ins_pt + Point::new(0, self.insertion.line_height as i16 - input_tv.margin.y) )).expect("IMEF: can't draw insertion point");
}
/*
what else do we need:
- current string that is being built up
- cursor position in string, so we can do insertion/deletion
*/
// this is just reference to remind me how to decode the key array
/*
for &k in newkeys.iter() {
if k != '\u{0000}' {
key_queue.push(k).unwrap();
if debug1{info!("IMEF: got key '{}'", k);}
}
}*/
Ok(())
}
}
@ -111,15 +150,15 @@ fn xmain() -> ! {
info!("IMEF: waiting for my canvas Gids");
loop {
let envelope = xous::receive_message(imef_sid).unwrap();
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
if let Ok(opcode) = ImefOpcode::try_from(&envelope.body) {
match opcode {
Opcode::SetInputCanvas(g) => {
ImefOpcode::SetInputCanvas(g) => {
if debug1{info!("IMEF: got input canvas {:?}", g);}
tracker.input_canvas = Some(g);
tracker.set_input_canvas(g);
},
Opcode::SetPredictionCanvas(g) => {
ImefOpcode::SetPredictionCanvas(g) => {
if debug1{info!("IMEF: got prediction canvas {:?}", g);}
tracker.pred_canvas = Some(g);
tracker.set_pred_canvas(g);
},
_ => info!("IMEF: expected canvas Gid, but got {:?}", opcode)
}
@ -136,17 +175,17 @@ fn xmain() -> ! {
loop {
let envelope = xous::receive_message(imef_sid).unwrap();
if debug1{info!("IMEF: got message {:?}", envelope);}
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
if let Ok(opcode) = ImefOpcode::try_from(&envelope.body) {
match opcode {
Opcode::SetInputCanvas(g) => {
ImefOpcode::SetInputCanvas(g) => {
// there are valid reasons for this to happen, but it should be rare.
info!("IMEF: warning: input canvas Gid has been reset");
tracker.input_canvas = Some(g);
tracker.set_input_canvas(g);
},
Opcode::SetPredictionCanvas(g) => {
ImefOpcode::SetPredictionCanvas(g) => {
// there are valid reasons for this to happen, but it should be rare.
info!("IMEF: warning: prediction canvas Gid has been reset");
tracker.pred_canvas = Some(g);
tracker.set_pred_canvas(g);
},
_ => info!("IMEF: unhandled opcode {:?}", opcode)
}
@ -154,10 +193,10 @@ fn xmain() -> ! {
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
let bytes = Pin::new(buf.as_ref());
let value = unsafe {
archived_value::<api::Opcode>(&bytes, m.id as usize)
archived_value::<ImefOpcode>(&bytes, m.id as usize)
};
match &*value {
rkyv::Archived::<api::Opcode>::SetPredictionServer(rkyv_s) => {
rkyv::Archived::<ImefOpcode>::SetPredictionServer(rkyv_s) => {
let s: xous::String<256> = rkyv_s.unarchive();
match xous_names::request_connection(s.as_str().expect("IMEF: SetPrediction received malformed server name")) {
Ok(pc) => tracker.set_predictor(ime_plugin_api::PredictionPlugin {connection: Some(pc)}),
@ -169,7 +208,7 @@ fn xmain() -> ! {
} else if let Ok(opcode) = keyboard::api::Opcode::try_from(&envelope.body) {
match opcode {
keyboard::api::Opcode::KeyboardEvent(keys) => {
tracker.update(keys);
tracker.update(keys).expect("IMEF: couldn't update input tracker with latest key presses");
},
_ => error!("IMEF: received KBD event opcode that wasn't expected"),
}

View file

@ -8,5 +8,6 @@ description = "IME Plugin Common APIs"
[dependencies]
xous = { path = "../../xous-rs" }
rkyv = { version = "0.3.1", default_features = false }
graphics-server = { path="../graphics-server"} # this is used by the IMEF portion of the API
log = "0.4"

View file

@ -1,6 +1,8 @@
#![cfg_attr(target_os = "none", no_std)]
use xous::{Message, ScalarMessage, String, CID};
use graphics_server::Gid;
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub struct Prediction {
pub index: u32,
@ -192,3 +194,101 @@ impl PredictionApi for PredictionPlugin {
}
}
}
//////////////////////////////////////////////////////
//////////////////// FRONT END API
//////////////////////////////////////////////////////
// Most people won't need to touch this, but it's packaged
// in this crate so we can break circular dependencies
// between the IMEF, GAM, and graphics server
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
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(xous::String<256>),
}
impl core::convert::TryFrom<& Message> for ImefOpcode {
type Error = &'static str;
fn try_from(message: & Message) -> Result<Self, Self::Error> {
match message {
Message::Scalar(m) => match m.id {
0 => Ok(ImefOpcode::SetInputCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))),
1 => Ok(ImefOpcode::SetPredictionCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))),
_ => Err("IMEF api: unknown Scalar ID"),
},
_ => Err("IMEF api: unhandled message type"),
}
}
}
impl Into<Message> for ImefOpcode {
fn into(self) -> Message {
match self {
ImefOpcode::SetInputCanvas(gid) => Message::Scalar(ScalarMessage {
id: 0, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _
}),
ImefOpcode::SetPredictionCanvas(gid) => Message::Scalar(ScalarMessage {
id: 0, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _
}),
_ => panic!("IMEF api: Opcode type not handled by into()"),
}
}
}
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>;
}
pub struct ImeFrontEnd {
pub connection: Option<CID>,
}
impl ImeFrontEndApi for ImeFrontEnd {
fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
match self.connection {
Some(cid) => {
xous::send_message(cid, ImefOpcode::SetInputCanvas(g).into())?;
Ok(())
},
_ => Err(xous::Error::UseBeforeInit)
}
}
fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
match self.connection {
Some(cid) => {
xous::send_message(cid, ImefOpcode::SetPredictionCanvas(g).into())?;
Ok(())
},
_ => Err(xous::Error::UseBeforeInit)
}
}
fn set_predictor(&self, servername: &str) -> Result<(), xous::Error> {
match self.connection {
Some(cid) => {
let mut server = xous::String::<256>::new();
use core::fmt::Write;
write!(server, "{}", servername).expect("IMEF: couldn't write set_predictor server name");
let ime_op = ImefOpcode::SetPredictionServer(server);
use rkyv::Write as ArchiveWrite;
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&ime_op).expect("IMEF: couldn't archive SetPredictionServer");
writer.into_inner().lend(cid, pos as u32).expect("IMEF: SetPredicitonServer request failure");
Ok(())
},
_ => Err(xous::Error::UseBeforeInit)
}
}
}

View file

@ -91,6 +91,33 @@ impl<const N: usize> String<N> {
pub fn to_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.bytes[0..self.len()]) }
}
pub fn push(&mut self, ch: char) -> core::result::Result<(), Error> {
match ch.len_utf8() {
1 => {
if self.len() < self.bytes.len() {
self.bytes[self.len()] = ch as u8;
self.len += 1;
Ok(())
} else {
Err(Error::OutOfMemory)
}
},
_ => {
let mut data: [u8; 4] = [0; 4];
ch.encode_utf8(&mut data);
if self.len() + data.len() < self.bytes.len() {
for &c in data.iter() {
self.bytes[self.len()] = c;
self.len += 1;
}
Ok(())
} else {
Err(Error::OutOfMemory)
}
},
}
}
}
impl<const N: usize> core::fmt::Display for String<N> {