Skip to content
adnan360 edited this page Sep 30, 2022 · 2 revisions

This script makes 'wob' easier to set up - without needing a clause in sway's config file. As a plus, it's self healing - if 'wob' dies for some reason, it is re-started on demand.

Usage is simply:

mywob 51
#!/usr/bin/env bash

# returns 0 (success) if $1 is running and is attached to this sway session; else 1
is_running_on_this_screen() {
    pkill -0 $1 || return 1
    for pid in $( pgrep $1 ); do
        WOB_SWAYSOCK="$( tr '\0' '\n' < /proc/$pid/environ | awk -F'=' '/^SWAYSOCK/ {print $2}' )"
        if [[ "$WOB_SWAYSOCK" == "$SWAYSOCK" ]]; then
            return 0
        fi
    done
    return 1
}

new_value=$1 # null or a percent; no checking!!

wob_pipe=~/.cache/$( basename $SWAYSOCK ).wob

[[ -p $wob_pipe ]] || mkfifo $wob_pipe

# wob does not appear in $(swaymsg -t get_msg), so:
is_running_on_this_screen wob || {
    tail -f $wob_pipe | wob &
}

[[ "$new_value" ]] && echo $new_value > $wob_pipe

This is another script that takes care of everything to implement a simple PulseAudio volume control with wob. It also takes care of startup and kill (if needed) commands:

#!/usr/bin/env bash
# Simple script that does 3 things:
# - handles startup and kill commands
# - changes volume, mute status, shows gui settings
# - shows appropriate wob overlay if anything changed
# Mainly written to work with PulseAudio but can be modified for alsa
# Also available here: https://framagit.org/-/snippets/6723
# Utilizes Wayland based overlay progress bar program - wob
# License: CC0 1.0 Universal
# Requirements: wob (and Wayland), pulseaudio, pavucontrol or pavucontrol-qt, bash
# Example usage:
# $ ./volume.sh startup &
# $ ./volume.sh up
# $ ./volume.sh down
# $ ./volume.sh togglemute
# $ ./volume.sh kill

command="$1"
show=''

if [ "$command" == 'up' ]; then
	pactl set-sink-volume @DEFAULT_SINK@ +5%
	show=true
elif [ "$command" == 'down' ]; then
	pactl set-sink-volume @DEFAULT_SINK@ -5%
	show=true
elif [ "$command" == 'togglemute' ]; then
	pactl set-sink-mute @DEFAULT_SINK@ toggle
	show=true
elif [ "$command" == 'mute' ]; then
	pactl set-sink-mute @DEFAULT_SINK@ 1
	show=true
elif [ "$command" == 'unmute' ]; then
	pactl set-sink-mute @DEFAULT_SINK@ 0
	show=true
elif [ "$command" == 'settings' ]; then
	pavucontrol || pavucontrol-qt
elif [ "$command" == 'startup' ]; then # isn't used in this config, just here as reference
	rm -f "${XDG_RUNTIME_DIR}/wob.sock" && mkfifo "${XDG_RUNTIME_DIR}/wob.sock" && tail -f "${XDG_RUNTIME_DIR}/wob.sock" | wob
elif [ "$command" == 'kill' ]; then # run when wob is no longer needed
	pkill wob
fi

if [ -n "$show" ]; then
	volume_amount="$(pactl get-sink-volume @DEFAULT_SINK@ | head -n 1 | awk '{print substr($5, 1, length($5)-1)}')"
	background_color='#000000FF'
	border_color='#FFFFFFFF'
	bar_color='#FFFFFFFF'
	# if muted, change the background color to red
	[ "$(pactl get-sink-mute @DEFAULT_SINK@ | awk '{print $2}')" == 'yes' ] && background_color='#FF000088'
	# echo to the sock file what to show
	# <value> or
	# <value> <#background_color> <#border_color> <#bar_color>
	echo "${volume_amount} ${background_color} ${border_color} ${bar_color}" > "${XDG_RUNTIME_DIR}/wob.sock"
fi
Clone this wiki locally