forked from antonafana/node-brightness
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
117 lines (88 loc) · 3.51 KB
/
main.js
File metadata and controls
117 lines (88 loc) · 3.51 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
var os = require('os'),
fs = require('fs'),
sys = require('util'),
exec = require('child_process').exec;
function linux(brightness,callback){
// Enumerate the backlight devices...
fs.readdir('/sys/class/backlight', function(err, brightnessDevices){
if(err) {
console.error('Error while listing brightness devices. Maybe you need superuser privileges to run this script?');
throw err;
}
var devicePath = '/sys/class/backlight/' + brightnessDevices[0];
//Read the maximum for the first device
fs.readFile(devicePath + '/max_brightness', function(err, maxBrightness){
maxBrightness = parseInt(maxBrightness, 10)
if(err) {
console.error('Error while reading maximum brightness. Maybe you need superuser privileges to run this script?');
throw err;
}
// Turn percent into the actual value we need
var brightnessValue = Math.floor(brightness / 100 * maxBrightness);
// Write out new value
fs.createWriteStream(devicePath + '/brightness')
.on('end', callback)
.write(new Buffer(brightnessValue.toString()));
});
})
}
function windows(brightness,callback){
//These are the identifiers for the current power scheme
var GUID;
var subgroup;
var powerSetting;
// powercfg -q gives information about power settings, but can only give accurate brightness info for laptops or other portable windows devices
// Get the current power scheme GUID
exec("powercfg /getactivescheme", function (error, stdout, stderr) {
if (error) throw error;
const match = stdout.match(/GUID:\s*([a-f0-9\-]+)/i);
if (!match) throw new Error("Failed to retrieve the power scheme GUID");
GUID = match[1];
// Get the SUB_VIDEO subgroup GUID
exec("powercfg -aliases | findstr /i SUB_VIDEO", function (error, stdout, stderr) {
if (error) throw error;
const matchSub = stdout.match(/([a-f0-9\-]+)\s+SUB_VIDEO/i);
if (!matchSub) throw new Error("Failed to retrieve the SUB_VIDEO GUID");
subgroup = matchSub[1];
// Get the GUID of VIDEONORMALLEVEL
exec("powercfg -aliases | findstr /i VIDEONORMALLEVEL", function (error, stdout, stderr) {
if (error) throw error;
const matchPower = stdout.match(
/([a-f0-9\-]+)\s+VIDEONORMALLEVEL/i
);
if (!matchPower) throw new Error("Failed to retrieve the VIDEONORMALLEVEL GUID");
powerSetting = matchPower[1];
//console.log(GUID,subgroup,powerSetting);
//Set the the brightness for AC power plan settings
exec('powercfg -SetAcValueIndex' + ' ' + GUID + ' ' + subgroup + ' ' + powerSetting + ' ' + brightness,function(err,out,stderror){
if(err) throw err;
//Set the brightness when on DC power plan settings
exec('powercfg -SetDcValueIndex' + ' ' + GUID + ' ' + subgroup + ' ' + powerSetting + ' ' + brightness,function(err,out,stderror){
if(err) throw err;
//Set the modified power plan as the current system plan
exec('powercfg -S' + ' ' + GUID,function(err,out,stderror){
if(err) throw err;
if(callback) callback();
return true;
});
});
});
});
});
});
}
function changeBrightness(brightness,callback){
//Brightness is in percent
switch(os.platform()){
case 'win32':
windows(brightness,callback);
break;
case 'linux':
linux(brightness, callback);
break;
default:
throw new Error('OS is not recognized or is unsupported');
break;
}
}
module.exports = changeBrightness;