Function: battery-linux-proc-acpi

battery-linux-proc-acpi is a byte-compiled function defined in battery.el.gz.

Signature

(battery-linux-proc-acpi)

Documentation

Get ACPI status information from Linux (the kernel).

This function works only with the /proc/acpi/ interface introduced in Linux version 2.4.20 and 2.6.0.

The following %-sequences are provided:
%c Current capacity (mAh)
%r Current rate of charge or discharge
%L AC line status (verbose)
%B Battery status (verbose)
%b Battery status, empty means high, - means low,
   ! means critical, and + means charging
%d Temperature (in degrees Celsius)
%p Battery load percentage
%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-linux-proc-acpi ()
  "Get ACPI status information from Linux (the kernel).
This function works only with the `/proc/acpi/' interface
introduced in Linux version 2.4.20 and 2.6.0.

The following %-sequences are provided:
%c Current capacity (mAh)
%r Current rate of charge or discharge
%L AC line status (verbose)
%B Battery status (verbose)
%b Battery status, empty means high, `-' means low,
   `!' means critical, and `+' means charging
%d Temperature (in degrees Celsius)
%p Battery load percentage
%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 ((design-capacity 0)
	(last-full-capacity 0)
	full-capacity
	(warn 0)
	(low 0)
	capacity rate rate-type charging-state minutes hours)
    ;; ACPI provides information about each battery present in the system in
    ;; a separate subdirectory.  We are going to merge the available
    ;; information together since displaying for a variable amount of
    ;; batteries seems overkill for format-strings.
    (with-temp-buffer
      (dolist (dir (battery--files "/proc/acpi/battery/"))
        (ignore-errors
          (insert-file-contents (expand-file-name "state" dir) nil nil nil t))
        (goto-char (point-min))
        (when (re-search-forward (rx "present:" (+ space) "yes" eol) nil t)
          (and (re-search-forward (rx "charging state:" (+ space)
                                      (group (not space) (* nonl)) eol)
                                  nil t)
	       (member charging-state '("unknown" "charged" nil))
	       ;; On most multi-battery systems, most of the time only one
	       ;; battery is "charging"/"discharging", the others are
	       ;; "unknown".
	       (setq charging-state (match-string 1)))
          (when (re-search-forward (rx "present rate:" (+ space)
                                       (battery--acpi-rate) eol)
				   nil t)
	    (setq rate (+ (or rate 0) (string-to-number (match-string 1))))
	    (when (> rate 0)
              (cond ((not rate-type)
                     (setq rate-type (match-string 2)))
                    ((not (string= rate-type (match-string 2)))
                     (error "Inconsistent rate types (%s vs. %s)"
                            rate-type (match-string 2))))))
          (when (re-search-forward (rx "remaining capacity:" (+ space)
                                       battery--acpi-capacity eol)
				   nil t)
	    (setq capacity
		  (+ (or capacity 0) (string-to-number (match-string 1))))))
	(goto-char (point-max))
	(ignore-errors (insert-file-contents (expand-file-name "info" dir)))
        (when (re-search-forward (rx "present:" (+ space) "yes" eol) nil t)
          (when (re-search-forward (rx "design capacity:" (+ space)
                                       battery--acpi-capacity eol)
				   nil t)
	    (cl-incf design-capacity (string-to-number (match-string 1))))
          (when (re-search-forward (rx "last full capacity:" (+ space)
                                       battery--acpi-capacity eol)
				   nil t)
	    (cl-incf last-full-capacity (string-to-number (match-string 1))))
          (when (re-search-forward (rx "design capacity warning:" (+ space)
                                       battery--acpi-capacity eol)
                                   nil t)
	    (cl-incf warn (string-to-number (match-string 1))))
          (when (re-search-forward (rx "design capacity low:" (+ space)
                                       battery--acpi-capacity eol)
				   nil t)
	    (cl-incf low (string-to-number (match-string 1)))))))
    (setq full-capacity (if (> last-full-capacity 0)
			    last-full-capacity design-capacity))
    (and capacity rate
	 (setq minutes (if (zerop rate) 0
			 (floor (* (if (string= charging-state
						"charging")
				       (- full-capacity capacity)
				     capacity)
				   60)
				rate))
	       hours (/ minutes 60)))
    (list (cons ?c (if capacity (number-to-string capacity) "N/A"))
	  (cons ?L (or (battery-search-for-one-match-in-files
                        (mapcar (lambda (d) (expand-file-name "state" d))
                                (battery--files "/proc/acpi/ac_adapter/"))
                        (rx "state:" (+ space) (group (not space) (* nonl)) eol)
                        1)
		       "N/A"))
	  (cons ?d (or (battery-search-for-one-match-in-files
                        (mapcar (lambda (d) (expand-file-name "temperature" d))
                                (battery--files "/proc/acpi/thermal_zone/"))
                        (rx "temperature:" (+ space) (group (+ digit)) " C" eol)
                        1)
		       "N/A"))
          (cons ?r (if rate
                       (concat (number-to-string rate) " " rate-type)
                     "N/A"))
	  (cons ?B (or charging-state "N/A"))
          (cons ?b (cond ((string= charging-state "charging") "+")
                         ((and capacity (< capacity low)) "!")
                         ((and capacity (< capacity warn)) "-")
                         ("")))
          (cons ?h (if hours (number-to-string hours) "N/A"))
          (cons ?m (if minutes (number-to-string minutes) "N/A"))
          (cons ?t (if minutes (format "%d:%02d" hours (% minutes 60)) "N/A"))
          (cons ?p (if (and full-capacity capacity (> full-capacity 0))
                       (number-to-string (floor (* 100 capacity) full-capacity))
                     "N/A")))))