3 #include <linux/input.h>
12 typedef struct event_s
{
27 int main(int argc
, char *argv
[]) {
30 event_t events
[EVENTS
];
33 while((optch
= getopt(argc
, argv
, "hn")) != -1) {
36 printf("Usage: %s [-n|-h]\n", argv
[0]);
37 printf("-n nonverbose\n");
48 printf("%s must be used as superuser\n", argv
[0]);
52 signal(SIGINT
, sigint
);
54 printf("Press CTRL-C to exit.\n\n");
56 memset(events
, 0, sizeof(events
));
59 for(i
= 0; i
< EVENTS
; i
++) {
62 sprintf(device
, "/dev/input/event%i", i
);
63 events
[i
].fd
= open(device
, O_RDONLY
);
65 if(events
[i
].fd
!= -1) {
66 ioctl(events
[i
].fd
, EVIOCGNAME(sizeof(events
[i
].name
)), events
[i
].name
);
67 if(verbose
) printf("event%i: %s\n", i
, events
[i
].name
);
76 for(i
= 0; i
< EVENTS
; i
++) {
77 if(events
[i
].fd
!= -1) {
78 FD_SET(events
[i
].fd
, &set
);
82 if(select(FD_SETSIZE
, &set
, NULL
, NULL
, NULL
) > 0) {
84 struct input_event event
;
86 for(i
= 0; i
< EVENTS
; i
++) {
87 if(events
[i
].fd
== -1 || !FD_ISSET(events
[i
].fd
, &set
)) {
91 bytes
= read(events
[i
].fd
, &event
, sizeof(event
));
93 if(bytes
!= sizeof(event
)) {
97 if(event
.type
== EV_REL
) {
101 time
= event
.time
.tv_sec
* 1000 + event
.time
.tv_usec
/ 1000;
102 hz
= 1000 / (time
- events
[i
].prvtime
);
108 events
[i
].hz
[events
[i
].count
& (HZ_LIST
- 1)] = hz
;
112 for(j
= 0; j
< HZ_LIST
; j
++) {
113 events
[i
].avghz
+= events
[i
].hz
[j
];
116 events
[i
].avghz
/= (events
[i
].count
> HZ_LIST
) ? HZ_LIST
: events
[i
].count
;
118 if(verbose
) printf("%s: Latest % 5iHz, Average % 5iHz\n", events
[i
].name
, hz
, events
[i
].avghz
);
121 events
[i
].prvtime
= time
;
127 for(i
= 0; i
< EVENTS
; i
++) {
128 if(events
[i
].fd
!= -1) {
129 if (events
[i
].avghz
!= 0) {
130 printf("\nAverage for %s: % 5iHz\n", events
[i
].name
, events
[i
].avghz
);