-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalight.c
More file actions
221 lines (189 loc) · 5.52 KB
/
Copy pathalight.c
File metadata and controls
221 lines (189 loc) · 5.52 KB
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
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright (C) 2026 Maika Namuo
*/
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef SYSFS
#define SYSFS "/sys/class/backlight"
#endif
enum { NAME_LEN = 256, TYPE_LEN = 32, LINE_LEN = 64 };
struct light {
char name[NAME_LEN], type[TYPE_LEN];
long now, max;
};
static int fail(int error) {
errno = error ? error : EIO;
return -1;
}
static int usage(FILE *out, int code) {
fputs("usage: alight [-d DEVICE] [N|+N|-N]\n"
" alight -l\n"
" alight -h\n", out);
return code;
}
static long clamp(long value, long min, long max) {
return value < min ? min : value > max ? max : value;
}
static long percent(long raw, long max) {
return (long)((long double)clamp(raw, 0, max) * 100 / max + 0.5L);
}
static long raw_percent(long value, long max) {
return max / 100 * value + (max % 100 * value + 50) / 100;
}
static long target_raw(const struct light *light, long adjustment,
char operation) {
long max = light->max;
long now = clamp(light->now, 0, max);
long delta = raw_percent(adjustment < 0 ? -adjustment : adjustment, max);
if (operation != '+' && operation != '-')
return delta;
if (adjustment > 0)
return delta > max - now ? max : now + delta;
return delta > now ? 0 : now - delta;
}
static int valid_name(const char *name) {
return *name && strcmp(name, ".") && strcmp(name, "..") &&
strlen(name) < NAME_LEN && !strchr(name, '/');
}
static FILE *open_attr(const char *dev, const char *attr, const char *mode) {
char path[sizeof SYSFS + NAME_LEN + sizeof "/max_brightness"];
snprintf(path, sizeof path, "%s/%s/%s", SYSFS, dev, attr);
return fopen(path, mode);
}
static int close_as(FILE *file, int error) {
if (fclose(file) && !error)
error = errno ? errno : EIO;
return error ? fail(error) : 0;
}
static int read_attr(const char *dev, const char *attr, char *buf, int len) {
FILE *file = open_attr(dev, attr, "r");
char *newline;
int error = 0;
if (!file)
return -1;
errno = 0;
if (!fgets(buf, len, file))
error = ferror(file) ? (errno ? errno : EIO) : EINVAL;
else if ((newline = strchr(buf, '\n')))
*newline = 0;
else if (!feof(file))
error = ENAMETOOLONG;
return close_as(file, error);
}
static int parse_number(const char *text, long *value) {
char *end;
errno = 0;
if ((unsigned char)*text <= ' ')
return fail(EINVAL);
*value = strtol(text, &end, 10);
return errno || end == text || *end ? fail(errno ? errno : EINVAL) : 0;
}
static int read_number(const char *dev, const char *attr, long *value) {
char buf[LINE_LEN];
if (read_attr(dev, attr, buf, sizeof buf) || parse_number(buf, value))
return -1;
return *value < 0 ? fail(EINVAL) : 0;
}
static int write_brightness(const char *dev, long value) {
FILE *file = open_attr(dev, "brightness", "w");
int error = 0;
if (!file)
return -1;
errno = 0;
if (fprintf(file, "%ld\n", value) < 0)
error = errno ? errno : EIO;
return close_as(file, error);
}
static int type_rank(const char *type) {
return !strcmp(type, "firmware") ? 0 :
!strcmp(type, "platform") ? 1 : !strcmp(type, "raw") ? 2 : 3;
}
static int load_light(struct light *light, const char *name) {
if (!valid_name(name))
return fail(EINVAL);
strcpy(light->name, name);
if (read_number(name, "max_brightness", &light->max))
return -1;
if (light->max <= 0)
return fail(EINVAL);
if (read_number(name, "brightness", &light->now))
return -1;
if (read_attr(name, "type", light->type, sizeof light->type))
strcpy(light->type, "unknown");
return 0;
}
static int better_light(const struct light *a, const struct light *b) {
int a_rank = type_rank(a->type), b_rank = type_rank(b->type);
return a_rank < b_rank ||
(a_rank == b_rank && strcmp(a->name, b->name) < 0);
}
static void scan_lights(struct light *best) {
DIR *dir = opendir(SYSFS);
struct dirent *entry;
struct light light;
int found = 0;
if (!dir)
err(1, "%s", SYSFS);
for (errno = 0; (entry = readdir(dir)); errno = 0) {
if (load_light(&light, entry->d_name))
continue;
if (!best)
printf("%s\t%ld%%\t%ld/%ld\t%s\n", light.name,
percent(light.now, light.max), light.now, light.max,
light.type);
else if (!found || better_light(&light, best))
*best = light;
found = 1;
}
if (errno || closedir(dir))
err(1, "%s", SYSFS);
if (!found)
errx(1, "no backlight in %s", SYSFS);
}
int main(int argc, char **argv) {
struct light light;
long adjustment = 0;
const char *device = getenv("ALIGHT_DEVICE"), *value_arg = NULL;
int list = 0, device_arg = 0;
for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
char option = *arg == '-' && arg[1] && !arg[2] ? arg[1] : 0;
if (option == 'h')
return usage(stdout, 0);
if (option == 'l')
list = 1;
else if (option == 'd') {
if (++i == argc)
return usage(stderr, 2);
device = argv[i];
device_arg = 1;
} else if (value_arg)
return usage(stderr, 2);
else
value_arg = arg;
}
if (list) {
if (value_arg || device_arg)
return usage(stderr, 2);
scan_lights(NULL);
return 0;
}
if (value_arg && parse_number(value_arg, &adjustment))
return usage(stderr, 2);
adjustment = clamp(adjustment, -100, 100);
if (!device_arg && (!device || !*device))
scan_lights(&light);
else if (load_light(&light, device))
err(1, "%s", device);
if (!value_arg)
printf("%ld\n", percent(light.now, light.max));
else if (write_brightness(
light.name, target_raw(&light, adjustment, *value_arg)))
err(1, "%s/brightness", light.name);
return 0;
}