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] #[xous::xous_main]
fn xmain() -> ! { fn xmain() -> ! {
let debug1 = false; let debug1 = false;
let dbglistener = true;
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
info!("IMEF: my PID is {}", xous::process::id()); info!("IMEF: my PID is {}", xous::process::id());
@ -616,9 +617,11 @@ fn xmain() -> ! {
}, },
rkyv::Archived::<ImefOpcode>::RegisterListener(registration) => { rkyv::Archived::<ImefOpcode>::RegisterListener(registration) => {
let s: xous::String<256> = registration.unarchive(); 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 // 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")) 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"); .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"); 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!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage")
@ -670,23 +673,27 @@ fn xmain() -> ! {
}, },
rkyv::Archived::<ImefOpcode>::RegisterListener(registration) => { rkyv::Archived::<ImefOpcode>::RegisterListener(registration) => {
let s: xous::String<256> = registration.unarchive(); 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 // 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")) 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"); .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"); 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) { } else if let Ok(opcode) = keyboard::api::Opcode::try_from(&envelope.body) {
match opcode { match opcode {
keyboard::api::Opcode::KeyboardEvent(keys) => { keyboard::api::Opcode::KeyboardEvent(keys) => {
if let Some(line) = tracker.update(keys).expect("IMEF: couldn't update input tracker with latest key presses") { 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() { for &conn in listeners.iter() {
use rkyv::Write; use rkyv::Write;
let op = ImefOpcode::GotInputLine(line);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&line).expect("IMEF: couldn't archive line for sending"); let pos = writer.archive(&op).expect("IMEF: couldn't archive line for sending");
let xous_buffer = writer.into_inner(); if dbglistener{info!("IMEF: sending to conn {:?}", conn);}
xous_buffer.send(conn, pos as u32).expect("IMEF: couldn't send input line to listener"); 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; use rkyv::Write as ArchiveWrite;
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&ime_op).expect("IMEF: couldn't archive SetPredictionServer"); 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(()) Ok(())
}, },
_ => Err(xous::Error::UseBeforeInit) _ => Err(xous::Error::UseBeforeInit)

View file

@ -14,16 +14,55 @@ use ime_plugin_api::{ImeFrontEndApi, ImeFrontEnd};
use heapless::spsc::Queue; use heapless::spsc::Queue;
use heapless::consts::U4; use heapless::consts::U4;
fn repl(history: &mut Queue<xous::String<1024>, U4>) -> Result<(), xous::Error> { struct Repl {
for &s in history.iter() { input: Option<xous::String<1024>>,
info!("SHCH: command history {}", s); 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] #[xous::xous_main]
fn xmain() -> ! { fn xmain() -> ! {
let debug1 = true;
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
info!("SHCH: my PID is {}", xous::process::id()); 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_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) }; let imef = ImeFrontEnd { connection: Some(imef_conn) };
imef.register_listener(xous::names::SERVER_NAME_IME_FRONT).expect("SHCH: couldn't request events from IMEF"); imef.register_listener(xous::names::SERVER_NAME_SHELLCHAT).expect("SHCH: couldn't request events from IMEF");
let mut history: Queue<xous::String<1024>, U4> = Queue::new(); // this has 2^4 elements = 16???
let mut repl = Repl::new();
let mut update_repl = false;
info!("SHCH: starting main loop"); info!("SHCH: starting main loop");
loop { loop {
let envelope = xous::receive_message(shch_sid).unwrap(); 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 buf = unsafe { xous::XousBuffer::from_memory_message(m) };
let bytes = Pin::new(buf.as_ref()); let bytes = Pin::new(buf.as_ref());
let value = unsafe { let value = unsafe {
@ -48,20 +88,18 @@ fn xmain() -> ! {
match &*value { match &*value {
rkyv::Archived::<ime_plugin_api::ImefOpcode>::GotInputLine(rkyv_s) => { rkyv::Archived::<ime_plugin_api::ImefOpcode>::GotInputLine(rkyv_s) => {
let s: xous::String<4000> = rkyv_s.unarchive(); let s: xous::String<4000> = rkyv_s.unarchive();
let mut local: xous::String<1024> = xous::String::new(); repl.input(s.as_str().expect("SHCH: couldn't convert incoming string")).expect("SHCH: REPL couldn't accept input string");
write!(local, "{}", s.as_str().expect("SHCH: couldn't convert incoming string")).expect("SHCH: probably exceeded input length"); update_repl = true; // set a flag, instead of calling here, so message can drop and calling server is released
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");
}, },
_ => panic!("SHCH: invalid response from server -- corruption occurred in MemoryMessage") _ => panic!("SHCH: invalid response from server -- corruption occurred in MemoryMessage")
}; };
} else { } else {
error!("SHCH: couldn't convert message"); error!("SHCH: couldn't convert message");
} }
if update_repl {
repl.update().expect("SHCH: REPL had problems updating");
update_repl = false;
}
} }
} }