speedwm-personal/scripts/speedwm-btctrl

116 lines
2.8 KiB
Bash
Executable file

#!/bin/sh
# speedwm-btctrl
# Basic dmenu/other run launcher Bluetooth manager written for speedwm.
# Licensed under GNU GPLv3.
# Check stuff
CHECK() {
case "$RUNLAUNCHER" in
"") RUNLAUNCHER=dmenu ;;
esac
BINDIR=$(cat /usr/share/speedwm-bindir) # Set binary directory to the contents of this file for NixOS support
}
CHECK
# Help
HELP() {
printf "\n1. Turn on your device\n2. Make sure the bluetooth service is running. If it is not, start it.\n3. Press the pair button on your device\n4. Select your device in the list of devices. If it does not show up, select 'Refresh'.\n5. Select 'Pair' and then optionally 'Trust' to save it in the list of devices.\n6. And finally, select 'Connect' to connect the device." | $RUNLAUNCHER -l 12 -g 1 -p 'How to use'
$0 && exit 0
}
# Enable bluetooth and scan for devices
ENABLE_BT() {
bluetoothctl power on > /dev/null && echo "Power: On"
bluetoothctl scan on & # Start scanning for devices
USEROPT_1="$(printf "$(bluetoothctl devices | cut -d\ -f3-)\n------\nRefresh\nHelp\nExit" | $RUNLAUNCHER -l 12 -g 1 -p "Select a device")"
# Check what to do
case "$USEROPT_1" in
"") $0 && exit 0 ;;
"Refresh") ENABLE_BT ;;
"Exit") exit 0 ;;
"Help") HELP && $0 && exit 0 ;;
esac
SELDEVICE_MAC="$(bluetoothctl devices | grep "$USEROPT_1$" | awk '{ print $2 }')"
echo "$SELDEVICE_MAC"
# Check if a MAC was grabbed
case "$SELDEVICE_MAC" in
"") echo "Could not get MAC" && exit 1 ;;
esac
}
ENABLE_BT
# List options for the device
LIST_OPTIONS() {
USEROPT_2="$(echo "-- Options --\nConnect\nDisconnect\n-- Toggle --\nPair\nRemove\nTrust\nUntrust\n--\nExit\nHelp" | $RUNLAUNCHER -g 1 -l 20 -p "What do you want to do with this device?" | awk '{ print $1 }')"
}
LIST_OPTIONS
# Trust device
TRUST() {
bluetoothctl trust $SELDEVICE_MAC
}
# Pair device
PAIR() {
bluetoothctl pair $SELDEVICE_MAC
}
# Remove device
REMOVE() {
bluetoothctl remove $SELDEVICE_MAC
}
UNTRUST() {
bluetoothctl untrust $SELDEVICE_MAC
}
# Connect
CONNECT() {
bluetoothctl connect $SELDEVICE_MAC
}
# Disconnect
DISCONNECT() {
bluetoothctl disconnect $SELDEVICE_MAC
}
# Notification when connecting
NOTIFY_CONNECT() {
notify-send " Connected to $USEROPT_1"
}
# Notification when disconnecting
NOTIFY_DISCONNECT() {
notify-send " Disconnected from $USEROPT_1"
}
NOTIFY_REMOVE() {
notify-send " Removed $USEROPT_1"
}
# Perform actions as per user choice
PERFORM() {
case "$USEROPT_2" in
"Trust") TRUST && LIST_OPTIONS ;;
"Pair") PAIR && LIST_OPTIONS ;;
"Connect") CONNECT && NOTIFY_CONNECT && LIST_OPTIONS ;;
"Disconnect") DISCONNECT && NOTIFY_DISCONNECT && LIST_OPTIONS ;;
"Untrust") UNTRUST && LIST_OPTIONS ;;
"Remove") REMOVE && NOTIFY_REMOVE && LIST_OPTIONS ;;
"Exit") exit 0 ;;
"Help") HELP ;;
"") $0 && exit 0 ;;
esac
}
PERFORM
$0 && exit 0