[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[STUMP] [PATCH] Changes to contrib/battery.lisp and contrib/cpu.lisp
From: |
Ian Ross |
Subject: |
[STUMP] [PATCH] Changes to contrib/battery.lisp and contrib/cpu.lisp |
Date: |
Wed, 28 May 2008 22:10:31 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) |
- Made battery name a defvar in contrib/battery.lisp to take account of
machines with non-standard battery names (requested by Bill Clementson).
- Removed uses of run-shell-command from contrib/cpu.lisp, replacing them
with native file processing.
- Made ACPI thermal zone name a defvar in contrib/cpu.lisp.
---
contrib/battery.lisp | 8 ++++++--
contrib/cpu.lisp | 18 ++++++++++++++++--
2 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/contrib/battery.lisp b/contrib/battery.lisp
index 1f2ac02..89fd562 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,6 +44,8 @@
(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))
@@ -56,8 +60,8 @@
(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)
- (let ((battery-state (read-battery-file "BAT0" "state"))
- (battery-info (read-battery-file "BAT0" "info")))
+ (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))
diff --git a/contrib/cpu.lisp b/contrib/cpu.lisp
index 994b546..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 | 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 | tr -d [:cntrl:][:space:]" t))
+ (get-proc-file-field (concatenate 'string "/proc/acpi/thermal_zone/"
+ *acpi-thermal-zone* "/temperature")
+ "temperature"))
--
1.5.2.5
- [STUMP] [PATCH] Changes to contrib/battery.lisp and contrib/cpu.lisp,
Ian Ross <=