Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
User Journal

Journal: Joystick source

Journal by figa
To compile it you need to link libXtst and libX11 from /usr/X11R6/lib.

Note that it opens and closes the X display for every event. That's the only way I could get it to work. I'm not sure why my events are swallowed otherwise.

#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>

#include <linux/joystick.h>

int main (int argc, char *argv[]) {
int fd;
unsigned char axes = 2;
unsigned char buttons = 2;
int version = 0x000800;
char name[128] = "Unknown";
struct js_event js;
fd_set set;
Display *dpy;

if ((fd = open("/dev/input/js0", O_RDONLY)) < 0) {
perror("Couldn't find joystick.");
exit(1);
}

ioctl(fd, JSIOCGVERSION, &version);
ioctl(fd, JSIOCGAXES, &axes);
ioctl(fd, JSIOCGBUTTONS, &buttons);
ioctl(fd, JSIOCGNAME(NAME_LENGTH), name);

printf("Joystick (%s) has %d axes and %d buttons.\nDriver version is %d.%d.%d.\n",
name, axes, buttons, version >> 16, (version >> 8) & 0xff, version & 0xff);

while (1) {

FD_ZERO(&set);
FD_SET(fd, &set);

if (select(fd+1, &set, NULL, NULL, NULL)) {

if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("Joystick: read error.");
exit (1);
}

if (js.type == 1) {
dpy = XOpenDisplay(NULL);

if (js.value == 1) {
XTestFakeButtonEvent(dpy, 1, True, CurrentTime);
}
else {
XTestFakeButtonEvent(dpy, 1, False, CurrentTime);
}

XCloseDisplay(dpy);
}
}
}

Never try to teach a pig to sing. It wastes your time and annoys the pig. -- Lazarus Long, "Time Enough for Love"

Working...