Dotfiles for my tiling window manager + terminal workflow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.2 KiB

6 years ago
6 years ago
  1. #!/bin/bash
  2. # Displays the default device, volume, and mute status for i3blocks
  3. # default vars {{{ #
  4. set -a
  5. SINK_SYMB=""
  6. AUDIO_HIGH_SYMBOL=${AUDIO_HIGH_SYMBOL:-''}
  7. AUDIO_MED_THRESH=${AUDIO_MED_THRESH:-50}
  8. AUDIO_MED_SYMBOL=${AUDIO_MED_SYMBOL:-''}
  9. AUDIO_LOW_THRESH=${AUDIO_LOW_THRESH:-0}
  10. AUDIO_LOW_SYMBOL=${AUDIO_LOW_SYMBOL:-''}
  11. AUDIO_MUTED_SYMBOL=${AUDIO_MUTED_SYMBOL:-''}
  12. . "/home/kevin/.cache/wal/colors.sh"
  13. # DEFAULT_COLOR=${DEFAULT_COLOR:-"#ffffff"}
  14. DEFAULT_COLOR="$color7"
  15. MUTED_COLOR=${MUTED_COLOR:-"#a0a0a0"}
  16. USE_ALSA_NAME=${USE_ALSA_NAME:-0}
  17. USE_DESCRIPTION=${USE_DESCRIPTION:-0}
  18. SUBSCRIBE=${SUBSCRIBE:-0}
  19. MIXER=${MIXER:-""}
  20. SCONTROL=${SCONTROL:-""}
  21. # }}} default vars #
  22. AUDIO_DELTA=${AUDIO_DELTA:-5}
  23. # LONG_FORMAT=${LONG_FORMAT:-'${SYMB} ${VOL}% [${INDEX}:${NAME}]'}
  24. LONG_FORMAT=${SHORT_FORMAT:-'${SINK_SYMB} ${VOL}%'}
  25. # SHORT_FORMAT=${SHORT_FORMAT:-'${SYMB} ${VOL}%[${INDEX}]'}
  26. SHORT_FORMAT=${SHORT_FORMAT:-'${SINK_SYMB} ${VOL}%'}
  27. # flags {{{ #
  28. while getopts F:Sf:adH:M:L:X:T:t:C:c:i:m:s:h opt; do
  29. case "$opt" in
  30. S) SUBSCRIBE=1 ;;
  31. F) LONG_FORMAT="$OPTARG" ;;
  32. f) SHORT_FORMAT="$OPTARG" ;;
  33. a) USE_ALSA_NAME=1 ;;
  34. d) USE_DESCRIPTION=1 ;;
  35. H) AUDIO_HIGH_SYMBOL="$OPTARG" ;;
  36. M) AUDIO_MED_SYMBOL="$OPTARG" ;;
  37. L) AUDIO_LOW_SYMBOL="$OPTARG" ;;
  38. X) AUDIO_MUTED_SYMBOL="$OPTARG" ;;
  39. T) AUDIO_MED_THRESH="$OPTARG" ;;
  40. t) AUDIO_LOW_THRESH="$OPTARG" ;;
  41. C) DEFAULT_COLOR="$OPTARG" ;;
  42. c) MUTED_COLOR="$OPTARG" ;;
  43. i) AUDIO_INTERVAL="$OPTARG" ;;
  44. m) MIXER="$OPTARG" ;;
  45. s) SCONTROL="$OPTARG" ;;
  46. h) printf \
  47. "Usage: volume-pulseaudio [-S] [-F format] [-f format] [-p] [-a|-d] [-H symb] [-M symb]
  48. [-L symb] [-X symb] [-T thresh] [-t thresh] [-C color] [-c color] [-i inter]
  49. [-m mixer] [-s scontrol] [-h]
  50. Options:
  51. -F, -f\tOutput format (-F long format, -f short format) to use, with exposed variables:
  52. \${SYMB}, \${VOL}, \${INDEX}, \${NAME}
  53. -S\tSubscribe to volume events (requires persistent block, always uses long format)
  54. -a\tUse ALSA name if possible
  55. -d\tUse device description instead of name if possible
  56. -H\tSymbol to use when audio level is high. Default: '$AUDIO_HIGH_SYMBOL'
  57. -M\tSymbol to use when audio level is medium. Default: '$AUDIO_MED_SYMBOL'
  58. -L\tSymbol to use when audio level is low. Default: '$AUDIO_LOW_SYMBOL'
  59. -X\tSymbol to use when audio is muted. Default: '$AUDIO_MUTED_SYMBOL'
  60. -T\tThreshold for medium audio level. Default: $AUDIO_MED_THRESH
  61. -t\tThreshold for low audio level. Default: $AUDIO_LOW_THRESH
  62. -C\tColor for non-muted audio. Default: $DEFAULT_COLOR
  63. -c\tColor for muted audio. Default: $MUTED_COLOR
  64. -i\tInterval size of volume increase/decrease. Default: $AUDIO_DELTA
  65. -m\tUse the given mixer.
  66. -s\tUse the given scontrol.
  67. -h\tShow this help text
  68. " && exit 0;;
  69. esac
  70. done
  71. # }}} flags #
  72. # functions {{{ #
  73. if [[ -z "$MIXER" ]] ; then
  74. MIXER="default"
  75. if amixer -D pulse info >/dev/null 2>&1 ; then
  76. MIXER="pulse"
  77. fi
  78. fi
  79. if [[ -z "$SCONTROL" ]] ; then
  80. SCONTROL=$(amixer -D "$MIXER" scontrols | sed -n "s/Simple mixer control '\([^']*\)',0/\1/p" | head -n1)
  81. fi
  82. CAPABILITY=$(amixer -D $MIXER get $SCONTROL | sed -n "s/ Capabilities:.*cvolume.*/Capture/p")
  83. function move_sinks_to_new_default {
  84. DEFAULT_SINK=$1
  85. pacmd list-sink-inputs | grep index: | grep -o '[0-9]\+' | while read SINK
  86. do
  87. pacmd move-sink-input $SINK $DEFAULT_SINK
  88. done
  89. }
  90. function set_default_playback_device_next {
  91. inc=${1:-1}
  92. num_devices=$(pacmd list-sinks | grep -c index:)
  93. sink_arr=($(pacmd list-sinks | grep index: | grep -o '[0-9]\+'))
  94. default_sink_index=$(( $(pacmd list-sinks | grep index: | grep -no '*' | grep -o '^[0-9]\+') - 1 ))
  95. default_sink_index=$(( ($default_sink_index + $num_devices + $inc) % $num_devices ))
  96. default_sink=${sink_arr[$default_sink_index]}
  97. pacmd set-default-sink $default_sink
  98. move_sinks_to_new_default $default_sink
  99. }
  100. # }}} functions #
  101. # triggers {{{ #
  102. case "$BLOCK_BUTTON" in
  103. 1) set_default_playback_device_next ;;
  104. 2) amixer -q -D $MIXER sset $SCONTROL $CAPABILITY toggle ;;
  105. 3) set_default_playback_device_next -1 ;;
  106. 4) amixer -q -D $MIXER sset $SCONTROL $CAPABILITY $AUDIO_DELTA%+ ;;
  107. 5) amixer -q -D $MIXER sset $SCONTROL $CAPABILITY $AUDIO_DELTA%- ;;
  108. esac
  109. # }}} triggers #
  110. # print_format {{{ #
  111. function print_format {
  112. # echo $NAME
  113. case $NAME in
  114. *"USB"*)
  115. SINK_SYMB=""
  116. ;;
  117. *"hdmi"*)
  118. SINK_SYMB=""
  119. ;;
  120. *"pci"*)
  121. SINK_SYMB=""
  122. ;;
  123. esac
  124. # echo "$1" | envsubst '${SYMB}${VOL}${INDEX}${NAME}'
  125. echo "$1" | envsubst '${SINK_SYMB}${VOL}${INDEX}${NAME}'
  126. }
  127. # }}} print_format #
  128. # print_block {{{ #
  129. function print_block {
  130. for name in INDEX NAME VOL MUTED; do
  131. read $name
  132. done < <(pacmd list-sinks | grep "index:\|name:\|volume: front\|muted:" | grep -A3 '*')
  133. INDEX=$(echo "$INDEX" | grep -o '[0-9]\+')
  134. VOL=$(echo "$VOL" | grep -o "[0-9]*%" | head -1 )
  135. VOL="${VOL%?}"
  136. NAME=$(echo "$NAME")
  137. # NAME=$(echo "$NAME" | sed \
  138. # 's/.*<.*\.\(.*\)>.*/\1/; t;'\
  139. # 's/.*<\(.*\)>.*/\1/; t;'\
  140. # 's/.*/unknown/')
  141. # NAME=$(echo "$NAME" | sed \
  142. # 's/.*<.*\.\(.*\)\..*>.*/\1/; t;')
  143. if [[ $USE_ALSA_NAME == 1 ]] ; then
  144. ALSA_NAME=$(pacmd list-sinks |\
  145. awk '/^\s*\*/{f=1}/^\s*index:/{f=0}f' |\
  146. grep "alsa.name\|alsa.mixer_name" |\
  147. head -n1 |\
  148. sed 's/.*= "\(.*\)".*/\1/')
  149. NAME=${ALSA_NAME:-$NAME}
  150. elif [[ $USE_DESCRIPTION == 1 ]] ; then
  151. DESCRIPTION=$(pacmd list-sinks |\
  152. awk '/^\s*\*/{f=1}/^\s*index:/{f=0}f' |\
  153. grep "device.description" |\
  154. head -n1 |\
  155. sed 's/.*= "\(.*\)".*/\1/')
  156. NAME=${DESCRIPTION:-$NAME}
  157. fi
  158. if [[ $MUTED =~ "no" ]] ; then
  159. SYMB=$AUDIO_HIGH_SYMBOL
  160. [[ $VOL -le $AUDIO_MED_THRESH ]] && SYMB=$AUDIO_MED_SYMBOL
  161. [[ $VOL -le $AUDIO_LOW_THRESH ]] && SYMB=$AUDIO_LOW_SYMBOL
  162. COLOR=$DEFAULT_COLOR
  163. else
  164. SYMB=$AUDIO_MUTED_SYMBOL
  165. COLOR=$MUTED_COLOR
  166. fi
  167. if [[ $SUBSCRIBE == 1 ]] ; then
  168. print_format "$LONG_FORMAT"
  169. else
  170. print_format "$LONG_FORMAT"
  171. print_format "$SHORT_FORMAT"
  172. echo "$COLOR"
  173. fi
  174. }
  175. # }}} print_block #
  176. print_block
  177. if [[ $SUBSCRIBE == 1 ]] ; then
  178. while read -r EVENT; do
  179. print_block
  180. done < <(pactl subscribe | stdbuf -oL grep change)
  181. fi