Files
nandi 40c5d6722c
CI / test (push) Failing after 51s
Face unlock: screen fill-light for low light; fix Quad-Bayer AE ramp
- Boost the backlight to 90% while enrolling with the light on or while sign-in is scanning, then restore the previous level when that ends or the window closes.
- Add a Light: On/Off toggle (L key) to the Face window; the backdrop turns soft white while the light is on.
- Quad-Bayer AE: compute the low-light gain once and cap it by the highlight limit, so ramp-up and highlight back-off agree.
- Tests: backlight round-trip through the Face window, Quad-Bayer debayer color/low-light/dark-frame cases, and the AE ramp, back-off and restore order.
2026-11-28 18:32:15 -08:00

269 lines
12 KiB
MoonBit

///| Face unlock in Settings > Session (docs/face-unlock.md), against a fake chandra-face: a shell
/// script that answers `status` from files and changes them for enroll / remove / pam, the way
/// the real one does after run0 or pkexec. CHANDRA_AUTH_SHOTS=dir also saves a PNG of the tab.
///| Write the fake tool into `dir` and return its path.
fn fake_face_tool(dir : String) -> String {
let script =
#|#!/bin/sh
#|S="$(dirname "$0")"
#|case "$1" in
#|status)
#| echo "backend opencv"; echo "device /dev/video2"
#| echo "pam $(cat "$S/pam" 2>/dev/null)"
#| cat "$S/faces" 2>/dev/null ;;
#|enroll)
#| echo "info Look at the camera…"
#| for i in 1 2 3 4 5; do echo "progress $i/5"; done
#| printf 'face 2\tFace 2\t2026-11-26\n' >> "$S/faces"
#| echo "done Added Face 2" ;;
#|remove)
#| grep -v "^face $2 " "$S/faces" > "$S/faces.new"; mv "$S/faces.new" "$S/faces"
#| echo "done Removed face $2" ;;
#|camera)
#| # The Face unlock window's camera: a grey 4x3 picture with a face box, a white 2x2
#| # thumbnail per snap, and an apply that changes the faces file.
#| pic() { printf 'frame 4 3 1 0 2 2 36\n'; head -c 36 /dev/zero | tr '\000' '\200'; }
#| if [ -e "$S/nocam" ]; then
#| echo "error no camera at /dev/v4l/by-id/usb-Old-video-index0 (not there: unplugged, or its name differs)"
#| printf 'camera /dev/v4l/by-id/usb-EMEET-video-index0\tEMEET SmartCam C960\t\n'
#| printf 'camera /dev/v4l/by-id/usb-EMEET-video-index1\tEMEET SmartCam C960\tnot a picture node\n'
#| exit 2
#| fi
#| echo "info Look at the camera and press Snap"; pic
#| n=0; kept=0
#| while read -r cmd arg; do
#| case "$cmd" in
#| snap) n=$((n+1)); kept=$((kept+1)); printf 'snap %d 2 2 12\n' $n; head -c 12 /dev/zero | tr '\000' '\377'; pic ;;
#| drop) kept=$((kept-1)); pic ;;
#| apply)
#| echo "info Waiting for the password…"
#| # With "$S/wait": hold on until the test's password dialog has been answered.
#| if [ -e "$S/wait" ]; then t=0; while [ ! -e "$S/go" ] && [ $t -lt 200 ]; do sleep 0.05; t=$((t+1)); done; fi
#| # With "$S/refuse": the password was not given; the camera goes on.
#| if [ -e "$S/refuse" ]; then
#| echo "error Nothing changed: the administrator password wasn't given"; echo "applied 1"; pic; continue
#| fi
#| for id in $(echo "$arg" | tr ',' ' '); do
#| grep -v "^face $id " "$S/faces" > "$S/faces.new"; mv "$S/faces.new" "$S/faces"
#| done
#| [ $kept -gt 0 ] && printf 'face 2\tFace 2\t2026-11-26\n' >> "$S/faces"
#| echo "$kept $arg" > "$S/applied"
#| echo "done Added Face 2 and removed 1 face"; echo "applied 0"; exit 0 ;;
#| quit) exit 0 ;;
#| esac
#| done ;;
#|set-camera)
#| echo "$2" > "$S/camera"; rm -f "$S/nocam"; echo "done Camera: EMEET SmartCam C960" ;;
#|pam)
#| if [ "$2" = enable ]; then echo "polkit-1,sudo" > "$S/pam"; else : > "$S/pam"; fi
#| echo "done ok" ;;
#|esac
ignore(@sys.mkdir_p(dir))
let tool = dir + "/chandra-face"
ignore(@sys.write_file_bytes(tool, @sys.str_to_utf8_bytes(script)))
ignore(@sys.write_file_bytes(dir + "/faces", @sys.str_to_utf8_bytes("face 1\tDesk\t2026-11-20\n")))
tool
}
///| Compose frames until `cond` holds (the tool runs on a socket), at most ~3 s.
fn face_settle(comp : @host.ChandraCompositor, cond : () -> Bool) -> Bool {
for i = 0; i < 300; i = i + 1 {
ignore(comp.compose_frame())
if cond() {
return true
}
@sys.sleep_ms(10)
}
false
}
///| The popup's texts.
fn face_texts(comp : @host.ChandraCompositor) -> Array[String] {
let slot = comp.shell.slot_of("session-settings")
comp.shell.slot_popup_texts(slot).map(t => t.0)
}
///| Click the popup text `label`.
fn face_click(comp : @host.ChandraCompositor, label : String, t : UInt) -> Unit raise {
let slot = comp.shell.slot_of("session-settings")
let pr = comp.shell.slot_popup_rect(slot)
match comp.shell.slot_popup_texts(slot).iter().find_first(x => x.0 == label) {
Some((_, x, y)) => {
comp.handle_input(@nested.InputEvent::PointerMove((pr.x + x + 4).to_double(), (pr.y + y).to_double()), t)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t + 1U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 2U)
ignore(comp.compose_frame())
}
None => fail("no \"" + label + "\" in the popup")
}
}
///|
test "Settings > Session: face unlock lists faces and turns it on" {
let dir = "/tmp/chandra-face-test-" + @sys.getpid().to_string()
let tool = fake_face_tool(dir)
let comp = new_comp("face")
// spawn-socket runs a program; the script has no x bit (sys has no chmod): through sh.
comp.shell.set_face_tool(["/bin/sh", tool])
assert_true(comp.shell.open_settings(Session))
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Desk") }))
let texts = face_texts(comp)
assert_true(texts.contains("Face unlock"))
assert_true(texts.contains("Use my face for passwords"))
assert_true(texts.contains("added 2026-11-20"))
assert_true(texts.contains("Add or remove faces…"))
assert_false(comp.shell.face.in_use())
// The switch: polkit-1 and sudo try the face first.
face_click(comp, "Use my face for passwords", 20U)
assert_true(face_settle(comp, fn() { comp.shell.face.in_use() }))
let shots = @sys.getenv("CHANDRA_AUTH_SHOTS")
if shots != "" {
ignore(@sys.write_file_bytes(shots + "/16-settings-face.png", @renderer.encode_png_compressed(comp.compose_frame())))
}
comp.close()
}
///|
fn face_key(comp : @host.ChandraCompositor, code : UInt, t : UInt) -> Unit {
comp.handle_input(@nested.InputEvent::Key(code, true), t)
comp.handle_input(@nested.InputEvent::Key(code, false), t + 1U)
ignore(comp.compose_frame())
}
///|
test "the Face unlock window: live picture, snaps, a face marked for removal, one Apply" {
let dir = "/tmp/chandra-face-win-" + @sys.getpid().to_string()
let tool = fake_face_tool(dir)
let comp = new_comp("facewin")
comp.shell.set_face_tool(["/bin/sh", tool])
assert_true(comp.shell.open_settings(Session))
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Desk") }))
// "Remove…" on a face opens the window with that face marked; nothing is removed yet.
face_click(comp, "Remove…", 10U)
assert_true(face_settle(comp, fn() { comp.shell.face.window.map(w => w.picture != 0U).unwrap_or(false) }))
let w = comp.shell.face.window.unwrap()
assert_eq(w.remove, ["1"])
assert_eq((w.pic_w, w.pic_h, w.face_box), (4, 3, Some((1, 0, 2, 2))))
assert_eq(w.message, "Look at the camera and press Snap")
// Space snaps (the window has the keyboard), twice; one snap is dropped again.
face_key(comp, 57U, 20U)
assert_true(face_settle(comp, fn() { w.snaps.length() == 1 }))
face_key(comp, 57U, 30U)
assert_true(face_settle(comp, fn() { w.snaps.length() == 2 }))
assert_eq(w.message, "2 pictures: snap more, or press Apply")
comp.shell.face.drop_snap(0)
assert_eq(w.snaps.map(s => s.id), ["2"])
assert_eq(w.message, "1 picture: turn a little and snap again, or press Apply")
let shots = @sys.getenv("CHANDRA_AUTH_SHOTS")
if shots != "" {
ignore(@sys.write_file_bytes(shots + "/17-face-window.png", @renderer.encode_png_compressed(comp.compose_frame())))
}
assert_eq(comp.shell.face.faces.length(), 1)
// A refused password keeps the window, the snap and the mark, and says why.
ignore(@sys.write_file_bytes(dir + "/refuse", b""))
face_key(comp, 28U, 35U)
assert_true(face_settle(comp, fn() { !w.applying && w.error }))
assert_eq(w.message, "Nothing changed: the administrator password wasn't given")
assert_eq((w.snaps.length(), w.remove), (1, ["1"]))
@sys.unlink(dir + "/refuse")
// Enter applies: one run for the snap and the removal; the window closes, Settings shows it.
face_key(comp, 28U, 40U)
assert_true(face_settle(comp, fn() { comp.shell.face.window is None }))
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Face 2") }))
assert_eq(comp.shell.face.faces.map(f => f.1), ["Face 2"])
assert_eq(comp.shell.face.message, "Added Face 2 and removed 1 face")
assert_eq(@sys.read_file_bytes(dir + "/applied") |> @utf8.decode_lossy, "1 1\n")
// Esc cancels without changing anything.
face_click(comp, "Add or remove faces…", 50U)
assert_true(face_settle(comp, fn() { comp.shell.face.window is Some(_) }))
face_key(comp, 1U, 60U)
assert_true(comp.shell.face.window is None)
comp.close()
}
///|
test "the Face unlock window: a missing camera says why and offers the cameras found" {
let dir = "/tmp/chandra-face-nocam-" + @sys.getpid().to_string()
let tool = fake_face_tool(dir)
ignore(@sys.write_file_bytes(dir + "/nocam", b""))
let comp = new_comp("facenocam")
comp.shell.set_face_tool(["/bin/sh", tool])
assert_true(comp.shell.open_settings(Session))
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Desk") }))
face_click(comp, "Add or remove faces…", 10U)
assert_true(face_settle(comp, fn() { comp.shell.face.window.map(w => w.ended).unwrap_or(false) }))
let w = comp.shell.face.window.unwrap()
assert_true(w.error)
assert_true(w.message.has_prefix("no camera at /dev/v4l/by-id/usb-Old-video-index0"))
assert_eq(w.cameras.length(), 2)
assert_eq(w.cameras[1].2, "not a picture node")
let shots = @sys.getenv("CHANDRA_AUTH_SHOTS")
if shots != "" {
ignore(@sys.write_file_bytes(shots + "/19-face-no-camera.png", @renderer.encode_png_compressed(comp.compose_frame())))
}
// "Use" saves it (as root) and opens the window again, now with a picture.
comp.shell.face.use_camera(w.cameras[0].0)
assert_true(face_settle(comp, fn() { comp.shell.face.window.map(w => w.picture != 0U).unwrap_or(false) }))
assert_eq(@sys.read_file_bytes(dir + "/camera") |> @utf8.decode_lossy, "/dev/v4l/by-id/usb-EMEET-video-index0\n")
comp.shell.face.cancel_window()
comp.close()
}
///|
test "face unlock status lines" {
let f = @shell.FaceUnlock::new()
f.apply_status([
"backend opencv", "device /dev/video0", "problem OpenCV is missing: run just install", "pam polkit-1,sudo",
"howdy login,gdm-password", "face 3\tGlasses\t2026-11-01",
])
assert_true(f.installed)
assert_eq(f.problem, "OpenCV is missing: run just install")
assert_true(f.in_use())
assert_eq(f.howdy, "login, gdm-password")
assert_eq(f.faces, [("3", "Glasses", "2026-11-01")])
// No tool: nothing is installed, whatever else was said.
f.apply_status([])
assert_false(f.installed)
assert_false(f.in_use())
assert_eq(@shell.face_progress("progress 2/5"), Some("Look at the camera… 2/5"))
assert_eq(@shell.face_progress("error couldn't see a face clearly"), Some("Couldn't see a face clearly"))
assert_eq(@shell.face_progress("face 1\tDesk\t2026-11-20"), None)
}
///|
test "the Face unlock window: screen fill-light boosts backlight and restores on close" {
let root = "/tmp/chandra-test-face-backlight-" + @sys.getpid().to_string()
let dev = root + "/ae94000.dsi.0"
ignore(@sys.mkdir_p(dev))
assert_eq(@sys.write_file_bytes(dev + "/max_brightness", @utf8.encode("1000\n")), 0)
assert_eq(@sys.write_file_bytes(dev + "/brightness", @utf8.encode("500\n")), 0)
@shell.set_backlight_root(root)
let dir = "/tmp/chandra-face-light-" + @sys.getpid().to_string()
let tool = fake_face_tool(dir)
let comp = new_comp("facelight")
comp.shell.set_face_tool(["/bin/sh", tool])
assert_true(comp.shell.open_settings(Session))
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Desk") }))
face_click(comp, "Add or remove faces…", 10U)
assert_true(face_settle(comp, fn() { comp.shell.face.window.map(w => w.picture != 0U).unwrap_or(false) }))
let w = comp.shell.face.window.unwrap()
assert_true(w.fill_light)
assert_eq(@shell.read_backlight_percent(), 90)
// Pressing L toggles fill-light off and restores previous backlight immediately.
face_key(comp, 38U, 20U)
assert_false(w.fill_light)
assert_eq(@shell.read_backlight_percent(), 50)
// Pressing L again boosts back to 90.
face_key(comp, 38U, 30U)
assert_true(w.fill_light)
assert_eq(@shell.read_backlight_percent(), 90)
// Esc closes window and restores backlight cleanly.
face_key(comp, 1U, 40U)
ignore(comp.compose_frame())
assert_true(comp.shell.face.window is None)
assert_eq(@shell.read_backlight_percent(), 50)
comp.close()
@shell.set_backlight_root("/sys/class/backlight")
}