From b293ecb51ad9aef918d54cf75f504b5bea4010ad Mon Sep 17 00:00:00 2001 From: nandithebull Date: Mon, 30 Nov 2026 16:47:03 -0800 Subject: [PATCH] Backlight: don't ask the host for the environment while instantiating The wasm compositor trapped at start since e2dc2bb: a top-level value called getenv. Co-Authored-By: Claude Opus 5.5 --- compositor/shell/backlight.mbt | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/compositor/shell/backlight.mbt b/compositor/shell/backlight.mbt index 1c90f72..bb86f89 100644 --- a/compositor/shell/backlight.mbt +++ b/compositor/shell/backlight.mbt @@ -1,6 +1,27 @@ ///| Linux backlight provider for the `power` capability (sysfs read + logind SetBrightness fallback). -let backlight_root : Ref[String] = { val: "/sys/class/backlight" } +///| 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" + } +} + +///| The directory in use; "" until it is first needed (a wasm component can't call the host while +/// it is being instantiated, which is when top-level values are made). +let backlight_root_set : Ref[String] = { val: "" } + +///| +fn backlight_root() -> String { + if backlight_root_set.val == "" { + backlight_root_set.val = default_backlight_root() + } + backlight_root_set.val +} let backlight_sample_sec : Ref[Int] = { val: -1 } @@ -8,7 +29,7 @@ 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_root_set.val = dir backlight_sample_sec.val = -1 backlight_cached.val = -1 } @@ -94,7 +115,7 @@ fn clamp_pct(pct : Int, lo : Int, hi : Int) -> Int { } ///| Read backlight brightness percentage in 0..100, or -1 if no backlight exists. -pub fn read_backlight_percent(root? : String = backlight_root.val) -> Int { +pub fn read_backlight_percent(root? : String = backlight_root()) -> Int { match find_backlight(root) { None => -1 Some(dev) => clamp_pct(((dev.cur * 100L + dev.max / 2L) / dev.max).to_int(), 0, 100) @@ -115,7 +136,7 @@ pub fn read_backlight_polled() -> Int { pub fn write_backlight_percent( pct : Int, display : String, - root? : String = backlight_root.val, + root? : String = backlight_root(), ) -> Int { match find_backlight(root) { None => -1 @@ -124,7 +145,9 @@ pub fn write_backlight_percent( 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") - if @sys.write_file_bytes(dev.path + "/brightness", data) < 0 { + // 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