[clfswm-cvs] [clfswm-git]CLFSWM - A(nother) Common Lisp FullScreen Window Manager branch master updated. R-1106-87-g54755d7

Philippe Brochard pbrochard at common-lisp.net
Thu Aug 16 20:43:45 UTC 2012


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "CLFSWM - A(nother) Common Lisp FullScreen Window Manager".

The branch, master has been updated
       via  54755d729577bffeaada06bb44a1475f2ec8614b (commit)
       via  788919822efa6da4598159b4da3cc3d479f9de80 (commit)
       via  0775a9397e6a3526d6106e5f59f6cf1fdd68b0b9 (commit)
      from  46bb64f71eca468fa55fc08e195e1924f4d559bb (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 54755d729577bffeaada06bb44a1475f2ec8614b
Author: Philippe Brochard <pbrochard at common-lisp.net>
Date:   Thu Aug 16 22:43:39 2012 +0200

    src/tools.lisp: Factorize system usage information collection.

diff --git a/ChangeLog b/ChangeLog
index 278c7cd..26d6f77 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2012-08-16  Philippe Brochard  <pbrochard at common-lisp.net>
 
+	* src/tools.lisp: Factorize system usage information collection.
+
 	* contrib/toolbar.lisp: Add memory, cpu and battery usage module
 	with a poll methode. More responsive but up to date every 10s by
 	default.
diff --git a/contrib/toolbar.lisp b/contrib/toolbar.lisp
index bcd1f62..8e0faac 100644
--- a/contrib/toolbar.lisp
+++ b/contrib/toolbar.lisp
@@ -371,7 +371,8 @@
 
 (defun close-all-toolbars ()
   (dolist (toolbar *toolbar-list*)
-    (close-toolbar toolbar)))
+    (close-toolbar toolbar))
+  (stop-system-poll))
 
 (defun create-toolbar-modules (modules)
   (loop for mod in modules
@@ -512,7 +513,7 @@
       (get-decoded-time)
     (declare (ignore s))
     (with-set-toolbar-module-rectangle (module)
-      (toolbar-module-text toolbar module "(~2,'0D:~2,'0D)" h m))))
+      (toolbar-module-text toolbar module "~2,'0D:~2,'0D" h m))))
 
 
 (defconfig *toolbar-clock-action* "xclock -analog"
@@ -535,7 +536,7 @@
   "(text placement) - Display an entry for the CLFSWM menu"
   (declare (ignore placement))
   (with-set-toolbar-module-rectangle (module)
-    (toolbar-module-text toolbar module (or text "(CLFSWM)"))))
+    (toolbar-module-text toolbar module (or text "CLFSWM"))))
 
 (define-toolbar-module-click (clfswm-menu text placement)
   "Open the CLFSWM main menu"
@@ -547,7 +548,7 @@
 ;;; CPU usage
 ;;;
 (define-toolbar-module (cpu)
-  "Display the CPU usage"
+  "Display the CPU usage (slow methode)"
   (toolbar-module-text toolbar module "CPU:~A%" (cpu-usage)))
 
 
@@ -555,7 +556,7 @@
 ;;; Memory usage
 ;;;
 (define-toolbar-module (mem)
-  "Display the memory usage"
+  "Display the memory usage (slow methode)"
   (multiple-value-bind (used total)
       (memory-usage)
     (toolbar-module-text toolbar module "Mem:~A%" (round (* (/ used total) 100.0)))))
@@ -566,7 +567,7 @@
 ;;; Battery usage
 ;;;
 (define-toolbar-module (bat)
-  "Display the battery usage"
+  "Display the battery usage (slow methode)"
   (let* ((bat (battery-usage))
          (alert (battery-alert-string bat)))
     (toolbar-module-text toolbar module "Bat:~A~A%~A" alert bat alert)))
@@ -595,3 +596,4 @@
     (toolbar-module-text toolbar module "CPU:~A% Mem:~A%"
                          cpu
                          (round (* (/ used total) 100)))))
+
diff --git a/src/tools.lisp b/src/tools.lisp
index 22b06b9..81f06c7 100644
--- a/src/tools.lisp
+++ b/src/tools.lisp
@@ -1048,56 +1048,82 @@ Useful for re-using the &REST arg after removing some options."
 ;;;
 ;;; System information functions
 ;;;
