user service works
This commit is contained in:
		| @@ -1,25 +1,18 @@ | ||||
| #!/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 | ||||
|     "304": "Return",    # A | ||||
|     "305": None,        # B (non utilisé) | ||||
|     "311": "Alt_L",     # R | ||||
|     "307": "Right",     # X | ||||
|     "308": "Left",      # Y | ||||
|     # 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: | ||||
| @@ -32,13 +25,16 @@ def find_device(): | ||||
|  | ||||
| def is_dolphin_running(): | ||||
|     try: | ||||
|         output = subprocess.check_output( | ||||
|             ["flatpak", "ps"] | ||||
|         ).decode() | ||||
|         output = subprocess.check_output(["flatpak", "ps"]).decode() | ||||
|         return "org.DolphinEmu.dolphin-emu" in output | ||||
|     except Exception: | ||||
|         return False | ||||
|  | ||||
| def press_key(key, press): | ||||
|     # press=True -> key down, press=False -> key up | ||||
|     action = "keydown" if press else "keyup" | ||||
|     subprocess.run(["xdotool", action, key]) | ||||
|  | ||||
| def handle_device(dev_path): | ||||
|     dev = evdev.InputDevice(dev_path) | ||||
|     try: | ||||
| @@ -50,19 +46,18 @@ def handle_device(dev_path): | ||||
|  | ||||
|             # cas spécial : joystick jaune (BTN_THUMBR, code 318) | ||||
|             if key_code == "318" and event.value == 1:  # pression | ||||
|                 keyboard.emit(uinput.KEY_LEFTALT, 1) | ||||
|                 press_key("Alt_L", True) | ||||
|                 time.sleep(0.02) | ||||
|                 keyboard.emit(uinput.KEY_ESC, 1) | ||||
|                 press_key("Escape", True) | ||||
|                 time.sleep(0.05) | ||||
|                 keyboard.emit(uinput.KEY_ESC, 0) | ||||
|                 keyboard.emit(uinput.KEY_LEFTALT, 0) | ||||
|                 press_key("Escape", False) | ||||
|                 press_key("Alt_L", False) | ||||
|                 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) | ||||
|                     press_key(mapped_key, event.value == 1) | ||||
|  | ||||
|     except OSError: | ||||
|         dev.close() | ||||
| @@ -70,7 +65,7 @@ def handle_device(dev_path): | ||||
|  | ||||
| def main(): | ||||
|     connected = False | ||||
|     time.sleep(5)  # délai pour que uinput et le serveur graphique soient prêts | ||||
|     time.sleep(5)  # délai pour que le serveur graphique soit prêt | ||||
|     while True: | ||||
|         dev_path = find_device() | ||||
|         if dev_path and not connected: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user