| 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
203×
203×
203×
203×
203×
203×
203×
203×
1×
44×
44×
11×
33×
44×
1×
89×
8×
81×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
45×
45×
45×
45×
45×
45×
45×
45×
45×
45×
1×
1×
44×
44×
44×
45×
1×
158×
158×
304×
216×
304×
304×
32×
304×
304×
122×
182×
1×
248×
248×
84×
164×
164×
164×
164×
4×
164×
1×
203×
203×
135×
135×
19×
19×
19×
19×
49×
49×
203×
1×
45×
45×
45×
94×
94×
9×
94×
45×
5×
40×
11×
29×
29×
45×
45×
1×
45×
45×
45×
45×
45×
44×
44×
44×
88×
88×
44×
44×
45×
1×
22×
1×
69×
69×
69×
69×
69×
1×
24×
1×
6×
1×
1×
113×
22×
4×
4×
2×
2×
| /*!
Windows SystemParametersInfo Settings Handler
Copyright 2012 Antranig Basman
Copyright 2012 Astea Solutions AD
Copyright 2014 OCAD University
Copyright 2014 Lucendo Development Ltd.
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
The research leading to these results has received funding from the European Union's
Seventh Framework Programme (FP7/2007-2013) under grant agreement no. 289016.
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 ffi = require("ffi");
var fluid = require("universal");
var gpii = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.windows.spi");
fluid.registerNamespace("gpii.windows.spiSettingsHandler");
require("../../WindowsUtilities/WindowsUtilities.js");
require("../../processHandling/processHandling.js");
var os = require("os");
// Guide to node-ffi types and conversions:
// https://github.com/rbranson/node-ffi/wiki/Node-FFI-Tutorial
// SystemParametersInfoW
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724947(v=vs.85).aspx
// UINT, UINT, PVOID, UINT; return type: BOOL
// We declare 2 versions of SystemParametersInfoW:
// - one with a void* signature for the pvParam parameter and
// - one with an integer signature for pvParam.
// For cases in which calls on SPI_SET* actions need a BOOL or UINT pvParam,
// the BOOL or UINT value is passed directly in pvParam, rather than as a
// pointer.
gpii.windows.spi.systemParametersInfoWPtr = ffi.Library("user32", {
"SystemParametersInfoW": [
"int32", [ "uint32", "uint32", "void*", "uint32" ]
]
});
gpii.windows.spi.systemParametersInfoWUint = ffi.Library("user32", {
"SystemParametersInfoW": [
"int32", [ "uint32", "uint32", "uint32", "uint32" ]
]
});
/**
* Makes a call to SystemParametersInfoW with the get action to retrieve the current settings
* applied on the system.
*/
gpii.windows.spi.getCurrentSettings = function (payload) {
var getAction = gpii.windows.actionConstants[payload.options.getAction];
var uiParam = gpii.windows.spi.getUiParam(payload);
var pvParam = gpii.windows.spi.createPvParam(payload);
var callSuccessful = gpii.windows.spi.systemParametersInfoWPtr.SystemParametersInfoW(getAction, uiParam, pvParam, 0);
Iif (!callSuccessful) {
var errorCode = gpii.windows.kernel32.GetLastError();
fluid.fail("SpiSettingsHandler.js: spi.getCurrentSettings() failed with error code " + errorCode + ".");
}
var currentSettings = {
"uiParam": uiParam,
"pvParam": undefined
};
currentSettings.pvParam = (payload.options.pvParam.type === "array") ?
gpii.windows.bufferToArray(pvParam, payload.options.pvParam.valueType) :
pvParam.deref();
return currentSettings;
};
gpii.windows.spi.systemParametersInfo = function (pvParamType, action, uiParam, pvParam) {
var callSuccessful = 0;
if (pvParamType === "BOOL" || pvParamType === "UINT") {
callSuccessful = gpii.windows.spi.systemParametersInfoWUint.SystemParametersInfoW(action, uiParam, pvParam, 0);
} else {
callSuccessful = gpii.windows.spi.systemParametersInfoWPtr.SystemParametersInfoW(action, uiParam, pvParam, 0);
}
return callSuccessful;
};
/**
* Waits for a previous call to SPI to complete. This is to work-around the high-contrast change
* taking some time to complete, and to prevent other calls in the meantime.
*
* @param action The action parameter of the SystemParametersInfo call.
* @return {promise} A promise that resolves when the last SPI call is complete.
*/
gpii.windows.spi.waitForSpi = function (action) {
if (action === gpii.windows.actionConstants.SPI_SETHIGHCONTRAST) {
/*
* Experimentation has uncovered an undocumented occurrence when the high-contrast is changed
* via the SPI call, where Windows executes %WINDIR%\system32\sethc.exe to perform the actual
* work of changing the contrast. Investigations have shown that this executable may be
* called with parameters "10", "11", "100", or "101" (numbers < 100 turn HC off).
*
* It has been noticed that the work behind setting SPI_SETHIGHCONTRAST is complete when
* this process terminates, so this script checks for the existence of this process and
* does not return until the has ended.
*/
return gpii.windows.waitForProcessTermination("sethc.exe");
} else {
// The setting has been applied as soon as SPI returns.
return fluid.promise().resolve();
}
};
/**
* Performs the SPI call in a child-process. This is used on certain SPI_SET* calls to SPI that are known to be
* troublesome and have the potential to hang.
*
* See GPII-2001 for an example, where a system process causes this phenomenon. While it may be possible to fix that
* particular issue, it raises the question: what prevents other processes from doing the same? To work around this,
* the call to SystemParametersInfo (for problematic actions) shall be invoked in a separate process.
*
* A promise is returned, resolving with the return value of the SystemParametersInfo, or rejects if the call does not
* complete in a timely manner.
*
* @param pvParamType The data type of pvParam.
* @param action The uiAction SPI parameter.
* @param uiParam The uiParam SPI parameter.
* @param pvParam The pvParam SPI parameter.
* @return {promise} Rejects if the SPI call hangs.
*/
gpii.windows.spi.callProblematicSpi = function (pvParamType, action, uiParam, pvParam) {
var cp = require("child_process");
var primitiveType = pvParamType in gpii.windows.types;
var options = {
env: {
GPII_SPI_ACTION: action,
GPII_SPI_UIPARAM: uiParam,
GPII_SPI_PVPARAM_PRIMITIVE: primitiveType ? 1 : 0,
GPII_SPI_PVPARAM: primitiveType ? pvParam : pvParam.toString("hex")
},
execArgv: []
};
var child = cp.fork(__dirname + "/SpiChildProcess.js", options);
var promise = fluid.promise();
var timer = setTimeout(function () {
child.kill();
timer = null;
}, 10000);
child.on("exit", function (code) {
Iif (timer) {
clearTimeout(timer);
}
Eif (code === null) {
// The child process was killed.
fluid.log("SPI call has failed");
promise.reject({
isError: true,
message: "Timed out waiting for the SPI call to complete (see GPII-2001)"
});
} else {
promise.resolve(code);
}
});
return promise;
};
/**
* Applies the settings stored in the payload, making a call to SystemParametersInfoW with the
* set action.
*/
gpii.windows.spi.applySettings = function (payload) {
var action = gpii.windows.actionConstants[payload.options.setAction];
var uiParam = gpii.windows.spi.getUiParam(payload);
var pvParam = gpii.windows.spi.getPvParam(payload);
uiParam = pvParam.uiParam; // this will be updated because it looks bad
pvParam = pvParam.pvParam;
var pvParamType = payload.options.pvParam.type;
var promiseTogo = fluid.promise();
// The SPI_SET* calls that could potentially halt the process.
var problematicSpiCalls = [
gpii.windows.actionConstants.SPI_SETNONCLIENTMETRICS
];
// Wait for sethc.exe to end before making the SPI call
gpii.windows.spi.waitForSpi()
.then(function () {
if (problematicSpiCalls.indexOf(action) >= 0) {
var p = gpii.windows.spi.callProblematicSpi(pvParamType, action, uiParam, pvParam);
fluid.promise.follow(p, promiseTogo);
} else {
var callSuccessful = gpii.windows.spi.systemParametersInfo(pvParamType, action, uiParam, pvParam);
Eif (callSuccessful) {
fluid.promise.follow(gpii.windows.spi.waitForSpi(action), promiseTogo);
} else {
var errorCode = gpii.windows.kernel32.GetLastError();
fluid.fail("SpiSettingsHandler.js: spi.applySettings() failed with error code " + errorCode + ".");
}
}
});
return promiseTogo;
};
/**
* Populates the results payload that is returned from the <code>SpiSettingsHandler</code>. These
* results contain the old and new values for each setting in the input payload.
*
* @param {Object} payload The input that is passed to the SPI Settings Handler
* @param {Boolean} isNewValue True if the updated values of the settings are populated, false
* otherwise.
* @param {Boolean} isGetting True if called within spiSettingsHandler.get, false otherwise.
* @param {Object} results The results object to be populated with the old and new settings values.
*/
gpii.windows.spi.populateResults = function (payload, isNewValue, isGetting, results) {
var systemSettings = gpii.windows.spi.getCurrentSettings(payload);
for (var currentSetting in payload.settings) {
if (!isNewValue || isGetting) {
results[currentSetting] = {};
}
var path = payload.settings[currentSetting].path;
if (path.get !== undefined) {
path = path.get;
}
var valueToSet = gpii.windows.resolvePath(systemSettings, path);
if (isGetting) {
results[currentSetting] = {
value: valueToSet,
path: payload.settings[currentSetting].path
};
} else {
results[currentSetting][isNewValue ? "newValue" : "oldValue"] = valueToSet;
}
}
};
/**
* Returns the value for the uiParam parameter of the SystemParametersInfo function from the
* payload.
*/
gpii.windows.spi.getUiParam = function (payload) {
var uiParam = payload.options.uiParam;
if (!isNaN(Number(uiParam))) {
return Number(uiParam);
}
Iif (uiParam === "true" || uiParam === "false") {
return Number(uiParam === "true");
}
Eif (uiParam === "struct_size") {
var result = gpii.windows.structures[payload.options.pvParam.name].size;
if (payload.options.pvParam.name === "NONCLIENTMETRICS" && os.release() < "6") {
result -= 4; // do not include NONCLIENTMETRICS.iPaddedBorderWidth
}
return result;
}
console.log("SpiSettingsHandler.js: spi.getUiParam() got unknown uiParam value: " + uiParam + " of type " + typeof uiParam + ".");
return 0;
};
/**
* Returns an empty pvParam - creates an empty structure or allocates memory for a
* given type. Primarily called as part of <code>getCurrentSettings</code>
*/
gpii.windows.spi.createPvParam = function (payload) {
var pvParam;
switch (payload.options.pvParam.type) {
case "struct":
pvParam = gpii.windows.createEmptyStructure(payload.options.pvParam.name).ref();
break;
case "array":
var length = payload.options.pvParam.length;
var size = ref.types[gpii.windows.types[payload.options.pvParam.valueType]].size;
pvParam = new Buffer(length * size);
break;
case "NULL":
pvParam = ref.NULL;
break;
default:
Eif (payload.options.pvParam.type in gpii.windows.types) {
pvParam = ref.alloc(gpii.windows.types[payload.options.pvParam.type]);
} else {
fluid.fail("SpiSettingsHandler.js: spi.createPvParam() failed: got unknown pvParam type " + payload.options.pvParam.type);
}
}
return pvParam;
};
/**
* Creates a pvParam populated with the settings requested in the payload.
* Primarily used within <code>applySettings</code>
*/
gpii.windows.spi.getPvParam = function (payload) {
var systemSettings = gpii.windows.spi.getCurrentSettings(payload);
var pvParam = systemSettings.pvParam;
for (var currentSetting in payload.settings) {
var path = payload.settings[currentSetting].path;
if (path.set !== undefined) {
path = path.set;
}
gpii.windows.resolvePath(systemSettings, path, payload.settings[currentSetting].value);
}
if (payload.options.pvParam.type === "array") {
pvParam = gpii.windows.arrayToBuffer(pvParam, payload.options.pvParam.valueType);
} else if (payload.options.pvParam.type in gpii.windows.types) {
pvParam = systemSettings.pvParam;
} else Eif (payload.options.pvParam.type === "struct") {
pvParam = pvParam.ref();
}
systemSettings.pvParam = pvParam;
return systemSettings;
};
/**
* Entry point function of the component. Takes a payload as an input and sets the corresponding
* settings using the SystemParametersInfoW Windows API function. Returns a promise, resolving
* to an object containing the old and new values for each of the settings, when the setting has
* been applied.
*/
gpii.windows.spi.setImpl = function (payload) {
gpii.windows.defineStruct(payload);
var results = {};
gpii.windows.spi.populateResults(payload, false, false, results);
var togo = fluid.promise();
gpii.windows.spi.applySettings(payload)
.then(function () {
gpii.windows.spi.populateResults(payload, true, false, results);
gpii.windows.spi.GPII1873_HighContrastBug("SET", payload, results);
// transform results here oldValue: x --> oldValue: { value: x, path: ... }
fluid.each(results, function (value, setting) {
results[setting].oldValue = {"value": value.oldValue, "path": payload.settings[setting].path};
results[setting].newValue = {"value": value.newValue, "path": payload.settings[setting].path};
});
fluid.log("SPI settings handler SET returning results ", results);
togo.resolve(results);
}, togo.reject);
return togo;
};
gpii.windows.spiSettingsHandler.set = function (payload) {
return gpii.settingsHandlers.invokeSettingsHandler(gpii.windows.spi.setImpl, payload);
};
gpii.windows.spi.getImpl = function (payload) {
gpii.windows.defineStruct(payload);
var results = {};
gpii.windows.spi.populateResults(payload, false, true, results);
gpii.windows.spi.GPII1873_HighContrastBug("GET", payload, results);
return results;
};
gpii.windows.spiSettingsHandler.get = function (payload) {
return gpii.settingsHandlers.invokeSettingsHandler(gpii.windows.spi.getImpl, payload);
};
/**
* To change the cursor size one must perform the following two steps:
* First write the path to the new cursor icons in the registry. This is done by the Registry
* Settings Handler.
*
* After that we must tell the system to load those new icons, calling SystemParametersInfo
* with the SPI_SETCURSORS action.
*
* The purpose of this function is to perform the second step.
*/
gpii.windows.spiSettingsHandler.updateCursors = function () {
// Call SPI with the SPI_SETCURSORS uiAction
gpii.windows.spi.systemParametersInfoWUint.SystemParametersInfoW(0x57, 0, 0, 0);
};
fluid.defaults("gpii.windows.spiSettingsHandler.updateCursors", {
gradeNames: "fluid.function",
argumentMap: {}
});
/**
* Fix for GPII-1873.
*
* This method overrides the output of the system based on the payload information. We
* actually know that Windows have a bug that disallow us to reset to the HighContrastTheme
* default scheme to null or empty string. That is the reason we are circumventing the
* output of this HIGHCONTRAST methods.
*
* More information at https://issues.gpii.net/browse/GPII-1873
*/
gpii.windows.spi.GPII1873_HighContrastBug = function (method, payload, results) {
if ((payload.options.pvParam.type === "struct") &&
(payload.options.pvParam.name === "HIGHCONTRAST")) {
if (payload.settings.HighContrastTheme &&
payload.settings.HighContrastTheme.value === "") {
fluid.log("Bug #1873 detected. We are forcing the return value to empty string although the system cannot set to it.");
if (method === "SET") {
results.HighContrastTheme.newValue = "";
} else {
results.HighContrastTheme.value = "";
}
}
}
};
|