7
|
1 /* |
|
2 * main.c -- adjust laptop backlight using ACPI |
|
3 * |
|
4 * Copyright (c) 2010-2019 David Demelier <markand@malikania.fr> |
|
5 * |
|
6 * Permission to use, copy, modify, and distribute this software for any |
|
7 * purpose with or without fee is hereby granted, provided that the above |
|
8 * copyright notice and this permission notice appear in all copies. |
|
9 * |
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
17 */ |
|
18 |
|
19 #include <errno.h> |
|
20 #include <stdarg.h> |
|
21 #include <stdio.h> |
|
22 #include <stdlib.h> |
|
23 #include <string.h> |
|
24 |
|
25 enum action { |
|
26 Up, |
|
27 Down |
|
28 }; |
|
29 |
|
30 static void |
|
31 usage(const char *name) |
|
32 { |
|
33 fprintf(stderr, "usage: %s down\n", name); |
|
34 fprintf(stderr, " %s up\n", name); |
|
35 exit(1); |
|
36 } |
|
37 |
|
38 static void |
|
39 die(const char *fmt, ...) |
|
40 { |
|
41 va_list ap; |
|
42 |
|
43 va_start(ap, fmt); |
|
44 fprintf(stderr, "abort: "); |
|
45 vfprintf(stderr, fmt, ap); |
|
46 va_end(ap); |
|
47 |
|
48 exit(1); |
|
49 } |
|
50 |
|
51 /* {{{ Support for FreeBSD */ |
|
52 |
|
53 #if defined(__FreeBSD__) |
|
54 |
|
55 #include <sys/types.h> |
|
56 #include <sys/sysctl.h> |
|
57 |
|
58 static int |
|
59 set(enum action type) |
|
60 { |
|
61 int current, next = 0, i; |
|
62 int msg[100]; |
|
63 size_t len, nextlen; |
|
64 |
|
65 /* First, get all values availables */ |
|
66 len = sizeof (msg); |
|
67 |
|
68 if (sysctlbyname("hw.acpi.video.lcd0.levels", msg, &len, NULL, 0) == -1) |
|
69 die("sysctl: %s\n", strerror(errno)); |
|
70 |
|
71 /* Get the current value */ |
|
72 len = sizeof (current); |
|
73 |
|
74 if (sysctlbyname("hw.acpi.video.lcd0.brightness", ¤t, &len, NULL, 0) == -1) |
|
75 die("sysctl: %s\n", strerror(errno)); |
|
76 |
|
77 /* First find the index of 0 */ |
|
78 for (i = 0; msg[i] != 0; ++i) |
|
79 continue; |
|
80 |
|
81 /* Find the current value index in msg */ |
|
82 for (; msg[i] != current; ++i) |
|
83 continue; |
|
84 |
|
85 if (type == Up) { |
|
86 if (msg[i] >= 100) |
|
87 return msg[i]; |
|
88 |
|
89 next = msg[i + 1]; |
|
90 } else { |
|
91 if (msg[i] == 0) |
|
92 return 0; |
|
93 |
|
94 next = msg[i - 1]; |
|
95 } |
|
96 |
|
97 nextlen = sizeof (next); |
|
98 |
|
99 if (sysctlbyname("hw.acpi.video.lcd0.brightness", ¤t, &len, &next, nextlen) == -1) |
|
100 die("sysctl: %s\n", strerror(errno)); |
|
101 |
|
102 return next; |
|
103 } |
|
104 |
|
105 #endif |
|
106 |
|
107 /* }}} */ |
|
108 |
|
109 /* {{{ Support for Linux */ |
|
110 |
|
111 #if defined(__linux__) |
|
112 |
|
113 #include <sys/types.h> |
|
114 #include <sys/stat.h> |
|
115 |
|
116 static int |
|
117 read_int(const char *path) |
|
118 { |
|
119 FILE *fp; |
|
120 int value = 0; |
|
121 |
|
122 if ((fp = fopen(path, "r")) == NULL) |
|
123 die("open: %s\n", strerror(errno)); |
|
124 if (fscanf(fp, "%d", &value) != 1) |
|
125 die("could not read value from %s\n", path); |
|
126 |
|
127 fclose(fp); |
|
128 |
|
129 return value; |
|
130 } |
|
131 |
|
132 static int |
|
133 write_int(const char *path, int value) |
|
134 { |
|
135 FILE *fp; |
|
136 |
|
137 if ((fp = fopen(path, "w+")) == NULL) |
|
138 die("open: %s\n", strerror(errno)); |
|
139 |
|
140 fprintf(fp, "%d", value); |
|
141 |
|
142 return value; |
|
143 } |
|
144 |
|
145 static const char * |
|
146 find_card(void) |
|
147 { |
|
148 static const char *list[] = { |
|
149 "/sys/class/backlight/acpi_video0", |
|
150 "/sys/class/backlight/intel_backlight", |
|
151 NULL |
|
152 }; |
|
153 |
|
154 struct stat st; |
|
155 |
|
156 for (const char **ptr = list; *ptr != NULL; ++ptr) |
|
157 if (stat(*ptr, &st) >= 0) |
|
158 return *ptr; |
|
159 |
|
160 return NULL; |
|
161 } |
|
162 |
|
163 static int |
|
164 set(int type) |
|
165 { |
|
166 int current, max; |
|
167 char file[BUFSIZ]; |
|
168 const char *card = find_card(); |
|
169 |
|
170 /* Find a card adaptor */ |
|
171 if (!card) |
|
172 die("could not find card adaptor\n"); |
|
173 |
|
174 /* Read actual */ |
|
175 snprintf(file, sizeof (file), "%s/actual_brightness", card); |
|
176 current = read_int(file); |
|
177 |
|
178 /* Read max */ |
|
179 snprintf(file, sizeof (file), "%s/max_brightness", card); |
|
180 max = read_int(file); |
|
181 |
|
182 if (type == Up) { |
|
183 current += max / 100; |
|
184 |
|
185 if (current > max) |
|
186 return max; |
|
187 } else { |
|
188 current -= max / 100; |
|
189 |
|
190 if (current < 0) |
|
191 return 0; |
|
192 } |
|
193 |
|
194 snprintf(file, sizeof (file), "%s/brightness", card); |
|
195 |
|
196 return write_int(file, current); |
|
197 } |
|
198 |
|
199 #else |
|
200 |
|
201 /* Not supported */ |
|
202 static int |
|
203 set(int type) |
|
204 { |
|
205 (void)type; |
|
206 |
|
207 die("backlight is not supported on this system"); |
|
208 } |
|
209 |
|
210 #endif |
|
211 |
|
212 /* }}} */ |
|
213 |
|
214 int |
|
215 main(int argc, char *argv[]) |
|
216 { |
|
217 int st, type; |
|
218 |
|
219 if (argc < 2) |
|
220 usage(argv[0]); |
|
221 |
|
222 if (strcmp(argv[1], "up") == 0) |
|
223 type = Up; |
|
224 else if (strcmp(argv[1], "down") == 0) |
|
225 type = Down; |
|
226 else |
|
227 usage(argv[0]); |
|
228 |
|
229 if ((st = set(type)) < 0) |
|
230 return 1; |
|
231 |
|
232 printf("switching to %d\n", st); |
|
233 |
|
234 return 0; |
|
235 } |