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,167 @@
|
||||
"""Corsair "Bragi" protocol as spoken by the K65 PLUS Wireless and its Slipstream receiver.
|
||||
|
||||
Every packet is 64 bytes: [target, command, args...].
|
||||
target 0x08 = the device behind this USB interface (receiver, or keyboard when wired)
|
||||
target 0x09 = the keyboard paired to the receiver (wireless)
|
||||
Responses echo [target, command, status, data...]; status 0 means success.
|
||||
|
||||
Protocol knowledge from the OpenLinkHub (GPL-3.0) and OpenRGB (GPL-2.0+) projects.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
from dataclasses import dataclass
|
||||
|
||||
VENDOR_CORSAIR = 0x1B1C
|
||||
PID_SLIPSTREAM_K65 = 0x2B07
|
||||
PIDS_K65_PLUS_WIRED = (0x2B10, 0x2B11, 0x2B23)
|
||||
|
||||
TARGET_LOCAL = 0x08
|
||||
TARGET_PAIRED = 0x09
|
||||
|
||||
# Commands
|
||||
CMD_SET = 0x01
|
||||
CMD_GET = 0x02
|
||||
CMD_CLOSE = 0x05
|
||||
CMD_WRITE_FIRST = 0x06
|
||||
CMD_WRITE_NEXT = 0x07
|
||||
CMD_READ = 0x08
|
||||
CMD_PREPARE = 0x09
|
||||
CMD_OPEN = 0x0D
|
||||
CMD_HEARTBEAT = 0x12
|
||||
CMD_DEVICE_LIST = 0x24
|
||||
|
||||
# Properties (CMD_SET / CMD_GET)
|
||||
PROP_BRIGHTNESS = 0x02 # u16, 0..1000
|
||||
PROP_MODE = 0x03 # 1 = hardware, 2 = software
|
||||
PROP_SLEEP = 0x0E # u32, milliseconds
|
||||
PROP_BATTERY = 0x0F # u16, per mille
|
||||
PROP_FIRMWARE = 0x13
|
||||
PROP_GAME_MODE = 0x45 # 0/1
|
||||
PROP_LOCKS = 0x4A # bitmask, see LOCK_*
|
||||
|
||||
MODE_HARDWARE = 0x01
|
||||
MODE_SOFTWARE = 0x02
|
||||
|
||||
LOCK_WIN = 0x01
|
||||
LOCK_ALT_TAB = 0x02
|
||||
LOCK_ALT_F4 = 0x04
|
||||
LOCK_SHIFT_TAB = 0x08
|
||||
|
||||
# Writable resources
|
||||
RESOURCE_LIGHTING = 0x6D60
|
||||
RESOURCE_KEYMAP = 0x02
|
||||
|
||||
HANDLE = 0x01
|
||||
CHUNK_FIRST = 61 # 64 - [target, cmd, handle]
|
||||
KEYMAP_SIZE = 123
|
||||
|
||||
# Notifications (second vendor interface)
|
||||
NOTIFY_CONNECTION = 0x36
|
||||
|
||||
# Lighting effect ids
|
||||
FX_STATIC = 0x207E
|
||||
FX_COLOR_PULSE = 0xAD4F
|
||||
FX_COLOR_SHIFT = 0xA5FA
|
||||
FX_COLOR_WAVE = 0x7BFF
|
||||
FX_RAINBOW_WAVE = 0xB94C
|
||||
FX_RAIN = 0xA07E
|
||||
FX_SPIRAL_RAINBOW = 0xAB87
|
||||
FX_WATERCOLOR = 0x0022
|
||||
FX_TYPE_KEY = 0xB1F9
|
||||
FX_TYPE_RIPPLE = 0x09A2
|
||||
FX_VISOR = 0x90C0
|
||||
|
||||
COLORS_NONE = 0x00
|
||||
COLORS_SPECIFIC = 0x01
|
||||
COLORS_RANDOM = 0x02
|
||||
COLORS_ALT = 0x03
|
||||
|
||||
SPEED_SLOW = 0x03
|
||||
SPEED_MEDIUM = 0x04
|
||||
SPEED_FAST = 0x05
|
||||
|
||||
# Default keymap: HID usage -> function code. 57 = regular key, 59 = key with an Fn layer,
|
||||
# 63 = modifier. Everything else stays 0x01 (no key at that position).
|
||||
_FN_LAYER = {18, 19, 47, 48, 55, 56, 58, 59, 60, 61, *range(64, 70)}
|
||||
_MODIFIERS = {105, 106, 107, 109, 110, 111, 122}
|
||||
_REGULAR = {*range(4, 18), *range(20, 47), *range(49, 50), *range(51, 55), 57,
|
||||
62, 63, 74, 75, 76, *range(78, 83), 108}
|
||||
_REGULAR -= _FN_LAYER
|
||||
|
||||
|
||||
def default_keymap() -> bytes:
|
||||
table = bytearray([0x01] * KEYMAP_SIZE)
|
||||
for usage in _REGULAR:
|
||||
table[usage] = 57
|
||||
for usage in _FN_LAYER:
|
||||
table[usage] = 59
|
||||
for usage in _MODIFIERS:
|
||||
table[usage] = 63
|
||||
return bytes(table)
|
||||
|
||||
|
||||
def packet(target: int, *parts: bytes | int) -> bytes:
|
||||
out = bytearray([target])
|
||||
for p in parts:
|
||||
out += bytes([p]) if isinstance(p, int) else p
|
||||
if len(out) > 64:
|
||||
raise ValueError("packet exceeds 64 bytes")
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def set_property(target: int, prop: int, value: bytes) -> bytes:
|
||||
return packet(target, CMD_SET, prop, 0x00, value)
|
||||
|
||||
|
||||
def get_property(target: int, prop: int) -> bytes:
|
||||
return packet(target, CMD_GET, prop)
|
||||
|
||||
|
||||
def block_packets(target: int, payload: bytes, declared: int | None = None) -> list[bytes]:
|
||||
"""Split a length-prefixed block write to HANDLE into first/next packets."""
|
||||
framed = struct.pack("<HH", len(payload) if declared is None else declared, 0) + payload
|
||||
chunks = [framed[i:i + CHUNK_FIRST] for i in range(0, len(framed), CHUNK_FIRST)]
|
||||
return [packet(target, CMD_WRITE_FIRST if i == 0 else CMD_WRITE_NEXT, HANDLE, c)
|
||||
for i, c in enumerate(chunks)]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Rgb:
|
||||
r: int
|
||||
g: int
|
||||
b: int
|
||||
|
||||
@classmethod
|
||||
def from_hex(cls, value: str) -> "Rgb":
|
||||
value = value.lstrip("#")
|
||||
return cls(int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16))
|
||||
|
||||
def hex(self) -> str:
|
||||
return f"#{self.r:02x}{self.g:02x}{self.b:02x}"
|
||||
|
||||
|
||||
LIGHTING_BLOCK_SIZE = 96
|
||||
|
||||
|
||||
def lighting_packets(target: int, effect: int, color_mode: int = COLORS_NONE, speed: int = 0,
|
||||
direction: int = 0, colors: list[Rgb] | tuple[Rgb, ...] = ()) -> list[bytes]:
|
||||
"""Hardware lighting effect: id, color mode, speed, direction, then colors as (A, B, G, R)."""
|
||||
body = bytearray(struct.pack("<H", effect))
|
||||
body += bytes([color_mode, speed, direction, 0x00])
|
||||
if colors:
|
||||
body.append(len(colors))
|
||||
for c in colors:
|
||||
body += bytes([0xFF, c.b, c.g, c.r])
|
||||
# Same declared lengths OpenLinkHub uses; everything past the colors is zero padding.
|
||||
declared = 93 if effect == FX_STATIC else 89
|
||||
return block_packets(target, bytes(body.ljust(LIGHTING_BLOCK_SIZE, b"\x00")), declared)
|
||||
|
||||
|
||||
def parse_u16(resp: bytes, offset: int = 3) -> int:
|
||||
return struct.unpack_from("<H", resp, offset)[0]
|
||||
|
||||
|
||||
def parse_firmware(resp: bytes) -> str:
|
||||
return f"{resp[3]}.{resp[4]}.{struct.unpack_from('<H', resp, 5)[0]}"
|
||||
Reference in new issue
Block a user