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.

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