(battery-mode-line-format): Remove initial spaces.
[bpt/emacs.git] / 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 ;;;###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 (defun display-battery ()
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 (interactive)
118 (setq battery-mode-line-string "")
119 (or global-mode-string (setq global-mode-string '("")))
120 (add-to-list 'global-mode-string 'battery-mode-line-string t)
121 (and battery-update-timer (cancel-timer battery-update-timer))
122 (setq battery-update-timer (run-at-time nil battery-update-interval
123 'battery-update-handler))
124 (battery-update))
125
126 (defun battery-update-handler ()
127 (battery-update)
128 (sit-for 0))
129
130 (defun battery-update ()
131 "Update battery status information in the mode line."
132 (setq battery-mode-line-string
133 (propertize (if (and battery-mode-line-format
134 battery-status-function)
135 (battery-format
136 battery-mode-line-format
137 (funcall battery-status-function))
138 "")
139 'help-echo "Battery status information"))
140 (force-mode-line-update))
141
142 \f
143 ;;; `/proc/apm' interface for Linux.
144
145 (defconst battery-linux-proc-apm-regexp
146 (concat "^\\([^ ]+\\)" ; Driver version.
147 " \\([^ ]+\\)" ; APM BIOS version.
148 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
149 " 0x\\([0-9a-f]+\\)" ; AC line status.
150 " 0x\\([0-9a-f]+\\)" ; Battery status.
151 " 0x\\([0-9a-f]+\\)" ; Battery flags.
152 " \\(-?[0-9]+\\)%" ; Load percentage.
153 " \\(-?[0-9]+\\)" ; Remaining time.
154 " \\(.*\\)" ; Time unit.
155 "$")
156 "Regular expression matching contents of `/proc/apm'.")
157
158 (defun battery-linux-proc-apm ()
159 "Get APM status information from Linux kernel.
160 This function works only with the new `/proc/apm' format introduced
161 in Linux version 1.3.58.
162
163 The following %-sequences are provided:
164 %v Linux driver version
165 %V APM BIOS version
166 %I APM BIOS status (verbose)
167 %L AC line status (verbose)
168 %B Battery status (verbose)
169 %b Battery status, empty means high, `-' means low,
170 `!' means critical, and `+' means charging
171 %p battery load percentage
172 %s Remaining time in seconds
173 %m Remaining time in minutes
174 %h Remaining time in hours
175 %t Remaining time in the form `h:min'"
176 (let (driver-version bios-version bios-interface line-status
177 battery-status battery-status-symbol load-percentage
178 seconds minutes hours remaining-time buffer tem)
179 (unwind-protect
180 (save-excursion
181 (setq buffer (get-buffer-create " *battery*"))
182 (set-buffer buffer)
183 (erase-buffer)
184 (insert-file-contents "/proc/apm")
185 (re-search-forward battery-linux-proc-apm-regexp)
186 (setq driver-version (match-string 1))
187 (setq bios-version (match-string 2))
188 (setq tem (string-to-number (match-string 3) 16))
189 (if (not (logand tem 2))
190 (setq bios-interface "not supported")
191 (setq bios-interface "enabled")
192 (cond ((logand tem 16) (setq bios-interface "disabled"))
193 ((logand tem 32) (setq bios-interface "disengaged")))
194 (setq tem (string-to-number (match-string 4) 16))
195 (cond ((= tem 0) (setq line-status "off-line"))
196 ((= tem 1) (setq line-status "on-line"))
197 ((= tem 2) (setq line-status "on backup")))
198 (setq tem (string-to-number (match-string 6) 16))
199 (if (= tem 255)
200 (setq battery-status "N/A")
201 (setq tem (string-to-number (match-string 5) 16))
202 (cond ((= tem 0) (setq battery-status "high"
203 battery-status-symbol ""))
204 ((= tem 1) (setq battery-status "low"
205 battery-status-symbol "-"))
206 ((= tem 2) (setq battery-status "critical"
207 battery-status-symbol "!"))
208 ((= tem 3) (setq battery-status "charging"
209 battery-status-symbol "+")))
210 (setq load-percentage (match-string 7))
211 (setq seconds (string-to-number (match-string 8)))
212 (and (string-equal (match-string 9) "min")
213 (setq seconds (* 60 seconds)))
214 (setq minutes (/ seconds 60)
215 hours (/ seconds 3600))
216 (setq remaining-time
217 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
218 (list (cons ?v (or driver-version "N/A"))
219 (cons ?V (or bios-version "N/A"))
220 (cons ?I (or bios-interface "N/A"))
221 (cons ?L (or line-status "N/A"))
222 (cons ?B (or battery-status "N/A"))
223 (cons ?b (or battery-status-symbol ""))
224 (cons ?p (or load-percentage "N/A"))
225 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
226 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
227 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
228 (cons ?t (or remaining-time "N/A")))))
229
230 \f
231 ;;; `/proc/acpi/' interface for Linux.
232
233 (defun battery-linux-proc-acpi ()
234 "Get ACPI status information from Linux kernel.
235 This function works only with the new `/proc/acpi/' format introduced
236 in Linux version 2.4.20 and 2.6.0.
237
238 The following %-sequences are provided:
239 %c Current capacity (mAh)
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