| 12 | #include <dwmapi.h> |
| 13 | |
| 14 | static std::optional<float> GetRefreshRateFromDisplayConfig(HWND hwnd) |
| 15 | { |
| 16 | // Partially based on Chromium ui/display/win/display_config_helper.cc. |
| 17 | const HMONITOR monitor = MonitorFromWindow(hwnd, 0); |
| 18 | if (!monitor) [[unlikely]] |
| 19 | { |
| 20 | ERROR_LOG("{}() failed: {}", "MonitorFromWindow", Error::CreateWin32(GetLastError()).GetDescription()); |
| 21 | return std::nullopt; |
| 22 | } |
| 23 | |
| 24 | MONITORINFOEXW mi = {}; |
| 25 | mi.cbSize = sizeof(mi); |
| 26 | if (!GetMonitorInfoW(monitor, &mi)) |
| 27 | { |
| 28 | ERROR_LOG("{}() failed: {}", "GetMonitorInfoW", Error::CreateWin32(GetLastError()).GetDescription()); |
| 29 | return std::nullopt; |
| 30 | } |
| 31 | |
| 32 | DynamicHeapArray<DISPLAYCONFIG_PATH_INFO> path_info; |
| 33 | DynamicHeapArray<DISPLAYCONFIG_MODE_INFO> mode_info; |
| 34 | |
| 35 | // I guess this could fail if it changes inbetween two calls... unlikely. |
| 36 | for (;;) |
| 37 | { |
| 38 | UINT32 path_size = 0, mode_size = 0; |
| 39 | LONG res = GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &path_size, &mode_size); |
| 40 | if (res != ERROR_SUCCESS) |
| 41 | { |
| 42 | ERROR_LOG("{}() failed: {}", "GetDisplayConfigBufferSizes", Error::CreateWin32(res).GetDescription()); |
| 43 | return std::nullopt; |
| 44 | } |
| 45 | |
| 46 | path_info.resize(path_size); |
| 47 | mode_info.resize(mode_size); |
| 48 | res = |
| 49 | QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &path_size, path_info.data(), &mode_size, mode_info.data(), nullptr); |
| 50 | if (res == ERROR_SUCCESS) |
| 51 | break; |
| 52 | if (res != ERROR_INSUFFICIENT_BUFFER) |
| 53 | { |
| 54 | ERROR_LOG("{}() failed: {}", "QueryDisplayConfig", Error::CreateWin32(res).GetDescription()); |
| 55 | return std::nullopt; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | for (const DISPLAYCONFIG_PATH_INFO& pi : path_info) |
| 60 | { |
| 61 | DISPLAYCONFIG_SOURCE_DEVICE_NAME sdn = {.header = {.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME, |
| 62 | .size = sizeof(DISPLAYCONFIG_SOURCE_DEVICE_NAME), |
| 63 | .adapterId = pi.sourceInfo.adapterId, |
| 64 | .id = pi.sourceInfo.id}}; |
| 65 | LONG res = DisplayConfigGetDeviceInfo(&sdn.header); |
| 66 | if (res != ERROR_SUCCESS) |
| 67 | { |
| 68 | ERROR_LOG("{}() failed: {}", "DisplayConfigGetDeviceInfo", Error::CreateWin32(res).GetDescription()); |
| 69 | continue; |
| 70 | } |
| 71 |
no test coverage detected