+(defparameter *bat-cmd* "acpi -b")
+(defparameter *cpu-cmd* "top -b -n 2 -d 1 -p 0")
+(defparameter *cpu-cmd-fast* "top -b -n 2 -d 0.1 -p 0")
+(defparameter *mem-cmd* "free")
+
 (defmacro with-search-line ((word line) &body body)
   `(let ((pos (search ,word ,line :test #'string-equal)))
     (when (>= (or pos -1) 0)
       , at body)))
 
-(defun memory-usage ()
-  (let ((output (do-shell "free"))
-        (used 0)
-        (total 0))
-    (loop for line = (read-line output nil nil)
-       while line
-       do (with-search-line ("cache:" line)
-            (setf used (parse-integer (subseq line (+ pos 6)) :junk-allowed t)))
-         (with-search-line ("mem:" line)
-           (setf total (parse-integer (subseq line (+ pos 4)) :junk-allowed t))))
-    (values used total)))
+(defun extract-battery-usage (line)
+  (with-search-line ("Battery" line)
+    (let ((pos (position #\% line)))
+      (when pos
+        (parse-integer (subseq line (- pos 3) pos) :junk-allowed t)))))
+
+(defun extract-cpu-usage (line)
+  (with-search-line ("%Cpu(s):" line)
+    (let ((pos1 (search "id" line)))
+      (when pos1
+        (let ((pos2 (position #\, line :from-end t :end pos1)))
+          (when pos2
+            (- 100 (parse-integer (subseq line (1+ pos2) pos1) :junk-allowed t))))))))
+
+(defun extract-mem-used (line)
+  (with-search-line ("cache:" line)
+    (parse-integer (subseq line (+ pos 6)) :junk-allowed t)))
+
+(defun extract-mem-total (line)
+  (with-search-line ("mem:" line)
+    (parse-integer (subseq line (+ pos 4)) :junk-allowed t)))
+
+(let ((total -1))
+  (defun memory-usage ()
+    (let ((output (do-shell *mem-cmd*))
+          (used -1))
+      (loop for line = (read-line output nil nil)
+         while line
+         do (awhen (extract-mem-used line)
+              (setf used it))
+           (awhen (and (= total -1) (extract-mem-total line))
+             (setf total it)))
+      (values used total))))
 
 
 (defun cpu-usage ()
-  (let ((output (do-shell "top -b -n 2 -d 0.1"))
-        (cpu 0))
+  (let ((output (do-shell *cpu-cmd-fast*))
+        (cpu -1))
     (loop for line = (read-line output nil nil)
        while line
-       do (with-search-line ("%Cpu(s):" line)
-            (setf cpu (parse-integer (subseq line (+ pos 8)) :junk-allowed t))))
+       do (awhen (extract-cpu-usage line)
+            (setf cpu it)))
     cpu))
 
 (defun battery-usage ()
-  (let ((output (do-shell "acpi -b"))
-        (bat 0))
+  (let ((output (do-shell *bat-cmd*))
+        (bat -1))
     (loop for line = (read-line output nil nil)
        while line
-       do (with-search-line ("%" line)
-            (setf bat (parse-integer (subseq line (- pos 3) pos) :junk-allowed t))))
+       do (awhen (extract-battery-usage line)
+            (setf bat it)))
     bat))
 
 (defun battery-alert-string (bat)
-  (cond ((<= bat 5) "/!\\")
-        ((<= bat 10) "!!")
-        ((<= bat 25) "!")
-        (t "")))
+  (if (numberp bat)
+      (cond ((<= bat 5) "/!\\")
+            ((<= bat 10) "!!")
+            ((<= bat 25) "!")
+            (t ""))
+      ""))
 
 ;;;
 ;;; System usage with a poll system - Memory, CPU and battery all in one
 ;;;
-(let ((poll-log "/tmp/clfswm-system.log")
-      (bat-cmd "acpi -b")
-      (cpu-cmd "top -b -n 2 -d 1 | grep '%Cpu(s)'")
-      (mem-cmd "free")
-      (poll-exec "/tmp/clfswm-system.sh")
+(let ((poll-log "/tmp/.clfswm-system.log")
+      (poll-exec "/tmp/.clfswm-system.sh")
       (running nil))
   (defun create-system-poll (delay)
     (with-open-file (stream poll-exec :direction :output :if-exists :supersede)
@@ -1107,8 +1133,7 @@ while true; do
  (~A; ~A ; ~A) > ~A.tmp;
   mv ~A.tmp ~A;
   sleep ~A;
-done~%" bat-cmd cpu-cmd mem-cmd poll-log poll-log poll-log delay))
-    (fdo-shell "/bin/chmod a+x ~A" poll-exec))
+done~%" *bat-cmd* *cpu-cmd* *mem-cmd* poll-log poll-log poll-log delay)))
 
   (defun system-poll-pid ()
     (let ((pid nil))
@@ -1122,13 +1147,17 @@ done~%" bat-cmd cpu-cmd mem-cmd poll-log poll-log poll-log delay))
   (defun stop-system-poll ()
     (dolist (pid (system-poll-pid))
       (fdo-shell "kill ~A" pid))
+    (when (probe-file poll-log)
+      (delete-file poll-log))
+    (when (probe-file poll-exec)
+      (delete-file poll-exec))
     (setf running nil))
 
   (defun start-system-poll (delay)
     (unless running
       (stop-system-poll)
       (create-system-poll delay)
-      (do-execute poll-exec nil nil :stream)
+      (fdo-shell "exec sh ~A" poll-exec)
       (setf running t)))
 
   (defun system-usage-poll (&optional (delay 10))
@@ -1141,17 +1170,15 @@ done~%" bat-cmd cpu-cmd mem-cmd poll-log poll-log poll-log delay))
         (with-open-file (stream poll-log :direction :input)
           (loop for line = (read-line stream nil nil)
              while line
-             do (with-search-line ("Battery" line)
-                  (let ((pos (position #\% line)))
-                    (when pos
-                      (setf bat (parse-integer (subseq line (- pos 3) pos) :junk-allowed t)))))
-               (with-search-line ("%Cpu(s):" line)
-                 (setf cpu (parse-integer (subseq line (+ pos 8)) :junk-allowed t)))
-               (with-search-line ("cache:" line)
-                 (setf used (parse-integer (subseq line (+ pos 6)) :junk-allowed t)))
-               (with-search-line ("mem:" line)
-                 (setf total (parse-integer (subseq line (+ pos 4)) :junk-allowed t))))
-          (values cpu used total bat))))))
+             do (awhen (extract-battery-usage line)
+                  (setf bat it))
+               (awhen (extract-cpu-usage line)
+                 (setf cpu it))
+               (awhen (extract-mem-used line)
+                 (setf used it))
+               (awhen (and (= total -1) (extract-mem-total line))
+                 (setf total it)))))
+      (values cpu used total bat))))
 
 
 

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog            |    9 +++
 contrib/toolbar.lisp |   71 ++++++++++++++++++++++-
 src/tools.lisp       |  153 +++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 228 insertions(+), 5 deletions(-)


hooks/post-receive
-- 
CLFSWM - A(nother) Common Lisp FullScreen Window Manager




More information about the clfswm-cvs mailing list