Friday, January 12, 2018

Audio Switch Daemon -- Hotplug Headphone Jack

#! /bin/bash
  ######################################################################
 ######################################################################
## Author: A.M. Danischewski, Created Date: 20180112, Version: v1.00
## Issues: If you find any issues emai1 me at my <first name> dot 
##         <my last name> at gmail dot com.  
##
## This is a simple script to automatically switch between the HDMI and 
## internal speakers when the headphone jack is plugged/unplugged. 
## Unfortunately ALSA events along these lines as far as I know do not 
## generate udev events (yet?), as evidenced by udev monitor. 
##  
## Save this file to /opt/system_scripts/audio_hotplug.bsh, and add the 
## following to your .bashrc file (uncomment the two ##'s to the left): 
##  if ! ps -ef | grep -q '[a]udio_hotplug.bsh'; then 
##    #Disable monitor mode, so that the new process will inherit the 
##    #PPID of the *parent* of the calling shell (/sbin/upstart --user), 
##    #therefore the process will continue running if the shell exits, 
##    #since SIGHUP is only sent to processes with the same PPID. 
##   set -m 
##   /opt/system_scripts/audio_hotplug.bsh &
##  fi 
## 
## Make sure the permissions are executable and test it out. If there is 
## a problem, check what value is returned from: 
##  $> grep -m1 'Amp-Out vals' /proc/asound/card0/codec#0
## 
## When the headphone jack is plugged and when its not. 
##
## If you have multiple cards make sure that correct card is referenced.
## 
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
 ######################################################################
  ######################################################################

declare USERNAME="${USER}" ## Change this to your username if necessary. 
declare SLEEPNICE=1 
declare ALREADY_SET=0
declare AMPOUT="" 

while (sleep ${SLEEPNICE}); do 
 AMPOUT=$(grep -m1 'Amp-Out vals' /proc/asound/card0/codec#0)
 ((${ALREADY_SET})) && grep -Eq 'Amp-Out vals:  \[0x57 0x57\]' <<< "${AMPOUT}" && ALREADY_SET=0
 ################# Note: Its \[0x57 0x57\] when connected.
 if ((${ALREADY_SET}==0)) &&  grep -Eq 'Amp-Out vals:  \[0x00 0x00\]' <<< "${AMPOUT}"; then 
  if grep -q "^connected" /sys/class/drm/card*-HDMI-A-1/status; then 
          sudo -u ${USERNAME} -E pacmd set-card-profile 0 output:hdmi-stereo
  else
          sudo -u ${USERNAME} -E pacmd set-card-profile 0 output:analog-stereo
  fi
  ALREADY_SET=1
 fi 
done 

exit 0