diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2026-09-23 21:08:35 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2026-09-23 21:08:35 +0200 |
| commit | ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71 (patch) | |
| tree | 141f738276cf84c6b7e5633c2bd5f2b44e9b0b75 /accounts/gkleen@skadhi/shell/quickshell/Services/Brightness.qml | |
| parent | 6a763272038e637a8a03dd9059d43a1ff44921dd (diff) | |
| download | nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.gz nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.bz2 nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.xz nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.zip | |
...
Diffstat (limited to 'accounts/gkleen@skadhi/shell/quickshell/Services/Brightness.qml')
| -rw-r--r-- | accounts/gkleen@skadhi/shell/quickshell/Services/Brightness.qml | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/accounts/gkleen@skadhi/shell/quickshell/Services/Brightness.qml b/accounts/gkleen@skadhi/shell/quickshell/Services/Brightness.qml new file mode 100644 index 00000000..f3ae6804 --- /dev/null +++ b/accounts/gkleen@skadhi/shell/quickshell/Services/Brightness.qml | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | pragma Singleton | ||
| 2 | |||
| 3 | import QtQml | ||
| 4 | import Quickshell | ||
| 5 | import Quickshell.Io | ||
| 6 | import Custom as Custom | ||
| 7 | import qs.Services | ||
| 8 | |||
| 9 | Singleton { | ||
| 10 | id: root | ||
| 11 | |||
| 12 | property string subsystem: "backlight" | ||
| 13 | property string device: "intel_backlight" | ||
| 14 | |||
| 15 | property real currBrightness | ||
| 16 | property real exponent: 4 | ||
| 17 | |||
| 18 | function calcCurrBrightness() { | ||
| 19 | if (!currFile.loaded || !maxFile.loaded) | ||
| 20 | return undefined; | ||
| 21 | const curr = Number(currFile.text()); | ||
| 22 | const max = Number(maxFile.text()); | ||
| 23 | const val = Math.pow(curr / max, 1 / root.exponent); | ||
| 24 | return val; | ||
| 25 | } | ||
| 26 | |||
| 27 | Connections { | ||
| 28 | target: currFile | ||
| 29 | function onLoaded() { | ||
| 30 | const b = root.calcCurrBrightness(); | ||
| 31 | if (typeof b !== 'undefined') | ||
| 32 | root.currBrightness = b; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | Connections { | ||
| 36 | target: maxFile | ||
| 37 | function onLoaded() { | ||
| 38 | const b = root.calcCurrBrightness(); | ||
| 39 | if (typeof b !== 'undefined') | ||
| 40 | root.currBrightness = b; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | onCurrBrightnessChanged: { | ||
| 45 | root.currBrightness = Math.max(0, Math.min(1, root.currBrightness)); | ||
| 46 | |||
| 47 | const prev = root.calcCurrBrightness(); | ||
| 48 | if (typeof prev === 'undefined' || Math.abs(root.currBrightness - prev) < 0.01) | ||
| 49 | return; | ||
| 50 | |||
| 51 | const max = Number(maxFile.text()); | ||
| 52 | const actual = Number(currFile.text()); | ||
| 53 | let curr = Math.max(0, Math.min(max, Math.pow(root.currBrightness, root.exponent) * max)); | ||
| 54 | if (Math.round(curr) == actual && curr < actual) | ||
| 55 | curr = Math.max(0, actual - 1); | ||
| 56 | else if (Math.round(curr) == actual && curr > actual) | ||
| 57 | curr = Math.min(max, actual + 1); | ||
| 58 | // root.currBrightness = Math.pow(curr / max, 1 / root.exponent); | ||
| 59 | Custom.Systemd.setBrightness(root.subsystem, root.device, Math.round(curr)); | ||
| 60 | } | ||
| 61 | |||
| 62 | FileView { | ||
| 63 | id: currFile | ||
| 64 | path: `/sys/class/${root.subsystem}/${root.device}/brightness` | ||
| 65 | blockAllReads: true | ||
| 66 | watchChanges: true | ||
| 67 | onFileChanged: reload() | ||
| 68 | } | ||
| 69 | FileView { | ||
| 70 | id: maxFile | ||
| 71 | path: `/sys/class/${root.subsystem}/${root.device}/max_brightness` | ||
| 72 | blockAllReads: true | ||
| 73 | watchChanges: true | ||
| 74 | onFileChanged: reload() | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | Timer { | ||
| 79 | id: startupDelay | ||
| 80 | interval: 500 | ||
| 81 | running: true | ||
| 82 | repeat: false | ||
| 83 | } | ||
| 84 | property bool onlyInternal: true | ||
| 85 | Connections { | ||
| 86 | target: NiriService | ||
| 87 | function onOutputsChanged() { | ||
| 88 | root.onlyInternal = Object.keys(NiriService.outputs).every(oname => oname == "eDP-1"); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | onOnlyInternalChanged: { | ||
| 92 | if (startupDelay.running) | ||
| 93 | return; | ||
| 94 | |||
| 95 | if (!root.onlyInternal) | ||
| 96 | root.currBrightness = 1 | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | IpcHandler { | ||
| 101 | target: "Brightness" | ||
| 102 | |||
| 103 | function set(brightness: real): void { | ||
| 104 | root.currBrightness = brightness; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
