October 28, 2016

problems with read(...); and mice-device (/dev/input/mice)

Hello,

i have this code:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
 
 
char mice_data[3];
 
int mice_position(){
 
    int fd = open("/dev/input/mice", O_RDWR);
    if(fd == -1){
            printf("ERROR Opening %s\n", "/dev/input/mice");
        return -1;
    }
         
    printf("\n");
    read(fd, mice_data, sizeof(mice_data));
     
    close(fd);
 
    return 0; 
}
 
int mouse_x = 0;
int mouse_y = 0;
 
int update_mouse(){
    mice_position();
    mouse_x += mice_data[1];
    mouse_y += mice_data[2];
    return 0;
}
 
int main(){
    while(1){
        update_mouse();
        printf("%i\t%i", mouse_x, mouse_y);
    }
    return 0;
}

and if im running it it works, but only if the printf(\n"), statement in line 16 is there. if it isnt the program doesnt show anything.

another problem i have is that the read-functions waits until the mice-file changes/until the mouse moves. how can i solve both problems?

Click Here!