|
1 | 1 | #include "fastfetch.h" |
2 | 2 | #include "battery.h" |
| 3 | +#include "util/apple/cfdict_helpers.h" |
3 | 4 |
|
4 | | -const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) { |
5 | | - FF_UNUSED(instance, results); |
6 | | - return "Unimplemented"; |
| 5 | +#include <IOKit/IOKitLib.h> |
| 6 | + |
| 7 | +const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) |
| 8 | +{ |
| 9 | + FF_UNUSED(instance); |
| 10 | + |
| 11 | + CFMutableDictionaryRef matchDict = IOServiceMatching("AppleSmartBattery"); |
| 12 | + if (matchDict == NULL) |
| 13 | + return "IOServiceMatching(\"AppleSmartBattery\") failed"; |
| 14 | + |
| 15 | + io_iterator_t iterator; |
| 16 | + if(IOServiceGetMatchingServices(0, matchDict, &iterator) != kIOReturnSuccess) |
| 17 | + return "IOServiceGetMatchingServices() failed"; |
| 18 | + |
| 19 | + io_registry_entry_t registryEntry; |
| 20 | + while((registryEntry = IOIteratorNext(iterator)) != 0) |
| 21 | + { |
| 22 | + CFMutableDictionaryRef properties; |
| 23 | + if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess) |
| 24 | + { |
| 25 | + IOObjectRelease(registryEntry); |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + int intValue; |
| 30 | + bool boolValue; |
| 31 | + |
| 32 | + BatteryResult* battery = ffListAdd(results); |
| 33 | + ffStrbufInit(&battery->capacity); |
| 34 | + if(ffCfDictGetInt(properties, "CurrentCapacity", &intValue)) |
| 35 | + ffStrbufAppendF(&battery->capacity, "%d", intValue); |
| 36 | + |
| 37 | + ffStrbufInit(&battery->manufacturer); |
| 38 | + ffStrbufInit(&battery->modelName); |
| 39 | + ffStrbufInit(&battery->technology); |
| 40 | + if (ffCfDictGetBool(properties, "built-in", &boolValue) && boolValue) |
| 41 | + { |
| 42 | + ffStrbufAppendS(&battery->manufacturer, "Apple Inc."); |
| 43 | + ffStrbufAppendS(&battery->modelName, "Builtin"); |
| 44 | + ffStrbufAppendS(&battery->technology, "Lithium"); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + ffStrbufAppendS(&battery->manufacturer, "Unknown"); |
| 49 | + ffStrbufAppendS(&battery->modelName, "Unknown"); |
| 50 | + ffStrbufAppendS(&battery->technology, "Unknown"); |
| 51 | + } |
| 52 | + |
| 53 | + ffStrbufInit(&battery->status); |
| 54 | + if (ffCfDictGetBool(properties, "FullyCharged", &boolValue) && boolValue) |
| 55 | + ffStrbufAppendS(&battery->status, "Fully charged"); |
| 56 | + else if (ffCfDictGetBool(properties, "IsCharging", &boolValue) && boolValue) |
| 57 | + ffStrbufAppendS(&battery->status, "Charging"); |
| 58 | + else |
| 59 | + ffStrbufAppendS(&battery->status, ""); |
| 60 | + |
| 61 | + CFRelease(properties); |
| 62 | + IOObjectRelease(registryEntry); |
| 63 | + } |
| 64 | + |
| 65 | + IOObjectRelease(iterator); |
| 66 | + |
| 67 | + return NULL; |
7 | 68 | } |
0 commit comments