Skip to content

Commit

Permalink
[FancyZones] Retry monitor identification attempt (microsoft#27005)
Browse files Browse the repository at this point in the history
* retry monitor identification

* check displays after retries

* reduce waiting time

* 30ms waiting time

* keep fallback values
  • Loading branch information
SeraphimaZykova committed Jun 23, 2023
1 parent 9511d17 commit 8dcdcba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
33 changes: 23 additions & 10 deletions src/modules/fancyzones/FancyZonesLib/MonitorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ namespace MonitorUtils
return result;
}

std::vector<FancyZonesDataTypes::MonitorId> GetDisplays()
std::pair<bool, std::vector<FancyZonesDataTypes::MonitorId>> GetDisplays()
{
auto allMonitors = FancyZonesUtils::GetAllMonitorInfo<&MONITORINFOEX::rcWork>();
bool success = true;
std::vector<FancyZonesDataTypes::MonitorId> result{};


auto allMonitors = FancyZonesUtils::GetAllMonitorInfo<&MONITORINFOEX::rcWork>();
for (auto& monitorData : allMonitors)
{
auto monitorInfo = monitorData.second;
Expand Down Expand Up @@ -269,10 +270,12 @@ namespace MonitorUtils
}
else
{
// Use the display name when no proper device was found.
success = false;

// Use the display name as a fallback value 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
Expand All @@ -289,7 +292,7 @@ namespace MonitorUtils
result.push_back(std::move(monitorId));
}

return result;
return {success, result};
}
}

Expand Down Expand Up @@ -367,12 +370,22 @@ namespace MonitorUtils
{
Logger::info(L"Identifying monitors");

auto displays = Display::GetDisplays();
auto displaysResult = Display::GetDisplays();
auto monitors = WMI::GetHardwareMonitorIds();


// retry
int retryCounter = 0;
while (!displaysResult.first && retryCounter < 100)
{
Logger::info("Retry display identification");
std::this_thread::sleep_for(std::chrono::milliseconds(30));
displaysResult = Display::GetDisplays();
retryCounter++;
}

for (const auto& monitor : monitors)
{
for (auto& display : displays)
for (auto& display : displaysResult.second)
{
if (monitor.deviceId.id == display.deviceId.id)
{
Expand All @@ -381,7 +394,7 @@ namespace MonitorUtils
}
}

return displays;
return displaysResult.second;
}

FancyZonesUtils::Rect GetWorkAreaRect(HMONITOR monitor)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fancyzones/FancyZonesLib/MonitorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MonitorUtils
{
namespace Display
{
std::vector<FancyZonesDataTypes::MonitorId> GetDisplays();
std::pair<bool, std::vector<FancyZonesDataTypes::MonitorId>> GetDisplays();
FancyZonesDataTypes::DeviceId SplitDisplayDeviceId(const std::wstring& str) noexcept;
FancyZonesDataTypes::DeviceId ConvertObsoleteDeviceId(const std::wstring& str) noexcept;
}
Expand Down

0 comments on commit 8dcdcba

Please sign in to comment.