-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacklight.c
64 lines (47 loc) · 1.24 KB
/
backlight.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
#include <stdio.h>
#include <stdlib.h>
#define BRIGHTNESS "/sys/class/backlight/intel_backlight/brightness"
#define MAX_BRIGHTNESS "/sys/class/backlight/intel_backlight/max_brightness"
#define SIZE 8
int read_file(char const *const filename) {
FILE *file = fopen(filename, "r");
if (file == NULL)
return 0;
char buffer[SIZE];
fgets(buffer, SIZE, file);
fclose(file);
return atoi(buffer);
}
void write_file(char const *const filename, int const brightness) {
FILE *file = fopen(filename, "w");
if (file == NULL)
return;
fprintf(file, "%i", brightness);
fclose(file);
}
int main(int argc, char *argv[]) {
if (argc > 3 || argc < 2) {
return 0;
}
int brightness = read_file(BRIGHTNESS);
int const maximum = read_file(MAX_BRIGHTNESS);
int const minimum = 0;
char const command = argv[1][0];
int const volume = argc == 3 ? atoi(argv[2]) : 1;
if (command == 'i') {
brightness += volume;
} else if (command == 'd') {
brightness -= volume;
} else if (command == 's') {
brightness = volume;
} else {
return 0;
}
if (brightness > maximum) {
brightness = maximum;
} else if (brightness < minimum) {
brightness = minimum;
}
write_file(BRIGHTNESS, brightness);
return 0;
}