mmswitch-sound (2714B)
1 #!/bin/sh 2 3 DMENU_ARGS='-nb #151515 -nf #787878 -sb #151515 -sf #a78edb -fn -*-*-medium-r-normal-*-*-*-*-*-*-80-*-*' 4 5 die() { 6 echo "$(basename $0): Error: $1" >&2 7 exit 1 8 } 9 10 list_sinks() { 11 pacmd list-sinks | grep -E "index: |name: |device.description = " | paste - - - | \ 12 tr '*' ' ' | tr '<>' '"' | sed 's/device.description =/description:/' | \ 13 sed -e 's/[ \t][ \t]*/ /g' | sed -e 's/^ //' 14 } 15 16 list_sink_inputs() { 17 pacmd list-sink-inputs | grep 'index: ' | awk '{print $2}' 18 } 19 20 get_index_from_desc() { 21 list_sinks | grep "description: \"$1\"" | awk '{print $2}' 22 } 23 24 get_index_from_name() { 25 list_sinks | grep " name: \"$1\" " | awk '{print $2}' 26 } 27 28 get_desc_from_index() { 29 list_sinks | grep -E "^index: $1 " | sed 's/.* description: \"\(.*\)\"$/\1/' 30 } 31 32 get_name_from_index() { 33 list_sinks | grep -E "^index: $1 " | sed 's/.* name: \"\(.*\)\" .*$/\1/' 34 } 35 36 set_sink_from_index() { 37 local x 38 pacmd set-default-sink $1 > /dev/null 2>&1 39 for x in $(list_sink_inputs); do 40 pacmd move-sink-input $x $1 > /dev/null 2>&1 41 done 42 } 43 44 usage() { 45 echo "Usage $(basename $0) [options] index:NUMBER | name:NAME | desc:DESCRIPTION" 46 echo 47 echo "Options:" 48 echo " -h, --help Show this help menu" 49 echo " -l, --list-cards List sound cards" 50 echo " --dmenu Use dmenu" 51 echo 52 echo "Examples:" 53 echo " $(basename $0) index:0" 54 echo " $(basename $0) name:$(get_name_from_index 0)" 55 echo " $(basename $0) desc:\"$(get_desc_from_index 0)\"" 56 } 57 58 ARGS=$(getopt -o lh -l "list-cards,dmenu,help" -n $(basename $0) -- "$@") 59 [ $? -ne 0 ] && exit 1 60 eval set -- "$ARGS" 61 62 while :; do 63 case "$1" in 64 -h|--help) 65 usage 66 exit 1 67 ;; 68 -l|--list-cards) 69 list_sinks 70 exit 0 71 ;; 72 --dmenu) 73 SELECT=$(list_sinks | sed 's/.* description: \"\(.*\)\"$/\1/' | \ 74 dmenu -i -p 'Choose sound card:' $DMENU_ARGS) 75 [ -z "$SELECT" ] && exit 0 76 ARG=desc:"$SELECT" 77 break 78 ;; 79 --) 80 shift 81 ARG="$@" 82 break 83 ;; 84 esac 85 done 86 87 if [ -z "$ARG" ]; then 88 usage 89 exit 1 90 fi 91 92 case "${ARG%%:*}" in 93 index) 94 [ $(list_sinks | grep -E -c "^index: ${ARG#*:} ") -eq 0 ] && \ 95 die "Sound card with index \`${ARG#*:}' does not exist." 96 set_sink_from_index "${ARG#*:}" 97 ;; 98 name) 99 INDEX=$(get_index_from_name "${ARG#*:}") 100 [ -z "$INDEX" ] && die "Sound card with name \`${ARG#*:}' does not exist." 101 set_sink_from_index $INDEX 102 ;; 103 desc) 104 INDEX=$(get_index_from_desc "${ARG#*:}") 105 [ -z "$INDEX" ] && die "Sound card with description \`${ARG#*:}' does not exist." 106 set_sink_from_index $INDEX 107 ;; 108 *) 109 die "You must specify if \`${ARG#*:}' is an index, a name or a description. See --help." 110 ;; 111 esac