- 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")) {
- watt_as_unit = true;
- remaining = atoi(walk+1);
- }
- else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
- watt_as_unit = false;
- 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_VOLTAGE_NOW"))
- voltage = 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"))
- 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(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
+ int state;
+ int sysctl_rslt;
+ size_t sysctl_size = sizeof(sysctl_rslt);
+
+ if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
+ OUTPUT_FULL_TEXT(format_down);
+ return false;
+ }
+
+ batt_info->percentage_remaining = sysctl_rslt;
+ if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
+ OUTPUT_FULL_TEXT(format_down);
+ return false;
+ }
+
+ batt_info->seconds_remaining = sysctl_rslt * 60;
+ if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
+ OUTPUT_FULL_TEXT(format_down);
+ return false;
+ }
+
+ state = sysctl_rslt;
+ if (state == 0 && batt_info->percentage_remaining == 100)
+ batt_info->status = CS_FULL;
+ else if ((state & ACPI_BATT_STAT_CHARGING) && batt_info->percentage_remaining < 100)
+ batt_info->status = CS_CHARGING;
+ else
+ batt_info->status = CS_DISCHARGING;
+#elif defined(__OpenBSD__)
+ /*
+ * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
+ * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
+ * probing acpi(4) devices.
+ */
+ struct apm_power_info apm_info;
+ int apm_fd;
+
+ apm_fd = open("/dev/apm", O_RDONLY);
+ if (apm_fd < 0) {
+ OUTPUT_FULL_TEXT("can't open /dev/apm");
+ return false;
+ }
+ if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
+ OUTPUT_FULL_TEXT("can't read power info");
+
+ close(apm_fd);
+
+ /* Don't bother to go further if there's no battery present. */
+ if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
+ (apm_info.battery_state == APM_BATT_UNKNOWN)) {
+ OUTPUT_FULL_TEXT(format_down);
+ return false;
+ }
+
+ switch (apm_info.ac_state) {
+ case APM_AC_OFF:
+ batt_info->status = CS_DISCHARGING;
+ break;
+ case APM_AC_ON:
+ batt_info->status = CS_CHARGING;
+ break;
+ default:
+ /* If we don't know what's going on, just assume we're discharging. */
+ batt_info->status = CS_DISCHARGING;
+ break;
+ }
+
+ batt_info->percentage_remaining = apm_info.battery_life;
+
+ /* Can't give a meaningful value for remaining minutes if we're charging. */
+ if (batt_info->status != CS_CHARGING) {
+ batt_info->seconds_remaining = apm_info.minutes_left * 60;
+ }
+#elif defined(__NetBSD__)
+ /*
+ * Using envsys(4) via sysmon(4).
+ */
+ int fd, rval;
+ bool is_found = false;
+ char sensor_desc[16];
+
+ prop_dictionary_t dict;
+ prop_array_t array;
+ prop_object_iterator_t iter;
+ prop_object_iterator_t iter2;
+ prop_object_t obj, obj2, obj3, obj4, obj5;
+
+ if (number >= 0)
+ (void)snprintf(sensor_desc, sizeof(sensor_desc), "acpibat%d", number);
+
+ fd = open("/dev/sysmon", O_RDONLY);
+ if (fd < 0) {
+ OUTPUT_FULL_TEXT("can't open /dev/sysmon");
+ return false;
+ }
+
+ rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
+ if (rval == -1) {
+ close(fd);
+ return false;
+ }
+
+ if (prop_dictionary_count(dict) == 0) {
+ prop_object_release(dict);
+ close(fd);
+ return false;
+ }
+
+ iter = prop_dictionary_iterator(dict);
+ if (iter == NULL) {
+ prop_object_release(dict);
+ close(fd);
+ }
+
+ /* iterate over the dictionary returned by the kernel */
+ while ((obj = prop_object_iterator_next(iter)) != NULL) {
+ /* skip this dict if it's not what we're looking for */
+ if (number < 0) {
+ /* we want all batteries */
+ if (!BEGINS_WITH(prop_dictionary_keysym_cstring_nocopy(obj),
+ "acpibat"))
+ continue;
+ } else {
+ /* we want a specific battery */
+ if (strcmp(sensor_desc,
+ prop_dictionary_keysym_cstring_nocopy(obj)) != 0)
+ continue;