Files
linux-env/gamecube-pad/gamecube-pad.sh
2025-09-25 07:35:52 +02:00

155 lines
4.4 KiB
Bash

#!/bin/bash
set -e
SERVICE_NAME="gamecube-pad.service"
SERVICE_PATH="/etc/systemd/system/$SERVICE_NAME"
PYTHON_SCRIPT="/home/$USER/.local/bin/gamecube-pad.py"
install_service() {
echo "[+] Installation des dépendances..."
sudo apt update
sudo apt install -y python3-evdev python3-uinput xdotool wmctrl
echo "[+] Création du script Python watcher..."
cat > "$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 <<EOF
[Unit]
Description=Pad Dolphin Watcher Service
After=graphical.target
[Service]
ExecStart=/usr/bin/python3 $PYTHON_SCRIPT
WorkingDirectory=/home/$USER
Restart=always
RestartSec=3
User=$USER
Group=$USER
StandardOutput=journal+console
StandardError=journal+console
[Install]
WantedBy=default.target
EOF
echo "[+] Activation du service..."
sudo systemctl daemon-reload
sudo systemctl enable "$SERVICE_NAME"
sudo systemctl restart "$SERVICE_NAME"
echo "[✓] Installation terminée. Consulte les logs avec : journalctl -u $SERVICE_NAME -f"
}
uninstall_service() {
echo "[+] Arrêt et suppression du service..."
sudo systemctl stop "$SERVICE_NAME" || true
sudo systemctl disable "$SERVICE_NAME" || true
sudo rm -f "$SERVICE_PATH"
sudo systemctl daemon-reload
rm -f "$PYTHON_SCRIPT"
echo "[✓] Désinstallation terminée."
}
case "$1" in
uninstall)
uninstall_service
;;
*)
install_service
;;
esac