Skip to content

Restore the keyboard backlight level after hibernation - #11669

Open
alanwcurry-dev wants to merge 7 commits into
omacom:quattrofrom
alanwcurry-dev:fix/keyboard-backlight-after-hibernate
Open

alanwcurry-dev wants to merge 7 commits into
omacom:quattrofrom
alanwcurry-dev:fix/keyboard-backlight-after-hibernate

Conversation

@alanwcurry-dev

@alanwcurry-dev alanwcurry-dev commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

An ASUS laptop comes out of a hibernate with a dark keyboard, and it stays dark until a brightness key is pressed.

/usr/lib/systemd/system-sleep/keyboard-backlight exists so an active LED cannot hang the S4 transition, and it clears the backlight in the pre phase. Clearing is one-way — there is nothing to read back once brightness is 0 — and the hook has no post phase, so the level is simply gone. Nothing else covers for it either: the paths that turn the keyboard back on are omarchy-system-wake and omarchy-brightness-keyboard restore (brightnessctl -r), used by idle and lock, and they only replay a level brightnessctl saved itself. The hook writes the LED directly and saves nothing, so once it has cleared the LED there is nothing left for them to restore.

Evidence from the machine

ASUS ExpertBook PM3406CKA, Omarchy 4.0.3-1, kernel 7.2.3-arch1-3. LED set to 3, RTC alarm armed, systemctl hibernate:

=== BEFORE 06:53:04 LED=3 ===
=== AFTER  06:58:47 LED=0 ===    # nothing recorded it; it never came back

The same cycle with the save/restore in place — a real hibernation, the machine off for ~140s in between, woken by the RTC alarm:

2026-09-13 06:53:10 pre recorded level 3 (before the clear hook runs)
2026-09-13 06:55:33 post restored level 3

The machine this was found on also runs a third-party idle-restore plugin (not part of Omarchy), which then adopts the restored level as its own hold — idle timeout reached; saved brightness 3, its state record holding {"brightness": 3, …} — so activity restores the user's level instead of leaving a dark keyboard.

What the change does

  • The hook records the level before the clear and writes it back in the post phase.
  • The restore only runs while the LED still reads 0. A lit LED wins, so a level already put back by omarchy-system-wake's restore (idle or lock) or by the user is never fought.
  • The record lives in /run/omarchy-keyboard-backlight-level: it survives the hibernate inside the image and disappears on reboot.
  • The LED is now read and written through sysfs. Since this hook became root-owned (migrations/1788662350.sh) it does not need brightnessctl, and dropping it means a missing brightnessctl can no longer silently skip the clear. It also lets the tests assert the LED instead of a stubbed command — the keyboard section of system-sleep-ownership-migration-test.sh now does that.
  • Only hibernate is touched. Suspend keeps the LED powered, so its level is never lost.

Reaching existing installs

omarchy-hibernation-setup returns early once hibernation is configured, so it never refreshes this hook on an existing machine — a fix in default/ alone would only reach fresh installs. The migration replaces the file exactly as this release supersedes it (byte-for-byte match against the previous version) and leaves everything else alone, including a customized hook and a machine where the hook was never installed. A second run is a no-op.

I am happy to move that refresh into omarchy-hibernation-setup instead if you would rather the setup command own it; the migration exists because that early return is precisely what misses existing installs.

Tests

./test/shell (239 files) in this checkout: the four files touched here pass. Five others fail, none of them related to this change — bar-icon-geometry-test.sh (a Quickshell geometry assertion), config-test.sh, snapper-test.sh and unowned-system-paths-test.sh (they want an omarchy-pkgs checkout) fail identically at upstream HEAD in a clean worktree, and locate-test.sh failed only because running the whole suite in my checkout left an ignored bin/__pycache__/ that the test reads as UTF-8 — it passes as soon as that artifact is removed.

Case Where
pre clears the LED and records its level test/shell.d/keyboard-backlight-hook-test.sh (sandboxed LED via path rewrite)
post restores the recorded level same
post leaves an LED that something else lit alone same
a keyboard already dark before hibernate stays dark same
suspend and other sleep actions untouched same
migration replaces the superseded hook, mode 0755 test/shell.d/keyboard-backlight-hook-migration-test.sh
migration is a no-op on re-run same
migration leaves a customized hook alone same
migration does not install the hook where it never was same
compound sleep clears only on the hibernate phase existing system-sleep-ownership-migration-test.sh, updated to assert the LED
real hibernation round trip, level preserved log above, on hardware

