1 ;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; There is at present support for GNU/Linux, OS X and Windows. This
27 ;; library supports both the `/proc/apm' file format of Linux version
28 ;; 1.3.58 or newer and the `/proc/acpi/' directory structure of Linux
29 ;; 2.4.20 and 2.6. Darwin (OS X) is supported by using the `pmset'
30 ;; program. Windows is supported by the GetSystemPowerStatus API call.
35 (eval-when-compile (require 'cl
))
39 "Display battery status information."
43 (defcustom battery-status-function
44 (cond ((and (eq system-type
'gnu
/linux
)
45 (file-readable-p "/proc/apm"))
46 'battery-linux-proc-apm
)
47 ((and (eq system-type
'gnu
/linux
)
48 (file-directory-p "/proc/acpi/battery"))
49 'battery-linux-proc-acpi
)
50 ((and (eq system-type
'gnu
/linux
)
51 (file-directory-p "/sys/class/power_supply/")
52 (directory-files "/sys/class/power_supply/" nil
"BAT[0-9]$"))
54 ((and (eq system-type
'darwin
)
57 (and (eq (call-process "pmset" nil t nil
"-g" "ps") 0)
61 ((eq system-type
'windows-nt
)
63 "Function for getting battery status information.
64 The function has to return an alist of conversion definitions.
65 Its cons cells are of the form
67 (CONVERSION . REPLACEMENT-TEXT)
69 CONVERSION is the character code of a \"conversion specification\"
70 introduced by a `%' character in a control string."
71 :type
'(choice (const nil
) function
)
74 (defcustom battery-echo-area-format
75 (cond ((eq battery-status-function
'battery-linux-proc-acpi
)
76 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
77 ((eq battery-status-function
'battery-linux-sysfs
)
78 "Power %L, battery %B (%p%% load)")
79 ((eq battery-status-function
'battery-pmset
)
80 "%L power, battery %B (%p%% load, remaining time %t)")
81 (battery-status-function
82 "Power %L, battery %B (%p%% load, remaining time %t)"))
83 "Control string formatting the string to display in the echo area.
84 Ordinary characters in the control string are printed as-is, while
85 conversion specifications introduced by a `%' character in the control
86 string are substituted as defined by the current value of the variable
87 `battery-status-function'. Here are the ones generally available:
88 %c Current capacity (mAh or mWh)
89 %r Current rate of charge or discharge
90 %B Battery status (verbose)
91 %b Battery status: empty means high, `-' means low,
92 `!' means critical, and `+' means charging
93 %d Temperature (in degrees Celsius)
94 %L AC line status (verbose)
95 %p Battery load percentage
96 %m Remaining time (to charge or discharge) in minutes
97 %h Remaining time (to charge or discharge) in hours
98 %t Remaining time (to charge or discharge) in the form `h:min'"
99 :type
'(choice string
(const nil
))
102 (defvar battery-mode-line-string nil
103 "String to display in the mode line.")
104 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
106 (defcustom battery-mode-line-format
107 (cond ((eq battery-status-function
'battery-linux-proc-acpi
)
109 (battery-status-function
111 "Control string formatting the string to display in the mode line.
112 Ordinary characters in the control string are printed as-is, while
113 conversion specifications introduced by a `%' character in the control
114 string are substituted as defined by the current value of the variable
115 `battery-status-function'. Here are the ones generally available:
116 %c Current capacity (mAh or mWh)
117 %r Current rate of charge or discharge
118 %B Battery status (verbose)
119 %b Battery status: empty means high, `-' means low,
120 `!' means critical, and `+' means charging
121 %d Temperature (in degrees Celsius)
122 %L AC line status (verbose)
123 %p Battery load percentage
124 %m Remaining time (to charge or discharge) in minutes
125 %h Remaining time (to charge or discharge) in hours
126 %t Remaining time (to charge or discharge) in the form `h:min'"
127 :type
'(choice string
(const nil
))
130 (defcustom battery-update-interval
60
131 "Seconds after which the battery status will be updated."
135 (defcustom battery-load-low
25
136 "Upper bound of low battery load percentage.
137 A battery load percentage below this number is considered low."
141 (defcustom battery-load-critical
10
142 "Upper bound of critical battery load percentage.
143 A battery load percentage below this number is considered critical."
147 (defvar battery-update-timer nil
148 "Interval timer object.")
152 "Display battery status information in the echo area.
153 The text being displayed in the echo area is controlled by the variables
154 `battery-echo-area-format' and `battery-status-function'."
156 (message "%s" (if (and battery-echo-area-format battery-status-function
)
157 (battery-format battery-echo-area-format
158 (funcall battery-status-function
))
159 "Battery status not available")))
162 (define-minor-mode display-battery-mode
163 "Display battery status information in the mode line.
164 The text being displayed in the mode line is controlled by the variables
165 `battery-mode-line-format' and `battery-status-function'.
166 The mode line will be updated automatically every `battery-update-interval'
168 :global t
:group
'battery
169 (setq battery-mode-line-string
"")
170 (or global-mode-string
(setq global-mode-string
'("")))
171 (and battery-update-timer
(cancel-timer battery-update-timer
))
172 (if (not display-battery-mode
)
173 (setq global-mode-string
174 (delq 'battery-mode-line-string global-mode-string
))
175 (add-to-list 'global-mode-string
'battery-mode-line-string t
)
176 (setq battery-update-timer
(run-at-time nil battery-update-interval
177 'battery-update-handler
))
180 (defun battery-update-handler ()
184 (defun battery-update ()
185 "Update battery status information in the mode line."
186 (setq battery-mode-line-string
187 (propertize (if (and battery-mode-line-format
188 battery-status-function
)
190 battery-mode-line-format
191 (funcall battery-status-function
))
193 'help-echo
"Battery status information"))
194 (force-mode-line-update))
197 ;;; `/proc/apm' interface for Linux.
199 (defconst battery-linux-proc-apm-regexp
200 (concat "^\\([^ ]+\\)" ; Driver version.
201 " \\([^ ]+\\)" ; APM BIOS version.
202 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
203 " 0x\\([0-9a-f]+\\)" ; AC line status.
204 " 0x\\([0-9a-f]+\\)" ; Battery status.
205 " 0x\\([0-9a-f]+\\)" ; Battery flags.
206 " \\(-?[0-9]+\\)%" ; Load percentage.
207 " \\(-?[0-9]+\\)" ; Remaining time.
208 " \\(.*\\)" ; Time unit.
210 "Regular expression matching contents of `/proc/apm'.")
212 (defun battery-linux-proc-apm ()
213 "Get APM status information from Linux kernel.
214 This function works only with the new `/proc/apm' format introduced
215 in Linux version 1.3.58.
217 The following %-sequences are provided:
218 %v Linux driver version
220 %I APM BIOS status (verbose)
221 %L AC line status (verbose)
222 %B Battery status (verbose)
223 %b Battery status, empty means high, `-' means low,
224 `!' means critical, and `+' means charging
225 %p Battery load percentage
226 %s Remaining time (to charge or discharge) in seconds
227 %m Remaining time (to charge or discharge) in minutes
228 %h Remaining time (to charge or discharge) in hours
229 %t Remaining time (to charge or discharge) in the form `h:min'"
230 (let (driver-version bios-version bios-interface line-status
231 battery-status battery-status-symbol load-percentage
232 seconds minutes hours remaining-time tem
)
234 (ignore-errors (insert-file-contents "/proc/apm"))
235 (when (re-search-forward battery-linux-proc-apm-regexp
)
236 (setq driver-version
(match-string 1))
237 (setq bios-version
(match-string 2))
238 (setq tem
(string-to-number (match-string 3) 16))
239 (if (not (logand tem
2))
240 (setq bios-interface
"not supported")
241 (setq bios-interface
"enabled")
242 (cond ((logand tem
16) (setq bios-interface
"disabled"))
243 ((logand tem
32) (setq bios-interface
"disengaged")))
244 (setq tem
(string-to-number (match-string 4) 16))
245 (cond ((= tem
0) (setq line-status
"off-line"))
246 ((= tem
1) (setq line-status
"on-line"))
247 ((= tem
2) (setq line-status
"on backup")))
248 (setq tem
(string-to-number (match-string 6) 16))
250 (setq battery-status
"N/A")
251 (setq tem
(string-to-number (match-string 5) 16))
252 (cond ((= tem
0) (setq battery-status
"high"
253 battery-status-symbol
""))
254 ((= tem
1) (setq battery-status
"low"
255 battery-status-symbol
"-"))
256 ((= tem
2) (setq battery-status
"critical"
257 battery-status-symbol
"!"))
258 ((= tem
3) (setq battery-status
"charging"
259 battery-status-symbol
"+")))
260 (setq load-percentage
(match-string 7))
261 (setq seconds
(string-to-number (match-string 8)))
262 (and (string-equal (match-string 9) "min")
263 (setq seconds
(* 60 seconds
)))
264 (setq minutes
(/ seconds
60)
265 hours
(/ seconds
3600))
267 (format "%d:%02d" hours
(- minutes
(* 60 hours
))))))))
268 (list (cons ?v
(or driver-version
"N/A"))
269 (cons ?V
(or bios-version
"N/A"))
270 (cons ?I
(or bios-interface
"N/A"))
271 (cons ?L
(or line-status
"N/A"))
272 (cons ?B
(or battery-status
"N/A"))
273 (cons ?b
(or battery-status-symbol
""))
274 (cons ?p
(or load-percentage
"N/A"))
275 (cons ?s
(or (and seconds
(number-to-string seconds
)) "N/A"))
276 (cons ?m
(or (and minutes
(number-to-string minutes
)) "N/A"))
277 (cons ?h
(or (and hours
(number-to-string hours
)) "N/A"))
278 (cons ?t
(or remaining-time
"N/A")))))
281 ;;; `/proc/acpi/' interface for Linux.
283 (defun battery-linux-proc-acpi ()
284 "Get ACPI status information from Linux kernel.
285 This function works only with the `/proc/acpi/' format introduced
286 in Linux version 2.4.20 and 2.6.0.
288 The following %-sequences are provided:
289 %c Current capacity (mAh)
291 %B Battery status (verbose)
292 %b Battery status, empty means high, `-' means low,
293 `!' means critical, and `+' means charging
294 %d Temperature (in degrees Celsius)
295 %L AC line status (verbose)
296 %p Battery load percentage
297 %m Remaining time (to charge or discharge) in minutes
298 %h Remaining time (to charge or discharge) in hours
299 %t Remaining time (to charge or discharge) in the form `h:min'"
300 (let ((design-capacity 0)
301 (last-full-capacity 0)
305 capacity rate rate-type charging-state minutes hours
)
306 ;; ACPI provides information about each battery present in the system in
307 ;; a separate subdirectory. We are going to merge the available
308 ;; information together since displaying for a variable amount of
309 ;; batteries seems overkill for format-strings.
311 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
314 (ignore-errors (insert-file-contents (expand-file-name "state" dir
)))
315 (when (re-search-forward "present: +yes$" nil t
)
316 (and (re-search-forward "charging state: +\\(.*\\)$" nil t
)
317 (member charging-state
'("unknown" "charged" nil
))
318 ;; On most multi-battery systems, most of the time only one
319 ;; battery is "charging"/"discharging", the others are
321 (setq charging-state
(match-string 1)))
322 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
324 (setq rate
(+ (or rate
0) (string-to-number (match-string 1)))
325 rate-type
(or (and rate-type
326 (if (string= rate-type
(match-string 2))
329 "Inconsistent rate types (%s vs. %s)"
330 rate-type
(match-string 2))))
332 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
335 (+ (or capacity
0) (string-to-number (match-string 1))))))
336 (goto-char (point-max))
337 (ignore-errors (insert-file-contents (expand-file-name "info" dir
)))
338 (when (re-search-forward "present: +yes$" nil t
)
339 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
341 (incf design-capacity
(string-to-number (match-string 1))))
342 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
344 (incf last-full-capacity
(string-to-number (match-string 1))))
345 (when (re-search-forward
346 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t
)
347 (incf warn
(string-to-number (match-string 1))))
348 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
350 (incf low
(string-to-number (match-string 1)))))))
351 (setq full-capacity
(if (> last-full-capacity
0)
352 last-full-capacity design-capacity
))
354 (setq minutes
(if (zerop rate
) 0
355 (floor (* (/ (float (if (string= charging-state
357 (- full-capacity capacity
)
361 hours
(/ minutes
60)))
362 (list (cons ?c
(or (and capacity
(number-to-string capacity
)) "N/A"))
363 (cons ?L
(or (battery-search-for-one-match-in-files
364 (mapcar (lambda (e) (concat e
"/state"))
366 (directory-files "/proc/acpi/ac_adapter/"
368 "state: +\\(.*\\)$" 1)
371 (cons ?d
(or (battery-search-for-one-match-in-files
372 (mapcar (lambda (e) (concat e
"/temperature"))
374 (directory-files "/proc/acpi/thermal_zone/"
376 "temperature: +\\([0-9]+\\) C$" 1)
379 (cons ?r
(or (and rate
(concat (number-to-string rate
) " "
381 (cons ?B
(or charging-state
"N/A"))
382 (cons ?b
(or (and (string= charging-state
"charging") "+")
383 (and capacity
(< capacity low
) "!")
384 (and capacity
(< capacity warn
) "-")
386 (cons ?h
(or (and hours
(number-to-string hours
)) "N/A"))
387 (cons ?m
(or (and minutes
(number-to-string minutes
)) "N/A"))
388 (cons ?t
(or (and minutes
389 (format "%d:%02d" hours
(- minutes
(* 60 hours
))))
391 (cons ?p
(or (and full-capacity capacity
395 (/ (float full-capacity
) 100)))))
399 ;;; `/sys/class/power_supply/BATN' interface for Linux.
401 (defun battery-linux-sysfs ()
402 "Get ACPI status information from Linux kernel.
403 This function works only with the new `/sys/class/power_supply/'
404 format introduced in Linux version 2.4.25.
406 The following %-sequences are provided:
407 %c Current capacity (mAh or mWh)
408 %B Battery status (verbose)
409 %p Battery load percentage
410 %L AC line status (verbose)"
416 ;; SysFS provides information about each battery present in the
417 ;; system in a separate subdirectory. We are going to merge the
418 ;; available information together.
420 (dolist (dir (ignore-errors
422 "/sys/class/power_supply/" t
"BAT[0-9]$")))
424 (ignore-errors (insert-file-contents
425 (expand-file-name "uevent" dir
)))
426 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t
)
427 (goto-char (point-min))
428 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t
)
429 (member charging-state
'("Unknown" "Full" nil
))
430 (setq charging-state
(match-string 1)))
431 (let (full-string now-string
)
432 ;; Sysfs may list either charge (mAh) or energy (mWh).
433 ;; Keep track of both, and choose which to report later.
434 (cond ((and (re-search-forward
435 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t
)
436 (setq full-string
(match-string 1))
438 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t
)
439 (setq now-string
(match-string 1)))
440 (setq charge-full
(+ charge-full
441 (string-to-number full-string
))
442 charge-now
(+ charge-now
443 (string-to-number now-string
))))
444 ((and (re-search-forward
445 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t
)
446 (setq full-string
(match-string 1))
448 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t
)
449 (setq now-string
(match-string 1)))
450 (setq energy-full
(+ energy-full
451 (string-to-number full-string
))
452 energy-now
(+ energy-now
453 (string-to-number now-string
)))))))))
454 (list (cons ?c
(cond ((or (> charge-full
0) (> charge-now
0))
455 (number-to-string charge-now
))
456 ((or (> energy-full
0) (> energy-now
0))
457 (number-to-string energy-now
))
459 (cons ?B
(or charging-state
"N/A"))
460 (cons ?p
(cond ((> charge-full
0)
462 (/ (* 100 charge-now
) charge-full
)))
465 (/ (* 100 energy-now
) energy-full
)))
467 (cons ?L
(if (file-readable-p "/sys/class/power_supply/AC/online")
468 (if (battery-search-for-one-match-in-files
469 (list "/sys/class/power_supply/AC/online"
470 "/sys/class/power_supply/ACAD/online")
478 ;;; `pmset' interface for Darwin (OS X).
480 (defun battery-pmset ()
481 "Get battery status information using `pmset'.
483 The following %-sequences are provided:
484 %L Power source (verbose)
485 %B Battery status (verbose)
486 %b Battery status, empty means high, `-' means low,
487 `!' means critical, and `+' means charging
488 %p Battery load percentage
489 %h Remaining time in hours
490 %m Remaining time in minutes
491 %t Remaining time in the form `h:min'"
492 (let (power-source load-percentage battery-status battery-status-symbol
493 remaining-time hours minutes
)
495 (ignore-errors (call-process "pmset" nil t nil
"-g" "ps"))
496 (goto-char (point-min))
497 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t
)
498 (setq power-source
(match-string 1))
499 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t
)
500 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
501 (setq load-percentage
(match-string 1))
502 (goto-char (match-end 0))
503 (cond ((looking-at "; charging")
504 (setq battery-status
"charging"
505 battery-status-symbol
"+"))
506 ((< (string-to-number load-percentage
) battery-load-low
)
507 (setq battery-status
"low"
508 battery-status-symbol
"-"))
509 ((< (string-to-number load-percentage
) battery-load-critical
)
510 (setq battery-status
"critical"
511 battery-status-symbol
"!"))
513 (setq battery-status
"high"
514 battery-status-symbol
"")))
515 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t
)
516 (setq remaining-time
(match-string 1))
517 (let ((h (string-to-number (match-string 2)))
518 (m (string-to-number (match-string 3))))
519 (setq hours
(number-to-string (+ h
(if (< m
30) 0 1)))
520 minutes
(number-to-string (+ (* h
60) m
)))))))))
521 (list (cons ?L
(or power-source
"N/A"))
522 (cons ?p
(or load-percentage
"N/A"))
523 (cons ?B
(or battery-status
"N/A"))
524 (cons ?b
(or battery-status-symbol
""))
525 (cons ?h
(or hours
"N/A"))
526 (cons ?m
(or minutes
"N/A"))
527 (cons ?t
(or remaining-time
"N/A")))))
530 ;;; Private functions.
532 (defun battery-format (format alist
)
533 "Substitute %-sequences in FORMAT."
534 (replace-regexp-in-string
537 (let ((char (aref str
1)))
539 (or (cdr (assoc char alist
)) ""))))
542 (defun battery-search-for-one-match-in-files (files regexp match-num
)
543 "Search REGEXP in the content of the files listed in FILES.
544 If a match occurred, return the parenthesized expression numbered by
545 MATCH-NUM in the match. Otherwise, return nil."
549 (and (ignore-errors (insert-file-contents file nil nil nil
'replace
))
550 (re-search-forward regexp nil t
)
551 (throw 'found
(match-string match-num
)))))))
556 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
557 ;;; battery.el ends here