-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathnativeSettingsHandler.js
516 lines (438 loc) · 17.6 KB
/
nativeSettingsHandler.js
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* Windows Native Settings Handler
*
* Deals with individual settings that are only accessible throught isolated
* API calls.
*
* Copyright 2018 Raising the Floor - International
*
* 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 fluid = require("gpii-universal"),
gpii = fluid.registerNamespace("gpii"),
windows = fluid.registerNamespace("gpii.windows"),
ref = require("ref"),
child_process = require("child_process"),
path = require("path");
require("../../WindowsUtilities/WindowsUtilities.js");
fluid.registerNamespace("gpii.windows.nativeSettingsHandler");
/**
* Gets the current double click time for mouse.
*
* @return {Promise} A promise holding the current system double click time for mouse.
*/
windows.nativeSettingsHandler.GetDoubleClickTime = function () {
var result = windows.user32.GetDoubleClickTime();
return fluid.toPromise(result);
};
/**
* Mirrors primary screen into a secondary screen.
*
* Note:
* The native function 'SetDisplayConfig' returns the error 'ERROR_GEN_FAILURE' which stands to code
* 31 when a second display isn't present, this error may be misleading because no system component
* is malfunctioning.
*
* @param {Boolean} mirror A boolean representing the desired state for the 'screen mirror' feature.
* @return {Promise} A promise that resolves on success or holds a error code and message on failure.
*/
windows.nativeSettingsHandler.SetScreenMirrored = function (mirror) {
var pRes = fluid.promise();
var err = 0;
var flags = mirror ?
(windows.API_constants.SDC_TOPOLOGY_CLONE | windows.API_constants.SDC_APPLY) :
(windows.API_constants.SDC_TOPOLOGY_INTERNAL | windows.API_constants.SDC_APPLY);
err = gpii.windows.user32.SetDisplayConfig(0, ref.NULL, 0, ref.NULL, flags);
if (err === 0) {
pRes.resolve();
} else {
pRes.reject({code: err, msg: windows.translateLastError(err)});
}
return pRes;
};
/**
* Queries if the main display is currently mirrored.
*
* @return {Promise} A promise holding a bool with the result.
*/
windows.nativeSettingsHandler.GetScreenMirrored = function () {
// The size of the structs for holding the display config information.
var DISPLAYCONFIG_PATH_INFO_SIZE = 72;
var DISPLAYCONFIG_MODE_INFO_SIZE = 64;
var pModePathArrayElem = ref.alloc(ref.types.uint32, 1);
var pModeInfoArrayElem = ref.alloc(ref.types.uint32, 1);
gpii.windows.user32.GetDisplayConfigBufferSizes(4, pModePathArrayElem, pModeInfoArrayElem);
var topologyIdBuf = ref.alloc(ref.types.uint32, 0);
var pModePathArray = Buffer.alloc(DISPLAYCONFIG_PATH_INFO_SIZE * pModePathArrayElem.deref());
var pModeInfoArray = Buffer.alloc(DISPLAYCONFIG_MODE_INFO_SIZE * pModeInfoArrayElem.deref());
var pRes = fluid.promise();
var err =
gpii.windows.user32.QueryDisplayConfig(
4,
pModePathArrayElem,
pModePathArray,
pModeInfoArrayElem,
pModeInfoArray,
topologyIdBuf
);
if (err === 0) {
var screenCloned = topologyIdBuf.deref() === windows.API_constants.SDC_TOPOLOGY_CLONE;
pRes.resolve(screenCloned);
} else {
pRes.reject({code: err, msg: windows.translateLastError(err)});
}
return pRes;
};
/**
* Sets the current double click time for mouse.
*
* @param {Number} num The double click time to set in the system.
* @return {Promise} A promise that resolves on success or holds a object with the error information
* when rejected.
*/
windows.nativeSettingsHandler.SetDoubleClickTime = function (num) {
var pRes = fluid.promise();
var retVal = windows.user32.SetDoubleClickTime(num);
if (retVal !== 0) {
pRes.resolve();
} else {
var lastErr = windows.kernel32.GetLastError();
var lastErrMsg = windows.translateLastError(lastErr);
pRes.reject({code: lastErr, msg: lastErrMsg});
}
return pRes;
};
/**
* Set Windows background solid color.
*
* @param {Object} rgbColor The color in RGB format, as and object of type: { r: 0x00, g: 0x00, b: 0x00 }
* @return {Promise} Resolve or rejects if failing to set the new system color.
*/
windows.nativeSettingsHandler.SetSolidColor = function (rgbColor) {
var pRes = fluid.promise();
var color = rgbColor.r | rgbColor.g << 8 | rgbColor.b << 16;
var bufElems = ref.alloc(windows.types.INT, windows.API_constants.COLOR_DESKTOP);
var bufColors = ref.alloc(windows.types.UINT, color);
var res = windows.user32.SetSysColors(1, bufElems, bufColors);
if (res === 0) {
pRes.reject("nativeSettingsHandler: Failed to set new system color.");
} else {
pRes.resolve();
}
return pRes;
};
/**
* Returns the current Windows dekstop solid color as a RGB color object of format: { r: 0x00, g: 0x00, b: 0x00 }
*
* @return {Promise} Resolves containing the current system solid color value.
*/
windows.nativeSettingsHandler.GetSolidColor = function () {
var pRes = fluid.promise();
var decimal = windows.user32.GetSysColor(windows.API_constants.COLOR_DESKTOP);
pRes.resolve({ r: (decimal & 0x0000ff), g: (decimal & 0x00ff00) >> 8, b: (decimal & 0xff0000) >> 16 });
return pRes;
};
/**
* Sets the width of the double-click rectangle.
*
* @param {Number} num The number to be set as the width of the double-click rectangle.
* @return {Promise} A promise that resolves on success or holds a object with the error information
* when rejected.
*/
windows.nativeSettingsHandler.SetDoubleClickWidth = function (num) {
var pRes = fluid.promise();
var retVal = gpii.windows.spi.systemParametersInfo("UINT", gpii.windows.spi.actions.SPI_SETDOUBLECLKWIDTH, num, 0, 0);
if (retVal === 0) {
var lastErr = windows.kernel32.GetLastError();
var lastErrMsg = windows.translateLastError(lastErr);
pRes.reject({code: lastErr, msg: lastErrMsg});
} else {
pRes.resolve(retVal);
}
return pRes;
};
/**
* Gets the current width of the double-click rectangle.
*
* @return {Promise} A promise holding the current value of the width for the double-click rectangle.
*/
windows.nativeSettingsHandler.GetDoubleClickWidth = function () {
var result = windows.user32.GetSystemMetrics(windows.API_constants.SM_CXDOUBLECLK);
return fluid.toPromise(result);
};
/**
* Sets the height of the double-click rectangle.
*
* @param {Number} num The number to be set as the height of the double-click rectangle.
* @return {Promise} A promise that resolves on success or holds a object with the error information
* when rejected.
*/
windows.nativeSettingsHandler.SetDoubleClickHeight = function (num) {
var pRes = fluid.promise();
var retVal = gpii.windows.spi.systemParametersInfo("UINT", gpii.windows.spi.actions.SPI_SETDOUBLECLKHEIGHT, num, 0, 0);
if (retVal === 0) {
var lastErr = windows.kernel32.GetLastError();
var lastErrMsg = windows.translateLastError(lastErr);
pRes.reject({code: lastErr, msg: lastErrMsg});
} else {
pRes.resolve(retVal);
}
return pRes;
};
/**
* Function that handles calling the native executable for volume control.
*
* @param {String} mode The operation mode, could be either "Set", "SetNear" or "Get". Calling with "SetNear" will
* adjust the volume so it's near the target volume, allowing room for the shell to do the rest.
* @param {Number} [num] The value to set as the current system volume. Range is normalized from 0 to 1.
* @return {Promise} A promise that resolves with the current volume (0 if there was an error)
*/
windows.nativeSettingsHandler.VolumeHandler = function (mode, num) {
var promise = fluid.promise();
try {
var fileName = path.join(__dirname, "../nativeSolutions/VolumeControl/Release/VolumeControl.exe");
var valueBuff;
if (mode === "Get") {
valueBuff = child_process.execFileSync(fileName, [mode], {});
} else {
valueBuff = child_process.execFileSync(fileName, [mode, num], {});
}
var strRes = valueBuff.toString("utf8");
var jsonRes = JSON.parse(strRes);
var value = fluid.get(jsonRes, "Value");
if (value) {
promise.resolve(parseFloat(value));
} else {
// To stop the QSS from crashing if there's no sound card, return zero. When the QSS is able to handle
// rejections, this can be a changed to a reject. [GPII-4092]
promise.resolve(0);
}
} catch (err) {
promise.resolve(0);
}
return promise;
};
/**
* Gets the current system volume, calling the native executable 'VolumeControl.exe'
* with the proper payload.
*
* @return {Promise} A promise that resolves on success or holds a object with the error information.
*/
windows.nativeSettingsHandler.GetVolume = function () {
return windows.nativeSettingsHandler.VolumeHandler("Get");
};
/**
* Sets the current system volume, calling the native executable 'VolumeControl.exe'
* with the proper payload.
*
* @param {Number} num The value to set as the current system volume. Range is normalized from 0 to 1.
* @return {Promise} A promise that resolves on success or holds a object with the error information.
*/
windows.nativeSettingsHandler.SetVolume = function (num) {
return windows.nativeSettingsHandler.VolumeHandler("SetNear", num).then(function (result) {
var current = parseFloat(result);
if (current !== num) {
// Complete the last bit of the change, as though the media keys where used, in order to show the new volume
// on the screen.
var action = current < num
? windows.API_constants.APPCOMMAND_VOLUME_UP : windows.API_constants.APPCOMMAND_VOLUME_DOWN;
// Send the volume up/down command directly to the task tray (rather than a simulated key press)
gpii.windows.messages.sendMessage("Shell_TrayWnd", windows.API_constants.WM_APPCOMMAND, 0,
windows.makeLong(0, action));
}
});
};
/**
* Gets the current height of the double-click rectangle.
*
* @return {Promise} A promise holding the current value of the height for the double-click rectangle.
*/
windows.nativeSettingsHandler.GetDoubleClickHeight = function () {
var result = windows.user32.GetSystemMetrics(windows.API_constants.SM_CYDOUBLECLK);
return fluid.toPromise(result);
};
windows.nativeSettingsHandler.functions = {
SolidColor: {
set: windows.nativeSettingsHandler.SetSolidColor,
get: windows.nativeSettingsHandler.GetSolidColor
},
DoubleClickTime: {
set: windows.nativeSettingsHandler.SetDoubleClickTime,
get: windows.nativeSettingsHandler.GetDoubleClickTime
},
DoubleClickWidth: {
set: windows.nativeSettingsHandler.SetDoubleClickWidth,
get: windows.nativeSettingsHandler.GetDoubleClickWidth
},
DoubleClickHeight: {
set: windows.nativeSettingsHandler.SetDoubleClickHeight,
get: windows.nativeSettingsHandler.GetDoubleClickHeight
},
ScreenMirror: {
set: windows.nativeSettingsHandler.SetScreenMirrored,
get: windows.nativeSettingsHandler.GetScreenMirrored
},
Volume: {
set: windows.nativeSettingsHandler.SetVolume,
get: windows.nativeSettingsHandler.GetVolume
}
};
/**
* Helper function that converts a error to string, so the rejection from the
* settings handler only holds a message.
*
* @param {Object} err The error to be stringified.
* @return {String} The error message.
*/
windows.nativeSettingsHandler.errorInfo = function (err) {
return "With error - '" + JSON.stringify(err) + "'";
};
/**
* Helper function for creating a custom message for error rejection.
*
* @param {Promise} promise The promise to be rejected.
* @param {String} settingKey The name of the requested setting for which an operation failed.
* @param {Object} err The error object.
* @param {String} message A message with information about the operation.
*/
windows.nativeSettingsHandler.reject = function (promise, settingKey, err, message) {
var operation = message + settingKey + "' failed. ";
var errMsg = "nativeSettingsHandler" + operation + windows.nativeSettingsHandler.errorInfo(err);
promise.reject(errMsg);
};
/**
* Setter for the nativeSettingsHandler.
*
* The payload should have the following format:
*
* {
* "functionName": "FunctionName",
* "setParam": val
* }
*
* The first two items:
* - function: Function name for the function to be used to set/get the value.
* - setParam: Parameter to be passed to the 'set' function.
*
* @param {Object} payload The payload.
* @return {Promise} Resolves with the response.
*/
windows.nativeSettingsHandler.setImpl = function (payload) {
var pRes = fluid.promise();
var fnName = fluid.get(payload, "options.functionName");
var setFn = fluid.get(windows.nativeSettingsHandler.functions, fnName + ".set");
var getFn = fluid.get(windows.nativeSettingsHandler.functions, fnName + ".get");
var fnNotDef = [": Failed due to '", " " + fnName + "' function not being defined."];
if (setFn === undefined) {
pRes.reject("nativeSettingsHandler" + fnNotDef[0] + "set" + fnNotDef[1]);
} else if (getFn === undefined) {
pRes.reject("nativeSettingsHandler" + fnNotDef[0] + "get" + fnNotDef[1]);
} else {
var results = {};
var settingsArray = fluid.makeArray(payload.settings);
if (settingsArray.length === 0) {
pRes.reject("nativeSettingsHandler: Failed due to empty payload.");
} else {
var uniqueSetting = settingsArray[0];
// Get the payload value to set.
var settingKey = fluid.keys(uniqueSetting)[0];
var valueToSet = fluid.values(uniqueSetting)[0].value;
// Create object for storing setting results.
results[settingKey] = {};
// Get the current values for the setting.
var pGetOld = getFn();
pGetOld.then(
function (oldVal) {
// Store the current value in results.
fluid.set(results[settingKey], "oldValue.value", oldVal);
var pSet = setFn(valueToSet);
pSet.then(
function () {
var pGetNew = getFn();
pGetNew.then(
function (newVal) {
fluid.set(results[settingKey], "newValue.value", newVal);
pRes.resolve(results);
},
function (err) {
windows.nativeSettingsHandler.reject(pRes, settingKey, err, ": Getting new value for setting '");
}
);
},
function (err) {
windows.nativeSettingsHandler.reject(pRes, settingKey, err, ": Setting new value for setting '");
}
);
},
function (err) {
windows.nativeSettingsHandler.reject(pRes, settingKey, err, ": Getting current value for setting '");
}
);
}
}
return pRes;
};
/**
* Getter for the nativeSettingsHandler.
*
* @param {Object} payload The payload.
* @return {Promise} Resolves with the response.
*/
windows.nativeSettingsHandler.getImpl = function (payload) {
var pRes = fluid.promise();
var fnName = fluid.get(payload, "options.functionName");
var getFn = fluid.get(windows.nativeSettingsHandler.functions, fnName + ".get");
var fnNotDef = [": Failed due to '", " " + fnName + "' function not being defined."];
if (getFn === undefined) {
pRes.reject("nativeSettingsHandler" + fnNotDef[0] + "get" + fnNotDef[1]);
} else {
var settingsArray = fluid.makeArray(payload.settings);
if (settingsArray.length === 0) {
pRes.reject("nativeSettingsHandler: Failed due to empty payload.");
} else {
var uniqueSetting = settingsArray[0];
var results = {};
// Get payload setting and prepare results for returning it
var settingKey = fluid.keys(uniqueSetting)[0];
results[settingKey] = {};
var pGetRes = getFn();
pGetRes.then(
function (curVal) {
results[settingKey].value = curVal;
pRes.resolve(results);
},
function (err) {
windows.nativeSettingsHandler.reject(pRes, settingKey, err, ": Getting current value for setting '");
}
);
}
}
return pRes;
};
/**
* Invoke the settings handler.
*
* @param {Object} payload The payload
* @return {Promise} Resolves with the response.
*/
windows.nativeSettingsHandler.get = function (payload) {
return gpii.settingsHandlers.invokeSettingsHandler(windows.nativeSettingsHandler.getImpl, payload);
};
/**
* Invoke the settings handler.
*
* @param {Object} payload The payload
* @return {Promise} Resolves with the response.
*/
windows.nativeSettingsHandler.set = function (payload) {
return gpii.settingsHandlers.invokeSettingsHandler(windows.nativeSettingsHandler.setImpl, payload);
};