Add readjoy to read joystick events from userspace

Also fix a bit of formatting in the joystick driver file.
pull/3/head
Nirenjan Krishnan 2012-09-30 19:17:46 -07:00
parent 9ac96dae97
commit a32cc47097
3 changed files with 53 additions and 0 deletions

View File

@ -221,6 +221,7 @@ static int to_hex(const char c)
} }
return -1; return -1;
} }
static ssize_t set_brightness(struct device *dev, const char *buf, static ssize_t set_brightness(struct device *dev, const char *buf,
size_t count, u8 target) size_t count, u8 target)
{ {

10
user/Makefile 100644
View File

@ -0,0 +1,10 @@
readjoy: readjoy.c
gcc -o $@ $<
all: readjoy
.PHONY: clean
clean:
rm -f readjoy

42
user/readjoy.c 100644
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/joystick.h>
int main(int argc, char *argv[])
{
struct js_event joy_event;
int fd;
if (argc != 2) {
fprintf(stderr, "Usage: %s /dev/input/jsX\n", argv[0]);
return -1;
}
fd = open(argv[1], O_RDONLY);
if (!fd) {
fprintf(stderr, "Error: Unable to open %s!\n", argv[1]);
return -2;
}
for(;;) {
if (read(fd, &joy_event, sizeof(joy_event)) != sizeof(joy_event)) {
fprintf(stderr, "Error: Unable to read from joystick!\n");
return -3;
} else {
if (joy_event.type & JS_EVENT_INIT) {
printf("*");
}
if (joy_event.type & JS_EVENT_BUTTON) {
printf("B");
} else if (joy_event.type & JS_EVENT_AXIS) {
printf("A");
} else {
printf("?");
}
printf("%u: %d\n", joy_event.number, joy_event.value);
}
}
return 0;
}