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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define BUF_SIZE 100
#define KBD_MAX_PATH "/sys/class/leds/tpacpi::kbd_backlight/max_brightness"
#define KBD_BRIGHTNESS_PATH "/sys/class/leds/tpacpi::kbd_backlight/brightness"
#define KBD_ERROR_ARGS -1
#define KBD_ERROR_MEM -2
#define KBD_SUCCESS 1
typedef enum direction { BL_DOWN, BL_UP, BL_OFF, BL_MAX } direction_t;
int read_int(const char *path, int *value);
int write_int(const char *path, int value);
int bl_new(int current, int max, direction_t direction);
void usage(const char *name);
int get_direction(const char *str, direction_t *dir);
int main(int argc, char *argv[]) {
int bl_max = -1, bl_current = -1, bl;
direction_t direction;
if (argc < 2) {
usage(argv[0]);
return -1;
}
if (get_direction(argv[1], &direction) != KBD_SUCCESS) {
fprintf(stderr, "E: Unexpected argument \"%s\"\n", argv[1]);
usage(argv[0]);
return 1;
}
if (read_int(KBD_MAX_PATH, &bl_max) != KBD_SUCCESS) {
fprintf(stderr, "E: Failed to read maximum brightness from \"%s\"\n", KBD_MAX_PATH);
usage(argv[0]);
return 1;
}
if (read_int(KBD_BRIGHTNESS_PATH, &bl_current) != KBD_SUCCESS) {
fprintf(stderr, "E: Failed to read current brightness from \"%s\"\n", KBD_BRIGHTNESS_PATH);
usage(argv[0]);
return 1;
}
bl = bl_new(bl_current, bl_max, direction);
if (write_int(KBD_BRIGHTNESS_PATH, bl) != KBD_SUCCESS) {
fprintf(stderr, "E: Failed to writing to \"%s\"\n", KBD_BRIGHTNESS_PATH);
usage(argv[0]);
return 1;
}
return 0;
}
int read_int(const char *path, int *value) {
FILE *fp = fopen(path, "r");
int pos;
if (fp == NULL)
return KBD_ERROR_MEM;
char buf[BUF_SIZE];
if (fread(buf, sizeof(char), BUF_SIZE, fp) == 0)
return KBD_ERROR_MEM;
int status = sscanf(buf, "%d%n", value, &pos);
if (status == EOF || pos != 1) {
return KBD_ERROR_MEM;
}
fclose(fp);
return KBD_SUCCESS;
}
int write_int(const char *path, int value) {
FILE *fp = fopen(path, "w");
if (fp == NULL)
return KBD_ERROR_MEM;
int status = fprintf(fp, "%d", value);
if (status < 0)
return KBD_ERROR_MEM;
fclose(fp);
return KBD_SUCCESS;
}
int bl_new(int current, int max, direction_t direction) {
switch(direction) {
case BL_DOWN:
return (current == 0 ? 0 : current - 1);
break;
case BL_UP:
return (current == max ? max : current + 1);
break;
case BL_OFF:
return 0;
break;
case BL_MAX:
return max;
break;
}
return 0;
}
void usage(const char *name) {
fprintf(stderr, "Usage: %s [up|down|off|max]\n", name);
}
int get_direction(const char *str, direction_t *dir) {
if (strcmp(str, "up") == 0) {
*dir = BL_UP;
} else if (strcmp(str, "down") == 0) {
*dir = BL_DOWN;
} else if (strcmp(str, "off") == 0) {
*dir = BL_OFF;
} else if (strcmp(str, "max") == 0) {
*dir = BL_MAX;
} else {
return KBD_ERROR_ARGS;
}
return KBD_SUCCESS;
}
|