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

93 lines
2.1 KiB
JavaScript
Raw Normal View History

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