(defpackage #:config.wifi (:use #:stumpwm #:cl) (:import-from #:config.helpers #:run-program #:run-program-safe #:with-destructured-alist) (:export #:wifi-status #:signal-strength)) (in-package #:config.wifi) (defun get-signal-strength () (let ((output (run-program "iwctl station wlan0 show | grep AverageRSSI"))) (second (str:split #\Space (str:collapse-whitespaces output) :omit-nulls t)))) (defun signal-strength () (handler-case (get-signal-strength) (condition () ""))) (defun connected-to-p (string) (str:starts-with-p "" string)) (defun clean-string (string) (let ((split (str:split #\Space string :omit-nulls t))) (if (connected-to-p string) (fourth split) (first split)))) (defun scan () (run-program "iwctl station wlan0 scan")) (defun list-wifi () (let* ((out (run-program "iwctl station wlan0 get-networks")) (split (str:split #\Newline out :omit-nulls t)) (wanted (nthcdr 4 split)) (cleaned (mapcar #'clean-entry wanted))) (select-from-menu (current-screen) (mapcar (lambda (cleaned) (cons cleaned cleaned)) cleaned)))) (defun connect-to-wifi (&optional password) (flet ((without-pass (station) (format nil "iwctl station wlan0 connect ~A" station)) (with-pass (station) (let ((pass (read-one-line (current-screen) "Wifi Pass: " :password t))) (when (and pass (string/= pass "")) (format nil "iwctl station wlan0 connect ~A password ~A" station pass))))) (with-destructured-alist (station ignored) (list-wifi) (when station (let ((conn? (funcall (if password #'with-pass #'without-pass) station ))) (when conn? (run-program conn?))))))) (defun disconnect-wifi () (let ((c (format nil "iwctl station ~A disconnect" *station*))) (echo (run-program c)))) (defparameter *wifi-menu* `(("Connect w/p" . ,(lambda () (connect-to-wifi t))) ("Connect wo/p" . ,(lambda () (connect-to-wifi nil))) ("Disconnect" . disconnect-wifi) ("Scan" . scan) ("List networks" . display-wifi))) (defcommand display-wifi-menu () () (with-destructured-alist (key command) (select-from-menu (current-screen) ; screen *wifi-menu* "Wifi: ") (when command (funcall command)))) (define-key *root-map* (kbd "M-w") "display-wifi-menu")