From e1b525eaa15c15d8a8e03cc5b70d035a055998d2 Mon Sep 17 00:00:00 2001 From: valere Date: Sat, 27 Sep 2025 11:27:10 +0200 Subject: [PATCH] gamecube pad V1 --- .../{gamecube-pad2.sh => gamecube-pad.py} | 96 +++-------- gamecube-pad/gamecube-pad.service | 15 ++ gamecube-pad/gamecube-pad.sh | 154 ------------------ gamecube-pad/install.sh | 46 ++++++ 4 files changed, 86 insertions(+), 225 deletions(-) rename gamecube-pad/{gamecube-pad2.sh => gamecube-pad.py} (55%) create mode 100644 gamecube-pad/gamecube-pad.service delete mode 100644 gamecube-pad/gamecube-pad.sh create mode 100755 gamecube-pad/install.sh diff --git a/gamecube-pad/gamecube-pad2.sh b/gamecube-pad/gamecube-pad.py similarity index 55% rename from gamecube-pad/gamecube-pad2.sh rename to gamecube-pad/gamecube-pad.py index 55da92c..ef5d5c4 100644 --- a/gamecube-pad/gamecube-pad2.sh +++ b/gamecube-pad/gamecube-pad.py @@ -1,18 +1,3 @@ -#!/bin/bash -set -e - -PYTHON_SCRIPT="$HOME/.local/bin/gamecube-pad.py" -SERVICE_DIR="$HOME/.config/systemd/user" -SERVICE_FILE="$SERVICE_DIR/gamecube-pad.service" - -install_service() { - echo "[+] Installation des dépendances..." - sudo apt update - sudo apt install -y python3-evdev python3-uinput xdotool wmctrl flatpak - - echo "[+] Création du script Python watcher..." - mkdir -p "$(dirname "$PYTHON_SCRIPT")" - cat > "$PYTHON_SCRIPT" <<'EOF' #!/usr/bin/env python3 import evdev import uinput @@ -23,7 +8,7 @@ import os DEVICE_NAMES = ["8BitDo NGC Modkit"] BUTTON_MAP = { "304": "KEY_ENTER", - "305": "KEY_ESC", + "305": None, "311": "KEY_LEFTALT", "307": "KEY_RIGHT", "308": "KEY_LEFT", @@ -54,14 +39,35 @@ def is_dolphin_running(): 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: - key_code = str(event.code) + 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: - keyboard.emit(getattr(uinput, BUTTON_MAP[key_code]), event.value) + 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 @@ -95,55 +101,3 @@ def main(): if __name__ == "__main__": main() -EOF - - chmod +x "$PYTHON_SCRIPT" - - echo "[+] Création du service systemd utilisateur..." - mkdir -p "$SERVICE_DIR" - cat > "$SERVICE_FILE" < "$PYTHON_SCRIPT" <<'EOF' -#!/usr/bin/env python3 -import evdev -import uinput -import subprocess -import time - -DEVICE_NAMES = ["8BitDo NGC Modkit"] -AXIS_MAP = { - "ABS_X": ("KEY_LEFT", "KEY_RIGHT"), - "ABS_Y": ("KEY_UP", "KEY_DOWN") -} -BUTTON_MAP = { - "304": "KEY_ENTER", - "305": "KEY_ESC", - "311": "KEY_LEFTALT", - "308": "KEY_TAB", - "310": "KEY_LEFTCTRL", - "307": "KEY_LEFTSHIFT", - "314": "KEY_T" -} -THRESHOLD = 100 -DOLPHIN_NAME = "Dolphin" - -events = [ - uinput.KEY_UP, uinput.KEY_DOWN, uinput.KEY_LEFT, uinput.KEY_RIGHT, - uinput.KEY_ENTER, uinput.KEY_ESC, uinput.KEY_LEFTALT, uinput.KEY_TAB, - uinput.KEY_LEFTCTRL, uinput.KEY_LEFTSHIFT, uinput.KEY_T -] -keyboard = uinput.Device(events) - -def find_device(): - devices = [] - for path in evdev.list_devices(): - try: - dev = evdev.InputDevice(path) - devices.append((path, dev.name)) - except Exception: - continue - for path, name in devices: - if any(dn in name for dn in DEVICE_NAMES): - return path - return None - -def handle_device(dev_path): - dev = evdev.InputDevice(dev_path) - abs_center = {axis: dev.absinfo(axis).value for axis in (evdev.ecodes.ABS_X, evdev.ecodes.ABS_Y)} - try: - for event in dev.read_loop(): - if event.type == evdev.ecodes.EV_KEY: - key_code = str(event.code) - if key_code in BUTTON_MAP: - keyboard.emit(getattr(uinput, BUTTON_MAP[key_code]), event.value) - if event.type == evdev.ecodes.EV_ABS: - axis = evdev.ecodes.ABS[event.code] - val = event.value - neg_key, pos_key = AXIS_MAP.get(axis, (None, None)) - if neg_key and pos_key: - keyboard.emit(getattr(uinput, neg_key), int(val < abs_center[event.code]-THRESHOLD)) - keyboard.emit(getattr(uinput, pos_key), int(val > abs_center[event.code]+THRESHOLD)) - except OSError: - dev.close() - raise - -def main(): - connected = False - while True: - dev_path = find_device() - if dev_path and not connected: - print(f"[+] Manette connectée: {dev_path}") - connected = True - try: - subprocess.Popen(["flatpak", "run", "org.DolphinEmu.dolphin-emu"]) # ou juste ["ma_commande"] si pas d'arguments - except Exception as e: - print(f"[!] Impossible de lancer la commande: {e}") - try: - handle_device(dev_path) - except OSError: - print("[-] Manette déconnectée") - connected = False - try: - subprocess.Popen(["flatpak", "kill", "org.DolphinEmu.dolphin-emu"]) # ou juste ["ma_commande"] si pas d'arguments - except Exception as e: - print(f"[!] Impossible de lancer la commande: {e}") - elif not dev_path and connected: - print("[-] Manette déconnectée") - connected = False - time.sleep(1) - -if __name__=="__main__": - main() -EOF - - chmod +x "$PYTHON_SCRIPT" - - echo "[+] Création du service systemd..." - sudo tee "$SERVICE_PATH" > /dev/null <