Files
linux-env/gamecube-pad/gamecube-pad.py
2025-09-27 16:33:40 +02:00

98 lines
3.0 KiB
Python

#!/usr/bin/env python3
import evdev
import uinput
import subprocess
import time
DEVICE_NAMES = ["8BitDo NGC Modkit"]
BUTTON_MAP = {
"304": "KEY_ENTER", # A
"305": None, # B (non utilisé)
"311": "KEY_LEFTALT", # R
"307": "KEY_RIGHT", # Stick droit → Droite
"308": "KEY_LEFT", # Stick droit → Gauche
# 318 (BTN_THUMBR) sera géré à part
}
events = [
uinput.KEY_LEFT, uinput.KEY_RIGHT,
uinput.KEY_ENTER, uinput.KEY_ESC, uinput.KEY_LEFTALT
]
keyboard = uinput.Device(events)
def find_device():
for path in evdev.list_devices():
try:
dev = evdev.InputDevice(path)
if any(dn in dev.name for dn in DEVICE_NAMES):
return path
except Exception:
continue
return None
def is_dolphin_running():
try:
output = subprocess.check_output(
["flatpak", "ps", "--columns=name"]
).decode()
return "org.DolphinEmu.dolphin-emu" in output
except Exception:
return False
def handle_device(dev_path):
dev = evdev.InputDevice(dev_path)
try:
for event in dev.read_loop():
if event.type != evdev.ecodes.EV_KEY:
continue
key_code = str(event.code)
# cas spécial : joystick jaune (BTN_THUMBR, code 318)
if key_code == "318" and event.value == 1: # pression
keyboard.emit(uinput.KEY_LEFTALT, 1)
keyboard.emit(uinput.KEY_ESC, 1)
keyboard.emit(uinput.KEY_ESC, 0)
keyboard.emit(uinput.KEY_LEFTALT, 0)
continue
# touches normales selon le mapping
if key_code in BUTTON_MAP:
mapped_key = BUTTON_MAP[key_code]
if mapped_key is not None:
keyboard.emit(getattr(uinput, mapped_key), event.value)
except OSError:
dev.close()
raise
def main():
connected = False
time.sleep(5) # délai pour que uinput et le serveur graphique soient prêts
while True:
dev_path = find_device()
if dev_path and not connected:
print(f"[+] Manette connectée: {dev_path}")
connected = True
if not is_dolphin_running():
try:
subprocess.Popen(["flatpak", "run", "org.DolphinEmu.dolphin-emu"])
except Exception as e:
print(f"[!] Impossible de lancer Dolphin: {e}")
try:
handle_device(dev_path)
except OSError:
print("[-] Manette déconnectée")
connected = False
try:
subprocess.Popen(["flatpak", "kill", "org.DolphinEmu.dolphin-emu"])
except Exception as e:
print(f"[!] Impossible de tuer Dolphin: {e}")
elif not dev_path and connected:
print("[-] Manette déconnectée")
connected = False
time.sleep(1)
if __name__ == "__main__":
main()