├── .babelrc ├── .eslintrc.json ├── .gitignore ├── README.md ├── ambientlight.mm ├── index.js └── package.json /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "standard", 3 | "parser": "babel-eslint", 4 | "rules": { 5 | "yoda": 0, 6 | "semi": [2, "never"], 7 | "no-extra-semi": 2 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build.js 2 | ambientlight 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AmbientLight 2 | 3 | Easily get ambient data sensor readings of mac ambient light sensors. 4 | 5 | ```bash 6 | $ npm install ambientlight 7 | ``` 8 | 9 | ```javascript 10 | const ambient = require('ambientlight') 11 | console.log(ambient()) 12 | ``` 13 | 14 | # Credits 15 | 16 | The undelying ambient light implementation is inspired from: https://bugzilla.mozilla.org/attachment.cgi?id=664102 17 | -------------------------------------------------------------------------------- /ambientlight.mm: -------------------------------------------------------------------------------- 1 | // ambientlight.mm 2 | // 3 | // Inspired from: https://bugzilla.mozilla.org/attachment.cgi?id=664102&action=edit 4 | // 5 | // clang -o ambientlight ambientlight.mm -framework IOKit -framework CoreFoundation 6 | 7 | #include 8 | #import 9 | #import 10 | 11 | static io_connect_t dataPort = 0; 12 | 13 | int main(void) { 14 | kern_return_t kr; 15 | io_service_t serviceObject; 16 | CFRunLoopTimerRef updateTimer; 17 | 18 | serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController")); 19 | if (!serviceObject) { 20 | fprintf(stderr, "failed to find ambient light sensors\n"); 21 | exit(1); 22 | } 23 | 24 | kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &dataPort); 25 | IOObjectRelease(serviceObject); 26 | if (kr != KERN_SUCCESS) { 27 | mach_error("IOServiceOpen:", kr); 28 | exit(kr); 29 | } 30 | 31 | setbuf(stdout, NULL); 32 | //printf("%8ld %8ld", 0L, 0L); 33 | 34 | kern_return_t krr; 35 | uint32_t outputs = 2; 36 | uint64_t values[outputs]; 37 | 38 | krr = IOConnectCallMethod(dataPort, 0, nil, 0, nil, 0, values, &outputs, nil, 0); 39 | if (krr == KERN_SUCCESS) { 40 | printf("%8lld", values[0]); 41 | } 42 | 43 | exit(0); 44 | } 45 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import exec from 'execa' 2 | 3 | module.exports = exports.default = function ambient () { 4 | const out = exec.sync(`${__dirname}/ambientlight`).stdout 5 | return Number(out) 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ambientlight", 3 | "version": "0.1.2", 4 | "description": "", 5 | "main": "build.js", 6 | "files": [ 7 | "build.json", 8 | "ambientlight" 9 | ], 10 | "scripts": { 11 | "test": "eslint index.js", 12 | "compile": "clang -o ambientlight ambientlight.mm -framework IOKit -framework CoreFoundation", 13 | "build": "npm run compile && babel index.js > build.js", 14 | "prepublish": "npm run build" 15 | }, 16 | "author": "Jarmo Isotalo", 17 | "repository": { 18 | "type": "git", 19 | "url": "git://github.com/jamox/ambientlight.git" 20 | }, 21 | "bugs": { 22 | "url": "https://github.com/jamox/ambientlight/issues" 23 | }, 24 | "license": "MIT", 25 | "licenses": [ 26 | { 27 | "type": "MIT", 28 | "url": "http://opensource.org/licenses/MIT" 29 | } 30 | ], 31 | "devDependencies": { 32 | "babel-cli": "^6.11.4", 33 | "babel-eslint": "^6.1.2", 34 | "babel-preset-es2015": "^6.9.0", 35 | "eslint": "^3.1.1", 36 | "eslint-config-standard": "^5.3.5", 37 | "eslint-plugin-promise": "^2.0.0", 38 | "eslint-plugin-standard": "^2.0.0" 39 | }, 40 | "dependencies": { 41 | "execa": "^0.4.0" 42 | }, 43 | "eslintConfig": { 44 | "extends": "standard", 45 | "rules": { 46 | "yoda": 0, 47 | "semi": [ 48 | 2, 49 | "never" 50 | ], 51 | "no-undef": 0, 52 | "no-unused-vars": 2, 53 | "no-extra-semi": 2, 54 | "semi-spacing": [ 55 | 2, 56 | { 57 | "before": false, 58 | "after": true 59 | } 60 | ] 61 | } 62 | } 63 | } 64 | --------------------------------------------------------------------------------