From da42108732b4411da2afd7bdc4d85038da174d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 9 Aug 2026 10:22:21 +0800 Subject: [PATCH] Cursor (macOS): migrates impl to pure C using CoreFoundation --- CMakeLists.txt | 2 +- src/detection/cursor/cursor_apple.c | 70 +++++++++++++++++++++++++++++ src/detection/cursor/cursor_apple.m | 63 -------------------------- 3 files changed, 71 insertions(+), 64 deletions(-) create mode 100644 src/detection/cursor/cursor_apple.c delete mode 100644 src/detection/cursor/cursor_apple.m diff --git a/CMakeLists.txt b/CMakeLists.txt index 238537f9f..49a8f2f8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1023,7 +1023,7 @@ elseif(APPLE) src/detection/cpu/cpu_apple.c src/detection/cpucache/cpucache_apple.c src/detection/cpuusage/cpuusage_apple.c - src/detection/cursor/cursor_apple.m + src/detection/cursor/cursor_apple.c src/detection/disk/disk_bsd.c src/detection/dns/dns_apple.c src/detection/physicaldisk/physicaldisk_apple.c diff --git a/src/detection/cursor/cursor_apple.c b/src/detection/cursor/cursor_apple.c new file mode 100644 index 000000000..957d08935 --- /dev/null +++ b/src/detection/cursor/cursor_apple.c @@ -0,0 +1,70 @@ +#include "cursor.h" +#include "common/apple/cf_helpers.h" + +static bool appendColor(FFstrbuf* str, CFDictionaryRef dict) { + double r, g, b, a; + if ( + ffCfDictGetDouble(dict, CFSTR("red"), &r) || + ffCfDictGetDouble(dict, CFSTR("green"), &g) || + ffCfDictGetDouble(dict, CFSTR("blue"), &b) || + ffCfDictGetDouble(dict, CFSTR("alpha"), &a)) { + return false; + } + + r = r * 255 + .5; + g = g * 255 + .5; + b = b * 255 + .5; + a = a * 255 + .5; + + uint32_t color = ((uint32_t) r << 24) | ((uint32_t) g << 16) | ((uint32_t) b << 8) | ((uint32_t) a); + + // clang-format off + switch (color) + { + case 0x000000FF: ffStrbufAppendS(str, "Black"); break; + case 0x0433FFFF: ffStrbufAppendS(str, "Blue"); break; + case 0xAA7942FF: ffStrbufAppendS(str, "Brown"); break; + case 0x00FDFFFF: ffStrbufAppendS(str, "Cyan"); break; + case 0x00F900FF: ffStrbufAppendS(str, "Green"); break; + case 0xFF40FFFF: ffStrbufAppendS(str, "Magenta"); break; + case 0xFF9300FF: ffStrbufAppendS(str, "Orange"); break; + case 0x942192FF: ffStrbufAppendS(str, "Purple"); break; + case 0xFF2600FF: ffStrbufAppendS(str, "Red"); break; + case 0xFFFB00FF: ffStrbufAppendS(str, "Yellow"); break; + case 0xFFFFFFFF: ffStrbufAppendS(str, "White"); break; + case 0x00000000: ffStrbufAppendS(str, "Transparent"); break; + default: ffStrbufAppendF(str, "#%08X", color); break; + } + // clang-format on + + return true; +} + +void ffDetectCursor(FFCursorResult* result) { + // Read via cfprefsd (equivalent to `defaults read com.apple.universalaccess `) + ffStrbufAppendS(&result->theme, "Fill - "); + { + FF_CFTYPE_AUTO_RELEASE CFTypeRef color = CFPreferencesCopyAppValue(CFSTR("cursorFill"), CFSTR("com.apple.universalaccess")); + if (!color || CFGetTypeID(color) != CFDictionaryGetTypeID() || !appendColor(&result->theme, (CFDictionaryRef) color)) { + ffStrbufAppendS(&result->theme, "Black"); + } + } + + ffStrbufAppendS(&result->theme, ", Outline - "); + { + FF_CFTYPE_AUTO_RELEASE CFTypeRef color = CFPreferencesCopyAppValue(CFSTR("cursorOutline"), CFSTR("com.apple.universalaccess")); + if (!color || CFGetTypeID(color) != CFDictionaryGetTypeID() || !appendColor(&result->theme, (CFDictionaryRef) color)) { + ffStrbufAppendS(&result->theme, "White"); + } + } + + { + FF_CFTYPE_AUTO_RELEASE CFTypeRef mouseDriverCursorSize = CFPreferencesCopyAppValue(CFSTR("mouseDriverCursorSize"), CFSTR("com.apple.universalaccess")); + double size = 32; + if (mouseDriverCursorSize && ffCfNumGetDouble(mouseDriverCursorSize, &size) == nullptr) { + ffStrbufAppendUInt(&result->size, (uint32_t) (size * 32 + 0.5)); + } else { + ffStrbufAppendS(&result->size, "32"); + } + } +} diff --git a/src/detection/cursor/cursor_apple.m b/src/detection/cursor/cursor_apple.m deleted file mode 100644 index 610bd1ed8..000000000 --- a/src/detection/cursor/cursor_apple.m +++ /dev/null @@ -1,63 +0,0 @@ -#include "cursor.h" - -#import - -static void appendColor(FFstrbuf* str, NSDictionary* dict) -{ - uint32_t r = (uint32_t) (((NSNumber*) dict[@"red"]).doubleValue * 255 + .5); - uint32_t g = (uint32_t) (((NSNumber*) dict[@"green"]).doubleValue * 255 + .5); - uint32_t b = (uint32_t) (((NSNumber*) dict[@"blue"]).doubleValue * 255 + .5); - uint32_t a = (uint32_t) (((NSNumber*) dict[@"alpha"]).doubleValue * 255 + .5); - uint32_t color = (r << 24) | (g << 16) | (b << 8) | a; - - switch (color) - { - case 0x000000FF: ffStrbufAppendS(str, "Black"); return; - case 0x0433FFFF: ffStrbufAppendS(str, "Blue"); return; - case 0xAA7942FF: ffStrbufAppendS(str, "Brown"); return; - case 0x00FDFFFF: ffStrbufAppendS(str, "Cyan"); return; - case 0x00F900FF: ffStrbufAppendS(str, "Green"); return; - case 0xFF40FFFF: ffStrbufAppendS(str, "Magenta"); return; - case 0xFF9300FF: ffStrbufAppendS(str, "Orange"); return; - case 0x942192FF: ffStrbufAppendS(str, "Purple"); return; - case 0xFF2600FF: ffStrbufAppendS(str, "Red"); return; - case 0xFFFB00FF: ffStrbufAppendS(str, "Yellow"); return; - case 0xFFFFFFFF: ffStrbufAppendS(str, "White"); return; - case 0x00000000: ffStrbufAppendS(str, "Transparent"); return; - default: ffStrbufAppendF(str, "#%08X", color); return; - } -} - -void ffDetectCursor(FFCursorResult* result) -{ - NSError* error; - NSString* fileName = [NSString stringWithFormat:@"file://%s/Library/Preferences/com.apple.universalaccess.plist", instance.state.platform.homeDir.chars]; - NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:fileName] - error:&error]; - if(error) - { - ffStrbufAppendS(&result->error, error.localizedDescription.UTF8String); - return; - } - - NSDictionary* color; - - ffStrbufAppendS(&result->theme, "Fill - "); - if ((color = dict[@"cursorFill"])) - appendColor(&result->theme, color); - else - ffStrbufAppendS(&result->theme, "Black"); - - ffStrbufAppendS(&result->theme, ", Outline - "); - - if ((color = dict[@"cursorOutline"])) - appendColor(&result->theme, color); - else - ffStrbufAppendS(&result->theme, "White"); - - NSNumber* mouseDriverCursorSize = dict[@"mouseDriverCursorSize"]; - if (mouseDriverCursorSize) - ffStrbufAppendF(&result->size, "%d", (int) (mouseDriverCursorSize.doubleValue * 32 + 0.5)); - else - ffStrbufAppendS(&result->size, "32"); -}