From 0775a9397e6a3526d6106e5f59f6cf1fdd68b0b9 Mon Sep 17 00:00:00 2001 From: Philippe Brochard Date: Wed, 15 Aug 2012 18:13:40 +0200 Subject: [PATCH] contrib/toolbar.lisp: Add memory, cpu and battery usage module. --- ChangeLog | 1 + contrib/toolbar.lisp | 43 +++++++++++++++++++++++++++++++++++++--- src/tools.lisp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index cc1b9ab..8a70d6e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ * contrib/toolbar.lisp: Add a clickable label module for toolbar. Add a clickable entry to open the CLFSWM main menu. + Add memory, cpu and battery usage module. * src/clfswm-menu.lisp (open-menu): Prevent to reopen an opened menu. Fixe a but with negative selected-item. diff --git a/contrib/toolbar.lisp b/contrib/toolbar.lisp index 3ddaa46..15c0e7a 100644 --- a/contrib/toolbar.lisp +++ b/contrib/toolbar.lisp @@ -88,6 +88,14 @@ (defparameter *toolbar-list* nil) (defparameter *toolbar-module-list* nil) +(defconfig *default-toolbar* '((clfswm-menu 1) + (bat 70) + (mem 80) + (cpu 90) + (clickable-clock 99)) + 'Toolbar "Default toolbar modules") + + ;;; CONFIG - Toolbar window string colors (defconfig *toolbar-window-font-string* *default-font-string* 'Toolbar "Toolbar window font string") @@ -500,7 +508,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" @@ -523,12 +531,41 @@ "(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" (declare (ignore text code state toolbar module)) - (dbg placement) (let ((*info-mode-placement* (or placement *info-mode-placement*))) (open-menu))) +;;; +;;; CPU usage +;;; +(define-toolbar-module (cpu) + "Display the CPU usage" + (toolbar-module-text toolbar module "CPU:~A%" (cpu-usage))) + + +;;; +;;; Memory usage +;;; +(define-toolbar-module (mem) + "Display the memory usage" + (multiple-value-bind (used total) + (memory-usage) + (toolbar-module-text toolbar module "Mem:~A%" (round (* (/ used total) 100.0))))) + + + +;;; +;;; Battery usage +;;; +(define-toolbar-module (bat) + "Display the battery usage" + (let* ((bat (battery-usage)) + (alert (battery-alert-string bat))) + (toolbar-module-text toolbar module "Bat:~A~A%~A" alert bat alert))) + + + diff --git a/src/tools.lisp b/src/tools.lisp index 19fd849..7c8cc43 100644 --- a/src/tools.lisp +++ b/src/tools.lisp @@ -121,7 +121,11 @@ :find-string :find-all-strings :subst-strings - :test-find-string)) + :test-find-string + :memory-usage + :cpu-usage + :battery-usage + :battery-alert-string)) (in-package :tools) @@ -1036,3 +1040,53 @@ Useful for re-using the &REST arg after removing some options." (nth day days) (nth (1- month) months) date year))))) +;;; +;;; System information functions +;;; +(defmacro with-search-line ((word line) &body body) + `(let ((pos (search ,word ,line :test #'string-equal))) + (when (>= (or pos -1) 0) + ,@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 cpu-usage () + (let ((output (do-shell "top -b -n 2 -d 0.1")) + (cpu 0)) + (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)))) + cpu)) + +(defun battery-usage () + (let ((output (do-shell "acpi -b")) + (bat 0)) + (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)))) + bat)) + +(defun battery-alert-string (bat) + (cond ((<= bat 5) "/!\\") + ((<= bat 10) "!!") + ((<= bat 25) "!") + (t ""))) + +;;; +;;; System usage poll system +;;; +;;; echo "while true; do (acpi -b; top -b -n 2 -d 1 | grep '%Cpu(s)' ; free) > /tmp/clfswm-sys-poll.tmp; mv /tmp/clfswm-sys-poll.tmp /tmp/clfswm-sys-poll; sleep 10; done" > poller + -- 2.11.4.GIT