Add backlight control to the shell and regenerate WIT bindings
CI / test (push) Canceled after 0s

- Add compositor/shell/backlight.mbt for backlight control in the shell
- Update wit/capabilities.wit and wit/host/plasmoids.wit, then regenerate the component and interface ffi.mbt bindings (mostly export reordering)
- Add world/launcher-widget/README.md
This commit is contained in:
nandi committed 2026-11-28 02:16:07 -08:00
1 parent 2e00009c79
commit 74235cb0ca
84 files changed
+1525 -864

No files matched your search

+151
View File
@@ -0,0 +1,151 @@
///| Linux backlight provider for the `power` capability (sysfs read + logind SetBrightness fallback).
let backlight_root : Ref[String] = { val: "/sys/class/backlight" }
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")
if @sys.write_file_bytes(dev.path + "/brightness", data) < 0 {
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
}
}
}
+2
View File
@@ -303,6 +303,7 @@ pub(all) struct DesktopShell {
/// (0: never). Saved in state.edn as :power {:screen-off 10 :suspend 0} when not the defaults.
mut screen_off_min : Int
mut suspend_min : Int
mut brightness_pct : Int
/// Keyboard shortcuts (shortcuts.mbt); saved in state.edn as :shortcuts when not the defaults.
shortcuts : Shortcuts
/// The shortcut waiting for its new combo from the Shortcuts tab (-1: none).
@@ -481,6 +482,7 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
window_style: @scene.WindowStyle::Flat,
screen_off_min: default_screen_off_min,
suspend_min: default_suspend_min,
brightness_pct: -1,
shortcuts: Shortcuts::defaults(),
shortcut_capture: -1,
xdg_dirs: XdgDirs::from_env(),
+17
View File
@@ -782,6 +782,7 @@ fn DesktopShell::publish_host_data(self : DesktopShell) -> Unit {
}
if self.any_cap("power") {
@wasm.publish_power(self.screen_off_min, self.suspend_min)
self.refresh_brightness()
}
if self.any_cap("shortcuts") {
self.publish_shortcuts()
@@ -915,6 +916,21 @@ pub fn DesktopShell::set_power_timeouts(self : DesktopShell, screen_off : Int, s
self.save_state()
}
///| Refresh cached backlight brightness (0..100, or -1 if absent) and publish to modules.
pub fn DesktopShell::refresh_brightness(self : DesktopShell) -> Unit {
let pct = read_backlight_polled()
self.brightness_pct = pct
@wasm.publish_brightness(pct)
}
///| Set display backlight percentage (5..100) and publish the updated value.
pub fn DesktopShell::set_brightness(self : DesktopShell, pct : Int) -> Int {
let res = write_backlight_percent(pct, self.display)
self.brightness_pct = res
@wasm.publish_brightness(res)
res
}
///| Start the catalog app `id` (a desktop file id) and remember it as the latest launch.
fn DesktopShell::launch_catalog_app(self : DesktopShell, id : String) -> Unit {
match self.command_for(id) {
@@ -950,6 +966,7 @@ fn DesktopShell::process_requests(self : DesktopShell) -> Unit {
Some(Screenshot(code)) => self.actions.push(Screenshot(code))
Some(SetScreenOff(m)) => self.set_power_timeouts(m, self.suspend_min)
Some(SetSuspendAfter(m)) => self.set_power_timeouts(self.screen_off_min, m)
Some(SetBrightness(p)) => ignore(self.set_brightness(p))
Some(Shortcut(code)) => self.shortcut_request(code)
Some(DefaultApp(code)) => self.default_app_request(code)
Some(FaceUnlock(code)) => self.face.request(code)