From 36d8dbc590580bd5694b143c62350f092c4fcdb6 Mon Sep 17 00:00:00 2001 From: OatmealDome Date: Fri, 9 Jan 2026 20:15:52 -0500 Subject: [PATCH] CommonFuncsObjC: Make function to fetch the current macOS version common --- Source/Core/Common/CMakeLists.txt | 2 ++ Source/Core/Common/CommonFuncs.h | 17 +++++++++++++ Source/Core/Common/CommonFuncsObjC.mm | 15 +++++++++++ Source/Core/Core/DolphinAnalytics.cpp | 36 ++++++--------------------- 4 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 Source/Core/Common/CommonFuncsObjC.mm diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index e134acabd3..fe7aa9f72b 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -204,6 +204,7 @@ if (APPLE) PRIVATE ${APPKIT_LIBRARY} ${COREFOUNDATION_LIBRARY} + ${FOUNDATION_LIBRARY} ${IOK_LIBRARY} ) elseif(WIN32) @@ -239,6 +240,7 @@ elseif(WIN32) ) elseif(APPLE) target_sources(common PRIVATE + CommonFuncsObjC.mm Logging/ConsoleListenerNix.cpp MemArenaDarwin.cpp ) diff --git a/Source/Core/Common/CommonFuncs.h b/Source/Core/Common/CommonFuncs.h index a2c20c67e3..b9238dad1c 100644 --- a/Source/Core/Common/CommonFuncs.h +++ b/Source/Core/Common/CommonFuncs.h @@ -8,6 +8,10 @@ #endif #include +#ifdef __APPLE__ +#include "Common/CommonTypes.h" +#endif + #ifndef _WIN32 // go to debugger mode @@ -61,4 +65,17 @@ std::string GetWin32ErrorString(unsigned long error_code); // Obtains a full path to the specified module. std::optional GetModuleName(void* hInstance); #endif + +#ifdef __APPLE__ +struct MacOSVersion // NSOperatingSystemVersion +{ + s64 major; // NSInteger majorVersion + s64 minor; // NSInteger minorVersion + s64 patch; // NSInteger patchVersion +}; + +// Helper function to get the current macOS version, which is easy to do with +// from Objective-C code, but a little harder from C++. +MacOSVersion GetMacOSVersion(); +#endif } // namespace Common diff --git a/Source/Core/Common/CommonFuncsObjC.mm b/Source/Core/Common/CommonFuncsObjC.mm new file mode 100644 index 0000000000..fdc7f48ce2 --- /dev/null +++ b/Source/Core/Common/CommonFuncsObjC.mm @@ -0,0 +1,15 @@ +// Copyright 2026 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "Common/CommonFuncs.h" + +#include + +namespace Common +{ +MacOSVersion GetMacOSVersion() +{ + const NSOperatingSystemVersion ver = [[NSProcessInfo processInfo] operatingSystemVersion]; + return {ver.majorVersion, ver.minorVersion, ver.patchVersion}; +} +} // namespace Common diff --git a/Source/Core/Core/DolphinAnalytics.cpp b/Source/Core/Core/DolphinAnalytics.cpp index 8b207c658c..f4f7c01c41 100644 --- a/Source/Core/Core/DolphinAnalytics.cpp +++ b/Source/Core/Core/DolphinAnalytics.cpp @@ -15,14 +15,16 @@ #if defined(_WIN32) #include #include "Common/WindowsRegistry.h" -#elif defined(__APPLE__) -#include #endif #if defined(ANDROID) #include #endif +#if defined(__APPLE__) +#include "Common/CommonFuncs.h" +#endif + #include "Common/Analytics.h" #include "Common/CPUDetect.h" #include "Common/CommonTypes.h" @@ -300,32 +302,10 @@ void DolphinAnalytics::MakeBaseBuilder() #elif defined(__APPLE__) builder.AddData("os-type", "osx"); - // id processInfo = [NSProcessInfo processInfo] - id processInfo = reinterpret_cast(objc_msgSend)( - objc_getClass("NSProcessInfo"), sel_getUid("processInfo")); - if (processInfo) - { - struct OSVersion // NSOperatingSystemVersion - { - s64 major_version; // NSInteger majorVersion - s64 minor_version; // NSInteger minorVersion - s64 patch_version; // NSInteger patchVersion - }; - // Under arm64, we need to call objc_msgSend to receive a struct. - // On x86_64, we need to explicitly call objc_msgSend_stret for a struct. -#ifdef _M_ARM_64 -#define msgSend objc_msgSend -#else -#define msgSend objc_msgSend_stret -#endif - // NSOperatingSystemVersion version = [processInfo operatingSystemVersion] - OSVersion version = reinterpret_cast(msgSend)( - processInfo, sel_getUid("operatingSystemVersion")); -#undef msgSend - builder.AddData("osx-ver-major", version.major_version); - builder.AddData("osx-ver-minor", version.minor_version); - builder.AddData("osx-ver-bugfix", version.patch_version); - } + Common::MacOSVersion version = Common::GetMacOSVersion(); + builder.AddData("osx-ver-major", version.major); + builder.AddData("osx-ver-minor", version.minor); + builder.AddData("osx-ver-bugfix", version.patch); #elif defined(__linux__) builder.AddData("os-type", "linux"); #elif defined(__FreeBSD__)