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