- for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
- if (*walk == '\n') {
- last = walk+1;
- continue;
- }
-
- if (*walk != '=')
- continue;
-
- if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
- BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
- remaining = atoi(walk+1);
- else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
- present_rate = atoi(walk+1);
- else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
- status = CS_CHARGING;
- else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
- status = CS_FULL;
- else {
- /* The only thing left is the full capacity */
- if (last_full_capacity) {
- if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
- !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
- continue;
- } else {
- if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
- !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
- continue;
- }
-
- full_design = atoi(walk+1);
- }
+ if (*walk != '=')
+ continue;
+
+ if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW=")) {
+ watt_as_unit = true;
+ batt_info->remaining = atoi(walk + 1);
+ batt_info->percentage_remaining = -1;
+ } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW=")) {
+ watt_as_unit = false;
+ batt_info->remaining = atoi(walk + 1);
+ batt_info->percentage_remaining = -1;
+ } else if (BEGINS_WITH(last, "POWER_SUPPLY_CAPACITY=") && batt_info->remaining == -1) {
+ batt_info->percentage_remaining = atoi(walk + 1);
+ } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW="))
+ batt_info->present_rate = abs(atoi(walk + 1));
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW="))
+ voltage = abs(atoi(walk + 1));
+ /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
+ * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
+ * unit instead of μAh. We will calculate it as we need it
+ * later. */
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW="))
+ batt_info->present_rate = abs(atoi(walk + 1));
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
+ batt_info->status = CS_CHARGING;
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
+ batt_info->status = CS_FULL;
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
+ batt_info->status = CS_DISCHARGING;
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
+ batt_info->status = CS_UNKNOWN;
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") ||
+ BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN="))
+ batt_info->full_design = atoi(walk + 1);
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL=") ||
+ BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL="))
+ batt_info->full_last = atoi(walk + 1);
+ }
+
+ /* the difference between POWER_SUPPLY_ENERGY_NOW and
+ * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
+ * given in mWh, the charge in mAh. So calculate every value given in
+ * ampere to watt */
+ if (!watt_as_unit && voltage >= 0) {
+ if (batt_info->present_rate > 0) {
+ batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0));
+ }
+ if (batt_info->remaining > 0) {
+ batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0));
+ }
+ if (batt_info->full_design > 0) {
+ batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.0));
+ }
+ if (batt_info->full_last > 0) {
+ batt_info->full_last = (((float)voltage / 1000.0) * ((float)batt_info->full_last / 1000.0));
+ }
+ }
+#elif defined(__DragonFly__)
+ union acpi_battery_ioctl_arg battio;
+ if (acpi_init()) {
+ battio.unit = number;
+ ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio);
+ batt_info->full_design = battio.bif.dcap;
+ batt_info->full_last = battio.bif.lfcap;
+ battio.unit = number;
+ ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio);
+ batt_info->percentage_remaining = battio.battinfo.cap;
+ batt_info->present_rate = battio.battinfo.rate;
+ batt_info->seconds_remaining = battio.battinfo.min * 60;
+ switch (battio.battinfo.state) {
+ case 0:
+ batt_info->status = CS_FULL;
+ break;
+ case ACPI_BATT_STAT_CHARGING:
+ batt_info->status = CS_CHARGING;
+ break;
+ case ACPI_BATT_STAT_DISCHARG:
+ batt_info->status = CS_DISCHARGING;
+ break;
+ default:
+ batt_info->status = CS_UNKNOWN;