Files
chandra/compositor/host/power.mbt
Claude 2636373a4f
CI / test (push) Failing after 18s
Power settings: screen off and suspend when idle, battery, suspend / restart / power off
- 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
2026-11-25 19:37:21 +00:00

85 lines
3.1 KiB
MoonBit

///| 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)
}