summaryrefslogtreecommitdiff
path: root/.config/wmii-hg/rbar_wifi
blob: 48ea06c8cfa48b62667618bab11742cab82f978b (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
#!/usr/bin/env bash

in_array() {
	local needle straw haystack
	needle=$1
	haystack=("${@:2}")
	for straw in "${haystack[@]}"; do
		if [[ "$needle" = "$straw" ]]; then
			return 0
		fi
	done
	return 1
}

update() {
	local id
	id=$1
	setup_trap "${id}.*"

	while true; do
		for iface in /sys/class/net/*; do
			iface=${iface##*/}
			parts=()
			case "$iface" in
				lo|docker*|veth*)
					continue
					;;
				w*)
					IFS=$'\n'
					parts+=($(iwdata "$iface" | sed -rn 's@^(ESSID|Link Quality)=@@p'))
					;;
				*)
					:
					;;
			esac
			parts+=("$(ifdata -pa "$iface")")
			IFS='|'
			printf '%s %s\n' "$iface" "${parts[*]}"
		done | (
			ifaces=()
			while read -r iface data; do
				ifaces+=("$iface")
				printf "label %s\n" "${iface}:${data}" | write "${id}.${iface}"
			done
			for file in "${XDG_RUNTIME_DIR}"/wmii*/rbar/"${id}".*; do
				    if ! in_array "${file##*/"$id".}" "${ifaces[@]}"; then
					    rm -f -- "$file"
				    fi
			done
		)
		sleep 1
	done
}

right-click() {
	local list
	mapfile -t list < <({ netctl-auto list | sort -k1.2; printf '> %s\n' 'New Network' 'Disable All' 'Enable All';} | sed 's,$,$,' | column -s '$' -t)

	local cur=''
	local any_enabled=false
	local item_enable_all=''
	local item_new_network=''
	local item
	for item in "${list[@]}"; do
		case "$item" in
		'* '*)            cur="$item";;
		'> New Network'*) item_new_network=$item;;
		'> Enable All'*)  item_enable_all=$item;;
		esac
		if [[ "$item" != '! '* ]] && [[ "$item" != '> '* ]]; then
			any_enabled=true
		fi
	done
	if [[ -z $cur ]]; then
		if $any_enabled; then
			cur=$item_new_network
		else
			cur=$item_enable_all
		fi
	fi

	IFS=$' \t\n'
	item=$(wmii9menu -i "$cur" "${list[@]}")
	case "$item" in
	'')
		# Nothing selected
		;;
	'* '*)
		# Already selected; do nothing
		;;
	'> New Network'*)
		urxvt +sb -title wifi-menu -e sudo wifi-menu
		;;
	'> Disable All'*)
		sudo netctl-auto disable-all
		;;
	'> Enable All'*)
		sudo netctl-auto enable-all
		;;
	*)
		# NB: do NOT quote $item; we need to strip whitespace
		sudo netctl-auto switch-to $item
		;;
	esac
}

. "$(dirname "$0")/rbar.sh"