i3-xrandr-toggle 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. # This is a simple program that toggles an external monitor on and off
  3. # using xrandr.
  4. # This program won't work out of the box. You will have to rename the variables
  5. # $Internal and $External to suit your needs.
  6. # i3-xrandr-toggle - A simple xrandr toggle addon
  7. # Copyright (C) 2017 Fabrizio Romano Genovese <egonigredo@gmail.com>
  8. # This copyrighted material is made available to anyone wishing to use,
  9. # modify, copy, or redistribute it subject to the terms and conditions of
  10. # the GNU General Public License v.2, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful, but WITHOUT
  13. # ANY WARRANTY expressed or implied, including the implied warranties of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. # Public License for more details.
  16. # You should have received a copy of the
  17. # GNU General Public License along with this program; if not,
  18. # see <http://www.gnu.org/licenses/>
  19. # Put this file in /bin/ or in /usr/bin. Do not forget to give
  20. # sudo chmod +x i3-xrandr-toggle
  21. # to make it executable.
  22. version=1.0
  23. if [[ "$#" -ge 2 ]]; then
  24. echo "Usage: $0 {-v, --version | -h --help}"
  25. exit 2
  26. elif [[ "$#" -eq 1 ]]; then
  27. case $1 in
  28. -v | --version)
  29. echo "$0 - Version $version"
  30. exit 0;;
  31. -h | --help)
  32. echo "This is a simple program that toggles an external monitor on and off using xrandr. This program won't work out of the box. You will have to rename the variables \$Internal and \$External in the script to suit your needs."
  33. echo "- v, --version Display current version"
  34. echo "-h, --help Display this message"
  35. exit 0;;
  36. *)
  37. echo "Usage: $0 {-v, --version | -h, --help}"
  38. exit 2;;
  39. esac
  40. fi
  41. Internal="eDP1"
  42. External="DP1-3"
  43. # Grep current external monitor status. Do noting if not connected
  44. if xrandr | grep "DP1-3 connected" > /dev/null; then
  45. # Check if the program has been used before. If yes, reads previous
  46. # Status from a file. If not, initialises the variables.
  47. if [ ! -f "/tmp/monitor_mode.dat" ]; then
  48. monitor_mode="ON"
  49. else
  50. monitor_mode=`cat /tmp/monitor_mode.dat`
  51. fi
  52. if [ $monitor_mode = "OFF" ]; then
  53. monitor_mode="ON"
  54. xrandr -d :0.0 --output $External --auto
  55. xrandr -d :0.0 --output $Internal --auto
  56. xrandr -d :0.0 --output $External --right-of $Internal
  57. # Reloads background if using feh
  58. if [ -a "/bin/feh" ]; then
  59. ~/.fehbg
  60. fi
  61. else
  62. monitor_mode="OFF"
  63. xrandr -d :0.0 --output $External --off
  64. xrandr -d :0.0 --output $Internal --auto
  65. fi
  66. # Rewrites the file for the next use.
  67. echo "${monitor_mode}" > /tmp/monitor_mode.dat
  68. fi