diff --git a/services/ime-plugin-api/src/lib.rs b/services/ime-plugin-api/src/lib.rs index 666902c79..93b4f91cb 100644 --- a/services/ime-plugin-api/src/lib.rs +++ b/services/ime-plugin-api/src/lib.rs @@ -6,7 +6,11 @@ use graphics_server::Gid; #[derive(Debug, rkyv::Archive, rkyv::Unarchive)] pub struct Prediction { pub index: u32, - pub string: xous::String<4096>, + pub valid: bool, + // to *return* a value in rkyv, we can't have a variable-length string, as the "pos" argument changes + // 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)] @@ -42,10 +46,10 @@ impl From for PredictionTriggers { #[derive(Debug, rkyv::Archive, rkyv::Unarchive)] pub enum Opcode { /// update with the latest input candidate. Replaces the previous input. - Input(xous::String<4096>), + Input(xous::String<4000>), /// feed back to the IME plugin as to what was picked, so predictions can be updated - Picked(xous::String<4096>), + Picked(xous::String<4000>), /// 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 @@ -96,9 +100,9 @@ impl Into for Opcode { pub trait PredictionApi { fn get_prediction_triggers(&self) -> Result; fn unpick(&self) -> Result<(), xous::Error>; - fn set_input(&self, s: String<4096>) -> Result<(), xous::Error>; - fn feedback_picked(&self, s: String<4096>) -> Result<(), xous::Error>; - fn get_prediction(&self, index: u32) -> Result, xous::Error>; + fn set_input(&self, s: String<4000>) -> Result<(), xous::Error>; + fn feedback_picked(&self, s: String<4000>) -> Result<(), xous::Error>; + fn get_prediction(&self, index: u32) -> Result>, xous::Error>; } // provide a convenience version of the API for generic/standard calls @@ -132,7 +136,7 @@ impl PredictionApi for PredictionPlugin { } } - fn set_input(&self, s: String<4096>) -> Result<(), xous::Error> { + fn set_input(&self, s: String<4000>) -> Result<(), xous::Error> { use rkyv::Write; match self.connection { Some(cid) => { @@ -148,7 +152,7 @@ impl PredictionApi for PredictionPlugin { } } - fn feedback_picked(&self, s: String<4096>) -> Result<(), xous::Error> { + fn feedback_picked(&self, s: String<4000>) -> Result<(), xous::Error> { use rkyv::Write; match self.connection { Some(cid) => { @@ -164,30 +168,42 @@ impl PredictionApi for PredictionPlugin { } } - fn get_prediction(&self, index: u32) -> Result, xous::Error> { + fn get_prediction(&self, index: u32) -> Result>, xous::Error> { use rkyv::Write; use rkyv::Unarchive; + let debug1 = true; match self.connection { Some(cid) => { let prediction = Prediction { index, - string: xous::String::<4096>::new(), + string: [0; 31], + len: 0, + valid: false, }; let pred_op = Opcode::Prediction(prediction); let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); let pos = writer.archive(&pred_op).expect("IME|API: couldn't archive prediction request"); 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"); + if debug1{log::info!("IME|API: returned from get_prediction");} let returned = unsafe { rkyv::archived_value::(xous_buffer.as_ref(), pos)}; if let rkyv::Archived::::Prediction(result) = returned { let pred_r: Prediction = result.unarchive(); - let retstring: xous::String<4096> = pred_r.string.clone(); - Ok(retstring) + if debug1{log::info!("IME|API: got {:?}", pred_r);} + if pred_r.valid { + let mut ret = xous::String::<4000>::new(); + use core::fmt::Write as CoreWrite; + write!(ret, "{}", core::str::from_utf8(&pred_r.string[0..pred_r.len as usize]).unwrap()).unwrap(); + Ok(Some(ret)) + } else { + Ok(None) + } } else { let r = returned.unarchive(); - log::error!("IME:API get_prediction returned an invalid result {:?}", r); + log::error!("IME: API get_prediction returned an invalid result {:?}", r); Err(xous::Error::InvalidString) } }, diff --git a/services/ime-plugin-shell/src/main.rs b/services/ime-plugin-shell/src/main.rs index ef36ec80a..16b67e8d4 100644 --- a/services/ime-plugin-shell/src/main.rs +++ b/services/ime-plugin-shell/src/main.rs @@ -6,7 +6,7 @@ use core::convert::TryFrom; use log::{error, info}; use heapless::spsc::Queue; -use heapless::consts::U3; +use heapless::consts::U8; use rkyv::Unarchive; use core::pin::Pin; @@ -21,8 +21,8 @@ fn xmain() -> ! { let ime_sh_sid = xous_names::register_name(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("IME_SH: can't register server"); if debug1{info!("IME_SH: registered with NS -- {:?}", ime_sh_sid);} - let mut history: Queue, U3> = Queue::new(); // this has 2^3 elements = 8 - let history_max = 8; + let mut history: Queue, U4> = Queue::new(); // this has 2^4 elements = 16??? or does it just have 4 elements. + let history_max = 4; let mytriggers = PredictionTriggers { newline: true, @@ -46,11 +46,16 @@ fn xmain() -> ! { // the picked results }, rkyv::Archived::::Picked(rkyv_s) => { - let s: xous::String<4096> = rkyv_s.unarchive(); + let s: xous::String<4000> = rkyv_s.unarchive(); + let mut local_s: xous::String<64> = xous::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(s).expect("IME_SH: couldn't store 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") }; @@ -62,29 +67,64 @@ fn xmain() -> ! { match &*value { rkyv::Archived::::Prediction(pred_r) => { let mut prediction: Prediction = pred_r.unarchive(); - if history.len() > 0 { + 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 = history.len() as u32; - let mut retstr = xous::String::new(); for &s in history.iter() { // iterator is from oldest to newest, so do some math to go from newest to oldest if (history.len() as u32 - i) == index { - retstr = s; + // 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; + i = i - 1; } - prediction.string = retstr; } else { // there is no history - // return the empty string - prediction.string = xous::String::new(); + 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 + use rkyv::Write; + let ret_op = Opcode::Prediction(prediction); + let mut writer = rkyv::ArchiveBuffer::new(buf); + let _pos = writer.archive(&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: unknown MutableBorrow message"), - } + _ => error!("IME_SH: Got invalid MutableBorrow") + }; } else if let Ok(opcode) = Opcode::try_from(&envelope.body) { match opcode { Opcode::Unpick => {