mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
refactor API into a separate crate with traits
This commit is contained in:
parent
92ca22c38c
commit
f565fdaf6f
4 changed files with 113 additions and 78 deletions
|
|
@ -12,7 +12,7 @@ use graphics_server::Gid;
|
|||
use heapless::Vec;
|
||||
use heapless::consts::U32;
|
||||
use core::pin::Pin;
|
||||
use ime_plugin_api::PredictionTriggers;
|
||||
use ime_plugin_api::{PredictionTriggers, PredictionApi};
|
||||
|
||||
use rkyv::Unarchive;
|
||||
use rkyv::archived_value;
|
||||
|
|
@ -38,7 +38,9 @@ struct InputTracker {
|
|||
}
|
||||
|
||||
impl InputTracker {
|
||||
pub fn new(gam_conn: xous::CID, pred_conn: xous::CID)-> InputTracker {
|
||||
pub fn new(gam_conn: xous::CID, pred_conn: xous::CID, predictor: impl PredictionApi)-> InputTracker {
|
||||
// a little dangerous: bypassing any explicit API call implementation for this one
|
||||
/*
|
||||
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 {
|
||||
|
|
@ -54,6 +56,19 @@ impl InputTracker {
|
|||
}
|
||||
} else {
|
||||
panic!("IMEF: InputTracker::new() failed to get prediction triggers")
|
||||
}*/
|
||||
|
||||
let pred_triggers = predictor.get_prediction_triggers(pred_conn)
|
||||
.expect("IMEF: InputTracker failed to get predictions from default plugin");
|
||||
InputTracker {
|
||||
gam_conn,
|
||||
input_canvas: None,
|
||||
pred_conn,
|
||||
pred_canvas: None,
|
||||
pred_triggers,
|
||||
line: xous::String::<4096>::new(),
|
||||
charwidths: [0; 4096],
|
||||
insertion: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -87,10 +102,12 @@ 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 default_predictor = ime_plugin_api::StandardPredictionPlugin {};
|
||||
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"),
|
||||
default_predictor
|
||||
);
|
||||
|
||||
// The first message should be my Gid from the GAM.
|
||||
|
|
|
|||
11
services/ime-plugin-api/Cargo.toml
Normal file
11
services/ime-plugin-api/Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "ime-plugin-api"
|
||||
version = "0.1.0"
|
||||
authors = ["bunnie <bunnie@kosagi.com>"]
|
||||
edition = "2018"
|
||||
description = "IME Plugin Common APIs"
|
||||
|
||||
[dependencies]
|
||||
xous = { path = "../../xous-rs" }
|
||||
rkyv = { version = "0.3.1", default_features = false }
|
||||
|
||||
|
|
@ -1,15 +1,19 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
|
||||
use xous::{Message, ScalarMessage};
|
||||
use xous::{Message, ScalarMessage, String, CID};
|
||||
#[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
|
||||
////////////////////////////////////////////////
|
||||
pub trait PredictionApi {
|
||||
fn get_prediction_triggers(self, cid: CID) -> Result<PredictionTriggers, xous::Error>;
|
||||
fn unpick(self, cid: CID) -> Result<(), xous::Error>;
|
||||
fn set_input(self, cid: CID, s: String<4096>) -> Result<(), xous::Error>;
|
||||
fn feedback_picked(self, cid: CID, s: String<4096>) -> Result<(), xous::Error>;
|
||||
fn get_prediction(self, cid: CID, index: u32) -> Result<xous::String<4096>, xous::Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Copy, Clone)]
|
||||
pub struct PredictionTriggers {
|
||||
|
|
@ -93,3 +97,77 @@ impl Into<Message> for Opcode {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// most implementation should not modify this, but it's made a trait in case for some weird reason it needs changes...
|
||||
#[derive(Debug, Default, Copy, Clone)]
|
||||
pub struct StandardPredictionPlugin {}
|
||||
|
||||
impl PredictionApi for StandardPredictionPlugin {
|
||||
fn get_prediction_triggers(self, 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")
|
||||
}
|
||||
}
|
||||
|
||||
fn unpick(self, cid: CID) -> Result<(), xous::Error> {
|
||||
xous::send_message(cid, Opcode::Unpick.into())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_input(self, cid: CID, s: String<4096>) -> Result<(), xous::Error> {
|
||||
use rkyv::Write;
|
||||
|
||||
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();
|
||||
|
||||
xous_buffer.lend(cid, pos as u32).expect("IMES|API: set_input operation failure");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn feedback_picked(self, cid: CID, s: String<4096>) -> Result<(), xous::Error> {
|
||||
use rkyv::Write;
|
||||
|
||||
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();
|
||||
|
||||
xous_buffer.lend(cid, pos as u32).expect("IMES|API: feedback_picked operation failure");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_prediction(self, cid: CID, index: u32) -> Result<xous::String<4096>, xous::Error> {
|
||||
use rkyv::Write;
|
||||
use rkyv::Unarchive;
|
||||
|
||||
let prediction = Prediction {
|
||||
index,
|
||||
string: xous::String::<4096>::new(),
|
||||
};
|
||||
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");
|
||||
let mut xous_buffer = writer.into_inner();
|
||||
|
||||
xous_buffer.lend_mut(cid, pos as u32).expect("IMES|API: prediction fetch operation failure");
|
||||
|
||||
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)
|
||||
} else {
|
||||
let r = returned.unarchive();
|
||||
Err(xous::Error::InvalidString)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,75 +1,4 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
|
||||
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 = 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();
|
||||
|
||||
xous_buffer.lend(cid, pos as u32).expect("IMES|API: set_input operation failure");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn feedback_picked(cid: CID, s: String<4096>) -> Result<(), xous::Error> {
|
||||
use rkyv::Write;
|
||||
|
||||
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();
|
||||
|
||||
xous_buffer.lend(cid, pos as u32).expect("IMES|API: feedback_picked operation failure");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_prediction(cid: CID, index: u32) -> Result<xous::String<4096>, xous::Error> {
|
||||
use rkyv::Write;
|
||||
use rkyv::Unarchive;
|
||||
|
||||
let prediction = Prediction {
|
||||
index,
|
||||
string: xous::String::<4096>::new(),
|
||||
};
|
||||
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");
|
||||
let mut xous_buffer = writer.into_inner();
|
||||
|
||||
xous_buffer.lend_mut(cid, pos as u32).expect("IMES|API: prediction fetch operation failure");
|
||||
|
||||
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)
|
||||
} else {
|
||||
let r = returned.unarchive();
|
||||
log::info!("get_prediciton saw an unhandled return of {:?}", r);
|
||||
Err(xous::Error::InvalidString)
|
||||
}
|
||||
}
|
||||
pub use ime_plugin_api::*;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue