gamecube pad V1
This commit is contained in:
@@ -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" <<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
|
||||
|
||||
15
gamecube-pad/gamecube-pad.service
Normal file
15
gamecube-pad/gamecube-pad.service
Normal file
@@ -0,0 +1,15 @@
|
||||
[Unit]
|
||||
Description=Pad Dolphin Watcher Service (User)
|
||||
After=graphical.target dev-uinput.device
|
||||
|
||||
[Service]
|
||||
ExecStartPre=/bin/sleep 10
|
||||
ExecStart=/home/$USER/.local/bin/gamecube-pad.py
|
||||
WorkingDirectory=/home/$USER
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
StandardOutput=journal+console
|
||||
StandardError=journal+console
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
@@ -1,154 +0,0 @@
|
||||
#!/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
|
||||
46
gamecube-pad/install.sh
Executable file
46
gamecube-pad/install.sh
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/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 install -y python3-evdev python3-uinput xdotool wmctrl
|
||||
|
||||
echo "[+] Copie du script Python..."
|
||||
mkdir -p "$(dirname "$PYTHON_SCRIPT")"
|
||||
cp gamecube-pad.py "$PYTHON_SCRIPT"
|
||||
chmod +x "$PYTHON_SCRIPT"
|
||||
|
||||
echo "[+] Copie du fichier systemd..."
|
||||
sudo cp gamecube-pad.service "$SERVICE_PATH"
|
||||
sudo sed -i "s|\$USER|$USER|g" "$SERVICE_PATH"
|
||||
|
||||
echo "[+] Activation du service..."
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable "$SERVICE_NAME"
|
||||
sudo systemctl restart "$SERVICE_NAME"
|
||||
|
||||
echo "[✓] Installation terminée. Consultez 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
|
||||
Reference in New Issue
Block a user