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

+9
View File
@@ -46,6 +46,15 @@ extern "C" fn c_close() = "mp_kms_close"
///|
extern "C" fn c_fd(i : Int) -> Int = "mp_kms_fd"
///|
extern "C" fn c_set_power(on : Int) = "mp_kms_set_power"
///| Turn the screen on or off (DPMS). While it is off no frame is shown (`frame_ready` is
/// false); on shows the last frame again.
pub fn Kms::set_power(_self : Kms, on : Bool) -> Unit {
c_set_power(if on { 1 } else { 0 })
}
///|
pub(all) struct Kms {
width : Int
+36 -2
View File
@@ -123,6 +123,7 @@ static int g_front; /* index currently scanned out */
static int g_flip_pending;
static int g_need_modeset;
static int g_width, g_height;
static int g_blank; /* screen turned off (DPMS) by mp_kms_set_power */
static int fb_create(struct fbuf *b, int w, int h) {
struct drm_mode_create_dumb c = { .width = (uint32_t)w, .height = (uint32_t)h, .bpp = 32 };
@@ -339,8 +340,40 @@ static void do_modeset(void) {
cursor_apply();
}
/* Legacy DPMS on the output's connector (the modeset path here is legacy too). */
static void set_dpms(int on) {
drmModeObjectProperties *props = drmModeObjectGetProperties(g_drm, g_conn, DRM_MODE_OBJECT_CONNECTOR);
if (!props) return;
for (uint32_t i = 0; i < props->count_props; i++) {
drmModePropertyRes *p = drmModeGetProperty(g_drm, props->props[i]);
if (!p) continue;
if (!strcmp(p->name, "DPMS")) {
if (drmModeConnectorSetProperty(g_drm, g_conn, p->prop_id, on ? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF) < 0)
fprintf(stderr, "kms: DPMS %s failed: %s\n", on ? "on" : "off", strerror(errno));
}
drmModeFreeProperty(p);
}
drmModeFreeObjectProperties(props);
}
/* ---- exported API -------------------------------------------------------- */
/* Turn the screen off (0) or on (1). While off, no frame is presented; on redoes the modeset so
* the last frame shows again. */
MOONBIT_FFI_EXPORT
void mp_kms_set_power(int32_t on) {
if (!g_seat || (on ? !g_blank : g_blank)) return;
g_blank = !on;
if (!g_seat_enabled || g_suspended) return; /* applied when the VT comes back */
if (on) {
set_dpms(1);
do_modeset();
} else {
set_dpms(0);
}
}
/* Open seat, DRM output and input. Returns 0 on success, negative step code on failure. */
MOONBIT_FFI_EXPORT
int32_t mp_kms_open(void) {
@@ -429,6 +462,7 @@ int32_t mp_kms_poll(void) {
drmSetMaster(g_drm);
if (g_li) libinput_resume(g_li);
do_modeset();
if (g_blank) set_dpms(0);
}
if (g_li) dispatch_input();
return g_seat_enabled;
@@ -450,13 +484,13 @@ int32_t mp_kms_fd(int32_t i) {
/* True when a new frame may be presented (seat active, no flip in flight). */
MOONBIT_FFI_EXPORT
int32_t mp_kms_frame_ready(void) {
return g_seat_enabled && !g_flip_pending;
return g_seat_enabled && !g_flip_pending && !g_blank;
}
/* Present an RGBA8888 frame of w x h (must match the mode). */
MOONBIT_FFI_EXPORT
int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h) {
if (!g_seat_enabled || g_flip_pending) return 0;
if (!g_seat_enabled || g_flip_pending || g_blank) return 0;
int back = 1 - g_front;
struct fbuf *b = &g_fb[back];
int cw = w < g_width ? w : g_width;
+6
View File
@@ -47,6 +47,12 @@ pub fn Kms::present_frame(_self : Kms) -> Unit {
@hostdisplay.present_frame()
}
///| Turn the screen on or off (DPMS). While it is off no frame is shown (`frame_ready` is
/// false); on shows the last frame again.
pub fn Kms::set_power(_self : Kms, on : Bool) -> Unit {
@hostdisplay.set_power(on)
}
///| Show the cursor for a wp_cursor_shape_v1 value.
pub fn Kms::set_cursor_shape(_self : Kms, shape : UInt) -> Unit {
@hostdisplay.set_cursor_shape(shape)