
#!/bin/bash

# ============================================================
# Mouse Click Sounds
# Works on Wayland + X11
# Uses libinput for mouse events and paplay for audio.
# ============================================================

# Change these to whatever sounds you want.
LEFT_CLICK="/home/josh/battery_sounds/click_sounds/click.mp3"
RIGHT_CLICK="$HOME/.sounds/rclick.wav"

# ------------------------------------------------------------
# Checks
# ------------------------------------------------------------

if ! command -v paplay >/dev/null 2>&1; then
    echo "Error: paplay is not installed."
    echo "Install PulseAudio/PipeWire Pulse support for your distro."
    exit 1
fi

if ! command -v libinput >/dev/null 2>&1; then
    echo "Error: libinput is not installed."
    exit 1
fi

if [[ ! -f "$LEFT_CLICK" ]]; then
    echo "Error: left-click sound not found:"
    echo "  $LEFT_CLICK"
    exit 1
fi

if [[ ! -f "$RIGHT_CLICK" ]]; then
    echo "Error: right-click sound not found:"
    echo "  $RIGHT_CLICK"
    exit 1
fi

# ------------------------------------------------------------
# Start listener
# ------------------------------------------------------------

echo "Mouse click sounds are enabled."
echo "Left : $LEFT_CLICK"
echo "Right: $RIGHT_CLICK"
echo
echo "Press Ctrl+C to stop."

# libinput needs access to /dev/input/event*
# sudo applies only to libinput. The while-loop and paplay
# still run as your normal user.
sudo libinput debug-events |
while IFS= read -r line; do

    # Left mouse button pressed
    if [[ "$line" == *"POINTER_BUTTON"* &&
          "$line" == *"BTN_LEFT"* &&
          "$line" == *"pressed"* ]]; then

        pw-play "$LEFT_CLICK" >/dev/null 2>&1 &

    # Right mouse button pressed
    elif [[ "$line" == *"POINTER_BUTTON"* &&
            "$line" == *"BTN_RIGHT"* &&
            "$line" == *"pressed"* ]]; then

        pw-play "$RIGHT_CLICK" >/dev/null 2>&1 &
    fi

done
