1 ;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1997-1998, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; There is at present support for GNU/Linux, OS X and Windows. This
26 ;; library supports both the `/proc/apm' file format of Linux version
27 ;; 1.3.58 or newer and the `/proc/acpi/' directory structure of Linux
28 ;; 2.4.20 and 2.6. Darwin (OS X) is supported by using the `pmset'
29 ;; program. Windows is supported by the GetSystemPowerStatus API call.
34 (eval-when-compile (require 'cl
))
38 "Display battery status information."
42 (defcustom battery-status-function
43 (cond ((and (eq system-type
'gnu
/linux
)
44 (file-readable-p "/proc/apm"))
45 'battery-linux-proc-apm
)
46 ((and (eq system-type
'gnu
/linux
)
47 (file-directory-p "/proc/acpi/battery"))
48 'battery-linux-proc-acpi
)
49 ((and (eq system-type
'gnu
/linux
)
50 (file-directory-p "/sys/class/power_supply/")
51 (directory-files "/sys/class/power_supply/" nil
"BAT[0-9]$"))
53 ((and (eq system-type
'darwin
)
56 (and (eq (call-process "pmset" nil t nil
"-g" "ps") 0)
60 ((eq system-type
'windows-nt
)
62 "Function for getting battery status information.
63 The function has to return an alist of conversion definitions.
64 Its cons cells are of the form
66 (CONVERSION . REPLACEMENT-TEXT)
68 CONVERSION is the character code of a \"conversion specification\"
69 introduced by a `%' character in a control string."
70 :type
'(choice (const nil
) function
)
73 (defcustom battery-echo-area-format
74 (cond ((eq battery-status-function
'battery-linux-proc-acpi
)
75 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
76 ((eq battery-status-function
'battery-linux-sysfs
)
77 "Power %L, battery %B (%p%% load)")
78 ((eq battery-status-function
'battery-pmset
)
79 "%L power, battery %B (%p%% load, remaining time %t)")
80 (battery-status-function
81 "Power %L, battery %B (%p%% load, remaining time %t)"))
82 "Control string formatting the string to display in the echo area.
83 Ordinary characters in the control string are printed as-is, while
84 conversion specifications introduced by a `%' character in the control
85 string are substituted as defined by the current value of the variable
86 `battery-status-function'. Here are the ones generally available:
87 %c Current capacity (mAh or mWh)
88 %r Current rate of charge or discharge
89 %B Battery status (verbose)
90 %b Battery status: empty means high, `-' means low,
91 `!' means critical, and `+' means charging
92 %d Temperature (in degrees Celsius)
93 %L AC line status (verbose)
94 %p Battery load percentage
95 %m Remaining time (to charge or discharge) in minutes
96 %h Remaining time (to charge or discharge) in hours
97 %t Remaining time (to charge or discharge) in the form `h:min'"
98 :type
'(choice string
(const nil
))
101 (defvar battery-mode-line-string nil
102 "String to display in the mode line.")
103 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
105 (defcustom battery-mode-line-limit
100
106 "Percentage of full battery load below which display battery status"
110 (defcustom battery-mode-line-format
111 (cond ((eq battery-status-function
'battery-linux-proc-acpi
)
113 (battery-status-function
115 "Control string formatting the string to display in the mode line.
116 Ordinary characters in the control string are printed as-is, while
117 conversion specifications introduced by a `%' character in the control
118 string are substituted as defined by the current value of the variable
119 `battery-status-function'. Here are the ones generally available:
120 %c Current capacity (mAh or mWh)
121 %r Current rate of charge or discharge
122 %B Battery status (verbose)
123 %b Battery status: empty means high, `-' means low,
124 `!' means critical, and `+' means charging
125 %d Temperature (in degrees Celsius)
126 %L AC line status (verbose)
127 %p Battery load percentage
128 %m Remaining time (to charge or discharge) in minutes
129 %h Remaining time (to charge or discharge) in hours
130 %t Remaining time (to charge or discharge) in the form `h:min'"
131 :type
'(choice string
(const nil
))
134 (defcustom battery-update-interval
60
135 "Seconds after which the battery status will be updated."
139 (defcustom battery-load-low
25
140 "Upper bound of low battery load percentage.
141 A battery load percentage below this number is considered low."
145 (defcustom battery-load-critical
10
146 "Upper bound of critical battery load percentage.
147 A battery load percentage below this number is considered critical."
151 (defvar battery-update-timer nil
152 "Interval timer object.")
156 "Display battery status information in the echo area.
157 The text being displayed in the echo area is controlled by the variables
158 `battery-echo-area-format' and `battery-status-function'."
160 (message "%s" (if (and battery-echo-area-format battery-status-function
)
161 (battery-format battery-echo-area-format
162 (funcall battery-status-function
))
163 "Battery status not available")))
166 (define-minor-mode display-battery-mode
167 "Display battery status information in the mode line.
168 The text being displayed in the mode line is controlled by the variables
169 `battery-mode-line-format' and `battery-status-function'.
170 The mode line will be updated automatically every `battery-update-interval'
172 :global t
:group
'battery
173 (setq battery-mode-line-string
"")
174 (or global-mode-string
(setq global-mode-string
'("")))
175 (and battery-update-timer
(cancel-timer battery-update-timer
))
176 (if (and battery-status-function battery-mode-line-format
)
177 (if (not display-battery-mode
)
178 (setq global-mode-string
179 (delq 'battery-mode-line-string global-mode-string
))
180 (add-to-list 'global-mode-string
'battery-mode-line-string t
)
181 (setq battery-update-timer
(run-at-time nil battery-update-interval
182 'battery-update-handler
))
184 (message "Battery status not available")
185 (setq display-battery-mode nil
)))
187 (defun battery-update-handler ()
191 (defun battery-update ()
192 "Update battery status information in the mode line."
193 (let ((data (and battery-status-function
(funcall battery-status-function
))))
194 (setq battery-mode-line-string
195 (propertize (if (and battery-mode-line-format
196 (<= (car (read-from-string (cdr (assq ?p data
))))
197 battery-mode-line-limit
))
199 battery-mode-line-format
203 (and (<= (car (read-from-string (cdr (assq ?p data
))))
204 battery-load-critical
)
206 'help-echo
"Battery status information")))
207 (force-mode-line-update))
209 ;;; `/proc/apm' interface for Linux.
211 (defconst battery-linux-proc-apm-regexp
212 (concat "^\\([^ ]+\\)" ; Driver version.
213 " \\([^ ]+\\)" ; APM BIOS version.
214 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
215 " 0x\\([0-9a-f]+\\)" ; AC line status.
216 " 0x\\([0-9a-f]+\\)" ; Battery status.
217 " 0x\\([0-9a-f]+\\)" ; Battery flags.
218 " \\(-?[0-9]+\\)%" ; Load percentage.
219 " \\(-?[0-9]+\\)" ; Remaining time.
220 " \\(.*\\)" ; Time unit.
222 "Regular expression matching contents of `/proc/apm'.")
224 (defun battery-linux-proc-apm ()
225 "Get APM status information from Linux kernel.
226 This function works only with the new `/proc/apm' format introduced
227 in Linux version 1.3.58.
229 The following %-sequences are provided:
230 %v Linux driver version
232 %I APM BIOS status (verbose)
233 %L AC line status (verbose)
234 %B Battery status (verbose)
235 %b Battery status, empty means high, `-' means low,
236 `!' means critical, and `+' means charging
237 %p Battery load percentage
238 %s Remaining time (to charge or discharge) in seconds
239 %m Remaining time (to charge or discharge) in minutes
240 %h Remaining time (to charge or discharge) in hours
241 %t Remaining time (to charge or discharge) in the form `h:min'"
242 (let (driver-version bios-version bios-interface line-status
243 battery-status battery-status-symbol load-percentage
244 seconds minutes hours remaining-time tem
)
246 (ignore-errors (insert-file-contents "/proc/apm"))
247 (when (re-search-forward battery-linux-proc-apm-regexp
)
248 (setq driver-version
(match-string 1))
249 (setq bios-version
(match-string 2))
250 (setq tem
(string-to-number (match-string 3) 16))
251 (if (not (logand tem
2))
252 (setq bios-interface
"not supported")
253 (setq bios-interface
"enabled")
254 (cond ((logand tem
16) (setq bios-interface
"disabled"))
255 ((logand tem
32) (setq bios-interface
"disengaged")))
256 (setq tem
(string-to-number (match-string 4) 16))
257 (cond ((= tem
0) (setq line-status
"off-line"))
258 ((= tem
1) (setq line-status
"on-line"))
259 ((= tem
2) (setq line-status
"on backup")))
260 (setq tem
(string-to-number (match-string 6) 16))
262 (setq battery-status
"N/A")
263 (setq tem
(string-to-number (match-string 5) 16))
264 (cond ((= tem
0) (setq battery-status
"high"
265 battery-status-symbol
""))
266 ((= tem
1) (setq battery-status
"low"
267 battery-status-symbol
"-"))
268 ((= tem
2) (setq battery-status
"critical"
269 battery-status-symbol
"!"))
270 ((= tem
3) (setq battery-status
"charging"
271 battery-status-symbol
"+")))
272 (setq load-percentage
(match-string 7))
273 (setq seconds
(string-to-number (match-string 8)))
274 (and (string-equal (match-string 9) "min")
275 (setq seconds
(* 60 seconds
)))
276 (setq minutes
(/ seconds
60)
277 hours
(/ seconds
3600))
279 (format "%d:%02d" hours
(- minutes
(* 60 hours
))))))))
280 (list (cons ?v
(or driver-version
"N/A"))
281 (cons ?V
(or bios-version
"N/A"))
282 (cons ?I
(or bios-interface
"N/A"))
283 (cons ?L
(or line-status
"N/A"))
284 (cons ?B
(or battery-status
"N/A"))
285 (cons ?b
(or battery-status-symbol
""))
286 (cons ?p
(or load-percentage
"N/A"))
287 (cons ?s
(or (and seconds
(number-to-string seconds
)) "N/A"))
288 (cons ?m
(or (and minutes
(number-to-string minutes
)) "N/A"))
289 (cons ?h
(or (and hours
(number-to-string hours
)) "N/A"))
290 (cons ?t
(or remaining-time
"N/A")))))
293 ;;; `/proc/acpi/' interface for Linux.
295 (defun battery-linux-proc-acpi ()
296 "Get ACPI status information from Linux kernel.
297 This function works only with the `/proc/acpi/' format introduced
298 in Linux version 2.4.20 and 2.6.0.
300 The following %-sequences are provided:
301 %c Current capacity (mAh)
303 %B Battery status (verbose)
304 %b Battery status, empty means high, `-' means low,
305 `!' means critical, and `+' means charging
306 %d Temperature (in degrees Celsius)
307 %L AC line status (verbose)
308 %p Battery load percentage
309 %m Remaining time (to charge or discharge) in minutes
310 %h Remaining time (to charge or discharge) in hours
311 %t Remaining time (to charge or discharge) in the form `h:min'"
312 (let ((design-capacity 0)
313 (last-full-capacity 0)
317 capacity rate rate-type charging-state minutes hours
)
318 ;; ACPI provides information about each battery present in the system in
319 ;; a separate subdirectory. We are going to merge the available
320 ;; information together since displaying for a variable amount of
321 ;; batteries seems overkill for format-strings.
323 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
326 (ignore-errors (insert-file-contents (expand-file-name "state" dir
)))
327 (when (re-search-forward "present: +yes$" nil t
)
328 (and (re-search-forward "charging state: +\\(.*\\)$" nil t
)
329 (member charging-state
'("unknown" "charged" nil
))
330 ;; On most multi-battery systems, most of the time only one
331 ;; battery is "charging"/"discharging", the others are
333 (setq charging-state
(match-string 1)))
334 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
336 (setq rate
(+ (or rate
0) (string-to-number (match-string 1)))
337 rate-type
(or (and rate-type
338 (if (string= rate-type
(match-string 2))
341 "Inconsistent rate types (%s vs. %s)"
342 rate-type
(match-string 2))))
344 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
347 (+ (or capacity
0) (string-to-number (match-string 1))))))
348 (goto-char (point-max))
349 (ignore-errors (insert-file-contents (expand-file-name "info" dir
)))
350 (when (re-search-forward "present: +yes$" nil t
)
351 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
353 (incf design-capacity
(string-to-number (match-string 1))))
354 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
356 (incf last-full-capacity
(string-to-number (match-string 1))))
357 (when (re-search-forward
358 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t
)
359 (incf warn
(string-to-number (match-string 1))))
360 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
362 (incf low
(string-to-number (match-string 1)))))))
363 (setq full-capacity
(if (> last-full-capacity
0)
364 last-full-capacity design-capacity
))
366 (setq minutes
(if (zerop rate
) 0
367 (floor (* (/ (float (if (string= charging-state
369 (- full-capacity capacity
)
373 hours
(/ minutes
60)))
374 (list (cons ?c
(or (and capacity
(number-to-string capacity
)) "N/A"))
375 (cons ?L
(or (battery-search-for-one-match-in-files
376 (mapcar (lambda (e) (concat e
"/state"))
378 (directory-files "/proc/acpi/ac_adapter/"
380 "state: +\\(.*\\)$" 1)
383 (cons ?d
(or (battery-search-for-one-match-in-files
384 (mapcar (lambda (e) (concat e
"/temperature"))
386 (directory-files "/proc/acpi/thermal_zone/"
388 "temperature: +\\([0-9]+\\) C$" 1)
391 (cons ?r
(or (and rate
(concat (number-to-string rate
) " "
393 (cons ?B
(or charging-state
"N/A"))
394 (cons ?b
(or (and (string= charging-state
"charging") "+")
395 (and capacity
(< capacity low
) "!")
396 (and capacity
(< capacity warn
) "-")
398 (cons ?h
(or (and hours
(number-to-string hours
)) "N/A"))
399 (cons ?m
(or (and minutes
(number-to-string minutes
)) "N/A"))
400 (cons ?t
(or (and minutes
401 (format "%d:%02d" hours
(- minutes
(* 60 hours
))))
403 (cons ?p
(or (and full-capacity capacity
407 (/ (float full-capacity
) 100)))))
411 ;;; `/sys/class/power_supply/BATN' interface for Linux.
413 (defun battery-linux-sysfs ()
414 "Get ACPI status information from Linux kernel.
415 This function works only with the new `/sys/class/power_supply/'
416 format introduced in Linux version 2.4.25.
418 The following %-sequences are provided:
419 %c Current capacity (mAh or mWh)
420 %B Battery status (verbose)
421 %p Battery load percentage
422 %L AC line status (verbose)"
428 ;; SysFS provides information about each battery present in the
429 ;; system in a separate subdirectory. We are going to merge the
430 ;; available information together.
432 (dolist (dir (ignore-errors
434 "/sys/class/power_supply/" t
"BAT[0-9]$")))
436 (ignore-errors (insert-file-contents
437 (expand-file-name "uevent" dir
)))
438 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t
)
439 (goto-char (point-min))
440 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t
)
441 (member charging-state
'("Unknown" "Full" nil
))
442 (setq charging-state
(match-string 1)))
443 (let (full-string now-string
)
444 ;; Sysfs may list either charge (mAh) or energy (mWh).
445 ;; Keep track of both, and choose which to report later.
446 (cond ((and (re-search-forward
447 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t
)
448 (setq full-string
(match-string 1))
450 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t
)
451 (setq now-string
(match-string 1)))
452 (setq charge-full
(+ charge-full
453 (string-to-number full-string
))
454 charge-now
(+ charge-now
455 (string-to-number now-string
))))
456 ((and (re-search-forward
457 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t
)
458 (setq full-string
(match-string 1))
460 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t
)
461 (setq now-string
(match-string 1)))
462 (setq energy-full
(+ energy-full
463 (string-to-number full-string
))
464 energy-now
(+ energy-now
465 (string-to-number now-string
)))))))))
466 (list (cons ?c
(cond ((or (> charge-full
0) (> charge-now
0))
467 (number-to-string charge-now
))
468 ((or (> energy-full
0) (> energy-now
0))
469 (number-to-string energy-now
))
471 (cons ?B
(or charging-state
"N/A"))
472 (cons ?p
(cond ((> charge-full
0)
474 (/ (* 100 charge-now
) charge-full
)))
477 (/ (* 100 energy-now
) energy-full
)))
479 (cons ?L
(if (file-readable-p "/sys/class/power_supply/AC/online")
480 (if (battery-search-for-one-match-in-files
481 (list "/sys/class/power_supply/AC/online"
482 "/sys/class/power_supply/ACAD/online")
490 ;;; `pmset' interface for Darwin (OS X).
492 (defun battery-pmset ()
493 "Get battery status information using `pmset'.
495 The following %-sequences are provided:
496 %L Power source (verbose)
497 %B Battery status (verbose)
498 %b Battery status, empty means high, `-' means low,
499 `!' means critical, and `+' means charging
500 %p Battery load percentage
501 %h Remaining time in hours
502 %m Remaining time in minutes
503 %t Remaining time in the form `h:min'"
504 (let (power-source load-percentage battery-status battery-status-symbol
505 remaining-time hours minutes
)
507 (ignore-errors (call-process "pmset" nil t nil
"-g" "ps"))
508 (goto-char (point-min))
509 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t
)
510 (setq power-source
(match-string 1))
511 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t
)
512 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
513 (setq load-percentage
(match-string 1))
514 (goto-char (match-end 0))
515 (cond ((looking-at "; charging")
516 (setq battery-status
"charging"
517 battery-status-symbol
"+"))
518 ((< (string-to-number load-percentage
) battery-load-low
)
519 (setq battery-status
"low"
520 battery-status-symbol
"-"))
521 ((< (string-to-number load-percentage
) battery-load-critical
)
522 (setq battery-status
"critical"
523 battery-status-symbol
"!"))
525 (setq battery-status
"high"
526 battery-status-symbol
"")))
527 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t
)
528 (setq remaining-time
(match-string 1))
529 (let ((h (string-to-number (match-string 2)))
530 (m (string-to-number (match-string 3))))
531 (setq hours
(number-to-string (+ h
(if (< m
30) 0 1)))
532 minutes
(number-to-string (+ (* h
60) m
)))))))))
533 (list (cons ?L
(or power-source
"N/A"))
534 (cons ?p
(or load-percentage
"N/A"))
535 (cons ?B
(or battery-status
"N/A"))
536 (cons ?b
(or battery-status-symbol
""))
537 (cons ?h
(or hours
"N/A"))
538 (cons ?m
(or minutes
"N/A"))
539 (cons ?t
(or remaining-time
"N/A")))))
542 ;;; Private functions.
544 (defun battery-format (format alist
)
545 "Substitute %-sequences in FORMAT."
546 (replace-regexp-in-string
549 (let ((char (aref str
1)))
551 (or (cdr (assoc char alist
)) ""))))
554 (defun battery-search-for-one-match-in-files (files regexp match-num
)
555 "Search REGEXP in the content of the files listed in FILES.
556 If a match occurred, return the parenthesized expression numbered by
557 MATCH-NUM in the match. Otherwise, return nil."
561 (and (ignore-errors (insert-file-contents file nil nil nil
'replace
))
562 (re-search-forward regexp nil t
)
563 (throw 'found
(match-string match-num
)))))))
568 ;;; battery.el ends here