Automatic xmodmap on keyboard connection
In continuation to my earlier post, this shows how I automated xmodmap with udev.
Description 🔗
Xorg keymap modifications done with xmodmap are keyboard specific. In addition, modifications are reset when keyboard is reconnected. I use USB switch to share most of my USB devices with multiple computers. I.e., devices are often connected and disconnected.
Single device connection may raise multiple udev add
events. For example, attachment of ThinkPad USB keyboard raised in total 9 events. Udev rules and script executions are per event, and I wanted to limit xmodmap execution only once per attachment. Albeit, my xmodmap configuration is idempotent.
I had two options:
- Find so strict udev rule which appears only once per attachment
- Implement filtering of repeated events in script
I chose the latter option because it was simpler, more reliable, and script can easily reused with other similar scenarios.
Lastly, udev waits until script exits, before it continues. My script invokes xmodmap asynchronously to not block udev. Moreover, xmodmap invocation is delayed, to be sure keyboard is ready for the keymap modification.
Udev rule 🔗
Rule for Lenovo ThinkPad Compact Keyboard with TrackPoint.
File /etc/udev/rules.d/90-keyboard.rules
:
|
|
Udev script 🔗
This script is called for all udev add
events. When the first events comes, it schedules xmodmap after 1 second. It ignores are following script calls during the next 5 seconds.
It creates and uses two external files:
/tmp/last-udev-xmodmap
for timestamp of the last xmodmap execution/tmp/last-udev-xmodmap.log
for all output of xmodmap execution
File /bin/udev-xmodmap.sh
:
|
|
Note, the example is actually a jinja2 template with normal_user
variable.