Skip to content

Preserve manual keyboard brightness changes across wake - #10364

Open
catlee wants to merge 1 commit into
omacom:quattrofrom
catlee:fix/keyboard-backlight-restore
Open

catlee wants to merge 1 commit into
omacom:quattrofrom
catlee:fix/keyboard-backlight-restore

Conversation

@catlee

@catlee catlee commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Keyboard brightness changes made while locked can be undone on wake. If the lock saves level 3 before blanking and I adjust the brightness to 1, restore brings it back to 3.

Manual adjustments now take the same lock as blank and restore, then clear the saved level after a successful change. Failed adjustments keep the saved level and return before showing the brightness overlay.

This builds on #15908 and keeps its state filename and restore handling. Hardware-key changes that bypass this command are outside this change.

Validation: the new regression fails on unchanged quattro. The keyboard behavior tests, ./test/cli, and Bash syntax checks pass, including manual adjustments to zero, failed adjustments, and a manual adjustment overlapping a blank. Physical keyboard behavior has not been tested for this revision.

@mmahut mmahut left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The change set looks good, but please:

  • Rename to test to brightness-keyboard-test.sh. It is keyboard-brightness-test.sh, but the repo convention is command-name order, as in brightness-display-test.sh.
  • Issue #10105 is the same bug and should be closed by this too. Add Fixes #10105.

@catlee
catlee requested a review from mmahut September 14, 2026 14:42
@catlee

catlee commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Thank you - I've made those changes.

@Susensio

Susensio commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Related finding, on the same symptom but a separate path this PR doesn't cover: after a long suspend-then-hibernate my Dell woke with the keyboard backlight off even with this patch applied.

The system-sleep hook that omarchy-hibernation-setup installs sets the backlight to 0 before hibernating (the ASUS S4 workaround) but never restores it. It also bypasses omarchy-brightness-keyboard, so the wake-time restore has nothing to restore.

I've opened #13729 to route the hook through omarchy-brightness-keyboard off/restore. It's independent of this PR and they complement each other: this one fixes the command, that one makes the hook use it. Happy to fold it in here instead if that's preferred.

@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 verified on a disposable Omarchy worker VM, and I pushed one commit to this branch (df749b8).

Reproduction. I used the real brightnessctl against a real LED class device named *kbd_backlight (created through /dev/uleds), not a mock. Starting at 2, off, off, restore leaves the keyboard at 0 on quattro and at 0 again with bin/omarchy-brightness-keyboard reverted, and restores 2 on this branch. A keyboard that starts at 0 stays at 0. ./test/cli passes, and test/shell.d/brightness-keyboard-test.sh passes 14/14 both on this head and merged with current quattro.

What I pushed. The lock service runs blank and wake as two separate processes, so a restore can land between off saving the level and setting 0. With the snapshot file, that restore consumed the snapshot, then off blanked the keyboard with nothing left to restore, and it stayed dark until changed by hand. On quattro, brightnessctl -r leaves its save file in place (checked on the worker), so the next wake recovered. The commit takes one flock across off, restore and manual changes. The lock is released before omarchy-osd, so held repeat keys don't queue behind shell IPC. The test now also asserts that off actually blanks the keyboard, and that a restore blocks on the lock until an in-flight blank has saved its level. That test fails against the previous head.

Second opinion. Codex Medium reviewed this in three rounds. It found the race above, then two follow-ups in my fix: the lock was held across the OSD call, and the first version of the race test didn't prove contention and could hang. Its last round on df749b8 was clean. It also agreed this change is the proportionate fix for repeated blanks.

Scope and related work. This fixes the double-off cause behind #7650 and #10105. It does not fix #6667: when firmware such as Dell's stop_timeout has already dimmed the keyboard before the lock, the first off correctly saves 0. #13729 is complementary, since it routes the hibernate sleep hook through this command. #8119 approaches the same symptom in shell/plugins/lock/Service.qml. Codex Medium compared the two and recommended this one, and both shouldn't land unchanged: #8119's direct restore would leave this branch's snapshot behind for a later omarchy-system-wake to apply.

