#!/usr/bin/env bash
# Toggle touchpad on/off. Niri doesn't expose a direct IPC for this;
# we fall back to libinput xinput if available, or notify with instructions.

STATUS_FILE="$XDG_RUNTIME_DIR/touchpad.status"

TOUCHPAD_ID=$(xinput --list --id-only "$(xinput --list --name-only 2>/dev/null | grep -i touchpad | head -1)" 2>/dev/null || echo "")

enable_touchpad() {
    printf "true" > "$STATUS_FILE"
    notify-send -u normal "Touchpad Enabled"
    if [ -n "$TOUCHPAD_ID" ]; then
        xinput enable "$TOUCHPAD_ID"
    fi
}

disable_touchpad() {
    printf "false" > "$STATUS_FILE"
    notify-send -u normal "Touchpad Disabled"
    if [ -n "$TOUCHPAD_ID" ]; then
        xinput disable "$TOUCHPAD_ID"
    fi
}

if [ -z "$TOUCHPAD_ID" ]; then
    notify-send -u normal "Touchpad" "No touchpad found via xinput.\nAdjust device name in niri-toggle-touchpad.sh"
    exit 0
fi

if [ ! -f "$STATUS_FILE" ]; then
    enable_touchpad
else
    if [ "$(cat "$STATUS_FILE")" = "true" ]; then
        disable_touchpad
    else
        enable_touchpad
    fi
fi
