From 75a6f779f8c98e9b491ffb27e2a55f8c0e028fe5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 5 Apr 2021 03:39:14 +0800 Subject: [PATCH] initial refactor of ime-frontend --- Cargo.lock | 3 +- RELEASE-v0.8.md | 6 +- services/ime-frontend/Cargo.toml | 8 +- services/ime-frontend/src/main.rs | 362 ++++++++++++++--------------- services/ime-plugin-api/src/lib.rs | 169 ++++++++------ xous-rs/src/names.rs | 6 +- xtask/src/main.rs | 2 +- 7 files changed, 282 insertions(+), 274 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 10e5c10e7..8f56d9691 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -561,11 +561,12 @@ dependencies = [ "blitstr-ref", "gam", "graphics-server", - "heapless", "ime-plugin-api", "keyboard", "log", "log-server", + "num-derive", + "num-traits", "rkyv 0.4.3", "ticktimer-server", "utralib", diff --git a/RELEASE-v0.8.md b/RELEASE-v0.8.md index 60f658f07..fa4d749cc 100644 --- a/RELEASE-v0.8.md +++ b/RELEASE-v0.8.md @@ -257,7 +257,7 @@ impl Drop for MyServer { fn drop(&mut self) { // if we have callbacks, destroy the callback server if let Some(sid) = self.callback_sid.take() { - // no need to tell the pstream server we're quitting: the next time a callback processes, + // no need to tell the upstream server we're quitting: the next time a callback processes, // it will automatically remove my entry as it will receive a ServerNotFound error. // tell my handler thread to quit @@ -316,7 +316,7 @@ fn xmain() -> ! { }), Some(Opcode::ExampleMemory) => { let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; - let rms = buffer.to_flat::().unwrap(); + let rms = buffer.as_flat::().unwrap(); // we can now access fields in rms without copying the data // use to_original if you need to invoke methods on the struct object } @@ -381,7 +381,7 @@ fn do_callback(cb_conns: &mut [Option; 32]) { *maybe_conn = None // automatically de-allocate callbacks for clients that have dropped }, Ok(xous::Result::Ok) => {} - _ => panic!("unhandled error or result in callback processing"); + _ => panic!("unhandled error or result in callback processing") } } } diff --git a/services/ime-frontend/Cargo.toml b/services/ime-frontend/Cargo.toml index ce342d869..b0cb738f2 100644 --- a/services/ime-frontend/Cargo.toml +++ b/services/ime-frontend/Cargo.toml @@ -9,7 +9,6 @@ version = "0.1.0" blitstr-ref = {path = "../../imports/blitstr-ref"} gam = {path = "../gam"} graphics-server = {path = "../graphics-server"} -heapless = "0.5.6" ime-plugin-api = {path = "../ime-plugin-api"} keyboard = {path = "../keyboard"} log = "0.4" @@ -19,10 +18,9 @@ xous = {path = "../../xous-rs"} xous-ipc = {path = "../../xous-ipc"} xous-names = {path = "../xous-names"} -[dependencies.rkyv] -default-features = false -features = ["const_generics"] -version = "0.4.1" +num-derive = {version = "0.3", default-features = false} +num-traits = {version = "0.2", default-features = false} +rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = {path = "../../utralib"} diff --git a/services/ime-frontend/src/main.rs b/services/ime-frontend/src/main.rs index 7daf48847..2991fa160 100644 --- a/services/ime-frontend/src/main.rs +++ b/services/ime-frontend/src/main.rs @@ -3,24 +3,19 @@ use gam::api::SetCanvasBoundsRequest; -use ime_plugin_api::ImefOpcode; - -use core::convert::TryFrom; -use core::fmt::Write; +use ime_plugin_api::{ImefOpcode, ImefCallback}; use log::{error, info}; use graphics_server::{Gid, Line, PixelColor, Point, Rectangle, TextBounds, TextView, DrawStyle}; use blitstr_ref as blitstr; -use core::pin::Pin; use ime_plugin_api::{PredictionTriggers, PredictionPlugin, PredictionApi}; -use rkyv::Deserialize; -use rkyv::archived_value; +use num_traits::{ToPrimitive,FromPrimitive}; +use xous_ipc::{String, Buffer}; +use xous::{CID, msg_scalar_unpack}; -use heapless::Vec; -use heapless::consts::*; -use xous_ipc::String; +use core::fmt::Write; /// max number of prediction options to track/render const MAX_PREDICTION_OPTIONS: usize = 4; @@ -84,7 +79,7 @@ impl InputTracker { 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")); + .expect("InputTracker failed to get prediction triggers from plugin")); } pub fn set_input_canvas(&mut self, input: Gid) { self.input_canvas = Some(input); @@ -98,7 +93,7 @@ impl InputTracker { 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"); + let pc_bounds: Point = gam::get_canvas_bounds(self.gam_conn, pc).expect("Couldn't get prediction canvas bounds"); gam::draw_rectangle(self.gam_conn, pc, Rectangle::new_with_style(Point::new(0, 0), pc_bounds, DrawStyle { @@ -106,7 +101,7 @@ impl InputTracker { stroke_color: None, stroke_width: 0 } - )).expect("IMEF: can't clear prediction area"); + )).expect("can't clear prediction area"); // add the border line on top gam::draw_line(self.gam_conn, pc, Line::new_with_style( @@ -117,11 +112,11 @@ impl InputTracker { stroke_color: Some(PixelColor::Dark), stroke_width: 1, }) - ).expect("IMEF: can't draw prediction top border"); + ).expect("can't draw prediction top border"); } if let Some(ic) = self.input_canvas { - let ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, ic).expect("IMEF: Couldn't get input canvas bounds"); + let ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, ic).expect("Couldn't get input canvas bounds"); gam::draw_rectangle(self.gam_conn, ic, Rectangle::new_with_style(Point::new(0, 0), ic_bounds, DrawStyle { @@ -129,7 +124,7 @@ impl InputTracker { stroke_color: None, stroke_width: 0 } - )).expect("IMEF: can't clear input area"); + )).expect("can't clear input area"); // add the border line on top gam::draw_line(self.gam_conn, ic, @@ -141,7 +136,7 @@ impl InputTracker { stroke_color: Some(PixelColor::Dark), stroke_width: 1, })) - .expect("IMEF: can't draw input top line border"); + .expect("can't draw input top line border"); } Ok(()) @@ -176,7 +171,7 @@ impl InputTracker { } // now push the characters in the predicted options onto the line for c in pred_str.as_str().unwrap().chars() { - self.line.push(c).expect("IMEF: ran out of space inserting prediction"); + self.line.push(c).expect("ran out of space inserting prediction"); chars += 1; } // forward until we find the next prediction trigger in the original string @@ -185,7 +180,7 @@ impl InputTracker { if trigger.whitespace && c.is_ascii_whitespace() || trigger.punctuation && c.is_ascii_punctuation() { // include the trigger that was found - self.line.push(c).expect("IMEF: ran out of space inserting prediction"); + self.line.push(c).expect("ran out of space inserting prediction"); chars += 1; break; } @@ -197,14 +192,14 @@ impl InputTracker { self.last_trigger_char = Some(chars); // copy the remainder of the line, if any while let Some(c) = c_iter.next() { - self.line.push(c).expect("IMEF: ran out of space inserting prediction"); + self.line.push(c).expect("ran out of space inserting prediction"); chars += 1; } self.characters = chars; } else { // just append the prediction to the line for c in pred_str.as_str().unwrap().chars() { - self.line.push(c).expect("IMEF: ran out of space inserting prediction"); + self.line.push(c).expect("ran out of space inserting prediction"); self.characters += 1; } self.last_trigger_char = Some(self.insertion); @@ -218,8 +213,8 @@ impl InputTracker { let mut update_predictor = false; let mut retstring: Option> = None; if let Some(ic) = self.input_canvas { - if debug1{info!("IMEF: updating input area");} - let ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, ic).expect("IMEF: Couldn't get input canvas bounds"); + if debug1{info!("updating input area");} + let ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, 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))); input_tv.draw_border = false; @@ -228,12 +223,12 @@ impl InputTracker { let mut do_redraw = false; for &k in newkeys.iter() { - if debug1{info!("IMEF: got key '{}'", k);} + if debug1{info!("got key '{}'", k);} match k { '\u{0000}' => (), '←' => { // move insertion point back if self.insertion > 0 { - info!("IMEF: moving insertion point back"); + info!("moving insertion point back"); self.insertion -= 1; } do_redraw = true; @@ -286,7 +281,7 @@ impl InputTracker { } '\u{0008}' => { // backspace if (self.characters > 0) && (self.insertion == self.characters) { - if debug1{info!("IMEF: simple backspace case")} + if debug1{info!("simple backspace case")} self.line.pop(); self.characters -= 1; self.insertion -= 1; @@ -294,14 +289,14 @@ impl InputTracker { if let Some(predictor) = self.predictor { if self.can_unpick { - predictor.unpick().expect("IMEF: couldn't unpick last prediction"); + predictor.unpick().expect("couldn't unpick last prediction"); self.can_unpick = false; update_predictor = true; } self.pred_phrase.clear(); } } else if (self.characters > 0) && (self.insertion > 0) { - if debug1{info!("IMEF: mid-string backspace case")} + 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 tempbytes: [u8; 4000] = self.line.as_bytes(); @@ -327,7 +322,7 @@ impl InputTracker { } } if debug1{info!("copying char {}", c);} - self.line.push(c).expect("IMEF: ran out of space inserting orignial characters into input line"); + self.line.push(c).expect("ran out of space inserting orignial characters into input line"); } i += 1; } @@ -340,20 +335,20 @@ impl InputTracker { } '\u{000d}' => { // carriage return let mut ret = String::<4000>::new(); - write!(ret, "{}", self.line.as_str().expect("IMEF: couldn't convert input line")).expect("IMEF: couldn't copy input ilne to output"); + write!(ret, "{}", self.line.as_str().expect("couldn't convert input line")).expect("couldn't copy input ilne to output"); retstring = Some(ret); if let Some(trigger) = self.pred_triggers { if trigger.newline { - self.predictor.unwrap().feedback_picked(self.line).expect("IMEF: couldn't send feedback to predictor"); + self.predictor.unwrap().feedback_picked(self.line).expect("couldn't send feedback to predictor"); } else if trigger.punctuation { - self.predictor.unwrap().feedback_picked(self.pred_phrase).expect("IMEF: couldn't send feedback to predictor"); + self.predictor.unwrap().feedback_picked(self.pred_phrase).expect("couldn't send feedback to predictor"); } } self.can_unpick = false; self.pred_phrase.clear(); - if debug1{info!("IMEF: got carriage return");} + if debug1{info!("got carriage return");} self.line.clear(); self.last_trigger_char = Some(0); // clear all the temporary variables @@ -365,22 +360,22 @@ impl InputTracker { requested: Point::new(0, 0), // size 0 will snap to the original smallest default size granted: None, }; - if debug1{info!("IMEF: attempting resize to {:?}", req.requested);} - gam::set_canvas_bounds_request(self.gam_conn, &mut req).expect("IMEF: couldn't call set_bounds_request on input area overflow"); + if debug1{info!("attempting resize to {:?}", req.requested);} + gam::set_canvas_bounds_request(self.gam_conn, &mut req).expect("couldn't call set_bounds_request on input area overflow"); if debug1{ - info!("IMEF: carriage return resize to {:?}", req.granted); + info!("carriage return resize to {:?}", req.granted); } self.last_height = 0; self.was_grown = false; } - self.clear_area().expect("IMEF: can't clear on carriage return"); + 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(self.pred_phrase).expect("IMEF: couldn't send feedback to predictor"); + self.predictor.unwrap().feedback_picked(self.pred_phrase).expect("couldn't send feedback to predictor"); self.pred_phrase.clear(); self.can_unpick = true; update_predictor = true; @@ -389,7 +384,7 @@ impl InputTracker { } if trigger.punctuation && k.is_ascii_punctuation() { if self.pred_phrase.len() > 0 { - self.predictor.unwrap().feedback_picked(self.pred_phrase).expect("IMEF: couldn't send feedback to predictor"); + self.predictor.unwrap().feedback_picked(self.pred_phrase).expect("couldn't send feedback to predictor"); self.pred_phrase.clear(); self.can_unpick = true; update_predictor = true; @@ -398,11 +393,11 @@ impl InputTracker { } } if self.insertion == self.characters { - self.line.push(k).expect("IMEF: ran out of space pushing character into input line"); + self.line.push(k).expect("ran out of space pushing character into input line"); if let Some(trigger) = self.pred_triggers { if !(trigger.punctuation && k.is_ascii_punctuation() || trigger.whitespace && k.is_ascii_whitespace() ) { - self.pred_phrase.push(k).expect("IMEF: ran out of space pushing character into prediction phrase"); + self.pred_phrase.push(k).expect("ran out of space pushing character into prediction phrase"); update_predictor = true; } } @@ -421,7 +416,7 @@ impl InputTracker { // goes back to appending words at the end of the sentence } - if debug1{info!("IMEF: handling case of inserting characters. insertion: {}", self.insertion)}; + 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 tempbytes: [u8; 4000] = self.line.as_bytes(); @@ -435,11 +430,11 @@ impl InputTracker { if debug1{info!("checking index {}", i);} if i == self.insertion { if debug1{info!("inserting char {}", k);} - self.line.push(k).expect("IMEF: ran out of space inserting new character into input line"); - self.line.push(c).expect("IMEF: ran out of space inserting orignial characters into input line"); + self.line.push(k).expect("ran out of space inserting new character into input line"); + self.line.push(c).expect("ran out of space inserting orignial characters into input line"); } else { if debug1{info!("copying char {}", c);} - self.line.push(c).expect("IMEF: ran out of space inserting orignial characters into input line"); + self.line.push(c).expect("ran out of space inserting orignial characters into input line"); } i += 1; } @@ -452,15 +447,15 @@ impl InputTracker { } input_tv.insertion = Some(self.insertion as _); - if debug1{info!("IMEF: 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 { - 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"); - if debug1{info!("IMEF: got computed cursor of {:?}", input_tv.cursor);} + write!(input_tv.text, "{}", self.line.as_str().expect("couldn't convert str")).expect("couldn't update TextView string in input canvas"); + gam::post_textview(self.gam_conn, &mut input_tv).expect("can't draw input TextView"); + 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 if input_tv.cursor.line_height == 0 && self.characters > 0 { - if debug1{info!("IMEF: 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 { @@ -471,19 +466,19 @@ impl InputTracker { requested: Point::new(0, ic_bounds.y + delta as i16), granted: None, }; - if debug1{info!("IMEF: attempting resize to {:?}", req.requested);} - gam::set_canvas_bounds_request(self.gam_conn, &mut req).expect("IMEF: couldn't call set_bounds_request on input area overflow"); - self.clear_area().expect("IMEF: couldn't clear area after resize"); + if debug1{info!("attempting resize to {:?}", req.requested);} + gam::set_canvas_bounds_request(self.gam_conn, &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!("IMEF: 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_computed = None; - gam::post_textview(self.gam_conn, &mut input_tv).expect("IMEF: can't draw input TextView"); + gam::post_textview(self.gam_conn, &mut input_tv).expect("can't draw input TextView"); }, - _ => info!("IMEF: 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; @@ -494,24 +489,24 @@ 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 if let Some(pc) = self.pred_canvas { - if debug1{info!("IMEF: updating prediction area");} - let pc_bounds: Point = gam::get_canvas_bounds(self.gam_conn, pc).expect("IMEF: Couldn't get prediction canvas bounds"); + if debug1{info!("updating prediction area");} + let pc_bounds: Point = gam::get_canvas_bounds(self.gam_conn, 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!("IMEF: got pc_bound {:?}", pc_bounds);} + if debug1{info!("got pc_bound {:?}", pc_bounds);} if update_predictor { if self.pred_phrase.len() > 0 { if let Some(pred) = self.predictor { - pred.set_input(self.pred_phrase).expect("IMEF: couldn't update predictor with current input"); + pred.set_input(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() { - self.pred_options[i] = pred.get_prediction(i as u32).expect("IMEF: couldn't query prediction engine"); + self.pred_options[i] = pred.get_prediction(i as u32).expect("couldn't query prediction engine"); } } } @@ -531,14 +526,14 @@ impl InputTracker { empty_tv.draw_border = false; empty_tv.border_width = 1; empty_tv.clear_area = true; - write!(empty_tv.text, "Ready for input...").expect("IMEF: couldn't set up empty TextView"); - if debug_canvas { info!("IMEF: pc canvas {:?}", pc) } - gam::post_textview(self.gam_conn, &mut empty_tv).expect("IMEF: can't draw prediction TextView"); + write!(empty_tv.text, "Ready for input...").expect("couldn't set up empty TextView"); + if debug_canvas { info!("pc canvas {:?}", pc) } + gam::post_textview(self.gam_conn, &mut empty_tv).expect("can't draw prediction TextView"); } else if update_predictor { // alright, first, let's clear the area - gam::draw_rectangle(self.gam_conn, pc, pc_clip).expect("IMEF: couldn't clear predictor area"); + gam::draw_rectangle(self.gam_conn, pc, pc_clip).expect("couldn't clear predictor area"); - if debug1{info!("IMEF: 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; @@ -555,7 +550,7 @@ impl InputTracker { 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("IMEF: couldn't draw dividing lines in prediction area"); + )).expect("couldn't draw dividing lines in prediction area"); } let mut p_tv = TextView::new(pc, TextBounds::BoundingBox(p_clip)); @@ -564,159 +559,156 @@ impl InputTracker { p_tv.clear_area = false; p_tv.ellipsis = true; p_tv.style = blitstr::GlyphStyle::Small; - write!(p_tv.text, "{}", pred_str).expect("IMEF: can't write the prediction string"); - gam::post_textview(self.gam_conn, &mut p_tv).expect("IMEF: couldn't post prediction text"); + write!(p_tv.text, "{}", pred_str).expect("can't write the prediction string"); + gam::post_textview(self.gam_conn, &mut p_tv).expect("couldn't post prediction text"); i += 1; } } } } - gam::redraw(self.gam_conn).expect("IMEF: couldn't redraw screen"); + gam::redraw(self.gam_conn).expect("couldn't redraw screen"); Ok(retstring) } } +// we have to store this connection state somewhere, either in the lib side or the local side +static mut CB_TO_MAIN_CONN: Option = None; +fn handle_keyevents(keys: [char; 4]) { + if let Some(cb_to_main_conn) = unsafe{CB_TO_MAIN_CONN} { + xous::send_message(cb_to_main_conn, + xous::Message::new_scalar(ImefOpcode::ProcessKeys.to_usize().unwrap(), + keys[0] as u32 as usize, + keys[1] as u32 as usize, + keys[2] as u32 as usize, + keys[3] as u32 as usize, + )).unwrap(); + } +} + #[xous::xous_main] fn xmain() -> ! { let debug1 = false; let dbglistener = false; let dbgcanvas = false; log_server::init_wait().unwrap(); - info!("IMEF: my PID is {}", xous::process::id()); + info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); - let imef_sid = xns.register_name(xous::names::SERVER_NAME_IME_FRONT).expect("IMEF: can't register server"); - info!("IMEF: registered with NS -- {:?}", imef_sid); + let imef_sid = xns.register_name(ime_plugin_api::SERVER_NAME_IME_FRONT).expect("can't register server"); + log::trace!("registered with NS -- {:?}", imef_sid); - let kbd_conn = xns.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"); + // hook the keyboard event server and have it forward keys to our local main loop + unsafe{CB_TO_MAIN_CONN = Some(xous::connect(imef_sid).unwrap())}; + let mut kbd = keyboard::Keyboard::new(&xns).expect("can't connect to KBD"); + kbd.hook_keyboard_events(handle_keyevents).unwrap(); let mut tracker = InputTracker::new( - xns.request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("IMEF: can't connect to GAM"), + xns.request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("can't connect to GAM"), ); - let mut listeners: Vec = Vec::new(); + let mut listeners: [Option; 32] = [None; 32]; // The main lop can't start until we've been assigned Gids from the GAM, and a Predictor. - info!("IMEF: waiting for my canvas Gids"); + info!("waiting for my canvas Gids"); + let mut init_done = false; loop { - let envelope = xous::receive_message(imef_sid).unwrap(); - if let Ok(opcode) = ImefOpcode::try_from(&envelope.body) { - match opcode { - ImefOpcode::SetInputCanvas(g) => { - if debug1 || dbgcanvas {info!("IMEF: got input canvas {:?}", g);} + let msg = xous::receive_message(imef_sid).unwrap(); + log::trace!("Message: {:?}", msg); + match FromPrimitive::from_usize(msg.body.id()) { + Some(ImefOpcode::SetInputCanvas) => { + msg_scalar_unpack!(msg, g0, g1, g2, g3, { + let g = Gid::new([g0 as _, g1 as _, g2 as _, g3 as _]); + if debug1 || dbgcanvas {info!("got input canvas {:?}", g);} tracker.set_input_canvas(g); - }, - ImefOpcode::SetPredictionCanvas(g) => { - if debug1 || dbgcanvas {info!("IMEF: got prediction canvas {:?}", g);} - tracker.set_pred_canvas(g); - }, - _ => info!("IMEF: expected canvas Gid, but got {:?}", opcode) + }); } - } 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::(&bytes, m.id as usize) - }; - match &*value { - rkyv::Archived::::SetPredictionServer(rkyv_s) => { - let s: String<256> = rkyv_s.deserialize(&mut xous_ipc::XousDeserializer {}).unwrap(); - 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")), - } - }, - rkyv::Archived::::RegisterListener(registration) => { - let s: String<256> = registration.deserialize(&mut xous_ipc::XousDeserializer {}).unwrap(); - if dbglistener{info!("IMEF: registering listener {:?}", s);} - // note second copy down below, this is put in early-init because we're likely to get these requests early on - let cid = xns.request_connection_blocking(s.as_str().expect("IMEF: can't decode RegisterListener string")) - .expect("IMEF: can't connect to requested listener for reporting events"); - if dbglistener{info!("IMEF: listener with cid {:?}", cid);} - listeners.push(cid).expect("IMEF: probably ran out of slots for input event reporting"); - }, - _ => panic!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage") - }; - } else { - info!("IMEF: expected canvas Gid, but got other message first {:?}", envelope); - } - if tracker.is_init() { - break; - } - } - - // force a redraw of the UI - if debug1{info!("IMEF: forcing initial UI redraw");} - tracker.clear_area().expect("IMEF: can't initially clear areas"); - tracker.update(['\u{0000}'; 4]).expect("IMEF: can't setup initial screen arrangement"); - - info!("IMEF: entering main loop"); - loop { - let envelope = xous::receive_message(imef_sid).unwrap(); - if debug1{info!("IMEF: got message {:?}", envelope);} - if let Ok(opcode) = ImefOpcode::try_from(&envelope.body) { - match opcode { - ImefOpcode::SetInputCanvas(g) => { - // there are valid reasons for this to happen, but it should be rare. - info!("IMEF: warning: input canvas Gid has been reset"); - tracker.set_input_canvas(g); - }, - ImefOpcode::SetPredictionCanvas(g) => { - // there are valid reasons for this to happen, but it should be rare. - info!("IMEF: warning: prediction canvas Gid has been reset"); + Some(ImefOpcode::SetPredictionCanvas) => { + msg_scalar_unpack!(msg, g0, g1, g2, g3, { + let g = Gid::new([g0 as _, g1 as _, g2 as _, g3 as _]); + if debug1 || dbgcanvas {info!("got prediction canvas {:?}", g);} tracker.set_pred_canvas(g); - }, - _ => info!("IMEF: unhandled opcode {:?}", opcode) + }); } - } 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::(&bytes, m.id as usize) - }; - match &*value { - rkyv::Archived::::SetPredictionServer(rkyv_s) => { - let s: String<256> = rkyv_s.deserialize(&mut xous_ipc::XousDeserializer {}).unwrap(); - 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")), + Some(ImefOpcode::SetPredictionServer) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let s = buffer.as_flat::, _>().unwrap(); + match xns.request_connection(s.as_str()) { + Ok(pc) => tracker.set_predictor(ime_plugin_api::PredictionPlugin {connection: Some(pc)}), + _ => error!("can't find predictive engine {}, retaining existing one.", s.as_str()), + } + } + Some(ImefOpcode::RegisterListener) => msg_scalar_unpack!(msg, sid0, sid1, sid2, sid3, { + let sid = xous::SID::from_u32(sid0 as _, sid1 as _, sid2 as _, sid3 as _); + let cid = Some(xous::connect(sid).unwrap()); + let mut found = false; + for entry in listeners.iter_mut() { + if *entry == None { + *entry = cid; + found = true; + break; } - }, - rkyv::Archived::::RegisterListener(registration) => { - let s: String<256> = registration.deserialize(&mut xous_ipc::XousDeserializer {}).unwrap(); - if dbglistener{info!("IMEF: registering listener {:?}", s);} - // note first copy above, put in early-init because we're likely to get these requests early on - let cid = xns.request_connection_blocking(s.as_str().expect("IMEF: can't decode RegisterListener string")) - .expect("IMEF: can't connect to requested listener for reporting events"); - if dbglistener{info!("IMEF: listener with cid {:?}", cid);} - listeners.push(cid).expect("IMEF: probably ran out of slots for input event reporting"); - }, - _ => panic!("IMEF: 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) => { - if let Some(line) = tracker.update(keys).expect("IMEF: couldn't update input tracker with latest key presses") { - if dbglistener{info!("IMEF: sending listeners {:?}", line);} - for &conn in listeners.iter() { - let op = ImefOpcode::GotInputLine(line); - let mut writer = rkyv::ser::serializers::BufferSerializer::new(xous::XousBuffer::new(4096)); + } + if !found { + error!("RegisterCallback listener ran out of space registering callback"); + } + }), + Some(ImefOpcode::ProcessKeys) => { + if init_done { + msg_scalar_unpack!(msg, k1, k2, k3, k4, { + let keys = [ + if let Some(a) = core::char::from_u32(k1 as u32) { + a + } else { + '\u{0000}' + }, + if let Some(a) = core::char::from_u32(k2 as u32) { + a + } else { + '\u{0000}' + }, + if let Some(a) = core::char::from_u32(k3 as u32) { + a + } else { + '\u{0000}' + }, + if let Some(a) = core::char::from_u32(k4 as u32) { + a + } else { + '\u{0000}' + }, + ]; + if let Some(line) = tracker.update(keys).expect("couldn't update input tracker with latest key presses") { + if dbglistener{info!("sending listeners {:?}", line);} + let buf = Buffer::into_buf(line).or(Err(xous::Error::InternalError)).unwrap(); - use rkyv::ser::Serializer; - let pos = writer.serialize_value(&op).expect("IMEF: couldn't archive line for sending"); - if dbglistener{info!("IMEF: sending to conn {:?}", conn);} - writer.into_inner().lend(conn, pos as u32).expect("IMEF: couldn't send input line to listener"); + for maybe_conn in listeners.iter_mut() { + if let Some(conn) = maybe_conn { + if dbglistener{info!("sending to conn {:?}", conn);} + match buf.lend(*conn, ImefCallback::GotInputLine.to_u32().unwrap()) { + Err(xous::Error::ServerNotFound) => { + *maybe_conn = None // automatically de-allocate callbacks for clients that have dropped + }, + Ok(xous::Result::Ok) => {} + _ => panic!("unhandled error or result in callback processing") + } + } + } } - } - }, - _ => error!("IMEF: received KBD event opcode that wasn't expected"), + }); + } else { + // ignore keyboard events until we've fully initialized + } } - } else { - info!("IMEF: expected canvas Gid, but got {:?}", envelope); + None => log::error!("couldn't convert opcode") + } + if !init_done && tracker.is_init() { + init_done = true; + // force a redraw of the UI + if debug1{info!("forcing initial UI redraw");} + tracker.clear_area().expect("can't initially clear areas"); + tracker.update(['\u{0000}'; 4]).expect("can't setup initial screen arrangement"); } } - } diff --git a/services/ime-plugin-api/src/lib.rs b/services/ime-plugin-api/src/lib.rs index 44901f8e8..b798b51bf 100644 --- a/services/ime-plugin-api/src/lib.rs +++ b/services/ime-plugin-api/src/lib.rs @@ -2,7 +2,7 @@ use xous::{Message, CID, send_message}; use xous_ipc::{Buffer, String}; -use num_traits::ToPrimitive; +use num_traits::{ToPrimitive, FromPrimitive}; #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub struct Prediction { @@ -188,105 +188,122 @@ pub enum ImefOpcode { /// register a listener for finalized inputs RegisterListener, //(String<64>), + /// internal for passing keyboard events from the keyboard callback + ProcessKeys, + // this is the event opcode used by listeners //GotInputLine, //(String<4000>), } -#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -pub(crate) enum ImefReturn { - GotInputLine(String<4000>), - Failure, +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum ImefCallback { + GotInputLine, //(String<4000>), + Drop, } -/* -impl core::convert::TryFrom<& Message> for ImefOpcode { - type Error = &'static str; - fn try_from(message: & Message) -> Result { - match message { - Message::Scalar(m) => match m.id { - 0 => Ok(ImefOpcode::SetInputCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))), - 1 => Ok(ImefOpcode::SetPredictionCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))), - _ => Err("IMEF api: unknown Scalar ID"), - }, - _ => Err("IMEF api: unhandled message type"), - } - } -} - -impl Into for ImefOpcode { - fn into(self) -> Message { - match self { - ImefOpcode::SetInputCanvas(gid) => Message::Scalar(ScalarMessage { - id: 0, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _ - }), - ImefOpcode::SetPredictionCanvas(gid) => Message::Scalar(ScalarMessage { - id: 1, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _ - }), - _ => panic!("IMEF api: Opcode type not handled by into()"), - } - } -}*/ - pub trait ImeFrontEndApi { fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error>; fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error>; fn set_predictor(&self, servername: &str) -> Result<(), xous::Error>; - fn register_listener(&self, servername: &str) -> Result<(), xous::Error>; + fn hook_listener_callback(&mut self, cb: fn(String::<4000>)) -> Result<(), xous::Error>; } +pub const SERVER_NAME_IME_FRONT: &str = "_IME front end_"; +static mut INPUT_CB: Option)> = None; + pub struct ImeFrontEnd { - pub connection: Option, + cid: CID, + callback_sid: Option, +} +impl ImeFrontEnd { + pub fn new(xns: &xous_names::XousNames) -> Result { + let conn = xns.request_connection_blocking(SERVER_NAME_IME_FRONT).expect("Can't connect to IMEF"); + Ok(ImeFrontEnd { + cid: conn, + callback_sid: None, + }) + } +} +impl Drop for ImeFrontEnd { + fn drop(&mut self) { + if let Some(sid) = self.callback_sid.take() { + // no need to tell the pstream server we're quitting: the next time a callback processes, + // it will automatically remove my entry as it will receive a ServerNotFound error. + + // tell my handler thread to quit + let cid = xous::connect(sid).unwrap(); + xous::send_message(cid, + Message::new_blocking_scalar(ImefCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); + unsafe{xous::disconnect(cid).unwrap();} + xous::destroy_server(sid).unwrap(); + } + + unsafe{xous::disconnect(self.cid).unwrap();} + } } impl ImeFrontEndApi for ImeFrontEnd { fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> { - match self.connection { - Some(cid) => { - send_message(cid, - Message::new_scalar(ImefOpcode::SetInputCanvas.to_usize().unwrap(), - g.gid()[0] as _, g.gid()[1] as _, g.gid()[2] as _, g.gid()[3] as _ - ))?; - Ok(()) - }, - _ => Err(xous::Error::UseBeforeInit) - } + send_message(self.cid, + Message::new_scalar(ImefOpcode::SetInputCanvas.to_usize().unwrap(), + g.gid()[0] as _, g.gid()[1] as _, g.gid()[2] as _, g.gid()[3] as _ + ))?; + Ok(()) } fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> { - match self.connection { - Some(cid) => { - send_message(cid, - Message::new_scalar(ImefOpcode::SetPredictionCanvas.to_usize().unwrap(), - g.gid()[0] as _, g.gid()[1] as _, g.gid()[2] as _, g.gid()[3] as _ - ))?; - Ok(()) - }, - _ => Err(xous::Error::UseBeforeInit) - } + send_message(self.cid, + Message::new_scalar(ImefOpcode::SetPredictionCanvas.to_usize().unwrap(), + g.gid()[0] as _, g.gid()[1] as _, g.gid()[2] as _, g.gid()[3] as _ + ))?; + Ok(()) } fn set_predictor(&self, servername: &str) -> Result<(), xous::Error> { - match self.connection { - Some(cid) => { - let mut server = String::<64>::new(); - use core::fmt::Write; - write!(server, "{}", servername).expect("couldn't write set_predictor server name"); - let buf = Buffer::into_buf(server).or(Err(xous::Error::InternalError))?; - buf.lend(cid, ImefOpcode::SetPredictionServer.to_u32().unwrap()).map(|_|()) - }, - _ => Err(xous::Error::UseBeforeInit) - } + let mut server = String::<64>::new(); + use core::fmt::Write; + write!(server, "{}", servername).expect("couldn't write set_predictor server name"); + let buf = Buffer::into_buf(server).or(Err(xous::Error::InternalError))?; + buf.lend(self.cid, ImefOpcode::SetPredictionServer.to_u32().unwrap()).map(|_|()) } - fn register_listener(&self, servername: &str) -> Result<(), xous::Error> { - match self.connection { - Some(cid) => { - let mut server = String::<64>::new(); - use core::fmt::Write; - write!(server, "{}", servername).expect("couldn't write set_predictor server name"); - let buf = Buffer::into_buf(server).or(Err(xous::Error::InternalError))?; - buf.lend(cid, ImefOpcode::RegisterListener.to_u32().unwrap()).map(|_|()) - }, - _ => Err(xous::Error::UseBeforeInit) + fn hook_listener_callback(&mut self, cb: fn(String::<4000>)) -> Result<(), xous::Error> { + if unsafe{INPUT_CB}.is_some() { + return Err(xous::Error::MemoryInUse) // can't hook it twice } + unsafe{INPUT_CB = Some(cb)}; + if self.callback_sid.is_none() { + let sid = xous::create_server().unwrap(); + self.callback_sid = Some(sid); + let sid_tuple = sid.to_u32(); + xous::create_thread_4(callback_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap(); + xous::send_message(self.cid, + Message::new_scalar(ImefOpcode::RegisterListener.to_usize().unwrap(), + sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize + )).unwrap(); + } + Ok(()) } } + +/// handles callback messages from server, in the library user's process space. +fn callback_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { + let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32); + loop { + let msg = xous::receive_message(sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + Some(ImefCallback::GotInputLine) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let inputline = buffer.to_original::, _>().unwrap(); + unsafe { + if let Some(cb) = INPUT_CB { + cb(inputline) + } + } + }, + Some(ImefCallback::Drop) => { + break; // this exits the loop and kills the thread + } + None => (), + } + } +} \ No newline at end of file diff --git a/xous-rs/src/names.rs b/xous-rs/src/names.rs index c5df4c476..9569882f9 100644 --- a/xous-rs/src/names.rs +++ b/xous-rs/src/names.rs @@ -2,15 +2,15 @@ /* A list of descriptions of servers ("names") that are used as keys by the xous-names server -for the core set of kernel services. +to refer to servers that are not statically bound into the kernel */ +pub const SERVER_NAME_IME_PLUGIN_SHELL: &str = "_IME shell plugin_"; + pub const SERVER_NAME_LLIO: &str = "_Low Level I/O manager_"; pub const SERVER_NAME_SHELL: &str = "_Shell_"; pub const SERVER_NAME_GAM: &str = "_Graphical Abstraction Manager_"; pub const SERVER_NAME_STATUS: &str = "_Status bar manager_"; -pub const SERVER_NAME_IME_FRONT: &str = "_IME front end_"; -pub const SERVER_NAME_IME_PLUGIN_SHELL: &str = "_IME shell plugin_"; pub const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_"; pub const SERVER_NAME_RTC: &str = "_Real time clock application server_"; diff --git a/xtask/src/main.rs b/xtask/src/main.rs index f11aae997..b3cbcbb5b 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -39,7 +39,7 @@ fn try_main() -> Result<(), DynError> { let hw_pkgs = [ //"gam", //"shellchat", - //"ime-frontend", + "ime-frontend", "ime-plugin-shell", "graphics-server", "ticktimer-server",