///| The dialog as the compositor sees it: the request and state the agent sent (wire.mbt), plus /// what only the screen needs: the typed text (kept as characters so it can be wiped, and /// never sent anywhere but the agent), the show/hide toggle, Caps Lock, which control has the /// keyboard, Details open or not, and the shake after a wrong password. /// /// It decides nothing about the password check: Enter sends the answer (or asks for a retry /// when the check failed) and the agent's state machine (machine.mbt) does the rest. ///| The dialog's controls, in Tab order. pub(all) enum Focus { Field User Reveal Details Cancel Allow } derive(Eq, Debug) ///| What the compositor sends the agent after a key or a click. pub(all) enum ViewAction { Nothing /// Send the typed answer (take it with `take_answer`). Submit /// Start the check again (it failed). Retry /// Answer as account n. PickUser(Int) /// The user dismissed the dialog. Dismiss } derive(Eq, Debug) ///| Evdev key codes the dialog knows. pub let key_esc : UInt = 1 ///| pub let key_backspace : UInt = 14 ///| pub let key_tab : UInt = 15 ///| pub let key_enter : UInt = 28 ///| pub let key_kp_enter : UInt = 96 ///| pub let key_space : UInt = 57 ///| pub let key_left : UInt = 105 ///| pub let key_right : UInt = 106 ///| pub let key_up : UInt = 103 ///| pub let key_down : UInt = 108 ///| pub let key_u : UInt = 22 ///| pub let key_d : UInt = 32 ///| pub let key_r : UInt = 19 ///| The dialog for one request. pub(all) struct AuthView { id : String title : String subtitle : String command : String program : String icon_name : String message : String details : Array[(String, String)] users : Array[AuthUser] /// The first state arrived: the dialog is shown. mut shown : Bool // From the agent. mut phase : Phase mut prompt : String mut echo : Bool mut error : String mut info : String mut face : FaceState /// When the face scan started (monotonic ms; the animation's clock). mut face_at : UInt64 mut failures : Int mut user_sel : Int // The screen's own. input : Array[Char] mut reveal : Bool mut caps : Bool mut details_open : Bool mut focus : Focus /// When the last wrong password shook the card (monotonic ms), 0 for never. mut shake_at : UInt64 } ///| A view from a `request` message's fields (after "request"). pub fn AuthView::from_request(f : Array[String]) -> AuthView { let g = fn(i : Int) { f.get(i).unwrap_or("") } { id: g(1), title: g(2), subtitle: g(3), command: g(4), program: g(5), icon_name: g(6), message: g(7), details: [], users: [], shown: false, phase: Starting, prompt: "Password", echo: false, error: "", info: "", face: NoFace, face_at: 0UL, failures: 0, user_sel: 0, input: [], reveal: false, caps: false, details_open: false, focus: Field, shake_at: 0UL, } } ///| Take a `detail`, `user` or `state` message for this request. `now`: monotonic ms. pub fn AuthView::apply(self : AuthView, f : Array[String], now : UInt64) -> Unit { let g = fn(i : Int) { f.get(i).unwrap_or("") } match g(0) { "detail" => self.details.push((g(2), g(3))) "user" => self.users.push({ name: g(2), real_name: g(3), uid: parse_uint(g(4)).unwrap_or(-1) }) "state" => { let phase = phase_of(g(2)) let failures = parse_uint(g(9)).unwrap_or(0) let user_sel = parse_uint(g(10)).unwrap_or(0) let face = face_of(g(7), g(8)) if failures > self.failures { // A wrong password: shake, and start over in the field. self.shake_at = if now == 0UL { 1UL } else { now } self.wipe() self.focus = Field } if user_sel != self.user_sel { self.wipe() } if face == Scanning && self.face != Scanning { self.face_at = if now == 0UL { 1UL } else { now } } self.shown = true self.phase = phase self.prompt = g(3) self.echo = g(4) == "1" self.error = g(5) self.info = g(6) self.face = face self.failures = failures self.user_sel = user_sel } _ => () } } ///| The account answering, if any. pub fn AuthView::user(self : AuthView) -> AuthUser? { self.users.get(self.user_sel) } ///| Wipe what was typed. pub fn AuthView::wipe(self : AuthView) -> Unit { for i = 0; i < self.input.length(); i = i + 1 { self.input[i] = '\u{0}' } self.input.clear() } ///| What was typed, as UTF-8 bytes in a buffer the caller zeroes after use; the field is wiped. pub fn AuthView::take_answer(self : AuthView) -> FixedArray[Byte] { let n = self.input.fold(init=0, fn(acc, c) { let v = c.to_int() acc + (if v < 0x80 { 1 } else if v < 0x800 { 2 } else if v < 0x10000 { 3 } else { 4 }) }) let out : FixedArray[Byte] = FixedArray::make(n, b'\x00') let mut k = 0 for c in self.input { let v = c.to_int() if v < 0x80 { out[k] = v.to_byte() k = k + 1 } else if v < 0x800 { out[k] = (0xC0 | (v >> 6)).to_byte() out[k + 1] = (0x80 | (v & 0x3F)).to_byte() k = k + 2 } else if v < 0x10000 { out[k] = (0xE0 | (v >> 12)).to_byte() out[k + 1] = (0x80 | ((v >> 6) & 0x3F)).to_byte() out[k + 2] = (0x80 | (v & 0x3F)).to_byte() k = k + 3 } else { out[k] = (0xF0 | (v >> 18)).to_byte() out[k + 1] = (0x80 | ((v >> 12) & 0x3F)).to_byte() out[k + 2] = (0x80 | ((v >> 6) & 0x3F)).to_byte() out[k + 3] = (0x80 | (v & 0x3F)).to_byte() k = k + 4 } } self.wipe() out } ///| Whether the field takes typing (not while PAM checks the answer, nor once it's over). pub fn AuthView::editable(self : AuthView) -> Bool { self.phase != Checking && self.phase != Done } ///| What the main button does now: Retry after a failure with nothing typed, else Submit. fn AuthView::submit(self : AuthView) -> ViewAction { match self.phase { Failed => if self.input.length() == 0 { Retry } else { Submit } Asking => if self.input.length() == 0 && !self.echo { Nothing } else { Submit } // Typed while a face is looked for: it goes in when PAM asks. Starting => if self.input.length() > 0 { Submit } else { Nothing } Checking | Done => Nothing } } ///| The main button's label. pub fn AuthView::main_label(self : AuthView) -> String { if self.phase == Failed && self.input.length() == 0 { "Try again" } else { "Authenticate" } } ///| Whether the main button does anything now. pub fn AuthView::main_enabled(self : AuthView) -> Bool { self.submit() != Nothing } ///| The controls Tab moves through, in order (the account picker only with several). pub fn AuthView::focus_order(self : AuthView) -> Array[Focus] { let out : Array[Focus] = [Field] if self.users.length() > 1 { out.push(User) } out.push(Reveal) out.push(Details) out.push(Cancel) out.push(Allow) out } ///| Move the keyboard focus by `delta` along the Tab order. fn AuthView::move_focus(self : AuthView, delta : Int) -> Unit { let order = self.focus_order() let i = order.search(self.focus).unwrap_or(0) let n = order.length() self.focus = order[((i + delta) % n + n) % n] } ///| The account `delta` away (wrapping). fn AuthView::cycle_user(self : AuthView, delta : Int) -> ViewAction { let n = self.users.length() if n < 2 { return Nothing } PickUser(((self.user_sel + delta) % n + n) % n) } ///| Activate control `f` (Enter or Space on it, or a click). pub fn AuthView::activate(self : AuthView, f : Focus) -> ViewAction { match f { Field => { self.focus = Field Nothing } User => { self.focus = User self.cycle_user(1) } Reveal => { self.focus = Reveal self.reveal = !self.reveal Nothing } Details => { self.focus = Details self.details_open = !self.details_open Nothing } Cancel => Dismiss Allow => self.submit() } } ///| A key press: `code` (evdev), the modifiers (mask_shift, mask_ctrl, mask_alt bits), and the /// character it types on the current layout, if any. pub fn AuthView::key(self : AuthView, code : UInt, mods : UInt, ch : Char?) -> ViewAction { let ctrl = (mods & mask_ctrl) != 0U let shift = (mods & mask_shift) != 0U let alt = (mods & mask_alt) != 0U if code == key_esc { return Dismiss } if code == key_tab { self.move_focus(if shift { -1 } else { 1 }) return Nothing } if code == key_enter || code == key_kp_enter { return match self.focus { Field | Allow => self.submit() other => self.activate(other) } } // Alt+D details, Alt+R reveal: reachable from the field without leaving it. if alt && code == key_d { self.details_open = !self.details_open return Nothing } if alt && code == key_r { self.reveal = !self.reveal return Nothing } if self.focus == User && (code == key_left || code == key_up) { return self.cycle_user(-1) } if self.focus == User && (code == key_right || code == key_down) { return self.cycle_user(1) } if code == key_space && self.focus != Field { return self.activate(self.focus) } if !self.editable() { return Nothing } if code == key_backspace || (ctrl && code == key_u) { if ctrl { self.wipe() } else if self.input.length() > 0 { let last = self.input.length() - 1 self.input[last] = '\u{0}' ignore(self.input.pop()) } self.focus = Field return Nothing } if ctrl || alt { return Nothing } match ch { Some(c) => { if self.input.length() < 512 { self.input.push(c) } self.focus = Field Nothing } None => Nothing } } ///| Horizontal offset of the card while it shakes after a wrong password (0 when still). pub fn AuthView::shake_offset(self : AuthView, now : UInt64) -> Int { if self.shake_at == 0UL || now < self.shake_at { return 0 } let t = (now - self.shake_at).to_int() let dur = 420 if t >= dur { return 0 } // Three swings, fading out. let period = dur / 6 let phase = t % period let tri = if phase < period / 2 { phase * 4 - period } else { 3 * period - phase * 4 } let amp = 12 * (dur - t) / dur tri * amp / period } ///| Whether the card is still shaking. pub fn AuthView::shaking(self : AuthView, now : UInt64) -> Bool { self.shake_at != 0UL && now >= self.shake_at && now - self.shake_at < 420UL } ///| The field's text as shown: the answer (echo or revealed) or one dot per character. pub fn AuthView::field_text(self : AuthView) -> String { if self.echo || self.reveal { String::from_array(self.input) } else { let sb = StringBuilder::new() for _ in self.input { sb.write_char('●') } sb.to_string() } } ///| The number of characters typed. pub fn AuthView::typed(self : AuthView) -> Int { self.input.length() }