| @@ -0,0 +1,165 @@ |
| 1 | + | pragma Singleton |
| 2 | + | import QtQuick |
| 3 | + | import Quickshell |
| 4 | + | import Quickshell.Io |
| 5 | + | import Quickshell.Services.UPower |
| 6 | + | |
| 7 | + | // Battery care: charge limit and low-battery warnings. |
| 8 | + | // The Legion firmware only has a fixed cap ("conservation mode", sysfs charge_types Long_Life). UPower |
| 9 | + | // switches it without sudo (EnableChargeThreshold, polkit allow_active) but Quickshell's UPower doesn't |
| 10 | + | // expose that, hence busctl. Settings.chargeMode: |
| 11 | + | // off normal charging |
| 12 | + | // conservation firmware cap on all the time (kept by the EC, works without aurora) |
| 13 | + | // limit80 aurora turns the cap on at 80% while plugged in (charging stops and the charge is held), |
| 14 | + | // and off again below 75% or when unplugged. Only while aurora runs. |
| 15 | + | Singleton { |
| 16 | + | id: root |
| 17 | + | |
| 18 | + | property string path: "" // the battery's UPower object, found with `upower -e` |
| 19 | + | property bool supported: false // ChargeThresholdSupported |
| 20 | + | property bool capOn: false // ChargeThresholdEnabled (conservation mode right now) |
| 21 | + | readonly property bool ready: path !== "" |
| 22 | + | // UPower reports 0% for a moment at startup: no decisions before this |
| 23 | + | property bool started: false |
| 24 | + | Timer { interval: 4000; running: true; onTriggered: { root.started = true; root.evaluate(); root.checkLow() } } |
| 25 | + | |
| 26 | + | readonly property var bat: UPower.displayDevice |
| 27 | + | readonly property bool hasBat: bat !== null && bat.isPresent |
| 28 | + | readonly property int pct: hasBat ? Math.round(bat.percentage > 1 ? bat.percentage : bat.percentage * 100) : 100 |
| 29 | + | readonly property bool onAc: !UPower.onBattery |
| 30 | + | // plugged in but not charging because of the cap |
| 31 | + | readonly property bool held: hasBat && onAc && capOn && bat.state !== UPowerDeviceState.Charging |
| 32 | + | |
| 33 | + | // ---- charge limit |
| 34 | + | function busctl(args) { |
| 35 | + | return ["busctl", "--system"].concat(args.slice(0, 1), ["org.freedesktop.UPower", root.path, |
| 36 | + | "org.freedesktop.UPower.Device"], args.slice(1)) |
| 37 | + | } |
| 38 | + | function setCap(on) { |
| 39 | + | if (!ready || !supported || setProc.running) return |
| 40 | + | setProc.want = on |
| 41 | + | setProc.command = busctl(["call", "EnableChargeThreshold", "b", on ? "true" : "false"]) |
| 42 | + | setProc.running = true |
| 43 | + | } |
| 44 | + | // what the cap should be for the current mode / charge / power source |
| 45 | + | function evaluate() { |
| 46 | + | if (!started || !ready || !supported || !hasBat || pct <= 0) return |
| 47 | + | var mode = Settings.chargeMode, want = capOn |
| 48 | + | if (mode === "conservation") want = true |
| 49 | + | else if (mode === "limit80") { |
| 50 | + | if (!onAc || pct < 75) want = false // unplugged: replugging under 80% charges again |
| 51 | + | else if (pct >= 80) want = true |
| 52 | + | } else want = false |
| 53 | + | if (want !== capOn) setCap(want) |
| 54 | + | } |
| 55 | + | |
| 56 | + | onPctChanged: { evaluate(); checkLow() } |
| 57 | + | onOnAcChanged: { evaluate(); checkLow() } |
| 58 | + | Connections { |
| 59 | + | target: Settings |
| 60 | + | function onChargeModeChanged() { root.evaluate() } |
| 61 | + | function onBatLowChanged() { root.checkLow() } |
| 62 | + | function onBatCriticalChanged() { root.checkLow() } |
| 63 | + | function onBatCriticalActionChanged() { root.warnedCritical = false; root.checkLow() } |
| 64 | + | } |
| 65 | + | |
| 66 | + | Process { |
| 67 | + | command: ["upower", "-e"] |
| 68 | + | running: true |
| 69 | + | stdout: StdioCollector { |
| 70 | + | onStreamFinished: { |
| 71 | + | var l = text.split("\n") |
| 72 | + | for (var i = 0; i < l.length; i++) |
| 73 | + | if (l[i].indexOf("/battery_") >= 0) { root.path = l[i].trim(); break } |
| 74 | + | if (root.ready) getProc.running = true |
| 75 | + | } |
| 76 | + | } |
| 77 | + | } |
| 78 | + | // "b true\nb false" |
| 79 | + | Process { |
| 80 | + | id: getProc |
| 81 | + | command: root.busctl(["get-property", "ChargeThresholdSupported", "ChargeThresholdEnabled"]) |
| 82 | + | stdout: StdioCollector { |
| 83 | + | onStreamFinished: { |
| 84 | + | var l = text.trim().split("\n") |
| 85 | + | if (l.length < 2) return |
| 86 | + | root.supported = l[0].trim() === "b true" |
| 87 | + | root.capOn = l[1].trim() === "b true" |
| 88 | + | root.evaluate() |
| 89 | + | } |
| 90 | + | } |
| 91 | + | } |
| 92 | + | Process { |
| 93 | + | id: setProc |
| 94 | + | property bool want: false |
| 95 | + | stderr: StdioCollector { id: setErr } |
| 96 | + | onExited: code => { |
| 97 | + | if (code !== 0) |
| 98 | + | Quickshell.execDetached(["notify-send", "-a", "aurora", "-i", "battery", "charge limit", |
| 99 | + | "couldn't switch conservation mode: " + setErr.text.trim()]) |
| 100 | + | getProc.running = true // read back the real state |
| 101 | + | } |
| 102 | + | } |
| 103 | + | |
| 104 | + | // ---- power-profiles-daemon present? (Quickshell's PowerProfiles has no "available" flag) |
| 105 | + | property bool hasProfiles: false |
| 106 | + | Process { |
| 107 | + | command: ["busctl", "--system", "status", "org.freedesktop.UPower.PowerProfiles"] |
| 108 | + | running: true |
| 109 | + | stdout: StdioCollector {} |
| 110 | + | onExited: code => root.hasProfiles = code === 0 |
| 111 | + | } |
| 112 | + | |
| 113 | + | // ---- low battery: one warning per level per discharge, then the critical action after 60s |
| 114 | + | property bool warnedLow: false |
| 115 | + | property bool warnedCritical: false |
| 116 | + | function checkLow() { |
| 117 | + | if (!started || (hasBat && pct <= 0)) return |
| 118 | + | if (!hasBat || onAc) { |
| 119 | + | warnedLow = warnedCritical = false |
| 120 | + | if (critical.running) { critical.stop(); critNote.running = false } |
| 121 | + | return |
| 122 | + | } |
| 123 | + | // re-arm once above a level again (a threshold changed in settings) |
| 124 | + | if (pct > Settings.batLow) warnedLow = false |
| 125 | + | if (pct > Settings.batCritical) { |
| 126 | + | warnedCritical = false |
| 127 | + | if (critical.running) { critical.stop(); critNote.running = false } |
| 128 | + | } |
| 129 | + | if (pct <= Settings.batCritical && !warnedCritical) { |
| 130 | + | warnedCritical = warnedLow = true |
| 131 | + | var act = Settings.batCriticalAction |
| 132 | + | if (act === "none") { |
| 133 | + | Quickshell.execDetached(["notify-send", "-a", "aurora", "-u", "critical", "-i", "battery-caution", |
| 134 | + | "battery critical", pct + "% left, plug in now"]) |
| 135 | + | } else { |
| 136 | + | critNote.command = ["notify-send", "-a", "aurora", "-u", "critical", "-i", "battery-caution", |
| 137 | + | "-A", "cancel=cancel", "battery critical", |
| 138 | + | pct + "% left: " + (act === "suspend" ? "suspending" : "powering off") |
| 139 | + | + " in 60s unless plugged in"] |
| 140 | + | critNote.running = true |
| 141 | + | critical.restart() |
| 142 | + | } |
| 143 | + | } else if (pct <= Settings.batLow && !warnedLow) { |
| 144 | + | warnedLow = true |
| 145 | + | Quickshell.execDetached(["notify-send", "-a", "aurora", "-u", "critical", "-i", "battery-caution", |
| 146 | + | "battery low", pct + "% left"]) |
| 147 | + | } |
| 148 | + | } |
| 149 | + | |
| 150 | + | Timer { |
| 151 | + | id: critical |
| 152 | + | interval: 60000 |
| 153 | + | onTriggered: { |
| 154 | + | critNote.running = false |
| 155 | + | Session.run(Settings.batCriticalAction === "suspend" ? "sleep" : "shutdown") |
| 156 | + | } |
| 157 | + | } |
| 158 | + | // the critical warning's "cancel" button |
| 159 | + | Process { |
| 160 | + | id: critNote |
| 161 | + | stdout: StdioCollector { |
| 162 | + | onStreamFinished: if (text.trim() === "cancel") critical.stop() |
| 163 | + | } |
| 164 | + | } |
| 165 | + | } |