diff --git a/dmenu_audioswitch_prev b/dmenu_audioswitch_prev index 4207cdb..564031e 100644 --- a/dmenu_audioswitch_prev +++ b/dmenu_audioswitch_prev @@ -1,23 +1,57 @@ #!/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 + if systemctl --user is-active pipewire; then + wpctl set-default $SINK_NAME + 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 | awk '/Powered/ {print $2}')" == "no" ]; then + notify-send "Enabling Bluetooth..." + bluetoothctl power on > /dev/null + fi + 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 +} 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 "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 -c -l 3 -i -p "Choose output: ") case "$choice" in Headphones) headphones;; @@ -26,4 +60,4 @@ choosespeakers() { \ esac } -choosespeakers +choosespeakers \ No newline at end of file