brightness-control.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/bash
  2. SAVE_FILE="${HOME}/.cache/brightness-control-cache"
  3. let really_notify=1
  4. notify-brightness() {
  5. ((${really_notify})) || return
  6. brightness="$(brightnessctl -m | sed 's/,/ /g' | awk '{print $4}' | tr -d '\n' | tr -d '%')"
  7. notify-send -h string:x-canonical-private-synchronous:brightness-control-sh -t 4000 -u normal -a "Brightness" -h int:value:"$brightness" "$brightness%"
  8. }
  9. save-brightness() {
  10. brightness="$(brightnessctl -m | sed 's/,/ /g' | awk '{print $4}' | tr -d '\n' | tr -d '%')"
  11. printf '%d' "${brightness}" >"${SAVE_FILE}"
  12. }
  13. restore-brightness() {
  14. if [ ! -e "${SAVE_FILE}" ]; then
  15. echo "brightness-control.sh: error: save file at \"${SAVE_FILE}\" not found" >&2
  16. exit 1
  17. fi
  18. brightness="$(cat "${SAVE_FILE}")"
  19. brightnessctl s "${brightness}%"
  20. }
  21. print-usage() {
  22. echo 'usage: brightness-control.sh [up|down|set|restore|display] <ammount>' 1>&2
  23. exit 1
  24. }
  25. if [ "$#" -lt 1 ]; then
  26. print-usage
  27. fi
  28. if [[ "${1}" == -n ]]; then
  29. really_notify=0
  30. shift 1
  31. fi
  32. ammount='5%'
  33. value='50%'
  34. if [ "${#}" -gt 1 ]; then
  35. ammount="${2}"
  36. value="${2}"
  37. fi
  38. case "${1}" in
  39. 'up')
  40. brightnessctl s "+${ammount}" > /dev/null
  41. notify-brightness
  42. save-brightness
  43. ;;
  44. 'down')
  45. brightnessctl s "${ammount}-" > /dev/null
  46. notify-brightness
  47. save-brightness
  48. ;;
  49. 'set')
  50. brightnessctl s "${value}" > /dev/null
  51. notify-brightness
  52. save-brightness
  53. ;;
  54. 'display')
  55. notify-brightness
  56. ;;
  57. 'restore')
  58. restore-brightness
  59. ;;
  60. *)
  61. print-usage
  62. ;;
  63. esac