Advertisement
Guest User

Read raw mouse coordinates from Linux /dev/input/mouse*

a guest
Dec 7th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include <inttypes.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6.  
  7. int main(int argc, char *argv[]) {
  8.   char *input_dev = (argc > 1) ? argv[1] : "/dev/input/mouse0";
  9.   int fd;
  10.   uint8_t mouse_inp[3];
  11.   int x = 0, y = 0;
  12.  
  13.   if ((fd = open(input_dev, O_RDONLY)) == -1) {
  14.     perror("open");
  15.     return -1;
  16.   }
  17.   while (read(fd, mouse_inp, sizeof(mouse_inp))) {
  18.     if (mouse_inp[0] & 0x1) {
  19.       x = 0;
  20.       y = 0;
  21.     }
  22.     else {
  23.       x += (int8_t)mouse_inp[1];
  24.       y += (int8_t)mouse_inp[2];
  25.     }
  26.     printf("x = %+d\ty = %+d\n", x, y);
  27.     fflush(stdout);
  28.   }
  29.   return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement