3

When I unplug the headphones, the sound device is set automatically to "Digital Output (S/PDIF)" instead of the "HDMI / Displayport" that I was using before plugging the headphones.

How can I configure the system so that it uses the headphones when they are connected, and the HDMI output when they are not? I do not want the system to use "Digital Output (S/PDIF)" at all.

Óscar
  • 1,006

1 Answers1

2

According to 'user.dz' - you can trigger a script when headphones is disconnected.

when Headphone disconnects - use ACPI to trigger a script

(What code is executed when headphones are disconnected?).

In most systems if not all, ACPI can handle this event. To test that:

Run acpi_listen

Unplug & replug headphones, example output: (mic/ears share in same >jack on my laptop)

jack/headphone HEADPHONE unplug jack/microphone MICROPHONE unplug jack/headphone HEADPHONE plug jack/microphone MICROPHONE plug

Put your-script.sh in /etc/acpi/

Add an event trigger file for your script in /etc/acpi/events/

event=jack/headphone HEADPHONE unplug action=/etc/acpi/your-script.sh

Check the other files there to learn from.

You may need to restart acpid service to reload changed rules in >/etc/acpi /events/

sudo service acpid restart

Default Headphone setting within terminal for the script

Could be done with recommendation to 'Takkat's answer from

(How can I change the default audio device from command line?)

You can control PulseAudio thoroughly through the command line using pacmd and >pactl commands. For options see man pages or the wiki at PulseAudio:

pacmd list-sinks (or pactl list short sinks) for name or index number of possible >sinks

pacmd set-default-sink "SINKNAME" to set the default output sink

pacmd set-default-source "SOURCENAME" to set the default input

pacmd set-sink-volume index volume

pacmd set-source-volume index volume for volume control (65536 = 100 %, 0 = mute; or a bit more intuitive 0x10000 = 100 %, 0x7500 = 75 %, 0x0 = 0 %)

and many many more CLI options.

This is my first answer - I hope it helps and pardon/let me know if theres anything I should've done differently.

Jake Cope
  • 46
  • 5
  • 1
    Thanks! The only think missing from your reply was adding this line before the pacmd in the script: export PULSE_RUNTIME_PATH="/run/user/<your user id>/pulse/" This is needed because ACPI scripts run as root and pulse audio runs and the logged user. – Óscar Sep 26 '20 at 09:20
  • 1
    The problem with this solution is that it breaks Ubuntu sound configuration. Now when I go to the Sound Settings window it is empty, there are no output devices... – Óscar Sep 26 '20 at 09:26
  • 1
    To avoid breaking Ubuntu sound settings you should use action=/bin/su -c "/etc/acpi/your_script.sh" - your_username – Óscar Sep 26 '20 at 09:33
  • Thanks alot for the details - think I'll do the same myself now! - cheers ~ – Jake Cope Sep 26 '20 at 10:15