unowned-system-paths-test.sh fails in this checkout with and without the change (it wants an omarchy-pkgs checkout), so that one is not from this PR.

Judgement calls I can change

  • the record path (/run/omarchy-keyboard-backlight-level);
  • whether the refresh belongs in the migration or in omarchy-hibernation-setup;
  • whether the existing sleep test's keyboard section should be rewritten to assert the LED, or keep brightnessctl in the clear path and stub it as before.

The hibernate hook clears the LED before S4, because an active backlight can
block the ASUS transition. Clearing it is one-way — the level cannot be read
back once it reads 0 — and nothing wrote it back, so the keyboard came out of
every hibernate dark and stayed dark until a brightness key was pressed. The
idle-restore plugin cannot cover for it: it refuses to remember a 0.

The hook now records the level before the clear and writes it back in the post
phase, and only while the LED still reads 0, so a level put back by the
idle-restore plugin or a lock's own restore is never fought. It also writes the
LED through sysfs now that the hook is root-owned, which is what lets the tests
assert the LED instead of a call to brightnessctl.

omarchy-hibernation-setup returns early once hibernation is configured, so an
existing install would keep the old hook; the migration replaces exactly the
file this release supersedes and leaves any other hook, or a customized one,
alone.
@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
omarchybot and others added 2 commits October 1, 2026 03:03
The record held a bare level, and each phase picks the first kbd_backlight device on its own. When that device changes across the hibernate, such as an external keyboard unplugged meanwhile, post would light a keyboard that was deliberately dark with another keyboard's level. Record the device beside the level and restore only when it matches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
sha256sum follows a symlink, so a link resolving to the superseded hook matched and mv replaced the link with a standalone file, detaching the administrator's target. 1788662350 preserves safe symlinks at the same path; this migration now does too.

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 1, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed and verified on a disposable Omarchy worker. I pushed two small fixes to your branch.

Reproduction. The hook is run pre then post hibernate against a sandboxed LED at level 3, with its logic as written and only the /sys/class/leds and /run paths rewritten. On quattro (8b4eae6) the keyboard comes back at 0. At this head it comes back at 3. With the hook reverted it is 0 again. That proves the mechanism: the hook clears the LED and nothing writes it back. The round trip on real ASUS hardware rests on the log in your description; the worker has no keyboard backlight.

Migration. Run for real as root on the worker. It replaced an installed copy of the previous hook with the new one, root-owned and 0755, and a second run left it alone. The upgrade path that crosses 1788662350 was also checked: that migration moves the older f313a81e… hook straight to the new source, and this one then skips it.

Pushed to your branch:

  • 51f29aea records the device beside the level and restores only to that device. Each phase picks the first *kbd_backlight* device on its own. So if the device list changes across the hibernate, for example an external keyboard unplugged meanwhile, post could light a keyboard that was deliberately dark with another keyboard's level. A test covers it.
  • 9979f542 makes the migration skip a symlink at the hook path. sha256sum follows the link, so a link to a copy of the old hook matched, and mv replaced the link with a plain file. 1788662350 preserves safe symlinks at the same path, and this now matches it. A test covers it.

Tests on the worker at 9979f542. ./test/cli passed. The four touched shell tests passed: keyboard-backlight-hook-test.sh, keyboard-backlight-hook-migration-test.sh, system-sleep-ownership-migration-test.sh and unowned-system-paths-test.sh.

The full shell suite is not green, and none of it comes from this change. On your branch as it stands, ascii-test.sh and branding-about-animation-test.sh fail exactly as they do on quattro. screenshot-sanity-test.sh fails exactly as it does at your fork point 692c02ca with none of your commits; that branch is 252 commits behind quattro. Merged into current quattro, the same 8 shell files and one CLI test fail as on quattro alone, and your two new files pass. locate-test.sh fails only after a full run leaves bin/__pycache__ behind, as you noted. There is no CI on this pull request.

One inaccuracy, not changed. The description and the comments in the hook and its test refer to an "idle-restore plugin" with a keyboardIsIdle guard. Nothing like that exists on quattro. The paths that relight the keyboard there are omarchy-system-wake and omarchy-brightness-keyboard restore (brightnessctl -r), used by idle and lock. The "a lit LED wins" guard is still sound against them, so I left your wording alone.

