aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2003-09-06 19:28:08 +0000
committerEli Zaretskii2003-09-06 19:28:08 +0000
commit2b3cb60aab00e6002556d63b33417522daec6418 (patch)
tree953515dc544f6c158f5ea0a9381146ae83474158
parent73804d02065ccc01eabd3c2dcc4b2b6c404b3f6d (diff)
downloademacs-2b3cb60aab00e6002556d63b33417522daec6418.tar.gz
emacs-2b3cb60aab00e6002556d63b33417522daec6418.zip
(battery-linux-proc-acpi): New function.
(battery-status-function): Modified default value calculation to also check for availability of ACPI. (battery-echo-area-format): Ditto. (battery-mode-line-format): Ditto.
-rw-r--r--lisp/battery.el111
1 files changed, 108 insertions, 3 deletions
diff --git a/lisp/battery.el b/lisp/battery.el
index 1ac157ccbf2..068f1ab50d5 100644
--- a/lisp/battery.el
+++ b/lisp/battery.el
@@ -42,7 +42,10 @@
42(defcustom battery-status-function 42(defcustom battery-status-function
43 (cond ((and (eq system-type 'gnu/linux) 43 (cond ((and (eq system-type 'gnu/linux)
44 (file-readable-p "/proc/apm")) 44 (file-readable-p "/proc/apm"))
45 'battery-linux-proc-apm)) 45 'battery-linux-proc-apm)
46 ((and (eq system-type 'gnu/linux)
47 (file-directory-p "/proc/acpi/battery"))
48 'battery-linux-proc-acpi))
46 "*Function for getting battery status information. 49 "*Function for getting battery status information.
47The function has to return an alist of conversion definitions. 50The function has to return an alist of conversion definitions.
48Its cons cells are of the form 51Its cons cells are of the form
@@ -56,7 +59,9 @@ introduced by a `%' character in a control string."
56 59
57(defcustom battery-echo-area-format 60(defcustom battery-echo-area-format
58 (cond ((eq battery-status-function 'battery-linux-proc-apm) 61 (cond ((eq battery-status-function 'battery-linux-proc-apm)
59 "Power %L, battery %B (%p%% load, remaining time %t)")) 62 "Power %L, battery %B (%p%% load, remaining time %t)")
63 ((eq battery-status-function 'battery-linux-proc-acpi)
64 "Power %L, battery %B at %r mA (%p%% load, remaining time %t)"))
60 "*Control string formatting the string to display in the echo area. 65 "*Control string formatting the string to display in the echo area.
61Ordinary characters in the control string are printed as-is, while 66Ordinary characters in the control string are printed as-is, while
62conversion specifications introduced by a `%' character in the control 67conversion specifications introduced by a `%' character in the control
@@ -70,7 +75,9 @@ string are substituted as defined by the current value of the variable
70 75
71(defcustom battery-mode-line-format 76(defcustom battery-mode-line-format
72 (cond ((eq battery-status-function 'battery-linux-proc-apm) 77 (cond ((eq battery-status-function 'battery-linux-proc-apm)
73 " [%b%p%%]")) 78 " [%b%p%%]")
79 ((eq battery-status-function 'battery-linux-proc-acpi)
80 " [%b%p%%,%d°C]"))
74 "*Control string formatting the string to display in the mode line. 81 "*Control string formatting the string to display in the mode line.
75Ordinary characters in the control string are printed as-is, while 82Ordinary characters in the control string are printed as-is, while
76conversion specifications introduced by a `%' character in the control 83conversion specifications introduced by a `%' character in the control
@@ -218,6 +225,104 @@ The following %-sequences are provided:
218 (cons ?t (or remaining-time "N/A"))))) 225 (cons ?t (or remaining-time "N/A")))))
219 226
220 227
228;;; `/proc/acpi/' interface for Linux.
229
230(defun battery-linux-proc-acpi ()
231 "Get ACPI status information from Linux kernel.
232This function works only with the new `/proc/acpi/' format introduced
233in Linux version 2.4.20 and 2.6.0.
234
235The following %-sequences are provided:
236%c Current capacity (mAh)
237%B Battery status (verbose)
238%b Battery status, empty means high, `-' means low,
239 `!' means critical, and `+' means charging
240%d Temperature (in degrees Celsius)
241%L AC line status (verbose)
242%p battery load percentage
243%m Remaining time in minutes
244%h Remaining time in hours
245%t Remaining time in the form `h:min'"
246 (let (capacity design-capacity rate charging-state warn low minutes hours)
247 (when (file-directory-p "/proc/acpi/battery/")
248 ;; ACPI provides information about each battery present in the system in
249 ;; a separate subdirectory. We are going to merge the available
250 ;; information together since displaying for a variable amount of
251 ;; batteries seems overkill for format-strings.
252 (mapc
253 (lambda (dir)
254 (with-temp-buffer
255 (insert-file-contents (expand-file-name "state" dir))
256 (when (re-search-forward "present: +yes$" nil t)
257 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
258 (or (null charging-state) (string= charging-state
259 "unknown"))
260 ;; On most multi-battery systems, most of the time only one
261 ;; battery is "charging"/"discharging", the others are
262 ;; "unknown".
263 (setq charging-state (match-string 1)))
264 (when (re-search-forward "present rate: +\\([0-9]+\\) mA$" nil t)
265 (setq rate (+ (or rate 0) (string-to-int (match-string 1)))))
266 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) mAh$"
267 nil t)
268 (setq capacity
269 (+ (or capacity 0) (string-to-int (match-string 1))))))
270 (goto-char (point-max))
271 (insert-file-contents (expand-file-name "info" dir))
272 (when (re-search-forward "present: +yes$" nil t)
273 (when (re-search-forward "design capacity: +\\([0-9]+\\) mAh$"
274 nil t)
275 (setq design-capacity (+ (or design-capacity 0)
276 (string-to-int (match-string 1)))))
277 (when (re-search-forward "design capacity warning: +\\([0-9]+\\) mAh$"
278 nil t)
279 (setq warn (+ (or warn 0) (string-to-int (match-string 1)))))
280 (when (re-search-forward "design capacity low: +\\([0-9]+\\) mAh$"
281 nil t)
282 (setq low (+ (or low 0)
283 (string-to-int (match-string 1))))))))
284 (directory-files "/proc/acpi/battery/" t "BAT")))
285 (and capacity rate
286 (setq minutes (floor (* (/ (float (if (string= charging-state
287 "charging")
288 (- design-capacity capacity)
289 capacity)) rate) 60))
290 hours (/ minutes 60)))
291 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
292 (cons ?L (or (when (file-exists-p "/proc/acpi/ac_adapter/AC/state")
293 (with-temp-buffer
294 (insert-file-contents
295 "/proc/acpi/ac_adapter/AC/state")
296 (when (re-search-forward "state: +\\(.*\\)$" nil t)
297 (match-string 1))))
298 "N/A"))
299 (cons ?d (or (when (file-exists-p
300 "/proc/acpi/thermal_zone/THRM/temperature")
301 (with-temp-buffer
302 (insert-file-contents
303 "/proc/acpi/thermal_zone/THRM/temperature")
304 (when (re-search-forward
305 "temperature: +\\([0-9]+\\) C$" nil t)
306 (match-string 1))))
307 "N/A"))
308 (cons ?r (or (and rate (number-to-string rate)) "N/A"))
309 (cons ?B (or charging-state "N/A"))
310 (cons ?b (or (and (string= charging-state "charging") "+")
311 (and warn (< capacity warn) "-")
312 (and low (< capacity low) "!")
313 ""))
314 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
315 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
316 (cons ?t (or (and minutes
317 (format "%d:%02d" hours (- minutes (* 60 hours))))
318 "N/A"))
319 (cons ?p (or (and design-capacity capacity
320 (number-to-string
321 (floor (/ capacity
322 (/ (float design-capacity) 100)))))
323 "N/A")))))
324
325
221;;; Private functions. 326;;; Private functions.
222 327
223(defun battery-format (format alist) 328(defun battery-format (format alist)