-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbacklight.c
191 lines (162 loc) · 3.77 KB
/
zbacklight.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CURRENT_BRIGHTNESS "/sys/class/backlight/intel_backlight/actual_brightness"
#define MAX_BRIGHTNESS "/sys/class/backlight/intel_backlight/max_brightness"
#define TARGET_BRIGHTNESS "/sys/class/backlight/intel_backlight/brightness"
#define RATIO(current, max) (current * 1.0 / max * 100.0)
typedef enum
{
Get,
Set,
Inc,
Dec
} op_type;
struct options
{
op_type operation;
float value;
};
struct br_info
{
int current;
int max;
};
struct options parse_options(int ac, char * const * const av)
{
struct options options;
if (ac < 2 || strcmp(av[1], "-get") == 0)
options.operation = Get;
else
{
if (ac < 3)
{
fprintf(stderr, "Missing argument.\n");
exit(EXIT_FAILURE);
}
float value = strtof(av[2], NULL);
if (strcmp(av[1], "-set") == 0)
{
options.operation = Set;
options.value = value;
}
else if (strcmp(av[1], "-inc") == 0)
{
options.operation = Inc;
options.value = value;
}
else if (strcmp(av[1], "-dec") == 0)
{
options.operation = Dec;
options.value = value;
}
else
{
fprintf(stderr, "Invalid argument.\n");
exit(EXIT_FAILURE);
}
}
return options;
}
int get_value_file(char const * path)
{
int fd;
char buf[256];
if ((fd = open(path, O_RDONLY)) == -1)
{
perror(path);
exit(EXIT_FAILURE);
}
bzero(buf, 256);
if (read(fd, buf, 256) == -1)
{
perror(path);
exit(EXIT_FAILURE);
}
close(fd);
return strtol(buf, NULL, 10);
}
void set_value_file(char const * path, int value)
{
char buf[256];
int fd;
int len;
if ((fd = open(path, O_WRONLY)) == -1)
{
perror(path);
exit(EXIT_FAILURE);
}
len = sprintf(buf, "%d", value);
if (write(fd, buf, len) == -1)
{
perror(path);
exit(EXIT_FAILURE);
}
close(fd);
}
int get_cmd(struct br_info const * const info)
{
printf("Current brightness: %0.02f%%\n", RATIO(info->current, info->max));
return EXIT_SUCCESS;
}
int set_cmd(struct br_info const * const info, float value)
{
int target;
if (value <= 0)
value = 0;
else if (value > 100)
value = 100;
target = value / 100.0 * info->max;
if (target <= 0)
target = 1;
printf("Setting brightness to %d\n", target);
set_value_file(TARGET_BRIGHTNESS, target);
return EXIT_SUCCESS;
}
int inc_cmd(struct br_info const * const info, float value)
{
float ratio = RATIO(info->current, info->max);
set_cmd(info, ratio + value);
return EXIT_SUCCESS;
}
int dec_cmd(struct br_info const * const info, float value)
{
float ratio = RATIO(info->current, info->max);
set_cmd(info, ratio - value);
return EXIT_SUCCESS;
}
void load_br_info(struct br_info * const info)
{
info->current = get_value_file(CURRENT_BRIGHTNESS);
info->max = get_value_file(MAX_BRIGHTNESS);
}
int main(int ac, char * const * const av)
{
struct options options;
struct br_info info;
options = parse_options(ac, av);
load_br_info(&info);
int ret = EXIT_SUCCESS;
switch (options.operation)
{
case Get:
ret = get_cmd(&info);
break ;
case Set:
ret = set_cmd(&info, options.value);
break ;
case Inc:
ret = inc_cmd(&info, options.value);
break ;
case Dec:
ret = dec_cmd(&info, options.value);
break ;
default:
fprintf(stderr, "Invalid option\n");
ret = EXIT_FAILURE;
break;
}
return ret;
}