Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/detection/battery/battery_apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results)
return "IOServiceMatching(\"AppleSmartBattery\") failed";

io_iterator_t iterator;
if(IOServiceGetMatchingServices(0, matchDict, &iterator) != kIOReturnSuccess)
if(IOServiceGetMatchingServices(MACH_PORT_NULL, matchDict, &iterator) != kIOReturnSuccess)
return "IOServiceGetMatchingServices() failed";

io_registry_entry_t registryEntry;
Expand All @@ -26,18 +26,19 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results)
continue;
}

int intValue;
bool boolValue;

BatteryResult* battery = ffListAdd(results);
ffStrbufInit(&battery->capacity);
if(ffCfDictGetInt(properties, "CurrentCapacity", &intValue))
ffStrbufAppendF(&battery->capacity, "%d", intValue);
int currentCapacity, maxCapacity;
if(ffCfDictGetInt(properties, CFSTR("CurrentCapacity"), &currentCapacity) &&
ffCfDictGetInt(properties, CFSTR("MaxCapacity"), &maxCapacity))
ffStrbufAppendF(&battery->capacity, "%.0f", currentCapacity * 100.0 / maxCapacity);

ffStrbufInit(&battery->manufacturer);
ffStrbufInit(&battery->modelName);
ffStrbufInit(&battery->technology);
if (ffCfDictGetBool(properties, "built-in", &boolValue) && boolValue)
if (ffCfDictGetBool(properties, CFSTR("built-in"), &boolValue) && boolValue)
{
ffStrbufAppendS(&battery->manufacturer, "Apple Inc.");
ffStrbufAppendS(&battery->modelName, "Builtin");
Expand All @@ -51,9 +52,9 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results)
}

ffStrbufInit(&battery->status);
if (ffCfDictGetBool(properties, "FullyCharged", &boolValue) && boolValue)
if (ffCfDictGetBool(properties, CFSTR("FullyCharged"), &boolValue) && boolValue)
ffStrbufAppendS(&battery->status, "Fully charged");
else if (ffCfDictGetBool(properties, "IsCharging", &boolValue) && boolValue)
else if (ffCfDictGetBool(properties, CFSTR("IsCharging"), &boolValue) && boolValue)
ffStrbufAppendS(&battery->status, "Charging");
else
ffStrbufAppendS(&battery->status, "");
Expand Down
5 changes: 1 addition & 4 deletions src/detection/battery/battery_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) {
if(instance->config.batteryDir.length > 0)
{
ffStrbufAppend(&baseDir, &instance->config.batteryDir);
if (!ffStrbufEndsWithC(&baseDir, '/'))
{
ffStrbufAppendC(&baseDir, '/');
}
ffStrbufEnsureEndsWithC(&baseDir, '/');
}
else
{
Expand Down
31 changes: 23 additions & 8 deletions src/detection/gpu/gpu_apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)

CFMutableDictionaryRef matchDict = IOServiceMatching(kIOAcceleratorClassName);
io_iterator_t iterator;
if(IOServiceGetMatchingServices(0, matchDict, &iterator) != kIOReturnSuccess)
if(IOServiceGetMatchingServices(MACH_PORT_NULL, matchDict, &iterator) != kIOReturnSuccess)
return "IOServiceGetMatchingServices() failed";

io_registry_entry_t registryEntry;
Expand All @@ -26,16 +26,31 @@ const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)

FFGPUResult* gpu = ffListAdd(gpus);

ffStrbufInit(&gpu->name);
ffCfDictGetString(properties, "model", &gpu->name);

if (!ffCfDictGetInt(properties, "gpu-core-count", &gpu->coreCount))
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;

ffStrbufInitA(&gpu->vendor, 0);

ffStrbufInit(&gpu->driver);
ffCfDictGetString(properties, "CFBundleIdentifier", &gpu->driver);
ffCfDictGetString(properties, CFSTR("CFBundleIdentifier"), &gpu->driver);

