Skip to content

Commit

Permalink
Merge pull request #1835 from keth-real/main
Browse files Browse the repository at this point in the history
cleaner volume script for extensibility and swayosd integration
  • Loading branch information
kRHYME7 authored Sep 15, 2024
2 parents 0ab8f30 + 9561ea3 commit 8a702f8
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 93 deletions.
3 changes: 2 additions & 1 deletion Configs/.config/hypr/userprefs.t2
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ bind = $mainMod+Shift, G, exec, pkill -x rofi || $scrPath/gamelauncher.sh # laun
# exec-once = swayidle -w timeout 600 'swaylock' timeout 900 'hyprctl dispatch dpms off' resume 'hyprctl dispatch dpms on' # lock after 10 mins, sleep after 15 mins // install swayidle
# exec-once = swayidle -w timeout 1200 'swaylock; hyprctl dispatch dpms off' resume 'hyprctl dispatch dpms on' timeout 1800 'systemctl suspend' # lock and sleep after 20 mins, suspend after 30 mins // install swayidle
# exec-once = libinput-gestures // install libinput-gestures

# exec-once = swayosd-server # enable swayosd service
# exec-once = `pkexec swayosd-libinput-backend` # swayosd service for keyboard input (requires to be run in a subshell)

# █░█░█ █ █▄░█ █▀▄ █▀█ █░█░█   █▀█ █░█ █░░ █▀▀ █▀
# ▀▄▀▄▀ █ █░▀█ █▄▀ █▄█ ▀▄▀▄▀   █▀▄ █▄█ █▄▄ ██▄ ▄█
Expand Down
56 changes: 37 additions & 19 deletions Configs/.local/share/bin/brightnesscontrol.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
#!/usr/bin/env sh

# Check if the script is already running
pgrep -cf "${0##*/}" | grep -qv 1 && echo "An instance of the script is already running..." && exit 1

scrDir=`dirname "$(realpath "$0")"`
source $scrDir/globalcontrol.sh

function print_error
# Check if SwayOSD is installed
use_swayosd=false
if command -v swayosd-client >/dev/null 2>&1 && pgrep -x swayosd-server >/dev/null; then
use_swayosd=true
fi

print_error()
{
cat << "EOF"
./brightnesscontrol.sh <action>
cat << EOF
$(basename ${0}) <action> [step]
...valid actions are...
i -- <i>ncrease brightness [+5%]
d -- <d>ecrease brightness [-5%]
Example:
$(basename ${0}) i 10 # Increase brightness by 10%
$(basename ${0}) d # Decrease brightness by default step (5%)
EOF
}

function send_notification {
send_notification() {
brightness=`brightnessctl info | grep -oP "(?<=\()\d+(?=%)" | cat`
brightinfo=$(brightnessctl info | awk -F "'" '/Device/ {print $2}')
angle="$(((($brightness + 2) / 5) * 5))"
Expand All @@ -22,33 +35,38 @@ function send_notification {
notify-send -a "t2" -r 91190 -t 800 -i "${ico}" "${brightness}${bar}" "${brightinfo}"
}

function get_brightness {
get_brightness() {
brightnessctl -m | grep -o '[0-9]\+%' | head -c-2
}

step="${2:-5}"

case $1 in
i) # increase the backlight
i|-i) # increase the backlight
if [[ $(get_brightness) -lt 10 ]] ; then
# increase the backlight by 1% if less than 10%
brightnessctl set +1%
else
# increase the backlight by 5% otherwise
brightnessctl set +5%
step=1
fi

$use_swayosd && swayosd-client --brightness raise "$step" && exit 0
brightnessctl set +${step}%
send_notification ;;
d) # decrease the backlight
if [[ $(get_brightness) -le 1 ]] ; then
# avoid 0% brightness
brightnessctl set 1%
elif [[ $(get_brightness) -le 10 ]] ; then
d|-d) # decrease the backlight

if [[ $(get_brightness) -le 10 ]] ; then
# decrease the backlight by 1% if less than 10%
brightnessctl set 1%-
step=1
fi

