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