NativeResolution (Windows): add support

This commit is contained in:
李通洲 2023-08-08 23:19:22 +08:00
parent 5c603145ab
commit 7598e57704
No known key found for this signature in database
GPG Key ID: 6E72B663408769DE
5 changed files with 70 additions and 3 deletions

View File

@ -588,7 +588,7 @@ elseif(WIN32)
src/detection/gamepad/gamepad_windows.c
src/detection/media/media_nosupport.c
src/detection/memory/memory_windows.c
src/detection/nativeresolution/nativeresolution_nosupport.c
src/detection/nativeresolution/nativeresolution_windows.c
src/detection/opengl/opengl_windows.c
src/detection/os/os_windows.cpp
src/detection/packages/packages_windows.c

View File

@ -0,0 +1,48 @@
#include "nativeresolution.h"
#include "common/io/io.h"
#include "util/edidHelper.h"
#include "util/stringUtils.h"
#include "util/windows/registry.h"
#include "util/windows/unicode.h"
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
static inline void wrapSetupDiDestroyDeviceInfoList(HDEVINFO* hdev)
{
if(*hdev)
SetupDiDestroyDeviceInfoList(*hdev);
}
const char* ffDetectNativeResolution(FFlist* results)
{
//https://learn.microsoft.com/en-us/windows/win32/power/enumerating-battery-devices
HDEVINFO hdev __attribute__((__cleanup__(wrapSetupDiDestroyDeviceInfoList))) =
SetupDiGetClassDevsW(&GUID_DEVCLASS_MONITOR, 0, 0, DIGCF_PRESENT);
if(hdev == INVALID_HANDLE_VALUE)
return "SetupDiGetClassDevsW(&GUID_DEVCLASS_MONITOR) failed";
SP_DEVINFO_DATA did = { .cbSize = sizeof(did) };
for (DWORD idev = 0; SetupDiEnumDeviceInfo(hdev, idev, &did); ++idev)
{
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;
uint32_t width, height;
ffEdidGetNativeResolution(edidData, &width, &height);
if (width == 0 || height == 0) continue;
wchar_t wName[MAX_PATH] = {}; // MONITOR\BOE09F9
if (!SetupDiGetDeviceRegistryPropertyW(hdev, &did, SPDRP_HARDWAREID, NULL, (BYTE*) wName, sizeof(wName), NULL)) continue;
FFNativeResolutionResult* display = (FFNativeResolutionResult*) ffListAdd(results);
display->width = width;
display->height = height;
ffStrbufInitWS(&display->name, wcschr(wName, L'\\') + 1);
}
return NULL;
}

View File

@ -3,6 +3,6 @@
void ffEdidGetNativeResolution(uint8_t edid[128], uint32_t* width, uint32_t* height)
{
const int dtd = 54;
*width = ((edid[dtd + 4] >> 4) << 8) | edid[dtd + 2];
*height = ((edid[dtd + 7] >> 4) << 8) | edid[dtd + 5];
*width = (((uint32_t) edid[dtd + 4] >> 4) << 8) | edid[dtd + 2];
*height = (((uint32_t) edid[dtd + 7] >> 4) << 8) | edid[dtd + 5];
}

View File

@ -64,6 +64,24 @@ 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)
{
static_assert(sizeof(DWORD) == sizeof(uint32_t), "");
LONG err = RegGetValueW(hKey, NULL, valueNameW, RRF_RT_REG_BINARY, NULL, result, (DWORD*) &bufSize);
if(err != ERROR_SUCCESS)
{
if(error)
{
if(!valueNameW)
valueNameW = L"(default)";
FF_STRBUF_AUTO_DESTROY valueNameA = ffStrbufCreateWS(valueNameW);
ffStrbufAppendF(error, "RegGetValueW(%s, NULL, RRF_RT_REG_SZ) failed", valueNameA.chars);
}
return false;
}
return true;
}
bool ffRegReadUint(HKEY hKey, const wchar_t* valueNameW, uint32_t* result, FFstrbuf* error)
{
DWORD bufSize = sizeof(*result);

View File

@ -17,6 +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 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);