Hi everyone,
I noticed that battery.el (battery
, display-battery-mode
) in Emacs is nonfunctional on the pocket reform (displaying N/A
for all values) despite sysfs exposing correct battery metrics in /sys/class/power_supply/BAT0
.
The cause of this seems to be that the detection logic in battery.el seems to rely on what value POWER_SUPPLY_PRESENT
holds in uevent, but as it doesn’t exist the detection logic fails. I’m not sure if this is an issue with how the battery is exposed to sysfs or a bug in battery.el
by assuming that POWER_SUPPLY_PRESENT
is a given?
I came up with a few lines of an advice function to bend the detection logic of battery-linux-sysfs
in battery.el to check for "POWER_SUPPLY_NAMEinstead of
POWER_SUPPLY_PRESENT` which isn’t ideal but allows me to calculate load and remaining time values:
(defun mnt-battery-detection (orig-fun &rest args)
"Override battery detection in `battery-linux-sysfs` to use POWER_SUPPLY_NAME."
(let ((battery-detected nil))
(dolist (dir (battery--find-linux-sysfs-batteries))
(with-temp-buffer
(ignore-errors
(insert-file-contents (expand-file-name "uevent" dir) nil nil nil t))
(goto-char (point-min))
(when (re-search-forward "POWER_SUPPLY_NAME=\\(.*\\)" nil t)
(setq battery-detected t))))
(if battery-detected
(apply orig-fun args)
'((?L . "N/A") (?B . "N/A") (?p . "N/A")))))
(advice-add 'battery-linux-sysfs :around #'mnt-battery-detection)
I don’t know enough on sysfs and how uevent is composed to make an educated guess whether that’s expected behavior. Maybe the workaround is helpful for folks who came across the same detection issue in battery.el.
Cheers!