mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
format services/ime-frontend
This commit is contained in:
parent
f1e04f189c
commit
43379c7ac4
2 changed files with 371 additions and 212 deletions
|
|
@ -1,20 +1,24 @@
|
|||
use gam::*;
|
||||
use ime_plugin_api::ImefOpcode;
|
||||
use num_traits::*;
|
||||
use xous_ipc::String;
|
||||
|
||||
use gam::*;
|
||||
|
||||
// imef_conn must come from outside the scope of the macro because of hygeine rules.
|
||||
macro_rules! emoji_item {
|
||||
($emoji: expr, $imef_conn: ident) => {
|
||||
($emoji:expr, $imef_conn:ident) => {
|
||||
MenuItem {
|
||||
name: String::from_str(&$emoji.to_string()),
|
||||
action_conn: Some($imef_conn),
|
||||
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,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// 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,
|
||||
None
|
||||
None,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,25 +2,20 @@
|
|||
#![cfg_attr(target_os = "none", no_main)]
|
||||
|
||||
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 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 log::{error, info};
|
||||
use num_traits::{FromPrimitive, ToPrimitive};
|
||||
#[cfg(feature = "tts")]
|
||||
use tts_frontend::*;
|
||||
use xous::{msg_scalar_unpack, CID};
|
||||
use xous_ipc::Buffer;
|
||||
|
||||
/// max number of prediction options to track/render
|
||||
const MAX_PREDICTION_OPTIONS: usize = 4;
|
||||
|
|
@ -29,7 +24,6 @@ struct InputTracker {
|
|||
/// connection for handling graphical update requests
|
||||
pub gam: gam::Gam,
|
||||
|
||||
|
||||
/// input area canvas, as given by the GAM
|
||||
input_canvas: Option<Gid>,
|
||||
/// prediction display area, as given by the GAM
|
||||
|
|
@ -40,22 +34,25 @@ struct InputTracker {
|
|||
/// our current prediction engine
|
||||
predictor: Option<PredictionPlugin>,
|
||||
/// 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
|
||||
pred_triggers: Option<PredictionTriggers>,
|
||||
/// set if we're in a state where a backspace should trigger an unpredict
|
||||
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
|
||||
/// 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>,
|
||||
|
||||
/// track the progress of our input line
|
||||
line: String,
|
||||
/// length of the line in *characters*, not bytes (which is what .len() returns), used to index char_locs
|
||||
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,
|
||||
/// last returned line height, which is used as a reference for growing the area when we run out of space
|
||||
last_height: u32,
|
||||
|
|
@ -72,7 +69,7 @@ struct InputTracker {
|
|||
}
|
||||
|
||||
impl InputTracker {
|
||||
pub fn new(xns: &xous_names::XousNames)-> InputTracker {
|
||||
pub fn new(xns: &xous_names::XousNames) -> InputTracker {
|
||||
InputTracker {
|
||||
gam: gam::Gam::new(&xns).unwrap(),
|
||||
input_canvas: None,
|
||||
|
|
@ -91,94 +88,112 @@ impl InputTracker {
|
|||
was_grown: false,
|
||||
pred_options: Default::default(),
|
||||
menu_mode: false,
|
||||
#[cfg(feature="tts")]
|
||||
#[cfg(feature = "tts")]
|
||||
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
|
||||
/// 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
|
||||
/// a context is swapped and when a predictor can run.
|
||||
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>) {
|
||||
self.predictor = predictor;
|
||||
if let Some(pred) = predictor {
|
||||
self.pred_triggers = Some(pred.get_prediction_triggers()
|
||||
.expect("InputTracker failed to get prediction triggers from plugin"));
|
||||
self.pred_triggers = Some(
|
||||
pred.get_prediction_triggers()
|
||||
.expect("InputTracker failed to get prediction triggers from plugin"),
|
||||
);
|
||||
}
|
||||
}
|
||||
pub fn get_predictor(&self) -> Option<PredictionPlugin> {
|
||||
self.predictor
|
||||
}
|
||||
pub fn set_input_canvas(&mut self, input: Gid) {
|
||||
self.input_canvas = Some(input);
|
||||
}
|
||||
|
||||
pub fn get_predictor(&self) -> Option<PredictionPlugin> { self.predictor }
|
||||
|
||||
pub fn set_input_canvas(&mut self, input: Gid) { self.input_canvas = Some(input); }
|
||||
|
||||
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 is_init(&self) -> bool {
|
||||
self.input_canvas.is_some() && self.pred_canvas.is_some() && self.predictor.is_some()
|
||||
}
|
||||
|
||||
pub fn activate_emoji(&self) {
|
||||
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> {
|
||||
if let Some(pc) = self.pred_canvas {
|
||||
let pc_bounds: Point = self.gam.get_canvas_bounds(pc).expect("Couldn't get prediction canvas bounds");
|
||||
self.gam.draw_rectangle(pc,
|
||||
Rectangle::new_with_style(Point::new(0, 0), pc_bounds,
|
||||
DrawStyle {
|
||||
fill_color: Some(PixelColor::Light),
|
||||
stroke_color: None,
|
||||
stroke_width: 0
|
||||
}
|
||||
)).expect("can't clear prediction area");
|
||||
let pc_bounds: Point =
|
||||
self.gam.get_canvas_bounds(pc).expect("Couldn't get prediction canvas bounds");
|
||||
self.gam
|
||||
.draw_rectangle(
|
||||
pc,
|
||||
Rectangle::new_with_style(
|
||||
Point::new(0, 0),
|
||||
pc_bounds,
|
||||
DrawStyle {
|
||||
fill_color: Some(PixelColor::Light),
|
||||
stroke_color: None,
|
||||
stroke_width: 0,
|
||||
},
|
||||
),
|
||||
)
|
||||
.expect("can't clear prediction area");
|
||||
// add the border line on top
|
||||
self.gam.draw_line(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("can't draw prediction top border");
|
||||
self.gam
|
||||
.draw_line(
|
||||
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("can't draw prediction top border");
|
||||
}
|
||||
|
||||
if let Some(ic) = self.input_canvas {
|
||||
let ic_bounds: Point = self.gam.get_canvas_bounds(ic).expect("Couldn't get input canvas bounds");
|
||||
self.gam.draw_rectangle(ic,
|
||||
Rectangle::new_with_style(Point::new(0, 0), ic_bounds,
|
||||
DrawStyle {
|
||||
fill_color: Some(PixelColor::Light),
|
||||
stroke_color: None,
|
||||
stroke_width: 0
|
||||
}
|
||||
)).expect("can't clear input area");
|
||||
self.gam
|
||||
.draw_rectangle(
|
||||
ic,
|
||||
Rectangle::new_with_style(
|
||||
Point::new(0, 0),
|
||||
ic_bounds,
|
||||
DrawStyle {
|
||||
fill_color: Some(PixelColor::Light),
|
||||
stroke_color: None,
|
||||
stroke_width: 0,
|
||||
},
|
||||
),
|
||||
)
|
||||
.expect("can't clear input area");
|
||||
|
||||
// add the border line on top
|
||||
self.gam.draw_line(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("can't draw input top line border");
|
||||
self.gam
|
||||
.draw_line(
|
||||
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("can't draw input top line border");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
@ -186,21 +201,27 @@ impl InputTracker {
|
|||
|
||||
fn insert_prediction(&mut self, index: usize) {
|
||||
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] {
|
||||
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 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();
|
||||
self.line.clear();
|
||||
let mut chars = 0;
|
||||
let mut c_iter = tempstr.chars();
|
||||
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;
|
||||
}
|
||||
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
|
||||
while let Some(c) = c_iter.next() {
|
||||
if let Some(trigger) = self.pred_triggers {
|
||||
if trigger.whitespace && c.is_ascii_whitespace() ||
|
||||
trigger.punctuation && c.is_ascii_punctuation() {
|
||||
// include the trigger that was found
|
||||
self.line.push(c);
|
||||
chars += 1;
|
||||
break;
|
||||
if trigger.whitespace && c.is_ascii_whitespace()
|
||||
|| trigger.punctuation && c.is_ascii_punctuation()
|
||||
{
|
||||
// include the trigger that was found
|
||||
self.line.push(c);
|
||||
chars += 1;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// 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> {
|
||||
let debug1= false;
|
||||
pub fn update(
|
||||
&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 retstring: Option<xous_ipc::String::<4000>> = None;
|
||||
let mut retstring: Option<xous_ipc::String<4000>> = None;
|
||||
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 mut input_tv = TextView::new(ic,
|
||||
TextBounds::BoundingBox(Rectangle::new(Point::new(0,1), ic_bounds)));
|
||||
let mut input_tv =
|
||||
TextView::new(ic, TextBounds::BoundingBox(Rectangle::new(Point::new(0, 1), ic_bounds)));
|
||||
input_tv.draw_border = false;
|
||||
input_tv.border_width = 1;
|
||||
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;
|
||||
for &k in newkeys.iter() {
|
||||
if debug1{info!("got key '{}'", k);}
|
||||
if debug1 {
|
||||
info!("got key '{}'", k);
|
||||
}
|
||||
match k {
|
||||
'\u{0000}' => (),
|
||||
'←' => { // move insertion point back
|
||||
'←' => {
|
||||
// move insertion point back
|
||||
if !self.menu_mode {
|
||||
if self.insertion > 0 {
|
||||
log::debug!("moving insertion point back");
|
||||
|
|
@ -314,14 +346,16 @@ impl InputTracker {
|
|||
do_redraw = true;
|
||||
self.pred_phrase.clear();
|
||||
self.can_unpick = false;
|
||||
// this means that when we resume typing after an edit, the predictor will set its insertion point
|
||||
// at the very end, not the space prior to the last word...
|
||||
// this means that when we resume typing after an edit, the predictor will set its
|
||||
// insertion point at the very end, not the space
|
||||
// prior to the last word...
|
||||
self.last_trigger_char = Some(self.characters);
|
||||
} else {
|
||||
return Ok(Some(xous_ipc::String::<4000>::from_str("↓")));
|
||||
}
|
||||
}
|
||||
'\u{0011}' => { // F1
|
||||
'\u{0011}' => {
|
||||
// F1
|
||||
if !self.menu_mode {
|
||||
self.insert_prediction(0);
|
||||
do_redraw = true;
|
||||
|
|
@ -330,7 +364,8 @@ impl InputTracker {
|
|||
do_redraw = true;
|
||||
}
|
||||
}
|
||||
'\u{0012}' => { // F2
|
||||
'\u{0012}' => {
|
||||
// F2
|
||||
if !self.menu_mode {
|
||||
self.insert_prediction(1);
|
||||
do_redraw = true;
|
||||
|
|
@ -339,7 +374,8 @@ impl InputTracker {
|
|||
do_redraw = true;
|
||||
}
|
||||
}
|
||||
'\u{0013}' => { // F3
|
||||
'\u{0013}' => {
|
||||
// F3
|
||||
if !self.menu_mode {
|
||||
self.insert_prediction(2);
|
||||
do_redraw = true;
|
||||
|
|
@ -348,7 +384,8 @@ impl InputTracker {
|
|||
do_redraw = true;
|
||||
}
|
||||
}
|
||||
'\u{0014}' => { // F4
|
||||
'\u{0014}' => {
|
||||
// F4
|
||||
if !self.menu_mode {
|
||||
self.insert_prediction(3);
|
||||
do_redraw = true;
|
||||
|
|
@ -357,11 +394,14 @@ impl InputTracker {
|
|||
do_redraw = true;
|
||||
}
|
||||
}
|
||||
'\u{0008}' => { // backspace
|
||||
#[cfg(feature="tts")]
|
||||
'\u{0008}' => {
|
||||
// backspace
|
||||
#[cfg(feature = "tts")]
|
||||
self.tts.tts_simple(t!("input.delete-tts", locales::LANG)).unwrap();
|
||||
if (self.characters > 0) && (self.insertion == self.characters) {
|
||||
if debug1{info!("simple backspace case")}
|
||||
if debug1 {
|
||||
info!("simple backspace case")
|
||||
}
|
||||
self.line.pop();
|
||||
self.characters -= 1;
|
||||
self.insertion -= 1;
|
||||
|
|
@ -374,12 +414,17 @@ impl InputTracker {
|
|||
update_predictor = true;
|
||||
}
|
||||
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) {
|
||||
if debug1{info!("mid-string backspace case")}
|
||||
// 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
|
||||
} else if (self.characters > 0) && (self.insertion > 0) {
|
||||
if debug1 {
|
||||
info!("mid-string backspace case")
|
||||
}
|
||||
// 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
|
||||
self.line.clear();
|
||||
|
|
@ -388,12 +433,17 @@ impl InputTracker {
|
|||
let mut i = 0; // character position
|
||||
let mut dest_chars = 0; // destination character position
|
||||
for c in tempstr.chars() {
|
||||
if debug1{info!("checking index {}", i);}
|
||||
if debug1 {
|
||||
info!("checking index {}", i);
|
||||
}
|
||||
if i == self.insertion - 1 {
|
||||
if debug1{info!("skipping char");}
|
||||
if debug1 {
|
||||
info!("skipping char");
|
||||
}
|
||||
} else {
|
||||
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 trigger.punctuation && c.is_ascii_punctuation() {
|
||||
self.last_trigger_char = Some(dest_chars);
|
||||
|
|
@ -401,7 +451,9 @@ impl InputTracker {
|
|||
self.last_trigger_char = Some(dest_chars);
|
||||
}
|
||||
}
|
||||
if debug1{info!("copying char {}", c);}
|
||||
if debug1 {
|
||||
info!("copying char {}", c);
|
||||
}
|
||||
self.line.push(c);
|
||||
}
|
||||
i += 1;
|
||||
|
|
@ -409,29 +461,39 @@ impl InputTracker {
|
|||
self.insertion -= 1;
|
||||
self.characters -= 1;
|
||||
do_redraw = true;
|
||||
if self.menu_mode { update_predictor = true; }
|
||||
if self.menu_mode {
|
||||
update_predictor = true;
|
||||
}
|
||||
} 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();
|
||||
write!(ret, "{}", self.line.as_str()).expect("couldn't copy input line to output");
|
||||
retstring = Some(ret);
|
||||
|
||||
if let Some(trigger) = self.pred_triggers {
|
||||
if trigger.newline {
|
||||
self.predictor.unwrap().feedback_picked(
|
||||
xous_ipc::String::<4000>::from_str(&self.line)).expect("couldn't send feedback to predictor");
|
||||
self.predictor
|
||||
.unwrap()
|
||||
.feedback_picked(xous_ipc::String::<4000>::from_str(&self.line))
|
||||
.expect("couldn't send feedback to predictor");
|
||||
} else if trigger.punctuation {
|
||||
self.predictor.unwrap().feedback_picked(
|
||||
xous_ipc::String::<4000>::from_str(&self.pred_phrase)).expect("couldn't send feedback to predictor");
|
||||
self.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.pred_phrase.clear();
|
||||
|
||||
if debug1{info!("got carriage return");}
|
||||
if debug1 {
|
||||
info!("got carriage return");
|
||||
}
|
||||
self.line.clear();
|
||||
self.last_trigger_char = Some(0);
|
||||
// clear all the temporary variables
|
||||
|
|
@ -439,14 +501,19 @@ impl InputTracker {
|
|||
self.insertion = 0;
|
||||
if self.was_grown {
|
||||
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,
|
||||
token_type: gam::TokenType::Gam,
|
||||
token: self.gam_token.unwrap(),
|
||||
};
|
||||
if debug1{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");
|
||||
if debug1{
|
||||
if debug1 {
|
||||
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");
|
||||
if debug1 {
|
||||
info!("carriage return resize to {:?}", req.granted);
|
||||
}
|
||||
self.last_height = 0;
|
||||
|
|
@ -454,13 +521,17 @@ impl InputTracker {
|
|||
}
|
||||
self.clear_area().expect("can't clear on carriage return");
|
||||
update_predictor = true;
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
if let Some(trigger) = self.pred_triggers {
|
||||
if trigger.whitespace && k.is_ascii_whitespace() {
|
||||
if self.pred_phrase.len() > 0 {
|
||||
self.predictor.unwrap().feedback_picked(
|
||||
xous_ipc::String::<4000>::from_str(&self.pred_phrase)).expect("couldn't send feedback to predictor");
|
||||
self.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.can_unpick = true;
|
||||
update_predictor = true;
|
||||
|
|
@ -468,8 +539,12 @@ impl InputTracker {
|
|||
self.last_trigger_char = Some(self.insertion);
|
||||
} else if trigger.punctuation && k.is_ascii_punctuation() {
|
||||
if self.pred_phrase.len() > 0 {
|
||||
self.predictor.unwrap().feedback_picked(
|
||||
xous_ipc::String::<4000>::from_str(&self.pred_phrase)).expect("couldn't send feedback to predictor");
|
||||
self.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.can_unpick = true;
|
||||
update_predictor = true;
|
||||
|
|
@ -478,7 +553,7 @@ impl InputTracker {
|
|||
}
|
||||
}
|
||||
if self.insertion == self.characters {
|
||||
#[cfg(feature="tts")]
|
||||
#[cfg(feature = "tts")]
|
||||
{
|
||||
if !k.is_ascii_whitespace() && !k.is_ascii_punctuation() {
|
||||
// this is disastisfying in how slow it is
|
||||
|
|
@ -487,8 +562,9 @@ impl InputTracker {
|
|||
}
|
||||
self.line.push(k);
|
||||
if let Some(trigger) = self.pred_triggers {
|
||||
if !(trigger.punctuation && k.is_ascii_punctuation() ||
|
||||
trigger.whitespace && k.is_ascii_whitespace() ) {
|
||||
if !(trigger.punctuation && k.is_ascii_punctuation()
|
||||
|| trigger.whitespace && k.is_ascii_whitespace())
|
||||
{
|
||||
self.pred_phrase.push(k);
|
||||
update_predictor = true;
|
||||
}
|
||||
|
|
@ -502,29 +578,40 @@ impl InputTracker {
|
|||
self.pred_phrase.clear();
|
||||
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
|
||||
// pressed, we need to reset the prediction trigger to the previous word, which we
|
||||
// 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
|
||||
// in order to do predictions on arbitrary words, every time the scroll keys
|
||||
// are pressed, we need to reset the
|
||||
// prediction trigger to the previous word, which we
|
||||
// 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)};
|
||||
// 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();
|
||||
if debug1 {
|
||||
info!("handling case of inserting characters. insertion: {}", self.insertion)
|
||||
};
|
||||
// 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
|
||||
self.line.clear();
|
||||
|
||||
// insert the character in the array
|
||||
let mut i = 0;
|
||||
for c in tempstr.chars() {
|
||||
if debug1{info!("checking index {}", i);}
|
||||
if debug1 {
|
||||
info!("checking index {}", i);
|
||||
}
|
||||
if i == self.insertion {
|
||||
if debug1{info!("inserting char {}", k);}
|
||||
if debug1 {
|
||||
info!("inserting char {}", k);
|
||||
}
|
||||
self.line.push(k);
|
||||
self.line.push(c);
|
||||
} else {
|
||||
if debug1{info!("copying char {}", c);}
|
||||
if debug1 {
|
||||
info!("copying char {}", c);
|
||||
}
|
||||
self.line.push(c);
|
||||
}
|
||||
i += 1;
|
||||
|
|
@ -533,20 +620,28 @@ impl InputTracker {
|
|||
self.characters += 1;
|
||||
do_redraw = true;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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");
|
||||
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 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 {
|
||||
self.last_height + 1 + 1 // 1 pixel allowance for interline space, plus 1 for fencepost
|
||||
} else {
|
||||
|
|
@ -558,19 +653,26 @@ impl InputTracker {
|
|||
token_type: gam::TokenType::Gam,
|
||||
token: self.gam_token.unwrap(),
|
||||
};
|
||||
if debug1{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");
|
||||
if debug1 {
|
||||
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");
|
||||
match req.granted {
|
||||
Some(bounds) => {
|
||||
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
|
||||
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;
|
||||
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 {
|
||||
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
|
||||
// just draw a rectangle for the prediction area for now
|
||||
// prediction area is drawn second because the area could be cleared on behalf of a resize of the text
|
||||
// box just draw a rectangle for the prediction area for now
|
||||
if let Some(pc) = self.pred_canvas {
|
||||
if debug1{info!("updating prediction area");}
|
||||
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!("updating prediction area");
|
||||
}
|
||||
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 self.pred_phrase.len() > 0 || self.menu_mode {
|
||||
if let Some(pred) = self.predictor {
|
||||
pred.set_input(
|
||||
xous_ipc::String::<4000>::from_str(&self.pred_phrase)).expect("couldn't update predictor with current input");
|
||||
pred.set_input(xous_ipc::String::<4000>::from_str(&self.pred_phrase))
|
||||
.expect("couldn't update predictor with current input");
|
||||
}
|
||||
}
|
||||
|
||||
// Query the prediction engine for the latest predictions
|
||||
if let Some(pred) = self.predictor {
|
||||
for i in 0..self.pred_options.len() {
|
||||
let p = if let Some(prediction) =
|
||||
pred.get_prediction(i as u32, api_token).expect("couldn't query prediction engine") {
|
||||
let p = if let Some(prediction) = pred
|
||||
.get_prediction(i as u32, api_token)
|
||||
.expect("couldn't query prediction engine")
|
||||
{
|
||||
Some(String::from(prediction.as_str().unwrap_or("UTF-8 Error")))
|
||||
} else {
|
||||
None
|
||||
|
|
@ -620,39 +731,55 @@ impl InputTracker {
|
|||
|
||||
let debug_canvas = false;
|
||||
if valid_predictions == 0 {
|
||||
let mut empty_tv = TextView::new(pc,
|
||||
TextBounds::BoundingBox(Rectangle::new(Point::new(0, 1), pc_bounds)));
|
||||
let mut empty_tv =
|
||||
TextView::new(pc, TextBounds::BoundingBox(Rectangle::new(Point::new(0, 1), pc_bounds)));
|
||||
empty_tv.draw_border = false;
|
||||
empty_tv.border_width = 1;
|
||||
empty_tv.clear_area = true;
|
||||
write!(empty_tv.text, "{}", t!("input.greeting", locales::LANG)).expect("couldn't set up empty TextView");
|
||||
if debug_canvas { info!("pc canvas {:?}", pc) }
|
||||
write!(empty_tv.text, "{}", t!("input.greeting", locales::LANG))
|
||||
.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");
|
||||
} else if update_predictor || force_redraw {
|
||||
// alright, first, let's clear the 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
|
||||
let approx_width = pc_bounds.x / valid_predictions as i16;
|
||||
|
||||
let mut i = 0;
|
||||
for p in self.pred_options.iter() {
|
||||
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(
|
||||
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 {
|
||||
self.gam.draw_line(pc,
|
||||
Line::new_with_style(
|
||||
Point::new(i * approx_width, 1),
|
||||
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");
|
||||
self.gam
|
||||
.draw_line(
|
||||
pc,
|
||||
Line::new_with_style(
|
||||
Point::new(i * approx_width, 1),
|
||||
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,
|
||||
TextBounds::BoundingBox(p_clip));
|
||||
let mut p_tv = TextView::new(pc, TextBounds::BoundingBox(p_clip));
|
||||
p_tv.draw_border = false;
|
||||
p_tv.border_width = 1;
|
||||
p_tv.clear_area = false;
|
||||
|
|
@ -684,7 +811,8 @@ fn main() -> ! {
|
|||
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
// 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);
|
||||
|
||||
let mut tracker = InputTracker::new(&xns);
|
||||
|
|
@ -706,13 +834,17 @@ fn main() -> ! {
|
|||
let descriptor = buffer.to_original::<ImefDescriptor, _>().unwrap();
|
||||
|
||||
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);
|
||||
} else {
|
||||
tracker.clear_input_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);
|
||||
} else {
|
||||
tracker.clear_pred_canvas();
|
||||
|
|
@ -730,23 +862,26 @@ fn main() -> ! {
|
|||
if let Some(s) = descriptor.predictor {
|
||||
match xns.request_connection_with_token(s.as_str().unwrap()) {
|
||||
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) {
|
||||
Ok(confirmation) => {
|
||||
api_token = Some(ApiToken {
|
||||
api_token: confirmation,
|
||||
gam_token: descriptor.token,
|
||||
});
|
||||
},
|
||||
}
|
||||
Err(e) => log::error!("Internal error: {:?}", e),
|
||||
}
|
||||
tracker.set_predictor( Some(pred) );
|
||||
tracker.predictor_conn = Some(
|
||||
(xous_ipc::String::<64>::from_str(s.as_str().unwrap()),
|
||||
token.expect("didn't get the disconnect token!"))
|
||||
);
|
||||
},
|
||||
_ => error!("can't find predictive engine {}, retaining existing one.", s.as_str().unwrap()),
|
||||
tracker.set_predictor(Some(pred));
|
||||
tracker.predictor_conn = Some((
|
||||
xous_ipc::String::<64>::from_str(s.as_str().unwrap()),
|
||||
token.expect("didn't get the disconnect token!"),
|
||||
));
|
||||
}
|
||||
_ => error!(
|
||||
"can't find predictive engine {}, retaining existing one.",
|
||||
s.as_str().unwrap()
|
||||
),
|
||||
}
|
||||
}
|
||||
log::debug!("predictor: {:?}, api_token: {:?}", tracker.get_predictor(), api_token);
|
||||
|
|
@ -778,20 +913,33 @@ fn main() -> ! {
|
|||
if keys[0] == '😊' {
|
||||
tracker.activate_emoji();
|
||||
} 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 dbglistener{info!("sending listeners {:?}", line);}
|
||||
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 dbglistener {
|
||||
info!("sending listeners {:?}", line);
|
||||
}
|
||||
if let Some(conn) = listener {
|
||||
if dbglistener{info!("sending to conn {:?}", conn);}
|
||||
let buf = Buffer::into_buf(line).or(Err(xous::Error::InternalError)).unwrap();
|
||||
if dbglistener {
|
||||
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()) {
|
||||
Err(xous::Error::ServerNotFound) => {
|
||||
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)) => {
|
||||
// ignore anything that's returned, but note it in case we're debugging
|
||||
log::trace!("memory was returned in callback: offset {:?}, valid {:?}", offset, valid);
|
||||
},
|
||||
// ignore anything that's returned, but note it in case we're
|
||||
// debugging
|
||||
log::trace!(
|
||||
"memory was returned in callback: offset {:?}, valid {:?}",
|
||||
offset,
|
||||
valid
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("unhandled error in callback processing: {:?}", e);
|
||||
}
|
||||
|
|
@ -812,7 +960,9 @@ fn main() -> ! {
|
|||
if tracker.is_init() && api_token.is_some() {
|
||||
let force = if arg != 0 { true } else { false };
|
||||
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 {
|
||||
log::trace!("got redraw, but we're not initialized");
|
||||
// ignore keyboard events until we've fully initialized
|
||||
|
|
@ -825,8 +975,13 @@ fn main() -> ! {
|
|||
tracker.set_menu_mode(false);
|
||||
}
|
||||
}),
|
||||
Some(ImefOpcode::Quit) => {log::error!("recevied quit, goodbye!"); break;}
|
||||
None => {log::error!("couldn't convert opcode");}
|
||||
Some(ImefOpcode::Quit) => {
|
||||
log::error!("recevied quit, goodbye!");
|
||||
break;
|
||||
}
|
||||
None => {
|
||||
log::error!("couldn't convert opcode");
|
||||
}
|
||||
}
|
||||
}
|
||||
log::trace!("main loop exit, destroying servers");
|
||||
|
|
|
|||
Loading…
Reference in a new issue