Files
chandra/wasmhost/bin/plasmoids.mbt
T
Claude 932fca627e
CI / test (push) Successful in 2m6s
Face unlock: a Howdy-style PAM module, shown in the password dialog and set up in Settings
- face/pam_chandra_face.so (`-auth sufficient`): runs `chandra-face compare` as root with a clean
  environment, forwards what it says as PAM_TEXT_INFO / PAM_ERROR_MSG, and maps its exit status
  to success / auth error / unavailable, so the password always still works. Skips remote
  sessions (PAM_RHOST, SSH_CONNECTION), a closed lid and users without faces; refuses a helper
  that isn't root's; kills one that hangs (timeout + 6 s).
- face/chandra-face (Python + OpenCV): YuNet finds the face, SFace embeds it, cosine similarity
  against the enrolled embeddings (threshold 0.42, two matching frames, dark IR warm-up frames
  skipped, optional require_ir). enroll / list / remove / clear / test / status / pam
  enable|disable / fetch-models (sha256-pinned opencv_zoo models). Embeddings only, in
  /var/lib/chandra-face/USER/faces.json (root, 0600); labels in faces.list (0644). Store, config
  and models must be root's and not writable by others. Root commands re-run through run0 (or
  pkexec), so polkit asks first. Settings in /etc/chandra/face.conf.
