Battery panel module: charge, charging state, time left, low-battery warning
CI / test (push) Failing after 18s

- New `battery` panel module (components/battery, logic in lib/battery): a battery icon filled to
  the charge with the percentage and time remaining ("73% 2:05"; time to full while charging).
  The fill turns green when charging or full, amber at 20 % and below, and at 10 % the whole
  button turns red and a "Battery low" popup opens by itself, once per discharge spell. A click
  opens the details (charge, state, time left in words). Without a battery it shows a plug.
- Right-click "Module settings": Show percentage, Show time remaining, and Without a battery
  (Show a plug / Show nothing). Saved per module in state.edn like the clocks' settings.
- New `battery` capability (wit/capabilities.wit: read -> status {percent, charge-state,
  secs-left}). Served by the wasmer stub (host ints 7-9), host-wasm and wasmhost (publish-battery).
- Host side (compositor/shell/battery.mbt): reads /sys/class/power_supply, every battery
  together (energy_* / power_now, or charge_* / current_now; capacity when that is all there
  is; device-scope batteries like mice are skipped). The `system` snapshot the Power tab uses
  now reads the battery through the same code. Time left follows a running average of the
  rate, starting over when the state changes.
- A fresh panel gets the battery module before the clock only on a machine with a battery.
- wasm_stub: MAX_INST 512 -> 2048. The host test binary creates ~70 compositors without
  unloading their modules; at 512 the last tests' panels came up half empty.
- Tests: sysfs reading against a fake power supply (energy and charge units, two packs,
  charging, full, capacity only, no battery), the running average, the module in the panel
  (text, settings, click, the critical warning, the plug), the rendering logic, and a
  host-wasm test of the module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hky51dmaUqiYW746Gz
This commit is contained in:
Claude committed 2026-11-25 19:53:42 +00:00
1 parent 05fe70a2f6
commit 88915842c2
48 files changed
+2143 -17

No files matched your search

+18 -5
View File
@@ -15,7 +15,7 @@
#include <wasm.h>
#include "moonbit.h"
#define MAX_INST 512
#define MAX_INST 2048
enum { EX_INFO, EX_RENDER, EX_ONEVENT, EX_POPUP, EX_POST_INFO, EX_POST_RENDER, EX_POST_POPUP, EX_REALLOC,
EX_CFG_ITEMS, EX_POST_CFG_ITEMS, EX_SET_CFG, EX_N };
static const char *ex_names[EX_N] = {
@@ -56,7 +56,8 @@ enum {
K_SESSION_SETTINGS, K_SESSION_IS_ON, K_SESSION_SET,
K_APP_ICON,
K_SHOT_TAKE, K_SHOT_RECORD, K_SHOT_STOP, K_SHOT_IS_REC, K_SHOT_PTR, K_SHOT_SET_PTR,
K_PW_SCREEN, K_PW_SET_SCREEN, K_PW_SUSP, K_PW_SET_SUSP, K_PW_SUSPEND, K_PW_OFF, K_PW_REBOOT
K_PW_SCREEN, K_PW_SET_SCREEN, K_PW_SUSP, K_PW_SET_SUSP, K_PW_SUSPEND, K_PW_OFF, K_PW_REBOOT,
K_BAT_READ
};
typedef struct Env { Inst *inst; int kind; } Env;
@@ -65,9 +66,10 @@ typedef struct Env { Inst *inst; int kind; } Env;
enum { T_APPS, T_WINDOWS, T_LAYOUTS, T_MODULES, T_INSTANCES, T_WALLPAPERS, T_SESSION_SETTINGS, T_ICONS, T_N };
typedef struct { uint8_t *data; size_t len; } Blob;
static Blob tables[T_N];
static int32_t host_ints[8]; /* 0 layout mode, 1 inspector on, 2 current wallpaper, 3 session settings on (bit i: row i),
static int32_t host_ints[12] = {[7] = -1}; /* 0 layout mode, 1 inspector on, 2 current wallpaper, 3 session settings on (bit i: row i),
4 screenshot state (bit 0 recording, bit 1 pointer in recordings),
5 screen-off minutes, 6 suspend minutes (0: never) */
5 screen-off minutes, 6 suspend minutes (0: never),
7 battery percent (-1: none), 8 battery charge-state, 9 battery seconds left */
/* ---- requests a guest made of the host (launch, focus, ...) ; drained by the shell after each call ---- */
typedef struct { int kind; int32_t n; char *s; } Req;
@@ -364,6 +366,16 @@ static wasm_trap_t *host_import(void *env, const wasm_val_vec_t *args, wasm_val_
if (results->size >= 1) results->data[0] = (wasm_val_t)WASM_I32_VAL(h);
break;
}
case K_BAT_READ: {
/* battery.status: percent s32 @0, charge-state u8 @4, secs-left u32 @8 */
uint8_t st[12] = {0};
int32_t state = host_ints[8] >= 0 && host_ints[8] <= 4 ? host_ints[8] : 0;
memcpy(st, &host_ints[7], 4);
st[4] = (uint8_t)state;
memcpy(st + 8, &host_ints[9], 4);
if (args->size >= 1) mem_write(in, a0, st, 12);
break;
}
case K_SYSTEM_READ:
if (args->size >= 1) mem_write(in, a0, g_system, 64);
break;
@@ -473,6 +485,7 @@ static int classify_import(const wasm_name_t *m, const wasm_name_t *f) {
if (!strcmp(iface, "windows") && !strcmp(fn, "all")) return K_WIN_ALL;
if (!strcmp(iface, "windows") && !strcmp(fn, "focus")) return K_WIN_FOCUS_ID;
if (!strcmp(iface, "system") && !strcmp(fn, "read")) return K_SYSTEM_READ;
if (!strcmp(iface, "battery") && !strcmp(fn, "read")) return K_BAT_READ;
if (!strcmp(iface, "icons") && !strcmp(fn, "app-icon")) return K_APP_ICON;
if (!strcmp(iface, "session") && !strcmp(fn, "logout")) return K_LOGOUT;
if (!strcmp(iface, "wallpaper") && !strcmp(fn, "wallpapers")) return K_WALLPAPERS;
@@ -753,7 +766,7 @@ void mp_host_set_table(int32_t which, moonbit_bytes_t data) {
MOONBIT_FFI_EXPORT
void mp_host_set_int(int32_t which, int32_t v) {
if (which >= 0 && which < 8) host_ints[which] = v;
if (which >= 0 && which < 12) host_ints[which] = v;
}
/* Pop the next guest request: returns its kind (0 = none). Fetch its arguments with req_int / req_str. */