commit 9aa73f964e3b6003fdccd1407611c0bfbf83c317
parent c478870c7c2315442e6b58d4b657cba6301a919f
Author: Chris Bracken <chris@bracken.jp>
Date: Thu, 9 Sep 2021 10:57:40 -0700
sway,i3: add volumectl script
Adds a platform-independent volume control script that can be used to
manage the input and output volume from the command line.
Diffstat:
3 files changed, 70 insertions(+), 27 deletions(-)
diff --git a/.config/i3/config b/.config/i3/config
@@ -102,17 +102,11 @@ bindsym $mod+Shift+e exec ~/.config/i3/exit_check.sh
############################################################
# Key bindings: Volume/Brightness
-# PulseAudio:
-# set $volume_up pactl set-sink-volume @DEFAULT_SINK@ +5%
-# set $volume_down pactl set-sink-volume @DEFAULT_SINK@ -5%
-# set $volume_toggle_mute pactl set-sink-mute @DEFAULT_SINK@ toggle
-# set $volume_toggle_mic_mute pactl set-source-mute @DEFAULT_SOURCE@ toggle
-#
-# Alsa:
-set $volume_up amixer -q sset Master 5%+
-set $volume_down amixer -q sset Master 5%-
-set $volume_toggle_mute amixer -q sset Master toggle
-set $volume_toggle_mic_mute amixer -q sset Mic toggle
+# OS-specific behaviour in ~/.local/bin/volumectl script.
+set $volume_up volumectl up 5
+set $volume_down volumectl down 5
+set $volume_toggle_mute volumectl mute
+set $volume_toggle_mic_mute volumectl muteinput
# Increase and decrease volume.
bindsym Control+$mod+F12 exec --no-startup-id $volume_up
diff --git a/.config/sway/config b/.config/sway/config
@@ -138,22 +138,11 @@ bindsym $mod+Shift+e exec ~/.config/sway/exit_check.sh
############################################################
# Key bindings: Volume/Brightness
-# PulseAudio:
-set $volume_up pactl set-sink-volume @DEFAULT_SINK@ +5%
-set $volume_down pactl set-sink-volume @DEFAULT_SINK@ -5%
-set $volume_toggle_mute pactl set-sink-mute @DEFAULT_SINK@ toggle
-set $volume_toggle_mic_mute pactl set-source-mute @DEFAULT_SOURCE@ toggle
-#
-# Alsa:
-#set $volume_up amixer -q sset Master 5%+
-#set $volume_down amixer -q sset Master 5%-
-#set $volume_toggle_mute amixer -q sset Master toggle
-#set $volume_toggle_mic_mute amixer -q sset Mic toggle
-#
-# FreeBSD
-#set $volume_up mixer -s pcm +5
-#set $volume_down mixer -s pcm -5
-#set $volume_toggle_mute mixer -s pcm 0
+# OS-specific behaviour in ~/.local/bin/volumectl script.
+set $volume_up volumectl up 5
+set $volume_down volumectl down 5
+set $volume_toggle_mute volumectl mute
+set $volume_toggle_mic_mute volumectl muteinput
# Increase and decrease volume.
bindsym Control+$mod+F12 exec $volume_up
diff --git a/.local/bin/volumectl b/.local/bin/volumectl
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+# Exit with usage.
+usage() {
+ echo >&2 "usage: volumectl COMMAND ARGS..."
+ echo >&2
+ echo >&2 "Commands:"
+ echo >&2 " up AMOUNT: Adjust output volume up by percentage"
+ echo >&2 " down AMOUNT: Adjust output volume down by percentage"
+ echo >&2 " mute: Toggle output mute"
+ echo >&2 " muteinput: Toggle input mute"
+}
+
+adjust_output() {
+ # Check for argument.
+ if [ $# -lt 2 ]; then
+ usage
+ exit 1
+ fi
+ SIGN="$1"
+ PERCENT="$2"
+ if [ "$(uname)" = FreeBSD ]; then
+ mixer -s pcm "${SIGN}${PERCENT}"
+ elif [ "$(uname)" = Linux ]; then
+ pactl set-sink-volume @DEFAULT_SINK@ "${SIGN}${PERCENT}%"
+ fi
+}
+
+toggle_mute_output() {
+ if [ "$(uname)" = FreeBSD ]; then
+ mixer -s pcm 0
+ elif [ "$(uname)" = Linux ]; then
+ pactl set-sink-mute @DEFAULT_SINK@ toggle
+ fi
+}
+
+toggle_mute_input() {
+ if [ "$(uname)" = Linux ]; then
+ pactl set-source-mute @DEFAULT_SINK@ toggle
+ fi
+}
+
+# Check for command.
+if [ $# -lt 1 ]; then
+ usage
+ exit 1
+fi
+
+# No-arg commands.
+COMMAND="$1"
+case "$COMMAND" in
+ up) adjust_output + $2 ;;
+ down) adjust_output - $2 ;;
+ mute) toggle_mute_output ;;
+ muteinput) toggle_mute_input ;;
+ *)
+ usage
+ exit 1
+ ;;
+esac