- if (present_rate > 0) {
- float remaining_time;
- int seconds, hours, minutes;
- if (status == CS_CHARGING)
- remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
- else if (status == CS_DISCHARGING)
- remaining_time = ((float)remaining / (float)present_rate);
- else remaining_time = 0;
-
- seconds = (int)(remaining_time * 3600.0);
- hours = seconds / 3600;
- seconds -= (hours * 3600);
- minutes = seconds / 60;
- seconds -= (minutes * 60);
+ /* 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 != -1) {
+ batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0));
+ batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0));
+ batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.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;