light.sh (2142B)
1 #!/bin/sh 2 # 3 # light 4 5 ICON='' 6 # intel_backlight for thinkpad at least 7 LIGHT='/sys/class/backlight/intel_backlight' 8 # 852 on thinkpad not sure if universal 9 MAX_BRIGHTNESS="$([ -f "${LIGHT}/max_brightness" ] && cat "${LIGHT}/max_brightness")" 10 11 get_brightness() { 12 # first get brightness as percentile of max brightness 13 brightness="$(cat "${LIGHT}/brightness")" 14 # then convert to percentage in terms of one hundred 15 brightness="$(printf '%s\n' "scale=10; (${brightness}/${MAX_BRIGHTNESS})*100" | bc)" 16 # dont care bout decimal 17 brightness="$(printf '%.0f\n' "${brightness}")" 18 } 19 20 set_brightness() { 21 get_brightness 22 23 if [ "${1}" = '+' ]; then 24 # convert brightness percentage to actual brightness units then add percentage in actual brightness units 25 if [ "${brightness}" -lt 100 ]; then brightness=$(printf '%s\n' "scale=10; ((${brightness}/100)*${MAX_BRIGHTNESS})+(${2}*(${MAX_BRIGHTNESS}/100))" | bc) 26 else return 27 fi 28 elif [ "${1}" = '-' ]; then 29 # same as above but substract 30 if [ "${brightness}" -gt 0 ]; then brightness=$(printf '%s\n' "scale=10; ((${brightness}/100)*${MAX_BRIGHTNESS})-(${2}*(${MAX_BRIGHTNESS}/100))" | bc) 31 else return 32 fi 33 fi 34 35 # can the decimal 36 brightness="$(printf '%.0f\n' ${brightness})" 37 38 # keep between 0 and max brightness 39 [ ${brightness} -lt 0 ] && brightness=0 40 [ ${brightness} -gt ${MAX_BRIGHTNESS} ] && brightness=${MAX_BRIGHTNESS} 41 42 # send brightness to file for future reading 43 printf '%s\n' ${brightness} | doas tee "${LIGHT}/brightness" 1>/dev/null 44 45 get_brightness 46 47 env HERBE_ID=/0 herbe "Brightness: ${brightness}%" & 48 } 49 50 bar() { 51 get_brightness 52 53 printf '%s\n' "${ICON} ${brightness}%" 54 } 55 56 main() { 57 # run only if backlight found 58 [ -d "${LIGHT}" ] || return 1 59 60 # called from bar 61 [ ${#} -eq 0 ] && bar 62 63 # bar usage 64 case ${BLOCK_BUTTON} in 65 4) set_brightness + 1 ;; 66 5) set_brightness - 1 ;; 67 esac 68 69 # adjust brightness based on args 70 # ${1} = +/- and ${2} = percentage 71 [ "${*}" ] && set_brightness "${1}" "${2}" 72 } 73 74 main "${@}"