#!/usr/bin/bash
# Toggle InputPlumber between gamepad mode and joystick→mouse mode.
# Both halves of the toggle need to set TWO things:
#   1. ProfilePath  — what events the source produces
#   2. TargetDevices — which virtual devices receive events
# (LoadProfilePath alone doesn't change targets; SetTargetDevices
#  alone keeps the previous mappings.)
set -eu
DEV=/org/shadowblip/InputPlumber/CompositeDevice0
SVC=org.shadowblip.InputPlumber
IFACE=org.shadowblip.Input.CompositeDevice

GAMEPAD=/usr/share/inputplumber/profiles/default.yaml
JOYMOUSE=/usr/share/inputplumber/profiles/pocketds-joymouse.yaml

cur=$(busctl get-property "$SVC" "$DEV" "$IFACE" ProfilePath 2>/dev/null \
      | sed -E 's/^s "(.*)"$/\1/')

case "$cur" in
    "$JOYMOUSE")
        # → gamepad
        busctl call "$SVC" "$DEV" "$IFACE" SetTargetDevices as 1 xbox-elite
        busctl call "$SVC" "$DEV" "$IFACE" LoadProfilePath s "$GAMEPAD"
        notify-send -u low "Pocket DS" "Gamepad mode" 2>/dev/null || true
        ;;
    *)
        # → joymouse
        busctl call "$SVC" "$DEV" "$IFACE" SetTargetDevices as 2 mouse keyboard
        busctl call "$SVC" "$DEV" "$IFACE" LoadProfilePath s "$JOYMOUSE"
        notify-send -u low "Pocket DS" "Joystick → mouse + keyboard" 2>/dev/null || true
        ;;
esac
