104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
import evdev
|
|
import uinput
|
|
import subprocess
|
|
import time
|
|
import os
|
|
|
|
DEVICE_NAMES = ["8BitDo NGC Modkit"]
|
|
BUTTON_MAP = {
|
|
"304": "KEY_ENTER",
|
|
"305": None,
|
|
"311": "KEY_LEFTALT",
|
|
"307": "KEY_RIGHT",
|
|
"308": "KEY_LEFT",
|
|
}
|
|
|
|
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
|
|
|
|
pressed_keys = set()
|
|
|
|
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)
|
|
|
|
# mettre à jour l'état des touches pressées
|
|
if event.value == 1:
|
|
pressed_keys.add(key_code)
|
|
elif event.value == 0:
|
|
pressed_keys.discard(key_code)
|
|
|
|
# logique R + B → Alt+Esc
|
|
if "311" in pressed_keys and "305" in pressed_keys:
|
|
keyboard.emit(uinput.KEY_LEFTALT, 1)
|
|
keyboard.emit(uinput.KEY_ESC, 1)
|
|
keyboard.emit(uinput.KEY_ESC, 0)
|
|
keyboard.emit(uinput.KEY_LEFTALT, 0)
|
|
else:
|
|
# envoyer les touches normales sauf celles mappées à None
|
|
if key_code in BUTTON_MAP:
|
|
mapped_key = BUTTON_MAP[key_code]
|
|
if mapped_key is not None: # uniquement si mapped_key n'est pas None ou vide
|
|
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()
|