Next. This is waiting on CI, then on the maintainer. Nothing is needed from you.

@greptile-apps

greptile-apps Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The new manual-change behavior appears safe to merge.

Summary

Manual keyboard brightness changes now share the lock used by blank and restore.

  • Manual keyboard brightness changes keep their setting through wake.

Reviews (3) · Last reviewed commit: "Preserve manual keyboard backlight chang..." · Reviewed by Greptile

Comment thread bin/omarchy-brightness-keyboard Resolved
@NikodemNowak

Copy link
Copy Markdown

I reproduced the same repeated-blank problem on an ASUS Vivobook S 16 M5606UA with Omarchy 4.0.4-1, kernel 7.2.5-3-omarchy, and asus::kbd_backlight (max 3).

A local serialized first-blank guard keeps level 2 through off -> off -> restore. The behavioral tests also preserve an intentional 0 and a manual brightness adjustment while blanked. I am using a local workaround and leaving the upstream saved-brightness fix to this PR rather than opening a duplicate.

There is another ASUS-specific detail: F4 can be consumed by asus-wmi and reported only through brightness_hw_changed. Hardware changes while blanked need to invalidate any old snapshot too, or a later wake can override the level the user just chose with the physical key.

@catlee

catlee commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the unsafe lock path and added checks for hardware brightness changes in caf7fc1, including changes to zero. The PR description documents the remaining limitation: a hardware cycle back to the same reported value with current brightness zero cannot be detected without a background monitor.

The keyboard regression tests and CLI suite pass.

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

Copy link
Copy Markdown
Collaborator

Thanks for this, @catlee, and for the follow-up in caf7fc1. Since the last review, quattro has merged #15908, which fixes the same repeated-blank bug in bin/omarchy-brightness-keyboard with a different design and state file name (${XDG_RUNTIME_DIR:-/tmp}/omarchy-brightness-keyboard-$device). #7650 and #10105 are closed. This branch now conflicts with quattro in both bin/omarchy-brightness-keyboard and test/shell.d/brightness-keyboard-test.sh.

What was checked: I read caf7fc1 against quattro at d367a56 and against #15908's commit 0c29fa5. Nothing was run on a worker this time. The head cannot merge, so a reproduction at this head would test code that cannot land as it is. The earlier worker reproduction was at df749b8, before the conflict. The second opinion came from Codex Medium and muse-wonder (via pi, high thinking). Both were given the two diffs. They agree with the reading below, but their independence from it is not guaranteed.

What #15908 already covers: it keeps the first saved level across repeated offs and consumes it on restore. It locks blank and wake against each other, and it keeps the record when a restore is rejected. That is the core of this PR.

What this PR still does that quattro does not:

  • It discards the saved level after a successful manual up/down/cycle. The keyboard-brightness bindings in default/hypr/bindings/media.lua:10-12 are locked = true, so on quattro a key press on the lock screen is undone by the next wake: blank at 2, press down to 1, unlock, and the level is back at 2. This is the part with the clearest user-facing value. It is a few lines on top of quattro's code: take the lock for manual directions too, and remove the record after a successful set.
  • It discards the saved level when the current level, or brightness_hw_changed, shows a change made outside the command. This is the ASUS asus-wmi case @NikodemNowak described. It is real but depends on the hardware, and it has the blind spot your description already names.
  • It refuses a shared or symlinked state directory when XDG_RUNTIME_DIR is unset. That only matters outside a normal logind session.

What a port would need to keep from quattro: both models flagged behaviour in this head that quattro's version handles better.

  • An empty or non-numeric record wedges restore here. If the level is 0 and the hardware value is unchanged, the record is not discarded. brightnessctl set "" then fails and exits before the rm (bin/omarchy-brightness-keyboard:43-62), so every later restore fails the same way. Quattro treats an empty record as none and drops one it cannot parse.
  • The renamed state file would orphan a level saved by quattro's off across the update.
  • The private-directory check runs before plain up/down too, so an existing fallback directory that is not mode 700 disables the keys entirely.

