nixos-configs/devices/wim/config/ags/services/brightness.js

78 lines
2 KiB
JavaScript
Raw Normal View History

2023-11-11 03:33:41 -05:00
import Service from 'resource:///com/github/Aylur/ags/service.js';
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
import { exec, execAsync } from 'resource:///com/github/Aylur/ags/utils.js';
2023-11-10 23:51:50 -05:00
const KBD = 'tpacpi::kbd_backlight';
2023-11-11 03:33:41 -05:00
const CAPS = 'input0::capslock';
2023-11-10 23:51:50 -05:00
class Brightness extends Service {
static {
Service.register(this, {
'screen': ['float'],
2023-11-11 03:33:41 -05:00
'kbd': ['float'],
2023-11-10 23:51:50 -05:00
'caps': ['int'],
});
}
_kbd = 0;
_screen = 0;
_caps = 0;
get kbd() { return this._kbd; }
get screen() { return this._screen; }
get caps() { return this._caps; }
set kbd(value) {
// TODO
}
set screen(percent) {
if (percent < 0)
percent = 0;
if (percent > 1)
percent = 1;
execAsync(`brightnessctl s ${percent * 100}% -q`)
.then(() => {
this._screen = percent;
this.emit('screen', this._screen);
})
.catch(console.error);
}
constructor() {
super();
try {
2023-11-11 03:33:41 -05:00
this.monitorKbdState();
this._caps = Number(exec(`brightnessctl -d ${CAPS} g`));
2023-11-10 23:51:50 -05:00
this._screen = Number(exec('brightnessctl g')) / Number(exec('brightnessctl m'));
} catch (error) {
console.error('missing dependancy: brightnessctl');
}
}
fetchCapsState() {
2023-11-11 03:33:41 -05:00
execAsync(`brightnessctl -d ${CAPS} g`)
2023-11-10 23:51:50 -05:00
.then(out => {
this._caps = out;
this.emit('caps', this._caps);
})
.catch(logError);
}
2023-11-11 03:33:41 -05:00
monitorKbdState() {
Variable(0, {
poll: [1000, `brightnessctl -d ${KBD} g`, out => {
if (out !== this._kbd) {
this._kbd = out;
this.emit('kbd', this._kbd);
}
}],
});
}
2023-11-10 23:51:50 -05:00
}
const brightnessService = new Brightness();
export default brightnessService;