refactor of the imef interface part 1

This commit is contained in:
bunnie 2021-04-05 00:30:46 +08:00
parent 9cc94d05ec
commit 8fed353a83
6 changed files with 161 additions and 248 deletions

4
Cargo.lock generated
View file

@ -580,6 +580,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"graphics-server", "graphics-server",
"log", "log",
"num-derive",
"num-traits",
"rkyv 0.4.3", "rkyv 0.4.3",
"xous", "xous",
"xous-ipc", "xous-ipc",
@ -594,6 +596,8 @@ dependencies = [
"ime-plugin-api", "ime-plugin-api",
"log", "log",
"log-server", "log-server",
"num-derive",
"num-traits",
"rkyv 0.4.3", "rkyv 0.4.3",
"ticktimer-server", "ticktimer-server",
"utralib", "utralib",

View file

@ -12,7 +12,6 @@ xous = {path = "../../xous-rs"}
xous-ipc = {path = "../../xous-ipc"} xous-ipc = {path = "../../xous-ipc"}
xous-names = {path = "../xous-names"}# used by the IMEF for registering listeners xous-names = {path = "../xous-names"}# used by the IMEF for registering listeners
[dependencies.rkyv] num-derive = {version = "0.3", default-features = false}
default-features = false num-traits = {version = "0.2", default-features = false}
features = ["const_generics"] rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
version = "0.4.1"

View file

@ -1,18 +1,14 @@
#![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_std)]
use xous::{Message, ScalarMessage, CID}; use xous::{Message, CID, send_message};
use xous_ipc::String; use xous_ipc::{Buffer, String};
use graphics_server::Gid; use num_traits::ToPrimitive;
use rkyv::ser::Serializer;
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct Prediction { pub struct Prediction {
pub index: u32, pub index: u32,
pub valid: bool, pub valid: bool,
// to *return* a value in rkyv, we can't have a variable-length string, as the "pos" argument changes pub string: String::<1000>,
// this is OK for predictions, we only show the first few characters anyways.
pub string: [u8; 31], // 31 is the longest fixed-length array rkyv supports
pub len: u32,
} }
#[derive(Debug, Default, Copy, Clone)] #[derive(Debug, Default, Copy, Clone)]
@ -44,14 +40,13 @@ impl From<usize> for PredictionTriggers {
} }
} }
#[allow(dead_code)] #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub enum Opcode { pub enum Opcode {
/// update with the latest input candidate. Replaces the previous input. /// update with the latest input candidate. Replaces the previous input.
Input(String<4000>), Input, //(String<4000>),
/// feed back to the IME plugin as to what was picked, so predictions can be updated /// feed back to the IME plugin as to what was picked, so predictions can be updated
Picked(String<4000>), Picked, //(String<4000>),
/// Undo the last Picked value. To be used when a user hits backspace after picking a prediction /// 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 /// note that repeated calls to Unpick will have an implementation-defined behavior
@ -59,44 +54,17 @@ pub enum Opcode {
/// fetch the prediction at a given index, where the index is ordered from 0..N, where 0 is the most likely prediction /// 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 /// if there is no prediction available, just return an empty string
Prediction(Prediction), Prediction, //(Prediction),
/// return the prediction triggers used by this IME. These are characters that can indicate that a /// return the prediction triggers used by this IME. These are characters that can indicate that a
/// whole predictive unit has been entered. /// whole predictive unit has been entered.
GetPredictionTriggers, GetPredictionTriggers,
} }
impl core::convert::TryFrom<& Message> for Opcode { #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
type Error = &'static str; pub enum Return {
fn try_from(message: & Message) -> Result<Self, Self::Error> { Prediction(Prediction),
match message { Failure,
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"),
}
}
} }
pub trait PredictionApi { pub trait PredictionApi {
@ -117,7 +85,8 @@ impl PredictionApi for PredictionPlugin {
fn get_prediction_triggers(&self) -> Result<PredictionTriggers, xous::Error> { fn get_prediction_triggers(&self) -> Result<PredictionTriggers, xous::Error> {
match self.connection { match self.connection {
Some(cid) => { Some(cid) => {
let response = xous::send_message(cid, Opcode::GetPredictionTriggers.into())?; let response = send_message(cid,
Message::new_blocking_scalar(Opcode::GetPredictionTriggers.to_usize().unwrap(), 0, 0, 0, 0))?;
if let xous::Result::Scalar1(code) = response { if let xous::Result::Scalar1(code) = response {
Ok(code.into()) Ok(code.into())
} else { } else {
@ -131,7 +100,8 @@ impl PredictionApi for PredictionPlugin {
fn unpick(&self) -> Result<(), xous::Error> { fn unpick(&self) -> Result<(), xous::Error> {
match self.connection { match self.connection {
Some(cid) => { Some(cid) => {
xous::send_message(cid, Opcode::Unpick.into())?; send_message(cid,
Message::new_scalar(Opcode::Unpick.to_usize().unwrap(), 0, 0, 0, 0))?;
Ok(()) Ok(())
}, },
_ => Err(xous::Error::UseBeforeInit) _ => Err(xous::Error::UseBeforeInit)
@ -141,13 +111,8 @@ impl PredictionApi for PredictionPlugin {
fn set_input(&self, s: String<4000>) -> Result<(), xous::Error> { fn set_input(&self, s: String<4000>) -> Result<(), xous::Error> {
match self.connection { match self.connection {
Some(cid) => { Some(cid) => {
let rkyv_input = Opcode::Input(s); let buf = Buffer::into_buf(s).or(Err(xous::Error::InternalError))?;
let mut writer = rkyv::ser::serializers::BufferSerializer::new(xous::XousBuffer::new(4096)); buf.lend(cid, Opcode::Input.to_u32().unwrap()).expect("|API: set_input operation failure");
let pos = writer.serialize_value(&rkyv_input).expect("IME|API: couldn't archive input string");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("IME|API: set_input operation failure");
Ok(()) Ok(())
}, },
_ => Err(xous::Error::UseBeforeInit), _ => Err(xous::Error::UseBeforeInit),
@ -157,13 +122,8 @@ impl PredictionApi for PredictionPlugin {
fn feedback_picked(&self, s: String<4000>) -> Result<(), xous::Error> { fn feedback_picked(&self, s: String<4000>) -> Result<(), xous::Error> {
match self.connection { match self.connection {
Some(cid) => { Some(cid) => {
let rkyv_picked = Opcode::Picked(s); let buf = Buffer::into_buf(s).or(Err(xous::Error::InternalError))?;
let mut writer = rkyv::ser::serializers::BufferSerializer::new(xous::XousBuffer::new(4096)); buf.lend(cid, Opcode::Picked.to_u32().unwrap()).expect("|API: feedback_picked operation failure");
let pos = writer.serialize_value(&rkyv_picked).expect("IME|API: couldn't archive picked string");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("IME|API: feedback_picked operation failure");
Ok(()) Ok(())
}, },
_ => Err(xous::Error::UseBeforeInit), _ => Err(xous::Error::UseBeforeInit),
@ -171,42 +131,34 @@ impl PredictionApi for PredictionPlugin {
} }
fn get_prediction(&self, index: u32) -> Result<Option<String<4000>>, xous::Error> { fn get_prediction(&self, index: u32) -> Result<Option<String<4000>>, xous::Error> {
use rkyv::Deserialize;
let debug1 = false;
match self.connection { match self.connection {
Some(cid) => { Some(cid) => {
let prediction = Prediction { let prediction = Prediction {
index, index,
string: [0; 31], string: String::<1000>::new(),
len: 0,
valid: false, valid: false,
}; };
let pred_op = Opcode::Prediction(prediction); let mut buf = Buffer::into_buf(prediction).or(Err(xous::Error::InternalError))?;
let mut writer = rkyv::ser::serializers::BufferSerializer::new(xous::XousBuffer::new(4096)); buf.lend_mut(cid, Opcode::Prediction.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
let pos = writer.serialize_value(&pred_op).expect("IME|API: couldn't archive prediction request"); log::trace!("IME|API: returned from get_prediction");
let mut xous_buffer = writer.into_inner();
if debug1{log::info!("IME|API: lending Prediction with pos {}", pos);}
xous_buffer.lend_mut(cid, pos as u32).expect("IME|API: prediction fetch operation failure"); match buf.to_original().unwrap() {
Return::Prediction(pred) => {
if debug1{log::info!("IME|API: returned from get_prediction");} log::trace!("|API: got {:?}", pred);
let returned = unsafe { rkyv::archived_value::<Opcode>(xous_buffer.as_ref(), pos)}; if pred.valid {
if let rkyv::Archived::<Opcode>::Prediction(result) = returned { let mut ret = String::<4000>::new();
let pred_r: Prediction = result.deserialize(&mut xous_ipc::XousDeserializer {}).unwrap(); use core::fmt::Write as CoreWrite;
if debug1{log::info!("IME|API: got {:?}", pred_r);} write!(ret, "{}", pred.string).unwrap();
if pred_r.valid { Ok(Some(ret))
let mut ret = String::<4000>::new(); } else {
use core::fmt::Write as CoreWrite; Ok(None)
write!(ret, "{}", core::str::from_utf8(&pred_r.string[0..pred_r.len as usize]).unwrap()).unwrap(); }
Ok(Some(ret)) }
} else { _ => {
Ok(None) log::error!("API get_prediction returned an invalid result");
Err(xous::Error::InternalError)
} }
} else {
let r = returned.deserialize(&mut xous_ipc::XousDeserializer {}).unwrap();
log::error!("IME: API get_prediction returned an invalid result {:?}", r);
Err(xous::Error::InvalidString)
} }
}, },
_ => Err(xous::Error::UseBeforeInit), _ => Err(xous::Error::UseBeforeInit),
@ -222,24 +174,30 @@ impl PredictionApi for PredictionPlugin {
// in this crate so we can break circular dependencies // in this crate so we can break circular dependencies
// between the IMEF, GAM, and graphics server // between the IMEF, GAM, and graphics server
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum ImefOpcode { pub enum ImefOpcode {
/// informs me where my input canvas is /// informs me where my input canvas is
SetInputCanvas(Gid), SetInputCanvas, //(Gid),
/// informs me where my prediction canvas is /// informs me where my prediction canvas is
SetPredictionCanvas(Gid), SetPredictionCanvas, //(Gid),
/// set prediction server. Must be a String of the name of a server that is loaded in the system. /// set prediction server. Must be a String of the name of a server that is loaded in the system.
SetPredictionServer(String<64>), SetPredictionServer, //(String<64>),
/// register a listener for finalized inputs /// register a listener for finalized inputs
RegisterListener(String<64>), RegisterListener, //(String<64>),
/// this is the event opcode used by listeners // this is the event opcode used by listeners
//GotInputLine, //(String<4000>),
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) enum ImefReturn {
GotInputLine(String<4000>), GotInputLine(String<4000>),
Failure,
} }
/*
impl core::convert::TryFrom<& Message> for ImefOpcode { impl core::convert::TryFrom<& Message> for ImefOpcode {
type Error = &'static str; type Error = &'static str;
fn try_from(message: & Message) -> Result<Self, Self::Error> { fn try_from(message: & Message) -> Result<Self, Self::Error> {
@ -266,7 +224,7 @@ impl Into<Message> for ImefOpcode {
_ => panic!("IMEF api: Opcode type not handled by into()"), _ => panic!("IMEF api: Opcode type not handled by into()"),
} }
} }
} }*/
pub trait ImeFrontEndApi { pub trait ImeFrontEndApi {
fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error>; fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error>;
@ -283,7 +241,10 @@ impl ImeFrontEndApi for ImeFrontEnd {
fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> { fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
match self.connection { match self.connection {
Some(cid) => { Some(cid) => {
xous::send_message(cid, ImefOpcode::SetInputCanvas(g).into())?; 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 _
))?;
Ok(()) Ok(())
}, },
_ => Err(xous::Error::UseBeforeInit) _ => Err(xous::Error::UseBeforeInit)
@ -293,7 +254,10 @@ impl ImeFrontEndApi for ImeFrontEnd {
fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> { fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
match self.connection { match self.connection {
Some(cid) => { Some(cid) => {
xous::send_message(cid, ImefOpcode::SetPredictionCanvas(g).into())?; 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 _
))?;
Ok(()) Ok(())
}, },
_ => Err(xous::Error::UseBeforeInit) _ => Err(xous::Error::UseBeforeInit)
@ -305,14 +269,9 @@ impl ImeFrontEndApi for ImeFrontEnd {
Some(cid) => { Some(cid) => {
let mut server = String::<64>::new(); let mut server = String::<64>::new();
use core::fmt::Write; use core::fmt::Write;
write!(server, "{}", servername).expect("IMEF: couldn't write set_predictor server name"); write!(server, "{}", servername).expect("couldn't write set_predictor server name");
let ime_op = ImefOpcode::SetPredictionServer(server); let buf = Buffer::into_buf(server).or(Err(xous::Error::InternalError))?;
buf.lend(cid, ImefOpcode::SetPredictionServer.to_u32().unwrap()).map(|_|())
let mut writer = rkyv::ser::serializers::BufferSerializer::new(xous::XousBuffer::new(4096));
let pos = writer.serialize_value(&ime_op).expect("IMEF: couldn't archive SetPredictionServer");
writer.into_inner().lend(cid, pos as u32).expect("IMEF: SetPredictionServer request failure");
Ok(())
}, },
_ => Err(xous::Error::UseBeforeInit) _ => Err(xous::Error::UseBeforeInit)
} }
@ -323,14 +282,9 @@ impl ImeFrontEndApi for ImeFrontEnd {
Some(cid) => { Some(cid) => {
let mut server = String::<64>::new(); let mut server = String::<64>::new();
use core::fmt::Write; use core::fmt::Write;
write!(server, "{}", servername).expect("IMEF: couldn't write set_predictor server name"); write!(server, "{}", servername).expect("couldn't write set_predictor server name");
let ime_op = ImefOpcode::RegisterListener(server); let buf = Buffer::into_buf(server).or(Err(xous::Error::InternalError))?;
buf.lend(cid, ImefOpcode::RegisterListener.to_u32().unwrap()).map(|_|())
let mut writer = rkyv::ser::serializers::BufferSerializer::new(xous::XousBuffer::new(4096));
let pos = writer.serialize_value(&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) _ => Err(xous::Error::UseBeforeInit)
} }

View file

@ -15,10 +15,9 @@ xous = {path = "../../xous-rs"}
xous-ipc = {path = "../../xous-ipc"} xous-ipc = {path = "../../xous-ipc"}
xous-names = {path = "../xous-names"} xous-names = {path = "../xous-names"}
[dependencies.rkyv] num-derive = {version = "0.3", default-features = false}
default-features = false num-traits = {version = "0.2", default-features = false}
features = ["const_generics"] rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
version = "0.4.1"
[target.'cfg(not(any(windows,unix)))'.dependencies] [target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = {path = "../../utralib"} utralib = {path = "../../utralib"}

View file

@ -2,27 +2,22 @@
#![cfg_attr(target_os = "none", no_main)] #![cfg_attr(target_os = "none", no_main)]
use ime_plugin_api::*; use ime_plugin_api::*;
use core::convert::TryFrom;
use log::{error, info}; use log::{error, info};
use heapless::spsc::Queue; use heapless::spsc::Queue;
use heapless::consts::U4; use heapless::consts::U4;
use rkyv::Deserialize; use xous_ipc::{String, Buffer};
use rkyv::ser::Serializer; use num_traits::FromPrimitive;
use core::pin::Pin;
use rkyv::{archived_value, archived_value_mut};
use xous_ipc::String;
#[xous::xous_main] #[xous::xous_main]
fn xmain() -> ! { fn xmain() -> ! {
let debug1 = false;
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
info!("IME_SH: my PID is {}", xous::process::id()); info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
let ime_sh_sid = xns.register_name(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("IME_SH: can't register server"); let ime_sh_sid = xns.register_name(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("can't register server");
if debug1{info!("IME_SH: registered with NS -- {:?}", ime_sh_sid);} 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. let mut history: Queue<String<64>, U4> = Queue::new(); // this has 2^4 elements = 16??? or does it just have 4 elements.
let history_max = 4; let history_max = 4;
@ -46,125 +41,87 @@ fn xmain() -> ! {
whitespace: false, whitespace: false,
}; };
info!("IME_SH: ready to accept requests"); info!("ready to accept requests");
loop { loop {
let envelope = xous::receive_message(ime_sh_sid).unwrap(); let mut msg = xous::receive_message(ime_sh_sid).unwrap();
if debug1{info!("IME_SH: received message {:?}", envelope);} log::trace!("received message {:?}", msg);
if let xous::Message::Borrow(m) = &envelope.body { match FromPrimitive::from_usize(msg.body.id()) {
let buf = unsafe { xous::XousBuffer::from_memory_message(m) }; Some(Opcode::Input) => {
let bytes = Pin::new(buf.as_ref()); // shell does nothing with the input, it only keeps track of
let value = unsafe { // the picked results
archived_value::<Opcode>(&bytes, m.id as usize)
};
match &*value {
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: String<4000> = rkyv_s.deserialize(&mut xous_ipc::XousDeserializer {}).unwrap();
let mut local_s: String<64> = String::new();
use core::fmt::Write;
write!(local_s, "{:32}", s).expect("IME_SH: overflowed history variable");
if debug1{info!("IME_SH: storing history value | {}", s);}
if history.len() == history_max {
history.dequeue().expect("IME_SH: couldn't dequeue history");
}
history.enqueue(local_s).expect("IME_SH: couldn't store history");
if debug1{info!("IME_SH: history has length {}", history.len());}
},
_ => 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::<Opcode>(Pin::new(buf.as_mut()), m.id as usize)
};
match &*value {
rkyv::Archived::<Opcode>::Prediction(pred_r) => {
let mut prediction: Prediction = pred_r.deserialize(&mut xous_ipc::XousDeserializer {}).unwrap();
if debug1{info!("IME_SH: querying prediction index {}", prediction.index);}
if debug1{info!("IME_SH: {:?}", prediction);}
if history.len() > 0 && ((prediction.index as usize) < history.len()) {
let mut index = prediction.index;
if index >= history.len() as u32 {
index = history.len() as u32 - 1;
}
let mut i = 1;
for &s in history.iter() {
// iterator is from oldest to newest, so do some math to go from newest to oldest
// TIL: there is a .rev() feature in iterators. Next time maybe use that instead.
if (history.len() as u32 - i) == index {
// decompose the string into a character-by-character sequence
// and then stuff byte-by-byte, as fits, into the return array
prediction.len = 0;
for ch in s.as_str().unwrap().chars() {
match ch.len_utf8() {
1 => {
if prediction.len < prediction.string.len() as u32 {
prediction.string[prediction.len as usize] = ch as u8;
prediction.len += 1;
} else {
break;
}
},
_ => {
let mut data: [u8; 4] = [0; 4];
let subslice = ch.encode_utf8(&mut data);
if prediction.len + (subslice.len() as u32) < prediction.string.len() as u32 {
for c in subslice.bytes() {
prediction.string[prediction.len as usize] = c;
prediction.len += 1;
}
} else {
break;
}
},
}
}
prediction.valid = true;
break;
}
i = i + 1;
}
} else { // there is no history
prediction.valid = false;
if debug1{info!("IME_SH: no prediction found");}
}
if debug1{info!("IME_SH: returning index {} string {:?}", prediction.index, prediction.string);}
// pack our data back into the buffer to return
let ret_op = Opcode::Prediction(prediction);
let mut writer = rkyv::ser::serializers::BufferSerializer::new(buf);
let _pos = writer.serialize_value(&ret_op).expect("IME_SH: Prediction query couldn't re-archive return value");
if debug1{info!("IME_SH: archived with pos {}", _pos);}
},
_ => error!("IME_SH: Got invalid MutableBorrow")
};
} 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 { Some(Opcode::Picked) => {
info!("IME_SH: received unknown message type {:?}", envelope); let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
panic!("IME_SH: received unknown message type"); let s = buffer.as_flat::<String::<4000>, _>().unwrap();
// the API allows for large picked feedback, but this implementation only keeps the first 64 characters
let mut local_s: String<64> = String::new();
use core::fmt::Write;
write!(local_s, "{:32}", s.as_str()).expect("overflowed history variable");
log::trace!("storing history value | {}", s.as_str());
if history.len() == history_max {
history.dequeue().expect("couldn't dequeue history");
}
history.enqueue(local_s).expect("couldn't store history");
log::trace!("history has length {}", history.len());
}
Some(Opcode::Prediction) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut prediction: Prediction = buffer.to_original::<Prediction, _>().unwrap();
log::trace!("querying prediction index {}", prediction.index);
log::trace!("{:?}", prediction);
if history.len() > 0 && ((prediction.index as usize) < history.len()) {
let mut index = prediction.index;
if index >= history.len() as u32 {
index = history.len() as u32 - 1;
}
let mut i = 1;
for &s in history.iter() {
// iterator is from oldest to newest, so do some math to go from newest to oldest
// TIL: there is a .rev() feature in iterators. Next time maybe use that instead.
if (history.len() as u32 - i) == index {
// decompose the string into a character-by-character sequence
// and then stuff byte-by-byte, as fits, into the return array
prediction.string.clear();
for ch in s.as_str().unwrap().chars() {
if let Ok(_) = prediction.string.push(ch) {
// it's ok, carry on.
} else {
// we ran out of space, stop copying
break;
}
}
prediction.valid = true;
break;
}
i = i + 1;
}
} else { // there is no history
prediction.valid = false;
log::trace!("no prediction found");
}
log::trace!("returning index {} string {:?}", prediction.index, prediction.string);
// pack our data back into the buffer to return
buffer.replace(Return::Prediction(prediction)).expect("couldn't return Prediction");
}
Some(Opcode::Unpick) => {
if history.len() == 1 {
history.dequeue().expect("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("couldn't dequee in Unpick (>1)");
history.enqueue(s).expect("couldn't enqueue in Unpick");
}
// discard the last entry
history.dequeue().expect("couldn't dequeue in Unpick (>1 last)");
}
// in case of 0 length, do nothing
}
Some(Opcode::GetPredictionTriggers) => {
xous::return_scalar(msg.sender, mytriggers.into()).expect("couldn't return GetPredictionTriggers");
}
None => error!("unknown Opcode")
} }
} }
} }

View file

@ -40,7 +40,7 @@ fn try_main() -> Result<(), DynError> {
//"gam", //"gam",
//"shellchat", //"shellchat",
//"ime-frontend", //"ime-frontend",
//"ime-plugin-shell", "ime-plugin-shell",
"graphics-server", "graphics-server",
"ticktimer-server", "ticktimer-server",
"log-server", "log-server",