Skip to content

Automatic keyboard lighting from the ambient light sensor - #14557

Draft
omarchybot wants to merge 4 commits into
omabot/pr-13362from
omabot/pr-14517
Draft

omarchybot wants to merge 4 commits into
omabot/pr-13362from
omabot/pr-14517

Conversation

@omarchybot

@omarchybot omarchybot commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Laptops with an ambient light sensor and a keyboard backlight can keep their keys lit in a dark room and dim them as the room brightens. Brightness keys and firmware keys still choose a level by hand, with automatic control resuming after a sufficient ambient-light change. The service skips machines without both devices.

Manual brightness commands and automatic updates share a lock. Lock-screen blanking and wake restoration wait for an update, while a brightness key gives up after two seconds rather than changing the LED without the lock. A stalled session-state probe counts as locked. A stale idle-wake restoration preserves an already observed manual brightness choice.

First-run enables the service, and a migration enables it for existing users, including updates without a live user manager. Both current settings recipes already install the unit whenever the desktop source contains it.

Stacked on #14431. This remains a draft: repeated wake calls during password unlock can still lose a manual level chosen just before blanking. That behavior needs resolving while preserving hibernate restoration before this feature lands. Unchanged manual off choices, stale blank records and unknown session-lock states also need handling.

Supersedes #14517 by @maralcbr, whose commits this carries unchanged.

maralcbr and others added 4 commits October 8, 2026 12:56
Brings back automatic keyboard lighting, split out of #14431 so it gets its own review: laptops with an ambient light sensor and a keyboard LED light the keys in the dark and dim them as the room brightens, and the brightness keys and firmware keys still choose by hand.
The lock screen's blank waited two seconds for the keyboard lock, then went
ahead without it. The automatic loop's lid check, made while holding that
lock, can itself take two seconds when logind stalls, so the blank could land
first and the update light the keys again while the screen stayed locked. A
brightness key that timed out went ahead the same way.

The blank and the wake restore now wait for the lock however long it is held,
and a key that cannot get it in two seconds changes nothing. The update keeps
the lock short: it asks about the lid before taking it, and a session check
that does not answer within a second is killed and counts as locked.
A wake can restore brightnessctl's saved lit level after the user turned the keys off. If automatic control is already paused on that choice, put its observed manual level back before considering the ambient-light threshold, so an idle wake cannot override the user's brightness keys or firmware choice.

Co-Authored-By: GPT-6 in Codex <[email protected]>
Co-Authored-By: Claude Opus 5.5 High via Pi <[email protected]>
First-run enablement does not run for users who have already completed setup. Enable the new service during their update too, using the existing user-unit migration pattern for graphical sessions and updates without a user manager. Include the service in the package-default contract check; both settings recipes already install it whenever the desktop source contains it.

Co-Authored-By: GPT-6 in Codex <[email protected]>
Co-Authored-By: Claude Opus 5.5 High via Pi <[email protected]>
@omarchybot

omarchybot commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator Author

Reviewed the separate keyboard-lighting scope with Claude Opus 5.5 High via Pi. The delivered changes preserve an already observed manual off after an idle wake and enable the service for existing installs. Those failures were exercised on an isolated ISO worker; 77 focused keyboard, systemd and package-contract checks pass. Both current settings recipes already install the unit when present, so no package change is needed.

A reproducible unlock defect remains: repeated wake calls during password unlock can lose a manual level chosen just before blanking. The worker test changes 201 back to the automatic 226 rather than retaining the choice. This remains a draft pending a correction that preserves hibernate restoration, and the review reached its three-round bound. It is stacked on #14431; its branch will need updating with the landed core and retargeting before it can land separately.

Greptile also flagged unchanged manual off choices, a stale blank record that can misclassify a later firmware off, and updates when the session-lock helper returns an unknown state. All three reproduce on the isolated worker using sensor, LED and lock-state fixtures: an unchanged off becomes 20, a firmware off becomes 226, and an unknown lock state also relights to 226. They need addressing within this separate feature; the draft is not being treated as ready because its existing suite passes.

@greptile-apps

greptile-apps Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Adds automatic keyboard backlight control from ambient light sensor.

The reviewed changes appear safe to merge, with non-blocking keyboard-lighting edge cases to fix.

