mac-dotfiles/bin/volume-brightness

259 lines
7.6 KiB
Text
Raw Normal View History

2024-08-30 23:09:35 +00:00
#!/bin/bash
# See README.md for usage instructions
volume_step=5
mic_volume_step=5
brightness_step=5
kbd_brightness_step=5
max_volume=100
max_mic_volume=100
volume_notification_timeout=1000
mic_volume_notification_timeout=1000
brightness_notification_timeout=1000
kbd_brightness_notification_timeout=1000
player_notification_timeout=5000
download_album_art=true
show_album_art=true
show_music_in_volume_indicator=true
# Uses regex to get volume from pactl
function get_volume {
pactl get-sink-volume @DEFAULT_SINK@ | grep -Po '[0-9]{1,3}(?=%)' | head -1
}
function get_mic_volume {
pactl get-source-volume @DEFAULT_SOURCE@ | grep -Po '[0-9]{1,3}(?=%)' | head -1
}
# Uses regex to get mute status from pactl
function get_mute {
pactl get-sink-mute @DEFAULT_SINK@ | grep -Po '(?<=Mute: )(yes|no)'
}
function get_mic_mute {
pactl get-source-mute @DEFAULT_SOURCE@ | grep -Po '(?<=Mute: )(yes|no)'
}
# Uses regex to get brightness from xbacklight
function get_brightness {
brightnessctl -c backlight -P get | grep -Po '[0-9]{1,3}' | head -n 1
}
function get_kbd_brightness {
brightnessctl -d kbd_backlight -P get | grep -Po '[0-9]{1,3}' | head -n 1
}
# Returns a mute icon, a volume-low icon, or a volume-high icon, depending on the volume
function get_volume_icon {
volume=$(get_volume)
mute=$(get_mute)
if [ "$mute" == "yes" ]; then
volume_icon="volume-level-muted"
elif [ "$volume" -eq 0 ]; then
volume_icon="volume-level-low"
elif [ "$volume" -lt 50 ]; then
volume_icon="volume-level-medium"
else
volume_icon="volume-level-high"
fi
}
function get_mic_volume_icon {
mic_volume=$(get_mic_volume)
mic_mute=$(get_mic_mute)
if [ "$mic_mute" == "yes" ]; then
mic_volume_icon="mic-volume-muted"
elif [ "$mic_volume" -eq 0 ]; then
mic_volume_icon="mic-volume-low"
elif [ "$mic_volume" -lt 50 ]; then
mic_volume_icon="mic-volume-medium"
else
mic_volume_icon="mic-volume-high"
fi
}
# Always returns the same icon - I couldn't get the brightness-low icon to work with fontawesome
function get_brightness_icon {
brightness=$(get_brightness)
if [ "$brightness" -eq 0 ]; then
brightness_icon="gpm-brightness-lcd-disabled"
else
brightness_icon="gpm-brightness-lcd"
fi
}
function get_kbd_brightness_icon {
kbd_brightness=$(get_kbd_brightness)
if [ "$kbd_brightness" -eq 0 ]; then
kbd_brightness_icon="gpm-brightness-kbd-disabled"
else
kbd_brightness_icon="gpm-brightness-kbd"
fi
}
function get_album_art {
url=$(playerctl -f "{{mpris:artUrl}}" metadata)
if [[ $url == "file://"* ]]; then
album_art="${url/file:\/\//}"
elif [[ $url == "http://"* ]] && [[ $download_album_art == "true" ]]; then
# Identify filename from URL
filename="$(echo $url | sed "s/.*\///")"
# Download file to /tmp if it doesn't exist
if [ ! -f "/tmp/$filename" ]; then
wget -O "/tmp/$filename" "$url"
fi
album_art="/tmp/$filename"
elif [[ $url == "https://"* ]] && [[ $download_album_art == "true" ]]; then
# Identify filename from URL
filename="$(echo $url | sed "s/.*\///")"
# Download file to /tmp if it doesn't exist
if [ ! -f "/tmp/$filename" ]; then
wget -O "/tmp/$filename" "$url"
fi
album_art="/tmp/$filename"
else
album_art=""
fi
}
# Displays a volume notification
function show_volume_notif {
volume=$(get_mute)
get_volume_icon
notify-send -a "volume-brightness" -t $volume_notification_timeout -h string:x-dunst-stack-tag:volume_notif -h int:value:$volume -i "$volume_icon" "Volume"
}
function show_mic_volume_notif {
mic_volume=$(get_mic_mute)
get_mic_volume_icon
notify-send -a "volume-brightness" -t $mic_volume_notification_timeout -h string:x-dunst-stack-tag:volume_notif -h int:value:$mic_volume -i "$mic_volume_icon" "Microphone volume"
}
# Displays a music notification
function show_music_notif {
song_title=$(playerctl -f "{{title}}" metadata)
song_artist=$(playerctl -f "{{artist}}" metadata)
song_album=$(playerctl -f "{{album}}" metadata)
if [[ $show_album_art == "true" ]]; then
get_album_art
fi
notify-send -a "volume-brightness" -t $player_notification_timeout -h string:x-dunst-stack-tag:music_notif -i "$album_art" "$song_title" "$song_artist - $song_album"
}
# Displays a brightness notification using dunstify
function show_brightness_notif {
brightness=$(get_brightness)
echo $brightness
get_brightness_icon
notify-send -a "volume-brightness" -t $brightness_notification_timeout -h string:x-dunst-stack-tag:brightness_notif -h int:value:$brightness -i "$brightness_icon" "Brightness"
}
function show_kbd_brightness_notif {
kbd_brightness=$(get_kbd_brightness)
echo $kbd_brightness
get_kbd_brightness_icon
notify-send -a "volume-brightness" -t $kbd_brightness_notification_timeout -h string:x-dunst-stack-tag:brightness_notif -h int:value:$kbd_brightness -i "$kbd_brightness_icon" "Keyboard brightness"
}
# Main function - Takes user input, "volume_up", "volume_down", "brightness_up", or "brightness_down"
case $1 in
volume_up)
# Unmutes and increases volume, then displays the notification
pactl set-sink-mute @DEFAULT_SINK@ 0
volume=$(get_volume)
if [ $(( "$volume" + "$volume_step" )) -gt $max_volume ]; then
pactl set-sink-volume @DEFAULT_SINK@ $max_volume%
else
pactl set-sink-volume @DEFAULT_SINK@ +$volume_step%
fi
show_volume_notif
;;
volume_down)
# Raises volume and displays the notification
pactl set-sink-volume @DEFAULT_SINK@ -$volume_step%
show_volume_notif
;;
volume_mute)
# Toggles mute and displays the notification
pactl set-sink-mute @DEFAULT_SINK@ toggle
show_volume_notif
;;
mic_volume_up)
# Unmutes and increases volume, then displays the notification
pactl set-source-mute @DEFAULT_SOURCE@ 0
mic_volume=$(get_mic_volume)
if [ $(( "$mic_volume" + "$mic_volume_step" )) -gt $max_mic_volume ]; then
pactl set-source-volume @DEFAULT_SOURCE@ $max_mic_volume%
else
pactl set-source-volume @DEFAULT_SOURCE@ +$mic_volume_step%
fi
show_mic_volume_notif
;;
mic_volume_down)
# Raises volume and displays the notification
pactl set-source-volume @DEFAULT_SOURCE@ -$mic_volume_step%
show_mic_volume_notif
;;
mic_volume_mute)
# Toggles mute and displays the notification
pactl set-source-mute @DEFAULT_SOURCE@ toggle
show_mic_volume_notif
;;
brightness_up)
# Increases brightness and displays the notification
brightnessctl -c backlight set $brightness_step%+
show_brightness_notif
;;
brightness_down)
# Decreases brightness and displays the notification
brightnessctl -c backlight set $brightness_step%-
show_brightness_notif
;;
kbd_brightness_up)
# Increases brightness and displays the notification
brightnessctl -d kbd_backlight set $kbd_brightness_step%+
show_kbd_brightness_notif
;;
kbd_brightness_down)
# Decreases brightness and displays the notification
brightnessctl -d kbd_backlight set $kbd_brightness_step%-
show_kbd_brightness_notif
;;
next_track)
# Skips to the next song and displays the notification
playerctl next
sleep 0.5 && show_music_notif
;;
prev_track)
# Skips to the previous song and displays the notification
playerctl previous
sleep 0.5 && show_music_notif
;;
play_pause)
playerctl play-pause
show_music_notif
# Pauses/resumes playback and displays the notification
;;
esac