summaryrefslogtreecommitdiff
path: root/wheel_read.c
blob: ae6b4d9312f7663066aeffc226beb83fb0d50ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <linux/input.h>

void usage(const char *progname) {
    fprintf(stderr, "Usage: %s /dev/input/eventX\n", progname);
    exit(EXIT_FAILURE);
}

int main(int argc, char *argv[]) {
    int fd;
    struct input_event ev;
    ssize_t n;

    if (argc != 2) {
        usage(argv[0]);
    }

    // Open the evdev device provided in the command line argument
    fd = open(argv[1], O_RDONLY);
    if (fd < 0) {
        perror("Error opening device");
        exit(EXIT_FAILURE);
    }

    printf("Listening for events on %s. Press Ctrl+C to exit.\n", argv[1]);

    // Event reading loop
    while (1) {
        n = read(fd, &ev, sizeof(ev));
        if (n == (ssize_t)-1) {
            if (errno == EINTR) {printf("EINTR\n");
                continue;}
            perror("Error reading event");
            break;
        } else if (n != sizeof(ev)) {
            fprintf(stderr, "Short read: %zd\n", n);
            break;
        }

        // Print the event information
        printf("Event: time %ld.%06ld, type %d, code %d, value %d\n",
               ev.time.tv_sec, ev.time.tv_usec, ev.type, ev.code, ev.value);
    }
 
    close(fd);
    return 0;
}