/*------------------- Joystick.c -----------------------*/
/* Version: 0.1						*/
/* Author: Tobias Weis, May 2008, Frankfurt	  	*/
/* Contact: tobias [dot] weis [at] gmx [dot] de		*/
/*							*/
/* Program to read your linux-driven joystick		*/
/* - Reads Axis, Buttons and Throttle-Control		*/
/*							*/
/* Devis is /dev/input/js0				*/
/* Joysticks i tested with this program:		*/
/* - Saitek ST30					*/
/*							*/
/* Copy, modify and play as much as you want!		*/
/*------------------------------------------------------*/


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <asm/types.h>
#include <X11/Xlib.h>
#include <linux/joystick.h>

#define DEBUG 0

int main(int argc, char **argv)
{
	struct js_event e;
	struct JS_DATA_TYPE js;
	uint val0, val1, val2, val3; 
	uint posx = 50;
	uint posy = 50;
        uint posz = 50;
        uint posk = 50;
	int fd;
	int	click_flag = 0;

	if (!(fd = open("/dev/input/js0", O_RDONLY))) {
		fprintf(stderr, "cannot open /dev/input/js0\n");
	}        

	while(1){
		read (fd, &e, sizeof(struct js_event));
		
#if DEBUG
		printf("time %d num %d value %d type %d\n",  
				e.time, e.number, e.value, e.type);
#endif


		if(e.type == JS_EVENT_AXIS){
#if DEBUG
		printf("X: %u - Y: %u - Z: %u - K: %u\n", posx, posy, posz,posk);
#endif
		
		printf("-------------\n");
			if (e.number == 0){
				val0 = e.value;
			}
			if (e.number == 1){
				val1 = e.value;
			}
			if (e.number == 2){
				val2 = e.value;
			}
			if(e.number == 3){
				val3 = e.value;
			}
		
			// calculate output in range 0-100
			posx = (float)((32768 + val0) / 65536.0) * 100;        
			posy = (float)((32768 + val1) / 65536.0) * 100;        
			posz = (float)((32768 + val2) / 65536.0) * 100;
			posk = (float)((32768 + val3) / 65536.0) * 100;
			
			// x-axis of joystick
			if(posx > 50 && posx < 70){
				printf("Langsam rechts\n");
			}
			if(posx > 70 && posx < 100){
				printf("Schnell rechts\n");
			}
			if(posx < 50 && posx > 30){
				printf("Langsam links\n");
			}
			if(posx < 30 && posx >= 0){
				printf("Schnell links\n");
			}

			// y-axis of joystick
			if(posy > 50 && posy < 70){
				printf("Langsam rückwärts\n");
			}
			if(posy > 70 && posy < 100){
				printf("Schnell rückwärts\n");
			}
			if(posy < 50 && posy > 30){
				printf("Langsam vorwärts\n");
			}
			if(posy < 30 && posy >= 0){
				printf("Schnell vorwärts\n");
			}
			printf("------------\n");

			// posk is the little "thumb-stick"
			if(posk == 0){
				printf("Kamera links\n");
			}
			if(posk == 99){
				printf("Kamera rechts\n");
			}
			printf("==============\n");

		}
	
		// if a button is pressed
		else if (e.type == JS_EVENT_BUTTON) {
			// read JS_RETURN to determine
			// which button was pressed
			read(fd, &js, JS_RETURN);
			if(js.buttons == 1){
				printf ("trigger-button pressed\n");
			}
			if(js.buttons == 2){
				printf ("middle-button pressed\n");
			}
			if(js.buttons == 4){
				printf ("left button pressed\n");
			}
			if(js.buttons == 8){
				printf ("right button pressed\n");
			}

#if DEBUG
			printf("js-button-state: %u\n",js.buttons);
#endif

			/*
			if(!click_flag)	{
				event1.type = ButtonPress;
				printf("Button clicked\n");
#if DEBUG
				fprintf(stdout,"clicked\n");
#endif
				click_flag = 1;
			}
			else {
				event1.type = ButtonRelease;
#if DEBUG
				fprintf(stdout,"released\n");
#endif
				click_flag = 0;
			}
			*/
		}
		
		else {	// Init flag
			click_flag = 0;
		}
	}
}


