* callproc.c (Fcall_process): Use 'volatile' to avoid vfork clobbering.
[emacs.git] / lisp / battery.el
blob9afe9de7b983a09b557acd599f969c37f88ec700
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>
6 ;; Keywords: hardware
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/>.
23 ;;; Commentary:
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.
31 ;;; Code:
33 (require 'timer)
34 (eval-when-compile (require 'cl))
37 (defgroup battery nil
38 "Display battery status information."
39 :prefix "battery-"
40 :group 'hardware)
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]$"))
52 'battery-linux-sysfs)
53 ((and (eq system-type 'darwin)
54 (condition-case nil
55 (with-temp-buffer
56 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
57 (> (buffer-size) 0)))
58 (error nil)))
59 'battery-pmset)
60 ((eq system-type 'windows-nt)
61 'w32-battery-status))
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)
71 :group 'battery)
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))
99 :group 'battery)
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-format
106 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
107 "[%b%p%%,%d°C]")
108 (battery-status-function
109 "[%b%p%%]"))
110 "Control string formatting the string to display in the mode line.
111 Ordinary characters in the control string are printed as-is, while
112 conversion specifications introduced by a `%' character in the control
113 string are substituted as defined by the current value of the variable
114 `battery-status-function'. Here are the ones generally available:
115 %c Current capacity (mAh or mWh)
116 %r Current rate of charge or discharge
117 %B Battery status (verbose)
118 %b Battery status: empty means high, `-' means low,
119 `!' means critical, and `+' means charging
120 %d Temperature (in degrees Celsius)
121 %L AC line status (verbose)
122 %p Battery load percentage
123 %m Remaining time (to charge or discharge) in minutes
124 %h Remaining time (to charge or discharge) in hours
125 %t Remaining time (to charge or discharge) in the form `h:min'"
126 :type '(choice string (const nil))
127 :group 'battery)
129 (defcustom battery-update-interval 60
130 "Seconds after which the battery status will be updated."
131 :type 'integer
132 :group 'battery)
134 (defcustom battery-load-low 25
135 "Upper bound of low battery load percentage.
136 A battery load percentage below this number is considered low."
137 :type 'integer
138 :group 'battery)
140 (defcustom battery-load-critical 10
141 "Upper bound of critical battery load percentage.
142 A battery load percentage below this number is considered critical."
143 :type 'integer
144 :group 'battery)
146 (defvar battery-update-timer nil
147 "Interval timer object.")
149 ;;;###autoload
150 (defun battery ()
151 "Display battery status information in the echo area.
152 The text being displayed in the echo area is controlled by the variables
153 `battery-echo-area-format' and `battery-status-function'."
154 (interactive)
155 (message "%s" (if (and battery-echo-area-format battery-status-function)
156 (battery-format battery-echo-area-format
157 (funcall battery-status-function))
158 "Battery status not available")))
160 ;;;###autoload
161 (define-minor-mode display-battery-mode
162 "Display battery status information in the mode line.
163 The text being displayed in the mode line is controlled by the variables
164 `battery-mode-line-format' and `battery-status-function'.
165 The mode line will be updated automatically every `battery-update-interval'
166 seconds."
167 :global t :group 'battery
168 (setq battery-mode-line-string "")
169 (or global-mode-string (setq global-mode-string '("")))
170 (and battery-update-timer (cancel-timer battery-update-timer))
171 (if (not display-battery-mode)
172 (setq global-mode-string
173 (delq 'battery-mode-line-string global-mode-string))
174 (add-to-list 'global-mode-string 'battery-mode-line-string t)
175 (setq battery-update-timer (run-at-time nil battery-update-interval
176 'battery-update-handler))
177 (battery-update)))
179 (defun battery-update-handler ()
180 (battery-update)
181 (sit-for 0))
183 (defun battery-update ()
184 "Update battery status information in the mode line."
185 (setq battery-mode-line-string
186 (propertize (if (and battery-mode-line-format
187 battery-status-function)
188 (battery-format
189 battery-mode-line-format
190 (funcall battery-status-function))
192 'help-echo "Battery status information"))
193 (force-mode-line-update))
196 ;;; `/proc/apm' interface for Linux.
198 (defconst battery-linux-proc-apm-regexp
199 (concat "^\\([^ ]+\\)" ; Driver version.
200 " \\([^ ]+\\)" ; APM BIOS version.
201 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
202 " 0x\\([0-9a-f]+\\)" ; AC line status.
203 " 0x\\([0-9a-f]+\\)" ; Battery status.
204 " 0x\\([0-9a-f]+\\)" ; Battery flags.
205 " \\(-?[0-9]+\\)%" ; Load percentage.
206 " \\(-?[0-9]+\\)" ; Remaining time.
207 " \\(.*\\)" ; Time unit.
208 "$")
209 "Regular expression matching contents of `/proc/apm'.")
211 (defun battery-linux-proc-apm ()
212 "Get APM status information from Linux kernel.
213 This function works only with the new `/proc/apm' format introduced
214 in Linux version 1.3.58.
216 The following %-sequences are provided:
217 %v Linux driver version
218 %V APM BIOS version
219 %I APM BIOS status (verbose)
220 %L AC line status (verbose)
221 %B Battery status (verbose)
222 %b Battery status, empty means high, `-' means low,
223 `!' means critical, and `+' means charging
224 %p Battery load percentage
225 %s Remaining time (to charge or discharge) in seconds
226 %m Remaining time (to charge or discharge) in minutes
227 %h Remaining time (to charge or discharge) in hours
228 %t Remaining time (to charge or discharge) in the form `h:min'"
229 (let (driver-version bios-version bios-interface line-status
230 battery-status battery-status-symbol load-percentage
231 seconds minutes hours remaining-time tem)
232 (with-temp-buffer
233 (ignore-errors (insert-file-contents "/proc/apm"))
234 (when (re-search-forward battery-linux-proc-apm-regexp)
235 (setq driver-version (match-string 1))
236 (setq bios-version (match-string 2))
237 (setq tem (string-to-number (match-string 3) 16))
238 (if (not (logand tem 2))
239 (setq bios-interface "not supported")
240 (setq bios-interface "enabled")
241 (cond ((logand tem 16) (setq bios-interface "disabled"))
242 ((logand tem 32) (setq bios-interface "disengaged")))
243 (setq tem (string-to-number (match-string 4) 16))
244 (cond ((= tem 0) (setq line-status "off-line"))
245 ((= tem 1) (setq line-status "on-line"))
246 ((= tem 2) (setq line-status "on backup")))
247 (setq tem (string-to-number (match-string 6) 16))
248 (if (= tem 255)
249 (setq battery-status "N/A")
250 (setq tem (string-to-number (match-string 5) 16))
251 (cond ((= tem 0) (setq battery-status "high"
252 battery-status-symbol ""))
253 ((= tem 1) (setq battery-status "low"
254 battery-status-symbol "-"))
255 ((= tem 2) (setq battery-status "critical"
256 battery-status-symbol "!"))
257 ((= tem 3) (setq battery-status "charging"
258 battery-status-symbol "+")))
259 (setq load-percentage (match-string 7))
260 (setq seconds (string-to-number (match-string 8)))
261 (and (string-equal (match-string 9) "min")
262 (setq seconds (* 60 seconds)))
263 (setq minutes (/ seconds 60)
264 hours (/ seconds 3600))
265 (setq remaining-time
266 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
267 (list (cons ?v (or driver-version "N/A"))
268 (cons ?V (or bios-version "N/A"))
269 (cons ?I (or bios-interface "N/A"))
270 (cons ?L (or line-status "N/A"))
271 (cons ?B (or battery-status "N/A"))
272 (cons ?b (or battery-status-symbol ""))
273 (cons ?p (or load-percentage "N/A"))
274 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
275 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
276 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
277 (cons ?t (or remaining-time "N/A")))))
280 ;;; `/proc/acpi/' interface for Linux.
282 (defun battery-linux-proc-acpi ()
283 "Get ACPI status information from Linux kernel.
284 This function works only with the `/proc/acpi/' format introduced
285 in Linux version 2.4.20 and 2.6.0.
287 The following %-sequences are provided:
288 %c Current capacity (mAh)
289 %r Current rate
290 %B Battery status (verbose)
291 %b Battery status, empty means high, `-' means low,
292 `!' means critical, and `+' means charging
293 %d Temperature (in degrees Celsius)
294 %L AC line status (verbose)
295 %p Battery load percentage
296 %m Remaining time (to charge or discharge) in minutes
297 %h Remaining time (to charge or discharge) in hours
298 %t Remaining time (to charge or discharge) in the form `h:min'"
299 (let ((design-capacity 0)
300 (last-full-capacity 0)
301 full-capacity
302 (warn 0)
303 (low 0)
304 capacity rate rate-type charging-state minutes hours)
305 ;; ACPI provides information about each battery present in the system in
306 ;; a separate subdirectory. We are going to merge the available
307 ;; information together since displaying for a variable amount of
308 ;; batteries seems overkill for format-strings.
309 (with-temp-buffer
310 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
311 t "\\`[^.]")))
312 (erase-buffer)
313 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
314 (when (re-search-forward "present: +yes$" nil t)
315 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
316 (member charging-state '("unknown" "charged" nil))
317 ;; On most multi-battery systems, most of the time only one
318 ;; battery is "charging"/"discharging", the others are
319 ;; "unknown".
320 (setq charging-state (match-string 1)))
321 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
322 nil t)
323 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
324 rate-type (or (and rate-type
325 (if (string= rate-type (match-string 2))
326 rate-type
327 (error
328 "Inconsistent rate types (%s vs. %s)"
329 rate-type (match-string 2))))
330 (match-string 2))))
331 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
332 nil t)
333 (setq capacity
334 (+ (or capacity 0) (string-to-number (match-string 1))))))
335 (goto-char (point-max))
336 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
337 (when (re-search-forward "present: +yes$" nil t)
338 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
339 nil t)
340 (incf design-capacity (string-to-number (match-string 1))))
341 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
342 nil t)
343 (incf last-full-capacity (string-to-number (match-string 1))))
344 (when (re-search-forward
345 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
346 (incf warn (string-to-number (match-string 1))))
347 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
348 nil t)
349 (incf low (string-to-number (match-string 1)))))))
350 (setq full-capacity (if (> last-full-capacity 0)
351 last-full-capacity design-capacity))
352 (and capacity rate
353 (setq minutes (if (zerop rate) 0
354 (floor (* (/ (float (if (string= charging-state
355 "charging")
356 (- full-capacity capacity)
357 capacity))
358 rate)
359 60)))
360 hours (/ minutes 60)))
361 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
362 (cons ?L (or (battery-search-for-one-match-in-files
363 (mapcar (lambda (e) (concat e "/state"))
364 (ignore-errors
365 (directory-files "/proc/acpi/ac_adapter/"
366 t "\\`[^.]")))
367 "state: +\\(.*\\)$" 1)
369 "N/A"))
370 (cons ?d (or (battery-search-for-one-match-in-files
371 (mapcar (lambda (e) (concat e "/temperature"))
372 (ignore-errors
373 (directory-files "/proc/acpi/thermal_zone/"
374 t "\\`[^.]")))
375 "temperature: +\\([0-9]+\\) C$" 1)
377 "N/A"))
378 (cons ?r (or (and rate (concat (number-to-string rate) " "
379 rate-type)) "N/A"))
380 (cons ?B (or charging-state "N/A"))
381 (cons ?b (or (and (string= charging-state "charging") "+")
382 (and capacity (< capacity low) "!")
383 (and capacity (< capacity warn) "-")
384 ""))
385 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
386 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
387 (cons ?t (or (and minutes
388 (format "%d:%02d" hours (- minutes (* 60 hours))))
389 "N/A"))
390 (cons ?p (or (and full-capacity capacity
391 (> full-capacity 0)
392 (number-to-string
393 (floor (/ capacity
394 (/ (float full-capacity) 100)))))
395 "N/A")))))
398 ;;; `/sys/class/power_supply/BATN' interface for Linux.
400 (defun battery-linux-sysfs ()
401 "Get ACPI status information from Linux kernel.
402 This function works only with the new `/sys/class/power_supply/'
403 format introduced in Linux version 2.4.25.
405 The following %-sequences are provided:
406 %c Current capacity (mAh or mWh)
407 %B Battery status (verbose)
408 %p Battery load percentage
409 %L AC line status (verbose)"
410 (let (charging-state
411 (charge-full 0.0)
412 (charge-now 0.0)
413 (energy-full 0.0)
414 (energy-now 0.0))
415 ;; SysFS provides information about each battery present in the
416 ;; system in a separate subdirectory. We are going to merge the
417 ;; available information together.
418 (with-temp-buffer
419 (dolist (dir (ignore-errors
420 (directory-files
421 "/sys/class/power_supply/" t "BAT[0-9]$")))
422 (erase-buffer)
423 (ignore-errors (insert-file-contents
424 (expand-file-name "uevent" dir)))
425 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t)
426 (goto-char (point-min))
427 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t)
428 (member charging-state '("Unknown" "Full" nil))
429 (setq charging-state (match-string 1)))
430 (let (full-string now-string)
431 ;; Sysfs may list either charge (mAh) or energy (mWh).
432 ;; Keep track of both, and choose which to report later.
433 (cond ((and (re-search-forward
434 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t)
435 (setq full-string (match-string 1))
436 (re-search-forward
437 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t)
438 (setq now-string (match-string 1)))
439 (setq charge-full (+ charge-full
440 (string-to-number full-string))
441 charge-now (+ charge-now
442 (string-to-number now-string))))
443 ((and (re-search-forward
444 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t)
445 (setq full-string (match-string 1))
446 (re-search-forward
447 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t)
448 (setq now-string (match-string 1)))
449 (setq energy-full (+ energy-full
450 (string-to-number full-string))
451 energy-now (+ energy-now
452 (string-to-number now-string)))))))))
453 (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0))
454 (number-to-string charge-now))
455 ((or (> energy-full 0) (> energy-now 0))
456 (number-to-string energy-now))
457 (t "N/A")))
458 (cons ?B (or charging-state "N/A"))
459 (cons ?p (cond ((> charge-full 0)
460 (format "%.1f"
461 (/ (* 100 charge-now) charge-full)))
462 ((> energy-full 0)
463 (format "%.1f"
464 (/ (* 100 energy-now) energy-full)))
465 (t "N/A")))
466 (cons ?L (if (file-readable-p "/sys/class/power_supply/AC/online")
467 (if (battery-search-for-one-match-in-files
468 (list "/sys/class/power_supply/AC/online"
469 "/sys/class/power_supply/ACAD/online")
470 "1" 0)
471 "AC"
472 "BAT")
473 "N/A")))))
477 ;;; `pmset' interface for Darwin (OS X).
479 (defun battery-pmset ()
480 "Get battery status information using `pmset'.
482 The following %-sequences are provided:
483 %L Power source (verbose)
484 %B Battery status (verbose)
485 %b Battery status, empty means high, `-' means low,
486 `!' means critical, and `+' means charging
487 %p Battery load percentage
488 %h Remaining time in hours
489 %m Remaining time in minutes
490 %t Remaining time in the form `h:min'"
491 (let (power-source load-percentage battery-status battery-status-symbol
492 remaining-time hours minutes)
493 (with-temp-buffer
494 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
495 (goto-char (point-min))
496 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
497 (setq power-source (match-string 1))
498 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
499 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
500 (setq load-percentage (match-string 1))
501 (goto-char (match-end 0))
502 (cond ((looking-at "; charging")
503 (setq battery-status "charging"
504 battery-status-symbol "+"))
505 ((< (string-to-number load-percentage) battery-load-low)
506 (setq battery-status "low"
507 battery-status-symbol "-"))
508 ((< (string-to-number load-percentage) battery-load-critical)
509 (setq battery-status "critical"
510 battery-status-symbol "!"))
512 (setq battery-status "high"
513 battery-status-symbol "")))
514 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
515 (setq remaining-time (match-string 1))
516 (let ((h (string-to-number (match-string 2)))
517 (m (string-to-number (match-string 3))))
518 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
519 minutes (number-to-string (+ (* h 60) m)))))))))
520 (list (cons ?L (or power-source "N/A"))
521 (cons ?p (or load-percentage "N/A"))
522 (cons ?B (or battery-status "N/A"))
523 (cons ?b (or battery-status-symbol ""))
524 (cons ?h (or hours "N/A"))
525 (cons ?m (or minutes "N/A"))
526 (cons ?t (or remaining-time "N/A")))))
529 ;;; Private functions.
531 (defun battery-format (format alist)
532 "Substitute %-sequences in FORMAT."
533 (replace-regexp-in-string
534 "%."
535 (lambda (str)
536 (let ((char (aref str 1)))
537 (if (eq char ?%) "%"
538 (or (cdr (assoc char alist)) ""))))
539 format t t))
541 (defun battery-search-for-one-match-in-files (files regexp match-num)
542 "Search REGEXP in the content of the files listed in FILES.
543 If a match occurred, return the parenthesized expression numbered by
544 MATCH-NUM in the match. Otherwise, return nil."
545 (with-temp-buffer
546 (catch 'found
547 (dolist (file files)
548 (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
549 (re-search-forward regexp nil t)
550 (throw 'found (match-string match-num)))))))
553 (provide 'battery)
555 ;;; battery.el ends here