Dual monitor, single monitor, and HDMI output switcher (including audio) for linux with PulseAudio

technology.pngOne of the minor annoyances I’ve had with computers are the ways they handle switching internal monitor, dual monitor, external monitor, and HDMI outputs. I was able to handle this to some degree with linux 1, using xrandr, as I originally helped someone with in this thread at UbuntuForums. That did not, however, help with HDMI – especially when it came to HDMI sound outputs. I found the pacmd utility to get the pulseaudio sound server to change where it was pointing without too much difficulty.

I created a somewhat complex script to handle all of this – pastebin of the bash script here. It’s a really good example of something that looks intimidating actually being fairly simple when you take each bit in turn.

1. After resetting the monitors, I use xrandr to tell me what is connected – and if it includes VGA output.
1a. If it’s not hooked up to VGA output, then check and see if you’re connected to HDMI output.
1aa. Not hooked up to either? Then just set up the internal monitor.
1ab. Hooked up to HDMI? Set HDMI as the output of choice and turn off the laptop monitor. Keep a record that we’re hooked up to HDMI
1b. Hooked up to VGA, then. Dual monitor output it is. Keep a record that we’re hooked up to VGA.

2. Use feh to set my background.

3. Use that record of what we’re hooked up to.
3a. Was it HDMI? Point the sound output that way.
3b. Everything else, point it to our normal duplex output.

If you take a look at the script with this guide, the structure should start to make some kind of sense.

1 I run openbox over ubuntu 10.04, for those who care…