1 ;;; battery.el --- display battery status information
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2003, 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
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)
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.
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.
35 (eval-when-compile (require 'cl
))
39 "Display battery status information."
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
)
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
))
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
)
81 ((eq battery-status-function
'battery-linux-proc-acpi
)
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
))
91 (defcustom battery-update-interval
60
92 "*Seconds after which the battery status will be updated."
96 (defvar battery-update-timer nil
97 "Interval timer object.")
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'."
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")))
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'
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
))
126 (defun battery-update-handler ()
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
)
136 battery-mode-line-format
137 (funcall battery-status-function
))
139 'help-echo
"Battery status information"))
140 (force-mode-line-update))
143 ;;; `/proc/apm' interface for Linux.
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.
156 "Regular expression matching contents of `/proc/apm'.")
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.
163 The following %-sequences are provided:
164 %v Linux driver 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
)
181 (setq buffer
(get-buffer-create " *battery*"))
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))
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))
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")))))
231 ;;; `/proc/acpi/' interface for Linux.
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.
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)
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.
258 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
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
268 (setq charging-state
(match-string 1)))
269 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
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))
276 "Inconsistent rate types (%s vs. %s)"
277 rate-type
(match-string 2))))
279 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
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$"
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$"
294 (incf low
(string-to-number (match-string 1)))))))
296 (setq minutes
(if (zerop rate
) 0
297 (floor (* (/ (float (if (string= charging-state
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")
305 (insert-file-contents
306 "/proc/acpi/ac_adapter/AC/state")
307 (when (re-search-forward "state: +\\(.*\\)$" nil t
)
310 (cons ?d
(or (when (file-exists-p
311 "/proc/acpi/thermal_zone/THRM/temperature")
313 (insert-file-contents
314 "/proc/acpi/thermal_zone/THRM/temperature")
315 (when (re-search-forward
316 "temperature: +\\([0-9]+\\) C$" nil t
)
319 "/proc/acpi/thermal_zone/THM/temperature")
321 (insert-file-contents
322 "/proc/acpi/thermal_zone/THM/temperature")
323 (when (re-search-forward
324 "temperature: +\\([0-9]+\\) C$" nil t
)
327 (cons ?r
(or (and rate
(concat (number-to-string rate
) " "
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
) "-")
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
))))
339 (cons ?p
(or (and design-capacity capacity
342 (/ (float design-capacity
) 100)))))
346 ;;; Private functions.
348 (defun battery-format (format alist
)
349 "Substitute %-sequences in FORMAT."
350 (replace-regexp-in-string
353 (let ((char (aref str
1)))
355 (or (cdr (assoc char alist
)) ""))))
361 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
362 ;;; battery.el ends here