omarchy icon indicating copy to clipboard operation
omarchy copied to clipboard

Keyboard backlight never comes back after screensaver/lock: `omarchy-brightness-keyboard off` saves 0 when already off, destroying the state `restore` needs

Open rebelpixel opened this issue 1 month ago • 4 comments

Summary

omarchy-brightness-keyboard off uses brightnessctl -s (save) unconditionally, so when it runs while the keyboard backlight is already at 0, it overwrites the previously saved level with 0. The matching restore then faithfully restores 0, and the backlight never comes back after the screensaver/lock.

Steps to reproduce

Any keyboard backlight device works; tpacpi::kbd_backlight (ThinkPad, 3 levels 0–2) on this machine:

D=tpacpi::kbd_backlight
S="$XDG_RUNTIME_DIR/brightnessctl/leds/$D"

brightnessctl -d "$D" set 2        # backlight on
omarchy-brightness-keyboard off    # -> saves 2, sets 0   (state file = 2)  ✅
omarchy-brightness-keyboard off    # -> saves 0, sets 0   (state file = 0)  ❌ value destroyed
omarchy-brightness-keyboard restore # -> restores 0, backlight stays off

Raw brightnessctl equivalent:

brightnessctl -sd "$D" set 0   # 1st: saved=2
brightnessctl -sd "$D" set 0   # 2nd: saved=0  <-- clobber
brightnessctl -rd "$D"         # restores 0

Why it happens in normal use

The lock screen re-arms its blank timer on every wake/password attempt. In shell/plugins/lock/Service.qml:

  • beginLock() → armBlankTimer() → 5s later runBlank() → blankProcess runs omarchy-brightness-keyboard off
  • runWake() → armBlankTimer() again while lockRequested, so off re-fires

The first off saves the real level and sets 0. A later off (still at 0) saves 0, destroying it. The same applies after a resume that zeroes the panel before the blank lands.

Restore is wired correctly — omarchy-system-wake → omarchy-brightness-keyboard restore → brightnessctl -rd — it just has nothing left to restore.

Expected

off should only save state when the backlight is actually lit, e.g.:

if [[ $direction == "off" ]]; then
  if [[ "$(brightnessctl -d "$device" get)" != "0" ]]; then
    brightnessctl -sd "$device" set 0 >/dev/null
  else
    brightnessctl -d "$device" set 0 >/dev/null   # already off; don't clobber the save
  fi
  exit 0
fi

That keeps restore working for the screensaver/lock cycle.

Environment

  • Omarchy 4.0.6-1
  • Kernel 7.2.8-9-omarchy
  • ThinkPad, AMD Ryzen 7 PRO 6850U
  • brightnessctl 0.5
  • Backlight device: tpacpi::kbd_backlight (max 2)

Additional context

brightnessctl's save file lives in $XDG_RUNTIME_DIR/brightnessctl/ (tmpfs), so the corruption also resets on reboot — but any single double-off within a session reproduces it.

Note: omarchy debug (referenced in the contributing guide) does not exist in 4.0.6-1 — Unknown Omarchy command: omarchy debug --no-sudo --print.

rebelpixel avatar Nov 02 '26 03:11 rebelpixel

Follow-up: the fix I suggested in the body is incomplete — I verified it fixes the clobber but introduces a regression. Recording it here so whoever picks this up doesn't ship the naive version.

The naive fix regresses intentional-off

My body suggested only saving state in off when the backlight is lit:

if [[ "$(brightnessctl -d "$device" get)" != "0" ]]; then
  brightnessctl -sd "$device" set 0   # lit: save level
else
  brightnessctl -d "$device" set 0    # dark: don't clobber
fi

That stops the clobber, but a deliberate user turn-off no longer survives lock/wake. The up/down/cycle keybindings use a plain set, so they never write the state file — it keeps holding the last non-zero level from a previous blank, and restore relights a backlight the user meant to leave off.

Repro (device tpacpi::kbd_backlight, level 2):

0. baseline:  br=2 state=2
1. user off:  br=0 state=2   <- plain set, state file untouched (stale)
2. blank off: br=0 state=2   <- naive fix skips -s, state still 2
3. wake:      br=2           <- PROBLEM: relit, user wanted OFF

Why a one-line fix can't work

The state file has to mean "what restore should put back", and two different events write it with opposite intent:

