Files
chandra/compositor/host/lock.mbt
T
nandi c790f96e58
CI / test (push) Waiting to run
Lock: add session lock from power button and chandra-ctl lock
- Lock the session from the power button or `chandra-ctl lock`
- Unlock with a face or the password; the password always works
- Add compositor lock module with tests, and a polkit unlock policy (org.chandra.unlock)
- Document the lock flow in docs/lock.md and link it from the README
- Add generated interface bindings for the compositor and widget components
2026-11-29 03:24:46 -08:00

122 lines
4.7 KiB
MoonBit

///| Session lock (docs/lock.md): the compositor hides the desktop and only lets a face or
/// password bring it back. Locking is driven from the power button (when CHANDRA_LOCK=1 /
/// Settings turns on lock-on-blank) or `chandra-ctl lock`; unlocking reuses the password dialog.
///
/// The unlock check runs in chandra-polkit-agent as a real polkit authorization of the
/// `org.chandra.unlock` action (compositor/polkit/agent_loop.mbt): the agent asks polkit, polkit
/// drives the agent's own dialog, and because the `polkit-1` PAM stack carries
/// `pam_chandra_face.so` (docs/face-unlock.md) the camera is tried first, with the password as
/// the fallback. The compositor only sends `unlock` and waits for `unlocked`.
///
/// lock_now ──► request_unlock ──"unlock"──► agent ──► polkit ──► dialog (face, then password)
/// ▲ │
/// │ success: "unlocked" ───┘──► unlocked
/// └── power off / chandra-ctl lock cancel: "unlock-failed" ──► stay locked
///| Whether the session is locked right now.
pub fn ChandraCompositor::is_locked(self : ChandraCompositor) -> Bool {
self.locked
}
///| Lock the session: hide the desktop and demand a face or password to return. Safe to call
/// when already locked. If the screen is on, the unlock prompt comes up at once; if it is off
/// (locked at blank), it comes up when the power button wakes the screen.
pub fn ChandraCompositor::lock_now(self : ChandraCompositor) -> Unit {
if self.locked {
return
}
// Dismiss any unrelated auth prompt before locking the session.
if self.auth is Some(_) {
self.close_auth(true)
}
self.locked = true
self.unlock_pending = false
// Nothing on the desktop keeps the keyboard or the pointer while locked.
self.set_kbd_focus(None)
self.leave_pointer()
self.osk_manual_show = false
self.osk_sync()
self.damage.mark_full()
self.frame_changed = true
if !self.manual_screen_off && !self.screen_off {
self.request_unlock()
}
}
///| Ask the agent to run an unlock check. No-op if not locked, already asked, or no agent runs
/// (then the session simply stays locked until an agent is available and input re-raises it).
pub fn ChandraCompositor::request_unlock(self : ChandraCompositor) -> Unit {
if !self.locked || self.unlock_pending {
return
}
match self.auth_agent {
Some(l) => {
ignore(l.conn.send(["unlock"]))
self.unlock_pending = true
self.damage.mark_full()
self.frame_changed = true
}
None => ()
}
}
///| Cancel any active or pending unlock dialog (used when turning the screen off while locked).
pub fn ChandraCompositor::cancel_unlock(self : ChandraCompositor) -> Unit {
if self.auth is Some(_) {
self.close_auth(true)
}
if self.unlock_pending {
match self.auth_agent {
Some(l) => ignore(l.conn.send(["cancel-unlock"]))
None => ()
}
self.unlock_pending = false
self.damage.mark_full()
self.frame_changed = true
}
}
///| The agent said the unlock succeeded: return to the desktop.
pub fn ChandraCompositor::unlocked(self : ChandraCompositor) -> Unit {
if !self.locked {
return
}
self.locked = false
self.unlock_pending = false
self.note_activity(@sys.monotonic_ms())
self.damage.mark_full()
self.frame_changed = true
}
///| The unlock attempt ended without success (the user cancelled, or it failed): stay locked.
/// The prompt is raised again by the next key/button press or wake, not in a tight loop here.
pub fn ChandraCompositor::unlock_failed(self : ChandraCompositor) -> Unit {
self.unlock_pending = false
self.damage.mark_full()
self.frame_changed = true
}
///| Draw the locked-screen backdrop and idle prompt when no auth card is open.
fn ChandraCompositor::lock_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] {
let ops : Array[@scene.DrawOp] = []
if !self.locked {
return ops
}
let sw = self.output_w
let sh = self.output_h
let bg = @scene.Rgba::new(10, 12, 18, 225)
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, sw, sh), bg, 0))
if self.auth is None {
let p = self.theme.palette
let fs = self.theme.font_size
let s = 44
let cx = sw / 2
let cy = sh / 2 - 20
lock_glyph(cx - s / 2, cy - s - 12, s, p.accent, bg, ops)
ops.push(@scene.DrawOp::Text(cx, cy + 8, "Screen locked", fs + 4, p.foreground, @scene.TextAlign::Center, true))
let sub = if self.unlock_pending { "Starting unlock…" } else { "Tap or press any key to unlock" }
ops.push(@scene.DrawOp::Text(cx, cy + 36, sub, fs - 1, p.foreground_muted, @scene.TextAlign::Center, false))
}
ops
}