i3-volume-toggle 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. #This script increases/decreases/toggles the volume by a fixed increment specified in step.
  3. #This script relies on the script notify-send-improved and on pactl.
  4. # i3-volume-toggle - Increases and decreases volume.
  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-volume-toggle
  19. # to make it executable.
  20. step=5
  21. # Check if pactl, amixer and notify-send-improved are installed
  22. check_installed()
  23. {
  24. if [ ! -f /bin/notify-send-improved -a -f /bin/pactl -a -f /bin/amixer ]; then
  25. echo "Notify-send-improved, amixer and/or pactl not present."
  26. echo "Please install them."
  27. exit 2
  28. fi
  29. }
  30. #Here we actually change the volume using pactl
  31. if [[ $# -le 1 ]]; then
  32. case $1 in
  33. -v | --version)
  34. echo "$0 - Version $version"
  35. exit 0
  36. ;;
  37. -h | --help)
  38. echo "- v, --version Display current version"
  39. echo "-h, --help Display this message"
  40. echo "up Increase volume by $step%"
  41. echo "down Decrease volume by $step%"
  42. echo "mute Mute/unmute"
  43. exit 0
  44. ;;
  45. "up")
  46. check_installed
  47. pactl set-sink-volume 1 +$step%
  48. #This produces sound feedback. Comment if you don't like it.
  49. (speaker-test -t sine -f 400)& pid=$!; sleep 0.1s; kill -9 $pid
  50. ;;
  51. "down")
  52. check_installed
  53. pactl set-sink-volume 1 -$step%
  54. #This produces sound feedback. Comment if you don't like it.
  55. (speaker-test -t sine -f 400)& pid=$!; sleep 0.1s; kill -9 $pid
  56. ;;
  57. "toggle")
  58. check_installed
  59. pactl set-sink-mute 1 toggle
  60. #This produces sound feedback. Comment if you don't like it.
  61. (speaker-test -t sine -f 400)& pid=$!; sleep 0.1s; kill -9 $pid
  62. ;;
  63. *)
  64. echo "Usage: $0 {-v, --version|-h, --help|up|toggle|down}"
  65. exit 2
  66. esac
  67. fi
  68. # Here we get the current status of audio from alsamixer
  69. muted=`amixer get Master|tail -n1|sed -E 's/.*\[([a-z]+)\]/\1/'`
  70. volume=`amixer get Master|tail -n1|sed -E 's/.*\[([0-9]+)\%\].*/\1/'`
  71. # Selects the right icon for the notification
  72. # If icons do not exist they won't be displayed
  73. muteicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-muted-symbolic.symbolic.png"
  74. if [ "$volume" -eq "0" ]; then
  75. volumeicon=$muteicon
  76. elif [ "$volume" -lt "33" ]; then
  77. volumeicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-low-symbolic.symbolic.png"
  78. elif [ "$volume" -lt "66" ]; then
  79. volumeicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-medium-symbolic.symbolic.png"
  80. elif [ "$volume" -lt "120" ]; then
  81. volumeicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-high-symbolic.symbolic.png"
  82. else
  83. # The following image is obtained with this command:
  84. # sudo convert "/usr/share/icons/Adwaita/32x32/status/audio-volume-high-symbolic.symbolic.png" -fuzz 100% -fill red -opaque "/usr/share/icons/Adwaita/32x32/status/audio-volume-danger-symbolic.symbolic.png"
  85. volumeicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-danger-symbolic.symbolic.png"
  86. fi
  87. # Here we read the notification id from a temporary file. If the file doesn't exist, we initialize it the notification to 0
  88. # Then we send the notification out and we reassign the notification id to the current one using tee
  89. #
  90. # If the file exists, we just replace that notification id, that will then stay the same and not increment.
  91. if [ ! -f "/tmp/volume_notification_id.dat" ]; then
  92. notification_id="0"
  93. if [[ $muted == "off" ]]; then
  94. notification_id=$(notify-send-improved "Volume Control" -i $muteicon --print-id -t 1000 "Muted \($volume\%\)" | tee /dev/tty)
  95. else
  96. notification_id=$(notify-send-improved "Volume Control" -i $volumeicon --print-id -t 1000 "Volume: $volume\%" | tee /dev/tty)
  97. fi
  98. else
  99. notification_id=`cat /tmp/volume_notification_id.dat`
  100. if [[ $muted == "off" ]]; then
  101. notify-send-improved "Volume Control" -i $muteicon --print-id --replace=$notification_id -t 1000 "Muted \($volume\%\)"
  102. else
  103. notify-send-improved "Volume Control" -i $volumeicon --print-id --replace=$notification_id -t 1000 "Volume: $volume\%"
  104. fi
  105. fi
  106. # Here we write the notification id to a file. Next time we use this script it will know if the notification has to be overwritten or not
  107. echo "${notification_id}" > /tmp/volume_notification_id.dat