Advertisement
ottojo

Untitled

Jul 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5.  
  6. struct js_event {
  7.     __uint32_t time;     /* event timestamp in milliseconds */
  8.     __uint16_t value;    /* value */
  9.     __uint8_t type;      /* event type */
  10.     __uint8_t number;    /* axis/button number */
  11. };
  12.  
  13. int main() {
  14.     int fd = open("/dev/input/js0", O_RDONLY);
  15.     if (fd == -1) {
  16.         perror("Open Joystick");
  17.         exit(1);
  18.     }
  19.  
  20.     struct js_event e;
  21.     while (1) {
  22.         int n_read = read(fd, &e, sizeof(e));
  23.  
  24.         if (n_read == 0) {
  25.             exit(0);
  26.         } else if (n_read == -1) {
  27.             perror("Read");
  28.             exit(1);
  29.         }
  30.  
  31.         printf("Axis %d: %d\n", e.number, e.value);
  32.  
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement