dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

vol.sh (2050B)


      1 #!/bin/sh
      2 #
      3 # vol
      4 
      5 VOLUME_HIGH_ICON=''
      6 VOLUME_MID_ICON=''
      7 VOLUME_LOW_ICON=''
      8 VOLUME_OFF_ICON=''
      9 VOLUME_MUTED_ICON=''
     10 
     11 get_scontrol() {
     12     # use PCM, which affects audio data played by software
     13     amixer scontrols | grep -iq 'PCM' && scontrol='PCM'
     14 }
     15 
     16 get_vol() { vol=$(amixer get "${scontrol}" -M | awk '{ for (i = 1; i<=NF; i++) { if ( $i ~ /\[[0-9][0-9]*%\]/ ) { print $i } } }' | head -n 1 | tr -d '[:punct:]') ; }
     17 
     18 toggle() {
     19     amixer set 'Master' toggle
     20 
     21     is_muted && msg='Volume muted' || msg='Volume unmuted'
     22 
     23     env HERBE_ID=/0 herbe "${msg}" &
     24 }
     25 
     26 # set volume linearly
     27 set_vol() {
     28     amixer set "${scontrol}" "${2}"%"${1}" -M
     29     get_vol
     30     env HERBE_ID=/0 herbe "Volume: ${vol}%" &
     31 }
     32 
     33 is_muted() {
     34     if amixer get 'Master' | sed 5q | grep -iq '\[off\]'; then
     35         return 0
     36     fi
     37 
     38     return 1
     39 }
     40 
     41 open() { "${TERMINAL}" -c "${MIXER}" -e "${MIXER}" ; }
     42 
     43 bar() {
     44     get_vol
     45 
     46     # check if muted
     47     is_muted && printf '%s\n' "${VOLUME_MUTED_ICON} ${vol}%" && return
     48 
     49     # volume 0 is off, 1-33 is low, 34-66 is med, 67-100 is high
     50     case ${vol} in
     51         0)                               printf '%s\n' "${VOLUME_OFF_ICON} ${vol}%" ;;
     52         [1-9]|1[0-9]|2[0-9]|3[0-3])      printf '%s\n' "${VOLUME_LOW_ICON} ${vol}%" ;;
     53         3[4-9]|4[0-9]|5[0-9]|6[0-6])     printf '%s\n' "${VOLUME_MID_ICON} ${vol}%" ;;
     54         6[7-9]|7[0-9]|8[0-9]|9[0-9]|100) printf '%s\n' "${VOLUME_HIGH_ICON} ${vol}%" ;;
     55     esac
     56 }
     57 
     58 main() {
     59     get_scontrol
     60 
     61     # called from bar
     62     [ ${#} -eq 0 ] && bar
     63 
     64     # bar usage
     65     case ${BLOCK_BUTTON} in
     66         1) open        ;;
     67         2) toggle      ;;
     68         4) set_vol + 1 ;;
     69         5) set_vol - 1 ;;
     70     esac
     71 
     72     while getopts 'ot' opt; do
     73         case "${opt}" in
     74             # open mixer
     75             o) open;   return ;;
     76             # toggle if t flag used
     77             t) toggle; return ;;
     78             *) return         ;;
     79         esac
     80     done
     81 
     82     # set volume based on args
     83     # ${1} = +/- and ${2} = percentage
     84     [ "${*}" ] && set_vol "${1}" "${2}"
     85 }
     86 
     87 main "${@}"