1 ;;; battery.el --- display battery status information
3 ;; Copyright (C) 1997-1998, 2000-2017 Free Software Foundation, Inc.
5 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; There is at present support for GNU/Linux, macOS 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 (macOS) is supported by using the `pmset'
29 ;; program. Windows is supported by the GetSystemPowerStatus API call.
34 (eval-when-compile (require 'cl-lib
))
37 "Display battery status information."
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."
48 (defcustom battery-upower-device
"battery_BAT1"
49 "Upower battery device name."
54 (defcustom battery-status-function
55 (cond ((and (eq system-type
'gnu
/linux
)
56 (file-readable-p "/proc/apm"))
57 #'battery-linux-proc-apm
)
58 ((and (eq system-type
'gnu
/linux
)
59 (file-directory-p "/proc/acpi/battery"))
60 #'battery-linux-proc-acpi
)
61 ((and (eq system-type
'gnu
/linux
)
62 (file-directory-p "/sys/class/power_supply/")
63 (directory-files "/sys/class/power_supply/" nil
64 battery-linux-sysfs-regexp
))
65 #'battery-linux-sysfs
)
66 ((and (eq system-type
'berkeley-unix
)
67 (file-executable-p "/usr/sbin/apm"))
69 ((and (eq system-type
'darwin
)
72 (and (eq (call-process "pmset" nil t nil
"-g" "ps") 0)
76 ((fboundp 'w32-battery-status
)
77 #'w32-battery-status
))
78 "Function for getting battery status information.
79 The function has to return an alist of conversion definitions.
80 Its cons cells are of the form
82 (CONVERSION . REPLACEMENT-TEXT)
84 CONVERSION is the character code of a \"conversion specification\"
85 introduced by a `%' character in a control string."
86 :type
'(choice (const nil
) function
)
89 (defcustom battery-echo-area-format
90 "Power %L, battery %B (%p%% load, remaining time %t)"
91 "Control string formatting the string to display in the echo area.
92 Ordinary characters in the control string are printed as-is, while
93 conversion specifications introduced by a `%' character in the control
94 string are substituted as defined by the current value of the variable
95 `battery-status-function'. Here are the ones generally available:
96 %c Current capacity (mAh or mWh)
97 %r Current rate of charge or discharge
98 %B Battery status (verbose)
99 %b Battery status: empty means high, `-' means low,
100 `!' means critical, and `+' means charging
101 %d Temperature (in degrees Celsius)
102 %L AC line status (verbose)
103 %p Battery load percentage
104 %m Remaining time (to charge or discharge) in minutes
105 %h Remaining time (to charge or discharge) in hours
106 %t Remaining time (to charge or discharge) in the form `h:min'"
107 :type
'(choice string
(const nil
))
110 (defvar battery-mode-line-string nil
111 "String to display in the mode line.")
112 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
114 (defcustom battery-mode-line-limit
100
115 "Percentage of full battery load below which display battery status"
120 (defcustom battery-mode-line-format
121 (cond ((eq battery-status-function
'battery-linux-proc-acpi
)
123 (battery-status-function
125 "Control string formatting the string to display in the mode line.
126 Ordinary characters in the control string are printed as-is, while
127 conversion specifications introduced by a `%' character in the control
128 string are substituted as defined by the current value of the variable
129 `battery-status-function'. Here are the ones generally available:
130 %c Current capacity (mAh or mWh)
131 %r Current rate of charge or discharge
132 %B Battery status (verbose)
133 %b Battery status: empty means high, `-' means low,
134 `!' means critical, and `+' means charging
135 %d Temperature (in degrees Celsius)
136 %L AC line status (verbose)
137 %p Battery load percentage
138 %m Remaining time (to charge or discharge) in minutes
139 %h Remaining time (to charge or discharge) in hours
140 %t Remaining time (to charge or discharge) in the form `h:min'"
141 :type
'(choice string
(const nil
))
144 (defcustom battery-update-interval
60
145 "Seconds after which the battery status will be updated."
149 (defcustom battery-load-low
25
150 "Upper bound of low battery load percentage.
151 A battery load percentage below this number is considered low."
155 (defcustom battery-load-critical
10
156 "Upper bound of critical battery load percentage.
157 A battery load percentage below this number is considered critical."
161 (defvar battery-update-timer nil
162 "Interval timer object.")
166 "Display battery status information in the echo area.
167 The text being displayed in the echo area is controlled by the variables
168 `battery-echo-area-format' and `battery-status-function'."
170 (message "%s" (if (and battery-echo-area-format battery-status-function
)
171 (battery-format battery-echo-area-format
172 (funcall battery-status-function
))
173 "Battery status not available")))
176 (define-minor-mode display-battery-mode
177 "Toggle battery status display in mode line (Display Battery mode).
178 With a prefix argument ARG, enable Display Battery mode if ARG is
179 positive, and disable it otherwise. If called from Lisp, enable
180 the mode if ARG is omitted or nil.
182 The text displayed in the mode line is controlled by
183 `battery-mode-line-format' and `battery-status-function'.
184 The mode line is be updated every `battery-update-interval'
186 :global t
:group
'battery
187 (setq battery-mode-line-string
"")
188 (or global-mode-string
(setq global-mode-string
'("")))
189 (and battery-update-timer
(cancel-timer battery-update-timer
))
190 (if (and battery-status-function battery-mode-line-format
)
191 (if (not display-battery-mode
)
192 (setq global-mode-string
193 (delq 'battery-mode-line-string global-mode-string
))
194 (add-to-list 'global-mode-string
'battery-mode-line-string t
)
195 (setq battery-update-timer
(run-at-time nil battery-update-interval
196 'battery-update-handler
))
198 (message "Battery status not available")
199 (setq display-battery-mode nil
)))
201 (defun battery-update-handler ()
205 (defun battery-update ()
206 "Update battery status information in the mode line."
207 (let* ((data (and battery-status-function
(funcall battery-status-function
)))
208 (percentage (car (read-from-string (cdr (assq ?p data
))))))
209 (setq battery-mode-line-string
210 (propertize (if (and battery-mode-line-format
212 (<= percentage battery-mode-line-limit
))
213 (battery-format battery-mode-line-format data
)
216 (and (numberp percentage
)
217 (<= percentage battery-load-critical
)
219 'help-echo
"Battery status information")))
220 (force-mode-line-update))
222 ;;; `/proc/apm' interface for Linux.
224 (defconst battery-linux-proc-apm-regexp
225 (concat "^\\([^ ]+\\)" ; Driver version.
226 " \\([^ ]+\\)" ; APM BIOS version.
227 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
228 " 0x\\([0-9a-f]+\\)" ; AC line status.
229 " 0x\\([0-9a-f]+\\)" ; Battery status.
230 " 0x\\([0-9a-f]+\\)" ; Battery flags.
231 " \\(-?[0-9]+\\)%" ; Load percentage.
232 " \\(-?[0-9]+\\)" ; Remaining time.
233 " \\(.*\\)" ; Time unit.
235 "Regular expression matching contents of `/proc/apm'.")
237 (defun battery-linux-proc-apm ()
238 "Get APM status information from Linux (the kernel).
239 This function works only with the new `/proc/apm' format introduced
240 in Linux version 1.3.58.
242 The following %-sequences are provided:
243 %v Linux driver version
245 %I APM BIOS status (verbose)
246 %L AC line status (verbose)
247 %B Battery status (verbose)
248 %b Battery status, empty means high, `-' means low,
249 `!' means critical, and `+' means charging
250 %p Battery load percentage
251 %s Remaining time (to charge or discharge) in seconds
252 %m Remaining time (to charge or discharge) in minutes
253 %h Remaining time (to charge or discharge) in hours
254 %t Remaining time (to charge or discharge) in the form `h:min'"
255 (let (driver-version bios-version bios-interface line-status
256 battery-status battery-status-symbol load-percentage
257 seconds minutes hours remaining-time tem
)
259 (ignore-errors (insert-file-contents "/proc/apm"))
260 (when (re-search-forward battery-linux-proc-apm-regexp
)
261 (setq driver-version
(match-string 1))
262 (setq bios-version
(match-string 2))
263 (setq tem
(string-to-number (match-string 3) 16))
264 (if (not (logand tem
2))
265 (setq bios-interface
"not supported")
266 (setq bios-interface
"enabled")
267 (cond ((logand tem
16) (setq bios-interface
"disabled"))
268 ((logand tem
32) (setq bios-interface
"disengaged")))
269 (setq tem
(string-to-number (match-string 4) 16))
270 (cond ((= tem
0) (setq line-status
"off-line"))
271 ((= tem
1) (setq line-status
"on-line"))
272 ((= tem
2) (setq line-status
"on backup")))
273 (setq tem
(string-to-number (match-string 6) 16))
275 (setq battery-status
"N/A")
276 (setq tem
(string-to-number (match-string 5) 16))
277 (cond ((= tem
0) (setq battery-status
"high"
278 battery-status-symbol
""))
279 ((= tem
1) (setq battery-status
"low"
280 battery-status-symbol
"-"))
281 ((= tem
2) (setq battery-status
"critical"
282 battery-status-symbol
"!"))
283 ((= tem
3) (setq battery-status
"charging"
284 battery-status-symbol
"+")))
285 (setq load-percentage
(match-string 7))
286 (setq seconds
(string-to-number (match-string 8)))
287 (and (string-equal (match-string 9) "min")
288 (setq seconds
(* 60 seconds
)))
289 (setq minutes
(/ seconds
60)
290 hours
(/ seconds
3600))
292 (format "%d:%02d" hours
(- minutes
(* 60 hours
))))))))
293 (list (cons ?v
(or driver-version
"N/A"))
294 (cons ?V
(or bios-version
"N/A"))
295 (cons ?I
(or bios-interface
"N/A"))
296 (cons ?L
(or line-status
"N/A"))
297 (cons ?B
(or battery-status
"N/A"))
298 (cons ?b
(or battery-status-symbol
""))
299 (cons ?p
(or load-percentage
"N/A"))
300 (cons ?s
(or (and seconds
(number-to-string seconds
)) "N/A"))
301 (cons ?m
(or (and minutes
(number-to-string minutes
)) "N/A"))
302 (cons ?h
(or (and hours
(number-to-string hours
)) "N/A"))
303 (cons ?t
(or remaining-time
"N/A")))))
306 ;;; `/proc/acpi/' interface for Linux.
308 (defun battery-linux-proc-acpi ()
309 "Get ACPI status information from Linux (the kernel).
310 This function works only with the `/proc/acpi/' format introduced
311 in Linux version 2.4.20 and 2.6.0.
313 The following %-sequences are provided:
314 %c Current capacity (mAh)
316 %B Battery status (verbose)
317 %b Battery status, empty means high, `-' means low,
318 `!' means critical, and `+' means charging
319 %d Temperature (in degrees Celsius)
320 %L AC line status (verbose)
321 %p Battery load percentage
322 %m Remaining time (to charge or discharge) in minutes
323 %h Remaining time (to charge or discharge) in hours
324 %t Remaining time (to charge or discharge) in the form `h:min'"
325 (let ((design-capacity 0)
326 (last-full-capacity 0)
330 capacity rate rate-type charging-state minutes hours
)
331 ;; ACPI provides information about each battery present in the system in
332 ;; a separate subdirectory. We are going to merge the available
333 ;; information together since displaying for a variable amount of
334 ;; batteries seems overkill for format-strings.
336 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
339 (ignore-errors (insert-file-contents (expand-file-name "state" dir
)))
340 (when (re-search-forward "present: +yes$" nil t
)
341 (and (re-search-forward "charging state: +\\(.*\\)$" nil t
)
342 (member charging-state
'("unknown" "charged" nil
))
343 ;; On most multi-battery systems, most of the time only one
344 ;; battery is "charging"/"discharging", the others are
346 (setq charging-state
(match-string 1)))
347 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
349 (setq rate
(+ (or rate
0) (string-to-number (match-string 1))))
351 (setq rate-type
(or (and rate-type
352 (if (string= rate-type
(match-string 2))
355 "Inconsistent rate types (%s vs. %s)"
356 rate-type
(match-string 2))))
358 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
361 (+ (or capacity
0) (string-to-number (match-string 1))))))
362 (goto-char (point-max))
363 (ignore-errors (insert-file-contents (expand-file-name "info" dir
)))
364 (when (re-search-forward "present: +yes$" nil t
)
365 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
367 (cl-incf design-capacity
(string-to-number (match-string 1))))
368 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
370 (cl-incf last-full-capacity
(string-to-number (match-string 1))))
371 (when (re-search-forward
372 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t
)
373 (cl-incf warn
(string-to-number (match-string 1))))
374 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
376 (cl-incf low
(string-to-number (match-string 1)))))))
377 (setq full-capacity
(if (> last-full-capacity
0)
378 last-full-capacity design-capacity
))
380 (setq minutes
(if (zerop rate
) 0
381 (floor (* (/ (float (if (string= charging-state
383 (- full-capacity capacity
)
387 hours
(/ minutes
60)))
388 (list (cons ?c
(or (and capacity
(number-to-string capacity
)) "N/A"))
389 (cons ?L
(or (battery-search-for-one-match-in-files
390 (mapcar (lambda (e) (concat e
"/state"))
392 (directory-files "/proc/acpi/ac_adapter/"
394 "state: +\\(.*\\)$" 1)
397 (cons ?d
(or (battery-search-for-one-match-in-files
398 (mapcar (lambda (e) (concat e
"/temperature"))
400 (directory-files "/proc/acpi/thermal_zone/"
402 "temperature: +\\([0-9]+\\) C$" 1)
405 (cons ?r
(or (and rate
(concat (number-to-string rate
) " "
407 (cons ?B
(or charging-state
"N/A"))
408 (cons ?b
(or (and (string= charging-state
"charging") "+")
409 (and capacity
(< capacity low
) "!")
410 (and capacity
(< capacity warn
) "-")
412 (cons ?h
(or (and hours
(number-to-string hours
)) "N/A"))
413 (cons ?m
(or (and minutes
(number-to-string minutes
)) "N/A"))
414 (cons ?t
(or (and minutes
415 (format "%d:%02d" hours
(- minutes
(* 60 hours
))))
417 (cons ?p
(or (and full-capacity capacity
421 (/ (float full-capacity
) 100)))))
425 ;;; `/sys/class/power_supply/BATN' interface for Linux.
427 (defun battery-linux-sysfs ()
428 "Get ACPI status information from Linux kernel.
429 This function works only with the new `/sys/class/power_supply/'
430 format introduced in Linux version 2.4.25.
432 The following %-sequences are provided:
433 %c Current capacity (mAh or mWh)
435 %B Battery status (verbose)
436 %d Temperature (in degrees Celsius)
437 %p Battery load percentage
438 %L AC line status (verbose)
439 %m Remaining time (to charge or discharge) in minutes
440 %h Remaining time (to charge or discharge) in hours
441 %t Remaining time (to charge or discharge) in the form `h:min'"
442 (let (charging-state temperature hours
443 ;; Some batteries report charges and current, other energy and power.
444 ;; In order to reliably be able to combine those data, we convert them
445 ;; all to energy/power (since we can't combine different charges if
446 ;; they're not at the same voltage).
450 (voltage-now 10.8)) ;Arbitrary default, in case the info is missing.
451 ;; SysFS provides information about each battery present in the
452 ;; system in a separate subdirectory. We are going to merge the
453 ;; available information together.
455 (dolist (dir (ignore-errors
457 "/sys/class/power_supply/" t
458 battery-linux-sysfs-regexp
)))
460 (ignore-errors (insert-file-contents
461 (expand-file-name "uevent" dir
)))
462 (goto-char (point-min))
463 (when (re-search-forward
464 "POWER_SUPPLY_VOLTAGE_NOW=\\([0-9]*\\)$" nil t
)
465 (setq voltage-now
(/ (string-to-number (match-string 1)) 1000000.0)))
466 (goto-char (point-min))
467 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t
)
468 (goto-char (point-min))
469 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t
)
470 (member charging-state
'("Unknown" "Full" nil
))
471 (setq charging-state
(match-string 1)))
472 (goto-char (point-min))
473 (when (re-search-forward
474 "POWER_SUPPLY_\\(CURRENT\\|POWER\\)_NOW=\\([0-9]*\\)$"
477 (* (float (string-to-number (match-string 2)))
478 (if (eq (char-after (match-beginning 1)) ?C
)
480 (goto-char (point-min))
481 (when (re-search-forward "POWER_SUPPLY_TEMP=\\([0-9]*\\)$" nil t
)
482 (setq temperature
(match-string 1)))
483 (goto-char (point-min))
484 (let (full-string now-string
)
485 ;; Sysfs may list either charge (mAh) or energy (mWh).
486 ;; Keep track of both, and choose which to report later.
487 (cond ((and (re-search-forward
488 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t
)
489 (setq full-string
(match-string 1))
491 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t
)
492 (setq now-string
(match-string 1)))
493 (cl-incf energy-full
(* (string-to-number full-string
)
495 (cl-incf energy-now
(* (string-to-number now-string
)
497 ((and (progn (goto-char (point-min)) t
)
499 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t
)
500 (setq full-string
(match-string 1))
502 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t
)
503 (setq now-string
(match-string 1)))
504 (cl-incf energy-full
(string-to-number full-string
))
505 (cl-incf energy-now
(string-to-number now-string
)))))
506 (goto-char (point-min))
507 (unless (zerop power-now
)
508 (let ((remaining (if (string= charging-state
"Discharging")
510 (- energy-full energy-now
))))
511 (setq hours
(/ remaining power-now
)))))))
512 (list (cons ?c
(cond ((or (> energy-full
0) (> energy-now
0))
513 (number-to-string (/ energy-now voltage-now
)))
515 (cons ?r
(if (> power-now
0.0)
516 (format "%.1f" (/ power-now
1000000.0))
518 (cons ?m
(if hours
(format "%d" (* hours
60)) "N/A"))
519 (cons ?h
(if hours
(format "%d" hours
) "N/A"))
521 (format "%d:%02d" hours
(* (- hours
(floor hours
)) 60))
523 (cons ?d
(or temperature
"N/A"))
524 (cons ?B
(or charging-state
"N/A"))
525 (cons ?p
(cond ((and (> energy-full
0) (> energy-now
0))
527 (/ (* 100 energy-now
) energy-full
)))
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")
536 ((battery-search-for-one-match-in-files
537 (list "/sys/class/power_supply/AC/online"
538 "/sys/class/power_supply/ACAD/online"
539 "/sys/class/power_supply/ADP1/online")
545 (declare-function dbus-get-property
"dbus.el"
546 (bus service path interface property
))
548 ;;; `upowerd' interface.
549 (defsubst battery-upower-prop
(pname &optional device
)
552 "org.freedesktop.UPower"
553 (concat "/org/freedesktop/UPower/devices/" (or device battery-upower-device
))
554 "org.freedesktop.UPower"
557 (defun battery-upower ()
558 "Get battery status from dbus Upower interface.
559 This function works only in systems with `upowerd' daemon
562 The following %-sequences are provided:
563 %c Current capacity (mWh)
564 %p Battery load percentage
566 %B Battery status (verbose)
567 %L AC line status (verbose)
568 %s Remaining time (to charge or discharge) in seconds
569 %m Remaining time (to charge or discharge) in minutes
570 %h Remaining time (to charge or discharge) in hours
571 %t Remaining time (to charge or discharge) in the form `h:min'"
572 (let ((percents (battery-upower-prop "Percentage"))
573 (time-to-empty (battery-upower-prop "TimeToEmpty"))
574 (time-to-full (battery-upower-prop "TimeToFull"))
575 (state (battery-upower-prop "State"))
576 (online (battery-upower-prop "Online" "line_power_ACAD"))
577 (energy (battery-upower-prop "Energy"))
578 (energy-rate (battery-upower-prop "EnergyRate"))
579 (battery-states '((0 .
"unknown") (1 .
"charging")
580 (2 .
"discharging") (3 .
"empty")
581 (4 .
"fully-charged") (5 .
"pending-charge")
582 (6 .
"pending-discharge")))
583 seconds minutes hours remaining-time
)
584 (cond ((and online time-to-full
)
585 (setq seconds time-to-full
))
586 ((and (not online
) time-to-empty
)
587 (setq seconds time-to-empty
)))
589 (setq minutes
(/ seconds
60)
592 (format "%d:%02d" (truncate hours
)
593 (- (truncate minutes
) (* 60 (truncate hours
))))))
594 (list (cons ?c
(or (and energy
595 (number-to-string (round (* 1000 energy
))))
597 (cons ?p
(or (and percents
(number-to-string (round percents
)))
599 (cons ?r
(or (and energy-rate
600 (concat (number-to-string energy-rate
) " W"))
602 (cons ?B
(or (and state
(cdr (assoc state battery-states
)))
604 (cons ?L
(or (and online
"on-line") "off-line"))
605 (cons ?s
(or (and seconds
(number-to-string seconds
)) "N/A"))
606 (cons ?m
(or (and minutes
(number-to-string minutes
)) "N/A"))
607 (cons ?h
(or (and hours
(number-to-string hours
)) "N/A"))
608 (cons ?t
(or remaining-time
"N/A")))))
611 ;;; `apm' interface for BSD.
612 (defun battery-bsd-apm ()
613 "Get APM status information from BSD apm binary.
614 The following %-sequences are provided:
615 %L AC line status (verbose)
616 %B Battery status (verbose)
617 %b Battery status, empty means high, `-' means low,
618 `!' means critical, and `+' means charging
619 %P Advanced power saving mode state (verbose)
620 %p Battery charge percentage
621 %s Remaining battery charge time in seconds
622 %m Remaining battery charge time in minutes
623 %h Remaining battery charge time in hours
624 %t Remaining battery charge time in the form `h:min'"
625 (let* ((os-name (car (split-string
626 (shell-command-to-string "/usr/bin/uname"))))
627 (apm-flag (if (equal os-name
"OpenBSD") "P" "s"))
628 (apm-cmd (concat "/usr/sbin/apm -ablm" apm-flag
))
629 (apm-output (split-string (shell-command-to-string apm-cmd
)))
632 (let ((stat (string-to-number (nth 0 apm-output
))))
633 (cond ((eq stat
0) '("high" .
""))
634 ((eq stat
1) '("low" .
"-"))
635 ((eq stat
2) '("critical" .
"!"))
636 ((eq stat
3) '("charging" .
"+"))
637 ((eq stat
4) '("absent" . nil
)))))
638 ;; Battery percentage
639 (battery-percentage (nth 1 apm-output
))
641 (battery-life (nth 2 apm-output
))
644 (let ((ac (string-to-number (nth 3 apm-output
))))
645 (cond ((eq ac
0) "disconnected")
646 ((eq ac
1) "connected")
647 ((eq ac
2) "backup power"))))
648 ;; Advanced power savings mode
650 (let ((apm (string-to-number (nth 4 apm-output
))))
651 (if (string= os-name
"OpenBSD")
652 (cond ((eq apm
0) "manual")
653 ((eq apm
1) "automatic")
654 ((eq apm
2) "cool running"))
655 (if (eq apm
1) "on" "off"))))
656 seconds minutes hours remaining-time
)
657 (unless (member battery-life
'("unknown" "-1"))
658 (if (member os-name
'("OpenBSD" "NetBSD"))
659 (setq minutes
(string-to-number battery-life
)
660 seconds
(* 60 minutes
))
661 (setq seconds
(string-to-number battery-life
)
662 minutes
(truncate (/ seconds
60))))
663 (setq hours
(truncate (/ minutes
60))
664 remaining-time
(format "%d:%02d" hours
665 (- minutes
(* 60 hours
)))))
666 (list (cons ?L
(or line-status
"N/A"))
667 (cons ?B
(or (car battery-status
) "N/A"))
668 (cons ?b
(or (cdr battery-status
) "N/A"))
669 (cons ?p
(if (string= battery-percentage
"255")
672 (cons ?P
(or apm-mode
"N/A"))
673 (cons ?s
(or (and seconds
(number-to-string seconds
)) "N/A"))
674 (cons ?m
(or (and minutes
(number-to-string minutes
)) "N/A"))
675 (cons ?h
(or (and hours
(number-to-string hours
)) "N/A"))
676 (cons ?t
(or remaining-time
"N/A")))))
679 ;;; `pmset' interface for Darwin (macOS).
681 (defun battery-pmset ()
682 "Get battery status information using `pmset'.
684 The following %-sequences are provided:
685 %L Power source (verbose)
686 %B Battery status (verbose)
687 %b Battery status, empty means high, `-' means low,
688 `!' means critical, and `+' means charging
689 %p Battery load percentage
690 %h Remaining time in hours
691 %m Remaining time in minutes
692 %t Remaining time in the form `h:min'"
693 (let (power-source load-percentage battery-status battery-status-symbol
694 remaining-time hours minutes
)
696 (ignore-errors (call-process "pmset" nil t nil
"-g" "ps"))
697 (goto-char (point-min))
698 (when (re-search-forward "\\(?:Currentl?y\\|Now\\) drawing from '\\(AC\\|Battery\\) Power'" nil t
)
699 (setq power-source
(match-string 1))
700 (when (re-search-forward "^ -InternalBattery-0\\([ \t]+(id=[0-9]+)\\)*[ \t]+" nil t
)
701 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
702 (setq load-percentage
(match-string 1))
703 (goto-char (match-end 0))
704 (cond ((looking-at "; charging")
705 (setq battery-status
"charging"
706 battery-status-symbol
"+"))
707 ((< (string-to-number load-percentage
) battery-load-critical
)
708 (setq battery-status
"critical"
709 battery-status-symbol
"!"))
710 ((< (string-to-number load-percentage
) battery-load-low
)
711 (setq battery-status
"low"
712 battery-status-symbol
"-"))
714 (setq battery-status
"high"
715 battery-status-symbol
"")))
716 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t
)
717 (setq remaining-time
(match-string 1))
718 (let ((h (string-to-number (match-string 2)))
719 (m (string-to-number (match-string 3))))
720 (setq hours
(number-to-string (+ h
(if (< m
30) 0 1)))
721 minutes
(number-to-string (+ (* h
60) m
)))))))))
722 (list (cons ?L
(or power-source
"N/A"))
723 (cons ?p
(or load-percentage
"N/A"))
724 (cons ?B
(or battery-status
"N/A"))
725 (cons ?b
(or battery-status-symbol
""))
726 (cons ?h
(or hours
"N/A"))
727 (cons ?m
(or minutes
"N/A"))
728 (cons ?t
(or remaining-time
"N/A")))))
731 ;;; Private functions.
733 (defun battery-format (format alist
)
734 "Substitute %-sequences in FORMAT."
735 (replace-regexp-in-string
738 (let ((char (aref str
1)))
740 (or (cdr (assoc char alist
)) ""))))
743 (defun battery-search-for-one-match-in-files (files regexp match-num
)
744 "Search REGEXP in the content of the files listed in FILES.
745 If a match occurred, return the parenthesized expression numbered by
746 MATCH-NUM in the match. Otherwise, return nil."
750 (and (ignore-errors (insert-file-contents file nil nil nil
'replace
))
751 (re-search-forward regexp nil t
)
752 (throw 'found
(match-string match-num
)))))))
757 ;;; battery.el ends here