2023-11-21 01:29:46 -05:00
|
|
|
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';
|
2023-11-21 01:29:46 -05:00
|
|
|
const INTERVAL = 500;
|
2023-12-07 17:25:27 -05:00
|
|
|
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, {
|
2023-11-21 01:29:46 -05:00
|
|
|
screen: ['float'],
|
|
|
|
kbd: ['float'],
|
|
|
|
caps: ['int'],
|
2023-11-27 20:57:07 -05:00
|
|
|
}, {
|
2023-12-07 17:25:27 -05:00
|
|
|
'screen-icon': ['string', 'rw'],
|
2023-11-27 20:57:07 -05:00
|
|
|
'caps-icon': ['string', 'rw'],
|
2023-11-10 23:51:50 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
#kbd = 0;
|
|
|
|
#screen = 0;
|
2023-12-07 17:25:27 -05:00
|
|
|
#screenIcon = 'display-brightness-symbolic';
|
2023-11-21 01:29:46 -05:00
|
|
|
#caps = 0;
|
2023-11-27 20:57:07 -05:00
|
|
|
#capsIcon = 'caps-lock-symbolic';
|
2023-11-10 23:51:50 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
get kbd() {
|
|
|
|
return this.#kbd;
|
|
|
|
}
|
|
|
|
|
|
|
|
get screen() {
|
|
|
|
return this.#screen;
|
|
|
|
}
|
|
|
|
|
2023-12-07 17:25:27 -05:00
|
|
|
get screenIcon() {
|
|
|
|
return this.#screenIcon;
|
|
|
|
}
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
get caps() {
|
|
|
|
return this.#caps;
|
|
|
|
}
|
2023-11-10 23:51:50 -05:00
|
|
|
|
2023-11-27 20:57:07 -05:00
|
|
|
get capsIcon() {
|
|
|
|
return this.#capsIcon;
|
|
|
|
}
|
|
|
|
|
2023-11-10 23:51:50 -05:00
|
|
|
set kbd(value) {
|
2023-11-21 01:29:46 -05:00
|
|
|
this.#kbd = value;
|
2023-11-10 23:51:50 -05:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
set screen(percent) {
|
2023-11-21 01:29:46 -05:00
|
|
|
if (percent < 0) {
|
2023-11-10 23:51:50 -05:00
|
|
|
percent = 0;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-10 23:51:50 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
if (percent > 1) {
|
2023-11-10 23:51:50 -05:00
|
|
|
percent = 1;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-10 23:51:50 -05:00
|
|
|
|
|
|
|
execAsync(`brightnessctl s ${percent * 100}% -q`)
|
|
|
|
.then(() => {
|
2023-11-21 01:29:46 -05:00
|
|
|
this.#screen = percent;
|
2023-12-07 17:25:27 -05:00
|
|
|
this.#getScreenIcon();
|
2023-11-21 01:29:46 -05:00
|
|
|
this.emit('screen', this.#screen);
|
2023-11-10 23:51:50 -05:00
|
|
|
})
|
|
|
|
.catch(console.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
try {
|
2023-11-21 01:29:46 -05:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 17:25:27 -05:00
|
|
|
#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() {
|
2023-11-11 03:33:41 -05:00
|
|
|
execAsync(`brightnessctl -d ${CAPS} g`)
|
2023-11-21 01:29:46 -05:00
|
|
|
.then((out) => {
|
2023-11-27 20:57:07 -05:00
|
|
|
this.#caps = Number(out);
|
|
|
|
this.#capsIcon = this.#caps ?
|
|
|
|
'caps-lock-symbolic' :
|
|
|
|
'capslock-disabled-symbolic';
|
|
|
|
|
|
|
|
this.notify('caps-icon');
|
2023-11-21 01:29:46 -05:00
|
|
|
this.emit('caps', this.#caps);
|
2023-11-10 23:51:50 -05:00
|
|
|
})
|
|
|
|
.catch(logError);
|
|
|
|
}
|
2023-11-11 03:33:41 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
#monitorKbdState() {
|
2023-11-11 03:33:41 -05:00
|
|
|
Variable(0, {
|
2023-12-23 01:14:21 -05:00
|
|
|
poll: [
|
|
|
|
INTERVAL,
|
|
|
|
`brightnessctl -d ${KBD} g`,
|
|
|
|
(out) => {
|
|
|
|
if (parseInt(out) !== this.#kbd) {
|
|
|
|
this.#kbd = parseInt(out);
|
|
|
|
this.emit('kbd', this.#kbd);
|
|
|
|
|
|
|
|
return this.#kbd;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
2023-11-11 03:33:41 -05:00
|
|
|
});
|
|
|
|
}
|
2023-11-10 23:51:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const brightnessService = new Brightness();
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-11-10 23:51:50 -05:00
|
|
|
export default brightnessService;
|