├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.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_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.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 | } 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 'iojs' 5 | - '0.12' 6 | - '0.10' 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | 5 | module.exports.get = function (cb) { 6 | var dir = '/sys/class/backlight'; 7 | 8 | if (process.platform !== 'linux') { 9 | throw new Error('Only Linux systems are supported'); 10 | } 11 | 12 | fs.readdir(dir, function (err, dirs) { 13 | if (err) { 14 | cb(err); 15 | return; 16 | } 17 | 18 | if (!dirs.length) { 19 | cb(new Error('No backlight device found')); 20 | return; 21 | } 22 | 23 | fs.readFile(path.join(dir, dirs[0], 'max_brightness'), 'utf8', function (err, max) { 24 | if (err) { 25 | cb(err); 26 | return; 27 | } 28 | 29 | fs.readFile(path.join(dir, dirs[0], 'brightness'), 'utf8', function (err, data) { 30 | if (err) { 31 | cb(err); 32 | return; 33 | } 34 | 35 | cb(null, parseInt(data) / parseInt(max)); 36 | }); 37 | }); 38 | }); 39 | }; 40 | 41 | module.exports.set = function (val, cb) { 42 | var dir = '/sys/class/backlight'; 43 | 44 | if (process.platform !== 'linux') { 45 | throw new Error('Only Linux systems are supported'); 46 | } 47 | 48 | if (typeof val !== 'number' || val < 0 || val > 1) { 49 | throw new Error('Expected a number between 0 and 1'); 50 | } 51 | 52 | fs.readdir(dir, function (err, dirs) { 53 | if (err) { 54 | cb(err); 55 | return; 56 | } 57 | 58 | if (!dirs.length) { 59 | cb(new Error('No backlight device found')); 60 | return; 61 | } 62 | 63 | fs.readFile(path.join(dir, dirs[0], 'max_brightness'), 'utf8', function (err, data) { 64 | if (err) { 65 | cb(err); 66 | return; 67 | } 68 | 69 | var max = parseInt(data); 70 | var brightness = Math.floor(val * max).toString(); 71 | 72 | fs.writeFile(path.join(dir, dirs[0], 'brightness'), brightness, function (err) { 73 | if (err && err.code === 'EACCES') { 74 | err.message = 'You don\'t seem to have permission to change the brightness. Try running this command with sudo.'; 75 | } 76 | 77 | if (err) { 78 | cb(err); 79 | return; 80 | } 81 | 82 | cb(); 83 | }); 84 | }); 85 | }); 86 | }; 87 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Kevin Mårtensson 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xdg-brightness", 3 | "version": "2.0.3", 4 | "description": "Change screen brightness on Linux systems", 5 | "license": "MIT", 6 | "repository": "kevva/xdg-brightness", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "https://github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "node test.js" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "brightness", 23 | "linux", 24 | "screen" 25 | ], 26 | "dependencies": { 27 | "meow": "^3.1.0" 28 | }, 29 | "devDependencies": { 30 | "ava": "^0.0.4" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # xdg-brightness [![Build Status](https://travis-ci.org/kevva/xdg-brightness.svg?branch=master)](https://travis-ci.org/kevva/xdg-brightness) 2 | 3 | > Change screen brightness on Linux systems 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save xdg-brightness 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | var xdgBrightness = require('xdg-brightness'); 17 | 18 | xdgBrightness.get(function (err, brightness) { 19 | console.log(brightness); 20 | // 0.5 21 | }); 22 | 23 | xdgBrightness.set(0.8, function (err) { 24 | console.log('Changed brightness to 80%'); 25 | }); 26 | ``` 27 | 28 | 29 | ## CLI 30 | 31 | See the [brightness](https://github.com/kevva/brightness) CLI. 32 | 33 | 34 | ## License 35 | 36 | MIT © [Kevin Mårtensson](https://github.com/kevva) 37 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var test = require('ava'); 3 | var xdgBrightness = require('./'); 4 | 5 | if (!process.env.CI) { 6 | test('should get brightness', function (t) { 7 | t.plan(2); 8 | 9 | xdgBrightness.get(function (err, brightness) { 10 | t.assert(!err, err); 11 | t.assert(brightness); 12 | }); 13 | }); 14 | 15 | test('should set brightness', function (t) { 16 | t.plan(3); 17 | 18 | xdgBrightness.set(0.5, function (err) { 19 | t.assert(!err, err); 20 | 21 | xdgBrightness.get(function (err, brightness) { 22 | t.assert(!err, err); 23 | t.assert(parseInt(brightness) === 0.5); 24 | }); 25 | }); 26 | }); 27 | } 28 | --------------------------------------------------------------------------------