Global (Windows): prefers NTAPI for querying registry values

This commit is contained in:
李通洲
2026-03-13 10:50:31 +08:00
parent 6ed8aac6bb
commit ea6ad4ac86
15 changed files with 575 additions and 201 deletions
+8 -18
View File
@@ -250,28 +250,18 @@ static const char* detectNCores(FFCPUResult* cpu)
static const char* detectByRegistry(FFCPUResult* cpu)
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", &hKey, NULL))
return "ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L\"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\", &hKey, NULL) failed";
ffRegReadStrbuf(hKey, L"ProcessorNameString", &cpu->name, NULL);
if (ffRegReadStrbuf(hKey, L"VendorIdentifier", &cpu->vendor, NULL))
if (ffRegReadValues(hKey, 3, (FFRegValueArg[]) {
FF_ARG(cpu->name, L"ProcessorNameString"),
FF_ARG(cpu->vendor, L"VendorIdentifier"),
FF_ARG(cpu->frequencyBase, L"~MHz"),
}, NULL))
ffStrbufTrimRightSpace(&cpu->vendor);
if (cpu->coresLogical == 0)
{
FF_HKEY_AUTO_DESTROY hProcsKey = NULL;
if (ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor", &hProcsKey, NULL))
{
uint32_t cores;
if (ffRegGetNSubKeys(hProcsKey, &cores, NULL))
cpu->coresOnline = cpu->coresPhysical = cpu->coresLogical = (uint16_t) cores;
}
}
uint32_t mhz;
if(ffRegReadUint(hKey, L"~MHz", &mhz, NULL))
cpu->frequencyBase = mhz;
else
return "ffRegReadValues() failed for CPU registry key";
return NULL;
}
+7 -6
View File
@@ -1,17 +1,18 @@
#include "cursor.h"
#include "common/io.h"
#include "common/windows/registry.h"
void ffDetectCursor(FFCursorResult* result)
{
FF_HKEY_AUTO_DESTROY hKey;
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if(ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"Control Panel\\Cursors", &hKey, &result->error))
{
if (!ffRegReadStrbuf(hKey, NULL, &result->theme, &result->error))
return;
uint32_t cursorBaseSize;
if (ffRegReadUint(hKey, L"CursorBaseSize", &cursorBaseSize, NULL))
ffStrbufAppendF(&result->size, "%u", (unsigned) cursorBaseSize);
if (ffRegReadValues(hKey, 2, (FFRegValueArg[]) {
FF_ARG(result->theme, NULL),
FF_ARG(cursorBaseSize, L"CursorBaseSize"),
}, &result->error))
ffStrbufAppendUInt(&result->size, cursorBaseSize);
}
}
@@ -1,6 +1,7 @@
#include "displayserver.h"
#include "common/windows/unicode.h"
#include "common/edidHelper.h"
#include "common/windows/registry.h"
#include "common/windows/unicode.h"
#include <windows.h>
#include <shellscalingapi.h>
@@ -49,8 +50,7 @@ static void detectDisplays(FFDisplayServerResult* ds)
.id = path->targetInfo.id,
},
};
uint8_t edidData[1024];
DWORD edidLength = 0;
FF_LIST_AUTO_DESTROY edid = ffListCreate(sizeof(uint8_t));
if(DisplayConfigGetDeviceInfo(&targetName.header) == ERROR_SUCCESS)
{
@@ -69,16 +69,17 @@ static void detectDisplays(FFDisplayServerResult* ds)
}
wcscpy(pRegPath, L"Device Parameters");
edidLength = ARRAY_SIZE(edidData);
if (RegGetValueW(HKEY_LOCAL_MACHINE, regPath, L"EDID", RRF_RT_REG_BINARY, NULL, edidData, &edidLength) == ERROR_SUCCESS &&
edidLength > 0 && edidLength % 128 == 0)
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if (ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, regPath, &hKey, NULL) &&
ffRegReadData(hKey, L"EDID", &edid, NULL) &&
ffEdidIsValid(edid.data, edid.length))
{
ffEdidGetName(edidData, &name);
ffEdidGetPhysicalSize(edidData, &physicalWidth, &physicalHeight);
ffEdidGetName(edid.data, &name);
ffEdidGetPhysicalSize(edid.data, &physicalWidth, &physicalHeight);
}
else
{
edidLength = 0;
ffListClear(&edid);
if (targetName.flags.friendlyNameFromEdid)
ffStrbufSetWS(&name, targetName.monitorFriendlyDeviceName);
else
@@ -209,8 +210,8 @@ static void detectDisplays(FFDisplayServerResult* ds)
else
display->hdrStatus = FF_DISPLAY_HDR_STATUS_UNKNOWN;
}
if (edidLength > 0)
ffEdidGetSerialAndManufactureDate(edidData, &display->serial, &display->manufactureYear, &display->manufactureWeek);
if (edid.length > 0)
ffEdidGetSerialAndManufactureDate(edid.data, &display->serial, &display->manufactureYear, &display->manufactureWeek);
display->drrStatus = path->flags & DISPLAYCONFIG_PATH_BOOST_REFRESH_RATE ? FF_DISPLAY_DRR_STATUS_ENABLED : FF_DISPLAY_DRR_STATUS_DISABLED;
}
}
+11 -4
View File
@@ -1,12 +1,12 @@
#include "gpu.h"
#include "detection/gpu/gpu_driver_specific.h"
#include "common/library.h"
#include "common/windows/unicode.h"
#include "common/windows/registry.h"
#include "common/mallocHelper.h"
#include "common/debug.h"
#include "common/windows/nt.h"
#include <windows.h>
#include <cfgmgr32.h>
#define FF_EMPTY_GUID_STR L"{00000000-0000-0000-0000-000000000000}"
@@ -19,6 +19,13 @@ const uint32_t regDriverKeyPrefixLength = (uint32_t) __builtin_strlen("SYSTEM\\C
#define GUID_DEVCLASS_DISPLAY_STRING L"{4d36e968-e325-11ce-bfc1-08002be10318}" // Found in <devguid.h>
static inline void wrapRegCloseKey(HKEY* phKey)
{
if(*phKey)
RegCloseKey(*phKey);
}
#define FF_HKEY_AUTO_DESTROY __attribute__((__cleanup__(wrapRegCloseKey)))
const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* gpus)
{
FF_DEBUG("Starting GPU detection");
@@ -115,7 +122,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
uint64_t adapterLuid = 0;
FF_HKEY_AUTO_DESTROY hVideoIdKey = NULL;
FF_HKEY_AUTO_DESTROY HKEY hVideoIdKey = NULL;
wchar_t buffer[256];
ULONG bufferLen = 0;
@@ -147,7 +154,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
{
FF_DEBUG("Found VideoID: %ls", buffer);
wmemcpy(regDirectxKey + regDirectxKeyPrefixLength, buffer, FF_GUID_STRLEN);
FF_HKEY_AUTO_DESTROY hDirectxKey = NULL;
FF_AUTO_CLOSE_FD HANDLE hDirectxKey = NULL;
if (ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, regDirectxKey, &hDirectxKey, NULL))
{
FF_DEBUG("Opened DirectX registry key");
@@ -237,7 +244,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
{
FF_DEBUG("Found driver GUID: %ls", buffer);
wmemcpy(regDriverKey + regDriverKeyPrefixLength, buffer, FF_GUID_STRLEN + strlen("\\0000"));
FF_HKEY_AUTO_DESTROY hRegDriverKey = NULL;
FF_AUTO_CLOSE_FD HANDLE hRegDriverKey = NULL;
if (ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, regDriverKey, &hRegDriverKey, NULL))
{
FF_DEBUG("Opened driver registry key");
+1 -1
View File
@@ -3,7 +3,7 @@
const char* ffDetectIcons(FFIconsResult* result)
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if(!ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\HideDesktopIcons\\NewStartPanel", &hKey, NULL) &&
!ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\HideDesktopIcons\\ClassicStartMenu", &hKey, NULL))
return "ffRegOpenKeyForRead(Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\HideDesktopIcons\\{NewStartPanel|ClassicStartMenu}) failed";
+4 -8
View File
@@ -10,7 +10,7 @@ PWSTR WINAPI BrandingFormatString(PCWSTR format);
static bool getCodeName(FFOSResult* os)
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", &hKey, NULL))
return false;
@@ -39,13 +39,9 @@ void ffDetectOSImpl(FFOSResult* os)
if(os->variant.length == 0) // Windows PE?
{
wchar_t buf[128];
DWORD bufSize = (DWORD) sizeof(buf); // with trailing '\0'
if(RegGetValueW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"ProductName", RRF_RT_REG_SZ, NULL, buf, &bufSize) == ERROR_SUCCESS)
{
assert(bufSize >= sizeof(wchar_t));
ffStrbufSetNWS(&os->variant, bufSize / sizeof(wchar_t) - 1, buf);
}
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if(ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", &hKey, NULL))
ffRegReadStrbuf(hKey, L"ProductName", &os->variant, NULL);
}
ffStrbufSet(&os->prettyName, &os->variant);
@@ -268,16 +268,25 @@ static void detectConEmu(FFTerminalFontResult* terminalFont)
static void detectWarp(FFTerminalFontResult* terminalFont)
{
FF_HKEY_AUTO_DESTROY key = NULL;
FF_AUTO_CLOSE_FD HANDLE key = NULL;
if (!ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"Software\\Warp.dev\\Warp", &key, &terminalFont->error))
return;
FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();
if (!ffRegReadStrbuf(key, L"FontName", &fontName, NULL))
if (ffRegReadValues(key, 2, (FFRegValueArg[]) {
FF_ARG(fontName, L"FontName"),
FF_ARG(fontSize, L"FontSize")
}, &terminalFont->error))
{
ffStrbufTrim(&fontName, '"');
ffStrbufAppendS(&fontSize, "px");
}
else
{
ffStrbufSetS(&fontName, "Hack");
if (!ffRegReadStrbuf(key, L"FontSize", &fontSize, &terminalFont->error))
ffStrbufSetS(&fontSize, "13");
ffStrbufSetS(&fontSize, "13.0px");
}
ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
}
@@ -191,7 +191,7 @@ static bool detectDefaultTerminal(FFTerminalResult* result)
goto conhost;
}
FF_HKEY_AUTO_DESTROY hKey = NULL;
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if(ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, regPath, &hKey, NULL))
{
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate();
+1 -1
View File
@@ -3,7 +3,7 @@
const char* ffDetectWallpaper(FFstrbuf* result)
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if(!ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"Control Panel\\Desktop", &hKey, NULL))
return "ffRegOpenKeyForRead(Control Panel\\Desktop) failed";
+24 -17
View File
@@ -63,18 +63,18 @@ const char* colorHexToString(DWORD hex)
bool ffDetectWmTheme(FFstrbuf* themeOrError)
{
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
if(ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes", &hKey, NULL))
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if (ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes", &hKey, NULL))
{
FF_STRBUF_AUTO_DESTROY theme = ffStrbufCreate();
if(ffRegReadStrbuf(hKey, L"CurrentTheme", &theme, NULL))
if (ffRegReadStrbuf(hKey, L"CurrentTheme", &theme, NULL))
{
ffStrbufSubstrBeforeLastC(&theme, '.');
ffStrbufSubstrAfterLastC(&theme, '\\');
if(isalpha(theme.chars[0]))
theme.chars[0] = (char)toupper(theme.chars[0]);
ffStrbufAppendF(themeOrError, "%s", theme.chars);
ffStrbufAppend(themeOrError, &theme);
}
}
}
@@ -82,28 +82,35 @@ bool ffDetectWmTheme(FFstrbuf* themeOrError)
do {
uint32_t rgbColor;
uint32_t bgrColor;
DWORD bufSize = sizeof(bgrColor);
if(RegGetValueW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", L"AccentColor", RRF_RT_REG_DWORD, NULL, &bgrColor, &bufSize) == ERROR_SUCCESS)
rgbColor = ((bgrColor & 0xFF) << 16) | (bgrColor & 0xFF00) | ((bgrColor >> 16) & 0xFF);
else if(RegGetValueW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", L"ColorizationColor", RRF_RT_REG_DWORD, NULL, &rgbColor, &bufSize) == ERROR_SUCCESS)
rgbColor &= 0xFFFFFF;
else
break;
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if (ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", &hKey, NULL))
{
if (ffRegReadUint(hKey, L"AccentColor", &bgrColor, NULL))
rgbColor = ((bgrColor & 0xFF) << 16) | (bgrColor & 0xFF00) | ((bgrColor >> 16) & 0xFF);
else if (ffRegReadUint(hKey, L"ColorizationColor", &rgbColor, NULL))
rgbColor &= 0xFFFFFF;
else
break;
}
else break;
if(themeOrError->length > 0) ffStrbufAppendS(themeOrError, " - ");
if (themeOrError->length > 0) ffStrbufAppendS(themeOrError, " - ");
const char* text = colorHexToString(rgbColor);
if(text)
if (text)
ffStrbufAppendS(themeOrError, text);
else
ffStrbufAppendF(themeOrError, "#%06lX", (long)rgbColor);
} while (false);
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
if(ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", &hKey, NULL))
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if (ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", &hKey, NULL))
{
uint32_t system = 1, apps = 1;
if (ffRegReadUint(hKey, L"SystemUsesLightTheme", &system, NULL) && ffRegReadUint(hKey, L"AppsUseLightTheme", &apps, NULL))
if (ffRegReadValues(hKey, 2, (FFRegValueArg[]) {
FF_ARG(system, L"SystemUsesLightTheme"),
FF_ARG(apps, L"AppsUseLightTheme"),
}, NULL))
{
bool paren = themeOrError->length > 0;
if (paren)
@@ -117,7 +124,7 @@ bool ffDetectWmTheme(FFstrbuf* themeOrError)
if(themeOrError->length == 0)
{
ffStrbufAppendS(themeOrError, "Failed to find current theme");
ffStrbufSetStatic(themeOrError, "Failed to find current theme");
return false;
}
return true;