~mil/sxmo-utils

sxmo-utils/scripts/core/sxmo_statusbar.sh -rwxr-xr-x 4.3 KiB
d8b64fd6Nathaniel Barragan Fix looping behavior 2 days ago
                                                                                
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
#!/usr/bin/env sh

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

percenticon() {
	if [ "$1" -lt 20 ]; then
		printf ""
	elif [ "$1" -lt 40 ]; then
		printf ""
	elif [ "$1" -lt 60 ]; then
		printf ""
	elif [ "$1" -lt 80 ]; then
		printf ""
	else
		printf ""
	fi
}

bar() {
	MMCLI="$(mmcli -m any -J)"

	# In-call.. show length of call
	if pgrep sxmo_modemcall.sh > /dev/null; then
		NOWS="$(date +"%s")"
		CALLSTARTS="$(date +"%s" -d "$(
			grep -aE 'call_start|call_pickup' "$XDG_DATA_HOME"/sxmo/modem/modemlog.tsv |
			tail -n1 |
			cut -f1
		)")"
		CALLSECONDS="$(printf "%s - %s" "$NOWS" "$CALLSTARTS" | bc)"
		printf "%ss " "$CALLSECONDS"
	fi

	MODEMSTATUS=""
	if [ -z "$MMCLI" ]; then
		printf ""
	else
		MODEMSTATUS="$(printf %s "$MMCLI" | jq -r .modem.generic.state)"
		case "$MODEMSTATUS" in
			locked)
				printf ""
				;;
			registered|connected)
				MODEMSIGNAL="$(printf %s "$MMCLI" | jq -r '.modem.generic."signal-quality".value')"
				percenticon "$MODEMSIGNAL"
				;;
			disconnected)
				printf "ﲁ"
				;;
		esac
	fi

	if [ "$MODEMSTATUS" = "connected" ]; then
		printf " "
		USEDTECHS="$(printf %s "$MMCLI" | jq -r '.modem.generic."access-technologies"[]')"
		case "$USEDTECHS" in
			*5gnr*)
				printf 5g # no icon yet
				;;
			*lte*)
				printf;;
			*umts*|*hsdpa*|*hsupa*|*hspa*|*1xrtt*|*evdo0*|*evdoa*|*evdob*)
				printf;;
			*edge*)
				printf E
				;;
			*pots*|*gsm*|*gprs*)
				printf;;
		esac
	fi

	if pgrep -f sxmo_modemmonitor.sh > /dev/null; then
		printf " "
	fi

	WLANSTATE="$(tr -d "\n" < /sys/class/net/wlan0/operstate)"
	if [ "$WLANSTATE" = "up" ]; then
		printf " "
	fi

	# symbol if wireguard/vpn is connected
	VPNDEVICE="$(nmcli con show | grep vpn | awk '{ print $4 }')"
	WGDEVICE="$(nmcli con show | grep wireguard | awk '{ print $4 }')"
	if [ -n "$VPNDEVICE" ] && [ "$VPNDEVICE" != "--" ]; then
		printf " "
	elif [ -n "$WGDEVICE" ] && [ "$WGDEVICE" != "--" ]; then
		printf " "
	fi

	# Find battery and get percentage + status
	for power_supply in /sys/class/power_supply/*; do
		if [ "$(cat "$power_supply"/type)" = "Battery" ]; then
			PCT="$(cat "$power_supply"/capacity)"
			BATSTATUS="$(cut -c1 "$power_supply"/status)"
		fi
	done

	printf " "
	if [ "$BATSTATUS" = "C" ]; then
		if [ "$PCT" -lt 20 ]; then
			printf ""
		elif [ "$PCT" -lt 30 ]; then
			printf ""
		elif [ "$PCT" -lt 40 ]; then
			printf ""
		elif [ "$PCT" -lt 60 ]; then
			printf ""
		elif [ "$PCT" -lt 80 ]; then
			printf ""
		elif [ "$PCT" -lt 90 ]; then
			printf ""
		else
			printf ""
		fi
	else
		if [ "$PCT" -lt 10 ]; then
			printf ""
		elif [ "$PCT" -lt 20 ]; then
			printf ""
		elif [ "$PCT" -lt 30 ]; then
			printf ""
		elif [ "$PCT" -lt 40 ]; then
			printf ""
		elif [ "$PCT" -lt 50 ]; then
			printf ""
		elif [ "$PCT" -lt 60 ]; then
			printf ""
		elif [ "$PCT" -lt 70 ]; then
			printf ""
		elif [ "$PCT" -lt 80 ]; then
			printf ""
		elif [ "$PCT" -lt 90 ]; then
			printf ""
		else
			printf ""
		fi
	fi

	[ -z "$SXMO_BAR_HIDE_BAT_PER" ] && printf " %s%%" "$PCT"

	printf " "

	# Volume
	AUDIODEV="$(sxmo_audiocurrentdevice.sh)"
	AUDIOSYMBOL="$(printf %s "$AUDIODEV" | cut -c1)"
	if [ "$AUDIOSYMBOL" = "L" ] || [ "$AUDIOSYMBOL" = "N" ]; then
		printf "" #speakers or none, use no special symbol
	elif [ "$AUDIOSYMBOL" = "H" ]; then
		printf " "
	elif [ "$AUDIOSYMBOL" = "E" ]; then
		printf " " #earpiece
	fi
	VOL=0
	[ "$AUDIODEV" = "None" ] || VOL="$(
		amixer sget "$AUDIODEV" |
		grep -oE '([0-9]+)%' |
		tr -d ' %' |
		awk '{ s += $1; c++ } END { print s/c }'  |
		xargs printf %.0f
	)"
	if [ "$AUDIODEV" != "None" ]; then
		if [ "$VOL" -gt 66 ]; then
			printf ""
		elif [ "$VOL" -gt 33 ]; then
			printf ""
		elif [ "$VOL" -gt 0 ]; then
			printf ""
		elif [ "$VOL" -eq 0 ]; then
			printf "ﱝ"
		fi
	fi

	printf " %s\0" "$(date +%R)"
}

WM="$(sxmo_wm.sh)"

forceupdate() {
	kill "$SLEEPID"
}
trap "forceupdate" USR1

update() {
	BAR="$(bar)"
	[ -z "$SLEEPID" ] && return # to prevent mid rendering interuption
	printf %s "$BAR" | case "$WM" in
		sway|ssh) xargs -0 printf "%s\n";;
		dwm) xargs -0 xsetroot -name;;
	esac
}

while :
do
	sleep 10 &
	SLEEPID=$!

	update &
	UPDATEID=$!

	wait "$SLEEPID"
	unset SLEEPID
	wait "$UPDATEID"
done