xorg: Fixing xmodmap persistence issues on USB replug

The Problem

There is a long-standing bug in x.org that loads the default xmodmap configuration when an USB keyboard (or mouse?) is unplugged and replugged. This wipes out any changes to the keymap configuration done via xmodmap. While ordinary user will rarely be affected, users of KVM switches will see their keymap changes wipes on every device switch.

Will there be a patch?

As far as I could determine, this is a lomg0term known issue and the x.org maintainers do not plan to fix it due to ressource shortage.

The Fix

I habe unsuccessfully tried a lot of things. I finally have found a solution that actually works, but is so inelegant that I had some real trouble bringing myself to use it.

The fix I found is to reload the xmodmap changes every second.

I can only call this a dirty hack and would like to stress that something like this is never a good idea, will probably cause secondary problems and should generally be avoided. Having said that, it seems to work pretty well.

Details

My local xmodmap changes are in the file $HOME/.xmodmap. I have written a small demon that gets started with the window manager (other ways should work too) and calls

   system("xmodmap - < /home/wagner/.xmodmap")

once a second in a loop. This does not seem to create any noticeable system load and so far I have not noticed any problems with keyboard input or mouse movements. If your set-up can switch faster than 1 second (my KVM + monitor combination cannot), you may want to run this more frequently, e.g. every 500ms.

Obviously other calls to xmodmap can be used as well.

Demon Implementations

There are different ways to do demons. Here are some. Obviously you can use whatever you prefer.
Last update: 2023 Arno Wagner