diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-11 20:29:52 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-11 20:29:52 +0100 |
| commit | fea2e9b221a2d18f9c73e5763adf662128cea5ec (patch) | |
| tree | 018ac7111d3dce1e4768c7a702d4394baf5589e1 | |
| parent | c6f23856f080feee37c4972fb6689332b9873574 (diff) | |
| download | deck-it-fea2e9b221a2d18f9c73e5763adf662128cea5ec.tar.gz deck-it-fea2e9b221a2d18f9c73e5763adf662128cea5ec.zip | |
feat(tablet): dim the backlight after an idle timeout
After [tablet] dim_after seconds with no touch the page covers itself and
the tablet module's dim action lowers the backlight to dim_brightness over
adb, with auto brightness off meanwhile; an LCD's backlight is the drain,
so a dark page alone saves little. The touch on the cover only calls
undim, which restores both settings, as does the module's stop.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| -rw-r--r-- | AGENTS.md | 6 | ||||
| -rw-r--r-- | config.toml | 6 | ||||
| -rw-r--r-- | deckit/modules/tablet.py | 45 | ||||
| -rw-r--r-- | static/app.js | 16 | ||||
| -rw-r--r-- | static/deck.css | 5 | ||||
| -rw-r--r-- | static/index.html | 1 | ||||
| -rw-r--r-- | tests/test_tablet.py | 22 |
7 files changed, 98 insertions, 3 deletions
@@ -79,7 +79,11 @@ Keep the server dependency-free apart from that one package. serves at `/fonts/nerd.ttf`; Markdown goes through `static/md.js` (vendored `marked`, HTML escaped, images never fetched). - The page holds a Screen Wake Lock while visible, so the tablet never sleeps - over the dashboard; nothing on the tablet's settings is changed. + over the dashboard. After `[tablet] dim_after` seconds untouched, `app.js` + covers the page with `#dimmer` and `tablet`'s `dim` action sets the + backlight to `dim_brightness` over adb (auto brightness off meanwhile); the + touch on the cover calls `undim`, which puts both back, as does the + module's `stop`. Before a suspend, poweroff or reboot `deckit/sleep.py` (an elogind delay inhibitor plus `dbus-monitor` on PrepareForSleep/PrepareForShutdown) marks answers with `X-Deckit-Sleep: asleep|shutting down` for 2s, and `app.js` diff --git a/config.toml b/config.toml index 5b84411..cbaf6ee 100644 --- a/config.toml +++ b/config.toml @@ -95,6 +95,12 @@ workspaces = [1, 2, 3, 4, 5, 6, 7, 8] # unless touched. 0 turns it off. peek = 8 +[tablet] +# Seconds without a touch before the tablet's backlight goes down to +# dim_brightness (0-255); a touch puts it back. 0 turns dimming off. +dim_after = 120 +dim_brightness = 8 + [icons] # Launcher icons follow the icon theme Keystone sets (gsettings # org.gnome.desktop.interface icon-theme); this is only the fallback. diff --git a/deckit/modules/tablet.py b/deckit/modules/tablet.py index 3f4b0d9..01a5eba 100644 --- a/deckit/modules/tablet.py +++ b/deckit/modules/tablet.py @@ -25,7 +25,7 @@ import subprocess import threading import time -from ..module import Module +from ..module import Module, ModuleError STATUS = {"2": "charging", "3": "discharging", "4": "not charging", "5": "full"} TTL = 10 # seconds; each read is two adb round trips @@ -105,4 +105,45 @@ def make(cfg: dict) -> Module: cache["t"] = time.monotonic() return cache["v"] - return Module(name="tablet", title="Tablet", state=state) + tcfg = cfg.get("tablet", {}) + dim_after = max(0, int(tcfg.get("dim_after", 120))) + level = min(255, max(0, int(tcfg.get("dim_brightness", 8)))) + saved = {} # mode and brightness from before a dim; empty while bright + + def shell(cmd): + serial = cache["serial"] or _find_tablet() + if not serial: + raise ModuleError("no tablet with the dashboard tunnel") + try: + return _adb("-s", serial, "shell", cmd).strip() + except (subprocess.SubprocessError, OSError) as e: + raise ModuleError(str(e) or type(e).__name__) + + # The backlight is what drains an LCD, so a dim page alone saves little. + # Auto brightness is switched off while dimmed, else it raises it again. + def dim(_=None): + with lock: + if not saved: + saved["mode"] = shell("settings get system screen_brightness_mode") + saved["level"] = shell("settings get system screen_brightness") + shell(f"settings put system screen_brightness_mode 0;" + f" settings put system screen_brightness {level}") + + # ponytail: the saved level lives in memory; a crash while dimmed leaves + # the tablet dim until its brightness is raised by hand. + def undim(_=None): + with lock: + if saved: + shell(f"settings put system screen_brightness {int(saved['level'])};" + f" settings put system screen_brightness_mode {int(saved['mode'])}") + saved.clear() + + def stop(): + try: + undim() + except ModuleError: + pass + + return Module(name="tablet", title="Tablet", + state=lambda: {**state(), "dim_after": dim_after}, + actions={"dim": dim, "undim": undim}, stop=stop) diff --git a/static/app.js b/static/app.js index 3e75a4b..b824667 100644 --- a/static/app.js +++ b/static/app.js @@ -270,6 +270,22 @@ setInterval(() => { if (asleep.hidden && Date.now() - lastAnswer > ASLEEP_AFTER) goodnight("The PC is not answering"); }, 2000); +// After [tablet] dim_after seconds with no touch the server lowers the +// tablet's backlight and a dark layer covers the page; the touch that lands +// on that layer only brings the brightness back. +const dimmer = document.getElementById("dimmer"); +let dimAfter = 0, dimTimer = null; +const tablet = (action) => fetch(`/api/tablet/${action}`, { + method: "POST", headers: { "Content-Type": "application/json" }, body: "{}", +}).catch(() => {}); +function touched() { + clearTimeout(dimTimer); + if (dimAfter > 0) dimTimer = setTimeout(() => { dimmer.hidden = false; tablet("dim"); }, dimAfter * 1000); +} +dimmer.addEventListener("click", () => { dimmer.hidden = true; tablet("undim"); }); +document.addEventListener("pointerdown", touched, true); +fetch("/api/tablet").then((r) => r.json()).then((t) => { dimAfter = t.dim_after ?? 0; touched(); }).catch(() => {}); + document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") { lastAnswer = Math.max(lastAnswer, Date.now() - ASLEEP_AFTER + 5000); // a moment for the refresh below diff --git a/static/deck.css b/static/deck.css index a25ee46..6311c75 100644 --- a/static/deck.css +++ b/static/deck.css @@ -602,6 +602,11 @@ select { .assistant .as-op.bad { color: var(--udt-red); } .assistant .md, .assistant .as-q { user-select: text; } +/* The tablet dimmed after [tablet] dim_after idle seconds (app.js): catches + the waking touch so it presses nothing underneath. */ +#dimmer { position: fixed; inset: 0; z-index: 90; background: rgb(0 0 0 / .6); } +#dimmer[hidden] { display: none; } + /* The PC asleep: over everything, and the wake lock is dropped (app.js). */ #asleep { position: fixed; inset: 0; z-index: 100; display: flex; flex-direction: column; diff --git a/static/index.html b/static/index.html index bb27c19..e446940 100644 --- a/static/index.html +++ b/static/index.html @@ -39,6 +39,7 @@ <button id="reload" class="reload" aria-label="Reload the dashboard" title="Reload"><i class="nf"></i></button> </header> <div id="tab-body"></div> +<div id="dimmer" hidden></div> <div id="asleep" hidden> <div class="asleep-moon nf"></div> <div class="off-title" id="asleep-title"></div> diff --git a/tests/test_tablet.py b/tests/test_tablet.py index f2bbed6..611810a 100644 --- a/tests/test_tablet.py +++ b/tests/test_tablet.py @@ -20,6 +20,7 @@ from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import unittest +import unittest.mock from deckit.modules import tablet @@ -57,5 +58,26 @@ class Parse(unittest.TestCase): self.assertEqual(tablet.parse_wifi("Wifi is disabled\n"), {"connected": False}) +class Dim(unittest.TestCase): + def test_dim_saves_and_undim_restores(self): + calls, orig = [], tablet._adb + answers = {"settings get system screen_brightness_mode": "1\n", + "settings get system screen_brightness": "200\n"} + tablet._adb = lambda *a, **k: calls.append(a[-1]) or answers.get(a[-1], "") + try: + m = tablet.make({"tablet": {"dim_brightness": 5}}) + with unittest.mock.patch.object(tablet, "_find_tablet", return_value="x"): + m.actions["dim"]({}) + m.actions["dim"]({}) # already dim: the saved level is kept + m.actions["undim"]({}) + m.actions["undim"]({}) # already bright: nothing sent + finally: + tablet._adb = orig + sets = [c for c in calls if "put" in c] + self.assertEqual(sets, [ + "settings put system screen_brightness_mode 0; settings put system screen_brightness 5", + "settings put system screen_brightness 200; settings put system screen_brightness_mode 1"]) + + if __name__ == "__main__": unittest.main() |
