#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    fd_set rfds;
    struct timeval tv;
    int retval;
    int fd, bytes;
    unsigned char data[3];
    int left, middle, right;
    signed char x, y; int rv;

    int fd_file = open("/dev/input/mice", O_RDONLY);

    FD_ZERO(&rfds);
    FD_SET(fd_file, &rfds);

    tv.tv_sec = 0;
    tv.tv_usec = 0;

   while (1)
   {
       FD_ZERO(&rfds);
    FD_SET(fd_file, &rfds);
    
     retval = select(fd_file+1, &rfds, NULL, NULL, &tv);
    printf(" ");
     if (retval == -1){perror("select()");}
     else if (retval){
        bytes = read(fd_file, data, sizeof(data));
        if(bytes > 0)
        {
            left = data[0] & 0x1;
            right = data[0] & 0x2;
            middle = data[0] & 0x4;
            x = data[1];
            y = data[2];
            printf("x=%d, y=%d, left=%d, middle=%d, right=%d\n", x, y, left, middle, right);
        }
        /*FD_ISSET(0, &rfds)*/
     }
   }

   exit(EXIT_SUCCESS);
}
