2021-03-08 19:52:39 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
2021-03-17 20:22:17 +00:00
|
|
|
use log::info;
|
2021-03-08 19:52:39 +00:00
|
|
|
|
|
|
|
|
use core::fmt::Write;
|
|
|
|
|
|
|
|
|
|
use ime_plugin_api::{ImeFrontEndApi, ImeFrontEnd};
|
2021-03-09 16:25:14 +00:00
|
|
|
use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, GlyphStyle, PixelColor};
|
2021-03-17 20:22:17 +00:00
|
|
|
use xous::MessageEnvelope;
|
2021-04-05 15:50:00 +00:00
|
|
|
use xous_ipc::{String, Buffer};
|
2021-03-08 19:52:39 +00:00
|
|
|
|
|
|
|
|
use heapless::spsc::Queue;
|
2021-03-09 16:25:14 +00:00
|
|
|
use heapless::consts::U16;
|
|
|
|
|
|
2021-03-09 20:18:57 +00:00
|
|
|
mod cmds;
|
|
|
|
|
use cmds::*;
|
|
|
|
|
|
2021-03-09 16:25:14 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct History {
|
|
|
|
|
// the history record
|
2021-03-26 12:22:33 +00:00
|
|
|
pub text: String<1024>,
|
2021-03-09 16:25:14 +00:00
|
|
|
// if true, this was input from the user; if false, it's a response from the shell
|
|
|
|
|
pub is_input: bool,
|
|
|
|
|
}
|
2021-03-08 19:52:39 +00:00
|
|
|
|
2021-03-09 16:25:14 +00:00
|
|
|
#[derive(Debug)]
|
2021-03-08 20:31:51 +00:00
|
|
|
struct Repl {
|
2021-03-09 16:25:14 +00:00
|
|
|
// optional structures that indicate new input to the Repl loop per iteration
|
|
|
|
|
// an input string
|
2021-03-26 12:22:33 +00:00
|
|
|
input: Option<String<1024>>,
|
2021-03-17 20:22:17 +00:00
|
|
|
// messages from other servers
|
|
|
|
|
msg: Option<MessageEnvelope>,
|
2021-03-09 16:25:14 +00:00
|
|
|
|
|
|
|
|
// record our input history
|
|
|
|
|
history: Queue<History, U16>,
|
|
|
|
|
content: Gid,
|
2021-04-05 15:50:00 +00:00
|
|
|
gam: gam::Gam,
|
2021-03-09 16:25:14 +00:00
|
|
|
|
|
|
|
|
// variables that define our graphical attributes
|
|
|
|
|
screensize: Point,
|
|
|
|
|
bubble_width: u16,
|
|
|
|
|
margin: Point, // margin to edge of canvas
|
|
|
|
|
bubble_margin: Point, // margin of text in bubbles
|
|
|
|
|
bubble_radius: u16,
|
|
|
|
|
bubble_space: i16, // spacing between text bubbles
|
2021-03-09 20:18:57 +00:00
|
|
|
|
|
|
|
|
// command environment
|
|
|
|
|
env: CmdEnv,
|
2021-03-08 20:31:51 +00:00
|
|
|
}
|
2021-03-10 17:45:02 +00:00
|
|
|
impl Repl{
|
2021-04-05 15:50:00 +00:00
|
|
|
fn new(xns: &xous_names::XousNames, my_server_name: &str) -> Self {
|
|
|
|
|
let gam = gam::Gam::new(&xns).expect("can't connect to GAM");
|
|
|
|
|
let content = gam.request_content_canvas(
|
|
|
|
|
my_server_name,
|
|
|
|
|
ShellOpcode::Redraw.to_usize().unwrap()
|
|
|
|
|
).expect("couldn't get content canvas");
|
|
|
|
|
let screensize = gam.get_canvas_bounds(content).expect("couldn't get dimensions of content canvas");
|
2021-03-08 20:31:51 +00:00
|
|
|
Repl {
|
|
|
|
|
input: None,
|
2021-03-17 20:22:17 +00:00
|
|
|
msg: None,
|
2021-03-08 20:31:51 +00:00
|
|
|
history: Queue::new(),
|
2021-03-09 16:25:14 +00:00
|
|
|
content,
|
2021-04-05 15:50:00 +00:00
|
|
|
gam,
|
2021-03-09 16:25:14 +00:00
|
|
|
screensize,
|
|
|
|
|
bubble_width: ((screensize.x / 5) * 4) as u16, // 80% width for the text bubbles
|
|
|
|
|
margin: Point::new(4, 4),
|
|
|
|
|
bubble_margin: Point::new(4, 4),
|
|
|
|
|
bubble_radius: 4,
|
|
|
|
|
bubble_space: 4,
|
2021-04-05 15:50:00 +00:00
|
|
|
env: CmdEnv::new(&xns),
|
2021-03-08 20:31:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// accept a new input string
|
|
|
|
|
fn input(&mut self, line: &str) -> Result<(), xous::Error> {
|
2021-03-26 12:22:33 +00:00
|
|
|
let mut local = String::<1024>::new();
|
2021-04-05 15:50:00 +00:00
|
|
|
write!(local, "{}", line).expect("line too long for history buffer");
|
2021-03-08 20:31:51 +00:00
|
|
|
|
|
|
|
|
self.input = Some(local);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2021-03-08 19:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-17 20:22:17 +00:00
|
|
|
fn msg(&mut self, message: MessageEnvelope) {
|
|
|
|
|
self.msg = Some(message);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 16:25:14 +00:00
|
|
|
fn circular_push(&mut self, item: History) {
|
|
|
|
|
if self.history.len() == self.history.capacity() {
|
2021-04-05 15:50:00 +00:00
|
|
|
self.history.dequeue().expect("couldn't dequeue historye");
|
2021-03-09 16:25:14 +00:00
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
self.history.enqueue(item).expect("couldn't store input line");
|
2021-03-09 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 20:31:51 +00:00
|
|
|
/// update the loop, in response to various inputs
|
|
|
|
|
fn update(&mut self) -> Result<(), xous::Error> {
|
2021-03-10 20:12:14 +00:00
|
|
|
let debug1 = false;
|
2021-03-08 20:31:51 +00:00
|
|
|
// if we had an input string, do something
|
|
|
|
|
if let Some(local) = self.input {
|
2021-03-09 16:25:14 +00:00
|
|
|
let input_history = History {
|
|
|
|
|
text: local,
|
|
|
|
|
is_input: true,
|
|
|
|
|
};
|
|
|
|
|
self.circular_push(input_history);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AT THIS POINT: if we have other inputs, update accordingly
|
2021-03-09 20:18:57 +00:00
|
|
|
// other inputs might be, for example, events that came in from other servers that would
|
|
|
|
|
// side effect our commands
|
2021-03-09 16:25:14 +00:00
|
|
|
|
2021-03-09 20:18:57 +00:00
|
|
|
// redraw UI once upon accepting all input
|
2021-04-05 15:50:00 +00:00
|
|
|
self.redraw().expect("can't redraw");
|
2021-03-09 16:25:14 +00:00
|
|
|
|
|
|
|
|
// take the input and pass it on to the various command parsers, and attach result
|
2021-03-09 20:18:57 +00:00
|
|
|
if let Some(mut local) = self.input {
|
2021-04-09 17:01:51 +00:00
|
|
|
log::trace!("processing line: {}", local);
|
2021-04-05 15:50:00 +00:00
|
|
|
if let Some(res) = self.env.dispatch(Some(&mut local), None).expect("command dispatch failed") {
|
2021-03-26 12:22:33 +00:00
|
|
|
let mut response = String::<1024>::new();
|
2021-04-05 15:50:00 +00:00
|
|
|
write!(response, "{}", res).expect("can't copy result to history");
|
2021-03-17 20:22:17 +00:00
|
|
|
let output_history = History {
|
|
|
|
|
text: response,
|
|
|
|
|
is_input: false
|
|
|
|
|
};
|
|
|
|
|
self.circular_push(output_history);
|
|
|
|
|
}
|
|
|
|
|
} else if let Some(msg) = &self.msg {
|
2021-04-09 17:01:51 +00:00
|
|
|
log::trace!("processing callback msg: {:?}", msg);
|
|
|
|
|
if let Some(res) = self.env.dispatch(None, Some(msg)).expect("callback failed") {
|
2021-03-26 12:22:33 +00:00
|
|
|
let mut response = String::<1024>::new();
|
2021-04-05 15:50:00 +00:00
|
|
|
write!(response, "{}", res).expect("can't copy result to history");
|
2021-03-09 20:18:57 +00:00
|
|
|
let output_history = History {
|
|
|
|
|
text: response,
|
|
|
|
|
is_input: false
|
|
|
|
|
};
|
|
|
|
|
self.circular_push(output_history);
|
|
|
|
|
}
|
2021-03-09 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// clear all the inputs to the loop, so we don't process them twice
|
|
|
|
|
self.input = None;
|
2021-03-17 20:22:17 +00:00
|
|
|
self.msg = None;
|
2021-03-09 16:25:14 +00:00
|
|
|
// redraw UI now that we've responded
|
2021-04-05 15:50:00 +00:00
|
|
|
self.redraw().expect("can't redraw");
|
2021-03-09 16:25:14 +00:00
|
|
|
|
|
|
|
|
if debug1 {
|
|
|
|
|
for h in self.history.iter() {
|
2021-04-05 15:50:00 +00:00
|
|
|
info!("command history is_input: {}, text:{}", h.is_input, h.text);
|
2021-03-08 20:31:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-09 16:25:14 +00:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2021-03-08 20:31:51 +00:00
|
|
|
|
2021-03-09 20:18:57 +00:00
|
|
|
fn clear_area(&self) {
|
2021-04-05 15:50:00 +00:00
|
|
|
self.gam.draw_rectangle(self.content,
|
2021-03-09 16:25:14 +00:00
|
|
|
Rectangle::new_with_style(Point::new(0, 0), self.screensize,
|
|
|
|
|
DrawStyle {
|
|
|
|
|
fill_color: Some(PixelColor::Light),
|
|
|
|
|
stroke_color: None,
|
|
|
|
|
stroke_width: 0
|
|
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
)).expect("can't clear content area");
|
2021-03-09 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
fn redraw(&mut self) -> Result<(), xous::Error> {
|
|
|
|
|
self.clear_area();
|
|
|
|
|
|
|
|
|
|
// this defines the bottom border of the text bubbles as they stack up wards
|
|
|
|
|
let mut bubble_baseline = self.screensize.y - self.margin.y;
|
|
|
|
|
|
|
|
|
|
// iterator returns from oldest to newest
|
|
|
|
|
// .rev() iterator is from newest to oldest
|
|
|
|
|
for h in self.history.iter().rev() {
|
|
|
|
|
let mut bubble_tv =
|
|
|
|
|
if h.is_input {
|
2021-03-11 10:52:10 +00:00
|
|
|
TextView::new(self.content,
|
2021-03-09 16:25:14 +00:00
|
|
|
TextBounds::GrowableFromBr(
|
|
|
|
|
Point::new(self.screensize.x - self.margin.x, bubble_baseline),
|
|
|
|
|
self.bubble_width))
|
|
|
|
|
} else {
|
2021-03-11 10:52:10 +00:00
|
|
|
TextView::new(self.content,
|
2021-03-09 16:25:14 +00:00
|
|
|
TextBounds::GrowableFromBl(
|
|
|
|
|
Point::new(self.margin.x, bubble_baseline),
|
|
|
|
|
self.bubble_width))
|
|
|
|
|
};
|
|
|
|
|
if h.is_input {
|
|
|
|
|
bubble_tv.border_width = 1;
|
|
|
|
|
} else {
|
|
|
|
|
bubble_tv.border_width = 2;
|
|
|
|
|
}
|
|
|
|
|
bubble_tv.draw_border = true;
|
|
|
|
|
bubble_tv.clear_area = true;
|
|
|
|
|
bubble_tv.rounded_border = Some(self.bubble_radius);
|
|
|
|
|
bubble_tv.style = GlyphStyle::Small;
|
|
|
|
|
bubble_tv.margin = self.bubble_margin;
|
|
|
|
|
bubble_tv.ellipsis = false; bubble_tv.insertion = None;
|
2021-04-05 15:50:00 +00:00
|
|
|
write!(bubble_tv.text, "{}", h.text.as_str().expect("couldn't convert history text")).expect("couldn't write history text to TextView");
|
|
|
|
|
self.gam.post_textview(&mut bubble_tv).expect("couldn't render bubble textview");
|
2021-03-09 16:25:14 +00:00
|
|
|
|
|
|
|
|
if let Some(bounds) = bubble_tv.bounds_computed {
|
|
|
|
|
// we only subtract 1x of the margin because the bounds were computed from a "bottom right" that already counted
|
|
|
|
|
// the margin once.
|
|
|
|
|
bubble_baseline -= (bounds.br.y - bounds.tl.y) + self.bubble_space + self.bubble_margin.y;
|
|
|
|
|
if bubble_baseline <= 0 {
|
|
|
|
|
// don't draw history that overflows the top of the screen
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break; // we get None on the bounds computed if the text view fell off the top of the screen
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-08 20:31:51 +00:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2021-03-08 19:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-05 15:50:00 +00:00
|
|
|
////////////////// local message passing from callback
|
|
|
|
|
use num_traits::{ToPrimitive, FromPrimitive};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
|
|
|
|
enum ShellOpcode {
|
|
|
|
|
/// a line of text has arrived
|
|
|
|
|
Line,
|
|
|
|
|
/// redraw our UI
|
|
|
|
|
Redraw,
|
|
|
|
|
/// exit the application
|
|
|
|
|
Quit,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static mut CB_TO_MAIN_CONN: Option<xous::CID> = None;
|
|
|
|
|
fn imef_cb(s: String::<4000>) {
|
|
|
|
|
if let Some(cb_to_main_conn) = unsafe{CB_TO_MAIN_CONN} {
|
|
|
|
|
let buf = xous_ipc::Buffer::into_buf(s).or(Err(xous::Error::InternalError)).unwrap();
|
|
|
|
|
buf.lend(cb_to_main_conn, ShellOpcode::Line.to_u32().unwrap()).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//////////////////
|
|
|
|
|
|
2021-04-08 15:07:02 +00:00
|
|
|
fn test_thread() {
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
|
|
|
|
let ticktimer = ticktimer_server::Ticktimer::new().unwrap();
|
2021-04-08 21:24:29 +00:00
|
|
|
ticktimer.sleep_ms(5000).unwrap();
|
2021-04-08 15:07:02 +00:00
|
|
|
let rtc = rtc::Rtc::new(&xns).unwrap();
|
|
|
|
|
loop {
|
|
|
|
|
rtc.request_datetime().unwrap();
|
2021-04-08 21:24:29 +00:00
|
|
|
ticktimer.sleep_ms(2000).unwrap();
|
2021-04-08 15:07:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 16:15:42 +00:00
|
|
|
pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_";
|
|
|
|
|
|
2021-03-08 19:52:39 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
|
|
|
|
log_server::init_wait().unwrap();
|
2021-04-07 20:38:01 +00:00
|
|
|
log::set_max_level(log::LevelFilter::Trace);
|
2021-04-05 15:50:00 +00:00
|
|
|
info!("my PID is {}", xous::process::id());
|
2021-03-08 19:52:39 +00:00
|
|
|
|
2021-03-29 18:13:23 +00:00
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
2021-04-05 15:50:00 +00:00
|
|
|
let shch_sid = xns.register_name(SERVER_NAME_SHELLCHAT).expect("can't register server");
|
|
|
|
|
log::trace!("registered with NS -- {:?}", shch_sid);
|
2021-03-08 19:52:39 +00:00
|
|
|
|
2021-04-05 18:48:55 +00:00
|
|
|
unsafe{CB_TO_MAIN_CONN = Some(xous::connect(shch_sid).unwrap())};
|
2021-04-05 15:50:00 +00:00
|
|
|
let mut imef = ImeFrontEnd::new(&xns).expect("can't connect to IMEF");
|
|
|
|
|
imef.hook_listener_callback(imef_cb).expect("couldn't request events from IMEF");
|
2021-03-08 19:52:39 +00:00
|
|
|
|
2021-04-05 15:50:00 +00:00
|
|
|
let mut repl = Repl::new(&xns, SERVER_NAME_SHELLCHAT);
|
2021-03-08 20:31:51 +00:00
|
|
|
let mut update_repl = false;
|
2021-04-08 15:07:02 +00:00
|
|
|
|
|
|
|
|
if false {
|
2021-04-08 16:15:42 +00:00
|
|
|
xous::create_thread_0(test_thread).unwrap();
|
2021-04-08 15:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-05 15:50:00 +00:00
|
|
|
log::trace!("starting main loop");
|
2021-03-08 19:52:39 +00:00
|
|
|
loop {
|
2021-04-05 15:50:00 +00:00
|
|
|
let msg = xous::receive_message(shch_sid).unwrap();
|
|
|
|
|
log::trace!("got message {:?}", msg);
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(ShellOpcode::Line) => {
|
|
|
|
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
|
|
|
|
let s = buffer.as_flat::<String<4000>, _>().unwrap();
|
2021-04-05 18:48:55 +00:00
|
|
|
log::trace!("shell got input line: {}", s.as_str());
|
2021-04-05 15:50:00 +00:00
|
|
|
repl.input(s.as_str()).expect("REPL couldn't accept input string");
|
|
|
|
|
update_repl = true; // set a flag, instead of calling here, so message can drop and calling server is released
|
|
|
|
|
}
|
|
|
|
|
Some(ShellOpcode::Redraw) => {
|
2021-04-09 17:01:51 +00:00
|
|
|
log::trace!("got Redraw");
|
2021-04-05 15:50:00 +00:00
|
|
|
repl.redraw().expect("REPL couldn't redraw");
|
|
|
|
|
}
|
|
|
|
|
Some(ShellOpcode::Quit) => {
|
2021-04-09 17:01:51 +00:00
|
|
|
log::trace!("got Quit");
|
2021-04-05 15:50:00 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
2021-04-09 17:01:51 +00:00
|
|
|
log::trace!("got unknown message, treating as callback");
|
2021-04-05 15:50:00 +00:00
|
|
|
repl.msg(msg);
|
|
|
|
|
update_repl = true;
|
2021-03-09 16:25:14 +00:00
|
|
|
}
|
2021-03-08 19:52:39 +00:00
|
|
|
}
|
2021-03-08 20:31:51 +00:00
|
|
|
if update_repl {
|
2021-04-05 15:50:00 +00:00
|
|
|
repl.update().expect("REPL had problems updating");
|
2021-03-08 20:31:51 +00:00
|
|
|
update_repl = false;
|
|
|
|
|
}
|
2021-04-09 17:01:51 +00:00
|
|
|
log::trace!("reached bottom of main loop");
|
2021-03-08 19:52:39 +00:00
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
// clean up our program
|
|
|
|
|
log::trace!("main loop exit, destroying servers");
|
|
|
|
|
unsafe{
|
|
|
|
|
if let Some(cb) = CB_TO_MAIN_CONN {
|
|
|
|
|
xous::disconnect(cb).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
xns.unregister_server(shch_sid).unwrap();
|
|
|
|
|
xous::destroy_server(shch_sid).unwrap();
|
|
|
|
|
log::trace!("quitting");
|
|
|
|
|
xous::terminate_process(); loop {}
|
2021-03-08 19:52:39 +00:00
|
|
|
}
|