From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: jmarant@nerim.net (=?iso-8859-15?q?J=E9r=F4me_Marant?=) Newsgroups: gmane.emacs.devel Subject: [Mario Lang] battery.el ACPI update Date: Fri, 21 May 2004 13:01:08 +0200 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <87u0yaavt7.fsf@marant.org> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1085140053 4825 80.91.224.253 (21 May 2004 11:47:33 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 21 May 2004 11:47:33 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Fri May 21 13:47:22 2004 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BR8Uk-0002mo-00 for ; Fri, 21 May 2004 13:47:22 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BR8Uj-0005WD-00 for ; Fri, 21 May 2004 13:47:21 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.34) id 1BR8Qw-0004Y5-TF for emacs-devel@quimby.gnus.org; Fri, 21 May 2004 07:43:27 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.34) id 1BR83d-0001S5-4U for emacs-devel@gnu.org; Fri, 21 May 2004 07:19:21 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.34) id 1BR82w-0001N3-Gv for emacs-devel@gnu.org; Fri, 21 May 2004 07:19:10 -0400 Original-Received: from [62.4.17.105] (helo=mallaury.noc.nerim.net) by monty-python.gnu.org with esmtp (Exim 4.34) id 1BR7mV-0006IB-72 for emacs-devel@gnu.org; Fri, 21 May 2004 07:01:39 -0400 Original-Received: from amboise (jmarant.net1.nerim.net [62.212.105.6]) by mallaury.noc.nerim.net (Postfix) with ESMTP id 9077B62D35 for ; Fri, 21 May 2004 12:58:36 +0200 (CEST) Original-Received: by amboise (Postfix, from userid 1000) id 8D6C72C0EC; Fri, 21 May 2004 13:01:08 +0200 (CEST) Original-To: emacs-devel@gnu.org User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:23812 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:23812 Hi, Mario Lang asked me to forward this message. Cheers, --=3D=3D-=3D-=3D Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 8bit To: emacs-devel@gnu.org Subject: battery.el ACPI update From: Mario Lang X-Draft-From: ("nnml:mail.emacs-devel" "") Date: Wed, 19 May 2004 17:44:16 +0200 Message-ID: <87n0447773.fsf@lexx.delysid.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Xref: lexx.delysid.org archive:2333 MIME-Version: 1.0 Content-Type: multipart/signed; boundary=3D"=3D-=3D-=3D" Lines: 178 --=3D-=3D-=3D Hi. The following patch makes `battery-linux-proc-acpi' works with more different systems and fixes an anoying division-by-zero bug in the remaining-time calculation. Please install. 2004-05-19 Mario Lang * battery.el (battery-linux-proc-acpi): mA was hardcored, but some systems appear to use mW, make the code handle this. Fix a division-by-zero bug while at it, and handle kernels with a slightly different layout in /proc/acpi. Index: lisp/battery.el =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/emacs/emacs/lisp/battery.el,v retrieving revision 1.18 diff -u -r1.18 battery.el --- lisp/battery.el 10 Sep 2003 19:51:01 -0000 1.18 +++ lisp/battery.el 19 May 2004 15:36:34 -0000 @@ -61,7 +61,7 @@ (cond ((eq battery-status-function 'battery-linux-proc-apm) "Power %L, battery %B (%p%% load, remaining time %t)") ((eq battery-status-function 'battery-linux-proc-acpi) - "Power %L, battery %B at %r mA (%p%% load, remaining time %t)")) + "Power %L, battery %B at %r (%p%% load, remaining time %t)")) "*Control string formatting the string to display in the echo area. Ordinary characters in the control string are printed as-is, while conversion specifications introduced by a `%' character in the control @@ -243,50 +243,59 @@ %m Remaining time in minutes %h Remaining time in hours %t Remaining time in the form `h:min'" - (let (capacity design-capacity rate charging-state warn low minutes hour= s) - (when (file-directory-p "/proc/acpi/battery/") - ;; ACPI provides information about each battery present in the syste= m in - ;; a separate subdirectory. We are going to merge the available - ;; information together since displaying for a variable amount of - ;; batteries seems overkill for format-strings. - (mapc - (lambda (dir) - (with-temp-buffer - (insert-file-contents (expand-file-name "state" dir)) - (when (re-search-forward "present: +yes$" nil t) - (and (re-search-forward "charging state: +\\(.*\\)$" nil t) - (or (null charging-state) (string=3D charging-state - "unknown")) - ;; On most multi-battery systems, most of the time only one - ;; battery is "charging"/"discharging", the others are - ;; "unknown". - (setq charging-state (match-string 1))) - (when (re-search-forward "present rate: +\\([0-9]+\\) mA$" nil t) - (setq rate (+ (or rate 0) (string-to-int (match-string 1))))) - (when (re-search-forward "remaining capacity: +\\([0-9]+\\) mAh$" - nil t) - (setq capacity - (+ (or capacity 0) (string-to-int (match-string 1)))))) - (goto-char (point-max)) - (insert-file-contents (expand-file-name "info" dir)) - (when (re-search-forward "present: +yes$" nil t) - (when (re-search-forward "design capacity: +\\([0-9]+\\) mAh$" - nil t) - (setq design-capacity (+ (or design-capacity 0) - (string-to-int (match-string 1))))) - (when (re-search-forward "design capacity warning: +\\([0-9]+\\) mAh= $" - nil t) - (setq warn (+ (or warn 0) (string-to-int (match-string 1))))) - (when (re-search-forward "design capacity low: +\\([0-9]+\\) mAh$" - nil t) - (setq low (+ (or low 0) - (string-to-int (match-string 1)))))))) - (directory-files "/proc/acpi/battery/" t "BAT"))) + (let (capacity design-capacity rate rate-type charging-state warn low + minutes hours) + ;; ACPI provides information about each battery present in the system = in + ;; a separate subdirectory. We are going to merge the available + ;; information together since displaying for a variable amount of + ;; batteries seems overkill for format-strings. + (mapc + (lambda (dir) + (with-temp-buffer + (insert-file-contents (expand-file-name "state" dir)) + (when (re-search-forward "present: +yes$" nil t) + (and (re-search-forward "charging state: +\\(.*\\)$" nil t) + (or (null charging-state) (string=3D charging-state + "unknown")) + ;; On most multi-battery systems, most of the time only one + ;; battery is "charging"/"discharging", the others are + ;; "unknown". + (setq charging-state (match-string 1))) + (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$" + nil t) + (setq rate (+ (or rate 0) (string-to-int (match-string 1))) + rate-type (or (and rate-type + (if (string=3D rate-type (match-string 2)) + rate-type + (error + "Inconsistent rate types (%s vs. %s)" + rate-type (match-string 2)))) + (match-string 2)))) + (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$" + nil t) + (setq capacity + (+ (or capacity 0) (string-to-int (match-string 1)))))) + (goto-char (point-max)) + (insert-file-contents (expand-file-name "info" dir)) + (when (re-search-forward "present: +yes$" nil t) + (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$" + nil t) + (setq design-capacity (+ (or design-capacity 0) + (string-to-int (match-string 1))))) + (when (re-search-forward "design capacity warning: +\\([0-9]+\\) m[AW]= h$" + nil t) + (setq warn (+ (or warn 0) (string-to-int (match-string 1))))) + (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$" + nil t) + (setq low (+ (or low 0) + (string-to-int (match-string 1)))))))) + (directory-files "/proc/acpi/battery/" t "BAT")) (and capacity rate - (setq minutes (floor (* (/ (float (if (string=3D charging-state - "charging") - (- design-capacity capacity) - capacity)) rate) 60)) + (setq minutes (if (zerop rate) 0 + (floor (* (/ (float (if (string=3D charging-state + "charging") + (- design-capacity capacity) + capacity)) rate) 60))) hours (/ minutes 60))) (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A")) (cons ?L (or (when (file-exists-p "/proc/acpi/ac_adapter/AC/state") @@ -304,13 +313,22 @@ (when (re-search-forward "temperature: +\\([0-9]+\\) C$" nil t) (match-string 1)))) + (when (file-exists-p + "/proc/acpi/thermal_zone/THM/temperature") + (with-temp-buffer + (insert-file-contents + "/proc/acpi/thermal_zone/THM/temperature") + (when (re-search-forward + "temperature: +\\([0-9]+\\) C$" nil t) + (match-string 1)))) "N/A")) - (cons ?r (or (and rate (number-to-string rate)) "N/A")) + (cons ?r (or (and rate (concat (number-to-string rate) " " + rate-type)) "N/A")) (cons ?B (or charging-state "N/A")) (cons ?b (or (and (string=3D charging-state "charging") "+") (and low (< capacity low) "!") - (and warn (< capacity warn) "-") - "")) + (and warn (< capacity warn) "-") + "")) (cons ?h (or (and hours (number-to-string hours)) "N/A")) (cons ?m (or (and minutes (number-to-string minutes)) "N/A")) (cons ?t (or (and minutes --=20 CYa, Mario | Debian Developer | Get my public key via finger mlang@db.debian.org | 1024D/7FC1A0854909BCCDBE6C102DDFFC022A6B113E44 --=3D=3D-=3D-=3D --=20 J=E9r=F4me Marant