-
Notifications
You must be signed in to change notification settings - Fork 8
/
kbdlight.c
86 lines (74 loc) · 2.05 KB
/
kbdlight.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define BLPATH "/sys/class/leds/smc::kbd_backlight"
#define BLVALFILE BLPATH "/brightness"
#define BLMAXFILE BLPATH "/max_brightness"
#define BLCHANGE 16
void usage() {
printf("Usage: kbdlight [up [<percentage>]|down [<percentage>]|off|max|get|getp|set <value>]\n");
exit(0);
}
int main(int argc, char* argv[]) {
int current, change, next, maxval;
FILE *file;
if (argc < 2) {
usage();
}
if(!(file = fopen(BLVALFILE, "r"))) {
perror("failed to open the keyboard backlight file: " BLVALFILE);
return 1;
}
fscanf(file, "%d", ¤t);
fclose(file);
if((file = fopen(BLMAXFILE, "r"))) {
fscanf(file, "%d", &maxval);
fclose(file);
} else {
maxval = 255;
}
if (!strcmp(argv[1], "up") || !strcmp(argv[1], "down")) {
if (argv[2]) {
char *endp;
change = strtol(argv[2], &endp, 10);
if (endp == argv[2]) {
fprintf(stderr, "percentage must be a number from 0 to 100.\n");
return 1;
}
change = change * 0.01 * maxval;
} else {
change = BLCHANGE;
}
}
if (!strcmp(argv[1], "up"))
next = current >= maxval - change ? maxval : current + change;
else if (!strcmp(argv[1], "down"))
next = current <= change ? 0 : current - change;
else if (!strcmp(argv[1], "off"))
next = 0;
else if (!strcmp(argv[1], "max"))
next = maxval;
else if (!strcmp(argv[1], "set")) {
char *endp;
if(!argv[2] || (next = strtol(argv[2], &endp, 10), endp == argv[2])) {
fprintf(stderr, "set must be followed by a number from 0 to %d\n", maxval);
return 1;
}
} else if(!strcmp(argv[1], "get")) {
printf("%d\n", current);
return 0;
} else if(!strcmp(argv[1], "getp")) {
float percentage = current * 100 / 255;
printf("%.f\n", percentage);
return 0;
} else
usage();
if((file = fopen(BLVALFILE, "w"))) {
fprintf(file, "%d", next);
fclose(file);
} else {
perror("failed to write to the keyboard backlight file: " BLVALFILE);
}
return 0;
}