mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[FancyZones]Fallback and fixes for GetDisplays (#19380)
* [FancyZones]Fallback and fixes for GetDisplays * fix infinite cycle
This commit is contained in:
@@ -222,7 +222,6 @@ namespace MonitorUtils
|
|||||||
std::vector<FancyZonesDataTypes::MonitorId> GetDisplays()
|
std::vector<FancyZonesDataTypes::MonitorId> GetDisplays()
|
||||||
{
|
{
|
||||||
auto allMonitors = FancyZonesUtils::GetAllMonitorInfo<&MONITORINFOEX::rcWork>();
|
auto allMonitors = FancyZonesUtils::GetAllMonitorInfo<&MONITORINFOEX::rcWork>();
|
||||||
std::unordered_map<std::wstring, DWORD> displayDeviceIdxMap;
|
|
||||||
std::vector<FancyZonesDataTypes::MonitorId> result{};
|
std::vector<FancyZonesDataTypes::MonitorId> result{};
|
||||||
|
|
||||||
for (auto& monitorData : allMonitors)
|
for (auto& monitorData : allMonitors)
|
||||||
@@ -230,32 +229,64 @@ namespace MonitorUtils
|
|||||||
auto monitorInfo = monitorData.second;
|
auto monitorInfo = monitorData.second;
|
||||||
|
|
||||||
DISPLAY_DEVICE displayDevice{ .cb = sizeof(displayDevice) };
|
DISPLAY_DEVICE displayDevice{ .cb = sizeof(displayDevice) };
|
||||||
std::wstring deviceId;
|
|
||||||
auto enumRes = EnumDisplayDevicesW(monitorInfo.szDevice, displayDeviceIdxMap[monitorInfo.szDevice], &displayDevice, EDD_GET_DEVICE_INTERFACE_NAME);
|
|
||||||
|
|
||||||
if (!enumRes)
|
FancyZonesDataTypes::MonitorId monitorId {
|
||||||
{
|
.monitor = monitorData.first
|
||||||
Logger::error(L"EnumDisplayDevicesW error: {}", get_last_error_or_default(GetLastError()));
|
};
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::info(L"Display: {}, number: {}", displayDevice.DeviceID);
|
|
||||||
FancyZonesDataTypes::MonitorId id{ .monitor = monitorData.first, .deviceId = SplitDisplayDeviceId(displayDevice.DeviceID) };
|
|
||||||
|
|
||||||
try
|
bool foundActiveMonitor = false;
|
||||||
|
|
||||||
|
DWORD displayDeviceIndex = 0;
|
||||||
|
|
||||||
|
while (EnumDisplayDevicesW(monitorInfo.szDevice, displayDeviceIndex, &displayDevice, EDD_GET_DEVICE_INTERFACE_NAME))
|
||||||
{
|
{
|
||||||
std::wstring numberStr = displayDevice.DeviceName; // \\.\DISPLAY1\Monitor0
|
Logger::info(L"Get display device for display {} : {}", monitorInfo.szDevice, displayDevice.DeviceID);
|
||||||
numberStr = numberStr.substr(0, numberStr.find_last_of('\\')); // \\.\DISPLAY1
|
if (WI_IsFlagSet(displayDevice.StateFlags, DISPLAY_DEVICE_ACTIVE) &&
|
||||||
numberStr = remove_non_digits(numberStr);
|
WI_IsFlagClear(displayDevice.StateFlags, DISPLAY_DEVICE_MIRRORING_DRIVER))
|
||||||
id.deviceId.number = std::stoi(numberStr);
|
{
|
||||||
}
|
// Find display devices associated with the display.
|
||||||
catch (...)
|
foundActiveMonitor = true;
|
||||||
{
|
break;
|
||||||
Logger::error(L"Failed to get monitor number from {}", displayDevice.DeviceName);
|
}
|
||||||
|
displayDeviceIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push_back(std::move(id));
|
if (foundActiveMonitor)
|
||||||
|
{
|
||||||
|
monitorId.deviceId = SplitDisplayDeviceId(displayDevice.DeviceID);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::wstring numberStr = displayDevice.DeviceName; // \\.\DISPLAY1\Monitor0
|
||||||
|
numberStr = numberStr.substr(0, numberStr.find_last_of('\\')); // \\.\DISPLAY1
|
||||||
|
numberStr = remove_non_digits(numberStr);
|
||||||
|
monitorId.deviceId.number = std::stoi(numberStr);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
Logger::error(L"Failed to get monitor number from {}", displayDevice.DeviceName);
|
||||||
|
monitorId.deviceId.number = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Use the display name when no proper device was found.
|
||||||
|
monitorId.deviceId.id = monitorInfo.szDevice;
|
||||||
|
monitorId.deviceId.instanceId = L"";
|
||||||
|
Logger::info(L"No active monitor found for {} : {}", monitorInfo.szDevice, get_last_error_or_default(GetLastError()));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::wstring numberStr = monitorInfo.szDevice; // \\.\DISPLAY1
|
||||||
|
numberStr = remove_non_digits(numberStr);
|
||||||
|
monitorId.deviceId.number = std::stoi(numberStr);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
Logger::error(L"Failed to get display number from {}", monitorInfo.szDevice);
|
||||||
|
monitorId.deviceId.number = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push_back(std::move(monitorId));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user