/[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.27 by lute, Mon Jul 4 23:08:52 2005 UTC revision 1.28 by lute, Tue Jul 5 08:13:41 2005 UTC
# Line 25  Line 25 
25    
26  ;;; Commentary:  ;;; Commentary:
27    
28  ;; There is at present support for interpreting the new `/proc/apm'  ;; There is at present support for GNU/Linux and OS X.  This library
29  ;; file format of Linux version 1.3.58 or newer and for the `/proc/acpi/'  ;; supports both the `/proc/apm' file format of Linux version 1.3.58
30  ;; directory structure of Linux 2.4.20 and 2.6.  ;; or newer and the `/proc/acpi/' directory structure of Linux 2.4.20
31    ;; and 2.6.  Darwin (OS X) is supported by using the `pmset' program.
32    
33  ;;; Code:  ;;; Code:
34    
# Line 46  Line 47 
47           'battery-linux-proc-apm)           'battery-linux-proc-apm)
48          ((and (eq system-type 'gnu/linux)          ((and (eq system-type 'gnu/linux)
49                (file-directory-p "/proc/acpi/battery"))                (file-directory-p "/proc/acpi/battery"))
50           'battery-linux-proc-acpi))           'battery-linux-proc-acpi)
51            ((and (eq system-type 'darwin)
52                  (ignore-errors
53                    (with-temp-buffer
54                      (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
55                           (> (buffer-size) 0)))))
56             'battery-pmset))
57    "*Function for getting battery status information.    "*Function for getting battery status information.
58  The function has to return an alist of conversion definitions.  The function has to return an alist of conversion definitions.
59  Its cons cells are of the form  Its cons cells are of the form
# Line 62  introduced by a `%' character in a contr Line 69  introduced by a `%' character in a contr
69    (cond ((eq battery-status-function 'battery-linux-proc-apm)    (cond ((eq battery-status-function 'battery-linux-proc-apm)
70           "Power %L, battery %B (%p%% load, remaining time %t)")           "Power %L, battery %B (%p%% load, remaining time %t)")
71          ((eq battery-status-function 'battery-linux-proc-acpi)          ((eq battery-status-function 'battery-linux-proc-acpi)
72           "Power %L, battery %B at %r (%p%% load, remaining time %t)"))           "Power %L, battery %B at %r (%p%% load, remaining time %t)")
73            ((eq battery-status-function 'battery-pmset)
74             "%L power, battery %B (%p%% load, remaining time %t)"))
75    "*Control string formatting the string to display in the echo area.    "*Control string formatting the string to display in the echo area.
76  Ordinary characters in the control string are printed as-is, while  Ordinary characters in the control string are printed as-is, while
77  conversion specifications introduced by a `%' character in the control  conversion specifications introduced by a `%' character in the control
# Line 79  string are substituted as defined by the Line 88  string are substituted as defined by the
88    (cond ((eq battery-status-function 'battery-linux-proc-apm)    (cond ((eq battery-status-function 'battery-linux-proc-apm)
89           "[%b%p%%]")           "[%b%p%%]")
90          ((eq battery-status-function 'battery-linux-proc-acpi)          ((eq battery-status-function 'battery-linux-proc-acpi)
91           "[%b%p%%,%d°C]"))           "[%b%p%%,%d°C]")
92            ((eq battery-status-function 'battery-pmset)
93             "[%b%p%%]"))
94    "*Control string formatting the string to display in the mode line.    "*Control string formatting the string to display in the mode line.
95  Ordinary characters in the control string are printed as-is, while  Ordinary characters in the control string are printed as-is, while
96  conversion specifications introduced by a `%' character in the control  conversion specifications introduced by a `%' character in the control
# Line 93  string are substituted as defined by the Line 104  string are substituted as defined by the
104    :type 'integer    :type 'integer
105    :group 'battery)    :group 'battery)
106    
107    (defcustom battery-load-low 25
108      "*Upper bound of low battery load percentage.
109    A battery load percentage below this number is considered low."
110      :type 'integer
111      :group 'battery)
112    
113    (defcustom battery-load-critical 10
114      "*Upper bound of critical battery load percentage.
115    A battery load percentage below this number is considered critical."
116      :type 'integer
117      :group 'battery)
118    
119  (defvar battery-update-timer nil  (defvar battery-update-timer nil
120    "Interval timer object.")    "Interval timer object.")
121    
# Line 343  The following %-sequences are provided: Line 366  The following %-sequences are provided:
366                         "N/A")))))                         "N/A")))))
367    
368    
369    ;;; `pmset' interface for Darwin (OS X).
370    
371    (defun battery-pmset ()
372      "Get battery status information using `pmset'.
373    
374    The following %-sequences are provided:
375    %L Power source (verbose)
376    %B Battery status (verbose)
377    %b Battery status, empty means high, `-' means low,
378       `!' means critical, and `+' means charging
379    %p Battery load percentage
380    %h Remaining time in hours
381    %m Remaining time in minutes
382    %t Remaining time in the form `h:min'"
383      (let (power-source load-percentage battery-status battery-status-symbol
384            remaining-time hours minutes)
385        (with-temp-buffer
386          (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
387          (goto-char (point-min))
388          (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
389            (setq power-source (match-string 1))
390            (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
391              (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
392                (setq load-percentage (match-string 1))
393                (goto-char (match-end 0))
394                (cond ((looking-at "; charging")
395                       (setq battery-status "charging"
396                             battery-status-symbol "+"))
397                      ((< (string-to-number load-percentage) battery-load-low)
398                       (setq battery-status "low"
399                             battery-status-symbol "-"))
400                      ((< (string-to-number load-percentage) battery-load-critical)
401                       (setq battery-status "critical"
402                             battery-status-symbol "!"))
403                      (t
404                       (setq battery-status "high"
405                             battery-status-symbol "")))
406                (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining"  nil t)
407                  (setq remaining-time (match-string 1))
408                  (let ((h (string-to-number (match-string 2)))
409                        (m (string-to-number (match-string 3))))
410                    (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
411                          minutes (number-to-string (+ (* h 60) m)))))))))
412        (list (cons ?L (or power-source "N/A"))
413              (cons ?p (or load-percentage "N/A"))
414              (cons ?B (or battery-status "N/A"))
415              (cons ?b (or battery-status-symbol ""))
416              (cons ?h (or hours "N/A"))
417              (cons ?m (or minutes "N/A"))
418              (cons ?t (or remaining-time "N/A")))))
419    
420    
421  ;;; Private functions.  ;;; Private functions.
422    
423  (defun battery-format (format alist)  (defun battery-format (format alist)

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.28

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