-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdpiWindows10.js
More file actions
364 lines (323 loc) · 13.7 KB
/
dpiWindows10.js
File metadata and controls
364 lines (323 loc) · 13.7 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* DPI support for Windows 10
*
* Copyright 2016 Raising the Floor - US
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
"use strict";
/* There is no documented method to change the DPI in Windows 10. The Display Settings applet in Windows calls
* DisplayConfigSetDeviceInfo with an undocumented structure, which looks like the following:
*
* struct DISPLAYCONFIG_SET_DPI {
* DISPLAYCONFIG_DEVICE_INFO_HEADER header; // https://msdn.microsoft.com/en-us/library/windows/hardware/ff553920.aspx
* INT32 dpiOffset; // see below
* };
*
* The 'type' member of the header, which is used to identify the type of packet, is (uint) -4.
*
* The procedure to getting the current DPI is similar. The "official" way of getting it is using
* GetDeviceCaps(LOGPIXELS[X/Y]), however that appears to only return the DPI that was set at the time of login.
* The Display Settings applet uses DisplayConfigGetDeviceInfo, and the packet looks something like:
*
* struct DISPLAYCONFIG_GET_DPI {
* DISPLAYCONFIG_DEVICE_INFO_HEADER header; // https://msdn.microsoft.com/en-us/library/windows/hardware/ff553920.aspx
* INT32 minDpiOffset; // The minimum value of dpiOffset.
* INT32 dpiOffset; // see below
* INT32 maxDpiOffset; // The maximum value of dpiOffset, determined by the screen resolution.
* };
*
* The 'type' member of header is (uint) -3.
* maxDpiOffset is the maximum DPI of the current resolution, dpiOffset can be higher but not the actual DPI.
*
* The dpiOffset is the number of values away from the display's recommended value, and can be negative. The values
* (which are a percentage of 96 DPI), are:
* [ 100, 125, 150, 175, 200, 225, 250, 300, 350, 400, 450, 500 ]
* For example, if the recommended DPI is 175%, then a dpiOffset of 0 is 175, 2 is 225, and -1 is 150.
*/
var ref = require("ref");
var Struct = require("ref-struct");
var arrayType = require("ref-array");
var ffi = require("ffi-napi");
var fluid = require("gpii-universal");
var windows = fluid.registerNamespace("gpii.windows");
var t = windows.types;
var c = windows.API_constants;
// These API calls are for only Windows 10
var user32 = ffi.Library("user32", {
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff553903.aspx
"DisplayConfigGetDeviceInfo": [
// DISPLAYCONFIG_DEVICE_INFO_HEADER*
t.LONG, [ "pointer" ]
],
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff553909.aspx
"DisplayConfigSetDeviceInfo": [
// DISPLAYCONFIG_DEVICE_INFO_HEADER*
t.LONG, [ "pointer" ]
],
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff566772.aspx
"GetDisplayConfigBufferSizes": [
t.LONG, [ "uint32", "uint32*", "uint32*" ]
],
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff569215.aspx
"QueryDisplayConfig":[
t.LONG, [ "uint32", "uint32*", "pointer", "uint*", "pointer", "pointer" ]
]
});
// sizeof(DISPLAYCONFIG_MODE_INFO)
var DISPLAYCONFIG_MODE_INFO_SIZE = 64;
// THe types of packets send to DisplayConfigGetDeviceInfo/DisplayConfigSetDeviceInfo
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff553924.aspx
var DISPLAYCONFIG_DEVICE_INFO_TYPE = {
GET_SOURCE_NAME: 1,
GET_TARGET_NAME: 2,
GET_TARGET_PREFERRED_MODE: 3,
GET_ADAPTER_NAME: 4,
SET_TARGET_PERSISTENCE: 5,
GET_TARGET_BASE_TYPE: 6,
GET_SUPPORT_VIRTUAL_RESOLUTION: 7,
SET_SUPPORT_VIRTUAL_RESOLUTION: 8,
// undocumented:
GET_DPI: 0xfffffffd,
SET_DPI: 0xfffffffc
};
// https://msdn.microsoft.com/en-us/library/dd145066.aspx
windows.MONITORINFOEX = new Struct([
[t.DWORD, "cbSize"],
[arrayType(t.LONG, 4), "rcMonitor"],
[arrayType(t.LONG, 4), "rcWork"],
[t.DWORD, "dwFlags"],
[arrayType(t.TCHAR, c.CCHDEVICENAME), "szDevice"]
]);
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff553945.aspx
windows.DISPLAYCONFIG_PATH_INFO = new Struct([
[t.LUID, "source_adapterId"],
["uint32", "source_id"],
["uint32", "source_modeInfoIdx"],
["uint32", "source_statusFlags"],
[t.LUID, "target_adapterId"],
["uint32", "target_id"],
["uint32", "target_modeInfoIdx"],
["uint32", "target_outputTechnology"],
["uint32", "target_rotation"],
["uint32", "target_scaling"],
["uint32", "target_refreshRateNumerator"],
["uint32", "target_refreshRateDenominator"],
["uint32", "target_scanLineOrdering"],
[t.BOOL, "target_targetAvailable"],
["uint32", "target_statusFlags"],
["uint32", "flags"]
], { packed: true });
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff553933.aspx
// This structure serves no useful purpose (to GPII), so an anonymous blob of memory is used, instead of defining a
// handful of nested structures.
windows.DISPLAYCONFIG_MODE_INFO = new Struct([
[arrayType("char", DISPLAYCONFIG_MODE_INFO_SIZE), "reserved"]
]);
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff553920.aspx
windows.DISPLAYCONFIG_DEVICE_INFO_HEADER = new Struct([
// DISPLAYCONFIG_DEVICE_INFO_TYPE enum
["uint32", "type"],
["uint32", "size"],
[t.LUID, "adapterId"],
["uint32", "id"]
], { packed: true });
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff553983.aspx
windows.DISPLAYCONFIG_SOURCE_DEVICE_NAME = new Struct([
[windows.DISPLAYCONFIG_DEVICE_INFO_HEADER, "header"],
[arrayType(t.TCHAR, c.CCHDEVICENAME), "viewGdiDeviceName"]
]);
// This structure is not documented or defined in the SDK, but it's what control panel appears to use when querying the
// current DPI setting.
windows.DISPLAYCONFIG_GET_DPI = new Struct([
[windows.DISPLAYCONFIG_DEVICE_INFO_HEADER, "header"],
["int32", "minDpiOffset"],
["int32", "dpiOffset"],
["int32", "maxDpiOffset"]
], {packed: true});
// This structure is not documented or defined in the SDK, but it's what control panel appears to use when adjusting
// the DPI.
windows.DISPLAYCONFIG_SET_DPI = new Struct([
[windows.DISPLAYCONFIG_DEVICE_INFO_HEADER, "header"],
["int32", "dpiOffset"]
]);
/**
* Create and initialise a new new MONITORINFOEX structure
* @return {MONITORINFOEX} The structure.
*/
windows.display.createMonitorInfoStruct = function () {
var obj = new windows.MONITORINFOEX();
obj.ref().fill(0);
obj.cbSize = windows.MONITORINFOEX.size;
return obj;
};
/**
* Create and initialise a new DISPLAYCONFIG_SOURCE_DEVICE_NAME structure
* @return {DISPLAYCONFIG_SOURCE_DEVICE_NAME} The structure.
*/
windows.display.createSourceDeviceNameStruct = function () {
var obj = new windows.DISPLAYCONFIG_SOURCE_DEVICE_NAME();
obj.ref().fill(0);
obj.header.size = windows.DISPLAYCONFIG_SOURCE_DEVICE_NAME.size;
obj.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.GET_SOURCE_NAME;
return obj;
};
/**
* Create and initialise a new DISPLAYCONFIG_SET_DPI structure
* @return {DISPLAYCONFIG_SET_DPI} The structure.
*/
windows.display.createSetDpiStruct = function () {
var obj = new windows.DISPLAYCONFIG_SET_DPI();
obj.ref().fill(0);
obj.header.size = windows.DISPLAYCONFIG_SET_DPI.size;
obj.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.SET_DPI;
return obj;
};
/**
* Create and initialise a new DISPLAYCONFIG_GET_DPI structure
* @return {DISPLAYCONFIG_GET_DPI} The structure.
*/
windows.display.createGetDpiStruct = function () {
var obj = new windows.DISPLAYCONFIG_GET_DPI();
obj.ref().fill(0);
obj.header.size = windows.DISPLAYCONFIG_GET_DPI.size;
obj.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.GET_DPI;
return obj;
};
/**
* Gets the adapter and monitor (or socket?) id pair of a given device name, by iterating through all display
* configurations until there's a match.
* Inspired by https://msdn.microsoft.com/en-us/library/windows/hardware/dn690039.aspx
*
* @param {String} deviceName Device name of the display (eg, \\.\DISPLAY1).
* @return {Object} The adapterId and sourceId of the display.
*/
windows.display.getAdapter = function (deviceName) {
// Get the array sizes for the configuration
var pathCountPtr = ref.alloc("uint32");
var modeInfoCountPtr = ref.alloc("uint32");
var ret = user32.GetDisplayConfigBufferSizes(c.QDC_ONLY_ACTIVE_PATHS, pathCountPtr, modeInfoCountPtr);
windows.checkReturnCode(ret);
var pathCount = pathCountPtr.deref();
var modeInfoCount = modeInfoCountPtr.deref();
// Allocate the arrays
var PathInfoArray = arrayType(windows.DISPLAYCONFIG_PATH_INFO);
var ModeInfoArray = arrayType(windows.DISPLAYCONFIG_MODE_INFO);
var pathInfo = new PathInfoArray(pathCount);
var modeInfo = new ModeInfoArray(modeInfoCount);
// Get the display configurations
ret = user32.QueryDisplayConfig(c.QDC_ONLY_ACTIVE_PATHS, pathCountPtr, pathInfo.buffer, modeInfoCountPtr, modeInfo.buffer, ref.NULL);
windows.checkReturnCode(ret);
var adapterToGo = null;
// Find a matching display.
var sourceName = windows.display.createSourceDeviceNameStruct();
for (var n = 0; n < pathCount; n++) {
// Get the device name.
sourceName.header.adapterId = pathInfo[n].source_adapterId;
sourceName.header.id = pathInfo[n].source_id;
ret = user32.DisplayConfigGetDeviceInfo(sourceName.ref());
if (ret) {
// There was some problem with this display - ignore it and move on.
continue;
}
var name = windows.fromWideChar(sourceName.viewGdiDeviceName.buffer);
if (name === deviceName) {
// There could be more than one matching monitor (a clone). The first one is preferred, but if there's an
// internal/built-in display later, then let's use that one.
var isInternal =
(pathInfo[n].target_outputTechnology === c.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EMBEDDED) ||
(pathInfo[n].target_outputTechnology === c.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EMBEDDED) ||
(pathInfo[n].target_outputTechnology === c.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL);
if (adapterToGo === null || isInternal) {
adapterToGo = {
adapterId: sourceName.header.adapterId,
sourceId: sourceName.header.id
};
}
}
}
fluid.log("Display adapter " + deviceName, adapterToGo || "(null)");
return adapterToGo;
};
/**
* Gets the device name of the primary monitor. (eg, \\.\DISPLAY1)
* @return {String} The device name of the primary monitor.
*/
windows.display.getPrimaryMonitorName = function () {
var monitor = windows.user32.MonitorFromWindow(ref.NULL_POINTER, c.MONITOR_DEFAULTTOPRIMARY);
var monitorInfo = windows.display.createMonitorInfoStruct();
windows.user32.GetMonitorInfoW(monitor, monitorInfo.ref());
return windows.fromWideChar(monitorInfo.szDevice.buffer);
};
/**
* Sets the DPI scale of the primary display, by specifying the number of values away from an from the recommended
* setting.
*
* @param {Number} offset The offset from the recommended setting.
* @return {DpiConfig} The newly configured, actual, and minimum/maximum DPI offsets (see getScreenDpi).
*/
windows.display.setScreenDpi = function (offset) {
// Only change the primary monitor, because that's what setScreenResolution does.
var monitorName = windows.display.getPrimaryMonitorName();
var adapter = windows.display.getAdapter(monitorName);
var setPacket = windows.display.createSetDpiStruct();
setPacket.header.adapterId = adapter.adapterId;
setPacket.header.id = adapter.sourceId;
if (offset < -3) {
// There isn't a lower limit, so add an artificial bottom cap. If the display supports lower settings then it
// would probably be unusable beyond this point.
offset = -3;
}
setPacket.dpiOffset = offset;
var ret = user32.DisplayConfigSetDeviceInfo(setPacket.ref());
windows.checkReturnCode(ret);
// Return the new configuration.
return windows.display.getScreenDpi(adapter);
};
/**
* Get the configured, maximum, and actual DPI values of the primary display.
*
* The value is the number of "notches" away from the recommended setting of the display.
*
* The configured scale is what DPI should be if the resolution is high enough, the maximum scale is the highest DPI
* scale that the current screen resolution supports. The actual scale (what the user is looking at) is the configured
* scale, capped at the maximum.
*
* @param {Object} adapter [optional] The adapter id pair (from getAdapter()).
* @return {DpiConfig} The newly configured, actual, and minimum/maximum DPI offsets
*/
windows.display.getScreenDpi = function (adapter) {
if (!adapter) {
var monitorName = windows.display.getPrimaryMonitorName();
adapter = windows.display.getAdapter(monitorName);
}
var getPacket = windows.display.createGetDpiStruct();
getPacket.header.adapterId = adapter.adapterId;
getPacket.header.id = adapter.sourceId;
var ret = user32.DisplayConfigGetDeviceInfo(getPacket.ref());
windows.checkReturnCode(ret);
var configured = getPacket.dpiOffset;
var maximum = getPacket.maxDpiOffset;
return {
configured: configured,
minimum: getPacket.minDpiOffset,
maximum: maximum,
actual: Math.min(configured, maximum)
};
};
/**
* DPI Configuration.
* @typedef {Object} DpiConfig
* @property {Number} configured The desired DPI setting.
* @property {Number} minimum The minimum available DPI setting.
* @property {Number} maximum The maximum available DPI setting.
* @property {Number} actual The actual DPI setting - the same as configured, but clamped to minimum and maximum.
*/
fluid.defaults("gpii.windows.display.getScreenDpi", {
gradeNames: "fluid.function",
argumentMap: {}
});