brightness 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2021 Jesús E. <heckyel@hyperbola.info>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. # One of the following: xrandr, xbacklight, kernel
  18. METHOD="xbacklight"
  19. case "${BLOCK_BUTTON}" in
  20. 1) xbacklight -inc 5 ;; # Left click
  21. 3) xbacklight -dec 5 ;; # Right click
  22. esac
  23. URGENT_VALUE=10
  24. case "${METHOD}" in
  25. xrandr) device="${BLOCK_INSTANCE:-primary}"
  26. xrandrOutput=$(xrandr --verbose)
  27. case "${device}" in
  28. primary) device=$(echo "${xrandrOutput}" | grep 'primary' | head -n 1 | awk -F ' ' '{print $1}') ;;
  29. esac
  30. curBrightness=$(echo "${xrandrOutput}" | grep "${device}" -A 5 | grep -i "Brightness" | awk -F ':' '{print $2}')
  31. ;;
  32. kernel) device="${BLOCK_INSTANCE:-intel_backlight}"
  33. maxBrightness=$(cat "/sys/class/backlight/${device}/max_brightness")
  34. curBrightness=$(cat "/sys/class/backlight/${device}/brightness")
  35. ;;
  36. xbacklight) curBrightness=$(xbacklight -get) ;;
  37. esac
  38. if [[ "${curBrightness}" -le 0 ]]; then
  39. exit
  40. fi
  41. case "${METHOD}" in
  42. xrandr) percent=$(echo "scale=0;${curBrightness} * 100" | bc -l) ;;
  43. kernel) percent=$(echo "scale=0;${curBrightness} * 100 / ${maxBrightness}" | bc -l) ;;
  44. xbacklight) percent=$(echo "scale=0;${curBrightness}" | bc -l) ;;
  45. esac
  46. percent=${percent%.*}
  47. if [[ "${percent}" -le 0 ]]; then
  48. exit
  49. fi
  50. echo "${percent}%"
  51. echo "${percent}%"
  52. echo ""
  53. if [[ "${percent}" -le "${URGENT_VALUE}" ]]; then
  54. exit 33
  55. fi