-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdisplaySettingsHandler.js
More file actions
341 lines (308 loc) · 11.6 KB
/
displaySettingsHandler.js
File metadata and controls
341 lines (308 loc) · 11.6 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
/*
* Windows Display Settings Handler
*
* 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";
var ref = require("ref");
var Struct = require("ref-struct");
var arrayType = require("ref-array");
var ffi = require("ffi");
var fluid = require("gpii-universal");
var os = require("os");
var semver = require("semver");
var gpii = fluid.registerNamespace("gpii");
var windows = fluid.registerNamespace("gpii.windows");
fluid.registerNamespace("gpii.windows.display");
fluid.registerNamespace("gpii.windows.displaySettingsHandler");
// The OS specific files can only be loaded from within the OS is it intended for.
var ver = os.release();
if (semver.gte(ver, "10.0.0")) {
// Windows 10
require("./dpiWindows10.js");
} else {
require("./dpiWindows8.js");
}
var t = windows.types;
var c = windows.API_constants;
var CCHFORMNAME = 32;
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd183565(v=vs.85).aspx
windows.DEVMODEW = new Struct([
[arrayType(t.TCHAR, c.CCHDEVICENAME), "dmDeviceName"],
[t.WORD, "dmSpecVersion"],
[t.WORD, "dmDriverVersion"],
[t.WORD, "dmSize"],
[t.WORD, "dmDriverExtra"],
[t.DWORD, "dmFields"],
["short", "dmOrientation"],
["short", "dmPaperSize"],
["short", "dmPaperLength"],
["short", "dmPaperWidth"],
["short", "dmScale"],
["short", "dmCopies"],
["short", "dmDefaultSource"],
["short", "dmPrintQuality"],
["short", "dmColor"],
["short", "dmDuplex"],
["short", "dmYResolution"],
["short", "dmTTOption"],
["short", "dmCollate"],
[arrayType(t.TCHAR, CCHFORMNAME), "dmFormName"],
[t.WORD, "dmLogPixels"],
[t.DWORD, "dmBitsPerPel"],
[t.DWORD, "dmPelsWidth"],
[t.DWORD, "dmPelsHeight"],
[t.DWORD, "dmDisplayFlags"],
[t.DWORD, "dmDisplayFrequency"],
[t.DWORD, "dmICMMethod"],
[t.DWORD, "dmICMIntent"],
[t.DWORD, "dmMediaType"],
[t.DWORD, "dmDitherType"],
[t.DWORD, "dmReserved1"],
[t.DWORD, "dmReserved2"],
[t.DWORD, "dmPanningWidth"],
[t.DWORD, "dmPanningHeight"]
]);
windows.display.user32 = ffi.Library("user32", {
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd162611(v=vs.85).aspx
// LPCWSTR, DWORD, DEVMODE*
"EnumDisplaySettingsW": [
t.BOOL, ["pointer", t.DWORD, "pointer"]
],
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd183411(v=vs.85).aspx
// DEVMODE*, DWORD
"ChangeDisplaySettingsW": [
t.LONG, ["pointer", t.DWORD]
]
});
/*
* Creates and initializes a new DEVMODEW structure
*/
windows.display.createDevModeStruct = function () {
var dm = new windows.DEVMODEW();
dm.ref().fill(0);
dm.dmSize = windows.DEVMODEW.size;
return dm;
};
fluid.defaults("gpii.windowsDisplay", {
gradeNames: ["fluid.component"],
invokers: {
getScreenResolution: {
funcName: "gpii.windows.display.getScreenResolution",
args: []
},
getAvailableResolutions: {
funcName: "gpii.windows.display.getAvailableResolutions",
args: ["{that}"]
},
getAllScreenResolutions: {
funcName: "gpii.windows.display.getAllScreenResolutions",
args: []
},
isScreenResolutionInvalid: {
funcName: "gpii.windows.display.isInvalid",
args: ["{that}", "{arguments}.0"]
// { width, height }
},
setScreenResolution: {
funcName: "gpii.windows.display.setScreenResolution",
args: ["{that}", "{arguments}.0"]
// { width, height }
},
getImpl: {
funcName: "gpii.windows.display.getImpl",
args: []
},
setImpl: {
funcName: "gpii.windows.display.setImpl",
args: ["{arguments}.0"]
// settingsRequest payload
}
}
});
/*
* Returns if a screen resolution is invalid
*
* @psrsm that {Component} An instance of gpii.displaySettingsHandler
* @param screenRes (Object) Screen resolution to test as {width, height}
* @returns {boolean} true if invalid
*/
gpii.windows.display.isInvalid = function (that, screenRes) {
var isInvalid = true;
if (typeof(screenRes.width) === "number" && typeof(screenRes.height) === "number" && screenRes.width > 0 && screenRes.height > 0) {
fluid.each(that.getAvailableResolutions(), function (validScreenRes) {
if (validScreenRes.dmPelsWidth === screenRes.width && validScreenRes.dmPelsHeight === screenRes.height) {
isInvalid = false;
};
});
}
return isInvalid;
};
/*
* Gets the current display's screen resolution
*
* @return {Object) The width and height of the screen.
*/
gpii.windows.display.getScreenResolution = function () {
var dm = windows.display.createDevModeStruct();
if (windows.display.user32.EnumDisplaySettingsW(ref.NULL, c.ENUM_CURRENT_SETTINGS, dm.ref()) !== c.FALSE) {
// note for unknown reason on win 10 the returned dmSize is 188 not expected 220
var closestScreenRes = windows.display.findClosestResolution({width: dm.dmPelsWidth, height: dm.dmPelsHeight});
return { width: closestScreenRes.dmPelsWidth, height: closestScreenRes.dmPelsHeight };
} else {
fluid.fail("Couldn't retrieve the current screen resolution");
}
};
/*
* Gets available resolutions
*
* @return {array} of resolutions
*/
gpii.windows.display.getAvailableResolutions = function () {
var index = 0;
var dm = windows.display.createDevModeStruct();
var availableResolutions = [];
while (windows.display.user32.EnumDisplaySettingsW(ref.NULL, index++, dm.ref()) !== c.FALSE) {
var curr = {
dmPelsWidth: dm.dmPelsWidth,
dmPelsHeight: dm.dmPelsHeight
};
availableResolutions.push(curr);
};
fluid.log("windows.display.getAvailableResolutions got available screen resolutions ", availableResolutions);
return availableResolutions;
};
/*
* Gets the current and all available screen resolutions
*
* @return {Object) The width and height as "current-resolution" and an array of
* widths/heights as "available-resolutions".
*/
gpii.windows.display.getAllScreenResolutions = function () {
var winDisplay = gpii.windowsDisplay();
var currentResolution = winDisplay.getScreenResolution();
var available = winDisplay.getAvailableResolutions();
var rule = {
width: "dmPelsWidth",
height: "dmPelsHeight"
};
available = fluid.transform (available, function (currRes) {
return fluid.model.transformWithRules (currRes, rule);
});
return {
"screen-resolution": currentResolution,
"available-resolutions": available
};
};
/*
* Sets the current display's screen resolution if possible
*
* @psrsm that {Component} An instance of gpii.displaySettingsHandler
* @param {Object} The new screen resolution width and height
* @return {boolean} true if successful
*/
gpii.windows.display.setScreenResolution = function (that, newRes) {
if (that.isScreenResolutionInvalid(newRes)) {
fluid.fail("Received an invalid screen resolution: ", newRes);
} else {
var dmCurrent = windows.display.createDevModeStruct();
if (windows.display.user32.EnumDisplaySettingsW(ref.NULL, c.ENUM_CURRENT_SETTINGS, dmCurrent.ref()) !== c.FALSE) {
fluid.log("windows.display.setScreenResolution about to set resolution ", newRes);
// Note: ChangeDisplaySettings has no effect if new is same as current
dmCurrent.dmPelsWidth = newRes.width;
dmCurrent.dmPelsHeight = newRes.height;
var lRet = windows.display.user32.ChangeDisplaySettingsW(dmCurrent.ref(), 0);
if (lRet === c.DISP_CHANGE_SUCCESSFUL) {
return true;
} else if (lRet === c.DISP_CHANGE_RESTART) {
// TODO: According to the returned code, the computer must be restarted for
// the graphics mode to work. For now we return 'true', but this special case
// is related to https://issues.gpii.net/browse/GPII-1982, and we must consider
// this as something that needs to be taken into account and even notified to
// the user.
//
return true;
} else {
// Return codes explained at https://msdn.microsoft.com/en-us/library/dd183411.aspx
fluid.fail("Couldn't set screen resolution, return code was " + lRet);
}
}
}
};
/*
* Sets the current display's screen resolution based on payload.
*
* @param {Object} Payload that contains a "screen-resolution" property.
* @return {Object} containing the resolution before changing it ("oldValue"),
* and a copy of the new resolution (newRes), both with
* memmbers {width, height}
*/
gpii.windows.display.setImpl = function (payload) {
var windowsDisplay = gpii.windowsDisplay();
var oldRes = windowsDisplay.getScreenResolution();
var newRes = oldRes;
var results = {};
var targetRes = payload.settings["screen-resolution"];
if (targetRes) {
if (typeof(targetRes.width) !== "number" || typeof(targetRes.height) !== "number") {
fluid.fail("Incorrect payload for screen resolution: " +
JSON.stringify(payload, null, 4));
}
else if (windowsDisplay.setScreenResolution(targetRes)) {
newRes = targetRes;
}
results["screen-resolution"] = { oldValue: oldRes, newValue: newRes };
}
var targetDpi = payload.settings["screen-dpi"];
if (targetDpi) {
var oldDpi = windows.display.getScreenDpi();
windows.display.setScreenDpi(targetDpi);
results["screen-dpi"] = { oldValue: oldDpi.configured, newValue: targetDpi };
}
fluid.log("display settings handler SET returning results ", results);
return results;
};
/*
* Returns the display's current screen resolution, and dpi.
*
* @return {Object} containing "screen-resolution" property that is the current
* resolution {width, height}, and a "screen-dpi" property.
*/
gpii.windows.display.getImpl = function () {
var results = {};
fluid.set(results, "screen-resolution", windows.display.getScreenResolution());
fluid.set(results, "screen-dpi", windows.display.getScreenDpi().configured);
return results;
};
windows.displaySettingsHandler.get = function (payload) {
return gpii.settingsHandlers.invokeSettingsHandler(windows.display.getImpl, payload);
};
windows.displaySettingsHandler.set = function (payload) {
return gpii.settingsHandlers.invokeSettingsHandler(windows.display.setImpl, payload);
};
// Internal utilities
// TODO: Move this into gpii.settingsHandlerUtilities and add tests
windows.display.calculateDistance = function (x, y) {
var a = x[0] - y[0];
var b = x[1] - y[1];
return Math.sqrt(a * a + b * b);
};
windows.display.findClosestResolution = function (res) {
var distance = null;
var closest = null;
var availableResolutions = windows.display.getAvailableResolutions();
fluid.each(availableResolutions, function (currRes) {
var currDist = windows.display.calculateDistance([res.width, res.height], [currRes.dmPelsWidth, currRes.dmPelsHeight]);
if (distance === null || currDist < distance) {
distance = currDist;
closest = currRes;
}
});
return closest;
};