CI / test (push) Canceled after 21m7s
"no camera at PATH" now says why (not there or a broken link, permission denied, not a V4L2 device, a metadata node rather than the picture node, OpenCV couldn't start it or it gives no picture: another app using it) and lists the cameras found (VIDIOC_QUERYCAP on /dev/v4l/by-id/* and /dev/video*). New commands: `chandra-face cameras`, `chandra-face set-camera PATH` (root, edits device in face.conf after checking it is a capture node). The Face unlock window shows the cameras found with a Use button (one password), then opens again. just install prints NOT READY with the reason when the configured camera can't be used. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
734 lines
21 KiB
MoonBit
734 lines
21 KiB
MoonBit
///| The `face-unlock` capability (docs/face-unlock.md): Settings > Session lists the session
|
|
/// user's enrolled faces and whether the password dialog and sudo try the face first. The shell
|
|
/// 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.
|
|
///
|
|
/// 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 {
|
|
/// How to run the tool: its path (CHANDRA_FACE_TOOL, else the installed helper; none in tests
|
|
/// unless set), or an interpreter and the path.
|
|
mut tool : Array[String]
|
|
/// The last status said "backend ..." (the tool runs).
|
|
mut installed : Bool
|
|
mut problem : String
|
|
/// PAM services using pam_chandra_face.so / Howdy's pam_howdy.so.
|
|
mut pam : Array[String]
|
|
mut howdy : String
|
|
/// (id, label, added) of each enrolled face.
|
|
mut faces : Array[(String, String, String)]
|
|
/// What the running change said last, or how the last one ended.
|
|
mut message : String
|
|
/// The run in progress: our end of its socket, what it has said so far, and whether it is a
|
|
/// change (the status is read again after one).
|
|
mut fd : Int
|
|
inbuf : Array[Byte]
|
|
mut lines : Array[String]
|
|
mut change : Bool
|
|
/// Runs waiting their turn (one at a time): (argv, is a change).
|
|
queue : Array[(Array[String], Bool)]
|
|
/// 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).
|
|
/// `reopen`: open it again when the running change (set-camera) ends.
|
|
mut reopen : Bool
|
|
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
|
|
/// The cameras chandra-face found when the configured one can't be used: (path, name, why
|
|
/// not; empty when it takes pictures). The window offers the usable ones.
|
|
cameras : Array[(String, String, String)]
|
|
}
|
|
|
|
///|
|
|
pub fn FaceUnlock::new() -> FaceUnlock {
|
|
let env = @sys.getenv("CHANDRA_FACE_TOOL")
|
|
{
|
|
tool: [
|
|
if env != "" {
|
|
env
|
|
} else if @sys.getenv("CHANDRA_TEST_CONFIG") != "" {
|
|
"/nonexistent/chandra-face" // tests don't see a chandra-face installed on the machine
|
|
} else {
|
|
"/usr/local/lib/chandra/chandra-face"
|
|
},
|
|
],
|
|
installed: false,
|
|
problem: "",
|
|
pam: [],
|
|
howdy: "",
|
|
faces: [],
|
|
message: "",
|
|
fd: -1,
|
|
inbuf: [],
|
|
lines: [],
|
|
change: false,
|
|
queue: [],
|
|
loaded: false,
|
|
dirty: true,
|
|
reopen: false,
|
|
window: None,
|
|
window_rev: 0,
|
|
pics: [],
|
|
spare_thumbs: [],
|
|
}
|
|
}
|
|
|
|
///| Run chandra-face as `argv` (tests: a fake, `["/bin/sh", script]`) and read the status again.
|
|
pub fn DesktopShell::set_face_tool(self : DesktopShell, argv : Array[String]) -> Unit {
|
|
self.face.tool = argv
|
|
self.face.loaded = false
|
|
self.face.refresh()
|
|
}
|
|
|
|
///| Whether the password dialog (polkit-1) tries the face first.
|
|
pub fn FaceUnlock::in_use(self : FaceUnlock) -> Bool {
|
|
self.pam.contains("polkit-1")
|
|
}
|
|
|
|
///| A change is running or waiting.
|
|
pub fn FaceUnlock::busy(self : FaceUnlock) -> Bool {
|
|
(self.fd >= 0 && self.change) || self.queue.iter().any(q => q.1)
|
|
}
|
|
|
|
///| Run `args` of the tool (after the one in progress).
|
|
fn FaceUnlock::run(self : FaceUnlock, args : Array[String], change : Bool) -> Unit {
|
|
let argv = self.tool.copy()
|
|
argv.append(args)
|
|
argv.push("--machine")
|
|
self.queue.push((argv, change))
|
|
self.next()
|
|
}
|
|
|
|
///| Start the next queued run when none is in progress.
|
|
fn FaceUnlock::next(self : FaceUnlock) -> Unit {
|
|
if self.fd >= 0 || self.queue.length() == 0 {
|
|
return
|
|
}
|
|
let (argv, change) = self.queue.remove(0)
|
|
self.lines = []
|
|
self.inbuf.clear()
|
|
self.change = change
|
|
self.fd = @sys.spawn_socket(argv)
|
|
if self.fd < 0 {
|
|
if change {
|
|
self.message = "Couldn't run chandra-face"
|
|
}
|
|
self.installed = false
|
|
self.loaded = true
|
|
self.dirty = true
|
|
self.next()
|
|
}
|
|
}
|
|
|
|
///| Read the status again.
|
|
pub fn FaceUnlock::refresh(self : FaceUnlock) -> Unit {
|
|
if self.queue.iter().any(q => !q.1) {
|
|
return
|
|
}
|
|
self.run(["status"], false)
|
|
}
|
|
|
|
///| Read what the run in progress said (non-blocking); finish it at EOF.
|
|
pub fn FaceUnlock::poll(self : FaceUnlock) -> Unit {
|
|
if self.fd < 0 {
|
|
return
|
|
}
|
|
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
|
|
let mut eof = false
|
|
while @sys.poll_read(self.fd, 0) > 0 {
|
|
let (n, extra) = @sys.recvmsg_fd(self.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 {
|
|
self.inbuf.push(buf[i])
|
|
}
|
|
}
|
|
// Whole lines; a change shows its latest one as it goes.
|
|
while true {
|
|
let nl = self.inbuf.search(b'\n').unwrap_or(-1)
|
|
if nl < 0 {
|
|
break
|
|
}
|
|
let line = @utf8.decode_lossy(Bytes::from_array(self.inbuf[:nl].to_array()))
|
|
let rest = self.inbuf[nl + 1:].to_array()
|
|
self.inbuf.clear()
|
|
self.inbuf.append(rest)
|
|
self.lines.push(line)
|
|
if self.change {
|
|
match face_progress(line) {
|
|
Some(m) => {
|
|
self.message = m
|
|
self.dirty = true
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
}
|
|
if !eof {
|
|
return
|
|
}
|
|
@sys.close_fd(self.fd)
|
|
self.fd = -1
|
|
if self.change {
|
|
if !self.lines.iter().any(l => l.has_prefix("done ") || l.has_prefix("error ")) {
|
|
// run0 / pkexec refused (no password, or it was cancelled) before the tool ran.
|
|
self.message = "Nothing changed"
|
|
}
|
|
self.change = false
|
|
self.dirty = true
|
|
self.refresh()
|
|
if self.reopen {
|
|
self.reopen = false
|
|
self.open_window(None)
|
|
}
|
|
} else {
|
|
self.apply_status(self.lines)
|
|
}
|
|
self.next()
|
|
}
|
|
|
|
///| What a change's line means for the Settings row ("Look at the camera… 3/5"), if anything.
|
|
pub fn face_progress(line : String) -> String? {
|
|
let (kind, text) = match line.find(" ") {
|
|
Some(i) => (line[:i].to_string(), line[i + 1:].to_string())
|
|
None => (line, "")
|
|
}
|
|
match kind {
|
|
"info" | "done" => Some(upper_first(text))
|
|
"error" => Some(upper_first(text))
|
|
"progress" => Some("Look at the camera… " + text)
|
|
_ => None
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn upper_first(s : String) -> String {
|
|
if s.length() == 0 {
|
|
s
|
|
} else {
|
|
s[:1].to_string().to_upper() + s[1:].to_string()
|
|
}
|
|
}
|
|
|
|
///| Take in `chandra-face status --machine` output.
|
|
pub fn FaceUnlock::apply_status(self : FaceUnlock, lines : Array[String]) -> Unit {
|
|
let mut installed = false
|
|
let mut problem = ""
|
|
let mut howdy = ""
|
|
let pam : Array[String] = []
|
|
let faces : Array[(String, String, String)] = []
|
|
for line in lines {
|
|
let (kind, text) = match line.find(" ") {
|
|
Some(i) => (line[:i].to_string(), line[i + 1:].to_string())
|
|
None => (line, "")
|
|
}
|
|
match kind {
|
|
"backend" => installed = true
|
|
"problem" => problem = text
|
|
"howdy" => howdy = text.replace_all(old=",", new=", ")
|
|
"pam" =>
|
|
for s in text.split(",") {
|
|
if s != "" {
|
|
pam.push(s.to_string())
|
|
}
|
|
}
|
|
"face" => {
|
|
let f = text.split("\t").map(s => s.to_string()).to_array()
|
|
if f.length() == 3 {
|
|
faces.push((f[0], f[1], f[2]))
|
|
}
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
self.installed = installed
|
|
self.problem = if installed { problem } else { "" }
|
|
self.howdy = howdy
|
|
self.pam = pam
|
|
self.faces = faces
|
|
self.loaded = true
|
|
self.dirty = true
|
|
}
|
|
|
|
///| A face-unlock request: a face index removes it; -1 refresh, -2 enroll, -3 / -4 stop / start
|
|
/// using it for the password dialog and sudo.
|
|
pub fn FaceUnlock::request(self : FaceUnlock, code : Int) -> Unit {
|
|
if code == -1 {
|
|
self.refresh()
|
|
return
|
|
}
|
|
if self.busy() {
|
|
return // one change at a time
|
|
}
|
|
match code {
|
|
-2 => self.open_window(None)
|
|
-3 => {
|
|
self.message = "Waiting for the password…"
|
|
self.run(["pam", "disable"], true)
|
|
}
|
|
-4 => {
|
|
self.message = "Waiting for the password…"
|
|
self.run(["pam", "enable"], true)
|
|
}
|
|
i if i >= 0 && i < self.faces.length() => self.open_window(Some(self.faces[i].0))
|
|
_ => return
|
|
}
|
|
self.dirty = true
|
|
}
|
|
|
|
///| Stop reading the run in progress (the shell is closing).
|
|
pub fn FaceUnlock::close(self : FaceUnlock) -> Unit {
|
|
if self.fd >= 0 {
|
|
@sys.close_fd(self.fd)
|
|
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,
|
|
cameras: [],
|
|
}
|
|
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()
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
|
|
///| Use the camera at `path` from now on (root: one password), then open the window again.
|
|
pub fn FaceUnlock::use_camera(self : FaceUnlock, path : String) -> Unit {
|
|
match self.window {
|
|
Some(w) if w.ended => {
|
|
self.close_window()
|
|
self.message = "Waiting for the password…"
|
|
self.reopen = true
|
|
self.run(["set-camera", path], true)
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
|
|
///| 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
|
|
}
|
|
"camera" => {
|
|
let f = text.split("\t").map(s => s.to_string()).to_array()
|
|
if f.length() == 3 {
|
|
w.cameras.push((f[0], f[1], f[2]))
|
|
}
|
|
}
|
|
"applied" =>
|
|
if text != "0" {
|
|
// Refused or cancelled: the camera starts again, the snaps and marks are still there.
|
|
w.applying = false
|
|
if !w.error {
|
|
w.message = "Nothing changed: the administrator password wasn't given"
|
|
w.error = true
|
|
}
|
|
}
|
|
_ => ()
|
|
}
|
|
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`.
|
|
/// Nothing runs until the module asks (`refresh`, when Settings opens on the Session tab).
|
|
fn DesktopShell::publish_face_unlock(self : DesktopShell) -> Unit {
|
|
if !self.any_cap("face-unlock") {
|
|
return
|
|
}
|
|
let f = self.face
|
|
f.poll()
|
|
f.poll_window()
|
|
if !f.dirty {
|
|
return
|
|
}
|
|
f.dirty = false
|
|
let st = @wasm.Table::new()
|
|
st.str(f.problem)
|
|
st.str(f.howdy)
|
|
st.str(f.message)
|
|
st.int(if f.installed { 1 } else { 0 })
|
|
st.int(if f.in_use() { 1 } else { 0 })
|
|
st.int(if f.busy() { 1 } else { 0 })
|
|
st.end_row()
|
|
@wasm.publish(FaceStatus, st)
|
|
let ft = @wasm.Table::new()
|
|
for face in f.faces {
|
|
ft.str(face.1)
|
|
ft.str(face.2)
|
|
ft.end_row()
|
|
}
|
|
@wasm.publish(Faces, ft)
|
|
}
|
|
|
|
///| 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.window is Some(_)
|
|
}
|