feat(ags osd): add kb backlight osd

This commit is contained in:
matt1432 2023-11-11 03:33:41 -05:00
parent 2bf8e0cf8e
commit a20c59185a
3 changed files with 51 additions and 9 deletions

View file

@ -0,0 +1,28 @@
import { Box, Icon, ProgressBar } from 'resource:///com/github/Aylur/ags/widget.js';
export default () => Box({
className: 'osd',
children: [
Icon({
hpack: 'start',
icon: 'keyboard-brightness-symbolic',
}),
ProgressBar({
vpack: 'center',
connections: [[Brightness, self => {
if (!self.value) {
self.value = Brightness.kbd / 2;
return;
}
self.value = Brightness.kbd / 2;
self.sensitive = Brightness.kbd !== 0;
const stack = self.get_parent().get_parent();
stack.shown = 'kbd';
stack.resetTimer();
}, 'kbd']],
}),
],
});

View file

@ -7,7 +7,8 @@ import PopupWindow from '../misc/popup.js';
import Audio from './audio.js'; import Audio from './audio.js';
import Brightness from './brightness.js'; import Brightness from './brightness.js';
import Caps from './caps.js'; import CapsLock from './caps.js';
import Keyboard from './kbd.js';
import Microphone from './mic.js'; import Microphone from './mic.js';
@ -19,7 +20,8 @@ export default () => {
items: [ items: [
['audio', Audio()], ['audio', Audio()],
['brightness', Brightness()], ['brightness', Brightness()],
['caps', Caps()], ['caps', CapsLock()],
['kbd', Keyboard()],
['mic', Microphone()], ['mic', Microphone()],
], ],
}); });

View file

@ -1,14 +1,15 @@
import Service from 'resource:///com/github/Aylur/ags/service.js'; import Service from 'resource:///com/github/Aylur/ags/service.js';
import { exec, execAsync, readFileAsync } from 'resource:///com/github/Aylur/ags/utils.js'; import Variable from 'resource:///com/github/Aylur/ags/variable.js';
import { exec, execAsync } from 'resource:///com/github/Aylur/ags/utils.js';
const KBD = 'tpacpi::kbd_backlight'; const KBD = 'tpacpi::kbd_backlight';
const CAPS = '/sys/class/leds/input0::capslock/brightness'; const CAPS = 'input0::capslock';
class Brightness extends Service { class Brightness extends Service {
static { static {
Service.register(this, { Service.register(this, {
'screen': ['float'], 'screen': ['float'],
'kbd': ['int'], 'kbd': ['float'],
'caps': ['int'], 'caps': ['int'],
}); });
} }
@ -43,8 +44,8 @@ class Brightness extends Service {
constructor() { constructor() {
super(); super();
try { try {
this._kbd = Number(exec(`brightnessctl -d ${KBD} g`)); this.monitorKbdState();
this._kbdMax = Number(exec(`brightnessctl -d ${KBD} m`)); this._caps = Number(exec(`brightnessctl -d ${CAPS} g`));
this._screen = Number(exec('brightnessctl g')) / Number(exec('brightnessctl m')); this._screen = Number(exec('brightnessctl g')) / Number(exec('brightnessctl m'));
} catch (error) { } catch (error) {
console.error('missing dependancy: brightnessctl'); console.error('missing dependancy: brightnessctl');
@ -52,13 +53,24 @@ class Brightness extends Service {
} }
fetchCapsState() { fetchCapsState() {
readFileAsync(CAPS) execAsync(`brightnessctl -d ${CAPS} g`)
.then(out => { .then(out => {
this._caps = out; this._caps = out;
this.emit('caps', this._caps); this.emit('caps', this._caps);
}) })
.catch(logError); .catch(logError);
} }
monitorKbdState() {
Variable(0, {
poll: [1000, `brightnessctl -d ${KBD} g`, out => {
if (out !== this._kbd) {
this._kbd = out;
this.emit('kbd', this._kbd);
}
}],
});
}
} }
const brightnessService = new Brightness(); const brightnessService = new Brightness();