user service works
This commit is contained in:
@@ -1,25 +1,18 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import evdev
|
import evdev
|
||||||
import uinput
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
|
||||||
DEVICE_NAMES = ["8BitDo NGC Modkit"]
|
DEVICE_NAMES = ["8BitDo NGC Modkit"]
|
||||||
BUTTON_MAP = {
|
BUTTON_MAP = {
|
||||||
"304": "KEY_ENTER", # A
|
"304": "Return", # A
|
||||||
"305": None, # B (non utilisé)
|
"305": None, # B (non utilisé)
|
||||||
"311": "KEY_LEFTALT", # R
|
"311": "Alt_L", # R
|
||||||
"307": "KEY_RIGHT", # Stick droit → Droite
|
"307": "Right", # X
|
||||||
"308": "KEY_LEFT", # Stick droit → Gauche
|
"308": "Left", # Y
|
||||||
# 318 (BTN_THUMBR) sera géré à part
|
# 318 (BTN_THUMBR) sera géré à part
|
||||||
}
|
}
|
||||||
|
|
||||||
events = [
|
|
||||||
uinput.KEY_LEFT, uinput.KEY_RIGHT,
|
|
||||||
uinput.KEY_ENTER, uinput.KEY_ESC, uinput.KEY_LEFTALT
|
|
||||||
]
|
|
||||||
keyboard = uinput.Device(events)
|
|
||||||
|
|
||||||
def find_device():
|
def find_device():
|
||||||
for path in evdev.list_devices():
|
for path in evdev.list_devices():
|
||||||
try:
|
try:
|
||||||
@@ -32,13 +25,16 @@ def find_device():
|
|||||||
|
|
||||||
def is_dolphin_running():
|
def is_dolphin_running():
|
||||||
try:
|
try:
|
||||||
output = subprocess.check_output(
|
output = subprocess.check_output(["flatpak", "ps"]).decode()
|
||||||
["flatpak", "ps"]
|
|
||||||
).decode()
|
|
||||||
return "org.DolphinEmu.dolphin-emu" in output
|
return "org.DolphinEmu.dolphin-emu" in output
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def press_key(key, press):
|
||||||
|
# press=True -> key down, press=False -> key up
|
||||||
|
action = "keydown" if press else "keyup"
|
||||||
|
subprocess.run(["xdotool", action, key])
|
||||||
|
|
||||||
def handle_device(dev_path):
|
def handle_device(dev_path):
|
||||||
dev = evdev.InputDevice(dev_path)
|
dev = evdev.InputDevice(dev_path)
|
||||||
try:
|
try:
|
||||||
@@ -50,19 +46,18 @@ def handle_device(dev_path):
|
|||||||
|
|
||||||
# cas spécial : joystick jaune (BTN_THUMBR, code 318)
|
# cas spécial : joystick jaune (BTN_THUMBR, code 318)
|
||||||
if key_code == "318" and event.value == 1: # pression
|
if key_code == "318" and event.value == 1: # pression
|
||||||
keyboard.emit(uinput.KEY_LEFTALT, 1)
|
press_key("Alt_L", True)
|
||||||
time.sleep(0.02)
|
time.sleep(0.02)
|
||||||
keyboard.emit(uinput.KEY_ESC, 1)
|
press_key("Escape", True)
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
keyboard.emit(uinput.KEY_ESC, 0)
|
press_key("Escape", False)
|
||||||
keyboard.emit(uinput.KEY_LEFTALT, 0)
|
press_key("Alt_L", False)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# touches normales selon le mapping
|
|
||||||
if key_code in BUTTON_MAP:
|
if key_code in BUTTON_MAP:
|
||||||
mapped_key = BUTTON_MAP[key_code]
|
mapped_key = BUTTON_MAP[key_code]
|
||||||
if mapped_key is not None:
|
if mapped_key is not None:
|
||||||
keyboard.emit(getattr(uinput, mapped_key), event.value)
|
press_key(mapped_key, event.value == 1)
|
||||||
|
|
||||||
except OSError:
|
except OSError:
|
||||||
dev.close()
|
dev.close()
|
||||||
@@ -70,7 +65,7 @@ def handle_device(dev_path):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
connected = False
|
connected = False
|
||||||
time.sleep(5) # délai pour que uinput et le serveur graphique soient prêts
|
time.sleep(5) # délai pour que le serveur graphique soit prêt
|
||||||
while True:
|
while True:
|
||||||
dev_path = find_device()
|
dev_path = find_device()
|
||||||
if dev_path and not connected:
|
if dev_path and not connected:
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Pad Dolphin Watcher Service (User)
|
Description=Pad Dolphin Watcher Service (User)
|
||||||
After=graphical.target dev-uinput.device
|
After=graphical.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStartPre=/bin/sleep 10
|
Type=simple
|
||||||
ExecStart=/home/$USER/.local/bin/gamecube-pad.py
|
ExecStart=/usr/bin/python3 /home/%u/.local/bin/gamecube-pad.py
|
||||||
WorkingDirectory=/home/$USER
|
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=3
|
RestartSec=3
|
||||||
StandardOutput=journal+console
|
WorkingDirectory=/home/%u
|
||||||
StandardError=journal+console
|
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=default.target
|
WantedBy=default.target
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
SERVICE_NAME="gamecube-pad.service"
|
SERVICE_NAME="gamecube-pad.service"
|
||||||
SERVICE_PATH="/etc/systemd/system/$SERVICE_NAME"
|
SERVICE_PATH="$HOME/.config/systemd/user/gamecube-pad.service"
|
||||||
PYTHON_SCRIPT="/home/$USER/.local/bin/gamecube-pad.py"
|
PYTHON_SCRIPT="/home/$USER/.local/bin/gamecube-pad.py"
|
||||||
|
|
||||||
install_service() {
|
install_service() {
|
||||||
@@ -14,25 +14,25 @@ install_service() {
|
|||||||
cp gamecube-pad.py "$PYTHON_SCRIPT"
|
cp gamecube-pad.py "$PYTHON_SCRIPT"
|
||||||
chmod +x "$PYTHON_SCRIPT"
|
chmod +x "$PYTHON_SCRIPT"
|
||||||
|
|
||||||
echo "[+] Copie du fichier systemd..."
|
echo "[+] Copie du fichier systemd utilisateur..."
|
||||||
sudo cp gamecube-pad.service "$SERVICE_PATH"
|
mkdir -p "$HOME/.config/systemd/user"
|
||||||
sudo sed -i "s|\$USER|$USER|g" "$SERVICE_PATH"
|
cp gamecube-pad.service "$SERVICE_PATH"
|
||||||
|
|
||||||
echo "[+] Activation du service..."
|
echo "[+] Activation du service..."
|
||||||
sudo systemctl daemon-reload
|
systemctl --user daemon-reload
|
||||||
sudo systemctl enable "$SERVICE_NAME"
|
systemctl --user enable gamecube-pad.service
|
||||||
sudo systemctl restart "$SERVICE_NAME"
|
systemctl --user restart gamecube-pad.service
|
||||||
|
|
||||||
echo "[✓] Installation terminée. Consultez les logs avec : journalctl -u $SERVICE_NAME -f"
|
echo "[✓] Installation terminée. Consultez les logs avec : journalctl --user -u gamecube-pad.service -f"
|
||||||
}
|
}
|
||||||
|
|
||||||
uninstall_service() {
|
uninstall_service() {
|
||||||
echo "[+] Arrêt et suppression du service..."
|
echo "[+] Arrêt et suppression du service..."
|
||||||
sudo systemctl stop "$SERVICE_NAME" || true
|
systemctl --user stop gamecube-pad.service || true
|
||||||
sudo systemctl disable "$SERVICE_NAME" || true
|
systemctl --user disable gamecube-pad.service || true
|
||||||
sudo rm -f "$SERVICE_PATH"
|
rm -f "$SERVICE_PATH"
|
||||||
sudo systemctl daemon-reload
|
|
||||||
rm -f "$PYTHON_SCRIPT"
|
rm -f "$PYTHON_SCRIPT"
|
||||||
|
systemctl --user daemon-reload
|
||||||
echo "[✓] Désinstallation terminée."
|
echo "[✓] Désinstallation terminée."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user