Second opinion. Codex Medium reviewed it in two rounds. The first round found the two defects fixed above. The second, at 9979f542, found nothing open and judged the change proportionate. On the second round it agreed with conclusions already written down, so its independence there is not guaranteed.

The change is proportionate: a save and restore in the hook, plus a narrowly matched migration, which is needed because omarchy-hibernation-setup returns early on machines that already hibernate.

This now waits on the maintainer.

@greptile-apps

greptile-apps Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Low impact] The change affects keyboard backlight behavior around hibernation.

The shell-style requirement in migrations/1789301036.sh must be satisfied before merging.

Findings

  1. P2 Comparison breaks required shell style ▶
  2. P2 Failed save still clears LED ▶

Summary

Saves the keyboard backlight level before hibernation and restores it afterward.

  • The keyboard backlight returns to its saved level after hibernation.

Reviews (2) · Last reviewed commit: "Hash a readable keyboard backlight hook ..." · Reviewed by Greptile

Comment on lines +40 to +41
printf '%s %s\n' "$device" "$(read_level "$device")" >"$level_record"
printf '0\n' >"$led" 2>/dev/null

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 Failed save still clears LED If saving the level fails—for example, because /run is full—the hook still clears the backlight. The post phase then has no usable level to restore, leaving the keyboard dark after hibernation. Check that the level was saved before clearing it.

@jandrusk

Copy link
Copy Markdown

Looks solid (save/restore + narrowly matched migration; omarchybot already fixed device-id + symlink cases). Crowded space: #13729 / #12553 / #12502 / #14066 also restore keyboard backlight across sleep — please pick one approach so we don't ship conflicting hooks.

@alanwcurry-dev

Copy link
Copy Markdown
Contributor Author

Thanks — good to know someone is tracking the crowding, because three of the five are the same file.

Same file, so only one of them survives: #11669 (this one), #13729 and #14066 all rewrite default/systemd/system-sleep/keyboard-backlight, and each carries its own migration for the copy that is already installed.

Different layers, not competing:

Which hook I would keep: #14066. It is the only one that also fixes the hooks being installed non-executable (#7618, which is why systemd-sleep was silently skipping them), it scopes the hook and its LED glob to ASUS, saves a level per LED name, and its migration recognises every version Omarchy shipped by hash while skipping symlinks and anything an administrator wrote. Its failed-save behaviour is a stated decision (the LED still goes off, and it is logged) rather than an oversight — that is the one open greptile note on mine, and theirs answers it.

One thing I would carry across before it lands: the restore should only write while the LED still reads 0. During post, omarchy-system-wake (idle or lock) or the user can already have relit the keyboard, and #14066 writes the saved level unconditionally, which can undo that. Mine has the guard, and a test for it ("post leaves an LED that something else lit alone"). If you would rather keep one command behind every off/restore, #13729's shape is the neater version of that — the hook calls omarchy-brightness-keyboard off/restore with XDG_RUNTIME_DIR moved to /run — and #12502's command-side guard covers the same case from the other end.

So: happy to close #11669 in favour of #14066, or of #13729 if you prefer the shared-command route. Either way I will port the "a lit LED wins" guard and my hardware logs (a real hibernate round trip, pre recorded level 3 / post restored level 3) into whichever you pick — the ROG Strix and Dell evidence on the other two comes from running the hook by hand, not from a full hibernate cycle.

One correction to my own description, now fixed: it leaned on an "idle-restore plugin" as if it were part of Omarchy. That is a third-party plugin on my machine; upstream, the paths that relight the keyboard are omarchy-system-wake and omarchy-brightness-keyboard restore, which is what it should have said. The two comments in the hook and its test that say the same thing need a commit — I will only make it if this PR is the one going forward.

