dotfiles/_gui/_scripts/selector-bluetooth

300 lines
6.7 KiB
Plaintext
Raw Normal View History

2021-07-07 14:37:03 +02:00
#!/usr/bin/env bash
# __ _ _ _ _ _ _
# _ __ ___ / _(_) | |__ | |_ _ ___| |_ ___ ___ | |_| |__
# | '__/ _ \| |_| |_____| '_ \| | | | |/ _ \ __/ _ \ / _ \| __| '_ \
# | | | (_) | _| |_____| |_) | | |_| | __/ || (_) | (_) | |_| | | |
# |_| \___/|_| |_| |_.__/|_|\__,_|\___|\__\___/ \___/ \__|_| |_|
#
# Author: Nick Clyde (clydedroid)
#
# Constants
2023-02-22 16:19:33 +01:00
div="---"
2021-08-24 23:35:49 +02:00
goback="back"
2021-07-07 14:37:03 +02:00
# Checks if bluetooth controller is powered on
power_on() {
if bluetoothctl show | grep -q "Powered: yes"; then
return 0
else
return 1
fi
}
# Toggles power state
toggle_power() {
if power_on; then
bluetoothctl power off
show_menu
else
if rfkill list bluetooth | grep -q 'blocked: yes'; then
rfkill unblock bluetooth && sleep 3
fi
bluetoothctl power on
show_menu
fi
}
# Checks if controller is scanning for new devices
scan_on() {
if bluetoothctl show | grep -q "Discovering: yes"; then
2021-08-24 23:35:49 +02:00
echo "scan: on"
2021-07-07 14:37:03 +02:00
return 0
else
2021-08-24 23:35:49 +02:00
echo "scan: off"
2021-07-07 14:37:03 +02:00
return 1
fi
}
# Toggles scanning state
toggle_scan() {
if scan_on; then
kill $(pgrep -f "bluetoothctl scan on")
bluetoothctl scan off
show_menu
else
bluetoothctl scan on &
2021-08-24 23:35:49 +02:00
echo "scanning..."
2021-07-07 14:37:03 +02:00
sleep 5
show_menu
fi
}
# Checks if controller is able to pair to devices
pairable_on() {
if bluetoothctl show | grep -q "Pairable: yes"; then
2021-08-24 23:35:49 +02:00
echo "pairable: on"
2021-07-07 14:37:03 +02:00
return 0
else
2021-08-24 23:35:49 +02:00
echo "pairable: off"
2021-07-07 14:37:03 +02:00
return 1
fi
}
# Toggles pairable state
toggle_pairable() {
if pairable_on; then
bluetoothctl pairable off
show_menu
else
bluetoothctl pairable on
show_menu
fi
}
# Checks if controller is discoverable by other devices
discoverable_on() {
if bluetoothctl show | grep -q "Discoverable: yes"; then
2021-08-24 23:35:49 +02:00
echo "discoverable: on"
2021-07-07 14:37:03 +02:00
return 0
else
2021-08-24 23:35:49 +02:00
echo "discoverable: off"
2021-07-07 14:37:03 +02:00
return 1
fi
}
# Toggles discoverable state
toggle_discoverable() {
if discoverable_on; then
bluetoothctl discoverable off
show_menu
else
bluetoothctl discoverable on
show_menu
fi
}
# Checks if a device is connected
device_connected() {
device_info=$(bluetoothctl info "$1")
if echo "$device_info" | grep -q "Connected: yes"; then
return 0
else
return 1
fi
}
# Toggles device connection
toggle_connection() {
if device_connected $1; then
bluetoothctl disconnect $1
device_menu "$device"
else
bluetoothctl connect $1
device_menu "$device"
fi
}
# Checks if a device is paired
device_paired() {
device_info=$(bluetoothctl info "$1")
if echo "$device_info" | grep -q "Paired: yes"; then
2021-08-24 23:35:49 +02:00
echo "paired: yes"
2021-07-07 14:37:03 +02:00
return 0
else
2021-08-24 23:35:49 +02:00
echo "paired: no"
2021-07-07 14:37:03 +02:00
return 1
fi
}
# Toggles device paired state
toggle_paired() {
if device_paired $1; then
bluetoothctl remove $1
device_menu "$device"
else
bluetoothctl pair $1
device_menu "$device"
fi
}
# Checks if a device is trusted
device_trusted() {
device_info=$(bluetoothctl info "$1")
if echo "$device_info" | grep -q "Trusted: yes"; then
2021-08-24 23:35:49 +02:00
echo "trusted: yes"
2021-07-07 14:37:03 +02:00
return 0
else
2021-08-24 23:35:49 +02:00
echo "trusted: no"
2021-07-07 14:37:03 +02:00
return 1
fi
}
# Toggles device connection
toggle_trust() {
if device_trusted $1; then
bluetoothctl untrust $1
device_menu "$device"
else
bluetoothctl trust $1
device_menu "$device"
fi
}
# Prints a short string with the current bluetooth status
# Useful for status bars like polybar, etc.
print_status() {
if power_on; then
printf ''
2022-10-22 01:03:35 +02:00
mapfile -t paired_devices < <(bluetoothctl devices Paired | grep Device | cut -d ' ' -f 2)
2021-07-07 14:37:03 +02:00
counter=0
for device in "${paired_devices[@]}"; 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++))
fi
done
printf "\n"
else
echo ""
fi
}
# A submenu for a specific device that allows connecting, pairing, and trusting
device_menu() {
device=$1
# Get device name and mac address
device_name=$(echo $device | cut -d ' ' -f 3-)
mac=$(echo $device | cut -d ' ' -f 2)
# Build options
if device_connected $mac; then
2021-08-24 23:35:49 +02:00
connected="connected: yes"
2021-07-07 14:37:03 +02:00
else
2021-08-24 23:35:49 +02:00
connected="connected: no"
2021-07-07 14:37:03 +02:00
fi
paired=$(device_paired $mac)
trusted=$(device_trusted $mac)
2023-02-22 16:19:33 +01:00
options="$connected\n$paired\n$trusted\n$div\n$goback\n$div\nexit"
2021-07-07 14:37:03 +02:00
chosen="$(echo -e "$options" | $rofi_command "$device_name")"
# Match chosen option to command
case $chosen in
2023-02-22 16:19:33 +01:00
"" | $div)
2021-08-24 23:35:49 +02:00
echo "no option chosen."
2021-07-07 14:37:03 +02:00
;;
$connected)
toggle_connection $mac
;;
$paired)
toggle_paired $mac
;;
$trusted)
toggle_trust $mac
;;
$goback)
show_menu
;;
esac
}
show_menu() {
# Get menu options
if power_on; then
2021-08-24 23:35:49 +02:00
power="power: on"
2021-07-07 14:37:03 +02:00
# Human-readable names of devices, one per line
# If scan is off, will only list paired devices
devices=$(bluetoothctl devices | grep Device | cut -d ' ' -f 3-)
# Get controller flags
scan=$(scan_on)
pairable=$(pairable_on)
discoverable=$(discoverable_on)
2023-02-22 16:19:33 +01:00
options="$devices\n$div\n$power\n$scan\n$pairable\n$discoverable\nexit"
2021-07-07 14:37:03 +02:00
else
2021-08-24 23:35:49 +02:00
power="power: off"
options="$power\nexit"
2021-07-07 14:37:03 +02:00
fi
2021-08-24 23:35:49 +02:00
chosen="$(echo -e "$options" | $rofi_command "bluetooth")"
2021-07-07 14:37:03 +02:00
# Match chosen option to command
case $chosen in
2023-02-22 16:19:33 +01:00
"" | $div)
2021-07-07 14:37:03 +02:00
echo "No option chosen."
;;
$power)
toggle_power
;;
$scan)
toggle_scan
;;
$discoverable)
toggle_discoverable
;;
$pairable)
toggle_pairable
;;
*)
device=$(bluetoothctl devices | grep "$chosen")
# Open a submenu if a device is selected
if [[ $device ]]; then device_menu "$device"; fi
;;
esac
}
2021-10-08 00:58:07 +02:00
# command to pipe into, can add any options here
2023-04-20 01:09:43 +02:00
rofi_command="dmenu -l 10 -c -i -p"
2021-07-07 14:37:03 +02:00
case "$1" in
--status)
print_status
;;
*)
show_menu
;;
esac