Ubuntu 12.04: Sound Toggle 1.3 – HDMI sound toggle/switch

So I did a few edits to my script that allows me to:

  • Enable/disable the lockscreen. That way, I can close my laptop without having the lockscreen coming up.
  • Restore the backlight level based off of set values.
  • Added notifications.

# Sound Toggle
# By Charles Cruz
# The following script toggles the between laptop speakers and hdmi audio (if detected).
# This also disables/enables
# Version 1.3
########### Settings ###########
# Backlight Settings
USERID="$(cat /var/run/ConsoleKit/database | grep -B 6 is_active=true | grep uid= | cut -f 2 -d '=')"
USER="$(grep $USERID /etc/passwd | cut -f 1 -d ':')"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"
GNOME_SCREENSAVER_PROC=`ps xa | grep gnome-screensaver | head -n 1 | awk '{print $1}'`
export XAUTHORITY="/home/$USER/.Xauthority"
export DISPLAY="$(cat /var/run/ConsoleKit/database | grep x11_display= | cut -f 2 -d '=')"
# HDMI is connected
if [ "$HDMI_STATUS" = connected ]; then
# Send notification
sudo -u $USER notify-send -i display "HDMI connected"
# Set the sound card profile
sudo -u $USER pactl set-card-profile 0 output:hdmi-stereo+input:analog-stereo
# Disable the lockscreen if it's enabled so you can close the lockscreen
if [[ $(sudo -u $USER gsettings get org.gnome.desktop.screensaver lock-enabled) = true ]]; then
sudo -u $USER gsettings set org.gnome.desktop.screensaver lock-enabled false
# HDMI is not connected
# Send notification
sudo -u $USER notify-send -i computer "HDMI disconnected"
# Set the sound card profile
sudo -u $USER pactl set-card-profile 0 output:analog-stereo+input:analog-stereo
# Restore the lock setting, if necessary
if [[ $(sudo -u $USER gsettings get org.gnome.desktop.screensaver lock-enabled) = false ]]; then
sudo -u $USER gsettings set org.gnome.desktop.screensaver lock-enabled true
# Restore default battery/power brightness
cat /proc/acpi/ac_adapter/AC/state | grep "on-line"
if [ $? -eq 0 ]; then
xbacklight -set $BACKLIGHT_AC # Power cable is connected
xbacklight -set $BACKLIGHT_BATTERY # Power cable is not connected
exit 0

BTW, I renamed the script to “toggle-sound” so if you used a different name, be sure to change the name in the instructions below.


  1. In a terminal:
    sudo gedit /usr/local/bin/toggle-sound
  2. Copy and paste the script above into the document. Save and close it.
  3. In the terminal:
    sudo chmod 755 /usr/local/bin/toggle-sound
    echo 'KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/toggle-sound"' | sudo tee /etc/udev/rules.d/hdmi.rules
    sudo udevadm control --reload-rules

19 thoughts on “Ubuntu 12.04: Sound Toggle 1.3 – HDMI sound toggle/switch

    • In a terminal, enter: monitor udevadm
      Afterwards, try plugging/unplugging your HDMI cable. I get some output about drm/card0. What does yours say? My laptop only has Intel HD 3000 graphics, so if you have another graphics card onboard, maybe that’s why it isn’t working? Also, you could try searching in /sys for anything related to “HDMI”. For me, /sys/class/drm/card0-HDMI-A-1 is only a link to some other, longer directory. Again, I only have Intel graphics on my laptop so I’m unsure of other scenarios. Hope you figure it out!

      • Uhh…”monitor udevadm” opens the Android device monitor. Is that supposed to happen?
        “lspci | grep VGA”: ATI RS880M [Mobility Radeon HD 4200 Series]
        “lspci | grep HDMI”: ATI RS880 HDMI Audio [Radeon HD 4200 Series]
        The only thing I’ve found related to HDMI is /proc/asound/HDMI. I noticed your script uses the “status” file, and mine appears to be located in /proc/asound/HDMI/pcm3p/sub0

      • I’m sorry! I meant to say “udevadm monitor”!

        When you plug/unplug your hdmi cable, does the /proc/asound/HDMI/pcm3p/sub0/status change from “connected” to “disconnected”? If so, then you probably need to change the HDMI_STATUS line to HDMI_STATUS="$(cat /proc/asound/HDMI/pcm3p/sub0/status)". Also, your udev rule will be different. What’s the output of udevadm monitor when you plug/unplug your hdmi cable?

      • Yeah, the status changes. However, I’m not getting any output from “udevadm monitor”…It tells me
        “monitor will print the received events for:
        UDEV – the event which udev sends out after rule processing
        KERNEL – the kernel uevent”
        and then sits there with a blinking cursor. Somehow I’m not triggering any events.

      • (I believe) “udevadm monitor” should show changes in the hardware of your system. For instance, if you plug in a USB flash drive, change the brighness, and so forth, then these changes should be printed in the terminal (under the header you quoted). But if plugging in your HDMI cable doesn’t produce any output, then I don’t know what else you could do; the setup for the sound toggle uses udev to detect the changes and if it isn’t being picked up, it won’t run the script.

  1. forget to change permissions at first but once I took care of that this is working great.

    Thanks for the advice this has been one of those annoyances that finally got me to get off my but and look for a solution today. I wish this was the default behavior.

  2. Just set up this script on my Acer Aspire running Ubuntu 12.10 and it seems to work just fine. Tested it briefly last night, taking it into the classroom today. Great solution to an annoying problem Charles, thanks!

  3. not exactly sure what your script is supposed to do, when i use it it lets me know that i plugged the hdmi in and switches to it correctly, but if i unplug it then i have no sound and your script doesnt seem to make it switch back. your older script worked better atleast until a reboot. the whole alt f2 and typing in killall pulse still doesnt make this an auto toggle at all….

    • Sorry it didn’t work. It could be the audio profiles being different and you may need to change that part. That’s all I can think of :/

      Also, sorry to everyone if don’t have time to approve/comment back. Super busy and I thought this script was okay/fixable to many.

  4. Maybe you could update your script with the “killall pulsaudio” command and post that up, I’ve tried implementing it in both of your scripts and it doesnt work and nobody seems to be able to help me out with this.

    • You shouldn’t need to kill pullaudio to get it to work. But every laptop is different, so I don’t know; if you need it, you can add it to your script for your own needs.

  5. never mind, im going back to windows after this crap, it may screw up all the time but when it is working it blows typing in a damn terminal to fix crap outta the water, sorry to bug you……….

  6. So Even after completely reinstalling this damn distro and using youre script it doesnt work, at all it extends my desktop, and when i close the lid on my laptop the gnome bars dissappear, the sound doesnt switch at all, if you’re going to post up instructions, you need to include your particular drivers and chipsets and everything else involved cause im so damn pissed right now im about to toss my fucking laptop…………. This shit should work outta the box …. period…. windows does this on the fly.

  7. So After i played around with all kinds of settings i finally got it working. please the next time you post up some instructions, assume anyone whos reading is like 5 alright, you didnt say anything about mirroring displays then disabling the laptop display, or anything.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s