Remove trailing whitespace.
[dockapps.git] / wmacpi / libacpi.c
blobfb770154423f5e64511992b9b6ccc9aa7bae0088
1 #define _GNU_SOURCE
3 #include <stdio.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <dirent.h>
10 #include <time.h>
12 #include "libacpi.h"
14 extern char *state[];
16 #define PROC_DATA_SOURCE 0
17 #define SYSFS_DATA_SOURCE 1
18 static int data_source;
20 /* local proto */
21 int acpi_get_design_cap(int batt);
23 static int read_sysfs_file(char *node, char *prop, char *buf, size_t buflen)
25 char tmp[256];
26 FILE *fp;
27 int ret;
29 ret = snprintf(tmp, sizeof(tmp), "/sys/class/power_supply/%s/%s", node, prop);
30 if (ret >= (int)sizeof(tmp)) {
31 perr("Path too long for %s/%s\n", node, prop);
32 return -1;
35 fp = fopen(tmp, "r");
36 if (fp == NULL) {
37 perr("Could not open %s/%s\n", node, prop);
38 return -2;
41 ret = fread(buf, 1, buflen - 1, fp);
43 fclose(fp);
45 if (ret == 0) {
46 perr("Could not read %s/%s\n", node, prop);
47 return -3;
50 buf[ret] = '\0';
52 return 0;
55 /* initialise the batteries */
56 static int sysfs_init_batteries(global_t *globals)
58 DIR *battdir;
59 struct dirent *batt;
60 char *name;
61 char *names[MAXBATT];
62 char ps_type[16];
63 int i, j;
65 /* now enumerate batteries */
66 globals->battery_count = 0;
67 battdir = opendir("/sys/class/power_supply");
68 if (battdir == NULL) {
69 pfatal("No batteries or ACPI not supported\n");
70 return 1;
72 while ((batt = readdir(battdir))) {
73 /* there's a serious problem with this code when there's
74 * more than one battery: the readdir won't return the
75 * entries in sorted order, so battery one won't
76 * necessarily be the first one returned. So, we need
77 * to sort them ourselves before adding them to the
78 * batteries array. */
79 name = batt->d_name;
81 /* skip ., .. and dotfiles */
82 if (name[0] == '.')
83 continue;
85 if (read_sysfs_file(name, "type", ps_type, sizeof(ps_type)) < 0)
86 continue;
88 if (strncmp("Battery", ps_type, 7) != 0)
89 continue;
91 names[globals->battery_count] = strdup(name);
92 globals->battery_count++;
94 closedir(battdir);
96 /* A nice quick insertion sort, ala CLR. */
98 char *tmp1, *tmp2;
100 for (i = 1; i < globals->battery_count; i++) {
101 tmp1 = names[i];
102 j = i - 1;
103 while ((j >= 0) && ((strcmp(tmp1, names[j])) < 0)) {
104 tmp2 = names[j+1];
105 names[j+1] = names[j];
106 names[j] = tmp2;
111 for (i = 0; i < globals->battery_count; i++) {
112 snprintf(batteries[i].name, MAX_NAME, "%s", names[i]);
113 pdebug("battery detected at /sys/class/power_supply/%s\n", batteries[i].name);
114 pinfo("found battery %s\n", names[i]);
116 if (read_sysfs_file(batteries[i].name, "energy_now", ps_type, sizeof(ps_type)) == 0)
117 batteries[i].sysfs_capa_mode = SYSFS_CAPA_ENERGY;
118 else if (read_sysfs_file(batteries[i].name, "charge_now", ps_type, sizeof(ps_type)) == 0)
119 batteries[i].sysfs_capa_mode = SYSFS_CAPA_CHARGE;
120 else if (read_sysfs_file(batteries[i].name, "capacity", ps_type, sizeof(ps_type)) == 0) {
121 batteries[i].sysfs_capa_mode = SYSFS_CAPA_PERCENT;
122 batteries[i].design_cap = 100;
123 batteries[i].last_full_cap = 100;
124 } else
125 batteries[i].sysfs_capa_mode = SYSFS_CAPA_ERR;
128 /* tell user some info */
129 pdebug("%d batteries detected\n", globals->battery_count);
130 pinfo("libacpi: found %d batter%s\n", globals->battery_count,
131 (globals->battery_count == 1) ? "y" : "ies");
133 return 0;
136 /* initialise the batteries */
137 static int procfs_init_batteries(global_t *globals)
139 DIR *battdir;
140 struct dirent *batt;
141 char *name;
142 char *names[MAXBATT];
143 int i, j;
145 /* now enumerate batteries */
146 globals->battery_count = 0;
147 battdir = opendir("/proc/acpi/battery");
148 if (battdir == NULL) {
149 pfatal("No batteries or ACPI not supported\n");
150 return 1;
152 while ((batt = readdir(battdir))) {
153 /* there's a serious problem with this code when there's
154 * more than one battery: the readdir won't return the
155 * entries in sorted order, so battery one won't
156 * necessarily be the first one returned. So, we need
157 * to sort them ourselves before adding them to the
158 * batteries array. */
159 name = batt->d_name;
161 /* skip . and .. */
162 if (!strncmp(".", name, 1) || !strncmp("..", name, 2))
163 continue;
165 names[globals->battery_count] = strdup(name);
166 globals->battery_count++;
168 closedir(battdir);
170 /* A nice quick insertion sort, ala CLR. */
172 char *tmp1, *tmp2;
174 for (i = 1; i < globals->battery_count; i++) {
175 tmp1 = names[i];
176 j = i - 1;
177 while ((j >= 0) && ((strcmp(tmp1, names[j])) < 0)) {
178 tmp2 = names[j+1];
179 names[j+1] = names[j];
180 names[j] = tmp2;
185 for (i = 0; i < globals->battery_count; i++) {
186 snprintf(batteries[i].name, MAX_NAME, "%s", names[i]);
187 snprintf(batteries[i].info_file, MAX_NAME,
188 "/proc/acpi/battery/%s/info", names[i]);
189 snprintf(batteries[i].state_file, MAX_NAME,
190 "/proc/acpi/battery/%s/state", names[i]);
191 pdebug("battery detected at %s\n", batteries[i].info_file);
192 pinfo("found battery %s\n", names[i]);
195 /* tell user some info */
196 pdebug("%d batteries detected\n", globals->battery_count);
197 pinfo("libacpi: found %d batter%s\n", globals->battery_count,
198 (globals->battery_count == 1) ? "y" : "ies");
200 return 0;
203 int init_batteries(global_t *globals)
205 if (data_source == SYSFS_DATA_SOURCE)
206 return sysfs_init_batteries(globals);
207 else
208 return procfs_init_batteries(globals);
211 /* a stub that just calls the current function */
212 int reinit_batteries(global_t *globals)
214 pdebug("reinitialising batteries\n");
215 return init_batteries(globals);
218 /* the actual name of the subdirectory under power_supply may
219 * be anything, so we need to read the directory and use the
220 * name we find there. */
221 static int sysfs_init_ac_adapters(global_t *globals)
223 DIR *acdir;
224 struct dirent *adapter;
225 adapter_t *ap = &globals->adapter;
226 char *name;
227 char ps_type[16];
229 acdir = opendir("/sys/class/power_supply");
230 if (acdir == NULL) {
231 pfatal("Unable to open /sys/class/power_supply -"
232 " are you sure this system supports ACPI?\n");
233 return 1;
235 name = NULL;
236 while ((adapter = readdir(acdir)) != NULL) {
237 name = adapter->d_name;
239 if (name[0] == '.') {
240 name = NULL;
241 continue;
244 if (read_sysfs_file(name, "type", ps_type, sizeof(ps_type)) < 0) {
245 name = NULL;
246 continue;
249 if (strncmp("Mains", ps_type, 5) == 0) {
250 pdebug("found adapter %s\n", name);
251 break;
252 } else {
253 name = NULL;
256 closedir(acdir);
258 if (name == NULL) {
259 perr("No AC adapter found !\n");
260 return 1;
263 /* we'll just use the first adapter we find ... */
264 ap->name = strdup(name);
265 pinfo("libacpi: found ac adapter %s\n", ap->name);
267 return 0;
270 /* the actual name of the subdirectory under ac_adapter may
271 * be anything, so we need to read the directory and use the
272 * name we find there. */
273 static int procfs_init_ac_adapters(global_t *globals)
275 DIR *acdir;
276 struct dirent *adapter;
277 adapter_t *ap = &globals->adapter;
278 char *name;
280 acdir = opendir("/proc/acpi/ac_adapter");
281 if (acdir == NULL) {
282 pfatal("Unable to open /proc/acpi/ac_adapter -"
283 " are you sure this system supports ACPI?\n");
284 return 1;
286 name = NULL;
287 while ((adapter = readdir(acdir)) != NULL) {
288 name = adapter->d_name;
290 if (!strncmp(".", name, 1) || !strncmp("..", name, 2))
291 continue;
292 pdebug("found adapter %s\n", name);
294 closedir(acdir);
295 /* we /should/ only see one filename other than . and .. so
296 * we'll just use the last value name acquires . . . */
297 ap->name = strdup(name);
298 snprintf(ap->state_file, MAX_NAME, "/proc/acpi/ac_adapter/%s/state",
299 ap->name);
300 pinfo("libacpi: found ac adapter %s\n", ap->name);
302 return 0;
305 int init_ac_adapters(global_t *globals)
307 if (data_source == SYSFS_DATA_SOURCE)
308 return sysfs_init_ac_adapters(globals);
309 else
310 return procfs_init_ac_adapters(globals);
313 /* stub that does nothing but call the normal init function */
314 int reinit_ac_adapters(global_t *globals)
316 pdebug("reinitialising ac adapters\n");
317 return init_ac_adapters(globals);
320 /* see if we have ACPI support and check version */
321 int power_init(global_t *globals)
323 FILE *acpi;
324 char buf[4096];
325 int acpi_ver = 0;
326 int retval;
327 unsigned int version_offset = 0;
329 if (!(acpi = fopen("/sys/module/acpi/parameters/acpica_version", "r"))) {
330 if (!(acpi = fopen("/proc/acpi/info", "r"))) {
331 pfatal("This system does not support ACPI\n");
332 return 1;
333 } else {
334 version_offset = 25;
338 /* okay, now see if we got the right version */
339 fread(buf, 4096, 1, acpi);
340 acpi_ver = strtol(buf + version_offset, NULL, 10);
341 pinfo("ACPI version detected: %d\n", acpi_ver);
342 if (acpi_ver < 20020214) {
343 pfatal("This version requires ACPI subsystem version 20020214\n");
344 fclose(acpi);
345 return 1;
347 /* yep, all good */
348 fclose(acpi);
350 /* determine data source */
351 if (access("/sys/class/power_supply", R_OK | X_OK) == 0) {
352 data_source = SYSFS_DATA_SOURCE;
353 pinfo("Selecting sysfs as the data source\n");
354 } else {
355 data_source = PROC_DATA_SOURCE;
356 pinfo("Selecting procfs as the data source\n");
359 if (!(retval = init_batteries(globals)))
360 retval = init_ac_adapters(globals);
362 return retval;
365 /* reinitialise everything, to deal with changing batteries or ac adapters */
366 int power_reinit(global_t *globals)
368 FILE *acpi;
369 int retval;
371 if (!(acpi = fopen("/sys/module/acpi/parameters/acpica_version", "r"))) {
372 if (!(acpi = fopen("/proc/acpi/info", "r"))) {
373 pfatal("Could not reopen ACPI info file - does this system support ACPI?\n");
374 return 1;
378 if (!(retval = reinit_batteries(globals)))
379 retval = reinit_ac_adapters(globals);
381 return retval;
384 static char *get_value(char *string)
386 char *retval;
387 int i;
389 if (string == NULL)
390 return NULL;
392 i = 0;
393 while (string[i] != ':') i++;
394 while (!isalnum(string[i])) i++;
395 retval = (string + i);
397 return retval;
400 static int check_error(char *buf)
402 if(strstr(buf, "ERROR") != NULL)
403 return 1;
404 return 0;
407 static power_state_t sysfs_get_power_status(global_t *globals)
409 char online[2];
410 adapter_t *ap = &globals->adapter;
412 if (read_sysfs_file(ap->name, "online", online, sizeof(online)) < 0)
413 return PS_ERR;
415 if (*online == '1')
416 return AC;
417 else
418 return BATT;
421 static power_state_t procfs_get_power_status(global_t *globals)
423 FILE *file;
424 char buf[1024];
425 char *val;
426 adapter_t *ap = &globals->adapter;
428 if ((file = fopen(ap->state_file, "r")) == NULL) {
429 snprintf(buf, 1024, "Could not open state file %s", ap->state_file);
430 perror(buf);
431 return PS_ERR;
434 fgets(buf, 1024, file);
435 fclose(file);
436 val = get_value(buf);
437 if ((strncmp(val, "on-line", 7)) == 0)
438 return AC;
439 else
440 return BATT;
443 power_state_t get_power_status(global_t *globals)
445 if (data_source == SYSFS_DATA_SOURCE)
446 return sysfs_get_power_status(globals);
447 else
448 return procfs_get_power_status(globals);
451 static int sysfs_get_battery_info(global_t *globals, int batt_no)
453 battery_t *info = &batteries[batt_no];
454 char buf[32];
455 int ret;
457 /* check to see if battery is present */
458 ret = read_sysfs_file(info->name, "present", buf, sizeof(buf));
459 if (ret < 0) {
460 /* interestingly, when the battery is not present, the whole
461 * /sys/class/power_supply/BATn directory does not exist.
462 * Yes, this is broken.
464 if (ret == -2)
465 info->present = 0;
467 /* reinit batteries, this one went away and it's very
468 possible there just isn't any other one */
469 reinit_batteries(globals);
471 return 0;
474 info->present = (*buf == '1');
475 if (!info->present) {
476 pinfo("Battery %s not present\n", info->name);
477 return 0;
480 /* get design capacity
481 * note that all these integer values can also contain the
482 * string 'unknown', so we need to check for this. */
483 if (info->sysfs_capa_mode == SYSFS_CAPA_ENERGY) {
484 if (read_sysfs_file(info->name, "energy_full_design", buf, sizeof(buf)) < 0)
485 info->design_cap = -1;
486 else
487 info->design_cap = strtoul(buf, NULL, 10) / 1000;
489 /* get last full capacity */
490 if (read_sysfs_file(info->name, "energy_full", buf, sizeof(buf)) < 0)
491 info->last_full_cap = -1;
492 else
493 info->last_full_cap = strtoul(buf, NULL, 10) / 1000;
494 } else if (info->sysfs_capa_mode == SYSFS_CAPA_CHARGE) {
495 /* get design capacity */
496 if (read_sysfs_file(info->name, "charge_full_design", buf, sizeof(buf)) < 0)
497 info->design_cap = -1;
498 else
499 info->design_cap = strtoul(buf, NULL, 10) / 1000;
501 /* get last full capacity */
502 if (read_sysfs_file(info->name, "charge_full", buf, sizeof(buf)) < 0)
503 info->last_full_cap = -1;
504 else
505 info->last_full_cap = strtoul(buf, NULL, 10) / 1000;
506 } else if (info->sysfs_capa_mode != SYSFS_CAPA_PERCENT) {
507 info->design_cap = -1;
508 info->last_full_cap = -1;
512 /* get design voltage */
513 if (read_sysfs_file(info->name, "voltage_min_design", buf, sizeof(buf)) < 0)
514 info->design_voltage = -1;
515 else
516 info->design_voltage = strtoul(buf, NULL, 10) / 1000;
518 /* get charging state */
519 if (read_sysfs_file(info->name, "status", buf, sizeof(buf)) < 0) {
520 info->charge_state = CH_ERR;
521 } else {
522 if (strncmp(buf, "Unknown", 7) == 0)
523 info->charge_state = CH_ERR;
524 else if (strncmp(buf, "Discharging", 11) == 0)
525 info->charge_state = DISCHARGE;
526 else if (strncmp(buf, "Charging", 8) == 0)
527 info->charge_state = CHARGE;
528 else if (strncmp(buf, "Not charging", 12) == 0)
529 info->charge_state = NO_CHARGE;
530 else if (strncmp(buf, "Full", 4) == 0)
531 info->charge_state = FULL; /* DISCHARGE ? as per old comment ... */
534 /* get current rate of burn
535 * note that if it's on AC, this will report 0 */
536 if (read_sysfs_file(info->name, "current_now", buf, sizeof(buf)) < 0)
537 info->present_rate = -1;
538 else {
539 int rate;
540 rate = strtoul(buf, NULL, 10) / 1000;
541 info->present_rate = (rate != 0) ? rate : info->present_rate;
544 if (info->sysfs_capa_mode == SYSFS_CAPA_ENERGY) {
545 /* get remaining capacity */
546 if (read_sysfs_file(info->name, "energy_now", buf, sizeof(buf)) < 0)
547 info->remaining_cap = -1;
548 else
549 info->remaining_cap = strtoul(buf, NULL, 10) / 1000;
551 } else if (info->sysfs_capa_mode == SYSFS_CAPA_CHARGE) {
552 /* get remaining capacity */
553 if (read_sysfs_file(info->name, "charge_now", buf, sizeof(buf)) < 0)
554 info->remaining_cap = -1;
555 else
556 info->remaining_cap = strtoul(buf, NULL, 10) / 1000;
557 } else if (info->sysfs_capa_mode == SYSFS_CAPA_PERCENT) {
558 /* get remaining capacity */
559 if (read_sysfs_file(info->name, "capacity", buf, sizeof(buf)) < 0)
560 info->remaining_cap = -1;
561 else
562 info->remaining_cap = strtoul(buf, NULL, 10) / 1000;
563 } else {
564 info->remaining_cap = -1;
567 /* get current voltage */
568 if (read_sysfs_file(info->name, "voltage_now", buf, sizeof(buf)) < 0)
569 info->present_voltage = -1;
570 else
571 info->present_voltage = strtoul(buf, NULL, 10) / 1000;
573 return 1;
576 static int procfs_get_battery_info(global_t *globals, int batt_no)
578 FILE *file;
579 battery_t *info = &batteries[batt_no];
580 char buf[1024];
581 char *entry;
582 int buflen;
583 char *val;
585 globals = globals; /* silencing a warning */
587 if ((file = fopen(info->info_file, "r")) == NULL) {
588 /* this is cheating, but string concatenation should work . . . */
589 pfatal("Could not open %s:", info->info_file );
590 perror(NULL);
591 return 0;
594 /* grab the contents of the file */
595 buflen = fread(buf, sizeof(buf), 1, file);
596 fclose(file);
598 /* check to see if there were any errors reported in the file */
599 if(check_error(buf)) {
600 pinfo("Error reported in file %s - discarding data\n",
601 info->info_file);
602 return 0;
605 /* check to see if battery is present */
606 entry = strstr(buf, "present:");
607 val = get_value(entry);
608 if ((strncmp(val, "yes", 3)) == 0) {
609 info->present = 1;
610 } else {
611 pinfo("Battery %s not present\n", info->name);
612 info->present = 0;
613 return 0;
616 /* get design capacity
617 * note that all these integer values can also contain the
618 * string 'unknown', so we need to check for this. */
619 entry = strstr(buf, "design capacity:");
620 val = get_value(entry);
621 if (val[0] == 'u')
622 info->design_cap = -1;
623 else
624 info->design_cap = strtoul(val, NULL, 10);
626 /* get last full capacity */
627 entry = strstr(buf, "last full capacity:");
628 val = get_value(entry);
629 if (val[0] == 'u')
630 info->last_full_cap = -1;
631 else
632 info->last_full_cap = strtoul(val, NULL, 10);
634 /* get design voltage */
635 entry = strstr(buf, "design voltage:");
636 val = get_value(entry);
637 if (val[0] == 'u')
638 info->design_voltage = -1;
639 else
640 info->design_voltage = strtoul(val, NULL, 10);
643 if ((file = fopen(info->state_file, "r")) == NULL) {
644 perr("Could not open %s:", info->state_file );
645 perror(NULL);
646 return 0;
649 /* grab the file contents */
650 memset(buf, 0, sizeof(buf));
651 buflen = fread(buf, sizeof(buf), 1, file);
652 fclose(file);
654 /* check to see if there were any errors reported in the file */
655 if(check_error(buf)) {
656 pinfo("Error reported in file %s - discarding data\n",
657 info->state_file);
658 return 0;
660 /* check to see if battery is present */
661 entry = strstr(buf, "present:");
662 val = get_value(entry);
663 if ((strncmp(val, "yes", 3)) == 0) {
664 info->present = 1;
665 } else {
666 info->present = 0;
667 perr("Battery %s no longer present\n", info->name);
668 return 0;
671 /* get charging state */
672 entry = strstr(buf, "charging state:");
673 val = get_value(entry);
674 if (val[0] == 'u')
675 info->charge_state = CH_ERR;
676 else if ((strncmp(val, "discharging", 10)) == 0)
677 info->charge_state = DISCHARGE;
678 else if ((strncmp(val, "charged", 7)) == 0)
679 /* this is a workaround for machines that report
680 * their charge state as 'charged', rather than
681 * what my laptop does, which is go straight to
682 * 'discharging'. dunno which matches the standard */
683 info->charge_state = DISCHARGE;
684 else
685 info->charge_state = CHARGE;
687 /* get current rate of burn
688 * note that if it's on AC, this will report 0 */
689 entry = strstr(buf, "present rate:");
690 val = get_value(entry);
691 if (val[0] == 'u') {
692 info->present_rate = -1;
693 } else {
694 int rate;
695 rate = strtoul(val, NULL, 10);
696 if (rate != 0)
697 info->present_rate = rate;
700 /* get remaining capacity */
701 entry = strstr(buf, "remaining capacity:");
702 val = get_value(entry);
703 if (val[0] == 'u')
704 info->remaining_cap = -1;
705 else
706 info->remaining_cap = strtoul(val, NULL, 10);
708 /* get current voltage */
709 entry = strstr(buf, "present voltage:");
710 val = get_value(entry);
711 if (val[0] == 'u')
712 info->present_voltage = -1;
713 else
714 info->present_voltage = strtoul(val, NULL, 10);
716 return 1;
719 int get_battery_info(global_t *globals, int batt_no)
721 if (data_source == SYSFS_DATA_SOURCE)
722 return sysfs_get_battery_info(globals, batt_no);
723 else
724 return procfs_get_battery_info(globals, batt_no);
728 * 2003-7-1.
729 * In order to make this code more convenient for things other than
730 * just plain old wmacpi-ng I'm breaking the basic functionality
731 * up into several chunks: collecting and collating info for a
732 * single battery, calculating the global info (such as rtime), and
733 * some stuff to provide a similar interface to now.
736 /* calculate the percentage remaining, using the values of
737 * remaining capacity and last full capacity, as outlined in
738 * the ACPI spec v2.0a, section 3.9.3. */
739 static int calc_remaining_percentage(int batt)
741 float rcap, lfcap;
742 battery_t *binfo;
743 int retval;
745 binfo = &batteries[batt];
747 rcap = (float)binfo->remaining_cap;
748 lfcap = (float)binfo->last_full_cap;
750 /* we use -1 to indicate that the value is unknown . . . */
751 if (rcap < 0) {
752 perr("unknown percentage value\n");
753 retval = -1;
754 } else {
755 if (lfcap <= 0)
756 lfcap = 1;
757 retval = (int)((rcap/lfcap) * 100.0);
758 pdebug("percent: %d\n", retval);
760 return retval;
763 /* check to see if we've been getting bad data from the batteries - if
764 * we get more than some limit we switch to using the remaining capacity
765 * for the calculations. */
766 static enum rtime_mode check_rt_mode(global_t *globals)
768 int i;
769 int bad_limit = 5;
770 battery_t *binfo;
772 /* if we were told what to do, we should keep doing it */
773 if(globals->rt_forced)
774 return globals->rt_mode;
776 for(i = 0; i < MAXBATT; i++) {
777 binfo = &batteries[i];
778 if(binfo->present && globals->adapter.power == BATT) {
779 if(binfo->present_rate <= 0) {
780 pdebug("Bad report from %s\n", binfo->name);
781 binfo->bad_count++;
785 for(i = 0; i < MAXBATT; i++) {
786 binfo = &batteries[i];
787 if(binfo->bad_count > bad_limit) {
788 if(globals->rt_mode != RT_CAP)
789 pinfo("More than %d bad reports from %s; "
790 "Switching to remaining capacity mode\n",
791 bad_limit, binfo->name);
792 return RT_CAP;
795 return RT_RATE;
798 /* calculate remaining time until the battery is charged.
799 * when charging, the battery state file reports the
800 * current being used to charge the battery. We can use
801 * this and the remaining capacity to work out how long
802 * until it reaches the last full capacity of the battery.
803 * XXX: make sure this is actually portable . . . */
804 static int calc_charge_time_rate(int batt)
806 float rcap, lfcap;
807 battery_t *binfo;
808 int charge_time = 0;
810 binfo = &batteries[batt];
812 if (binfo->charge_state == CHARGE) {
813 if (binfo->present_rate == -1) {
814 perr("unknown present rate\n");
815 charge_time = -1;
816 } else {
817 lfcap = (float)binfo->last_full_cap;
818 rcap = (float)binfo->remaining_cap;
820 charge_time = (int)(((lfcap - rcap)/binfo->present_rate) * 60.0);
822 } else
823 if (binfo->charge_time)
824 charge_time = 0;
825 return charge_time;
828 /* we need to calculate the present rate the same way we do in rt_cap
829 * mode, and then use that to estimate charge time. This will
830 * necessarily be even less accurate than it is for remaining time, but
831 * it's just as neessary . . . */
832 static int calc_charge_time_cap(int batt)
834 static float cap_samples[CAP_SAMPLES];
835 static int time_samples[CAP_SAMPLES];
836 static int sample_count = 0;
837 static int current = 0;
838 static int old = 1;
839 int rtime;
840 int tdiff;
841 float cdiff;
842 float current_rate;
843 battery_t *binfo = &batteries[batt];
845 cap_samples[current] = (float) binfo->remaining_cap;
846 time_samples[current] = time(NULL);
848 if (sample_count == 0) {
849 /* we can't do much if we don't have any data . . . */
850 current_rate = 0;
851 } else if (sample_count < CAP_SAMPLES) {
852 /* if we have less than SAMPLES samples so far, we use the first
853 * sample and the current one */
854 cdiff = cap_samples[current] - cap_samples[0];
855 tdiff = time_samples[current] - time_samples[0];
856 current_rate = cdiff/tdiff;
857 } else {
858 /* if we have more than SAMPLES samples, we use the oldest
859 * current one, which at this point is current + 1. This will
860 * wrap the same way that current will wrap, but one cycle
861 * ahead */
862 cdiff = cap_samples[current] - cap_samples[old];
863 tdiff = time_samples[current] - time_samples[old];
864 current_rate = cdiff/(float)tdiff;
866 if (current_rate == 0)
867 rtime = 0;
868 else {
869 float cap_left = (float)(binfo->last_full_cap - binfo->remaining_cap);
870 rtime = (int)(cap_left/(current_rate * 60.0));
872 sample_count++, current++, old++;
873 if (current >= CAP_SAMPLES)
874 current = 0;
875 if (old >= CAP_SAMPLES)
876 old = 0;
878 pdebug("cap charge time rem: %d\n", rtime);
879 return rtime;
882 static int calc_charge_time(global_t *globals, int batt)
884 int ctime = 0;
886 globals->rt_mode = check_rt_mode(globals);
888 switch(globals->rt_mode) {
889 case RT_RATE:
890 ctime = calc_charge_time_rate(batt);
891 break;
892 case RT_CAP:
893 ctime = calc_charge_time_cap(batt);
894 break;
896 return ctime;
899 void acquire_batt_info(global_t *globals, int batt)
901 battery_t *binfo;
902 adapter_t *ap = &globals->adapter;
904 get_battery_info(globals, batt);
906 binfo = &batteries[batt];
908 if (!binfo->present) {
909 binfo->percentage = 0;
910 binfo->valid = 0;
911 binfo->charge_time = 0;
912 globals->rtime = 0;
913 return;
916 binfo->percentage = calc_remaining_percentage(batt);
918 /* set the battery's capacity state, based (at present) on some
919 * guesstimated values: more than 75% == HIGH, 25% to 75% MED, and
920 * less than 25% is LOW. Less than globals->crit_level is CRIT. */
921 if (binfo->percentage == -1)
922 binfo->state = BS_ERR;
923 if (binfo->percentage < globals->crit_level)
924 binfo->state = CRIT;
925 else if (binfo->percentage > 75)
926 binfo->state = HIGH;
927 else if (binfo->percentage > 25)
928 binfo->state = MED;
929 else
930 binfo->state = LOW;
932 /* we need to /know/ that we've got a valid state for the
933 * globals->power value . . . .*/
934 ap->power = get_power_status(globals);
936 binfo->charge_time = calc_charge_time(globals, batt);
938 /* and finally, we tell anyone who wants to use this information
939 * that it's now valid . . .*/
940 binfo->valid = 1;
943 void acquire_all_batt_info(global_t *globals)
945 int i;
947 for(i = 0; i < globals->battery_count; i++)
948 acquire_batt_info(globals, i);
952 * One of the feature requests I've had is for some way to deal with
953 * batteries that are too dumb or too b0rken to report a present rate
954 * value. The way to do this, obviously, is to record the time that
955 * samples were taken and use that information to calculate the rate
956 * at which the battery is draining/charging. This still won't help
957 * systems where the battery doesn't even report the remaining
958 * capacity, but without the present rate or the remaining capacity, I
959 * don't think there's /anything/ we can do to work around it.
961 * So, what we need to do is provide a way to use a different method
962 * to calculate the time remaining. What seems most sensible is to
963 * split out the code to calculate it into a seperate function, and
964 * then provide multiple implementations . . .
968 * the default implementation - if present rate and remaining capacity
969 * are both reported correctly, we use them.
971 int calc_time_remaining_rate(global_t *globals)
973 int i;
974 int rtime;
975 float rcap = 0;
976 float rate = 0;
977 battery_t *binfo;
978 static float rate_samples[SAMPLES];
979 static int sample_count = 0;
980 static int j = 0;
981 static int n = 0;
983 /* calculate the time remaining, using the battery's remaining
984 * capacity and the reported burn rate (3.9.3).
985 * For added accuracy, we average the value over the last
986 * SAMPLES number of calls, or for anything less than this we
987 * simply report the raw number. */
988 /* XXX: this needs to correctly handle the case where
989 * any of the values used is unknown (which we flag using
990 * -1). */
991 for (i = 0; i < globals->battery_count; i++) {
992 binfo = &batteries[i];
993 if (binfo->present && binfo->valid) {
994 rcap += (float)binfo->remaining_cap;
995 rate += (float)binfo->present_rate;
998 rate_samples[j] = rate;
999 j++, sample_count++;
1000 if (j >= SAMPLES)
1001 j = 0;
1003 /* for the first SAMPLES number of calls we calculate the
1004 * average based on sample_count, then we use SAMPLES to
1005 * calculate the rolling average. */
1007 /* when this fails, n should be equal to SAMPLES. */
1008 if (sample_count < SAMPLES)
1009 n++;
1010 for (i = 0, rate = 0; i < n; i++) {
1011 /* if any of our samples are invalid, we drop
1012 * straight out, and flag our unknown values. */
1013 if (rate_samples[i] < 0) {
1014 rate = -1;
1015 rtime = -1;
1016 goto out;
1018 rate += rate_samples[i];
1020 rate = rate/(float)n;
1022 if ((rcap < 1) || (rate < 1)) {
1023 rtime = 0;
1024 goto out;
1026 if (rate <= 0)
1027 rate = 1;
1028 /* time remaining in minutes */
1029 rtime = (int)((rcap/rate) * 60.0);
1030 if(rtime <= 0)
1031 rtime = 0;
1032 out:
1033 pdebug("discharge time rem: %d\n", rtime);
1034 return rtime;
1038 * the alternative implementation - record the time at which each
1039 * sample was taken, and then use the difference between the latest
1040 * sample and the one SAMPLES ago to calculate the difference over
1041 * that time, and from there the rate of change of capacity.
1043 * XXX: this code sucks, but largely because batteries aren't exactly
1044 * precision instruments - mine only report with about 70mAH
1045 * resolution, so they don't report any changes until the difference
1046 * is 70mAH. This means that calculating the current rate from the
1047 * remaining capacity is very choppy . . .
1049 * To fix this, we should calculate an average over some number of
1050 * samples at the old end of the set - this would smooth out the
1051 * transitions.
1053 int calc_time_remaining_cap(global_t *globals)
1055 static float cap_samples[CAP_SAMPLES];
1056 static int time_samples[CAP_SAMPLES];
1057 static int sample_count = 0;
1058 static int current = 0;
1059 static int old = 1;
1060 battery_t *binfo;
1061 int i;
1062 int rtime;
1063 int tdiff;
1064 float cdiff;
1065 float cap = 0;
1066 float current_rate;
1068 for (i = 0; i < globals->battery_count; i++) {
1069 binfo = &batteries[i];
1070 if (binfo->present && binfo->valid)
1071 cap += binfo->remaining_cap;
1073 cap_samples[current] = cap;
1074 time_samples[current] = time(NULL);
1076 if (sample_count == 0) {
1077 /* we can't do much if we don't have any data . . . */
1078 current_rate = 0;
1079 } else if (sample_count < CAP_SAMPLES) {
1080 /* if we have less than SAMPLES samples so far, we use the first
1081 * sample and the current one */
1082 cdiff = cap_samples[0] - cap_samples[current];
1083 tdiff = time_samples[current] - time_samples[0];
1084 current_rate = cdiff/tdiff;
1085 } else {
1086 /* if we have more than SAMPLES samples, we use the oldest
1087 * current one, which at this point is current + 1. This will
1088 * wrap the same way that current will wrap, but one cycle
1089 * ahead */
1090 cdiff = cap_samples[old] - cap_samples[current];
1091 tdiff = time_samples[current] - time_samples[old];
1092 current_rate = cdiff/tdiff;
1094 if (current_rate == 0)
1095 rtime = 0;
1096 else
1097 rtime = (int)(cap_samples[current]/(current_rate * 60.0));
1099 sample_count++, current++, old++;
1100 if (current >= CAP_SAMPLES)
1101 current = 0;
1102 if (old >= CAP_SAMPLES)
1103 old = 0;
1105 pdebug("cap discharge time rem: %d\n", rtime);
1106 return rtime;
1109 void acquire_global_info(global_t *globals)
1111 adapter_t *ap = &globals->adapter;
1113 globals->rt_mode = check_rt_mode(globals);
1115 switch(globals->rt_mode) {
1116 case RT_RATE:
1117 globals->rtime = calc_time_remaining_rate(globals);
1118 break;
1119 case RT_CAP:
1120 globals->rtime = calc_time_remaining_cap(globals);
1121 break;
1124 /* get the power status.
1125 * note that this is actually reported seperately from the
1126 * battery info, under /proc/acpi/ac_adapter/AC/state */
1127 ap->power = get_power_status(globals);
1130 void acquire_all_info(global_t *globals)
1132 acquire_all_batt_info(globals);
1133 acquire_global_info(globals);