#include <linux/input.h>
#include <fcntl.h>
#include <signal.h>
+#include <getopt.h>
+#include <unistd.h>
#define EVENTS 50
#define HZ_LIST 64
int fd;
int hz[HZ_LIST];
int count;
+ int avghz;
double prvtime;
+ char name[128];
} event_t;
int quit = 0;
void sigint() {
- signal(SIGINT, sigint);
quit = 1;
}
-int main(int argc, char **argv) {
+int main(int argc, char *argv[]) {
+ int optch;
int i;
event_t events[EVENTS];
+ int verbose = 0;
+
+ while((optch = getopt(argc, argv, "hv")) != -1) {
+ switch(optch) {
+ case('h'):
+ printf("Usage: %s [-v|-h]\n", argv[0]);
+ return 0;
+ break;
+ case('v'):
+ verbose = 1;
+ break;
+ }
+ }
signal(SIGINT, sigint);
memset(events, 0, sizeof(events));
+ // List input devices
for(i = 0; i < EVENTS; i++) {
char device[18];
events[i].fd = open(device, O_RDONLY);
if(events[i].fd != -1) {
- char name[128];
- ioctl(events[i].fd, EVIOCGNAME(sizeof(name)), name);
- printf("event%i: %s\n", i, name);
+ ioctl(events[i].fd, EVIOCGNAME(sizeof(events[i].name)), events[i].name);
+ if(verbose) printf("event%i: %s\n", i, events[i].name);
}
}
struct input_event event;
for(i = 0; i < EVENTS; i++) {
- if(events[i].fd == -1) {
- continue;
- }
-
- if(!FD_ISSET(events[i].fd, &set)) {
+ if(events[i].fd == -1 || !FD_ISSET(events[i].fd, &set)) {
continue;
}
hz = 1000 / (time - events[i].prvtime);
if(hz > 0) {
- int avghz;
int j;
events[i].count++;
events[i].hz[events[i].count & (HZ_LIST - 1)] = hz;
- avghz = 0;
+ events[i].avghz = 0;
for(j = 0; j < HZ_LIST; j++) {
- avghz += events[i].hz[j];
+ events[i].avghz += events[i].hz[j];
}
- avghz /= (events[i].count > HZ_LIST) ? HZ_LIST : events[i].count;
+ events[i].avghz /= (events[i].count > HZ_LIST) ? HZ_LIST : events[i].count;
- printf("event%i: latest hz = %i (average hz = %i)\n", i, hz, avghz);
+ if(verbose) printf("%s: Latest % 5iHz, Average % 5iHz\n", events[i].name, hz, events[i].avghz);
}
events[i].prvtime = time;
for(i = 0; i < EVENTS; i++) {
if(events[i].fd != -1) {
+ if (events[i].avghz != 0) {
+ printf("\nAverage for %s: % 5iHz\n", events[i].name, events[i].avghz);
+ }
close(events[i].fd);
}
}
+
+ return 0;
}