(all): Check if symbol is void.
[emacs.git] / lisp / battery.el
blob69bd68bb0b91c17a9f758e3f852fb95bf47d335e
1 ;;; battery.el --- display battery status information
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
6 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
7 ;; Keywords: hardware
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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.
32 ;;; Code:
34 (require 'timer)
35 (eval-when-compile (require 'cl))
38 (defgroup battery nil
39 "Display battery status information."
40 :prefix "battery-"
41 :group 'hardware)
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
54 (CONVERSION . REPLACEMENT-TEXT)
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)
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)
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)
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)
91 (defcustom battery-update-interval 60
92 "*Seconds after which the battery status will be updated."
93 :type 'integer
94 :group 'battery)
96 (defvar battery-update-timer nil
97 "Interval timer object.")
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")))
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
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)))
129 (defun battery-update-handler ()
130 (battery-update)
131 (sit-for 0))
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))
142 'help-echo "Battery status information"))
143 (force-mode-line-update))
146 ;;; `/proc/apm' interface for Linux.
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'.")
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.
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 buffer tem)
182 (unwind-protect
183 (save-excursion
184 (setq buffer (get-buffer-create " *battery*"))
185 (set-buffer buffer)
186 (erase-buffer)
187 (insert-file-contents "/proc/apm")
188 (re-search-forward battery-linux-proc-apm-regexp)
189 (setq driver-version (match-string 1))
190 (setq bios-version (match-string 2))
191 (setq tem (string-to-number (match-string 3) 16))
192 (if (not (logand tem 2))
193 (setq bios-interface "not supported")
194 (setq bios-interface "enabled")
195 (cond ((logand tem 16) (setq bios-interface "disabled"))
196 ((logand tem 32) (setq bios-interface "disengaged")))
197 (setq tem (string-to-number (match-string 4) 16))
198 (cond ((= tem 0) (setq line-status "off-line"))
199 ((= tem 1) (setq line-status "on-line"))
200 ((= tem 2) (setq line-status "on backup")))
201 (setq tem (string-to-number (match-string 6) 16))
202 (if (= tem 255)
203 (setq battery-status "N/A")
204 (setq tem (string-to-number (match-string 5) 16))
205 (cond ((= tem 0) (setq battery-status "high"
206 battery-status-symbol ""))
207 ((= tem 1) (setq battery-status "low"
208 battery-status-symbol "-"))
209 ((= tem 2) (setq battery-status "critical"
210 battery-status-symbol "!"))
211 ((= tem 3) (setq battery-status "charging"
212 battery-status-symbol "+")))
213 (setq load-percentage (match-string 7))
214 (setq seconds (string-to-number (match-string 8)))
215 (and (string-equal (match-string 9) "min")
216 (setq seconds (* 60 seconds)))
217 (setq minutes (/ seconds 60)
218 hours (/ seconds 3600))
219 (setq remaining-time
220 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
221 (list (cons ?v (or driver-version "N/A"))
222 (cons ?V (or bios-version "N/A"))
223 (cons ?I (or bios-interface "N/A"))
224 (cons ?L (or line-status "N/A"))
225 (cons ?B (or battery-status "N/A"))
226 (cons ?b (or battery-status-symbol ""))
227 (cons ?p (or load-percentage "N/A"))
228 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
229 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
230 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
231 (cons ?t (or remaining-time "N/A")))))
234 ;;; `/proc/acpi/' interface for Linux.
236 (defun battery-linux-proc-acpi ()
237 "Get ACPI status information from Linux kernel.
238 This function works only with the new `/proc/acpi/' format introduced
239 in Linux version 2.4.20 and 2.6.0.
241 The following %-sequences are provided:
242 %c Current capacity (mAh)
243 %B Battery status (verbose)
244 %b Battery status, empty means high, `-' means low,
245 `!' means critical, and `+' means charging
246 %d Temperature (in degrees Celsius)
247 %L AC line status (verbose)
248 %p battery load percentage
249 %m Remaining time in minutes
250 %h Remaining time in hours
251 %t Remaining time in the form `h:min'"
252 (let ((design-capacity 0)
253 (warn 0)
254 (low 0)
255 capacity rate rate-type charging-state minutes hours)
256 ;; ACPI provides information about each battery present in the system in
257 ;; a separate subdirectory. We are going to merge the available
258 ;; information together since displaying for a variable amount of
259 ;; batteries seems overkill for format-strings.
260 (with-temp-buffer
261 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
262 t "\\`[^.]")))
263 (erase-buffer)
264 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
265 (when (re-search-forward "present: +yes$" nil t)
266 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
267 (member charging-state '("unknown" nil))
268 ;; On most multi-battery systems, most of the time only one
269 ;; battery is "charging"/"discharging", the others are
270 ;; "unknown".
271 (setq charging-state (match-string 1)))
272 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
273 nil t)
274 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
275 rate-type (or (and rate-type
276 (if (string= rate-type (match-string 2))
277 rate-type
278 (error
279 "Inconsistent rate types (%s vs. %s)"
280 rate-type (match-string 2))))
281 (match-string 2))))
282 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
283 nil t)
284 (setq capacity
285 (+ (or capacity 0) (string-to-number (match-string 1))))))
286 (goto-char (point-max))
287 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
288 (when (re-search-forward "present: +yes$" nil t)
289 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
290 nil t)
291 (incf design-capacity (string-to-number (match-string 1))))
292 (when (re-search-forward
293 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
294 (incf warn (string-to-number (match-string 1))))
295 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
296 nil t)
297 (incf low (string-to-number (match-string 1)))))))
298 (and capacity rate
299 (setq minutes (if (zerop rate) 0
300 (floor (* (/ (float (if (string= charging-state
301 "charging")
302 (- design-capacity capacity)
303 capacity)) rate) 60)))
304 hours (/ minutes 60)))
305 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
306 (cons ?L (or (when (file-exists-p "/proc/acpi/ac_adapter/AC/state")
307 (with-temp-buffer
308 (insert-file-contents
309 "/proc/acpi/ac_adapter/AC/state")
310 (when (re-search-forward "state: +\\(.*\\)$" nil t)
311 (match-string 1))))
312 "N/A"))
313 (cons ?d (or (when (file-exists-p
314 "/proc/acpi/thermal_zone/THRM/temperature")
315 (with-temp-buffer
316 (insert-file-contents
317 "/proc/acpi/thermal_zone/THRM/temperature")
318 (when (re-search-forward
319 "temperature: +\\([0-9]+\\) C$" nil t)
320 (match-string 1))))
321 (when (file-exists-p
322 "/proc/acpi/thermal_zone/THM/temperature")
323 (with-temp-buffer
324 (insert-file-contents
325 "/proc/acpi/thermal_zone/THM/temperature")
326 (when (re-search-forward
327 "temperature: +\\([0-9]+\\) C$" nil t)
328 (match-string 1))))
329 "N/A"))
330 (cons ?r (or (and rate (concat (number-to-string rate) " "
331 rate-type)) "N/A"))
332 (cons ?B (or charging-state "N/A"))
333 (cons ?b (or (and (string= charging-state "charging") "+")
334 (and (< capacity low) "!")
335 (and (< capacity warn) "-")
336 ""))
337 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
338 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
339 (cons ?t (or (and minutes
340 (format "%d:%02d" hours (- minutes (* 60 hours))))
341 "N/A"))
342 (cons ?p (or (and design-capacity capacity
343 (number-to-string
344 (floor (/ capacity
345 (/ (float design-capacity) 100)))))
346 "N/A")))))
349 ;;; Private functions.
351 (defun battery-format (format alist)
352 "Substitute %-sequences in FORMAT."
353 (replace-regexp-in-string
354 "%."
355 (lambda (str)
356 (let ((char (aref str 1)))
357 (if (eq char ?%) "%"
358 (or (cdr (assoc char alist)) ""))))
359 format t t))
362 (provide 'battery)
364 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
365 ;;; battery.el ends here