(diff-default-read-only): Change default.
[emacs.git] / lisp / battery.el
blobc82d3ac02b3c450c782bce24705e0b5193a22721
1 ;;; battery.el --- display battery status information
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
5 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
6 ;; Keywords: hardware
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; There is at present support for interpreting the new `/proc/apm'
28 ;; file format of Linux version 1.3.58 or newer and for the `/proc/acpi/'
29 ;; directory structure of Linux 2.4.20 and 2.6.
31 ;;; Code:
33 (require 'timer)
37 (defgroup battery nil
38 "Display battery status information."
39 :prefix "battery-"
40 :group 'hardware)
42 (defcustom battery-status-function
43 (cond ((and (eq system-type 'gnu/linux)
44 (file-readable-p "/proc/apm"))
45 'battery-linux-proc-apm)
46 ((and (eq system-type 'gnu/linux)
47 (file-directory-p "/proc/acpi/battery"))
48 'battery-linux-proc-acpi))
49 "*Function for getting battery status information.
50 The function has to return an alist of conversion definitions.
51 Its cons cells are of the form
53 (CONVERSION . REPLACEMENT-TEXT)
55 CONVERSION is the character code of a \"conversion specification\"
56 introduced by a `%' character in a control string."
57 :type '(choice (const nil) function)
58 :group 'battery)
60 (defcustom battery-echo-area-format
61 (cond ((eq battery-status-function 'battery-linux-proc-apm)
62 "Power %L, battery %B (%p%% load, remaining time %t)")
63 ((eq battery-status-function 'battery-linux-proc-acpi)
64 "Power %L, battery %B at %r mA (%p%% load, remaining time %t)"))
65 "*Control string formatting the string to display in the echo area.
66 Ordinary characters in the control string are printed as-is, while
67 conversion specifications introduced by a `%' character in the control
68 string are substituted as defined by the current value of the variable
69 `battery-status-function'."
70 :type '(choice string (const nil))
71 :group 'battery)
73 (defvar battery-mode-line-string nil
74 "String to display in the mode line.")
76 (defcustom battery-mode-line-format
77 (cond ((eq battery-status-function 'battery-linux-proc-apm)
78 " [%b%p%%]")
79 ((eq battery-status-function 'battery-linux-proc-acpi)
80 " [%b%p%%,%d°C]"))
81 "*Control string formatting the string to display in the mode line.
82 Ordinary characters in the control string are printed as-is, while
83 conversion specifications introduced by a `%' character in the control
84 string are substituted as defined by the current value of the variable
85 `battery-status-function'."
86 :type '(choice string (const nil))
87 :group 'battery)
89 (defcustom battery-update-interval 60
90 "*Seconds after which the battery status will be updated."
91 :type 'integer
92 :group 'battery)
94 (defvar battery-update-timer nil
95 "Interval timer object.")
97 ;;;###autoload
98 (defun battery ()
99 "Display battery status information in the echo area.
100 The text being displayed in the echo area is controlled by the variables
101 `battery-echo-area-format' and `battery-status-function'."
102 (interactive)
103 (message "%s" (if (and battery-echo-area-format battery-status-function)
104 (battery-format battery-echo-area-format
105 (funcall battery-status-function))
106 "Battery status not available")))
108 ;;;###autoload
109 (defun display-battery ()
110 "Display battery status information in the mode line.
111 The text being displayed in the mode line is controlled by the variables
112 `battery-mode-line-format' and `battery-status-function'.
113 The mode line will be updated automatically every `battery-update-interval'
114 seconds."
115 (interactive)
116 (setq battery-mode-line-string "")
117 (or global-mode-string (setq global-mode-string '("")))
118 (add-to-list 'global-mode-string 'battery-mode-line-string t)
119 (and battery-update-timer (cancel-timer battery-update-timer))
120 (setq battery-update-timer (run-at-time nil battery-update-interval
121 'battery-update-handler))
122 (battery-update))
124 (defun battery-update-handler ()
125 (battery-update)
126 (sit-for 0))
128 (defun battery-update ()
129 "Update battery status information in the mode line."
130 (setq battery-mode-line-string (propertize (if (and battery-mode-line-format
131 battery-status-function)
132 (battery-format
133 battery-mode-line-format
134 (funcall battery-status-function))
136 'help-echo "Battery status information"))
137 (force-mode-line-update))
140 ;;; `/proc/apm' interface for Linux.
142 (defconst battery-linux-proc-apm-regexp
143 (concat "^\\([^ ]+\\)" ; Driver version.
144 " \\([^ ]+\\)" ; APM BIOS version.
145 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
146 " 0x\\([0-9a-f]+\\)" ; AC line status.
147 " 0x\\([0-9a-f]+\\)" ; Battery status.
148 " 0x\\([0-9a-f]+\\)" ; Battery flags.
149 " \\(-?[0-9]+\\)%" ; Load percentage.
150 " \\(-?[0-9]+\\)" ; Remaining time.
151 " \\(.*\\)" ; Time unit.
152 "$")
153 "Regular expression matching contents of `/proc/apm'.")
155 (defun battery-linux-proc-apm ()
156 "Get APM status information from Linux kernel.
157 This function works only with the new `/proc/apm' format introduced
158 in Linux version 1.3.58.
160 The following %-sequences are provided:
161 %v Linux driver version
162 %V APM BIOS version
163 %I APM BIOS status (verbose)
164 %L AC line status (verbose)
165 %B Battery status (verbose)
166 %b Battery status, empty means high, `-' means low,
167 `!' means critical, and `+' means charging
168 %p battery load percentage
169 %s Remaining time in seconds
170 %m Remaining time in minutes
171 %h Remaining time in hours
172 %t Remaining time in the form `h:min'"
173 (let (driver-version bios-version bios-interface line-status
174 battery-status battery-status-symbol load-percentage
175 seconds minutes hours remaining-time buffer tem)
176 (unwind-protect
177 (save-excursion
178 (setq buffer (get-buffer-create " *battery*"))
179 (set-buffer buffer)
180 (erase-buffer)
181 (insert-file-contents "/proc/apm")
182 (re-search-forward battery-linux-proc-apm-regexp)
183 (setq driver-version (match-string 1))
184 (setq bios-version (match-string 2))
185 (setq tem (battery-hex-to-int-2 (match-string 3)))
186 (if (not (logand tem 2))
187 (setq bios-interface "not supported")
188 (setq bios-interface "enabled")
189 (cond ((logand tem 16) (setq bios-interface "disabled"))
190 ((logand tem 32) (setq bios-interface "disengaged")))
191 (setq tem (battery-hex-to-int-2 (match-string 4)))
192 (cond ((= tem 0) (setq line-status "off-line"))
193 ((= tem 1) (setq line-status "on-line"))
194 ((= tem 2) (setq line-status "on backup")))
195 (setq tem (battery-hex-to-int-2 (match-string 6)))
196 (if (= tem 255)
197 (setq battery-status "N/A")
198 (setq tem (battery-hex-to-int-2 (match-string 5)))
199 (cond ((= tem 0) (setq battery-status "high"
200 battery-status-symbol ""))
201 ((= tem 1) (setq battery-status "low"
202 battery-status-symbol "-"))
203 ((= tem 2) (setq battery-status "critical"
204 battery-status-symbol "!"))
205 ((= tem 3) (setq battery-status "charging"
206 battery-status-symbol "+")))
207 (setq load-percentage (match-string 7))
208 (setq seconds (string-to-number (match-string 8)))
209 (and (string-equal (match-string 9) "min")
210 (setq seconds (* 60 seconds)))
211 (setq minutes (/ seconds 60)
212 hours (/ seconds 3600))
213 (setq remaining-time
214 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
215 (list (cons ?v (or driver-version "N/A"))
216 (cons ?V (or bios-version "N/A"))
217 (cons ?I (or bios-interface "N/A"))
218 (cons ?L (or line-status "N/A"))
219 (cons ?B (or battery-status "N/A"))
220 (cons ?b (or battery-status-symbol ""))
221 (cons ?p (or load-percentage "N/A"))
222 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
223 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
224 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
225 (cons ?t (or remaining-time "N/A")))))
228 ;;; `/proc/acpi/' interface for Linux.
230 (defun battery-linux-proc-acpi ()
231 "Get ACPI status information from Linux kernel.
232 This function works only with the new `/proc/acpi/' format introduced
233 in Linux version 2.4.20 and 2.6.0.
235 The following %-sequences are provided:
236 %c Current capacity (mAh)
237 %B Battery status (verbose)
238 %b Battery status, empty means high, `-' means low,
239 `!' means critical, and `+' means charging
240 %d Temperature (in degrees Celsius)
241 %L AC line status (verbose)
242 %p battery load percentage
243 %m Remaining time in minutes
244 %h Remaining time in hours
245 %t Remaining time in the form `h:min'"
246 (let (capacity design-capacity rate charging-state warn low minutes hours)
247 (when (file-directory-p "/proc/acpi/battery/")
248 ;; ACPI provides information about each battery present in the system in
249 ;; a separate subdirectory. We are going to merge the available
250 ;; information together since displaying for a variable amount of
251 ;; batteries seems overkill for format-strings.
252 (mapc
253 (lambda (dir)
254 (with-temp-buffer
255 (insert-file-contents (expand-file-name "state" dir))
256 (when (re-search-forward "present: +yes$" nil t)
257 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
258 (or (null charging-state) (string= charging-state
259 "unknown"))
260 ;; On most multi-battery systems, most of the time only one
261 ;; battery is "charging"/"discharging", the others are
262 ;; "unknown".
263 (setq charging-state (match-string 1)))
264 (when (re-search-forward "present rate: +\\([0-9]+\\) mA$" nil t)
265 (setq rate (+ (or rate 0) (string-to-int (match-string 1)))))
266 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) mAh$"
267 nil t)
268 (setq capacity
269 (+ (or capacity 0) (string-to-int (match-string 1))))))
270 (goto-char (point-max))
271 (insert-file-contents (expand-file-name "info" dir))
272 (when (re-search-forward "present: +yes$" nil t)
273 (when (re-search-forward "design capacity: +\\([0-9]+\\) mAh$"
274 nil t)
275 (setq design-capacity (+ (or design-capacity 0)
276 (string-to-int (match-string 1)))))
277 (when (re-search-forward "design capacity warning: +\\([0-9]+\\) mAh$"
278 nil t)
279 (setq warn (+ (or warn 0) (string-to-int (match-string 1)))))
280 (when (re-search-forward "design capacity low: +\\([0-9]+\\) mAh$"
281 nil t)
282 (setq low (+ (or low 0)
283 (string-to-int (match-string 1))))))))
284 (directory-files "/proc/acpi/battery/" t "BAT")))
285 (and capacity rate
286 (setq minutes (floor (* (/ (float (if (string= charging-state
287 "charging")
288 (- design-capacity capacity)
289 capacity)) rate) 60))
290 hours (/ minutes 60)))
291 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
292 (cons ?L (or (when (file-exists-p "/proc/acpi/ac_adapter/AC/state")
293 (with-temp-buffer
294 (insert-file-contents
295 "/proc/acpi/ac_adapter/AC/state")
296 (when (re-search-forward "state: +\\(.*\\)$" nil t)
297 (match-string 1))))
298 "N/A"))
299 (cons ?d (or (when (file-exists-p
300 "/proc/acpi/thermal_zone/THRM/temperature")
301 (with-temp-buffer
302 (insert-file-contents
303 "/proc/acpi/thermal_zone/THRM/temperature")
304 (when (re-search-forward
305 "temperature: +\\([0-9]+\\) C$" nil t)
306 (match-string 1))))
307 "N/A"))
308 (cons ?r (or (and rate (number-to-string rate)) "N/A"))
309 (cons ?B (or charging-state "N/A"))
310 (cons ?b (or (and (string= charging-state "charging") "+")
311 (and low (< capacity low) "!")
312 (and warn (< capacity warn) "-")
313 ""))
314 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
315 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
316 (cons ?t (or (and minutes
317 (format "%d:%02d" hours (- minutes (* 60 hours))))
318 "N/A"))
319 (cons ?p (or (and design-capacity capacity
320 (number-to-string
321 (floor (/ capacity
322 (/ (float design-capacity) 100)))))
323 "N/A")))))
326 ;;; Private functions.
328 (defun battery-format (format alist)
329 "Substitute %-sequences in FORMAT."
330 (let ((index 0)
331 (length (length format))
332 (result "")
333 char flag elem)
334 (while (< index length)
335 (setq char (aref format index))
336 (if (not flag)
337 (if (char-equal char ?%)
338 (setq flag t)
339 (setq result (concat result (char-to-string char))))
340 (cond ((char-equal char ?%)
341 (setq result (concat result "%")))
342 ((setq elem (assoc char alist))
343 (setq result (concat result (cdr elem)))))
344 (setq flag nil))
345 (setq index (1+ index)))
346 (or (null flag)
347 (setq result (concat result "%")))
348 result))
350 (defconst battery-hex-map '((?0 . 0) (?1 . 1) (?2 . 2) (?3 . 3)
351 (?4 . 4) (?5 . 5) (?6 . 6) (?7 . 7)
352 (?8 . 8) (?9 . 9) (?a . 10) (?b . 11)
353 (?c . 12) (?d . 13) (?e . 14) (?f . 15)))
355 (defun battery-hex-to-int (string)
356 "Convert a hexadecimal number (a string) into a number."
357 (save-match-data
358 (and (string-match "^[ \t]+" string)
359 (setq string (substring string (match-end 0))))
360 (and (string-match "^0[xX]" string)
361 (setq string (substring string (match-end 0)))))
362 (battery-hex-to-int-2 string))
364 (defun battery-hex-to-int-2 (string)
365 (let ((index 0)
366 (length (length string))
367 (value 0)
368 (elem nil))
369 (while (and (< index length)
370 (setq elem (assoc (downcase (aref string index))
371 battery-hex-map)))
372 (setq value (+ (* 16 value) (cdr elem))
373 index (1+ index)))
374 value))
377 (provide 'battery)
379 ;;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
380 ;;; battery.el ends here