Merge pull request #2 from appeasementPolitik/patch-6

Improve dmenubluetooth and add sb-bluetooth
This commit is contained in:
M.Zeinali 2022-09-13 18:36:12 +04:30 committed by GitHub
commit fd33d944b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 8 deletions

View File

@ -29,11 +29,13 @@ power_on() {
toggle_power() { toggle_power() {
if power_on; then if power_on; then
bluetoothctl power off bluetoothctl power off
pkill -RTMIN+2 "${STATUSBAR:-dwmblocks}"
show_menu show_menu
else else
rfkill list bluetooth | grep -q 'blocked: yes' && rfkill unblock bluetooth && sleep 3 rfkill list bluetooth | grep -q 'blocked: yes' && rfkill unblock bluetooth && sleep 3
bluetoothctl power on bluetoothctl power on
pkill -RTMIN+2 "${STATUSBAR:-dwmblocks}"
show_menu show_menu
fi fi
} }
@ -48,8 +50,8 @@ scan_on() {
# Toggles scanning state # Toggles scanning state
toggle_scan() { toggle_scan() {
scan_on && scan_on &&
kill "$(pgrep -f 'bluetoothctl scan on')" && bluetoothctl scan off && show_menu || pkill -f 'bluetoothctl scan on' && bluetoothctl scan off && show_menu ||
{ bluetoothctl scan on & echo 'Scanning...' && sleep 5 && show_menu; } { bluetoothctl scan on & notify-send 'Scanning...' && sleep 5 && show_menu; }
} }
# Checks if controller is able to pair to devices # Checks if controller is able to pair to devices
@ -108,6 +110,8 @@ toggle_paired() {
device_paired "$1" && device_paired "$1" &&
bluetoothctl remove "$1" && device_menu "$device" || bluetoothctl remove "$1" && device_menu "$device" ||
{ bluetoothctl pair "$1" && device_menu "$device"; } { bluetoothctl pair "$1" && device_menu "$device"; }
pkill -RTMIN+2 "${STATUSBAR:-dwmblocks}"
} }
# Checks if a device is trusted # Checks if a device is trusted
@ -141,15 +145,13 @@ device_menu() {
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${goback}\nExit"
# Open dmenu, read chosen option # Open dmenu, read chosen option
chosen="$(printf '%b' "$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")
echo 'No option chosen.' ;;
"$connected") "$connected")
toggle_connection "$mac" ;; toggle_connection "$mac" ;;
"$paired") "$paired")
@ -158,6 +160,8 @@ device_menu() {
toggle_trust "$mac" ;; toggle_trust "$mac" ;;
"$goback") "$goback")
show_menu ;; show_menu ;;
*)
exit ;;
esac esac
} }
@ -177,7 +181,7 @@ 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:+$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"
@ -188,8 +192,8 @@ show_menu() {
# Match chosen option to command # Match chosen option to command
case $chosen in case $chosen in
"" | "$divider") "" | "$divider" | 'Exit')
echo 'No option chosen.' ;; exit ;;
"$power") "$power")
toggle_power ;; toggle_power ;;
"$scan") "$scan")

View File

@ -0,0 +1,32 @@
#!/bin/sh
#
# Shows names of paired and connected bluetooth devices if bluetooth is on
case $BLOCK_BUTTON in
1) dmenubluetooth; pkill -RTMIN+2 dwmblocks ;;
3) notify-send " Bluetooth module" "\- Shows paired devices.
- Click to configure bluetooth." ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
if bluetoothctl show | grep -q 'Powered: yes'; then
printf ''
i=0; while IFS= read -r device; do
if bluetoothctl info "$device" | grep -q 'Connected: yes'; then
device_alias=$(bluetoothctl info "$device" | grep 'Alias' | cut -d ' ' -f 2-)
[ $i = 0 ] &&
printf "$device_alias" ||
printf ', %s' "$device_alias"
i=$((i + 1))
fi
done <<-EOT
$(bluetoothctl devices Paired | cut -d ' ' -f 2)
EOT
printf "\n"
else
echo ''
fi