unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [Mario Lang] battery.el ACPI update
@ 2004-05-21 11:01 Jérôme Marant
  2004-05-27 20:26 ` Jérôme Marant
  0 siblings, 1 reply; 4+ messages in thread
From: Jérôme Marant @ 2004-05-21 11:01 UTC (permalink / 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Mario Lang] battery.el ACPI update
  2004-05-21 11:01 [Mario Lang] battery.el ACPI update Jérôme Marant
@ 2004-05-27 20:26 ` Jérôme Marant
  2004-05-29  1:44   ` Richard Stallman
  0 siblings, 1 reply; 4+ messages in thread
From: Jérôme Marant @ 2004-05-27 20:26 UTC (permalink / raw)



Could someone please take care of this?

jmarant@nerim.net (Jérôme Marant) writes:

> 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
>
>
> _______________________________________________
> Emacs-devel mailing list
> Emacs-devel@gnu.org
> http://mail.gnu.org/mailman/listinfo/emacs-devel
>

-- 
Jérôme Marant

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Mario Lang] battery.el ACPI update
  2004-05-27 20:26 ` Jérôme Marant
@ 2004-05-29  1:44   ` Richard Stallman
  2004-06-01 11:55     ` Jérôme Marant
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Stallman @ 2004-05-29  1:44 UTC (permalink / raw)
  Cc: emacs-devel

I forwarded that to rs@nunatak.allgaeu.org who maintains battery.el

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Mario Lang] battery.el ACPI update
  2004-05-29  1:44   ` Richard Stallman
@ 2004-06-01 11:55     ` Jérôme Marant
  0 siblings, 0 replies; 4+ messages in thread
From: Jérôme Marant @ 2004-06-01 11:55 UTC (permalink / raw)
  Cc: emacs-devel

Quoting Richard Stallman <rms@gnu.org>:

> I forwarded that to rs@nunatak.allgaeu.org who maintains battery.el

Thanks.

-- 
Jérôme Marant

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-06-01 11:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-05-21 11:01 [Mario Lang] battery.el ACPI update Jérôme Marant
2004-05-27 20:26 ` Jérôme Marant
2004-05-29  1:44   ` Richard Stallman
2004-06-01 11:55     ` Jérôme Marant

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).