aboutsummaryrefslogtreecommitdiff
path: root/.config/ags/service/brightness.ts
blob: a0b8eb55c6b9fa4d4e46b06682ec0d071d57d55d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { bash, dependencies, sh } from "lib/utils"

if (!dependencies("brightnessctl"))
    App.quit()

const get = (args: string) => Number(Utils.exec(`brightnessctl ${args}`))
const screen = await bash`ls -w1 /sys/class/backlight | head -1`
const kbd = await bash`ls -w1 /sys/class/leds | head -1`

class Brightness extends Service {
    static {
        Service.register(this, {}, {
            "screen": ["float", "rw"],
            "kbd": ["int", "rw"],
        })
    }

    #kbdMax = get(`--device ${kbd} max`)
    #kbd = get(`--device ${kbd} get`)
    #screenMax = get("max")
    #screen = get("get") / get("max")

    get kbd() { return this.#kbd }
    get screen() { return this.#screen }

    set kbd(value) {
        if (value < 0 || value > this.#kbdMax)
            return

        sh(`brightnessctl -d ${kbd} s ${value} -q`).then(() => {
            this.#kbd = value
            this.changed("kbd")
        })
    }

    set screen(percent) {
        if (percent < 0)
            percent = 0

        if (percent > 1)
            percent = 1

        sh(`brightnessctl set ${Math.floor(percent * 100)}% -q`).then(() => {
            this.#screen = percent
            this.changed("screen")
        })
    }

    constructor() {
        super()

        const screenPath = `/sys/class/backlight/${screen}/brightness`
        const kbdPath = `/sys/class/leds/${kbd}/brightness`

        Utils.monitorFile(screenPath, async f => {
            const v = await Utils.readFileAsync(f)
            this.#screen = Number(v) / this.#screenMax
            this.changed("screen")
        })

        Utils.monitorFile(kbdPath, async f => {
            const v = await Utils.readFileAsync(f)
            this.#kbd = Number(v) / this.#kbdMax
            this.changed("kbd")
        })
    }
}

export default new Brightness