mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
build a prototype UI for the totp mode
This commit is contained in:
parent
c393f590dc
commit
79b841142d
4 changed files with 170 additions and 59 deletions
|
|
@ -26,6 +26,7 @@ rand_core = "0.6.3"
|
||||||
ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.65" }
|
ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.65" }
|
||||||
userprefs = { path = "../../libs/userprefs" }
|
userprefs = { path = "../../libs/userprefs" }
|
||||||
ux-api = { path = "../../libs/ux-api" }
|
ux-api = { path = "../../libs/ux-api" }
|
||||||
|
cramium-api = { path = "../../libs/cramium-api" }
|
||||||
cram-hal-service = { path = "../../services/cram-hal-service", optional = true }
|
cram-hal-service = { path = "../../services/cram-hal-service", optional = true }
|
||||||
cramium-emu = { path = "../../services/cramium-emu", optional = true }
|
cramium-emu = { path = "../../services/cramium-emu", optional = true }
|
||||||
blitstr2 = { path = "../../libs/blitstr2" }
|
blitstr2 = { path = "../../libs/blitstr2" }
|
||||||
|
|
|
||||||
|
|
@ -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 num_traits::*;
|
||||||
use ux_api::minigfx::*;
|
|
||||||
use ux_api::service::api::Gid;
|
mod ux;
|
||||||
use ux_api::service::gfx::Gfx;
|
use totp::PumpOp;
|
||||||
|
use ux::*;
|
||||||
|
mod totp;
|
||||||
|
|
||||||
pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_";
|
pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_";
|
||||||
|
|
||||||
|
|
@ -14,31 +15,13 @@ pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_";
|
||||||
pub(crate) enum VaultOp {
|
pub(crate) enum VaultOp {
|
||||||
/// Redraw the screen
|
/// Redraw the screen
|
||||||
Redraw = 0,
|
Redraw = 0,
|
||||||
|
KeyPress,
|
||||||
/// Quit the application
|
|
||||||
Quit,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VaultUi {
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
pub gfx: Gfx,
|
pub enum VaultMode {
|
||||||
}
|
Totp,
|
||||||
|
Password,
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
|
|
@ -50,26 +33,26 @@ fn main() -> ! {
|
||||||
|
|
||||||
// Register the server with xous
|
// Register the server with xous
|
||||||
let sid = xns.register_name(SERVER_NAME_VAULT2, None).expect("can't register server");
|
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({
|
// global shared state
|
||||||
let sid = sid.clone();
|
let mode = Arc::new(Mutex::new(VaultMode::Totp));
|
||||||
move || {
|
let allow_totp_rendering = Arc::new(AtomicBool::new(true));
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
// 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 {
|
loop {
|
||||||
let msg = xous::receive_message(sid).unwrap();
|
let msg = xous::receive_message(sid).unwrap();
|
||||||
log::debug!("Got message: {:?}", msg);
|
log::debug!("Got message: {:?}", msg);
|
||||||
|
|
@ -77,18 +60,22 @@ fn main() -> ! {
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
Some(VaultOp::Redraw) => {
|
Some(VaultOp::Redraw) => {
|
||||||
log::debug!("Got redraw");
|
log::debug!("Got redraw");
|
||||||
vault_ui.redraw();
|
match *mode.lock().unwrap() {
|
||||||
}
|
VaultMode::Totp => {
|
||||||
Some(VaultOp::Quit) => {
|
vault_ui.redraw_totp();
|
||||||
log::info!("Quitting application");
|
}
|
||||||
break;
|
_ => {
|
||||||
|
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::error!("Got unknown message");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log::info!("Quitting");
|
|
||||||
xous::terminate_process(0)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -173,7 +173,7 @@ pub(crate) fn pumper(
|
||||||
{
|
{
|
||||||
// we really want mode.lock() to be in a different scope so...
|
// we really want mode.lock() to be in a different scope so...
|
||||||
if mode_cache == VaultMode::Totp {
|
if mode_cache == VaultMode::Totp {
|
||||||
tt.sleep_ms(2000).unwrap();
|
tt.sleep_ms(50).unwrap();
|
||||||
send_message(
|
send_message(
|
||||||
self_conn,
|
self_conn,
|
||||||
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0),
|
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
|
|
|
||||||
|
|
@ -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
|
use core::fmt::Write;
|
||||||
// environment.
|
|
||||||
|
|
||||||
pub mod framework;
|
use blitstr2::GlyphStyle;
|
||||||
pub mod icontray;
|
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<u32>,
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue