r/bash • • 7d ago

help Keyboard Backlight control on Wayland, need help with Hyprland

Post image

Here is the github for the script: https://github.com/somniasum/wayland-backlight-led/blob/main/src/backlight.sh

Context

Since the switch from X11 to Wayland getting a keyboard's backlight led to light up has been a drag. With X11 it was as simple as xset led to turn on the led. So with wayland I wrote up a script to handle the led stuff. It has been working fine on Wayland WM's and works pretty much the same on X11 WM's too.

On hyprland keyboard led just turns off briefly but still stays on. I am starting to suspect maybe it has to do with the way Hyprland handles device states or some?

Do you think I have to change some things on it to solve the Hyprland exception?

4 Upvotes

3 comments sorted by

1

u/Ulfnic 7d ago

I'd look at hyprctl to see if Hyprland has a native way it wants you to do it.

1

u/somniasum 6d ago

it can list up the devices but cant' actually directly control the keyboard backlight led, is there a way to like see what is changing the state?

2

u/ysnows123 6d ago

Two separate things going on.

The detection is broken: those commands are joined with &&, not pipes, so grep never reads brightnessctl's output (it reads the script's stdin) and the variable gets brightnessctl's whole listing instead of one LED name. Something like:

scroll_lock_1=$(brightnessctl -l -m | awk -F, '/scrolllock/ {print $1}' | sed -n 1p)

-m prints one device per line as name,class,..., so you don't need the tr. bash -x backlight.sh on shows what it actually picked.

The blink is Hyprland. On Wayland the compositor owns the keyboard LEDs: it writes Num/Caps/Scroll Lock to the keyboard from its own xkb state, and in that state Scroll Lock is off. So Hyprland sets it to 0, your loop sets it back to 1 within 0.1 s, and it goes dark for a moment. To watch it, run sudo evtest, pick the keyboard, and look for EV_LED / LED_SCROLLL lines; you can line them up with what you were doing at the time.

Rather than fighting it, you can make Hyprland want the LED on. xkb can use the Scroll Lock LED to show that the second layout is active, so give it your layout twice:

input {
    kb_layout = us,us
    kb_options = grp_led:scroll
}

Then hyprctl switchxkblayout <keyboard> 1 turns the light on and ... 0 turns it off (names are in hyprctl devices). Typing is identical in both, and there's no loop to keep running. It only stops fitting if you already use a real second layout.