improve license notice
[evhz] / evhz.c
diff --git a/evhz.c b/evhz.c
index e515ce5415eeeda6bb46690d9185b29453d0db93..950f727fcc6a7cd379fc76e153420878bfed6208 100644 (file)
--- a/evhz.c
+++ b/evhz.c
@@ -1,3 +1,9 @@
+/* The license of this program  follows the GNU license recommendations at */
+/* https://www.gnu.org/licenses/license-recommendations.en.html. They */
+/* recommend that small programs, < 300 lines, be licensed under the */
+/* Apache License 2.0. This file contains or is part of one or more small */
+/* programs. */
+
 /* Copyright (C) 2016 Ian Kelling */
 
 /* Licensed under the Apache License, Version 2.0 (the "License"); */
@@ -19,7 +25,7 @@
 #include <getopt.h>
 #include <unistd.h>
 
-#define EVENTS 200
+#define EVENTS 400
 #define HZ_LIST 64
 
 typedef struct event_s {
@@ -27,7 +33,7 @@ typedef struct event_s {
     int hz[HZ_LIST];
     int count;
     int avghz;
-    double prev_time;
+    unsigned long long prev_time;
     char name[128];
 } event_t;
 
@@ -115,25 +121,32 @@ int main(int argc, char *argv[]) {
             }
 
             if(event.type == EV_REL || event.type == EV_ABS) {
-                double time;
-                int hz;
+                unsigned long long time, timediff;
+                unsigned hz = 0;
+
+                time = (unsigned long long)event.time.tv_sec * 1000000ULL;
+                time += (unsigned long long)event.time.tv_usec;
 
-                time = event.time.tv_sec * 1000 + event.time.tv_usec / 1000;
-                hz = 1000 / (time - events[i].prev_time);
+                timediff = time - events[i].prev_time;
+
+                if(timediff != 0)
+                    hz = 1000000ULL / timediff;
 
                 if(hz > 0) {
-                    int j;
+                    unsigned j, maxavg;
 
                     events[i].count++;
                     events[i].hz[events[i].count & (HZ_LIST - 1)] = hz;
 
                     events[i].avghz = 0;
 
-                    for(j = 0; j < HZ_LIST; j++) {
+                    maxavg = (events[i].count > HZ_LIST) ? HZ_LIST : events[i].count;
+
+                    for(j = 0; j < maxavg; j++) {
                         events[i].avghz += events[i].hz[j];
                     }
 
-                    events[i].avghz /= (events[i].count > HZ_LIST) ? HZ_LIST : events[i].count;
+                    events[i].avghz /= maxavg;
 
                     if(verbose) printf("%s: Latest % 5iHz, Average % 5iHz\n", events[i].name, hz, events[i].avghz);
                 }