Skip to content

Commit

Permalink
[FancyZones]Fallback and fixes for GetDisplays (#19380)
Browse files Browse the repository at this point in the history
* [FancyZones]Fallback and fixes for GetDisplays

* fix infinite cycle
  • Loading branch information
jaimecbernardo committed Jul 13, 2022
1 parent d37bab3 commit 0fbec1c
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions src/modules/fancyzones/FancyZonesLib/MonitorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,40 +222,71 @@ namespace MonitorUtils
std::vector<FancyZonesDataTypes::MonitorId> GetDisplays()
{
auto allMonitors = FancyZonesUtils::GetAllMonitorInfo<&MONITORINFOEX::rcWork>();
std::unordered_map<std::wstring, DWORD> displayDeviceIdxMap;
std::vector<FancyZonesDataTypes::MonitorId> result{};

for (auto& monitorData : allMonitors)
{
auto monitorInfo = monitorData.second;

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
};

bool foundActiveMonitor = false;

DWORD displayDeviceIndex = 0;

while (EnumDisplayDevicesW(monitorInfo.szDevice, displayDeviceIndex, &displayDevice, EDD_GET_DEVICE_INTERFACE_NAME))
{
Logger::error(L"EnumDisplayDevicesW error: {}", get_last_error_or_default(GetLastError()));
continue;
Logger::info(L"Get display device for display {} : {}", monitorInfo.szDevice, displayDevice.DeviceID);
if (WI_IsFlagSet(displayDevice.StateFlags, DISPLAY_DEVICE_ACTIVE) &&
WI_IsFlagClear(displayDevice.StateFlags, DISPLAY_DEVICE_MIRRORING_DRIVER))
{
// Find display devices associated with the display.
foundActiveMonitor = true;
break;
}
displayDeviceIndex++;
}

Logger::info(L"Display: {}, number: {}", displayDevice.DeviceID);
FancyZonesDataTypes::MonitorId id{ .monitor = monitorData.first, .deviceId = SplitDisplayDeviceId(displayDevice.DeviceID) };

try
if (foundActiveMonitor)
{
std::wstring numberStr = displayDevice.DeviceName; // \\.\DISPLAY1\Monitor0
numberStr = numberStr.substr(0, numberStr.find_last_of('\\')); // \\.\DISPLAY1
numberStr = remove_non_digits(numberStr);
id.deviceId.number = std::stoi(numberStr);
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;
}
}
catch (...)
else
{
Logger::error(L"Failed to get monitor number from {}", displayDevice.DeviceName);
// 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(id));

result.push_back(std::move(monitorId));
}

return result;
Expand Down

0 comments on commit 0fbec1c

Please sign in to comment.