Created
September 15, 2024 15:10
-
-
Save JohnnyonFlame/b660e847e04d174f81abd1dd4fe4b4a8 to your computer and use it in GitHub Desktop.
Simple PipeWire AutoSwitcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -x | |
# Simple PipeWire AutoSwitcher | |
# LICENSE: BSD-0 | |
# Copyright (C) 2024 by JohnnyonFlame | |
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. | |
# # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, | |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF | |
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
function use_device() | |
{ | |
[[ "$1" =~ .*/usb[0-9]+/.* ]] && return 0 | |
return 1 | |
} | |
udevadm monitor --udev --subsystem-match=sound | while read -r line; do | |
if echo "$line" | grep -q "change"; then | |
# Ignore non-usb devices | |
use_device "$line" || continue | |
# Query for the device's alsa id | |
devpath=$(echo "$line" | awk '{ print $4; }') | |
card=$(basename "$devpath") | |
id=$(echo "$card" | sed 's/card//') | |
[ "$card" == "" ] && continue | |
# Query for the PipeWire ID for the device's node | |
pwid=$(pw-dump | jq --argjson card "$id" 'first(.[] | select(.type == "PipeWire:Interface:Node" and .info.props["alsa.card"] == $card)).id') | |
[ "$pwid" == "" ] && continue | |
# Set the node as default | |
wpctl set-default "$pwid" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment