From: jmarant@nerim.net (Jérôme Marant)
Subject: [Mario Lang] battery.el ACPI update
Date: Fri, 21 May 2004 13:01:08 +0200 [thread overview]
Message-ID: <87u0yaavt7.fsf@marant.org> (raw)
Hi,
Mario Lang asked me to forward this message.
Cheers,
--==-=-=
Content-Type: message/rfc822
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
To: emacs-devel@gnu.org
Subject: battery.el ACPI update
From: Mario Lang <mlang@delysid.org>
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="=-=-="
Lines: 178
--=-=-=
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 <mlang@delysid.org>
* 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
===================================================================
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 hours)
- (when (file-directory-p "/proc/acpi/battery/")
- ;; 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= 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= 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= 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= charging-state
- "charging")
- (- design-capacity capacity)
- capacity)) rate) 60))
+ (setq minutes (if (zerop rate) 0
+ (floor (* (/ (float (if (string= 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= 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
--
CYa,
Mario | Debian Developer <URL:http://debian.org/>
| Get my public key via finger mlang@db.debian.org
| 1024D/7FC1A0854909BCCDBE6C102DDFFC022A6B113E44
--==-=-=
--
Jérôme Marant
next reply other threads:[~2004-05-21 11:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-05-21 11:01 Jérôme Marant [this message]
2004-05-27 20:26 ` [Mario Lang] battery.el ACPI update Jérôme Marant
2004-05-29 1:44 ` Richard Stallman
2004-06-01 11:55 ` Jérôme Marant
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87u0yaavt7.fsf@marant.org \
--to=jmarant@nerim.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).