if [[ $(get_brightness) -le 1 ]]; then
brightnessctl set ${step}%
$use_swayosd && exit 0
else
# decrease the backlight by 5% otherwise
brightnessctl set 5%-
$use_swayosd && swayosd-client --brightness lower "$step" && exit 0
brightnessctl set ${step}%-
fi

send_notification ;;
*) # print error
print_error ;;
esac

195 changes: 122 additions & 73 deletions Configs/.local/share/bin/volumecontrol.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
#!/usr/bin/env sh

# Source global control script
scrDir=$(dirname "$(realpath "$0")")
source $scrDir/globalcontrol.sh

# define functions

print_error() {
cat <<"EOF"
./volumecontrol.sh -[device] <actions>
...valid device are...
i -- input device
o -- output device
p -- player application
...valid actions are...
i -- increase volume [+5]
d -- decrease volume [-5]
m -- mute [x]
source "$scrDir/globalcontrol.sh"

# Check if SwayOSD is installed
use_swayosd=false
if command -v swayosd-client >/dev/null 2>&1 && pgrep -x swayosd-server >/dev/null; then
use_swayosd=true
fi

# Define functions

print_usage() {
cat <<EOF
Usage: $(basename "$0") -[device] <action> [step]
Devices/Actions:
-i Input device
-o Output device
-p Player application
-s Select output device
-t Toggle to next output device
Actions:
i Increase volume
d Decrease volume
m Toggle mute
Optional:
step Volume change step (default: 5)
Examples:
$(basename "$0") -o i 5 # Increase output volume by 5
$(basename "$0") -i m # Toggle input mute
$(basename "$0") -p spotify d 10 # Decrease Spotify volume by 10
$(basename "$0") -p '' d 10 # Decrease volume by 10 for all players
EOF
exit 1
}

notify_vol() {
angle="$(((($vol + 2) / 5) * 5))"
angle=$(( (($vol + 2) / 5) * 5 ))
ico="${icodir}/vol-${angle}.svg"
bar=$(seq -s "." $(($vol / 15)) | sed 's/[0-9]//g')
notify-send -a "t2" -r 91190 -t 800 -i "${ico}" "${vol}${bar}" "${nsink}"
Expand All @@ -37,78 +58,106 @@ notify_mute() {
fi
}

action_pamixer() {
pamixer "${srce}" -"${1}" "${step}"
vol=$(pamixer "${srce}" --get-volume | cat)
change_volume() {
local action=$1
local step=$2
local device=$3
local delta="-"
local mode="--output-volume"

[ "${action}" = "i" ] && delta="+"
[ "${srce}" = "--default-source" ] && mode="--input-volume"
case $device in
"pamixer")
$use_swayosd && swayosd-client ${mode} "${delta}${step}" && exit 0
pamixer $srce -"$action" "$step"
vol=$(pamixer $srce --get-volume)
;;
"playerctl")
playerctl --player="$srce" volume "$(awk -v step="$step" 'BEGIN {print step/100}')${delta}"
vol=$(playerctl --player="$srce" volume | awk '{ printf "%.0f\n", $0 * 100 }')
;;
esac

notify_vol
}

action_playerctl() {
[ "${1}" == "i" ] && pvl="+" || pvl="-"
playerctl --player="${srce}" volume "0.0${step}${pvl}"
vol=$(playerctl --player="${srce}" volume | awk '{ printf "%.0f\n", $0 * 100 }')
toggle_mute() {
local device=$1
local mode="--output-volume"
[ "${srce}" = "--default-source" ] && mode="--input-volume"
case $device in
"pamixer")
$use_swayosd && swayosd-client "${mode}" mute-toggle && exit 0
pamixer $srce -t
notify_mute
;;
"playerctl")
local volume_file="/tmp/$(basename "$0")_last_volume_${srce:-all}"
if [ "$(playerctl --player="$srce" volume | awk '{ printf "%.2f", $0 }')" != "0.00" ]; then
playerctl --player="$srce" volume | awk '{ printf "%.2f", $0 }' > "$volume_file"
playerctl --player="$srce" volume 0
else
if [ -f "$volume_file" ]; then
last_volume=$(cat "$volume_file")
playerctl --player="$srce" volume "$last_volume"
else
playerctl --player="$srce" volume 0.5 # Default to 50% if no saved volume
fi
fi
notify_mute
;;
esac
}

