Merge pull request #44 from betrusted-io/hardware

IME front end basics
This commit is contained in:
bunnie 2021-03-06 01:13:17 +08:00 committed by GitHub
commit 87f60518b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 1170 additions and 371 deletions

17
Cargo.lock generated
View file

@ -390,7 +390,7 @@ dependencies = [
"com",
"graphics-server",
"heapless",
"ime-frontend",
"ime-plugin-api",
"keyboard",
"log",
"log-server",
@ -530,8 +530,12 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
name = "ime-frontend"
version = "0.1.0"
dependencies = [
"blitstr-ref",
"gam",
"graphics-server",
"heapless",
"ime-plugin-api",
"keyboard",
"log",
"log-server",
"rkyv",
@ -541,11 +545,22 @@ dependencies = [
"xous-names",
]
[[package]]
name = "ime-plugin-api"
version = "0.1.0"
dependencies = [
"graphics-server",
"log",
"rkyv",
"xous",
]
[[package]]
name = "ime-plugin-shell"
version = "0.1.0"
dependencies = [
"heapless",
"ime-plugin-api",
"log",
"log-server",
"rkyv",

View file

@ -9,8 +9,6 @@ members = [
"services/log-server",
"services/ticktimer-server",
"services/com",
"services/llio",
"services/trng-tester",
"services/fcc-agent",
"services/xous-names",
"services/keyboard",
@ -25,26 +23,30 @@ members = [
"xtask",
"imports/blitstr-ref",
"imports/com_rs-ref",
"services/ime-plugin-api",
"services/llio",
"services/trng-tester",
]
default-members = [
"services/shell",
"services/log-server",
"services/graphics-server",
"services/ticktimer-server",
"services/trng-tester",
"services/fcc-agent",
"services/xous-names",
"services/keyboard",
"services/com",
"services/llio",
"services/trng",
"services/gam",
"imports/blitstr-ref",
"imports/com_rs-ref",
"services/ime-plugin-api",
"services/benchmark",
"services/benchmark-target",
"services/ime-frontend",
"services/ime-plugin-shell",
"services/llio",
"services/trng-tester",
]
# These packages have custom RUSTFLAGS, so if they

View file

@ -19,7 +19,7 @@ heapless = "0.5"
log = "0.4"
com = { path = "../com"}
blitstr-ref = { path = "../../imports/blitstr-ref" }
ime-frontend = {path="../ime-frontend"}
ime-plugin-api = {path="../ime-plugin-api"}
[target.'cfg(not(any(windows,unix)))'.dependencies]

View file

@ -62,6 +62,7 @@ impl Canvas {
}
})
}
pub fn pan_offset(&self) -> Point { self.pan_offset }
pub fn clip_rect(&self) -> Rectangle { self.clip_rect }
pub fn gid(&self) -> Gid { self.gid }
pub fn trust_level(&self) -> u8 { self.trust_level }
@ -154,6 +155,7 @@ pub fn recompute_canvases(canvases: FnvIndexMap<Gid, Canvas, U32>, screen: Recta
let mut higher_clipregions: BinaryHeap<Canvas, U32, Max> = BinaryHeap::new();
let mut trust_level: u8 = 255;
// sorted_clipregions is a Max heap keyed on trust, so popping the elements off will return them sorted from most to least trusted
info!("CANVAS: received screen argument of {:?}", screen);
info!("CANVAS: now determining which regions are drawable");
loop {
if let Some(c) = sorted_clipregions.pop() {
@ -167,14 +169,14 @@ pub fn recompute_canvases(canvases: FnvIndexMap<Gid, Canvas, U32>, screen: Recta
}
if !clip_region.intersects(screen) {
drawable = false;
if debug { info!(" CANVAS: not drawable, does not intersect");}
if debug { info!(" * CANVAS: not drawable, does not intersect");}
} else { // short circuit this computation if it's not drawable because it's off screen
// note that this .iter() is *not* sorted by trust level, but all elements will be of greater than or equal to the current trust level
for &region in higher_clipregions.iter() {
// regions of the same trust level can draw over each other. Draw order is arbitrary.
if region.clip_rect().intersects(clip_region) && (region.trust_level() < trust_level) {
drawable = false;
if debug { info!(" CANVAS: not drawable, lower trust intersecting with higher trust region");}
if debug { info!(" * CANVAS: not drawable, lower trust intersecting with higher trust region");}
}
}
}

View file

@ -54,9 +54,12 @@ pub fn draw_line(gam_cid: xous::CID, gid: Gid, line: Line) -> Result<(), xous::E
}
pub fn get_canvas_bounds(gam_cid: xous::CID, gid: Gid) -> Result<Point, xous::Error> {
let debug1 = false;
if debug1{info!("GAM_API: get_canvas_bounds");}
let response = xous::send_message(gam_cid, api::Opcode::GetCanvasBounds(gid).into())?;
if let xous::Result::Scalar2(tl, br) = response {
// note that the result should always be normalized so the rectangle's "tl" should be (0,0)
if debug1{info!("GAM_API: tl:{}, br:{}", tl, br);}
assert!(tl == 0, "GAM_API: api call returned non-zero top left for canvas bounds");
Ok(br.into())
} else {

View file

@ -11,6 +11,7 @@ use canvas::*;
use blitstr_ref as blitstr;
use blitstr::GlyphStyle;
use graphics_server::*;
use ime_plugin_api::ImeFrontEndApi;
use log::info;
@ -69,6 +70,7 @@ fn add_chat_layout(gfx_conn: xous::CID, trng_conn: xous::CID, canvases: &mut Fnv
// get the height of various text regions to compute the layout
let small_height: i16 = graphics_server::glyph_height_hint(gfx_conn, GlyphStyle::Small).expect("GAM: couldn't get glyph height") as i16;
let regular_height: i16 = graphics_server::glyph_height_hint(gfx_conn, GlyphStyle::Regular).expect("GAM: couldn't get glyph height") as i16;
let margin = 4;
// allocate canvases in structures, and record their GID for future reference
let status_canvas = Canvas::new(
@ -78,14 +80,14 @@ fn add_chat_layout(gfx_conn: xous::CID, trng_conn: xous::CID, canvases: &mut Fnv
canvases.insert(status_canvas.gid(), status_canvas).expect("GAM: can't store status canvus");
let predictive_canvas = Canvas::new(
Rectangle::new_coords(0, screensize.y - regular_height, screensize.x, screensize.y),
Rectangle::new_coords(0, screensize.y - regular_height - margin*2, screensize.x, screensize.y),
254,
trng_conn, None
).expect("GAM: couldn't create predictive text canvas");
canvases.insert(predictive_canvas.gid(), predictive_canvas).expect("GAM: couldn't store predictive canvas");
let input_canvas = Canvas::new(
Rectangle::new_v_stack(predictive_canvas.clip_rect(), -regular_height),
Rectangle::new_v_stack(predictive_canvas.clip_rect(), -regular_height - margin*2),
254, trng_conn, None
).expect("GAM: couldn't create input text canvas");
canvases.insert(input_canvas.gid(), input_canvas).expect("GAM: couldn't store input canvas");
@ -136,18 +138,23 @@ fn xmain() -> ! {
xous::create_thread_simple(status_thread, chatlayout.status.gid()).expect("GAM: couldn't create status thread");
// connect to the IME front end, and set its canvas
/*
info!("GAM: acquiring connection to IMEF...");
let imef_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_FRONT).expect("GAM: can't connect to the IME front end");
ime_frontend::set_canvas(imef_conn, chatlayout.input).expect("GAM: couldn't set IMEF canvas");
*/
let imef = ime_plugin_api::ImeFrontEnd{ connection: Some(imef_conn), };
imef.set_input_canvas(chatlayout.input).expect("GAM: couldn't set IMEF input canvas");
imef.set_prediction_canvas(chatlayout.predictive).expect("GAM: couldn't set IMEF prediction canvas");
// ASSUME: shell is our default application, so set a default predictor of Shell
imef.set_predictor(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("GAM: couldn't set IMEF prediction to shell");
// NOTE: all three API calls (set_input_canvas, set_prediction_canvas, set_predictor) are mandatory for IMEF initialization
let mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap();
info!("GAM: entering main loop");
loop {
let maybe_env = xous::try_receive_message(gam_sid).unwrap();
if debug1 { if maybe_env.is_some() {info!("GAM: Message: {:?}", maybe_env);} }
match maybe_env {
Some(envelope) => {
// let envelope = xous::receive_message(gam_sid).unwrap();
if debug1 {info!("GAM: Message: {:?}", envelope); }
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
if debug1 {info!("GAM: Opcode: {:?}", opcode);}
@ -170,7 +177,7 @@ fn xmain() -> ! {
match canvases.get(&gid) {
Some(c) => {
let mut rect = c.clip_rect();
rect.translate(rect.tl); // normalize to 0,0 coordinates
rect.normalize(); // normalize to 0,0 coordinates
xous::return_scalar2(envelope.sender,
rect.tl.into(),
rect.br.into(),
@ -214,8 +221,14 @@ fn xmain() -> ! {
// issue the draw command
graphics_server::draw_textview(gfx_conn, &mut tv_clone).expect("GAM: text view draw could not complete.");
// copy back the fields that we want to be mutable
if debug1{info!("GAM: got computed cursor of {:?}", tv_clone.cursor);}
tv.cursor = tv_clone.cursor;
tv.bounds_computed = tv_clone.bounds_computed;
// pack our data back into the buffer to return
use rkyv::Write;
let mut writer = rkyv::ArchiveBuffer::new(buf);
writer.archive(&api::Opcode::RenderTextView(tv)).expect("GAM: couldn't re-archive return value");
} else {
info!("GAM: attempt to draw TextView on non-drawable canvas. Not fatal, but request ignored.");
}
@ -242,17 +255,37 @@ fn xmain() -> ! {
// first, figure out if we should even be drawing to this canvas.
if canvas.is_drawable() {
match obj.obj {
GamObjectType::Line(line) => {
graphics_server::draw_line(gfx_conn, line).expect("GAM: couldn't draw line");
GamObjectType::Line(mut line) => {
line.translate(canvas.clip_rect().tl);
line.translate(canvas.pan_offset());
graphics_server::draw_line_clipped(gfx_conn,
line,
canvas.clip_rect(),
).expect("GAM: couldn't draw line");
},
GamObjectType::Circ(circ) => {
graphics_server::draw_circle(gfx_conn, circ).expect("GAM: couldn't draw circle");
GamObjectType::Circ(mut circ) => {
circ.translate(canvas.clip_rect().tl);
circ.translate(canvas.pan_offset());
graphics_server::draw_circle_clipped(gfx_conn,
circ,
canvas.clip_rect(),
).expect("GAM: couldn't draw circle");
},
GamObjectType::Rect(rect) => {
graphics_server::draw_rectangle(gfx_conn, rect).expect("GAM: couldn't draw rectangle");
GamObjectType::Rect(mut rect) => {
rect.translate(canvas.clip_rect().tl);
rect.translate(canvas.pan_offset());
graphics_server::draw_rectangle_clipped(gfx_conn,
rect,
canvas.clip_rect(),
).expect("GAM: couldn't draw rectangle");
},
GamObjectType::RoundRect(rr) => {
graphics_server::draw_rounded_rectangle(gfx_conn, rr).expect("GAM: couldn't draw rounded rectangle");
GamObjectType::RoundRect(mut rr) => {
rr.translate(canvas.clip_rect().tl);
rr.translate(canvas.pan_offset());
graphics_server::draw_rounded_rectangle_clipped(gfx_conn,
rr,
canvas.clip_rect(),
).expect("GAM: couldn't draw rounded rectangle");
}
}
} else {
@ -277,6 +310,10 @@ fn xmain() -> ! {
if let Ok(elapsed_time) = ticktimer_server::elapsed_ms(ticktimer_conn) {
if elapsed_time - last_time > 33 { // rate limit updates to 30fps
last_time = elapsed_time;
// TODO: enhance this update with a methods that:
// 1. deface dirty areas that require defacing
// 2. update the drawn dirty bits indicating things have been drawn, as appropriate
graphics_server::flush(gfx_conn).expect("GAM: couldn't flush buffer to screen");
}
}

View file

@ -99,8 +99,11 @@ pub enum Opcode {
/// gets info about the current glyph to assist with layout
QueryGlyphProps(GlyphStyle),
// draws a textview
/// draws a textview
DrawTextView(TextView),
/// draws an object that requires clipping
DrawClipObject(ClipObject),
}
impl core::convert::TryFrom<& Message> for Opcode {
@ -264,3 +267,18 @@ impl Into<Message> for Opcode {
}
}
}
#[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)]
pub enum ClipObjectType {
Line(Line),
Circ(Circle),
Rect(Rectangle),
RoundRect(RoundedRectangle),
}
#[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)]
pub struct ClipObject {
pub clip: Rectangle,
pub obj: ClipObjectType,
}

View file

@ -35,13 +35,13 @@ impl Rectangle {
}
}
// stack a new rectangle on top of the current one (same width)
// positive widths go *below*, negative go *above* in screen coordinate space. borders are non-overlapping.
pub fn new_v_stack(reference: Rectangle, width: i16) -> Rectangle {
if width >= 0 { // rectangle below
// positive heights go *below*, negative go *above* in screen coordinate space. borders are non-overlapping.
pub fn new_v_stack(reference: Rectangle, height: i16) -> Rectangle {
if height >= 0 { // rectangle below
Rectangle::new_coords(reference.tl.x, reference.br.y + 1,
reference.br.x, reference.br.y + width + 1)
reference.br.x, reference.br.y + height)
} else { // rectangle above
Rectangle::new_coords(reference.tl.x, reference.tl.y + width - 1,
Rectangle::new_coords(reference.tl.x, reference.tl.y + height,
reference.br.x, reference.tl.y - 1)
}
}
@ -64,17 +64,54 @@ impl Rectangle {
pub fn new_h_span(left: Rectangle, right: Rectangle) -> Rectangle {
Rectangle::new_coords(left.br.x + 1, left.tl.y, right.tl.x - 1, right.br.y)
}
pub fn tl(&self) -> Point { self.tl }
pub fn br(&self) -> Point { self.br }
pub fn bl(&self) -> Point {
Point {
x: self.tl.x,
y: self.br.y,
}
}
pub fn tr(&self) -> Point {
Point {
x: self.br.x,
y: self.tl.y,
}
}
pub fn intersects(&self, other: Rectangle) -> bool {
((other.tl.x >= self.tl.x) && (other.tl.x <= self.br.x)) &&
((other.tl.y >= self.tl.y) && (other.tl.y <= self.br.y))
||
((other.br.x >= self.tl.x) && (other.br.x <= self.br.x)) &&
((other.br.y >= self.tl.y) && (other.br.y <= self.br.y))
!(self.br.x < other.tl.x ||
self.tl.y > other.br.y ||
self.br.y < other.tl.y ||
self.tl.x > other.br.x)
}
pub fn intersects_point(&self, point: Point) -> bool {
((point.x >= self.tl.x) && (point.x <= self.br.x)) &&
((point.y >= self.tl.y) && (point.y <= self.br.y))
}
/// takes the current Rectangle, and clips it with a clipping Rectangle; returns a new rectangle as the result
pub fn clip_with(&self, clip: Rectangle) -> Option<Rectangle> {
// check to see if we even overlap; if not, don't do any computation
if !self.intersects(clip) {
return None;
}
let tl: Point = Point::new(
if self.tl.x < clip.tl.x {
clip.tl.x
} else { self.tl.x },
if self.tl.y < clip.tl.y {
clip.tl.y
} else { self.tl.y },
);
let br: Point = Point::new(
if self.br.x > clip.br.x {
clip.br.x
} else { self.br.x },
if self.br.y > clip.br.y {
clip.br.y
} else { self.br.y },
);
Some(Rectangle::new(tl, br))
}
pub fn new_coords_with_style(
x0: i16,
y0: i16,
@ -114,6 +151,32 @@ impl Rectangle {
self.tl.y += offset.y;
self.br.y += offset.y;
}
pub fn normalize(&mut self) {
self.br.x -= self.tl.x;
self.br.y -= self.tl.y;
self.tl.x = 0;
self.tl.y = 0;
}
// this "margins in" a rectangle on all sides; if the margin is more than the twice any
// dimension it just reduces the rectangle to a line at the midpoint of the axis dimension
pub fn margin(&mut self, margin: Point) {
if margin.x * 2 <= (self.br.x - self.tl.x) {
self.tl.x += margin.x;
self.br.x -= margin.x;
} else {
let midpoint = (self.br.x + self.tl.x) / 2;
self.tl.x = midpoint;
self.br.x = midpoint;
}
if margin.y * 2 <= (self.br.y - self.tl.y) {
self.tl.y += margin.y;
self.br.y -= margin.y;
} else {
let midpoint = (self.br.y + self.tl.y) / 2;
self.tl.y = midpoint;
self.br.y = midpoint;
}
}
/// Make a rectangle of the full screen size
pub fn full_screen() -> Rectangle {
@ -140,7 +203,6 @@ impl Into<ClipRect> for Rectangle {
}
}
//////////////////////////// LINE
#[derive(Debug, Clone, Copy, rkyv::Archive, rkyv::Unarchive)]
@ -167,6 +229,10 @@ impl Line {
style: style,
}
}
pub fn translate(&mut self, offset: Point) {
self.start = self.start + offset;
self.end = self.end + offset;
}
}
@ -196,6 +262,9 @@ impl Circle {
style,
}
}
pub fn translate(&mut self, offset: Point) {
self.center = self.center + offset;
}
}
//////////////////////// Rounded Rectangle
@ -219,4 +288,110 @@ impl RoundedRectangle {
radius: r_adj,
}
}
pub fn translate(&mut self, offset: Point) {
self.border.translate(offset);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn intersection_test() {
let a = Rectangle::new(
Point::new(0, 0,),
Point::new(100, 150));
assert!(a.intersects(a));
// br corner
assert!(a.intersects(Rectangle::new(
Point::new(50, 50),
Point::new(200, 200),
)));
// tl corner
assert!(a.intersects(Rectangle::new(
Point::new(-50, -50),
Point::new(50, 50),
)));
// tr corner
assert!(a.intersects(Rectangle::new(
Point::new(50, -50),
Point::new(250, 50),
)));
// bl corner
assert!(a.intersects(Rectangle::new(
Point::new(-50, 50),
Point::new(50, 250),
)));
// enclosed
assert!(a.intersects(Rectangle::new(
Point::new(-50, -50),
Point::new(250, 250),
)));
// enclosing
assert!(a.intersects(Rectangle::new(
Point::new(10, 10),
Point::new(20, 20),
)));
// left border
assert!(a.intersects(Rectangle::new(
Point::new(-10, 10),
Point::new(0, 20),
)));
// right border
assert!(a.intersects(Rectangle::new(
Point::new(100, 10),
Point::new(150, 20),
)));
// top border
assert!(a.intersects(Rectangle::new(
Point::new(-100, -10),
Point::new(150, 0),
)));
// bottom border
assert!(a.intersects(Rectangle::new(
Point::new(50, 150),
Point::new(60, 151),
)));
// within, bordering
assert!(a.intersects(Rectangle::new(
Point::new(0, 20),
Point::new(100, 50),
)));
// wider than, from above
assert!(a.intersects(Rectangle::new(
Point::new(-50, -50),
Point::new(300, 50)
)));
// wider than, from below
assert!(a.intersects(Rectangle::new(
Point::new(-50, 100),
Point::new(300, 300)
)));
// taller than, from left
assert!(a.intersects(Rectangle::new(
Point::new(-50, -50),
Point::new(20, 300)
)));
// taller than, from right
assert!(a.intersects(Rectangle::new(
Point::new(20, 50),
Point::new(300, 300)
)));
// to the left of
assert!(!a.intersects(Rectangle::new(
Point::new(-10, -10),
Point::new(-5, -5),
)));
// to the right of
assert!(!a.intersects(Rectangle::new(
Point::new(101, 0),
Point::new(150, 150),
)));
}
}

View file

@ -1,8 +1,9 @@
use utralib::generated::*;
use xous::MemoryRange;
use crate::api::Point;
const FB_WIDTH_WORDS: usize = 11;
// const FB_WIDTH_PIXELS: usize = 336;
const FB_WIDTH_PIXELS: usize = 336;
const FB_LINES: usize = 536;
const FB_SIZE: usize = FB_WIDTH_WORDS * FB_LINES; // 44 bytes by 536 lines
const CONFIG_CLOCK_FREQUENCY: u32 = 100_000_000;
@ -66,6 +67,8 @@ impl XousDisplay {
display
}
pub fn screen_size(&self) -> Point { Point::new(FB_WIDTH_PIXELS as i16, FB_LINES as i16) }
pub fn redraw(&mut self) {
while self.busy() {xous::yield_slice()}
let fb: *mut [u32; FB_SIZE] = self.fb.as_mut_ptr() as *mut [u32; FB_SIZE];

View file

@ -1,4 +1,5 @@
use minifb::{Key, Window, WindowOptions, KeyRepeat};
use crate::api::Point;
const WIDTH: usize = 336;
const HEIGHT: usize = 536;
@ -54,6 +55,8 @@ impl XousDisplay {
}
}
pub fn screen_size(&self) -> Point { Point::new(WIDTH as i16, HEIGHT as i16) }
pub fn blit_screen(&mut self, bmp: [u32; FB_SIZE]) {
for (dest, src) in self.emulated_buffer.iter_mut().zip(bmp.iter()) {
*dest = *src;

View file

@ -2,7 +2,7 @@
// pub mod size;
pub mod api;
pub use api::{Circle, DrawStyle, Line, PixelColor, Point, Rectangle, TextView, TextBounds, Gid, TextOp, RoundedRectangle};
pub use api::{Circle, DrawStyle, Line, PixelColor, Point, Rectangle, TextView, TextBounds, Gid, TextOp, RoundedRectangle, ClipObject, ClipObjectType};
use blitstr_ref as blitstr;
pub use blitstr::{ClipRect, Cursor, GlyphStyle};
use xous::String;
@ -10,6 +10,9 @@ pub mod op;
use xous::{send_message, CID};
use core::fmt::Write;
use rkyv::Write as RkyvWrite;
use rkyv::Unarchive;
pub fn draw_line(cid: CID, line: Line) -> Result<(), xous::Error> {
let m: xous::Message = api::Opcode::Line(line).into();
@ -116,10 +119,6 @@ pub fn glyph_height_hint(cid: CID, glyph: GlyphStyle) -> Result<usize, xous::Err
}
pub fn draw_textview(cid: CID, tv: &mut TextView) -> Result<(), xous::Error> {
use log::info;
use rkyv::Write;
use rkyv::Unarchive;
let rkyv_tv = api::Opcode::DrawTextView(*tv);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_tv).expect("couldn't archive textview");
@ -130,12 +129,74 @@ pub fn draw_textview(cid: CID, tv: &mut TextView) -> Result<(), xous::Error> {
let returned = unsafe { rkyv::archived_value::<api::Opcode>(xous_buffer.as_ref(), pos)};
if let rkyv::Archived::<api::Opcode>::DrawTextView(result) = returned {
let tvr: TextView = result.unarchive();
//log::info!("draw_textview: got cursor of {:?}, bounds of {:?}", tvr.cursor, tvr.bounds_computed);
tv.bounds_computed = tvr.bounds_computed;
tv.cursor = tvr.cursor;
Ok(())
} else {
let tvr = returned.unarchive();
info!("draw_textview saw an unhandled return type of {:?}", tvr);
log::info!("draw_textview saw an unhandled return type of {:?}", tvr);
Err(xous::Error::InternalError)
}
}
pub fn draw_line_clipped(cid: xous::CID, line: Line, clip: Rectangle) -> Result<(), xous::Error> {
let rkyv = api::Opcode::DrawClipObject(
ClipObject {
clip: clip,
obj: ClipObjectType::Line(line),
});
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv).expect("GFX_API: couldn't archive ClipObject");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("GFX_API: ClipObject operation failure");
Ok(())
}
pub fn draw_circle_clipped(cid: xous::CID, circ: Circle, clip: Rectangle) -> Result<(), xous::Error> {
let rkyv = api::Opcode::DrawClipObject(
ClipObject {
clip: clip,
obj: ClipObjectType::Circ(circ),
});
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv).expect("GFX_API: couldn't archive ClipObject");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("GFX_API: ClipObject operation failure");
Ok(())
}
pub fn draw_rectangle_clipped(cid: xous::CID, rect: Rectangle, clip: Rectangle) -> Result<(), xous::Error> {
let rkyv = api::Opcode::DrawClipObject(
ClipObject {
clip: clip,
obj: ClipObjectType::Rect(rect),
});
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv).expect("GFX_API: couldn't archive ClipObject");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("GFX_API: ClipObject operation failure");
Ok(())
}
pub fn draw_rounded_rectangle_clipped(cid: xous::CID, rr: RoundedRectangle, clip: Rectangle) -> Result<(), xous::Error> {
let rkyv = api::Opcode::DrawClipObject(
ClipObject {
clip: clip,
obj: ClipObjectType::RoundRect(rr),
});
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv).expect("GFX_API: couldn't archive ClipObject");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("GFX_API: ClipObject operation failure");
Ok(())
}

View file

@ -4,7 +4,7 @@
use log::{error, info};
mod api;
use api::Opcode;
use api::{Opcode, ClipObject, ClipObjectType};
use core::pin::Pin;
use rkyv::{archived_value, Unarchive, archived_value_mut};
@ -18,7 +18,7 @@ use core::convert::TryFrom;
mod logo;
use api::{DrawStyle, PixelColor, Rectangle, TextBounds, RoundedRectangle};
use api::{DrawStyle, PixelColor, Rectangle, TextBounds, RoundedRectangle, Point};
use blitstr_ref as blitstr;
mod fontmap;
@ -57,9 +57,13 @@ fn map_fonts() {
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = false;
log_server::init_wait().unwrap();
info!("GFX: my PID is {}", xous::process::id());
let sid = xous_names::register_name(xous::names::SERVER_NAME_GFX).expect("GFX: can't register server");
info!("GFX: Server listening on address {:?}", sid);
// Create a new monochrome simulator display.
let mut display = XousDisplay::new();
@ -71,9 +75,6 @@ fn xmain() -> ! {
let mut current_string_clip = blitstr::ClipRect::full_screen();
let mut current_cursor = blitstr::Cursor::from_top_left_of(current_string_clip);
let sid = xous_names::register_name(xous::names::SERVER_NAME_GFX).expect("GFX: can't register server");
info!("GFX: Server listening on address {:?}", sid);
if false {
// leave this test case around
// for some reason, the top right quadrant draws an extra pixel inside the fill area
@ -88,9 +89,11 @@ fn xmain() -> ! {
stroke_width: 1,
};
let rr = RoundedRectangle::new(r, 16);
op::rounded_rectangle(display.native_buffer(), rr);
op::rounded_rectangle(display.native_buffer(), rr, None);
}
let screen_clip = Rectangle::new(Point::new(0,0), display.screen_size());
display.redraw();
loop {
let msg = xous::receive_message(sid).unwrap();
@ -128,6 +131,24 @@ fn xmain() -> ! {
blitstr::xor_char
);
},
rkyv::Archived::<api::Opcode>::DrawClipObject(rco) => {
let obj: ClipObject = rco.unarchive();
if debug1{info!("GFX: DrawClipObject {:?}", obj);}
match obj.obj {
ClipObjectType::Line(line) => {
op::line(display.native_buffer(), line, Some(obj.clip));
},
ClipObjectType::Circ(circ) => {
op::circle(display.native_buffer(), circ, Some(obj.clip));
},
ClipObjectType::Rect(rect) => {
op::rectangle(display.native_buffer(), rect, Some(obj.clip));
},
ClipObjectType::RoundRect(rr) => {
op::rounded_rectangle(display.native_buffer(), rr, Some(obj.clip));
}
}
},
_ => panic!("GFX: invalid response from server -- corruption occurred in MemoryMessage")
};
} else if let xous::Message::MutableBorrow(m) = &msg.body {
@ -135,27 +156,22 @@ fn xmain() -> ! {
let value = unsafe {
archived_value_mut::<api::Opcode>(Pin::new(buf.as_mut()), m.id as usize)
};
//use rkyv::Write;
//let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let debugtv: bool = false;
match &*value {
rkyv::Archived::<api::Opcode>::DrawTextView(rtv) => {
let mut tv = rtv.unarchive();
if tv.clip_rect.is_none() { continue } // if no clipping rectangle is specified, nothing to draw
let screen_offset: Point = tv.clip_rect.unwrap().tl; // this is the translation vector to and from screen space
let paintfn = if tv.dry_run {
if debugtv { info!("GFX(TV): doing dry run"); }
blitstr::simulate_char
} else {
if debugtv { info!("GFX(TV): drawing"); }
if debugtv { info!("GFX(TV): doing live run"); }
blitstr::xor_char
};
/*
1. figure out text bounds
2. clear background, if requested
3. draw surrounding rectangle, if requested
4. draw text
*/
// first compute the bounding box, if it isn't computed
if tv.bounds_computed.is_none() {
match tv.bounds_hint {
@ -177,7 +193,10 @@ fn xmain() -> ! {
// clear the bounding box if requested
let mut clear_rect = tv.bounds_computed.unwrap();
clear_rect.translate(tv.clip_rect.unwrap().tl);
// move things into screen coordinates
clear_rect.translate(screen_offset);
let bordercolor = if tv.draw_border {
Some(PixelColor::Dark)
} else {
@ -201,37 +220,51 @@ fn xmain() -> ! {
};
if tv.rounded_border.is_some() {
op::rounded_rectangle(display.native_buffer(),
RoundedRectangle::new(clear_rect, tv.rounded_border.unwrap() as _));
RoundedRectangle::new(clear_rect, tv.rounded_border.unwrap() as _), tv.clip_rect);
} else {
if debugtv { info!("GFX(TV): clearing rectangle {:?}", clear_rect); }
op::rectangle(display.native_buffer(), clear_rect);
op::rectangle(display.native_buffer(), clear_rect, tv.clip_rect);
}
// this is the actual draw operation
clear_rect.translate(tv.margin);
/////// TODO: I think we need to clip the clear_rect.tl.x/y to within the screen area for this to be valid?
let mut ref_cursor = blitstr::Cursor{
pt: blitstr::Pt{x: clear_rect.tl.x as u32, y: clear_rect.tl.y as u32},
line_height: 0,
// compute the final clipping region for the string
clear_rect.margin(tv.margin);
let cr = match clear_rect.clip_with(screen_clip) {
Some(r) => r,
_ => continue, // don't draw anything if somehow this doesn't fit in the creen.
};
if debugtv { info!("GFX(TV): paint_str with {:?} | {:?} | {:?} | {:?}", clear_rect, ref_cursor, tv.style, tv.text); }
let mut ref_cursor = blitstr::Cursor::from_top_left_of(cr.into());
if debugtv { info!("GFX(TV): paint_str with {:?} | {:?} | {:?} | {:?}", cr, ref_cursor, tv.style, tv.text); }
blitstr::paint_str(
display.native_buffer(),
clear_rect.into(),
cr.into(),
&mut ref_cursor,
tv.style.into(),
tv.text.as_str().unwrap(),
false,
paintfn
);
tv.cursor = ref_cursor;
// translate the cursor return value back to canvas coordinates
tv.cursor = blitstr::Cursor {
pt: blitstr::Pt::new(
ref_cursor.pt.x - screen_offset.x as u32,
ref_cursor.pt.y - screen_offset.y as u32,
),
line_height: ref_cursor.line_height,
};
if debugtv{info!("GFX(TV): returning cursor of {:?}", tv.cursor);}
// pack our data back into the buffer to return
use rkyv::Write;
let mut writer = rkyv::ArchiveBuffer::new(buf);
writer.archive(&api::Opcode::DrawTextView(tv)).expect("GFX: couldn't re-archive return value");
},
_ => panic!("GFX: invalid mutable borrow message"),
}
};
} else if let Ok(opcode) = Opcode::try_from(&msg.body) {
// info!("GFX: Opcode: {:?}", opcode);
let debugop = false;
if debugop {info!("GFX: Opcode: {:?}", opcode);}
match opcode {
Opcode::Flush => {
display.update();
@ -240,19 +273,19 @@ fn xmain() -> ! {
Opcode::Clear => {
let mut r = Rectangle::full_screen();
r.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 0);
op::rectangle(display.native_buffer(), r)
op::rectangle(display.native_buffer(), r, None)
}
Opcode::Line(l) => {
op::line(display.native_buffer(), l);
op::line(display.native_buffer(), l, None);
}
Opcode::Rectangle(r) => {
op::rectangle(display.native_buffer(), r);
op::rectangle(display.native_buffer(), r, None);
}
Opcode::RoundedRectangle(rr) => {
op::rounded_rectangle(display.native_buffer(), rr);
op::rounded_rectangle(display.native_buffer(), rr, None);
}
Opcode::Circle(c) => {
op::circle(display.native_buffer(), c);
op::circle(display.native_buffer(), c, None);
}
Opcode::SetGlyphStyle(glyph) => {
current_glyph = glyph;
@ -270,7 +303,8 @@ fn xmain() -> ! {
current_string_clip = r;
}
Opcode::ScreenSize => {
xous::return_scalar2(msg.sender, 336 as usize, 536 as usize)
let pt = display.screen_size();
xous::return_scalar2(msg.sender, pt.x as usize, pt.y as usize)
.expect("GFX: couldn't return ScreenSize request");
}
Opcode::QueryGlyphStyle => {

View file

@ -32,7 +32,7 @@ fn put_pixel(fb: &mut LcdFB, x: i16, y: i16, color: PixelColor) {
fb[clip_y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000;
}
pub fn line(fb: &mut LcdFB, l: Line) {
pub fn line(fb: &mut LcdFB, l: Line, clip: Option<Rectangle>) {
let color: PixelColor;
if l.style.stroke_color.is_some() {
color = l.style.stroke_color.unwrap();
@ -52,7 +52,9 @@ pub fn line(fb: &mut LcdFB, l: Line) {
loop {
/* loop */
if x0 >= 0 && y0 >= 0 && x0 < (WIDTH as _) && y0 < (HEIGHT as _) {
put_pixel(fb, x0 as _, y0 as _, color);
if clip.is_none() || (clip.unwrap().intersects_point(Point::new(x0, y0))) {
put_pixel(fb, x0 as _, y0 as _, color);
}
}
if x0 == x1 && y0 == y1 {
break;
@ -79,6 +81,7 @@ pub struct CircleIterator {
radius: u16,
style: DrawStyle,
p: Point,
clip: Option<Rectangle>
}
impl Iterator for CircleIterator {
@ -98,26 +101,31 @@ impl Iterator for CircleIterator {
let outer_radius_sq = outer_radius * outer_radius;
loop {
let t = self.p;
let len = t.x * t.x + t.y * t.y;
let mut item = None;
let is_border = len > radius_sq - radius && len < outer_radius_sq + radius;
if self.clip.is_none() || // short-circuit evaluation makes this safe
(self.clip.unwrap().intersects_point(self.p)) {
let t = self.p;
let len = t.x * t.x + t.y * t.y;
let is_fill = len <= outer_radius_sq + 1;
let is_border = len > radius_sq - radius && len < outer_radius_sq + radius;
let item = if is_border && self.style.stroke_color.is_some() {
Some(Pixel(
self.center + t,
self.style.stroke_color.expect("Border color not defined"),
))
} else if is_fill && self.style.fill_color.is_some() {
Some(Pixel(
self.center + t,
self.style.fill_color.expect("Fill color not defined"),
))
} else {
None
};
let is_fill = len <= outer_radius_sq + 1;
item = if is_border && self.style.stroke_color.is_some() {
Some(Pixel(
self.center + t,
self.style.stroke_color.expect("Border color not defined"),
))
} else if is_fill && self.style.fill_color.is_some() {
Some(Pixel(
self.center + t,
self.style.fill_color.expect("Fill color not defined"),
))
} else {
None
};
}
self.p.x += 1;
@ -137,12 +145,13 @@ impl Iterator for CircleIterator {
}
}
pub fn circle(fb: &mut LcdFB, circle: Circle) {
pub fn circle(fb: &mut LcdFB, circle: Circle, clip: Option<Rectangle>) {
let c = CircleIterator {
center: circle.center,
radius: circle.radius as _,
style: circle.style,
p: Point::new(-(circle.radius as i16), -(circle.radius as i16)),
clip: clip,
};
for pixel in c {
@ -158,6 +167,7 @@ pub struct RectangleIterator {
bottom_right: Point,
style: DrawStyle,
p: Point,
clip: Option<Rectangle>,
}
impl Iterator for RectangleIterator {
@ -177,30 +187,33 @@ impl Iterator for RectangleIterator {
break None;
}
let border_width = self.style.stroke_width;
let tl = self.top_left;
let br = self.bottom_right;
if self.clip.is_none() || // short-circuit evaluation makes this safe
(self.clip.unwrap().intersects_point(self.p)) {
let border_width = self.style.stroke_width;
let tl = self.top_left;
let br = self.bottom_right;
// Border
if (
// Top border
(self.p.y >= tl.y && self.p.y < tl.y + border_width)
// Bottom border
|| (self.p.y <= br.y && self.p.y > br.y - border_width)
// Left border
|| (self.p.x >= tl.x && self.p.x < tl.x + border_width)
// Right border
|| (self.p.x <= br.x && self.p.x > br.x - border_width)
) && self.style.stroke_color.is_some()
{
out = Some(Pixel(
self.p,
self.style.stroke_color.expect("Expected stroke"),
));
}
// Fill
else if let Some(fill) = self.style.fill_color {
out = Some(Pixel(self.p, fill));
// Border
if (
// Top border
(self.p.y >= tl.y && self.p.y < tl.y + border_width)
// Bottom border
|| (self.p.y <= br.y && self.p.y > br.y - border_width)
// Left border
|| (self.p.x >= tl.x && self.p.x < tl.x + border_width)
// Right border
|| (self.p.x <= br.x && self.p.x > br.x - border_width)
) && self.style.stroke_color.is_some()
{
out = Some(Pixel(
self.p,
self.style.stroke_color.expect("Expected stroke"),
));
}
// Fill
else if let Some(fill) = self.style.fill_color {
out = Some(Pixel(self.p, fill));
}
}
self.p.x += 1;
@ -218,12 +231,13 @@ impl Iterator for RectangleIterator {
}
}
pub fn rectangle(fb: &mut LcdFB, rect: Rectangle) {
pub fn rectangle(fb: &mut LcdFB, rect: Rectangle, clip: Option<Rectangle>) {
let r = RectangleIterator {
top_left: rect.tl,
bottom_right: rect.br,
style: rect.style,
p: rect.tl,
clip: clip,
};
for pixel in r {
@ -248,6 +262,7 @@ pub struct QuadrantIterator {
style: DrawStyle,
p: Point,
quad: Quadrant,
clip: Option<Rectangle>,
}
impl Iterator for QuadrantIterator {
@ -267,26 +282,31 @@ impl Iterator for QuadrantIterator {
let outer_radius_sq = outer_radius * outer_radius;
loop {
let t = self.p;
let len = t.x * t.x + t.y * t.y;
let mut item = None;
if self.clip.is_none() || // short-circuit evaluation makes this safe
(self.clip.unwrap().intersects_point(self.p)) {
let is_border = len > (inner_radius_sq - inner_radius) && len < (outer_radius_sq + inner_radius);
let t = self.p;
let len = t.x * t.x + t.y * t.y;
let is_fill = len <= outer_radius_sq + 1;
let is_border = len > (inner_radius_sq - inner_radius) && len < (outer_radius_sq + inner_radius);
let item = if is_border && self.style.stroke_color.is_some() {
Some(Pixel(
self.center + t,
self.style.stroke_color.expect("Border color not defined"),
))
} else if is_fill && self.style.fill_color.is_some() {
Some(Pixel(
self.center + t,
self.style.fill_color.expect("Fill color not defined"),
))
} else {
None
};
let is_fill = len <= outer_radius_sq + 1;
item = if is_border && self.style.stroke_color.is_some() {
Some(Pixel(
self.center + t,
self.style.stroke_color.expect("Border color not defined"),
))
} else if is_fill && self.style.fill_color.is_some() {
Some(Pixel(
self.center + t,
self.style.fill_color.expect("Fill color not defined"),
))
} else {
None
};
}
self.p.x += 1;
@ -344,7 +364,7 @@ impl Iterator for QuadrantIterator {
}
}
pub fn quadrant(fb: &mut LcdFB, circle: Circle, quad: Quadrant) {
pub fn quadrant(fb: &mut LcdFB, circle: Circle, quad: Quadrant, clip: Option<Rectangle>) {
let starting_pixel = match quad {
Quadrant::TopLeft => {
Point::new( -(circle.radius as i16), -(circle.radius as i16))
@ -365,6 +385,7 @@ pub fn quadrant(fb: &mut LcdFB, circle: Circle, quad: Quadrant) {
style: circle.style,
p: starting_pixel,
quad: quad,
clip: clip,
};
for pixel in q {
@ -382,6 +403,7 @@ pub struct RoundedRectangleIterator {
style: DrawStyle,
radius: i16,
p: Point,
clip: Option<Rectangle>,
// the four quadrants for drawing the rounded corners
tlq: Rectangle,
trq: Rectangle,
@ -405,35 +427,39 @@ impl Iterator for RoundedRectangleIterator {
break None;
}
// suppress the output pixel if we happen to be in the corner quadrant area
if self.tlq.intersects_point(self.p) || self.trq.intersects_point(self.p) ||
self.blq.intersects_point(self.p) || self.brq.intersects_point(self.p) {
out = None
} else {
let border_width = self.style.stroke_width;
let tl = self.top_left;
let br = self.bottom_right;
if self.clip.is_none() || // short-circuit evaluation makes this safe
(self.clip.unwrap().intersects_point(self.p)) {
// Border
if (
// Top border
(self.p.y >= tl.y && self.p.y < tl.y + border_width)
// Bottom border
|| (self.p.y <= br.y && self.p.y > br.y - border_width)
// Left border
|| (self.p.x >= tl.x && self.p.x < tl.x + border_width)
// Right border
|| (self.p.x <= br.x && self.p.x > br.x - border_width)
) && self.style.stroke_color.is_some()
{
out = Some(Pixel(
self.p,
self.style.stroke_color.expect("Expected stroke"),
));
}
// Fill
else if let Some(fill) = self.style.fill_color {
out = Some(Pixel(self.p, fill));
// suppress the output pixel if we happen to be in the corner quadrant area
if self.tlq.intersects_point(self.p) || self.trq.intersects_point(self.p) ||
self.blq.intersects_point(self.p) || self.brq.intersects_point(self.p) {
out = None
} else {
let border_width = self.style.stroke_width;
let tl = self.top_left;
let br = self.bottom_right;
// Border
if (
// Top border
(self.p.y >= tl.y && self.p.y < tl.y + border_width)
// Bottom border
|| (self.p.y <= br.y && self.p.y > br.y - border_width)
// Left border
|| (self.p.x >= tl.x && self.p.x < tl.x + border_width)
// Right border
|| (self.p.x <= br.x && self.p.x > br.x - border_width)
) && self.style.stroke_color.is_some()
{
out = Some(Pixel(
self.p,
self.style.stroke_color.expect("Expected stroke"),
));
}
// Fill
else if let Some(fill) = self.style.fill_color {
out = Some(Pixel(self.p, fill));
}
}
}
@ -453,7 +479,7 @@ impl Iterator for RoundedRectangleIterator {
}
}
pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle) {
pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle, clip: Option<Rectangle>) {
// compute the four quadrants
// call the rr iterator on the rectangle
// then call it on one each of the four circle quadrants
@ -464,6 +490,7 @@ pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle) {
style: rr.border.style,
radius: rr.radius,
p: rr.border.tl,
clip: clip,
tlq: Rectangle::new(
rr.border.tl,
Point::new(rr.border.tl.x + rr.radius, rr.border.tl.y + rr.radius)),
@ -484,18 +511,22 @@ pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle) {
// now draw the corners
quadrant(fb,
Circle::new_with_style(rri.tlq.br, rr.radius, rr.border.style),
Quadrant::TopLeft
Quadrant::TopLeft,
clip
);
quadrant(fb,
Circle::new_with_style(Point::new(rri.trq.tl.x, rri.trq.br.y), rr.radius, rr.border.style),
Quadrant::TopRight
Quadrant::TopRight,
clip
);
quadrant(fb,
Circle::new_with_style(Point::new(rri.blq.br.x, rri.blq.tl.y), rr.radius, rr.border.style),
Quadrant::BottomLeft
Quadrant::BottomLeft,
clip
);
quadrant(fb,
Circle::new_with_style(rri.brq.tl, rr.radius, rr.border.style),
Quadrant::BottomRight
Quadrant::BottomRight,
clip
);
}

View file

@ -12,8 +12,12 @@ log-server = { path = "../log-server" }
ticktimer-server = { path = "../ticktimer-server" }
xous-names = { path = "../xous-names" }
graphics-server = {path = "../graphics-server"}
blitstr-ref = {path = "../../imports/blitstr-ref"}
gam = {path="../gam"}
log = "0.4"
rkyv = { version = "0.3.1", default_features = false }
keyboard = {path="../keyboard"}
ime-plugin-api = {path="../ime-plugin-api"}
[target.'cfg(not(any(windows,unix)))'.dependencies]

View file

@ -1,37 +0,0 @@
use xous::{Message, ScalarMessage};
use graphics_server::Gid;
#[allow(dead_code)]
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub enum Opcode {
/// informs me where my canvas is
SetCanvas(Gid),
/// set prediction. Must be a String of the name of a server that is loaded in the system.
SetPrediction(xous::String<256>),
}
impl core::convert::TryFrom<& Message> for Opcode {
type Error = &'static str;
fn try_from(message: & Message) -> Result<Self, Self::Error> {
match message {
Message::Scalar(m) => match m.id {
0 => Ok(Opcode::SetCanvas(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<Message> for Opcode {
fn into(self) -> Message {
match self {
Opcode::SetCanvas(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 _
}),
_ => panic!("IMEF api: Opcode type not handled by into()"),
}
}
}

View file

@ -1,9 +1,3 @@
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use xous::{CID, send_message};
pub fn set_canvas(cid: CID, g: graphics_server::Gid) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::SetCanvas(g).into())?;
Ok(())
}
pub use ime_plugin_api::{ImeFrontEnd, ImeFrontEndApi};

View file

@ -1,37 +1,174 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
mod api;
use api::*;
use ime_plugin_api::ImefOpcode;
use core::convert::TryFrom;
use core::fmt::Write;
use log::{error, info};
use graphics_server::Gid;
use graphics_server::{Gid, Line, PixelColor, Point, Rectangle, TextBounds, TextView, DrawStyle};
use blitstr_ref as blitstr;
use blitstr::Cursor;
use heapless::Vec;
use heapless::consts::U32;
use core::pin::Pin;
use ime_plugin_api::{PredictionTriggers, PredictionPlugin, PredictionApi};
/*
what else do we need:
- current string that is being built up
- cursor position in string, so we can do insertion/deletion
*/
fn draw_canvas(gam_conn: xous::CID, canvas: Gid, pred_conn: xous::CID, newkeys: [char; 4]) {
use rkyv::Unarchive;
use rkyv::archived_value;
// this is just reference to remind me how to decode the key array
for &k in newkeys.iter() {
if k != '\u{0000}' {
key_queue.push(k).unwrap();
if debug1{info!("IMEF: got key '{}'", k);}
struct InputTracker {
/// connection for handling graphical update requests
pub gam_conn: xous::CID,
/// input area canvas, as given by the GAM
input_canvas: Option<Gid>,
/// prediction display area, as given by the GAM
pred_canvas: Option<Gid>,
/// our current prediction engine
predictor: Option<PredictionPlugin>,
/// cached copy of the predictor's triggers for predictions. Only valid if predictor is not None
pred_triggers: Option<PredictionTriggers>,
/// track the progress of our input line
line: xous::String::<4096>,
/// we need to track character widths so we can draw a cursor
charwidths: [u8; 4096],
/// what position the insertion cursor is at in the string. 0 is inserting elements into the front of the string
insertion: Cursor,
}
impl InputTracker {
pub fn new(gam_conn: xous::CID)-> InputTracker {
InputTracker {
gam_conn,
input_canvas: None,
pred_canvas: None,
predictor: None,
pred_triggers: None,
line: xous::String::<4096>::new(),
charwidths: [0; 4096],
insertion: Cursor::new(0, 0, 0), // canvases always have (0,0) as the top left
}
}
pub fn set_predictor(&mut self, predictor: PredictionPlugin) {
self.predictor = Some(predictor);
self.pred_triggers = Some(predictor.get_prediction_triggers()
.expect("IMEF: InputTracker failed to get prediction triggers from plugin"));
}
pub fn set_input_canvas(&mut self, input: Gid) {
self.input_canvas = Some(input);
}
pub fn set_pred_canvas(&mut self, pred: Gid) {
self.pred_canvas = Some(pred);
}
pub fn is_init(&self) -> bool {
self.input_canvas.is_some() && self.pred_canvas.is_some() && self.predictor.is_some()
}
pub fn update(&mut self, newkeys: [char; 4]) -> Result<(), xous::Error> {
let debug1= false;
// 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!("IMEF: got pc_bound {:?}", pc_bounds);}
let mut starting_tv = TextView::new(pc, 255,
TextBounds::BoundingBox(Rectangle::new(Point::new(0, 0), pc_bounds)));
starting_tv.draw_border = false;
starting_tv.border_width = 1;
starting_tv.clear_area = false;
if debug1{info!("IMEF: posting textview {:?}", starting_tv);}
gam::post_textview(self.gam_conn, &mut starting_tv).expect("IMEF: can't draw prediction TextView");
// add the border line on top
gam::draw_line(self.gam_conn, pc,
Line::new_with_style(
Point::new(0,0),
Point::new(pc_bounds.x, 0),
DrawStyle {
fill_color: None,
stroke_color: Some(PixelColor::Dark),
stroke_width: 1,
})
).expect("IMEF: can't draw prediction top border");
}
if let Some(ic) = self.input_canvas {
if debug1{info!("IMEF: updating input area");}
let mut ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, ic).expect("IMEF: Couldn't get input canvas bounds");
let mut input_tv = TextView::new(ic, 255,
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;
for &k in newkeys.iter() {
if debug1{info!("IMEF: got key '{}'", k);}
match k {
'\u{0000}' => (),
'\u{000d}' => {
// carriage return case
write!(self.line, "").expect("IMEF: can't clear line after carriage return");
},
_ => {
self.line.push(k).expect("IMEF: ran out of space pushing character into input line");
write!(input_tv.text, "{}", self.line.as_str().expect("IMEF: couldn't convert str")).expect("IMEF: couldn't update TextView string in input canvas");
gam::post_textview(self.gam_conn, &mut input_tv).expect("IMEF: can't draw input TextView");
if debug1{info!("IMEF: got computed cursor of {:?}", input_tv.cursor);}
if self.insertion.pt.y == input_tv.cursor.pt.y {
self.charwidths[self.line.len()] = (input_tv.cursor.pt.x - self.insertion.pt.x) as u8;
} else {
// line wrapped, assume we wrapped to x = 0
self.charwidths[self.line.len()] = (input_tv.cursor.pt.x - 0) as u8;
}
self.insertion.pt.y = input_tv.cursor.pt.y;
self.insertion.pt.x = input_tv.cursor.pt.x;
self.insertion.line_height = input_tv.cursor.line_height;
},
}
}
// draw the insertion point
// do a manual type conversion, because external crate etc. etc.
let ins_pt: Point = Point::new(self.insertion.pt.x as i16, self.insertion.pt.y as i16);
if debug1{info!("IMEF: drawing insertion point: {:?}, height: {}", ins_pt, self.insertion.line_height);}
gam::draw_line(self.gam_conn, ic,
Line::new(ins_pt,
ins_pt + Point::new(0, self.insertion.line_height as i16 - input_tv.margin.y) ))
.expect("IMEF: can't draw insertion point");
// add the border line on top
gam::draw_line(self.gam_conn, ic,
Line::new_with_style(
Point::new(0,0),
Point::new(ic_bounds.x, 0),
DrawStyle {
fill_color: None,
stroke_color: Some(PixelColor::Dark),
stroke_width: 1,
}))
.expect("IMEF: can't draw input top line border");
}
/*
what else do we need:
- current string that is being built up
- cursor position in string, so we can do insertion/deletion
*/
Ok(())
}
}
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = true;
let debug1 = false;
log_server::init_wait().unwrap();
info!("IMEF: my PID is {}", xous::process::id());
@ -41,59 +178,92 @@ fn xmain() -> ! {
let kbd_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_KBD).expect("IMEF: can't connect to KBD");
keyboard::request_events(xous::names::SERVER_NAME_IME_FRONT, kbd_conn).expect("IMEF: couldn't request events from keyboard");
let gam_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("IMEF: can't connect to GAM");
// set a "default" prediction that is a shell-like history buffer of things previously typed
let mut prediction_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_PLUGIN_SHELL)
.expect("IMEF: can't connect to shell prediction engine");
// The first message should be my Gid from the GAM.
let mut canvas: Gid = Gid::new([0,0,0,0]);
info!("IMEF: waiting for my canvas Gid");
let mut tracker = InputTracker::new(
xous_names::request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("IMEF: can't connect to GAM"),
);
// 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");
loop {
let envelope = xous::receive_message(imef_sid).unwrap();
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
if let Ok(opcode) = ImefOpcode::try_from(&envelope.body) {
match opcode {
Opcode::SetCanvas(g) => {
canvas = g;
ImefOpcode::SetInputCanvas(g) => {
if debug1{info!("IMEF: got input canvas {:?}", g);}
tracker.set_input_canvas(g);
},
ImefOpcode::SetPredictionCanvas(g) => {
if debug1{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::<ImefOpcode>(&bytes, m.id as usize)
};
match &*value {
rkyv::Archived::<ImefOpcode>::SetPredictionServer(rkyv_s) => {
let s: xous::String<256> = rkyv_s.unarchive();
match xous_names::request_connection(s.as_str().expect("IMEF: SetPrediction received malformed server name")) {
Ok(pc) => tracker.set_predictor(ime_plugin_api::PredictionPlugin {connection: Some(pc)}),
_ => error!("IMEF: can't find predictive engine {}, retaining existing one.", s.as_str().expect("IMEF: SetPrediction received malformed server name")),
}
},
_ => panic!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage")
};
} else {
info!("IMEF: expected canvas Gid, but got other message first {:?}", envelope);
}
if tracker.is_init() {
break;
}
}
// force a redraw of the UI with no keys
if debug1{info!("IMEF: forcing initial UI redraw");}
tracker.update(['\u{0000}'; 4]).expect("IMEF: couldn't redraw initial UI");
let mut key_queue: Vec<char, U32> = Vec::new();
info!("IMEF: entering main loop");
loop {
let envelope = xous::receive_message(imef_sid).unwrap();
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
if debug1{info!("IMEF: got message {:?}", envelope);}
if let Ok(opcode) = ImefOpcode::try_from(&envelope.body) {
match opcode {
Opcode::SetCanvas(g) => {
ImefOpcode::SetInputCanvas(g) => {
// there are valid reasons for this to happen, but it should be rare.
info!("IMEF: warning: canvas Gid has been reset");
canvas = g;
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");
tracker.set_pred_canvas(g);
},
_ => info!("IMEF: unhandled opcode {:?}", opcode)
}
} else if let xous::Message::Borrow(m) = &msg.body {
} 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::<api::Opcode>(&bytes, m.id as usize)
archived_value::<ImefOpcode>(&bytes, m.id as usize)
};
match &*value {
rkyv::Archived::<api::Opcode>::SetPrediction(rkyv_s) => {
let s: xous::String<4096> = rkyv_s.unarchive();
prediction_conn = xous_names::request_connection(s.as_str());
rkyv::Archived::<ImefOpcode>::SetPredictionServer(rkyv_s) => {
let s: xous::String<256> = rkyv_s.unarchive();
match xous_names::request_connection(s.as_str().expect("IMEF: SetPrediction received malformed server name")) {
Ok(pc) => tracker.set_predictor(ime_plugin_api::PredictionPlugin {connection: Some(pc)}),
_ => error!("IMEF: can't find predictive engine {}, retaining existing one.", s.as_str().expect("IMEF: SetPrediction received malformed server name")),
}
},
_ => panic!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage")
};
} else if let Ok(opcode) = keyboard::api::Opcode::try_from(&envelope.body) {
match opcode {
keyboard::api::Opcode::KeyboardEvent(keys) => {
draw_canvas(gam_conn, canvas, prediction_conn, keys);
tracker.update(keys).expect("IMEF: couldn't update input tracker with latest key presses");
},
_ => error!("IMEF: received KBD event opcode that wasn't expected"),
}

View file

@ -0,0 +1,13 @@
[package]
name = "ime-plugin-api"
version = "0.1.0"
authors = ["bunnie <bunnie@kosagi.com>"]
edition = "2018"
description = "IME Plugin Common APIs"
[dependencies]
xous = { path = "../../xous-rs" }
rkyv = { version = "0.3.1", default_features = false }
graphics-server = { path="../graphics-server"} # this is used by the IMEF portion of the API
log = "0.4"

View file

@ -0,0 +1,294 @@
#![cfg_attr(target_os = "none", no_std)]
use xous::{Message, ScalarMessage, String, CID};
use graphics_server::Gid;
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub struct Prediction {
pub index: u32,
pub string: xous::String<4096>,
}
#[derive(Debug, Default, Copy, Clone)]
pub struct PredictionTriggers {
/// trigger predictions on newline
pub trigger_newline: bool,
/// trigger predictions punctuation
pub trigger_punctuation: bool,
/// trigger predictions on whitespace
pub trigger_whitespace: bool,
}
impl Into<usize> for PredictionTriggers {
fn into(self) -> usize {
let mut ret: usize = 0;
if self.trigger_newline { ret |= 0x1; }
if self.trigger_punctuation { ret |= 0x2; }
if self.trigger_whitespace { ret |= 0x4; }
ret
}
}
impl From<usize> for PredictionTriggers {
fn from(code: usize) -> PredictionTriggers {
PredictionTriggers {
trigger_newline: (code & 0x1) != 0,
trigger_punctuation: (code & 0x2) != 0,
trigger_whitespace: (code & 0x4) != 0,
}
}
}
#[allow(dead_code)]
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub enum Opcode {
/// update with the latest input candidate. Replaces the previous input.
Input(xous::String<4096>),
/// feed back to the IME plugin as to what was picked, so predictions can be updated
Picked(xous::String<4096>),
/// Undo the last Picked value. To be used when a user hits backspace after picking a prediction
/// note that repeated calls to Unpick will have an implementation-defined behavior
Unpick,
/// fetch the prediction at a given index, where the index is ordered from 0..N, where 0 is the most likely prediction
/// if there is no prediction available, just return an empty string
Prediction(Prediction),
/// return the prediction triggers used by this IME. These are characters that can indicate that a
/// whole predictive unit has been entered.
GetPredictionTriggers,
}
impl core::convert::TryFrom<& Message> for Opcode {
type Error = &'static str;
fn try_from(message: & Message) -> Result<Self, Self::Error> {
match message {
Message::Scalar(m) => match m.id {
0 => Ok(Opcode::Unpick),
_ => Err("IME_SH api: unknown Scalar ID"),
},
Message::BlockingScalar(m) => match m.id {
1 => Ok(Opcode::GetPredictionTriggers),
_ => Err("IME_SH api: unknown BlockingScalar ID"),
},
_ => Err("IME_SH api: unhandled message type"),
}
}
}
impl Into<Message> for Opcode {
fn into(self) -> Message {
match self {
Opcode::Unpick => Message::Scalar(ScalarMessage {
id: 0,
arg1: 0, arg2: 0, arg3: 0, arg4: 0,
}),
Opcode::GetPredictionTriggers => Message::BlockingScalar(ScalarMessage {
id: 1,
arg1: 0, arg2: 0, arg3: 0, arg4: 0,
}),
_ => panic!("IME_SH api: Opcode type not handled by Into(), refer to helper method"),
}
}
}
pub trait PredictionApi {
fn get_prediction_triggers(&self) -> Result<PredictionTriggers, xous::Error>;
fn unpick(&self) -> Result<(), xous::Error>;
fn set_input(&self, s: String<4096>) -> Result<(), xous::Error>;
fn feedback_picked(&self, s: String<4096>) -> Result<(), xous::Error>;
fn get_prediction(&self, index: u32) -> Result<xous::String<4096>, xous::Error>;
}
// provide a convenience version of the API for generic/standard calls
#[derive(Debug, Default, Copy, Clone)]
pub struct PredictionPlugin {
pub connection: Option<CID>,
}
impl PredictionApi for PredictionPlugin {
fn get_prediction_triggers(&self) -> Result<PredictionTriggers, xous::Error> {
match self.connection {
Some(cid) => {
let response = xous::send_message(cid, Opcode::GetPredictionTriggers.into())?;
if let xous::Result::Scalar1(code) = response {
Ok(code.into())
} else {
Err(xous::Error::InternalError)
}
},
_ => Err(xous::Error::UseBeforeInit),
}
}
fn unpick(&self) -> Result<(), xous::Error> {
match self.connection {
Some(cid) => {
xous::send_message(cid, Opcode::Unpick.into())?;
Ok(())
},
_ => Err(xous::Error::UseBeforeInit)
}
}
fn set_input(&self, s: String<4096>) -> Result<(), xous::Error> {
use rkyv::Write;
match self.connection {
Some(cid) => {
let rkyv_input = Opcode::Input(s);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_input).expect("IME|API: couldn't archive input string");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("IME|API: set_input operation failure");
Ok(())
},
_ => Err(xous::Error::UseBeforeInit),
}
}
fn feedback_picked(&self, s: String<4096>) -> Result<(), xous::Error> {
use rkyv::Write;
match self.connection {
Some(cid) => {
let rkyv_picked = Opcode::Picked(s);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_picked).expect("IME|API: couldn't archive picked string");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("IME|API: feedback_picked operation failure");
Ok(())
},
_ => Err(xous::Error::UseBeforeInit),
}
}
fn get_prediction(&self, index: u32) -> Result<xous::String<4096>, xous::Error> {
use rkyv::Write;
use rkyv::Unarchive;
match self.connection {
Some(cid) => {
let prediction = Prediction {
index,
string: xous::String::<4096>::new(),
};
let pred_op = Opcode::Prediction(prediction);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&pred_op).expect("IME|API: couldn't archive prediction request");
let mut xous_buffer = writer.into_inner();
xous_buffer.lend_mut(cid, pos as u32).expect("IME|API: prediction fetch operation failure");
let returned = unsafe { rkyv::archived_value::<Opcode>(xous_buffer.as_ref(), pos)};
if let rkyv::Archived::<Opcode>::Prediction(result) = returned {
let pred_r: Prediction = result.unarchive();
let retstring: xous::String<4096> = pred_r.string.clone();
Ok(retstring)
} else {
let r = returned.unarchive();
log::error!("IME:API get_prediction returned an invalid result {:?}", r);
Err(xous::Error::InvalidString)
}
},
_ => Err(xous::Error::UseBeforeInit),
}
}
}
//////////////////////////////////////////////////////
//////////////////// FRONT END API
//////////////////////////////////////////////////////
// Most people won't need to touch this, but it's packaged
// in this crate so we can break circular dependencies
// between the IMEF, GAM, and graphics server
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub enum ImefOpcode {
/// informs me where my input canvas is
SetInputCanvas(Gid),
/// informs me where my prediction canvas is
SetPredictionCanvas(Gid),
/// set prediction server. Must be a String of the name of a server that is loaded in the system.
SetPredictionServer(xous::String<256>),
}
impl core::convert::TryFrom<& Message> for ImefOpcode {
type Error = &'static str;
fn try_from(message: & Message) -> Result<Self, Self::Error> {
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<Message> 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>;
}
pub struct ImeFrontEnd {
pub connection: Option<CID>,
}
impl ImeFrontEndApi for ImeFrontEnd {
fn set_input_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
match self.connection {
Some(cid) => {
xous::send_message(cid, ImefOpcode::SetInputCanvas(g).into())?;
Ok(())
},
_ => Err(xous::Error::UseBeforeInit)
}
}
fn set_prediction_canvas(&self, g: graphics_server::Gid) -> Result<(), xous::Error> {
match self.connection {
Some(cid) => {
xous::send_message(cid, ImefOpcode::SetPredictionCanvas(g).into())?;
Ok(())
},
_ => Err(xous::Error::UseBeforeInit)
}
}
fn set_predictor(&self, servername: &str) -> Result<(), xous::Error> {
match self.connection {
Some(cid) => {
let mut server = xous::String::<256>::new();
use core::fmt::Write;
write!(server, "{}", servername).expect("IMEF: couldn't write set_predictor server name");
let ime_op = ImefOpcode::SetPredictionServer(server);
use rkyv::Write as ArchiveWrite;
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&ime_op).expect("IMEF: couldn't archive SetPredictionServer");
writer.into_inner().lend(cid, pos as u32).expect("IMEF: SetPredicitonServer request failure");
Ok(())
},
_ => Err(xous::Error::UseBeforeInit)
}
}
}

View file

@ -13,7 +13,7 @@ ticktimer-server = { path = "../ticktimer-server" }
xous-names = { path = "../xous-names" }
log = "0.4"
rkyv = { version = "0.3.1", default_features = false }
ime-plugin-api = {path="../ime-plugin-api"}
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}

View file

@ -1,21 +0,0 @@
use xous::{Message, ScalarMessage};
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub struct Prediction {
index: u32,
string: xous::String,
}
#[allow(dead_code)]
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub enum Opcode {
/// update with the latest input candidate. Replaces the previous input.
Input(xous::String),
/// feed back to the IME plugin as to what was picked, so predictions can be updated
Picked(xous::String),
/// fetch the prediction at a given index, where the index is ordered from 0..N, where 0 is the most likely prediction
/// if there is no prediction available, just return an empty string
Prediction(Prediction),
}

View file

@ -1,60 +1,4 @@
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use xous::{CID, send_message, String};
pub fn set_input(cid: CID, s: String) -> Result<(), xous::Error> {
use rkyv::Write;
let mut rkyv_input = api::Opcode::Input(*s);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_s).expect("IMES|API: couldn't archive input string");
let mut xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("IMES|API: set_input operation failure");
Ok(())
}
pub fn feedback_picked(cid: CID, s: String) -> Result<(), xous::Error> {
use rkyv::Write;
let mut rkyv_input = api::Opcode::Picked(*s);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_s).expect("IMES|API: couldn't archive picked string");
let mut xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("IMES|API: feedback_picked operation failure");
Ok(())
}
pub fn get_prediction(cid: CID, index: u32) -> Result<xous::String, xous::Error> {
use rkyv::Write;
use rkyv::Unarchive;
let mut mutstr = xous::String::new();
let mut prediction = api::Prediction {
index,
string: mutstr
};
let mut pred_op = api::Opcode::Prediction(*prediction);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&pred_op).expect("IMES|API: couldn't archive prediction request");
let mut xous_buffer = writer.into_inner();
xous_buffer.lend_mut(cid, pos as u32).expect("IMES|API: prediction fetch operation failure");
let returned = unsafe { rkyv::archived_value::<api::Opcode>(xous_buffer.as_ref(), pos)};
if let rkyv::Archived::<api::Opcode>::Prediction(result) = returned {
let pred_r: api::Prediction = result.unarchive();
let retstring: xous::String = pred_r.string.clone();
Ok(retstring)
} else {
let r = returned.unarchive();
info!("get_prediciton saw an unhandled return of {:?}", r);
Err(xous::Error::InvalidString)
}
}
// just inherit all the default from the ime_plugin_api
pub use ime_plugin_api::*;

View file

@ -1,16 +1,17 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
mod api;
use api::*;
use ime_plugin_api::*;
use core::convert::TryFrom;
use log::{error, info};
use heapless::spsc::Queue;
use heapless::consts::U3;
use rkyv::Unarchive;
use core::pin::Pin;
use rkyv::{archived_value, archived_value_mut};
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = true;
@ -18,69 +19,95 @@ fn xmain() -> ! {
info!("IME_SH: my PID is {}", xous::process::id());
let ime_sh_sid = xous_names::register_name(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("IME_SH: can't register server");
info!("IME_SH: registered with NS -- {:?}", ime_sh_sid);
if debug1{info!("IME_SH: registered with NS -- {:?}", ime_sh_sid);}
let imef_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_FRONT).expect("IME_SH: can't connect to IMEF");
info!("IME_SH: ready to accept requests");
let mut history: Queue<xous::String, U3> = Queue::new(); // this has 2^3 elements = 8
let mut history: Queue<xous::String<4096>, U3> = Queue::new(); // this has 2^3 elements = 8
let history_max = 8;
let mytriggers = PredictionTriggers {
trigger_newline: true,
trigger_punctuation: false,
trigger_whitespace: false,
};
info!("IME_SH: ready to accept requests");
loop {
let envelope = xous::receive_message(ime_sh_sid).unwrap();
if let xous::Message::Borrow(m) = &msg.body {
if debug1{info!("IME_SH: received message {:?}", envelope);}
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::<api::Opcode>(&bytes, m.id as usize)
archived_value::<Opcode>(&bytes, m.id as usize)
};
match &*value {
rkyv::Archived::<api::Opcode>::Input(rkyv_s) => {
rkyv::Archived::<Opcode>::Input(_rkyv_s) => {
// shell does nothing with the input, it only keeps track of
// the picked results
},
rkyv::Archived::<Opcode>::Picked(rkyv_s) => {
let s: xous::String<4096> = rkyv_s.unarchive();
if history.len() == history_max {
history.dequeue()?;
history.dequeue().expect("IME_SH: couldn't dequeue history");
}
history.enqueue(s).expect("IME_SH: couldn't store history");
},
rkyv::Archived::<api::Opcode>::Picked(rkyv_s) => {
let s: xous::String<4096> = rkyv_s.unarchive();
if debug1{info!("Received prediction feedback but ignored: {}", s);}
},
_ => panic!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage")
_ => error!("IME_SH: unknown Borrow message")
};
} else if let xous::Message::MutableBorrow(m) = &msg.body {
} else if let xous::Message::MutableBorrow(m) = &envelope.body {
let mut buf = unsafe { xous::XousBuffer::from_memory_message(m) };
let value = unsafe {
archived_value_mut::<api::Opcode>(Pin::new(buf.as_mut()), m.id as usize)
archived_value_mut::<Opcode>(Pin::new(buf.as_mut()), m.id as usize)
};
match &*value {
rkyv::Archived::<api::Opcode>::Prediction(pred_r) => {
let mut prediction: api::Prediction = pred_r.unarchive();
rkyv::Archived::<Opcode>::Prediction(pred_r) => {
let mut prediction: Prediction = pred_r.unarchive();
if history.len() > 0 {
let mut index = prediction.index;
if index >= history.len() {
index = history.len() - 1;
if index >= history.len() as u32 {
index = history.len() as u32 - 1;
}
let mut i = history_len();
let mut i = history.len() as u32;
let mut retstr = xous::String::new();
for s in history.iter() {
for &s in history.iter() {
// iterator is from oldest to newest, so do some math to go from newest to oldest
if (history.len() - i) == index {
if (history.len() as u32 - i) == index {
retstr = s;
break;
}
i = i + 1;
}
prediction.string = s;
prediction.string = retstr;
} else { // there is no history
// return the empty string
prediction.string = xous::String::new();
}
}
},
_ => error!("IME_SH: unknown MutableBorrow message"),
}
} else if let Ok(opcode) = Opcode::try_from(&envelope.body) {
match opcode {
Opcode::Unpick => {
if history.len() == 1 {
history.dequeue().expect("IME_SH: couldn't dequeue in Unpick (1)");
} else if history.len() > 1 {
// rotate everything around except the last entry
for _ in 0 .. history.len() - 1 {
let s = history.dequeue().expect("IME_SH: couldn't dequee in Unpick (>1)");
history.enqueue(s).expect("IME_SH: couldn't enqueue in Unpick");
}
// discard the last entry
history.dequeue().expect("IME_SH: couldn't dequeue in Unpick (>1 last)");
}
// in case of 0 length, do nothing
},
Opcode::GetPredictionTriggers => {
xous::return_scalar(envelope.sender, mytriggers.into()).expect("IME_SH: couldn't return GetPredictionTriggers");
},
_ => error!("IME_SH: unknown Opcode"),
}
} else {
info!("IME_SH: received unknown message type {}", envelope);
info!("IME_SH: received unknown message type {:?}", envelope);
panic!("IME_SH: received unknown message type");
}
}

View file

@ -17,7 +17,7 @@ const FAIL_TIMEOUT_MS: u64 = 100;
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = false;
let debug1 = true;
log_server::init_wait().unwrap();
info!("NS: my PID is {}", xous::process::id());

View file

@ -91,6 +91,33 @@ impl<const N: usize> String<N> {
pub fn to_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.bytes[0..self.len()]) }
}
pub fn push(&mut self, ch: char) -> core::result::Result<(), Error> {
match ch.len_utf8() {
1 => {
if self.len() < self.bytes.len() {
self.bytes[self.len()] = ch as u8;
self.len += 1;
Ok(())
} else {
Err(Error::OutOfMemory)
}
},
_ => {
let mut data: [u8; 4] = [0; 4];
ch.encode_utf8(&mut data);
if self.len() + data.len() < self.bytes.len() {
for &c in data.iter() {
self.bytes[self.len()] = c;
self.len += 1;
}
Ok(())
} else {
Err(Error::OutOfMemory)
}
},
}
}
}
impl<const N: usize> core::fmt::Display for String<N> {

View file

@ -39,8 +39,8 @@ fn try_main() -> Result<(), DynError> {
let hw_pkgs = [
"gam",
//"shell",
//"ime-frontend",
//"ime-plugin-shell",
"ime-frontend",
"ime-plugin-shell",
"graphics-server",
"ticktimer-server",
"log-server",
@ -192,8 +192,8 @@ fn run(debug: bool) -> Result<(), DynError> {
let init = [
//"shell",
"gam",
//"ime-frontend",
//"ime-plugin-shell",
"ime-frontend",
"ime-plugin-shell",
//"benchmark",
//"benchmark-target",
"log-server",