garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

cartopolis-agent · 2026-11-30T13:51:12Z

feat(display): the battery in the status bar; the lid closed locks

Left of the clock: a battery drawn as full as it is, its percentage, a
bolt while charging, red at 10 % or less. Asked each minute and when the
kernel says the power changed. Closing the lid locks the screen.

cargo xtask laptop gives QEMU an SSDT written with pinnacle_aml::asm (a
battery, a charger, a lid) and requires them read; its home screenshot
shows the battery at 55 %.
programs/src/bin/display.rs | 68 ++++++++++++++++++++++++++++++++++
 xtask/Cargo.toml            |  1 +
 xtask/src/install.rs        | 90 ++++++++++++++++++++++++++++++++++++++++-----
 xtask/src/main.rs           |  4 ++
 4 files changed, 153 insertions(+), 10 deletions(-)

diff --git a/programs/src/bin/display.rs b/programs/src/bin/display.rs
index eb8a0b5..5c736c3 100644
--- a/programs/src/bin/display.rs
+++ b/programs/src/bin/display.rs
@@ -101,6 +101,9 @@ struct Server {
     /// pixels under it.
     pointer: Option<(i32, i32)>,
     under: Vec<u32>,
+    /// The battery, charger and lid, asked again each minute and when the
+    /// kernel says one changed.
+    power: pinnacle_rt::Power,
 }
 
 /// The screen reader's state: what has its focus, and the gesture under
@@ -1011,6 +1014,12 @@ impl Server {
         };
         let closable = self.closable();
         let (kept, unseen, listing) = (!self.notices.is_empty(), self.unseen, self.listing);
+        let battery = self
+            .power
+            .batteries
+            .iter()
+            .find(|b| b.state != "absent")
+            .and_then(|b| Some((b.percent?, b.state == "charging")));
         let clock = clock_text();
         let mut canvas = self.canvas();
         let bar = Rect::new(0, 0, width, shell::STATUS);
@@ -1052,6 +1061,41 @@ impl Server {
         }
         let x = width - margin - font.measure(&clock);
         canvas.text(font, x, baseline, &clock, theme::TEXT);
+        // The battery, left of the clock: how full, drawn and as a number;
+        // a bolt while it charges, red when it is nearly empty.
+        if let Some((percent, charging)) = battery {
+            let small = theme::small();
+            let label = alloc::format!("{percent}%");
+            let label_x = x - 14 - small.measure(&label);
+            canvas.text(small, label_x, baseline, &label, theme::TEXT_DIM);
+            let (w, h) = (24, 12);
+            let body = Rect::new(label_x - 8 - w - 3, (shell::STATUS - h) / 2, w, h);
+            let low = percent <= 10 && !charging;
+            let ink = if low { theme::BAD } else { theme::TEXT };
+            canvas.fill_rounded(body, 3, ink);
+            canvas.fill_rounded(
+                Rect::new(body.x + 2, body.y + 2, w - 4, h - 4),
+                2,
+                theme::BACKGROUND,
+            );
+            let fill = ((w - 6) * i32::from(percent) / 100).max(1);
+            canvas.fill_rect(Rect::new(body.x + 3, body.y + 3, fill, h - 6), ink);
+            canvas.fill_rect(Rect::new(body.right(), body.y + 3, 3, h - 6), ink);
+            if charging {
+                // A bolt across the body, in the bar's own colour around
+                // it so it reads on any fill.
+                let (cx, cy) = (body.x + w / 2, body.y + h / 2);
+                for (dx, dy, bw, bh) in [(0, -5, 3, 6), (-3, -1, 6, 2), (-2, 0, 3, 6)] {
+                    canvas.fill_rect(
+                        Rect::new(cx + dx - 1, cy + dy - 1, bw + 2, bh + 2),
+                        theme::BACKGROUND,
+                    );
+                }
+                for (dx, dy, bw, bh) in [(0, -5, 3, 6), (-3, -1, 6, 2), (-2, 0, 3, 6)] {
+                    canvas.fill_rect(Rect::new(cx + dx, cy + dy, bw, bh), theme::ACCENT);
+                }
+            }
+        }
         flush(bar);
         self.pointer_over(bar);
     }
