Face unlock window: live camera preview, Snap, thumbnails, one password on Apply
CI / test (push) Failing after 12m30s

Settings > Session "Add or remove faces…" (and "Remove…" on a face) opens a
compositor-drawn Face unlock window instead of running root steps one by one:

- chandra-face camera runs as the user (no password): it streams a small live
  picture with the face box and a cropped thumbnail per snap; the snaps'
  embeddings stay in that process, nothing is written to disk.
- Click a snap to drop it; mark saved faces Remove/Keep.
- Apply saves the snaps as one face and removes the marked ones in one root
  `chandra-face apply` (JSON on stdin, validated) through run0/pkexec: one
  password prompt.
- New raster host function image-pixels (host-wasm, wasmhost, native) for the
  window's pictures.

Tests: camera session and apply in face/test_face.py; the window end to end
against a fake tool in compositor/host/face_test.mbt. Docs explain why the
change itself still needs root.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
This commit is contained in:
Claude committed 2026-11-26 02:36:29 +00:00
1 parent 0a9787ae36
commit 9f7489cfea
74 files changed
+2617 -89

No files matched your search

+384 -11
View File
@@ -3,8 +3,14 @@
/// gets all of it from `chandra-face` (face/chandra-face), run on a socket so the compositor
/// never waits on it: `status` as the user (labels only, no face data), and enroll / remove /
/// `pam enable|disable` the same way, which re-run themselves through run0 or pkexec, so polkit
/// asks for an administrator's password (in Chandra's own dialog) before anything changes. The
/// camera pictures stay inside that root process.
/// asks for an administrator's password (in Chandra's own dialog) before anything changes.
///
/// Adding and removing faces happens in the Face unlock window (FaceWindow, drawn by the host in
/// compositor/host/face_window.mbt): `chandra-face camera` runs as the user, with no password,
/// and streams a small live picture with the face box; Snap keeps a picture (its embedding stays
/// in that process, its thumbnail comes here); saved faces can be marked for removal; Apply hands
/// all of it to one root `chandra-face apply`, so the password is asked once. The pictures only
/// live in memory: that process's and the images here, which are dropped when the window closes.
///| What the shell knows about face unlock, and the chandra-face run in progress.
pub(all) struct FaceUnlock {
@@ -32,6 +38,43 @@ pub(all) struct FaceUnlock {
/// The status was read at least once.
mut loaded : Bool
mut dirty : Bool
/// The Face unlock window, while it is open, and a count of its changes (the host redraws).
mut window : FaceWindow?
mut window_rev : Int
/// Image handles to reuse for the live picture (two, in turn, so a new picture is a changed
/// draw op) and for snap thumbnails.
pics : Array[UInt]
spare_thumbs : Array[UInt]
}
///| A snap kept in the Face unlock window: its id in the camera process, and its thumbnail.
pub(all) struct FaceSnap {
id : String
image : UInt
}
///| The Face unlock window: the camera process (`chandra-face camera`) and what it showed.
pub(all) struct FaceWindow {
mut fd : Int
inbuf : Array[Byte]
/// A binary message's header words while its bytes are still coming, and how many.
mut header : Array[String]
mut need : Int
/// The live picture (0 until the first one), its size, and the face box in it.
mut picture : UInt
mut pic_w : Int
mut pic_h : Int
mut face_box : (Int, Int, Int, Int)?
mut pictures : Int
snaps : Array[FaceSnap]
/// Ids of saved faces marked for removal.
remove : Array[String]
mut message : String
mut error : Bool
/// Apply was pressed: the root step runs (or polkit is asking).
mut applying : Bool
/// When the camera process ended without a picture: the window shows why.
mut ended : Bool
}
///|
@@ -60,6 +103,10 @@ pub fn FaceUnlock::new() -> FaceUnlock {
queue: [],
loaded: false,
dirty: true,
window: None,
window_rev: 0,
pics: [],
spare_thumbs: [],
}
}
@@ -255,10 +302,7 @@ pub fn FaceUnlock::request(self : FaceUnlock, code : Int) -> Unit {
return // one change at a time
}
match code {
-2 => {
self.message = "Waiting for the password…"
self.run(["enroll"], true)
}
-2 => self.open_window(None)
-3 => {
self.message = "Waiting for the password…"
self.run(["pam", "disable"], true)
@@ -267,10 +311,7 @@ pub fn FaceUnlock::request(self : FaceUnlock, code : Int) -> Unit {
self.message = "Waiting for the password…"
self.run(["pam", "enable"], true)
}
i if i >= 0 && i < self.faces.length() => {
self.message = "Waiting for the password…"
self.run(["remove", self.faces[i].0], true)
}
i if i >= 0 && i < self.faces.length() => self.open_window(Some(self.faces[i].0))
_ => return
}
self.dirty = true
@@ -283,6 +324,337 @@ pub fn FaceUnlock::close(self : FaceUnlock) -> Unit {
self.fd = -1
}
self.queue.clear()
self.close_window()
}
// ---- the Face unlock window ----
///| Open the Face unlock window (with saved face `mark` already marked for removal), starting
/// the camera as the user. When it is open already, only the mark is added.
pub fn FaceUnlock::open_window(self : FaceUnlock, mark : String?) -> Unit {
match self.window {
Some(w) => {
match mark {
Some(id) => if !w.remove.contains(id) { w.remove.push(id) }
None => ()
}
self.touch()
return
}
None => ()
}
let argv = self.tool.copy()
argv.append(["camera", "--machine"])
let fd = @sys.spawn_socket(argv)
let w : FaceWindow = {
fd,
inbuf: [],
header: [],
need: 0,
picture: 0U,
pic_w: 0,
pic_h: 0,
face_box: None,
pictures: 0,
snaps: [],
remove: match mark {
Some(id) => [id]
None => []
},
message: "Starting the camera…",
error: false,
applying: false,
ended: fd < 0,
}
if fd < 0 {
w.message = "Couldn't run chandra-face"
w.error = true
}
self.window = Some(w)
self.touch()
}
///| Something in the window changed: Settings and the host redraw.
fn FaceUnlock::touch(self : FaceUnlock) -> Unit {
self.dirty = true
self.window_rev = self.window_rev + 1
}
///| Send a command line to the camera process.
fn FaceWindow::send(self : FaceWindow, line : String) -> Unit {
if self.fd >= 0 {
@sys.write_nonblock(self.fd, @utf8.encode(line + "\n")) |> ignore
}
}
///| Keep the picture in view now.
pub fn FaceUnlock::snap(self : FaceUnlock) -> Unit {
match self.window {
Some(w) if !w.applying && !w.ended => w.send("snap")
_ => ()
}
}
///| Forget snap `i`.
pub fn FaceUnlock::drop_snap(self : FaceUnlock, i : Int) -> Unit {
match self.window {
Some(w) if !w.applying && i >= 0 && i < w.snaps.length() => {
let s = w.snaps.remove(i)
self.spare_thumbs.push(s.image)
w.send("drop " + s.id)
w.message = snapped_text(w.snaps.length())
w.error = false
self.touch()
}
_ => ()
}
}
///| Mark saved face `id` for removal, or unmark it.
pub fn FaceUnlock::toggle_remove(self : FaceUnlock, id : String) -> Unit {
match self.window {
Some(w) if !w.applying => {
if w.remove.contains(id) {
w.remove.retain(r => r != id)
} else {
w.remove.push(id)
}
self.touch()
}
_ => ()
}
}
///| Whether Apply has anything to do.
pub fn FaceWindow::can_apply(self : FaceWindow) -> Bool {
!self.applying && self.fd >= 0 && (self.snaps.length() > 0 || self.remove.length() > 0)
}
///| Save the snaps and remove the marked faces, in one root step (one password).
pub fn FaceUnlock::apply(self : FaceUnlock) -> Unit {
match self.window {
Some(w) if w.can_apply() => {
w.applying = true
w.message = "Waiting for the password…"
w.error = false
w.send("apply " + w.remove.join(","))
self.touch()
}
_ => ()
}
}
///| Close the window without changing anything (the camera stops). Not while Apply runs: the
/// root step can't be taken back half way.
pub fn FaceUnlock::cancel_window(self : FaceUnlock) -> Unit {
match self.window {
Some(w) if !w.applying || w.fd < 0 => self.close_window()
_ => ()
}
}
///| Close the window and drop its pictures.
fn FaceUnlock::close_window(self : FaceUnlock) -> Unit {
match self.window {
Some(w) => {
if w.fd >= 0 {
w.send("quit")
@sys.close_fd(w.fd)
w.fd = -1
}
for s in w.snaps {
self.spare_thumbs.push(s.image)
}
self.window = None
self.touch()
}
None => ()
}
}
///| Read what the camera process sent (non-blocking).
pub fn FaceUnlock::poll_window(self : FaceUnlock) -> Unit {
let w = match self.window {
Some(w) if w.fd >= 0 => w
_ => return
}
let buf : FixedArray[Byte] = FixedArray::make(65536, b'\x00')
let mut eof = false
let mut rounds = 0
// At most ~1 MB a call: a live picture is ~300 KB.
while rounds < 16 && @sys.poll_read(w.fd, 0) > 0 {
rounds = rounds + 1
let (n, extra) = @sys.recvmsg_fd(w.fd, buf, buf.length())
if extra >= 0 {
@sys.close_fd(extra)
}
if n == -1 {
break
}
if n <= 0 {
eof = true
break
}
for i = 0; i < n; i = i + 1 {
w.inbuf.push(buf[i])
}
}
let mut at = 0
while true {
if w.need > 0 {
if w.inbuf.length() - at < w.need {
break
}
let data = Bytes::from_array(w.inbuf[at:at + w.need].to_array())
at = at + w.need
w.need = 0
self.window_blob(w, w.header, data)
continue
}
let mut nl = -1
for i = at; i < w.inbuf.length(); i = i + 1 {
if w.inbuf[i] == b'\n' {
nl = i
break
}
}
if nl < 0 {
break
}
let line = @utf8.decode_lossy(Bytes::from_array(w.inbuf[at:nl].to_array()))
at = nl + 1
let words = line.split(" ").map(s => s.to_string()).to_array()
if (words[0] == "frame" || words[0] == "snap") && words.length() >= 2 {
let n = parse_int(words[words.length() - 1])
if n > 0 && n <= 4 * 1024 * 1024 {
w.header = words
w.need = n
}
continue
}
self.window_line(w, words[0], if line.length() > words[0].length() { line[words[0].length() + 1:].to_string() } else { "" })
}
if at > 0 {
let rest = w.inbuf[at:].to_array()
w.inbuf.clear()
w.inbuf.append(rest)
}
if eof && self.window is Some(_) {
@sys.close_fd(w.fd)
w.fd = -1
if w.applying {
// Apply ended (chandra-face said "applied" or went away): close and show the outcome.
self.message = if w.error || w.message == "Waiting for the password…" {
if w.message == "Waiting for the password…" { "Nothing changed" } else { w.message }
} else {
w.message
}
self.close_window()
self.refresh()
} else {
w.ended = true
if !w.error {
w.message = "The camera stopped"
w.error = true
}
self.touch()
}
}
}
///| A text line from the camera process.
fn FaceUnlock::window_line(self : FaceUnlock, w : FaceWindow, kind : String, text : String) -> Unit {
match kind {
"info" | "done" => {
w.message = text
w.error = false
}
"error" => {
w.message = text
w.error = true
}
"applied" => if text != "0" && !w.error { w.message = "Nothing changed" }
_ => ()
}
self.touch()
}
///| A picture from the camera process: the live one, or a snap's thumbnail.
fn FaceUnlock::window_blob(self : FaceUnlock, w : FaceWindow, header : Array[String], rgb : Bytes) -> Unit {
let num = (i : Int) => if i < header.length() { parse_int(header[i]) } else { -1 }
if header[0] == "frame" && header.length() >= 8 {
let pw = num(1)
let ph = num(2)
let rgba = rgb_to_rgba(rgb, pw, ph)
if rgba.length() == 0 {
return
}
// Two handles in turn: a new picture is a different image op, so the host redraws it.
while self.pics.length() < 2 {
self.pics.push(0U)
}
let k = w.pictures % 2
let h = @raster.image_pixels(pw, ph, rgba, self.pics[k])
self.pics[k] = h
w.picture = h
w.pic_w = pw
w.pic_h = ph
w.pictures = w.pictures + 1
w.face_box = if num(5) > 0 { Some((num(3), num(4), num(5), num(6))) } else { None }
if w.pictures == 1 && w.message == "Starting the camera…" {
w.message = "Look at the camera and press Snap"
}
} else if header[0] == "snap" && header.length() >= 5 {
let rgba = rgb_to_rgba(rgb, num(2), num(3))
if rgba.length() == 0 {
return
}
let reuse = self.spare_thumbs.pop().unwrap_or(0U)
let h = @raster.image_pixels(num(2), num(3), rgba, reuse)
w.snaps.push({ id: header[1], image: h })
w.message = snapped_text(w.snaps.length())
w.error = false
}
self.touch()
}
///| What the window says after a snap or a drop.
fn snapped_text(n : Int) -> String {
match n {
0 => "Look at the camera and press Snap"
1 => "1 picture: turn a little and snap again, or press Apply"
_ => n.to_string() + " pictures: snap more, or press Apply"
}
}
///| RGB bytes of a `w` x `h` picture as RGBA (empty when the sizes don't add up).
fn rgb_to_rgba(rgb : Bytes, w : Int, h : Int) -> Bytes {
if w <= 0 || h <= 0 || w > 1024 || h > 1024 || rgb.length() < w * h * 3 {
return b""
}
let out = FixedArray::make(w * h * 4, b'\xff')
for i = 0; i < w * h; i = i + 1 {
out[i * 4] = rgb[i * 3]
out[i * 4 + 1] = rgb[i * 3 + 1]
out[i * 4 + 2] = rgb[i * 3 + 2]
}
Bytes::from_fixedarray(out)
}
///| A non-negative decimal number, or -1.
fn parse_int(s : String) -> Int {
if s.length() == 0 || s.length() > 9 {
return -1
}
let mut n = 0
for ch in s {
let d = ch.to_int() - 48
if d < 0 || d > 9 {
return -1
}
n = n * 10 + d
}
n
}
///| Publish the face unlock tables (24: status, 25: faces) while a module imports `face-unlock`.
@@ -293,6 +665,7 @@ fn DesktopShell::publish_face_unlock(self : DesktopShell) -> Unit {
}
let f = self.face
f.poll()
f.poll_window()
if !f.dirty {
return
}
@@ -318,5 +691,5 @@ fn DesktopShell::publish_face_unlock(self : DesktopShell) -> Unit {
///| Whether a chandra-face run is in progress: the main loop keeps its frame pace meanwhile, so
/// what it says shows up (it runs seconds, while the camera looks).
pub fn DesktopShell::face_busy(self : DesktopShell) -> Bool {
self.face.fd >= 0
self.face.fd >= 0 || self.face.window is Some(_)
}