/[emacs]/emacs/lisp/battery.el
ViewVC logotype

Diff of /emacs/lisp/battery.el

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.15 by friedman, Mon Dec 10 10:22:02 2001 UTC revision 1.15.4.1 by miles, Tue Oct 14 23:50:49 2003 UTC
# Line 1  Line 1 
1  ;;; battery.el --- display battery status information  ;;; battery.el --- display battery status information
2    
3  ;; Copyright (C) 1997, 1998, 2000, 2001 Free Software Foundation, Inc.  ;; Copyright (C) 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
4    
5  ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>  ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
6  ;; Keywords: hardware  ;; Keywords: hardware
# Line 24  Line 24 
24    
25  ;;; Commentary:  ;;; Commentary:
26    
27  ;; There is at present only a function interpreting the new `/proc/apm'  ;; There is at present support for interpreting the new `/proc/apm'
28  ;; file format of Linux version 1.3.58 or newer.  That is, what a lucky  ;; file format of Linux version 1.3.58 or newer and for the `/proc/acpi/'
29  ;; coincidence, exactly the interface provided by the author's laptop.  ;; directory structure of Linux 2.4.20 and 2.6.
30    
31  ;;; Code:  ;;; Code:
32    
# Line 42  Line 42 
42  (defcustom battery-status-function  (defcustom battery-status-function
43    (cond ((and (eq system-type 'gnu/linux)    (cond ((and (eq system-type 'gnu/linux)
44                (file-readable-p "/proc/apm"))                (file-readable-p "/proc/apm"))
45           'battery-linux-proc-apm))           'battery-linux-proc-apm)
46            ((and (eq system-type 'gnu/linux)
47                  (file-directory-p "/proc/acpi/battery"))
48             'battery-linux-proc-acpi))
49    "*Function for getting battery status information.    "*Function for getting battery status information.
50  The function has to return an alist of conversion definitions.  The function has to return an alist of conversion definitions.
51  Its cons cells are of the form  Its cons cells are of the form
# Line 56  introduced by a `%' character in a contr Line 59  introduced by a `%' character in a contr
59    
60  (defcustom battery-echo-area-format  (defcustom battery-echo-area-format
61    (cond ((eq battery-status-function 'battery-linux-proc-apm)    (cond ((eq battery-status-function 'battery-linux-proc-apm)
62           "Power %L, battery %B (%p%% load, remaining time %t)"))           "Power %L, battery %B (%p%% load, remaining time %t)")
63            ((eq battery-status-function 'battery-linux-proc-acpi)
64             "Power %L, battery %B at %r mA (%p%% load, remaining time %t)"))
65    "*Control string formatting the string to display in the echo area.    "*Control string formatting the string to display in the echo area.
66  Ordinary characters in the control string are printed as-is, while  Ordinary characters in the control string are printed as-is, while
67  conversion specifications introduced by a `%' character in the control  conversion specifications introduced by a `%' character in the control
# Line 70  string are substituted as defined by the Line 75  string are substituted as defined by the
75    
76  (defcustom battery-mode-line-format  (defcustom battery-mode-line-format
77    (cond ((eq battery-status-function 'battery-linux-proc-apm)    (cond ((eq battery-status-function 'battery-linux-proc-apm)
78           " [%b%p%%]"))           " [%b%p%%]")
79            ((eq battery-status-function 'battery-linux-proc-acpi)
80             " [%b%p%%,%d°C]"))
81    "*Control string formatting the string to display in the mode line.    "*Control string formatting the string to display in the mode line.
82  Ordinary characters in the control string are printed as-is, while  Ordinary characters in the control string are printed as-is, while
83  conversion specifications introduced by a `%' character in the control  conversion specifications introduced by a `%' character in the control
# Line 218  The following %-sequences are provided: Line 225  The following %-sequences are provided:
225            (cons ?t (or remaining-time "N/A")))))            (cons ?t (or remaining-time "N/A")))))
226    
227    
228    ;;; `/proc/acpi/' interface for Linux.
229    
230    (defun battery-linux-proc-acpi ()
231      "Get ACPI status information from Linux kernel.
232    This function works only with the new `/proc/acpi/' format introduced
233    in Linux version 2.4.20 and 2.6.0.
234    
235    The following %-sequences are provided:
236    %c Current capacity (mAh)
237    %B Battery status (verbose)
238    %b Battery status, empty means high, `-' means low,
239       `!' means critical, and `+' means charging
240    %d Temperature (in degrees Celsius)
241    %L AC line status (verbose)
242    %p battery load percentage
243    %m Remaining time in minutes
244    %h Remaining time in hours
245    %t Remaining time in the form `h:min'"
246      (let (capacity design-capacity rate charging-state warn low minutes hours)
247        (when (file-directory-p "/proc/acpi/battery/")
248          ;; ACPI provides information about each battery present in the system in
249          ;; a separate subdirectory.  We are going to merge the available
250          ;; information together since displaying for a variable amount of
251          ;; batteries seems overkill for format-strings.
252          (mapc
253           (lambda (dir)
254             (with-temp-buffer
255               (insert-file-contents (expand-file-name "state" dir))
256               (when (re-search-forward "present: +yes$" nil t)
257                 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
258                      (or (null charging-state) (string= charging-state
259                                                         "unknown"))
260                      ;; On most multi-battery systems, most of the time only one
261                      ;; battery is "charging"/"discharging", the others are
262                      ;; "unknown".
263                      (setq charging-state (match-string 1)))
264                 (when (re-search-forward "present rate: +\\([0-9]+\\) mA$" nil t)
265                   (setq rate (+ (or rate 0) (string-to-int (match-string 1)))))
266                 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) mAh$"
267                                          nil t)
268                   (setq capacity
269                         (+ (or capacity 0) (string-to-int (match-string 1))))))
270               (goto-char (point-max))
271               (insert-file-contents (expand-file-name "info" dir))
272               (when (re-search-forward "present: +yes$" nil t)
273                 (when (re-search-forward "design capacity: +\\([0-9]+\\) mAh$"
274                                          nil t)
275                   (setq design-capacity (+ (or design-capacity 0)
276                                            (string-to-int (match-string 1)))))
277                 (when (re-search-forward "design capacity warning: +\\([0-9]+\\) mAh$"
278                                          nil t)
279                   (setq warn (+ (or warn 0) (string-to-int (match-string 1)))))
280                 (when (re-search-forward "design capacity low: +\\([0-9]+\\) mAh$"
281                                          nil t)
282                   (setq low (+ (or low 0)
283                                (string-to-int (match-string 1))))))))
284           (directory-files "/proc/acpi/battery/" t "BAT")))
285        (and capacity rate
286             (setq minutes (floor (* (/ (float (if (string= charging-state
287                                                            "charging")
288                                                   (- design-capacity capacity)
289                                                 capacity)) rate) 60))
290                   hours (/ minutes 60)))
291        (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
292              (cons ?L (or (when (file-exists-p "/proc/acpi/ac_adapter/AC/state")
293                             (with-temp-buffer
294                               (insert-file-contents
295                                "/proc/acpi/ac_adapter/AC/state")
296                               (when (re-search-forward "state: +\\(.*\\)$" nil t)
297                                 (match-string 1))))
298                           "N/A"))
299              (cons ?d (or (when (file-exists-p
300                                  "/proc/acpi/thermal_zone/THRM/temperature")
301                             (with-temp-buffer
302                               (insert-file-contents
303                                "/proc/acpi/thermal_zone/THRM/temperature")
304                               (when (re-search-forward
305                                      "temperature: +\\([0-9]+\\) C$" nil t)
306                                 (match-string 1))))
307                           "N/A"))
308              (cons ?r (or (and rate (number-to-string rate)) "N/A"))
309              (cons ?B (or charging-state "N/A"))
310              (cons ?b (or (and (string= charging-state "charging") "+")
311                           (and low (< capacity low) "!")
312                           (and warn (< capacity warn) "-")
313                           ""))
314              (cons ?h (or (and hours (number-to-string hours)) "N/A"))
315              (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
316              (cons ?t (or (and minutes
317                                (format "%d:%02d" hours (- minutes (* 60 hours))))
318                           "N/A"))
319              (cons ?p (or (and design-capacity capacity
320                                (number-to-string
321                                 (floor (/ capacity
322                                           (/ (float design-capacity) 100)))))
323                           "N/A")))))
324    
325    
326  ;;; Private functions.  ;;; Private functions.
327    
328  (defun battery-format (format alist)  (defun battery-format (format alist)
# Line 271  The following %-sequences are provided: Line 376  The following %-sequences are provided:
376    
377  (provide 'battery)  (provide 'battery)
378    
379    ;;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
380  ;;; battery.el ends here  ;;; battery.el ends here

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.15.4.1

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26