Findings

  1. P2 Off choice gets ignored ▶
  2. P2 Firmware off gets undone ▶
  3. P2 Locked keys can relight ▶

Summary

Adds automatic keyboard lighting from an ambient light sensor.

  • Adds a user service, first-run enablement, and an upgrade migration.
  • Shares a lock and brightness records with manual keys and lock-screen blanking.
  • Adds shell tests and user documentation.
  • Three non-blocking issues remain: unchanged manual choices are missed, stale blank records can undo firmware off choices, and unknown lock states allow updates.

omarchybot acknowledges that repeated wake calls during password unlock can lose a manual level chosen just before blanking. That known issue is not reported again.

Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Sensor[Ambient light sensor] --> Tick[Automatic tick]
  Tick --> Guards[Check lid and screen lock]
  Guards --> SharedLock[Shared brightness lock]
  Keys[Brightness keys] --> SharedLock
  LockScreen[Lock blank and wake restore] --> SharedLock
  SharedLock --> Records[Brightness records]
  Records --> Choice{Manual choice still applies?}
  Choice -->|Yes| Keep[Keep chosen level]
  Choice -->|No| LED[Set level from room light]
Loading

Reviews (1) · Last reviewed commit: "Enable automatic keyboard lighting for e..." · Reviewed by Greptile

rm -f "$MANUAL_LEVEL_FILE"
fi

if [[ -n $last_set && $current != "$last_set" ]]; then

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Off choice gets ignored

tick misses brightness-key choices that leave the LED unchanged. With max=255 and lux 179, auto sets zero. Pressing Down records 0, but current == last_set skips the pause. At lux 165, auto lights the keys to 20 even though the change is below OVERRIDE_LUX_DELTA.

Detect a fresh numeric manual record even when the LED has not moved.

Comment on lines +199 to +201
if (( delta * 100 < max * DEADBAND_PERCENT )); then
last_set=$current
return 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Firmware off gets undone

apply_percent leaves a stale blank record when no LED change is needed. If auto already has the keys off, lock blanking writes blank. After unlock, current == last_set and the early return leave that record in place.

A later firmware brightness increase starts a pause without clearing the record. Turning the keys off with firmware then looks like lock blanking, so auto cancels the pause and lights them again. Clear a consumed blank record even when auto does not write a new level.

session_locked() {
local status=0
timeout -k 1 1 omarchy-hyprland-session-locked >/dev/null 2>&1 || status=$?
(( status == 0 || status == 124 || status == 137 ))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Locked keys can relight

session_locked treats an unknown lock state as unlocked. omarchy-hyprland-session-locked returns 2 when hyprctl fails or cannot establish the state, but this check only stops updates for 0 or a timeout. A failed probe during a locked session can therefore relight keys that the lock screen blanked.

Allow updates only when the helper explicitly returns 1.

@maralcbr

Copy link
Copy Markdown
Member

@ryanrhughes three things beyond the open items the description already lists (repeated wake restores, unchanged manual off, stale blank records, unknown lock states):

  1. Polling vs # Background Work (Tell agents to react to events instead of polling #15029). The daemon wakes every 5 s (POLL_SECONDS=5), and each tick starts about nine processes, including a hyprctl call, even in steady light. Polling the light level is justified on Apple: the Asahi AOP light sensor driver has read_raw only, with no IIO buffer or events. Lid and lock state do have change signals (logind LidClosed, and the lock screen's own blank/restore path), and the LED and lux values can be read with bash builtins. To fit the rule it should read sysfs without forking, take lid and lock state from events, and only probe when it's about to change the LED.

  2. Default-on for existing installs, with no way to turn it off. The migration enables it for every existing user with a light sensor and a keyboard LED, x86 laptops included (Framework 13, XPS, ASUS/HP/Surface, Intel T1 Macs). Similar features have an off toggle (e.g. crash-capture's ConditionPathExists=!…/toggles/crash-capture-off); this needs one too.

  3. Overlaps.

    Which approach should own keyboard lighting? Once you pick, I can rework this one or close it in favour of Turn the keyboard backlight on in the dark using the ambient light sensor #13148.

The base is still the merged omabot/pr-13362. Its one conflict with quattro (install/user/first-run/enable-user-units.sh, after #14796) is just adding the unit to the new loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants