wmshutdown: Add icon for freedesktop.org icon themes.
[dockapps.git] / wmbattery / acpi.c
blob1cf3a8ea59ce2216b0df3e8829f45b969dd44c7f
1 /*
2 * A not-yet-general-purpose ACPI library, by Joey Hess <joey@kitenet.net>
3 */
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <dirent.h>
10 #include <string.h>
11 #include <fnmatch.h>
12 #ifdef ACPI_APM
13 #include "apm.h"
14 #endif
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
19 #include "acpi.h"
21 #define SYSFS_PATH "/sys/class/power_supply"
22 #define ACPI_MAXITEM 8
24 int acpi_batt_count = 0;
25 /* Filenames of the battery info files for each system battery. */
26 char acpi_batt_info[ACPI_MAXITEM][128];
27 /* Filenames of the battery status files for each system battery. */
28 char acpi_batt_status[ACPI_MAXITEM][128];
29 /* Stores battery capacity, or 0 if the battery is absent. */
30 int acpi_batt_capacity[ACPI_MAXITEM];
32 int acpi_ac_count = 0;
33 char acpi_ac_adapter_info[ACPI_MAXITEM][128];
34 char acpi_ac_adapter_status[ACPI_MAXITEM][128];
36 char *acpi_labels[] = {
37 "uevent",
38 "status",
39 "Battery",
40 "Mains",
41 "POWER_SUPPLY_CAPACITY=",
42 "POWER_SUPPLY_??????_FULL_DESIGN=", /* CHARGE or ENERGY */
43 "POWER_SUPPLY_PRESENT=",
44 "POWER_SUPPLY_??????_NOW=",
45 "POWER_SUPPLY_CURRENT_NOW=",
46 "POWER_SUPPLY_STATUS=",
47 #if ACPI_THERMAL
48 "thermal_zone",
49 #endif
50 "POWER_SUPPLY_ONLINE=",
51 "POWER_SUPPLY_??????_FULL=",
52 NULL
55 #if ACPI_THERMAL
56 int acpi_thermal_count = 0;
57 char acpi_thermal_info[ACPI_MAXITEM][128];
58 char acpi_thermal_status[ACPI_MAXITEM][128];
59 #endif
61 /* Read in an entire ACPI proc file (well, the first 1024 bytes anyway), and
62 * return a statically allocated array containing it. */
63 inline char *get_acpi_file(const char *file)
65 int fd;
66 int end;
67 static char buf[1024];
68 fd = open(file, O_RDONLY);
69 if (fd == -1)
70 return NULL;
71 end = read(fd, buf, sizeof(buf));
72 buf[end-1] = '\0';
73 close(fd);
74 return buf;
77 int strmcmp(const char *s1, const char *s2)
79 for (; (*s1 == *s2) || (*s2 == '?'); s1++, s2++) {
80 if (*s1 == '\0')
81 return 0;
83 if (*s2 == '\0')
84 return 0;
85 else
86 return 1;
89 /* Given a buffer holding an acpi file, searches for the given key in it,
90 * and returns the numeric value. 0 is returned on failure. */
91 inline int scan_acpi_num(const char *buf, const char *key)
93 char *ptr;
94 int ret = 0;
96 do {
97 ptr = strchr(buf, '\n');
98 if (!strmcmp(buf, key)) {
99 ptr = strchr(buf, '=');
100 if (ptr) {
101 sscanf(ptr + 1, "%d", &ret);
102 return ret;
103 } else {
104 return 0;
107 if (ptr)
108 ptr++;
109 buf = ptr;
110 } while (buf != NULL);
111 return 0;
114 /* Given a buffer holding an acpi file, searches for the given key in it,
115 * and returns its value in a statically allocated string. */
116 inline char *scan_acpi_value(const char *buf, const char *key)
118 char *ptr;
119 static char ret[256];
121 do {
122 ptr = strchr(buf, '\n');
123 if (!strmcmp(buf, key)) {
124 ptr = strchr(buf, '=');
125 if (ptr) {
126 if (sscanf(ptr + 1, "%255s", ret) == 1)
127 return ret;
128 else
129 return NULL;
130 } else {
131 return NULL;
134 if (ptr)
135 ptr++;
136 buf = ptr;
137 } while (buf != NULL);
138 return NULL;
141 /* Read an ACPI proc file, pull out the requested piece of information, and
142 * return it (statically allocated string). Returns NULL on error, This is
143 * the slow, dumb way, fine for initialization or if only one value is needed
144 * from a file, slow if called many times. */
145 char *get_acpi_value(const char *file, const char *key)
147 char *buf = get_acpi_file(file);
148 if (!buf)
149 return NULL;
150 return scan_acpi_value(buf, key);
153 /* Returns the last full charge capacity of a battery.
155 int get_acpi_batt_capacity(int battery)
157 char *s;
159 s = get_acpi_value(acpi_batt_info[battery], acpi_labels[label_last_full_capacity]);
160 if (s == NULL)
161 return 0;
162 else
163 return atoi(s);
166 /* Comparison function for qsort. */
167 int _acpi_compare_strings(const void *a, const void *b)
169 const char **pa = (const char **)a;
170 const char **pb = (const char **)b;
171 return strcasecmp((const char *)*pa, (const char *)*pb);
174 /* Find something (batteries, ac adpaters, etc), and set up a string array
175 * to hold the paths to info and status files of the things found.
176 * Returns the number of items found. */
177 int find_items(char *itemname, char infoarray[ACPI_MAXITEM][128],
178 char statusarray[ACPI_MAXITEM][128])
180 DIR *dir;
181 struct dirent *ent;
182 int num_devices = 0;
183 int i;
184 char **devices = malloc(ACPI_MAXITEM * sizeof(char *));
186 char pathname[128];
188 sprintf(pathname, SYSFS_PATH);
190 dir = opendir(pathname);
191 if (dir == NULL) {
192 free(devices);
193 return 0;
195 while ((ent = readdir(dir))) {
196 char filename[128];
197 char buf[1024];
199 if (!strcmp(".", ent->d_name) ||
200 !strcmp("..", ent->d_name))
201 continue;
203 snprintf(filename, sizeof(filename), SYSFS_PATH "/%s/type", ent->d_name);
204 int fd = open(filename, O_RDONLY);
205 if (fd != -1) {
206 int end = read(fd, buf, sizeof(buf));
207 buf[end-1] = '\0';
208 close(fd);
209 if (strstr(buf, itemname) != buf)
210 continue;
213 devices[num_devices] = strdup(ent->d_name);
214 num_devices++;
215 if (num_devices >= ACPI_MAXITEM)
216 break;
218 closedir(dir);
220 /* Sort, since readdir can return in any order. /sys/ does
221 * sometimes list BAT1 before BAT0. */
222 qsort(devices, num_devices, sizeof(char *), _acpi_compare_strings);
224 for (i = 0; i < num_devices; i++) {
225 snprintf(infoarray[i], sizeof(infoarray[i]), SYSFS_PATH "/%s/%s", devices[i],
226 acpi_labels[label_info]);
227 snprintf(statusarray[i], sizeof(statusarray[i]), SYSFS_PATH "/%s/%s", devices[i],
228 acpi_labels[label_status]);
229 free(devices[i]);
232 free(devices);
233 return num_devices;
236 /* Find batteries, return the number, and set acpi_batt_count to it as well. */
237 int find_batteries(void)
239 int i;
240 acpi_batt_count = find_items(acpi_labels[label_battery], acpi_batt_info, acpi_batt_status);
241 for (i = 0; i < acpi_batt_count; i++)
242 acpi_batt_capacity[i] = get_acpi_batt_capacity(i);
243 return acpi_batt_count;
246 /* Find AC power adapters, return the number found, and set acpi_ac_count to it
247 * as well. */
248 int find_ac_adapters(void)
250 acpi_ac_count = find_items(acpi_labels[label_ac_adapter], acpi_ac_adapter_info, acpi_ac_adapter_status);
251 return acpi_ac_count;
254 #if ACPI_THERMAL
255 /* Find thermal information sources, return the number found, and set
256 * thermal_count to it as well. */
257 int find_thermal(void)
259 acpi_thermal_count = find_items(acpi_labels[label_thermal], acpi_thermal_info, acpi_thermal_status);
260 return acpi_thermal_count;
262 #endif
264 /* Returns true if the system is on ac power. Call find_ac_adapters first. */
265 int on_ac_power(void)
267 int i;
268 for (i = 0; i < acpi_ac_count; i++) {
269 char *online = get_acpi_value(acpi_ac_adapter_info[i], acpi_labels[label_ac_state]);
270 if (online && atoi(online))
271 return 1;
272 else
273 return 0;
275 return 0;
278 /* See if we have ACPI support and check version. Also find batteries and
279 * ac power adapters. */
280 int acpi_supported(void)
282 char *version;
283 DIR *dir;
284 int num;
286 dir = opendir(SYSFS_PATH);
287 if (!dir)
288 return 0;
289 closedir(dir);
291 /* If kernel is 2.6.21 or newer, version is in
292 /sys/module/acpi/parameters/acpica_version */
294 version = get_acpi_file("/sys/module/acpi/parameters/acpica_version");
295 if (version == NULL)
296 return 0;
297 num = atoi(version);
298 if (num < ACPI_VERSION) {
299 fprintf(stderr, "ACPI subsystem %s too is old, consider upgrading to %i.\n",
300 version, ACPI_VERSION);
301 return 0;
304 find_batteries();
305 find_ac_adapters();
306 #if ACPI_THERMAL
307 find_thermal();
308 #endif
310 return 1;
313 #ifdef ACPI_APM
314 /* Read ACPI info on a given power adapter and battery, and fill the passed
315 * apm_info struct. */
316 int acpi_read(int battery, apm_info *info)
318 char *buf, *state;
320 if (acpi_batt_count == 0) {
321 info->battery_percentage = 0;
322 info->battery_time = 0;
323 info->battery_status = BATTERY_STATUS_ABSENT;
324 acpi_batt_capacity[battery] = 0;
325 /* Where else would the power come from, eh? ;-) */
326 info->ac_line_status = 1;
327 return 0;
330 /* Internally it's zero indexed. */
331 battery--;
333 buf = get_acpi_file(acpi_batt_info[battery]);
334 if (buf == NULL) {
335 fprintf(stderr, "unable to read %s\n", acpi_batt_info[battery]);
336 perror("read");
337 exit(1);
340 info->ac_line_status = 0;
341 info->battery_flags = 0;
342 info->using_minutes = 1;
344 /* Work out if the battery is present, and what percentage of full
345 * it is and how much time is left. */
346 if (strcmp(scan_acpi_value(buf, acpi_labels[label_present]), "1") == 0) {
347 int pcap = scan_acpi_num(buf, acpi_labels[label_remaining_capacity]);
348 int rate = scan_acpi_num(buf, acpi_labels[label_present_rate]);
349 if (rate) {
350 /* time remaining = (current_capacity / discharge rate) */
351 info->battery_time = (float) pcap / (float) rate * 60;
352 } else {
353 char *rate_s = scan_acpi_value(buf, acpi_labels[label_present_rate]);
354 if (!rate_s) {
355 /* Time remaining unknown. */
356 info->battery_time = 0;
357 } else {
358 /* a zero or unknown in the file; time
359 * unknown so use a negative one to
360 * indicate this */
361 info->battery_time = -1;
365 state = scan_acpi_value(buf, acpi_labels[label_charging_state]);
366 if (state) {
367 if (state[0] == 'D') { /* discharging */
368 info->battery_status = BATTERY_STATUS_CHARGING;
369 /* Expensive ac power check used here
370 * because AC power might be on even if a
371 * battery is discharging in some cases. */
372 info->ac_line_status = on_ac_power();
373 } else if (state[0] == 'C' && state[1] == 'h') { /* charging */
374 info->battery_status = BATTERY_STATUS_CHARGING;
375 info->ac_line_status = 1;
376 info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
377 if (rate)
378 info->battery_time = -1 * (float) (acpi_batt_capacity[battery] - pcap) /
379 (float) rate * 60;
380 else
381 info->battery_time = 0;
382 if (abs(info->battery_time) < 0.5)
383 info->battery_time = 0;
384 } else if (state[0] == 'F') { /* full */
385 /* charged, on ac power */
386 info->battery_status = BATTERY_STATUS_HIGH;
387 info->ac_line_status = 1;
388 } else if (state[0] == 'C') { /* not charging, so must be critical */
389 info->battery_status = BATTERY_STATUS_CRITICAL;
390 /* Expensive ac power check used here
391 * because AC power might be on even if a
392 * battery is critical in some cases. */
393 info->ac_line_status = on_ac_power();
394 } else if (state[0] == 'U') { /* unknown */
395 info->ac_line_status = on_ac_power();
396 int current = scan_acpi_num(buf, acpi_labels[label_present_rate]);
397 if (info->ac_line_status) {
398 if (current == 0)
399 info->battery_status = BATTERY_STATUS_HIGH;
400 else
401 info->battery_status = BATTERY_STATUS_CHARGING;
402 } else {
403 info->battery_status = BATTERY_STATUS_CHARGING;
405 } else {
406 fprintf(stderr, "unknown battery state: %s\n", state);
408 } else {
409 /* Battery state unknown. */
410 info->battery_status = BATTERY_STATUS_ABSENT;
413 if (acpi_batt_capacity[battery] == 0) {
414 /* The battery was absent, and now is present.
415 * Well, it might be a different battery. So
416 * re-probe the battery. */
417 /* NOTE that this invalidates buf. No accesses of
418 * buf below this point! */
419 acpi_batt_capacity[battery] = get_acpi_batt_capacity(battery);
420 } else if (pcap > acpi_batt_capacity[battery]) {
421 /* Battery is somehow charged to greater than max
422 * capacity. Rescan for a new max capacity. */
423 find_batteries();
426 if (pcap && acpi_batt_capacity[battery]) {
427 info->battery_percentage = 100 * pcap / acpi_batt_capacity[battery];
428 if (info->battery_percentage > 100)
429 info->battery_percentage = 100;
430 } else {
431 info->battery_percentage = -1;
434 } else {
435 info->battery_percentage = 0;
436 info->battery_time = 0;
437 info->battery_status = BATTERY_STATUS_ABSENT;
438 acpi_batt_capacity[battery] = 0;
439 if (acpi_batt_count == 0) {
440 /* Where else would the power come from, eh? ;-) */
441 info->ac_line_status = 1;
442 } else {
443 /* Expensive ac power check. */
444 info->ac_line_status = on_ac_power();
448 return 0;
450 #endif