(Info-last-search): Variable removed.
[bpt/emacs.git] / lisp / battery.el
CommitLineData
6b279740
RS
1;;; battery.el --- display battery status information.
2
d5323ba0 3;; Copyright (C) 1997, 1998 Free Software Foundation, Inc.
6b279740
RS
4
5;; Author: Ralph Schleicher <rs@purple.UL.BaWue.DE>
d5323ba0 6;; Keywords: hardware
6b279740 7
f12b0b96 8;; This file is part of GNU Emacs.
6b279740 9
f12b0b96 10;; GNU Emacs is free software; you can redistribute it and/or modify
6b279740
RS
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.
14
f12b0b96 15;; GNU Emacs is distributed in the hope that it will be useful,
6b279740
RS
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.
19
20;; You should have received a copy of the GNU General Public License
f12b0b96
RS
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
6b279740
RS
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; There is at present only a function interpreting the new `/proc/apm'
28;; file format of Linux version 1.3.58 or newer. That is, what a lucky
29;; coincidence, exactly the interface provided by the author's labtop.
30
31;;; Code:
32
33(require 'timer)
34
35\f
4bef9110
SE
36
37(defgroup battery nil
38 "Display battery status information."
39 :prefix "battery-"
40 :group 'hardware)
41
42(defcustom battery-status-function
6b279740
RS
43 (cond ((and (eq system-type 'gnu/linux)
44 (file-readable-p "/proc/apm"))
4bef9110 45 'battery-linux-proc-apm))
6b279740
RS
46 "*Function for getting battery status information.
47The function have to return an alist of conversion definitions.
48Cons cells are of the form
49
50 (CONVERSION . REPLACEMENT-TEXT)
51
52CONVERSION is the character code of a \"conversion specification\"
4bef9110
SE
53introduced by a `%' character in a control string."
54 :type 'function
55 :group 'battery)
6b279740 56
4bef9110 57(defcustom battery-echo-area-format
6b279740
RS
58 (cond ((eq battery-status-function 'battery-linux-proc-apm)
59 "Power %L, battery %B (%p%% load, remaining time %t)"))
60 "*Control string formatting the string to display in the echo area.
61Ordinary characters in the control string are printed as-is, while
62conversion specifications introduced by a `%' character in the control
63string are substituted as defined by the current value of the variable
4bef9110
SE
64`battery-status-function'."
65 :type '(choice string (const nil))
66 :group 'battery)
6b279740
RS
67
68(defvar battery-mode-line-string nil
69 "String to display in the mode line.")
70
4bef9110 71(defcustom battery-mode-line-format
6b279740
RS
72 (cond ((eq battery-status-function 'battery-linux-proc-apm)
73 " [%b%p%%]"))
74 "*Control string formatting the string to display in the mode line.
75Ordinary characters in the control string are printed as-is, while
76conversion specifications introduced by a `%' character in the control
77string are substituted as defined by the current value of the variable
4bef9110
SE
78`battery-status-function'."
79 :type '(choice string (const nil))
80 :group 'battery)
81
82(defcustom battery-update-interval 60
83 "*Seconds after which the battery status will be updated."
84 :type 'integer
85 :group 'battery)
6b279740
RS
86
87(defvar battery-update-timer nil
88 "Interval timer object.")
89
1720e508 90;;;###autoload
6b279740
RS
91(defun battery ()
92 "Display battery status information in the echo area.
b624447c 93The text being displayed in the echo area is controlled by the variables
6b279740
RS
94`battery-echo-area-format' and `battery-status-function'."
95 (interactive)
96 (message "%s" (if (and battery-echo-area-format battery-status-function)
97 (battery-format battery-echo-area-format
98 (funcall battery-status-function))
99 "Battery status not available")))
100
1720e508 101;;;###autoload
6b279740
RS
102(defun display-battery ()
103 "Display battery status information in the mode line.
104The text beeing displayed in the mode line is controlled by the variables
105`battery-mode-line-format' and `battery-status-function'.
106The mode line will be updated automatically every `battery-update-interval'
107seconds."
108 (interactive)
109 (setq battery-mode-line-string "")
110 (or global-mode-string (setq global-mode-string '("")))
111 (or (memq 'battery-mode-line-string global-mode-string)
112 (setq global-mode-string (append global-mode-string
113 '(battery-mode-line-string))))
114 (and battery-update-timer (cancel-timer battery-update-timer))
115 (setq battery-update-timer (run-at-time nil battery-update-interval
116 'battery-update-handler))
117 (battery-update))
118
119(defun battery-update-handler ()
120 (battery-update)
121 (sit-for 0))
122
123(defun battery-update ()
124 "Update battery status information in the mode line."
125 (setq battery-mode-line-string (if (and battery-mode-line-format
126 battery-status-function)
127 (battery-format
128 battery-mode-line-format
129 (funcall battery-status-function))
130 ""))
131 (force-mode-line-update))
132
133\f
134;;; `/proc/apm' interface for Linux.
135
136(defconst battery-linux-proc-apm-regexp
137 (concat "^\\([^ ]+\\)" ; Driver version.
138 " \\([^ ]+\\)" ; APM BIOS version.
139 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
140 " 0x\\([0-9a-f]+\\)" ; AC line status.
141 " 0x\\([0-9a-f]+\\)" ; Battery status.
142 " 0x\\([0-9a-f]+\\)" ; Battery flags.
d5323ba0
KH
143 " \\(-?[0-9]+\\)%" ; Load percentage.
144 " \\(-?[0-9]+\\)" ; Remaining time.
6b279740
RS
145 " \\(.*\\)" ; Time unit.
146 "$")
147 "Regular expression matching contents of `/proc/apm'.")
148
149(defun battery-linux-proc-apm ()
150 "Get APM status information from Linux kernel.
151This function works only with the new `/proc/apm' format introduced
152in Linux version 1.3.58.
153
154The following %-sequences are provided:
155%v Linux driver version
156%V APM BIOS version
157%I APM BIOS status (verbose)
158%L AC line status (verbose)
159%B Battery status (verbose)
160%b Battery status, empty means high, `-' means low,
161 `!' means critical, and `+' means charging
162%p battery load percentage
163%s Remaining time in seconds
164%m Remaining time in minutes
165%h Remaining time in hours
166%t Remaining time in the form `h:min'"
167 (let (driver-version bios-version bios-interface line-status
168 battery-status battery-status-symbol load-percentage
169 seconds minutes hours remaining-time buffer tem)
170 (unwind-protect
171 (save-excursion
3691e056 172 (setq buffer (get-buffer-create " *battery*"))
6b279740 173 (set-buffer buffer)
3691e056 174 (erase-buffer)
6b279740
RS
175 (battery-insert-file-contents "/proc/apm")
176 (re-search-forward battery-linux-proc-apm-regexp)
177 (setq driver-version (match-string 1))
178 (setq bios-version (match-string 2))
179 (setq tem (battery-hex-to-int-2 (match-string 3)))
180 (if (not (logand tem 2))
181 (setq bios-interface "not supported")
182 (setq bios-interface "enabled")
183 (cond ((logand tem 16) (setq bios-interface "disabled"))
184 ((logand tem 32) (setq bios-interface "disengaged")))
185 (setq tem (battery-hex-to-int-2 (match-string 4)))
186 (cond ((= tem 0) (setq line-status "off-line"))
187 ((= tem 1) (setq line-status "on-line"))
188 ((= tem 2) (setq line-status "on backup")))
189 (setq tem (battery-hex-to-int-2 (match-string 6)))
190 (if (= tem 255)
191 (setq battery-status "N/A")
192 (setq tem (battery-hex-to-int-2 (match-string 5)))
193 (cond ((= tem 0) (setq battery-status "high"
194 battery-status-symbol ""))
195 ((= tem 1) (setq battery-status "low"
196 battery-status-symbol "-"))
197 ((= tem 2) (setq battery-status "critical"
198 battery-status-symbol "!"))
199 ((= tem 3) (setq battery-status "charging"
200 battery-status-symbol "+")))
201 (setq load-percentage (match-string 7))
202 (setq seconds (string-to-number (match-string 8)))
203 (and (string-equal (match-string 9) "min")
204 (setq seconds (* 60 seconds)))
205 (setq minutes (/ seconds 60)
206 hours (/ seconds 3600))
207 (setq remaining-time
3691e056 208 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
d5323ba0
KH
209 (list (cons ?v (or driver-version "N/A"))
210 (cons ?V (or bios-version "N/A"))
211 (cons ?I (or bios-interface "N/A"))
212 (cons ?L (or line-status "N/A"))
213 (cons ?B (or battery-status "N/A"))
214 (cons ?b (or battery-status-symbol ""))
215 (cons ?p (or load-percentage "N/A"))
216 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
217 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
218 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
219 (cons ?t (or remaining-time "N/A")))))
6b279740
RS
220
221\f
222;;; Private functions.
223
224(defun battery-format (format alist)
225 "Substitute %-sequences in FORMAT."
226 (let ((index 0)
227 (length (length format))
228 (result "")
229 char flag elem)
230 (while (< index length)
231 (setq char (aref format index))
232 (if (not flag)
233 (if (char-equal char ?%)
234 (setq flag t)
235 (setq result (concat result (char-to-string char))))
236 (cond ((char-equal char ?%)
237 (setq result (concat result "%")))
238 ((setq elem (assoc char alist))
239 (setq result (concat result (cdr elem)))))
240 (setq flag nil))
241 (setq index (1+ index)))
242 (or (null flag)
243 (setq result (concat result "%")))
244 result))
245
246(defun battery-insert-file-contents (file-name)
247 "Insert contents of file FILE-NAME after point.
248FILE-NAME can be a non-ordinary file, for example, a named pipe.
249Return t if file exists."
250 (let ((load-read-function 'battery-read-function)
3691e056 251 (load-source-file-function nil)
6b279740
RS
252 (load-path '("."))
253 (load-history nil))
254 (save-excursion
255 (load file-name nil t t))))
256
257(defun battery-read-function (&optional stream)
258 "Function for reading expressions from STREAM.
259Value is always nil."
260 (let (char)
261 (while (not (< (setq char (get-file-char)) 0))
262 (insert char))))
263
264(defconst battery-hex-map '((?0 . 0) (?1 . 1) (?2 . 2) (?3 . 3)
265 (?4 . 4) (?5 . 5) (?6 . 6) (?7 . 7)
266 (?8 . 8) (?9 . 9) (?a . 10) (?b . 11)
267 (?c . 12) (?d . 13) (?e . 14) (?f . 15)))
268
269(defun battery-hex-to-int (string)
270 "Convert a hexadecimal number (a string) into a number."
271 (save-match-data
272 (and (string-match "^[ \t]+" string)
273 (setq string (substring string (match-end 0))))
274 (and (string-match "^0[xX]" string)
275 (setq string (substring string (match-end 0)))))
276 (battery-hex-to-int-2 string))
277
278(defun battery-hex-to-int-2 (string)
279 (let ((index 0)
280 (length (length string))
281 (value 0)
282 (elem nil))
283 (while (and (< index length)
284 (setq elem (assoc (downcase (aref string index))
285 battery-hex-map)))
286 (setq value (+ (* 16 value) (cdr elem))
287 index (1+ index)))
288 value))
289
290\f
291(provide 'battery)
292
293;;; battery.el ends here