From 48e82a9a7437fdad28b566c850fce8c79ac1e477 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 29 Jan 2020 17:54:12 +0100 Subject: bin: Replace soundswitch_dmenu fully with soundswitch --- bin/soundswitch | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 bin/soundswitch (limited to 'bin/soundswitch') diff --git a/bin/soundswitch b/bin/soundswitch new file mode 100755 index 0000000..42b6b21 --- /dev/null +++ b/bin/soundswitch @@ -0,0 +1,88 @@ +#!/bin/sh +set -e + +DEFAULT_PROFILE="output:analog-stereo+input:analog-stereo" +HDMI_PROFILE="output:hdmi-stereo" + +global= +focused= +hdmi= +reset= +while getopts fgHRh name; do + case ${name} in + g) global=1 ;; + f) focused=1 ;; + H) hdmi=1 ;; + R) reset=1 ;; + h|?) printf "usage: %s [-gfhr]\n" "$(basename "$0")" + printf "\n" + printf " -g \t(set default sink and switch global output)\n" + printf " -f \t(switch focused container output)\n" + printf " -h \t(switch card to HDMI profile)\n" + printf " -r \t(switch card to default profile)\n" + printf "\n" + printf "Needs swq(1) to work.\n" + ;; + esac +done + +if [ "$#" -lt 1 ]; then + printf "global\nfocused\nhdmi_profile\nreset_profile\n" | menu | { + read -r line + case ${line} in + global) exec "$0" -g ;; + focused) exec "$0" -f ;; + hdmi_profile) exec "$0" -H ;; + reset_profile) exec "$0" -R ;; + esac + } +fi + +# returns the sink_id +select_sink() +{ + pactl list short sinks | awk '{print $2}' | menu +} + +if [ -n "${global}" ] +then + target_sink=$(select_sink) + notify-send -a soundswitch "soundswitch: global" "switch to ${target_sink}" + pactl set-default-sink "${target_sink}" + +elif [ -n "${focused}" ] +then + # parse pid of currently focused container + # then get the sink-inputs for it and parse the sink-input-id + focused_binary=$(swq current con | jq -r '.pid' | xargs ps -o comm=) + sink_input_id=$(printf "%s\n" "${focused_binary}" \ + | xargs pacmd list-sink-inputs \ + | tr '\n' '\r' \ + | perl -pe 's/[^\r]*index: ([0-9]+).+?application\.process\.binary = "([^\r]+)"\r.+?(?=index:|$)/\2=\1\r/g' \ + | tr '\r' '\n' \ + | grep "^{}" \ + | cut -d'=' -f2) + [ -z "${sink_input_id}" ] && exit 1 + + # select which sink to switch to + target_sink=$(select_sink) + [ -z "${sink_input_id}" ] && exit 1 + + notify-send -a soundswitch "soundswitch: ${focused_binary}" "switch to ${target_sink}" + pacmd move-sink-input "${sink_input_id}" "${target_sink}" + +elif [ -n "${hdmi}" ] +then + target_card=$(pactl list short cards | awk '{print $2}' | menu) + notify-send -a soundswitch "soundswitch: hdmi" "switch ${target_card} to HDMI profile" + pactl set-card-profile "${target_card}" ${HDMI_PROFILE} + +elif [ -n "${reset}" ] +then + target_card=$(pactl list short cards | awk '{print $2}' | menu) + notify-send -a soundswitch "soundswitch: reset" "switch ${target_card} to default profile" + pactl set-card-profile "${target_card}" ${DEFAULT_PROFILE} + +fi + +exit $? -- cgit 1.4.1