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

+13
View File
@@ -44,6 +44,13 @@ pub(all) struct ChandraCompositor {
/// Also set the system-wide dark/light preference for apps (system_theme.mbt). The login
/// session turns it on; nested windows and tests leave it off.
mut sync_system_theme : Bool
/// Running as the login session (--drm): power actions act on the machine (power.mbt).
mut login_session : Bool
/// Power (power.mbt): when input last came (monotonic ms), whether the screen is off for being
/// idle, and whether this idle spell already suspended.
mut last_input_ms : UInt64
mut screen_off : Bool
mut idle_suspended : Bool
mut output_w : Int
mut output_h : Int
mut work_area : @wm.Rect
@@ -149,6 +156,10 @@ pub fn ChandraCompositor::new(
theme,
dark_theme: theme,
sync_system_theme: false,
login_session: false,
last_input_ms: @sys.monotonic_ms(),
screen_off: false,
idle_suspended: false,
output_w,
output_h,
work_area,
@@ -1122,6 +1133,7 @@ fn ChandraCompositor::run_shell_actions(self : ChandraCompositor) -> Unit {
@shell.ShellAction::Logout => self.quit_requested = true
@shell.ShellAction::ThemeChanged => self.apply_theme()
@shell.ShellAction::Screenshot(code) => self.screenshot_request(code)
@shell.ShellAction::SystemPower(a) => self.system_power(a)
@shell.ShellAction::SetLayout(m) =>
match m {
0 => self.set_layout_mode(@wm.LayoutMode::Tiling)
@@ -1204,6 +1216,7 @@ pub fn ChandraCompositor::handle_input(
ev : @nested.InputEvent,
time_ms : UInt,
) -> Unit {
self.note_activity(@sys.monotonic_ms())
if self.screenshot_input(ev, time_ms) {
return
}
+73 -2
View File
@@ -1221,10 +1221,10 @@ test "the settings module lists the wallpapers and switches the desktop to the o
assert_eq(comp.shell.slot_of("wallpaper"), "") // the standalone chooser is gone
assert_eq(comp.shell.wallpaper_sel, 0)
// A click opens the popup on its Session tab; the Desktop tab (the second of three 96 px
// tabs) lists the wallpapers below a heading. Nothing changes yet.
// tabs; four 72 px tabs) lists the wallpapers below a heading. Nothing changes yet.
click_module(comp, "session-settings", 1U)
let p0 = comp.shell.slot_popup_rect(slot)
click_at(comp, p0.x + 6 + 96 + 48, p0.y + 6 + 14, 3U)
click_at(comp, p0.x + 6 + 72 + 36, p0.y + 6 + 14, 3U)
let pr = comp.shell.slot_popup_rect(slot)
assert_true(pr.h >= 40 + 6 * 28) // heading and one row per wallpaper below the tab bar
assert_eq(comp.shell.wallpaper_sel, 0)
@@ -1443,3 +1443,74 @@ test "open_settings opens the settings module on the tab asked for" {
ignore(comp.compose_frame())
assert_eq(comp.shell.slot_popup_rect(slot).h, 40 + 3 * 28 + 6) // the three settings
}
///|
test "idle timeouts turn the screen off, suspend once per idle spell, and input wakes the screen" {
let comp = new_comp("powertick")
comp.shell.screen_off_min = 1
comp.shell.suspend_min = 2
comp.note_activity(0UL)
assert_eq(comp.power_tick(30000UL), NoChange)
assert_eq(comp.power_tick(60000UL), ScreenOff)
assert_eq(comp.power_tick(61000UL), NoChange)
assert_eq(comp.power_tick(120000UL), Suspend)
assert_eq(comp.power_tick(200000UL), NoChange) // not again in the same idle spell
comp.note_activity(210000UL)
assert_eq(comp.power_tick(210500UL), ScreenOn)
assert_eq(comp.power_tick(250000UL), NoChange)
// A new idle spell suspends again; 0 means never.
assert_eq(comp.power_tick(270000UL), ScreenOff)
assert_eq(comp.power_tick(330000UL), Suspend)
comp.shell.screen_off_min = 0
comp.shell.suspend_min = 0
comp.note_activity(400000UL)
assert_eq(comp.power_tick(400100UL), ScreenOn)
assert_eq(comp.power_tick(99999999UL), NoChange)
}
///|
test "the settings module's Power tab steps the timeouts and confirms power off" {
let comp = new_comp("powertab")
let slot = comp.shell.slot_of("session-settings")
assert_true(comp.shell.open_settings(Power))
ignore(comp.compose_frame())
let pr = comp.shell.slot_popup_rect(slot)
// Rows of 28 below the tab bar (40): [Battery heading, battery,] When idle, screen off,
// suspend, Now, Suspend, Restart, Power off.
let n = (pr.h - 40 - 6) / 28
assert_true(n == 7 || n == 9)
let first = n - 7
let row_y = fn(i : Int) { pr.y + 40 + (first + i) * 28 + 13 }
assert_eq(comp.shell.screen_off_min, 10)
click_at(comp, pr.x + 40, row_y(1), 5U) // screen off: 10 -> 15 min
assert_eq(comp.shell.screen_off_min, 15)
click_at(comp, pr.x + 40, row_y(2), 10U) // suspend: never -> 5 min
assert_eq(comp.shell.suspend_min, 5)
// Power off asks for a second click; outside the login session it only says so.
comp.shell.notice = ""
click_at(comp, pr.x + 40, row_y(6), 15U)
assert_eq(comp.shell.notice, "")
click_at(comp, pr.x + 40, row_y(6), 20U)
assert_true(comp.shell.notice.contains("login session"))
}
///|
test "power timeouts are saved to state.edn and restored" {
if @sys.getenv("CHANDRA_TEST_CONFIG") == "1" {
write_state_edn("{:note \"keep\"}\n")
let a = new_comp("pow-a")
a.shell.persist = true
a.restore_state()
assert_eq((a.shell.screen_off_min, a.shell.suspend_min), (10, 0)) // the defaults
a.shell.set_power_timeouts(5, 30)
let text = read_state_edn()
assert_true(text.contains(":power {:screen-off 5 :suspend 30}"))
assert_true(text.contains(":note \"keep\""))
let b = new_comp("pow-b")
b.shell.persist = true
b.restore_state()
assert_eq((b.shell.screen_off_min, b.shell.suspend_min), (5, 30))
b.shell.set_power_timeouts(10, 0)
assert_false(read_state_edn().contains(":power"))
}
}
+84
View File
@@ -0,0 +1,84 @@
///| Power in the login session: the screen goes off and the computer suspends after the minutes
/// without input the settings module's Power tab set (shell.screen_off_min / suspend_min), and the
/// tab's Suspend / Restart / Power off buttons. Suspend, power off and restart go through logind
/// (`systemctl`, else `loginctl`), so its inhibitors and polkit rules apply.
///
/// The main loop calls `power_tick` every pass. An idle loop still wakes about once a second
/// (IdleClock in compositor/app), so the timeouts fire within a second. CLOCK_MONOTONIC stops
/// while the machine is suspended, so after a resume the idle time hasn't grown past the suspend
/// timeout and the machine doesn't suspend again until there has been input and a new idle spell.
///| What `power_tick` wants the display / system to do now.
pub(all) enum PowerChange {
NoChange
ScreenOff
ScreenOn
Suspend
} derive(Eq, Debug)
///| Note input: it restarts the idle time (and wakes a screen that is off at the next tick).
pub fn ChandraCompositor::note_activity(self : ChandraCompositor, now_ms : UInt64) -> Unit {
self.last_input_ms = now_ms
}
///| Check the idle timeouts at monotonic time `now_ms`.
pub fn ChandraCompositor::power_tick(self : ChandraCompositor, now_ms : UInt64) -> PowerChange {
let idle = if now_ms > self.last_input_ms { now_ms - self.last_input_ms } else { 0UL }
let minutes = fn(m : Int) { m.to_uint64() * 60000UL }
let off = self.shell.screen_off_min
let susp = self.shell.suspend_min
if susp > 0 && idle >= minutes(susp) {
if !self.idle_suspended {
self.idle_suspended = true
return Suspend
}
} else {
self.idle_suspended = false
}
if off > 0 && idle >= minutes(off) {
if !self.screen_off {
self.screen_off = true
return ScreenOff
}
} else if self.screen_off {
self.screen_off = false
self.damage.mark_full()
return ScreenOn
}
NoChange
}
///| The command for a power action: 0 suspend, 1 power off, 2 restart.
pub fn power_command(action : Int) -> String? {
let verb = match action {
0 => "suspend"
1 => "poweroff"
2 => "reboot"
_ => return None
}
Some("systemctl " + verb + " || loginctl " + verb)
}
///| Suspend, power off or restart now. Only the login session does it: in a nested window (or a
/// test) a notice says so instead, since it would act on the machine the outer desktop runs on.
pub fn ChandraCompositor::system_power(self : ChandraCompositor, action : Int) -> Unit {
match power_command(action) {
None => ()
Some(cmd) =>
if self.login_session {
if !@sys.spawn(cmd, self.shell.display) {
self.shell.say("Could not run " + cmd)
}
} else {
self.shell.say("Suspend, restart and power off work in the login session only")
}
}
}
///|
test "power commands go through logind" {
assert_eq(power_command(0), Some("systemctl suspend || loginctl suspend"))
assert_eq(power_command(1), Some("systemctl poweroff || loginctl poweroff"))
assert_eq(power_command(2), Some("systemctl reboot || loginctl reboot"))
assert_eq(power_command(3), None)
}