mirror of https://github.com/xzeldon/htop.git
Properly handle multiple batteries on darwin
This makes the behaviour consistent with other platforms where AC is marked as present if at least one power source is marked as AC_PRESENT. Fixes: #711
This commit is contained in:
parent
44e01dd32b
commit
e341217fea
|
@ -347,67 +347,57 @@ bool Platform_getNetworkIO(NetworkIOData* data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform_getBattery(double* percent, ACPresence* isOnAC) {
|
void Platform_getBattery(double* percent, ACPresence* isOnAC) {
|
||||||
CFTypeRef power_sources = IOPSCopyPowerSourcesInfo();
|
|
||||||
|
|
||||||
*percent = NAN;
|
*percent = NAN;
|
||||||
*isOnAC = AC_ERROR;
|
*isOnAC = AC_ERROR;
|
||||||
|
|
||||||
if (NULL == power_sources)
|
CFArrayRef list = NULL;
|
||||||
return;
|
|
||||||
|
|
||||||
CFArrayRef list = IOPSCopyPowerSourcesList(power_sources);
|
CFTypeRef power_sources = IOPSCopyPowerSourcesInfo();
|
||||||
CFDictionaryRef battery = NULL;
|
if (!power_sources)
|
||||||
int len;
|
goto cleanup;
|
||||||
|
|
||||||
if (NULL == list) {
|
list = IOPSCopyPowerSourcesList(power_sources);
|
||||||
CFRelease(power_sources);
|
if (!list)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
return;
|
double cap_current = 0.0;
|
||||||
}
|
double cap_max = 0.0;
|
||||||
|
|
||||||
len = CFArrayGetCount(list);
|
|
||||||
|
|
||||||
/* Get the battery */
|
/* Get the battery */
|
||||||
for (int i = 0; i < len && battery == NULL; ++i) {
|
for (int i = 0, len = CFArrayGetCount(list); i < len; ++i) {
|
||||||
CFDictionaryRef candidate = IOPSGetPowerSourceDescription(power_sources,
|
CFDictionaryRef power_source = IOPSGetPowerSourceDescription(power_sources, CFArrayGetValueAtIndex(list, i)); /* GET rule */
|
||||||
CFArrayGetValueAtIndex(list, i)); /* GET rule */
|
|
||||||
CFStringRef type;
|
|
||||||
|
|
||||||
if (NULL != candidate) {
|
if (!power_source)
|
||||||
type = (CFStringRef) CFDictionaryGetValue(candidate,
|
continue;
|
||||||
CFSTR(kIOPSTransportTypeKey)); /* GET rule */
|
|
||||||
|
|
||||||
if (kCFCompareEqualTo == CFStringCompare(type, CFSTR(kIOPSInternalType), 0)) {
|
CFStringRef power_type = CFDictionaryGetValue(power_source, CFSTR(kIOPSTransportTypeKey)); /* GET rule */
|
||||||
CFRetain(candidate);
|
|
||||||
battery = candidate;
|
if (kCFCompareEqualTo != CFStringCompare(power_type, CFSTR(kIOPSInternalType), 0))
|
||||||
}
|
continue;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (NULL != battery) {
|
|
||||||
/* Determine the AC state */
|
/* Determine the AC state */
|
||||||
CFStringRef power_state = CFDictionaryGetValue(battery, CFSTR(kIOPSPowerSourceStateKey));
|
CFStringRef power_state = CFDictionaryGetValue(power_source, CFSTR(kIOPSPowerSourceStateKey));
|
||||||
|
|
||||||
*isOnAC = (kCFCompareEqualTo == CFStringCompare(power_state, CFSTR(kIOPSACPowerValue), 0))
|
if (*isOnAC != AC_PRESENT)
|
||||||
? AC_PRESENT
|
*isOnAC = (kCFCompareEqualTo == CFStringCompare(power_state, CFSTR(kIOPSACPowerValue), 0)) ? AC_PRESENT : AC_ABSENT;
|
||||||
: AC_ABSENT;
|
|
||||||
|
|
||||||
/* Get the percentage remaining */
|
/* Get the percentage remaining */
|
||||||
double current;
|
double tmp;
|
||||||
double max;
|
CFNumberGetValue(CFDictionaryGetValue(power_source, CFSTR(kIOPSCurrentCapacityKey)), kCFNumberDoubleType, &tmp);
|
||||||
|
cap_current += tmp;
|
||||||
CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSCurrentCapacityKey)),
|
CFNumberGetValue(CFDictionaryGetValue(power_source, CFSTR(kIOPSMaxCapacityKey)), kCFNumberDoubleType, &tmp);
|
||||||
kCFNumberDoubleType, ¤t);
|
cap_max += tmp;
|
||||||
CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSMaxCapacityKey)),
|
|
||||||
kCFNumberDoubleType, &max);
|
|
||||||
|
|
||||||
*percent = (current * 100.0) / max;
|
|
||||||
|
|
||||||
CFRelease(battery);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CFRelease(list);
|
if (cap_max > 0.0)
|
||||||
CFRelease(power_sources);
|
*percent = 100.0 * cap_current / cap_max;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (list)
|
||||||
|
CFRelease(list);
|
||||||
|
|
||||||
|
if (power_sources)
|
||||||
|
CFRelease(power_sources);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform_gettime_monotonic(uint64_t* msec) {
|
void Platform_gettime_monotonic(uint64_t* msec) {
|
||||||
|
|
Loading…
Reference in New Issue