From f9a5cc1d653d5a38924dfbc25ad8a5679bfbf92d Mon Sep 17 00:00:00 2001 From: Wansti Date: Wed, 23 Nov 2022 05:35:54 +0000 Subject: [PATCH] Save audio settings when switching to bluetooth --- .../jelos/sources/autostart/common/005-alsa | 14 ++++---- packages/jelos/sources/scripts/set-audio | 34 ++++++++++++++++++- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/packages/jelos/sources/autostart/common/005-alsa b/packages/jelos/sources/autostart/common/005-alsa index c2ab6ffcb..08360d6f5 100755 --- a/packages/jelos/sources/autostart/common/005-alsa +++ b/packages/jelos/sources/autostart/common/005-alsa @@ -4,16 +4,14 @@ . /etc/profile -### REMINDER: This needs to be refactored or it will break audio on other devices. -# Revert to default when bluetooth was still selected at last shutdown. +# Revert to stored setting when bluetooth was still selected at last shutdown. # This is a workaround - headsets that auto-connect at boot tend to be # unreliable, so for now it's better to force a manual reconnect. -#LAST_AUDIO_DEVICE=$(set-audio get) -#if [[ "${LAST_AUDIO_DEVICE}" =~ ^Device.* ]] -#then -# set-audio set "DEFAULT (SYSTEM PROVIDED)" -# set-audio esset "DEFAULT (SYSTEM PROVIDED)" -#fi +LAST_AUDIO_DEVICE=$(set-audio get) +if [[ "${LAST_AUDIO_DEVICE}" =~ ^Device.* ]] +then + set-audio restore +fi if [ ! -e "/storage/.config/asound.conf" ] then diff --git a/packages/jelos/sources/scripts/set-audio b/packages/jelos/sources/scripts/set-audio index 310f4f78d..a5f12ac73 100755 --- a/packages/jelos/sources/scripts/set-audio +++ b/packages/jelos/sources/scripts/set-audio @@ -6,6 +6,31 @@ ES_SETTINGS="/storage/.config/emulationstation/es_settings.cfg" +function save_state() +{ + ACTIVE_DEVICE=$(get_audio_device) + ACTIVE_PATH=$(get_es_path) + echo "$ACTIVE_DEVICE" > /tmp/active_device.cfg + echo "$ACTIVE_PATH" > /tmp/active_path.cfg + cp -f /storage/.config/asound.conf /tmp + cp -f /storage/.config/asound.state /tmp + zip -j -r /storage/.cache/audio_settings.zip /tmp/active_device.cfg /tmp/active_path.cfg /tmp/asound.* +} + +function restore_state() +{ + if [ -e /storage/.cache/audio_settings.zip ] + then + unzip -o -d /tmp/ /storage/.cache/audio_settings.zip + STORED_DEVICE=$(cat /tmp/active_device.cfg) + STORED_PATH=$(cat /tmp/active_path.cfg) + mv /tmp/asound.conf /storage/.config/ + mv /tmp/asound.state /storage/.config/ + set_audio_device "${STORED_DEVICE}" + set_es_path "${STORED_PATH}" + fi +} + function list_audio_controls() { IFS="" ACTIVE_DEVICE=$(get_audio_device) @@ -89,7 +114,8 @@ function set_audio_device() { # This doesn't seem necessary anymore, re-activate in case of issues. # bluetoothctl disconnect ${MAC} if bluetoothctl connect ${MAC} - then + then + save_state cp /usr/config/asound.conf.bluealsa /storage/.config/asound.conf set_es_path "DEFAULT (SYSTEM PROVIDED)" fi @@ -191,4 +217,10 @@ case $1 in esget) get_es_path ;; + save) + save_state + ;; + restore) + restore_state + ;; esac