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.

224 lines
6.4 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. *"Fiio"*)
  119. SINK_SYMB=""
  120. ;;
  121. *"hdmi"*)
  122. SINK_SYMB=""
  123. ;;
  124. *"pci"*)
  125. SINK_SYMB=""
  126. ;;
  127. esac
  128. # echo "$1" | envsubst '${SYMB}${VOL}${INDEX}${NAME}'
  129. echo "$1" | envsubst '${SINK_SYMB}${VOL}${INDEX}${NAME}'
  130. }
  131. # }}} print_format #
  132. # print_block {{{ #
  133. function print_block {
  134. for name in INDEX NAME VOL MUTED; do
  135. read $name
  136. done < <(pacmd list-sinks | grep "index:\|name:\|volume: front\|muted:" | grep -A3 '*')
  137. INDEX=$(echo "$INDEX" | grep -o '[0-9]\+')
  138. VOL=$(echo "$VOL" | grep -o "[0-9]*%" | head -1 )
  139. VOL="${VOL%?}"
  140. NAME=$(echo "$NAME")
  141. # NAME=$(echo "$NAME" | sed \
  142. # 's/.*<.*\.\(.*\)>.*/\1/; t;'\
  143. # 's/.*<\(.*\)>.*/\1/; t;'\
  144. # 's/.*/unknown/')
  145. # NAME=$(echo "$NAME" | sed \
  146. # 's/.*<.*\.\(.*\)\..*>.*/\1/; t;')
  147. if [[ $USE_ALSA_NAME == 1 ]] ; then
  148. ALSA_NAME=$(pacmd list-sinks |\
  149. awk '/^\s*\*/{f=1}/^\s*index:/{f=0}f' |\
  150. grep "alsa.name\|alsa.mixer_name" |\
  151. head -n1 |\
  152. sed 's/.*= "\(.*\)".*/\1/')
  153. NAME=${ALSA_NAME:-$NAME}
  154. elif [[ $USE_DESCRIPTION == 1 ]] ; then
  155. DESCRIPTION=$(pacmd list-sinks |\
  156. awk '/^\s*\*/{f=1}/^\s*index:/{f=0}f' |\
  157. grep "device.description" |\
  158. head -n1 |\
  159. sed 's/.*= "\(.*\)".*/\1/')
  160. NAME=${DESCRIPTION:-$NAME}
  161. fi
  162. if [[ $MUTED =~ "no" ]] ; then
  163. SYMB=$AUDIO_HIGH_SYMBOL
  164. [[ $VOL -le $AUDIO_MED_THRESH ]] && SYMB=$AUDIO_MED_SYMBOL
  165. [[ $VOL -le $AUDIO_LOW_THRESH ]] && SYMB=$AUDIO_LOW_SYMBOL
  166. COLOR=$DEFAULT_COLOR
  167. else
  168. SYMB=$AUDIO_MUTED_SYMBOL
  169. COLOR=$MUTED_COLOR
  170. fi
  171. if [[ $SUBSCRIBE == 1 ]] ; then
  172. print_format "$LONG_FORMAT"
  173. else
  174. print_format "$LONG_FORMAT"
  175. print_format "$SHORT_FORMAT"
  176. echo "$COLOR"
  177. fi
  178. }
  179. # }}} print_block #
  180. print_block
  181. if [[ $SUBSCRIBE == 1 ]] ; then
  182. while read -r EVENT; do
  183. print_block
  184. done < <(pactl subscribe | stdbuf -oL grep change)
  185. fi