-
Notifications
You must be signed in to change notification settings - Fork 1
/
gobolight.c
145 lines (123 loc) · 2.93 KB
/
gobolight.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
#include <dirent.h>
#include <err.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#ifndef CLAMP
#define CLAMP(x,a,b) ((x)<(a)?(a):((x)>(b)?(b):(x)))
#endif
enum modes {
INC,
DEC,
GET,
SET
};
int readFile(const char* filename) {
FILE* fd = fopen(filename, "r");
if (!fd) {
err(1, filename);
}
char buffer[256];
char* rc = fgets(buffer, 255, fd);
if (rc != buffer) {
err(1, filename);
}
buffer[255] = '\0';
int n = strtol(buffer, NULL, 10);
if (errno != 0) {
err(1, filename);
}
fclose(fd);
return n;
}
int setBrightness(uid_t user, int units) {
seteuid(0);
FILE* fd = fopen("brightness", "w");
setuid(user);
if (!fd) {
err(1, "brightness");
}
fprintf(fd, "%d", units);
int ret = errno;
fclose(fd);
return ret;
}
int main(int argc, char** argv) {
uid_t user = getuid();
seteuid(user);
enum modes mode = GET;
int value = 0;
if (argc > 1) {
int v = 0;
if (argc == 2) {
mode = SET;
v = 1;
} else if (argc == 3) {
if (strcmp(argv[1], "-inc") == 0) {
mode = INC;
} else if (strcmp(argv[1], "-dec") == 0) {
mode = DEC;
} else {
errx(1, "invalid argument: %s", argv[1]);
}
v = 2;
} else {
errx(1, "invalid number of arguments");
}
char* endptr;
value = strtol(argv[v], &endptr, 10);
if (errno != 0 || *endptr != '\0') {
errx(1, "invalid argument: %s", argv[v]);
}
if (value < 0 || value > 100) {
errx(1, "value out of range (not between 0 and 100): %d", value);
}
}
DIR* dirp = opendir("/sys/class/backlight");
if (!dirp) {
err(1, "/sys/class/backlight");
}
int rc = chdir("/sys/class/backlight");
if (rc != 0) {
err(1, "/sys/class/backlight");
}
struct dirent* ent;
const char* name;
get_name:
ent = readdir(dirp);
if (!ent) {
err(1, "no backlight");
}
name = ent->d_name;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
goto get_name;
}
rc = chdir(ent->d_name);
if (rc != 0) {
err(1, "/sys/class/backlight/%s", ent->d_name);
}
int brightness = readFile("brightness");
int maxBrightness = readFile("max_brightness");
int units = (int)((double)value / 100.0 * maxBrightness);
int ret = 0;
switch (mode) {
case SET:
ret = setBrightness(user, units);
break;
case INC:
ret = setBrightness(user, CLAMP(brightness + units, 0, maxBrightness));
break;
case DEC:
ret = setBrightness(user, CLAMP(brightness - units, 0, maxBrightness));
break;
}
if (mode != GET) {
brightness = readFile("brightness");
}
printf("%d\n", (int)ceil((double)brightness / maxBrightness * 100.0));
return ret;
}