forked from Layerex/dmenu-bluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmenu-bluetooth
executable file
·217 lines (192 loc) · 4.94 KB
/
dmenu-bluetooth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env bash
# _ _ _ _ _ _
# __| |_ __ ___ ___ _ __ _ _ | |__ | |_ _ ___| |_ ___ ___ | |_ | |__
# / _` | '_ ` _ \ / _ \ '_ \| | | |_____| '_ \| | | | |/ _ \ __/ _ \ / _ \| __|| '_ \
# | (_| | | | | | | __/ | | | |_| |_____| |_) | | |_| | __/ || (_) | (_) | |_ | | | |
# \__,_|_| |_| |_|\___|_| |_|\__,_| |_.__/|_|\__,_|\___|\__\___/ \___/ \__||_| |_|
#
# Author: Nick Clyde (clydedroid)
# dmenu support by: Layerex
# status update by: DusanLesan
# Original script: https://github.com/nickclyde/rofi-bluetooth
#
# A script that generates a dmenu menu that uses bluetoothctl to
# connect to bluetooth devices and display status info.
#
# Inspired by networkmanager-dmenu (https://github.com/firecat53/networkmanager-dmenu)
# Thanks to x70b1 (https://github.com/polybar/polybar-scripts/tree/master/polybar-scripts/system-bluetooth-bluetoothctl)
#
# Depends on:
# Arch repositories: dmenu, bluez-utils (contains bluetoothctl)
divider='---------'
goback='Back'
prompt() {
if [ -z $DISPLAY ] || [ -n "$USE_TERM" ]; then
[ -z "$2" ] && additionalBind=' --bind=enter:print-query'
fzf --bind=ctrl-J:print-query --bind=right-click:abort $additionalBind --prompt "$1" <<< "$2"
else
dmenu -i -p "$1" <<< "$2"
fi
}
power_on() {
if bluetoothctl show | grep -F -q 'Powered: yes'; then
return 0
else
return 1
fi
}
toggle_power() {
if power_on; then
bluetoothctl power off
show_menu
else
if rfkill list bluetooth | grep -F -q 'blocked: yes'; then
rfkill unblock bluetooth && sleep 3
fi
bluetoothctl power on
show_menu
fi
}
scan_on() {
if bluetoothctl show | grep -F -q 'Discovering: yes'; then
echo 'Scan: on'
return 0
else
echo 'Scan: off'
return 1
fi
}
toggle_scan() {
if scan_on; then
kill "$(pgrep -f 'bluetoothctl scan on')"
bluetoothctl scan off
show_menu
else
bluetoothctl scan on &
echo 'Scanning...'
sleep 5
show_menu
fi
}
pairable_on() {
if bluetoothctl show | grep -F -q 'Pairable: yes'; then
echo 'Pairable: on'
return 0
else
echo 'Pairable: off'
return 1
fi
}
toggle_pairable() {
if pairable_on; then
bluetoothctl pairable off
show_menu
else
bluetoothctl pairable on
show_menu
fi
}
discoverable_on() {
if bluetoothctl show | grep -F -q 'Discoverable: yes'; then
echo 'Discoverable: on'
return 0
else
echo 'Discoverable: off'
return 1
fi
}
toggle_discoverable() {
if discoverable_on; then
bluetoothctl discoverable off
show_menu
else
bluetoothctl discoverable on
show_menu
fi
}
device_connected() {
bluetoothctl info "$1" | grep -F -q 'Connected: yes'
return $?
}
toggle_state() {
[[ $2 == *" yes" ]] && action="$3" || action="$4"
bluetoothctl "$action" "$1" 1>/dev/null 2>&1
sleep 1
}
device_menu() {
while IFS=':' read -r key value; do
key=${key##$'\t'}
case $key in
'Connected'|'Trusted'|'Paired') declare "$key"="$key:$value" ;;
esac
done < <(bluetoothctl info "$1")
options="$Connected\n$Paired\n$Trusted\n$divider\n$goback"
chosen="$(prompt "$2" "$(printf "%b" "$options")")"
case $chosen in
"$Connected") toggle_state "$1" "$chosen" 'disconnect' 'connect' ;;
"$Paired") toggle_state "$1" "$chosen" 'remove' 'pair' ;;
"$Trusted") toggle_state "$1" "$chosen" 'untrust' 'trust' ;;
"*") show_menu ;;
esac
}
show_menu() {
if power_on; then
power='Power: on'
declare -A devices
while read -r device; do
device_name="$(echo "$device" | cut -d ' ' -f 3-)"
mac="$(echo "$device" | cut -d ' ' -f 2)"
device_connected "$mac" && icon='' || icon='X'
devices["$device_name $icon"]="$mac"
done < <(bluetoothctl devices)
scan="$(scan_on)"
pairable="$(pairable_on)"
discoverable="$(discoverable_on)"
options="$(printf "%s\n" "${!devices[@]}")\n$divider\n$power\n$scan\n$pairable\n$discoverable"
else
power='Power: off'
options="$power"
fi
chosen="$(prompt 'Bluetooth' "$(printf "%b" "$options")")" || return 0
case $chosen in
"$power") toggle_power ;;
"$scan") toggle_scan ;;
"$discoverable") toggle_discoverable ;;
"$pairable") toggle_pairable ;;
*)
device="${devices[$chosen]}"
[[ -n "$device" ]] && device_menu "$device" "$chosen" ;;
esac
}
systemctl is-active --quiet bluetooth
case $? in
3)
notify-send 'Bluetooth daemon is not running'
systemctl start bluetooth || exit 3
;;
4)
notify-send 'Bluetooth daemon is not present'
exit 4
;;
esac
print_status() {
if power_on; then
mapfile -t connected_devices < <(bluetoothctl devices Connected | grep -F Device | cut -d ' ' -f 2)
devices=''
for device in "${connected_devices[@]}"; do
while IFS=':' read -r key value; do
key=${key##$'\t'}
case $key in
'Alias') alias=$value ;;
'Battery Percentage') [[ $value =~ \(([^\)]+)\) ]] && battery="${BASH_REMATCH[1]}" ;;
esac
done < <(bluetoothctl info "$device")
devices="${devices}$alias $battery"
done
printf '%s\n' "$devices"
else
echo ''
fi
}
[[ $BLOCK_BUTTON == 1 ]] && show_menu
print_status