K65 Studio: Linux control app for the Corsair K65 PLUS Wireless
Bragi protocol over hidraw, device service with heartbeat and notifications, PySide6 UI with animated preview, profiles, tray, autostart, udev rule and installer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
commit
7e276912da
23 files changed
+2631
No files matched your search
@@ -0,0 +1,217 @@
|
||||
"""Animated keyboard preview that approximates the hardware effects."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import colorsys
|
||||
import math
|
||||
import random
|
||||
import time
|
||||
|
||||
from PySide6.QtCore import QRectF, Qt, QTimer
|
||||
from PySide6.QtGui import QColor, QFont, QPainter, QPainterPath, QPen
|
||||
from PySide6.QtWidgets import QWidget
|
||||
|
||||
from .. import layout
|
||||
from ..profiles import Profile
|
||||
|
||||
SPEED_FACTOR = {"slow": 0.5, "medium": 1.0, "fast": 1.8}
|
||||
RGB = tuple[float, float, float]
|
||||
|
||||
|
||||
def _hex(c: str) -> RGB:
|
||||
q = QColor(c)
|
||||
return q.redF(), q.greenF(), q.blueF()
|
||||
|
||||
|
||||
def _hue(h: float) -> RGB:
|
||||
return colorsys.hsv_to_rgb(h % 1.0, 1.0, 1.0)
|
||||
|
||||
|
||||
def _mix(a: RGB, b: RGB, t: float) -> RGB:
|
||||
return tuple(x + (y - x) * t for x, y in zip(a, b))
|
||||
|
||||
|
||||
def _scale(a: RGB, f: float) -> RGB:
|
||||
return tuple(x * f for x in a)
|
||||
|
||||
|
||||
class KeyboardPreview(QWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.setMinimumHeight(220)
|
||||
self.profile = Profile()
|
||||
self.connected = True
|
||||
self._t0 = time.monotonic()
|
||||
self._presses: list[tuple[float, layout.Key, RGB]] = []
|
||||
self._next_press = 0.0
|
||||
self._rand = random.Random(7)
|
||||
self._palette_seed = [self._rand.random() for _ in range(64)]
|
||||
self._timer = QTimer(self, interval=33, timeout=self.update)
|
||||
self._timer.start()
|
||||
|
||||
def set_profile(self, profile: Profile) -> None:
|
||||
self.profile = profile
|
||||
self.update()
|
||||
|
||||
def set_connected(self, connected: bool) -> None:
|
||||
self.connected = connected
|
||||
self.update()
|
||||
|
||||
def showEvent(self, e):
|
||||
self._timer.start()
|
||||
super().showEvent(e)
|
||||
|
||||
def hideEvent(self, e):
|
||||
self._timer.stop()
|
||||
super().hideEvent(e)
|
||||
|
||||
# --- effect simulation ---
|
||||
|
||||
def _colors(self) -> tuple[RGB, RGB]:
|
||||
cs = self.profile.colors + ["#ffffff", "#ffffff"]
|
||||
return _hex(cs[0]), _hex(cs[1])
|
||||
|
||||
def _random(self, n: int) -> RGB:
|
||||
return _hue(self._palette_seed[n % len(self._palette_seed)])
|
||||
|
||||
def _key_color(self, k: layout.Key, t: float) -> RGB:
|
||||
p = self.profile
|
||||
s = SPEED_FACTOR.get(p.speed, 1.0)
|
||||
c0, c1 = self._colors()
|
||||
rnd = p.random_colors
|
||||
x, y = k.cx / layout.WIDTH, k.cy / layout.HEIGHT
|
||||
|
||||
match p.effect:
|
||||
case "off":
|
||||
return (0, 0, 0)
|
||||
case "static":
|
||||
return c0
|
||||
case "rainbow_wave":
|
||||
return _hue(x - t * 0.25 * s)
|
||||
case "spiral_rainbow":
|
||||
a = math.atan2(y - 0.5, (x - 0.5) * 2.5) / (2 * math.pi)
|
||||
return _hue(a + t * 0.2 * s)
|
||||
case "color_wave":
|
||||
phase = x - t * 0.3 * s
|
||||
n = math.floor(phase)
|
||||
band = 0.5 + 0.5 * math.cos((phase - n) * 2 * math.pi)
|
||||
col = self._random(-n) if rnd else (c0 if n % 2 == 0 else c1)
|
||||
return _scale(col, band ** 2)
|
||||
case "color_shift":
|
||||
phase = t * 0.25 * s
|
||||
n = math.floor(phase)
|
||||
a = self._random(n) if rnd else (c0 if n % 2 == 0 else c1)
|
||||
b = self._random(n + 1) if rnd else (c1 if n % 2 == 0 else c0)
|
||||
return _mix(a, b, 0.5 - 0.5 * math.cos((phase - n) * math.pi))
|
||||
case "color_pulse":
|
||||
phase = t * 0.5 * s
|
||||
n = math.floor(phase)
|
||||
col = self._random(n) if rnd else (c0 if n % 2 == 0 else c1)
|
||||
return _scale(col, math.sin((phase - n) * math.pi) ** 2)
|
||||
case "rain":
|
||||
col_i = int(k.cx * 2)
|
||||
seed = self._palette_seed[col_i % 64]
|
||||
head = ((t * 0.5 * s + seed * 3) % 1.6) * layout.HEIGHT - 1
|
||||
d = head - k.cy
|
||||
lvl = max(0.0, 1 - d / 2.0) if d >= 0 else 0.0
|
||||
col = self._random(col_i) if rnd else (c0 if col_i % 2 else c1)
|
||||
return _scale(col, lvl)
|
||||
case "watercolor":
|
||||
h = 0.5 + 0.25 * math.sin(x * 4 + t * 0.6 * s) + 0.25 * math.sin(y * 5 - t * 0.4 * s)
|
||||
return colorsys.hsv_to_rgb(h % 1.0, 0.55, 1.0)
|
||||
case "type_key" | "type_ripple":
|
||||
return self._typing(k, t, c0, c1)
|
||||
case "visor":
|
||||
pos = (math.sin(t * 1.2 * s) * 0.5 + 0.5) * layout.WIDTH
|
||||
lvl = max(0.0, 1 - abs(k.cx - pos) / 2.5)
|
||||
col = self._random(int(t * 1.2 * s / math.pi)) if rnd else c0
|
||||
return _scale(col, lvl)
|
||||
return c0
|
||||
|
||||
def _typing(self, k: layout.Key, t: float, c0: RGB, c1: RGB) -> RGB:
|
||||
level = 0.0
|
||||
best: RGB = (0, 0, 0)
|
||||
for start, key, col in self._presses:
|
||||
age = t - start
|
||||
if self.profile.effect == "type_key":
|
||||
if key is k:
|
||||
lvl = max(0.0, 1 - age / 1.2)
|
||||
else:
|
||||
continue
|
||||
else:
|
||||
d = math.hypot(k.cx - key.cx, k.cy - key.cy)
|
||||
front = age * 9 * SPEED_FACTOR.get(self.profile.speed, 1.0)
|
||||
lvl = max(0.0, 1 - abs(d - front) / 1.2) * max(0.0, 1 - age / 1.6)
|
||||
if lvl > level:
|
||||
level, best = lvl, col
|
||||
return _scale(best, level)
|
||||
|
||||
def _update_presses(self, t: float) -> None:
|
||||
if self.profile.effect not in ("type_key", "type_ripple"):
|
||||
self._presses.clear()
|
||||
return
|
||||
if t >= self._next_press:
|
||||
key = self._rand.choice(layout.KEYS)
|
||||
c0, c1 = self._colors()
|
||||
col = _hue(self._rand.random()) if self.profile.random_colors else c0
|
||||
self._presses.append((t, key, col))
|
||||
self._next_press = t + self._rand.uniform(0.15, 0.6)
|
||||
self._presses = [p for p in self._presses if t - p[0] < 2.0]
|
||||
|
||||
# --- painting ---
|
||||
|
||||
def paintEvent(self, _):
|
||||
t = time.monotonic() - self._t0
|
||||
self._update_presses(t)
|
||||
p = QPainter(self)
|
||||
p.setRenderHint(QPainter.Antialiasing)
|
||||
|
||||
margin = 18
|
||||
unit = min((self.width() - 2 * margin) / (layout.WIDTH + 0.5),
|
||||
(self.height() - 2 * margin) / (layout.HEIGHT + 0.5))
|
||||
bw, bh = unit * (layout.WIDTH + 0.5), unit * (layout.HEIGHT + 0.5)
|
||||
ox, oy = (self.width() - bw) / 2, (self.height() - bh) / 2
|
||||
|
||||
body = QRectF(ox, oy, bw, bh)
|
||||
p.setPen(QPen(QColor("#2c2f36"), 1.5))
|
||||
p.setBrush(QColor("#15171b"))
|
||||
p.drawRoundedRect(body, unit * 0.35, unit * 0.35)
|
||||
|
||||
bright = self.profile.brightness / 100 if self.connected else 0.0
|
||||
font = QFont(self.font())
|
||||
font.setPixelSize(max(7, int(unit * 0.24)))
|
||||
p.setFont(font)
|
||||
pad = unit * 0.07
|
||||
kx, ky = ox + unit * 0.25, oy + unit * 0.25
|
||||
|
||||
for k in layout.KEYS:
|
||||
r, g, b = _scale(self._key_color(k, t), bright)
|
||||
glow = QColor.fromRgbF(min(r, 1), min(g, 1), min(b, 1))
|
||||
rect = QRectF(kx + k.x * unit + pad, ky + k.y * unit + pad, k.w * unit - 2 * pad, k.h * unit - 2 * pad)
|
||||
if k.iso_enter:
|
||||
top, stem = QPainterPath(), QPainterPath()
|
||||
top.addRoundedRect(QRectF(rect.x(), rect.y(), rect.width(), unit - 2 * pad), 4, 4)
|
||||
stem.addRoundedRect(QRectF(rect.x() + unit * 0.25, rect.y(), rect.width() - unit * 0.25,
|
||||
rect.height()), 4, 4)
|
||||
path = top.united(stem)
|
||||
else:
|
||||
path = QPainterPath()
|
||||
path.addRoundedRect(rect, 4, 4)
|
||||
# Keycap: dark top, the light leaks around the edge like on the real board.
|
||||
p.setPen(QPen(glow, max(1.5, unit * 0.05)))
|
||||
cap = QColor.fromRgbF(0.09 + r * 0.22, 0.10 + g * 0.22, 0.11 + b * 0.22)
|
||||
p.setBrush(cap)
|
||||
p.drawPath(path)
|
||||
if k.label:
|
||||
p.setPen(QColor.fromRgbF(0.35 + min(r, 1) * 0.65, 0.35 + min(g, 1) * 0.65,
|
||||
0.35 + min(b, 1) * 0.65))
|
||||
label_rect = QRectF(rect.x(), rect.y(), rect.width(), unit - 2 * pad)
|
||||
p.drawText(label_rect, Qt.AlignCenter, k.label)
|
||||
|
||||
if not self.connected:
|
||||
p.setPen(QColor("#9aa0aa"))
|
||||
f = QFont(self.font())
|
||||
f.setPixelSize(15)
|
||||
p.setFont(f)
|
||||
p.drawText(body, Qt.AlignCenter, "Tastatur nicht verbunden")
|
||||
p.end()
|
||||
Reference in new issue
Block a user