Power settings: screen off and suspend when idle, battery, suspend / restart / power off
CI / test (push) Failing after 18s

- Settings gets a Power tab (fourth; key 4 or SettingsTab::Power): the battery when there is
  one (the system capability), "Turn screen off after" (Never, 1 min ... 1 hour; default 10 min)
  and "Suspend after" (Never, 5 min ... 2 hours; default never), a click stepping through the
  choices, and Suspend / Restart / Power off now. Restart and Power off need a second click.
- New `power` capability (screen-off-minutes, suspend-minutes and setters, suspend, power-off,
  reboot), served by the wasmer stub (host ints 5/6, requests 13-15) and host-wasm
  (publish-power, set-screen-off / set-suspend-after / power-action).
- The timeouts are kept in state.edn as :power {:screen-off N :suspend N} when not the defaults.
- Login session only (compositor/host/power.mbt): input restarts the idle time; power_tick,
  run every pass of the main loop (an idle loop still wakes about once a second), turns the
  screen off through DPMS and back on at the next input, and suspends once per idle spell.
  Monotonic time stops in suspend, so a resumed machine waits for input and a new idle spell.
  Suspend, restart and power off run `systemctl <verb> || loginctl <verb>` (logind, so its
  inhibitors and polkit rules apply); a nested window only shows a notice.
- Display: chandra:host/display set-power and Kms::set_power; the shared C shim sets the
  connector's legacy DPMS property, shows no frames while off (frame_ready false, so the idle
  loop still sleeps), redoes the modeset when turned on, and keeps the screen off across a VT
  switch.
