From 1aeca9de1c066607f9e2d5291cf65e532871ed6f Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Wed, 7 Apr 2021 00:31:48 +0200 Subject: [PATCH] Add native helpers to get the CPU name --- plugins/editor/src/editor/NativeHelpers.cpp | 49 +++++++++++++++++++++ plugins/editor/src/editor/NativeHelpers.h | 1 + plugins/editor/src/editor/NativeHelpers.mm | 19 ++++++++ 3 files changed, 69 insertions(+) diff --git a/plugins/editor/src/editor/NativeHelpers.cpp b/plugins/editor/src/editor/NativeHelpers.cpp index 336c81d6..311693e2 100644 --- a/plugins/editor/src/editor/NativeHelpers.cpp +++ b/plugins/editor/src/editor/NativeHelpers.cpp @@ -111,6 +111,32 @@ std::string getOperatingSystemName() std::unique_ptr valueUTF8(stringToUTF8(valueW.get())); return valueUTF8.get(); } + +std::string getProcessorName() +{ + LSTATUS status; + HKEY key = nullptr; + const WCHAR keyPath[] = L"Hardware\\Description\\System\\CentralProcessor\\0"; + const WCHAR valueName[] = L"ProcessorNameString"; + const char fallbackName[] = "Unknown"; + + status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyPath, 0, KEY_QUERY_VALUE, &key); + if (status != ERROR_SUCCESS) + return fallbackName; + + DWORD valueSize = 32768 * sizeof(WCHAR); + std::unique_ptr valueW(new WCHAR[(valueSize / sizeof(WCHAR)) + 1]()); + DWORD valueType; + status = RegQueryValueExW( + key, valueName, nullptr, + &valueType, reinterpret_cast(valueW.get()), &valueSize); + RegCloseKey(key); + if (status != ERROR_SUCCESS || (valueType != REG_SZ && valueType != REG_EXPAND_SZ)) + return fallbackName; + + std::unique_ptr valueUTF8(stringToUTF8(valueW.get())); + return valueUTF8.get(); +} #elif defined(__APPLE__) // implemented in NativeHelpers.mm #else @@ -120,6 +146,8 @@ std::string getOperatingSystemName() #include #include #include +#include +#include #include #include extern "C" { extern char **environ; } @@ -247,4 +275,25 @@ std::string getOperatingSystemName() return name; } + +std::string getProcessorName() +{ + std::string name; + std::string line; + std::ifstream in("/proc/cpuinfo", std::ios::binary); + std::regex re("^model name\\s*:\\s*(.*)"); + + line.reserve(256); + + while (name.empty() && std::getline(in, line) && !line.empty()) { + std::smatch match; + if (std::regex_match(line, match, re)) + name = match[1]; + } + + if (name.empty()) + name = "Unknown"; + + return name; +} #endif diff --git a/plugins/editor/src/editor/NativeHelpers.h b/plugins/editor/src/editor/NativeHelpers.h index db21af28..41a88887 100644 --- a/plugins/editor/src/editor/NativeHelpers.h +++ b/plugins/editor/src/editor/NativeHelpers.h @@ -12,6 +12,7 @@ bool openDirectoryInExplorer(const char *filename); bool openURLWithExternalProgram(const char *url); bool askQuestion(const char *text); std::string getOperatingSystemName(); +std::string getProcessorName(); #if !defined(_WIN32) && !defined(__APPLE__) bool isZenityAvailable(); diff --git a/plugins/editor/src/editor/NativeHelpers.mm b/plugins/editor/src/editor/NativeHelpers.mm index 1f6ee93f..831c3c52 100644 --- a/plugins/editor/src/editor/NativeHelpers.mm +++ b/plugins/editor/src/editor/NativeHelpers.mm @@ -10,6 +10,9 @@ #import #import #import +#include +#include +#include static bool openFileWithApplication(const char *fileName, NSString *application) { @@ -59,4 +62,20 @@ std::string getOperatingSystemName() NSString *osVersion = [[NSProcessInfo processInfo] operatingSystemVersionString]; return [[NSString stringWithFormat:@"%@ %@", osName, osVersion] UTF8String]; } + +std::string getProcessorName() +{ + char nameBuf[256]; + size_t size = sizeof(nameBuf); + const char* fallbackName = "Unknown"; + + if (sysctlbyname("machdep.cpu.brand_string", nameBuf, &size, nullptr, 0) == -1) + return fallbackName; + + size = strnlen(nameBuf, sizeof(nameBuf)); + if (size == 0) + return fallbackName; + + return std::string(nameBuf, size); +} #endif