Setting up Volume and Brightness Keys with Percentage Indicators with a Tiling WM

- Posted in Tutorial by

Hello readers, and welcome to our first actual tutorial! So setting up volume and brightness keys with a tiling WM is pretty much trivial, and may even already be done for you. For reference, the keys you want to use are called XF86AudioRaiseVolume, XF86AudioLowerVolume, XF86AudioMute, XF86MonBrightnessUp, XF86MonBrightnessDown, etc, with the full list of supported keys being at I use sxhkd, so you may need to consult your keybind or window manager's documentation for the correct syntax, but the actual commands will be the same. So basically, I just use pamixer and xbacklight like so:

pamixer --increase 5
pamixer --decrease 5
pamixer --toggle-mute
xbacklight -inc 10
xbacklight -dec 10

This gets the job done, but we can go further. Wouldn't it be nice if a little box popped up telling us what the new volume or brightness is whenever we change it? Preferably without spawning a million of them for every time you use the shortcut of course. Thankfully, dunstify has a feature for this, namely notification ID's, which let you overwrite an existing notification by giving them a set ID with the -r [num] flag. To get the current volume, we use pamixer --get-volume-human, which gives us the final command:

dunstify -r 13 $(pamixer --get-volume-human)

For brightness the process is a little more involved, since the max setting in the sysfs file for brightness is often an odd number that doesn't divide nicely, and xbacklight will give the result to the millionths place which is annoying. Fortunately, that's nothing a little awk scripting can't solve, with the command awk '{printf "%.0fn", $1}'. (If you're wondering how it works, well sorry but I don't know awk and I forgot what it said on StackOverflow.) But anyway our final command will be:

dunstify -r 37 $(xbacklight -get | awk '{printf "%.0fn", $1}')%

Now we can just append these commands with an AND operator (the "&&" thing), and depending on your window/shortcut manager you may even be able to use shorthand to condense the commands in your config file. For me the final result is:

{XF86AudioRaiseVolume, XF86AudioLowerVolume, XF86AudioMute}
    pamixer {--increase 5, --decrease 5, --toggle-mute} && dunstify -r 13 $(pamixer --get-volume-human)
{XF86MonBrightnessUp, XF86MonBrightnessDown}
    xbacklight {-inc, -dec} 10 && dunstify -r 37 $(xbacklight -get | awk '{printf "%.0fn", $1}')%

Hope you found this tutorial helpful, if you liked it then please be sure to subscribe to my RSS feed, and until I get email protection setup if you have any questions then please feel free to message me over on Gab.