Skip to content

Commit f0f9d94

Browse files
committed
Retrieve battery charge information on Darwin
1 parent 477f50b commit f0f9d94

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

darwin/Platform.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,29 @@ void Platform_getBattery(BatteryInfo* info) {
736736
info->energyFull = cap_max;
737737
}
738738

739+
io_service_t batt = IOServiceGetMatchingService(iokit_port, IOServiceMatching("AppleSmartBattery"));
740+
if (batt) {
741+
CFNumberRef ampRef = IORegistryEntryCreateCFProperty(batt, CFSTR("Amperage"), kCFAllocatorDefault, 0);
742+
CFNumberRef voltRef = IORegistryEntryCreateCFProperty(batt, CFSTR("Voltage"), kCFAllocatorDefault, 0);
743+
744+
if (ampRef && voltRef) {
745+
double ampMA = 0.0;
746+
CFNumberGetValue(ampRef, kCFNumberDoubleType, &ampMA);
747+
748+
double voltMV = 0.0;
749+
CFNumberGetValue(voltRef, kCFNumberDoubleType, &voltMV);
750+
751+
info->powerCurr = ampMA * voltMV / 1e6;
752+
}
753+
754+
if (ampRef)
755+
CFRelease(ampRef);
756+
if (voltRef)
757+
CFRelease(voltRef);
758+
759+
IOObjectRelease(batt);
760+
}
761+
739762
cleanup:
740763
if (list)
741764
CFRelease(list);

0 commit comments

Comments
 (0)