summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2020-01-29 17:54:12 +0100
committerRobert Günzler <r@gnzler.io>2020-01-29 17:56:55 +0100
commit48e82a9a7437fdad28b566c850fce8c79ac1e477 (patch)
treee38c5b0eed4685adab0ce9698791395e8733a6c5
parentea3a79d71a6432c58bc26738ff929d1f774aecf0 (diff)
bin: Replace soundswitch_dmenu fully with soundswitch
-rwxr-xr-xbin/hibiki/soundswitch41
-rwxr-xr-xbin/soundswitch88
-rwxr-xr-xbin/soundswitch_dmenu133
3 files changed, 88 insertions, 174 deletions
diff --git a/bin/hibiki/soundswitch b/bin/hibiki/soundswitch
deleted file mode 100755
index 9ba73c9..0000000
--- a/bin/hibiki/soundswitch
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/sh
-set -ex
-
-global=
-focused=
-profile=
-
-while getopts fgp: name; do
-	case $name in
-		g) global=1 ;;
-		f) focused=1 ;;
-		p) profile=1 ;;
-		?) printf "usage: %s [-gf]\n" "$0" >&2; exit 2 ;;
-	esac
-done
-
-if [ "$#" -lt 1 ]; then
-	printf "global\nfocused\nprofile\n" | menu | {
-		read -r line
-		case "${line}" in
-			global) exec "$0" -g ;;
-			focused) exec "$0" -f ;;
-			profile) exec "$0" -p ;;
-		esac
-	}
-fi
-
-if [ -n "${global}" ]; then
-	pactl list short sinks | awk '{print $2}' | menu | xargs pactl set-default-sink
-	exit 0
-fi
-
-if [ -n "${focused}" ]; then
-	printf "not implemented\n" >&2
-	exit 1
-fi
-
-if [ -n "${profile}" ]; then
-	printf "not implemented\n" >&2
-	exit 1
-fi
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 <i>${target_sink}</i>"
+	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 <i>${target_sink}</i>"
+        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 <i>${target_card}</i> 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 <i>${target_card}</i> to default profile"
+	pactl set-card-profile "${target_card}" ${DEFAULT_PROFILE}
+
+fi
+
+exit $?
diff --git a/bin/soundswitch_dmenu b/bin/soundswitch_dmenu
deleted file mode 100755
index f4a6338..0000000
--- a/bin/soundswitch_dmenu
+++ /dev/null
@@ -1,133 +0,0 @@
-#!/bin/sh
-# 
-# This script changes the pulseaudio sink for the currently focused application 
-#
-# If it is run with $1 = global the default sink will be switched instead
-
-set -eo pipefail
-
-rofi() {
-    # exec rofi -dmenu -p pactl -l 5 -i "$@"
-    exec wofi --dmenu -k /dev/null --prompt pactl "$@"
-}
-bail() {
-    exec echo "$@" | wofi -p pactrl-error
-    exit 1
-}
-
-list_sinks() {
-    pacmd list-sinks | tr '\n' '\r' | perl -pe 's/[^\r]*index: ([0-9]+).+?name: <([^\r]+)>.+?device.description = "([^\r]+)"\r.+?(?=index:|$)/\1: \3 \t<\2>\r/g' | tr '\r' '\n' | tail -n +2
-}
-parse_sink() {
-    perl -pe 's/.*<([^\r]+)>/\1/g'
-}
-
-# usage: move_sink_inputs_to <target-sink> <sink-input-ids>
-move_sink_inputs_to() {
-    target_sink_id="${1}"
-    shift
-    for sink_input_id in "${@}"; do
-        # notify-send "pactl-debug" "pacmd move-sink-input ${sink_input_id} ${target_sink_id}"
-        pacmd move-sink-input ${sink_input_id} ${target_sink_id}
-    done
-}
-
-list_active_sink_input_ids() {
-    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' | tail -n +2
-}
-parse_active_sink_input_id() {
-    perl -pe 's/.*:([^\r]+)/\1/g'
-}
-
-list_cards() {
-    pacmd list-cards | tr '\n' '\r' | perl -pe 's/[^\r]*index: ([0-9]+).+?device.description = \"([^\r]+)\".+?active.profile: <([^\r]+)>.+?(?=index|$)/\1: \2\tProfile: <\3>\r/g' | tr '\r' '\n' | tail -n +2
-}
-
-list_card_profiles() {
-    idx="${1}"
-    pacmd list-cards | tr '\n' '\r' | perl -pe "s/.+index: $idx\\r.+?[^\\r]*profiles:\\r(.+?)[^\\r]*active.+?$/\\1/g;" -e 's/\t\t//g;' -e 's/\([^\r]+\)\r/\r/g;' -e 's/([^\r]+):[\s]+([^\r]+)[\s]+(\r|$)/\2  <\1>\r/g;' | tr '\r' '\n' | tail -n +2
-}
-parse_profile() {
-    perl -pe 's/^.+<(.+)>$/\1/g'
-}
-
-get_focused_command() {
-    focused_pid=`swaymsg -t get_tree | jq '.. | select(.focused? == true) | .pid'`
-    ps -o comm= $focused_pid
-}
-
-# switch default global sink
-_default() {
-    target_sink=`echo "${sel}" | parse_sink`
-    pacmd set-default-sink $target_sink
-    notify-send --expire-time=2000 "New default sink" "<b>${target_sink}</b>"
-    move_sink_inputs_to $target_sink `pactl list sink-inputs short | cut -f 1 | tr '\n' ' '`
-}
-
-# switch output sink of focused application
-_focused() {
-    focused_app=`get_focused_command`
-    focused_app_input_sink_id=`list_active_sink_input_ids | grep "${focused_app}" | parse_active_sink_input_id`
-    target_sink=`list_sinks | rofi | parse_sink`
-
-    notify-send --expire-time=2000 "Switch sink" "Switch <b>${focused_app}</b> to <i>${target_sink}</i>"
-    move_sink_inputs_to "${target_sink}" "${focused_app_input_sink_id}"
-}
-
-# switch output sink of application
-_app() {
-    ret=`list_active_sink_input_ids | rofi`
-    focused_app_input_sink_id=`echo $ret | parse_active_sink_input_id`
-    focused_app=`echo $ret | perl -pe 's/^(.+):.+$/\1/g'`
-
-    target_sink=`list_sinks | rofi | parse_sink`
-
-    notify-send --expire-time=2000 "Switch sink" "Switch <b>${focused_app}</b> to <i>${target_sink}</i>"
-    move_sink_inputs_to "${target_sink}" "${focused_app_input_sink_id}"
-}
-
-_switch_profile() {
-    card=`list_cards | rofi`
-    card_id=`echo $card | perl -pe 's/([0-9]+):.+$/\1/g'`
-    card_name=`echo $card | perl -pe 's/^.+: (.+?)[\s]+Profile.+$/\1/g'`
-    new_profile=`list_card_profiles $card_id | rofi | parse_profile`
-
-    notify-send -t 2000 "Change card profile" "Switch <b>$card_name</b> to <i>$new_profile</i>"
-    # notify-send "pactl-debug" "pacmd set-card-profile $card_id $new_profile"
-    pacmd set-card-profile $card_id $new_profile
-}
-
-ACTIONS=(
-    "Switch profile"
-    "Change app output sink"
-)
-list_actions() {
-    for a in "${ACTIONS[@]}"; do echo "$a"; done
-}
-
-_global() {
-    sel=`{ list_sinks; echo ; list_actions; } | rofi`
-    if [[ -z $sel ]]; then
-        return
-    fi
-    case "${sel}" in
-        ${ACTIONS[0]}) 
-            _switch_profile 
-            ;;
-        ${ACTIONS[1]}) 
-            _app
-            ;;
-        *)
-            _default
-            ;;
-    esac
-}
-
-case "${1}" in
-    global)
-        _global
-        ;;
-    *)
-        _focused
-        ;;
-esac