- components/screenshot bindings regenerated (they were stale: the module didn't build).
- Tests: idle timer sequence, the Power tab driving the shell (timeouts, two-click power off),
  the state.edn round trip, a host-wasm test of the tab, and logind commands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgxf6exeYrDGcy4WW3
This commit is contained in:
Claude committed 2026-11-25 19:37:21 +00:00
1 parent ba82d3c59b
commit 2636373a4f
40 files changed
+817 -77

No files matched your search

@@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
PopClip => {
mbt_ffi_store8((iter_base) + 0, (6))
()
}
Image(payload6) => {
mbt_ffi_store8((iter_base) + 0, (7))
mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x)
mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y)
mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int())
mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int())
mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int())
()
}
}
@@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
}
5 => ()
6 => ()
7 => ()
_ => panic()
}
@@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int {
PopClip => {
mbt_ffi_store8((iter_base) + 0, (6))
()
}
Image(payload8) => {
mbt_ffi_store8((iter_base) + 0, (7))
mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x)
mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y)
mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int())
mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int())
mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int())
()
}
}
@@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
}
5 => ()
6 => ()
7 => ()
_ => panic()
}
@@ -368,6 +390,11 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
}
///|
#owned(str)
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0)
///|
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
@@ -379,10 +406,6 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
///|
extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref)
///|
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
@@ -398,12 +421,11 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#| local.get 1 call $moonbit.init_array16
#| local.get 0)
///|
#owned(str)
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0)
///|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
///|
extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref)
@@ -4,6 +4,8 @@ import {
"chandra/desktop/interface/chandra/desktop/session-settings" @settings,
"chandra/desktop/interface/chandra/desktop/wallpaper" @wallpaper,
"chandra/desktop/interface/chandra/desktop/modules" @modules,
"chandra/desktop/interface/chandra/desktop/power" @power,
"chandra/desktop/interface/chandra/desktop/system" @system,
"chandra/desktop/lib/ui" @ui,
}
@@ -8,10 +8,13 @@
/// get their own section here;
/// - Panel: the modules in the panel (click to remove), every module found on the search path
/// (click to add another instance) and "Browse..." (`modules`). The panel menu's "Modules..."
/// opens this tab.
/// opens this tab;
/// - Power: the battery (`system`, when there is one), how long without input before the screen
/// turns off and before the computer suspends (a click steps through the choices), and Suspend,
/// Restart and Power off now (`power`); Restart and Power off ask for a second click.
///
/// Clicking a row acts at once and the popup stays open. Up/Down move the highlight, Left/Right
/// (or 1, 2, 3) switch tabs, Enter or Space acts on the highlighted row, Escape or a click
/// (or 1-4) switch tabs, Enter or Space acts on the highlighted row, Escape or a click
/// elsewhere closes. The shell remembers settings and wallpaper in state.edn, the panel in
/// panel.edn.
@@ -37,7 +40,7 @@ let switch_w : Int = 34
let switch_h : Int = 18
///|
let tab_names : Array[String] = ["Session", "Desktop", "Panel"]
let tab_names : Array[String] = ["Session", "Desktop", "Panel", "Power"]
///| A popup row.
enum Row {
@@ -49,6 +52,43 @@ enum Row {
/// A module on the search path: path, name and how many instances are loaded.
Available(String, String, UInt)
Browse
/// A line of text (the battery).
Info(String)
/// A timeout that steps through `choices`: 0 screen off, 1 suspend.
Timeout(Int, String)
/// Suspend (0), power off (1) or restart (2) now.
PowerNow(Int, String)
}
///| The timeout choices, in minutes (0: never).
let screen_off_choices : Array[Int] = [0, 1, 2, 5, 10, 15, 30, 60]
///|
let suspend_choices : Array[Int] = [0, 5, 10, 15, 30, 60, 120]
///| The power action waiting for its confirming second click (-1: none).
let armed : Ref[Int] = { val: -1 }
///| "Never", "5 min", "1 hour", "2 hours".
fn minutes_label(m : Int) -> String {
if m <= 0 {
"Never"
} else if m % 60 == 0 {
let h = m / 60
h.to_string() + (if h == 1 { " hour" } else { " hours" })
} else {
m.to_string() + " min"
}
}
///| The choice after `cur` in `choices` (wrapping round to the first).
fn next_choice(choices : Array[Int], cur : Int) -> Int {
for c in choices {
if c > cur {
return c
}
}
choices[0]
}
///| Whether the popup is open, its tab, and which row is highlighted (-1: none).
@@ -77,6 +117,21 @@ fn rows() -> Array[Row] {
rs.push(Wallpaper(i, names[i]))
}
}
3 => {
let snap = @system.read()
if snap.battery_percent >= 0 {
rs.push(Heading("Battery"))
let state = if snap.charging { " (charging)" } else { "" }
rs.push(Info(snap.battery_percent.to_string() + "%" + state))
}
rs.push(Heading("When idle"))
rs.push(Timeout(0, "Turn screen off after"))
rs.push(Timeout(1, "Suspend after"))
rs.push(Heading("Now"))
rs.push(PowerNow(0, "Suspend"))
rs.push(PowerNow(2, "Restart"))
rs.push(PowerNow(1, "Power off"))
}
_ => {
rs.push(Heading("In the panel (click to remove)"))
for inst in @modules.loaded() {
@@ -94,7 +149,7 @@ fn rows() -> Array[Row] {
///| Whether row `i` of `rs` can be highlighted and acted on (headings can't).
fn active(rs : Array[Row], i : Int) -> Bool {
i >= 0 && i < rs.length() && !(rs[i] is Heading(_))
i >= 0 && i < rs.length() && !(rs[i] is (Heading(_) | Info(_)))
}
///| First active row of `rs`, or -1.
@@ -138,12 +193,14 @@ fn popup_h(n : Int) -> Int {
///|
fn set_tab(i : Int) -> Unit {
tab.val = i
armed.val = -1
hover.val = first_active(rows())
}
///|
fn close() -> Unit {
open_.val = false
armed.val = -1
hover.val = -1
}
@@ -152,7 +209,24 @@ fn act(rs : Array[Row], i : Int) -> Unit {
if !active(rs, i) {
return
}
let was_armed = armed.val
armed.val = -1
match rs[i] {
Timeout(0, _) => @power.set_screen_off_minutes(next_choice(screen_off_choices, @power.screen_off_minutes().reinterpret_as_int()).reinterpret_as_uint())
Timeout(_, _) => @power.set_suspend_minutes(next_choice(suspend_choices, @power.suspend_minutes().reinterpret_as_int()).reinterpret_as_uint())
PowerNow(0, _) => @power.suspend()
PowerNow(a, _) =>
// Restart and power off take a second click on the same row.
if was_armed == a {
if a == 1 {
@power.power_off()
} else {
@power.reboot()
}
} else {
armed.val = a
}
Info(_) => ()
Setting(k, _) => {
let idx = k.reinterpret_as_uint()
@settings.set_setting(idx, !@settings.is_on(idx))
@@ -294,6 +368,16 @@ pub fn popup() -> PopupSurface? {
}
}
Browse => ops.push(@ui.text(margin + 10, mid, "Browse...", fs - 1, list_fg, bold=true))
Info(text) => ops.push(@ui.text(margin + 10, mid, text, fs - 1, p.foreground))
Timeout(k, label) => {
let m = if k == 0 { @power.screen_off_minutes() } else { @power.suspend_minutes() }
ops.push(@ui.text(margin + 10, mid, label, fs - 1, list_fg))
ops.push(@ui.text(w - margin - 10, mid, minutes_label(m.reinterpret_as_int()), fs - 1, list_fg, align=RIGHT, bold=true))
}
PowerNow(a, label) => {
let text = if armed.val == a { "Click again to " + label.to_lower() } else { label }
ops.push(@ui.text(margin + 10, mid, text, fs - 1, list_fg, bold=armed.val == a))
}
}
}
Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops })
@@ -371,7 +455,7 @@ pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool {
} else if k == "ArrowLeft" || k == "Left" {
set_tab((tab.val + tab_names.length() - 1) % tab_names.length())
true
} else if k == "1" || k == "2" || k == "3" {
} else if k == "1" || k == "2" || k == "3" || k == "4" {
set_tab(k[0].to_int() - '1'.to_int())
true
} else {