Linux KDE RGB keyboard G213
A priori the RGB keyboard appears to be somewhat pointless. Nevertheless, I found myself having an uncontrollable urge to own one. In a review of various models the author pondered what reaction people would have if he could take a current generation RGB keyboard back in time to the late nineties. They could marvel at the progress of computer technology and how far human civilization had progressed 20 years hence.

The model I ended up buying was the Logitech G213 Prodigy. It was available at OfficeWorks around the corner for $79, which is relatively cheap by gaming standards. My application of course is serious software engineering. It's also pretty clear that I've been living in a parallel universe blissfully unaware of the existence of such equipment.
It is also clear from the amount of effort put in here to integrating an RGB KB into KDE that it has a serious deficiency (same for Windows and MacOS). RGB has been around for about 10 years now and is pretty much totally ignored. If human existence is to improve (even further) then RGB must be integrated seemlessly into the window context.
Linux Compatibility
The first issue to adding RGB to a linux keyboard environment is that of software support and compatibility.
Well the good news is that this keyboard will just plug in and run. If you do nothing else you can use the G213's built in ability to run effects directly by ifself. Searching the web and watching Youtube videos, however, will not reveal the needed, pretty much undocumented, key sequences:
- Light button + 1: Color wave left to right
- Light button + 2: Color wave right to left
- Light button + 3: Color wave centre out
- Light button + 4: Breathing effect
- Light button + 5: All color cycle together
- Light button + 0: Fixed color (keep pressing to cycle)
Software drivers
The very first thing you'll need to do is install g810-led by Mat Moul to allow "profiles" to loaded in to the keyboard. After attempting to use alternatives, this is by far and away the easiest and most reliable.
Security
The second issue to overcome is that only the root user has sufficient security access to talk to the G213. That's essentially because g213-led tampers with the USB directly. Overcoming that restriction was done by using the trusty linux sudo mechanism.
First, login as the root user and add a group named g213. Then add your userid to the new group.
groupadd g213
usermod -aG myuserid g213
Use the visudo command as the root user to edit the sudoers file and add this line.
## Allows people in group g213 to run /usr/bin/g810-led
%g213 ALL=(root) NOPASSWD:/usr/bin/g810-led
Essentially it says any user in group g213 can run /usr/bin/g810-led with an effective user id of root.
Profiles
Once users in the g213 group have access to be able to setup the key colors and effects, they can do cool stuff from the commandline.
To run the profiles below use:
sudo /usr/bin/g810-led -p /etc/g810-led/profile.g213.login
All profile files in /etc/g810-led have their permissions set to 755.
Login profile

The first profile needed is for login. This establishes a default keyboard color. For this I selected white for the main keyboard keys, green for the nav keys and blue for the numberics.
This profile is produced by:
/etc/g810-led/profile.g213.login.
# Login profile by groups keys
r 1 ffffff
r 2 ffffff
r 3 ffffff
r 4 00ff00
r 5 0000ff
c # Commit changes
Root login profile

The "root" profile is simply an all red static color. Essentially it means you can do bad things here by accident.
/etc/g810-led/profile.g213.root
# Sample profile by groups keys
a ff0000
c # Commit changes
Sleep profile

Sleep profile uses the g213's breath effect where the keyboard slowly pulses on and off with all keys set to one color. The color is set to green with a moderate breathing speed.
# Sleep profile by groups keys
fx breathing all 00ff00 18
c # Commit changes
CapsLock (new for V2)


CapsLock overwrites the color profile for the main part of the keyboard (with yellow) when the CapsLock key is active. Activation is detected by using xbindkeys (which is now an installation prerequisite).
# Capslock profile by groups keys
r 1 ffff00
r 2 ffff00
r 3 ffff00
c # Commit changes
NumLock (new for V2)


Numlock also overwrites the color profile for the numpad with a sightly light blue.
# Capslock profile by groups keys
r 5 00ffff
c # Commit changes
KDE integration
Once the G213 starts doing stuff the question becomes: how can this be integrated with KDE's desktop to become really cool?
Essentially the color and effects profiles need to be triggered by various system events. The method used here is to use bash shell scripts which are activated by various system mechanisms. The simplest of these is the .bash_profile and .bash_logout with the root user's account.
Root user login and logout color changes
Login as the root user and add this line to the end of /root/.bash_profile to change the keyboard to red.
/usr/bin/g810-led -p /etc/g810-led/profile.g213.root
Similarly add this to /root/.bash_logout to change the keyboard back to the default login colors when the root user logs out of a terminal session:
/usr/bin/g810-led -p /etc/g810-led/profile.g213.login
KDE graphical login and logout color changes
The next step is to get KDE's autostart function to run some bash shell scripts to light up the keyboard on graphical logins.


For login:
/etc/g810-led/g213-led-login.sh
#!/bin/bash
sudo /usr/bin/g810-led -p /etc/g810-led/profile.g213.login
and for logout:
/etc/g810-led/g213-led-logout.sh
#!/bin/bash
# Keyboard sleep (breathing effect).
#
sudo /usr/bin/g810-led -p /etc/g810-led/profile.g213.sleep
# Kill session's dbus monitor as we're logging out.
#
screen -ls | grep 'g213-dbus-monitor' | cut -f 1 -d'.' | tr -d "\t" | xargs -I '{}' kill -SIGTERM {}
ScreenLock mode breathing effect
KDE uses dbus to send messages to various system service providers. One of the subsystems providing services is the screenlock program. It's possible to listen to the dbus and intercept these messages and use them to trigger bash scripts.
A prerequisite for running this script is the ability to background it via "screen". So in Fedora, dnf install screen (or yum install screen for other Linux flavours).
/etc/g810-led/g213-led-dbus-monitor.sh
#!/bin/bash
screen -dmS g213-dbus-monitor '/etc/g810-led/g213-led-screenlock.sh'
/etc/g810-led/g213-led-screenlock.sh
#!/bin/bash
#
# https://unix.stackexchange.com/questions/353998/run-script-on-screen-lock-in-kde
#
dbus-monitor --session "type='signal',interface='org.freedesktop.ScreenSaver'" |
while read x; do
case "$x" in
# You can call your desired script in the following line instead of the echo:
*"boolean true"*) echo "Locked"; sudo /usr/bin/g810-led -p /etc/g810-led/profile.g213.sleep;;
*"boolean false"*) echo "Unlocked"; sudo /usr/bin/g810-led -p /etc/g810-led/profile.g213.login;;
esac
done