What happens next: this waits on the maintainer to choose between two options. One is to close this as covered by #15908 and take the manual-change invalidation, plus the external-change check if wanted, as a small change on top of quattro's script. The other is for you to rework this branch onto quattro's design along those lines. Nothing was pushed to your branch.

@catlee
catlee force-pushed the fix/keyboard-backlight-restore branch from caf7fc1 to 390e475 Compare November 1, 2026 03:08
@catlee catlee changed the title Preserve keyboard backlight across repeated blanks Preserve manual keyboard brightness changes across wake Nov 1, 2026
@catlee

catlee commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for pointing this out. I've reworked this on top of quattro in 390e475 and narrowed it to preserving manual brightness changes across wake.

Manual up/down/cycle now take the same lock as off and restore, and clear the saved level after a successful adjustment. If a set fails, the saved level stays intact and the command returns before showing the brightness overlay. This keeps #15908's state filename, record validation, and restore retry handling.

I've left hardware changes that bypass the command out of this revision. I think those need a separate change with hardware testing.

The new regression fails on unchanged quattro. The keyboard behavior tests, ./test/cli, and Bash syntax checks pass, including adjustments to zero, failed adjustments, and an adjustment overlapping a blank. I haven't tested this revision on a physical keyboard.

@omarchybot

Copy link
Copy Markdown
Collaborator

Thanks for reworking this onto quattro, @catlee. I re-reviewed 390e475 against quattro d367a56. Claude Opus 5.5 did the review, and Codex Medium and muse-wonder (via pi, thinking high) gave second opinions.

The bug is real and the approach fits it. The keyboard brightness keys are bound with locked = true, so they work on the lock screen after the lock has blanked the keyboard. On quattro, the next omarchy-system-wake writes the pre-blank level back over whatever was chosen. Taking the existing lock for every direction and dropping the saved level after a manual change is the right fix for that. Both reviewers agreed.

One case it gets wrong. In bin/omarchy-brightness-keyboard, line 78 removes the saved level after any successful set, including one that changes nothing. Here is the sequence: the keyboard is at 3, the lock blanks it and saves 3, and then brightness-down is pressed (or a repeat arrives) while it is dark. That press sets 0 over 0, deletes the saved 3, and the keyboard stays off after unlock. The new tests step down from 1 to 0, which is a real change, so they never hit 0 to 0. I found this by reading the code, and both reviewers traced the same case independently. I did not run it on a worker.

Competing fixes. #10366 by @marcindyguda, opened 21 minutes after this one, makes the same change: the same lock for every direction, and it drops the saved level after a manual change. It only drops it when the level actually moves, though, so down at 0 keeps the saved level. It also leaves alone a keyboard that something outside this script, like a firmware-handled key, has lit since the blank. The oldest fix normally wins, but that no-op case is a defect this one has and #10366 doesn't, so #10366 is the one being brought forward. #6760 is older still, but it conflicts with quattro and keeps a persistent preference model that was left for the maintainer to decide separately.

What this one still adds. Two things here are better than #10366: a failed set exits before showing the OSD (#10366 shows a level that never applied), and the lock is closed before the OSD runs (#10366 holds it through the OSD call). Both are handed over to be folded into #10366, crediting you as co-author. Nothing was pushed to this branch.

This waits on the maintainer to decide between the two.

omarchybot added a commit to marcindyguda/omarchy that referenced this pull request Nov 1, 2026
A manual change held the lock through omarchy-osd, which can wait on a stalled shell, so held keys, blank and wake queued behind it. A key the device rejected still showed the level it never set. Both come from omacom#10364.

Co-Authored-By: Chris AtLee <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

5 participants