Add auto headphone sink switcher

master
Kuba Orlik 3 days ago
parent 535e8c5a72
commit ad6a0b4cd2

@ -3,7 +3,7 @@
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
echo $SCRIPT_DIR
cat | sudo tee /etc/systemd/system/cec-remote.service <<EOF
sudo tee /etc/systemd/system/cec-remote.service <<EOF
[Unit]
Description=CEC Receiver Service
After=network.target
@ -19,3 +19,5 @@ Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF
echo "done"

@ -0,0 +1,82 @@
#!/bin/bash
# =================================================================
# CONFIGURATION EDIT THESE TWO LINES
# =================================================================
SINK_G435="alsa_output.usb-Logitech_G_series_G435_Wireless_Gaming_Headset_202105190004-00.analog-stereo"
SINK_FALLBACK="alsa_output.pci-0000_03_00.1.hdmi-surround-extra3"
# Rapidvolumeup detection
RAPID_THRESHOLD=10 # number of VOLUMEUP events in the window
RAPID_WINDOW=3 # seconds
# =================================================================
# Find the G435 Consumer Control input device automatically
# =================================================================
find_g435_event() {
for link in /dev/input/by-id/*G435*event*; do
if [ -e "$link" ]; then
readlink -f "$link"
return 0
fi
done
return 1
}
DEVICE=""
while [ -z "$DEVICE" ]; do
DEVICE=$(find_g435_event)
if [ -z "$DEVICE" ]; then
echo "G435 input device not found retrying in 2s..." >&2
sleep 2
fi
done
echo "Found G435 device: $DEVICE"
# =================================================================
# Switch function
# =================================================================
switch_to_sink() {
local target="$1"
pactl set-default-sink "$target"
for id in $(pactl list short sink-inputs | awk '{print $1}'); do
pactl move-sink-input "$id" "$target" 2>/dev/null
done
echo "Switched to: $target"
}
# =================================================================
# Event loop grab the device exclusively
# =================================================================
up_timestamps=()
exec 3< <(stdbuf -oL evtest "$DEVICE")
while read -u 3 line; do
# Volume UP press (115)
if [[ "$line" == *"code 115 (KEY_VOLUMEUP)"*"value 1"* ]]; then
now=$(date +%s)
up_timestamps+=("$now")
# prune timestamps older than window
cutoff=$((now - RAPID_WINDOW))
new_ts=()
for ts in "${up_timestamps[@]}"; do
[ "$ts" -ge "$cutoff" ] && new_ts+=("$ts")
done
up_timestamps=("${new_ts[@]}")
count=${#up_timestamps[@]}
if [ "$count" -gt "$RAPID_THRESHOLD" ]; then
echo "Rapid VOLUMEUP ($count presses) switching to FALLBACK"
switch_to_sink "$SINK_FALLBACK"
else
echo "Normal VOLUMEUP switching to G435"
switch_to_sink "$SINK_G435"
fi
# Volume DOWN press (114) always switch to G435
elif [[ "$line" == *"code 114 (KEY_VOLUMEDOWN)"*"value 1"* ]]; then
echo "VOLUMEDOWN switching to G435"
switch_to_sink "$SINK_G435"
fi
done

@ -0,0 +1,21 @@
#!/bin/bash
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
echo $SCRIPT_DIR
sudo tee /home/kuba/.config/systemd/user/g435-sink-switch.service <<EOF
[Unit]
Description=Switch audio sink to G435 when its volume rocker is pressed
After=pipewire-pulse.service
Wants=pipewire-pulse.service
[Service]
Type=simple
ExecStart=$SCRIPT_DIR/auto-switch-to-headphones.sh
Restart=always
RestartSec=5
Environment=XDG_RUNTIME_DIR=%t
[Install]
WantedBy=default.target
EOF

@ -0,0 +1,21 @@
#!/bin/bash
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
echo $SCRIPT_DIR
cat | sudo tee /etc/systemd/system/cec-remote.service <<EOF
[Unit]
Description=CEC Receiver Service
After=network.target
[Service]
ExecStart=/usr/bin/python3 $SCRIPT_DIR/cec_receiver.py
WorkingDirectory=/home/kuba/cec-remote
Restart=on-failure
RestartSec=5
User=kuba
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF

@ -1,5 +1,7 @@
#!/bin/bash
set -e
set -o xtrace
./cec-remote/setup.sh
./g534-sink-switcher/setup.sh

Loading…
Cancel
Save