Function: battery-bsd-apm

battery-bsd-apm is a byte-compiled function defined in battery.el.gz.

Signature

(battery-bsd-apm)

Documentation

Get APM status information from BSD apm binary.

The following %-sequences are provided:
%P Advanced power saving mode state (verbose)
%L AC line status (verbose)
%B Battery status (verbose)
%b Battery status, empty means high, - means low,
   ! means critical, and + means charging
%p Battery load percentage
%s Remaining time (to charge or discharge) in seconds
%m Remaining time (to charge or discharge) in minutes
%h Remaining time (to charge or discharge) in hours
%t Remaining time (to charge or discharge) in the form h:min

Source Code

;; Defined in /usr/src/emacs/lisp/battery.el.gz
(defun battery-bsd-apm ()
  "Get APM status information from BSD apm binary.
The following %-sequences are provided:
%P Advanced power saving mode state (verbose)
%L AC line status (verbose)
%B Battery status (verbose)
%b Battery status, empty means high, `-' means low,
   `!' means critical, and `+' means charging
%p Battery load percentage
%s Remaining time (to charge or discharge) in seconds
%m Remaining time (to charge or discharge) in minutes
%h Remaining time (to charge or discharge) in hours
%t Remaining time (to charge or discharge) in the form `h:min'"
  (let* ((os-name (car (split-string
                        ;; We cannot use `system-type' because some BSD
                        ;; systems fall under the 'berkeley-unix umbrella
                        ;; and we're trying to make the distinction
                        ;; among them here.
                        (battery--call-process-to-string "uname"))))
         (apm-flag (pcase os-name
                     ("OpenBSD" "mP")
                     ("FreeBSD" "st")
                     (_         "ms")))
         (apm-args (concat "-abl" apm-flag))
         (apm-output (split-string
                      (battery--call-process-to-string "apm" apm-args)))
         (indices (pcase os-name
                    ;; FreeBSD's manpage documents that multiple
                    ;; outputs are ordered by "the order in which
                    ;; they're listed in the manpage", which is alphabetical
                    ;; and is also the order in which we pass them.
                    ("FreeBSD" '((ac . 0)
                                 (battery-status . 1)
                                 (battery-percent . 2)
                                 (apm-mode . 3)
                                 (battery-life . 4)))
                    ;; For NetBSD and OpenBSD, the manpage doesn't document
                    ;; the order.  The previous code used this order, so let's
                    ;; assume it's right.
                    (_         '((ac . 3)
                                 (battery-status . 0)
                                 (battery-percent . 1)
                                 (apm-mode . 4)
                                 (battery-life . 2)))))
         ;; Battery status
         (battery-status
          (pcase (string-to-number
                  (nth (alist-get 'battery-status indices) apm-output))
            (0 '("high" . ""))
            (1 '("low" . "-"))
            (2 '("critical" . "!"))
            (3 '("charging" . "+"))
            (4 '("absent" . nil))))
         ;; Battery percentage
         (battery-percentage
          (nth (alist-get 'battery-percent indices) apm-output))
         ;; Battery life
         (battery-life (nth (alist-get 'battery-life indices) apm-output))
         ;; AC status
         (line-status
          (pcase (string-to-number (nth (alist-get 'ac indices) apm-output))
            (0 "disconnected")
            (1 "connected")
            (2 "backup power")))
         ;; Advanced power savings mode
         (apm-mode
          (let ((apm (string-to-number
                      (nth (alist-get 'apm-mode indices) apm-output))))
            (if (string= os-name "OpenBSD")
                (pcase apm
                  (0 "manual")
                  (1 "automatic")
		  (2 "cool running"))
	      (if (eql apm 1) "on" "off"))))
	 seconds minutes hours remaining-time)
    (unless (member battery-life '("unknown" "-1"))
      (if (member os-name '("OpenBSD" "NetBSD"))
	  (setq minutes (string-to-number battery-life)
		seconds (* 60 minutes))
	(setq seconds (string-to-number battery-life)
	      minutes (truncate seconds 60)))
      (setq hours (truncate minutes 60)
            remaining-time (format "%d:%02d" hours (% minutes 60))))
    (list (cons ?L (or line-status "N/A"))
	  (cons ?B (or (car battery-status) "N/A"))
	  (cons ?b (or (cdr battery-status) "N/A"))
	  (cons ?p (if (string= battery-percentage "255")
		       "N/A"
		     battery-percentage))
	  (cons ?P (or apm-mode "N/A"))
          (cons ?s (if seconds (number-to-string seconds) "N/A"))
          (cons ?m (if minutes (number-to-string minutes) "N/A"))
          (cons ?h (if hours (number-to-string hours) "N/A"))
	  (cons ?t (or remaining-time "N/A")))))