+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
+double dict_get_double (CFDictionaryRef dict, char *key_string)
+{
+ double val_double;
+ long long val_int;
+ CFNumberRef val_obj;
+ CFStringRef key_obj;
+
+ key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
+ kCFStringEncodingASCII);
+ if (key_obj == NULL)
+ {
+ DBG ("CFStringCreateWithCString (%s) failed.\n", key_string);
+ return (INVALID_VALUE);
+ }
+
+ if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
+ {
+ DBG ("CFDictionaryGetValue (%s) failed.", key_string);
+ CFRelease (key_obj);
+ return (INVALID_VALUE);
+ }
+ CFRelease (key_obj);
+
+ if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
+ {
+ if (CFNumberIsFloatType (val_obj))
+ {
+ CFNumberGetValue (val_obj,
+ kCFNumberDoubleType,
+ &val_double);
+ }
+ else
+ {
+ CFNumberGetValue (val_obj,
+ kCFNumberLongLongType,
+ &val_int);
+ val_double = val_int;
+ }
+ }
+ else
+ {
+ DBG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
+ return (INVALID_VALUE);
+ }
+
+ return (val_double);
+}
+#endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
+
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H
+static void get_via_io_power_sources (double *ret_charge,
+ double *ret_current,
+ double *ret_voltage)
+{
+ CFTypeRef ps_raw;
+ CFArrayRef ps_array;
+ int ps_array_len;
+ CFDictionaryRef ps_dict;
+ CFTypeRef ps_obj;
+
+ double temp_double;
+ int i;
+
+ ps_raw = IOPSCopyPowerSourcesInfo ();
+ ps_array = IOPSCopyPowerSourcesList (ps_raw);
+ ps_array_len = CFArrayGetCount (ps_array);
+
+ DBG ("ps_array_len == %i", ps_array_len);
+
+ for (i = 0; i < ps_array_len; i++)
+ {
+ ps_obj = CFArrayGetValueAtIndex (ps_array, i);
+ ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
+
+ if (ps_dict == NULL)
+ {
+ DBG ("IOPSGetPowerSourceDescription failed.");
+ continue;
+ }
+
+ if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
+ {
+ DBG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
+ continue;
+ }
+
+ /* FIXME: Check if this is really an internal battery */
+
+ if (*ret_charge == INVALID_VALUE)
+ {
+ /* This is the charge in percent. */
+ temp_double = dict_get_double (ps_dict,
+ kIOPSCurrentCapacityKey);
+ if ((temp_double != INVALID_VALUE)
+ && (temp_double >= 0.0)
+ && (temp_double <= 100.0))
+ *ret_charge = temp_double;
+ }
+
+ if (*ret_current == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (ps_dict,
+ kIOPSCurrentKey);
+ if (temp_double != INVALID_VALUE)
+ *ret_current = temp_double / 1000.0;
+ }
+
+ if (*ret_voltage == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (ps_dict,
+ kIOPSVoltageKey);
+ if (temp_double != INVALID_VALUE)
+ *ret_voltage = temp_double / 1000.0;
+ }
+ }
+
+ CFRelease(ps_array);
+ CFRelease(ps_raw);
+}
+#endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+
+#if HAVE_IOKIT_IOKITLIB_H
+static void get_via_generic_iokit (double *ret_charge,
+ double *ret_current,
+ double *ret_voltage)
+{
+ kern_return_t status;
+ io_iterator_t iterator;
+ io_object_t io_obj;
+
+ CFDictionaryRef bat_root_dict;
+ CFArrayRef bat_info_arry;
+ CFIndex bat_info_arry_len;
+ CFIndex bat_info_arry_pos;
+ CFDictionaryRef bat_info_dict;
+
+ double temp_double;
+
+ status = IOServiceGetMatchingServices (kIOMasterPortDefault,
+ IOServiceNameMatching ("battery"),
+ &iterator);
+ if (status != kIOReturnSuccess)
+ {
+ DBG ("IOServiceGetMatchingServices failed.");
+ return;
+ }
+
+ while ((io_obj = IOIteratorNext (iterator)))
+ {
+ status = IORegistryEntryCreateCFProperties (io_obj,
+ (CFMutableDictionaryRef *) &bat_root_dict,
+ kCFAllocatorDefault,
+ kNilOptions);
+ if (status != kIOReturnSuccess)
+ {
+ DBG ("IORegistryEntryCreateCFProperties failed.");
+ continue;
+ }
+
+ bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
+ CFSTR ("IOBatteryInfo"));
+ if (bat_info_arry == NULL)
+ {
+ CFRelease (bat_root_dict);
+ continue;
+ }
+ bat_info_arry_len = CFArrayGetCount (bat_info_arry);
+
+ for (bat_info_arry_pos = 0;
+ bat_info_arry_pos < bat_info_arry_len;
+ bat_info_arry_pos++)
+ {
+ bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
+
+ if (*ret_charge == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (bat_info_dict,
+ "Capacity");
+ if (temp_double != INVALID_VALUE)
+ *ret_charge = temp_double / 1000.0;
+ }
+
+ if (*ret_current == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (bat_info_dict,
+ "Current");
+ if (temp_double != INVALID_VALUE)
+ *ret_current = temp_double / 1000.0;
+ }
+
+ if (*ret_voltage == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (bat_info_dict,
+ "Voltage");
+ if (temp_double != INVALID_VALUE)
+ *ret_voltage = temp_double / 1000.0;
+ }
+ }
+
+ CFRelease (bat_root_dict);
+ }
+
+ IOObjectRelease (iterator);
+}
+#endif /* HAVE_IOKIT_IOKITLIB_H */
+