summary refs log tree commit diff
path: root/bin/blum
blob: 19c2aef0965db0041e3f46106fb74b0bf4c876b2 (plain) (blame)
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
#!/bin/bash
set -eu

description="bluetooth manager"

export FUZL_PROMPT=blum
export FUZL_ICON=
. libfuzl.sh

main() {
	local connected

	if ! bt_enabled; then
		fuzl_action_add "enable" "󰷗" "Enable Bluetooth"
	else
		connected=$(bt_devices Connected | bt_devices_getname)
		if [ -n "$connected" ]; then
			fuzl_action_add "disconnect" "󰂱" "Connected to $connected"
		fi
		fuzl_action_add "devices" "󰾰" "Available devices"
		fuzl_action_add "scan" "󱄙" "Scan"
		fuzl_action_add "repl" "" "Run REPL"
		fuzl_action_add "disable" "󰷘" "Disable Bluetooth"
		fuzl_action_add "tether_amagiri" "󰀃" "Tether amagiri"
	fi

	fuzl_action_menu || exit 1
}

bt_enabled() {
	pgrep -cx bluetoothd >/dev/null 2>&1
}

bt_devices() {
	echo "devices" "${1:-}" | bluetoothctl |
		awk '/^Device /{match($0, /[0-9A-F:]{17}/); mac=substr($0,RSTART,RLENGTH); name=substr($0,RSTART+RLENGTH+1); print mac "\t" name}'
}

bt_devices_getmac() {
	awk -F '\t' '{print $1}'
}

bt_devices_getname() {
	awk -F '\t' '{print $2}'
}

#
# actions
#

blum_enable() {
	fuzl_try -- doas -n rc-service bluetooth start
	fuzl_notification "enabled" "-"
	main
}

blum_disable() {
	fuzl_try -- doas -n rc-service bluetooth stop
	fuzl_notification "disabled" "-"
}

blum_disconnect() {
	echo "disconnect" | fuzl_try -- bluetoothctl
	fuzl_notification "disconnected" "-"
}

blum_devices() {
	device=$(bt_devices |
		FUZL_PROMPT='devices' fuzl_menu $FUZL_MENU_FILTER)

	[ -n "$device" ] || main
	device_mac=$(printf %s "$device" | bt_devices_getmac)

	echo "connect" "$device_mac" | fuzl_try -- bluetoothctl
	fuzl_notification "connected" "$device"

}

blum_scan() {
	fuzl_loading "scanning"
	echo "scan" "on" | bluetoothctl || return
	sleep 2
	fuzl_loading_cancel # TODO: this kills everything
	main
}

blum_repl() {
	riverctl spawn 'footclient -- bluetoothctl'
}

blum_tether_amagiri() {
	local amagiri_mac='3C:38:F4:5D:3D:49'
	local amagiri_net='e4e75ff7-b907-43b7-98b7-e0bf94a56d08'
	echo "connect" "$amagiri_mac" | fuzl_try -- bluetoothctl
	fuzl_notification "connected" "amagiri"
	fuzl_try -- nmcli connection up "$amagiri_net"
	fuzl_notification "network up" "amagiri"
}

main