i3-brightness-toggle 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. # This script increases/decreases brightness by a fixed increment specified in step.
  3. # This script relies on the script notify-send-improved.
  4. # i3-brightness-toggle - Increase and decrease brightness.
  5. # Copyright (C) 2017 Fabrizio Romano Genovese <egonigredo@gmail.com>
  6. # This copyrighted material is made available to anyone wishing to use,
  7. # modify, copy, or redistribute it subject to the terms and conditions of
  8. # the GNU General Public License v.2, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY expressed or implied, including the implied warranties of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. # Public License for more details.
  14. # You should have received a copy of the
  15. # GNU General Public License along with this program; if not,
  16. # see <http://www.gnu.org/licenses/>
  17. # Put this file in /bin/ or in /usr/bin. Do not forget to give
  18. # sudo chmod +x i3-blur
  19. # to make it executable.
  20. version=1.0
  21. step=10
  22. # Check if xbacklight and notify-send-improved are installed
  23. check_installed()
  24. {
  25. if [ ! -f /bin/notify-send-improved -a -f /bin/xbacklight ]; then
  26. echo "Notify-send-improved and/or xbacklight not present."
  27. echo "Please install them."
  28. exit 2
  29. fi
  30. }
  31. #Here we display the help messages and change the brightness using xbacklight
  32. if [[ "$#" -ge 2 ]]; then
  33. echo "Usage: $0 {-v, --version | -h --help}"
  34. exit 2
  35. elif [[ $# -le 1 ]]; then
  36. case $1 in
  37. -v | --version)
  38. echo "$0 - Version $version"
  39. exit 0
  40. ;;
  41. -h | --help)
  42. echo "- v, --version Display current version"
  43. echo "-h, --help Display this message"
  44. echo "up Increase brightness by $step%"
  45. echo "down Decrease brightness by $step%"
  46. exit 0
  47. ;;
  48. "up")
  49. check_installed
  50. xbacklight -steps 1 -time 0 -inc $step
  51. ;;
  52. "down")
  53. check_installed
  54. xbacklight -steps 1 -time 0 -dec $step
  55. ;;
  56. *)
  57. echo "Usage: $0 {up|down| -v, --version | -h, --help}"
  58. exit 2
  59. esac
  60. fi
  61. # Here we get the current brightness status from xbacklight
  62. # awk is necessary because xbacklight is not precise and the
  63. # resulting brightness number is always slightly less than it should,
  64. # by ~0.05 usually. If we were to use cut instead of awk the result would
  65. # then be aesthetically unpleasant.
  66. brightness=`xbacklight | awk '{printf("%d\n", $0+=$0<0?0:0.9)}'`
  67. # Selects the right icon for the notification
  68. # If the icon is not present it will not be displayed.
  69. brightnessicon="/usr/share/icons/Adwaita/32x32/status/display-brightness-symbolic.symbolic.png"
  70. # Here we read the notification id from a temporary file.
  71. # If the file doesn't exist, we initialize it the notification to 0
  72. # Then we send the notification out and we reassign the notification
  73. # id to the current one using tee
  74. #If the file exists, we just replace that notification id, that will
  75. # then stay the same and not increment.
  76. if [ ! -f "/tmp/brightness_notification_id.dat" ]; then
  77. notification_id="0"
  78. notification_id=$(notify-send-improved "Brightness Control" -i $muteicon --print-id -t 1000 "Brightness: $brightness" | tee /dev/tty)
  79. else
  80. notification_id=`cat /tmp/volume_notification_id.dat`
  81. notify-send-improved "Brightness Control" -i $brightnessicon --print-id --replace=$notification_id -t 1000 "Brightness: $brightness\%"
  82. fi
  83. # Here we write the notification id to a file. Next time we use this
  84. # script it will know if the notification has to be overwritten or not
  85. echo "${notification_id}" > /tmp/brightness_notification_id.dat