format services/ime-frontend

This commit is contained in:
bunnie 2024-01-14 22:14:28 +08:00
parent f1e04f189c
commit 43379c7ac4
2 changed files with 371 additions and 212 deletions

View file

@ -1,20 +1,24 @@
use gam::*;
use ime_plugin_api::ImefOpcode; use ime_plugin_api::ImefOpcode;
use num_traits::*; use num_traits::*;
use xous_ipc::String; use xous_ipc::String;
use gam::*;
// imef_conn must come from outside the scope of the macro because of hygeine rules. // imef_conn must come from outside the scope of the macro because of hygeine rules.
macro_rules! emoji_item { macro_rules! emoji_item {
($emoji: expr, $imef_conn: ident) => { ($emoji:expr, $imef_conn:ident) => {
MenuItem { MenuItem {
name: String::from_str(&$emoji.to_string()), name: String::from_str(&$emoji.to_string()),
action_conn: Some($imef_conn), action_conn: Some($imef_conn),
action_opcode: ImefOpcode::ProcessKeys.to_u32().unwrap(), action_opcode: ImefOpcode::ProcessKeys.to_u32().unwrap(),
action_payload: MenuPayload::Scalar([$emoji.into(), '\u{0000}'.into(), '\u{0000}'.into(), '\u{0000}'.into(), ]), action_payload: MenuPayload::Scalar([
$emoji.into(),
'\u{0000}'.into(),
'\u{0000}'.into(),
'\u{0000}'.into(),
]),
close_on_select: true, close_on_select: true,
} }
} };
} }
// clearly, this does not scale. but, it's a good demo of the menu system. // clearly, this does not scale. but, it's a good demo of the menu system.
@ -40,7 +44,7 @@ pub(crate) fn emoji_menu(imef_conn: xous::CID) {
}, },
], ],
gam::EMOJI_MENU_NAME, gam::EMOJI_MENU_NAME,
None None,
); );
} }
}); });

View file

