- double current = INVALID_VALUE;
- double voltage = INVALID_VALUE;
- double charge = INVALID_VALUE;
- double *valptr = NULL;
- int charging = 0;
-
- struct dirent *ent;
- DIR *dh;
-
- if ((dh = opendir ("/proc/acpi/battery")) == NULL)
- {
- char errbuf[1024];
- ERROR ("Cannot open `/proc/acpi/battery': %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (-1);
- }
-
- while ((ent = readdir (dh)) != NULL)
- {
- if (ent->d_name[0] == '.')
- continue;
-
- len = ssnprintf (filename, sizeof (filename),
- "/proc/acpi/battery/%s/state",
- ent->d_name);
- if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
- continue;
-
- if ((fh = fopen (filename, "r")) == NULL)
- {
- char errbuf[1024];
- ERROR ("Cannot open `%s': %s", filename,
- sstrerror (errno, errbuf,
- sizeof (errbuf)));
- continue;
- }
-
- /*
- * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
- * [11:00] <@tokkee> present: yes
- * [11:00] <@tokkee> capacity state: ok
- * [11:00] <@tokkee> charging state: charging
- * [11:00] <@tokkee> present rate: 1724 mA
- * [11:00] <@tokkee> remaining capacity: 4136 mAh
- * [11:00] <@tokkee> present voltage: 12428 mV
- */
- while (fgets (buffer, sizeof (buffer), fh) != NULL)
- {
- numfields = strsplit (buffer, fields, 8);
-
- if (numfields < 3)
- continue;
-
- if ((strcmp (fields[0], "present") == 0)
- && (strcmp (fields[1], "rate:") == 0))
- valptr = ¤t;
- else if ((strcmp (fields[0], "remaining") == 0)
- && (strcmp (fields[1], "capacity:") == 0))
- valptr = &charge;
- else if ((strcmp (fields[0], "present") == 0)
- && (strcmp (fields[1], "voltage:") == 0))
- valptr = &voltage;
- else
- valptr = NULL;
-
- if ((strcmp (fields[0], "charging") == 0)
- && (strcmp (fields[1], "state:") == 0))
- {
- if (strcmp (fields[2], "charging") == 0)
- charging = 1;
- else
- charging = 0;
- }
-
- if (valptr != NULL)
- {
- char *endptr;
-
- endptr = NULL;
- errno = 0;
-
- *valptr = strtod (fields[2], &endptr) / 1000.0;
-
- if ((fields[2] == endptr) || (errno != 0))
- *valptr = INVALID_VALUE;
- }
- } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
-
- fclose (fh);
-
- if ((current != INVALID_VALUE) && (charging == 0))
- current *= -1;
-
- if (charge != INVALID_VALUE)
- battery_submit ("0", "charge", charge);
- if (current != INVALID_VALUE)
- battery_submit ("0", "current", current);
- if (voltage != INVALID_VALUE)
- battery_submit ("0", "voltage", voltage);
- }
-
- closedir (dh);