summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell/Services/Brightness.qml
blob: 87c7c05b113c92a37b5615bb6344834863c05626 (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
pragma Singleton

import QtQml
import Quickshell
import Quickshell.Io
import Custom as Custom

Singleton {
  id: root

  property string subsystem: "backlight"
  property string device: "intel_backlight"

  property real currBrightness
  property real exponent: 4

  function calcCurrBrightness() {
    if (!currFile.loaded || !maxFile.loaded)
      return undefined;
    const curr = Number(currFile.text());
    const max = Number(maxFile.text());
    const val = Math.pow(curr / max, 1 / root.exponent);
    return val;
  }

  Component.onCompleted: root.currBrightness = root.calcCurrBrightness()
  Connections {
    target: currFile
    function onLoaded() { root.currBrightness = root.calcCurrBrightness(); }
  }
  Connections {
    target: maxFile
    function onLoaded() { root.currBrightness = root.calcCurrBrightness(); }
  }

  onCurrBrightnessChanged: {
    root.currBrightness = Math.max(0, Math.min(1, root.currBrightness));

    const prev = root.calcCurrBrightness();
    if (typeof prev === 'undefined' || Math.abs(root.currBrightness - prev) < 0.01)
      return;

    const max = Number(maxFile.text());
    const actual = Number(currFile.text());
    let curr = Math.max(0, Math.min(max, Math.pow(root.currBrightness, root.exponent) * max));
    if (Math.round(curr) == actual && curr < actual)
      curr = Math.max(0, actual - 1);
    else if (Math.round(curr) == actual && curr > actual)
      curr = Math.min(max, actual + 1);
    // root.currBrightness = Math.pow(curr / max, 1 / root.exponent);
    Custom.Systemd.setBrightness(root.subsystem, root.device, Math.round(curr));
  }

  FileView {
    id: currFile
    path: `/sys/class/${root.subsystem}/${root.device}/brightness`
    blockAllReads: true
    watchChanges: true
    onFileChanged: reload()
  }
  FileView {
    id: maxFile
    path: `/sys/class/${root.subsystem}/${root.device}/max_brightness`
    blockAllReads: true
    watchChanges: true
    onFileChanged: reload()
  }
}