initial structuring of the shellchat app

This commit is contained in:
bunnie 2021-03-09 04:31:51 +08:00
parent b5411851fd
commit 07a9cbb02d
3 changed files with 67 additions and 22 deletions

View file

@ -569,6 +569,7 @@ impl InputTracker {
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = false;
let dbglistener = true;
log_server::init_wait().unwrap();
info!("IMEF: my PID is {}", xous::process::id());
@ -616,9 +617,11 @@ fn xmain() -> ! {
},
rkyv::Archived::<ImefOpcode>::RegisterListener(registration) => {
let s: xous::String<256> = registration.unarchive();
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 = xous_names::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")
@ -670,23 +673,27 @@ fn xmain() -> ! {
},
rkyv::Archived::<ImefOpcode>::RegisterListener(registration) => {
let s: xous::String<256> = registration.unarchive();
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 = xous_names::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")
_ => 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() {
use rkyv::Write;
let op = ImefOpcode::GotInputLine(line);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&line).expect("IMEF: couldn't archive line for sending");
let xous_buffer = writer.into_inner();
xous_buffer.send(conn, pos as u32).expect("IMEF: couldn't send input line to listener");
let pos = writer.archive(&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");
}
}
},

View file

@ -309,7 +309,7 @@ impl ImeFrontEndApi for ImeFrontEnd {
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");
writer.into_inner().lend(cid, pos as u32).expect("IMEF: SetPredictionServer request failure");
Ok(())
},
_ => Err(xous::Error::UseBeforeInit)

View file

@ -14,16 +14,55 @@ use ime_plugin_api::{ImeFrontEndApi, ImeFrontEnd};
use heapless::spsc::Queue;
use heapless::consts::U4;
fn repl(history: &mut Queue<xous::String<1024>, U4>) -> Result<(), xous::Error> {
for &s in history.iter() {
info!("SHCH: command history {}", s);
struct Repl {
input: Option<xous::String<1024>>,
history: Queue<xous::String<1024>, U4>,
gam: xous::CID,
}
impl Repl {
fn new() -> Self {
let gam_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("SHCH: can't connect to GAM");
Repl {
input: None,
history: Queue::new(),
gam: gam_conn,
}
}
Ok(())
/// accept a new input string
fn input(&mut self, line: &str) -> Result<(), xous::Error> {
let mut local = xous::String::<1024>::new();
write!(local, "{}", line).expect("SHCH: line too long for history buffer");
self.input = Some(local);
Ok(())
}
/// update the loop, in response to various inputs
fn update(&mut self) -> Result<(), xous::Error> {
// if we had an input string, do something
if let Some(local) = self.input {
if self.history.len() == self.history.capacity() {
self.history.dequeue().expect("SHCH: couldn't dequeue historye");
}
self.history.enqueue(local).expect("SHCH: couldn't store input line");
self.input = None;
for &s in self.history.iter() {
info!("SHCH: command history {}", s);
}
}
// other things to do based on other events...
Ok(())
}
}
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = true;
log_server::init_wait().unwrap();
info!("SHCH: my PID is {}", xous::process::id());
@ -32,14 +71,15 @@ fn xmain() -> ! {
let imef_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_FRONT).expect("SHCH: can't connect to IMEF");
let imef = ImeFrontEnd { connection: Some(imef_conn) };
imef.register_listener(xous::names::SERVER_NAME_IME_FRONT).expect("SHCH: couldn't request events from IMEF");
let mut history: Queue<xous::String<1024>, U4> = Queue::new(); // this has 2^4 elements = 16???
imef.register_listener(xous::names::SERVER_NAME_SHELLCHAT).expect("SHCH: couldn't request events from IMEF");
let mut repl = Repl::new();
let mut update_repl = false;
info!("SHCH: starting main loop");
loop {
let envelope = xous::receive_message(shch_sid).unwrap();
if let xous::Message::Move(m) = &envelope.body {
if debug1{info!("SHCH: got 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 {
@ -48,20 +88,18 @@ fn xmain() -> ! {
match &*value {
rkyv::Archived::<ime_plugin_api::ImefOpcode>::GotInputLine(rkyv_s) => {
let s: xous::String<4000> = rkyv_s.unarchive();
let mut local: xous::String<1024> = xous::String::new();
write!(local, "{}", s.as_str().expect("SHCH: couldn't convert incoming string")).expect("SHCH: probably exceeded input length");
if history.len() == history.capacity() {
history.dequeue().expect("SHCH: couldn't dequeue historye");
}
history.enqueue(local).expect("SHCH: couldn't store input line");
repl(&mut history).expect("SHCH: repl experienced an error");
repl.input(s.as_str().expect("SHCH: couldn't convert incoming string")).expect("SHCH: 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
},
_ => panic!("SHCH: invalid response from server -- corruption occurred in MemoryMessage")
};
} else {
error!("SHCH: couldn't convert message");
}
if update_repl {
repl.update().expect("SHCH: REPL had problems updating");
update_repl = false;
}
}
}