]> code.delx.au - gnu-emacs/blob - lisp/battery.el
Wrap around error in coreutil's ls
[gnu-emacs] / lisp / battery.el
1 ;;; battery.el --- display battery status information
2
3 ;; Copyright (C) 1997-1998, 2000-2016 Free Software Foundation, Inc.
4
5 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
6 ;; Keywords: hardware
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs 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 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; There is at present support for GNU/Linux, OS X and Windows. This
26 ;; library supports both the `/proc/apm' file format of Linux version
27 ;; 1.3.58 or newer and the `/proc/acpi/' directory structure of Linux
28 ;; 2.4.20 and 2.6. Darwin (OS X) is supported by using the `pmset'
29 ;; program. Windows is supported by the GetSystemPowerStatus API call.
30
31 ;;; Code:
32
33 (require 'timer)
34 (eval-when-compile (require 'cl-lib))
35 \f
36 (defgroup battery nil
37 "Display battery status information."
38 :prefix "battery-"
39 :group 'hardware)
40
41 (defcustom battery-linux-sysfs-regexp "[bB][aA][tT][0-9]?$"
42 "Regexp for folder names to be searched under
43 /sys/class/power_supply/ that contain battery information."
44 :version "25.2"
45 :type 'regexp
46 :group 'battery)
47
48 (defcustom battery-status-function
49 (cond ((and (eq system-type 'gnu/linux)
50 (file-readable-p "/proc/apm"))
51 #'battery-linux-proc-apm)
52 ((and (eq system-type 'gnu/linux)
53 (file-directory-p "/proc/acpi/battery"))
54 #'battery-linux-proc-acpi)
55 ((and (eq system-type 'gnu/linux)
56 (file-directory-p "/sys/class/power_supply/")
57 (directory-files "/sys/class/power_supply/" nil
58 battery-linux-sysfs-regexp))
59 #'battery-linux-sysfs)
60 ((and (eq system-type 'berkeley-unix)
61 (file-executable-p "/usr/sbin/apm"))
62 #'battery-bsd-apm)
63 ((and (eq system-type 'darwin)
64 (condition-case nil
65 (with-temp-buffer
66 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
67 (> (buffer-size) 0)))
68 (error nil)))
69 #'battery-pmset)
70 ((fboundp 'w32-battery-status)
71 #'w32-battery-status))
72 "Function for getting battery status information.
73 The function has to return an alist of conversion definitions.
74 Its cons cells are of the form
75
76 (CONVERSION . REPLACEMENT-TEXT)
77
78 CONVERSION is the character code of a \"conversion specification\"
79 introduced by a `%' character in a control string."
80 :type '(choice (const nil) function)
81 :group 'battery)
82
83 (defcustom battery-echo-area-format
84 "Power %L, battery %B (%p%% load, remaining time %t)"
85 "Control string formatting the string to display in the echo area.
86 Ordinary characters in the control string are printed as-is, while
87 conversion specifications introduced by a `%' character in the control
88 string are substituted as defined by the current value of the variable
89 `battery-status-function'. Here are the ones generally available:
90 %c Current capacity (mAh or mWh)
91 %r Current rate of charge or discharge
92 %B Battery status (verbose)
93 %b Battery status: empty means high, `-' means low,
94 `!' means critical, and `+' means charging
95 %d Temperature (in degrees Celsius)
96 %L AC line status (verbose)
97 %p Battery load percentage
98 %m Remaining time (to charge or discharge) in minutes
99 %h Remaining time (to charge or discharge) in hours
100 %t Remaining time (to charge or discharge) in the form `h:min'"
101 :type '(choice string (const nil))
102 :group 'battery)
103
104 (defvar battery-mode-line-string nil
105 "String to display in the mode line.")
106 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
107
108 (defcustom battery-mode-line-limit 100
109 "Percentage of full battery load below which display battery status"
110 :version "24.1"
111 :type 'integer
112 :group 'battery)
113
114 (defcustom battery-mode-line-format
115 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
116 "[%b%p%%,%d°C]")
117 (battery-status-function
118 "[%b%p%%]"))
119 "Control string formatting the string to display in the mode line.
120 Ordinary characters in the control string are printed as-is, while
121 conversion specifications introduced by a `%' character in the control
122 string are substituted as defined by the current value of the variable
123 `battery-status-function'. Here are the ones generally available:
124 %c Current capacity (mAh or mWh)
125 %r Current rate of charge or discharge
126 %B Battery status (verbose)
127 %b Battery status: empty means high, `-' means low,
128 `!' means critical, and `+' means charging
129 %d Temperature (in degrees Celsius)
130 %L AC line status (verbose)
131 %p Battery load percentage
132 %m Remaining time (to charge or discharge) in minutes
133 %h Remaining time (to charge or discharge) in hours
134 %t Remaining time (to charge or discharge) in the form `h:min'"
135 :type '(choice string (const nil))
136 :group 'battery)
137
138 (defcustom battery-update-interval 60
139 "Seconds after which the battery status will be updated."
140 :type 'integer
141 :group 'battery)
142
143 (defcustom battery-load-low 25
144 "Upper bound of low battery load percentage.
145 A battery load percentage below this number is considered low."
146 :type 'integer
147 :group 'battery)
148
149 (defcustom battery-load-critical 10
150 "Upper bound of critical battery load percentage.
151 A battery load percentage below this number is considered critical."
152 :type 'integer
153 :group 'battery)
154
155 (defvar battery-update-timer nil
156 "Interval timer object.")
157
158 ;;;###autoload
159 (defun battery ()
160 "Display battery status information in the echo area.
161 The text being displayed in the echo area is controlled by the variables
162 `battery-echo-area-format' and `battery-status-function'."
163 (interactive)
164 (message "%s" (if (and battery-echo-area-format battery-status-function)
165 (battery-format battery-echo-area-format
166 (funcall battery-status-function))
167 "Battery status not available")))
168
169 ;;;###autoload
170 (define-minor-mode display-battery-mode
171 "Toggle battery status display in mode line (Display Battery mode).
172 With a prefix argument ARG, enable Display Battery mode if ARG is
173 positive, and disable it otherwise. If called from Lisp, enable
174 the mode if ARG is omitted or nil.
175
176 The text displayed in the mode line is controlled by
177 `battery-mode-line-format' and `battery-status-function'.
178 The mode line is be updated every `battery-update-interval'
179 seconds."
180 :global t :group 'battery
181 (setq battery-mode-line-string "")
182 (or global-mode-string (setq global-mode-string '("")))
183 (and battery-update-timer (cancel-timer battery-update-timer))
184 (if (and battery-status-function battery-mode-line-format)
185 (if (not display-battery-mode)
186 (setq global-mode-string
187 (delq 'battery-mode-line-string global-mode-string))
188 (add-to-list 'global-mode-string 'battery-mode-line-string t)
189 (setq battery-update-timer (run-at-time nil battery-update-interval
190 'battery-update-handler))
191 (battery-update))
192 (message "Battery status not available")
193 (setq display-battery-mode nil)))
194
195 (defun battery-update-handler ()
196 (battery-update)
197 (sit-for 0))
198
199 (defun battery-update ()
200 "Update battery status information in the mode line."
201 (let* ((data (and battery-status-function (funcall battery-status-function)))
202 (percentage (car (read-from-string (cdr (assq ?p data))))))
203 (setq battery-mode-line-string
204 (propertize (if (and battery-mode-line-format
205 (numberp percentage)
206 (<= percentage battery-mode-line-limit))
207 (battery-format battery-mode-line-format data)
208 "")
209 'face
210 (and (numberp percentage)
211 (<= percentage battery-load-critical)
212 'error)
213 'help-echo "Battery status information")))
214 (force-mode-line-update))
215 \f
216 ;;; `/proc/apm' interface for Linux.
217
218 (defconst battery-linux-proc-apm-regexp
219 (concat "^\\([^ ]+\\)" ; Driver version.
220 " \\([^ ]+\\)" ; APM BIOS version.
221 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
222 " 0x\\([0-9a-f]+\\)" ; AC line status.
223 " 0x\\([0-9a-f]+\\)" ; Battery status.
224 " 0x\\([0-9a-f]+\\)" ; Battery flags.
225 " \\(-?[0-9]+\\)%" ; Load percentage.
226 " \\(-?[0-9]+\\)" ; Remaining time.
227 " \\(.*\\)" ; Time unit.
228 "$")
229 "Regular expression matching contents of `/proc/apm'.")
230
231 (defun battery-linux-proc-apm ()
232 "Get APM status information from Linux (the kernel).
233 This function works only with the new `/proc/apm' format introduced
234 in Linux version 1.3.58.
235
236 The following %-sequences are provided:
237 %v Linux driver version
238 %V APM BIOS version
239 %I APM BIOS status (verbose)
240 %L AC line status (verbose)
241 %B Battery status (verbose)
242 %b Battery status, empty means high, `-' means low,
243 `!' means critical, and `+' means charging
244 %p Battery load percentage
245 %s Remaining time (to charge or discharge) in seconds
246 %m Remaining time (to charge or discharge) in minutes
247 %h Remaining time (to charge or discharge) in hours
248 %t Remaining time (to charge or discharge) in the form `h:min'"
249 (let (driver-version bios-version bios-interface line-status
250 battery-status battery-status-symbol load-percentage
251 seconds minutes hours remaining-time tem)
252 (with-temp-buffer
253 (ignore-errors (insert-file-contents "/proc/apm"))
254 (when (re-search-forward battery-linux-proc-apm-regexp)
255 (setq driver-version (match-string 1))
256 (setq bios-version (match-string 2))
257 (setq tem (string-to-number (match-string 3) 16))
258 (if (not (logand tem 2))
259 (setq bios-interface "not supported")
260 (setq bios-interface "enabled")
261 (cond ((logand tem 16) (setq bios-interface "disabled"))
262 ((logand tem 32) (setq bios-interface "disengaged")))
263 (setq tem (string-to-number (match-string 4) 16))
264 (cond ((= tem 0) (setq line-status "off-line"))
265 ((= tem 1) (setq line-status "on-line"))
266 ((= tem 2) (setq line-status "on backup")))
267 (setq tem (string-to-number (match-string 6) 16))
268 (if (= tem 255)
269 (setq battery-status "N/A")
270 (setq tem (string-to-number (match-string 5) 16))
271 (cond ((= tem 0) (setq battery-status "high"
272 battery-status-symbol ""))
273 ((= tem 1) (setq battery-status "low"
274 battery-status-symbol "-"))
275 ((= tem 2) (setq battery-status "critical"
276 battery-status-symbol "!"))
277 ((= tem 3) (setq battery-status "charging"
278 battery-status-symbol "+")))
279 (setq load-percentage (match-string 7))
280 (setq seconds (string-to-number (match-string 8)))
281 (and (string-equal (match-string 9) "min")
282 (setq seconds (* 60 seconds)))
283 (setq minutes (/ seconds 60)
284 hours (/ seconds 3600))
285 (setq remaining-time
286 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
287 (list (cons ?v (or driver-version "N/A"))
288 (cons ?V (or bios-version "N/A"))
289 (cons ?I (or bios-interface "N/A"))
290 (cons ?L (or line-status "N/A"))
291 (cons ?B (or battery-status "N/A"))
292 (cons ?b (or battery-status-symbol ""))
293 (cons ?p (or load-percentage "N/A"))
294 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
295 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
296 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
297 (cons ?t (or remaining-time "N/A")))))
298
299 \f
300 ;;; `/proc/acpi/' interface for Linux.
301
302 (defun battery-linux-proc-acpi ()
303 "Get ACPI status information from Linux (the kernel).
304 This function works only with the `/proc/acpi/' format introduced
305 in Linux version 2.4.20 and 2.6.0.
306
307 The following %-sequences are provided:
308 %c Current capacity (mAh)
309 %r Current rate
310 %B Battery status (verbose)
311 %b Battery status, empty means high, `-' means low,
312 `!' means critical, and `+' means charging
313 %d Temperature (in degrees Celsius)
314 %L AC line status (verbose)
315 %p Battery load percentage
316 %m Remaining time (to charge or discharge) in minutes
317 %h Remaining time (to charge or discharge) in hours
318 %t Remaining time (to charge or discharge) in the form `h:min'"
319 (let ((design-capacity 0)
320 (last-full-capacity 0)
321 full-capacity
322 (warn 0)
323 (low 0)
324 capacity rate rate-type charging-state minutes hours)
325 ;; ACPI provides information about each battery present in the system in
326 ;; a separate subdirectory. We are going to merge the available
327 ;; information together since displaying for a variable amount of
328 ;; batteries seems overkill for format-strings.
329 (with-temp-buffer
330 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
331 t "\\`[^.]")))
332 (erase-buffer)
333 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
334 (when (re-search-forward "present: +yes$" nil t)
335 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
336 (member charging-state '("unknown" "charged" nil))
337 ;; On most multi-battery systems, most of the time only one
338 ;; battery is "charging"/"discharging", the others are
339 ;; "unknown".
340 (setq charging-state (match-string 1)))
341 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
342 nil t)
343 (setq rate (+ (or rate 0) (string-to-number (match-string 1))))
344 (when (> rate 0)
345 (setq rate-type (or (and rate-type
346 (if (string= rate-type (match-string 2))
347 rate-type
348 (error
349 "Inconsistent rate types (%s vs. %s)"
350 rate-type (match-string 2))))
351 (match-string 2)))))
352 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
353 nil t)
354 (setq capacity
355 (+ (or capacity 0) (string-to-number (match-string 1))))))
356 (goto-char (point-max))
357 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
358 (when (re-search-forward "present: +yes$" nil t)
359 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
360 nil t)
361 (cl-incf design-capacity (string-to-number (match-string 1))))
362 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
363 nil t)
364 (cl-incf last-full-capacity (string-to-number (match-string 1))))
365 (when (re-search-forward
366 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
367 (cl-incf warn (string-to-number (match-string 1))))
368 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
369 nil t)
370 (cl-incf low (string-to-number (match-string 1)))))))
371 (setq full-capacity (if (> last-full-capacity 0)
372 last-full-capacity design-capacity))
373 (and capacity rate
374 (setq minutes (if (zerop rate) 0
375 (floor (* (/ (float (if (string= charging-state
376 "charging")
377 (- full-capacity capacity)
378 capacity))
379 rate)
380 60)))
381 hours (/ minutes 60)))
382 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
383 (cons ?L (or (battery-search-for-one-match-in-files
384 (mapcar (lambda (e) (concat e "/state"))
385 (ignore-errors
386 (directory-files "/proc/acpi/ac_adapter/"
387 t "\\`[^.]")))
388 "state: +\\(.*\\)$" 1)
389
390 "N/A"))
391 (cons ?d (or (battery-search-for-one-match-in-files
392 (mapcar (lambda (e) (concat e "/temperature"))
393 (ignore-errors
394 (directory-files "/proc/acpi/thermal_zone/"
395 t "\\`[^.]")))
396 "temperature: +\\([0-9]+\\) C$" 1)
397
398 "N/A"))
399 (cons ?r (or (and rate (concat (number-to-string rate) " "
400 rate-type)) "N/A"))
401 (cons ?B (or charging-state "N/A"))
402 (cons ?b (or (and (string= charging-state "charging") "+")
403 (and capacity (< capacity low) "!")
404 (and capacity (< capacity warn) "-")
405 ""))
406 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
407 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
408 (cons ?t (or (and minutes
409 (format "%d:%02d" hours (- minutes (* 60 hours))))
410 "N/A"))
411 (cons ?p (or (and full-capacity capacity
412 (> full-capacity 0)
413 (number-to-string
414 (floor (/ capacity
415 (/ (float full-capacity) 100)))))
416 "N/A")))))
417
418 \f
419 ;;; `/sys/class/power_supply/BATN' interface for Linux.
420
421 (defun battery-linux-sysfs ()
422 "Get ACPI status information from Linux kernel.
423 This function works only with the new `/sys/class/power_supply/'
424 format introduced in Linux version 2.4.25.
425
426 The following %-sequences are provided:
427 %c Current capacity (mAh or mWh)
428 %r Current rate
429 %B Battery status (verbose)
430 %d Temperature (in degrees Celsius)
431 %p Battery load percentage
432 %L AC line status (verbose)
433 %m Remaining time (to charge or discharge) in minutes
434 %h Remaining time (to charge or discharge) in hours
435 %t Remaining time (to charge or discharge) in the form `h:min'"
436 (let (charging-state temperature hours
437 ;; Some batteries report charges and current, other energy and power.
438 ;; In order to reliably be able to combine those data, we convert them
439 ;; all to energy/power (since we can't combine different charges if
440 ;; they're not at the same voltage).
441 (energy-full 0.0)
442 (energy-now 0.0)
443 (power-now 0.0)
444 (voltage-now 10.8)) ;Arbitrary default, in case the info is missing.
445 ;; SysFS provides information about each battery present in the
446 ;; system in a separate subdirectory. We are going to merge the
447 ;; available information together.
448 (with-temp-buffer
449 (dolist (dir (ignore-errors
450 (directory-files
451 "/sys/class/power_supply/" t
452 battery-linux-sysfs-regexp)))
453 (erase-buffer)
454 (ignore-errors (insert-file-contents
455 (expand-file-name "uevent" dir)))
456 (goto-char (point-min))
457 (when (re-search-forward
458 "POWER_SUPPLY_VOLTAGE_NOW=\\([0-9]*\\)$" nil t)
459 (setq voltage-now (/ (string-to-number (match-string 1)) 1000000.0)))
460 (goto-char (point-min))
461 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t)
462 (goto-char (point-min))
463 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t)
464 (member charging-state '("Unknown" "Full" nil))
465 (setq charging-state (match-string 1)))
466 (goto-char (point-min))
467 (when (re-search-forward
468 "POWER_SUPPLY_\\(CURRENT\\|POWER\\)_NOW=\\([0-9]*\\)$"
469 nil t)
470 (cl-incf power-now
471 (* (float (string-to-number (match-string 2)))
472 (if (eq (char-after (match-beginning 1)) ?C)
473 voltage-now 1.0))))
474 (goto-char (point-min))
475 (when (re-search-forward "POWER_SUPPLY_TEMP=\\([0-9]*\\)$" nil t)
476 (setq temperature (match-string 1)))
477 (goto-char (point-min))
478 (let (full-string now-string)
479 ;; Sysfs may list either charge (mAh) or energy (mWh).
480 ;; Keep track of both, and choose which to report later.
481 (cond ((and (re-search-forward
482 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t)
483 (setq full-string (match-string 1))
484 (re-search-forward
485 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t)
486 (setq now-string (match-string 1)))
487 (cl-incf energy-full (* (string-to-number full-string)
488 voltage-now))
489 (cl-incf energy-now (* (string-to-number now-string)
490 voltage-now)))
491 ((and (progn (goto-char (point-min)) t)
492 (re-search-forward
493 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t)
494 (setq full-string (match-string 1))
495 (re-search-forward
496 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t)
497 (setq now-string (match-string 1)))
498 (cl-incf energy-full (string-to-number full-string))
499 (cl-incf energy-now (string-to-number now-string)))))
500 (goto-char (point-min))
501 (unless (zerop power-now)
502 (let ((remaining (if (string= charging-state "Discharging")
503 energy-now
504 (- energy-full energy-now))))
505 (setq hours (/ remaining power-now)))))))
506 (list (cons ?c (cond ((or (> energy-full 0) (> energy-now 0))
507 (number-to-string (/ energy-now voltage-now)))
508 (t "N/A")))
509 (cons ?r (if (> power-now 0.0)
510 (format "%.1f" (/ power-now 1000000.0))
511 "N/A"))
512 (cons ?m (if hours (format "%d" (* hours 60)) "N/A"))
513 (cons ?h (if hours (format "%d" hours) "N/A"))
514 (cons ?t (if hours
515 (format "%d:%02d" hours (* (- hours (floor hours)) 60))
516 "N/A"))
517 (cons ?d (or temperature "N/A"))
518 (cons ?B (or charging-state "N/A"))
519 (cons ?p (cond ((and (> energy-full 0) (> energy-now 0))
520 (format "%.1f"
521 (/ (* 100 energy-now) energy-full)))
522 (t "N/A")))
523 (cons ?L (cond
524 ((battery-search-for-one-match-in-files
525 (list "/sys/class/power_supply/AC/online"
526 "/sys/class/power_supply/ACAD/online"
527 "/sys/class/power_supply/ADP1/online")
528 "1" 0)
529 "AC")
530 ((battery-search-for-one-match-in-files
531 (list "/sys/class/power_supply/AC/online"
532 "/sys/class/power_supply/ACAD/online"
533 "/sys/class/power_supply/ADP1/online")
534 "0" 0)
535 "BAT")
536 (t "N/A"))))))
537
538 \f
539 ;;; `apm' interface for BSD.
540 (defun battery-bsd-apm ()
541 "Get APM status information from BSD apm binary.
542 The following %-sequences are provided:
543 %L AC line status (verbose)
544 %B Battery status (verbose)
545 %b Battery status, empty means high, `-' means low,
546 `!' means critical, and `+' means charging
547 %P Advanced power saving mode state (verbose)
548 %p Battery charge percentage
549 %s Remaining battery charge time in seconds
550 %m Remaining battery charge time in minutes
551 %h Remaining battery charge time in hours
552 %t Remaining battery charge time in the form `h:min'"
553 (let* ((os-name (car (split-string
554 (shell-command-to-string "/usr/bin/uname"))))
555 (apm-flag (if (equal os-name "OpenBSD") "P" "s"))
556 (apm-cmd (concat "/usr/sbin/apm -ablm" apm-flag))
557 (apm-output (split-string (shell-command-to-string apm-cmd)))
558 ;; Battery status
559 (battery-status
560 (let ((stat (string-to-number (nth 0 apm-output))))
561 (cond ((eq stat 0) '("high" . ""))
562 ((eq stat 1) '("low" . "-"))
563 ((eq stat 2) '("critical" . "!"))
564 ((eq stat 3) '("charging" . "+"))
565 ((eq stat 4) '("absent" . nil)))))
566 ;; Battery percentage
567 (battery-percentage (nth 1 apm-output))
568 ;; Battery life
569 (battery-life (nth 2 apm-output))
570 ;; AC status
571 (line-status
572 (let ((ac (string-to-number (nth 3 apm-output))))
573 (cond ((eq ac 0) "disconnected")
574 ((eq ac 1) "connected")
575 ((eq ac 2) "backup power"))))
576 ;; Advanced power savings mode
577 (apm-mode
578 (let ((apm (string-to-number (nth 4 apm-output))))
579 (if (string= os-name "OpenBSD")
580 (cond ((eq apm 0) "manual")
581 ((eq apm 1) "automatic")
582 ((eq apm 2) "cool running"))
583 (if (eq apm 1) "on" "off"))))
584 seconds minutes hours remaining-time)
585 (unless (member battery-life '("unknown" "-1"))
586 (if (member os-name '("OpenBSD" "NetBSD"))
587 (setq minutes (string-to-number battery-life)
588 seconds (* 60 minutes))
589 (setq seconds (string-to-number battery-life)
590 minutes (truncate (/ seconds 60))))
591 (setq hours (truncate (/ minutes 60))
592 remaining-time (format "%d:%02d" hours
593 (- minutes (* 60 hours)))))
594 (list (cons ?L (or line-status "N/A"))
595 (cons ?B (or (car battery-status) "N/A"))
596 (cons ?b (or (cdr battery-status) "N/A"))
597 (cons ?p (if (string= battery-percentage "255")
598 "N/A"
599 battery-percentage))
600 (cons ?P (or apm-mode "N/A"))
601 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
602 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
603 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
604 (cons ?t (or remaining-time "N/A")))))
605
606 \f
607 ;;; `pmset' interface for Darwin (OS X).
608
609 (defun battery-pmset ()
610 "Get battery status information using `pmset'.
611
612 The following %-sequences are provided:
613 %L Power source (verbose)
614 %B Battery status (verbose)
615 %b Battery status, empty means high, `-' means low,
616 `!' means critical, and `+' means charging
617 %p Battery load percentage
618 %h Remaining time in hours
619 %m Remaining time in minutes
620 %t Remaining time in the form `h:min'"
621 (let (power-source load-percentage battery-status battery-status-symbol
622 remaining-time hours minutes)
623 (with-temp-buffer
624 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
625 (goto-char (point-min))
626 (when (re-search-forward "\\(?:Currentl?y\\|Now\\) drawing from '\\(AC\\|Battery\\) Power'" nil t)
627 (setq power-source (match-string 1))
628 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
629 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
630 (setq load-percentage (match-string 1))
631 (goto-char (match-end 0))
632 (cond ((looking-at "; charging")
633 (setq battery-status "charging"
634 battery-status-symbol "+"))
635 ((< (string-to-number load-percentage) battery-load-critical)
636 (setq battery-status "critical"
637 battery-status-symbol "!"))
638 ((< (string-to-number load-percentage) battery-load-low)
639 (setq battery-status "low"
640 battery-status-symbol "-"))
641 (t
642 (setq battery-status "high"
643 battery-status-symbol "")))
644 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
645 (setq remaining-time (match-string 1))
646 (let ((h (string-to-number (match-string 2)))
647 (m (string-to-number (match-string 3))))
648 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
649 minutes (number-to-string (+ (* h 60) m)))))))))
650 (list (cons ?L (or power-source "N/A"))
651 (cons ?p (or load-percentage "N/A"))
652 (cons ?B (or battery-status "N/A"))
653 (cons ?b (or battery-status-symbol ""))
654 (cons ?h (or hours "N/A"))
655 (cons ?m (or minutes "N/A"))
656 (cons ?t (or remaining-time "N/A")))))
657
658 \f
659 ;;; Private functions.
660
661 (defun battery-format (format alist)
662 "Substitute %-sequences in FORMAT."
663 (replace-regexp-in-string
664 "%."
665 (lambda (str)
666 (let ((char (aref str 1)))
667 (if (eq char ?%) "%"
668 (or (cdr (assoc char alist)) ""))))
669 format t t))
670
671 (defun battery-search-for-one-match-in-files (files regexp match-num)
672 "Search REGEXP in the content of the files listed in FILES.
673 If a match occurred, return the parenthesized expression numbered by
674 MATCH-NUM in the match. Otherwise, return nil."
675 (with-temp-buffer
676 (catch 'found
677 (dolist (file files)
678 (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
679 (re-search-forward regexp nil t)
680 (throw 'found (match-string match-num)))))))
681
682 \f
683 (provide 'battery)
684
685 ;;; battery.el ends here