-
Notifications
You must be signed in to change notification settings - Fork 3
/
jackman_udev_unplug
executable file
·76 lines (58 loc) · 3.48 KB
/
jackman_udev_unplug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=jackman
. /usr/bin/gettext.sh
/usr/bin/echo "--$(date) jackman_udev_unplug--" >> /var/log/jackman.log
jackuser=$(ps -p `pidof jackdbus | cut -d ' ' -f1` -o user=)
jackhome=$(eval /usr/bin/echo ~$jackuser)
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u $jackuser)/bus"
function jack_config {
line=$(grep -n "name=\"alsa\"" < $jackhome/.config/jack/conf.xml | grep -o -P '[0-9]+')
keys=$(tail -n +$line $jackhome/.config/jack/conf.xml | grep "option name" | grep "\"$1\"" | grep -o -P '(?<=name=")[^"]*')
values=$(tail -n +$line $jackhome/.config/jack/conf.xml | grep "option name" | grep "\"$1\"" | grep -o -P '(?<=>)[^<]*')
echo "$values"
}
if [[ ! -z $1 ]]; then
/usr/bin/echo "arg: $1" >> /var/log/jackman.log
CARD=$(grep -o -P '[0-9]*' <<< "$1" | head -1)
DISPLAY=":0"
fi
if [ ! -z $CARD ]; then
currentcard="$(jack_config device | grep -o -P '[0-9]' | head -1)"
currentdevice="$(jack_config device | grep -o -P '[0-9]' | tail -n +2 | head -1)"
currentname="$(grep "$currentcard \[" /proc/asound/cards | grep -o -P '[^-]*' | tail -1 | sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g')"
/usr/bin/echo "current card is ${currentcard},${currentdevice} ($currentname)."
if [[ $currentcard -eq $CARD ]]; then
/usr/bin/echo "need to switch master"
msg="`eval_gettext \"Removed card \\\${currentcard}. Trying to switch master...\"`"
su $jackuser - -c "DISPLAY=:0 /usr/bin/kdialog --passivepopup \"$msg\" 10 --title \"$(gettext "Audio Interface")\" --icon audio-card || \
/usr/bin/zenity --notification --text \"$msg\" --display \":0\" --title \"$(gettext "Audio Interface")\" --window-icon audio-card || \
/usr/bin/echo \"$msg\""
cns="$(su $jackuser - -c '/usr/bin/jack_lsp -c')"
IFS=';' read -r -a cons <<< "${cns//$'\n'/;}"
/usr/bin/su $jackuser - -c "/usr/bin/jack_control stop" &
if [[ $(grep audio <<< $(id $jackuser -Gn)) ]]; then
/usr/bin/su $jackuser - -g audio -c "DBUS_SESSION_BUS_ADDRESS=\"$DBUS_SESSION_BUS_ADDRESS\" /usr/bin/jackman -fb\"${currentname},${currentdevice}\"" >> /var/log/jackman.log 2>&1
else
/usr/bin/su $jackuser - -c "DBUS_SESSION_BUS_ADDRESS=\"$DBUS_SESSION_BUS_ADDRESS\" /usr/bin/jackman -fb\"${currentname},${currentdevice}\"" >> /var/log/jackman.log 2>&1
fi
for ((i=0; i<${#cons[@]}; i++)); do
firstchar=${cons[$i]:0:1}
cons[$i]=$(echo "${cons[$i]}" | sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g')
whitespace=$(printf '\n\t ')
case "$firstchar" in
*[!$whitespace]*) master="${cons[$i]}"
#/usr/bin/echo "master \"$master\"..."
;;
*) /usr/bin/su $jackuser - -c "/usr/bin/jack_connect \"${cons[$i]}\" \"$master\"">/dev/null 2>&1
#/usr/bin/echo "connecting \"${cons[$i]}\" to \"$master\"";;
esac
done
else
/usr/bin/echo "removing alsa_in/alsa_out for this card"
#only kill alsa_in and alsa_out for this card
for line in $(ps ax | grep -o -P "\s(\d+)(?= .*a[l]sa.*hw:$CARD)"); do
/usr/bin/kill -9 ${line} & disown
done
fi;
fi