-(defun ff/battery-info (path)
-
- (let ((state nil)
- (full nil)
- (charge nil)
- (rate nil))
-
- (with-temp-buffer
- (insert-file-contents-literally (concat path "/state"))
- (while (re-search-forward "^\\([a-z ]*\\): *\\(.*\\)$" nil t)
- (let ((field (match-string 1))
- (value (match-string 2)))
-
- (cond ((string= field "charging state")
- (cond ((string= value "charged") (setq state 'charged))
- ((string= value "charging") (setq state 'charging))
- ((string= value "discharging")(setq state 'discharging))
- (t (setq state 'unknown))))
-
- ((string= field "remaining capacity")
- (setq charge (string-to-number value)))
-
- ((string= field "present rate")
- (setq rate (string-to-number value)))))))
-
- (with-temp-buffer
- (insert-file-contents-literally (concat path "/info"))
- (while (re-search-forward "^\\([a-z ]*\\): *\\(.*\\)$" nil t)
- (let ((field (match-string 1))
- (value (match-string 2)))
-
- (cond ((string= field "last full capacity")
- (setq full (string-to-number value)))))))
-
- (list state full charge rate)))
-
-(defun ff/thermal-info (path)
- (let ((temperature nil))
- (with-temp-buffer
- (insert-file-contents-literally (concat path "/temperature"))
- (while (re-search-forward "^\\([a-z ]*\\): *\\(.*\\)$" nil t)
- (let ((field (match-string 1))
- (value (match-string 2)))
-
- (cond ((string= field "temperature")
- (setq temperature (string-to-number value)))))))
+(defun ff/file-first-line (file)
+ (with-temp-buffer
+ (insert-file-contents-literally file)
+ (buffer-substring (point-at-bol) (point-at-eol))))
+
+(defun ff/battery-discharging (l)
+ (and l (or (string= (ff/file-first-line (concat (car l) "/status")) "Discharging")
+ (ff/battery-discharging (cdr l)))))
+
+;; If there is one "Discharging" among the states of all the
+;; batteries, the global state is 'discharging. Otherwise, if there is
+;; a "Charging", the state is 'charging. If none is true, it is
+;; 'unknown
+(defun ff/battery-state (l)
+ (if l
+ (let ((u (ff/file-first-line (concat (car l) "/status"))))
+ (if (string= u "Discharging") 'discharging
+ (let ((s (ff/battery-state (cdr l))))
+ (if (eq s 'discharging) 'discharging
+ (if (or (eq s 'charging) (string= u "Charging"))
+ 'charging
+ 'unknown))
+ )
+ )
+ )
+ 'unknown))