summaryrefslogtreecommitdiff
path: root/lisp/battery.el
diff options
context:
space:
mode:
authorEvgeny Zajcev <lg.zevlg@gmail.com>2016-12-02 12:17:38 +0200
committerEli Zaretskii <eliz@gnu.org>2016-12-02 12:17:38 +0200
commit05a969265cabdf361492ed471f1a8dc369840401 (patch)
tree31a8d7f61f102368900ef04817cde31ce7e44ede /lisp/battery.el
parent0b3f8ca863e7df08e79a935946f7190e595cae2b (diff)
downloademacs-05a969265cabdf361492ed471f1a8dc369840401.tar.gz
* lisp/battery.el: Add 'battery-upower' -- very fast battery status.
Diffstat (limited to 'lisp/battery.el')
-rw-r--r--lisp/battery.el69
1 files changed, 69 insertions, 0 deletions
diff --git a/lisp/battery.el b/lisp/battery.el
index 24eb8a58f63..773ab0d1d67 100644
--- a/lisp/battery.el
+++ b/lisp/battery.el
@@ -45,6 +45,12 @@
:type 'regexp
:group 'battery)
+(defcustom battery-upower-device "battery_BAT1"
+ "*Upower battery device name."
+ :version "26.1"
+ :type 'string
+ :group 'battery)
+
(defcustom battery-status-function
(cond ((and (eq system-type 'gnu/linux)
(file-readable-p "/proc/apm"))
@@ -536,6 +542,69 @@ The following %-sequences are provided:
(t "N/A"))))))
+;;; `upowerd' interface.
+(defsubst battery-upower-prop (pname &optional device)
+ (dbus-get-property
+ :system
+ "org.freedesktop.UPower"
+ (concat "/org/freedesktop/UPower/devices/" (or device battery-upower-device))
+ "org.freedesktop.UPower"
+ pname))
+
+(defun battery-upower ()
+ "Get battery status from dbus Upower interface.
+This function works only in systems with `upowerd' daemon
+running.
+
+The following %-sequences are provided:
+%c Current capacity (mWh)
+%p Battery load percentage
+%r Current rate
+%B Battery status (verbose)
+%L AC line status (verbose)
+%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 ((percents (battery-upower-prop "Percentage"))
+ (time-to-empty (battery-upower-prop "TimeToEmpty"))
+ (time-to-full (battery-upower-prop "TimeToFull"))
+ (state (battery-upower-prop "State"))
+ (online (battery-upower-prop "Online" "line_power_ACAD"))
+ (energy (battery-upower-prop "Energy"))
+ (energy-rate (battery-upower-prop "EnergyRate"))
+ (battery-states '((0 . "unknown") (1 . "charging")
+ (2 . "discharging") (3 . "empty")
+ (4 . "fully-charged") (5 . "pending-charge")
+ (6 . "pending-discharge")))
+ seconds minutes hours remaining-time)
+ (cond ((and online time-to-full)
+ (setq seconds time-to-full))
+ ((and (not online) time-to-empty)
+ (setq seconds time-to-empty)))
+ (when seconds
+ (setq minutes (/ seconds 60)
+ hours (/ minutes 60)
+ remaining-time
+ (format "%d:%02d" (truncate hours)
+ (- (truncate minutes) (* 60 (truncate hours))))))
+ (list (cons ?c (or (and energy
+ (number-to-string (round (* 1000 energy))))
+ "N/A"))
+ (cons ?p (or (and percents (number-to-string (round percents)))
+ "N/A"))
+ (cons ?r (or (and energy-rate
+ (concat (number-to-string energy-rate) " W"))
+ "N/A"))
+ (cons ?B (or (and state (cdr (assoc state battery-states)))
+ "unknown"))
+ (cons ?L (or (and online "on-line") "off-line"))
+ (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
+ (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
+ (cons ?h (or (and hours (number-to-string hours)) "N/A"))
+ (cons ?t (or remaining-time "N/A")))))
+
+
;;; `apm' interface for BSD.
(defun battery-bsd-apm ()
"Get APM status information from BSD apm binary.