You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
250 lines
9.1 KiB
Python
250 lines
9.1 KiB
Python
#!/usr/bin/python3
|
|
|
|
import time
|
|
import os
|
|
import evdev
|
|
from evdev import ecodes, UInput
|
|
|
|
from ha_volume import adjust_volume, HAError
|
|
|
|
DEVICE_NAME = "XING WEI 2.4G USB USB Composite Device Consumer Control"
|
|
|
|
# HID consumer scancodes carried in MSC_SCAN.value for the volume keys.
|
|
VOLUP_SCAN = 0xC00E9 # -> KEY_VOLUMEUP
|
|
VOLDOWN_SCAN = 0xC00EA # -> KEY_VOLUMEDOWN
|
|
|
|
# How much to change HA volume_level per keypress, in percentage points.
|
|
VOLUME_STEP = 2
|
|
|
|
# Bidirectional maps between EV_KEY codes and MSC_SCAN values
|
|
SCAN_TO_KEY = {
|
|
VOLUP_SCAN: ecodes.KEY_VOLUMEUP,
|
|
VOLDOWN_SCAN: ecodes.KEY_VOLUMEDOWN,
|
|
}
|
|
KEY_TO_SCAN = {v: k for k, v in SCAN_TO_KEY.items()}
|
|
SCAN_TO_DIR = {VOLUP_SCAN: "up", VOLDOWN_SCAN: "down"}
|
|
|
|
|
|
def kodi_running():
|
|
for pid in os.listdir("/proc"):
|
|
if not pid.isdigit():
|
|
continue
|
|
try:
|
|
with open(f"/proc/{pid}/comm") as f:
|
|
if f.read().strip() in ("kodi", "kodi.bin"):
|
|
return True
|
|
except (OSError, PermissionError):
|
|
pass
|
|
return False
|
|
|
|
|
|
def find_device():
|
|
while True:
|
|
for path in evdev.list_devices():
|
|
try:
|
|
dev = evdev.InputDevice(path)
|
|
if dev.name == DEVICE_NAME:
|
|
if ecodes.KEY_BACK in dev.capabilities().get(ecodes.EV_KEY, []):
|
|
print(f"Found device: {path}", flush=True)
|
|
return dev
|
|
dev.close()
|
|
except (OSError, PermissionError):
|
|
pass
|
|
print("Waiting for XING WEI device...", flush=True)
|
|
time.sleep(2)
|
|
|
|
|
|
def run():
|
|
while True:
|
|
dev = find_device()
|
|
|
|
passthrough = None
|
|
remapper = None
|
|
|
|
# Remember which key was used for the current BACK press.
|
|
back_key = None
|
|
|
|
# Per-scancode state for volume keys while the device is grabbed.
|
|
# "intercepting" -> we swallowed the press; swallow the release too.
|
|
# "passthrough" -> we forwarded the press; forward the release too.
|
|
# Entry present -> key currently held (press is in progress).
|
|
# Entry absent -> key is released.
|
|
vol_state = {}
|
|
|
|
try:
|
|
passthrough = UInput.from_device(
|
|
dev,
|
|
name="XING WEI Passthrough",
|
|
vendor=0x1915,
|
|
product=0x1025,
|
|
version=0x0201
|
|
)
|
|
|
|
remapper = UInput(
|
|
events={
|
|
ecodes.EV_KEY: [
|
|
ecodes.KEY_ESC,
|
|
ecodes.KEY_BACKSPACE,
|
|
ecodes.KEY_LEFTSHIFT,
|
|
ecodes.KEY_TAB,
|
|
]
|
|
},
|
|
name="XING WEI Remapped Keys",
|
|
bustype=0x03,
|
|
vendor=0x1915,
|
|
product=0x1026,
|
|
version=0x0201
|
|
)
|
|
|
|
dev.grab()
|
|
|
|
print("Device grabbed successfully!", flush=True)
|
|
print("Created: XING WEI Passthrough", flush=True)
|
|
print("Created: XING WEI Remapped Keys", flush=True)
|
|
print("BACK: ESC / BACKSPACE (Kodi)", flush=True)
|
|
print("HOME: SHIFT + TAB", flush=True)
|
|
print(f"VOL UP/DOWN: HA volume {VOLUME_STEP:+d}/{-VOLUME_STEP:+d} (Kodi)",
|
|
flush=True)
|
|
|
|
for event in dev.read_loop():
|
|
|
|
# ------------------------------------------------------------------
|
|
# MSC_SCAN: runs *before* the matching EV_KEY in each batch. We use
|
|
# it to decide (and remember) whether to intercept this press.
|
|
# ------------------------------------------------------------------
|
|
if event.type == ecodes.EV_MSC and event.code == ecodes.MSC_SCAN:
|
|
|
|
scan = event.value
|
|
if scan in SCAN_TO_KEY:
|
|
state = vol_state.get(scan)
|
|
|
|
if state == "intercepting":
|
|
# Release scan of an intercepted key: keep suppressed.
|
|
continue
|
|
|
|
if state == "passthrough":
|
|
# Release scan of a passed-through key: forward it.
|
|
passthrough.write_event(event)
|
|
continue
|
|
|
|
# New press. Decide now based on Kodi; remember the
|
|
# decision so the release cannot flip mid-press.
|
|
if kodi_running():
|
|
vol_state[scan] = "intercepting"
|
|
continue
|
|
else:
|
|
vol_state[scan] = "passthrough"
|
|
passthrough.write_event(event)
|
|
continue
|
|
|
|
# Non-volume scan: forward as before.
|
|
passthrough.write_event(event)
|
|
|
|
# ------------------------------------------------------------------
|
|
# EV_KEY
|
|
# ------------------------------------------------------------------
|
|
elif event.type == ecodes.EV_KEY:
|
|
|
|
scan = KEY_TO_SCAN.get(event.code)
|
|
state = vol_state.get(scan) if scan is not None else None
|
|
|
|
# ---- Volume key that we're intercepting (Kodi is running) ----
|
|
if state == "intercepting":
|
|
if event.value == 1:
|
|
delta = VOLUME_STEP if SCAN_TO_DIR[scan] == "up" else -VOLUME_STEP
|
|
try:
|
|
adjust_volume(delta)
|
|
except HAError as e:
|
|
print(f"volume adjust failed: {e}", flush=True)
|
|
except Exception as e:
|
|
print(f"volume adjust error: {e!r}", flush=True)
|
|
elif event.value == 0:
|
|
# Release completes the pair; clear state.
|
|
del vol_state[scan]
|
|
# Suppress either way.
|
|
continue
|
|
|
|
# ---- Volume key we're passing through ----
|
|
if state == "passthrough":
|
|
passthrough.write_event(event)
|
|
if event.value == 0:
|
|
del vol_state[scan]
|
|
continue
|
|
|
|
# ---- BACK -> ESC / BACKSPACE ----
|
|
if event.code == ecodes.KEY_BACK:
|
|
|
|
if event.value == 1:
|
|
back_key = (
|
|
ecodes.KEY_BACKSPACE
|
|
if kodi_running()
|
|
else ecodes.KEY_ESC
|
|
)
|
|
elif back_key is None:
|
|
back_key = (
|
|
ecodes.KEY_BACKSPACE
|
|
if kodi_running()
|
|
else ecodes.KEY_ESC
|
|
)
|
|
|
|
remapper.write(
|
|
ecodes.EV_KEY,
|
|
back_key,
|
|
event.value
|
|
)
|
|
|
|
if event.value == 0:
|
|
back_key = None
|
|
|
|
# ---- HOME -> SHIFT + TAB ----
|
|
elif event.code == ecodes.KEY_HOMEPAGE:
|
|
|
|
if event.value == 1:
|
|
remapper.write(ecodes.EV_KEY, ecodes.KEY_LEFTSHIFT, 1)
|
|
remapper.write(ecodes.EV_KEY, ecodes.KEY_TAB, 1)
|
|
remapper.write(ecodes.EV_KEY, ecodes.KEY_TAB, 0)
|
|
remapper.write(ecodes.EV_KEY, ecodes.KEY_LEFTSHIFT, 0)
|
|
remapper.syn()
|
|
|
|
# ---- Everything else goes to passthrough ----
|
|
else:
|
|
passthrough.write_event(event)
|
|
|
|
# ------------------------------------------------------------------
|
|
# SYN
|
|
# ------------------------------------------------------------------
|
|
elif event.type == ecodes.EV_SYN:
|
|
passthrough.write_event(event)
|
|
if event.code == ecodes.SYN_REPORT:
|
|
remapper.syn()
|
|
|
|
# ------------------------------------------------------------------
|
|
# Anything else (REL, ABS, etc.)
|
|
# ------------------------------------------------------------------
|
|
else:
|
|
passthrough.write_event(event)
|
|
|
|
except (OSError, IOError) as e:
|
|
print(f"Device error: {e}", flush=True)
|
|
|
|
finally:
|
|
if dev:
|
|
try:
|
|
dev.ungrab()
|
|
except OSError:
|
|
pass
|
|
dev.close()
|
|
|
|
if passthrough:
|
|
passthrough.close()
|
|
|
|
if remapper:
|
|
remapper.close()
|
|
|
|
print("Reconnecting in 2 seconds...", flush=True)
|
|
time.sleep(2)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run()
|