├── .gitignore ├── .gitattributes ├── .jshintrc ├── .editorconfig ├── test.js ├── README.md ├── package.json ├── LICENSE └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise": true, 3 | "curly": true, 4 | "esnext": true, 5 | "immed": true, 6 | "newcap": true, 7 | "noarg": true, 8 | "node": true, 9 | "strict": true, 10 | "undef": true, 11 | "unused": "vars" 12 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.{json,yml}] 11 | indent_size = 2 12 | indent_style = space 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var test = require('ava'); 3 | var winBrightness = require('./'); 4 | 5 | if (!process.env.CI) { 6 | test('get level', function (t) { 7 | t.plan(2); 8 | 9 | winBrightness.get(function (err, brightness) { 10 | t.assert(!err, err); 11 | t.assert(typeof brightness === 'number'); 12 | }); 13 | }); 14 | 15 | test('set level to 75%', function (t) { 16 | t.plan(3); 17 | 18 | var brightness = 0.75; 19 | 20 | winBrightness.set(brightness, function (err) { 21 | t.assert(!err, err); 22 | 23 | winBrightness.get(function (err, brightness) { 24 | t.assert(!err, err); 25 | t.assert(brightness === brightness); 26 | }); 27 | }); 28 | }); 29 | 30 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # win-brightness 2 | > Get or set screen brightness in Windows 3 | 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install --save win-brightness 9 | ``` 10 | 11 | 12 | ## Usage 13 | 14 | ```js 15 | var winBrightness = require('win-brightness'); 16 | 17 | winBrightness.get(function (err, brightness) { 18 | console.log(brightness); 19 | //=> 0.5 20 | }); 21 | 22 | winBrightness.set(0.75, function (err) { 23 | console.log('Changed brightness to 75%'); 24 | }); 25 | 26 | ``` 27 | 28 | Based on [osx-brightness](https://github.com/gillstrom/osx-brightness) and [xdg-brightness](https://www.npmjs.com/package/xdg-brightness). Built to be used 29 | by [brightness](https://www.npmjs.com/package/brightness). 30 | 31 | ## License 32 | 33 | MIT © [Sondre Bjellås](http://sondreb.com) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "win-brightness", 3 | "version": "1.0.0", 4 | "description": "Get or set screen brightness in Windows", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/sondreb/win-brightness.git" 12 | }, 13 | "keywords": [ 14 | "win", 15 | "windows", 16 | "brightness", 17 | "screen" 18 | ], 19 | "author": { 20 | "name": "Sondre Bjellås", 21 | "email": "sondre@outlook.com", 22 | "url": "sondreb.com" 23 | }, 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/sondreb/win-brightness/issues" 27 | }, 28 | "homepage": "https://github.com/sondreb/win-brightness", 29 | "dependencies": { 30 | "nircmd": "^1.1.1", 31 | "wmi-client": "0.1.4" 32 | }, 33 | "devDependencies": { 34 | "ava": "^0.0.4" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sondre Bjellås 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var WmiClient = require('wmi-client'); 4 | var nircmd = require('nircmd'); 5 | var path = require('path'); 6 | 7 | // Will be added whenever wmic support works properly. 8 | //module.exports.options = { 9 | // timeout: 0, // Seconds for brightness level to change, can be used to get smooth change. 10 | // monitor: 0 // Used to specify index for monitor to get/set level on. 11 | //}; 12 | 13 | module.exports.get = function(cb) { 14 | if (process.platform !== 'win32') { 15 | throw new Error('Only Windows systems are supported.'); 16 | } 17 | 18 | var wmi = new WmiClient({ 19 | host: 'localhost', 20 | namespace: '\\\\root\\WMI' 21 | }); 22 | 23 | wmi.query('SELECT CurrentBrightness,InstanceName FROM WmiMonitorBrightness', function (err, result) { 24 | 25 | if (err) { 26 | cb(err); 27 | return; 28 | } 29 | 30 | if (result.length == 0) { 31 | cb(new Error('Unable to find any monitors to read brightness levels from.')); 32 | } 33 | 34 | var brightnessValue = result[0].CurrentBrightness; 35 | 36 | cb(null, brightnessValue / 100); 37 | }); 38 | }; 39 | 40 | module.exports.set = function(val, cb) { 41 | 42 | if (process.platform !== 'win32') { 43 | throw new Error('Only Windows systems are supported.'); 44 | } 45 | 46 | if (typeof val !== 'number' || isNaN(val)) { 47 | throw new TypeError('Expected a number.'); 48 | } 49 | 50 | nircmd(['setbrightness', Math.round(val * 100)], function (err) { 51 | if (err) { 52 | cb(err); 53 | return; 54 | } 55 | 56 | cb(); 57 | }); 58 | 59 | // Would like to use wmic directly to set brightness, but execution of the method returns errors for different syntax. 60 | // Need some help figuring out the proper way to CALL WmiSetBrightness using wmic. 61 | // Want this working to remove dependency on NirCmd. "wmic" is installed with Windows. 62 | 63 | // wmic /namespace:\\root\wmi CLASS WmiMonitorBrightnessMethods CALL WmiSetBrightness Timeout=0 Brightness=100 64 | // Description = Invalid method Parameter(s) 65 | 66 | // wmic /namespace:\\root\wmi CLASS WmiMonitorBrightnessMethods CALL WmiSetBrightness(0,100) 67 | // Description = Out of present range. 68 | 69 | // wmic /namespace:\\root\wmi CLASS WmiMonitorBrightnessMethods CALL WmiSetBrightness 0 25 70 | // Invalid format. 71 | // Hint: = [, ]. 72 | 73 | // Execution with powershell.exe from node.js have such a high performance penatly, it works to execute WmiSetBrightness 74 | // from PowerShell, yet wmic is prefered and need a solution that works. 75 | }; 76 | --------------------------------------------------------------------------------