3 Commits
Author SHA1 Message Date
MrBlakeandClaude Opus 5.5 571cfcca2a Add power settings for the lighting, bump to 1.4.0
New Power page:
- toggle for the firmware's 30 s keyboard backlight timeout
- idle timeout for keyboard and lightbar, separately for AC and battery;
  predatord watches all input devices and restores the lighting on the
  next input
- battery rules: dimmed keyboard brightness, keyboard off, lightbar off

The rules only change what is sent to the hardware; the saved lighting
settings stay untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 17:59:42 +02:00
MrBlakeandClaude Opus 5.5 5b7534560f Open the app with the Predator key, bump to 1.3.0
The Predator key sends keyboard usage 0xC9, which lies outside the
logical range the keyboard declares, so the kernel never emits an input
event for it. It is also reported on vendor report 4 of USB interface 2
(04 81 ff on press). predatord now listens there and opens the app in
the active graphical session via the user's systemd manager.

The action is configurable on the System page: open the app, switch to
the next performance mode, or do nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 17:51:21 +02:00
MrBlakeandClaude Opus 5.5 cc4fdb9cd0 Add built-in updater, bump to 1.2.0
- predatord checks the Gitea releases every 6 hours, downloads the
  newest pacman package, verifies its SHA-256 from the release notes
  and installs it with `pacman -U` in a separate systemd unit
- Update notice and install button on the System page, in the sidebar
  and in the panel widget; `predatorctl update [--check] [--yes]`
