-
Notifications
You must be signed in to change notification settings - Fork 9
/
backlight_control.c
58 lines (52 loc) · 1.19 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
#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 print_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
);
}
FILE *open_file(char *name) {
FILE *file;
if (!(file = fopen(name, "r+"))) {
fprintf(stderr, "failed to open %s\n", name);
exit(EXIT_FAILURE);
}
return file;
}
int main(int argc, char **argv) {
if (argc != 2) {
print_usage(argv[0]);
return EXIT_FAILURE;
}
int value = strtol(argv[1], NULL, 10);
FILE *brightness = open_file(BRIGHTNESS_FILE);
int brightness_value = MIN_BRIGHTNESS;
switch (argv[1][0]) {
case '+':
case '-':
fscanf(brightness, "%d", &brightness_value);
brightness_value += MAX_BRIGHTNESS * value / 100;
break;
default:
brightness_value = MAX_BRIGHTNESS * value / 100;
}
brightness_value = MIN(brightness_value, MAX_BRIGHTNESS);
brightness_value = MAX(brightness_value, MIN_BRIGHTNESS);
fprintf(brightness, "%d", brightness_value);
fclose(brightness);
return EXIT_SUCCESS;
}