From 9e4a55d2852f158ea78cb7f00e7e54ecee57c3c4 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 17 Sep 2022 00:33:00 +0800 Subject: [PATCH 1/7] util/cfdict_helpers: fix stupid copy & paste errors --- src/util/apple/cfdict_helpers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/apple/cfdict_helpers.c b/src/util/apple/cfdict_helpers.c index 9da7ca917f..e7e4020ace 100644 --- a/src/util/apple/cfdict_helpers.c +++ b/src/util/apple/cfdict_helpers.c @@ -26,7 +26,7 @@ bool ffCfDictGetString(CFMutableDictionaryRef dict, const char* key, FFstrbuf* r bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result) { CFBooleanRef cf = (CFBooleanRef)ffCfDictGetValue(dict, key); - if(cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID()) + if(cf == NULL || CFGetTypeID(cf) != CFBooleanGetTypeID()) return false; *result = CFBooleanGetValue(cf); @@ -36,7 +36,7 @@ bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result) bool ffCfDictGetInt(CFMutableDictionaryRef dict, const char* key, int* result) { CFNumberRef cf = (CFNumberRef)ffCfDictGetValue(dict, key); - if (cf == NULL || CFGetTypeID(cf) != CFStringGetTypeID()) + if (cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID()) return false; if(!CFNumberGetValue(cf, kCFNumberSInt32Type, result)) From d5500ad4f72d9e27276136db2412d146c628de02 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 17 Sep 2022 00:34:37 +0800 Subject: [PATCH 2/7] Battery: fix Intel compatibility `MaxCapacity` on Intel MBP is not 100 --- src/detection/battery/battery_apple.c | 6 +++--- src/detection/battery/battery_linux.c | 5 +---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/detection/battery/battery_apple.c b/src/detection/battery/battery_apple.c index 53a2431e5a..9f3e3e3717 100644 --- a/src/detection/battery/battery_apple.c +++ b/src/detection/battery/battery_apple.c @@ -26,13 +26,13 @@ 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, "CurrentCapacity", ¤tCapacity) && ffCfDictGetInt(properties, "MaxCapacity", &maxCapacity)) + ffStrbufAppendF(&battery->capacity, "%.0f", currentCapacity * 100.0 / maxCapacity); ffStrbufInit(&battery->manufacturer); ffStrbufInit(&battery->modelName); diff --git a/src/detection/battery/battery_linux.c b/src/detection/battery/battery_linux.c index db6aee1629..909ed493f0 100644 --- a/src/detection/battery/battery_linux.c +++ b/src/detection/battery/battery_linux.c @@ -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 { From bb3689b3487ef19c606ba5ed8388eb7efed0420c Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 17 Sep 2022 00:35:07 +0800 Subject: [PATCH 3/7] GPU: fix Intel compatibility --- src/detection/gpu/gpu_apple.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/detection/gpu/gpu_apple.c b/src/detection/gpu/gpu_apple.c index b8a2b34e69..b0388c2349 100644 --- a/src/detection/gpu/gpu_apple.c +++ b/src/detection/gpu/gpu_apple.c @@ -26,17 +26,20 @@ 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); + 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, "model", &gpu->name) && gpu->driver.length > 0) + ffStrbufAppendS(&gpu->name, gpu->driver.chars + ffStrbufLastIndexC(&gpu->driver, '.') + 1); + + if(!ffCfDictGetInt(properties, "gpu-core-count", &gpu->coreCount)) + gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; + gpu->temperature = FF_GPU_TEMP_UNSET; CFRelease(properties); From 3d4ce078923cd9efe98d884a3b6b1fc0f8c473ed Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 17 Sep 2022 01:47:56 +0800 Subject: [PATCH 4/7] Vulkan: set `gpu->coreCount` --- src/detection/vulkan.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/detection/vulkan.c b/src/detection/vulkan.c index 108101174b..944a5c8edf 100644 --- a/src/detection/vulkan.c +++ b/src/detection/vulkan.c @@ -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; } From d46f9ab4dcfddcdd81251fad94ccd67fa3305316 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 17 Sep 2022 02:17:27 +0800 Subject: [PATCH 5/7] util/cfdict_helpers: code improvements 1. simplify CFString usage 2. support get string from CFDataRef --- src/detection/battery/battery_apple.c | 9 ++--- src/detection/gpu/gpu_apple.c | 6 ++-- src/util/apple/cfdict_helpers.c | 49 ++++++++++++++++----------- src/util/apple/cfdict_helpers.h | 7 ++-- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/detection/battery/battery_apple.c b/src/detection/battery/battery_apple.c index 9f3e3e3717..f3d98c071e 100644 --- a/src/detection/battery/battery_apple.c +++ b/src/detection/battery/battery_apple.c @@ -31,13 +31,14 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) BatteryResult* battery = ffListAdd(results); ffStrbufInit(&battery->capacity); int currentCapacity, maxCapacity; - if(ffCfDictGetInt(properties, "CurrentCapacity", ¤tCapacity) && ffCfDictGetInt(properties, "MaxCapacity", &maxCapacity)) + if(ffCfDictGetInt(properties, CFSTR("CurrentCapacity"), ¤tCapacity) && + 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"); @@ -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, ""); diff --git a/src/detection/gpu/gpu_apple.c b/src/detection/gpu/gpu_apple.c index b0388c2349..e7e58116b1 100644 --- a/src/detection/gpu/gpu_apple.c +++ b/src/detection/gpu/gpu_apple.c @@ -29,15 +29,15 @@ const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance) 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, "model", &gpu->name) && gpu->driver.length > 0) + if(!ffCfDictGetString(properties, CFSTR("model"), &gpu->name) && gpu->driver.length > 0) ffStrbufAppendS(&gpu->name, gpu->driver.chars + ffStrbufLastIndexC(&gpu->driver, '.') + 1); - if(!ffCfDictGetInt(properties, "gpu-core-count", &gpu->coreCount)) + if(!ffCfDictGetInt(properties, CFSTR("gpu-core-count"), &gpu->coreCount)) gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; gpu->temperature = FF_GPU_TEMP_UNSET; diff --git a/src/util/apple/cfdict_helpers.c b/src/util/apple/cfdict_helpers.c index e7e4020ace..f03c1dcc45 100644 --- a/src/util/apple/cfdict_helpers.c +++ b/src/util/apple/cfdict_helpers.c @@ -1,31 +1,42 @@ #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); + CFBooleanRef cf = (CFBooleanRef)CFDictionaryGetValue(dict, key); if(cf == NULL || CFGetTypeID(cf) != CFBooleanGetTypeID()) return false; @@ -33,9 +44,9 @@ bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result) 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); + CFNumberRef cf = (CFNumberRef)CFDictionaryGetValue(dict, key); if (cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID()) return false; diff --git a/src/util/apple/cfdict_helpers.h b/src/util/apple/cfdict_helpers.h index a00522e740..cfb1e3c81d 100644 --- a/src/util/apple/cfdict_helpers.h +++ b/src/util/apple/cfdict_helpers.h @@ -6,9 +6,8 @@ #include "fastfetch.h" #include -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 From 4d569db0fd279af762791ba895c50ec71e5a6901 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 17 Sep 2022 02:58:32 +0800 Subject: [PATCH 6/7] refactor: use MACH_PORT_NULL instead of magic number 0 --- src/detection/battery/battery_apple.c | 2 +- src/detection/gpu/gpu_apple.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/detection/battery/battery_apple.c b/src/detection/battery/battery_apple.c index f3d98c071e..3af71bf56e 100644 --- a/src/detection/battery/battery_apple.c +++ b/src/detection/battery/battery_apple.c @@ -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; diff --git a/src/detection/gpu/gpu_apple.c b/src/detection/gpu/gpu_apple.c index e7e58116b1..745704457b 100644 --- a/src/detection/gpu/gpu_apple.c +++ b/src/detection/gpu/gpu_apple.c @@ -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; From 45d6f2140203ed832c8e059d24e7188927af56b0 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 17 Sep 2022 02:59:07 +0800 Subject: [PATCH 7/7] GPU: better support Intel GPUs --- src/detection/gpu/gpu_apple.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/detection/gpu/gpu_apple.c b/src/detection/gpu/gpu_apple.c index 745704457b..e756b9467f 100644 --- a/src/detection/gpu/gpu_apple.c +++ b/src/detection/gpu/gpu_apple.c @@ -34,8 +34,20 @@ const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance) 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) && gpu->driver.length > 0) - ffStrbufAppendS(&gpu->name, gpu->driver.chars + ffStrbufLastIndexC(&gpu->driver, '.') + 1); + 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;