Skip to content

Commit

Permalink
[FancyZones]Fallback and fixes for GetDisplays
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimecbernardo committed Jul 12, 2022
1 parent d37bab3 commit 4f8463a
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions src/modules/fancyzones/FancyZonesLib/MonitorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,32 +230,61 @@ namespace MonitorUtils
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;

while(EnumDisplayDevicesW(monitorInfo.szDevice, displayDeviceIdxMap[monitorInfo.szDevice], &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;
}
}

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 4f8463a

Please sign in to comment.