aboutsummaryrefslogtreecommitdiff
path: root/dmenu-bluetooth
blob: c1a2f2c1cca5b973a51331dd370ecc632ca1f78f (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env bash
#      _                                  _     _            _              _    _
#   __| |_ __ ___   ___ _ __  _   _      | |__ | |_   _  ___| |_ ___   ___ | |_ | |__
#  / _` | '_ ` _ \ / _ \ '_ \| | | |_____| '_ \| | | | |/ _ \ __/ _ \ / _ \| __|| '_ \
# | (_| | | | | | |  __/ | | | |_| |_____| |_) | | |_| |  __/ || (_) | (_) | |_ | | | |
#  \__,_|_| |_| |_|\___|_| |_|\__,_|     |_.__/|_|\__,_|\___|\__\___/ \___/ \__||_| |_|
#
# Author: Nick Clyde (clydedroid)
# dmenu support by: Layerex
# 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)

# Constants
divider="---------"
goback="Back"
exit="Exit"
connected_icon=""

# Checks if bluetooth controller is powered on
power_on() {
    if bluetoothctl show | grep -F -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 -F -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 -F -q "Discovering: yes"; then
        echo "Scan: on"
        return 0
    else
        echo "Scan: off"
        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 &
        echo "Scanning..."
        sleep 5
        show_menu
    fi
}

# Checks if controller is able to pair to devices
pairable_on() {
    if bluetoothctl show | grep -F -q "Pairable: yes"; then
        echo "Pairable: on"
        return 0
    else
        echo "Pairable: off"
        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 -F -q "Discoverable: yes"; then
        echo "Discoverable: on"
        return 0
    else
        echo "Discoverable: off"
        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() {
    if bluetoothctl info "$1" | grep -F -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() {
    if bluetoothctl info "$1" | grep -F -q "Paired: yes"; then
        echo "Paired: yes"
        return 0
    else
        echo "Paired: no"
        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() {
    if bluetoothctl info "$1" | grep -F -q "Trusted: yes"; then
        echo "Trusted: yes"
        return 0
    else
        echo "Trusted: no"
        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 ''

        mapfile -t paired_devices < <(bluetoothctl paired-devices | grep -F Device | cut -d ' ' -f 2)
        counter=0

        for device in "${paired_devices[@]}"; do
            if device_connected "$device"; then
                device_alias="$(bluetoothctl info "$device" | grep -F "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
        connected="Connected: yes"
    else
        connected="Connected: no"
    fi
    paired="$(device_paired "$mac")"
    trusted="$(device_trusted "$mac")"
    options="$connected\n$paired\n$trusted\n$divider\n$goback\n$exit"

    # Open dmenu menu, read chosen option
    chosen="$(echo -e "$options" | run_dmenu "$device_name")"

    # Match chosen option to command
    case $chosen in
        "" | "$divider")
            echo "No option chosen."
            ;;
        "$connected")
            toggle_connection "$mac"
            ;;
        "$paired")
            toggle_paired "$mac"
            ;;
        "$trusted")
            toggle_trust "$mac"
            ;;
        "$goback")
            show_menu
            ;;
    esac
}

# Opens a dmenu menu with current bluetooth status and options to connect
show_menu() {
    # Get menu options
    if power_on; then
        power="Power: on"

        # Human-readable names of devices, one per line
        # If scan is off, will only list paired devices
        if [[ -n "$connected_icon" ]]; then
            devices="$(bluetoothctl devices | grep -F Device | while read -r device; do
                device_name="$(echo "$device" | cut -d ' ' -f 3-)"
                mac="$(echo "$device" | cut -d ' ' -f 2)"
                icon=""

                if device_connected "$mac" && [[ -n $connected_icon ]]; then
                    icon=" $connected_icon"
                fi

                echo "$device_name${icon}"
            done)"
        else
            devices="$(bluetoothctl devices | grep -F Device | cut -d ' ' -f 3-)"
        fi

        # Get controller flags
        scan="$(scan_on)"
        pairable="$(pairable_on)"
        discoverable="$(discoverable_on)"

        # Options passed to dmenu
        [[ -n $devices ]] && devices_part="$devices\n$divider\n"
        options="$devices_part$power\n$scan\n$pairable\n$discoverable\n$exit"
    else
        power="Power: off"
        options="$power\n$exit"
    fi

    # Open dmenu menu, read chosen option
    chosen="$(echo -e "$options" | run_dmenu "Bluetooth")"

    # Match chosen option to command
    case $chosen in
        "" | "$divider")
            echo "No option chosen."
            ;;
        "$power")
            toggle_power
            ;;
        "$scan")
            toggle_scan
            ;;
        "$discoverable")
            toggle_discoverable
            ;;
        "$pairable")
            toggle_pairable
            ;;
        *)
            if [[ -n "$connected_icon" ]]; then
                chosen="${chosen%% ${connected_icon}}"
            fi
            device="$(bluetoothctl devices | grep -F "$chosen")"
            # Open a submenu if a device is selected
            if [[ -n "$device" ]]; then device_menu "$device"; fi
            ;;
    esac
}

