0

After updating to 20.04 I found that the headphone jack does not work as it should. So as workaround I thought of running a script on an acpi event. eg when headphone jack is plugged-in to reload the sound module. This is the script:

#!/bin/sh

#if [ -z "$1" ]; then
#  echo "Pulseaudio has these cards:"
#  pacmd list-cards | grep 'name: '
#fi

MODULE_INDEX=`pacmd list-modules | tac | grep -A 10 -e "argument: .*$1" | grep 'index:' | head -n 1 | cut -d ':' -f 2 | tr -d ' '`
MODULE_NAME=`pacmd list-modules | tac | grep -A 10 -e "argument: .*$1" | grep 'name:' | head -n 1 | cut -d ':' -f 2 | tr -d '<>'`
MODULE_ARGUMENTS=`pacmd list-modules | tac | grep -e "argument: .*$1" | head -n 1 | cut -d ':' -f 2 | tr -d '<>'`
#echo "Module index is $MODULE_INDEX"
#echo "Module name: $MODULE_NAME"
#echo "Module args: $MODULE_ARGUMENTS"

if [ -z "$MODULE_INDEX" ]; then echo "Could not find module index"; exit 0; fi
if [ -z "$MODULE_NAME" ]; then echo "Could not find module name"; exit 0; fi
if [ -z "$MODULE_ARGUMENTS" ]; then echo "Could not find module arguments"; exit 0; fi

#echo "Unloading module"
pacmd unload-module $MODULE_INDEX
#echo ""
#echo "Reloading module"
pacmd load-module $MODULE_NAME $MODULE_ARGUMENTS
#echo ""

This works fine if I run it manually.

So next I want to run it on the acpi event. Using acpi_listen when I plug in the jack I get

jack/headphone HEADPHONE plug

Next in /etc/acpi/events I create file that contains

event=jack/headphone HEADPHONE plug
action=/etc/acpi/soundcard2.sh

I restart acpid with

sudo service acpid restart

(I even restarted the system) But when I plugin the headphone jack nothing happens. Am I doing something wrong ?

Thanks in advance

dlin
  • 3,900

1 Answers1

0

I only have Ubuntu 18.04 so perhaps systemd has taken over control of more acpi events in 20.04 and you will need to find out how to disable that (e.g. in /etc/systemd/logind.conf). To get logging I added a line to /etc/default/acpid

OPTIONS=--logevents

and after sudo systemctl restart acpid I saw events in the systemd journal when plugging in the headphone, and it ran my script ok:

$ sudo journalctl -u acpid -f
... acpid[2554]: 9 rules loaded
... acpid[2554]: waiting for events: event logging is on
... acpid[2554]: received input layer event "jack/headphone HEADPHONE plug"
... acpid[2554]: rule from /etc/acpi/events/meuh matched
... acpid[2558]: executing action "/home/meuh/bin/myscript"
... acpid[2554]: action exited with status 0
... acpid[2554]: 1 total rule matched
... acpid[2554]: completed input layer event "jack/headphone HEADPHONE plug"
meuh
  • 3,444