From 46b0c6e76b56f7a69f2e5b1be91e5caf20fdd8ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 12 Aug 2023 00:07:24 +0800 Subject: [PATCH] Monitor (Windows): code refactor --- src/detection/monitor/monitor_windows.c | 10 ++++++++-- src/util/windows/registry.c | 26 +++++++++++++++++++++---- src/util/windows/registry.h | 2 +- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/detection/monitor/monitor_windows.c b/src/detection/monitor/monitor_windows.c index 5bd57dc64..f56c5a188 100644 --- a/src/detection/monitor/monitor_windows.c +++ b/src/detection/monitor/monitor_windows.c @@ -2,6 +2,7 @@ #include "common/io/io.h" #include "util/edidHelper.h" +#include "util/mallocHelper.h" #include "util/stringUtils.h" #include "util/windows/registry.h" #include "util/windows/unicode.h" @@ -30,8 +31,12 @@ const char* ffDetectMonitor(FFlist* results) FF_HKEY_AUTO_DESTROY hKey = SetupDiOpenDevRegKey(hdev, &did, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE); if (!hKey) continue; - uint8_t edidData[256] = {}; - if (!ffRegReadData(hKey, L"EDID", edidData, sizeof(edidData), NULL)) continue; + FF_AUTO_FREE uint8_t* edidData = NULL; + uint32_t edidLength = 0; + if (!ffRegReadData(hKey, L"EDID", &edidData, &edidLength, NULL)) continue; + if (edidLength == 0 || edidLength % 128 != 0) + continue; + uint32_t width, height; ffEdidGetPhysicalResolution(edidData, &width, &height); if (width == 0 || height == 0) continue; @@ -39,6 +44,7 @@ const char* ffDetectMonitor(FFlist* results) FFMonitorResult* display = (FFMonitorResult*) ffListAdd(results); display->width = width; display->height = height; + display->hdrCompatible = ffEdidGetHdrCompatible(edidData, edidLength); // Doesn't work. edidLength is always 128 ffStrbufInit(&display->name); ffEdidGetName(edidData, &display->name); ffEdidGetPhysicalSize(edidData, &display->physicalWidth, &display->physicalHeight); diff --git a/src/util/windows/registry.c b/src/util/windows/registry.c index f56d59c50..998bfaf3c 100644 --- a/src/util/windows/registry.c +++ b/src/util/windows/registry.c @@ -64,10 +64,25 @@ bool ffRegReadStrbuf(HKEY hKey, const wchar_t* valueNameW, FFstrbuf* result, FFs return true; } -bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t* result, uint32_t bufSize, FFstrbuf* error) +bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t** result, uint32_t* length, FFstrbuf* error) { - static_assert(sizeof(DWORD) == sizeof(uint32_t), ""); - LONG err = RegGetValueW(hKey, NULL, valueNameW, RRF_RT_REG_BINARY, NULL, result, (DWORD*) &bufSize); + assert(result && length); + DWORD bufSize = 0; + LONG err = RegGetValueW(hKey, NULL, valueNameW, RRF_RT_REG_BINARY, NULL, NULL, &bufSize); + if(err != ERROR_SUCCESS || bufSize == 0) + { + if(error) + { + if(!valueNameW) + valueNameW = L"(default)"; + FF_STRBUF_AUTO_DESTROY valueNameA = ffStrbufCreateWS(valueNameW); + ffStrbufAppendF(error, "RegGetValueW(%s, NULL, RRF_RT_REG_BINARY, NULL, NULL, &bufSize) failed", valueNameA.chars); + } + return false; + } + + uint8_t* buf = (uint8_t*) malloc(bufSize); + err = RegGetValueW(hKey, NULL, valueNameW, RRF_RT_REG_BINARY, NULL, buf, &bufSize); if(err != ERROR_SUCCESS) { if(error) @@ -75,10 +90,13 @@ bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t* result, uint32 if(!valueNameW) valueNameW = L"(default)"; FF_STRBUF_AUTO_DESTROY valueNameA = ffStrbufCreateWS(valueNameW); - ffStrbufAppendF(error, "RegGetValueW(%s, NULL, RRF_RT_REG_SZ) failed", valueNameA.chars); + ffStrbufAppendF(error, "RegGetValueW(%s, NULL, RRF_RT_REG_BINARY, NULL, length) failed", valueNameA.chars); } + free(buf); return false; } + *result = buf; + *length = bufSize; return true; } diff --git a/src/util/windows/registry.h b/src/util/windows/registry.h index 8e582ac54..42dd08aaf 100644 --- a/src/util/windows/registry.h +++ b/src/util/windows/registry.h @@ -17,7 +17,7 @@ static inline void wrapRegCloseKey(HKEY* phKey) bool ffRegOpenKeyForRead(HKEY hKey, const wchar_t* subKeyW, HKEY* result, FFstrbuf* error); bool ffRegReadStrbuf(HKEY hKey, const wchar_t* valueNameW, FFstrbuf* result, FFstrbuf* error); -bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t* result, uint32_t bufSize, FFstrbuf* error); +bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t** result, uint32_t* length, FFstrbuf* error); bool ffRegReadUint(HKEY hKey, const wchar_t* valueNameW, uint32_t* result, FFstrbuf* error); bool ffRegReadUint64(HKEY hKey, const wchar_t* valueNameW, uint64_t* result, FFstrbuf* error); bool ffRegGetSubKey(HKEY hKey, uint32_t index, FFstrbuf* result, FFstrbuf* error);