Skip to content

Commit

Permalink
Use better IDs on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
hensm committed Jun 14, 2018
1 parent 349d0a7 commit 2d3fb14
Showing 1 changed file with 62 additions and 33 deletions.
95 changes: 62 additions & 33 deletions ddcci.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,70 @@ void populateMonitorMap() {
EnumDisplayMonitors(NULL, NULL, &monitorEnumProc
, reinterpret_cast<LPARAM>(&monitorHandles));

for (auto const& handle : monitorHandles) {
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(handle, &monitorInfo);

DWORD numPhysicalMonitors;
LPPHYSICAL_MONITOR physicalMonitors = NULL;

if (!GetNumberOfPhysicalMonitorsFromHMONITOR(handle
, &numPhysicalMonitors)) {
throw std::runtime_error("Failed to get number of physical monitors");
break;
DISPLAY_DEVICE displayAdapter;
displayAdapter.cb = sizeof(DISPLAY_DEVICE);

DWORD adapterIndex = 0;
while (EnumDisplayDevices(0, adapterIndex, &displayAdapter, 0)) {
DISPLAY_DEVICE displayMonitor;
displayMonitor.cb = sizeof(DISPLAY_DEVICE);

DWORD monitorIndex = 0;
while (EnumDisplayDevices(displayAdapter.DeviceName, monitorIndex
, &displayMonitor, EDD_GET_DEVICE_INTERFACE_NAME)) {
if (!(displayMonitor.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER)) {

for (auto const& handle : monitorHandles) {
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(handle, &monitorInfo);

DWORD numPhysicalMonitors;
LPPHYSICAL_MONITOR physicalMonitors = NULL;

if (!GetNumberOfPhysicalMonitorsFromHMONITOR(handle
, &numPhysicalMonitors)) {
throw std::runtime_error("Failed to get number of physical monitors");
break;
}

physicalMonitors = new PHYSICAL_MONITOR[numPhysicalMonitors];
if (physicalMonitors == NULL) {
throw std::runtime_error("Failed to allocate monitor array");
break;
}

if (!GetPhysicalMonitorsFromHMONITOR(
handle, numPhysicalMonitors, physicalMonitors)) {
throw std::runtime_error("Failed to get physical monitors");
break;
}

for (DWORD i = 0; i < numPhysicalMonitors; i++) {
std::string monitorName =
static_cast<std::string>(monitorInfo.szDevice)
+ "\\Monitor"
+ std::to_string(i);

std::string deviceName =
static_cast<std::string>(displayMonitor.DeviceName);

if (monitorName == deviceName) {
monitorMap.insert({
static_cast<std::string>(displayMonitor.DeviceID)
, physicalMonitors[i].hPhysicalMonitor
});
}
}

delete[] physicalMonitors;
}
}

monitorIndex++;
}

physicalMonitors = new PHYSICAL_MONITOR[numPhysicalMonitors];
if (physicalMonitors == NULL) {
throw std::runtime_error("Failed to allocate monitor array");
break;
}

if (!GetPhysicalMonitorsFromHMONITOR(
handle, numPhysicalMonitors, physicalMonitors)) {
throw std::runtime_error("Failed to get physical monitors");
break;
}

std::string monitorName =
static_cast<std::string>(monitorInfo.szDevice);

monitorMap.insert({
monitorName
, physicalMonitors[0].hPhysicalMonitor
});

delete physicalMonitors;
adapterIndex++;
}
}

Expand Down

0 comments on commit 2d3fb14

Please sign in to comment.