/*
 * Fvwm Virtual Desktop Changer
 * 2003/11/4
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/joystick.h>

static int joy_fd = 0;
static char buf[256];

void movePage(int num)
{
	printf("%d\n", num);
	snprintf(buf, 256, "FvwmCommand 'GotoPage %d 0'", num);
	system(buf);
}

void main_loop()
{
	struct js_event event;
	int ret;

	for(;;) {
		ret = read(joy_fd, &event, sizeof(struct js_event));
		if(ret != sizeof(event)) {
			continue;
		}
		switch(event.type) {
			case JS_EVENT_BUTTON:
				if(event.value == 1) {
					movePage(event.number);
				}
				break;
			default:
				break;
		}
	}
}

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

	if(argc < 2) {
		str = "/dev/input/js0";
	} else {
		str = argv[1];
	}

	joy_fd = open(str, O_RDONLY);
	if(joy_fd < 0) {
		perror("Failed to open device: ");
		exit(1);
	}
	main_loop();

	return 0;
}

