#!/bin/bash # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2020-present Shanti Gilbert (https://github.com/shantigilbert) # Copyright (C) 2023 JELOS (https://github.com/JustEnoughLinuxOS) . /etc/profile if [[ ${AUDIO_MANAGEMENT} == "UCM" ]]; then /usr/bin/volume restore exit 0 fi # Sync with the REAL jack state at startup. The kernel keeps the switch # state in the input device (EV_SW/SW_HEADPHONE_INSERT), so query it via # evtest --query (exit 10 = bit set) instead of trusting a saved setting # or the legacy /sys/class/gpio number, which mainline kernels don't # even expose. On these boards the switch is inverted: value 0 means # headphones inserted (see HP_ON below), so bit set = speakers. # Fallback order: input switch, legacy gpio node when it exists, speakers. JACK_STATE="speakers" if [ -n "${DEVICE_HEADPHONE_DEV}" ] && [ -e "${DEVICE_HEADPHONE_DEV}" ]; then evtest --query "${DEVICE_HEADPHONE_DEV}" EV_SW SW_HEADPHONE_INSERT [ $? -eq 0 ] && JACK_STATE="headphone" elif [ -n "${DEVICE_JACK}" ] && [ -e "/sys/class/gpio/gpio${DEVICE_JACK}/value" ]; then [ "$(cat /sys/class/gpio/gpio${DEVICE_JACK}/value)" = "0" ] && JACK_STATE="headphone" fi set_setting "audio.device" "${JACK_STATE}" ### Set the default audio path, needed for some devices if [ -z "${DEVICE_PLAYBACK_PATH}" ] then DEVICE_PLAYBACK_PATH="Playback Path" fi # An explicit request wins; "auto" (or nothing) follows the jack state # detected above, so a reboot with headphones plugged starts on HP. case "${2:-${JACK_STATE}}" in "headphone") amixer -c 0 -M cset "name=${DEVICE_PLAYBACK_PATH}" "${DEVICE_PLAYBACK_PATH_HP}" ;; "auto"|"speakers"|*) amixer -c 0 -M cset "name=${DEVICE_PLAYBACK_PATH}" "${DEVICE_PLAYBACK_PATH_SPK}" ;; esac /usr/bin/volume $(get_setting "audio.volume") # Headphone sensing DEVICE="${DEVICE_HEADPHONE_DEV}" # No jack input device on this board: nothing to watch, the initial # path set above is all there is (evtest on an empty path would just # die and burn a service restart cycle). if [ -z "${DEVICE}" ] || [ ! -e "${DEVICE}" ]; then exit 0 fi HP_ON='*(SW_HEADPHONE_INSERT), value 0*' HP_OFF='*(SW_HEADPHONE_INSERT), value 1*' evtest "${DEVICE}" | while read line; do case $line in (${HP_ON}) amixer -c 0 cset "name=${DEVICE_PLAYBACK_PATH}" "${DEVICE_PLAYBACK_PATH_HP}" set_setting "audio.device" "headphone" ;; (${HP_OFF}) amixer -c 0 cset "name=${DEVICE_PLAYBACK_PATH}" "${DEVICE_PLAYBACK_PATH_SPK}" set_setting "audio.device" "speakers" ;; esac done