mirror of
https://github.com/LukeSmithxyz/voidrice.git
synced 2026-03-20 01:37:45 +01:00
Rewrite to POSIX sh
This commit is contained in:
parent
0b503734e4
commit
89345a4a9b
@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/env bash
|
#!/bin/sh
|
||||||
#
|
#
|
||||||
# Author: Nick Clyde (clydedroid)
|
# Author: Nick Clyde (clydedroid)
|
||||||
#
|
#
|
||||||
# A script that generates a that uses bluetoothctl with dmenu to
|
# A script that generates a dmenu prompt that uses bluetoothctl to
|
||||||
# connect to bluetooth devices and display status info.
|
# connect to bluetooth devices and display status info.
|
||||||
#
|
#
|
||||||
# Inspired by networkmanager-dmenu (https://github.com/firecat53/networkmanager-dmenu)
|
# Inspired by networkmanager-dmenu (https://github.com/firecat53/networkmanager-dmenu)
|
||||||
@ -14,16 +14,15 @@
|
|||||||
# Original script: https://github.com/nickclyde/rofi-bluetooth
|
# Original script: https://github.com/nickclyde/rofi-bluetooth
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
divider="<🔗-⚒>"
|
divider='<🔗-⚒>'
|
||||||
goback="Back"
|
goback='Back'
|
||||||
|
|
||||||
|
# Dmenu command to pipe into, can add any options here
|
||||||
|
dmenu_command='dmenu -i -p'
|
||||||
|
|
||||||
# Checks if bluetooth controller is powered on
|
# Checks if bluetooth controller is powered on
|
||||||
power_on() {
|
power_on() {
|
||||||
if bluetoothctl show | grep -q "Powered: yes"; then
|
bluetoothctl show | grep -q 'Powered: yes' && return 0 || return 1
|
||||||
return 0
|
|
||||||
else
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Toggles power state
|
# Toggles power state
|
||||||
@ -32,9 +31,8 @@ toggle_power() {
|
|||||||
bluetoothctl power off
|
bluetoothctl power off
|
||||||
show_menu
|
show_menu
|
||||||
else
|
else
|
||||||
if rfkill list bluetooth | grep -q 'blocked: yes'; then
|
rfkill list bluetooth | grep -q 'blocked: yes' && rfkill unblock bluetooth && sleep 3
|
||||||
rfkill unblock bluetooth && sleep 3
|
|
||||||
fi
|
|
||||||
bluetoothctl power on
|
bluetoothctl power on
|
||||||
show_menu
|
show_menu
|
||||||
fi
|
fi
|
||||||
@ -42,166 +40,90 @@ toggle_power() {
|
|||||||
|
|
||||||
# Checks if controller is scanning for new devices
|
# Checks if controller is scanning for new devices
|
||||||
scan_on() {
|
scan_on() {
|
||||||
if bluetoothctl show | grep -q "Discovering: yes"; then
|
bluetoothctl show | grep -q 'Discovering: yes' &&
|
||||||
echo "Scan: on"
|
echo 'Scan: on' && return 0 ||
|
||||||
return 0
|
{ echo 'Scan: off' && return 1; }
|
||||||
else
|
|
||||||
echo "Scan: off"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Toggles scanning state
|
# Toggles scanning state
|
||||||
toggle_scan() {
|
toggle_scan() {
|
||||||
if scan_on; then
|
scan_on &&
|
||||||
kill $(pgrep -f "bluetoothctl scan on")
|
kill "$(pgrep -f 'bluetoothctl scan on')" && bluetoothctl scan off && show_menu ||
|
||||||
bluetoothctl scan off
|
{ bluetoothctl scan on & echo 'Scanning...' && sleep 5 && show_menu; }
|
||||||
show_menu
|
|
||||||
else
|
|
||||||
bluetoothctl scan on &
|
|
||||||
echo "Scanning..."
|
|
||||||
sleep 5
|
|
||||||
show_menu
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Checks if controller is able to pair to devices
|
# Checks if controller is able to pair to devices
|
||||||
pairable_on() {
|
pairable_on() {
|
||||||
if bluetoothctl show | grep -q "Pairable: yes"; then
|
bluetoothctl show | grep -q 'Pairable: yes' &&
|
||||||
echo "Pairable: on"
|
echo 'Pairable: on' && return 0 ||
|
||||||
return 0
|
{ echo 'Pairable: off' && return 1; }
|
||||||
else
|
|
||||||
echo "Pairable: off"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Toggles pairable state
|
# Toggles pairable state
|
||||||
toggle_pairable() {
|
toggle_pairable() {
|
||||||
if pairable_on; then
|
pairable_on &&
|
||||||
bluetoothctl pairable off
|
bluetoothctl pairable off && show_menu ||
|
||||||
show_menu
|
{ bluetoothctl pairable on && show_menu; }
|
||||||
else
|
|
||||||
bluetoothctl pairable on
|
|
||||||
show_menu
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Checks if controller is discoverable by other devices
|
# Checks if controller is discoverable by other devices
|
||||||
discoverable_on() {
|
discoverable_on() {
|
||||||
if bluetoothctl show | grep -q "Discoverable: yes"; then
|
bluetoothctl show | grep -q 'Discoverable: yes' &&
|
||||||
echo "Discoverable: on"
|
echo 'Discoverable: on' && return 0 ||
|
||||||
return 0
|
{ echo 'Discoverable: off' && return 1; }
|
||||||
else
|
|
||||||
echo "Discoverable: off"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Toggles discoverable state
|
# Toggles discoverable state
|
||||||
toggle_discoverable() {
|
toggle_discoverable() {
|
||||||
if discoverable_on; then
|
discoverable_on &&
|
||||||
bluetoothctl discoverable off
|
bluetoothctl discoverable off && show_menu ||
|
||||||
show_menu
|
{ bluetoothctl discoverable on && show_menu; }
|
||||||
else
|
|
||||||
bluetoothctl discoverable on
|
|
||||||
show_menu
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Checks if a device is connected
|
# Checks if a device is connected
|
||||||
device_connected() {
|
device_connected() {
|
||||||
device_info=$(bluetoothctl info "$1")
|
device_info=$(bluetoothctl info "$1")
|
||||||
if echo "$device_info" | grep -q "Connected: yes"; then
|
|
||||||
return 0
|
echo "$device_info" | grep -q 'Connected: yes' && return 0 || return 1
|
||||||
else
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Toggles device connection
|
# Toggles device connection
|
||||||
toggle_connection() {
|
toggle_connection() {
|
||||||
if device_connected $1; then
|
device_connected "$1" &&
|
||||||
bluetoothctl disconnect $1
|
bluetoothctl disconnect "$1" && device_menu "$device" ||
|
||||||
device_menu "$device"
|
{ bluetoothctl connect "$1" && device_menu "$device"; }
|
||||||
else
|
|
||||||
bluetoothctl connect $1
|
|
||||||
device_menu "$device"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Checks if a device is paired
|
# Checks if a device is paired
|
||||||
device_paired() {
|
device_paired() {
|
||||||
device_info=$(bluetoothctl info "$1")
|
device_info=$(bluetoothctl info "$1")
|
||||||
if echo "$device_info" | grep -q "Paired: yes"; then
|
|
||||||
echo "Paired: yes"
|
echo "$device_info" | grep -q 'Paired: yes' &&
|
||||||
return 0
|
echo 'Paired: yes' && return 0 ||
|
||||||
else
|
{ echo 'Paired: no' && return 1; }
|
||||||
echo "Paired: no"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Toggles device paired state
|
# Toggles device paired state
|
||||||
toggle_paired() {
|
toggle_paired() {
|
||||||
if device_paired $1; then
|
device_paired "$1" &&
|
||||||
bluetoothctl remove $1
|
bluetoothctl remove "$1" && device_menu "$device" ||
|
||||||
device_menu "$device"
|
{ bluetoothctl pair "$1" && device_menu "$device"; }
|
||||||
else
|
|
||||||
bluetoothctl pair $1
|
|
||||||
device_menu "$device"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Checks if a device is trusted
|
# Checks if a device is trusted
|
||||||
device_trusted() {
|
device_trusted() {
|
||||||
device_info=$(bluetoothctl info "$1")
|
device_info=$(bluetoothctl info "$1")
|
||||||
if echo "$device_info" | grep -q "Trusted: yes"; then
|
|
||||||
echo "Trusted: yes"
|
echo "$device_info" | grep -q 'Trusted: yes' &&
|
||||||
return 0
|
echo 'Trusted: yes' && return 0 ||
|
||||||
else
|
{ echo 'Trusted: no' && return 1; }
|
||||||
echo "Trusted: no"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Toggles device connection
|
# Toggles device connection
|
||||||
toggle_trust() {
|
toggle_trust() {
|
||||||
if device_trusted $1; then
|
device_trusted "$1" &&
|
||||||
bluetoothctl untrust $1
|
bluetoothctl untrust "$1" && device_menu "$device" ||
|
||||||
device_menu "$device"
|
{ bluetoothctl trust "$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 ''
|
|
||||||
|
|
||||||
mapfile -t paired_devices < <(bluetoothctl paired-devices | grep Device | cut -d ' ' -f 2)
|
|
||||||
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
|
# A submenu for a specific device that allows connecting, pairing, and trusting
|
||||||
@ -209,39 +131,33 @@ device_menu() {
|
|||||||
device=$1
|
device=$1
|
||||||
|
|
||||||
# Get device name and mac address
|
# Get device name and mac address
|
||||||
device_name=$(echo $device | cut -d ' ' -f 3-)
|
device_name=$(echo "$device" | cut -d ' ' -f 3-)
|
||||||
mac=$(echo $device | cut -d ' ' -f 2)
|
mac=$(echo "$device" | cut -d ' ' -f 2)
|
||||||
|
|
||||||
# Build options
|
# Build options
|
||||||
if device_connected $mac; then
|
device_connected "$mac" &&
|
||||||
connected="Connected: yes"
|
connected='Connected: yes' ||
|
||||||
else
|
connected='Connected: no'
|
||||||
connected="Connected: no"
|
|
||||||
fi
|
paired=$(device_paired "$mac")
|
||||||
paired=$(device_paired $mac)
|
trusted=$(device_trusted "$mac")
|
||||||
trusted=$(device_trusted $mac)
|
options="${connected}\n${paired}\n${trusted}\n${divider}\n${goback}\nExit"
|
||||||
options="$connected\n$paired\n$trusted\n$divider\n$goback\nExit"
|
|
||||||
|
|
||||||
# Open dmenu, read chosen option
|
# Open dmenu, read chosen option
|
||||||
chosen="$(echo -e "$options" | $dmenu_command "$device_name")"
|
chosen="$(printf '%b' "$options" | $dmenu_command "$device_name")"
|
||||||
|
|
||||||
# Match chosen option to command
|
# Match chosen option to command
|
||||||
case $chosen in
|
case $chosen in
|
||||||
"" | $divider)
|
"" | "$divider")
|
||||||
echo "No option chosen."
|
echo 'No option chosen.' ;;
|
||||||
;;
|
"$connected")
|
||||||
$connected)
|
toggle_connection "$mac" ;;
|
||||||
toggle_connection $mac
|
"$paired")
|
||||||
;;
|
toggle_paired "$mac" ;;
|
||||||
$paired)
|
"$trusted")
|
||||||
toggle_paired $mac
|
toggle_trust "$mac" ;;
|
||||||
;;
|
"$goback")
|
||||||
$trusted)
|
show_menu ;;
|
||||||
toggle_trust $mac
|
|
||||||
;;
|
|
||||||
$goback)
|
|
||||||
show_menu
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,11 +165,11 @@ device_menu() {
|
|||||||
show_menu() {
|
show_menu() {
|
||||||
# Get menu options
|
# Get menu options
|
||||||
if power_on; then
|
if power_on; then
|
||||||
power="Power: on"
|
power='Power: on'
|
||||||
|
|
||||||
# Human-readable names of devices, one per line
|
# Human-readable names of devices, one per line
|
||||||
# If scan is off, will only list paired devices
|
# If scan is off, will only list paired devices
|
||||||
devices=$(bluetoothctl devices | grep Device | cut -d ' ' -f 3-)
|
devices=$(bluetoothctl devices | cut -d ' ' -f 3-)
|
||||||
|
|
||||||
# Get controller flags
|
# Get controller flags
|
||||||
scan=$(scan_on)
|
scan=$(scan_on)
|
||||||
@ -261,48 +177,32 @@ show_menu() {
|
|||||||
discoverable=$(discoverable_on)
|
discoverable=$(discoverable_on)
|
||||||
|
|
||||||
# Options passed to dmenu
|
# Options passed to dmenu
|
||||||
options="$devices\n$divider\n$power\n$scan\n$pairable\n$discoverable\nExit"
|
options="${devices}\n${divider}\n${power}\n${scan}\n${pairable}\n${discoverable}\nExit"
|
||||||
else
|
else
|
||||||
power="Power: off"
|
power='Power: off'
|
||||||
options="$power\nExit"
|
options="$power\nExit"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Open dmenu, read chosen option
|
# Open dmenu, read chosen option
|
||||||
chosen="$(echo -e "$options" | $dmenu_command "Bluetooth")"
|
chosen="$(printf '%b' "$options" | $dmenu_command 'Bluetooth')"
|
||||||
|
|
||||||
# Match chosen option to command
|
# Match chosen option to command
|
||||||
case $chosen in
|
case $chosen in
|
||||||
"" | $divider)
|
"" | "$divider")
|
||||||
echo "No option chosen."
|
echo 'No option chosen.' ;;
|
||||||
;;
|
"$power")
|
||||||
$power)
|
toggle_power ;;
|
||||||
toggle_power
|
"$scan")
|
||||||
;;
|
toggle_scan ;;
|
||||||
$scan)
|
"$discoverable")
|
||||||
toggle_scan
|
toggle_discoverable ;;
|
||||||
;;
|
"$pairable")
|
||||||
$discoverable)
|
toggle_pairable ;;
|
||||||
toggle_discoverable
|
|
||||||
;;
|
|
||||||
$pairable)
|
|
||||||
toggle_pairable
|
|
||||||
;;
|
|
||||||
*)
|
*)
|
||||||
device=$(bluetoothctl devices | grep "$chosen")
|
device=$(bluetoothctl devices | grep "$chosen")
|
||||||
# Open a submenu if a device is selected
|
# Open a submenu if a device is selected
|
||||||
if [[ $device ]]; then device_menu "$device"; fi
|
[ -n "$device" ] && device_menu "$device" ;;
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
# Dmenu command to pipe into, can add any options here
|
show_menu
|
||||||
dmenu_command="dmenu -i -p"
|
|
||||||
|
|
||||||
case "$1" in
|
|
||||||
--status)
|
|
||||||
print_status
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
show_menu
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user