#!/bin/bash # on-usb-role-change.sh — Called by udev on USB role/connect events. # Re-detects role, stops/restarts Pd with appropriate config. set -euo pipefail ROLE_FILE="/run/usb-role" LOG_TAG="[USB-HOTPLUG]" # Debounce: skip if we just ran (within 3s) LOCK="/tmp/usb-role-change.lock" if [ -f "$LOCK" ]; then age=$(($(date +%s) - $(stat -c %Y "$LOCK" 2>/dev/null || echo 0))) [ "$age" -lt 3 ] && exit 0 fi touch "$LOCK" logger -t "$LOG_TAG" "USB role change event — re-detecting..." /usr/local/bin/detect-usb-role.sh # Restart Pd if it's running if systemctl is-active pd-synth-auto &>/dev/null; then logger -t "$LOG_TAG" "Restarting Pd with new role" systemctl restart pd-synth-auto fi