2010年1月22日金曜日

Gamepad on linux

Today, I wanna talk about handling a gamepad on a linux environment.
We can get the device information using cat a device file.

cat /proc/bus/input/devices

I: Bus=0003 Vendor=0079 Product=0011 Version=0110
N: Name="USB Gamepad "
P: Phys=usb-0000:00:1d.0-1/input0
S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1:1.0/input/input4
U: Uniq=
H: Handlers=event4 js0
B: EV=1b
B: KEY=3ff 0 0 0 0 0 0 0 0 0
B: ABS=100 1f
B: MSC=10

And now, the device has a file for the event.
In this case event4 for js0.
(See /proc/bus/input/event?, /proc/bus/input/js0)

So we can read the action using a codes.

#include
#include

int main(int argc, char **argv);

int main(int argc, char **argv)
{
FILE *dev = fopen("/dev/input/event4", "r+");
if (NULL == dev) {
perror("fopen");
return 1;
}
for (int i = 0; ; i++) {
uint8_t c = fgetc(dev);
fprintf(stderr, "%c0x%02x", ((i % 16) == 0) ? '\n':' ', c);
}
return 0;
}

That's great.

0 件のコメント:

コメントを投稿