radio-win
This commit is contained in:
@@ -6,6 +6,7 @@ emit() {
|
||||
if bluetoothctl show 2>/dev/null | grep -q "Powered: yes"; then
|
||||
powered=true
|
||||
while IFS= read -r line; do
|
||||
local mac info
|
||||
mac=$(echo "$line" | awk '{ print $2 }')
|
||||
info=$(bluetoothctl info "$mac" 2>/dev/null)
|
||||
if echo "$info" | grep -q "Connected: yes"; then
|
||||
@@ -20,8 +21,18 @@ emit() {
|
||||
}
|
||||
|
||||
emit
|
||||
bluetoothctl monitor 2>/dev/null | while IFS= read -r line; do
|
||||
case "$line" in
|
||||
*"Powered"*|*"Connected"*|*"Device"*) emit ;;
|
||||
esac
|
||||
|
||||
tmp=$(mktemp -d)
|
||||
pipe="$tmp/bt-events"
|
||||
mkfifo "$pipe"
|
||||
trap 'rm -rf "$tmp"; kill 0 2>/dev/null' EXIT INT TERM
|
||||
|
||||
# Poll every 2s as reliable fallback for missed events
|
||||
(while true; do sleep 2; echo poll; done) > "$pipe" &
|
||||
|
||||
# bluetoothctl monitor for reactive device connect/disconnect events
|
||||
(bluetoothctl monitor 2>/dev/null | grep --line-buffered -E "Powered|Connected|Device") > "$pipe" &
|
||||
|
||||
while IFS= read -r _ < "$pipe"; do
|
||||
emit
|
||||
done
|
||||
|
||||
@@ -4,6 +4,13 @@ get_wifi_iface() {
|
||||
awk 'NR > 2 { gsub(":", "", $1); print $1; exit }' /proc/net/wireless
|
||||
}
|
||||
|
||||
get_usb_iface() {
|
||||
ip link 2>/dev/null | awk '
|
||||
/^[0-9]+: usb[0-9]/ { gsub(":", "", $2); print $2; exit }
|
||||
/^[0-9]+: enx/ { gsub(":", "", $2); print $2; exit }
|
||||
'
|
||||
}
|
||||
|
||||
signal_icon() {
|
||||
local dbm="$1"
|
||||
if [ -z "$dbm" ]; then echo ""; return; fi
|
||||
@@ -27,8 +34,18 @@ make_content() {
|
||||
[ "$eth_state" = "UP" ] && eth_connected=true
|
||||
fi
|
||||
|
||||
# USB tethering
|
||||
local usb_iface usb_connected=false
|
||||
usb_iface=$(get_usb_iface)
|
||||
if [ -n "$usb_iface" ] && ip -4 addr show "$usb_iface" 2>/dev/null | grep -q "inet "; then
|
||||
usb_connected=true
|
||||
fi
|
||||
|
||||
# WiFi — use IP presence as connection indicator (more reliable than wpa_cli)
|
||||
local wifi_connected=false wifi_icon="" wifi_ssid=""
|
||||
local wifi_connected=false wifi_enabled=false wifi_icon="" wifi_ssid=""
|
||||
if ! rfkill list wlan 2>/dev/null | grep -q "Soft blocked: yes"; then
|
||||
wifi_enabled=true
|
||||
fi
|
||||
if [ -n "$wifi_iface" ] && ip -4 addr show "$wifi_iface" 2>/dev/null | grep -q "inet "; then
|
||||
wifi_connected=true
|
||||
wifi_ssid=$(wpa_cli -g "/run/wpa_supplicant/$wifi_iface" status 2>/dev/null \
|
||||
@@ -37,8 +54,8 @@ make_content() {
|
||||
wifi_icon=$(signal_icon "$signal")
|
||||
fi
|
||||
|
||||
printf '{"wifi":{"connected":%s,"icon":"%s","ssid":"%s"},"ethernet":{"connected":%s}}\n' \
|
||||
"$wifi_connected" "$wifi_icon" "$wifi_ssid" "$eth_connected"
|
||||
printf '{"wifi":{"connected":%s,"enabled":%s,"icon":"%s","ssid":"%s"},"ethernet":{"connected":%s},"usb":{"connected":%s}}\n' \
|
||||
"$wifi_connected" "$wifi_enabled" "$wifi_icon" "$wifi_ssid" "$eth_connected" "$usb_connected"
|
||||
}
|
||||
|
||||
make_content
|
||||
|
||||
@@ -4,6 +4,13 @@ get_wifi_iface() {
|
||||
awk 'NR > 2 { gsub(":", "", $1); print $1; exit }' /proc/net/wireless
|
||||
}
|
||||
|
||||
get_usb_iface() {
|
||||
ip link 2>/dev/null | awk '
|
||||
/^[0-9]+: usb[0-9]/ { gsub(":", "", $2); print $2; exit }
|
||||
/^[0-9]+: enx/ { gsub(":", "", $2); print $2; exit }
|
||||
'
|
||||
}
|
||||
|
||||
freq_band() {
|
||||
local f="$1"
|
||||
if [ "$f" -ge 6000 ] 2>/dev/null; then echo "6 GHz"
|
||||
@@ -51,11 +58,19 @@ make_content() {
|
||||
fi
|
||||
fi
|
||||
|
||||
local usb_iface usb_ip=""
|
||||
usb_iface=$(get_usb_iface)
|
||||
if [ -n "$usb_iface" ] && ip -4 addr show "$usb_iface" 2>/dev/null | grep -q "inet "; then
|
||||
usb_ip=$(ip -4 addr show "$usb_iface" | awk '/inet / { print $2 }')
|
||||
fi
|
||||
|
||||
# shellcheck disable=SC2059
|
||||
printf '{"wifi":{"ssid":"%s","ip":"%s","freq":%s,"band":"%s","gen":"%s","signal":%s},' \
|
||||
"$wifi_ssid" "$wifi_ip" "${wifi_freq:-0}" "$wifi_band" "$wifi_gen" "${wifi_signal:-0}"
|
||||
printf '"ethernet":{"state":"%s","ip":"%s","speed":"%s","interface":"%s"}}\n' \
|
||||
printf '"ethernet":{"state":"%s","ip":"%s","speed":"%s","interface":"%s"},' \
|
||||
"$eth_state" "$eth_ip" "$eth_speed" "${eth_iface:-}"
|
||||
printf '"usb":{"interface":"%s","ip":"%s"}}\n' \
|
||||
"${usb_iface:-}" "$usb_ip"
|
||||
}
|
||||
|
||||
make_content
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
#!/usr/bin/env zsh
|
||||
Reference in New Issue
Block a user