1 /* Copyright (C) 2016 Ian Kelling */
3 /* Licensed under the Apache License, Version 2.0 (the "License"); */
4 /* you may not use this file except in compliance with the License. */
5 /* You may obtain a copy of the License at */
7 /* http://www.apache.org/licenses/LICENSE-2.0 */
9 /* Unless required by applicable law or agreed to in writing, software */
10 /* distributed under the License is distributed on an "AS IS" BASIS, */
11 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
12 /* See the License for the specific language governing permissions and */
13 /* limitations under the License. */
16 #include <linux/input.h>
25 typedef struct event_s
{
40 int main(int argc
, char *argv
[]) {
43 event_t events
[EVENTS
];
46 while((optch
= getopt(argc
, argv
, "hn")) != -1) {
49 printf("Usage: %s [-n|-h]\n", argv
[0]);
50 printf("-n nonverbose\n");
61 printf("%s must be used as superuser\n", argv
[0]);
65 signal(SIGINT
, sigint
);
67 printf("Press CTRL-C to exit.\n\n");
69 memset(events
, 0, sizeof(events
));
72 for(i
= 0; i
< EVENTS
; i
++) {
75 sprintf(device
, "/dev/input/event%i", i
);
76 events
[i
].fd
= open(device
, O_RDONLY
);
78 if(events
[i
].fd
!= -1) {
79 ioctl(events
[i
].fd
, EVIOCGNAME(sizeof(events
[i
].name
)), events
[i
].name
);
80 if(verbose
) printf("event%i: %s\n", i
, events
[i
].name
);
89 for(i
= 0; i
< EVENTS
; i
++) {
90 if(events
[i
].fd
!= -1) {
91 FD_SET(events
[i
].fd
, &set
);
95 if(select(FD_SETSIZE
, &set
, NULL
, NULL
, NULL
) > 0) {
97 struct input_event event
;
99 for(i
= 0; i
< EVENTS
; i
++) {
100 if(events
[i
].fd
== -1 || !FD_ISSET(events
[i
].fd
, &set
)) {
104 bytes
= read(events
[i
].fd
, &event
, sizeof(event
));
106 if(bytes
!= sizeof(event
)) {
110 if(event
.type
== EV_REL
|| event
.type
== EV_ABS
) {
114 time
= event
.time
.tv_sec
* 1000 + event
.time
.tv_usec
/ 1000;
115 hz
= 1000 / (time
- events
[i
].prvtime
);
121 events
[i
].hz
[events
[i
].count
& (HZ_LIST
- 1)] = hz
;
125 for(j
= 0; j
< HZ_LIST
; j
++) {
126 events
[i
].avghz
+= events
[i
].hz
[j
];
129 events
[i
].avghz
/= (events
[i
].count
> HZ_LIST
) ? HZ_LIST
: events
[i
].count
;
131 if(verbose
) printf("%s: Latest % 5iHz, Average % 5iHz\n", events
[i
].name
, hz
, events
[i
].avghz
);
134 events
[i
].prvtime
= time
;
140 for(i
= 0; i
< EVENTS
; i
++) {
141 if(events
[i
].fd
!= -1) {
142 if (events
[i
].avghz
!= 0) {
143 printf("\nAverage for %s: % 5iHz\n", events
[i
].name
, events
[i
].avghz
);