[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [STUMP] [PATCH] Changes to contrib/battery.lisp and contrib/cpu.lisp
From: |
Ian Ross |
Subject: |
Re: [STUMP] [PATCH] Changes to contrib/battery.lisp and contrib/cpu.lisp |
Date: |
Thu, 19 Jun 2008 21:35:12 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) |
Hi Shawn,
"Shawn Betts" <address@hidden> writes:
>
> Sorry for the delay in getting back to you.
No problem!
> Can you roll together this patch and any others that are required to
> apply it to the repository into one nice, neat patch that I can apply?
Here's a rolled-up patch for both the changes I made (I've not yet done
the stuff related to the new kernel version or multiple batteries, but
this at least removes the run-shell-command dependencies that were
causing some people problems).
Cheers,
Ian.
- Removed uses of run-shell-command from contrib/battery.lisp and
contrib/cpu.lisp, replacing them with native file processing.
- Made battery name a defvar in contrib/battery.lisp to take account of
machines with non-standard battery names (requested by Bill Clementson).
- Made ACPI thermal zone name a defvar in contrib/cpu.lisp.
---
contrib/battery.lisp | 80 +++++++++++++++++++++++++++++++++++++++++---------
contrib/cpu.lisp | 18 ++++++++++-
2 files changed, 82 insertions(+), 16 deletions(-)
diff --git a/contrib/battery.lisp b/contrib/battery.lisp
index 9be6ae2..900bc08 100644
--- a/contrib/battery.lisp
+++ b/contrib/battery.lisp
@@ -34,6 +34,8 @@
(in-package :stumpwm)
+(export '(*battery-name*))
+
(dolist (a '((#\b fmt-bat-charge)))
(push a *screen-mode-line-formatters*))
@@ -42,26 +44,44 @@
(defvar *bat-remain-time* nil)
(defvar *bat-prev-time* 0)
+(defvar *battery-name* "BAT0")
+
+(defun read-battery-file (battery fname)
+ (let ((fields (make-hash-table :test #'equal)))
+ (with-open-file (s (concatenate 'string "/proc/acpi/battery/" battery "/"
fname))
+ (do ((line (read-line s nil nil) (read-line s nil nil)))
+ ((null line) fields)
+ (let ((split (cl-ppcre:split ":\\s*" line)))
+ (setf (gethash (string-trim '(#\Space) (car split)) fields)
+ (string-trim '(#\Space) (cadr split))))))))
+
(defun current-battery-charge ()
"Calculate remaining battery charge. Don't make calculation more than once
in 15 seconds."
(let ((now (/ (get-internal-real-time) internal-time-units-per-second)))
(when (or (= 0 *bat-prev-time*) (>= (- now *bat-prev-time*) 15))
(setf *bat-prev-time* now)
- (if (string= "no" (string-trim '(#\Newline) (run-shell-command "grep
present /proc/acpi/battery/BAT0/state | grep -o -e '\\w*$'" t)))
- (setq *bat-state* nil)
- (let ((remain (parse-integer (run-shell-command "grep remaining\\
capacity /proc/acpi/battery/BAT0/state | sed -n 's/[a-zA-Z\\ \\:]*//gp'" t)))
- (rate (/ (parse-integer (run-shell-command "grep present\\ rate
/proc/acpi/battery/BAT0/state | sed -n 's/[a-zA-Z\\ \\:]*//gp'" t)) 60))
- (full (parse-integer (run-shell-command "grep last\\ full\\
capacity /proc/acpi/battery/BAT0/info | sed -n 's/[a-zA-Z\\:\\ ]//gp'" t))))
-
- (setq *bat-remain* (round (/ (* 100 remain) full))
- *bat-state* (string-trim '(#\Newline) (run-shell-command
"grep charging\\ state /proc/acpi/battery/BAT0/state | grep -o -e '\\w*$'" t))
- *bat-remain-time* nil)
+ (let ((battery-state (read-battery-file *battery-name* "state"))
+ (battery-info (read-battery-file *battery-name* "info")))
+ (if (string= "no" (gethash "present" battery-state))
+ (setq *bat-state* nil)
+ (let ((charge-state (gethash "charging state" battery-state))
+ (remain (parse-integer (gethash "remaining capacity"
battery-state)
+ :junk-allowed t))
+ (rate (/ (or (parse-integer (gethash "present rate"
battery-state)
+ :junk-allowed t) 0) 60))
+ (full (parse-integer (gethash "design capacity" battery-info)
+ :junk-allowed t)))
+ (setq *bat-remain* (round (/ (* 100 remain) full))
+ *bat-state* charge-state
+ *bat-remain-time* nil)
- (when (> rate 0)
- (let* ((online (round (/ (if (string= "charging" *bat-state*) (-
full remain) remain) rate))))
- (setq *bat-remain-time* (multiple-value-bind (h m)
- (truncate online 60)
- (list h m))))))))))
+ (when (> rate 0)
+ (let* ((online (round (/ (if (string= "charging" *bat-state*)
+ (- full remain) remain)
+ rate))))
+ (setq *bat-remain-time* (multiple-value-bind (h m)
+ (truncate online 60)
+ (list h m)))))))))))
(defun fmt-bat-charge (ml)
"Returns a string representing the remaining battery charge (for laptop
users.)"
@@ -72,3 +92,35 @@
*bat-remain*
(if *bat-remain-time*
(format nil " (~2,'0d:~2,'0d) ~A" (car *bat-remain-time*)
(cadr *bat-remain-time*) *bat-state*) "")) "no battery"))
+
+
+;; Alternative display:
+;;
+;; TT: RRR% (HH:MM) [or "NO BAT" if present = no]
+;;
+;; TT = AC/DC (AC if charging state = charged/charging,
+;; DC if charging state = discharging)
+;;
+;; RRR = remain/full
+;;
+;; HH:MM = time until charged/discharged (present when state is charging
+;; or discharging)
+;;
+;; (defun fmt-bat-charge (ml)
+;; "Returns a string representing the remaining battery charge (for laptop
users.)"
+;; (declare (ignore ml))
+;; (current-battery-charge)
+;; (if (not *bat-state*)
+;; "NO BAT"
+;; (format nil "~A:~D%~A"
+;; (if (or (string= *bat-state* "charging")
+;; (string= *bat-state* "charged"))
+;; "AC" "DC")
+;; *bat-remain*
+;; (if (and (string/= *bat-state* "charged") *bat-remain-time*)
+;; (format nil (if (and (= (car *bat-remain-time*) 0)
+;; (< (cadr *bat-remain-time*) 30))
+;; " (^[^B^1*~2,'0d:~2,'0d^])" "
(~2,'0d:~2,'0d)")
+;; (car *bat-remain-time*)
+;; (cadr *bat-remain-time*))
+;; ""))))
diff --git a/contrib/cpu.lisp b/contrib/cpu.lisp
index 9e2d41f..d62a080 100644
--- a/contrib/cpu.lisp
+++ b/contrib/cpu.lisp
@@ -34,6 +34,8 @@
(in-package :stumpwm)
+(export '(*acpi-thermal-zone*))
+
;; Install formatters.
(dolist (a '((#\c fmt-cpu-usage)
(#\C fmt-cpu-usage-bar)
@@ -104,13 +106,25 @@ utilization."
(let ((cpu (truncate (* 100 (current-cpu-usage)))))
(bar cpu width full empty)))
+(defun get-proc-file-field (fname field)
+ (with-open-file (s fname)
+ (do ((line (read-line s nil nil) (read-line s nil nil)))
+ ((null line) nil)
+ (let ((split (cl-ppcre:split "\\s*:\\s*" line)))
+ (when (string= (car split) field) (return (cadr split)))))))
+
(defun fmt-cpu-freq (ml)
"Returns a string representing the current CPU frequency (especially useful
for laptop users.)"
(declare (ignore ml))
- (let ((mhz (parse-integer (run-shell-command "sed -n 's/^cpu MHz\\s\\+:
\\(.\\+\\)\\..*$/\\1/p' /proc/cpuinfo 2>/dev/null | head -1 | tr -d [:cntrl:]"
t))))
+ (let ((mhz (parse-integer (get-proc-file-field "/proc/cpuinfo" "cpu MHz")
+ :junk-allowed t)))
(apply 'format nil "~F~A" (if (>= mhz 1000) (list (/ mhz 1000) "GHz")
(list mhz "MHz")))))
+(defvar *acpi-thermal-zone* "THRM")
+
(defun fmt-cpu-temp (ml)
"Returns a string representing the current CPU temperature."
(declare (ignore ml))
- (run-shell-command "sed -n 's/temperature:[[:space:]]*//p'
/proc/acpi/thermal_zone/THRM/temperature 2>/dev/null | tr -d
[:cntrl:][:space:]" t))
+ (get-proc-file-field (concatenate 'string "/proc/acpi/thermal_zone/"
+ *acpi-thermal-zone* "/temperature")
+ "temperature"))
--
1.5.2.5