Files
MrBlakeandClaude Opus 5.5 7e276912da 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]>
2026-10-23 21:02:59 +02:00

159 lines
5.7 KiB
Python

"""Application wiring: Qt app, tray icon, single instance and the device service."""
from __future__ import annotations
import os
import signal
import sys
from pathlib import Path
from PySide6.QtCore import QObject, QTimer, Signal
from PySide6.QtGui import QAction, QActionGroup, QIcon
from PySide6.QtNetwork import QLocalServer, QLocalSocket
from PySide6.QtWidgets import QApplication, QMenu, QSystemTrayIcon
from .device import DeviceService, DeviceState
from .profiles import Config
from .ui.main_window import MainWindow
from .ui.style import STYLESHEET
ICON = Path(__file__).parent / "assets" / "k65studio.svg"
INSTANCE_NAME = f"k65studio-{os.getuid()}"
LOW_BATTERY = 15
class _Bridge(QObject):
state = Signal(object)
class App:
def __init__(self, argv: list[str], minimized: bool):
self.qapp = QApplication(argv)
self.qapp.setApplicationName("K65 Studio")
self.qapp.setDesktopFileName("k65studio")
self.qapp.setQuitOnLastWindowClosed(False)
self.qapp.setStyleSheet(STYLESHEET)
self.icon = QIcon(str(ICON))
self.qapp.setWindowIcon(self.icon)
self.config = Config.load()
self.window = MainWindow(self.config)
self.window.closeEvent = self._close_window
self.bridge = _Bridge()
self.bridge.state.connect(self._on_state)
self.device = DeviceService(self.bridge.state.emit)
self.window.profile_changed.connect(lambda profile: self.device.apply(profile))
self.window.reinit_requested.connect(self._reinit)
self._warned_battery = False
self._hint_shown = False
self.tray = self._build_tray() if QSystemTrayIcon.isSystemTrayAvailable() else None
self.server = QLocalServer()
QLocalServer.removeServer(INSTANCE_NAME)
self.server.listen(INSTANCE_NAME)
self.server.newConnection.connect(self._show_window)
self.qapp.aboutToQuit.connect(self._shutdown)
signal.signal(signal.SIGTERM, lambda *_: self.qapp.quit())
signal.signal(signal.SIGINT, lambda *_: self.qapp.quit())
self.device.start(self.config.profile)
if not ((minimized or self.config.start_minimized) and self.tray):
self._show_window()
def _build_tray(self) -> QSystemTrayIcon:
tray = QSystemTrayIcon(self.icon)
tray.setToolTip("K65 Studio")
menu = QMenu()
menu.addAction("Öffnen", self._show_window)
self.profile_menu = menu.addMenu("Profil")
self.profile_menu.aboutToShow.connect(self._fill_profile_menu)
menu.addSeparator()
menu.addAction("Beenden", self.qapp.quit)
tray.setContextMenu(menu)
tray.activated.connect(lambda reason: reason == QSystemTrayIcon.Trigger and self._toggle_window())
tray.show()
self._tray_menu = menu
return tray
def _fill_profile_menu(self) -> None:
self.profile_menu.clear()
group = QActionGroup(self.profile_menu)
for name in self.config.profiles:
act = QAction(name, self.profile_menu, checkable=True, checked=name == self.config.active)
act.triggered.connect(lambda _=False, n=name: self.window.activate_profile(n))
group.addAction(act)
self.profile_menu.addAction(act)
def _show_window(self) -> None:
while self.server.hasPendingConnections():
self.server.nextPendingConnection().close()
self.window.showNormal()
self.window.raise_()
self.window.activateWindow()
def _toggle_window(self) -> None:
if self.window.isVisible():
self.window.hide()
else:
self._show_window()
def _close_window(self, event) -> None:
if self.tray is None:
event.accept()
self.qapp.quit()
return
event.ignore()
self.window.hide()
if not self._hint_shown:
self._hint_shown = True
self.tray.showMessage("K65 Studio", "Läuft im Hintergrund weiter, damit die Beleuchtung erhalten bleibt.",
self.icon, 4000)
def _on_state(self, state: DeviceState) -> None:
self.window.set_state(state)
if self.tray is None:
return
if state.keyboard and state.battery is not None:
self.tray.setToolTip(f"K65 Studio · K65 PLUS · Akku {state.battery} %")
if state.battery <= LOW_BATTERY and not self._warned_battery:
self._warned_battery = True
self.tray.showMessage("Akku schwach", f"K65 PLUS: noch {state.battery} %. Bitte bald laden.",
QSystemTrayIcon.Warning, 8000)
elif state.battery > LOW_BATTERY + 5:
self._warned_battery = False
else:
self.tray.setToolTip("K65 Studio · Tastatur nicht verbunden")
def _reinit(self) -> None:
self.device.stop(restore_hardware=False)
self.device = DeviceService(self.bridge.state.emit)
self.device.start(self.config.profile)
def _shutdown(self) -> None:
self.config.save()
self.device.stop(restore_hardware=self.config.restore_hardware_on_exit)
if self.tray:
self.tray.hide()
def run(self) -> int:
# Wake the interpreter regularly so Python signal handlers (SIGTERM/SIGINT) run.
self._sig_timer = QTimer(interval=300, timeout=lambda: None)
self._sig_timer.start()
return self.qapp.exec()
def notify_running_instance() -> bool:
sock = QLocalSocket()
sock.connectToServer(INSTANCE_NAME)
ok = sock.waitForConnected(300)
sock.close()
return ok
def main(minimized: bool = False) -> int:
if notify_running_instance():
return 0
return App(sys.argv, minimized).run()