Revision: emacs@sv.gnu.org/emacs--devo--0--patch-106
[emacs.git] / lisp / battery.el
blobfa816c585b41f4a733d7fa19a7aceb27c4a6178e
1 ;;; battery.el --- display battery status information
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; There is at present support for GNU/Linux and OS X. This library
29 ;; supports both the `/proc/apm' file format of Linux version 1.3.58
30 ;; or newer and the `/proc/acpi/' directory structure of Linux 2.4.20
31 ;; and 2.6. Darwin (OS X) is supported by using the `pmset' program.
33 ;;; Code:
35 (require 'timer)
36 (eval-when-compile (require 'cl))
39 (defgroup battery nil
40 "Display battery status information."
41 :prefix "battery-"
42 :group 'hardware)
44 (defcustom battery-status-function
45 (cond ((and (eq system-type 'gnu/linux)
46 (file-readable-p "/proc/apm"))
47 'battery-linux-proc-apm)
48 ((and (eq system-type 'gnu/linux)
49 (file-directory-p "/proc/acpi/battery"))
50 'battery-linux-proc-acpi)
51 ((and (eq system-type 'darwin)
52 (condition-case nil
53 (with-temp-buffer
54 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
55 (> (buffer-size) 0)))
56 (error nil)))
57 'battery-pmset))
58 "*Function for getting battery status information.
59 The function has to return an alist of conversion definitions.
60 Its cons cells are of the form
62 (CONVERSION . REPLACEMENT-TEXT)
64 CONVERSION is the character code of a \"conversion specification\"
65 introduced by a `%' character in a control string."
66 :type '(choice (const nil) function)
67 :group 'battery)
69 (defcustom battery-echo-area-format
70 (cond ((eq battery-status-function 'battery-linux-proc-apm)
71 "Power %L, battery %B (%p%% load, remaining time %t)")
72 ((eq battery-status-function 'battery-linux-proc-acpi)
73 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
74 ((eq battery-status-function 'battery-pmset)
75 "%L power, battery %B (%p%% load, remaining time %t)"))
76 "*Control string formatting the string to display in the echo area.
77 Ordinary characters in the control string are printed as-is, while
78 conversion specifications introduced by a `%' character in the control
79 string are substituted as defined by the current value of the variable
80 `battery-status-function'."
81 :type '(choice string (const nil))
82 :group 'battery)
84 (defvar battery-mode-line-string nil
85 "String to display in the mode line.")
86 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
88 (defcustom battery-mode-line-format
89 (cond ((eq battery-status-function 'battery-linux-proc-apm)
90 "[%b%p%%]")
91 ((eq battery-status-function 'battery-linux-proc-acpi)
92 "[%b%p%%,%d°C]")
93 ((eq battery-status-function 'battery-pmset)
94 "[%b%p%%]"))
95 "*Control string formatting the string to display in the mode line.
96 Ordinary characters in the control string are printed as-is, while
97 conversion specifications introduced by a `%' character in the control
98 string are substituted as defined by the current value of the variable
99 `battery-status-function'."
100 :type '(choice string (const nil))
101 :group 'battery)
103 (defcustom battery-update-interval 60
104 "*Seconds after which the battery status will be updated."
105 :type 'integer
106 :group 'battery)
108 (defcustom battery-load-low 25
109 "*Upper bound of low battery load percentage.
110 A battery load percentage below this number is considered low."
111 :type 'integer
112 :group 'battery)
114 (defcustom battery-load-critical 10
115 "*Upper bound of critical battery load percentage.
116 A battery load percentage below this number is considered critical."
117 :type 'integer
118 :group 'battery)
120 (defvar battery-update-timer nil
121 "Interval timer object.")
123 ;;;###autoload
124 (defun battery ()
125 "Display battery status information in the echo area.
126 The text being displayed in the echo area is controlled by the variables
127 `battery-echo-area-format' and `battery-status-function'."
128 (interactive)
129 (message "%s" (if (and battery-echo-area-format battery-status-function)
130 (battery-format battery-echo-area-format
131 (funcall battery-status-function))
132 "Battery status not available")))
134 ;;;###autoload
135 (define-minor-mode display-battery-mode
136 "Display battery status information in the mode line.
137 The text being displayed in the mode line is controlled by the variables
138 `battery-mode-line-format' and `battery-status-function'.
139 The mode line will be updated automatically every `battery-update-interval'
140 seconds."
141 :global t :group 'battery
142 (setq battery-mode-line-string "")
143 (or global-mode-string (setq global-mode-string '("")))
144 (and battery-update-timer (cancel-timer battery-update-timer))
145 (if (not display-battery-mode)
146 (setq global-mode-string
147 (delq 'battery-mode-line-string global-mode-string))
148 (add-to-list 'global-mode-string 'battery-mode-line-string t)
149 (setq battery-update-timer (run-at-time nil battery-update-interval
150 'battery-update-handler))
151 (battery-update)))
153 (defun battery-update-handler ()
154 (battery-update)
155 (sit-for 0))
157 (defun battery-update ()
158 "Update battery status information in the mode line."
159 (setq battery-mode-line-string
160 (propertize (if (and battery-mode-line-format
161 battery-status-function)
162 (battery-format
163 battery-mode-line-format
164 (funcall battery-status-function))
166 'help-echo "Battery status information"))
167 (force-mode-line-update))
170 ;;; `/proc/apm' interface for Linux.
172 (defconst battery-linux-proc-apm-regexp
173 (concat "^\\([^ ]+\\)" ; Driver version.
174 " \\([^ ]+\\)" ; APM BIOS version.
175 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
176 " 0x\\([0-9a-f]+\\)" ; AC line status.
177 " 0x\\([0-9a-f]+\\)" ; Battery status.
178 " 0x\\([0-9a-f]+\\)" ; Battery flags.
179 " \\(-?[0-9]+\\)%" ; Load percentage.
180 " \\(-?[0-9]+\\)" ; Remaining time.
181 " \\(.*\\)" ; Time unit.
182 "$")
183 "Regular expression matching contents of `/proc/apm'.")
185 (defun battery-linux-proc-apm ()
186 "Get APM status information from Linux kernel.
187 This function works only with the new `/proc/apm' format introduced
188 in Linux version 1.3.58.
190 The following %-sequences are provided:
191 %v Linux driver version
192 %V APM BIOS version
193 %I APM BIOS status (verbose)
194 %L AC line status (verbose)
195 %B Battery status (verbose)
196 %b Battery status, empty means high, `-' means low,
197 `!' means critical, and `+' means charging
198 %p Battery load percentage
199 %s Remaining time in seconds
200 %m Remaining time in minutes
201 %h Remaining time in hours
202 %t Remaining time in the form `h:min'"
203 (let (driver-version bios-version bios-interface line-status
204 battery-status battery-status-symbol load-percentage
205 seconds minutes hours remaining-time tem)
206 (with-temp-buffer
207 (ignore-errors (insert-file-contents "/proc/apm"))
208 (when (re-search-forward battery-linux-proc-apm-regexp)
209 (setq driver-version (match-string 1))
210 (setq bios-version (match-string 2))
211 (setq tem (string-to-number (match-string 3) 16))
212 (if (not (logand tem 2))
213 (setq bios-interface "not supported")
214 (setq bios-interface "enabled")
215 (cond ((logand tem 16) (setq bios-interface "disabled"))
216 ((logand tem 32) (setq bios-interface "disengaged")))
217 (setq tem (string-to-number (match-string 4) 16))
218 (cond ((= tem 0) (setq line-status "off-line"))
219 ((= tem 1) (setq line-status "on-line"))
220 ((= tem 2) (setq line-status "on backup")))
221 (setq tem (string-to-number (match-string 6) 16))
222 (if (= tem 255)
223 (setq battery-status "N/A")
224 (setq tem (string-to-number (match-string 5) 16))
225 (cond ((= tem 0) (setq battery-status "high"
226 battery-status-symbol ""))
227 ((= tem 1) (setq battery-status "low"
228 battery-status-symbol "-"))
229 ((= tem 2) (setq battery-status "critical"
230 battery-status-symbol "!"))
231 ((= tem 3) (setq battery-status "charging"
232 battery-status-symbol "+")))
233 (setq load-percentage (match-string 7))
234 (setq seconds (string-to-number (match-string 8)))
235 (and (string-equal (match-string 9) "min")
236 (setq seconds (* 60 seconds)))
237 (setq minutes (/ seconds 60)
238 hours (/ seconds 3600))
239 (setq remaining-time
240 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
241 (list (cons ?v (or driver-version "N/A"))
242 (cons ?V (or bios-version "N/A"))
243 (cons ?I (or bios-interface "N/A"))
244 (cons ?L (or line-status "N/A"))
245 (cons ?B (or battery-status "N/A"))
246 (cons ?b (or battery-status-symbol ""))
247 (cons ?p (or load-percentage "N/A"))
248 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
249 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
250 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
251 (cons ?t (or remaining-time "N/A")))))
254 ;;; `/proc/acpi/' interface for Linux.
256 (defun battery-linux-proc-acpi ()
257 "Get ACPI status information from Linux kernel.
258 This function works only with the new `/proc/acpi/' format introduced
259 in Linux version 2.4.20 and 2.6.0.
261 The following %-sequences are provided:
262 %c Current capacity (mAh)
263 %r Current rate
264 %B Battery status (verbose)
265 %b Battery status, empty means high, `-' means low,
266 `!' means critical, and `+' means charging
267 %d Temperature (in degrees Celsius)
268 %L AC line status (verbose)
269 %p Battery load percentage
270 %m Remaining time in minutes
271 %h Remaining time in hours
272 %t Remaining time in the form `h:min'"
273 (let ((design-capacity 0)
274 (last-full-capacity 0)
275 full-capacity
276 (warn 0)
277 (low 0)
278 capacity rate rate-type charging-state minutes hours)
279 ;; ACPI provides information about each battery present in the system in
280 ;; a separate subdirectory. We are going to merge the available
281 ;; information together since displaying for a variable amount of
282 ;; batteries seems overkill for format-strings.
283 (with-temp-buffer
284 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
285 t "\\`[^.]")))
286 (erase-buffer)
287 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
288 (when (re-search-forward "present: +yes$" nil t)
289 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
290 (member charging-state '("unknown" nil))
291 ;; On most multi-battery systems, most of the time only one
292 ;; battery is "charging"/"discharging", the others are
293 ;; "unknown".
294 (setq charging-state (match-string 1)))
295 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
296 nil t)
297 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
298 rate-type (or (and rate-type
299 (if (string= rate-type (match-string 2))
300 rate-type
301 (error
302 "Inconsistent rate types (%s vs. %s)"
303 rate-type (match-string 2))))
304 (match-string 2))))
305 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
306 nil t)
307 (setq capacity
308 (+ (or capacity 0) (string-to-number (match-string 1))))))
309 (goto-char (point-max))
310 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
311 (when (re-search-forward "present: +yes$" nil t)
312 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
313 nil t)
314 (incf design-capacity (string-to-number (match-string 1))))
315 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
316 nil t)
317 (incf last-full-capacity (string-to-number (match-string 1))))
318 (when (re-search-forward
319 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
320 (incf warn (string-to-number (match-string 1))))
321 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
322 nil t)
323 (incf low (string-to-number (match-string 1)))))))
324 (setq full-capacity (if (> last-full-capacity 0)
325 last-full-capacity design-capacity))
326 (and capacity rate
327 (setq minutes (if (zerop rate) 0
328 (floor (* (/ (float (if (string= charging-state
329 "charging")
330 (- full-capacity capacity)
331 capacity))
332 rate)
333 60)))
334 hours (/ minutes 60)))
335 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
336 (cons ?L (or (when (file-exists-p "/proc/acpi/ac_adapter/AC/state")
337 (with-temp-buffer
338 (insert-file-contents
339 "/proc/acpi/ac_adapter/AC/state")
340 (when (re-search-forward "state: +\\(.*\\)$" nil t)
341 (match-string 1))))
342 "N/A"))
343 (cons ?d (or (when (file-exists-p
344 "/proc/acpi/thermal_zone/THRM/temperature")
345 (with-temp-buffer
346 (insert-file-contents
347 "/proc/acpi/thermal_zone/THRM/temperature")
348 (when (re-search-forward
349 "temperature: +\\([0-9]+\\) C$" nil t)
350 (match-string 1))))
351 (when (file-exists-p
352 "/proc/acpi/thermal_zone/THM/temperature")
353 (with-temp-buffer
354 (insert-file-contents
355 "/proc/acpi/thermal_zone/THM/temperature")
356 (when (re-search-forward
357 "temperature: +\\([0-9]+\\) C$" nil t)
358 (match-string 1))))
359 (when (file-exists-p
360 "/proc/acpi/thermal_zone/THM0/temperature")
361 (with-temp-buffer
362 (insert-file-contents
363 "/proc/acpi/thermal_zone/THM0/temperature")
364 (when (re-search-forward
365 "temperature: +\\([0-9]+\\) C$" nil t)
366 (match-string 1))))
367 "N/A"))
368 (cons ?r (or (and rate (concat (number-to-string rate) " "
369 rate-type)) "N/A"))
370 (cons ?B (or charging-state "N/A"))
371 (cons ?b (or (and (string= charging-state "charging") "+")
372 (and (< capacity low) "!")
373 (and (< capacity warn) "-")
374 ""))
375 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
376 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
377 (cons ?t (or (and minutes
378 (format "%d:%02d" hours (- minutes (* 60 hours))))
379 "N/A"))
380 (cons ?p (or (and full-capacity capacity
381 (number-to-string
382 (floor (/ capacity
383 (/ (float full-capacity) 100)))))
384 "N/A")))))
387 ;;; `pmset' interface for Darwin (OS X).
389 (defun battery-pmset ()
390 "Get battery status information using `pmset'.
392 The following %-sequences are provided:
393 %L Power source (verbose)
394 %B Battery status (verbose)
395 %b Battery status, empty means high, `-' means low,
396 `!' means critical, and `+' means charging
397 %p Battery load percentage
398 %h Remaining time in hours
399 %m Remaining time in minutes
400 %t Remaining time in the form `h:min'"
401 (let (power-source load-percentage battery-status battery-status-symbol
402 remaining-time hours minutes)
403 (with-temp-buffer
404 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
405 (goto-char (point-min))
406 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
407 (setq power-source (match-string 1))
408 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
409 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
410 (setq load-percentage (match-string 1))
411 (goto-char (match-end 0))
412 (cond ((looking-at "; charging")
413 (setq battery-status "charging"
414 battery-status-symbol "+"))
415 ((< (string-to-number load-percentage) battery-load-low)
416 (setq battery-status "low"
417 battery-status-symbol "-"))
418 ((< (string-to-number load-percentage) battery-load-critical)
419 (setq battery-status "critical"
420 battery-status-symbol "!"))
422 (setq battery-status "high"
423 battery-status-symbol "")))
424 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
425 (setq remaining-time (match-string 1))
426 (let ((h (string-to-number (match-string 2)))
427 (m (string-to-number (match-string 3))))
428 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
429 minutes (number-to-string (+ (* h 60) m)))))))))
430 (list (cons ?L (or power-source "N/A"))
431 (cons ?p (or load-percentage "N/A"))
432 (cons ?B (or battery-status "N/A"))
433 (cons ?b (or battery-status-symbol ""))
434 (cons ?h (or hours "N/A"))
435 (cons ?m (or minutes "N/A"))
436 (cons ?t (or remaining-time "N/A")))))
439 ;;; Private functions.
441 (defun battery-format (format alist)
442 "Substitute %-sequences in FORMAT."
443 (replace-regexp-in-string
444 "%."
445 (lambda (str)
446 (let ((char (aref str 1)))
447 (if (eq char ?%) "%"
448 (or (cdr (assoc char alist)) ""))))
449 format t t))
452 (provide 'battery)
454 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
455 ;;; battery.el ends here