Silence nnir.el compilation.
[emacs.git] / lisp / battery.el
blobfc5926efd175de0d49acfbd24b39d7a8b6e33fc0
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>
7 ;; Keywords: hardware
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/>.
24 ;;; Commentary:
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.
32 ;;; Code:
34 (require 'timer)
35 (eval-when-compile (require 'cl))
38 (defgroup battery nil
39 "Display battery status information."
40 :prefix "battery-"
41 :group 'hardware)
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]$"))
53 'battery-linux-sysfs)
54 ((and (eq system-type 'darwin)
55 (condition-case nil
56 (with-temp-buffer
57 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
58 (> (buffer-size) 0)))
59 (error nil)))
60 'battery-pmset)
61 ((eq system-type 'windows-nt)
62 'w32-battery-status))
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)
72 :group 'battery)
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))
100 :group 'battery)
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)
108 "[%b%p%%,%d°C]")
109 (battery-status-function
110 "[%b%p%%]"))
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))
128 :group 'battery)
130 (defcustom battery-update-interval 60
131 "Seconds after which the battery status will be updated."
132 :type 'integer
133 :group 'battery)
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."
138 :type 'integer
139 :group 'battery)
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."
144 :type 'integer
145 :group 'battery)
147 (defvar battery-update-timer nil
148 "Interval timer object.")
150 ;;;###autoload
151 (defun battery ()
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'."
155 (interactive)
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")))
161 ;;;###autoload
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'
167 seconds."
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))
178 (battery-update)))
180 (defun battery-update-handler ()
181 (battery-update)
182 (sit-for 0))
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)
189 (battery-format
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.
209 "$")
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
219 %V APM BIOS 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)
233 (with-temp-buffer
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))
249 (if (= tem 255)
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))
266 (setq remaining-time
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)
290 %r Current rate
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)
302 full-capacity
303 (warn 0)
304 (low 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.
310 (with-temp-buffer
311 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
312 t "\\`[^.]")))
313 (erase-buffer)
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
320 ;; "unknown".
321 (setq charging-state (match-string 1)))
322 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
323 nil t)
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))
327 rate-type
328 (error
329 "Inconsistent rate types (%s vs. %s)"
330 rate-type (match-string 2))))
331 (match-string 2))))
332 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
333 nil t)
334 (setq capacity
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$"
340 nil t)
341 (incf design-capacity (string-to-number (match-string 1))))
342 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
343 nil t)
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$"
349 nil t)
350 (incf low (string-to-number (match-string 1)))))))
351 (setq full-capacity (if (> last-full-capacity 0)
352 last-full-capacity design-capacity))
353 (and capacity rate
354 (setq minutes (if (zerop rate) 0
355 (floor (* (/ (float (if (string= charging-state
356 "charging")
357 (- full-capacity capacity)
358 capacity))
359 rate)
360 60)))
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"))
365 (ignore-errors
366 (directory-files "/proc/acpi/ac_adapter/"
367 t "\\`[^.]")))
368 "state: +\\(.*\\)$" 1)
370 "N/A"))
371 (cons ?d (or (battery-search-for-one-match-in-files
372 (mapcar (lambda (e) (concat e "/temperature"))
373 (ignore-errors
374 (directory-files "/proc/acpi/thermal_zone/"
375 t "\\`[^.]")))
376 "temperature: +\\([0-9]+\\) C$" 1)
378 "N/A"))
379 (cons ?r (or (and rate (concat (number-to-string rate) " "
380 rate-type)) "N/A"))
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) "-")
385 ""))
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))))
390 "N/A"))
391 (cons ?p (or (and full-capacity capacity
392 (> full-capacity 0)
393 (number-to-string
394 (floor (/ capacity
395 (/ (float full-capacity) 100)))))
396 "N/A")))))
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)"
411 (let (charging-state
412 (charge-full 0.0)
413 (charge-now 0.0)
414 (energy-full 0.0)
415 (energy-now 0.0))
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.
419 (with-temp-buffer
420 (dolist (dir (ignore-errors
421 (directory-files
422 "/sys/class/power_supply/" t "BAT[0-9]$")))
423 (erase-buffer)
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))
437 (re-search-forward
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))
447 (re-search-forward
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))
458 (t "N/A")))
459 (cons ?B (or charging-state "N/A"))
460 (cons ?p (cond ((> charge-full 0)
461 (format "%.1f"
462 (/ (* 100 charge-now) charge-full)))
463 ((> energy-full 0)
464 (format "%.1f"
465 (/ (* 100 energy-now) energy-full)))
466 (t "N/A")))
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")
471 "1" 0)
472 "AC"
473 "BAT")
474 "N/A")))))
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)
494 (with-temp-buffer
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
535 "%."
536 (lambda (str)
537 (let ((char (aref str 1)))
538 (if (eq char ?%) "%"
539 (or (cdr (assoc char alist)) ""))))
540 format t t))
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."
546 (with-temp-buffer
547 (catch 'found
548 (dolist (file files)
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)))))))
554 (provide 'battery)
556 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
557 ;;; battery.el ends here