-
Notifications
You must be signed in to change notification settings - Fork 4
/
timeout.c
172 lines (146 loc) · 5.4 KB
/
timeout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* timeout.c - a little program to blank the RPi touchscreen and unblank it
on touch. Original by https://github.com/timothyhollabaugh
Loosely based on the original. The main difference is that the
brightness is progressively reduced to zero.
On a touch event we reset the brightness to the original value (read when
the program started.
Unless you change the permissions in udev this program needs to run as
root or setuid as root.
2019-01-22 Dougie Lawson
(C) Copyright 2019, Dougie Lawson, all right reserved.
2019-01-24 Jon Eskdale
2020-01-11 Jon Eskdale change to full brightness if changing when touch detected
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
long int readint(char* filenm) {
FILE* filefd;
filefd = fopen(filenm, "r");
if(filefd == NULL){
int err = errno;
printf("Error opening %s file: %d", filenm, err);
exit(1);
}
char number[10];
char* end;
fscanf(filefd, "%s", &number);
printf("File: %s ,The number is: %s\n",filenm, number);
fclose(filefd);
return strtol(number,&end,10);
}
int main(int argc, char* argv[]){
if (argc < 3) {
printf("Usage: timeout <timeout_sec> <min_brightness> <device> [<device>...]\n");
printf(" Use lsinput to see input devices.\n");
printf(" Device to use is shown as /dev/input/<device>\n");
exit(1);
}
int i;
int tlen;
int timeout;
int min_brightness;
tlen = strlen(argv[1]);
for (i=0;i<tlen; i++)
if (!isdigit(argv[1][i])) {
printf ("Entered timeout value is not a number\n");
exit(1);
}
timeout = atoi(argv[1]);
printf ("Timeout = %d seconds\n", timeout);
tlen = strlen(argv[2]);
for (i=0;i<tlen; i++)
if (!isdigit(argv[2][i])) {
printf ("Entered Min Brightness value is not a number\n");
exit(1);
}
min_brightness = atoi(argv[2]);
printf ("Minimum brightness = %d\n", min_brightness);
if (min_brightness > 254) {
printf ("Min Brightness must be 0-254\n");
exit(1);
}
int num_dev = argc - 3;
int eventfd[num_dev];
char device[num_dev][32];
for (i = 0; i < num_dev; i++) {
device[i][0] = '\0';
strcat(device[i], "/dev/input/");
strcat(device[i], argv[i + 3]);
int event_dev = open(device[i], O_RDONLY | O_NONBLOCK);
if(event_dev == -1){
int err = errno;
printf("Error opening %s: %d\n", device[i], err);
exit(1);
}
eventfd[i] = event_dev;
}
printf("Using input device%s: ", (num_dev > 1) ? "s" : "");
for (i = 0; i < num_dev; i++) {
printf("%s ", device[i]);
}
printf("\n");
printf("Starting...\n");
struct input_event event[64];
FILE* brightfd;
int event_size;
int light_size;
int size = sizeof(struct input_event);
long int actual_brightness;
long int max_brightness;
long int current_brightness;
/* new sleep code to bring CPU usage down from 100% on a core */
struct timespec sleepTime;
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = 100000000L; /* 1 seconds - larger values may reduce load even more */
char actual[53] = "/sys/class/backlight/rpi_backlight/actual_brightness";
char max[50] = "/sys/class/backlight/rpi_backlight/max_brightness";
char bright[46] = "/sys/class/backlight/rpi_backlight/brightness";
brightfd = fopen(bright, "w");
if(brightfd == NULL){
int err = errno;
printf("Error opening %s file: %d", bright, err);
exit(1);
}
actual_brightness = readint(actual);
max_brightness = readint(max);
current_brightness = actual_brightness;
printf("actual_brightness %d, max_brightness %d\n", actual_brightness, max_brightness);
time_t now = time(NULL);
time_t touch = now;
while(1) {
now = time(NULL);
for (i = 0; i < num_dev; i++){
event_size = read(eventfd[i], event, size*64);
if(event_size != -1) {
touch = now;
if (current_brightness < actual_brightness) {
printf("%s Value: %d, Code: %x\n", device[i], event[0].value, event[0].code);
current_brightness = actual_brightness;
printf("Brightness now %d\n", current_brightness);
fprintf(brightfd, "%d\n", current_brightness);
fflush(brightfd);
}
fseek(brightfd, 0, SEEK_SET);
}
}
if(difftime(now, touch) > timeout) {
if (current_brightness != min_brightness) {
current_brightness -= 5;
if (current_brightness < min_brightness) current_brightness = min_brightness;
printf("Brightness now %d\n", current_brightness);
fprintf(brightfd, "%d\n", current_brightness);
fflush(brightfd);
fseek(brightfd, 0, SEEK_SET);
}
}
nanosleep(&sleepTime, NULL);
}
}