[idd] ensure the only active monitor is the IDD
Some checks failed
build / client (Debug, map[cc:clang cxx:clang++], libdecor) (push) Has been cancelled
build / client (Debug, map[cc:clang cxx:clang++], xdg-shell) (push) Has been cancelled
build / client (Debug, map[cc:gcc cxx:g++], libdecor) (push) Has been cancelled
build / client (Debug, map[cc:gcc cxx:g++], xdg-shell) (push) Has been cancelled
build / client (Release, map[cc:clang cxx:clang++], libdecor) (push) Has been cancelled
build / client (Release, map[cc:clang cxx:clang++], xdg-shell) (push) Has been cancelled
build / client (Release, map[cc:gcc cxx:g++], libdecor) (push) Has been cancelled
build / client (Release, map[cc:gcc cxx:g++], xdg-shell) (push) Has been cancelled
build / module (push) Has been cancelled
build / host-linux (push) Has been cancelled
build / host-windows-cross (push) Has been cancelled
build / host-windows-native (push) Has been cancelled
build / idd (push) Has been cancelled
build / obs (clang) (push) Has been cancelled
build / obs (gcc) (push) Has been cancelled
build / docs (push) Has been cancelled

This commit is contained in:
Geoffrey McRae
2026-07-18 22:20:36 +10:00
parent 83209fd3a9
commit ab96c7ea81
5 changed files with 256 additions and 7 deletions

View File

