Add native helpers to get the CPU name

This commit is contained in:
Jean Pierre Cimalando 2021-04-07 00:31:48 +02:00
parent 7d8a77db05
commit 1aeca9de1c
3 changed files with 69 additions and 0 deletions

View file

@ -111,6 +111,32 @@ std::string getOperatingSystemName()
std::unique_ptr<char[]> 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<WCHAR[]> valueW(new WCHAR[(valueSize / sizeof(WCHAR)) + 1]());
DWORD valueType;
status = RegQueryValueExW(
key, valueName, nullptr,
&valueType, reinterpret_cast<LPBYTE>(valueW.get()), &valueSize);
RegCloseKey(key);
if (status != ERROR_SUCCESS || (valueType != REG_SZ && valueType != REG_EXPAND_SZ))
return fallbackName;
std::unique_ptr<char[]> valueUTF8(stringToUTF8(valueW.get()));
return valueUTF8.get();
}
#elif defined(__APPLE__)
// implemented in NativeHelpers.mm
#else
@ -120,6 +146,8 @@ std::string getOperatingSystemName()
#include <sys/utsname.h>
#include <unistd.h>
#include <vector>
#include <regex>
#include <fstream>
#include <cstring>
#include <cerrno>
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

View file

@ -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();

View file

@ -10,6 +10,9 @@
#import <AppKit/AppKit.h>
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <cstring>
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