stumpwm-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [STUMP] Hangs with run-shell-command


From: Ian Ross
Subject: Re: [STUMP] Hangs with run-shell-command
Date: Wed, 28 May 2008 16:03:39 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

Julian Stecklina <address@hidden> writes:
>
> Milan Zamazal <address@hidden> writes:
>
>>     JS> Could you post this to the SBCL people? Perhaps you can build a
>>     JS> minimal example outside of stumpwm. I guess, it would be quite
>>     JS> helpful in solving this problem.
>>
>> I'll try when I have some time...
>
> I also have problems with a non-responsive stumpwm with SBCL 1.0.16. It
> happened when I left my box unattended for the night. I'll try to
> rewrite contrib/{cpu,battery}.lisp to not use run-shell-command. Perhaps
> this makes a difference.

I have a modified version of battery.lisp that doesn't use
run-shell-command.  However, it uses split-sequence from the
cl-utilities package, and I don't know how people feel about adding that
sort of dependency to stumpwm.  I've put the code below for you to look
at.  Any guidance on whether this would be acceptable as a patch?  (I
seem to remember making some other changes at the same time, as I was
having trouble with stumpwm hanging when the battery state changed.  I
can figure out the differences needed just to remove the
run-shell-command calls and offer those as a patch if that seems like a
good idea.)

Cheers,

Ian.

--  contrib/battery.lisp  --------------------------------------------
;;; Battery charge formatters for the mode-line
;;;
;;; Copyright 2008 Vitaly Mayatskikh
;;;
;;; This module is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2, or (at your option)
;;; any later version.
;;;
;;; This module is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this software; see the file COPYING.  If not, write to
;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;;; Boston, MA 02111-1307 USA
;;;

;;; USAGE:
;;;
;;; Put:
;;;
;;;     (load "/path/to/battery.lisp")
;;;
;;; In your ~/.stumpwmrc
;;;
;;; Then you can use "%b" in your mode line format.
;;;
;;; NOTES:
;;;
;;; This is specific to Linux.

(in-package :stumpwm)

(require 'cl-utilities)

(dolist (a '((#\b fmt-bat-charge)))
  (push a *screen-mode-line-formatters*))

(defvar *bat-state* nil)
(defvar *bat-remain* 0)
(defvar *bat-remain-time* nil)
(defvar *bat-prev-time* 0)

(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-utilities:split-sequence #\: 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)
      (let ((battery-state (read-battery-file "BAT0" "state"))
            (battery-info (read-battery-file "BAT0" "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)))))))))))

;; 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*))
                  ""))))
----------------------------------------------------------------------

-- 
Ian Ross, Geographical Sciences, University of Bristol, U.K.
address@hidden




reply via email to

[Prev in Thread] Current Thread [Next in Thread]