nixos-configs/nixosModules/ags/config/services/brightness.ts

152 lines
3.7 KiB
TypeScript
Raw Normal View History

const { exec, execAsync } = Utils;
2023-11-10 23:51:50 -05:00
const KBD = 'tpacpi::kbd_backlight';
const INTERVAL = 500;
const SCREEN_ICONS = {
90: 'display-brightness-high-symbolic',
70: 'display-brightness-medium-symbolic',
20: 'display-brightness-low-symbolic',
5: 'display-brightness-off-symbolic',
};
2023-11-10 23:51:50 -05:00
class Brightness extends Service {
static {
Service.register(this, {
screen: ['float'],
kbd: ['float'],
caps: ['int'],
}, {
'screen-icon': ['string', 'rw'],
'caps-icon': ['string', 'rw'],
2023-11-10 23:51:50 -05:00
});
}
#kbd = 0;
2023-12-27 20:06:28 -05:00
#kbdMax = 0;
#screen = 0;
#screenIcon = 'display-brightness-symbolic';
2024-03-24 17:23:38 -04:00
#capsName = 'input0::capslock';
#caps = 0;
#capsIcon = 'caps-lock-symbolic';
2023-11-10 23:51:50 -05:00
2024-03-24 17:23:38 -04:00
get capsName() {
return this.#capsName;
}
set capsName(value: string) {
this.#capsName = value;
}
get kbd() {
return this.#kbd;
}
2023-11-10 23:51:50 -05:00
set kbd(value) {
2023-12-27 20:06:28 -05:00
if (value < 0 || value > this.#kbdMax) {
return;
}
execAsync(`brightnessctl -d ${KBD} s ${value} -q`)
.then(() => {
this.#kbd = value;
this.emit('kbd', this.#kbd);
})
.catch(console.error);
2023-11-10 23:51:50 -05:00
}
get screen() {
return this.#screen;
}
2023-11-10 23:51:50 -05:00
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.#getScreenIcon();
this.emit('screen', this.#screen);
2023-11-10 23:51:50 -05:00
})
.catch(console.error);
}
get screenIcon() {
return this.#screenIcon;
}
get caps() {
return this.#caps;
}
get capsIcon() {
return this.#capsIcon;
}
2023-11-10 23:51:50 -05:00
constructor() {
super();
try {
this.#monitorKbdState();
2023-12-27 20:06:28 -05:00
this.#kbdMax = Number(exec(`brightnessctl -d ${KBD} m`));
2024-03-24 17:23:38 -04:00
this.#caps = Number(exec(`bash -c brightnessctl -d ${this.#capsName} g`));
this.#screen = Number(exec('brightnessctl g')) / Number(exec('brightnessctl m'));
}
catch (_e) {
2023-11-10 23:51:50 -05:00
console.error('missing dependancy: brightnessctl');
}
}
#getScreenIcon() {
const brightness = this.#screen * 100;
// eslint-disable-next-line
for (const threshold of [4, 19, 69, 89]) {
if (brightness > threshold + 1) {
this.#screenIcon = SCREEN_ICONS[threshold + 1];
this.notify('screen-icon');
}
}
}
2023-11-10 23:51:50 -05:00
fetchCapsState() {
2024-03-24 17:23:38 -04:00
execAsync(`brightnessctl -d ${this.#capsName} g`)
.then((out) => {
this.#caps = Number(out);
this.#capsIcon = this.#caps ?
'caps-lock-symbolic' :
'capslock-disabled-symbolic';
this.notify('caps-icon');
this.emit('caps', this.#caps);
2023-11-10 23:51:50 -05:00
})
.catch(logError);
}
2023-11-11 03:33:41 -05:00
#monitorKbdState() {
2024-03-21 22:16:55 -04:00
const interval = setInterval(() => {
execAsync(`brightnessctl -d ${KBD} g`).then(
2023-12-23 01:14:21 -05:00
(out) => {
if (parseInt(out) !== this.#kbd) {
this.#kbd = parseInt(out);
this.emit('kbd', this.#kbd);
return this.#kbd;
}
},
2024-03-21 22:16:55 -04:00
).catch(() => {
// @ts-expect-error this works in ags
2024-03-21 22:16:55 -04:00
interval.destroy();
});
}, INTERVAL);
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;