From 758ddae3449b0a72bf95b7d7e6dc02cfddd66535 Mon Sep 17 00:00:00 2001 From: Shu Niu Date: Sun, 19 May 2024 09:31:08 +0800 Subject: [PATCH] macOS: Add support for old MacOS version (#936) * Add support for old MacOS version, fix-1. * Add support for old MacOS version, fix-2. * Add support for old MacOS version with fixing metal gpu api. * Update disk_bsd.c * Update disk_bsd.c * Update gpu_apple.m * Update monitor_apple.m * Update os_apple.m * Update wifi_apple.m * Update camera_apple.m * Update camera_apple.m --------- Co-authored-by: Carter Li --- src/detection/camera/camera_apple.m | 5 +++- src/detection/disk/disk_bsd.c | 4 ++++ .../displayserver/displayserver_apple.c | 18 +++++++++++++++ src/detection/font/font_apple.m | 4 ++++ src/detection/gpu/gpu_apple.m | 19 +++++++++++---- src/detection/monitor/monitor_apple.m | 23 ++++++++++++++++++- src/detection/os/os_apple.m | 3 +-- .../physicaldisk/physicaldisk_apple.c | 12 +++++++++- .../terminalshell/terminalshell_linux.c | 4 ++++ src/detection/wifi/wifi_apple.m | 6 ++--- 10 files changed, 86 insertions(+), 12 deletions(-) diff --git a/src/detection/camera/camera_apple.m b/src/detection/camera/camera_apple.m index 66e18a7d6..ab15fb99e 100644 --- a/src/detection/camera/camera_apple.m +++ b/src/detection/camera/camera_apple.m @@ -8,6 +8,7 @@ const char* ffDetectCamera(FFlist* result) { + #ifdef MAC_OS_X_VERSION_10_15 FF_SUPPRESS_IO(); // #822 AVCaptureDeviceType deviceType; @@ -47,6 +48,8 @@ const char* ffDetectCamera(FFlist* result) camera->width = size.width < 0 ? 0 : (uint32_t) size.width; camera->height = size.height < 0 ? 0 : (uint32_t) size.height; } - return NULL; + #else + return "No support for old MacOS version"; + #endif } diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index 446e2e610..3046a3577 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -66,6 +66,10 @@ static void detectFsInfo(struct statfs* fs, FFDisk* disk) #include #include +#ifndef MAC_OS_X_VERSION_10_15 + #define MNT_REMOVABLE 0x00000200 +#endif + struct CmnAttrBuf { uint32_t length; attrreference_t nameRef; diff --git a/src/detection/displayserver/displayserver_apple.c b/src/detection/displayserver/displayserver_apple.c index 3a63fb303..964a6981f 100644 --- a/src/detection/displayserver/displayserver_apple.c +++ b/src/detection/displayserver/displayserver_apple.c @@ -8,6 +8,10 @@ #include extern CFDictionaryRef CoreDisplay_DisplayCreateInfoDictionary(CGDirectDisplayID display) __attribute__((weak_import)); +#ifndef MAC_OS_X_VERSION_10_15 +#import +extern CFDictionaryRef CoreDisplay_IODisplayCreateInfoDictionary(io_service_t framebuffer, IOOptionBits options) __attribute__((weak_import)); +#endif static void detectDisplays(FFDisplayServerResult* ds) { @@ -38,6 +42,7 @@ static void detectDisplays(FFDisplayServerResult* ds) } FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate(); + #ifdef MAC_OS_X_VERSION_10_15 if(CoreDisplay_DisplayCreateInfoDictionary) { CFDictionaryRef FF_CFTYPE_AUTO_RELEASE displayInfo = CoreDisplay_DisplayCreateInfoDictionary(screen); @@ -48,6 +53,19 @@ static void detectDisplays(FFDisplayServerResult* ds) ffCfDictGetString(productNames, CFSTR("en_US"), &name); } } + #else + if(CoreDisplay_IODisplayCreateInfoDictionary) + { + io_service_t servicePort = CGDisplayIOServicePort(screen); + CFDictionaryRef FF_CFTYPE_AUTO_RELEASE displayInfo = CoreDisplay_IODisplayCreateInfoDictionary(servicePort, kIODisplayOnlyPreferredName); + if(displayInfo) + { + CFDictionaryRef productNames; + if(!ffCfDictGetDict(displayInfo, CFSTR(kDisplayProductName), &productNames)) + ffCfDictGetString(productNames, CFSTR("en_US"), &name); + } + } + #endif ffdsAppendDisplay(ds, (uint32_t)CGDisplayModeGetPixelWidth(mode), diff --git a/src/detection/font/font_apple.m b/src/detection/font/font_apple.m index 9a9888e5f..683acad03 100644 --- a/src/detection/font/font_apple.m +++ b/src/detection/font/font_apple.m @@ -25,7 +25,11 @@ const char* ffDetectFontImpl(FFFontResult* result) { ffStrbufAppendS(&result->fonts[0], [NSFont systemFontOfSize:12].familyName.UTF8String); ffStrbufAppendS(&result->fonts[1], [NSFont userFontOfSize:12].familyName.UTF8String); + #ifdef MAC_OS_X_VERSION_10_15 ffStrbufAppendS(&result->fonts[2], [NSFont monospacedSystemFontOfSize:12 weight:400].familyName.UTF8String); + #else + ffStrbufAppendS(&result->fonts[2], ""); + #endif ffStrbufAppendS(&result->fonts[3], [NSFont userFixedPitchFontOfSize:12].familyName.UTF8String); generateString(result); diff --git a/src/detection/gpu/gpu_apple.m b/src/detection/gpu/gpu_apple.m index 85d951724..c63abaf5f 100644 --- a/src/detection/gpu/gpu_apple.m +++ b/src/detection/gpu/gpu_apple.m @@ -5,10 +5,14 @@ #ifndef MAC_OS_VERSION_13_0 #define MTLGPUFamilyMetal3 ((MTLGPUFamily) 5001) #endif +#ifndef MAC_OS_X_VERSION_10_15 + #define MTLFeatureSet_macOS_GPUFamily1_v4 ((MTLFeatureSet) 10004) + #define MTLFeatureSet_macOS_GPUFamily2_v1 ((MTLFeatureSet) 10005) +#endif const char* ffGpuDetectMetal(FFlist* gpus) { - if (@available(macOS 10.15, *)) + if (@available(macOS 10.13, *)) { for (id device in MTLCopyAllDevices()) { @@ -23,6 +27,12 @@ const char* ffGpuDetectMetal(FFlist* gpus) } if (!gpu) continue; + #ifndef MAC_OS_X_VERSION_10_15 + if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily2_v1]) + ffStrbufSetStatic(&gpu->platformApi, "Metal Feature Set 2"); + else if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v1]) + ffStrbufSetStatic(&gpu->platformApi, "Metal Feature Set 1"); + #else // MAC_OS_X_VERSION_10_15 if ([device supportsFamily:MTLGPUFamilyMetal3]) ffStrbufSetStatic(&gpu->platformApi, "Metal 3"); else if ([device supportsFamily:MTLGPUFamilyCommon3]) @@ -32,15 +42,16 @@ const char* ffGpuDetectMetal(FFlist* gpus) else if ([device supportsFamily:MTLGPUFamilyCommon1]) ffStrbufSetStatic(&gpu->platformApi, "Metal Common 1"); - if (device.hasUnifiedMemory) + gpu->type = device.hasUnifiedMemory ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE; + #endif + + if (gpu->type == FF_GPU_TYPE_INTEGRATED) { - gpu->type = FF_GPU_TYPE_INTEGRATED; gpu->shared.total = device.recommendedMaxWorkingSetSize; gpu->shared.used = device.currentAllocatedSize; } else { - gpu->type = FF_GPU_TYPE_DISCRETE; gpu->dedicated.total = device.recommendedMaxWorkingSetSize; gpu->dedicated.used = device.currentAllocatedSize; } diff --git a/src/detection/monitor/monitor_apple.m b/src/detection/monitor/monitor_apple.m index 348a1ad33..31b7ab9e3 100644 --- a/src/detection/monitor/monitor_apple.m +++ b/src/detection/monitor/monitor_apple.m @@ -9,11 +9,22 @@ extern CFDictionaryRef CoreDisplay_DisplayCreateInfoDictionary(CGDirectDisplayID display) __attribute__((weak_import)); +#ifndef MAC_OS_X_VERSION_10_15 +#import +extern CFDictionaryRef CoreDisplay_IODisplayCreateInfoDictionary(io_service_t framebuffer, IOOptionBits options) __attribute__((weak_import)); +#endif + static bool detectHdrSupportWithNSScreen(FFDisplayResult* display) { NSScreen* mainScreen = NSScreen.mainScreen; if (display->primary) + { + #ifdef MAC_OS_X_VERSION_10_15 return mainScreen.maximumPotentialExtendedDynamicRangeColorComponentValue > 1; + #else + return mainScreen.maximumExtendedDynamicRangeColorComponentValue > 1; + #endif + } else { for (NSScreen* screen in NSScreen.screens) @@ -22,7 +33,11 @@ static bool detectHdrSupportWithNSScreen(FFDisplayResult* display) NSNumber* screenNumber = [screen.deviceDescription valueForKey:@"NSScreenNumber"]; if (screenNumber && screenNumber.longValue == (long) display->id) { + #ifdef MAC_OS_X_VERSION_10_15 return screen.maximumPotentialExtendedDynamicRangeColorComponentValue > 1; + #else + return screen.maximumExtendedDynamicRangeColorComponentValue > 1; + #endif } } } @@ -31,13 +46,19 @@ static bool detectHdrSupportWithNSScreen(FFDisplayResult* display) const char* ffDetectMonitor(FFlist* results) { + #ifdef MAC_OS_X_VERSION_10_15 if(!CoreDisplay_DisplayCreateInfoDictionary) return "CoreDisplay_DisplayCreateInfoDictionary is not available"; - + #endif const FFDisplayServerResult* displayServer = ffConnectDisplayServer(); FF_LIST_FOR_EACH(FFDisplayResult, display, displayServer->displays) { + #ifdef MAC_OS_X_VERSION_10_15 CFDictionaryRef FF_CFTYPE_AUTO_RELEASE displayInfo = CoreDisplay_DisplayCreateInfoDictionary((CGDirectDisplayID) display->id); + #else + io_service_t servicePort = CGDisplayIOServicePort((CGDirectDisplayID) display->id); + CFDictionaryRef FF_CFTYPE_AUTO_RELEASE displayInfo = CoreDisplay_IODisplayCreateInfoDictionary(servicePort, kIODisplayOnlyPreferredName); + #endif if(!displayInfo) continue; bool isVirtual = false; diff --git a/src/detection/os/os_apple.m b/src/detection/os/os_apple.m index dbb55539e..d08aed435 100644 --- a/src/detection/os/os_apple.m +++ b/src/detection/os/os_apple.m @@ -97,8 +97,7 @@ void ffDetectOSImpl(FFOSResult* os) { parseSystemVersion(os); - if(ffStrbufStartsWithIgnCaseS(&os->name, "MacOS")) - ffStrbufSetStatic(&os->id, "macos"); + ffStrbufSetStatic(&os->id, "macos"); if(os->version.length == 0) ffSysctlGetString("kern.osproductversion", &os->version); diff --git a/src/detection/physicaldisk/physicaldisk_apple.c b/src/detection/physicaldisk/physicaldisk_apple.c index 8f4a11420..b60e466d3 100644 --- a/src/detection/physicaldisk/physicaldisk_apple.c +++ b/src/detection/physicaldisk/physicaldisk_apple.c @@ -7,17 +7,22 @@ #include #include #include -#include +#ifdef MAC_OS_X_VERSION_10_15 + #include +#endif +#ifdef MAC_OS_X_VERSION_10_15 static inline void wrapIoDestroyPlugInInterface(IOCFPlugInInterface*** pluginInf) { assert(pluginInf); if (*pluginInf) IODestroyPlugInInterface(*pluginInf); } +#endif static const char* detectSsdTemp(io_service_t entryPhysical, double* temp) { + #ifdef MAC_OS_X_VERSION_10_15 __attribute__((__cleanup__(wrapIoDestroyPlugInInterface))) IOCFPlugInInterface** pluginInf = NULL; int32_t score; if (IOCreatePlugInInterfaceForService(entryPhysical, kIONVMeSMARTUserClientTypeID, kIOCFPlugInInterfaceID, &pluginInf, &score) != kIOReturnSuccess) @@ -36,6 +41,9 @@ static const char* detectSsdTemp(io_service_t entryPhysical, double* temp) (*pluginInf)->Release(smartInf); return error; + #else + return "No support for old MacOS version"; + #endif } const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) @@ -117,12 +125,14 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) } } + #ifdef MAC_OS_X_VERSION_10_15 if (options->temp) { FF_CFTYPE_AUTO_RELEASE CFBooleanRef nvmeSMARTCapable = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyNVMeSMARTCapableKey), kCFAllocatorDefault, kNilOptions); if (nvmeSMARTCapable && CFBooleanGetValue(nvmeSMARTCapable)) detectSsdTemp(entryPhysical, &device->temperature); } + #endif } } diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index fbb90b207..77fd23c2a 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -56,6 +56,10 @@ static void getProcessInformation(pid_t pid, FFstrbuf* processName, FFstrbuf* ex int mibs[] = { CTL_KERN, KERN_PROCARGS2, pid }; if (sysctl(mibs, sizeof(mibs) / sizeof(*mibs), NULL, &len, NULL, 0) == 0) { + #ifndef MAC_OS_X_VERSION_10_15 + //don't know why if don't let len longer, proArgs2 and len will change during the following sysctl() in old MacOS version. + len++; + #endif FF_AUTO_FREE char* const procArgs2 = malloc(len); if (sysctl(mibs, sizeof(mibs) / sizeof(*mibs), procArgs2, &len, NULL, 0) == 0) { diff --git a/src/detection/wifi/wifi_apple.m b/src/detection/wifi/wifi_apple.m index 64d90ccb2..8fdb40637 100644 --- a/src/detection/wifi/wifi_apple.m +++ b/src/detection/wifi/wifi_apple.m @@ -135,13 +135,13 @@ const char* ffDetectWifi(FFlist* result) case kCWSecurityEnterprise: ffStrbufSetStatic(&item->conn.security, "Enterprise"); break; - case kCWSecurityWPA3Personal: + case 11 /*kCWSecurityWPA3Personal*/: ffStrbufSetStatic(&item->conn.security, "WPA3 Personal"); break; - case kCWSecurityWPA3Enterprise: + case 12 /*kCWSecurityWPA3Enterprise*/: ffStrbufSetStatic(&item->conn.security, "WPA3 Enterprise"); break; - case kCWSecurityWPA3Transition: + case 13 /*kCWSecurityWPA3Transition*/: ffStrbufSetStatic(&item->conn.security, "WPA3 Transition"); break; case 14 /*kCWSecurityOWE*/: