Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions rofi-bluetooth
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh
# __ _ _ _ _ _ _
# _ __ ___ / _(_) | |__ | |_ _ ___| |_ ___ ___ | |_| |__
# | '__/ _ \| |_| |_____| '_ \| | | | |/ _ \ __/ _ \ / _ \| __| '_ \
Expand Down Expand Up @@ -179,32 +179,28 @@ toggle_trust() {
fi
}

# Backwards-compatible function to print paired devices.
paired_devices() {
if [ -n "$(bluetoothctl version | cut -d ' ' -f 2 | awk -F. '($1 > 5 || $1 == 5 && $2 > 64)')" ]; then
bluetoothctl devices Paired
else
bluetoothctl paired-devices
fi
}


# Prints a short string with the current bluetooth status
# Useful for status bars like polybar, etc.
print_status() {
if power_on; then
printf ''

paired_devices_cmd="devices Paired"
# Check if an outdated version of bluetoothctl is used to preserve backwards compatibility
if (( $(echo "$(bluetoothctl version | cut -d ' ' -f 2) < 5.65" | bc -l) )); then
paired_devices_cmd="paired-devices"
fi

mapfile -t paired_devices < <(bluetoothctl $paired_devices_cmd | grep Device | cut -d ' ' -f 2)
counter=0

for device in "${paired_devices[@]}"; do
sep=""
paired_devices | grep Device | cut -d ' ' -f 2 |
while IFS= read -r device; do
if device_connected "$device"; then
device_alias=$(bluetoothctl info "$device" | grep "Alias" | cut -d ' ' -f 2-)

if [ $counter -gt 0 ]; then
printf ", %s" "$device_alias"
else
printf " %s" "$device_alias"
fi

((counter++))
printf "$sep %s" "$device_alias"
sep=","
fi
done
printf "\n"
Expand All @@ -229,10 +225,10 @@ device_menu() {
fi
paired=$(device_paired "$mac")
trusted=$(device_trusted "$mac")
options="$connected\n$paired\n$trusted\n$divider\n$goback\nExit"

# Open rofi menu, read chosen option
chosen="$(echo -e "$options" | $rofi_command "$device_name")"
chosen="$(printf "%s\n%s\n%s\n%s\n%s\nExit\n" "$connected" "$paired" "$trusted" "$divider" "$goback" \
| $rofi_command "$device_name")"

# Match chosen option to command
case "$chosen" in
Expand Down Expand Up @@ -269,15 +265,16 @@ show_menu() {
pairable=$(pairable_on)
discoverable=$(discoverable_on)

# Options passed to rofi
options="$devices\n$divider\n$power\n$scan\n$pairable\n$discoverable\nExit"
# Open rofi menu, read chosen option
chosen="$(printf "%s\n%s\n%s\n%s\n%s\n%s\nExit\n" \
"$devices" "$divider" "$power" "$scan" "$pairable" "$discoverable" \
| $rofi_command "Bluetooth")"
else
power="Power: off"
options="$power\nExit"
# Open rofi menu, read chosen option
chosen="$(printf "%s\nExit\n" "$power" | $rofi_command "Bluetooth")"
fi

# Open rofi menu, read chosen option
chosen="$(echo -e "$options" | $rofi_command "Bluetooth")"

# Match chosen option to command
case "$chosen" in
Expand All @@ -299,7 +296,7 @@ show_menu() {
*)
device=$(bluetoothctl devices | grep "$chosen")
# Open a submenu if a device is selected
if [[ $device ]]; then device_menu "$device"; fi
if [ "$device" ]; then device_menu "$device"; fi
;;
esac
}
Expand Down