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.

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