dots

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

monitor.sh (1608B)


      1 #!/bin/sh
      2 #
      3 # monitor
      4 
      5 ICON=''
      6 # brightness vcp code is 10
      7 VCP_CODE=10
      8 # cache used b/c ddcutil getvcp too expensive
      9 BRIGHTNESS="${XDG_CACHE_HOME:-${HOME}/.cache/}/dots/bar/brightness"
     10 
     11 get_brightness() {
     12     [ ! -f "${BRIGHTNESS}" ] && return 1
     13     # brightness cache should contain only number
     14     ! brightness=$(grep '^[0-9][0-9]*$' "${BRIGHTNESS}") && return
     15 
     16 }
     17 
     18 set_brightness() {
     19     get_brightness
     20 
     21     # keep between 0-100
     22     if [ "${1}" = '+' ]; then
     23         if [ "${brightness}" -lt 100 ]; then
     24             brightness=$((brightness + ${2}))
     25             [ "${brightness}" -gt 100 ] && brightness=100
     26         else return
     27         fi
     28     elif [ "${1}" = '-' ]; then
     29         if [ "${brightness}" -gt 0 ]; then
     30             brightness=$((brightness - ${2}))
     31             [ "${brightness}" -lt 0 ] && brightness=0
     32         else return
     33         fi
     34     fi
     35 
     36     # set brightness using ddcutil
     37     doas ddcutil setvcp ${VCP_CODE} "${brightness}" >/dev/null 2>&1
     38 
     39     env HERBE_ID=/0 herbe "Brightness: ${brightness}%" &
     40 
     41     # get actual brightness and send output to cache
     42     printf '%s\n' "$(doas ddcutil getvcp ${VCP_CODE} | awk '{ print $9 }' | tr -d '[:punct:]' 2>/dev/null)" > "${BRIGHTNESS}"
     43 }
     44 
     45 bar() {
     46     get_brightness
     47     printf '%s\n' "${ICON} ${brightness}%"
     48 }
     49 
     50 main() {
     51     # called from bar
     52     [ ${#} -eq 0 ] && bar
     53 
     54     # bar usage
     55     case ${BLOCK_BUTTON} in
     56         4) set_brightness + 25 & ;;
     57         5) set_brightness - 25 & ;;
     58     esac
     59 
     60     # modify monitor brightness based on args
     61     # ${1} = +/- and ${2} = percentage
     62     [ "${*}" ] && set_brightness "${1}" "${2}" &
     63 }
     64 
     65 main "${@}"