Lukas pointed out on IRC, that the hardware for detecting whether headphones are plugged in or not is wired as it should. Detecting it is a software problem that nobody sat down and solved yet.
Alas, until this is solved, let me present another solution that toggles between speakers and headphones using waybar, based on the solution by @lykso. Here is my diff of my ~/.config/waybar/config
:
--- /etc/skel/.config/waybar/config 2022-10-23 13:04:34.000000000 +0200
+++ /home/josch/.config/waybar/config 2022-11-19 16:40:12.239605516 +0100
@@ -6,7 +6,7 @@
// Choose the order of the modules
"modules-left": ["sway/workspaces", "sway/mode", "sway/window"],
"modules-center": [],
- "modules-right": ["tray", "network", "custom/stat", "memory", "temperature", "pulseaudio", "clock"],
+ "modules-right": ["tray", "network", "custom/stat", "memory", "temperature", "custom/patoggle", "pulseaudio", "clock"],
"sway/window": {
},
"sway/mode": {
@@ -61,5 +63,13 @@
"interval": 0,
"exec": "reform-compstat -d 1 -i 0.3",
"on-click": "gnome-system-monitor"
+ },
+ "custom/patoggle": {
+ "format": "{}",
+ "interval": "once",
+ "return-type": "json",
+ "exec": "patoggle status",
+ "on-click": "patoggle",
+ "signal": 8
}
}
And here is my ~/.local/bin/patoggle
script:
#!/bin/sh
set -eu
ret=0
pacmd list | grep -q "active port: <analog-output-speaker>" || ret=$?
if [ "${1:-}" = "status" ]; then
if [ "$ret" -eq 0 ]; then
echo '{"text": "🔈", "tooltip": "speakers"}'
else
echo '{"text": "🎧", "tooltip": "headphones"}'
fi
else
if [ "$ret" -eq 0 ]; then
pacmd set-sink-port 0 analog-output-headphones
else
pacmd set-sink-port 0 analog-output-speaker
fi
pkill -SIGRTMIN+8 waybar
fi
Essentially, the script can be called with or without the status
argument. If it’s walled with status
as the first argument it outputs json as can be interpreted by waybar, setting the icon and tooltip text to the currently selected output sink, either
for speakers or
for headphones. In all other cases it toggles the output between headphones and speakers and sends a signal to waybar, forcing the update of the icon to the current value. That way, I can also set up a sway keybinding which will toggle the audio sink and update the icon in waybar:
bindsym $mod+F6 exec patoggle
The only thing that my current setup cannot do is to switch the icon of the output sink gets changed in any other way, for example by using pavucontrol. This could be fixed letting waybar call patoggle status
in regular intervals. So set interval
from once
to a number of seconds if you need that.