#!/bin/bash # Default Values — overridden by wofi-network-manager.conf if present. # LOCATION: wofi window placement (0 = top-left corner, see wofi -location). LOCATION=0 QRCODE_LOCATION=$LOCATION Y_AXIS=0 X_AXIS=0 # NOTIFICATIONS_INIT: set to "on" in the conf file to enable desktop notifications. NOTIFICATIONS_INIT="off" # Directory where QR-code PNGs are cached. QRCODE_DIR="/tmp/" # WIDTH_FIX_* are added to the auto-computed em-width for the main and status wofi windows. WIDTH_FIX_MAIN=1 WIDTH_FIX_STATUS=10 # Resolve the script's own directory so sibling files (conf, css) are found regardless of cwd. DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Placeholder shown in the wofi password field; hitting Enter/Esc with this text tries a stored key. PASSWORD_ENTER="if connection is stored,hit enter/esc." # Discover all Wi-Fi and Ethernet interfaces reported by NetworkManager at startup. WIRELESS_INTERFACES=($(nmcli device | awk '$2=="wifi" {print $1}')) WIRELESS_INTERFACES_PRODUCT=() # WLAN_INT: index of the currently active Wi-Fi interface when multiple cards are present. WLAN_INT=0 WIRED_INTERFACES=($(nmcli device | awk '$2=="ethernet" {print $1}')) WIRED_INTERFACES_PRODUCT=() function initialization() { # Load config from the script's directory first; fall back to XDG config home. # The CSS theme file must exist — exits if neither location has one. source "$DIR/wofi-network-manager.conf" || source "${XDG_CONFIG_HOME:-$HOME/.config}/wofi/wofi-network-manager.conf" { [[ -s "$DIR/wofi-network-manager.css" ]] && RASI_DIR="$DIR/wofi-network-manager.css"; } || { [[ -s "${XDG_CONFIG_HOME:-$HOME/.config}/wofi/wofi-network-manager.css" ]] && RASI_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/wofi/wofi-network-manager.css"; } || exit # Populate human-readable product names (e.g. "Intel Wi-Fi 6") for each interface. for i in "${WIRELESS_INTERFACES[@]}"; do WIRELESS_INTERFACES_PRODUCT+=("$(nmcli -f general.product device show "$i" | awk '{print $2}')"); done for i in "${WIRED_INTERFACES[@]}"; do WIRED_INTERFACES_PRODUCT+=("$(nmcli -f general.product device show "$i" | awk '{print $2}')"); done # Refresh global state variables used by the menu. wireless_interface_state && ethernet_interface_state } function notification() { # Only fire when NOTIFICATIONS_INIT is "on" and notify-send is executable. # -r "5" replaces any previous notification with the same replacement-id, avoiding spam. [[ "$NOTIFICATIONS_INIT" == "on" && -x "$(command -v notify-send)" ]] && notify-send -r "5" -u "normal" $1 "$2" } function wireless_interface_state() { # No-op when no Wi-Fi cards were found at startup. [[ ${#WIRELESS_INTERFACES[@]} -eq "0" ]] || { # Read the active SSID (column 4) and connection state (column 3) for the current card. ACTIVE_SSID=$(nmcli device status | grep "^${WIRELESS_INTERFACES[WLAN_INT]}." | awk '{print $4}') WIFI_CON_STATE=$(nmcli device status | grep "^${WIRELESS_INTERFACES[WLAN_INT]}." | awk '{print $3}') # Build the OPTIONS string that will be fed into wofi. # "unavailable" means the radio is off; offer a toggle and a scan shortcut. # "connected" means we have an active link; list nearby networks plus management options. { [[ "$WIFI_CON_STATE" == "unavailable" ]] && WIFI_LIST="***Wi-Fi Disabled***" && WIFI_SWITCH="~Wi-Fi On" && OPTIONS="${WIFI_LIST}\n${WIFI_SWITCH}\n~Scan\n"; } || { [[ "$WIFI_CON_STATE" =~ "connected" ]] && { PROMPT=${WIRELESS_INTERFACES_PRODUCT[WLAN_INT]}[${WIRELESS_INTERFACES[WLAN_INT]}] # List nearby networks: -F selects fields; awk deduplicates by SSID; sed strips the # "IN-USE" header row and the "*" marker for the active network; awk drops hidden SSIDs ("--"). WIFI_LIST=$(nmcli --fields IN-USE,SSID,SECURITY,BARS device wifi list ifname "${WIRELESS_INTERFACES[WLAN_INT]}" | awk -F' +' '{ if (!seen[$2]++) print}' | sed "s/^IN-USE\s//g" | sed "/*/d" | sed "s/^ *//" | awk '$1!="--" {print}') # Offer "Disconnect" only when actually associated (ACTIVE_SSID is not "--"). [[ "$ACTIVE_SSID" == "--" ]] && WIFI_SWITCH="~Scan\n~Manual/Hidden\n~Wi-Fi Off" || WIFI_SWITCH="~Scan\n~Disconnect\n~Manual/Hidden\n~Wi-Fi Off" OPTIONS="${WIFI_LIST}\n${WIFI_SWITCH}\n" }; } } } function ethernet_interface_state() { # No-op when no wired interfaces were found. [[ ${#WIRED_INTERFACES[@]} -eq "0" ]] || { # head -1 picks the first ethernet device in case multiple are present. WIRED_CON_STATE=$(nmcli device status | grep "ethernet" | head -1 | awk '{print $3}') # Append the appropriate wired control entry to OPTIONS. { [[ "$WIRED_CON_STATE" == "disconnected" ]] && WIRED_SWITCH="~Eth On"; } || { [[ "$WIRED_CON_STATE" == "connected" ]] && WIRED_SWITCH="~Eth Off"; } || { [[ "$WIRED_CON_STATE" == "unavailable" ]] && WIRED_SWITCH="***Wired Unavailable***"; } || { [[ "$WIRED_CON_STATE" == "connecting" ]] && WIRED_SWITCH="***Wired Initializing***"; } OPTIONS="${OPTIONS}${WIRED_SWITCH}\n" } } function wofi_menu() { # Add "Change Wifi Interface" only when more than one Wi-Fi card exists. { [[ ${#WIRELESS_INTERFACES[@]} -gt "1" ]] && OPTIONS="${OPTIONS}~Change Wifi Interface\n~More Options"; } || { OPTIONS="${OPTIONS}~More Options"; } # Show the wired interface name in the prompt when Ethernet is active. { [[ "$WIRED_CON_STATE" == "connected" ]] && PROMPT="${WIRED_INTERFACES_PRODUCT}[$WIRED_INTERFACES]"; } || PROMPT="${WIRELESS_INTERFACES_PRODUCT[WLAN_INT]}[${WIRELESS_INTERFACES[WLAN_INT]}]" SELECTION=$(echo -e "$OPTIONS" | wofi_cmd "$OPTIONS" $WIDTH_FIX_MAIN "-a 0") # Collapse multi-space separators to | then extract the first field (SSID without decorators). SSID=$(echo "$SELECTION" | sed "s/\s\{2,\}/\|/g" | awk -F "|" '{print $1}') selection_action } function wofi_cmd() { # Auto-size the wofi window to fit the widest entry. # $1 = option list for width calculation, $2 = width adjustment, $3/$4 = extra flags. # WIDTH is halved because wofi uses character-width em units on a proportional font. { [[ -n "${1}" ]] && WIDTH=$(echo -e "$1" | awk '{print length}' | sort -n | tail -1) && ((WIDTH += $2)) && ((WIDTH = WIDTH / 2)); } || { ((WIDTH = $2 / 2)); } # -dmenu: read from stdin; -i: case-insensitive; --normal-window: don't use a special surface. # -theme-str injects CSS overrides: sets window width and the prompt colon label. wofi -dmenu -i --normal-window=false -location "$LOCATION" -yoffset "$Y_AXIS" -xoffset "$X_AXIS" $3 -theme "$RASI_DIR" -theme-str 'window{width: '$WIDTH'em;}textbox-prompt-colon{str:"'$PROMPT':";}'"$4"'' } function change_wireless_interface() { # If exactly two cards exist, toggle between index 0 and 1 without showing a menu. # Otherwise, present all cards in wofi and let the user pick. { [[ ${#WIRELESS_INTERFACES[@]} -eq "2" ]] && { [[ $WLAN_INT -eq "0" ]] && WLAN_INT=1 || WLAN_INT=0; }; } || { LIST_WLAN_INT="" for i in "${!WIRELESS_INTERFACES[@]}"; do LIST_WLAN_INT=("${LIST_WLAN_INT[@]}${WIRELESS_INTERFACES_PRODUCT[$i]}[${WIRELESS_INTERFACES[$i]}]\n"); done # Strip the trailing \n from the last entry to avoid a blank line in wofi. LIST_WLAN_INT[-1]=${LIST_WLAN_INT[-1]::-2} CHANGE_WLAN_INT=$(echo -e "${LIST_WLAN_INT[@]}" | wofi_cmd "${LIST_WLAN_INT[@]}" $WIDTH_FIX_STATUS) # Resolve the selected label back to its index in WIRELESS_INTERFACES. for i in "${!WIRELESS_INTERFACES[@]}"; do [[ $CHANGE_WLAN_INT == "${WIRELESS_INTERFACES_PRODUCT[$i]}[${WIRELESS_INTERFACES[$i]}]" ]] && WLAN_INT=$i && break; done } # Refresh state after the interface switch and redraw the menu. wireless_interface_state && ethernet_interface_state wofi_menu } function scan() { # If Wi-Fi is off, turn it on and wait 2 s for the radio to come up before scanning. [[ "$WIFI_CON_STATE" =~ "unavailable" ]] && change_wifi_state "Wi-Fi" "Enabling Wi-Fi connection" "on" && sleep 2 notification "-t 0 Wifi" "Please Wait Scanning" # --rescan yes forces an active scan; without it nmcli may return a stale cache. # awk deduplicates SSIDs; sed cleans up the header and the currently-connected marker. WIFI_LIST=$(nmcli --fields IN-USE,SSID,SECURITY,BARS device wifi list ifname "${WIRELESS_INTERFACES[WLAN_INT]}" --rescan yes | awk -F' +' '{ if (!seen[$2]++) print}' | sed "s/^IN-USE\s//g" | sed "/*/d" | sed "s/^ *//" | awk '$1!="--" {print}') wireless_interface_state && ethernet_interface_state notification "-t 1 Wifi" "Please Wait Scanning" wofi_menu } function change_wifi_state() { # Toggle the Wi-Fi radio via nmcli: "on" enables it, "off" disables it. notification "$1" "$2" nmcli radio wifi "$3" } function change_wired_state() { # Connect or disconnect a wired interface: nmcli device connect/disconnect . notification "$1" "$2" nmcli device "$3" "$4" } function net_restart() { # Hard-cycle the NetworkManager stack; 3 s sleep lets the kernel teardown complete. notification "$1" "$2" nmcli networking off && sleep 3 && nmcli networking on } function disconnect() { # Resolve the connection profile name for the active SSID, then bring it down. # -t (terse) + cut removes the "GENERAL.CONNECTION:" key prefix from nmcli output. ACTIVE_SSID=$(nmcli -t -f GENERAL.CONNECTION dev show "${WIRELESS_INTERFACES[WLAN_INT]}" | cut -d ':' -f2) notification "$1" "You're now disconnected from Wi-Fi network '$ACTIVE_SSID'" nmcli con down id "$ACTIVE_SSID" } function check_wifi_connected() { # Drop any existing connection before attempting a new one; avoids "already connected" errors. [[ "$(nmcli device status | grep "^${WIRELESS_INTERFACES[WLAN_INT]}." | awk '{print $3}')" == "connected" ]] && disconnect "Connection_Terminated" } function connect() { check_wifi_connected notification "-t 0 Wi-Fi" "Connecting to $1" # Try to join the network with the supplied password; report success or failure. { [[ $(nmcli dev wifi con "$1" password "$2" ifname "${WIRELESS_INTERFACES[WLAN_INT]}" | grep -c "successfully activated") -eq "1" ]] && notification "Connection_Established" "You're now connected to Wi-Fi network '$1'"; } || notification "Connection_Error" "Connection can not be established" } function enter_passwword() { # Open a password-masked wofi prompt; the placeholder text acts as a "stored key" signal. PROMPT="Enter_Password" && PASS=$(echo "$PASSWORD_ENTER" | wofi_cmd "$PASSWORD_ENTER" 4 "-password") } function enter_ssid() { # Open a plain text wofi prompt with no pre-filled options for manual SSID entry. PROMPT="Enter_SSID" && SSID=$(wofi_cmd "" 40) } function stored_connection() { # Re-activate an already-known connection profile without supplying a password. check_wifi_connected notification "-t 0 Wi-Fi" "Connecting to $1" { [[ $(nmcli dev wifi con "$1" ifname "${WIRELESS_INTERFACES[WLAN_INT]}" | grep -c "successfully activated") -eq "1" ]] && notification "Connection_Established" "You're now connected to Wi-Fi network '$1'"; } || notification "Connection_Error" "Connection can not be established" } function ssid_manual() { # Let the user type an SSID; if a password is entered use it, otherwise try stored credentials. enter_ssid [[ -n $SSID ]] && { enter_passwword { [[ -n "$PASS" ]] && [[ "$PASS" != "$PASSWORD_ENTER" ]] && connect "$SSID" "$PASS"; } || stored_connection "$SSID" } } function ssid_hidden() { # Connect to a hidden SSID by creating or reusing an NM connection profile. enter_ssid [[ -n $SSID ]] && { enter_passwword && check_wifi_connected [[ -n "$PASS" ]] && [[ "$PASS" != "$PASSWORD_ENTER" ]] && { # Create a new Wi-Fi connection profile, then configure WPA-PSK security on it. nmcli con add type wifi con-name "$SSID" ssid "$SSID" ifname "${WIRELESS_INTERFACES[WLAN_INT]}" nmcli con modify "$SSID" wifi-sec.key-mgmt wpa-psk nmcli con modify "$SSID" wifi-sec.psk "$PASS" } || [[ $(nmcli -g NAME con show | grep -c "$SSID") -eq "0" ]] && nmcli con add type wifi con-name "$SSID" ssid "$SSID" ifname "${WIRELESS_INTERFACES[WLAN_INT]}" notification "-t 0 Wifi" "Connecting to $SSID" { [[ $(nmcli con up id "$SSID" | grep -c "successfully activated") -eq "1" ]] && notification "Connection_Established" "You're now connected to Wi-Fi network '$SSID'"; } || notification "Connection_Error" "Connection can not be established" } } function interface_status() { # local -n creates a nameref — $1 and $2 are the names of the caller's arrays, # not their values; this avoids passing large arrays by copy. local -n INTERFACES=$1 && local -n INTERFACES_PRODUCT=$2 for i in "${!INTERFACES[@]}"; do CON_STATE=$(nmcli device status | grep "^${INTERFACES[$i]}." | awk '{print $3}') INT_NAME=${INTERFACES_PRODUCT[$i]}[${INTERFACES[$i]}] # When connected, show the profile name and IPv4 address; otherwise capitalise the state word. # awk -F '[:]' splits on colon to strip the "GENERAL.CONNECTION:" prefix. # awk -F '[:/]' splits on colon or slash to extract the bare IP from "IP4.ADDRESS[1]: x.x.x.x/24". [[ "$CON_STATE" == "connected" ]] && STATUS="$INT_NAME:\n\t$(nmcli -t -f GENERAL.CONNECTION dev show "${INTERFACES[$i]}" | awk -F '[:]' '{print $2}') ~ $(nmcli -t -f IP4.ADDRESS dev show "${INTERFACES[$i]}" | awk -F '[:/]' '{print $2}')" || STATUS="$INT_NAME: ${CON_STATE^}" echo -e "${STATUS}" done } function status() { # Build a combined status view of all wired and wireless interfaces plus any active VPN. OPTIONS="" [[ ${#WIRED_INTERFACES[@]} -ne "0" ]] && ETH_STATUS="$(interface_status WIRED_INTERFACES WIRED_INTERFACES_PRODUCT)" && OPTIONS="${OPTIONS}${ETH_STATUS}" [[ ${#WIRELESS_INTERFACES[@]} -ne "0" ]] && WLAN_STATUS="$(interface_status WIRELESS_INTERFACES WIRELESS_INTERFACES_PRODUCT)" && { [[ -n ${OPTIONS} ]] && OPTIONS="${OPTIONS}\n${WLAN_STATUS}" || OPTIONS="${OPTIONS}${WLAN_STATUS}"; } # -g NAME,TYPE filters to name:type pairs; awk/sed extract just the name for vpn-type connections. ACTIVE_VPN=$(nmcli -g NAME,TYPE con show --active | awk '/:vpn/' | sed 's/:vpn.*//g') [[ -n $ACTIVE_VPN ]] && OPTIONS="${OPTIONS}\n${ACTIVE_VPN}[VPN]: $(nmcli -g ip4.address con show "${ACTIVE_VPN}" | awk -F '[:/]' '{print $1}')" # "mainbox{children:[listview];}" hides the search field for a read-only status display. echo -e "$OPTIONS" | wofi_cmd "$OPTIONS" $WIDTH_FIX_STATUS "" "mainbox{children:[listview];}" } function share_pass() { # Retrieve the current network's SSID and password via nmcli, then display them. # -oP with a lookbehind extracts only the value after "SSID: " / "Password: ". SSID=$(nmcli dev wifi show-password | grep -oP '(?<=SSID: ).*' | head -1) PASSWORD=$(nmcli dev wifi show-password | grep -oP '(?<=Password: ).*' | head -1) OPTIONS="SSID: ${SSID}\nPassword: ${PASSWORD}" # Offer QR-code generation only when qrencode is installed. [[ -x "$(command -v qrencode)" ]] && OPTIONS="${OPTIONS}\n~QrCode" SELECTION=$(echo -e "$OPTIONS" | wofi_cmd "$OPTIONS" $WIDTH_FIX_STATUS "-a -1" "mainbox{children:[listview];}") selection_action } function gen_qrcode() { # Generate a WPA QR-code PNG using qrencode and display it inside a wofi window. # -l H: high error correction; -s 25: module size 25px; -m 2: 2-module border; --dpi=192: HiDPI. DIRECTIONS=("Center" "Northwest" "North" "Northeast" "East" "Southeast" "South" "Southwest" "West") [[ -e $QRCODE_DIR$SSID.png ]] || qrencode -t png -o $QRCODE_DIR$SSID.png -l H -s 25 -m 2 --dpi=192 "WIFI:S:""$SSID"";T:""$(nmcli dev wifi show-password | grep -oP '(?<=Security: ).*' | head -1)"";P:""$PASSWORD"";;" # Open a decorationless wofi window at the chosen corner and fill it with the QR image. wofi_cmd "" "0" "" "entry{enabled:false;}window{location:"${DIRECTIONS[QRCODE_LOCATION]}";border-radius:6mm;padding:1mm;width:100mm;height:100mm; background-image:url(\"$QRCODE_DIR$SSID.png\",both);}" } function manual_hidden() { # Sub-menu to choose between a visible-but-unlisted (manual) or a truly hidden SSID. OPTIONS="~Manual\n~Hidden" && SELECTION=$(echo -e "$OPTIONS" | wofi_cmd "$OPTIONS" $WIDTH_FIX_STATUS "" "mainbox{children:[listview];}") selection_action } function vpn() { # If a VPN is already active offer to deactivate it; otherwise list all known VPN profiles. ACTIVE_VPN=$(nmcli -g NAME,TYPE con show --active | awk '/:vpn/' | sed 's/:vpn.*//g') [[ $ACTIVE_VPN ]] && OPTIONS="~Deactive $ACTIVE_VPN" || OPTIONS="$(nmcli -g NAME,TYPE connection | awk '/:vpn/' | sed 's/:vpn.*//g')" VPN_ACTION=$(echo -e "$OPTIONS" | wofi_cmd "$OPTIONS" "$WIDTH_FIX_STATUS" "" "mainbox {children:[listview];}") [[ -n "$VPN_ACTION" ]] && { { [[ "$VPN_ACTION" =~ "~Deactive" ]] && nmcli connection down "$ACTIVE_VPN" && notification "VPN_Deactivated" "$ACTIVE_VPN"; } || { # 2>/dev/null suppresses "Error: ... already active" messages from nmcli. notification "-t 0 Activating_VPN" "$VPN_ACTION" && VPN_OUTPUT=$(nmcli connection up "$VPN_ACTION" 2>/dev/null) { [[ $(echo "$VPN_OUTPUT" | grep -c "Connection successfully activated") -eq "1" ]] && notification "VPN_Successfully_Activated" "$VPN_ACTION"; } || notification "Error_Activating_VPN" "Check your configuration for $VPN_ACTION" }; } } function more_options() { # Secondary menu: share password (only when connected), status, restart, VPN, editor. OPTIONS="" [[ "$WIFI_CON_STATE" == "connected" ]] && OPTIONS="~Share Wifi Password\n" OPTIONS="${OPTIONS}~Status\n~Restart Network" # Only add the VPN entry when at least one VPN profile is configured. [[ $(nmcli -g NAME,TYPE connection | awk '/:vpn/' | sed 's/:vpn.*//g') ]] && OPTIONS="${OPTIONS}\n~VPN" # nm-connection-editor is a GTK app from NetworkManager-applet — only show if installed. [[ -x "$(command -v nm-connection-editor)" ]] && OPTIONS="${OPTIONS}\n~Open Connection Editor" SELECTION=$(echo -e "$OPTIONS" | wofi_cmd "$OPTIONS" "$WIDTH_FIX_STATUS" "" "mainbox {children:[listview];}") selection_action } function selection_action() { # Central dispatcher: map each menu label to its handler function. case "$SELECTION" in "~Disconnect") disconnect "Connection_Terminated" ;; "~Scan") scan ;; "~Status") status ;; "~Share Wifi Password") share_pass ;; "~Manual/Hidden") manual_hidden ;; "~Manual") ssid_manual ;; "~Hidden") ssid_hidden ;; "~Wi-Fi On") change_wifi_state "Wi-Fi" "Enabling Wi-Fi connection" "on" ;; "~Wi-Fi Off") change_wifi_state "Wi-Fi" "Disabling Wi-Fi connection" "off" ;; "~Eth Off") change_wired_state "Ethernet" "Disabling Wired connection" "disconnect" "${WIRED_INTERFACES}" ;; "~Eth On") change_wired_state "Ethernet" "Enabling Wired connection" "connect" "${WIRED_INTERFACES}" ;; # Informational entries — user clicked a status label, nothing to do. "***Wi-Fi Disabled***") ;; "***Wired Unavailable***") ;; "***Wired Initializing***") ;; "~Change Wifi Interface") change_wireless_interface ;; "~Restart Network") net_restart "Network" "Restarting Network" ;; "~QrCode") gen_qrcode ;; "~More Options") more_options ;; "~Open Connection Editor") nm-connection-editor ;; "~VPN") vpn ;; *) # Default: treat the selection as a network from WIFI_LIST. [[ -n "$SELECTION" ]] && [[ "$WIFI_LIST" =~ .*"$SELECTION".* ]] && { # When SSID is "*" the user selected the currently-active network row (which starts with *). [[ "$SSID" == "*" ]] && SSID=$(echo "$SELECTION" | sed "s/\s\{2,\}/\|/g " | awk -F "|" '{print $3}') # If already connected to this SSID just bring the profile up (re-apply settings). # Otherwise prompt for a password if the network is WPA2/WEP secured, then connect. { [[ "$ACTIVE_SSID" == "$SSID" ]] && nmcli con up "$SSID" ifname "${WIRELESS_INTERFACES[WLAN_INT]}"; } || { [[ "$SELECTION" =~ "WPA2" ]] || [[ "$SELECTION" =~ "WEP" ]] && enter_passwword { [[ -n "$PASS" ]] && [[ "$PASS" != "$PASSWORD_ENTER" ]] && connect "$SSID" "$PASS"; } || stored_connection "$SSID" } } ;; esac } function main() { # Entry point: load config and launch the menu. initialization && wofi_menu } main