1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
#include "envmetrics.h"
#include "env.h"
#include <QScreen>
#include <Windows.h>
#include <log.h>
#include <shellscalingapi.h>
#include <utility.h>
namespace env
{
using namespace MOBase;
// fallback for windows 7
//
int getDesktopDpi()
{
// desktop DC
DesktopDCPtr dc(GetDC(0));
if (!dc) {
const auto e = GetLastError();
log::error("can't get desktop DC, {}", formatSystemMessage(e));
return 0;
}
return GetDeviceCaps(dc.get(), LOGPIXELSX);
}
// finds a monitor by device name; there's no real good way to do that except
// by enumerating all the monitors and checking their name
//
HMONITOR findMonitor(const QString& name)
{
// passed to the enumeration callback
struct Data
{
QString name;
HMONITOR hm;
};
Data data = {name, 0};
// callback
auto callback = [](HMONITOR hm, HDC, RECT*, LPARAM lp) {
auto& data = *reinterpret_cast<Data*>(lp);
MONITORINFOEX mi = {};
mi.cbSize = sizeof(mi);
// monitor info will include the name
if (!GetMonitorInfoW(hm, &mi)) {
const auto e = GetLastError();
log::error("GetMonitorInfo() failed for '{}', {}", data.name,
formatSystemMessage(e));
// error for this monitor, but continue
return TRUE;
}
if (QString::fromWCharArray(mi.szDevice) == data.name) {
// found, stop
data.hm = hm;
return FALSE;
}
// not found, continue to the next monitor
return TRUE;
};
// for each monitor
EnumDisplayMonitors(0, nullptr, callback, reinterpret_cast<LPARAM>(&data));
return data.hm;
}
// returns the dpi for the given monitor; for systems that do not support
// per-monitor dpi (such as windows 7), this is the desktop dpi
//
int getDpi(const QString& monitorDevice)
{
using GetDpiForMonitorFunction =
HRESULT WINAPI(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*);
static LibraryPtr shcore;
static GetDpiForMonitorFunction* GetDpiForMonitor = nullptr;
static bool checked = false;
if (!checked) {
// try to find GetDpiForMonitor() from shcored.dll
shcore.reset(LoadLibraryW(L"Shcore.dll"));
if (shcore) {
// windows 8.1+ only
GetDpiForMonitor = reinterpret_cast<GetDpiForMonitorFunction*>(
GetProcAddress(shcore.get(), "GetDpiForMonitor"));
}
checked = true;
}
if (!GetDpiForMonitor) {
// get the desktop dpi instead
return getDesktopDpi();
}
// there's no way to get an HMONITOR from a device name, so all monitors
// will have to be enumerated and their name checked
HMONITOR hm = findMonitor(monitorDevice);
if (!hm) {
log::error("can't get dpi for monitor '{}', not found", monitorDevice);
return 0;
}
UINT dpiX = 0, dpiY = 0;
const auto r = GetDpiForMonitor(hm, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
if (FAILED(r)) {
log::error("GetDpiForMonitor() failed for '{}', {}", monitorDevice,
formatSystemMessage(r));
return 0;
}
// dpiX and dpiY are always identical, as per the documentation
return dpiX;
}
Display::Display(QString adapter, QString monitorDevice, bool primary)
: m_adapter(std::move(adapter)), m_monitorDevice(std::move(monitorDevice)),
m_primary(primary), m_resX(0), m_resY(0), m_dpi(0), m_refreshRate(0)
{
getSettings();
m_dpi = getDpi(m_monitorDevice);
}
const QString& Display::adapter() const
{
return m_adapter;
}
const QString& Display::monitorDevice() const
{
return m_monitorDevice;
}
bool Display::primary()
{
return m_primary;
}
int Display::resX() const
{
return m_resX;
}
int Display::resY() const
{
return m_resY;
}
int Display::dpi()
{
return m_dpi;
}
int Display::refreshRate() const
{
return m_refreshRate;
}
QString Display::toString() const
{
return QString("%1*%2 %3hz dpi=%4 on %5%6")
.arg(m_resX)
.arg(m_resY)
.arg(m_refreshRate)
.arg(m_dpi)
.arg(m_adapter)
.arg(m_primary ? " (primary)" : "");
}
void Display::getSettings()
{
DEVMODEW dm = {};
dm.dmSize = sizeof(dm);
const auto wsDevice = m_monitorDevice.toStdWString();
if (!EnumDisplaySettingsW(wsDevice.c_str(), ENUM_CURRENT_SETTINGS, &dm)) {
log::error("EnumDisplaySettings() failed for '{}'", m_monitorDevice);
return;
}
// all these fields should be available
if (dm.dmFields & DM_DISPLAYFREQUENCY) {
m_refreshRate = dm.dmDisplayFrequency;
}
if (dm.dmFields & DM_PELSWIDTH) {
m_resX = dm.dmPelsWidth;
}
if (dm.dmFields & DM_PELSHEIGHT) {
m_resY = dm.dmPelsHeight;
}
}
Metrics::Metrics()
{
getDisplays();
}
const std::vector<Display>& Metrics::displays() const
{
return m_displays;
}
QRect Metrics::desktopGeometry() const
{
QRect r;
for (auto* s : QGuiApplication::screens()) {
r = r.united(s->geometry());
}
return r;
}
void Metrics::getDisplays()
{
// don't bother if it goes over 100
for (int i = 0; i < 100; ++i) {
DISPLAY_DEVICEW device = {};
device.cb = sizeof(device);
if (!EnumDisplayDevicesW(nullptr, i, &device, 0)) {
// no more
break;
}
// EnumDisplayDevices() seems to be returning a lot of devices that are
// not actually monitors, but those don't have the
// DISPLAY_DEVICE_ATTACHED_TO_DESKTOP bit set
if ((device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) == 0) {
continue;
}
m_displays.emplace_back(QString::fromWCharArray(device.DeviceString),
QString::fromWCharArray(device.DeviceName),
(device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE));
}
}
} // namespace env
|