Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / battery.el
blob22b09e15e8011b530acf4cd2f96438008650b7a0
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 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 'darwin)
51 (condition-case nil
52 (with-temp-buffer
53 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
54 (> (buffer-size) 0)))
55 (error nil)))
56 'battery-pmset)
57 ((eq system-type 'windows-nt)
58 'w32-battery-status))
59 "*Function for getting battery status information.
60 The function has to return an alist of conversion definitions.
61 Its cons cells are of the form
63 (CONVERSION . REPLACEMENT-TEXT)
65 CONVERSION is the character code of a \"conversion specification\"
66 introduced by a `%' character in a control string."
67 :type '(choice (const nil) function)
68 :group 'battery)
70 (defcustom battery-echo-area-format
71 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
72 "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 (battery-status-function
76 "Power %L, battery %B (%p%% load, remaining time %t)"))
77 "*Control string formatting the string to display in the echo area.
78 Ordinary characters in the control string are printed as-is, while
79 conversion specifications introduced by a `%' character in the control
80 string are substituted as defined by the current value of the variable
81 `battery-status-function'. Here are the ones generally available:
82 %c Current capacity (mAh or mWh)
83 %r Current rate of charge or discharge
84 %B Battery status (verbose)
85 %b Battery status: empty means high, `-' means low,
86 `!' means critical, and `+' means charging
87 %d Temperature (in degrees Celsius)
88 %L AC line status (verbose)
89 %p Battery load percentage
90 %m Remaining time (to charge or discharge) in minutes
91 %h Remaining time (to charge or discharge) in hours
92 %t Remaining time (to charge or discharge) in the form `h:min'"
93 :type '(choice string (const nil))
94 :group 'battery)
96 (defvar battery-mode-line-string nil
97 "String to display in the mode line.")
98 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
100 (defcustom battery-mode-line-format
101 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
102 "[%b%p%%,%d°C]")
103 (battery-status-function
104 "[%b%p%%]"))
105 "*Control string formatting the string to display in the mode line.
106 Ordinary characters in the control string are printed as-is, while
107 conversion specifications introduced by a `%' character in the control
108 string are substituted as defined by the current value of the variable
109 `battery-status-function'. Here are the ones generally available:
110 %c Current capacity (mAh or mWh)
111 %r Current rate of charge or discharge
112 %B Battery status (verbose)
113 %b Battery status: empty means high, `-' means low,
114 `!' means critical, and `+' means charging
115 %d Temperature (in degrees Celsius)
116 %L AC line status (verbose)
117 %p Battery load percentage
118 %m Remaining time (to charge or discharge) in minutes
119 %h Remaining time (to charge or discharge) in hours
120 %t Remaining time (to charge or discharge) in the form `h:min'"
121 :type '(choice string (const nil))
122 :group 'battery)
124 (defcustom battery-update-interval 60
125 "*Seconds after which the battery status will be updated."
126 :type 'integer
127 :group 'battery)
129 (defcustom battery-load-low 25
130 "*Upper bound of low battery load percentage.
131 A battery load percentage below this number is considered low."
132 :type 'integer
133 :group 'battery)
135 (defcustom battery-load-critical 10
136 "*Upper bound of critical battery load percentage.
137 A battery load percentage below this number is considered critical."
138 :type 'integer
139 :group 'battery)
141 (defvar battery-update-timer nil
142 "Interval timer object.")
144 ;;;###autoload
145 (defun battery ()
146 "Display battery status information in the echo area.
147 The text being displayed in the echo area is controlled by the variables
148 `battery-echo-area-format' and `battery-status-function'."
149 (interactive)
150 (message "%s" (if (and battery-echo-area-format battery-status-function)
151 (battery-format battery-echo-area-format
152 (funcall battery-status-function))
153 "Battery status not available")))
155 ;;;###autoload
156 (define-minor-mode display-battery-mode
157 "Display battery status information in the mode line.
158 The text being displayed in the mode line is controlled by the variables
159 `battery-mode-line-format' and `battery-status-function'.
160 The mode line will be updated automatically every `battery-update-interval'
161 seconds."
162 :global t :group 'battery
163 (setq battery-mode-line-string "")
164 (or global-mode-string (setq global-mode-string '("")))
165 (and battery-update-timer (cancel-timer battery-update-timer))
166 (if (not display-battery-mode)
167 (setq global-mode-string
168 (delq 'battery-mode-line-string global-mode-string))
169 (add-to-list 'global-mode-string 'battery-mode-line-string t)
170 (setq battery-update-timer (run-at-time nil battery-update-interval
171 'battery-update-handler))
172 (battery-update)))
174 (defun battery-update-handler ()
175 (battery-update)
176 (sit-for 0))
178 (defun battery-update ()
179 "Update battery status information in the mode line."
180 (setq battery-mode-line-string
181 (propertize (if (and battery-mode-line-format
182 battery-status-function)
183 (battery-format
184 battery-mode-line-format
185 (funcall battery-status-function))
187 'help-echo "Battery status information"))
188 (force-mode-line-update))
191 ;;; `/proc/apm' interface for Linux.
193 (defconst battery-linux-proc-apm-regexp
194 (concat "^\\([^ ]+\\)" ; Driver version.
195 " \\([^ ]+\\)" ; APM BIOS version.
196 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
197 " 0x\\([0-9a-f]+\\)" ; AC line status.
198 " 0x\\([0-9a-f]+\\)" ; Battery status.
199 " 0x\\([0-9a-f]+\\)" ; Battery flags.
200 " \\(-?[0-9]+\\)%" ; Load percentage.
201 " \\(-?[0-9]+\\)" ; Remaining time.
202 " \\(.*\\)" ; Time unit.
203 "$")
204 "Regular expression matching contents of `/proc/apm'.")
206 (defun battery-linux-proc-apm ()
207 "Get APM status information from Linux kernel.
208 This function works only with the new `/proc/apm' format introduced
209 in Linux version 1.3.58.
211 The following %-sequences are provided:
212 %v Linux driver version
213 %V APM BIOS version
214 %I APM BIOS status (verbose)
215 %L AC line status (verbose)
216 %B Battery status (verbose)
217 %b Battery status, empty means high, `-' means low,
218 `!' means critical, and `+' means charging
219 %p Battery load percentage
220 %s Remaining time (to charge or discharge) in seconds
221 %m Remaining time (to charge or discharge) in minutes
222 %h Remaining time (to charge or discharge) in hours
223 %t Remaining time (to charge or discharge) in the form `h:min'"
224 (let (driver-version bios-version bios-interface line-status
225 battery-status battery-status-symbol load-percentage
226 seconds minutes hours remaining-time tem)
227 (with-temp-buffer
228 (ignore-errors (insert-file-contents "/proc/apm"))
229 (when (re-search-forward battery-linux-proc-apm-regexp)
230 (setq driver-version (match-string 1))
231 (setq bios-version (match-string 2))
232 (setq tem (string-to-number (match-string 3) 16))
233 (if (not (logand tem 2))
234 (setq bios-interface "not supported")
235 (setq bios-interface "enabled")
236 (cond ((logand tem 16) (setq bios-interface "disabled"))
237 ((logand tem 32) (setq bios-interface "disengaged")))
238 (setq tem (string-to-number (match-string 4) 16))
239 (cond ((= tem 0) (setq line-status "off-line"))
240 ((= tem 1) (setq line-status "on-line"))
241 ((= tem 2) (setq line-status "on backup")))
242 (setq tem (string-to-number (match-string 6) 16))
243 (if (= tem 255)
244 (setq battery-status "N/A")
245 (setq tem (string-to-number (match-string 5) 16))
246 (cond ((= tem 0) (setq battery-status "high"
247 battery-status-symbol ""))
248 ((= tem 1) (setq battery-status "low"
249 battery-status-symbol "-"))
250 ((= tem 2) (setq battery-status "critical"
251 battery-status-symbol "!"))
252 ((= tem 3) (setq battery-status "charging"
253 battery-status-symbol "+")))
254 (setq load-percentage (match-string 7))
255 (setq seconds (string-to-number (match-string 8)))
256 (and (string-equal (match-string 9) "min")
257 (setq seconds (* 60 seconds)))
258 (setq minutes (/ seconds 60)
259 hours (/ seconds 3600))
260 (setq remaining-time
261 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
262 (list (cons ?v (or driver-version "N/A"))
263 (cons ?V (or bios-version "N/A"))
264 (cons ?I (or bios-interface "N/A"))
265 (cons ?L (or line-status "N/A"))
266 (cons ?B (or battery-status "N/A"))
267 (cons ?b (or battery-status-symbol ""))
268 (cons ?p (or load-percentage "N/A"))
269 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
270 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
271 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
272 (cons ?t (or remaining-time "N/A")))))
275 ;;; `/proc/acpi/' interface for Linux.
277 (defun battery-linux-proc-acpi ()
278 "Get ACPI status information from Linux kernel.
279 This function works only with the new `/proc/acpi/' format introduced
280 in Linux version 2.4.20 and 2.6.0.
282 The following %-sequences are provided:
283 %c Current capacity (mAh)
284 %r Current rate
285 %B Battery status (verbose)
286 %b Battery status, empty means high, `-' means low,
287 `!' means critical, and `+' means charging
288 %d Temperature (in degrees Celsius)
289 %L AC line status (verbose)
290 %p Battery load percentage
291 %m Remaining time (to charge or discharge) in minutes
292 %h Remaining time (to charge or discharge) in hours
293 %t Remaining time (to charge or discharge) in the form `h:min'"
294 (let ((design-capacity 0)
295 (last-full-capacity 0)
296 full-capacity
297 (warn 0)
298 (low 0)
299 capacity rate rate-type charging-state minutes hours)
300 ;; ACPI provides information about each battery present in the system in
301 ;; a separate subdirectory. We are going to merge the available
302 ;; information together since displaying for a variable amount of
303 ;; batteries seems overkill for format-strings.
304 (with-temp-buffer
305 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
306 t "\\`[^.]")))
307 (erase-buffer)
308 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
309 (when (re-search-forward "present: +yes$" nil t)
310 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
311 (member charging-state '("unknown" "charged" nil))
312 ;; On most multi-battery systems, most of the time only one
313 ;; battery is "charging"/"discharging", the others are
314 ;; "unknown".
315 (setq charging-state (match-string 1)))
316 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
317 nil t)
318 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
319 rate-type (or (and rate-type
320 (if (string= rate-type (match-string 2))
321 rate-type
322 (error
323 "Inconsistent rate types (%s vs. %s)"
324 rate-type (match-string 2))))
325 (match-string 2))))
326 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
327 nil t)
328 (setq capacity
329 (+ (or capacity 0) (string-to-number (match-string 1))))))
330 (goto-char (point-max))
331 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
332 (when (re-search-forward "present: +yes$" nil t)
333 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
334 nil t)
335 (incf design-capacity (string-to-number (match-string 1))))
336 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
337 nil t)
338 (incf last-full-capacity (string-to-number (match-string 1))))
339 (when (re-search-forward
340 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
341 (incf warn (string-to-number (match-string 1))))
342 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
343 nil t)
344 (incf low (string-to-number (match-string 1)))))))
345 (setq full-capacity (if (> last-full-capacity 0)
346 last-full-capacity design-capacity))
347 (and capacity rate
348 (setq minutes (if (zerop rate) 0
349 (floor (* (/ (float (if (string= charging-state
350 "charging")
351 (- full-capacity capacity)
352 capacity))
353 rate)
354 60)))
355 hours (/ minutes 60)))
356 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
357 (cons ?L (or (battery-search-for-one-match-in-files
358 (mapcar (lambda (e) (concat e "/state"))
359 (ignore-errors
360 (directory-files "/proc/acpi/ac_adapter/"
361 t "\\`[^.]")))
362 "state: +\\(.*\\)$" 1)
364 "N/A"))
365 (cons ?d (or (battery-search-for-one-match-in-files
366 (mapcar (lambda (e) (concat e "/temperature"))
367 (ignore-errors
368 (directory-files "/proc/acpi/thermal_zone/"
369 t "\\`[^.]")))
370 "temperature: +\\([0-9]+\\) C$" 1)
372 "N/A"))
373 (cons ?r (or (and rate (concat (number-to-string rate) " "
374 rate-type)) "N/A"))
375 (cons ?B (or charging-state "N/A"))
376 (cons ?b (or (and (string= charging-state "charging") "+")
377 (and capacity (< capacity low) "!")
378 (and capacity (< capacity warn) "-")
379 ""))
380 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
381 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
382 (cons ?t (or (and minutes
383 (format "%d:%02d" hours (- minutes (* 60 hours))))
384 "N/A"))
385 (cons ?p (or (and full-capacity capacity
386 (> full-capacity 0)
387 (number-to-string
388 (floor (/ capacity
389 (/ (float full-capacity) 100)))))
390 "N/A")))))
393 ;;; `pmset' interface for Darwin (OS X).
395 (defun battery-pmset ()
396 "Get battery status information using `pmset'.
398 The following %-sequences are provided:
399 %L Power source (verbose)
400 %B Battery status (verbose)
401 %b Battery status, empty means high, `-' means low,
402 `!' means critical, and `+' means charging
403 %p Battery load percentage
404 %h Remaining time in hours
405 %m Remaining time in minutes
406 %t Remaining time in the form `h:min'"
407 (let (power-source load-percentage battery-status battery-status-symbol
408 remaining-time hours minutes)
409 (with-temp-buffer
410 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
411 (goto-char (point-min))
412 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
413 (setq power-source (match-string 1))
414 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
415 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
416 (setq load-percentage (match-string 1))
417 (goto-char (match-end 0))
418 (cond ((looking-at "; charging")
419 (setq battery-status "charging"
420 battery-status-symbol "+"))
421 ((< (string-to-number load-percentage) battery-load-low)
422 (setq battery-status "low"
423 battery-status-symbol "-"))
424 ((< (string-to-number load-percentage) battery-load-critical)
425 (setq battery-status "critical"
426 battery-status-symbol "!"))
428 (setq battery-status "high"
429 battery-status-symbol "")))
430 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
431 (setq remaining-time (match-string 1))
432 (let ((h (string-to-number (match-string 2)))
433 (m (string-to-number (match-string 3))))
434 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
435 minutes (number-to-string (+ (* h 60) m)))))))))
436 (list (cons ?L (or power-source "N/A"))
437 (cons ?p (or load-percentage "N/A"))
438 (cons ?B (or battery-status "N/A"))
439 (cons ?b (or battery-status-symbol ""))
440 (cons ?h (or hours "N/A"))
441 (cons ?m (or minutes "N/A"))
442 (cons ?t (or remaining-time "N/A")))))
445 ;;; Private functions.
447 (defun battery-format (format alist)
448 "Substitute %-sequences in FORMAT."
449 (replace-regexp-in-string
450 "%."
451 (lambda (str)
452 (let ((char (aref str 1)))
453 (if (eq char ?%) "%"
454 (or (cdr (assoc char alist)) ""))))
455 format t t))
457 (defun battery-search-for-one-match-in-files (files regexp match-num)
458 "Search REGEXP in the content of the files listed in FILES.
459 If a match occurred, return the parenthesized expression numbered by
460 MATCH-NUM in the match. Otherwise, return nil."
461 (with-temp-buffer
462 (catch 'found
463 (dolist (file files)
464 (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
465 (re-search-forward regexp nil t)
466 (throw 'found (match-string match-num)))))))
469 (provide 'battery)
471 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
472 ;;; battery.el ends here