Pages

Sunday, June 6, 2010

Alternative input devices and udev

If you have or own:
  1. an alternative character input device, e.g. typematrix keyboard (why);
  2. a keyboard with its own unique layout e.g. dvorak;
  3. a family where most of the members use the same computer;
  4. a gnu/linux computer with udev support, where you have root permission;
  5. a desire to automate changing the keyboard layout when plugging your alternative device in;
Udev is this nice system in linux where you can use certain attributes of devices to write rules so you could run certain scripts, mount devices etc. i.e. give behaviour to certain devices, when they are introduced to your operating system (Linux).

There might be predefined rules already in your /etc/udev/rules.d, check it out.

To gather info about your device, hit lsusb for usb devices and hit lspci for more info about any other non-usb devices.

You need the following command to figure out the attributes required to create the conditions for a certain rule:

$ udevadm info -a -p $(udevadm info -q path -n /dev/input/eventX) --walk-attributes

# NOTE: /dev/input is where your mouse or keyboard is usually put.

You can create general rules or device-specific rules for certain devices; For instance you can figure out what the ATTRS{idVendor} of a certain device and it would match with every device made by this vendor (in theory).
 You could also match with ATTRS{serial} and only that device containing that serial number would make the rule fire (in theory).

In udev rules file, only a rule per line is allowed. You can test the validity of a rule by writing a dummy function, i.e. echo to a file in /tmp/udevtest.txt. While plugging and unplugging the device (in my case a keyboard), check if the 'add' and 'remove' ACTION(s) are actually firing, by checking 'udevadm trigger'.

Next step is to write decide what to do with the rule. Recall I mentioned dvorak (pronounced as: dvor-zh-ak, it's czech), if you want to load a certain keyboard layout try writing a udev rule like this in /etc/udev/rules.d/111-typematrix-kb.rules:

## via Xorg
ACTION=="add", KERNEL=="event?", SUBSYSTEMS=="usb",ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="8081",RUN+="/usr/bin/setxkbmap -model 102 -layout dvorak"

ACTION=="remove", KERNEL=="event?", SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="8081", RUN+="/usr/bin/setxkbmap -model 102 -layout us_intl"


If your rule fires repeatedly for no apparent reason, try writing less general rules and change the wildcard * to ? so it matches with only a single character. Hit man udev when you're clueless about how your rule should look like. Also use ATTR instead of ATTRS where appropriate, hit the --walk-attributes in udevadm I just mentioned.

Next post: How to setup udev and OCZ NIA to control your computer with your mind. (TODO)

No comments:

Post a Comment

Please help to keep this blog clean. Don't litter with spam.