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