Skip to content

fix(keyboard): keep backlight restore across repeated off - #12960

Closed
kvnloo wants to merge 3 commits into
omacom:quattrofrom
kvnloo:fix/12942-keyboard-brightness-double-off
Closed

kvnloo wants to merge 3 commits into
omacom:quattrofrom
kvnloo:fix/12942-keyboard-brightness-double-off

Conversation

@kvnloo

@kvnloo kvnloo commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Summary

omarchy-brightness-keyboard off uses brightnessctl -s, which saves the current level before zeroing. A second off while the LED is already 0 overwrites the restore value with 0, so unlock/restore leaves the keyboard dark.

Only save when the current brightness is greater than zero before blanking.

Test plan

  • ./test/shell — brightness-keyboard-restore-test.sh
  • off → restore keeps prior non-zero level
  • off → off → restore still restores the original non-zero level

Fixes #12942

Only save brightness when turning off from a non-zero level so a second
off does not overwrite the restore value with 0.
@llstrk

llstrk commented Sep 23, 2026

Copy link
Copy Markdown

One restore-state regression was found at eef4cfc3: a keyboard backlight deliberately turned off after an earlier unlock can turn back on after the next lock/blank/unlock cycle.

Preserve a user-selected zero across separate lock cycles

The new guard in bin/omarchy-brightness-keyboard treats every zero as an already-blanked backlight. However, brightnessctl -r retains its saved value, and the normal down/cycle commands do not replace it. After a previous lock saved a nonzero level, the next off skips saving a user-selected zero and restore brings back that older level.

A synthetic device with maximum brightness 100 reproduced this using the production command with a remapped LED path and a brightnessctl stub modeled on version 0.5.1:

Sequence                               Base       PR
Start at 50; off; restore               50         50
--no-osd down five times                 0          0
off; restore                            0         50

The lock service invokes off when blanking and omarchy-system-wake invokes restore, so this maps to locking long enough to blank, unlocking, turning the keyboard light off, then repeating the lock/blank/unlock cycle.

Impact: the second cycle reverses the user's choice to leave the keyboard backlight off. This requires a prior nonzero saved level in the same login session.

Suggested change: distinguish a repeated blank within the same blank/restore cycle from a fresh cycle starting at a user-selected zero. Preserve zero on the latter path while retaining the repeated-off fix, and cover the sequence above in the regression test. Explicit per-device cycle state is one possible approach, not a verified patch.

Verified: the intended 77 → off → off → restore sequence restores 77 on the PR and 0 on the base in the synthetic checks. The added test passes on the PR and fails on an equivalent baseline control at the second-off assertion.


Review information

Test scope: pinned base/head command checks with synthetic LEDs and stubbed brightnessctl; lock/wake callers and brightnessctl 0.5.1 save-file behavior inspected in source. No physical keyboard, native brightnessctl, running lock-screen session or broad shell-suite validation.

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Automated AI review: Astra Medium initial inspection, independent Opus 5.5 High and GPT 6 Sol Xhigh technical assessments, followed by source/evidence verification and post-review adjudication. A fresh Astra Medium check assessed presentation and disclosures, not technical correctness.

@layolayo

Copy link
Copy Markdown

There is another edge case tied to this that helps resolve the regression noted by @llstrk: screensaver wake-up and hardware hotkeys (Fn + Space).

1. The Screensaver Asymmetry

In shell/plugins/services/idle/Service.qml, dismissing the screensaver (cancelIdleCycle("screensaver-dismissed")) triggers omarchy-system-wake, which unconditionally calls omarchy-brightness-keyboard restore. However, entering the screensaver never called off.

If a user turned on their keyboard backlight via laptop hardware keys (e.g. Fn + Space on ThinkPads, which changes the sysfs LED directly without brightnessctl knowing), and an old 0 was lingering in brightnessctl's saved state file, simply nudging the mouse to exit the screensaver immediately turns the keyboard backlight back off.

2. Solving the Regression and the Screensaver Bug Together

Because brightnessctl -r does not remove its save file (${XDG_RUNTIME_DIR:-/tmp}/brightnessctl/leds/$device), the saved state persists across completely separate lock cycles.

We can solve both issues cleanly by:

  1. Never turning off an already lit keyboard on restore: If current > 0 when restore is called (such as waking from screensaver or after a hardware Fn + Space), exit early.
  2. Unlinking the save file on restore: Once a restore completes, remove the ephemeral save file.

This directly satisfies @llstrk's test case: if a user later turns their backlight to 0 and locks again, there is no lingering saved file from the prior cycle to resurrect the old non-zero level.

Suggested Patch

save_file="${XDG_RUNTIME_DIR:-/tmp}/brightnessctl/leds/$device"
[[ -f $save_file ]] || save_file="/tmp/brightnessctl/leds/$device"

if [[ $direction == "off" ]]; then
  current="$(brightnessctl -d "$device" get 2>/dev/null || echo 0)"
  if (( current > 0 )); then
    brightnessctl -sd "$device" set 0 >/dev/null
  else
    brightnessctl -d "$device" set 0 >/dev/null
  fi
  exit 0