@@ -1609,6 +1653,28 @@ impl Server {
     }
 
     fn input(&mut self, event: InputEvent) {
+        // The machine's power, not a hand: asked again, and the lid
+        // closed locks the screen, as the time without input would.
+        if event.kind == InputEvent::POWER {
+            self.power = pinnacle_rt::power();
+            if event.code == InputEvent::LID {
+                println!(
+                    "display: the lid {}",
+                    if event.value == 1 { "opened" } else { "closed" }
+                );
+                if event.value == 0 {
+                    self.lock();
+                }
+            }
+            if event.code == InputEvent::CHARGER {
+                println!(
+                    "display: the charger {}",
+                    if event.value == 1 { "in" } else { "out" }
+                );
+            }
+            self.draw_status();
+            return;
+        }
         if self.prefs.reader
             && !matches!(event.kind, InputEvent::SCREEN)
             && !(event.kind == InputEvent::KEY && event.code == KEY_F12)
@@ -1863,6 +1929,7 @@ fn main(_argument: u64) -> u64 {
         speech: None,
         pointer: None,
         under: Vec::new(),
+        power: pinnacle_rt::power(),
     };
     access::set(server.prefs);
     // The volume Settings kept: the kernel's mixer starts at its own.
@@ -1939,6 +2006,7 @@ fn main(_argument: u64) -> u64 {
                 server.tap_due();
                 if now >= next_minute {
                     server.minute += 1;
+                    server.power = pinnacle_rt::power();
                     server.draw_status();
                 }
                 if server.unlocking.is_some() {
diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml
index f6f19c0..7cb26ac 100644
--- a/xtask/Cargo.toml
+++ b/xtask/Cargo.toml
@@ -11,6 +11,7 @@ name = "xtask"
 test = false
 
 [dependencies]
+pinnacle-aml = { path = "../aml" }
 pinnacle-disk = { path = "../disk" }
 pinnacle-fs = { path = "../pfs" }
 pinnacle-json = { path = "../json" }
diff --git a/xtask/src/install.rs b/xtask/src/install.rs
index 313e9a2..23ca54e 100644
--- a/xtask/src/install.rs
+++ b/xtask/src/install.rs
@@ -598,16 +598,18 @@ fn laptop_machine(stick: Option<&Path>, target: &Path, wav: &Path) -> Result<Com
             "hda-output,audiodev=snd0",
         ])
         .args(["-device", "qemu-xhci,id=xhci"])
-        .args(["-device", "usb-hub,bus=xhci.0,port=1"])
-        .args([
-            "-drive",
-            &format!("if=none,id=target,format=raw,file={}", target.display()),
-            "-device",
-            &format!(
-                "nvme,serial=pinnacle0,drive=target{}",
-                if stick.is_none() { ",bootindex=0" } else { "" }
-            ),
-        ]);
+        .args(["-device", "usb-hub,bus=xhci.0,port=1"]);
+    let ssdt = root().join("target/laptop-ssdt.aml");
+    std::fs::write(&ssdt, laptop_ssdt()).map_err(|e| format!("{}: {e}", ssdt.display()))?;
+    q.args(["-acpitable", &format!("file={}", ssdt.display())]).args([
+        "-drive",
+        &format!("if=none,id=target,format=raw,file={}", target.display()),
+        "-device",
+        &format!(
+            "nvme,serial=pinnacle0,drive=target{}",
+            if stick.is_none() { ",bootindex=0" } else { "" }
+        ),
+    ]);
     // Behind the hub, a blank USB disk: what a person plugs into a dock.
     let dock = root().join("target/laptop-dock.img");
     blank(&dock, 16)?;
@@ -629,6 +631,67 @@ fn laptop_machine(stick: Option<&Path>, target: &Path, wav: &Path) -> Result<Com
     Ok(q)
 }
 
+/// What QEMU's machine has not and a laptop has, given as an SSDT
+/// (`-acpitable`), written with `pinnacle_aml::asm`: a battery that loses
+/// 50 mWh each time it is asked, a charger unplugged, a lid open. No
+/// embedded controller: QEMU has none, so the battery's numbers are the
+/// table's own.
+fn laptop_ssdt() -> Vec<u8> {
+    use pinnacle_aml::asm::*;
+    const LGREATER: u8 = 0x94;
+    let battery = device(
+        "BAT0",
+        &[
+            name("_HID", &eisa("PNP0C0A")),
+            name("_UID", &int(1)),
+            name("BRC0", &int(30_000)),
+            method("_STA", 0, &[ret(&int(0x1f))]),
+            method(
+                "_BIF",
+                0,
+                &[ret(&package(&[
+                    int(0),
+                    int(57_000),
+                    int(54_000),
+                    int(1),
+                    int(11_520),
+                    int(5_400),
+                    int(1_000),
+                    int(100),
+                    int(100),
+                    string("Pinnacle Test"),
+                    string("0001"),
+                    string("LION"),
+                    string("QEMU"),
+                ]))],
+            ),
+            method(
+                "_BST",
+                0,
+                &[
+                    if_(
+                        &compare(LGREATER, &path("BRC0"), &int(1_000)),
+                        &[op2(SUBTRACT, &path("BRC0"), &int(50), &path("BRC0"))],
+                    ),
+                    ret(&package(&[int(1), int(9_000), path("BRC0"), int(11_400)])),
+                ],
+            ),
+        ],
+    );
+    let charger = device(
+        "ADP0",
+        &[
+            name("_HID", &string("ACPI0003")),
+            method("_PSR", 0, &[ret(&int(0))]),
+        ],
+    );
+    let lid = device(
+        "LID0",
+        &[name("_HID", &eisa("PNP0C0D")), method("_LID", 0, &[ret(&int(1))])],
+    );
+    table(b"SSDT", &scope("\\_SB", &[battery, charger, lid]))
+}
+
 /// The lines a laptop's start must say, from the stick and from the disk.
 const LAPTOP_LINES: &[&str] = &[
     "Pinnacle: starting the kernel",
@@ -636,6 +699,13 @@ const LAPTOP_LINES: &[&str] = &[
     "acpi: a computer, in ACPI mode; off by \\_S5; restart by its reset register; the power button heard",
     "(firmware framebuffer)",
     "ps2: a keyboard and a mouse or touchpad, with a wheel",
+    // The firmware's AML run: QEMU's DSDT whole, and the SSDT's battery,
+    // charger and lid read through their methods.
+    " 0 statements failed",
+    "power: ac=0",
+    "power: lid=open",
+    "power: battery=0|state=discharging|percent=",
+    "|full=54000|design=57000|unit=mWh|model=Pinnacle Test|maker=QEMU",
 ];
 
 /// `cargo xtask laptop`: Pinnacle on a machine with a laptop's hardware
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index 20e5a8d..f5e965f 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -1758,6 +1758,8 @@ fn gate() -> Result {
         "pinnacle-locale",
         "-p",
         "pinnacle-speech",
+        "-p",
+        "pinnacle-aml",
     ])?;
     cargo(&[
         "clippy",
@@ -1802,6 +1804,8 @@ fn gate() -> Result {
         "pinnacle-locale",
         "-p",
         "pinnacle-speech",
+        "-p",
+        "pinnacle-aml",
         "--",
         "-D",
         "warnings",