I have written a small 'daemon' in bash that will switch to the headphones if they are detected, and if not, switch to an external USB speaker with PulseAudio.
What I'm looking for is some way to get notification of changes on the file /proc/asound/card0/codec#0
, just like inotifywait
does on real files (considering files under /proc to be as "pseudo-files").
I find my code a bit insane, because it runs sleep 1
with awk
for the whole day, that is 86400 times a day :)
while sleep 1; do
_1=${_2:-}
_2=$(awk '/Pin-ctls/{n++;if(n==4)print}' '/proc/asound/card0/codec#0')
[[ ${_1:-} = $_2 ]] ||
if [[ $_2 =~ OUT ]]; then
use_speakers
else
use_internal
fi
done
What I'm looking for is something like (this example doesn't work):
codec=/proc/asound/card0/codec#0
while inotifywait $codec; do
if [[ $(awk '/Pin-ctls/{n++;if(n==4)print}' $codec) =~ OUT ]]; then
use_speakers
else
use_internal
fi
done
This way the commands inside the loop would be run only when there are real changes on the $codec
file.
top
and GUI system monitors read a whole lot more than that from/proc
at short intervals. Of course, they probably do it much more efficiently as compiled executables, but the point is: polling for information is a common task./proc
, you can probably trigger your script with a udev rule, which would be pretty ideal. Less ideal is how tedious it can be coming up with udev rules ;)