script v0.1

This commit is contained in:
valere
2025-09-25 07:35:52 +02:00
parent 21aa417aa6
commit f188344fa6
7 changed files with 404 additions and 0 deletions

View File

@@ -0,0 +1,154 @@
#!/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

View File

@@ -0,0 +1,149 @@
#!/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