refactor IME plugin API into a common crate

This will allow other APIs to just include the API crate
to define all the gazintas and gozoutas.
This commit is contained in:
bunnie 2021-03-04 03:14:26 +08:00
parent 2b768e9b86
commit 92ca22c38c
12 changed files with 279 additions and 81 deletions

10
Cargo.lock generated
View file

@ -532,6 +532,7 @@ version = "0.1.0"
dependencies = [
"graphics-server",
"heapless",
"ime-plugin-api",
"keyboard",
"log",
"log-server",
@ -542,11 +543,20 @@ dependencies = [
"xous-names",
]
[[package]]
name = "ime-plugin-api"
version = "0.1.0"
dependencies = [
"rkyv",
"xous",
]
[[package]]
name = "ime-plugin-shell"
version = "0.1.0"
dependencies = [
"heapless",
"ime-plugin-api",
"log",
"log-server",
"rkyv",

View file

@ -25,6 +25,7 @@ members = [
"xtask",
"imports/blitstr-ref",
"imports/com_rs-ref",
"services/ime-plugin-api",
]
default-members = [
"services/shell",
@ -41,6 +42,7 @@ default-members = [
"services/gam",
"imports/blitstr-ref",
"imports/com_rs-ref",
"services/ime-plugin-api",
"services/benchmark",
"services/benchmark-target",
"services/ime-frontend",

View file

@ -138,7 +138,8 @@ 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_canvas(imef_conn, chatlayout.input).expect("GAM: couldn't set IMEF canvas");
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 mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap();
info!("GAM: entering main loop");

View file

@ -15,6 +15,7 @@ graphics-server = {path = "../graphics-server"}
log = "0.4"
rkyv = { version = "0.3.1", default_features = false }
keyboard = {path="../keyboard"}
ime-plugin-api = {path="../ime-plugin-api"}
[target.'cfg(not(any(windows,unix)))'.dependencies]

View file

@ -5,11 +5,14 @@ use graphics_server::Gid;
#[allow(dead_code)]
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub enum Opcode {
/// informs me where my canvas is
SetCanvas(Gid),
/// informs me where my input canvas is
SetInputCanvas(Gid),
/// set prediction. Must be a String of the name of a server that is loaded in the system.
SetPrediction(xous::String<256>),
/// 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 Opcode {
@ -17,7 +20,8 @@ impl core::convert::TryFrom<& Message> for Opcode {
fn try_from(message: & Message) -> Result<Self, Self::Error> {
match message {
Message::Scalar(m) => match m.id {
0 => Ok(Opcode::SetCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))),
0 => Ok(Opcode::SetInputCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))),
1 => Ok(Opcode::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"),
@ -28,7 +32,10 @@ impl core::convert::TryFrom<& Message> for Opcode {
impl Into<Message> for Opcode {
fn into(self) -> Message {
match self {
Opcode::SetCanvas(gid) => Message::Scalar(ScalarMessage {
Opcode::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 _
}),
Opcode::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()"),

View file

@ -3,7 +3,12 @@
pub mod api;
use xous::{CID, send_message};
pub fn set_canvas(cid: CID, g: graphics_server::Gid) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::SetCanvas(g).into())?;
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(())
}

View file

@ -12,27 +12,68 @@ use graphics_server::Gid;
use heapless::Vec;
use heapless::consts::U32;
use core::pin::Pin;
use ime_plugin_api::PredictionTriggers;
use rkyv::Unarchive;
use rkyv::archived_value;
/*
what else do we need:
- current string that is being built up
- cursor position in string, so we can do insertion/deletion
*/
fn draw_canvas(gam_conn: xous::CID, canvas: Gid, pred_conn: xous::CID, newkeys: [char; 4]) {
struct InputTracker {
/// connection for handling graphical update requests
pub gam_conn: xous::CID,
/// input area canvas, as given by the GAM
pub input_canvas: Option<Gid>,
/// a connection to our current prediction engine
pub pred_conn: xous::CID,
/// prediction display area, as given by the GAM
pub pred_canvas: Option<Gid>,
/// triggers for predictions
pub pred_triggers: PredictionTriggers,
// 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);}
}
}*/
/// track the progress of our input line
line: xous::String::<4096>,
/// 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,
}
impl InputTracker {
pub fn new(gam_conn: xous::CID, pred_conn: xous::CID)-> InputTracker {
let response = xous::send_message(pred_conn, ime_plugin_api::Opcode::GetPredictionTriggers.into())
.expect("IMEF: InputTracker failed to get predictions from default plugin");
if let xous::Result::Scalar1(code) = response {
InputTracker {
gam_conn,
input_canvas: None,
pred_conn,
pred_canvas: None,
pred_triggers: code.into(),
line: xous::String::<4096>::new(),
charwidths: [0; 4096],
insertion: 0,
}
} else {
panic!("IMEF: InputTracker::new() failed to get prediction triggers")
}
}
pub fn update(&mut self, newkeys: [char; 4]) {
/*
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);}
}
}*/
}
}
#[xous::xous_main]
fn xmain() -> ! {
@ -46,28 +87,34 @@ fn xmain() -> ! {
let kbd_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_KBD).expect("IMEF: can't connect to KBD");
keyboard::request_events(xous::names::SERVER_NAME_IME_FRONT, kbd_conn).expect("IMEF: couldn't request events from keyboard");
let gam_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("IMEF: can't connect to GAM");
// set a "default" prediction that is a shell-like history buffer of things previously typed
let mut prediction_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_PLUGIN_SHELL)
.expect("IMEF: can't connect to shell prediction engine");
let mut tracker = InputTracker::new(
xous_names::request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("IMEF: can't connect to GAM"),
// set a "default" prediction that is a shell-like history buffer of things previously typed
xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("IMEF: can't connect to shell prediction engine"),
);
// The first message should be my Gid from the GAM.
let mut canvas: Gid = Gid::new([0,0,0,0]);
info!("IMEF: waiting for my canvas Gid");
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) {
match opcode {
Opcode::SetCanvas(g) => {
canvas = g;
break;
Opcode::SetInputCanvas(g) => {
if debug1{info!("IMEF: got input canvas {:?}", g);}
tracker.input_canvas = Some(g);
},
Opcode::SetPredictionCanvas(g) => {
if debug1{info!("IMEF: got prediction canvas {:?}", g);}
tracker.pred_canvas = Some(g);
},
_ => info!("IMEF: expected canvas Gid, but got {:?}", opcode)
}
} else {
info!("IMEF: expected canvas Gid, but got other message first {:?}", envelope);
}
if tracker.input_canvas.is_some() && tracker.pred_canvas.is_some() {
break;
}
}
let mut key_queue: Vec<char, U32> = Vec::new();
@ -77,10 +124,15 @@ fn xmain() -> ! {
if debug1{info!("IMEF: got message {:?}", envelope);}
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
match opcode {
Opcode::SetCanvas(g) => {
Opcode::SetInputCanvas(g) => {
// there are valid reasons for this to happen, but it should be rare.
info!("IMEF: warning: canvas Gid has been reset");
canvas = g;
info!("IMEF: warning: input canvas Gid has been reset");
tracker.input_canvas = Some(g);
},
Opcode::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);
},
_ => info!("IMEF: unhandled opcode {:?}", opcode)
}
@ -91,11 +143,11 @@ fn xmain() -> ! {
archived_value::<api::Opcode>(&bytes, m.id as usize)
};
match &*value {
rkyv::Archived::<api::Opcode>::SetPrediction(rkyv_s) => {
let s: xous::String<4096> = rkyv_s.unarchive();
rkyv::Archived::<api::Opcode>::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) => prediction_conn = pc,
_ => error!("IMEF: can't find predictive engine {}, retaining existign one.", s.as_str().expect("IMEF: SetPrediction received malformed server name")),
Ok(pc) => tracker.pred_conn = pc,
_ => error!("IMEF: can't find predictive engine {}, retaining existing one.", s.as_str().expect("IMEF: SetPrediction received malformed server name")),
}
},
_ => panic!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage")
@ -103,7 +155,7 @@ fn xmain() -> ! {
} else if let Ok(opcode) = keyboard::api::Opcode::try_from(&envelope.body) {
match opcode {
keyboard::api::Opcode::KeyboardEvent(keys) => {
draw_canvas(gam_conn, canvas, prediction_conn, keys);
tracker.update(keys);
},
_ => error!("IMEF: received KBD event opcode that wasn't expected"),
}

View file

@ -0,0 +1,95 @@
#![cfg_attr(target_os = "none", no_std)]
use xous::{Message, ScalarMessage};
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub struct Prediction {
pub index: u32,
pub string: xous::String<4096>,
}
////////////////////////////////////////////////
/////////////// TODO: refactor this into a multi-crate API, so that we an inheret this API across multiple implementations
////////////////////////////////////////////////
#[derive(Debug, Default, Copy, Clone)]
pub struct PredictionTriggers {
/// trigger predictions on newline
pub trigger_newline: bool,
/// trigger predictions punctuation
pub trigger_punctuation: bool,
/// trigger predictions on whitespace
pub trigger_whitespace: bool,
}
impl Into<usize> for PredictionTriggers {
fn into(self) -> usize {
let mut ret: usize = 0;
if self.trigger_newline { ret |= 0x1; }
if self.trigger_punctuation { ret |= 0x2; }
if self.trigger_whitespace { ret |= 0x4; }
ret
}
}
impl From<usize> for PredictionTriggers {
fn from(code: usize) -> PredictionTriggers {
PredictionTriggers {
trigger_newline: (code & 0x1) != 0,
trigger_punctuation: (code & 0x2) != 0,
trigger_whitespace: (code & 0x4) != 0,
}
}
}
#[allow(dead_code)]
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub enum Opcode {
/// update with the latest input candidate. Replaces the previous input.
Input(xous::String<4096>),
/// feed back to the IME plugin as to what was picked, so predictions can be updated
Picked(xous::String<4096>),
/// Undo the last Picked value. To be used when a user hits backspace after picking a prediction
/// note that repeated calls to Unpick will have an implementation-defined behavior
Unpick,
/// fetch the prediction at a given index, where the index is ordered from 0..N, where 0 is the most likely prediction
/// if there is no prediction available, just return an empty string
Prediction(Prediction),
/// return the prediction triggers used by this IME. These are characters that can indicate that a
/// whole predictive unit has been entered.
GetPredictionTriggers,
}
impl core::convert::TryFrom<& Message> for Opcode {
type Error = &'static str;
fn try_from(message: & Message) -> Result<Self, Self::Error> {
match message {
Message::Scalar(m) => match m.id {
0 => Ok(Opcode::Unpick),
_ => Err("IME_SH api: unknown Scalar ID"),
},
Message::BlockingScalar(m) => match m.id {
1 => Ok(Opcode::GetPredictionTriggers),
_ => Err("IME_SH api: unknown BlockingScalar ID"),
},
_ => Err("IME_SH api: unhandled message type"),
}
}
}
impl Into<Message> for Opcode {
fn into(self) -> Message {
match self {
Opcode::Unpick => Message::Scalar(ScalarMessage {
id: 0,
arg1: 0, arg2: 0, arg3: 0, arg4: 0,
}),
Opcode::GetPredictionTriggers => Message::BlockingScalar(ScalarMessage {
id: 1,
arg1: 0, arg2: 0, arg3: 0, arg4: 0,
}),
_ => panic!("IME_SH api: Opcode type not handled by Into(), refer to helper method"),
}
}
}

View file

@ -13,7 +13,7 @@ ticktimer-server = { path = "../ticktimer-server" }
xous-names = { path = "../xous-names" }
log = "0.4"
rkyv = { version = "0.3.1", default_features = false }
ime-plugin-api = {path="../ime-plugin-api"}
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}

View file

@ -1,19 +0,0 @@
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub struct Prediction {
pub index: u32,
pub string: xous::String<4096>,
}
#[allow(dead_code)]
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub enum Opcode {
/// update with the latest input candidate. Replaces the previous input.
Input(xous::String<4096>),
/// feed back to the IME plugin as to what was picked, so predictions can be updated
Picked(xous::String<4096>),
/// fetch the prediction at a given index, where the index is ordered from 0..N, where 0 is the most likely prediction
/// if there is no prediction available, just return an empty string
Prediction(Prediction),
}

View file

@ -1,12 +1,27 @@
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use ime_plugin_api::*;
use xous::{CID, String};
pub fn get_prediction_triggers(cid: CID) -> Result<PredictionTriggers, xous::Error> {
let response = xous::send_message(cid, Opcode::GetPredictionTriggers.into())?;
if let xous::Result::Scalar1(code) = response {
Ok(code.into())
} else {
panic!("get_prediciton_triggers failed")
}
}
pub fn unpick(cid: CID) -> Result<(), xous::Error> {
xous::send_message(cid, Opcode::Unpick.into())?;
Ok(())
}
pub fn set_input(cid: CID, s: String<4096>) -> Result<(), xous::Error> {
use rkyv::Write;
let rkyv_input = api::Opcode::Input(s);
let rkyv_input = Opcode::Input(s);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_input).expect("IMES|API: couldn't archive input string");
let xous_buffer = writer.into_inner();
@ -19,7 +34,7 @@ pub fn set_input(cid: CID, s: String<4096>) -> Result<(), xous::Error> {
pub fn feedback_picked(cid: CID, s: String<4096>) -> Result<(), xous::Error> {
use rkyv::Write;
let rkyv_picked = api::Opcode::Picked(s);
let rkyv_picked = Opcode::Picked(s);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_picked).expect("IMES|API: couldn't archive picked string");
let xous_buffer = writer.into_inner();
@ -33,11 +48,11 @@ pub fn get_prediction(cid: CID, index: u32) -> Result<xous::String<4096>, xous::
use rkyv::Write;
use rkyv::Unarchive;
let prediction = api::Prediction {
let prediction = Prediction {
index,
string: xous::String::<4096>::new(),
};
let pred_op = api::Opcode::Prediction(prediction);
let pred_op = Opcode::Prediction(prediction);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&pred_op).expect("IMES|API: couldn't archive prediction request");
@ -45,9 +60,9 @@ pub fn get_prediction(cid: CID, index: u32) -> Result<xous::String<4096>, xous::
xous_buffer.lend_mut(cid, pos as u32).expect("IMES|API: prediction fetch operation failure");
let returned = unsafe { rkyv::archived_value::<api::Opcode>(xous_buffer.as_ref(), pos)};
if let rkyv::Archived::<api::Opcode>::Prediction(result) = returned {
let pred_r: api::Prediction = result.unarchive();
let returned = unsafe { rkyv::archived_value::<Opcode>(xous_buffer.as_ref(), pos)};
if let rkyv::Archived::<Opcode>::Prediction(result) = returned {
let pred_r: Prediction = result.unarchive();
let retstring: xous::String<4096> = pred_r.string.clone();
Ok(retstring)
@ -56,4 +71,5 @@ pub fn get_prediction(cid: CID, index: u32) -> Result<xous::String<4096>, xous::
log::info!("get_prediciton saw an unhandled return of {:?}", r);
Err(xous::Error::InvalidString)
}
}
}

View file

@ -1,8 +1,8 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
mod api;
use api::*;
use ime_plugin_api::*;
use core::convert::TryFrom;
use log::{error, info};
use heapless::spsc::Queue;
@ -28,36 +28,43 @@ fn xmain() -> ! {
let mut history: Queue<xous::String<4096>, U3> = Queue::new(); // this has 2^3 elements = 8
let history_max = 8;
let mytriggers = PredictionTriggers {
trigger_newline: true,
trigger_punctuation: false,
trigger_whitespace: false,
};
loop {
let envelope = xous::receive_message(ime_sh_sid).unwrap();
if debug1{info!("IME_SH: received message {:?}", envelope);}
if let xous::Message::Borrow(m) = &envelope.body {
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::<Opcode>(&bytes, m.id as usize)
};
match &*value {
rkyv::Archived::<api::Opcode>::Input(rkyv_s) => {
rkyv::Archived::<Opcode>::Input(_rkyv_s) => {
// shell does nothing with the input, it only keeps track of
// the picked results
},
rkyv::Archived::<Opcode>::Picked(rkyv_s) => {
let s: xous::String<4096> = rkyv_s.unarchive();
if history.len() == history_max {
history.dequeue().expect("IME_SH: couldn't dequeue history");
}
history.enqueue(s).expect("IME_SH: couldn't store history");
},
rkyv::Archived::<api::Opcode>::Picked(rkyv_s) => {
let s: xous::String<4096> = rkyv_s.unarchive();
if debug1{info!("Received prediction feedback but ignored: {}", s);}
},
_ => error!("IME_SH: unknown Borrow message")
};
} else if let xous::Message::MutableBorrow(m) = &envelope.body {
let mut buf = unsafe { xous::XousBuffer::from_memory_message(m) };
let value = unsafe {
archived_value_mut::<api::Opcode>(Pin::new(buf.as_mut()), m.id as usize)
archived_value_mut::<Opcode>(Pin::new(buf.as_mut()), m.id as usize)
};
match &*value {
rkyv::Archived::<api::Opcode>::Prediction(pred_r) => {
let mut prediction: api::Prediction = pred_r.unarchive();
rkyv::Archived::<Opcode>::Prediction(pred_r) => {
let mut prediction: Prediction = pred_r.unarchive();
if history.len() > 0 {
let mut index = prediction.index;
if index >= history.len() as u32 {
@ -81,6 +88,27 @@ fn xmain() -> ! {
},
_ => error!("IME_SH: unknown MutableBorrow message"),
}
} else if let Ok(opcode) = Opcode::try_from(&envelope.body) {
match opcode {
Opcode::Unpick => {
if history.len() == 1 {
history.dequeue().expect("IME_SH: couldn't dequeue in Unpick (1)");
} else if history.len() > 1 {
// rotate everything around except the last entry
for _ in 0 .. history.len() - 1 {
let s = history.dequeue().expect("IME_SH: couldn't dequee in Unpick (>1)");
history.enqueue(s).expect("IME_SH: couldn't enqueue in Unpick");
}
// discard the last entry
history.dequeue().expect("IME_SH: couldn't dequeue in Unpick (>1 last)");
}
// in case of 0 length, do nothing
},
Opcode::GetPredictionTriggers => {
xous::return_scalar(envelope.sender, mytriggers.into()).expect("IME_SH: couldn't return GetPredictionTriggers");
},
_ => error!("IME_SH: unknown Opcode"),
}
} else {
info!("IME_SH: received unknown message type {:?}", envelope);
panic!("IME_SH: received unknown message type");