#!/bin/bash # Script: pulseaudioselector.sh # Task: Provide a zenity GUI to select sinks and sources for Audio # global variables SCRIPTNAME=$(basename ${0} .sh) EXIT_SUCCESS=0 EXIT_FAILURE=1 EXIT_ERROR=2 EXIT_BUG=10 # variables for option switches with default values SINKORSOURCE="both" # functions function usage { echo "${SCRIPTNAME} lets you select an input and output device" >&2 echo "for the PulseAudio audiosystem. Unfortunately, the GUI" >&2 echo "padevchooser doesn't have that functionality..." >&2 echo >&2 echo "Usage: ${SCRIPTNAME} [-o (both|sink|source)]" >&2 [[ ${#} -eq 1 ]] && exit ${1} || exit ${EXIT_FAILURE} } # Option -o takes one argument, either "both", "sink" or "source" and # defines what io device to change. while getopts 'o:' OPTION ; do case ${OPTION} in o) SINKORSOURCE="${OPTARG}" if [ "${SINKORSOURCE}" != "both" -a "${SINKORSOURCE}" != "sink" -a "${SINKORSOURCE}" != "source" ] ; then usage ${EXIT_ERROR} fi ;; h) usage ${EXIT_SUCCESS} ;; \?) echo "unknown option \"-${OPTARG}\"." >&2 usage ${EXIT_ERROR} ;; :) echo "option \"-${OPTARG}\" requires an argument." >&2 usage ${EXIT_ERROR} ;; *) echo "Impossible error. parameter: ${OPTION}" >&2 usage ${EXIT_BUG} ;; esac done if [ "${SINKORSOURCE}" == "both" ] ; then # Call ourselves twice $0 -o sink $0 -o source exit ${EXIT_SUCCESS} fi # set language to C so that we get parsable output export LANG=C tmpdir="`mktemp -d`" if [ -z "${tmpdir}" ] ; then zenity --error --text='Could not create a temporary directory!' exit ${EXIT_ERROR} fi trap "rm -f \${tmpdir}/*; rmdir \${tmpdir}" EXIT unset name description devices while IFS=: read key value ; do if [ "${key}" == "index" -o "${key}" == "* index" ] ; then if [ -n "${name}" -a -n "${description}" ]; then devices="${devices}${devices:+ }${active} \"${description}\" \"${name}\"" fi active="FALSE" [ "${key:0:1}" == "*" ] && active="TRUE" unset name description fi if [ "${key}" == "name" ] ; then name="${value}" name="${name##*<}" name="${name%%>*}" name="${name#\"}" name="${name%\"}" fi if [ "${key}" == "device.description" ] ; then description="${value}" description="${description#\"}" description="${description%\"}" fi done < <( pacmd list-${SINKORSOURCE}s | sed -e 's,^[ \t]\+,,g' -e 's, = ,:,' ; echo -e "\nindex: -1" ) io="output" [ "${SINKORSOURCE}" == "source" ] && io="input" eval zenity --list '--title="Please select ${io} device"' --radiolist '--text="Please select ${io} device"' \ --column '" "' --column '"Description"' --column '" "' --hide-column 3 --print-column "ALL" \ --height 400 --width 800 ${devices} > ${tmpdir}/newdevice IFS=\| read desc newdevice < ${tmpdir}/newdevice # User pressed Cancel [ -z "${newdevice}" ] && exit ${EXIT_FAILURE} ( echo "set-default-${SINKORSOURCE} ${newdevice}" ) | pacmd 2>&1 > ${tmpdir}/returnmessage if [ ${?} -eq 0 ] ; then zenity --info --text "New ${io} device: ${desc}" else zenity --error < ${tmpdir}/returnmessage fi exit ${EXIT_SUCCESS}