Battery: add dirty support for macOS

This commit is contained in:
李通洲
2022-09-16 17:41:24 +08:00
committed by Carter Li
parent 9eeb216b70
commit 413d126bcc
4 changed files with 124 additions and 3 deletions
+1
View File
@@ -243,6 +243,7 @@ if(APPLE)
src/detection/battery/battery_apple.c
src/detection/memory/memory_apple.c
src/detection/displayserver/displayserver_apple.c
src/util/apple/cfdict_helpers.c
)
elseif(ANDROID)
list(APPEND LIBFASTFETCH_SRC
+64 -3
View File
@@ -1,7 +1,68 @@
#include "fastfetch.h"
#include "battery.h"
#include "util/apple/cfdict_helpers.h"
const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) {
FF_UNUSED(instance, results);
return "Unimplemented";
#include <IOKit/IOKitLib.h>
const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results)
{
FF_UNUSED(instance);
CFMutableDictionaryRef matchDict = IOServiceMatching("AppleSmartBattery");
if (matchDict == NULL)
return "IOServiceMatching(\"AppleSmartBattery\") failed";
io_iterator_t iterator;
if(IOServiceGetMatchingServices(0, matchDict, &iterator) != kIOReturnSuccess)
return "IOServiceGetMatchingServices() failed";
io_registry_entry_t registryEntry;
while((registryEntry = IOIteratorNext(iterator)) != 0)
{
CFMutableDictionaryRef properties;
if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
{
IOObjectRelease(registryEntry);
continue;
}
int intValue;
bool boolValue;
BatteryResult* battery = ffListAdd(results);
ffStrbufInit(&battery->capacity);
if(ffCfDictGetInt(properties, "CurrentCapacity", &intValue))
ffStrbufAppendF(&battery->capacity, "%d", intValue);
ffStrbufInit(&battery->manufacturer);
ffStrbufInit(&battery->modelName);
ffStrbufInit(&battery->technology);
if (ffCfDictGetBool(properties, "built-in", &boolValue) && boolValue)
{
ffStrbufAppendS(&battery->manufacturer, "Apple Inc.");
ffStrbufAppendS(&battery->modelName, "Builtin");
ffStrbufAppendS(&battery->technology, "Lithium");
}
else
{
ffStrbufAppendS(&battery->manufacturer, "Unknown");
ffStrbufAppendS(&battery->modelName, "Unknown");
ffStrbufAppendS(&battery->technology, "Unknown");
}
ffStrbufInit(&battery->status);
if (ffCfDictGetBool(properties, "FullyCharged", &boolValue) && boolValue)
ffStrbufAppendS(&battery->status, "Fully charged");
else if (ffCfDictGetBool(properties, "IsCharging", &boolValue) && boolValue)
ffStrbufAppendS(&battery->status, "Charging");
else
ffStrbufAppendS(&battery->status, "");
CFRelease(properties);
IOObjectRelease(registryEntry);
}
IOObjectRelease(iterator);
return NULL;
}
+45
View File
@@ -0,0 +1,45 @@
#include "cfdict_helpers.h"
const void* ffCfDictGetValue(CFMutableDictionaryRef dict, const char* key)
{
CFStringRef cfKey = CFStringCreateWithCStringNoCopy(NULL, key, kCFStringEncodingASCII, kCFAllocatorNull);
return CFDictionaryGetValue(dict, cfKey);
}
bool ffCfDictGetString(CFMutableDictionaryRef dict, const char* key, FFstrbuf* result)
{
CFStringRef cf = (CFStringRef)ffCfDictGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFStringGetTypeID())
return false;
uint32_t length = (uint32_t)CFStringGetLength(cf);
ffStrbufEnsureFree(result, length + 1);
if(CFStringGetCString(cf, result->chars, length + 1, kCFStringEncodingASCII))
{
result->length = length;
// CFStringGetCString ensures the buffer is NUL terminated
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
}
return true;
}
bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result)
{
CFBooleanRef cf = (CFBooleanRef)ffCfDictGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID())
return false;
*result = CFBooleanGetValue(cf);
return true;
}
bool ffCfDictGetInt(CFMutableDictionaryRef dict, const char* key, int* result)
{
CFNumberRef cf = (CFNumberRef)ffCfDictGetValue(dict, key);
if (cf == NULL || CFGetTypeID(cf) != CFStringGetTypeID())
return false;
if(!CFNumberGetValue(cf, kCFNumberSInt32Type, result))
return false;
return true;
}
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#ifndef FASTFETCH_INCLUDED_cfdict_helpers
#define FASTFETCH_INCLUDED_cfdict_helpers
#include "fastfetch.h"
#include <CoreFoundation/CoreFoundation.h>
const void* ffCfDictGetValue(CFMutableDictionaryRef dict, const char* str);
bool ffCfDictGetString(CFMutableDictionaryRef dict, const char* key, FFstrbuf* result);
bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result);
bool ffCfDictGetInt(CFMutableDictionaryRef dict, const char* key, int* result);
#endif