]> git.sesse.net Git - keycount/blob - keycount.c
Initial checkin for move to Git (no prior version history available).
[keycount] / keycount.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <linux/input.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <time.h>
7 #include <sys/time.h>
8
9 int main(void)
10 {
11         FILE *log = fopen("/var/log/keycount.log", "a");
12         struct input_event iev;
13         time_t start = time(NULL);
14         unsigned count = 0;
15         int fd = open("/dev/input/event0", O_RDONLY);
16         if (fd == -1) {
17                 perror("/dev/input/event0");
18                 exit(1);
19         }
20
21         for ( ;; ) {
22                 int ret = read(fd, &iev, sizeof(iev)); 
23                 time_t now;
24                 if (ret != sizeof(iev)) {
25                         perror("read");
26                         exit(1);
27                 }
28
29                 if (iev.type != EV_KEY)
30                         continue;
31                 if (iev.value != 1)
32                         continue;
33
34                 // see if we want to write out
35                 now = time(NULL);
36                 if (now/60 != start/60) {
37                         fprintf(log, "%u %u\n", (start/60)*60, count);
38                         fflush(log);
39                         count = 0;
40                         start = now;
41                 }
42                 ++count;
43         }
44 }