# dmenu command to pipe into. Extra arguments to dmenu-bluetooth are passed through to dmenu. This
# allows the user to set fonts, sizes, colours, etc.
DMENU_BLUETOOTH_LAUNCHER="${DMENU_BLUETOOTH_LAUNCHER:-dmenu}"
run_dmenu() {
    case "$DMENU_BLUETOOTH_LAUNCHER" in
        rofi)
            DMENU_BLUETOOTH_LAUNCHER="rofi -dmenu"
            ;;
        fuzzel)
            DMENU_BLUETOOTH_LAUNCHER="fuzzel --dmenu"
            ;;
    esac
    $DMENU_BLUETOOTH_LAUNCHER -i -c -bw 4 -z 500 -l 10 -p "$DMENU_BLUETOOTH_PROMPT" "${dmenu_args[@]}"
}

print_help() {
    echo "usage: $0 [--help] [--status] [--connected-icon [ICON]] [PROMPT] DMENU_ARGS..."
    echo ""
    echo "A script that generates a dmenu menu that uses bluetoothctl to connect to bluetooth devices and display status info."
    echo ""
    echo "positional arguments:"
    echo "  PROMPT                    dmenu prompt"
    echo "  DMENU_ARGS...             arguments passed to dmenu"
    echo ""
    echo "options:"
    echo "--help                      show this help message and exit"
    echo "--status                    print a short string about current bluetooth status and exit"
    echo "--connected-icon [ICON]     add icon on device list next to connected devices"
    echo ""
    echo "environment variables:"
    echo "  DMENU_BLUETOOTH_PROMPT    dmenu prompt"
    echo "  DMENU_BLUETOOTH_LAUNCHER  command to use instead of 'dmenu'"
    echo ""
    echo "Positional arguments have to be placed after all other arguments."
    echo "A PROMPT positional argument will be interpreted as part of DMENU_ARGS if it starts with '-'. It won't be parsed if the DMENU_BLUETOOTH_PROMPT environment variable is set."
    echo "Use the DMENU_BLUETOOTH_LAUNCHER environment variable to use launchers other than dmenu. Rofi, fuzzel, and any dmenu-compatible launchers are supported."
}

command_present() {
    command -v "$1" >/dev/null 2>&1
}

error() {
    echo "$1. $2." >&2
    command_present notify-send && notify-send "$1" "$2."
}

# Check if bluetooth daemon is running. Start it if possible.
if command_present systemctl; then
    systemctl is-active --quiet bluetooth
    case $? in
        3)
            error "Bluetooth daemon is not running" "Start it to use this script"
            systemctl start bluetooth || exit 3
            ;;
        4)
            error "Bluetooth daemon is not present" "On Arch Linux install bluez and bluez-utils packages"
            exit 4
            ;;
    esac
fi

dmenu_args=("$@")
case "$1" in
    --help)
        print_help
        exit
        ;;
    --status)
        print_status
        exit
        ;;
    --connected-icon)
        if [[ "$2" == "--" ]]; then
            connected_icon=""
        else
            connected_icon="$2"
        fi
        dmenu_args=("${dmenu_args[@]:2}")
        ;;
esac
case "${dmenu_args[0]}" in
    -*)
        ;;
    *)
        if [[ -z "$DMENU_BLUETOOTH_PROMPT" ]]; then
            DMENU_BLUETOOTH_PROMPT="${dmenu_args[0]}"
            dmenu_args=("${dmenu_args[@]:1}")
        fi
        ;;
esac

show_menu