Inspired by Martin Fowler articles on domain specific languages, I’ve come up with this kind of thing to use a USB gamepad device to control things on my desktop.

device "/dev/input/by-id/usb-050d_0805-event-joystick"
key_map 304, :ctrl, :alt, :left
key_map 306, :ctrl, :alt, :right
rule 'ABS', 'THROTTLE' do |value|
	`/home/edward/bin/set-volume #{adjust_range(value, 255, 0, 0, 100, true)}`

The device statement specifies the device to listen to. (In my case it’s a Nostromo Speedpad N50). The implementation underneath uses evdev for ruby by buffalo.

key_map maps from a key on the device to a set of keyboard keys to emulate – I have an underlying hash to convert simple symbols into X keycodes. This relies on some XTest hackery also by buffalo. In this case, I map two of the keys on my device to key combinations which switch workspace on Ubuntu.

rule is the real workhorse of the system, which lets you specify an arbitrary event type and attach a block of code to it. This is where it’s handy to use Ruby for the DSL, since the block can contain any Ruby code. In this case, I map the ‘throttle wheel’ control on the device so that it sets the sound volume on my desktop. adjust_range is a utility function, since the wheel produces values from 0 to 255, while the volume is set with a value from 0 to 100, and also inverted in direction.

I’ve only just got this working, so the underlying code is fairly messy. It has more potential than this simple example shows – it would be nice to add more utility functions and more examples. I’m also thinking about how to make it deal with more than one device at a time, and to use outputs on the devices (my Speedpad has LEDs which could indicate something).

3 Trackbacks

  1. By BuffaloBlog on January 10, 2008 at 4:22 pm

    Somebody actually uses ruby evdev!…

    It’s just so cool when you look out across this vast strange internet ocean and find somebody else using what you have built. Here’s the link –
    Maybe he’ll email me if he has any q…

  2. By The Junction » single-key alt-tab on February 1, 2008 at 12:00 pm

    […] device control program has been […]

  3. By The Junction » JNI hacking on February 20, 2008 at 2:49 pm

    […] event interface is fairly fresh in my mind, due to working with buffalo’s ruby library for it recently so I decided to re-implement a wrapper for some of that. Only some, because after implementing two […]

Post a Comment

Your email is never published nor shared. Required fields are marked *