/*
sample by Eric Weiss eric@eric-weiss.de
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
int
main(void) {
    fd_set rfds;
    struct timeval tv;
    int retval;
    struct input_event evt;
    struct input_event evt1;
    int file,file1;
    int a;
   int rep[2];             /* Repeat rate array (milliseconds) */
   rep[0] = 1000;  /* Delay before first repeat */
   rep[1] = 250;   /* Repeating delay */
    int x,y;
 
   /* Watch stdin (fd 0) to see when it has input. */
    file=open("/dev/input/event0",O_NONBLOCK +O_ASYNC+O_RDONLY);
    file1=open("/dev/input/touchscreen0",O_NONBLOCK +O_ASYNC+O_RDONLY);
    printf(  "fds %i , %i \n",file,file1);

   /* Wait up to five seconds. */
    while(1)
      {
        tv.tv_sec = 1;
        tv.tv_usec = 0;
	FD_ZERO(&rfds);
	FD_SET(file, &rfds);
	FD_SET(file1, &rfds);
   // if(ioctl(file1, EVIOCSREP, rep)) 
	//{
        //perror("evdev ioctl");
	//}
        retval = select(5, &rfds, NULL, NULL, &tv);
 //       printf("retval %i\n",retval);
        if( FD_ISSET(file, &rfds))
          {
            read(file,&evt, sizeof(struct input_event));
            printf("event: code  %i val  %i type %i ret %i\n",evt.code,evt.value,evt.type,retval);
	}
        if( FD_ISSET(file1, &rfds))
          {
            read(file1,&evt1, sizeof(struct input_event));
            if (( evt1.code==0 ) & (evt1.type==3 ))
		{ 
          x=(evt1.value-200)/300;
		}
	 if (( evt1.code==1 ) & (evt1.type==3 ))
		{ 
          y=(evt1.value-200)/300;
		}
//		printf("event1:  code %i value %i val %i ret%i\n", evt1.code,evt1.value,evt1.type,retval);
		printf ("X %i Y %i \n",x,y);
          }
        if ( evt.type == EV_KEY)
          {
   //         printf ("%i %i %i\n",evt.type,evt.value,evt.code);
          }
      }
    
    /* Don't rely on the value of tv now! */

   if (retval == -1)
        perror("select()");
    else if (retval)
        printf("Data is available now.\n");
        /* FD_ISSET(0, &rfds) will be true. */
    else
        printf("No data within five seconds.\n");
   return 0;
}

