+ 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;
+ }
+ OUTPUT_FULL_TEXT(format_down);
+ }
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+ 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;