SUMMARY: A brief guide on using evremap to create a media remote control device.
The Problem
I have a home theater PC (an Intel NUC) and did not really have a nice way to control media without using the mouse and keyboard. I did however have an old 8BitDo Zero 2 -- this device was designed to be game controller, not a media control device and it does not support reprogramming or remapping its keys.
In its "PC" mode it acts as another keyboard and sends (seemingly) random keycodes to the computer that you are (I guess) intended to configure a game emulator to recognize. This wouldn't work for media control as you typically want this to be system wide and ideally use standard multimedia keys.
The Solution
So the solution here was to use a piece of software that was capable of remapping keys (in Wayland) and that was specific to a particular device. The Arch Linux Wiki lists several tools -- I selected evremap because it was simple, device specific, and had an easy way to run it automatically with an example systemd service.
The Config
First you need to discover the name of the device, evremap provides evremap list-devices or you can use libinput (via sudo apt install libinput-tools). This allows you to name the device in the toml config.
The config to remap the Zero 2 keys is pretty simple:
device_name = "8BitDo Zero 2 gamepad Keyboard"
[[remap]]
input = ["KEY_G"]
output = ["KEY_PLAYPAUSE"]
[[remap]]
input = ["KEY_F"]
output = ["KEY_FASTFORWARD"]
[[remap]]
input = ["KEY_E"]
output = ["KEY_REWIND"]
[[remap]]
input = ["KEY_C"]
output = ["KEY_VOLUMEUP"]
[[remap]]
input = ["KEY_D"]
output = ["KEY_VOLUMEDOWN"]
[[remap]]
input = ["KEY_K"]
output = ["KEY_PAGEDOWN"]
...
The Complication
This works fine as long as the device is always connected, if the device is not, evremap will exit and the systemd service will eventually be disabled (because it will exit immediately when the device is not found).
The obvious solution is to just change the systemd service to wait until the device is connected:
[Service]
WorkingDirectory=/
ExecStart=bash -c "while ! find /sys/devices/virtual/misc/uhid -name '0005:2DC8:9018*' | wc -l | grep -v 0 >/dev/null; do sleep 2; done && /usr/local/bin/evremap remap /etc/evremap.toml -d 0"
Restart=always
RestartSec=1
RestartSteps=4
RestartMaxDelaySec=8s
[Install]
WantedBy=multi-user.target
There is probably a better way to discover the /sys/devices/virtual/misc/uhid path, but I just did this by trial and error (disconnecting the device and seeing what disappeared from this /sys directory). The name of the device seems to also be stored in /sys/devices/virtual/misc/uhid/0005:2DC8:9018*/input/input*/name.
Maintenance
Input key names seem to track Linux input codes:
-
https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
-
Or: https://www.kernel.org/doc/html/latest/userspace-api/media/rc/rc-tables.html
The evremap tool also provides evremap list-keys.
Future Considerations
Improvements in the future could include a modification to evremap to allow it to wait for a device to attach instead of just exiting.