-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacklight.c
More file actions
97 lines (79 loc) · 2.03 KB
/
backlight.c
File metadata and controls
97 lines (79 loc) · 2.03 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
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "errno.h"
int read_value(char *fn) {
char buf[16];
int fd = open(fn, O_RDONLY);
if(fd < 0) {
fprintf(stderr, "open(%s) for reading failed: %s\n", fn, strerror(errno));
return -1;
}
memset(&buf, 0, sizeof(buf));
ssize_t len = read(fd, &buf, sizeof(buf) - 1);
if(len < 0) {
fprintf(stderr, "read(%s) failed: %s\n", fn, strerror(errno));
return -1;
}
int value = atoi((char*)&buf);
close(fd);
return value;
}
int write_value(char *fn, int value) {
char buf[16];
int fd = open(fn, O_WRONLY);
if(fd < 0) {
fprintf(stderr, "open(%s) for writing failed: %s\n", fn, strerror(errno));
return -1;
}
int len = snprintf((char*)&buf, 15, "%d", value);
if(write(fd, &buf, len) != len) {
fprintf(stderr, "write(%s) failed: %s\n", fn, strerror(errno));
return -1;
}
close(fd);
return 0;
}
int main(int argc, char* argv[]) {
// read current and max brightness
int actual_brightness = read_value("/sys/class/backlight/intel_backlight/actual_brightness");
int max_brightness = read_value("/sys/class/backlight/intel_backlight/max_brightness");
if((actual_brightness < 0) || (max_brightness < 0)) {
return -1;
}
// calculate 5% difference
int diff_brightness = max_brightness * 0.05;
if(diff_brightness == 0) {
diff_brightness = 1;
}
// new brightness based on command name
int new_br = actual_brightness;
if(strcmp(argv[0], "backlight_up") == 0) {
new_br += diff_brightness;
}
if(strcmp(argv[0], "backlight_down") == 0) {
new_br -= diff_brightness;
}
// new brightness as parameter
if(argc == 2) {
new_br = atoi(argv[1]);
}
// update brightness
if(new_br > max_brightness) {
new_br = max_brightness;
}
if(new_br < 0) {
new_br = 0;
}
if(new_br != actual_brightness) {
if(write_value("/sys/class/backlight/intel_backlight/brightness", new_br) != 0) {
return -1;
}
}
printf("%d -> %d [%d]\n", actual_brightness, new_br, max_brightness);
return 0;
}