From 8d942a1eea14ec899aae532a56726a6b815beb30 Mon Sep 17 00:00:00 2001 From: Haletran Date: Mon, 2 Jun 2025 13:33:05 +0200 Subject: [PATCH 1/5] feature: compatility with pipewire and bluetooth connection --- dmenu_audioswitch_prev | 44 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/dmenu_audioswitch_prev b/dmenu_audioswitch_prev index 4207cdb..3ef6a87 100644 --- a/dmenu_audioswitch_prev +++ b/dmenu_audioswitch_prev @@ -1,19 +1,55 @@ #!/usr/bin/env bash # Set audio sinks before using! # `pactl list sinks` if on pulseaudio. +# `wpctl status` if on pipewire + +set_source() { \ + local SINK_NAME=$1 + local PIPEWIRE=0 + local PULSEAUDIO=0 + + if [ "$(systemctl --user is-active pipewire)" = "active" ]; then + PIPEWIRE=1 + elif [ "$(systemctl --user is-active pulseaudio)" = "active" ]; then + PULSEAUDIO=1 + fi + if [ $PULSEAUDIO -eq 1 ]; then + pacmd set-default-sink $SINK_NAME & + elif [ $PIPEWIRE -eq 1 ]; then + wpctl set-default $SINK_NAME + fi +} + +enable_and_connect() { + local B_NAME=$1 + if [ $(bluetoothctl show | grep Powered | awk '{print $2}') == "no" ]; then + echo "Bluetooth is disabled" + echo "Enabling Bluetooth..." + bluetoothctl power on > /dev/null + fi + local DEVICE=$(bluetoothctl devices | grep -w $B_NAME | awk '{print $2}') + if [ -z "$DEVICE" ]; then + echo "Device not found: $B_NAME" + return 1 + fi + if bluetoothctl info "$DEVICE" | grep -q "Connected: no"; then + bluetoothctl connect $DEVICE > /dev/null + fi +} headphones () { \ - pacmd set-default-sink "SET SINK NAME" & + set_source "SET SINK NAME OR DEVICE ID" notify-send -h string:bgcolor:#a3be8c "Audio switched to headphones!" } speakers () { \ - pacmd set-default-sink "SET SINK NAME" & + set_source "SET SINK NAME OR DEVICE ID" notify-send -h string:bgcolor:#bf616a "Audio switched to speakers!" } bluetooth () { \ - pacmd set-default-sink "SET SINK NAME" & + enable_and_connect "SET NAME OF YOUR BLUETOOTH DEVICE" + set_source "SET SINK NAME OR DEVICE ID" notify-send -h string:bgcolor:#88c0d0 "Audio switched to bluetooth!" } @@ -26,4 +62,4 @@ choosespeakers() { \ esac } -choosespeakers +choosespeakers \ No newline at end of file From 6bfcc7df34e54540ee43ba9ab9330b5e538dd8bb Mon Sep 17 00:00:00 2001 From: Haletran Date: Mon, 2 Jun 2025 13:34:25 +0200 Subject: [PATCH 2/5] fix: simplify the enable_and_connect function --- dmenu_audioswitch_prev | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/dmenu_audioswitch_prev b/dmenu_audioswitch_prev index 3ef6a87..0458f63 100644 --- a/dmenu_audioswitch_prev +++ b/dmenu_audioswitch_prev @@ -9,14 +9,9 @@ set_source() { \ local PULSEAUDIO=0 if [ "$(systemctl --user is-active pipewire)" = "active" ]; then - PIPEWIRE=1 + wpctl set-default $SINK_NAME elif [ "$(systemctl --user is-active pulseaudio)" = "active" ]; then - PULSEAUDIO=1 - fi - if [ $PULSEAUDIO -eq 1 ]; then pacmd set-default-sink $SINK_NAME & - elif [ $PIPEWIRE -eq 1 ]; then - wpctl set-default $SINK_NAME fi } From 45b953f140d7fe551984d88aecc5ff04bfcbee16 Mon Sep 17 00:00:00 2001 From: Haletran Date: Mon, 2 Jun 2025 13:40:45 +0200 Subject: [PATCH 3/5] fix: add comment for dmenu rendering in choosespeakers function --- dmenu_audioswitch_prev | 1 + 1 file changed, 1 insertion(+) diff --git a/dmenu_audioswitch_prev b/dmenu_audioswitch_prev index 0458f63..ad69c73 100644 --- a/dmenu_audioswitch_prev +++ b/dmenu_audioswitch_prev @@ -49,6 +49,7 @@ bluetooth () { \ } choosespeakers() { \ + # If the script is not rendering in dmenu, remove the `-c` flag from dmenu commands. choice=$(printf "Headphones\\nSpeakers\\nBluetooth" | dmenu -c -l 3 -i -p "Choose output: ") case "$choice" in Headphones) headphones;; From 1ce2016b083649987ac1b3b670986ac9336ab315 Mon Sep 17 00:00:00 2001 From: Haletran Date: Wed, 4 Jun 2025 11:29:23 +0200 Subject: [PATCH 4/5] fix: mistakes in my PR regarding useless code and bash logic --- dmenu_audioswitch_prev | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/dmenu_audioswitch_prev b/dmenu_audioswitch_prev index ad69c73..2c596b0 100644 --- a/dmenu_audioswitch_prev +++ b/dmenu_audioswitch_prev @@ -5,28 +5,28 @@ set_source() { \ local SINK_NAME=$1 - local PIPEWIRE=0 - local PULSEAUDIO=0 - - if [ "$(systemctl --user is-active pipewire)" = "active" ]; then + if systemctl --user is-active pipewire; then wpctl set-default $SINK_NAME - elif [ "$(systemctl --user is-active pulseaudio)" = "active" ]; then + elif systemctl --user is-active pulseaudio; then pacmd set-default-sink $SINK_NAME & + else + notify-send "No audio system detected. Please ensure PipeWire or PulseAudio is running." + return 1 fi } enable_and_connect() { local B_NAME=$1 - if [ $(bluetoothctl show | grep Powered | awk '{print $2}') == "no" ]; then - echo "Bluetooth is disabled" - echo "Enabling Bluetooth..." + if [ "$(bluetoothctl show | awk '/Powered/ {print $2}')" == "no" ]; then + notify-send "Enabling Bluetooth..." bluetoothctl power on > /dev/null fi - local DEVICE=$(bluetoothctl devices | grep -w $B_NAME | awk '{print $2}') - if [ -z "$DEVICE" ]; then - echo "Device not found: $B_NAME" + local DEVICE_INDEX="$(bluetoothctl devices | cut -d ' ' -f3- | grep -nFx "$B_NAME" | cut -d ':' -f1)" + if [ -z "$DEVICE_INDEX" ]; then + notify-send "Device not found: $B_NAME" return 1 fi + local DEVICE="$(bluetoothctl devices | head -n"$DEVICE_INDEX" | tail -n1 | cut -d ' ' -f2)" if bluetoothctl info "$DEVICE" | grep -q "Connected: no"; then bluetoothctl connect $DEVICE > /dev/null fi @@ -38,19 +38,19 @@ headphones () { \ } speakers () { \ - set_source "SET SINK NAME OR DEVICE ID" + set_source 45 notify-send -h string:bgcolor:#bf616a "Audio switched to speakers!" } bluetooth () { \ - enable_and_connect "SET NAME OF YOUR BLUETOOTH DEVICE" - set_source "SET SINK NAME OR DEVICE ID" + enable_and_connect "WH-1000XM4" + set_source 50 # the source ID will change every time you connect a bluetooth device, so you may need to change this often notify-send -h string:bgcolor:#88c0d0 "Audio switched to bluetooth!" } choosespeakers() { \ # If the script is not rendering in dmenu, remove the `-c` flag from dmenu commands. - choice=$(printf "Headphones\\nSpeakers\\nBluetooth" | dmenu -c -l 3 -i -p "Choose output: ") + choice=$(printf "Headphones\\nSpeakers\\nBluetooth" | dmenu -l 3 -i -p "Choose output: ") case "$choice" in Headphones) headphones;; Speakers) speakers;; From a648924a2d4606de360828d6a542842bc91c1e23 Mon Sep 17 00:00:00 2001 From: Haletran Date: Wed, 4 Jun 2025 11:33:43 +0200 Subject: [PATCH 5/5] fix: removed my custom devices ID --- dmenu_audioswitch_prev | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dmenu_audioswitch_prev b/dmenu_audioswitch_prev index 2c596b0..564031e 100644 --- a/dmenu_audioswitch_prev +++ b/dmenu_audioswitch_prev @@ -38,19 +38,21 @@ headphones () { \ } speakers () { \ - set_source 45 + set_source "SET SINK NAME OR DEVICE ID" notify-send -h string:bgcolor:#bf616a "Audio switched to speakers!" } bluetooth () { \ - enable_and_connect "WH-1000XM4" - set_source 50 # the source ID will change every time you connect a bluetooth device, so you may need to change this often + enable_and_connect "DEVICE NAME" + set_source "SET SINK NAME OR DEVICE ID" + # the source ID will change every time you connect a bluetooth device, so you may need to change this often + # might need to do a function to get the source ID or name dynamically notify-send -h string:bgcolor:#88c0d0 "Audio switched to bluetooth!" } choosespeakers() { \ # If the script is not rendering in dmenu, remove the `-c` flag from dmenu commands. - choice=$(printf "Headphones\\nSpeakers\\nBluetooth" | dmenu -l 3 -i -p "Choose output: ") + choice=$(printf "Headphones\\nSpeakers\\nBluetooth" | dmenu -c -l 3 -i -p "Choose output: ") case "$choice" in Headphones) headphones;; Speakers) speakers;;