From 081c669b4373ca1b628b2e6c3211d334713f60b8 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sat, 29 Jul 2023 07:21:14 +1000 Subject: [PATCH] init mtxchat with bones of mtxcli --- Cargo.toml | 1 + apps/manifest.json | 8 +++++ apps/mtxchat/Cargo.toml | 38 +++++++++++++++++++++ apps/mtxchat/src/api.rs | 17 +++++++++ apps/mtxchat/src/main.rs | 74 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+) create mode 100644 apps/mtxchat/Cargo.toml create mode 100644 apps/mtxchat/src/api.rs create mode 100644 apps/mtxchat/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b4cc83db8..02c1cd441 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,6 +74,7 @@ members = [ "services/early_settings", "apps/ball", "apps/hello", + "apps/mtxchat", "apps/mtxcli", "apps/repl", "apps/vault", diff --git a/apps/manifest.json b/apps/manifest.json index 5cc78adec..c1946cb50 100644 --- a/apps/manifest.json +++ b/apps/manifest.json @@ -35,6 +35,14 @@ } } }, + "mtxchat": { + "context_name": "Matrix chat", + "menu_name": { + "appmenu.mtxchat": { + "en": "Matrix chat" + } + } + }, "repl": { "context_name": "repl demo app", "menu_name": { diff --git a/apps/mtxchat/Cargo.toml b/apps/mtxchat/Cargo.toml new file mode 100644 index 000000000..f9b36752d --- /dev/null +++ b/apps/mtxchat/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "mtxchat" +version = "0.1.0" +authors = ["john "] +edition = "2018" +description = "Matrix chat" + +# Dependency versions enforced by Cargo.lock. +[dependencies] +log = "0.4.14" +num-derive = { version = "0.3.3", default-features = false} +num-traits = { version = "0.2.14", default-features = false} +xous = "0.9.47" +xous-ipc = "0.9.47" +log-server = { package = "xous-api-log", version = "0.1.43" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.43" } +xous-names = { package = "xous-api-names", version = "0.9.45" } +gam = { path = "../../services/gam" } +graphics-server = { path = "../../services/graphics-server" } +trng = { path = "../../services/trng"} +locales = { path = "../../locales"} +ime-plugin-api = { path = "../../services/ime-plugin-api"} +ime-plugin-shell = { path = "../../services/ime-plugin-shell"} +content-plugin-api = { path = "../../services/content-plugin-api"} # all content canvas providers must provide this API +codec = { path = "../../services/codec"} +com = { path = "../../services/com"} +com_rs = { git = "https://github.com/betrusted-io/com_rs", rev = "891bdd3ca8e41f81510d112483e178aea3e3a921" } +llio = { path = "../../services/llio"} +net = { path = "../../services/net" } +pddb = { path = "../../services/pddb" } +status = { path = "../../services/status" } +# new dependencies for mtxcli +percent-encoding = "2.2" +serde = { version = "1.0", features = [ "derive" ] } +ureq = { version = "2.5", features = ["json"] } + +[features] +default = [] diff --git a/apps/mtxchat/src/api.rs b/apps/mtxchat/src/api.rs new file mode 100644 index 000000000..06d0864b5 --- /dev/null +++ b/apps/mtxchat/src/api.rs @@ -0,0 +1,17 @@ +use rkyv::{Archive, Deserialize, Serialize}; + +pub(crate) const SERVER_NAME_MTXCHAT: &str = "_Matrix chat_"; + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum MtxchatOp { + /// chat ui event + Event = 0, + /// chat ui user post + Post, + /// chat ui keystroke + Rawkeys, + /// exit the application + Quit, +} + + diff --git a/apps/mtxchat/src/main.rs b/apps/mtxchat/src/main.rs new file mode 100644 index 000000000..72014351f --- /dev/null +++ b/apps/mtxchat/src/main.rs @@ -0,0 +1,74 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +mod api; +use api::*; +use num_traits::*; +use xous_ipc::Buffer; + +fn main () -> ! { + let stack_size = 1024 * 1024; + std::thread::Builder::new() + .stack_size(stack_size) + .spawn(wrapped_main) + .unwrap() + .join() + .unwrap() +} + +fn wrapped_main() -> ! { + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); + + const HEAP_LARGER_LIMIT: usize = 2048 * 1024; + let new_limit = HEAP_LARGER_LIMIT; + let result = xous::rsyscall(xous::SysCall::AdjustProcessLimit( + xous::Limits::HeapMaximum as usize, + 0, + new_limit, + )); + + if let Ok(xous::Result::Scalar2(1, current_limit)) = result { + xous::rsyscall(xous::SysCall::AdjustProcessLimit( + xous::Limits::HeapMaximum as usize, + current_limit, + new_limit, + )) + .unwrap(); + log::info!("Heap limit increased to: {}", new_limit); + } else { + panic!("Unsupported syscall!"); + } + + let xns = xous_names::XousNames::new().unwrap(); + let sid = xns + .register_name(SERVER_NAME_MTXCHAT, None) + .expect("can't register server"); + // log::trace!("registered with NS -- {:?}", sid); + + loop { + let msg = xous::receive_message(sid).unwrap(); + log::debug!("got message {:?}", msg); + match FromPrimitive::from_usize(msg.body.id()) { + Some(MtxchatOp::Event) => { + } + Some(MtxchatOp::Post) => { + log::info!("TODO Post to Matrix server"); + } + Some(MtxchatOp::Rawkeys) => {} + Some(MtxchatOp::Quit) => { + log::error!("got Quit"); + chat.forward(msg); + break; + } + _ => (), + } + } + // clean up our program + log::error!("main loop exit, destroying servers"); + xns.unregister_server(sid).unwrap(); + xous::destroy_server(sid).unwrap(); + log::trace!("quitting"); + xous::terminate_process(0) +}