0

I'm trying to get my head around shell scripting to solve a minor Wayland/Mutter bug that hasn't been addressed in over two years. I'm completely unfamiliar with shell scripting, but I think this should be possible.

The problem is that on Wayland the xkb option to indicate keyboard layouts with a LED doesn't work correctly. It works on first layout switch, turning the LED on, but then stops working, leaving the LED always on.

So my idea is to have a script that would check for the contents of the system LED file upon pressing a particular key (CapsLock in my case), and change it to the opposite value. The logic would be as follows:

listen to CapsLock and upon keypress
  do cat /sys/class/leds/input7::capslock/brightness
if cat returns 0
  then echo 1 > /sys/class/leds/input7::capslock/brightness
if cat returns 1
  then echo 0 > /sys/class/leds/input7::capslock/brightness
wait for next CapsLock keypress

I'm good at learning stuff from examples, but I couldn't find relevant info online. I guess I just don't know how to phrase this correctly to get relevant results. So any tips and pointers in the right direction greatly appreciated!

UPD:
I got as far as creating the shell script that switches the LED on and off depending on the current state of the LED. How do I make it execute every time CapsLock is pressed?

#!/bin/bash

led=`cat /sys/class/leds/input7::capslock/brightness`

if [[ $led = "1" ]]
then
    echo 0 > /sys/class/leds/input7::capslock/brightness
else
    echo 1 > /sys/class/leds/input7::capslock/brightness
fi

UPD2:
I obviously made the shell script executable, moved it to /usr/local/bin/, and added it to a file in /etc/sudoers.d/ to allow it to run without a password.

UPD3
I noticed that the ìnput number in the directory structure can change between reboots, so I modified the script to account for that.

#!/bin/bash

led=`cat /sys/class/leds/input?::capslock/brightness`
file=`ls /sys/class/leds/input?::capslock/brightness`

if [[ $led = "1" ]]
then
    echo 0 > $file
else
    echo 1 > $file
fi

Now I need to execute this every time Caps Lock is pressed.

UPD4:
Maybe I should be trying to catch the actual keyboard layout change event instead of CapsLock press? This way would be more universal and allow the LED indicator to show the alternative layout irrespective of the shortcut being used to switch layouts. Is there a way to monitor such events?

4
  • Are you using the wayland-X-compatability module? (Forget the actual name.) If no, maybe this is the reason that xkb isn't working properly?
    – Nate T
    Nov 15 at 1:49
  • If you are on Ubuntu, (going off the Wayland mention) you can do this in Settings. Look under the Keyboard Shortcuts tab, and scroll all the way down. Youll see a custom shortcuts section. Set the keys to Caps lock and the command to /usr/bin/bash -c /path/to/script. They are system-wide shortcuts, so it should be good anywhere. Let me know if this works for you. If yes, Ill switch to answer. If no, Ill delete comment.
    – Nate T
    Nov 15 at 2:02
  • @NateT Thank you for the tips. I'm on Arch with xorg-xwayland installed and using both Wayland native and X11 apps. Upstream has not removed my bug report, so I guess it's not related to that. I'll try out the custom shortcuts way later today and post a comment.
    – somepaulo
    Nov 15 at 12:27
  • @NateT Just tried the custom shortcuts option, but it doesn't let me set CapsLock as the shortcut key. Looking for another way of doing this.
    – somepaulo
    Nov 15 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.