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,830

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,211
  • After enableing logging I see the same thing as you. The script seems to be executed but nothing happens I also get "action exited with status 0" but although the script seems to have run nothing happens. Still no sound. If I run the script manually it runs fine – user3292026 Apr 30 '20 at 17:28
  • You could change your script to exit 1 when there is an error, rather than exit 0 as it does now. Also, you could get it to write to a file by putting exec >/tmp/log 2>&1 after line 1. pulseaudio might be expecting to be running from a user login environment these days; systemd changed a lot of things in that area. – meuh Apr 30 '20 at 17:35
  • 1
    So I figured it out. The problem was that acpid script should run as a regular user, So I modified the "action" section as follows action=/bin/su -c "/etc/acpi/soundcard2.sh alsa" - username. Reason is that as root pulseaudio is not running so It can not reload something it is not already running. Thanks very much meuh for your help – user3292026 Apr 30 '20 at 17:51