Files
chandra/compositor/shell/sysinfo.mbt
T
nandiandClaude Sonnet 5.5 0145981c6a Wasm panel modules, widget host, merged plasmoid host design, and shell/UI work
Panel and wasm host
- The panel is built from wasm plasmoid modules (compositor/wasm: wasmer-backed host).
  Modules get capabilities by importing host interfaces; the host publishes data tables
  and serves a request queue (launch, focus window, set layout, inspector, load/unload
  modules). New WIT: windows, layout-control, inspector, modules, system.
- Merged two competing host designs: generic tables + requests are the base; the
  `system` snapshot and the id-stable `windows.all/focus` (Desktop Pulse) run on top of it.
- Panel widgets: load a .wasm from a path or file chooser, reorder by dragging,
  right-click replace/remove/add, saved to panel.edn.
- Desktop widget, system sampler (sysinfo), app catalog/humanised names, Interface
  Explorer (inspector), real font rendering (compositor/font, assets/fonts).

Compositor
- Drag the divider between tiled windows; lower tiled rows leave room for the title bar.
- Title-bar buttons, 12-hour clock, truncated titles, task-list/layout menus.
- DRM/KMS session, tcc debug builds (vendored tinycc incl. conftest.c), justfile
  recipes (modules, run, test), pixi.toml.

Known gaps
- The sources for the pulse, tasks, layout, inspector, module-manager, moon-* and
  clock-digital12 modules are not in the tree yet, so `just modules` stops at pulse and
  the host tests that load those modules fail until they land.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 23:38:05 -08:00

200 lines
5.1 KiB
MoonBit

///| Linux provider for the `system` interface: samples /proc and /sys about once a second
/// and hands widgets a `SystemSnapshot`. Widgets never see these paths; swapping this file
/// for another provider (a VM, a remote host, a mock) changes nothing for them.
///|
pub(all) struct SysSampler {
mut last_secs : Int
mut prev_busy : Int64
mut prev_total : Int64
mut prev_rx : Int64
mut prev_tx : Int64
mut primed : Bool
mut cur : @wasm.SystemSnapshot
}
///|
fn SysSampler::new() -> SysSampler {
{
last_secs: -1,
prev_busy: 0L,
prev_total: 0L,
prev_rx: 0L,
prev_tx: 0L,
primed: false,
cur: {
cpu_permille: 0,
mem_used_kib: 0L,
mem_total_kib: 0L,
battery_percent: -1,
charging: false,
uptime_secs: 0L,
rx_bps: 0L,
tx_bps: 0L,
load_centi: 0,
},
}
}
///|
fn proc_text(path : String) -> String {
@utf8.decode_lossy(@sys.read_proc_bytes(path))
}
///| Leading unsigned integer of `s` (0 if none).
fn parse_i64(s : String) -> Int64 {
let mut v = 0L
for ch in s {
let c = ch.to_int()
if c >= 48 && c <= 57 {
v = v * 10L + (c - 48).to_int64()
} else {
break
}
}
v
}
///| Whitespace-separated fields.
fn fields(s : String) -> Array[String] {
let out : Array[String] = []
let mut cur = ""
for ch in s {
if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
if cur.length() > 0 {
out.push(cur)
cur = ""
}
} else {
cur = cur + ch.to_string()
}
}
if cur.length() > 0 {
out.push(cur)
}
out
}
///| "0.52" -> 52 (hundredths).
fn parse_centi(s : String) -> Int {
let parts = s.split(".").collect()
let whole = parse_i64(parts[0].to_string()).to_int()
let frac = if parts.length() > 1 {
let f = parts[1].to_string()
let two = if f.length() >= 2 { f[:2].to_string() } else { f + "0" }
parse_i64(two).to_int()
} else {
0
}
whole * 100 + frac
}
///| (busy, total) jiffies from the aggregate "cpu" line of /proc/stat.
fn cpu_times() -> (Int64, Int64) {
let text = proc_text("/proc/stat")
for line in text.split("\n") {
let f = fields(line.to_string())
if f.length() > 5 && f[0] == "cpu" {
let mut total = 0L
for i = 1; i < f.length() && i <= 8; i = i + 1 {
total = total + parse_i64(f[i])
}
let idle = parse_i64(f[4]) + parse_i64(f[5])
return (total - idle, total)
}
}
(0L, 0L)
}
///| (rx, tx) bytes summed over every interface but loopback.
fn net_bytes() -> (Int64, Int64) {
let mut rx = 0L
let mut tx = 0L
for line in proc_text("/proc/net/dev").split("\n") {
let l = line.to_string()
match l.find(":") {
None => ()
Some(i) => {
let name = l[:i].to_string().trim(chars=" ").to_string()
if name != "lo" {
let f = fields(l[i + 1:].to_string())
if f.length() > 8 {
rx = rx + parse_i64(f[0])
tx = tx + parse_i64(f[8])
}
}
}
}
}
(rx, tx)
}
///| (total, available) KiB from /proc/meminfo.
fn mem_kib() -> (Int64, Int64) {
let mut total = 0L
let mut avail = 0L
for line in proc_text("/proc/meminfo").split("\n") {
let f = fields(line.to_string())
if f.length() >= 2 {
if f[0] == "MemTotal:" {
total = parse_i64(f[1])
} else if f[0] == "MemAvailable:" {
avail = parse_i64(f[1])
}
}
}
(total, avail)
}
///| (percent, plugged-in) for the first battery, or (-1, true) without one.
fn battery() -> (Int, Bool) {
for name in ["BAT0", "BAT1", "BATT"] {
let base = "/sys/class/power_supply/" + name
let cap = proc_text(base + "/capacity")
if cap.length() > 0 {
let status = proc_text(base + "/status").trim(chars=" \n").to_string()
return (parse_i64(cap).to_int(), status != "Discharging")
}
}
(-1, true)
}
///| Re-sample if a wall-clock second passed since the last call; cheap otherwise.
fn SysSampler::refresh(self : SysSampler) -> Unit {
let now = wall_secs()
if now == self.last_secs {
return
}
let dt = if self.last_secs < 0 {
1
} else {
let d = now - self.last_secs
if d <= 0 { d + 86400 } else { d }
}
self.last_secs = now
let (busy, total) = cpu_times()
let (rx, tx) = net_bytes()
let (mt, ma) = mem_kib()
let (pct, plugged) = battery()
let dtotal = total - self.prev_total
let cpu = if self.primed && dtotal > 0L { ((busy - self.prev_busy) * 1000L / dtotal).to_int() } else { 0 }
let load = parse_centi(fields(proc_text("/proc/loadavg")).get(0).unwrap_or("0"))
self.cur = {
cpu_permille: if cpu < 0 { 0 } else if cpu > 1000 { 1000 } else { cpu },
mem_used_kib: mt - ma,
mem_total_kib: mt,
battery_percent: pct,
charging: plugged,
uptime_secs: parse_i64(fields(proc_text("/proc/uptime")).get(0).unwrap_or("0")),
rx_bps: if self.primed && rx >= self.prev_rx { (rx - self.prev_rx) / dt.to_int64() } else { 0L },
tx_bps: if self.primed && tx >= self.prev_tx { (tx - self.prev_tx) / dt.to_int64() } else { 0L },
load_centi: load,
}
self.prev_busy = busy
self.prev_total = total
self.prev_rx = rx
self.prev_tx = tx
self.primed = true
@wasm.set_system(self.cur)
}