feat: add brightness service

This commit is contained in:
Anthony Rodriguez 2025-02-14 14:32:02 +01:00
parent 1a23a45283
commit 96098fae19
Signed by: nezia
SSH key fingerprint: SHA256:R/ue1eTzTHUoo77lJD/3fSUsyL4AwvcHImU5BAZai+8
2 changed files with 52 additions and 0 deletions

View file

@ -46,6 +46,7 @@
(ags.packages.${system}.default.override { (ags.packages.${system}.default.override {
extraPackages = astalPkgs; extraPackages = astalPkgs;
}) })
pkgs.brightnessctl
]; ];
}; };
}; };

51
service/brightness.ts Normal file
View file

@ -0,0 +1,51 @@
import GObject, { register, property } from "astal/gobject";
import { monitorFile, readFileAsync } from "astal/file";
import { exec, execAsync } from "astal/process";
const get = (args: string) => Number(exec(`brightnessctl ${args}`));
const screen = exec(`bash -c "ls -w1 /sys/class/backlight | head -1"`);
@register({ GTypeName: "Brightness" })
export default class Brightness extends GObject.Object {
static instance: Brightness;
static get_default() {
if (!this.instance) this.instance = new Brightness();
return this.instance;
}
#screenMax = get("max");
#screen = get("get") / (this.#screenMax || 1);
@property(Number)
get screen() {
return this.#screen;
}
set screen(percent) {
console.log(percent);
if (percent < 0) percent = 0;
if (percent > 1) percent = 1;
execAsync(`brightnessctl set ${Math.floor(percent * 100)}% -q`).then(
() => {
console.log(percent);
this.#screen = percent;
this.notify("screen");
},
);
}
constructor() {
super();
const screenPath = `/sys/class/backlight/${screen}/brightness`;
monitorFile(screenPath, async (f) => {
const value = await readFileAsync(f);
this.#screen = Number(value) / this.#screenMax;
this.notify("screen");
});
}
}