Added new platform function to get cpu string (#3533)

Co-authored-by: cold-brewed <cold-brewed@users.noreply.github.com>
This commit is contained in:
cold-brewed
2023-08-09 17:12:27 -04:00
committed by GitHub
parent bf18fa3297
commit 15013e1ede
4 changed files with 80 additions and 0 deletions

View File

@@ -137,6 +137,7 @@ extern void plat_vidapi_reload(void);
extern void plat_vid_reload_options(void);
extern uint32_t plat_language_code(char *langcode);
extern void plat_language_code_r(uint32_t lcid, char *outbuf, int len);
extern void plat_get_cpu_string(char *outbuf, uint8_t len);
/* Resource management. */
extern void set_language(uint32_t id);

View File

@@ -36,6 +36,7 @@
#include <QDateTime>
#include <QLocalSocket>
#include <QTimer>
#include <QProcess>
#include <QLibrary>
#include <QElapsedTimer>
@@ -660,3 +661,69 @@ plat_init_rom_paths()
#endif
}
}
void
plat_get_cpu_string(char *outbuf, uint8_t len) {
auto cpu_string = QString("Unknown");
/* Write the default string now in case we have to exit early from an error */
qstrncpy(outbuf, cpu_string.toUtf8().constData(), len);
#if defined(Q_OS_MACOS)
auto *process = new QProcess(nullptr);
QStringList arguments;
QString program = "/usr/sbin/sysctl";
arguments << "machdep.cpu.brand_string";
process->start(program, arguments);
if (!process->waitForStarted()) {
return;
}
if (!process->waitForFinished()) {
return;
}
QByteArray result = process->readAll();
auto command_result = QString(result).split(": ").last();
if(!command_result.isEmpty()) {
cpu_string = command_result;
}
#elif defined(Q_OS_WINDOWS)
const LPCSTR keyName = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
const LPCSTR valueName = "ProcessorNameString";
unsigned char buf[32768];
DWORD bufSize;
HKEY hKey;
bufSize = 32768;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyName, 0, 1, &hKey) == ERROR_SUCCESS) {
if (RegQueryValueExA(hKey, valueName, NULL, NULL, buf, &bufSize) == ERROR_SUCCESS) {
cpu_string = reinterpret_cast<const char*>(buf);
}
RegCloseKey(hKey);
}
#elif defined(Q_OS_LINUX)
auto cpuinfo = QString("/proc/cpuinfo");
auto cpuinfo_fi = QFileInfo(cpuinfo);
if(!cpuinfo_fi.isReadable()) {
return;
}
QFile file(cpuinfo);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream textStream(&file);
while(true) {
QString line = textStream.readLine();
if (line.isNull()) {
break;
}
if(line.contains(QRegExp("model name.*:"))) {
auto list = line.split(": ");
if(!list.last().isEmpty()) {
cpu_string = list.last();
break;
}
}
}
}
#endif
qstrncpy(outbuf, cpu_string.toUtf8().constData(), len);
}

View File

@@ -1309,6 +1309,12 @@ plat_language_code(char *langcode)
return 0;
}
void
plat_get_cpu_string(char *outbuf, uint8_t len) {
char cpu_string[] = "Unknown";
strncpy(outbuf, cpu_string, len);
}
/* Converts back the language code to LCID */
void
plat_language_code_r(uint32_t lcid, char *outbuf, int len)

View File

@@ -1250,6 +1250,12 @@ plat_language_code_r(uint32_t lcid, char *outbuf, int len)
c16stombs(outbuf, buffer, len);
}
void
plat_get_cpu_string(char *outbuf, uint8_t len) {
char cpu_string[] = "Unknown";
strncpy(outbuf, cpu_string, len);
}
void
take_screenshot(void)
{