Skip to content

Commit 46b0c6e

Browse files
committed
Monitor (Windows): code refactor
1 parent 519f3bf commit 46b0c6e

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/detection/monitor/monitor_windows.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "common/io/io.h"
44
#include "util/edidHelper.h"
5+
#include "util/mallocHelper.h"
56
#include "util/stringUtils.h"
67
#include "util/windows/registry.h"
78
#include "util/windows/unicode.h"
@@ -30,15 +31,20 @@ const char* ffDetectMonitor(FFlist* results)
3031
FF_HKEY_AUTO_DESTROY hKey = SetupDiOpenDevRegKey(hdev, &did, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
3132
if (!hKey) continue;
3233

33-
uint8_t edidData[256] = {};
34-
if (!ffRegReadData(hKey, L"EDID", edidData, sizeof(edidData), NULL)) continue;
34+
FF_AUTO_FREE uint8_t* edidData = NULL;
35+
uint32_t edidLength = 0;
36+
if (!ffRegReadData(hKey, L"EDID", &edidData, &edidLength, NULL)) continue;
37+
if (edidLength == 0 || edidLength % 128 != 0)
38+
continue;
39+
3540
uint32_t width, height;
3641
ffEdidGetPhysicalResolution(edidData, &width, &height);
3742
if (width == 0 || height == 0) continue;
3843

3944
FFMonitorResult* display = (FFMonitorResult*) ffListAdd(results);
4045
display->width = width;
4146
display->height = height;
47+
display->hdrCompatible = ffEdidGetHdrCompatible(edidData, edidLength); // Doesn't work. edidLength is always 128
4248
ffStrbufInit(&display->name);
4349
ffEdidGetName(edidData, &display->name);
4450
ffEdidGetPhysicalSize(edidData, &display->physicalWidth, &display->physicalHeight);

src/util/windows/registry.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,39 @@ bool ffRegReadStrbuf(HKEY hKey, const wchar_t* valueNameW, FFstrbuf* result, FFs
6464
return true;
6565
}
6666

67-
bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t* result, uint32_t bufSize, FFstrbuf* error)
67+
bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t** result, uint32_t* length, FFstrbuf* error)
6868
{
69-
static_assert(sizeof(DWORD) == sizeof(uint32_t), "");
70-
LONG err = RegGetValueW(hKey, NULL, valueNameW, RRF_RT_REG_BINARY, NULL, result, (DWORD*) &bufSize);
69+
assert(result && length);
70+
DWORD bufSize = 0;
71+
LONG err = RegGetValueW(hKey, NULL, valueNameW, RRF_RT_REG_BINARY, NULL, NULL, &bufSize);
72+
if(err != ERROR_SUCCESS || bufSize == 0)
73+
{
74+
if(error)
75+
{
76+
if(!valueNameW)
77+
valueNameW = L"(default)";
78+
FF_STRBUF_AUTO_DESTROY valueNameA = ffStrbufCreateWS(valueNameW);
79+
ffStrbufAppendF(error, "RegGetValueW(%s, NULL, RRF_RT_REG_BINARY, NULL, NULL, &bufSize) failed", valueNameA.chars);
80+
}
81+
return false;
82+
}
83+
84+
uint8_t* buf = (uint8_t*) malloc(bufSize);
85+
err = RegGetValueW(hKey, NULL, valueNameW, RRF_RT_REG_BINARY, NULL, buf, &bufSize);
7186
if(err != ERROR_SUCCESS)
7287
{
7388
if(error)
7489
{
7590
if(!valueNameW)
7691
valueNameW = L"(default)";
7792
FF_STRBUF_AUTO_DESTROY valueNameA = ffStrbufCreateWS(valueNameW);
78-
ffStrbufAppendF(error, "RegGetValueW(%s, NULL, RRF_RT_REG_SZ) failed", valueNameA.chars);
93+
ffStrbufAppendF(error, "RegGetValueW(%s, NULL, RRF_RT_REG_BINARY, NULL, length) failed", valueNameA.chars);
7994
}
95+
free(buf);
8096
return false;
8197
}
98+
*result = buf;
99+
*length = bufSize;
82100
return true;
83101
}
84102

src/util/windows/registry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static inline void wrapRegCloseKey(HKEY* phKey)
1717

1818
bool ffRegOpenKeyForRead(HKEY hKey, const wchar_t* subKeyW, HKEY* result, FFstrbuf* error);
1919
bool ffRegReadStrbuf(HKEY hKey, const wchar_t* valueNameW, FFstrbuf* result, FFstrbuf* error);
20-
bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t* result, uint32_t bufSize, FFstrbuf* error);
20+
bool ffRegReadData(HKEY hKey, const wchar_t* valueNameW, uint8_t** result, uint32_t* length, FFstrbuf* error);
2121
bool ffRegReadUint(HKEY hKey, const wchar_t* valueNameW, uint32_t* result, FFstrbuf* error);
2222
bool ffRegReadUint64(HKEY hKey, const wchar_t* valueNameW, uint64_t* result, FFstrbuf* error);
2323
bool ffRegGetSubKey(HKEY hKey, uint32_t index, FFstrbuf* result, FFstrbuf* error);

0 commit comments

Comments
 (0)