i3-screensaver-toggle 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/bash
  2. # This is a simple program that toggles/untoggles the screensaver
  3. # and power management, useful to watch videos fullscreen.
  4. # This script relies on the script notify-send-improved.
  5. # i3-screensaver-toggle - A simple program to toggle screensaver
  6. # Copyright (C) 2017 Fabrizio Romano Genovese <egonigredo@gmail.com>
  7. # This copyrighted material is made available to anyone wishing to use,
  8. # modify, copy, or redistribute it subject to the terms and conditions of
  9. # the GNU General Public License v.2, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY expressed or implied, including the implied warranties of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  14. # Public License for more details.
  15. # You should have received a copy of the
  16. # GNU General Public License along with this program; if not,
  17. # see <http://www.gnu.org/licenses/>
  18. # Put this file in /bin/ or in /usr/bin. Do not forget to give
  19. # sudo chmod +x i3-toggle-screensaver
  20. # to make it executable.
  21. version=1.0
  22. if [[ "$#" -ge 2 ]]; then
  23. echo "Usage: $0 {-v, --version | -h --help}"
  24. exit 2
  25. elif [[ "$#" -eq 1 ]]; then
  26. case $1 in
  27. -v | --version)
  28. echo "$0 - Version $version"
  29. exit 0;;
  30. -h | --help)
  31. echo "This is a simple program that toggles/untoggles the screensaver and power management, useful to watch videos fullscreen."
  32. echo "- v, --version Display current version"
  33. echo "-h, --help Display this message"
  34. exit 0;;
  35. *)
  36. echo "Usage: $0 {-v, --version | -h, --help}"
  37. exit 2;;
  38. esac
  39. fi
  40. # Check if notify-send-improved is installed
  41. if [ ! -f /bin/notify-send-improved ]; then
  42. echo "Notify-send-improved not present."
  43. echo "Please install it."
  44. exit 2
  45. fi
  46. # Here we read the status and notification id from a temporary file.
  47. # If the file doesn't exist, we initialize variables parsing xset.
  48. # If the file exists, we just parse it.
  49. if [ ! -f "/tmp/i3-screensaver-toggle.dat" ]; then
  50. notification_id="0"
  51. if xset q | grep "DPMS is Disabled" > /dev/null; then
  52. screensaver_mode="OFF"
  53. else
  54. screensaver_mode="ON"
  55. fi
  56. else
  57. screensaver_mode=`cat /tmp/i3-screensaver-toggle.dat | cut -d'.' -f 1`
  58. notification_id=`cat /tmp/i3-screensaver-toggle.dat | cut -d'.' -f 2`
  59. fi
  60. # Here we check the previous status and notification, then proceed to change.
  61. if [ $screensaver_mode = "ON" ]; then
  62. screensaver_mode="OFF"
  63. xset s off
  64. xset -dpms
  65. # Here we check if the notification was already used before or not, if it was, we replace it with the new one.
  66. if [ $notification_id = "0" ]; then
  67. notification_id=$(notify-send-improved "Screensaver Toggle" --print-id -t 1000 "Screensaver/PowerManagement off" | tee /dev/tty)
  68. else
  69. notify-send-improved "Screensaver Toggle" --print-id --replace=$notification_id -t 1000 "Screensaver/PowerManagement off"
  70. fi
  71. else
  72. screensaver_mode="ON"
  73. xset s on
  74. xset +dpms
  75. # Here we check if the notification was already used before or not, if it was, we replace it with the new one.
  76. if [ $notification_id = "0" ]; then
  77. notification_id=$(notify-send-improved "Screensaver Toggle" --print-id -t 1000 "Screensaver/PowerManagement on" | tee /dev/tty)
  78. else
  79. notify-send-improved "Screensaver Toggle" --print-id --replace=$notification_id -t 1000 "Screensaver/PowerManagement on"
  80. fi
  81. fi
  82. echo "$screensaver_mode.$notification_id" > /tmp/i3-screensaver-toggle.dat