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