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!
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
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.
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.
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?