2021-03-03 19:14:26 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
2021-04-04 16:30:46 +00:00
|
|
|
use xous::{Message, CID, send_message};
|
|
|
|
|
use xous_ipc::{Buffer, String};
|
|
|
|
|
use num_traits::ToPrimitive;
|
2021-03-04 11:02:56 +00:00
|
|
|
|
2021-03-15 18:02:53 +00:00
|
|
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
2021-03-03 19:14:26 +00:00
|
|
|
pub struct Prediction {
|
|
|
|
|
pub index: u32,
|
2021-03-07 20:25:58 +00:00
|
|
|
pub valid: bool,
|
2021-04-04 16:30:46 +00:00
|
|
|
pub string: String::<1000>,
|
2021-03-03 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Copy, Clone)]
|
|
|
|
|
pub struct PredictionTriggers {
|
2021-03-07 16:57:50 +00:00
|
|
|
/// trigger line predictions on newline -- if set, sends the *whole* line to the predictor
|
|
|
|
|
/// if just wanting the last word, set `punctuation = true`
|
|
|
|
|
pub newline: bool,
|
|
|
|
|
/// trigger word predictions punctuation
|
|
|
|
|
pub punctuation: bool,
|
|
|
|
|
/// trigger word predictions on whitespace
|
|
|
|
|
pub whitespace: bool,
|
2021-03-03 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
impl Into<usize> for PredictionTriggers {
|
|
|
|
|
fn into(self) -> usize {
|
|
|
|
|
let mut ret: usize = 0;
|
2021-03-07 16:57:50 +00:00
|
|
|
if self.newline { ret |= 0x1; }
|
|
|
|
|
if self.punctuation { ret |= 0x2; }
|
|
|
|
|
if self.whitespace { ret |= 0x4; }
|
2021-03-03 19:14:26 +00:00
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl From<usize> for PredictionTriggers {
|
|
|
|
|
fn from(code: usize) -> PredictionTriggers {
|
|
|
|
|
PredictionTriggers {
|
2021-03-07 16:57:50 +00:00
|
|
|
newline: (code & 0x1) != 0,
|
|
|
|
|
punctuation: (code & 0x2) != 0,
|
|
|
|
|
whitespace: (code & 0x4) != 0,
|
2021-03-03 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 16:30:46 +00:00
|
|
|
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
2021-03-03 19:14:26 +00:00
|
|
|
pub enum Opcode {
|
|
|
|
|
/// update with the latest input candidate. Replaces the previous input.
|
2021-04-04 16:30:46 +00:00
|
|
|
Input, //(String<4000>),
|
2021-03-03 19:14:26 +00:00
|
|
|
|
|
|
|
|
/// feed back to the IME plugin as to what was picked, so predictions can be updated
|
2021-04-04 16:30:46 +00:00
|
|
|
Picked, //(String<4000>),
|
2021-03-03 19:14:26 +00:00
|
|
|
|
|
|
|
|
/// 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
|
2021-04-04 16:30:46 +00:00
|
|
|
Prediction, //(Prediction),
|
2021-03-03 19:14:26 +00:00
|
|
|
|
|
|
|
|
/// return the prediction triggers used by this IME. These are characters that can indicate that a
|
|
|
|
|
/// whole predictive unit has been entered.
|
|
|
|
|
GetPredictionTriggers,
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 16:30:46 +00:00
|
|
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
|
|
|
|
pub enum Return {
|
|
|
|
|
Prediction(Prediction),
|
|
|
|
|
Failure,
|
2021-03-03 19:14:26 +00:00
|
|
|
}
|
2021-03-03 20:33:20 +00:00
|
|
|
|
2021-03-04 08:02:42 +00:00
|
|
|
pub trait PredictionApi {
|
|
|
|
|
fn get_prediction_triggers(&self) -> Result<PredictionTriggers, xous::Error>;
|
|
|
|
|
fn unpick(&self) -> Result<(), xous::Error>;
|
2021-03-07 20:25:58 +00:00
|
|
|
fn set_input(&self, s: String<4000>) -> Result<(), xous::Error>;
|
|
|
|
|
fn feedback_picked(&self, s: String<4000>) -> Result<(), xous::Error>;
|
2021-03-26 12:22:33 +00:00
|
|
|
fn get_prediction(&self, index: u32) -> Result<Option<String<4000>>, xous::Error>;
|
2021-03-04 08:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-03 20:48:22 +00:00
|
|
|
// provide a convenience version of the API for generic/standard calls
|
2021-03-03 20:33:20 +00:00
|
|
|
#[derive(Debug, Default, Copy, Clone)]
|
2021-03-04 08:02:42 +00:00
|
|
|
pub struct PredictionPlugin {
|
|
|
|
|
pub connection: Option<CID>,
|
|
|
|
|
}
|
2021-03-03 20:33:20 +00:00
|
|
|
|
2021-03-04 08:02:42 +00:00
|
|
|
impl PredictionApi for PredictionPlugin {
|
|
|
|
|
fn get_prediction_triggers(&self) -> Result<PredictionTriggers, xous::Error> {
|
|
|
|
|
match self.connection {
|
|
|
|
|
Some(cid) => {
|
2021-04-04 16:30:46 +00:00
|
|
|
let response = send_message(cid,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::GetPredictionTriggers.to_usize().unwrap(), 0, 0, 0, 0))?;
|
2021-03-04 08:02:42 +00:00
|
|
|
if let xous::Result::Scalar1(code) = response {
|
|
|
|
|
Ok(code.into())
|
|
|
|
|
} else {
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit),
|
2021-03-03 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 08:02:42 +00:00
|
|
|
fn unpick(&self) -> Result<(), xous::Error> {
|
|
|
|
|
match self.connection {
|
|
|
|
|
Some(cid) => {
|
2021-04-04 16:30:46 +00:00
|
|
|
send_message(cid,
|
|
|
|
|
Message::new_scalar(Opcode::Unpick.to_usize().unwrap(), 0, 0, 0, 0))?;
|
2021-03-04 08:02:42 +00:00
|
|
|
Ok(())
|
|
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit)
|
|
|
|
|
}
|
2021-03-03 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-07 20:25:58 +00:00
|
|
|
fn set_input(&self, s: String<4000>) -> Result<(), xous::Error> {
|
2021-03-04 08:02:42 +00:00
|
|
|
match self.connection {
|
|
|
|
|
Some(cid) => {
|
2021-04-04 16:30:46 +00:00
|
|
|
let buf = Buffer::into_buf(s).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(cid, Opcode::Input.to_u32().unwrap()).expect("|API: set_input operation failure");
|
2021-03-04 08:02:42 +00:00
|
|
|
Ok(())
|
|
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit),
|
|
|
|
|
}
|
2021-03-03 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-07 20:25:58 +00:00
|
|
|
fn feedback_picked(&self, s: String<4000>) -> Result<(), xous::Error> {
|
2021-03-04 08:02:42 +00:00
|
|
|
match self.connection {
|
|
|
|
|
Some(cid) => {
|
2021-04-04 16:30:46 +00:00
|
|
|
let buf = Buffer::into_buf(s).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(cid, Opcode::Picked.to_u32().unwrap()).expect("|API: feedback_picked operation failure");
|
2021-03-04 08:02:42 +00:00
|
|
|
Ok(())
|
|
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit),
|
|
|
|
|
}
|
2021-03-03 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-26 12:22:33 +00:00
|
|
|
fn get_prediction(&self, index: u32) -> Result<Option<String<4000>>, xous::Error> {
|
2021-03-04 08:02:42 +00:00
|
|
|
match self.connection {
|
|
|
|
|
Some(cid) => {
|
|
|
|
|
let prediction = Prediction {
|
|
|
|
|
index,
|
2021-04-04 16:30:46 +00:00
|
|
|
string: String::<1000>::new(),
|
2021-03-07 20:25:58 +00:00
|
|
|
valid: false,
|
2021-03-04 08:02:42 +00:00
|
|
|
};
|
2021-04-04 16:30:46 +00:00
|
|
|
let mut buf = Buffer::into_buf(prediction).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend_mut(cid, Opcode::Prediction.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
|
|
|
|
|
log::trace!("IME|API: returned from get_prediction");
|
|
|
|
|
|
|
|
|
|
match buf.to_original().unwrap() {
|
|
|
|
|
Return::Prediction(pred) => {
|
|
|
|
|
log::trace!("|API: got {:?}", pred);
|
|
|
|
|
if pred.valid {
|
|
|
|
|
let mut ret = String::<4000>::new();
|
|
|
|
|
use core::fmt::Write as CoreWrite;
|
|
|
|
|
write!(ret, "{}", pred.string).unwrap();
|
|
|
|
|
Ok(Some(ret))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
log::error!("API get_prediction returned an invalid result");
|
|
|
|
|
Err(xous::Error::InternalError)
|
2021-03-07 20:25:58 +00:00
|
|
|
}
|
2021-03-04 08:02:42 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit),
|
2021-03-03 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-04 11:02:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
//////////////////// 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
|
|
|
|
|
|
2021-04-04 16:30:46 +00:00
|
|
|
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
2021-03-04 11:02:56 +00:00
|
|
|
pub enum ImefOpcode {
|
|
|
|
|
/// informs me where my input canvas is
|
2021-04-04 16:30:46 +00:00
|
|
|
SetInputCanvas, //(Gid),
|
2021-03-04 11:02:56 +00:00
|
|
|
|
|
|
|
|
/// informs me where my prediction canvas is
|
2021-04-04 16:30:46 +00:00
|
|
|
SetPredictionCanvas, //(Gid),
|
2021-03-04 11:02:56 +00:00
|
|
|
|
|
|
|
|
/// set prediction server. Must be a String of the name of a server that is loaded in the system.
|
2021-04-04 16:30:46 +00:00
|
|
|
SetPredictionServer, //(String<64>),
|
2021-03-08 19:08:34 +00:00
|
|
|
|
|
|
|
|
/// register a listener for finalized inputs
|
2021-04-04 16:30:46 +00:00
|
|
|
RegisterListener, //(String<64>),
|
2021-03-08 19:08:34 +00:00
|
|
|
|
2021-04-04 16:30:46 +00:00
|
|
|
// this is the event opcode used by listeners
|
|
|
|
|
//GotInputLine, //(String<4000>),
|
|
|
|
|
}
|
|
|
|
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
|
|
|
|
pub(crate) enum ImefReturn {
|
2021-03-26 12:22:33 +00:00
|
|
|
GotInputLine(String<4000>),
|
2021-04-04 16:30:46 +00:00
|
|
|
Failure,
|
2021-03-04 11:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-04 16:30:46 +00:00
|
|
|
/*
|
2021-03-04 11:02:56 +00:00
|
|
|
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 {
|
2021-03-05 17:10:18 +00:00
|
|
|
id: 1, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _
|
2021-03-04 11:02:56 +00:00
|
|
|
}),
|
|
|
|
|
_ => panic!("IMEF api: Opcode type not handled by into()"),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-04 16:30:46 +00:00
|
|
|
}*/
|
2021-03-04 11:02:56 +00:00
|
|
|
|
|
|
|
|
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>;
|
2021-03-08 19:52:39 +00:00
|
|
|
fn register_listener(&self, servername: &str) -> Result<(), xous::Error>;
|
2021-03-04 11:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) => {
|
2021-04-04 16:30:46 +00:00
|
|
|
send_message(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 _
|
|
|
|
|
))?;
|
2021-03-04 11:02:56 +00:00
|
|
|
Ok(())
|
|
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
|
|
|
|
|
match self.connection {
|
|
|
|
|
Some(cid) => {
|
2021-04-04 16:30:46 +00:00
|
|
|
send_message(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 _
|
|
|
|
|
))?;
|
2021-03-04 11:02:56 +00:00
|
|
|
Ok(())
|
|
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_predictor(&self, servername: &str) -> Result<(), xous::Error> {
|
|
|
|
|
match self.connection {
|
|
|
|
|
Some(cid) => {
|
2021-03-26 12:22:33 +00:00
|
|
|
let mut server = String::<64>::new();
|
2021-03-04 11:02:56 +00:00
|
|
|
use core::fmt::Write;
|
2021-04-04 16:30:46 +00:00
|
|
|
write!(server, "{}", servername).expect("couldn't write set_predictor server name");
|
|
|
|
|
let buf = Buffer::into_buf(server).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(cid, ImefOpcode::SetPredictionServer.to_u32().unwrap()).map(|_|())
|
2021-03-04 11:02:56 +00:00
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-08 19:52:39 +00:00
|
|
|
|
|
|
|
|
fn register_listener(&self, servername: &str) -> Result<(), xous::Error> {
|
|
|
|
|
match self.connection {
|
|
|
|
|
Some(cid) => {
|
2021-03-26 12:22:33 +00:00
|
|
|
let mut server = String::<64>::new();
|
2021-03-08 19:52:39 +00:00
|
|
|
use core::fmt::Write;
|
2021-04-04 16:30:46 +00:00
|
|
|
write!(server, "{}", servername).expect("couldn't write set_predictor server name");
|
|
|
|
|
let buf = Buffer::into_buf(server).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(cid, ImefOpcode::RegisterListener.to_u32().unwrap()).map(|_|())
|
2021-03-08 19:52:39 +00:00
|
|
|
},
|
|
|
|
|
_ => Err(xous::Error::UseBeforeInit)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-26 12:22:33 +00:00
|
|
|
}
|