@@ -24,6 +24,83 @@
#include <setupapi.h>
#include <tchar.h>
#include <vector>
namespace
{
struct DisplayState
{
DISPLAY_DEVICE device;
DEVMODE mode;
bool isLG;
};
bool IsLGDisplay(const DISPLAY_DEVICE& device)
{
static const TCHAR deviceId[] = _T("ROOT\\LGIDD");
const size_t deviceIdLength = _countof(deviceId) - 1;
if (_tcsnicmp(device.DeviceID, deviceId, deviceIdLength) == 0 &&
(device.DeviceID[deviceIdLength] == _T('\\') ||
device.DeviceID[deviceIdLength] == _T('\0')))
return true;
if (_tcsicmp(device.DeviceString,
_T("Looking Glass Indirect Display Device")) == 0)
return true;
DISPLAY_DEVICE monitor = {};
monitor.cb = sizeof(monitor);
for (DWORD i = 0; EnumDisplayDevices(device.DeviceName, i, &monitor, 0); ++i)
{
if (_tcsnicmp(monitor.DeviceID, _T("MONITOR\\LGD1DDD"), 15) == 0 ||
_tcsicmp(monitor.DeviceString, _T("Looking Glass")) == 0)
return true;
monitor = {};
monitor.cb = sizeof(monitor);
}
return false;
}
bool GetDisplayStates(std::vector<DisplayState>& displays, size_t& lgIndex)
{
lgIndex = SIZE_MAX;
DISPLAY_DEVICE device = {};
device.cb = sizeof(device);
for (DWORD i = 0; EnumDisplayDevices(NULL, i, &device, 0); ++i)
{
if ((device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) &&
!(device.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
{
DisplayState state = {};
state.device = device;
state.mode.dmSize = sizeof(state.mode);
state.isLG = IsLGDisplay(device);
if (!EnumDisplaySettingsEx(device.DeviceName, ENUM_CURRENT_SETTINGS,
&state.mode, 0))
{
DEBUG_WARN_HR(GetLastError(),
"Failed to query the current mode for %ls", device.DeviceName);
return false;
}
if (state.isLG && lgIndex == SIZE_MAX)
lgIndex = displays.size();
displays.emplace_back(state);
}
device = {};
device.cb = sizeof(device);
}
return lgIndex != SIZE_MAX;
}
}
CPipeClient g_pipe;
@@ -181,6 +258,115 @@ void CPipeClient::ReloadSettings()
WriteMsg(msg);
}
bool CPipeClient::EnsureOnlyDisplayLocked()
{
std::vector<DisplayState> displays;
size_t lgIndex;
if (!GetDisplayStates(displays, lgIndex))
return false;
// The only active display is necessarily the primary display.
if (displays.size() == 1)
return true;
for (unsigned int attempt = 0; attempt < 3; ++attempt)
{
UINT32 pathCount = 0;
UINT32 modeCount = 0;
LONG result = GetDisplayConfigBufferSizes(
QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount);
if (result != ERROR_SUCCESS)
{
DEBUG_ERROR("GetDisplayConfigBufferSizes failed (%ld)", result);
return false;
}
std::vector<DISPLAYCONFIG_PATH_INFO> paths(pathCount);
std::vector<DISPLAYCONFIG_MODE_INFO> modes(modeCount);
result = QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS,
&pathCount, paths.data(), &modeCount, modes.data(), NULL);
if (result == ERROR_INSUFFICIENT_BUFFER)
continue;
if (result != ERROR_SUCCESS)
{
DEBUG_ERROR("QueryDisplayConfig failed (%ld)", result);
return false;
}
paths.resize(pathCount);
modes.resize(modeCount);
for (size_t i = 0; i < paths.size(); ++i)
{
DISPLAYCONFIG_SOURCE_DEVICE_NAME sourceName = {};
sourceName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
sourceName.header.size = sizeof(sourceName);
sourceName.header.adapterId = paths[i].sourceInfo.adapterId;
sourceName.header.id = paths[i].sourceInfo.id;
result = DisplayConfigGetDeviceInfo(&sourceName.header);
if (result != ERROR_SUCCESS)
continue;
if (_tcsicmp(sourceName.viewGdiDeviceName,
displays[lgIndex].device.DeviceName) != 0)
continue;
const UINT32 sourceModeIndex = paths[i].sourceInfo.modeInfoIdx;
const UINT32 targetModeIndex = paths[i].targetInfo.modeInfoIdx;
if (sourceModeIndex == DISPLAYCONFIG_PATH_MODE_IDX_INVALID ||
sourceModeIndex >= modes.size() ||
targetModeIndex == DISPLAYCONFIG_PATH_MODE_IDX_INVALID ||
targetModeIndex >= modes.size() ||
modes[sourceModeIndex].infoType != DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE ||
modes[targetModeIndex].infoType != DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
{
DEBUG_ERROR("Looking Glass display path has invalid mode indices");
return false;
}
DISPLAYCONFIG_PATH_INFO path = paths[i];
DISPLAYCONFIG_MODE_INFO selectedModes[2] = {
modes[sourceModeIndex], modes[targetModeIndex]
};
selectedModes[0].sourceMode.position.x = 0;
selectedModes[0].sourceMode.position.y = 0;
path.sourceInfo.modeInfoIdx = 0;
path.targetInfo.modeInfoIdx = 1;
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
result = SetDisplayConfig(1, &path, 2, selectedModes,
SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG |
SDC_ALLOW_CHANGES | SDC_SAVE_TO_DATABASE);
if (result != ERROR_SUCCESS)
{
DEBUG_ERROR("Failed to apply the LG-only display topology (%ld)",
result);
return false;
}
DEBUG_INFO("Looking Glass display set as the only active display");
return true;
}
DEBUG_ERROR("Looking Glass display configuration path not found");
return false;
}
DEBUG_WARN("Display topology kept changing while selecting Looking Glass");
return false;
}
bool CPipeClient::EnsureOnlyDisplay()
{
AcquireSRWLockExclusive(&m_displayLock);
const bool result = EnsureOnlyDisplayLocked();
ReleaseSRWLockExclusive(&m_displayLock);
return result;
}
void CPipeClient::Thread()
{
DEBUG_INFO("Pipe thread started");
@@ -305,16 +491,32 @@ void CPipeClient::HandleSetCursorPos(const LGPipeMsg& msg)
void CPipeClient::HandleSetDisplayMode(const LGPipeMsg& msg)
{
DEVMODE dm = {};
dm.dmSize = sizeof(dm);
AcquireSRWLockExclusive(&m_displayLock);
std::vector<DisplayState> displays;
size_t lgIndex;
if (!GetDisplayStates(displays, lgIndex))
{
ReleaseSRWLockExclusive(&m_displayLock);
DEBUG_ERROR("Looking Glass display not found while setting its mode");
return;
}
DEVMODE dm = displays[lgIndex].mode;
dm.dmPelsWidth = msg.displayMode.width;
dm.dmPelsHeight = msg.displayMode.height;
dm.dmDisplayFrequency = msg.displayMode.refresh;
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
LONG result = ChangeDisplaySettingsEx(NULL, &dm, NULL, CDS_UPDATEREGISTRY, NULL);
LONG result = ChangeDisplaySettingsEx(displays[lgIndex].device.DeviceName,
&dm, NULL, CDS_UPDATEREGISTRY, NULL);
if (result != DISP_CHANGE_SUCCESSFUL)
DEBUG_ERROR("ChangeDisplaySettingsEx Failed (0x%08x)", result);
ReleaseSRWLockExclusive(&m_displayLock);
if (result == DISP_CHANGE_SUCCESSFUL)
EnsureOnlyDisplay();
}
void CPipeClient::HandleGPUStatus(const LGPipeMsg& msg)