-
Notifications
You must be signed in to change notification settings - Fork 12
/
brightlight.c
320 lines (265 loc) · 10.1 KB
/
brightlight.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* brightlight v8 - change backlight brightness on Linux systems
* Copyright (C) 2016, 2017, 2018, 2019 multiplexd <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Requires the BSD libc function strtonum(). Either add '-lbsd' to the linker
* flags or get a copy of the strtonum() source file and link it into the final
* executable.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define PROGNAM "brightlight"
#define PROGVER 8
#define DEFAULT_CTRL_DIR "/sys/class/backlight/intel_backlight"
#define MAXBRIGHT "max_brightness"
#define CURBRIGHT "brightness"
#define OP_READ 0x01
#define OP_WRIT 0x02
#define OP_INCR 0x04
#define OP_DECR 0x08
#define OP_MAX 0x10
#define rounded_div(a, b) ((a + b / 2) / b)
extern long long strtonum(const char *, long long, long long, const char **);
void usage(void) {
printf("Usage: brightlight [OPTIONS]\n"
"Options:\n\n"
" -v, --version Print program version and exit.\n"
" -h, --help Show this help message.\n"
" -p, --percentage Read or write the brightness level as a percentage\n"
" (0 to 100) instead of the internal scale the kernel\n"
" uses (such as e.g. 0 to 7812).\n"
" -r, --read Read the backlight brightness level.\n"
" -w, --write <val> Set the backlight brightness level to <val>, where\n"
" <val> is a positive integer.\n"
" -i, --increment <val> Increment/increase the backlight brightness level by\n"
" <val>, where <val> is a positive integer.\n"
" --increase <val> Same as --increment.\n"
" -d, --decrement <val> Decrement/decrease the backlight brightness level by\n"
" <val>, where <val> is a positive integer.\n"
" --decrease <val> Same as --decrement.\n"
" -f, --file <path> Specify alternative path to backlight control\n"
" directory. This is likely to be a subdirectory under\n"
" \"/sys/class/backlight/\".\n"
" -m, --maximum Show maximum brightness level of the screen\n"
" backlight on the kernel's scale. The compile-time\n"
" default control directory is used if -f or --file is\n"
" not specified. The -p and --percentage flags are\n"
" ignored when this option is specified.\n\n"
"The flags -r, -w, -m, -i, -d and their corresponding long options are mutually\n"
"exclusive. If none of these flags are explicitly specified then -r is the\n"
"default.\n");
}
void versioninfo(void) {
printf("%s, v%u\n"
"Copyright (C) 2016, 2017, 2018, 2019 multiplexd <[email protected]>\n\n"
"This is free software, licenced under an ISC-like licence. You are\n"
"free to use, redistribute and modify this software as you wish, but\n"
"there is NO WARRANTY.\n", PROGNAM, PROGVER);
}
void checkargs(uint8_t *flags) {
uint8_t num = 0;
for (int i = 0; i < 8; i++)
num += (*flags >> i) % 2;
if (num > 1)
errx(1, "conflicting arguments (pass '-h' for help)");
else if (num == 0)
*flags = OP_READ;
return;
}
uint32_t read_file(int dirfd, char *path) {
char buf[200], *p;
int fd;
ssize_t rv;
uint32_t ret;
const char *errstr;
if ((fd = openat(dirfd, path, O_RDONLY)) < 0)
err(1, "could not open \"%s\" file", path);
p = buf;
while ((rv = read(fd, p, sizeof(buf) - (p - buf))) != 0) {
if (rv == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
err(1, "could not read from \"%s\" file", path);
}
p += rv;
}
if (close(fd) < 0)
warn("could not close file descriptor to \"%s\" file (continuing)", path);
p = buf;
while (p < (buf + sizeof(buf) - 1) && isdigit(*p))
p++;
*p = '\0';
ret = strtonum(buf, 0, UINT32_MAX, &errstr);
if (errstr)
errx(1, "invalid value \"%s\" found in \"%s\" file", buf, path);
return ret;
}
void write_file(int dirfd, char *path, uint32_t val) {
char buf[200], *p;
int fd;
ssize_t rv;
if (snprintf(buf, sizeof(buf), "%" PRIu32 "\n", val) >= sizeof(buf))
errx(1, "could not format output buffer");
if ((fd = openat(dirfd, path, O_RDWR)) < 0)
err(1, "could not open \"%s\" file", path);
p = buf;
while ((rv = write(fd, p, sizeof(buf) - (p - buf))) != 0) {
if (rv == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
err(1, "could not write to \"%s\" file", path);
}
p += rv;
}
if (close(fd) < 0)
warn("could not close file descriptor to \"%s\" file (continuing)", path);
return;
}
int main(int argc, char *argv[]) {
uint8_t flags = 0;
uint32_t delta = 0;
uint32_t max, current, newb = 0;
uint8_t pflag = 0;
char *ctrldir = NULL;
char *numarg = NULL;
int ch, dirfd;
const char *errstr;
struct option longopts[] = {
{"decrement", required_argument, NULL, 'd'},
{"decrease", required_argument, NULL, 'd'},
{"file", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"increment", required_argument, NULL, 'i'},
{"increase", required_argument, NULL, 'i'},
{"maximum", no_argument, NULL, 'm'},
{"percentage", no_argument, NULL, 'p'},
{"read", no_argument, NULL, 'r'},
{"version", no_argument, NULL, 'v'},
{"write", required_argument, NULL, 'w'},
{0, 0, 0, 0}
};
while ((ch = getopt_long(argc, argv, "+:d:f:hi:mprvw:", longopts, NULL)) != -1) {
switch (ch) {
case 'd':
flags |= OP_DECR;
numarg = optarg;
break;
case 'f':
ctrldir = optarg;
break;
case 'h':
usage();
exit(0);
break;
case 'i':
flags |= OP_INCR;
numarg = optarg;
break;
case 'm':
flags |= OP_MAX;
break;
case 'p':
pflag = 1;
break;
case 'r':
flags |= OP_READ;
break;
case 'v':
versioninfo();
exit(0);
break;
case 'w':
flags |= OP_WRIT;
numarg = optarg;
break;
case '?':
errx(1, "invalid option: '%c'", optopt);
break;
default:
errx(1, "error parsing options");
break;
}
}
argc -= optind;
argv += optind;
if (argc != 0)
errx(1, "too many arguments (pass '-h' for help)");
checkargs(&flags);
if (ctrldir == NULL) {
ctrldir = DEFAULT_CTRL_DIR;
}
if (strlen(ctrldir) > PATH_MAX)
errx(1, "path to control directory exceeds maximum path length");
if (flags & (OP_WRIT | OP_INCR | OP_DECR)) {
delta = strtonum(numarg, 0, UINT32_MAX, &errstr);
if (errstr)
errx(1, "invalid numeric argument: %s", errstr);
}
if ((dirfd = open(ctrldir, O_RDONLY | O_DIRECTORY)) == -1)
err(1, "could not open control directory \"%s\"", ctrldir);
max = read_file(dirfd, MAXBRIGHT);
if (flags & OP_MAX) {
printf("maximum backlight brightness is: %" PRIu32 "\n", max);
goto end;
}
current = read_file(dirfd, CURBRIGHT);
#define current_pct() (rounded_div(current * 100, max))
#define maybe_pct() (pflag ? "%" : "")
if (flags & OP_READ) {
printf("current backlight brightness is: %" PRIu32 "%s\n",
(pflag ? current_pct() : current), maybe_pct());
goto end;
}
switch (flags) {
case OP_INCR:
if (pflag ? (current_pct() + delta > 100) : (current + delta > max))
errx(1, "invalid increment: %" PRIu32 "%s", delta, maybe_pct());
newb = (pflag ? current + rounded_div(delta * max, 100) : current + delta);
break;
case OP_DECR:
if (pflag ? (current_pct() < delta) : (current < delta))
errx(1, "invalid decrement: %" PRIu32"%s", delta, maybe_pct());
newb = (pflag ? current - rounded_div(delta * max, 100) : current - delta);
break;
case OP_WRIT:
/* the underflow case is handled by strtonum above */
if (pflag ? (delta > 100) : (delta > max))
errx(1, "invalid argument: %" PRIu32 "%s", delta, maybe_pct());
newb = (pflag ? rounded_div(delta * max, 100) : delta);
break;
}
write_file(dirfd, CURBRIGHT, newb);
printf("changed backlight brightness: %" PRIu32 "%s => %" PRIu32 "%s\n",
(pflag ? current_pct() : current), maybe_pct(),
(pflag ? rounded_div(newb * 100, max) : newb), maybe_pct());
end:
if (close(dirfd) < 0)
warn("could not close control directory file descriptor");
exit(0);
#undef current_pct
#undef maybe_pct
}