150 lines
3.9 KiB
Bash
150 lines
3.9 KiB
Bash
#!/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
|
|
import subprocess
|
|
import time
|
|
import os
|
|
|
|
DEVICE_NAMES = ["8BitDo NGC Modkit"]
|
|
BUTTON_MAP = {
|
|
"304": "KEY_ENTER",
|
|
"305": "KEY_ESC",
|
|
"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
|
|
|
|
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 key_code in BUTTON_MAP:
|
|
keyboard.emit(getattr(uinput, BUTTON_MAP[key_code]), 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()
|
|
EOF
|
|
|
|
chmod +x "$PYTHON_SCRIPT"
|
|
|
|
echo "[+] Création du service systemd utilisateur..."
|
|
mkdir -p "$SERVICE_DIR"
|
|
cat > "$SERVICE_FILE" <<EOF
|
|
[Unit]
|
|
Description=Pad Dolphin Watcher Service (User)
|
|
After=graphical.target dev-uinput.device
|
|
|
|
[Service]
|
|
ExecStartPre=/bin/sleep 10
|
|
ExecStart=$PYTHON_SCRIPT
|
|
WorkingDirectory=$HOME
|
|
Restart=always
|
|
RestartSec=3
|
|
StandardOutput=journal+console
|
|
StandardError=journal+console
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
EOF
|
|
|
|
echo "[+] Activation du service utilisateur..."
|
|
systemctl --user daemon-reload
|
|
systemctl --user enable gamecube-pad.service
|
|
systemctl --user start gamecube-pad.service
|
|
|
|
echo "[✓] Installation terminée."
|
|
echo "Consulte les logs avec : journalctl --user-unit gamecube-pad.service -f"
|
|
}
|
|
|
|
uninstall_service() {
|
|
echo "[+] Arrêt et suppression du service utilisateur..."
|
|
systemctl --user stop gamecube-pad.service || true
|
|
systemctl --user disable gamecube-pad.service || true
|
|
rm -f "$SERVICE_FILE"
|
|
rm -f "$PYTHON_SCRIPT"
|
|
systemctl --user daemon-reload
|
|
echo "[✓] Désinstallation terminée."
|
|
}
|
|
|
|
case "$1" in
|
|
uninstall)
|
|
uninstall_service
|
|
;;
|
|
*)
|
|
install_service
|
|
;;
|
|
esac
|
|
|