forked from Hendrikto/backlight_control
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacklight_control.c
81 lines (58 loc) · 1.47 KB
/
backlight_control.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
#include <stdio.h>
#include <stdlib.h>
/**
* A program to control the backlight brightness.
*
* @author: Hendrik Werner
*/
#define MIN_BRIGHTNESS 1
#define MAX(a, b) ((a > b) ? a : b)
#define MIN(a, b) ((a < b) ? a : b)
void usage(char *name) {
printf(
"Usage: %1$s [+|-]<value>\n\n"
"Examples:\n"
"\t%1$s +10\n"
"\t%1$s -10\n"
"\t%1$s 50\n",
name
);
}
int main(int argc, char **argv) {
if (argc != 2) {
usage(argv[0]);
return EXIT_FAILURE;
}
char *endptr;
long int value = strtol(argv[1], &endptr, 10);
if (endptr == argv[1]) {
fputs("brightness value must be an integer\n", stderr);
return EXIT_FAILURE;
}
if (!((value < 0 ? value *= -1 : value) >= 1 && value <= 100)) {
fputs("brightness value must be between 1 and 100 (inclusively)\n", stderr);
return EXIT_FAILURE;
}
FILE *fp;
if (!(fp = fopen(BRIGHTNESS_FILE, "r+"))) {
fprintf(stderr, "failed to open %s\n", BRIGHTNESS_FILE);
return (EXIT_FAILURE);
}
float brightness_value;
fscanf(fp, "%e", &brightness_value);
switch (argv[1][0]) {
case '+':
brightness_value += MAX_BRIGHTNESS * ((float)value / 100.0);
break;
case '-':
brightness_value -= MAX_BRIGHTNESS * ((float)value / 100.0);
break;
default:
brightness_value = MAX_BRIGHTNESS * ((float)value / 100.0);
}
brightness_value = MIN(brightness_value, MAX_BRIGHTNESS);
brightness_value = MAX(brightness_value, MIN_BRIGHTNESS);
fprintf(fp, "%.0f", brightness_value);
fclose(fp);
return EXIT_SUCCESS;
}