You are not logged in.

#1 2025-05-04 08:41:06

micko01
void main()
From: Queensland, Australia
Registered: 2024-04-07
Posts: 497
Website

demo: Yad --notification volume control

This is more POC than to be actually useed, but that said I'm using it!

It is a shell script that displays a yad status icon in the systray that is adjustable. As stated, POC.

It's gleaned from many sources (links below)  with a bit of my spin included. Note, only for X11. Will not work in wayland and also newer DE's, panels etc.

I've named it simply yvol.sh but you can do as you will.

#!/bin/bash

#*******************            variables           *******************#

# create a FIFO file, used to manage the I/O redirection from shell
PIPE=$(mktemp -u --tmpdir ${0##*/}.XXXXXXXX)
mkfifo $PIPE
export PIPE

# attach a file descriptor to the file
exec 3<> $PIPE

# default Icon
ICO='audio-volume-medium'

#*******************            functions           *******************#

# get volume
get_vol() {
	pactl list sinks |\
	grep -o 'Volume.*right'|grep -o '[0-9]*%.*/'|sed 's/% \///'
}; export -f get_vol

# set volume
set_vol() {
	VAL=$1
	pactl -- set-sink-volume 0 ${VAL}%
	on_scale
}; export -f set_vol

# add handler to manage process shutdown
function on_exit() {
	echo "quit" >&3
	rm -f $PIPE
}
trap on_exit EXIT

# add handler for tray icon left click
function on_scale() {
	VOL=$(get_vol)
	if [[ $VOL -eq 0 ]]; then
		ICO=0
	elif [[ $VOL -gt 0 && $VOL -lt 34 ]]; then
		ICO=1
	elif [[ $VOL -ge 24 && $VOL -lt 67 ]]; then
		ICO=2
	else
		ICO=3
	fi
	echo $ICO #debug
	exec 3<> $PIPE
	case $ICO in
		0)echo "icon:audio-volume-muted" >&3;;
		1)echo "icon:audio-volume-low" >&3;;
		2)echo "icon:audio-volume-medium" >&3;;
		3)echo "icon:audio-volume-high" >&3;;
	esac
}; export -f on_scale

_yad_gui() {
	CVOL=$(get_vol)
	VAL=$(yad --scale \
		--value=$CVOL \
		--max-value=150 \
		--print-partial \
		--mark 0:0 \
		--mark=50:50 \
		--mark=100:100 \
		--mark=150:150 \
		--no-buttons \
		--width=350 \
		--close-on-unfocus \
		--undecorated \
		--mouse \
		--skip-taskbar)
	[[ -n "$VAL" ]] && set_vol $VAL
}; export -f _yad_gui

yad --notification \
	--listen \
	--image=audio-volume-low \
	--text="Volume Control" \
	--command="bash -c _yad_gui" <&3

5ps8OXdt.png

It's using pactl and could be extended with a menu. A middle click on the status icon closes the app (yad default, can be overridden). The icon updates as you increase the volume but only for the next use, which is good enough for some brief visual feedback. Of course the tool-tip could be made to update too.

This was kind of inspired by the drop box saga, see https://forums.bunsenlabs.org/viewtopic.php?id=9396

Do with it what you will!

Refs:
https://yad-guide.ingk.se/notification/ … tification
https://code.google.com/archive/p/yad/w … nIcon.wiki
http://yad-guide.ingk.se/scale/yad-scal … nc_buttons
https://ubuntu-mate.community/t/pulse-a … tion/15669
https://daemonforums.org/showthread.php?t=12298

Ha, someone somewhat familiar posting in there somewhere. smile


#!/bin/sh
echo '#include <stdio.h>\nvoid main() { printf("Hi, bunsenlabs\\n"); return; }' > bunsen.c
gcc bunsen.c -o bunsen
./bunsen

Offline

#2 2025-05-04 10:15:56

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: demo: Yad --notification volume control

The get_vol() function can be simplified to

pactl list sinks | awk '/Volume.*right/{gsub(/%/,"");print $5}'
micko01 wrote:

someone somewhat familiar posting

I do get around big_smile

Offline

#3 2025-05-04 15:20:40

marens
Member
From: World without M$
Registered: 2023-02-02
Posts: 872

Re: demo: Yad --notification volume control

^ The same here:

pactl list sinks | grep dB, | awk '{print $5}' | sed 's/.$//'

If people would know how little brain is ruling the world, they would die of fear.

Offline

#4 2025-05-04 16:11:50

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: demo: Yad --notification volume control

^ My point was that the 3 filtering commands could be replaced with a single awk pass. It's not really important for such a short script but I really like efficiency.

Offline

#5 2025-05-04 17:14:20

marens
Member
From: World without M$
Registered: 2023-02-02
Posts: 872

Re: demo: Yad --notification volume control

^ Of course I agree with you.

P.S.
I always try to find a solution in my own way.
Maybe sometimes it's not the most efficient, but it's important that it works.
It can always be improved later.


If people would know how little brain is ruling the world, they would die of fear.

Offline

#6 2025-05-05 06:21:47

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,652
Website

Re: demo: Yad --notification volume control

For those interested in the general topic of using yad as a notification icon, there's another example in bunsen-apt-update-checker
I had much help from @sleekmason in putting the yad code together:
https://github.com/BunsenLabs/bunsen-ap … date-check


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#7 2025-05-05 11:33:45

micko01
void main()
From: Queensland, Australia
Registered: 2024-04-07
Posts: 497
Website

Re: demo: Yad --notification volume control

