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.

222 lines
6.3 KiB

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