- Optional API token for private repositories
- packaging/release.py builds and publishes releases in the format
  the updater expects

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 17:42:02 +02:00
17 changed files with 738 additions and 15 deletions
+29
View File
@@ -1,5 +1,34 @@
# Changelog
## 1.4.0 – 2026-10-07
### Added
- **Power page** with energy settings for the lighting:
- toggle for the firmware's automatic keyboard backlight switch-off after 30 s
(moved here from the System page)
- turn keyboard and lightbar off after a configurable idle time, separately for AC and battery
(never, 30 s … 30 min); any keyboard, touchpad or mouse input turns them back on
- on battery: reduced keyboard brightness, keyboard lighting off, lightbar off
## 1.3.0 – 2026-10-07
### Added
- **Predator key opens the app.** The key produces no input event on Linux (its usage code is
outside the range the keyboard declares), so predatord listens on the keyboard's vendor HID
report instead and opens Predator Control in the active desktop session
- Choose the key's action on the System page: open the app, switch to the next performance mode,
or do nothing
## 1.2.0 – 2026-10-07
### Added
- **Built-in updater**: predatord checks the release page every 6 hours (configurable), downloads the
new pacman package, verifies its SHA-256 and installs it with `pacman -U` in a separate systemd unit
- Update notice and *Install* button on the System page, in the sidebar and in the panel widget
- `predatorctl update [--check] [--yes]`
- Optional API token in `/etc/predator-control/update-token` for private repositories
- `packaging/release.py` builds the package and publishes a release in the format the updater expects
## 1.1.0 – 2026-10-07
### Added
+23 -1
View File
@@ -48,7 +48,7 @@ Download `predator-control-<version>-any.pkg.tar.zst` from the
```bash
sudo pacman -S --needed linux-cachyos-headers clang lld llvm # headers for your kernel (stock Arch: linux-headers)
sudo pacman -U predator-control-1.1.0-1-any.pkg.tar.zst
sudo pacman -U predator-control-*-any.pkg.tar.zst
```
The service starts automatically and the kernel module is built by DKMS for every installed kernel.
@@ -83,6 +83,27 @@ Keyboard RGB and performance profiles work without the module.
Uninstall with `sudo pacman -R predator-control` (package) or `sudo ./uninstall.sh` (add `--purge` to also delete your settings).
## Power settings
The **Power** page controls when the lighting switches off: the firmware's 30-second backlight
timeout can be disabled, a custom idle timeout can be set separately for AC and battery, and on
battery the keyboard can be dimmed or turned off and the lightbar disabled.
## Predator key
Pressing the **Predator key** opens Predator Control, just like PredatorSense on Windows.
On the **System** page you can instead make it switch to the next performance mode, or disable it.
## Updates
Installed via the pacman package, Predator Control updates itself: the service checks for new
releases every 6 hours, and a notice with an *Install* button appears on the **System** page, in the
sidebar and in the panel widget. Downloads are verified with the SHA-256 from the release notes and
installed with `pacman -U`. From the terminal: `predatorctl update` (or `--check`).
Automatic checks can be turned off on the System page.
If you installed with `install.sh`, update with `git pull && sudo ./install.sh`.
## Usage
### Panel widget
@@ -108,6 +129,7 @@ predatorctl kbd-brightness +25
predatorctl lightbar wave --color 00d4ff --speed 7
predatorctl lightbar --sync on # lightbar follows the keyboard
predatorctl battery-limit on
predatorctl update # check for and install updates
predatorctl open rgb # open the app on a page
```
Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

+1 -1
View File
@@ -4,7 +4,7 @@
# sudo ./install.sh --no-deps skip package installation (non-Arch distros)
set -euo pipefail
VERSION="1.1.0"
VERSION="1.4.0"
MODULE="predator-wmi"
SRC="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PREFIX="/usr"
+1 -1
View File
@@ -1,5 +1,5 @@
PACKAGE_NAME="predator-wmi"
PACKAGE_VERSION="1.1.0"
PACKAGE_VERSION="1.4.0"
BUILT_MODULE_NAME[0]="predator_wmi"
DEST_MODULE_LOCATION[0]="/kernel/drivers/platform/x86"
MAKE[0]="make KVER=${kernelver} KDIR=${kernel_source_dir}"
+2 -2
View File
@@ -5,7 +5,7 @@
# sudo pacman -U predator-control-*.pkg.tar.zst
pkgname=predator-control
pkgver=1.1.0
pkgver=1.4.0
pkgrel=1
pkgdesc="PredatorSense replacement for Acer Predator laptops: performance modes, fans, keyboard RGB, lightbar, battery (Qt app + Plasma widget)"
arch=('any')
@@ -22,7 +22,7 @@ install=predator-control.install
source=()
_src="$(realpath "$startdir/../..")"
_modver=1.1.0
_modver=1.4.0
package() {
cd "$_src"
+72
View File
@@ -0,0 +1,72 @@
#!/usr/bin/env python3
"""Build the pacman package and publish a Gitea release for the current version.
Usage: GITEA_TOKEN=... packaging/release.py
Requires a clean working tree and the tag v<version> already pushed.
The release notes are taken from CHANGELOG.md and contain the package's SHA-256,
which the built-in updater uses to verify downloads.
"""
import hashlib
import json
import os
import re
import subprocess
import sys
import urllib.request
ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
API = "https://git.mrblake.cc/api/v1/repos/MrBlake/Cachy-Predator-Keyboard"
def api(method, path, data=None, ctype="application/json"):
headers = {"Authorization": "token " + os.environ["GITEA_TOKEN"]}
if data is not None:
headers["Content-Type"] = ctype
req = urllib.request.Request(API + path, data=data, method=method, headers=headers)
return json.load(urllib.request.urlopen(req))
def main():
if "GITEA_TOKEN" not in os.environ:
sys.exit("set GITEA_TOKEN")
version = re.search(r'__version__ = "([^"]+)"', open(os.path.join(ROOT, "predator_control/__init__.py")).read())[1]
pkgbuild = open(os.path.join(ROOT, "packaging/arch/PKGBUILD")).read()
if "pkgver=%s\n" % version not in pkgbuild:
sys.exit("PKGBUILD pkgver does not match version %s" % version)
pkgrel = re.search(r"^pkgrel=(\d+)", pkgbuild, re.M)[1]
arch_dir = os.path.join(ROOT, "packaging/arch")
subprocess.run(["makepkg", "-f", "--cleanbuild"], cwd=arch_dir, check=True)
pkg = "predator-control-%s-%s-any.pkg.tar.zst" % (version, pkgrel)
data = open(os.path.join(arch_dir, pkg), "rb").read()
sha = hashlib.sha256(data).hexdigest()
changelog = open(os.path.join(ROOT, "CHANGELOG.md")).read()
notes = changelog.split("## %s" % version, 1)[1].split("\n## ", 1)[0].split("\n", 1)[1].strip()
body = notes + f"""
### Install (Arch / CachyOS / Manjaro)
```bash
sudo pacman -S --needed linux-cachyos-headers clang lld llvm # stock Arch kernel: linux-headers
sudo pacman -U {pkg}
```
Already installed via pacman? Use the built-in updater (System page, panel widget or `predatorctl update`).
SHA-256 `{pkg}`: `{sha}`
Not affiliated with Acer Inc."""
rel = api("POST", "/releases", json.dumps({
"tag_name": "v" + version, "name": "Predator Control " + version,
"body": body, "draft": False, "prerelease": False}).encode())
boundary = "----predator" + sha[:16]
mp = (f'--{boundary}\r\nContent-Disposition: form-data; name="attachment"; filename="{pkg}"\r\n'
f"Content-Type: application/octet-stream\r\n\r\n").encode() + data + f"\r\n--{boundary}--\r\n".encode()
api("POST", "/releases/%d/assets?name=%s" % (rel["id"], pkg), mp, "multipart/form-data; boundary=" + boundary)
print("Released", rel["html_url"])
print("SHA-256", sha)
if __name__ == "__main__":
main()
@@ -72,6 +72,7 @@ PlasmoidItem {
? profileLabel(st.profile) + " · CPU " + temp(st.sensors.cpu_temp) + " · GPU " + temp(st.sensors.gpu_temp)
+ "\nFans " + (st.sensors.cpu_fan || 0) + " / " + (st.sensors.gpu_fan || 0) + " RPM"
+ "\nBattery " + batteryText()
+ (st.update && st.update.available ? "\nUpdate " + st.update.latest + " available" : "")
: "Service predatord not running"
P5Support.DataSource {
@@ -352,6 +353,27 @@ PlasmoidItem {
}
}
Kirigami.InlineMessage {
Layout.fillWidth: true
visible: root.ok && root.st.update !== null && root.st.update !== undefined && root.st.update.available
type: Kirigami.MessageType.Positive
text: root.ok && root.st.update ? (root.st.update_status === "running"
? "Installing update " + root.st.update.latest + "…"
: "Update " + root.st.update.latest + " available") : ""
actions: [
Kirigami.Action {
text: "Install"
icon.name: "update-none"
visible: root.ok && root.st.update && root.st.update.can_install && root.st.update_status !== "running"
onTriggered: root.run("update --yes")
},
Kirigami.Action {
text: "Details"
onTriggered: root.openPage("system")
}
]
}
PC3.Label {
Layout.fillWidth: true
visible: root.lastError !== ""
@@ -12,7 +12,7 @@
"Id": "org.predatorcontrol.panel",
"License": "GPL-2.0-only",
"Name": "Predator Control",
"Version": "1.1.0",
"Version": "1.4.0",
"Website": "https://git.mrblake.cc/MrBlake/Cachy-Predator-Keyboard"
},
"X-Plasma-API-Minimum-Version": "6.0",
+1 -1
View File
@@ -2,7 +2,7 @@
import os
__version__ = "1.1.0"
__version__ = "1.4.0"
APP_NAME = "Predator Control"
RUN_DIR = os.environ.get("PREDATOR_CONTROL_RUN_DIR", "/run/predator-control")
+19
View File
@@ -61,6 +61,10 @@ def main(argv=None):
sp = sub.add_parser("battery-limit", help="80 %% charge limit")
sp.add_argument("value", choices=["on", "off"])
sp = sub.add_parser("update", help="check for and install updates")
sp.add_argument("--check", action="store_true", help="only check, do not install")
sp.add_argument("--yes", "-y", action="store_true", help="install without asking")
sp = sub.add_parser("open", help="open the main window")
sp.add_argument("page", nargs="?", default="home")
@@ -139,6 +143,21 @@ def main(argv=None):
if a.sync:
args["sync_keyboard"] = a.sync == "on"
c.call("set_lightbar", **args)
elif a.cmd == "update":
u = c.call("check_update")
if not u["available"]:
print("Predator Control %s is up to date." % u["current"])
return 0
print("Update available: %s -> %s\n%s" % (u["current"], u["latest"], u["url"]))
if a.check:
return 0
if not u["can_install"]:
print("Automatic install needs the pacman package. Update with: git pull && sudo ./install.sh")
return 1
if not a.yes and sys.stdin.isatty() and input("Install now? [Y/n] ").strip().lower() not in ("", "y", "yes"):
return 0
c.call("install_update")
print("Installing %s in the background (journalctl -fu predator-control-update)." % u["asset"])
elif a.cmd == "battery-limit":
c.call("set_battery_limit", enabled=_bool(a.value))
except DaemonError as e:
+133 -2
View File
@@ -24,6 +24,9 @@ import traceback
from . import CONFIG_DIR, CONFIG_PATH, SOCKET_PATH, STATE_PATH, __version__
from . import hardware as hw
from . import updater
from .hotkey import HotkeyListener, launch_in_session
from .idle import IdleWatcher
from .keyboard import EFFECTS, NUM_CELLS, Keyboard
DEFAULT_CONFIG = {
@@ -47,6 +50,15 @@ DEFAULT_CONFIG = {
},
"battery": {"limit": None}, # None = leave the firmware state alone
"keyboard_presets": {},
"updates": {"auto_check": True, "interval_hours": 6},
"predator_key": "open", # open | cycle_profile | none
"power": {
"idle_off_ac": 0, # seconds without input until lighting turns off, 0 = never
"idle_off_battery": 120,
"battery_brightness": 100, # keyboard brightness on battery, % of the normal value
"battery_lights_off": False,
"battery_lightbar_off": False,
},
}
@@ -93,6 +105,12 @@ class Daemon:
self.state = {}
self.battery_limit = None
self._ticks = 0
self.update_info = None
self.update_error = None
self._update_checking = False
self.idle = IdleWatcher(self._activity)
self.lights_idle = False
self.on_ac = True
# ---------------------------------------------------------- Configuration
def _load_config(self):
@@ -125,8 +143,35 @@ class Daemon:
self.errors[key] = str(e)
return None
# ---------------------------------------------------------- Power rules
def keyboard_effective(self):
k = dict(self.config["keyboard"])
p = self.config["power"]
if self.lights_idle or (not self.on_ac and p.get("battery_lights_off")):
k["effect"] = "off"
elif not self.on_ac and p.get("battery_brightness", 100) < 100:
k["brightness"] = int(k["brightness"] * p["battery_brightness"] / 100)
return k
def idle_timeout(self):
p = self.config["power"]
return int(p.get("idle_off_ac" if self.on_ac else "idle_off_battery") or 0)
def update_idle(self):
t = self.idle_timeout()
idle = t > 0 and self.idle.idle_seconds() >= t
if idle != self.lights_idle:
self.lights_idle = idle
log("Lighting %s" % ("off (idle)" if idle else "restored"))
self.apply_keyboard()
def _activity(self):
if self.lights_idle:
with self.lock:
self.update_idle()
def apply_keyboard(self, cfg=None):
k = cfg or self.config["keyboard"]
k = cfg or self.keyboard_effective()
frame = bytes.fromhex(k.get("frame") or "") if k["effect"] == "perkey" else None
self._try("keyboard", self.kbd.apply, k["effect"], hex_to_rgb(k["color"]),
k["brightness"], frame, int(k.get("effect_id") or 0))
@@ -135,8 +180,11 @@ class Daemon:
def lightbar_effective(self):
lb = dict(self.config["lightbar"])
if self.lights_idle or (not self.on_ac and self.config["power"].get("battery_lightbar_off")):
lb["mode"] = "off"
return lb
if lb.get("sync_keyboard"):
k = self.config["keyboard"]
k = self.keyboard_effective()
lb["brightness"] = k["brightness"]
if k["effect"] == "off":
lb["mode"] = "off"
@@ -172,6 +220,9 @@ class Daemon:
def restore(self):
log("Restoring settings")
bat = hw.battery()
self.on_ac = bat.get("ac_online", True)
self.idle.last_input = time.monotonic() # e.g. after resume: lights on
self.lights_idle = False
if self.config["profile"].get("auto_switch"):
self.apply_profile_for_power(bat.get("ac_online", True))
self.apply_fans()
@@ -230,6 +281,10 @@ class Daemon:
"lightbar_effective": self.lightbar_effective(),
"fan_curve_applied": self.curve_applied,
"battery_limit": self.battery_limit,
"lights_idle": self.lights_idle,
"update": self.update_info,
"update_error": self.update_error,
"update_status": updater.status(),
"available": {
"keyboard": self.kbd_present,
"wmi": hw.wmi_available(hw.GUID_GAMING),
@@ -241,6 +296,46 @@ class Daemon:
}
return st
def check_update_async(self):
if self._update_checking:
return
self._update_checking = True
def work():
try:
info = updater.check()
if info["available"] and (self.update_info or {}).get("latest") != info["latest"]:
log("Update available: %s -> %s" % (info["current"], info["latest"]))
self.update_info, self.update_error = info, None
except Exception as e:
self.update_error = str(e)
finally:
self._update_checking = False
threading.Thread(target=work, daemon=True).start()
def maybe_check_update(self):
u = self.config.get("updates", {})
if not u.get("auto_check", True):
return
last = (self.update_info or {}).get("checked", 0)
if time.time() - last > float(u.get("interval_hours", 6)) * 3600:
if self.update_error and time.time() - getattr(self, "_last_try", 0) < 900:
return # offline: retry every 15 min
self._last_try = time.time()
self.check_update_async()
def predator_key(self):
action = self.config.get("predator_key", "open")
if action == "open":
if not launch_in_session(["predator-control"]):
log("Predator key: no active graphical session to open the app in")
elif action == "cycle_profile":
with self.lock:
ch = hw.profile_choices()
cur = hw.get_profile()
nxt = ch[(ch.index(cur) + 1) % len(ch)] if cur in ch else ch[0]
self._try("profile", hw.set_profile, nxt)
def loop(self):
while True:
try:
@@ -258,9 +353,15 @@ class Daemon:
log("Power source changed:", "AC" if ac else "battery")
if self.config["profile"].get("auto_switch"):
self.apply_profile_for_power(ac)
if ac != self.on_ac:
self.on_ac = ac
self.apply_keyboard() # battery lighting rules
self.last_ac = ac
self.update_idle()
self.fan_curve_tick()
self._ticks += 1
if self._ticks % 20 == 1:
self.maybe_check_update()
if self._ticks % 40 == 0:
self.refresh_battery_limit()
self.state = self.build_state()
@@ -279,6 +380,34 @@ class Daemon:
if cmd == "state":
self.state = self.build_state()
return self.state
if cmd == "check_update":
self.update_info, self.update_error = updater.check(), None
return self.update_info
if cmd == "install_update":
info = updater.check()
self.update_info = info
return updater.install(info)
if cmd == "set_updates":
merge(self.config["updates"], {k: a[k] for k in ("auto_check", "interval_hours") if k in a})
self.save_config()
return {}
if cmd == "set_power":
p = self.config["power"]
for key in ("idle_off_ac", "idle_off_battery", "battery_brightness",
"battery_lights_off", "battery_lightbar_off"):
if key in a:
p[key] = a[key]
p["battery_brightness"] = max(0, min(100, int(p["battery_brightness"])))
self.save_config()
self.update_idle()
self.apply_keyboard()
return {}
if cmd == "set_predator_key":
if a["action"] not in ("open", "cycle_profile", "none"):
raise ValueError("unknown action")
self.config["predator_key"] = a["action"]
self.save_config()
return {}
if cmd == "restore":
self.restore()
return {}
@@ -462,6 +591,8 @@ def main():
signal.signal(signal.SIGTERM, lambda *_: (server.server_close(), sys.exit(0)))
signal.signal(signal.SIGUSR1, lambda *_: threading.Thread(target=lambda: (d.lock.acquire(), d.restore(), d.lock.release())).start())
threading.Thread(target=d.loop, daemon=True).start()
HotkeyListener(d.predator_key, log).start()
d.idle.start()
server.serve_forever()
+169 -4
View File
@@ -4,9 +4,10 @@ import argparse
import os
import sys
import threading
import time
from PySide6.QtCore import QObject, Qt, QTimer, Signal
from PySide6.QtGui import QColor, QIcon
from PySide6.QtGui import QColor, QIcon, QPalette
from PySide6.QtNetwork import QLocalServer, QLocalSocket
from PySide6.QtWidgets import (QApplication, QButtonGroup, QCheckBox, QComboBox, QFrame,
QGridLayout, QHBoxLayout, QInputDialog, QLabel, QListWidget,
@@ -25,6 +26,7 @@ PAGES = [
("fans", "Fans"),
("rgb", "RGB Studio"),
("battery", "Battery"),
("power", "Power"),
("system", "System"),
]
@@ -590,6 +592,92 @@ class RgbPage(Page):
self.lb_speed.setValue(lb["speed"])
IDLE_CHOICES = [(0, "Never"), (30, "30 seconds"), (60, "1 minute"), (120, "2 minutes"), (300, "5 minutes"),
(600, "10 minutes"), (900, "15 minutes"), (1800, "30 minutes")]
class PowerPage(Page):
def __init__(self, win):
super().__init__(win)
self.lay.addWidget(_title("Power", "Energy settings for the keyboard lighting and lightbar"))
fw = Card("Firmware backlight timeout")
self.fw_timeout = QCheckBox("Turn the keyboard backlight off after 30 s without input (firmware)")
self.fw_timeout.clicked.connect(lambda v: (win.backend.run("set_firmware", name="kbd_timeout", value=bool(v)),
QTimer.singleShot(800, lambda: win.backend.run("get_firmware"))))
fw.body.addWidget(self.fw_timeout)
fw.body.addWidget(_muted("This is the built-in automatic switch-off of the laptop. Turn it off to keep the "
"lighting on permanently, or use the adjustable timeout below instead."))
self.lay.addWidget(fw)
idle = Card("Turn lighting off when idle")
g = QGridLayout()
self.idle_ac = QComboBox()
self.idle_bat = QComboBox()
for combo, key, label in ((self.idle_ac, "idle_off_ac", "On AC power"),
(self.idle_bat, "idle_off_battery", "On battery")):
for val, text in IDLE_CHOICES:
combo.addItem(text, val)
combo.activated.connect(lambda _=0, c=combo, k=key: win.backend.run("set_power", **{k: c.currentData()}))
g.addWidget(QLabel("On AC power"), 0, 0)
g.addWidget(self.idle_ac, 0, 1)
g.addWidget(QLabel("On battery"), 1, 0)
g.addWidget(self.idle_bat, 1, 1)
g.setColumnStretch(2, 1)
idle.body.addLayout(g)
idle.body.addWidget(_muted("Keyboard and lightbar turn off after this time without keyboard, touchpad or "
"mouse input, and come back on with the next input."))
self.idle_state = _muted("")
idle.body.addWidget(self.idle_state)
self.lay.addWidget(idle)
bat = Card("On battery")
h = QHBoxLayout()
h.addWidget(QLabel("Keyboard brightness"))
self.bat_bright = _slider(10, 100, 100)
self.bat_bright.setSingleStep(10)
self.bat_bright_l = QLabel("100 %")
self.bat_bright.valueChanged.connect(lambda v: self.bat_bright_l.setText("%d %%" % v))
self.bat_bright.sliderReleased.connect(lambda: win.backend.run("set_power", battery_brightness=self.bat_bright.value()))
h.addWidget(self.bat_bright, 1)
h.addWidget(self.bat_bright_l)
bat.body.addLayout(h)
bat.body.addWidget(_muted("Percentage of your normal keyboard brightness while running on battery."))
self.bat_off = QCheckBox("Turn the keyboard lighting off completely on battery")
self.bat_off.clicked.connect(lambda v: win.backend.run("set_power", battery_lights_off=bool(v)))
bat.body.addWidget(self.bat_off)
self.bat_lb = QCheckBox("Turn the lightbar off on battery")
self.bat_lb.clicked.connect(lambda v: win.backend.run("set_power", battery_lightbar_off=bool(v)))
bat.body.addWidget(self.bat_lb)
self.lay.addWidget(bat)
self.lay.addStretch(1)
def showEvent(self, e):
super().showEvent(e)
self.win.backend.run("get_firmware")
def firmware(self, fw):
v = fw.get("kbd_timeout", {})
self.fw_timeout.setEnabled(v.get("value") is not None)
self.fw_timeout.setChecked(bool(v.get("value")))
self.fw_timeout.setToolTip(v.get("error", "") or "")
def update_state(self, st):
p = st["config"].get("power", {})
for combo, key in ((self.idle_ac, "idle_off_ac"), (self.idle_bat, "idle_off_battery")):
if not combo.view().isVisible():
i = combo.findData(p.get(key, 0))
if i < 0:
combo.addItem("%d seconds" % p[key], p[key])
i = combo.count() - 1
combo.setCurrentIndex(i)
if not self.bat_bright.isSliderDown():
self.bat_bright.setValue(p.get("battery_brightness", 100))
self.bat_off.setChecked(bool(p.get("battery_lights_off")))
self.bat_lb.setChecked(bool(p.get("battery_lightbar_off")))
self.idle_state.setText("Lighting is currently off because the laptop is idle." if st.get("lights_idle") else "")
class BatteryPage(Page):
def __init__(self, win):
super().__init__(win)
@@ -673,6 +761,42 @@ class SystemPage(Page):
self.fw_card.body.addWidget(_muted("Values are stored in the laptop firmware (EC/BIOS)."))
self.lay.addWidget(self.fw_card)
key = Card("Predator key")
kr = QHBoxLayout()
kr.addWidget(QLabel("When the Predator key is pressed"))
self.key_action = QComboBox()
for val, text in (("open", "Open Predator Control"), ("cycle_profile", "Switch to the next performance mode"),
("none", "Do nothing")):
self.key_action.addItem(text, val)
self.key_action.activated.connect(lambda: win.backend.run("set_predator_key", action=self.key_action.currentData()))
kr.addWidget(self.key_action)
kr.addStretch(1)
key.body.addLayout(kr)
self.lay.addWidget(key)
upd = Card("Updates")
self.upd_head = QLabel("")
self.upd_head.setObjectName("big")
upd.body.addWidget(self.upd_head)
self.upd_text = _muted("")
self.upd_text.setTextFormat(Qt.MarkdownText)
self.upd_text.setOpenExternalLinks(True)
upd.body.addWidget(self.upd_text)
h = QHBoxLayout()
self.upd_check = QPushButton("Check now")
self.upd_check.clicked.connect(lambda: (self.upd_check.setEnabled(False), win.backend.run("check_update")))
h.addWidget(self.upd_check)
self.upd_install = QPushButton("Install update")
self.upd_install.setObjectName("primary")
self.upd_install.clicked.connect(self._install)
h.addWidget(self.upd_install)
self.upd_auto = QCheckBox("Check for updates automatically")
self.upd_auto.clicked.connect(lambda v: win.backend.run("set_updates", auto_check=bool(v)))
h.addWidget(self.upd_auto)
h.addStretch(1)
upd.body.addLayout(h)
self.lay.addWidget(upd)
svc = Card("Status")
self.status = _muted("")
svc.body.addWidget(self.status)
@@ -698,7 +822,7 @@ class SystemPage(Page):
def firmware(self, fw):
for name, info in fw.items():
if "label" not in info:
if "label" not in info or name == "kbd_timeout": # shown on the Power page
continue
if name == "usb_charging":
w = self.fw_boxes.get(name)
@@ -728,7 +852,39 @@ class SystemPage(Page):
box.setChecked(bool(info.get("value")))
box.setToolTip(info.get("error", "") or "")
def _install(self):
u = self.win.state.get("update") or {}
if QMessageBox.question(self, "Install update",
"Install Predator Control %s now?\n\nThe background service restarts briefly; "
"reopen the app afterwards." % u.get("latest")) == QMessageBox.Yes:
self.upd_install.setEnabled(False)
self.win.backend.run("install_update")
def update_state(self, st):
u = st.get("update") or {}
status = st.get("update_status")
self.upd_check.setEnabled(True)
if status == "running":
self.upd_head.setText("Installing update…")
self.upd_text.setText("pacman is installing the new version.")
self.upd_install.hide()
elif u.get("available"):
self.upd_head.setText("Version %s available" % u["latest"])
text = "Installed: %s · [Release page](%s)\n\n%s" % (u["current"], u.get("url"), u.get("notes", "").split("### Install")[0].strip())
if not u.get("can_install"):
text += "\n\n*Automatic install needs the pacman package – otherwise run `git pull && sudo ./install.sh`.*"
self.upd_text.setText(text)
self.upd_install.setVisible(bool(u.get("can_install")))
else:
self.upd_head.setText("Up to date" if u else "Not checked yet")
when = time.strftime("%d.%m.%Y %H:%M", time.localtime(u["checked"])) if u.get("checked") else "–"
extra = "\n\nLast update failed – see: journalctl -u predator-control-update" if status == "failed" else ""
err = ("\n\n⚠ %s" % st["update_error"]) if st.get("update_error") else ""
self.upd_text.setText("Installed version %s · last check %s%s%s" % (st.get("version"), when, err, extra))
self.upd_install.hide()
if not self.key_action.view().isVisible():
self.key_action.setCurrentIndex(max(0, self.key_action.findData(st["config"].get("predator_key", "open"))))
self.upd_auto.setChecked(st["config"].get("updates", {}).get("auto_check", True))
a = st["available"]
yes = lambda v: "✔" if v else "✘"
lines = ["%s Service predatord %s" % (yes(True), st.get("version")),
@@ -774,7 +930,8 @@ class MainWindow(QMainWindow):
self.nav = QButtonGroup(self)
self.pages = {}
classes = {"home": HomePage, "performance": PerformancePage, "fans": FansPage,
"rgb": RgbPage, "battery": BatteryPage, "system": SystemPage}
"rgb": RgbPage, "battery": BatteryPage, "power": PowerPage,
"system": SystemPage}
for key, label in PAGES:
b = QPushButton(label)
b.setObjectName("nav")
@@ -833,16 +990,21 @@ class MainWindow(QMainWindow):
if self.stack.currentWidget() is not bat_page:
bat_page.indicator.set_battery(st["battery"]) # keep the power history going
s = st["sensors"]
self.footer.setText("%s\nCPU %s °C · GPU %s" % (
u = st.get("update") or {}
badge = ("⬆ Update %s available\n" % u["latest"]) if u.get("available") else ""
self.footer.setText(badge + "%s\nCPU %s °C · GPU %s" % (
PROFILE_INFO.get(st["profile"], (st["profile"],))[0], s.get("cpu_temp"),
("%s °C" % s["gpu_temp"]) if s.get("gpu_temp") else "off"))
def _done(self, cmd, result, err):
if err:
self.statusBar().showMessage("Error: %s" % err, 8000)
elif cmd == "install_update":
self.statusBar().showMessage("Update is being installed…", 8000)
elif cmd == "get_firmware":
self.pages["system"][0].firmware(result)
self.pages["battery"][0].firmware(result)
self.pages["power"][0].firmware(result)
return
else:
self.statusBar().showMessage("Applied", 2000)
@@ -860,6 +1022,9 @@ def main(argv=None):
app.setDesktopFileName("predator-control")
app.setStyle("Fusion")
app.setStyleSheet(theme.QSS)
pal = app.palette()
pal.setColor(QPalette.Link, QColor(theme.ACCENT))
app.setPalette(pal)
# single instance: forward the requested page to a running window
name = "predator-control-%d" % os.getuid()
+83
View File
@@ -0,0 +1,83 @@
"""Predator key listener.
The PH16-71 Predator key produces no input event: its keyboard usage (0xC9) is outside the
range the keyboard declares, so the kernel drops it. The key is also reported on the
vendor report 4 of USB interface 2: `04 81 ff` on press, `04 00 00` on release.
"""
import os
import pwd
import select
import subprocess
import threading
import time
from .keyboard import HOTKEY_INTERFACE, find_hidraw
PRESS_PREFIX = b"\x04\x81"
def active_session_user():
"""(uid, name) of the active graphical session, or None."""
out = subprocess.run(["loginctl", "list-sessions", "--no-legend"],
capture_output=True, text=True).stdout
for line in out.splitlines():
sid = line.split()[0] if line.split() else None
if not sid:
continue
props = dict(l.split("=", 1) for l in subprocess.run(
["loginctl", "show-session", sid, "-p", "Active", "-p", "Type", "-p", "User", "-p", "Name"],
capture_output=True, text=True).stdout.splitlines() if "=" in l)
if props.get("Active") == "yes" and props.get("Type") in ("wayland", "x11"):
return int(props["User"]), props["Name"]
return None
def launch_in_session(args):
sess = active_session_user()
if not sess:
return False
uid, name = sess
# runs in the user's systemd manager, which carries WAYLAND_DISPLAY/DISPLAY on Plasma
r = subprocess.run(["systemd-run", "--quiet", "--user", "--machine=%s@.host" % name, "--collect",
"--unit=predator-control-launch-%d" % int(time.time() * 1000)] + args,
capture_output=True, text=True)
return r.returncode == 0
class HotkeyListener(threading.Thread):
def __init__(self, on_press, log):
super().__init__(daemon=True)
self.on_press = on_press
self.log = log
def run(self):
while True:
path = find_hidraw(HOTKEY_INTERFACE)
if not path:
time.sleep(5)
continue
try:
fd = os.open(path, os.O_RDONLY)
except OSError as e:
self.log("hotkey: cannot open %s: %s" % (path, e))
time.sleep(5)
continue
last = 0.0
try:
while True:
select.select([fd], [], [])
data = os.read(fd, 64)
if not data:
break
if data.startswith(PRESS_PREFIX) and time.monotonic() - last > 0.4:
last = time.monotonic()
try:
self.on_press()
except Exception as e:
self.log("hotkey: %s" % e)
except OSError:
pass # device vanished (suspend/replug) – reopen
finally:
os.close(fd)
time.sleep(1)
+56
View File
@@ -0,0 +1,56 @@
"""Input activity watcher used for the 'turn lighting off when idle' feature."""
import glob
import os
import select
import threading
import time
class IdleWatcher(threading.Thread):
"""Tracks the time of the last keyboard/touchpad/mouse event on any input device."""
RESCAN = 30 # seconds between scans for new devices (hotplug, resume)
def __init__(self, on_activity=None):
super().__init__(daemon=True)
self.last_input = time.monotonic()
self.on_activity = on_activity
def idle_seconds(self):
return time.monotonic() - self.last_input
def _open_all(self, fds):
for path in glob.glob("/dev/input/event*"):
if path in fds.values():
continue
try:
fds[os.open(path, os.O_RDONLY | os.O_NONBLOCK)] = path
except OSError:
pass
def run(self):
fds = {}
next_scan = 0.0
while True:
if time.monotonic() >= next_scan:
self._open_all(fds)
next_scan = time.monotonic() + self.RESCAN
if not fds:
time.sleep(5)
continue
ready, _, _ = select.select(list(fds), [], [], 5)
active = False
for fd in ready:
try:
if os.read(fd, 4096):
active = True
except BlockingIOError:
pass
except OSError:
os.close(fd)
fds.pop(fd, None)
if active:
self.last_input = time.monotonic()
if self.on_activity:
self.on_activity()
+3 -2
View File
@@ -13,6 +13,7 @@ import threading
ACER_VID = 0x04F2
KNOWN_PIDS = (0x0117, 0x011A)
LED_INTERFACE = 3
HOTKEY_INTERFACE = 2 # vendor report 4 carries the Predator key
NUM_CELLS = 128
OP_BEGIN = 0x88
@@ -57,7 +58,7 @@ def checksum(body7):
return (0xFF - (sum(body7) & 0xFF)) & 0xFF
def find_hidraw():
def find_hidraw(interface=LED_INTERFACE):
"""Path of the hidraw node of the LED interface, or None."""
for node in sorted(glob.glob("/sys/class/hidraw/hidraw*")):
try:
@@ -76,7 +77,7 @@ def find_hidraw():
devpath = os.path.realpath(os.path.join(node, "device"))
# .../1-7:1.3/0003:04F2:011A.0004 -> interface number from the parent
iface = os.path.basename(os.path.dirname(devpath)).rsplit(".", 1)[-1]
if iface == str(LED_INTERFACE):
if iface == str(interface):
return "/dev/" + os.path.basename(node)
return None
+123
View File
@@ -0,0 +1,123 @@
"""Self-updater: checks the Gitea releases for a newer pacman package and installs it.
The release must carry a `predator-control-<ver>-<rel>-any.pkg.tar.zst` asset and its
SHA-256 in the release notes. Downloads are verified before `pacman -U` runs.
An optional API token (for private repositories) is read from
/etc/predator-control/update-token.
"""
import hashlib
import json
import os
import re
import shutil
import subprocess
import time
import urllib.request
from . import CONFIG_DIR, __version__
API = "https://git.mrblake.cc/api/v1/repos/MrBlake/Cachy-Predator-Keyboard"
CACHE_DIR = "/var/cache/predator-control"
TOKEN_PATH = CONFIG_DIR + "/update-token"
PKG_RE = re.compile(r"^predator-control-[\w.+]+-\d+-any\.pkg\.tar\.zst$")
UNIT = "predator-control-update"
class UpdateError(RuntimeError):
pass
def parse_version(v):
return tuple(int(x) for x in re.findall(r"\d+", v.lstrip("v"))[:3])
def _request(url, binary=False):
headers = {"Accept": "application/json", "User-Agent": "predator-control/" + __version__}
try:
with open(TOKEN_PATH) as f:
token = f.read().strip()
if token:
headers["Authorization"] = "token " + token
except OSError:
pass
try:
with urllib.request.urlopen(urllib.request.Request(url, headers=headers), timeout=30) as r:
data = r.read()
except OSError as e:
raise UpdateError("cannot reach the update server: %s" % e) from e
return data if binary else json.loads(data)
def installed_as_package():
"""True if the app was installed through pacman (only then can it update itself)."""
if not shutil.which("pacman"):
return False
return subprocess.run(["pacman", "-Qq", "predator-control"], capture_output=True).returncode == 0
def check():
"""Return information about the latest release."""
releases = [r for r in _request(API + "/releases?limit=50")
if not r.get("draft") and not r.get("prerelease") and parse_version(r.get("tag_name", ""))]
if not releases:
raise UpdateError("no releases found")
rel = max(releases, key=lambda r: parse_version(r["tag_name"]))
latest = rel.get("tag_name", "").lstrip("v")
asset = next((a for a in rel.get("assets", []) if PKG_RE.match(a.get("name", ""))), None)
sha = None
if asset:
m = re.search(re.escape(asset["name"]) + r"\W+([0-9a-f]{64})", rel.get("body", ""))
sha = m.group(1) if m else None
return {
"current": __version__,
"latest": latest,
"available": parse_version(latest) > parse_version(__version__),
"notes": rel.get("body", ""),
"url": rel.get("html_url"),
"asset": asset["name"] if asset else None,
"asset_url": asset["browser_download_url"] if asset else None,
"sha256": sha,
"can_install": installed_as_package() and bool(asset and sha),
"checked": time.time(),
}
def install(info):
"""Download, verify and install the package in a separate systemd unit.
pacman's post_upgrade restarts predatord, so the installation must not run
inside the daemon's own process.
"""
if not info.get("available"):
raise UpdateError("already up to date")
if not installed_as_package():
raise UpdateError("not installed via pacman – update with git pull && sudo ./install.sh")
if not info.get("asset_url") or not info.get("sha256"):
raise UpdateError("release has no verifiable package")
os.makedirs(CACHE_DIR, exist_ok=True)
path = os.path.join(CACHE_DIR, info["asset"])
data = _request(info["asset_url"], binary=True)
digest = hashlib.sha256(data).hexdigest()
if digest != info["sha256"]:
raise UpdateError("checksum mismatch – download rejected")
with open(path, "wb") as f:
f.write(data)
subprocess.run(["systemctl", "reset-failed", UNIT + ".service"], capture_output=True)
r = subprocess.run(["systemd-run", "--unit=" + UNIT, "--description=Predator Control update",
"pacman", "-U", "--noconfirm", "--needed", path],
capture_output=True, text=True)
if r.returncode != 0:
raise UpdateError("could not start the update: %s" % r.stderr.strip())
return {"started": True, "package": info["asset"]}
def status():
"""State of the installer unit: 'running', 'failed' or 'idle'."""
r = subprocess.run(["systemctl", "is-active", UNIT + ".service"], capture_output=True, text=True)
s = r.stdout.strip()
if s in ("active", "activating"):
return "running"
if s == "failed":
return "failed"
return "idle"