ffStrbufInit(&gpu->name);
//IOAccelerator returns model property for Apple Silicon, but not for Intel Iris GPUs.
//Still needs testing for AMD's
if(!ffCfDictGetString(properties, CFSTR("model"), &gpu->name))
{
CFRelease(properties);

io_registry_entry_t parentEntry;
IORegistryEntryGetParentEntry(registryEntry, kIOServicePlane, &parentEntry);
if(IORegistryEntryCreateCFProperties(parentEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
{
IOObjectRelease(parentEntry);
IOObjectRelease(registryEntry);
continue;
}
ffCfDictGetString(properties, CFSTR("model"), &gpu->name);
}

if(!ffCfDictGetInt(properties, CFSTR("gpu-core-count"), &gpu->coreCount))
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;

gpu->temperature = FF_GPU_TEMP_UNSET;

Expand Down
1 change: 1 addition & 0 deletions src/detection/vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ static const char* detectVulkan(const FFinstance* instance, FFVulkanResult* resu
//No way to detect those using vulkan
ffStrbufInit(&gpu->vendor);
ffStrbufInit(&gpu->driver);
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
}

Expand Down
53 changes: 32 additions & 21 deletions src/util/apple/cfdict_helpers.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
#include "cfdict_helpers.h"

const void* ffCfDictGetValue(CFMutableDictionaryRef dict, const char* key)
bool ffCfDictGetString(CFMutableDictionaryRef dict, CFStringRef key, FFstrbuf* result)
{
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())
CFTypeRef cf = (CFTypeRef)CFDictionaryGetValue(dict, key);
if(cf == NULL)
return false;

uint32_t length = (uint32_t)CFStringGetLength(cf);
ffStrbufEnsureFree(result, length + 1);
if(CFStringGetCString(cf, result->chars, length + 1, kCFStringEncodingASCII))
if(CFGetTypeID(cf) == CFStringGetTypeID())
{
CFStringRef cfStr = (CFStringRef)cf;
uint32_t length = (uint32_t)CFStringGetLength(cfStr);
ffStrbufEnsureFree(result, length + 1);
if(CFStringGetCString(cfStr, result->chars, length + 1, kCFStringEncodingASCII))
{
result->length = length;
// CFStringGetCString ensures the buffer is NUL terminated
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
}
}
else if(CFGetTypeID(cf) == CFDataGetTypeID())
{
CFDataRef cfData = (CFDataRef)cf;
uint32_t length = (uint32_t)CFDataGetLength(cfData);
ffStrbufEnsureFree(result, length + 1);
CFDataGetBytes(cfData, CFRangeMake(0, length), (uint8_t*)result->chars);
result->length = (uint32_t)strnlen(result->chars, length);
result->chars[result->length] = '\0';
}
else
{
result->length = length;
// CFStringGetCString ensures the buffer is NUL terminated
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
return false;
}
return true;
}

bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result)
bool ffCfDictGetBool(CFMutableDictionaryRef dict, CFStringRef key, bool* result)
{
CFBooleanRef cf = (CFBooleanRef)ffCfDictGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID())
CFBooleanRef cf = (CFBooleanRef)CFDictionaryGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFBooleanGetTypeID())
return false;

*result = CFBooleanGetValue(cf);
return true;
}

bool ffCfDictGetInt(CFMutableDictionaryRef dict, const char* key, int* result)
bool ffCfDictGetInt(CFMutableDictionaryRef dict, CFStringRef key, int* result)
{
CFNumberRef cf = (CFNumberRef)ffCfDictGetValue(dict, key);
if (cf == NULL || CFGetTypeID(cf) != CFStringGetTypeID())
CFNumberRef cf = (CFNumberRef)CFDictionaryGetValue(dict, key);
if (cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID())
return false;

if(!CFNumberGetValue(cf, kCFNumberSInt32Type, result))
Expand Down
7 changes: 3 additions & 4 deletions src/util/apple/cfdict_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#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);
bool ffCfDictGetString(CFMutableDictionaryRef dict, CFStringRef key, FFstrbuf* result);
bool ffCfDictGetBool(CFMutableDictionaryRef dict, CFStringRef key, bool* result);
bool ffCfDictGetInt(CFMutableDictionaryRef dict, CFStringRef key, int* result);

#endif