Event Desired restore
Lock blank turns it off the previous lit level
User keybinding turns it off 0 (stay dark)

off alone can't distinguish them, because by the time off runs during a blank, the user may already have turned it off. The intent has to be recorded at the moment the user acts.

Fix that handles both (verified, 4/4 cases)

Two changes:

1. off (the lock blank) saves only when lit — avoids the clobber:

if [[ $direction == "off" ]]; then
  if [[ "$(brightnessctl -d "$device" get)" != "0" ]]; then
    brightnessctl -sd "$device" set 0   # lit: save level for restore
  else
    brightnessctl -d "$device" set 0    # already dark: don't touch the save
  fi
  exit 0
fi

2. The user keybinding path records intent when it lands on 0 — note -s saves the value before the set, so the save must happen after the level is already 0, otherwise it records the old level:

brightnessctl -d "$device" set "$new_brightness"    # apply the keypress
if (( new_brightness == 0 )); then
  brightnessctl -sd "$device" set 0                 # current is 0 now, so -s records 0
fi

Results with both parts:

CASE 1  blank while lit            wake -> br=2   ✅ comes back
CASE 2  user off, blank, wake      wake -> br=0   ✅ stays off
CASE 3  double-off clobber         wake -> br=2   ✅ comes back (the bug in this issue)
CASE 4  user off, back on, blank   wake -> br=1   ✅ comes back

Note on detection

The only reliable way to tell a user turn-off from a blank is that only off writes the state file — up/down/cycle use a plain set and leave it alone. Worth preserving that property if this gets refactored; a workaround I'm running locally keys off exactly that signal.

If off starts saving unconditionally again (or the keybindings start saving), that distinction breaks.

rebelpixel avatar Nov 02 '26 09:11 rebelpixel

This looks fixed on current quattro (ae64a4b). omarchy-brightness-keyboard no longer uses brightnessctl -s. off now writes its own state file under $XDG_RUNTIME_DIR only when that file is empty, behind a flock. restore deletes the file once the level is applied. A second off while the backlight is already dark can't overwrite the saved level, and the up/down/cycle keybindings never touch the file. Should be safe to close once it ships.

jamesponwith avatar Nov 03 '26 12:11 jamesponwith

I reproduced a related case: a quick lock/unlock turns the keyboard backlight on even though it was off immediately before locking.

Environment

  • Lenovo ThinkPad X1 Carbon Gen 9
  • Omarchy 4.0.6-1
  • Kernel 7.2.8-9-omarchy
  • brightnessctl 0.5.1-3
  • Device: tpacpi::kbd_backlight, levels 0–2
  • No local fixes applied

Reproduction

  1. Have a previous saved keyboard brightness of 1.
  2. Manually turn the keyboard light off (0).
  3. Lock, then unlock within five seconds.

Expected: the light remains off. Actual: the light turns on at level 1.

Evidence from November 4, 2026 (UTC+8)

  • Lock requested: 12:37:08.127
  • Unlocked: 12:37:10.985
  • Actual brightness afterward: 1
  • Saved brightness remained 1, with its modification time unchanged at 11:39:34.899.

In the installed lock service, brightness is saved only when the five-second blank timer runs. Unlocking before that cancels the timer and runs omarchy-system-wake, restoring the older saved value without first capturing the current off state.

This provides a quick-unlock reproduction of intentional-off being lost, without modifying the backlight scripts.

AFwcxx avatar Nov 04 '26 04:11 AFwcxx

Fixed in 4.5.0 — confirmed on 4.5.0-3.

omarchy-brightness-keyboard was reworked to keep its own record at $XDG_RUNTIME_DIR/omarchy-brightness-keyboard-<device> instead of using brightnessctl -s/-r:

  • off records a level only when no record exists, so a re-armed blank can no longer save 0 over a valid level (the bug reported here).
  • restore applies the recorded level and removes the record.
  • off and restore are serialized with flock, covering the overlapping lock-blank/wake case.

Verified on a ThinkPad tpacpi::kbd_backlight with no local workarounds running:

  • double off while lit → wake restores the original level
  • 3× blank/wake cycles → level preserved, record cleaned up
  • user turns the backlight off → off → wake → stays off (intentional off preserved)
  • 20 concurrent off/restore → no corruption, no crash

Closing as fixed.

rebelpixel avatar Nov 08 '26 18:11 rebelpixel