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