Files
chandra/compositor/shell/backlight.mbt
T
nandiandClaude Opus 5.5 e2dc2bb930
CI / test (push) Successful in 1m21s
Deploy to the handset / build-and-deploy (push) Failing after 1m29s
Tests never change the real screen's brightness
Tests that open the Face unlock window or the face auth view turned the fill light on: they read
the real /sys/class/backlight, set 90 % and restored it on close, and since the sysfs file is
root-owned the write went to logind's SetBrightness over the real system bus. With tests in
parallel the screen flickered between levels, and an aborted test left it at 90 %.

- The backlight root comes from CHANDRA_BACKLIGHT_ROOT when set (default_backlight_root);
  test-native.py gives every test its own fake intel_backlight (25 of 100).
- Under CHANDRA_TEST_CONFIG=1 the logind SetBrightness fallback is never spawned.
- test-native.py drops XDG_SESSION_ID and points DBUS_SYSTEM_BUS_ADDRESS nowhere; tests that
  want a system bus give their own.
- Tests that set a fake root put the default back, not the real /sys/class/backlight.

Checked: the face, auth and backlight tests leave /sys/class/backlight/intel_backlight/brightness
untouched (value and mtime) while they run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 12:24:33 -08:00

165 lines
4.4 KiB
MoonBit

///| Linux backlight provider for the `power` capability (sysfs read + logind SetBrightness fallback).
///| Where backlight devices are: /sys/class/backlight, or $CHANDRA_BACKLIGHT_ROOT (the test runner
/// gives every test a fake one, so no test changes the real screen's brightness).
pub fn default_backlight_root() -> String {
let env = @sys.getenv("CHANDRA_BACKLIGHT_ROOT")
if env.length() > 0 {
env
} else {
"/sys/class/backlight"
}
}
let backlight_root : Ref[String] = { val: default_backlight_root() }
let backlight_sample_sec : Ref[Int] = { val: -1 }
let backlight_cached : Ref[Int] = { val: -1 }
///| Override /sys/class/backlight root for unit tests.
pub fn set_backlight_root(dir : String) -> Unit {
backlight_root.val = dir
backlight_sample_sec.val = -1
backlight_cached.val = -1
}
let backlight_names : Array[String] = [
"ae94000.dsi.0",
"intel_backlight",
"amdgpu_bl0",
"amdgpu_bl1",
"acpi_video0",
"acpi_video1",
"nvidia_0",
"nvidia_wmi_ec_backlight",
"apple-panel-bl",
"gmux_backlight",
"backlight",
"panel_backlight",
"pwm-backlight",
"qcom-wled",
"soc:backlight",
"rpi_backlight",
]
priv struct BacklightDev {
name : String
path : String
cur : Int64
max : Int64
}
fn safe_backlight_name(s : String) -> Bool {
if s.length() == 0 {
return false
}
for ch in s {
let ok = (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '.' ||
ch == '_' ||
ch == ':' ||
ch == '-'
if !ok {
return false
}
}
!s.has_prefix(".")
}
fn try_backlight(root : String, name : String) -> BacklightDev? {
if !safe_backlight_name(name) {
return None
}
let path = root + "/" + name
let max = sysfs_num(path + "/max_brightness")
if max <= 0L {
return None
}
let cur = sysfs_num(path + "/brightness")
if cur < 0L {
return None
}
Some({ name, path, cur, max })
}
fn find_backlight(root : String) -> BacklightDev? {
let override_name = @sys.getenv("CHANDRA_BACKLIGHT").trim(chars=" \n\r\t").to_string()
match try_backlight(root, override_name) {
Some(dev) => return Some(dev)
None => ()
}
for name in backlight_names {
match try_backlight(root, name) {
Some(dev) => return Some(dev)
None => ()
}
}
None
}
fn clamp_pct(pct : Int, lo : Int, hi : Int) -> Int {
if pct < lo { lo } else if pct > hi { hi } else { pct }
}
///| Read backlight brightness percentage in 0..100, or -1 if no backlight exists.
pub fn read_backlight_percent(root? : String = backlight_root.val) -> Int {
match find_backlight(root) {
None => -1
Some(dev) => clamp_pct(((dev.cur * 100L + dev.max / 2L) / dev.max).to_int(), 0, 100)
}
}
///| Sample backlight at most once per wall-clock second.
pub fn read_backlight_polled() -> Int {
let now = wall_secs()
if now != backlight_sample_sec.val {
backlight_sample_sec.val = now
backlight_cached.val = read_backlight_percent()
}
backlight_cached.val
}
///| Write display backlight percentage (clamped to 5..100) via sysfs or systemd-logind SetBrightness.
pub fn write_backlight_percent(
pct : Int,
display : String,
root? : String = backlight_root.val,
) -> Int {
match find_backlight(root) {
None => -1
Some(dev) => {
let clamped = clamp_pct(pct, 5, 100)
let raw0 = (clamped.to_int64() * dev.max + 50L) / 100L
let raw = if raw0 < 1L { 1L } else if raw0 > dev.max { dev.max } else { raw0 }
let data = @utf8.encode(raw.to_string() + "\n")
// Under test (CHANDRA_TEST_CONFIG=1) never ask logind: that would change the real screen.
if @sys.write_file_bytes(dev.path + "/brightness", data) < 0 &&
@sys.getenv("CHANDRA_TEST_CONFIG") != "1" {
let sid = @sys.getenv("XDG_SESSION_ID").trim(chars=" \n\r\t").to_string()
let sess = if sid.length() > 0 && safe_backlight_name(sid) {
"/org/freedesktop/login1/session/" + sid
} else {
"/org/freedesktop/login1/session/auto"
}
ignore(
@sys.spawn(
"busctl call org.freedesktop.login1 " +
sess +
" org.freedesktop.login1.Session SetBrightness ssu backlight " +
dev.name +
" " +
raw.to_string(),
display,
),
)
}
backlight_sample_sec.val = wall_secs()
backlight_cached.val = clamped
clamped
}
}
}