Short answer
- To see notifications you have to install notify-osd
sudo apt install notify-osd
- Create a text file
<my_name>.sh
in your /home/<user_name>/
directory. And add execute permission to the file chmod +x /home/<user_name>/<my_name>.sh
Paste the text below to the <my_name>.sh
file
#!/bin/bash
acpi_listen | while IFS= read -r line;
do
if [ "$line" = "jack/headphone HEADPHONE plug" ]
then
amixer -D pulse set Master mute
notify-send "headphones connected. Sound is muted"
elif [ "$line" = "jack/headphone HEADPHONE unplug" ]
then
amixer -D pulse set Master mute
notify-send "headphones disconnected. Sound is muted"
fi
done
Go to Startup Configurations menu of your linux system (ubuntu, kubuntu, xubuntu etc). Set /home/<user_name>/<my_name>.sh
file to autostart with you desktop environment (xfce, KDE, Gnome etc).
Warning! If you use KDE then DON NOT add the file for "Start script before KDE"
or "Application"
with bash /home/<user_name>/<my_name>.sh
.
Use as script (scenario) and "Start WITH KDE".
IF you did that already then press ctrl + alt + F2
, login as user and delete rm /home/<user_name>/<my_name>.sh
. If you don't have permissions to delete then remount your disk before deleting mount -o remount, rw /
.
How to edit the script
Some linux based systems cache autostart scripts. That means even if you edit /home/<user_name>/<my_name>.sh
and logout / login
or restart
the system then the script will stay the same.
In KDE you can find the cached script symbolic link in /home/<user_name>/.config/autostart-scripts/<my_name>.sh
. Delete the <my_name>.sh
file manually, logout / login
system, add the script to autostart
again and logout / login
again.
It seems to me it's KDE bug. I don't know about other DE (xfce, Gnome etc)
What you need to know
You can see text as "jack/headphone HEADPHONE plug"
in the code above. It's the headphones event name.
To see events:
1. Type in the console acpi_listen
2. Plug and unplug headphones (or another devices)
3. You will see event names in the console. It'll be something like a text below
user@user-PC:~$ acpi_listen
jack/headphone HEADPHONE unplug
jack/microphone MICROPHONE unplug
jack/headphone HEADPHONE plug
jack/microphone MICROPHONE plug
If event names in the script above are different then replace them by your event names from the console output.
ACPI and events
To see and test ACPI events you can use evtest
sudo apt install evtest
user@user-PC:~$ sudo evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0: Lid Switch
/dev/input/event1: Power Button
/dev/input/event2: Power Button
/dev/input/event3: AT Translated Set 2 keyboard
/dev/input/event4: Video Bus
/dev/input/event5: SynPS/2 Synaptics TouchPad
/dev/input/event6: COMPANY USB Device
/dev/input/event7: COMPANY USB Device
/dev/input/event8: COMPANY USB Device
/dev/input/event9: HP Wireless hotkeys
/dev/input/event10: HDA Intel HDMI HDMI/DP,pcm=3
/dev/input/event11: HDA Intel HDMI HDMI/DP,pcm=7
/dev/input/event12: HDA Intel HDMI HDMI/DP,pcm=8
/dev/input/event13: HDA Intel HDMI HDMI/DP,pcm=9
/dev/input/event14: HDA Intel HDMI HDMI/DP,pcm=10
/dev/input/event15: HDA Intel PCH Mic
/dev/input/event16: HDA Intel PCH Headphone
/dev/input/event17: HP WMI hotkeys
/dev/input/event18: HP Webcam: HP Webcam
Select the device event number [0-18]: 16
Input driver version is 1.0.1
Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
Input device name: "HDA Intel PCH Headphone"
Supported events:
Event type 0 (EV_SYN)
Event type 5 (EV_SW)
Event code 2 (SW_HEADPHONE_INSERT) state 1
Properties:
Testing ... (interrupt to exit)
Event: time 1522588668.319567, type 5 (EV_SW), code 2 (SW_HEADPHONE_INSERT), value 0
Event: time 1522588668.319567, -------------- SYN_REPORT ------------
Event: time 1522588670.035275, type 5 (EV_SW), code 2 (SW_HEADPHONE_INSERT), value 1
Event: time 1522588670.035275, -------------- SYN_REPORT ------------
For some reasons /etc/acpi/events/jack
does not work for me. Possibly I have wrong CONFIG_SND_HDA_INPUT_JACK option. To test that use
sudo grep CONFIG_SND_HDA_INPUT_JACK /boot/config-$(uname -r)
My answer is based on this answer.