Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
Check out the new SourceForge HTML5 internet speed test! No Flash necessary and runs on all devices. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. ×
User Journal

Journal Journal: Joystick source

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);
}
}
}

Slashdot Top Deals

Nothing in progression can rest on its original plan. We may as well think of rocking a grown man in the cradle of an infant. -- Edmund Burke

Working...