@ -2,25 +2,20 @@
#![cfg_attr(target_os = "none", no_main)] #![cfg_attr(target_os = "none", no_main)]
mod emoji; mod emoji;
use emoji::*;
use gam::api::SetCanvasBoundsRequest;
use ime_plugin_api::{ImefCallback, ImefDescriptor, ImefOpcode};
use log::{error, info};
use graphics_server::{Gid, Line, PixelColor, Point, Rectangle, TextBounds, TextView, DrawStyle};
use ime_plugin_api::{PredictionTriggers, PredictionPlugin, PredictionApi, ApiToken};
use num_traits::{ToPrimitive,FromPrimitive};
use xous_ipc::Buffer;
use xous::{CID, msg_scalar_unpack};
use core::fmt::Write; use core::fmt::Write;
use emoji::*;
use gam::api::SetCanvasBoundsRequest;
use graphics_server::{DrawStyle, Gid, Line, PixelColor, Point, Rectangle, TextBounds, TextView};
use ime_plugin_api::{ApiToken, PredictionApi, PredictionPlugin, PredictionTriggers};
use ime_plugin_api::{ImefCallback, ImefDescriptor, ImefOpcode};
use locales::t; use locales::t;
use log::{error, info};
use num_traits::{FromPrimitive, ToPrimitive};
#[cfg(feature = "tts")] #[cfg(feature = "tts")]
use tts_frontend::*; use tts_frontend::*;
use xous::{msg_scalar_unpack, CID};
use xous_ipc::Buffer;
/// max number of prediction options to track/render /// max number of prediction options to track/render
const MAX_PREDICTION_OPTIONS: usize = 4; const MAX_PREDICTION_OPTIONS: usize = 4;
@ -29,7 +24,6 @@ struct InputTracker {
/// connection for handling graphical update requests /// connection for handling graphical update requests
pub gam: gam::Gam, pub gam: gam::Gam,
/// input area canvas, as given by the GAM /// input area canvas, as given by the GAM
input_canvas: Option<Gid>, input_canvas: Option<Gid>,
/// prediction display area, as given by the GAM /// prediction display area, as given by the GAM
@ -40,22 +34,25 @@ struct InputTracker {
/// our current prediction engine /// our current prediction engine
predictor: Option<PredictionPlugin>, predictor: Option<PredictionPlugin>,
/// the name & token of our engine, so we can disconnect later on /// the name & token of our engine, so we can disconnect later on
pub predictor_conn: Option<(xous_ipc::String::<64>, [u32; 4])>, pub predictor_conn: Option<(xous_ipc::String<64>, [u32; 4])>,
/// cached copy of the predictor's triggers for predictions. Only valid if predictor is not None /// cached copy of the predictor's triggers for predictions. Only valid if predictor is not None
pred_triggers: Option<PredictionTriggers>, pred_triggers: Option<PredictionTriggers>,
/// set if we're in a state where a backspace should trigger an unpredict /// set if we're in a state where a backspace should trigger an unpredict
can_unpick: bool, // note: untested as of Mar 7 2021 can_unpick: bool, // note: untested as of Mar 7 2021
/// the predictor string -- this is different from the input line, because it can be broken up by spaces and punctuatino /// the predictor string -- this is different from the input line, because it can be broken up by spaces
/// and punctuatino
pred_phrase: String, // note: untested as of Mar 7 2021 pred_phrase: String, // note: untested as of Mar 7 2021
/// character position of the last prediction trigger -- this is where the prediction overwrite starts /// character position of the last prediction trigger -- this is where the prediction overwrite starts
/// if None, it means we were unable to determine the trigger (e.g., we went back and edited text manually) /// if None, it means we were unable to determine the trigger (e.g., we went back and edited text
/// manually)
last_trigger_char: Option<usize>, last_trigger_char: Option<usize>,
/// track the progress of our input line /// track the progress of our input line
line: String, line: String,
/// length of the line in *characters*, not bytes (which is what .len() returns), used to index char_locs /// length of the line in *characters*, not bytes (which is what .len() returns), used to index char_locs
characters: usize, characters: usize,
/// the insertion point, 0 is inserting characters before the first, 1 inserts characters after the first, etc. /// the insertion point, 0 is inserting characters before the first, 1 inserts characters after the
/// first, etc.
insertion: usize, insertion: usize,
/// last returned line height, which is used as a reference for growing the area when we run out of space /// last returned line height, which is used as a reference for growing the area when we run out of space
last_height: u32, last_height: u32,
@ -72,7 +69,7 @@ struct InputTracker {
} }
impl InputTracker { impl InputTracker {
pub fn new(xns: &xous_names::XousNames)-> InputTracker { pub fn new(xns: &xous_names::XousNames) -> InputTracker {
InputTracker { InputTracker {
gam: gam::Gam::new(&xns).unwrap(), gam: gam::Gam::new(&xns).unwrap(),
input_canvas: None, input_canvas: None,
@ -91,94 +88,112 @@ impl InputTracker {
was_grown: false, was_grown: false,
pred_options: Default::default(), pred_options: Default::default(),
menu_mode: false, menu_mode: false,
#[cfg(feature="tts")] #[cfg(feature = "tts")]
tts: TtsFrontend::new(xns).unwrap(), tts: TtsFrontend::new(xns).unwrap(),
} }
} }
pub fn set_gam_token(&mut self, token: [u32; 4]) {
self.gam_token = Some(token); pub fn set_gam_token(&mut self, token: [u32; 4]) { self.gam_token = Some(token); }
}
/// this is a separate, non-blocking call instead of a return because /// this is a separate, non-blocking call instead of a return because
/// the call which sets the predictor *must* complete to allow further drawing /// the call which sets the predictor *must* complete to allow further drawing
/// this does mean there is a tiny bit of a race condition between when /// this does mean there is a tiny bit of a race condition between when
/// a context is swapped and when a predictor can run. /// a context is swapped and when a predictor can run.
pub fn send_api_token(&self, at: &ApiToken) { pub fn send_api_token(&self, at: &ApiToken) {
self.gam.set_predictor_api_token(at.api_token, at.gam_token).expect("couldn't set predictor API token"); self.gam
.set_predictor_api_token(at.api_token, at.gam_token)
.expect("couldn't set predictor API token");
} }
pub fn set_predictor(&mut self, predictor: Option<PredictionPlugin>) { pub fn set_predictor(&mut self, predictor: Option<PredictionPlugin>) {
self.predictor = predictor; self.predictor = predictor;
if let Some(pred) = predictor { if let Some(pred) = predictor {
self.pred_triggers = Some(pred.get_prediction_triggers() self.pred_triggers = Some(
.expect("InputTracker failed to get prediction triggers from plugin")); pred.get_prediction_triggers()
.expect("InputTracker failed to get prediction triggers from plugin"),
);
} }
} }
pub fn get_predictor(&self) -> Option<PredictionPlugin> {
self.predictor pub fn get_predictor(&self) -> Option<PredictionPlugin> { self.predictor }
}
pub fn set_input_canvas(&mut self, input: Gid) { pub fn set_input_canvas(&mut self, input: Gid) { self.input_canvas = Some(input); }
self.input_canvas = Some(input);
}
pub fn clear_input_canvas(&mut self) { self.input_canvas = None } pub fn clear_input_canvas(&mut self) { self.input_canvas = None }
pub fn set_pred_canvas(&mut self, pred: Gid) {
self.pred_canvas = Some(pred); pub fn set_pred_canvas(&mut self, pred: Gid) { self.pred_canvas = Some(pred); }
}
pub fn clear_pred_canvas(&mut self) { self.pred_canvas = None } pub fn clear_pred_canvas(&mut self) { self.pred_canvas = None }
pub fn is_init(&self) -> bool { pub fn is_init(&self) -> bool {
self.input_canvas.is_some() && self.pred_canvas.is_some() && self.predictor.is_some() self.input_canvas.is_some() && self.pred_canvas.is_some() && self.predictor.is_some()
} }
pub fn activate_emoji(&self) { pub fn activate_emoji(&self) {
self.gam.raise_menu(gam::EMOJI_MENU_NAME).expect("couldn't activate emoji menu"); self.gam.raise_menu(gam::EMOJI_MENU_NAME).expect("couldn't activate emoji menu");
} }
pub fn set_menu_mode(&mut self, mode: bool) {
self.menu_mode = mode; pub fn set_menu_mode(&mut self, mode: bool) { self.menu_mode = mode; }
}
pub fn clear_area(&mut self) -> Result<(), xous::Error> { pub fn clear_area(&mut self) -> Result<(), xous::Error> {
if let Some(pc) = self.pred_canvas { if let Some(pc) = self.pred_canvas {
let pc_bounds: Point = self.gam.get_canvas_bounds(pc).expect("Couldn't get prediction canvas bounds"); let pc_bounds: Point =
self.gam.draw_rectangle(pc, self.gam.get_canvas_bounds(pc).expect("Couldn't get prediction canvas bounds");
Rectangle::new_with_style(Point::new(0, 0), pc_bounds, self.gam
DrawStyle { .draw_rectangle(
fill_color: Some(PixelColor::Light), pc,
stroke_color: None, Rectangle::new_with_style(
stroke_width: 0 Point::new(0, 0),
} pc_bounds,
)).expect("can't clear prediction area"); DrawStyle {
fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0,
},
),
)
.expect("can't clear prediction area");
// add the border line on top // add the border line on top
self.gam.draw_line(pc, self.gam
Line::new_with_style( .draw_line(
Point::new(0,0), pc,
Point::new(pc_bounds.x, 0), Line::new_with_style(
DrawStyle { Point::new(0, 0),
fill_color: None, Point::new(pc_bounds.x, 0),
stroke_color: Some(PixelColor::Dark), DrawStyle { fill_color: None, stroke_color: Some(PixelColor::Dark), stroke_width: 1 },
stroke_width: 1, ),
}) )
).expect("can't draw prediction top border"); .expect("can't draw prediction top border");
} }
if let Some(ic) = self.input_canvas { if let Some(ic) = self.input_canvas {
let ic_bounds: Point = self.gam.get_canvas_bounds(ic).expect("Couldn't get input canvas bounds"); let ic_bounds: Point = self.gam.get_canvas_bounds(ic).expect("Couldn't get input canvas bounds");
self.gam.draw_rectangle(ic, self.gam
Rectangle::new_with_style(Point::new(0, 0), ic_bounds, .draw_rectangle(
DrawStyle { ic,
fill_color: Some(PixelColor::Light), Rectangle::new_with_style(
stroke_color: None, Point::new(0, 0),
stroke_width: 0 ic_bounds,
} DrawStyle {
)).expect("can't clear input area"); fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0,
},
),
)
.expect("can't clear input area");
// add the border line on top // add the border line on top
self.gam.draw_line(ic, self.gam
Line::new_with_style( .draw_line(
Point::new(0,0), ic,
Point::new(ic_bounds.x, 0), Line::new_with_style(
DrawStyle { Point::new(0, 0),
fill_color: None, Point::new(ic_bounds.x, 0),
stroke_color: Some(PixelColor::Dark), DrawStyle { fill_color: None, stroke_color: Some(PixelColor::Dark), stroke_width: 1 },
stroke_width: 1, ),
})) )
.expect("can't draw input top line border"); .expect("can't draw input top line border");
} }
Ok(()) Ok(())
@ -186,21 +201,27 @@ impl InputTracker {
fn insert_prediction(&mut self, index: usize) { fn insert_prediction(&mut self, index: usize) {
let debug1 = false; let debug1 = false;
if debug1{info!("IMEF|insert_prediction index {}", index);} if debug1 {
info!("IMEF|insert_prediction index {}", index);
}
let pred_str = match &self.pred_options[index] { let pred_str = match &self.pred_options[index] {
Some(s) => s, Some(s) => s,
_ => return // if the index doesn't exist for some reason, do nothing without throwing an error _ => return, // if the index doesn't exist for some reason, do nothing without throwing an error
}; };
if debug1{info!("IMEF|insert_prediction string {}, last_trigger {:?}", pred_str, self.last_trigger_char);} if debug1 {
info!("IMEF|insert_prediction string {}, last_trigger {:?}", pred_str, self.last_trigger_char);
}
if let Some(offset) = self.last_trigger_char { if let Some(offset) = self.last_trigger_char {
if offset < self.characters { if offset < self.characters {
// copy the bytes in the original string, up to the offset; and then copy the bytes in the selected predictor // copy the bytes in the original string, up to the offset; and then copy the bytes in the
// selected predictor
let tempstr = self.line.to_string(); let tempstr = self.line.to_string();
self.line.clear(); self.line.clear();
let mut chars = 0; let mut chars = 0;
let mut c_iter = tempstr.chars(); let mut c_iter = tempstr.chars();
loop { loop {
if chars == offset + 1 { // +1 to include the original trigger (don't overwrite it) if chars == offset + 1 {
// +1 to include the original trigger (don't overwrite it)
break; break;
} }
if let Some(c) = c_iter.next() { if let Some(c) = c_iter.next() {
@ -218,12 +239,13 @@ impl InputTracker {
// forward until we find the next prediction trigger in the original string // forward until we find the next prediction trigger in the original string
while let Some(c) = c_iter.next() { while let Some(c) = c_iter.next() {
if let Some(trigger) = self.pred_triggers { if let Some(trigger) = self.pred_triggers {
if trigger.whitespace && c.is_ascii_whitespace() || if trigger.whitespace && c.is_ascii_whitespace()
trigger.punctuation && c.is_ascii_punctuation() { || trigger.punctuation && c.is_ascii_punctuation()
// include the trigger that was found {
self.line.push(c); // include the trigger that was found
chars += 1; self.line.push(c);
break; chars += 1;
break;
} }
} else { } else {
// skip the replaced characters // skip the replaced characters
@ -249,15 +271,22 @@ impl InputTracker {
} }
} }
pub fn update(&mut self, newkeys: [char; 4], force_redraw: bool, api_token: [u32; 4]) -> Result<Option<xous_ipc::String::<4000>>, xous::Error> { pub fn update(
let debug1= false; &mut self,
newkeys: [char; 4],
force_redraw: bool,
api_token: [u32; 4],
) -> Result<Option<xous_ipc::String<4000>>, xous::Error> {
let debug1 = false;
let mut update_predictor = force_redraw; let mut update_predictor = force_redraw;
let mut retstring: Option<xous_ipc::String::<4000>> = None; let mut retstring: Option<xous_ipc::String<4000>> = None;
if let Some(ic) = self.input_canvas { if let Some(ic) = self.input_canvas {
if debug1{info!("updating input area");} if debug1 {
info!("updating input area");
}
let ic_bounds: Point = self.gam.get_canvas_bounds(ic).expect("Couldn't get input canvas bounds"); let ic_bounds: Point = self.gam.get_canvas_bounds(ic).expect("Couldn't get input canvas bounds");
let mut input_tv = TextView::new(ic, let mut input_tv =
TextBounds::BoundingBox(Rectangle::new(Point::new(0,1), ic_bounds))); TextView::new(ic, TextBounds::BoundingBox(Rectangle::new(Point::new(0, 1), ic_bounds)));
input_tv.draw_border = false; input_tv.draw_border = false;
input_tv.border_width = 1; input_tv.border_width = 1;
input_tv.clear_area = true; // need this so that the insertion point is cleared and moved input_tv.clear_area = true; // need this so that the insertion point is cleared and moved
@ -265,10 +294,13 @@ impl InputTracker {
let mut do_redraw = false; let mut do_redraw = false;
for &k in newkeys.iter() { for &k in newkeys.iter() {
if debug1{info!("got key '{}'", k);} if debug1 {
info!("got key '{}'", k);
}
match k { match k {
'\u{0000}' => (), '\u{0000}' => (),
'←' => { // move insertion point back '←' => {
// move insertion point back
if !self.menu_mode { if !self.menu_mode {
if self.insertion > 0 { if self.insertion > 0 {
log::debug!("moving insertion point back"); log::debug!("moving insertion point back");
@ -314,14 +346,16 @@ impl InputTracker {
do_redraw = true; do_redraw = true;
self.pred_phrase.clear(); self.pred_phrase.clear();
self.can_unpick = false; self.can_unpick = false;
// this means that when we resume typing after an edit, the predictor will set its insertion point // this means that when we resume typing after an edit, the predictor will set its
// at the very end, not the space prior to the last word... // insertion point at the very end, not the space
// prior to the last word...
self.last_trigger_char = Some(self.characters); self.last_trigger_char = Some(self.characters);
} else { } else {
return Ok(Some(xous_ipc::String::<4000>::from_str("↓"))); return Ok(Some(xous_ipc::String::<4000>::from_str("↓")));
} }
} }
'\u{0011}' => { // F1 '\u{0011}' => {
// F1
if !self.menu_mode { if !self.menu_mode {
self.insert_prediction(0); self.insert_prediction(0);
do_redraw = true; do_redraw = true;
@ -330,7 +364,8 @@ impl InputTracker {
do_redraw = true; do_redraw = true;
} }
} }
'\u{0012}' => { // F2 '\u{0012}' => {
// F2
if !self.menu_mode { if !self.menu_mode {
self.insert_prediction(1); self.insert_prediction(1);
do_redraw = true; do_redraw = true;
@ -339,7 +374,8 @@ impl InputTracker {
do_redraw = true; do_redraw = true;
} }
} }
'\u{0013}' => { // F3 '\u{0013}' => {
// F3
if !self.menu_mode { if !self.menu_mode {
self.insert_prediction(2); self.insert_prediction(2);
do_redraw = true; do_redraw = true;
@ -348,7 +384,8 @@ impl InputTracker {
do_redraw = true; do_redraw = true;
} }
} }
'\u{0014}' => { // F4 '\u{0014}' => {
// F4
if !self.menu_mode { if !self.menu_mode {
self.insert_prediction(3); self.insert_prediction(3);
do_redraw = true; do_redraw = true;
@ -357,11 +394,14 @@ impl InputTracker {
do_redraw = true; do_redraw = true;
} }
} }
'\u{0008}' => { // backspace '\u{0008}' => {
#[cfg(feature="tts")] // backspace
#[cfg(feature = "tts")]
self.tts.tts_simple(t!("input.delete-tts", locales::LANG)).unwrap(); self.tts.tts_simple(t!("input.delete-tts", locales::LANG)).unwrap();
if (self.characters > 0) && (self.insertion == self.characters) { if (self.characters > 0) && (self.insertion == self.characters) {
if debug1{info!("simple backspace case")} if debug1 {
info!("simple backspace case")
}
self.line.pop(); self.line.pop();
self.characters -= 1; self.characters -= 1;
self.insertion -= 1; self.insertion -= 1;
@ -374,12 +414,17 @@ impl InputTracker {
update_predictor = true; update_predictor = true;
} }
self.pred_phrase.pop(); self.pred_phrase.pop();
if self.menu_mode { update_predictor = true; } if self.menu_mode {
update_predictor = true;
}
} }
} else if (self.characters > 0) && (self.insertion > 0) { } else if (self.characters > 0) && (self.insertion > 0) {
if debug1{info!("mid-string backspace case")} if debug1 {
// awful O(N) algo because we have to decode variable-length utf8 strings to figure out character boundaries info!("mid-string backspace case")
// first, make a copy of the string }
// awful O(N) algo because we have to decode variable-length utf8 strings to
// figure out character boundaries first, make a copy
// of the string
let tempstr = self.line.to_string(); let tempstr = self.line.to_string();
// clear the string // clear the string
self.line.clear(); self.line.clear();
@ -388,12 +433,17 @@ impl InputTracker {
let mut i = 0; // character position let mut i = 0; // character position
let mut dest_chars = 0; // destination character position let mut dest_chars = 0; // destination character position
for c in tempstr.chars() { for c in tempstr.chars() {
if debug1{info!("checking index {}", i);} if debug1 {
info!("checking index {}", i);
}
if i == self.insertion - 1 { if i == self.insertion - 1 {
if debug1{info!("skipping char");} if debug1 {
info!("skipping char");
}
} else { } else {
dest_chars += 1; dest_chars += 1;
// if we encounter a trigger character, set the trigger to just after this point (hence the += before this line) // if we encounter a trigger character, set the trigger to just after this
// point (hence the += before this line)
if let Some(trigger) = self.pred_triggers { if let Some(trigger) = self.pred_triggers {
if trigger.punctuation && c.is_ascii_punctuation() { if trigger.punctuation && c.is_ascii_punctuation() {
self.last_trigger_char = Some(dest_chars); self.last_trigger_char = Some(dest_chars);
@ -401,7 +451,9 @@ impl InputTracker {
self.last_trigger_char = Some(dest_chars); self.last_trigger_char = Some(dest_chars);
} }
} }
if debug1{info!("copying char {}", c);} if debug1 {
info!("copying char {}", c);
}
self.line.push(c); self.line.push(c);
} }
i += 1; i += 1;
@ -409,29 +461,39 @@ impl InputTracker {
self.insertion -= 1; self.insertion -= 1;
self.characters -= 1; self.characters -= 1;
do_redraw = true; do_redraw = true;
if self.menu_mode { update_predictor = true; } if self.menu_mode {
update_predictor = true;
}
} else { } else {
// ignore, we are either at the front of the string, or the string had no characters // ignore, we are either at the front of the string, or the string had no
// characters
} }
} }
'\u{000d}' => { // carriage return '\u{000d}' => {
// carriage return
let mut ret = xous_ipc::String::<4000>::new(); let mut ret = xous_ipc::String::<4000>::new();
write!(ret, "{}", self.line.as_str()).expect("couldn't copy input line to output"); write!(ret, "{}", self.line.as_str()).expect("couldn't copy input line to output");
retstring = Some(ret); retstring = Some(ret);
if let Some(trigger) = self.pred_triggers { if let Some(trigger) = self.pred_triggers {
if trigger.newline { if trigger.newline {
self.predictor.unwrap().feedback_picked( self.predictor
xous_ipc::String::<4000>::from_str(&self.line)).expect("couldn't send feedback to predictor"); .unwrap()
.feedback_picked(xous_ipc::String::<4000>::from_str(&self.line))
.expect("couldn't send feedback to predictor");
} else if trigger.punctuation { } else if trigger.punctuation {
self.predictor.unwrap().feedback_picked( self.predictor
xous_ipc::String::<4000>::from_str(&self.pred_phrase)).expect("couldn't send feedback to predictor"); .unwrap()
.feedback_picked(xous_ipc::String::<4000>::from_str(&self.pred_phrase))
.expect("couldn't send feedback to predictor");
} }
} }
self.can_unpick = false; self.can_unpick = false;
self.pred_phrase.clear(); self.pred_phrase.clear();
if debug1{info!("got carriage return");} if debug1 {
info!("got carriage return");
}
self.line.clear(); self.line.clear();
self.last_trigger_char = Some(0); self.last_trigger_char = Some(0);
// clear all the temporary variables // clear all the temporary variables
@ -439,14 +501,19 @@ impl InputTracker {
self.insertion = 0; self.insertion = 0;
if self.was_grown { if self.was_grown {
let mut req = SetCanvasBoundsRequest { let mut req = SetCanvasBoundsRequest {
requested: Point::new(0, 0), // size 0 will snap to the original smallest default size requested: Point::new(0, 0), /* size 0 will snap to the original smallest
* default size */
granted: None, granted: None,
token_type: gam::TokenType::Gam, token_type: gam::TokenType::Gam,
token: self.gam_token.unwrap(), token: self.gam_token.unwrap(),
}; };
if debug1{info!("attempting resize to {:?}", req.requested);} if debug1 {
self.gam.set_canvas_bounds_request(&mut req).expect("couldn't call set_bounds_request on input area overflow"); info!("attempting resize to {:?}", req.requested);
if debug1{ }
self.gam
.set_canvas_bounds_request(&mut req)
.expect("couldn't call set_bounds_request on input area overflow");
if debug1 {
info!("carriage return resize to {:?}", req.granted); info!("carriage return resize to {:?}", req.granted);
} }
self.last_height = 0; self.last_height = 0;
@ -454,13 +521,17 @@ impl InputTracker {
} }
self.clear_area().expect("can't clear on carriage return"); self.clear_area().expect("can't clear on carriage return");
update_predictor = true; update_predictor = true;
}, }
_ => { _ => {
if let Some(trigger) = self.pred_triggers { if let Some(trigger) = self.pred_triggers {
if trigger.whitespace && k.is_ascii_whitespace() { if trigger.whitespace && k.is_ascii_whitespace() {
if self.pred_phrase.len() > 0 { if self.pred_phrase.len() > 0 {
self.predictor.unwrap().feedback_picked( self.predictor
xous_ipc::String::<4000>::from_str(&self.pred_phrase)).expect("couldn't send feedback to predictor"); .unwrap()
.feedback_picked(xous_ipc::String::<4000>::from_str(
&self.pred_phrase,
))
.expect("couldn't send feedback to predictor");
self.pred_phrase.clear(); self.pred_phrase.clear();
self.can_unpick = true; self.can_unpick = true;
update_predictor = true; update_predictor = true;
@ -468,8 +539,12 @@ impl InputTracker {
self.last_trigger_char = Some(self.insertion); self.last_trigger_char = Some(self.insertion);
} else if trigger.punctuation && k.is_ascii_punctuation() { } else if trigger.punctuation && k.is_ascii_punctuation() {
if self.pred_phrase.len() > 0 { if self.pred_phrase.len() > 0 {
self.predictor.unwrap().feedback_picked( self.predictor
xous_ipc::String::<4000>::from_str(&self.pred_phrase)).expect("couldn't send feedback to predictor"); .unwrap()
.feedback_picked(xous_ipc::String::<4000>::from_str(
&self.pred_phrase,
))
.expect("couldn't send feedback to predictor");
self.pred_phrase.clear(); self.pred_phrase.clear();
self.can_unpick = true; self.can_unpick = true;
update_predictor = true; update_predictor = true;
@ -478,7 +553,7 @@ impl InputTracker {
} }
} }
if self.insertion == self.characters { if self.insertion == self.characters {
#[cfg(feature="tts")] #[cfg(feature = "tts")]
{ {
if !k.is_ascii_whitespace() && !k.is_ascii_punctuation() { if !k.is_ascii_whitespace() && !k.is_ascii_punctuation() {
// this is disastisfying in how slow it is // this is disastisfying in how slow it is
@ -487,8 +562,9 @@ impl InputTracker {
} }
self.line.push(k); self.line.push(k);
if let Some(trigger) = self.pred_triggers { if let Some(trigger) = self.pred_triggers {
if !(trigger.punctuation && k.is_ascii_punctuation() || if !(trigger.punctuation && k.is_ascii_punctuation()
trigger.whitespace && k.is_ascii_whitespace() ) { || trigger.whitespace && k.is_ascii_whitespace())
{
self.pred_phrase.push(k); self.pred_phrase.push(k);
update_predictor = true; update_predictor = true;
} }
@ -502,29 +578,40 @@ impl InputTracker {
self.pred_phrase.clear(); self.pred_phrase.clear();
self.can_unpick = false; // we don't know how far back the user is going to make the edit self.can_unpick = false; // we don't know how far back the user is going to make the edit
// in order to do predictions on arbitrary words, every time the scroll keys are // in order to do predictions on arbitrary words, every time the scroll keys
// pressed, we need to reset the prediction trigger to the previous word, which we // are pressed, we need to reset the
// don't keep. so, for now, we just keep the old predictions around, until the user // prediction trigger to the previous word, which we
// goes back to appending words at the end of the sentence // don't keep. so, for now, we just keep the old predictions around, until the
// user goes back to appending words at the
// end of the sentence
} }
if debug1{info!("handling case of inserting characters. insertion: {}", self.insertion)}; if debug1 {
// awful O(N) algo because we have to decode variable-length utf8 strings to figure out character boundaries info!("handling case of inserting characters. insertion: {}", self.insertion)
// first, make a copy of the string };
let tempstr =self.line.to_string(); // awful O(N) algo because we have to decode variable-length utf8 strings to
// figure out character boundaries first, make a copy
// of the string
let tempstr = self.line.to_string();
// clear the string // clear the string
self.line.clear(); self.line.clear();
// insert the character in the array // insert the character in the array
let mut i = 0; let mut i = 0;
for c in tempstr.chars() { for c in tempstr.chars() {
if debug1{info!("checking index {}", i);} if debug1 {
info!("checking index {}", i);
}
if i == self.insertion { if i == self.insertion {
if debug1{info!("inserting char {}", k);} if debug1 {
info!("inserting char {}", k);
}
self.line.push(k); self.line.push(k);
self.line.push(c); self.line.push(c);
} else { } else {
if debug1{info!("copying char {}", c);} if debug1 {
info!("copying char {}", c);
}
self.line.push(c); self.line.push(c);
} }
i += 1; i += 1;
@ -533,20 +620,28 @@ impl InputTracker {
self.characters += 1; self.characters += 1;
do_redraw = true; do_redraw = true;
} }
}, }
} }
} }
input_tv.insertion = Some(self.insertion as _); input_tv.insertion = Some(self.insertion as _);
if debug1{info!("insertion point is {}, characters in string {}", self.insertion, self.characters);} if debug1 {
info!("insertion point is {}, characters in string {}", self.insertion, self.characters);
}
if do_redraw || force_redraw { if do_redraw || force_redraw {
write!(input_tv.text, "{}", self.line.as_str()).expect("couldn't update TextView string in input canvas"); write!(input_tv.text, "{}", self.line.as_str())
.expect("couldn't update TextView string in input canvas");
self.gam.post_textview(&mut input_tv).expect("can't draw input TextView"); self.gam.post_textview(&mut input_tv).expect("can't draw input TextView");
if debug1{info!("got computed cursor of {:?}", input_tv.cursor);} if debug1 {
info!("got computed cursor of {:?}", input_tv.cursor);
}
// check if the cursor is now at the bottom of the textview, this means we need to grow the box // check if the cursor is now at the bottom of the textview, this means we need to grow the
// box
if input_tv.cursor.line_height == 0 && self.characters > 0 { if input_tv.cursor.line_height == 0 && self.characters > 0 {
if debug1{info!("caught case of overflowed text box, attempting to resize");} if debug1 {
info!("caught case of overflowed text box, attempting to resize");
}
let delta = if self.last_height > 0 { let delta = if self.last_height > 0 {
self.last_height + 1 + 1 // 1 pixel allowance for interline space, plus 1 for fencepost self.last_height + 1 + 1 // 1 pixel allowance for interline space, plus 1 for fencepost
} else { } else {
@ -558,19 +653,26 @@ impl InputTracker {
token_type: gam::TokenType::Gam, token_type: gam::TokenType::Gam,
token: self.gam_token.unwrap(), token: self.gam_token.unwrap(),
}; };
if debug1{info!("attempting resize to {:?}", req.requested);} if debug1 {
self.gam.set_canvas_bounds_request(&mut req).expect("couldn't call set_bounds_request on input area overflow"); info!("attempting resize to {:?}", req.requested);
}
self.gam
.set_canvas_bounds_request(&mut req)
.expect("couldn't call set_bounds_request on input area overflow");
self.clear_area().expect("couldn't clear area after resize"); self.clear_area().expect("couldn't clear area after resize");
match req.granted { match req.granted {
Some(bounds) => { Some(bounds) => {
self.was_grown = true; self.was_grown = true;
if debug1{info!("refresh succeeded, now redrawing with height of {:?}", bounds);} if debug1 {
info!("refresh succeeded, now redrawing with height of {:?}", bounds);
}
// request was approved, redraw with the new bounding box // request was approved, redraw with the new bounding box
input_tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(Point::new(0,1), bounds)); input_tv.bounds_hint =
TextBounds::BoundingBox(Rectangle::new(Point::new(0, 1), bounds));
input_tv.bounds_computed = None; input_tv.bounds_computed = None;
self.gam.post_textview(&mut input_tv).expect("can't draw input TextView"); self.gam.post_textview(&mut input_tv).expect("can't draw input TextView");
}, }
_ => info!("couldn't resize input canvas after overflow of text") _ => info!("couldn't resize input canvas after overflow of text"),
} }
} else { } else {
self.last_height = input_tv.cursor.line_height as u32; self.last_height = input_tv.cursor.line_height as u32;
@ -578,29 +680,38 @@ impl InputTracker {
} }
} }
// prediction area is drawn second because the area could be cleared on behalf of a resize of the text box // prediction area is drawn second because the area could be cleared on behalf of a resize of the text
// just draw a rectangle for the prediction area for now // box just draw a rectangle for the prediction area for now
if let Some(pc) = self.pred_canvas { if let Some(pc) = self.pred_canvas {
if debug1{info!("updating prediction area");} if debug1 {
let pc_bounds: Point = self.gam.get_canvas_bounds(pc).expect("Couldn't get prediction canvas bounds"); info!("updating prediction area");
let pc_clip: Rectangle = Rectangle::new_with_style(Point::new(0,1), pc_bounds, }
DrawStyle { fill_color: Some(PixelColor::Light), stroke_color: None, stroke_width: 0 } let pc_bounds: Point =
self.gam.get_canvas_bounds(pc).expect("Couldn't get prediction canvas bounds");
let pc_clip: Rectangle = Rectangle::new_with_style(
Point::new(0, 1),
pc_bounds,
DrawStyle { fill_color: Some(PixelColor::Light), stroke_color: None, stroke_width: 0 },
); );
if debug1{info!("got pc_bound {:?}", pc_bounds);} if debug1 {
info!("got pc_bound {:?}", pc_bounds);
}
if update_predictor { if update_predictor {
if self.pred_phrase.len() > 0 || self.menu_mode { if self.pred_phrase.len() > 0 || self.menu_mode {
if let Some(pred) = self.predictor { if let Some(pred) = self.predictor {
pred.set_input( pred.set_input(xous_ipc::String::<4000>::from_str(&self.pred_phrase))
xous_ipc::String::<4000>::from_str(&self.pred_phrase)).expect("couldn't update predictor with current input"); .expect("couldn't update predictor with current input");
} }
} }
// Query the prediction engine for the latest predictions // Query the prediction engine for the latest predictions
if let Some(pred) = self.predictor { if let Some(pred) = self.predictor {
for i in 0..self.pred_options.len() { for i in 0..self.pred_options.len() {
let p = if let Some(prediction) = let p = if let Some(prediction) = pred
pred.get_prediction(i as u32, api_token).expect("couldn't query prediction engine") { .get_prediction(i as u32, api_token)
.expect("couldn't query prediction engine")
{
Some(String::from(prediction.as_str().unwrap_or("UTF-8 Error"))) Some(String::from(prediction.as_str().unwrap_or("UTF-8 Error")))
} else { } else {
None None
@ -620,39 +731,55 @@ impl InputTracker {
let debug_canvas = false; let debug_canvas = false;
if valid_predictions == 0 { if valid_predictions == 0 {
let mut empty_tv = TextView::new(pc, let mut empty_tv =
TextBounds::BoundingBox(Rectangle::new(Point::new(0, 1), pc_bounds))); TextView::new(pc, TextBounds::BoundingBox(Rectangle::new(Point::new(0, 1), pc_bounds)));
empty_tv.draw_border = false; empty_tv.draw_border = false;
empty_tv.border_width = 1; empty_tv.border_width = 1;
empty_tv.clear_area = true; empty_tv.clear_area = true;
write!(empty_tv.text, "{}", t!("input.greeting", locales::LANG)).expect("couldn't set up empty TextView"); write!(empty_tv.text, "{}", t!("input.greeting", locales::LANG))
if debug_canvas { info!("pc canvas {:?}", pc) } .expect("couldn't set up empty TextView");
if debug_canvas {
info!("pc canvas {:?}", pc)
}
self.gam.post_textview(&mut empty_tv).expect("can't draw prediction TextView"); self.gam.post_textview(&mut empty_tv).expect("can't draw prediction TextView");
} else if update_predictor || force_redraw { } else if update_predictor || force_redraw {
// alright, first, let's clear the area // alright, first, let's clear the area
self.gam.draw_rectangle(pc, pc_clip).expect("couldn't clear predictor area"); self.gam.draw_rectangle(pc, pc_clip).expect("couldn't clear predictor area");
if debug1{info!("valid_predictions: {}", valid_predictions);} if debug1 {
info!("valid_predictions: {}", valid_predictions);
}
// OK, let's start initially with just a naive, split-by-N layout of the prediction area // OK, let's start initially with just a naive, split-by-N layout of the prediction area
let approx_width = pc_bounds.x / valid_predictions as i16; let approx_width = pc_bounds.x / valid_predictions as i16;
let mut i = 0; let mut i = 0;
for p in self.pred_options.iter() { for p in self.pred_options.iter() {
if let Some(pred_str) = p { if let Some(pred_str) = p {
// the post-clip is necessary because the approx_width is rounded to some integer fraction // the post-clip is necessary because the approx_width is rounded to some integer
// fraction
let p_clip = Rectangle::new( let p_clip = Rectangle::new(
Point::new(i * approx_width, 1), Point::new(i * approx_width, 1),
Point::new((i+1) * approx_width, pc_bounds.y)).clip_with(pc_clip).unwrap(); Point::new((i + 1) * approx_width, pc_bounds.y),
)
.clip_with(pc_clip)
.unwrap();
if i > 0 { if i > 0 {
self.gam.draw_line(pc, self.gam
Line::new_with_style( .draw_line(
Point::new(i * approx_width, 1), pc,
Point::new( i * approx_width, pc_bounds.y), Line::new_with_style(
DrawStyle { fill_color: None, stroke_color: Some(PixelColor::Dark), stroke_width: 1 } Point::new(i * approx_width, 1),
)).expect("couldn't draw dividing lines in prediction area"); Point::new(i * approx_width, pc_bounds.y),
DrawStyle {
fill_color: None,
stroke_color: Some(PixelColor::Dark),
stroke_width: 1,
},
),
)
.expect("couldn't draw dividing lines in prediction area");
} }
let mut p_tv = TextView::new(pc, let mut p_tv = TextView::new(pc, TextBounds::BoundingBox(p_clip));
TextBounds::BoundingBox(p_clip));
p_tv.draw_border = false; p_tv.draw_border = false;
p_tv.border_width = 1; p_tv.border_width = 1;
p_tv.clear_area = false; p_tv.clear_area = false;
@ -684,7 +811,8 @@ fn main() -> ! {
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
// only one public connection allowed: GAM // only one public connection allowed: GAM
let imef_sid = xns.register_name(ime_plugin_api::SERVER_NAME_IME_FRONT, Some(1)).expect("can't register server"); let imef_sid =
xns.register_name(ime_plugin_api::SERVER_NAME_IME_FRONT, Some(1)).expect("can't register server");
log::trace!("registered with NS -- {:?}", imef_sid); log::trace!("registered with NS -- {:?}", imef_sid);
let mut tracker = InputTracker::new(&xns); let mut tracker = InputTracker::new(&xns);
@ -706,13 +834,17 @@ fn main() -> ! {
let descriptor = buffer.to_original::<ImefDescriptor, _>().unwrap(); let descriptor = buffer.to_original::<ImefDescriptor, _>().unwrap();
if let Some(input) = descriptor.input_canvas { if let Some(input) = descriptor.input_canvas {
if debug1 || dbgcanvas {info!("got input canvas {:?}", input);} if debug1 || dbgcanvas {
info!("got input canvas {:?}", input);
}
tracker.set_input_canvas(input); tracker.set_input_canvas(input);
} else { } else {
tracker.clear_input_canvas(); tracker.clear_input_canvas();
} }
if let Some(pred) = descriptor.prediction_canvas { if let Some(pred) = descriptor.prediction_canvas {
if debug1 || dbgcanvas {info!("got prediction canvas {:?}", pred);} if debug1 || dbgcanvas {
info!("got prediction canvas {:?}", pred);
}
tracker.set_pred_canvas(pred); tracker.set_pred_canvas(pred);
} else { } else {
tracker.clear_pred_canvas(); tracker.clear_pred_canvas();
@ -730,23 +862,26 @@ fn main() -> ! {
if let Some(s) = descriptor.predictor { if let Some(s) = descriptor.predictor {
match xns.request_connection_with_token(s.as_str().unwrap()) { match xns.request_connection_with_token(s.as_str().unwrap()) {
Ok((pc, token)) => { Ok((pc, token)) => {
let pred = ime_plugin_api::PredictionPlugin {connection: Some(pc)}; let pred = ime_plugin_api::PredictionPlugin { connection: Some(pc) };
match pred.acquire(descriptor.predictor_token) { match pred.acquire(descriptor.predictor_token) {
Ok(confirmation) => { Ok(confirmation) => {
api_token = Some(ApiToken { api_token = Some(ApiToken {
api_token: confirmation, api_token: confirmation,
gam_token: descriptor.token, gam_token: descriptor.token,
}); });
}, }
Err(e) => log::error!("Internal error: {:?}", e), Err(e) => log::error!("Internal error: {:?}", e),
} }
tracker.set_predictor( Some(pred) ); tracker.set_predictor(Some(pred));
tracker.predictor_conn = Some( tracker.predictor_conn = Some((
(xous_ipc::String::<64>::from_str(s.as_str().unwrap()), xous_ipc::String::<64>::from_str(s.as_str().unwrap()),
token.expect("didn't get the disconnect token!")) token.expect("didn't get the disconnect token!"),
); ));
}, }
_ => error!("can't find predictive engine {}, retaining existing one.", s.as_str().unwrap()), _ => error!(
"can't find predictive engine {}, retaining existing one.",
s.as_str().unwrap()
),
} }
} }
log::debug!("predictor: {:?}, api_token: {:?}", tracker.get_predictor(), api_token); log::debug!("predictor: {:?}, api_token: {:?}", tracker.get_predictor(), api_token);
@ -778,20 +913,33 @@ fn main() -> ! {
if keys[0] == '😊' { if keys[0] == '😊' {
tracker.activate_emoji(); tracker.activate_emoji();
} else { } else {
if let Some(line) = tracker.update(keys, false, api_token.as_ref().unwrap().api_token).expect("couldn't update input tracker with latest key presses") { if let Some(line) = tracker
if dbglistener{info!("sending listeners {:?}", line);} .update(keys, false, api_token.as_ref().unwrap().api_token)
.expect("couldn't update input tracker with latest key presses")
{
if dbglistener {
info!("sending listeners {:?}", line);
}
if let Some(conn) = listener { if let Some(conn) = listener {
if dbglistener{info!("sending to conn {:?}", conn);} if dbglistener {
let buf = Buffer::into_buf(line).or(Err(xous::Error::InternalError)).unwrap(); info!("sending to conn {:?}", conn);
}
let buf =
Buffer::into_buf(line).or(Err(xous::Error::InternalError)).unwrap();
match buf.send(conn, ImefCallback::GotInputLine.to_u32().unwrap()) { match buf.send(conn, ImefCallback::GotInputLine.to_u32().unwrap()) {
Err(xous::Error::ServerNotFound) => { Err(xous::Error::ServerNotFound) => {
listener = None; // the listener went away, free up our slot so a new one can register listener = None; // the listener went away, free up our slot so a new one can register
}, }
Ok(xous::Result::Ok) => {}, Ok(xous::Result::Ok) => {}
Ok(xous::Result::MemoryReturned(offset, valid)) => { Ok(xous::Result::MemoryReturned(offset, valid)) => {
// ignore anything that's returned, but note it in case we're debugging // ignore anything that's returned, but note it in case we're
log::trace!("memory was returned in callback: offset {:?}, valid {:?}", offset, valid); // debugging
}, log::trace!(
"memory was returned in callback: offset {:?}, valid {:?}",
offset,
valid
);
}
Err(e) => { Err(e) => {
log::error!("unhandled error in callback processing: {:?}", e); log::error!("unhandled error in callback processing: {:?}", e);
} }
@ -812,7 +960,9 @@ fn main() -> ! {
if tracker.is_init() && api_token.is_some() { if tracker.is_init() && api_token.is_some() {
let force = if arg != 0 { true } else { false }; let force = if arg != 0 { true } else { false };
tracker.clear_area().expect("can't initially clear areas"); tracker.clear_area().expect("can't initially clear areas");
tracker.update(['\u{0000}'; 4], force, api_token.as_ref().unwrap().api_token).expect("can't setup initial screen arrangement"); tracker
.update(['\u{0000}'; 4], force, api_token.as_ref().unwrap().api_token)
.expect("can't setup initial screen arrangement");
} else { } else {
log::trace!("got redraw, but we're not initialized"); log::trace!("got redraw, but we're not initialized");
// ignore keyboard events until we've fully initialized // ignore keyboard events until we've fully initialized
@ -825,8 +975,13 @@ fn main() -> ! {
tracker.set_menu_mode(false); tracker.set_menu_mode(false);
} }
}), }),
Some(ImefOpcode::Quit) => {log::error!("recevied quit, goodbye!"); break;} Some(ImefOpcode::Quit) => {
None => {log::error!("couldn't convert opcode");} log::error!("recevied quit, goodbye!");
break;
}
None => {
log::error!("couldn't convert opcode");
}
} }
} }
log::trace!("main loop exit, destroying servers"); log::trace!("main loop exit, destroying servers");