c7ba33d5e59637d01822648767b89a8c3603364d
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
{
30 unsigned long long prev_time
;
40 int main(int argc
, char *argv
[]) {
43 event_t events
[EVENTS
];
47 while((optch
= getopt(argc
, argv
, "hn")) != -1) {
50 printf("Usage: %s [-n|-h]\n", argv
[0]);
51 printf("-n nonverbose\n");
62 printf("%s must be used as superuser\n", argv
[0]);
66 signal(SIGINT
, sigint
);
68 printf("Press CTRL-C to exit.\n\n");
70 memset(events
, 0, sizeof(events
));
73 for(i
= 0; i
< EVENTS
; i
++) {
74 // 20 is needed for 3 digits of event devs under the expected format, but
75 // just give it some extra in case.
78 sprintf(device
, "/dev/input/event%i", i
);
79 events
[i
].fd
= open(device
, O_RDONLY
);
81 if(events
[i
].fd
!= -1) {
83 ioctl(events
[i
].fd
, EVIOCGNAME(sizeof(events
[i
].name
)), events
[i
].name
);
84 if(verbose
) printf("event%i: %s\n", i
, events
[i
].name
);
93 for(i
= 0; i
<= max_event
; i
++) {
94 if(events
[i
].fd
!= -1) {
95 FD_SET(events
[i
].fd
, &set
);
99 if(select(FD_SETSIZE
, &set
, NULL
, NULL
, NULL
) <= 0) {
104 struct input_event event
;
106 for(i
= 0; i
<= max_event
; i
++) {
107 if(events
[i
].fd
== -1 || !FD_ISSET(events
[i
].fd
, &set
)) {
111 bytes
= read(events
[i
].fd
, &event
, sizeof(event
));
113 if(bytes
!= sizeof(event
)) {
117 if(event
.type
== EV_REL
|| event
.type
== EV_ABS
) {
118 unsigned long long time
, timediff
;
121 time
= (unsigned long long)event
.time
.tv_sec
* 1000000ULL;
122 time
+= (unsigned long long)event
.time
.tv_usec
;
124 timediff
= time
- events
[i
].prev_time
;
127 hz
= 1000000ULL / timediff
;
133 events
[i
].hz
[events
[i
].count
& (HZ_LIST
- 1)] = hz
;
137 maxavg
= (events
[i
].count
> HZ_LIST
) ? HZ_LIST
: events
[i
].count
;
139 for(j
= 0; j
< maxavg
; j
++) {
140 events
[i
].avghz
+= events
[i
].hz
[j
];
143 events
[i
].avghz
/= maxavg
;
145 if(verbose
) printf("%s: Latest % 5iHz, Average % 5iHz\n", events
[i
].name
, hz
, events
[i
].avghz
);
148 events
[i
].prev_time
= time
;
153 for(i
= 0; i
< max_event
; i
++) {
154 if(events
[i
].fd
!= -1) {
155 if (events
[i
].avghz
!= 0) {
156 printf("\nAverage for %s: % 5iHz\n", events
[i
].name
, events
[i
].avghz
);