@omarchybot omarchybot removed the verified Omarchy Triage has verified that this issue is ready for final review label Oct 31, 2026
omarchybot and others added 4 commits November 1, 2026 10:52
quattro removed migration tests (omacom#16302) and its AGENTS.md now says a migration gets no suite test, so the conflicting keyboard section of system-sleep-ownership-migration-test.sh goes with that file, and keyboard-backlight-hook-migration-test.sh and its fixture are dropped. The hook test stays: it tests the hook, not the migration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The post phase picked the first *kbd_backlight* device again and restored only when that matched the record, so a keyboard that sorts first appearing during the resume left the cleared keyboard dark. Write the level back to the recorded device itself, while it still exists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Post exited before reading the record when no *kbd_backlight* device was listed, so the record survived in /run and a later resume could apply that level to a keyboard the cycle never cleared. Post now goes by the record alone; only pre needs a device.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: muse-wonder High <[email protected]>
The installed hook is world-readable, so a rerun for a second user can see it was already replaced without prompting for sudo, as 1788662350 does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: muse-wonder High <[email protected]>
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Nov 1, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

This branch had stopped merging into quattro, so I brought it across and re-verified it on a disposable Omarchy worker. It merges cleanly again at 33086c5d.

The merge (0ae4629d). quattro removed migration tests in #16302, and its AGENTS.md now says a migration gets no suite test. So the conflicting keyboard section of system-sleep-ownership-migration-test.sh went with that file, and keyboard-backlight-hook-migration-test.sh and its fixture were dropped. keyboard-backlight-hook-test.sh stays, because it tests the hook rather than the migration.

Pushed to your branch:

  • 734a54f5 makes post write the level back to the keyboard it was recorded from. Before this, post picked the first *kbd_backlight* device again and restored only if that matched the record, so a keyboard that sorts earlier appearing during the resume left yours dark. Greptile raised this.
  • 24d1fd7a makes post consume the record even when no keyboard is listed at resume. Otherwise the record stayed in /run, and a later resume could apply that level to a keyboard that cycle never cleared.
  • 33086c5d makes the migration hash a readable hook without sudo, as 1788662350 does. That way the rerun for a second user does not prompt just to find there is nothing to do.

Each commit has a test case except the sudo one, which was checked by hand on the worker.

Verified on the worker at 33086c5d:

  • Reproduction. I ran pre then post hibernate against a sandboxed LED at 3. quattro's hook leaves it at 0, this head restores 3, and with quattro's hook put back it is 0 again.
  • Migration. Run as root, it replaced quattro's hook (root, 0755). It left alone a second run, a customized hook, a symlink and a missing hook. A rerun with sudo unavailable exits 0.
  • Tests. ./test/cli passes. ./test/shell: 6 of 414 files fail (agent-account-exec, ascii, branding-about-animation, first-run, remote-session-service, runtime-smoke), and each fails the same way on quattro 9e0b5e2. None of them is from this change.
  • Hardware. The real ASUS hibernate round trip still rests on your log; the worker has no keyboard backlight.

Second opinion. Codex Medium and muse-wonder (via pi, thinking high) reviewed it in two rounds. Codex found nothing. muse-wonder found the stale record and the sudo-on-rerun issue fixed above, and the second round was clean from both. Both models, and I, agree with keeping the clear even if saving the level fails, contrary to Greptile's other comment. The clear is what keeps the S4 power-off from hanging, so a dark keyboard is the better failure. Their agreement here is not guaranteed to be independent of my own reasoning.

Small things left, @alanwcurry-dev:

  • The description still mentions the migration test and the system-sleep-ownership-migration-test.sh change, which are no longer in the diff.
  • The comment at default/systemd/system-sleep/keyboard-backlight:51 names "the idle-restore plugin", which is your third-party plugin rather than part of Omarchy. Omarchy's own path is omarchy-brightness-keyboard restore from idle and lock.

Waiting on the maintainer for the choices you listed: the record path, and whether the refresh belongs in this migration or in omarchy-hibernation-setup.

Comment thread migrations/1789301036.sh
installed_sha256=$(as_root sha256sum "$hook_destination" | cut -d' ' -f1)
fi

if [[ $installed_sha256 != "$previous_hook_sha256" ]]; 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 Comparison breaks required shell style

This comparison quotes $previous_hook_sha256 inside [[ ]], but the repository guide requires variables to stay unquoted in these tests. Use [[ $installed_sha256 != $previous_hook_sha256 ]]. The comparison works as written, but this repository requirement must be satisfied before merging.

Suggested change
if [[ $installed_sha256 != "$previous_hook_sha256" ]]; then
if [[ $installed_sha256 != $previous_hook_sha256 ]]; then

File Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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

Labels

bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants