]> code.delx.au - gnu-emacs/blobdiff - lisp/battery.el
(ibuffer-compressed-file-name-regexp): Avoid loading
[gnu-emacs] / lisp / battery.el
index 2ae6607f8ef3c1929251392a4052c200f6c4e141..34f74aa9932d44a0901a31bb7b56410645f5ad1f 100644 (file)
@@ -1,7 +1,7 @@
 ;;; battery.el --- display battery status information
 
-;; Copyright (C) 1997, 1998, 2000, 2001, 2003, 2004, 2005
-;;           Free Software Foundation, Inc.
+;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
 ;; Keywords: hardware
              (file-directory-p "/proc/acpi/battery"))
         'battery-linux-proc-acpi)
        ((and (eq system-type 'darwin)
-             (ignore-errors 
-               (with-temp-buffer 
-                 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
-                      (> (buffer-size) 0)))))
+             (condition-case nil  
+                 (with-temp-buffer 
+                   (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
+                        (> (buffer-size) 0)))
+               (error nil)))
         'battery-pmset))
   "*Function for getting battery status information.
 The function has to return an alist of conversion definitions.
@@ -76,7 +77,18 @@ introduced by a `%' character in a control string."
 Ordinary characters in the control string are printed as-is, while
 conversion specifications introduced by a `%' character in the control
 string are substituted as defined by the current value of the variable
-`battery-status-function'."
+`battery-status-function'.  Here are the ones generally available:
+%c Current capacity (mAh or mWh)
+%r Current rate of charge or discharge
+%B Battery status (verbose)
+%b Battery status: empty means high, `-' means low,
+   `!' means critical, and `+' means charging
+%d Temperature (in degrees Celsius)
+%L AC line status (verbose)
+%p Battery load percentage
+%m Remaining time (to charge or discharge) in minutes
+%h Remaining time (to charge or discharge) in hours
+%t Remaining time (to charge or discharge) in the form `h:min'"
   :type '(choice string (const nil))
   :group 'battery)
 
@@ -95,7 +107,18 @@ string are substituted as defined by the current value of the variable
 Ordinary characters in the control string are printed as-is, while
 conversion specifications introduced by a `%' character in the control
 string are substituted as defined by the current value of the variable
-`battery-status-function'."
+`battery-status-function'.  Here are the ones generally available:
+%c Current capacity (mAh or mWh)
+%r Current rate of charge or discharge
+%B Battery status (verbose)
+%b Battery status: empty means high, `-' means low,
+   `!' means critical, and `+' means charging
+%d Temperature (in degrees Celsius)
+%L AC line status (verbose)
+%p Battery load percentage
+%m Remaining time (to charge or discharge) in minutes
+%h Remaining time (to charge or discharge) in hours
+%t Remaining time (to charge or discharge) in the form `h:min'"
   :type '(choice string (const nil))
   :group 'battery)
 
@@ -195,10 +218,10 @@ The following %-sequences are provided:
 %b Battery status, empty means high, `-' means low,
    `!' means critical, and `+' means charging
 %p Battery load percentage
-%s Remaining time in seconds
-%m Remaining time in minutes
-%h Remaining time in hours
-%t Remaining time in the form `h:min'"
+%s Remaining time (to charge or discharge) in seconds
+%m Remaining time (to charge or discharge) in minutes
+%h Remaining time (to charge or discharge) in hours
+%t Remaining time (to charge or discharge) in the form `h:min'"
   (let (driver-version bios-version bios-interface line-status
        battery-status battery-status-symbol load-percentage
        seconds minutes hours remaining-time tem)
@@ -266,10 +289,12 @@ The following %-sequences are provided:
 %d Temperature (in degrees Celsius)
 %L AC line status (verbose)
 %p Battery load percentage
-%m Remaining time in minutes
-%h Remaining time in hours
-%t Remaining time in the form `h:min'"
+%m Remaining time (to charge or discharge) in minutes
+%h Remaining time (to charge or discharge) in hours
+%t Remaining time (to charge or discharge) in the form `h:min'"
   (let ((design-capacity 0)
+       (last-full-capacity 0)
+       full-capacity
        (warn 0)
        (low 0)
        capacity rate rate-type charging-state minutes hours)
@@ -284,7 +309,7 @@ The following %-sequences are provided:
        (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
        (when (re-search-forward "present: +yes$" nil t)
          (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
-              (member charging-state '("unknown" nil))
+              (member charging-state '("unknown" "charged" nil))
               ;; On most multi-battery systems, most of the time only one
               ;; battery is "charging"/"discharging", the others are
               ;; "unknown".
@@ -309,18 +334,25 @@ The following %-sequences are provided:
          (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
                                   nil t)
            (incf design-capacity (string-to-number (match-string 1))))
+         (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
+                                  nil t)
+           (incf last-full-capacity (string-to-number (match-string 1))))
          (when (re-search-forward
                 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
            (incf warn (string-to-number (match-string 1))))
          (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
                                   nil t)
            (incf low (string-to-number (match-string 1)))))))
+    (setq full-capacity (if (> last-full-capacity 0)
+                           last-full-capacity design-capacity))
     (and capacity rate
         (setq minutes (if (zerop rate) 0
                         (floor (* (/ (float (if (string= charging-state
                                                          "charging")
-                                                (- design-capacity capacity)
-                                              capacity)) rate) 60)))
+                                                (- full-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")
@@ -346,23 +378,31 @@ The following %-sequences are provided:
                           (when (re-search-forward
                                  "temperature: +\\([0-9]+\\) C$" nil t)
                             (match-string 1))))
+                      (when (file-exists-p
+                             "/proc/acpi/thermal_zone/THM0/temperature")
+                        (with-temp-buffer
+                          (insert-file-contents
+                           "/proc/acpi/thermal_zone/THM0/temperature")
+                          (when (re-search-forward
+                                 "temperature: +\\([0-9]+\\) C$" nil t)
+                            (match-string 1))))
                       "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 (< capacity low) "!")
-                      (and (< capacity warn) "-")
+                      (and capacity (< capacity low) "!")
+                      (and capacity (< 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
                            (format "%d:%02d" hours (- minutes (* 60 hours))))
                       "N/A"))
-         (cons ?p (or (and design-capacity capacity
+         (cons ?p (or (and full-capacity capacity
                            (number-to-string
                             (floor (/ capacity
-                                      (/ (float design-capacity) 100)))))
+                                      (/ (float full-capacity) 100)))))
                       "N/A")))))
 
 \f