- `just install-face [--pam]`, `just uninstall-face [--purge]` (face/install.py; uninstall takes
  the module out of every PAM service first). `pam enable` inserts before the first auth or
  @include line (Debian's sudo has only @include common-auth), keeping a .bak.
- Password dialog: a face row ("Looking for your face…" with a sweeping ring, "Face
  recognized", or why not, "Type your password instead"); typing and Enter during the scan go in
  when PAM asks for the password. Howdy's pam_howdy.so messages are understood too.
- Settings > Session > Face unlock: "Use my face for passwords" (polkit-1 + sudo), the enrolled
  faces (Remove takes a second click), "Add a face…", and the tool's progress. New `face-unlock`
  capability: tables 24 (status) and 25 (faces), request 44, in the wasmer/wasmtime stub, the Rust
  host, the MoonBit wasmhost and chandra:host/plasmoids. compositor/shell/face.mbt runs the tool
  on a socket so the compositor never waits.
- Tests: face/test_face.py (fake camera of PGM frames + fake recognizer; --pam builds the module
  and runs it through pamtester), dialog states and message parsing (polkit_test), the dialog via
  a mock helper (auth_test, shots 14/15), Settings against a fake chandra-face (face_test).
  scripts/test.sh runs them (compositor/polkit was missing from its native list), CI installs
  libpam0g-dev + pamtester. Also regenerated two stale desktop-module bindings (doc comments).
- docs/face-unlock.md, including the limits of 2D face recognition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
2026-11-26 01:19:29 +00:00

574 lines
14 KiB
MoonBit

///| `chandra:host/plasmoids`: the panel modules. They run in this host, through compositor/wasm
/// (the native compositor's module runtime, on the wasm.h API that libwasmtime implements too),
/// so both builds serve modules the same capabilities from the same code.
///| compositor/wasm's shim: the rows behind table `which` (a LE u32 row count, then the rows).
#borrow(data)
extern "C" fn mp_host_set_table(which : Int, data : Bytes) = "mp_host_set_table"
///| Loaded modules by handle.
let modules : Map[Int, @wasm.Plasmoid] = {}
///| The theme the modules see (`set-tokens`).
let tokens : Ref[@theme.Tokens?] = Ref(None)
///|
fn default_tokens() -> @theme.Tokens {
let c : @types.Color = { r: b'\x00', g: b'\x00', b: b'\x00', a: b'\xff' }
{
name: "",
palette: {
desktop_top: c,
desktop_bottom: c,
surface: c,
surface_hover: c,
surface_popup: c,
foreground: c,
foreground_muted: c,
accent: c,
accent_foreground: c,
border: c,
},
font_size: 13,
radius: 6,
spacing: 8,
panel_height: 44,
}
}
///|
fn current_tokens() -> @theme.Tokens {
match tokens.val {
Some(t) => t
None => default_tokens()
}
}
// ---- values to and from the module types ----
///|
fn color_of(v : @wt.Val) -> @types.Color {
{
r: v.field("r").to_int().to_byte(),
g: v.field("g").to_int().to_byte(),
b: v.field("b").to_int().to_byte(),
a: v.field("a").to_int().to_byte(),
}
}
///|
fn tokens_of(v : @wt.Val) -> @theme.Tokens {
let p = v.field("palette")
{
name: v.field("name").to_string(),
palette: {
desktop_top: color_of(p.field("desktop-top")),
desktop_bottom: color_of(p.field("desktop-bottom")),
surface: color_of(p.field("surface")),
surface_hover: color_of(p.field("surface-hover")),
surface_popup: color_of(p.field("surface-popup")),
foreground: color_of(p.field("foreground")),
foreground_muted: color_of(p.field("foreground-muted")),
accent: color_of(p.field("accent")),
accent_foreground: color_of(p.field("accent-foreground")),
border: color_of(p.field("border")),
},
font_size: v.field("font-size").to_uint(),
radius: v.field("radius").to_uint(),
spacing: v.field("spacing").to_uint(),
panel_height: v.field("panel-height").to_uint(),
}
}
///|
fn color_val(c : @types.Color) -> @wt.Val {
Record([
("r", U8(c.r.to_int())),
("g", U8(c.g.to_int())),
("b", U8(c.b.to_int())),
("a", U8(c.a.to_int())),
])
}
///|
fn point_val(p : @types.Point) -> @wt.Val {
Record([("x", S32(p.x)), ("y", S32(p.y))])
}
///|
fn rect_val(r : @types.Rect) -> @wt.Val {
Record([("x", S32(r.x)), ("y", S32(r.y)), ("w", U32(r.w)), ("h", U32(r.h))])
}
///|
fn size_val(w : Int, h : Int) -> @wt.Val {
Record([
("w", U32(w.reinterpret_as_uint())),
("h", U32(h.reinterpret_as_uint())),
])
}
///|
fn op_val(op : @types.DrawOp) -> @wt.Val {
match op {
FillRect(f) =>
Variant(
"fill-rect",
Some(
Record([
("rect", rect_val(f.rect)),
("color", color_val(f.color)),
("radius", U32(f.radius)),
]),
),
)
StrokeRect(s) =>
Variant(
"stroke-rect",
Some(
Record([
("rect", rect_val(s.rect)),
("color", color_val(s.color)),
("width", U32(s.width)),
("radius", U32(s.radius)),
]),
),
)
Line(l) =>
Variant(
"line",
Some(
Record([
("start", point_val(l.start)),
("end", point_val(l.end)),
("color", color_val(l.color)),
("width", U32(l.width)),
]),
),
)
Circle(c) =>
Variant(
"circle",
Some(
Record([
("center", point_val(c.center)),
("radius", U32(c.radius)),
("color", color_val(c.color)),
("filled", Bool(c.filled)),
]),
),
)
Text(t) => {
let align = match t.style.align {
LEFT => "left"
CENTER => "center"
RIGHT => "right"
}
Variant(
"text",
Some(
Record([
("origin", point_val(t.origin)),
("text", Str(t.text)),
(
"style",
Record([
("size", U32(t.style.size)),
("color", color_val(t.style.color)),
("align", Enum(align)),
("bold", Bool(t.style.bold)),
]),
),
]),
),
)
}
PushClip(r) => Variant("push-clip", Some(rect_val(r)))
PopClip => Variant("pop-clip", None)
Image(i) =>
Variant(
"image",
Some(Record([("rect", rect_val(i.rect)), ("image", U32(i.image))])),
)
}
}
///|
fn ops_val(ops : Array[@types.DrawOp]) -> @wt.Val {
List(ops.map(op_val))
}
///|
fn event_of(v : @wt.Val) -> @wasm.PlasmoidEvent? {
guard v is Variant(name, payload) else { None }
let xy = fn() -> (Int, Int) {
match payload {
Some(p) => (p.field("x").to_int(), p.field("y").to_int())
None => (0, 0)
}
}
match name {
"pointer-move" => {
let (x, y) = xy()
Some(PointerMove(x, y))
}
"pointer-down" => {
let (x, y) = xy()
Some(PointerDown(x, y))
}
"pointer-up" => {
let (x, y) = xy()
Some(PointerUp(x, y))
}
"pointer-leave" => Some(PointerLeave)
"focus-lost" => Some(FocusLost)
"key-down" =>
match payload {
Some(k) => Some(KeyDown(k.to_string()))
None => None
}
_ => None
}
}
///|
fn request_val(r : @wasm.HostRequest) -> @wt.Val {
match r {
Launch(s) => Variant("launch", Some(Str(s)))
FocusWindow(n) => Variant("focus-window", Some(S32(n)))
SetLayout(n) => Variant("set-layout", Some(S32(n)))
SetInspector(b) => Variant("set-inspector", Some(Bool(b)))
LoadModule(s) => Variant("load-module", Some(Str(s)))
UnloadInstance(s) => Variant("unload-instance", Some(Str(s)))
MoveInstance(slot, delta) =>
Variant("move-instance", Some(Record([("slot", Str(slot)), ("delta", S32(delta))])))
Browse => Variant("browse", None)
Logout => Variant("logout", None)
SetWallpaper(n) => Variant("set-wallpaper", Some(S32(n)))
SetSessionSetting(index, on) =>
Variant(
"set-session-setting",
Some(Record([("index", S32(index)), ("on", Bool(on))])),
)
Screenshot(n) => Variant("screenshot", Some(S32(n)))
SetScreenOff(n) => Variant("set-screen-off", Some(S32(n)))
SetSuspendAfter(n) => Variant("set-suspend-after", Some(S32(n)))
PowerAction(n) => Variant("power-action", Some(S32(n)))
Network(action, ssid, password) =>
Variant(
"network",
Some(Record([("action", S32(action)), ("ssid", Str(ssid)), ("password", Str(password))])),
)
Shortcut(n) => Variant("shortcut", Some(S32(n)))
ReloadSession => Variant("reload-session", None)
DefaultApp(n) => Variant("default-app", Some(S32(n)))
SetDesktop(n) => Variant("set-desktop", Some(S32(n)))
FaceUnlock(n) => Variant("face-unlock", Some(S32(n)))
}
}
///| The file chooser "Add widget" opened, while it is open.
let picker : Ref[@wasm.Picker?] = Ref(None)
///|
fn with_module(a : Array[@wt.Val], f : (@wasm.Plasmoid) -> Array[@wt.Val], or : Array[@wt.Val]) -> Array[@wt.Val] {
match modules.get(a[0].to_int()) {
Some(m) => f(m)
None => or
}
}
///|
fn plasmoid_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
[
(
"load",
a => match @wasm.load(a[0].to_string(), current_tokens()) {
Ok(p) => {
modules.set(p.handle, p)
[
Result(
true,
Some(
Record([
("handle", U32(p.handle.reinterpret_as_uint())),
("id", Str(p.id)),
("name", Str(p.name)),
("preferred", size_val(p.pref_w, p.pref_h)),
("min-width", U32(p.min_w.reinterpret_as_uint())),
("stretch", Bool(p.stretch)),
("imports", @wt.Val::strings(p.imports)),
]),
),
),
]
}
Err(e) => [Result(false, Some(Str(e)))]
},
),
(
"unload",
a => {
let h = a[0].to_int()
match modules.get(h) {
Some(m) => {
m.unload()
modules.remove(h)
}
None => ()
}
none
},
),
(
"set-tokens",
a => {
tokens.val = Some(tokens_of(a[0]))
none
},
),
(
"render",
a => with_module(
a,
m => {
let b = a[1]
[ops_val(m.render(b.field("w").to_int(), b.field("h").to_int(), current_tokens()))]
},
[List([])],
),
),
(
"popup",
a => with_module(
a,
m => match m.popup() {
Some(p) =>
[
Option(
Some(Record([("box", size_val(p.w, p.h)), ("ops", ops_val(p.ops))])),
),
]
None => [Option(None)]
},
[Option(None)],
),
),
(
"on-event",
a => with_module(
a,
m => match event_of(a[2]) {
Some(ev) => boolean(m.on_event(a[1].to_bool(), ev))
None => boolean(false)
},
boolean(false),
),
),
(
"config-items",
a => with_module(
a,
m => [
List(
m
.config_items()
.map(c => Record([
("key", Str(c.key)),
("label", Str(c.label)),
("choices", @wt.Val::strings(c.choices)),
("value", Str(c.value)),
])),
),
],
[List([])],
),
),
(
"set-config",
a => with_module(
a,
m => boolean(m.set_config(a[1].to_string(), a[2].to_string())),
boolean(false),
),
),
(
"publish-table",
a => {
mp_host_set_table(a[0].to_int(), a[1].to_bytes())
none
},
),
(
"publish-state",
a => {
@wasm.publish_state(a[0].to_int(), a[1].to_bool())
none
},
),
(
"desktop-backdrop",
a => with_module(a, m => s32(m.desktop_backdrop()), s32(-1)),
),
(
"desktop-scroll",
a => with_module(
a,
m => boolean(
m.desktop_scroll(
a[1].to_int(),
a[2].to_int(),
a[3].to_int(),
a[4].to_int(),
),
),
boolean(false),
),
),
(
"desktop-tick",
a => with_module(
a,
m => [U32(m.desktop_tick(a[1].to_uint().reinterpret_as_int()).reinterpret_as_uint())],
[U32(0)],
),
),
(
"publish-desktop",
a => {
@wasm.publish_desktop(a[0].to_int())
none
},
),
(
"publish-wallpaper",
a => {
@wasm.publish_wallpaper(a[0].to_int())
none
},
),
(
"publish-session-settings",
a => {
@wasm.publish_session_settings(a[0].to_int())
none
},
),
(
"publish-power",
a => {
@wasm.publish_power(a[0].to_int(), a[1].to_int())
none
},
),
(
"publish-battery",
a => {
@wasm.publish_battery(a[0].to_int(), a[1].to_int(), a[2].to_int())
none
},
),
(
"publish-screenshot",
a => {
@wasm.publish_screenshot(a[0].to_int())
none
},
),
(
"publish-system",
a => {
let s = a[0]
let i64 = fn(name : String) -> Int64 {
match s.field(name) {
U64(v) => v.reinterpret_as_int64()
v => v.to_int().to_int64()
}
}
@wasm.set_system({
cpu_permille: s.field("cpu-permille").to_int(),
mem_used_kib: i64("mem-used-kib"),
mem_total_kib: i64("mem-total-kib"),
battery_percent: s.field("battery-percent").to_int(),
charging: s.field("charging").to_bool(),
uptime_secs: i64("uptime-secs"),
rx_bps: i64("net-rx-bytes-per-sec"),
tx_bps: i64("net-tx-bytes-per-sec"),
load_centi: s.field("load-centi").to_int(),
})
none
},
),
(
"publish-windows",
a => {
@wasm.set_windows(
a[0]
.to_list()
.map(w => {
id: match w.field("id") {
U64(v) => v
v => v.to_int().to_uint64()
},
title: w.field("title").to_string(),
app_id: w.field("app-id").to_string(),
focused: w.field("focused").to_bool(),
}),
)
none
},
),
(
"next-request",
_ => [
Option(
match @wasm.next_request() {
Some(r) => Some(request_val(r))
None => None
},
),
],
),
(
"take-focus-request",
_ => [
Option(
match @wasm.take_focus_request() {
Some(id) => Some(U64(id))
None => None
},
),
],
),
(
"list-wasm-files",
a => [@wt.Val::strings(@wasm.list_wasm_files(a[0].to_string()))],
),
(
"picker-start",
_ => {
picker.val = @wasm.Picker::start()
boolean(picker.val is Some(_))
},
),
(
"picker-poll",
_ => match picker.val {
None => [Variant("cancelled", None)]
Some(p) =>
match p.poll() {
None => [Variant("open", None)]
Some(None) => {
picker.val = None
[Variant("cancelled", None)]
}
Some(Some(path)) => {
picker.val = None
[Variant("chosen", Some(Str(path)))]
}
}
},
),
]
}