2021-03-03 06:53:33 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
2021-03-04 11:02:56 +00:00
|
|
|
|
|
|
|
|
use ime_plugin_api::ImefOpcode;
|
2021-03-03 06:53:33 +00:00
|
|
|
|
|
|
|
|
use core::convert::TryFrom;
|
2021-03-04 11:02:56 +00:00
|
|
|
use core::fmt::Write;
|
2021-03-03 06:53:33 +00:00
|
|
|
|
|
|
|
|
use log::{error, info};
|
|
|
|
|
|
2021-03-05 17:10:18 +00:00
|
|
|
use graphics_server::{Gid, Line, PixelColor, Point, Rectangle, TextBounds, TextView, DrawStyle};
|
2021-03-04 11:02:56 +00:00
|
|
|
use blitstr_ref as blitstr;
|
|
|
|
|
use blitstr::Cursor;
|
2021-03-03 06:53:33 +00:00
|
|
|
use heapless::Vec;
|
|
|
|
|
use heapless::consts::U32;
|
2021-03-03 15:27:30 +00:00
|
|
|
use core::pin::Pin;
|
2021-03-04 08:02:42 +00:00
|
|
|
use ime_plugin_api::{PredictionTriggers, PredictionPlugin, PredictionApi};
|
2021-03-03 15:27:30 +00:00
|
|
|
|
|
|
|
|
use rkyv::Unarchive;
|
|
|
|
|
use rkyv::archived_value;
|
2021-03-03 06:53:33 +00:00
|
|
|
|
2021-03-03 19:14:26 +00:00
|
|
|
struct InputTracker {
|
|
|
|
|
/// connection for handling graphical update requests
|
|
|
|
|
pub gam_conn: xous::CID,
|
2021-03-04 08:02:42 +00:00
|
|
|
|
|
|
|
|
|
2021-03-03 19:14:26 +00:00
|
|
|
/// input area canvas, as given by the GAM
|
2021-03-04 08:02:42 +00:00
|
|
|
input_canvas: Option<Gid>,
|
2021-03-03 19:14:26 +00:00
|
|
|
/// prediction display area, as given by the GAM
|
2021-03-04 08:02:42 +00:00
|
|
|
pred_canvas: Option<Gid>,
|
|
|
|
|
|
|
|
|
|
/// our current prediction engine
|
|
|
|
|
predictor: Option<PredictionPlugin>,
|
|
|
|
|
/// cached copy of the predictor's triggers for predictions. Only valid if predictor is not None
|
|
|
|
|
pred_triggers: Option<PredictionTriggers>,
|
2021-03-03 19:14:26 +00:00
|
|
|
|
|
|
|
|
/// track the progress of our input line
|
|
|
|
|
line: xous::String::<4096>,
|
|
|
|
|
/// we need to track character widths so we can draw a cursor
|
|
|
|
|
charwidths: [u8; 4096],
|
|
|
|
|
/// what position the insertion cursor is at in the string. 0 is inserting elements into the front of the string
|
2021-03-04 11:02:56 +00:00
|
|
|
insertion: Cursor,
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-04 08:02:42 +00:00
|
|
|
impl InputTracker {
|
|
|
|
|
pub fn new(gam_conn: xous::CID)-> InputTracker {
|
2021-03-03 20:33:20 +00:00
|
|
|
InputTracker {
|
|
|
|
|
gam_conn,
|
|
|
|
|
input_canvas: None,
|
|
|
|
|
pred_canvas: None,
|
2021-03-04 08:02:42 +00:00
|
|
|
predictor: None,
|
|
|
|
|
pred_triggers: None,
|
2021-03-03 20:33:20 +00:00
|
|
|
line: xous::String::<4096>::new(),
|
|
|
|
|
charwidths: [0; 4096],
|
2021-03-04 11:02:56 +00:00
|
|
|
insertion: Cursor::new(0, 0, 0), // canvases always have (0,0) as the top left
|
2021-03-03 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-04 08:02:42 +00:00
|
|
|
pub fn set_predictor(&mut self, predictor: PredictionPlugin) {
|
|
|
|
|
self.predictor = Some(predictor);
|
|
|
|
|
self.pred_triggers = Some(predictor.get_prediction_triggers()
|
|
|
|
|
.expect("IMEF: InputTracker failed to get prediction triggers from plugin"));
|
|
|
|
|
}
|
|
|
|
|
pub fn set_input_canvas(&mut self, input: Gid) {
|
|
|
|
|
self.input_canvas = Some(input);
|
|
|
|
|
}
|
|
|
|
|
pub fn set_pred_canvas(&mut self, pred: Gid) {
|
|
|
|
|
self.pred_canvas = Some(pred);
|
|
|
|
|
}
|
|
|
|
|
pub fn is_init(&self) -> bool {
|
2021-03-05 17:10:18 +00:00
|
|
|
self.input_canvas.is_some() && self.pred_canvas.is_some() && self.predictor.is_some()
|
2021-03-04 08:02:42 +00:00
|
|
|
}
|
2021-03-03 19:14:26 +00:00
|
|
|
|
2021-03-05 18:45:47 +00:00
|
|
|
pub fn clear_area(&mut self) -> Result<(), xous::Error> {
|
|
|
|
|
if let Some(pc) = self.pred_canvas {
|
|
|
|
|
let pc_bounds: Point = gam::get_canvas_bounds(self.gam_conn, pc).expect("IMEF: Couldn't get prediction canvas bounds");
|
|
|
|
|
gam::draw_rectangle(self.gam_conn, pc,
|
|
|
|
|
Rectangle::new_with_style(Point::new(0, 0), pc_bounds,
|
|
|
|
|
DrawStyle {
|
|
|
|
|
fill_color: Some(PixelColor::Light),
|
|
|
|
|
stroke_color: None,
|
|
|
|
|
stroke_width: 0
|
|
|
|
|
}
|
|
|
|
|
)).expect("IMEF: can't clear prediction area");
|
|
|
|
|
// add the border line on top
|
|
|
|
|
gam::draw_line(self.gam_conn, pc,
|
|
|
|
|
Line::new_with_style(
|
|
|
|
|
Point::new(0,0),
|
|
|
|
|
Point::new(pc_bounds.x, 0),
|
|
|
|
|
DrawStyle {
|
|
|
|
|
fill_color: None,
|
|
|
|
|
stroke_color: Some(PixelColor::Dark),
|
|
|
|
|
stroke_width: 1,
|
|
|
|
|
})
|
|
|
|
|
).expect("IMEF: can't draw prediction top border");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(ic) = self.input_canvas {
|
|
|
|
|
let mut ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, ic).expect("IMEF: Couldn't get input canvas bounds");
|
|
|
|
|
gam::draw_rectangle(self.gam_conn, ic,
|
|
|
|
|
Rectangle::new_with_style(Point::new(0, 0), ic_bounds,
|
|
|
|
|
DrawStyle {
|
|
|
|
|
fill_color: Some(PixelColor::Light),
|
|
|
|
|
stroke_color: None,
|
|
|
|
|
stroke_width: 0
|
|
|
|
|
}
|
|
|
|
|
)).expect("IMEF: can't clear input area");
|
|
|
|
|
|
|
|
|
|
// add the border line on top
|
|
|
|
|
gam::draw_line(self.gam_conn, ic,
|
|
|
|
|
Line::new_with_style(
|
|
|
|
|
Point::new(0,0),
|
|
|
|
|
Point::new(ic_bounds.x, 0),
|
|
|
|
|
DrawStyle {
|
|
|
|
|
fill_color: None,
|
|
|
|
|
stroke_color: Some(PixelColor::Dark),
|
|
|
|
|
stroke_width: 1,
|
|
|
|
|
}))
|
|
|
|
|
.expect("IMEF: can't draw input top line border");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 11:02:56 +00:00
|
|
|
pub fn update(&mut self, newkeys: [char; 4]) -> Result<(), xous::Error> {
|
2021-03-05 19:26:13 +00:00
|
|
|
let debug1= true;
|
2021-03-04 08:02:42 +00:00
|
|
|
// just draw a rectangle for the prediction area for now
|
2021-03-04 11:02:56 +00:00
|
|
|
if let Some(pc) = self.pred_canvas {
|
2021-03-05 17:10:18 +00:00
|
|
|
if debug1{info!("IMEF: updating prediction area");}
|
2021-03-04 11:02:56 +00:00
|
|
|
let pc_bounds: Point = gam::get_canvas_bounds(self.gam_conn, pc).expect("IMEF: Couldn't get prediction canvas bounds");
|
2021-03-05 17:10:18 +00:00
|
|
|
if debug1{info!("IMEF: got pc_bound {:?}", pc_bounds);}
|
2021-03-04 11:02:56 +00:00
|
|
|
let mut starting_tv = TextView::new(pc, 255,
|
|
|
|
|
TextBounds::BoundingBox(Rectangle::new(Point::new(0, 0), pc_bounds)));
|
2021-03-05 17:10:18 +00:00
|
|
|
starting_tv.draw_border = false;
|
2021-03-04 11:02:56 +00:00
|
|
|
starting_tv.border_width = 1;
|
2021-03-05 17:10:18 +00:00
|
|
|
starting_tv.clear_area = false;
|
2021-03-04 11:02:56 +00:00
|
|
|
|
2021-03-05 17:10:18 +00:00
|
|
|
if debug1{info!("IMEF: posting textview {:?}", starting_tv);}
|
2021-03-04 11:02:56 +00:00
|
|
|
gam::post_textview(self.gam_conn, &mut starting_tv).expect("IMEF: can't draw prediction TextView");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(ic) = self.input_canvas {
|
2021-03-05 17:10:18 +00:00
|
|
|
if debug1{info!("IMEF: updating input area");}
|
|
|
|
|
let mut ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, ic).expect("IMEF: Couldn't get input canvas bounds");
|
2021-03-04 11:02:56 +00:00
|
|
|
let mut input_tv = TextView::new(ic, 255,
|
2021-03-05 17:10:18 +00:00
|
|
|
TextBounds::BoundingBox(Rectangle::new(Point::new(0,1), ic_bounds)));
|
|
|
|
|
input_tv.draw_border = false;
|
2021-03-04 11:02:56 +00:00
|
|
|
input_tv.border_width = 1;
|
2021-03-05 19:26:13 +00:00
|
|
|
input_tv.clear_area = true; // need this so that the insertion point is cleared and moved
|
2021-03-04 08:02:42 +00:00
|
|
|
|
2021-03-04 11:02:56 +00:00
|
|
|
for &k in newkeys.iter() {
|
|
|
|
|
if debug1{info!("IMEF: got key '{}'", k);}
|
|
|
|
|
match k {
|
|
|
|
|
'\u{0000}' => (),
|
|
|
|
|
'\u{000d}' => {
|
2021-03-05 19:26:13 +00:00
|
|
|
// TODO: send string to registered listeners
|
|
|
|
|
// TODO: update the predictor on carriage return
|
|
|
|
|
|
2021-03-04 11:02:56 +00:00
|
|
|
// carriage return case
|
2021-03-05 19:26:13 +00:00
|
|
|
if debug1{info!("IMEF: got carriage return");}
|
|
|
|
|
self.line.clear();
|
|
|
|
|
// clear all the temporary variables
|
|
|
|
|
self.insertion.pt.y = 0;
|
|
|
|
|
self.insertion.pt.x = 0;
|
|
|
|
|
self.insertion.line_height = 0;
|
|
|
|
|
self.charwidths = [0; 4096];
|
|
|
|
|
self.clear_area().expect("IMEF: can't clear on carriage return");
|
2021-03-04 11:02:56 +00:00
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
self.line.push(k).expect("IMEF: ran out of space pushing character into input line");
|
|
|
|
|
write!(input_tv.text, "{}", self.line.as_str().expect("IMEF: couldn't convert str")).expect("IMEF: couldn't update TextView string in input canvas");
|
|
|
|
|
gam::post_textview(self.gam_conn, &mut input_tv).expect("IMEF: can't draw input TextView");
|
2021-03-05 17:10:18 +00:00
|
|
|
if debug1{info!("IMEF: got computed cursor of {:?}", input_tv.cursor);}
|
2021-03-04 11:02:56 +00:00
|
|
|
if self.insertion.pt.y == input_tv.cursor.pt.y {
|
|
|
|
|
self.charwidths[self.line.len()] = (input_tv.cursor.pt.x - self.insertion.pt.x) as u8;
|
|
|
|
|
} else {
|
|
|
|
|
// line wrapped, assume we wrapped to x = 0
|
|
|
|
|
self.charwidths[self.line.len()] = (input_tv.cursor.pt.x - 0) as u8;
|
|
|
|
|
}
|
|
|
|
|
self.insertion.pt.y = input_tv.cursor.pt.y;
|
2021-03-05 17:10:18 +00:00
|
|
|
self.insertion.pt.x = input_tv.cursor.pt.x;
|
|
|
|
|
self.insertion.line_height = input_tv.cursor.line_height;
|
2021-03-04 11:02:56 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// draw the insertion point
|
|
|
|
|
// do a manual type conversion, because external crate etc. etc.
|
|
|
|
|
let ins_pt: Point = Point::new(self.insertion.pt.x as i16, self.insertion.pt.y as i16);
|
2021-03-05 17:10:18 +00:00
|
|
|
|
|
|
|
|
if debug1{info!("IMEF: drawing insertion point: {:?}, height: {}", ins_pt, self.insertion.line_height);}
|
|
|
|
|
gam::draw_line(self.gam_conn, ic,
|
|
|
|
|
Line::new(ins_pt,
|
|
|
|
|
ins_pt + Point::new(0, self.insertion.line_height as i16 - input_tv.margin.y) ))
|
|
|
|
|
.expect("IMEF: can't draw insertion point");
|
2021-03-04 11:02:56 +00:00
|
|
|
}
|
2021-03-03 19:14:26 +00:00
|
|
|
/*
|
|
|
|
|
what else do we need:
|
2021-03-05 19:26:13 +00:00
|
|
|
- backspace capability
|
|
|
|
|
- insertion point movement and character insert
|
|
|
|
|
|
|
|
|
|
- height up request of canvas when string wraps
|
|
|
|
|
- registration for listening to string results
|
2021-03-03 19:14:26 +00:00
|
|
|
*/
|
2021-03-04 11:02:56 +00:00
|
|
|
Ok(())
|
2021-03-03 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-03 06:53:33 +00:00
|
|
|
|
|
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
2021-03-05 17:10:18 +00:00
|
|
|
let debug1 = false;
|
2021-03-03 06:53:33 +00:00
|
|
|
log_server::init_wait().unwrap();
|
|
|
|
|
info!("IMEF: my PID is {}", xous::process::id());
|
|
|
|
|
|
|
|
|
|
let imef_sid = xous_names::register_name(xous::names::SERVER_NAME_IME_FRONT).expect("IMEF: can't register server");
|
|
|
|
|
info!("IMEF: registered with NS -- {:?}", imef_sid);
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
2021-03-03 19:14:26 +00:00
|
|
|
let mut tracker = InputTracker::new(
|
|
|
|
|
xous_names::request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("IMEF: can't connect to GAM"),
|
|
|
|
|
);
|
2021-03-05 17:10:18 +00:00
|
|
|
// The main lop can't start until we've been assigned Gids from the GAM, and a Predictor.
|
2021-03-03 19:14:26 +00:00
|
|
|
info!("IMEF: waiting for my canvas Gids");
|
2021-03-03 06:53:33 +00:00
|
|
|
loop {
|
|
|
|
|
let envelope = xous::receive_message(imef_sid).unwrap();
|
2021-03-04 11:02:56 +00:00
|
|
|
if let Ok(opcode) = ImefOpcode::try_from(&envelope.body) {
|
2021-03-03 06:53:33 +00:00
|
|
|
match opcode {
|
2021-03-04 11:02:56 +00:00
|
|
|
ImefOpcode::SetInputCanvas(g) => {
|
2021-03-03 19:14:26 +00:00
|
|
|
if debug1{info!("IMEF: got input canvas {:?}", g);}
|
2021-03-04 11:02:56 +00:00
|
|
|
tracker.set_input_canvas(g);
|
2021-03-03 19:14:26 +00:00
|
|
|
},
|
2021-03-04 11:02:56 +00:00
|
|
|
ImefOpcode::SetPredictionCanvas(g) => {
|
2021-03-03 19:14:26 +00:00
|
|
|
if debug1{info!("IMEF: got prediction canvas {:?}", g);}
|
2021-03-04 11:02:56 +00:00
|
|
|
tracker.set_pred_canvas(g);
|
2021-03-03 06:53:33 +00:00
|
|
|
},
|
|
|
|
|
_ => info!("IMEF: expected canvas Gid, but got {:?}", opcode)
|
|
|
|
|
}
|
2021-03-05 17:10:18 +00:00
|
|
|
} else if let xous::Message::Borrow(m) = &envelope.body {
|
|
|
|
|
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
|
|
|
|
let bytes = Pin::new(buf.as_ref());
|
|
|
|
|
let value = unsafe {
|
|
|
|
|
archived_value::<ImefOpcode>(&bytes, m.id as usize)
|
|
|
|
|
};
|
|
|
|
|
match &*value {
|
|
|
|
|
rkyv::Archived::<ImefOpcode>::SetPredictionServer(rkyv_s) => {
|
|
|
|
|
let s: xous::String<256> = rkyv_s.unarchive();
|
|
|
|
|
match xous_names::request_connection(s.as_str().expect("IMEF: SetPrediction received malformed server name")) {
|
|
|
|
|
Ok(pc) => tracker.set_predictor(ime_plugin_api::PredictionPlugin {connection: Some(pc)}),
|
|
|
|
|
_ => 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")
|
|
|
|
|
};
|
2021-03-03 06:53:33 +00:00
|
|
|
} else {
|
|
|
|
|
info!("IMEF: expected canvas Gid, but got other message first {:?}", envelope);
|
|
|
|
|
}
|
2021-03-04 08:02:42 +00:00
|
|
|
if tracker.is_init() {
|
2021-03-03 19:14:26 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-05 18:45:47 +00:00
|
|
|
// force a redraw of the UI
|
2021-03-05 17:10:18 +00:00
|
|
|
if debug1{info!("IMEF: forcing initial UI redraw");}
|
2021-03-05 18:45:47 +00:00
|
|
|
tracker.clear_area().expect("IMEF: can't initially clear areas");
|
2021-03-05 17:10:18 +00:00
|
|
|
|
2021-03-03 06:53:33 +00:00
|
|
|
let mut key_queue: Vec<char, U32> = Vec::new();
|
|
|
|
|
info!("IMEF: entering main loop");
|
|
|
|
|
loop {
|
|
|
|
|
let envelope = xous::receive_message(imef_sid).unwrap();
|
2021-03-03 15:27:30 +00:00
|
|
|
if debug1{info!("IMEF: got message {:?}", envelope);}
|
2021-03-04 11:02:56 +00:00
|
|
|
if let Ok(opcode) = ImefOpcode::try_from(&envelope.body) {
|
2021-03-03 06:53:33 +00:00
|
|
|
match opcode {
|
2021-03-04 11:02:56 +00:00
|
|
|
ImefOpcode::SetInputCanvas(g) => {
|
2021-03-03 19:14:26 +00:00
|
|
|
// there are valid reasons for this to happen, but it should be rare.
|
|
|
|
|
info!("IMEF: warning: input canvas Gid has been reset");
|
2021-03-04 11:02:56 +00:00
|
|
|
tracker.set_input_canvas(g);
|
2021-03-03 19:14:26 +00:00
|
|
|
},
|
2021-03-04 11:02:56 +00:00
|
|
|
ImefOpcode::SetPredictionCanvas(g) => {
|
2021-03-03 06:53:33 +00:00
|
|
|
// there are valid reasons for this to happen, but it should be rare.
|
2021-03-03 19:14:26 +00:00
|
|
|
info!("IMEF: warning: prediction canvas Gid has been reset");
|
2021-03-04 11:02:56 +00:00
|
|
|
tracker.set_pred_canvas(g);
|
2021-03-03 06:53:33 +00:00
|
|
|
},
|
|
|
|
|
_ => info!("IMEF: unhandled opcode {:?}", opcode)
|
|
|
|
|
}
|
2021-03-03 15:27:30 +00:00
|
|
|
} else if let xous::Message::Borrow(m) = &envelope.body {
|
2021-03-03 06:53:33 +00:00
|
|
|
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
|
|
|
|
let bytes = Pin::new(buf.as_ref());
|
|
|
|
|
let value = unsafe {
|
2021-03-04 11:02:56 +00:00
|
|
|
archived_value::<ImefOpcode>(&bytes, m.id as usize)
|
2021-03-03 06:53:33 +00:00
|
|
|
};
|
|
|
|
|
match &*value {
|
2021-03-04 11:02:56 +00:00
|
|
|
rkyv::Archived::<ImefOpcode>::SetPredictionServer(rkyv_s) => {
|
2021-03-03 19:14:26 +00:00
|
|
|
let s: xous::String<256> = rkyv_s.unarchive();
|
2021-03-03 15:27:30 +00:00
|
|
|
match xous_names::request_connection(s.as_str().expect("IMEF: SetPrediction received malformed server name")) {
|
2021-03-04 08:02:42 +00:00
|
|
|
Ok(pc) => tracker.set_predictor(ime_plugin_api::PredictionPlugin {connection: Some(pc)}),
|
2021-03-03 19:14:26 +00:00
|
|
|
_ => error!("IMEF: can't find predictive engine {}, retaining existing one.", s.as_str().expect("IMEF: SetPrediction received malformed server name")),
|
2021-03-03 15:27:30 +00:00
|
|
|
}
|
2021-03-03 06:53:33 +00:00
|
|
|
},
|
|
|
|
|
_ => panic!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage")
|
|
|
|
|
};
|
|
|
|
|
} else if let Ok(opcode) = keyboard::api::Opcode::try_from(&envelope.body) {
|
|
|
|
|
match opcode {
|
|
|
|
|
keyboard::api::Opcode::KeyboardEvent(keys) => {
|
2021-03-04 11:02:56 +00:00
|
|
|
tracker.update(keys).expect("IMEF: couldn't update input tracker with latest key presses");
|
2021-03-03 06:53:33 +00:00
|
|
|
},
|
|
|
|
|
_ => error!("IMEF: received KBD event opcode that wasn't expected"),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
info!("IMEF: expected canvas Gid, but got {:?}", envelope);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|