select_output() {
if [ "$@" ]; then
desc="$*"
device=$(pactl list sinks | grep -C2 -F "Description: $desc" | grep Name | cut -d: -f2 | xargs)
local selection=$1
if [ -n "$selection" ]; then
device=$(pactl list sinks | grep -C2 -F "Description: $selection" | grep Name | cut -d: -f2 | xargs)
if pactl set-default-sink "$device"; then
notify-send -t 2000 -r 2 -u low "Activated: $desc"
notify-send -t 2000 -r 2 -u low "Activated: $selection"
else
notify-send -t 2000 -r 2 -u critical "Error activating $desc"
notify-send -t 2000 -r 2 -u critical "Error activating $selection"
fi
else
pactl list sinks | grep -ie "Description:" | awk -F ': ' '{print $2}' | sort |
while IFS= read -r x; do echo "$x"; done
pactl list sinks | grep -ie "Description:" | awk -F ': ' '{print $2}' | sort
fi
}

# eval device option

while getopts iop:s: DeviceOpt; do
case "${DeviceOpt}" in
i)
nsink=$(pamixer --list-sources | awk -F '"' 'END {print $(NF - 1)}')
[ -z "${nsink}" ] && echo "ERROR: Input device not found..." && exit 0
ctrl="pamixer"
srce="--default-source"
;;
o)
nsink=$(pamixer --get-default-sink | awk -F '"' 'END{print $(NF - 1)}')
[ -z "${nsink}" ] && echo "ERROR: Output device not found..." && exit 0
ctrl="pamixer"
srce=""
;;
p)
player_name="${OPTARG}"
nsink=$(playerctl --list-all | grep -w "${player_name}")
[ -z "${nsink}" ] && echo "ERROR: Player ${player_name} not active..." && exit 0
ctrl="playerctl"
srce="${player_name}"
;;
s)
default_sink="$(pamixer --get-default-sink | awk -F '"' 'END{print $(NF - 1)}')"
export selected_sink="$(select_output "${@}" | rofi -dmenu -select "${default_sink}" -config "${confDir}/rofi/notification.rasi")"
select_output "$selected_sink"
exit
;;
*) print_error ;;
esac
done
toggle_output() {
local default_sink=$(pamixer --get-default-sink | awk -F '"' 'END{print $(NF - 1)}')
mapfile -t sink_array < <(select_output)
local current_index=$(printf '%s\n' "${sink_array[@]}" | grep -n "$default_sink" | cut -d: -f1)
local next_index=$(( (current_index % ${#sink_array[@]}) + 1 ))
local next_sink="${sink_array[next_index-1]}"
select_output "$next_sink"
}

# set default variables
# Main script logic

# Set default variables
icodir="${confDir}/dunst/icons/vol"
shift $((OPTIND - 1))
step="${2:-5}"
step=5
# Parse options
while getopts "iop:st" opt; do
case $opt in
i) device="pamixer"; srce="--default-source"; nsink=$(pamixer --list-sources | awk -F '"' 'END {print $(NF - 1)}') ;;
o) device="pamixer"; srce=""; nsink=$(pamixer --get-default-sink | awk -F '"' 'END{print $(NF - 1)}') ;;
p) device="playerctl"; srce="${OPTARG}"; nsink=$(playerctl --list-all | grep -w "$srce") ;;
s) select_output "$(select_output | rofi -dmenu -config "${confDir}/rofi/notification.rasi")"; exit ;;
t) toggle_output; exit ;;
*) print_usage ;;
esac
done

# execute action
shift $((OPTIND-1))

case "${1}" in
i) action_${ctrl} i ;;
d) action_${ctrl} d ;;
m) "${ctrl}" "${srce}" -t && notify_mute && exit 0 ;;
*) print_error ;;
esac
# Check if device is set
[ -z "$device" ] && print_usage

notify_vol
# Execute action
case $1 in
i|d) change_volume "$1" "${2:-$step}" "$device" ;;
m) toggle_mute "$device" ;;
*) print_usage ;;
esac

0 comments on commit 8a702f8

Please sign in to comment.