^ @johnraff, I did mention it as a possibility for dropbox --> https://forums.bunsenlabs.org/viewtopic … 70#p142970

---

Anyway @HoaS and @marens I did some tests:

  1. a reworked bash only version - the advantage here is no externals

  2. @HoaS' version

  3. @marens's version

  4. my orig

mick@dellhome:~$ time y=$(while read -r x ; do [[ "${x:0:7}" == "Volume:" ]] && echo ${x/Volume: front-left:} || continue;done <<<$(pactl list sinks));y=${y/*[0-9]\ \/\ };y=${y%%\%*};echo $y

real	0m0.014s
user	0m0.005s
sys	0m0.008s
40
mick@dellhome:~$ time pactl list sinks | awk '/Volume.*right/{gsub(/%/,"");print $5}'
40

real	0m0.011s
user	0m0.004s
sys	0m0.011s
mick@dellhome:~$ time pactl list sinks | grep dB, | awk '{print $5}' | sed 's/.$//'
40

real	0m0.012s
user	0m0.006s
sys	0m0.017s
mick@dellhome:~$ time pactl list sinks | grep -o 'Volume.*right'|grep -o '[0-9]*%.*/'|sed 's/% \///'
40

real	0m0.012s
user	0m0.008s
sys	0m0.013s

Really, the speed differences are negligible. They may only make a difference on really old challenged kit. I'm on a 2018 core i5 with 8gig RAM.

The only interesting difference is that the bash version prints the value after 'time' has done it's thing, which at the end of the day is irrelevant.

EDIT: bug in the bash version.

Last edited by micko01 (2025-05-05 12:01:59)


#!/bin/sh
echo '#include <stdio.h>\nvoid main() { printf("Hi, bunsenlabs\\n"); return; }' > bunsen.c
gcc bunsen.c -o bunsen
./bunsen

Offline

#8 2025-05-05 11:35:47

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: demo: Yad --notification volume control

I win! \o/

EDIT: try the pure bash version with https://packages.debian.org/bookworm/mksh instead, it's literally twice as fast as bash.

Last edited by Head_on_a_Stick (2025-05-05 11:38:46)

Offline

#9 2025-05-05 12:30:47

micko01
void main()
From: Queensland, Australia
Registered: 2024-04-07
Posts: 497
Website

Re: demo: Yad --notification volume control

mick@dellhome:~$ time /usr/bin/mksh -c 'y=$(pactl list sinks | while read -r x ; do [[ "${x:0:7}" == "Volume:" ]] && echo ${x/Volume: front-left:} || continue;done);y=${y/*[0-9]\ \/\ };y=${y%%\%*};echo $y'
40

real	0m0.015s
user	0m0.000s
sys	0m0.014s

Not bad smile

Also, busybox ash isn't too bad either.

time /usr/bin/busybox ash -c 'y=$(pactl list sinks | while read -r x ; do [[ "${x:0:7}" == "Volume:" ]] && echo ${x/Volume: front-left:} || continue;done);y=${y/*[0-9]\ \/\ };y=${y%%\%*};echo $y'
40

real	0m0.018s
user	0m0.006s
sys	0m0.011s

Of course none of this is really all that scientific because I have a browser open with several tabs, other background processes, etcetera. It's fun though smile


#!/bin/sh
echo '#include <stdio.h>\nvoid main() { printf("Hi, bunsenlabs\\n"); return; }' > bunsen.c
gcc bunsen.c -o bunsen
./bunsen

Offline

#10 2025-05-05 13:03:15

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: demo: Yad --notification volume control

I'm surprised your results for mksh & ash aren't quicker but:

type time

So this would be a fairer comparison with bash:

time /usr/bin/bash -c 'y=$(pactl list sinks | while read -r x ; do [[ "${x:0:7}" == "Volume:" ]] && echo ${x/Volume: front-left:} || continue;done);y=${y/*[0-9]\ \/\ };y=${y%%\%*};echo $y'

And for ash try Debian's /bin/sh instead, that's probably quicker than the Busybox version.

The fastest Bourne shell of all is still ksh93 because it refuses to open subshells unless absolutely necessary.

EDIT: https://unix.stackexchange.com/question … 93-so-fast

Last edited by Head_on_a_Stick (2025-05-05 17:27:27)

Offline

#11 2025-05-06 01:45:40

marens
Member
From: World without M$
Registered: 2023-02-02
Posts: 872

Re: demo: Yad --notification volume control

micko01 wrote:

Really, the speed differences are negligible.

This was expected because we had the same idea:

  • Search (Find)

  • Remove the %  sign

  • Print

I like this approach, but it doesn't have much impact on such a short code:

HoaS wrote:

My point was that the 3 filtering commands could be replaced with a single awk pass.

Off topic.
Honestly, I'm not a fan of Yad notifications.

I mostly use notify-send  (libnotify-bin) in my scripts.
https://linuxconfig.org/how-to-send-des … otify-send

This is a useful script (as an example) for those who use redshift.

~/.config/redshift/hooks/transition.sh

#!/bin/sh

sleep 60

case $1 in
    period-changed)
        exec notify-send --hint=string:sound-name:dialog-question "Redshift" "Eyes protection changed to $3 mode" --icon=redshift
esac

I added a sound warning and an icon in the notifications.
Also, the start is delayed by one minute after startup.

The original script is located at the end of man redshift.

P.S.
You might find this interesting because there are many possibilities for using it in various apps.


If people would know how little brain is ruling the world, they would die of fear.

Offline

Board footer

Powered by FluxBB