Volume Controls in dwm

By Erik Letson, Aug 13th, 2020 (blog index)

I've been hacking away at my installation of dwm recently, and this has resulted in some updates to my config.h, which you can check out right here. One particular thing that I think is worth addressing is the way that that I handle volume control. I use a laptop that has media keys, including keys for volume, but I don't like how far away they are from the home row. In dwm, I have configured MOD+Up to raise my volume, MOD+Down to lower it, and MOD+SHIFT+UP and MOD+SHIFT+Down to max and mute it respectively. These keys are easy for me to reach with my pinky fingers, and, as far as dwm goes, this behavior is pretty easily achieved in a generic way using amixer.

amixer is described in the man pages as being a "command-line mixer for ALSA" sound cards. It is very easy to use it to do things like volume control. The man pages are pretty self-explanatory, so I'll just move right on to showing you the commands that I use to utilise amixer in dwm:

  static const char *volupcmd[] = { "/bin/bash", "-c", "amixer -D default sset Master Playback 5%+" };
  static const char *volmaxcmd[] = { "/bin/bash", "-c", "amixer -D default sset Master Playback 100%" };
  static const char *voldowncmd[] = { "/bin/bash", "-c", "amixer -D default sset Master Playback 5%-" };
  static const char *volmutecmd[] = { "/bin/bash", "-c", "amixer -D default sset Master Playback 0%" };

Using the -D default option chooses the default device. If you use Pulseaudio (as I do), this device will be changed on the fly if you were to, say, plug in your headphones, meaning that this command is always going to modifying the right volume knob on the right device. It is totally compatible with regular ALSA by itself as well. As I said, I assigned these to the up and down arrow keys, but in case you want to stick them somewhere else, here's a free link to a nice keysym list you can use to identify your keys.

Return to blog index