Pause media when unplugging headphones on Linux
I've had a couple of unfortunate accidents where I removed my headphones from my laptop only to have the entire cafe/coworking/airplane cabin rudely introduced to my (patrician) music tastes.
To work around this, you can use a combination of
$ yay -S acpid playerctl
playerctl is just a convenience wrapper for MPRIS media
events. You can skip installing it if
you prefer the long
dbus-send commands that are listed in the wiki.
acpid is a daemon that outputs messages whenever things happen relating to
power or various buttons/lids on your device:
$ acpi_listen battery PNP0C0A:00 00000080 00000001 cd/play CDPLAY 00000080 00000000 K cd/play CDPLAY 00000080 00000000 K jack/headphone HEADPHONE unplug jack/headphone HEADPHONE plug jack/headphone HEADPHONE unplug jack/headphone HEADPHONE plug button/lid LID close button/lid LID open battery PNP0C0A:00 00000080 00000001
To subscribe to these events and run scripts whenever one occurs, you can
create a file in
/etc/acpi/events, for example:
$ sudo vim /etc/acpi/events/stop-music-when-headphones-unplug event=jack/headphone HEADPHONE unplug action=su - USER -c "env DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus playerctl pause"
dbus related command like
playerctl, you'll have to
su - YOUR_USER
and also export the display session and the
$ sudo systemctl enable --now acpid
And that should be it, when you unplug your headphones
journalctl -fu acpid
should show a message:
Apr 16 16:11:30 fwk su: (to deity) root on none Apr 16 16:11:30 fwk su: pam_unix(su-l:session): session opened for user deity(uid=1000) by (uid=0) Apr 16 16:11:30 fwk su: pam_unix(su-l:session): session closed for user deity Apr 16 16:11:30 fwk root: ACPI group/action undefined: jack/headphone / HEADPHONE