Repository navigation
Turn the keyboard backlight on in the dark using the ambient light sensor - #13148
terracatta wants to merge 14 commits into
Conversation
Automated AI review
Verified: the pure model in The integrated service, however, does not yet match the described behavior in several common situations. The main issue is a stale brightness read that makes the service treat its own writes as manual changes. Brightness is read one step late, so the service's own writes look like a manual off
Synthetic timeline from a headless run of The same timeline with only Impact: after any automatic off of a lit backlight, a manual off is recorded and saved. If the room gets dark again within Suggested change: read synchronously ( Lock blanking and wake undo automatic decisions and pin the result as the user's choiceLocking blanks the keyboard with Both sequences reproduced in the headless runs with stale and with synchronous reads, so this is independent of the previous finding. Impact: the keyboard can light up while the session is locked with the screen off, then come back held off in the dark after wake (for up to Suggested change: skip writes while the lock service reports blanked displays and re-evaluate from the current lux on wake instead of adopting the restored value, or have Comma-decimal locales disable the feature silently
Impact: on sessions with a comma decimal separator ( Suggested change: start The toggle no longer stops
|
|
Thanks, this was accurate and useful. Everything is addressed in 5e0a1b9:
I also added |
Automated AI review
Follow-up on the earlier review, checked at Verified: all nine earlier points are resolved at
None of the earlier points needed to be withdrawn. A key press made since the last sensor change is lost at wakeThe service checks for a level it did not set itself only in Reproduced with the unmodified Impact: in a dark room, when the sensor reports no change between the key press and the lock, turning the keys off brings them back on after wake (after the settle time, 5 s by default), and a chosen level is replaced by the old one. With one sensor change in between, the manual off was kept. This contradicts the manual's "stays off until the room has been bright for ten minutes..." and "a brightness you pick is reused". From source (not run), in bright light the result matches the documented "decides again", so the regression concerns dark rooms. Suggested change: observe and persist the current brightness in function pause() {
if (root.active && root.state && !root.paused && !setProcess.running && root.pendingLevel < 0) {
var before = root.state.manualOffSince
root.state = KeyboardBacklightModel.observeBrightness(root.state, readBrightness(), Date.now())
if (root.state.manualOffSince !== before) manualOffFile.setText(root.state.manualOffSince > 0 ? String(root.state.manualOffSince) : "")
}
root.paused = true
checkTimer.stop()
}With this change both cases above keep the user's choice, and the PR's service test still passes (2 of 2 runs).
|
|
Thanks again. Both findings are addressed in 1ed7e08:
Model tests pass 35/35 and the service test passes 10/10. |
Automated AI review
Outcome: a key press can still be lost on a wake with no lock blank before it, a path the earlier suggestion missed. Earlier findings (previous review): key press lost after a lock blank, resolved; ISO package list, resolved. Key press still lost when
|
[Medium impact] The changes affect automatic keyboard brightness and its coordination with lock, wake, and lid workflows. Fix dimming after waking before merging; the lid-settling issue is smaller. FindingsSummaryAdds ambient-light control for the keyboard backlight, hardware checks, installation support, and a toggle. The latest changes add gradual dimming and make brightness keys wait for automatic writes.
|
|
Thanks, confirmed and fixed in ccb62ef. Your reproducer is now e0a59d2 also covers a closed lid. The sensor sits in the lid, so when docked in clamshell mode it reads dark, and the service would have lit the keys under it. The service now reads the lid state at each decision and makes no change while it's closed. The lid-open binding sends a On #12553: agreed. Whichever lands second has to keep The model (35), service (12), and wake (3) tests pass. |
|
Migration filename collision: git mv migrations/1790188786.sh "migrations/$(date +%s).sh"Also, #14557 (omarchybot, stacked on #14431) is going after the same feature: ALS-driven keyboard backlight. A maintainer will need to choose one design. |
…3148 omacom#13148 also adds migrations/1790188786.sh, so whichever of the two merged second would conflict on the file, and a resolution that kept one would drop the other migration. Give this one a fresh timestamp, after the newest migration on quattro, and move the rebuild marker and the test's marker with it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Reviewed at e0a59d2. This is a feature, so there is no bug to reproduce; the question is whether it does what the description says. None of that could be checked on real hardware: the test worker has no ambient light sensor or keyboard LED, so every behavioural result below comes from the stubbed sensor, LED and What ran, on a disposable Omarchy worker with the shell linked to this branch: the model test 35/35, the service test 12/12, the wake test 3/3, and monitor-recovery, manifest-entrypoints, menu and menu-guards all pass. It no longer merges.
Found, and reproduced on the worker: turning the keys off in a steadily dark room is not held across a shell restart, and Found by reading. Narrower, and not checked on hardware. These match Greptile's comments that are still open; I checked each against the source.
Checked and fine: on machines without both devices, nothing runs except one probe at shell start. Second opinion: Codex Medium and muse-spark-1.3-contributor (via pi, thinking high) both reviewed this head. Both found the restart loss and the four narrower items above to be real. My brief had already raised them, and the reviewers can read this session, so treat that agreement as confirmation, not independent findings. Codex pointed to the existing Related: #14557 (a draft from this account) builds the same feature as a systemd user service. #10331 drives the keyboard backlight from the T1 sensor on the models it targets. #12553 and #10366 change the same Waiting on: @terracatta to rebase onto |
…nsor Laptops with an ambient light sensor now get an automatic keyboard backlight. A shell service watches `monitor-sensor --light` (iio-sensor-proxy only reports changes) and switches the backlight when the room crosses from dark to bright or back, after the reading has held for five seconds. Levels set with the keys, `omarchy brightness keyboard`, or idle blanking are respected. A manual level is reused the next time it gets dark. A manual off in the dark is held across restarts until the room has been bright for ten minutes, eight hours pass, or the backlight is turned back on. - omarchy-hw-ambient-light detects the sensor. The hardware setup and a migration install iio-sensor-proxy where one is present. - omarchy toggle keyboard backlight auto disables it, which also releases the sensor. It is also in the Toggle menu on supported hardware. - Thresholds can be tuned under `keyboardBacklight` in shell.json. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Read brightness with blockAllReads. reload() with blockLoading returned the previous load, so the service took its own writes as manual changes and could hold the backlight off in the dark. Skip observations while a write is in flight. - Pause across session blanking. `omarchy-brightness-keyboard off` and `restore` now pause and resume the service over IPC. Nothing is lit behind a blanked lock screen, and the restored level becomes a baseline rather than the user's choice. A held manual off is put back if a wake restores a lit level. - Run monitor-sensor with LC_ALL=C and accept comma decimals, so comma-decimal locales don't silently disable it. - Manage monitor-sensor's running state imperatively, so a restart after an early exit can't detach it from the toggle. - Match every IIO light sensor attribute iio-sensor-proxy supports. Add omarchy-hw-keyboard-backlight and require it for the menu entry and the install. - Clamp the check timer to the 32-bit interval range. - Manual: add the block to an existing shell.json, and note that the remembered level lasts until the shell restarts. - Add a headless service test with stub monitor-sensor and brightnessctl covering stale reads, blanking, locale, and the toggle after a restart. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…on the ISO - Take one last look at the level in pause(). A key press only shows up at the next sensor change or check, so in a steady dark room an off made just before locking was taken as the session's at wake and undone. - Add iio-sensor-proxy to omarchy-other.packages so the ISO's offline mirror carries it for the hardware install. - Rename the tests to keyboard-backlight-auto-*, clear of the keyboard-backlight-test.sh other open PRs add. - Tighten the service test: check for a phantom off after a single reading, and cover a key press just before locking and a restore without a blank. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
omarchy-system-wake runs restore without an off before it when the idle screensaver is dismissed before the lock, or on unlock before the first blank. The service was never paused, so nothing looked at the level before restore overwrote it, and a key press to off in a steady dark room was undone. restore now pauses first. After an off, that pause is a no-op. Adds keyboard-backlight-auto-wake-test.sh, adapted from the community review's reproducer. It drives the real off/restore script against the real service, and also checks that a repeated off during a blank isn't taken as the user's. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The light sensor sits in the lid, so with the lid shut (docked, in clamshell mode) it reads dark and the service would light the keys under it. The service now reads the lid state at each decision and makes no change while it's closed. The lid-open binding nudges it to decide from the light right away. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…gration to a fresh timestamp Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d changes - The backlight keys nudge the service, so an off made in steady light is saved at once, and the last level seen is kept in the runtime dir, so one made while the shell was down (firmware-handled keys too) is still the user's after a restart. - A manual off is cleared as soon as the room has been bright long enough, not at the next dark, so a restart in between can't bring it back. - Writes take the lock `omarchy-brightness-keyboard off`/`restore` use and skip while a blank is saved, so none lands behind the lock. A queued write is dropped once paused or the lid closes, and a failed or dropped write resyncs instead of being taken as a manual off. - The service is kept loaded across plugin reloads, and a shell started while blanked starts paused. - The lid is read with omarchy-hw-laptop-closed (so Apple Silicon's SMC lid counts too), both lid bindings nudge it, and opening the lid restarts monitor-sensor for a fresh reading rather than acting on the one taken under the lid. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
e0a59d2 to
2c43404
Compare
|
Rebased onto Rebase (thanks for the list,
Restart case: I took two of the options. The four narrower items:
The replies on each Greptile thread have the details. Tests: model 44, service 20, wake 7. I checked each new service and wake case by putting its bug back, and it failed each time. |
…eyboard-test.sh Co-Authored-By: Claude Opus 5.5 <[email protected]>
…al off Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ing it Restarting the bright period on every pause meant a wake with no blank before it (the mouse moved after idle) set it back to zero, so waking every few minutes kept a manual off from ever clearing in a bright room. Leave out only the time readings weren't evaluated, and keep the period running when off is decided again after a wake. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| if (!cfg.dimming || lux === null || lux === undefined || state.applied !== "on" || state.held || state.dimHeld || | ||
| state.manualOffSince > 0 || state.pending !== "" || !(state.expected > 0)) { |
There was a problem hiding this comment.
resume() clears applied, but this new guard requires it to be "on". Wake with the keyboard lit at 30 lux, then let the room brighten to 45 lux: neither reading can restore applied, so the keyboard never dims. It stays fixed until the room crosses a threshold again. Keep enough state after waking to resume dimming between the thresholds.
| } else if (now - next.dimSince >= cfg.settleMs) { | ||
| next.dimTarget = -1 | ||
| next.expected = target | ||
| return { state: next, set: target } |
There was a problem hiding this comment.
Opening skips the settling wait
A pending dim counts time with the lid closed toward its five-second wait. interrupt() and uninterrupt() leave dimTarget and dimSince unchanged. If the lid closes while a dim is pending, the first fresh reading after opening can apply it immediately when it maps to the same target. A brief shadow while opening can therefore change the brightness without settling. Clear the pending dim when readings stop, or restart its wait after opening.
|
Measurement from a MacBook Pro 14" M3 Pro (J514S, Apple Silicon), for this sensor path. I took the SMC total system power, display off and idle, 120 one-second samples per state, back to back:
So 50% keyboard backlight costs about 0.7 W here, and today it stays lit in a bright room and with the display off. On this machine the ambient light sensor is exposed as IIO |
What changed
omarchy.keyboard-backlightshell service that follows the ambient light sensor throughiio-sensor-proxy(monitor-sensor --light, which only reports changes). It turns the keyboard backlight on below 10 lux and off above 50 lux, after the reading holds for 5 s. In between, a lit backlight dims with the light, from full at 10 lux down to the lowest lit step just before 50 lux. Each dimming step settles the same way, and changes under a twentieth of the full level are skipped on keyboards with many levels."dimming": falsekeeps it at full.omarchy brightness keyboard.omarchy brightness keyboard up/down/cyclenudge the service, so a choice made in steady light is seen (and saved) right away. The last level seen is kept in$XDG_RUNTIME_DIR, so a level changed while the shell was down, including by firmware-handled keys, is still taken as the user's after a restart. It's cleared on reboot, when the firmware or systemd-backlight sets the level instead.omarchy-brightness-keyboard offandrestorepause and resume the service over IPC, so nothing is lit behind a blanked lock screen. On wake, it decides again from the room's light instead of adopting the restored level. The service's writes take the same lock asoff/restoreand skip while a blank is saved, so a write already in flight can't land behind the blank. The service stays loaded across plugin reloads, and a shell started mid-blank starts paused.omarchy-hw-laptop-closed(ACPI, or logind for Apple Silicon's SMC lid), and the open and close bindings for both lid switches nudge the service. On open,monitor-sensoris restarted for a fresh reading rather than acting on the one taken under the lid.omarchy-hw-ambient-light, which matches the same IIO attributes asiio-sensor-proxy's udev rules, andomarchy-hw-keyboard-backlight.install/hardware/ambient-light.shand a migration installiio-sensor-proxywhere both exist, and it's inomarchy-other.packagesfor the ISO's offline mirror.omarchy toggle keyboard backlight auto(flagkeyboard-backlight-auto-off, named for the off state like the other toggles). It's also in the Toggle menu as an on/off pair, guarded by both hardware checks, and its notifications go throughomarchy-i18n. Disabling it stopsmonitor-sensor, which releases the sensor.keyboardBacklightinshell.json, with defaults in code. A user'sshell.jsonreplaces the defaults entirely, so existing configs work unchanged.Discussion: #13147 Turn the keyboard backlight on in the dark on laptops with an ambient light sensor
Design notes
The logic is a pure model (
KeyboardBacklightModel.js), andService.qmlonly does I/O, following the battery and night light services. The service has no timers running in steady light. A single-shot timer handles the settle time and hold expiry.monitor-sensorruns undersetpriv --pdeathsig TERM, like the clipboard watchers. An orphan left behind by a killed shell would keep the sensor claimed and never report again. It runs withLC_ALL=C, since readings are printed with%lf, and itsrunningstate is set only from code, so a restart after an early exit can't detach it from the toggle.A write that fails or is dropped (paused, lid closed) resyncs the model to the actual level and decides again, rather than being taken as a manual off. A failed write is retried after the settle time, up to three times, since in steady light nothing else would decide again.
Time spent blanked or with the lid closed doesn't count toward the ten bright minutes that clear a manual off; the bright period starts over at the next bright reading.
IPC registers through
ShellIpc, like the other first-party services.Brightness is read with
blockAllReads. WithblockLoading,reload()returns the previous load, so the service would take its own writes as manual changes. Readings taken while a write is in flight are ignored.The lock service is an authentication service and isn't reachable from other services, so blanking is signalled from
omarchy-brightness-keyboard off/restore, which bracket both lock blanking and idle wake.pauseis sent before the blank, and beforerestorewrites too, since a wake can come without a blank before it.resumeis sent after the restore, so neither races a write.pausealso takes one last look at the level, so a key press made in a steady dark room just before locking or waking isn't lost. After anoff, the secondpauseis a no-op.Firmware idle timeouts that darken the keys without changing
brightness(Dell'sstop_timeout) are invisible to the service, so they aren't mistaken for a manual off. I verified this on the XPS 14.Overlap
Testing
test/shell.d/keyboard-backlight-auto-test.sh: 62 model cases covering parsing (including comma decimals), settle and spike filtering, the dead band, holding and clearing a manual off, the remembered level, restart persistence (including a level changed while the shell was down), the hold clearing when the bright period ends, a blank or closed lid not counting as bright time, dimming (settling, the lowest lit step, skipping invisible changes on keyboards with many levels, a picked level stopping it, anddimming: false), and resuming after a session wake into a dark or bright room.test/shell.d/keyboard-backlight-auto-service-test.sh: runs the realService.qmlunder headless Quickshell with a stubmonitor-sensor, a stubbrightnessctlwhose writes land late, and a synthetic LED directory (OMARCHY_LEDS_PATH). Its 22 checks cover restart after an early exit, the C locale, its own on and off writes not being taken as manual changes, nothing being written while blanked, deciding again from the light after wake, a key press just before locking being kept, a restore without a blank not being taken as the user's, nothing being lit under a closed lid, deciding from the light once the lid opens, a fresh reading once the lid opens instead of the one taken under it, a failed write not being taken as a manual off and being retried a bounded number of times, a key press seen at once throughrefresh, an off made while the shell was down being held, a shell restarted mid-blank writing nothing, and the toggle still stoppingmonitor-sensor. Each check was confirmed against a copy of the service with its bug put back. The tests are namedkeyboard-backlight-auto-*to stay clear of thekeyboard-backlight-test.shthat Fix keyboard backlight restore after screensaver dismiss #10366, Restore ThinkPad keyboard backlight after sleep and boot #12553, and Add optional idle timeout for keyboard backlight #12558 add.test/shell.d/brightness-keyboard-test.sh(the existing suite foromarchy-brightness-keyboard) gains a section that drives the realomarchy-brightness-keyboard off/restoreagainst the real service (adapted from the community review's reproducer). Its 8 checks cover a key press kept through a lock, a key press kept through a wake without a blank, a repeatedoffduring a blank not being taken as the user's, a key press throughup/downsaved at once, a write still in flight when the lock blanks not being left lit, and a key press during an automatic write stepping from the level that write sets.quattroat d9cc361../test/cli,i18n-test.sh,plugins-test.sh(the service is now required to bekeepLoaded),shell-ipc-test.sh,menu-test.sh,menu-guards-test.sh,manifest-entrypoints-test.sh,lid-close-test.sh, andmonitor-modeless-test.shpass, as does the rest of./test/shellapart fromshell-idle-spawn-test.sh. That one fails the same way onquattrowithout this change when it's run on a live desktop: Hyprland window events start the workspace-layout and keyboard widget probes.monitor-sensor.monitor-sensorruns withLC_ALL=C, and thepause/resumeIPC functions are registered.quattroshell from this branch was run on the same XPS 14 against the real sensor, LED and lid, withbrightnessctlstubbed to log. The service loaded as a first-party plugin, read 108 lux from the real sensor, wrote off after the 5 s settle through the new locked write path, and answeredrefresh,lid,pauseandresume.Power
In steady light the service does nothing, and power is only spent in the sensor stack.
iio-sensor-proxy3.9 falls back to polling HID sensor hub light sensors about once a second, which costs ~5 sensor hub interrupts/s on the XPS while any client listens. A fix is in progress upstream. With it, this measured 0 interrupts in steady light and 113 → 11 per 20 s overall.Development
Claude Code (Claude Opus 5.5) wrote the code under my supervision, and I ran the live hardware tests with it.
🤖 Generated with Claude Code