mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
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:
parent
2b768e9b86
commit
92ca22c38c
12 changed files with 279 additions and 81 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
|
@ -532,6 +532,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"graphics-server",
|
"graphics-server",
|
||||||
"heapless",
|
"heapless",
|
||||||
|
"ime-plugin-api",
|
||||||
"keyboard",
|
"keyboard",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
|
|
@ -542,11 +543,20 @@ dependencies = [
|
||||||
"xous-names",
|
"xous-names",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ime-plugin-api"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rkyv",
|
||||||
|
"xous",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ime-plugin-shell"
|
name = "ime-plugin-shell"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heapless",
|
"heapless",
|
||||||
|
"ime-plugin-api",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv",
|
"rkyv",
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ members = [
|
||||||
"xtask",
|
"xtask",
|
||||||
"imports/blitstr-ref",
|
"imports/blitstr-ref",
|
||||||
"imports/com_rs-ref",
|
"imports/com_rs-ref",
|
||||||
|
"services/ime-plugin-api",
|
||||||
]
|
]
|
||||||
default-members = [
|
default-members = [
|
||||||
"services/shell",
|
"services/shell",
|
||||||
|
|
@ -41,6 +42,7 @@ default-members = [
|
||||||
"services/gam",
|
"services/gam",
|
||||||
"imports/blitstr-ref",
|
"imports/blitstr-ref",
|
||||||
"imports/com_rs-ref",
|
"imports/com_rs-ref",
|
||||||
|
"services/ime-plugin-api",
|
||||||
"services/benchmark",
|
"services/benchmark",
|
||||||
"services/benchmark-target",
|
"services/benchmark-target",
|
||||||
"services/ime-frontend",
|
"services/ime-frontend",
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,8 @@ fn xmain() -> ! {
|
||||||
// connect to the IME front end, and set its canvas
|
// connect to the IME front end, and set its canvas
|
||||||
info!("GAM: acquiring connection to IMEF...");
|
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");
|
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();
|
let mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap();
|
||||||
info!("GAM: entering main loop");
|
info!("GAM: entering main loop");
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ graphics-server = {path = "../graphics-server"}
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
rkyv = { version = "0.3.1", default_features = false }
|
rkyv = { version = "0.3.1", default_features = false }
|
||||||
keyboard = {path="../keyboard"}
|
keyboard = {path="../keyboard"}
|
||||||
|
ime-plugin-api = {path="../ime-plugin-api"}
|
||||||
|
|
||||||
|
|
||||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,14 @@ use graphics_server::Gid;
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
|
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
|
||||||
pub enum Opcode {
|
pub enum Opcode {
|
||||||
/// informs me where my canvas is
|
/// informs me where my input canvas is
|
||||||
SetCanvas(Gid),
|
SetInputCanvas(Gid),
|
||||||
|
|
||||||
/// set prediction. Must be a String of the name of a server that is loaded in the system.
|
/// informs me where my prediction canvas is
|
||||||
SetPrediction(xous::String<256>),
|
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 {
|
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> {
|
fn try_from(message: & Message) -> Result<Self, Self::Error> {
|
||||||
match message {
|
match message {
|
||||||
Message::Scalar(m) => match m.id {
|
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: unknown Scalar ID"),
|
||||||
},
|
},
|
||||||
_ => Err("IMEF api: unhandled message type"),
|
_ => Err("IMEF api: unhandled message type"),
|
||||||
|
|
@ -28,7 +32,10 @@ impl core::convert::TryFrom<& Message> for Opcode {
|
||||||
impl Into<Message> for Opcode {
|
impl Into<Message> for Opcode {
|
||||||
fn into(self) -> Message {
|
fn into(self) -> Message {
|
||||||
match self {
|
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 _
|
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()"),
|
_ => panic!("IMEF api: Opcode type not handled by into()"),
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,12 @@
|
||||||
pub mod api;
|
pub mod api;
|
||||||
use xous::{CID, send_message};
|
use xous::{CID, send_message};
|
||||||
|
|
||||||
pub fn set_canvas(cid: CID, g: graphics_server::Gid) -> Result<(), xous::Error> {
|
pub fn set_input_canvas(cid: CID, g: graphics_server::Gid) -> Result<(), xous::Error> {
|
||||||
send_message(cid, api::Opcode::SetCanvas(g).into())?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,27 +12,68 @@ use graphics_server::Gid;
|
||||||
use heapless::Vec;
|
use heapless::Vec;
|
||||||
use heapless::consts::U32;
|
use heapless::consts::U32;
|
||||||
use core::pin::Pin;
|
use core::pin::Pin;
|
||||||
|
use ime_plugin_api::PredictionTriggers;
|
||||||
|
|
||||||
use rkyv::Unarchive;
|
use rkyv::Unarchive;
|
||||||
use rkyv::archived_value;
|
use rkyv::archived_value;
|
||||||
|
|
||||||
/*
|
struct InputTracker {
|
||||||
what else do we need:
|
/// connection for handling graphical update requests
|
||||||
- current string that is being built up
|
pub gam_conn: xous::CID,
|
||||||
- cursor position in string, so we can do insertion/deletion
|
/// input area canvas, as given by the GAM
|
||||||
*/
|
pub input_canvas: Option<Gid>,
|
||||||
fn draw_canvas(gam_conn: xous::CID, canvas: Gid, pred_conn: xous::CID, newkeys: [char; 4]) {
|
/// 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
|
/// track the progress of our input line
|
||||||
/*
|
line: xous::String::<4096>,
|
||||||
for &k in newkeys.iter() {
|
/// we need to track character widths so we can draw a cursor
|
||||||
if k != '\u{0000}' {
|
charwidths: [u8; 4096],
|
||||||
key_queue.push(k).unwrap();
|
/// what position the insertion cursor is at in the string. 0 is inserting elements into the front of the string
|
||||||
if debug1{info!("IMEF: got key '{}'", k);}
|
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]
|
#[xous::xous_main]
|
||||||
fn xmain() -> ! {
|
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");
|
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");
|
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");
|
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
|
// 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)
|
xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("IMEF: can't connect to shell prediction engine"),
|
||||||
.expect("IMEF: can't connect to shell prediction engine");
|
);
|
||||||
|
|
||||||
// The first message should be my Gid from the GAM.
|
// 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 Gids");
|
||||||
info!("IMEF: waiting for my canvas Gid");
|
|
||||||
loop {
|
loop {
|
||||||
let envelope = xous::receive_message(imef_sid).unwrap();
|
let envelope = xous::receive_message(imef_sid).unwrap();
|
||||||
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
|
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
|
||||||
match opcode {
|
match opcode {
|
||||||
Opcode::SetCanvas(g) => {
|
Opcode::SetInputCanvas(g) => {
|
||||||
canvas = g;
|
if debug1{info!("IMEF: got input canvas {:?}", g);}
|
||||||
break;
|
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)
|
_ => info!("IMEF: expected canvas Gid, but got {:?}", opcode)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
info!("IMEF: expected canvas Gid, but got other message first {:?}", envelope);
|
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();
|
let mut key_queue: Vec<char, U32> = Vec::new();
|
||||||
|
|
@ -77,10 +124,15 @@ fn xmain() -> ! {
|
||||||
if debug1{info!("IMEF: got message {:?}", envelope);}
|
if debug1{info!("IMEF: got message {:?}", envelope);}
|
||||||
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
|
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
|
||||||
match opcode {
|
match opcode {
|
||||||
Opcode::SetCanvas(g) => {
|
Opcode::SetInputCanvas(g) => {
|
||||||
// there are valid reasons for this to happen, but it should be rare.
|
// there are valid reasons for this to happen, but it should be rare.
|
||||||
info!("IMEF: warning: canvas Gid has been reset");
|
info!("IMEF: warning: input canvas Gid has been reset");
|
||||||
canvas = g;
|
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)
|
_ => info!("IMEF: unhandled opcode {:?}", opcode)
|
||||||
}
|
}
|
||||||
|
|
@ -91,11 +143,11 @@ fn xmain() -> ! {
|
||||||
archived_value::<api::Opcode>(&bytes, m.id as usize)
|
archived_value::<api::Opcode>(&bytes, m.id as usize)
|
||||||
};
|
};
|
||||||
match &*value {
|
match &*value {
|
||||||
rkyv::Archived::<api::Opcode>::SetPrediction(rkyv_s) => {
|
rkyv::Archived::<api::Opcode>::SetPredictionServer(rkyv_s) => {
|
||||||
let s: xous::String<4096> = rkyv_s.unarchive();
|
let s: xous::String<256> = rkyv_s.unarchive();
|
||||||
match xous_names::request_connection(s.as_str().expect("IMEF: SetPrediction received malformed server name")) {
|
match xous_names::request_connection(s.as_str().expect("IMEF: SetPrediction received malformed server name")) {
|
||||||
Ok(pc) => prediction_conn = pc,
|
Ok(pc) => tracker.pred_conn = pc,
|
||||||
_ => error!("IMEF: can't find predictive engine {}, retaining existign one.", s.as_str().expect("IMEF: SetPrediction received malformed server name")),
|
_ => 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")
|
_ => 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) {
|
} else if let Ok(opcode) = keyboard::api::Opcode::try_from(&envelope.body) {
|
||||||
match opcode {
|
match opcode {
|
||||||
keyboard::api::Opcode::KeyboardEvent(keys) => {
|
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"),
|
_ => error!("IMEF: received KBD event opcode that wasn't expected"),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
95
services/ime-plugin-api/src/lib.rs
Normal file
95
services/ime-plugin-api/src/lib.rs
Normal 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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,7 +13,7 @@ ticktimer-server = { path = "../ticktimer-server" }
|
||||||
xous-names = { path = "../xous-names" }
|
xous-names = { path = "../xous-names" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
rkyv = { version = "0.3.1", default_features = false }
|
rkyv = { version = "0.3.1", default_features = false }
|
||||||
|
ime-plugin-api = {path="../ime-plugin-api"}
|
||||||
|
|
||||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||||
utralib = { path = "../../utralib"}
|
utralib = { path = "../../utralib"}
|
||||||
|
|
|
||||||
|
|
@ -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),
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +1,27 @@
|
||||||
#![cfg_attr(target_os = "none", no_std)]
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
|
|
||||||
pub mod api;
|
use ime_plugin_api::*;
|
||||||
|
|
||||||
use xous::{CID, String};
|
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> {
|
pub fn set_input(cid: CID, s: String<4096>) -> Result<(), xous::Error> {
|
||||||
use rkyv::Write;
|
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 mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
|
||||||
let pos = writer.archive(&rkyv_input).expect("IMES|API: couldn't archive input string");
|
let pos = writer.archive(&rkyv_input).expect("IMES|API: couldn't archive input string");
|
||||||
let xous_buffer = writer.into_inner();
|
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> {
|
pub fn feedback_picked(cid: CID, s: String<4096>) -> Result<(), xous::Error> {
|
||||||
use rkyv::Write;
|
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 mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
|
||||||
let pos = writer.archive(&rkyv_picked).expect("IMES|API: couldn't archive picked string");
|
let pos = writer.archive(&rkyv_picked).expect("IMES|API: couldn't archive picked string");
|
||||||
let xous_buffer = writer.into_inner();
|
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::Write;
|
||||||
use rkyv::Unarchive;
|
use rkyv::Unarchive;
|
||||||
|
|
||||||
let prediction = api::Prediction {
|
let prediction = Prediction {
|
||||||
index,
|
index,
|
||||||
string: xous::String::<4096>::new(),
|
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 mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
|
||||||
let pos = writer.archive(&pred_op).expect("IMES|API: couldn't archive prediction request");
|
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");
|
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)};
|
let returned = unsafe { rkyv::archived_value::<Opcode>(xous_buffer.as_ref(), pos)};
|
||||||
if let rkyv::Archived::<api::Opcode>::Prediction(result) = returned {
|
if let rkyv::Archived::<Opcode>::Prediction(result) = returned {
|
||||||
let pred_r: api::Prediction = result.unarchive();
|
let pred_r: Prediction = result.unarchive();
|
||||||
|
|
||||||
let retstring: xous::String<4096> = pred_r.string.clone();
|
let retstring: xous::String<4096> = pred_r.string.clone();
|
||||||
Ok(retstring)
|
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);
|
log::info!("get_prediciton saw an unhandled return of {:?}", r);
|
||||||
Err(xous::Error::InvalidString)
|
Err(xous::Error::InvalidString)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
#![cfg_attr(target_os = "none", no_std)]
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
#![cfg_attr(target_os = "none", no_main)]
|
#![cfg_attr(target_os = "none", no_main)]
|
||||||
|
|
||||||
mod api;
|
use ime_plugin_api::*;
|
||||||
use api::*;
|
use core::convert::TryFrom;
|
||||||
|
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use heapless::spsc::Queue;
|
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 mut history: Queue<xous::String<4096>, U3> = Queue::new(); // this has 2^3 elements = 8
|
||||||
let history_max = 8;
|
let history_max = 8;
|
||||||
|
|
||||||
|
let mytriggers = PredictionTriggers {
|
||||||
|
trigger_newline: true,
|
||||||
|
trigger_punctuation: false,
|
||||||
|
trigger_whitespace: false,
|
||||||
|
};
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let envelope = xous::receive_message(ime_sh_sid).unwrap();
|
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 {
|
if let xous::Message::Borrow(m) = &envelope.body {
|
||||||
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
||||||
let bytes = Pin::new(buf.as_ref());
|
let bytes = Pin::new(buf.as_ref());
|
||||||
let value = unsafe {
|
let value = unsafe {
|
||||||
archived_value::<api::Opcode>(&bytes, m.id as usize)
|
archived_value::<Opcode>(&bytes, m.id as usize)
|
||||||
};
|
};
|
||||||
match &*value {
|
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();
|
let s: xous::String<4096> = rkyv_s.unarchive();
|
||||||
if history.len() == history_max {
|
if history.len() == history_max {
|
||||||
history.dequeue().expect("IME_SH: couldn't dequeue history");
|
history.dequeue().expect("IME_SH: couldn't dequeue history");
|
||||||
}
|
}
|
||||||
history.enqueue(s).expect("IME_SH: couldn't store 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")
|
_ => error!("IME_SH: unknown Borrow message")
|
||||||
};
|
};
|
||||||
} else if let xous::Message::MutableBorrow(m) = &envelope.body {
|
} else if let xous::Message::MutableBorrow(m) = &envelope.body {
|
||||||
let mut buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
let mut buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
||||||
let value = unsafe {
|
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 {
|
match &*value {
|
||||||
rkyv::Archived::<api::Opcode>::Prediction(pred_r) => {
|
rkyv::Archived::<Opcode>::Prediction(pred_r) => {
|
||||||
let mut prediction: api::Prediction = pred_r.unarchive();
|
let mut prediction: Prediction = pred_r.unarchive();
|
||||||
if history.len() > 0 {
|
if history.len() > 0 {
|
||||||
let mut index = prediction.index;
|
let mut index = prediction.index;
|
||||||
if index >= history.len() as u32 {
|
if index >= history.len() as u32 {
|
||||||
|
|
@ -81,6 +88,27 @@ fn xmain() -> ! {
|
||||||
},
|
},
|
||||||
_ => error!("IME_SH: unknown MutableBorrow message"),
|
_ => 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 {
|
} else {
|
||||||
info!("IME_SH: received unknown message type {:?}", envelope);
|
info!("IME_SH: received unknown message type {:?}", envelope);
|
||||||
panic!("IME_SH: received unknown message type");
|
panic!("IME_SH: received unknown message type");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue