implement chat icontray

This commit is contained in:
nworbnhoj 2023-08-22 17:03:29 +10:00
parent 197ed63058
commit 77f25bc416
4 changed files with 198 additions and 11 deletions

View file

@ -15,7 +15,7 @@ pub enum ChatOp {
GamRawkeys,
/// redraw our UI
GamRedraw,
ListenSet,
Icontray,
MenuAdd,
PostAdd,
PostDel,
@ -27,7 +27,13 @@ pub enum ChatOp {
Quit,
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum IconOp {
PostMenu = 0,
F2Op,
F3Op,
AppMenu,
}
pub const POST_TEXT_MAX: usize = 3072;

130
libs/chat/src/icontray.rs Normal file
View file

@ -0,0 +1,130 @@
use ime_plugin_api::*;
use num_traits::*;
use std::thread;
use xous::{msg_scalar_unpack, CID};
use xous_ipc::Buffer;
pub(crate) const SERVER_NAME_ICONTRAY: &'static str = "_chat icon tray plugin_";
pub struct Icontray {
cid: Option<CID>,
}
impl Icontray {
pub fn new(cid: Option<CID>, icons: [&'static str; 4]) -> Self {
log::info!("Starting icontray handler server",);
let _ = thread::spawn({
move || {
server(cid, icons);
}
});
Icontray { cid }
}
}
pub(crate) fn server(_cid: Option<CID>, icons: [&str; 4]) {
let xns = xous_names::XousNames::new().unwrap();
// one connection only, should be the GAM
// however, because the predictor is connected only on demand -- we leave this as open-ended, which
// means anyone could send something to this server if they knew the name of it.
let ime_sh_sid = xns
.register_name(SERVER_NAME_ICONTRAY, None)
.expect("can't register server");
let mytriggers = PredictionTriggers {
newline: false,
punctuation: false,
whitespace: false,
};
let mut api_token: Option<[u32; 4]> = None;
loop {
let mut msg = xous::receive_message(ime_sh_sid).unwrap();
log::trace!("received message {:?}", msg);
match FromPrimitive::from_usize(msg.body.id()) {
Some(Opcode::Acquire) => {
let mut buffer = unsafe {
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
};
let mut ret = buffer.to_original::<AcquirePredictor, _>().unwrap();
if api_token.is_none() {
if let Some(token) = ret.token {
api_token = Some(token);
} else {
let new_token = xous::create_server_id().unwrap().to_array();
ret.token = Some(new_token);
api_token = Some(new_token);
}
} else {
ret.token = None;
log::warn!("attempt to acquire lock on a predictor that was already locked");
}
buffer.replace(ret).unwrap();
}
Some(Opcode::Release) => msg_scalar_unpack!(msg, t0, t1, t2, t3, {
let token = [t0 as u32, t1 as u32, t2 as u32, t3 as u32];
if let Some(t) = api_token {
if t == token {
api_token.take();
} else {
log::warn!("Release called with an invalid token");
}
} else {
log::warn!("Release called on a predictor that was in a released state");
}
}),
Some(Opcode::Input) => {
log::trace!("got chat icontray Opcode::Input");
}
Some(Opcode::Picked) => {
log::trace!("got chat icontray Opcode::Picked");
}
Some(Opcode::Prediction) => {
// we don't check the API token here, because our "predictions" are just the four menu slots
let mut buffer = unsafe {
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
};
let mut prediction: Prediction = buffer.to_original::<Prediction, _>().unwrap();
// every key press, the four slots get queried
prediction.string.clear();
if prediction.index < icons.len() as u32 {
prediction
.string
.append(icons[prediction.index as usize])
.ok();
prediction.valid = true;
} else {
prediction.valid = false;
}
// pack our data back into the buffer to return
buffer
.replace(Return::Prediction(prediction))
.expect("couldn't return Prediction");
}
Some(Opcode::Unpick) => {
// ignore
}
Some(Opcode::GetPredictionTriggers) => {
xous::return_scalar(msg.sender, mytriggers.into())
.expect("couldn't return GetPredictionTriggers");
}
Some(Opcode::Quit) => {
if api_token.is_some() {
log::error!("received quit, goodbye!");
break;
}
}
None => {
log::error!("unknown Opcode");
}
}
}
log::trace!("main loop exit, destroying servers");
xns.unregister_server(ime_sh_sid).unwrap();
xous::destroy_server(ime_sh_sid).unwrap();
log::trace!("quitting");
xous::terminate_process(0)
}

View file

@ -1,6 +1,7 @@
pub mod api;
pub mod cmd;
pub mod dialogue;
pub mod icontray;
pub mod ui;
pub use api::*;
@ -10,7 +11,7 @@ use std::convert::TryInto;
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};
use xous::{Error, MessageEnvelope, CID, SID};
use xous::{Error, CID, SID};
use xous_ipc::Buffer;
pub struct Chat {
@ -209,10 +210,51 @@ pub fn server(
}
Some(ChatOp::GamLine) => {
log::info!("got ChatOp::GamLine");
if let Some(cid) = app_cid {
if let Some(opcode) = opcode_post {
log::info!("Forwarding msg to Chat App: {:?}", msg);
msg.forward(cid, opcode).expect("failed to fwd msg");
let buffer =
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let s = buffer.as_flat::<xous_ipc::String<4000>, _>().unwrap();
match s.as_str() {
"\u{0011}" => {
log::info!("click F1 : raise app menu");
ui.event(Event::F1);
ui.raise_menu();
}
"\u{0012}" => {
log::info!("click F2 : noop : suggestions welcome!");
ui.event(Event::F2);
}
"\u{0013}" => {
log::info!("click F3: noop : suggestions welcome!");
ui.event(Event::F3);
}
"\u{0014}" => {
log::info!("click F4 : raise msg menu : pull request welcome!");
ui.event(Event::F4);
}
"↑" => {
log::info!("click ↑ : previous msg : pull request welcome!");
ui.event(Event::Up);
}
"↓" => {
log::info!("click ↓ : next msg : pull request welcome!");
ui.event(Event::Down);
}
"←" => {
log::info!("click ← : noop : suggestions welcome!");
ui.event(Event::Left);
}
"→" => {
log::info!("click → : noop : suggestions welcome!");
ui.event(Event::Right);
}
_ => {
drop(buffer);
if let Some(cid) = app_cid {
if let Some(opcode) = opcode_post {
log::info!("Forwarding msg to Chat App: {:?}", msg);
msg.forward(cid, opcode).expect("failed to fwd msg");
}
}
}
}
}

View file

@ -1,7 +1,8 @@
use super::*;
//use crate::{ChatOp, Dialogue, Event, Post, CHAT_SERVER_NAME};
use crate::icontray::Icontray;
use dialogue::{author::Author, post::Post, Dialogue};
use gam::{menu_matic, MenuMatic, MenuPayload, UxRegistration};
use gam::{menu_matic, MenuMatic, UxRegistration};
use graphics_server::api::GlyphStyle;
use graphics_server::{DrawStyle, Gid, PixelColor, Point, Rectangle, TextBounds, TextView};
use locales::t;
@ -74,7 +75,7 @@ impl Ui {
app_name: xous_ipc::String::<128>::from_str(app_name),
ux_type: gam::UxType::Chat,
predictor: Some(xous_ipc::String::<64>::from_str(
ime_plugin_shell::SERVER_NAME_IME_PLUGIN_SHELL,
crate::icontray::SERVER_NAME_ICONTRAY,
)),
listener: sid.to_array(), // note disclosure of our SID to the GAM -- the secret is now shared with the GAM!
redraw_id: ChatOp::GamRedraw as u32,
@ -83,15 +84,23 @@ impl Ui {
rawkeys_id: Some(ChatOp::GamRawkeys as u32),
focuschange_id: Some(ChatOp::GamChangeFocus as u32),
})
.expect("couldn't register Ux context for chat");
.expect("couldn't register Ux context for chat")
.unwrap();
gam.toggle_menu_mode(token)
.expect("couldnt't toggle menu mode");
let xns = XousNames::new().unwrap();
let modals = Modals::new(&xns).unwrap();
let canvas = gam
.request_content_canvas(token.unwrap())
.request_content_canvas(token)
.expect("couldn't get content canvas");
let screensize = gam
.get_canvas_bounds(canvas)
.expect("couldn't get dimensions of content canvas");
let _icontray = Icontray::new(
Some(xous::connect(sid).unwrap()),
[app_name, "F2", "F3", "F4"],
);
let pddb = pddb::Pddb::new();
pddb.try_mount();
let menu_mgr = menu_matic(