From 79b841142df75fbc920610abe90a0e5ad58f7319 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 1 Jun 2025 23:24:23 +0800 Subject: [PATCH] build a prototype UI for the totp mode --- apps/vault2/Cargo.toml | 1 + apps/vault2/src/main.rs | 95 +++++++++++++---------------- apps/vault2/src/totp.rs | 2 +- apps/vault2/src/ux.rs | 131 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 170 insertions(+), 59 deletions(-) diff --git a/apps/vault2/Cargo.toml b/apps/vault2/Cargo.toml index 51af0e717..e0ffe1901 100644 --- a/apps/vault2/Cargo.toml +++ b/apps/vault2/Cargo.toml @@ -26,6 +26,7 @@ rand_core = "0.6.3" ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.65" } userprefs = { path = "../../libs/userprefs" } ux-api = { path = "../../libs/ux-api" } +cramium-api = { path = "../../libs/cramium-api" } cram-hal-service = { path = "../../services/cram-hal-service", optional = true } cramium-emu = { path = "../../services/cramium-emu", optional = true } blitstr2 = { path = "../../libs/blitstr2" } diff --git a/apps/vault2/src/main.rs b/apps/vault2/src/main.rs index c335d4a59..5c576c13d 100644 --- a/apps/vault2/src/main.rs +++ b/apps/vault2/src/main.rs @@ -1,11 +1,12 @@ -use core::fmt::Write; +use core::sync::atomic::{AtomicBool, AtomicU32, Ordering}; +use std::sync::{Arc, Mutex}; -use blitstr2::GlyphStyle; -use locales::t; use num_traits::*; -use ux_api::minigfx::*; -use ux_api::service::api::Gid; -use ux_api::service::gfx::Gfx; + +mod ux; +use totp::PumpOp; +use ux::*; +mod totp; pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_"; @@ -14,31 +15,13 @@ pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_"; pub(crate) enum VaultOp { /// Redraw the screen Redraw = 0, - - /// Quit the application - Quit, + KeyPress, } -struct VaultUi { - pub gfx: Gfx, -} - -impl VaultUi { - fn new(xns: &xous_names::XousNames, sid: xous::SID) -> Self { Self { gfx: Gfx::new(&xns).unwrap() } } - - /// Clear the entire screen. - fn clear_area(&self) { self.gfx.clear().ok(); } - - /// Redraw the text view onto the screen. - fn redraw(&mut self) { - let mut tv = TextView::new( - Gid::dummy(), - TextBounds::CenteredTop(Rectangle::new(Point::new(0, 0), Point::new(127, 12))), - ); - write!(tv, "hello world").ok(); - self.gfx.draw_textview(&mut tv).expect("couldn't draw text"); - self.gfx.flush().ok(); - } +#[derive(Copy, Clone, PartialEq, Eq, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub enum VaultMode { + Totp, + Password, } fn main() -> ! { @@ -50,26 +33,26 @@ fn main() -> ! { // Register the server with xous let sid = xns.register_name(SERVER_NAME_VAULT2, None).expect("can't register server"); + let conn = xous::connect(sid).unwrap(); - let mut vault_ui = VaultUi::new(&xns, sid); + let mut vault_ui = VaultUi::new(&xns, conn); - std::thread::spawn({ - let sid = sid.clone(); - move || { - let tt = ticktimer_server::Ticktimer::new().unwrap(); - let cid_to_self = xous::connect(sid).unwrap(); - loop { - tt.sleep_ms(500).ok(); - log::info!("ping"); - xous::send_message( - cid_to_self, - xous::Message::new_scalar(VaultOp::Redraw.to_usize().unwrap(), 0, 0, 0, 0), - ) - .expect("couldn't pump the main loop event thread"); - } - } - }); + // global shared state + let mode = Arc::new(Mutex::new(VaultMode::Totp)); + let allow_totp_rendering = Arc::new(AtomicBool::new(true)); + // spawn the TOTP pumper + let pump_sid = xous::create_server().unwrap(); + crate::totp::pumper(mode.clone(), pump_sid, conn, allow_totp_rendering.clone()); + let pump_conn = xous::connect(pump_sid).unwrap(); + + // respond to keyboard events + let kbd = cramium_api::keyboard::Keyboard::new(&xns).unwrap(); + kbd.register_listener(SERVER_NAME_VAULT2, VaultOp::KeyPress.to_u32().unwrap() as usize); + + // kickstart the pumper + xous::send_message(pump_conn, xous::Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)) + .expect("couldn't start the pumper"); loop { let msg = xous::receive_message(sid).unwrap(); log::debug!("Got message: {:?}", msg); @@ -77,18 +60,22 @@ fn main() -> ! { match FromPrimitive::from_usize(msg.body.id()) { Some(VaultOp::Redraw) => { log::debug!("Got redraw"); - vault_ui.redraw(); - } - Some(VaultOp::Quit) => { - log::info!("Quitting application"); - break; + match *mode.lock().unwrap() { + VaultMode::Totp => { + vault_ui.redraw_totp(); + } + _ => { + unimplemented!() + } + } } + Some(VaultOp::KeyPress) => xous::msg_scalar_unpack!(msg, k1, _k2, _k3, _k4, { + let k = char::from_u32(k1 as u32).unwrap_or('\u{0000}'); + log::info!("got key {}", k); + }), _ => { log::error!("Got unknown message"); } } } - - log::info!("Quitting"); - xous::terminate_process(0) } diff --git a/apps/vault2/src/totp.rs b/apps/vault2/src/totp.rs index 2d2a34b6d..b3fcb5c0c 100644 --- a/apps/vault2/src/totp.rs +++ b/apps/vault2/src/totp.rs @@ -173,7 +173,7 @@ pub(crate) fn pumper( { // we really want mode.lock() to be in a different scope so... if mode_cache == VaultMode::Totp { - tt.sleep_ms(2000).unwrap(); + tt.sleep_ms(50).unwrap(); send_message( self_conn, Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0), diff --git a/apps/vault2/src/ux.rs b/apps/vault2/src/ux.rs index ab430be6c..b110b19b5 100644 --- a/apps/vault2/src/ux.rs +++ b/apps/vault2/src/ux.rs @@ -1,5 +1,128 @@ -// Note: FIDO UX items have been moved to "env", so that the UX can take different forms based on the host -// environment. +use core::fmt::Write; -pub mod framework; -pub mod icontray; +use blitstr2::GlyphStyle; +use locales::t; +use num_traits::*; +use ux_api::minigfx::*; +use ux_api::service::api::Gid; +use ux_api::service::gfx::Gfx; +use ux_api::widgets::ScrollableList; +use xous::CID; + +use crate::VaultOp; + +/// Centralizes tunable UI parameters for TOTP +struct TotpLayout {} +impl TotpLayout { + pub fn totp_box() -> RoundedRectangle { + RoundedRectangle::new(Rectangle::new(Point::new(0, 0), Point::new(127, 40)), 0) + } + + /// Vertical margin for the font because the centering algorithm also aligns-top, and we want a little + /// more verticale space for aesthetic reasons than the centering algorithm gives by default. + pub fn totp_font_vmargin() -> Point { Point::new(0, 4) } + + pub fn totp_margin() -> Point { Point::new(10, 0) } + + pub fn totp_font() -> GlyphStyle { GlyphStyle::ExtraLarge } + + pub fn timer_box() -> Rectangle { Rectangle::new(Point::new(0, 40), Point::new(127, 50)) } + + pub fn list_box() -> Rectangle { Rectangle::new(Point::new(0, 50), Point::new(127, 127)) } + + pub fn list_font() -> GlyphStyle { GlyphStyle::Bold } +} + +pub struct VaultUi { + main_cid: CID, + gfx: Gfx, + totp_list: ScrollableList, + + /// totp redraw state + totp_code: Option, + last_epoch: u64, +} + +impl VaultUi { + pub fn new(xns: &xous_names::XousNames, cid: xous::CID) -> Self { + let mut totp_list = ScrollableList::default() + .set_margin(TotpLayout::totp_margin()) + .pane_size(TotpLayout::list_box()) + .style(TotpLayout::list_font()); + for i in 0..6 { + totp_list.add_item(0, &format!("example {}", i)); + } + Self { + main_cid: cid, + gfx: Gfx::new(&xns).unwrap(), + totp_list, + totp_code: None, + last_epoch: crate::totp::get_current_unix_time().expect("couldn't get current time") / 30, + } + } + + /// Clear the entire screen. + pub fn clear_area(&self) { self.gfx.clear().ok(); } + + /// Redraw the text view onto the screen. + pub fn redraw_totp(&mut self) { + self.gfx.clear().ok(); + + // decorative box around code + let mut totp_box = TotpLayout::totp_box(); + totp_box.border.style = DrawStyle::new(PixelColor::Dark, PixelColor::Light, 1); + self.gfx.draw_rounded_rectangle(totp_box).ok(); + + // the TOTP code + let mut tv = TextView::new( + Gid::dummy(), + TextBounds::CenteredTop( + TotpLayout::totp_box().border.translate_chain(TotpLayout::totp_font_vmargin()), + ), + ); + tv.invert = true; + tv.margin = Point::new(0, 0); + tv.style = TotpLayout::totp_font(); + tv.draw_border = false; + + match self.totp_code { + Some(code) => { + write!(tv, "{} {}", code / 1000, code % 1000).ok(); + } + _ => { + write!(tv, "*** ***").ok(); + } + } + self.gfx.draw_textview(&mut tv).expect("couldn't draw text"); + + // list of codes to pick from + self.totp_list.draw(TotpLayout::timer_box().br().y); + + // draw the timer element + let mut timer_box = TotpLayout::timer_box(); + timer_box.style = DrawStyle::new(PixelColor::Dark, PixelColor::Light, 1); + self.gfx.draw_rectangle(timer_box).ok(); + + // draw the duration bar + let current_time = std::time::SystemTime::now() + .duration_since(std::time::SystemTime::UNIX_EPOCH) + .map(|duration| duration.as_millis()) + .expect("couldn't get time as millis"); + + // manage the epoch as well + let epoch = (current_time / (30 * 1000)) as u64; + if self.last_epoch != epoch { + self.last_epoch = epoch; + } + + let mut timer_remaining = TotpLayout::timer_box(); + let delta = (current_time - (self.last_epoch as u128 * 30 * 1000)) as isize; + let width = timer_remaining.width() as isize; + let delta_width = (delta * width * 128) / (30 * 128 * 1000); + timer_remaining.br = Point::new(width - delta_width, timer_remaining.br().y); + timer_remaining.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1); + self.gfx.draw_rectangle(timer_remaining).ok(); + + self.gfx.flush().ok(); + } +}