elif [[ $direction == "restore" ]]; then
  current="$(brightnessctl -d "$device" get 2>/dev/null || echo 0)"
  # If the keyboard is already illuminated (e.g. toggled via hardware Fn+Space,
  # or waking from screensaver which never turned it off), leave it on.
  if (( current > 0 )); then
    exit 0
  fi

  if [[ -f $save_file ]]; then
    saved_val="$(cat "$save_file" 2>/dev/null || echo 0)"
    if (( saved_val > 0 )); then
      brightnessctl -rd "$device" >/dev/null
    fi
    # Unlink the temporary save file so subsequent locks don't resurrect stale state
    rm -f "$save_file" 2>/dev/null
  fi
  exit 0
fi

omarchybot and others added 2 commits October 3, 2026 00:41
Skipping the save whenever the backlight is at 0 also skipped it when the user had turned the light off themselves, so restore brought back the level an earlier lock had saved: brightnessctl's save file outlives each restore. A marker written by off and removed by restore tells a repeated off apart from a fresh one, so only the repeat skips the save.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The keyboard bindings work while locked, so a user can pick 0 between two blanks. With the marker still present, the second off took that 0 for its own and skipped the save, and unlock brought back the level from before the lock. An explicit adjustment now ends the pending blank.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Oct 2, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed and tested on a disposable Omarchy worker. The bug in #12942 is real, and this fixes it. I pushed two commits on top of yours, because skipping the save whenever the backlight is at 0 also skips it when the user turned the light off on purpose.

What was wrong. brightnessctl does not delete its save file after -r, so the level saved by one lock is still there at the next. With only the current > 0 check, @llstrk's sequence holds: lock at 50, unlock, turn the keyboard light down to 0, lock again. That off skips the save, and unlock turns the light back on at 50. On quattro it stays off. The same happens if you press the brightness keys while locked (the bindings are locked = true) and pick 0 between two blanks.

What I changed.

  • 1ddb613: off writes a marker to $XDG_RUNTIME_DIR/omarchy-brightness-keyboard-<device>.blanked, and restore removes it. The save is skipped only when the light is at 0 and the marker is there, which means it was our own earlier off. A 0 the user chose still gets saved, the way it is on quattro.
  • 111f759: the up/down/cycle path removes the marker as well, so a level picked with the keys while locked counts as the user's choice.
  • Each case has a test in test/shell.d/brightness-keyboard-restore-test.sh.

What ran, on the worker.

  • brightness-keyboard-restore-test.sh against four versions of the script:
  • ./test/cli is green.
  • ./test/shell has five failing files, and none of them touch this change. ascii, branding-about-animation, kernel-headers-migration and omarchy-kernel-migration fail the same way on current quattro. screenshot-sanity fails at this branch's fork point d3cfd53 and passes on current quattro.
  • On the worker's brightnessctl 0.5 I confirmed the save file is still there after -r.
  • No physical keyboard backlight was available, so the device itself was a stub.

Second opinion. Codex Medium reviewed it. It raised the locked-keys case, which became 111f759. Its re-review of the final head found nothing left to fix.

Known limits.

  • A level changed by keys the firmware handles itself, outside brightnessctl, can still be mistaken for the blank.
  • @layolayo's point is real but already on quattro, and this change does not alter it: dismissing the screensaver runs restore without any off first. It is a separate fix.

Next. Waiting on the maintainer. No test CI runs on this pull request yet; only the Greptile review was pending when I looked.

@omarchybot omarchybot removed the verified Omarchy Triage has verified that this issue is ready for final review label Oct 2, 2026
@greptile-apps

greptile-apps Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Keyboard backlight off command now tracks state across repeated calls.

This PR appears safe to merge; no actionable issues were found.

What we checked:

  • Repeated off loses saved brightness: When brightness is zero and the marker exists, off skips saving. The test confirms that two off calls followed by restore return the original brightness.

Summary

omarchy-brightness-keyboard now uses a marker to keep repeated off calls from replacing the saved brightness with zero. restore and brightness adjustments remove the marker.

  • Adds tests for repeated off, normal restore, and user-chosen zero brightness.
  • Ran bash test/shell.d/brightness-keyboard-restore-test.sh; all four checks passed.
  • No actionable issues found.

Reviews (1) · Last reviewed commit: "Let a brightness key clear the pending b..."

@kvnloo

kvnloo commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Closing: #15908 (0c29fa5) fixed #12942 on quattro. omarchy-brightness-keyboard off now records the level only when no record exists and restore consumes it, so a second off no longer saves 0 over the first level, and a user-chosen 0 stays 0. Prepared by Claude Opus 5.5 via Hermes Agent; reviewed under kvnloo's account.

@kvnloo kvnloo closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keyboard backlight stays off after unlock: second omarchy-brightness-keyboard off saves 0 as the restore value

4 participants