Commit | Line | Data |
---|---|---|
c38e0c97 | 1 | ;;; battery.el --- display battery status information -*- coding: utf-8 -*- |
6b279740 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1997-1998, 2000-2014 Free Software Foundation, Inc. |
6b279740 | 4 | |
3b361901 | 5 | ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org> |
d5323ba0 | 6 | ;; Keywords: hardware |
6b279740 | 7 | |
f12b0b96 | 8 | ;; This file is part of GNU Emacs. |
6b279740 | 9 | |
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
6b279740 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
6b279740 | 14 | |
f12b0b96 | 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
6b279740 RS |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
6b279740 RS |
22 | |
23 | ;;; Commentary: | |
24 | ||
20d4381e JR |
25 | ;; There is at present support for GNU/Linux, OS X and Windows. This |
26 | ;; library supports both the `/proc/apm' file format of Linux version | |
27 | ;; 1.3.58 or newer and the `/proc/acpi/' directory structure of Linux | |
28 | ;; 2.4.20 and 2.6. Darwin (OS X) is supported by using the `pmset' | |
29 | ;; program. Windows is supported by the GetSystemPowerStatus API call. | |
6b279740 RS |
30 | |
31 | ;;; Code: | |
32 | ||
33 | (require 'timer) | |
f58e0fd5 | 34 | (eval-when-compile (require 'cl-lib)) |
6b279740 | 35 | \f |
4bef9110 SE |
36 | (defgroup battery nil |
37 | "Display battery status information." | |
38 | :prefix "battery-" | |
39 | :group 'hardware) | |
40 | ||
6decb6c2 SM |
41 | ;; Either BATn or yeeloong-bat, basically. |
42 | (defconst battery--linux-sysfs-regexp "[bB][aA][tT][0-9]?$") | |
43 | ||
4bef9110 | 44 | (defcustom battery-status-function |
6b279740 RS |
45 | (cond ((and (eq system-type 'gnu/linux) |
46 | (file-readable-p "/proc/apm")) | |
2b3cb60a EZ |
47 | 'battery-linux-proc-apm) |
48 | ((and (eq system-type 'gnu/linux) | |
49 | (file-directory-p "/proc/acpi/battery")) | |
71d21198 | 50 | 'battery-linux-proc-acpi) |
3660b4f5 CY |
51 | ((and (eq system-type 'gnu/linux) |
52 | (file-directory-p "/sys/class/power_supply/") | |
6decb6c2 SM |
53 | (directory-files "/sys/class/power_supply/" nil |
54 | battery--linux-sysfs-regexp)) | |
3660b4f5 | 55 | 'battery-linux-sysfs) |
19b748ad TM |
56 | ((and (eq system-type 'berkeley-unix) |
57 | (file-executable-p "/usr/sbin/apm")) | |
58 | 'battery-bsd-apm) | |
71d21198 | 59 | ((and (eq system-type 'darwin) |
785428c7 RF |
60 | (condition-case nil |
61 | (with-temp-buffer | |
248634e9 LK |
62 | (and (eq (call-process "pmset" nil t nil "-g" "ps") 0) |
63 | (> (buffer-size) 0))) | |
64 | (error nil))) | |
20d4381e | 65 | 'battery-pmset) |
0fda9b75 | 66 | ((fboundp 'w32-battery-status) |
20d4381e | 67 | 'w32-battery-status)) |
9201cc28 | 68 | "Function for getting battery status information. |
681e6b4b DL |
69 | The function has to return an alist of conversion definitions. |
70 | Its cons cells are of the form | |
6b279740 RS |
71 | |
72 | (CONVERSION . REPLACEMENT-TEXT) | |
73 | ||
74 | CONVERSION is the character code of a \"conversion specification\" | |
4bef9110 | 75 | introduced by a `%' character in a control string." |
681e6b4b | 76 | :type '(choice (const nil) function) |
4bef9110 | 77 | :group 'battery) |
6b279740 | 78 | |
4bef9110 | 79 | (defcustom battery-echo-area-format |
20d4381e | 80 | (cond ((eq battery-status-function 'battery-linux-proc-acpi) |
71d21198 | 81 | "Power %L, battery %B at %r (%p%% load, remaining time %t)") |
3660b4f5 | 82 | ((eq battery-status-function 'battery-linux-sysfs) |
54071013 | 83 | "Power %L, battery %B (%p%% load, remaining time %t)") |
71d21198 | 84 | ((eq battery-status-function 'battery-pmset) |
20d4381e JR |
85 | "%L power, battery %B (%p%% load, remaining time %t)") |
86 | (battery-status-function | |
87 | "Power %L, battery %B (%p%% load, remaining time %t)")) | |
9201cc28 | 88 | "Control string formatting the string to display in the echo area. |
6b279740 RS |
89 | Ordinary characters in the control string are printed as-is, while |
90 | conversion specifications introduced by a `%' character in the control | |
91 | string are substituted as defined by the current value of the variable | |
76815b2a RS |
92 | `battery-status-function'. Here are the ones generally available: |
93 | %c Current capacity (mAh or mWh) | |
94 | %r Current rate of charge or discharge | |
95 | %B Battery status (verbose) | |
96 | %b Battery status: empty means high, `-' means low, | |
97 | `!' means critical, and `+' means charging | |
98 | %d Temperature (in degrees Celsius) | |
99 | %L AC line status (verbose) | |
100 | %p Battery load percentage | |
101 | %m Remaining time (to charge or discharge) in minutes | |
102 | %h Remaining time (to charge or discharge) in hours | |
103 | %t Remaining time (to charge or discharge) in the form `h:min'" | |
4bef9110 SE |
104 | :type '(choice string (const nil)) |
105 | :group 'battery) | |
6b279740 RS |
106 | |
107 | (defvar battery-mode-line-string nil | |
108 | "String to display in the mode line.") | |
f4c4fc74 | 109 | ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t) |
6b279740 | 110 | |
43d5bf84 RS |
111 | (defcustom battery-mode-line-limit 100 |
112 | "Percentage of full battery load below which display battery status" | |
2bed3f04 | 113 | :version "24.1" |
43d5bf84 RS |
114 | :type 'integer |
115 | :group 'battery) | |
116 | ||
4bef9110 | 117 | (defcustom battery-mode-line-format |
20d4381e | 118 | (cond ((eq battery-status-function 'battery-linux-proc-acpi) |
c38e0c97 | 119 | "[%b%p%%,%d°C]") |
20d4381e | 120 | (battery-status-function |
71d21198 | 121 | "[%b%p%%]")) |
9201cc28 | 122 | "Control string formatting the string to display in the mode line. |
6b279740 RS |
123 | Ordinary characters in the control string are printed as-is, while |
124 | conversion specifications introduced by a `%' character in the control | |
125 | string are substituted as defined by the current value of the variable | |
76815b2a RS |
126 | `battery-status-function'. Here are the ones generally available: |
127 | %c Current capacity (mAh or mWh) | |
128 | %r Current rate of charge or discharge | |
129 | %B Battery status (verbose) | |
130 | %b Battery status: empty means high, `-' means low, | |
131 | `!' means critical, and `+' means charging | |
132 | %d Temperature (in degrees Celsius) | |
133 | %L AC line status (verbose) | |
134 | %p Battery load percentage | |
135 | %m Remaining time (to charge or discharge) in minutes | |
136 | %h Remaining time (to charge or discharge) in hours | |
137 | %t Remaining time (to charge or discharge) in the form `h:min'" | |
4bef9110 SE |
138 | :type '(choice string (const nil)) |
139 | :group 'battery) | |
140 | ||
141 | (defcustom battery-update-interval 60 | |
9201cc28 | 142 | "Seconds after which the battery status will be updated." |
4bef9110 SE |
143 | :type 'integer |
144 | :group 'battery) | |
6b279740 | 145 | |
71d21198 | 146 | (defcustom battery-load-low 25 |
9201cc28 | 147 | "Upper bound of low battery load percentage. |
71d21198 LK |
148 | A battery load percentage below this number is considered low." |
149 | :type 'integer | |
150 | :group 'battery) | |
151 | ||
152 | (defcustom battery-load-critical 10 | |
9201cc28 | 153 | "Upper bound of critical battery load percentage. |
71d21198 LK |
154 | A battery load percentage below this number is considered critical." |
155 | :type 'integer | |
156 | :group 'battery) | |
157 | ||
6b279740 RS |
158 | (defvar battery-update-timer nil |
159 | "Interval timer object.") | |
160 | ||
1720e508 | 161 | ;;;###autoload |
6b279740 RS |
162 | (defun battery () |
163 | "Display battery status information in the echo area. | |
b624447c | 164 | The text being displayed in the echo area is controlled by the variables |
6b279740 RS |
165 | `battery-echo-area-format' and `battery-status-function'." |
166 | (interactive) | |
167 | (message "%s" (if (and battery-echo-area-format battery-status-function) | |
168 | (battery-format battery-echo-area-format | |
169 | (funcall battery-status-function)) | |
170 | "Battery status not available"))) | |
171 | ||
1720e508 | 172 | ;;;###autoload |
73a6a972 | 173 | (define-minor-mode display-battery-mode |
06e21633 CY |
174 | "Toggle battery status display in mode line (Display Battery mode). |
175 | With a prefix argument ARG, enable Display Battery mode if ARG is | |
176 | positive, and disable it otherwise. If called from Lisp, enable | |
177 | the mode if ARG is omitted or nil. | |
178 | ||
179 | The text displayed in the mode line is controlled by | |
6b279740 | 180 | `battery-mode-line-format' and `battery-status-function'. |
06e21633 | 181 | The mode line is be updated every `battery-update-interval' |
6b279740 | 182 | seconds." |
62631b2c | 183 | :global t :group 'battery |
6b279740 RS |
184 | (setq battery-mode-line-string "") |
185 | (or global-mode-string (setq global-mode-string '(""))) | |
6b279740 | 186 | (and battery-update-timer (cancel-timer battery-update-timer)) |
f1cf7a31 JL |
187 | (if (and battery-status-function battery-mode-line-format) |
188 | (if (not display-battery-mode) | |
189 | (setq global-mode-string | |
190 | (delq 'battery-mode-line-string global-mode-string)) | |
191 | (add-to-list 'global-mode-string 'battery-mode-line-string t) | |
192 | (setq battery-update-timer (run-at-time nil battery-update-interval | |
193 | 'battery-update-handler)) | |
194 | (battery-update)) | |
195 | (message "Battery status not available") | |
196 | (setq display-battery-mode nil))) | |
6b279740 RS |
197 | |
198 | (defun battery-update-handler () | |
199 | (battery-update) | |
200 | (sit-for 0)) | |
201 | ||
202 | (defun battery-update () | |
203 | "Update battery status information in the mode line." | |
0507406b NR |
204 | (let* ((data (and battery-status-function (funcall battery-status-function))) |
205 | (percentage (car (read-from-string (cdr (assq ?p data)))))) | |
43d5bf84 RS |
206 | (setq battery-mode-line-string |
207 | (propertize (if (and battery-mode-line-format | |
0507406b NR |
208 | (numberp percentage) |
209 | (<= percentage battery-mode-line-limit)) | |
210 | (battery-format battery-mode-line-format data) | |
43d5bf84 RS |
211 | "") |
212 | 'face | |
0507406b NR |
213 | (and (numberp percentage) |
214 | (<= percentage battery-load-critical) | |
215 | 'error) | |
43d5bf84 | 216 | 'help-echo "Battery status information"))) |
6b279740 | 217 | (force-mode-line-update)) |
6b279740 RS |
218 | \f |
219 | ;;; `/proc/apm' interface for Linux. | |
220 | ||
221 | (defconst battery-linux-proc-apm-regexp | |
222 | (concat "^\\([^ ]+\\)" ; Driver version. | |
223 | " \\([^ ]+\\)" ; APM BIOS version. | |
224 | " 0x\\([0-9a-f]+\\)" ; APM BIOS flags. | |
225 | " 0x\\([0-9a-f]+\\)" ; AC line status. | |
226 | " 0x\\([0-9a-f]+\\)" ; Battery status. | |
227 | " 0x\\([0-9a-f]+\\)" ; Battery flags. | |
d5323ba0 KH |
228 | " \\(-?[0-9]+\\)%" ; Load percentage. |
229 | " \\(-?[0-9]+\\)" ; Remaining time. | |
6b279740 RS |
230 | " \\(.*\\)" ; Time unit. |
231 | "$") | |
232 | "Regular expression matching contents of `/proc/apm'.") | |
233 | ||
234 | (defun battery-linux-proc-apm () | |
30710442 | 235 | "Get APM status information from Linux (the kernel). |
6b279740 RS |
236 | This function works only with the new `/proc/apm' format introduced |
237 | in Linux version 1.3.58. | |
238 | ||
239 | The following %-sequences are provided: | |
240 | %v Linux driver version | |
241 | %V APM BIOS version | |
242 | %I APM BIOS status (verbose) | |
243 | %L AC line status (verbose) | |
244 | %B Battery status (verbose) | |
245 | %b Battery status, empty means high, `-' means low, | |
246 | `!' means critical, and `+' means charging | |
12d6b124 | 247 | %p Battery load percentage |
76815b2a RS |
248 | %s Remaining time (to charge or discharge) in seconds |
249 | %m Remaining time (to charge or discharge) in minutes | |
250 | %h Remaining time (to charge or discharge) in hours | |
251 | %t Remaining time (to charge or discharge) in the form `h:min'" | |
6b279740 RS |
252 | (let (driver-version bios-version bios-interface line-status |
253 | battery-status battery-status-symbol load-percentage | |
12d6b124 LK |
254 | seconds minutes hours remaining-time tem) |
255 | (with-temp-buffer | |
256 | (ignore-errors (insert-file-contents "/proc/apm")) | |
257 | (when (re-search-forward battery-linux-proc-apm-regexp) | |
258 | (setq driver-version (match-string 1)) | |
259 | (setq bios-version (match-string 2)) | |
260 | (setq tem (string-to-number (match-string 3) 16)) | |
261 | (if (not (logand tem 2)) | |
262 | (setq bios-interface "not supported") | |
263 | (setq bios-interface "enabled") | |
264 | (cond ((logand tem 16) (setq bios-interface "disabled")) | |
265 | ((logand tem 32) (setq bios-interface "disengaged"))) | |
266 | (setq tem (string-to-number (match-string 4) 16)) | |
267 | (cond ((= tem 0) (setq line-status "off-line")) | |
268 | ((= tem 1) (setq line-status "on-line")) | |
269 | ((= tem 2) (setq line-status "on backup"))) | |
270 | (setq tem (string-to-number (match-string 6) 16)) | |
271 | (if (= tem 255) | |
272 | (setq battery-status "N/A") | |
273 | (setq tem (string-to-number (match-string 5) 16)) | |
274 | (cond ((= tem 0) (setq battery-status "high" | |
275 | battery-status-symbol "")) | |
276 | ((= tem 1) (setq battery-status "low" | |
277 | battery-status-symbol "-")) | |
278 | ((= tem 2) (setq battery-status "critical" | |
279 | battery-status-symbol "!")) | |
280 | ((= tem 3) (setq battery-status "charging" | |
281 | battery-status-symbol "+"))) | |
282 | (setq load-percentage (match-string 7)) | |
283 | (setq seconds (string-to-number (match-string 8))) | |
284 | (and (string-equal (match-string 9) "min") | |
285 | (setq seconds (* 60 seconds))) | |
286 | (setq minutes (/ seconds 60) | |
287 | hours (/ seconds 3600)) | |
288 | (setq remaining-time | |
289 | (format "%d:%02d" hours (- minutes (* 60 hours)))))))) | |
d5323ba0 KH |
290 | (list (cons ?v (or driver-version "N/A")) |
291 | (cons ?V (or bios-version "N/A")) | |
292 | (cons ?I (or bios-interface "N/A")) | |
293 | (cons ?L (or line-status "N/A")) | |
294 | (cons ?B (or battery-status "N/A")) | |
295 | (cons ?b (or battery-status-symbol "")) | |
296 | (cons ?p (or load-percentage "N/A")) | |
297 | (cons ?s (or (and seconds (number-to-string seconds)) "N/A")) | |
298 | (cons ?m (or (and minutes (number-to-string minutes)) "N/A")) | |
299 | (cons ?h (or (and hours (number-to-string hours)) "N/A")) | |
300 | (cons ?t (or remaining-time "N/A"))))) | |
6b279740 RS |
301 | |
302 | \f | |
2b3cb60a EZ |
303 | ;;; `/proc/acpi/' interface for Linux. |
304 | ||
305 | (defun battery-linux-proc-acpi () | |
30710442 | 306 | "Get ACPI status information from Linux (the kernel). |
3660b4f5 | 307 | This function works only with the `/proc/acpi/' format introduced |
2b3cb60a EZ |
308 | in Linux version 2.4.20 and 2.6.0. |
309 | ||
310 | The following %-sequences are provided: | |
311 | %c Current capacity (mAh) | |
12d6b124 | 312 | %r Current rate |
2b3cb60a EZ |
313 | %B Battery status (verbose) |
314 | %b Battery status, empty means high, `-' means low, | |
315 | `!' means critical, and `+' means charging | |
316 | %d Temperature (in degrees Celsius) | |
317 | %L AC line status (verbose) | |
12d6b124 | 318 | %p Battery load percentage |
76815b2a RS |
319 | %m Remaining time (to charge or discharge) in minutes |
320 | %h Remaining time (to charge or discharge) in hours | |
321 | %t Remaining time (to charge or discharge) in the form `h:min'" | |
0f4a15f8 | 322 | (let ((design-capacity 0) |
d0600240 RS |
323 | (last-full-capacity 0) |
324 | full-capacity | |
0f4a15f8 SM |
325 | (warn 0) |
326 | (low 0) | |
327 | capacity rate rate-type charging-state minutes hours) | |
328 | ;; ACPI provides information about each battery present in the system in | |
329 | ;; a separate subdirectory. We are going to merge the available | |
d66fe334 EZ |
330 | ;; information together since displaying for a variable amount of |
331 | ;; batteries seems overkill for format-strings. | |
332 | (with-temp-buffer | |
0f4a15f8 SM |
333 | (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/" |
334 | t "\\`[^.]"))) | |
335 | (erase-buffer) | |
336 | (ignore-errors (insert-file-contents (expand-file-name "state" dir))) | |
337 | (when (re-search-forward "present: +yes$" nil t) | |
338 | (and (re-search-forward "charging state: +\\(.*\\)$" nil t) | |
76815b2a | 339 | (member charging-state '("unknown" "charged" nil)) |
0f4a15f8 SM |
340 | ;; On most multi-battery systems, most of the time only one |
341 | ;; battery is "charging"/"discharging", the others are | |
342 | ;; "unknown". | |
343 | (setq charging-state (match-string 1))) | |
344 | (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$" | |
345 | nil t) | |
6a8c9eaf DN |
346 | (setq rate (+ (or rate 0) (string-to-number (match-string 1)))) |
347 | (when (> rate 0) | |
348 | (setq rate-type (or (and rate-type | |
0f4a15f8 SM |
349 | (if (string= rate-type (match-string 2)) |
350 | rate-type | |
351 | (error | |
352 | "Inconsistent rate types (%s vs. %s)" | |
353 | rate-type (match-string 2)))) | |
6a8c9eaf | 354 | (match-string 2))))) |
0f4a15f8 SM |
355 | (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$" |
356 | nil t) | |
357 | (setq capacity | |
358 | (+ (or capacity 0) (string-to-number (match-string 1)))))) | |
359 | (goto-char (point-max)) | |
360 | (ignore-errors (insert-file-contents (expand-file-name "info" dir))) | |
361 | (when (re-search-forward "present: +yes$" nil t) | |
362 | (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$" | |
363 | nil t) | |
f58e0fd5 | 364 | (cl-incf design-capacity (string-to-number (match-string 1)))) |
d0600240 RS |
365 | (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$" |
366 | nil t) | |
f58e0fd5 | 367 | (cl-incf last-full-capacity (string-to-number (match-string 1)))) |
0f4a15f8 SM |
368 | (when (re-search-forward |
369 | "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t) | |
f58e0fd5 | 370 | (cl-incf warn (string-to-number (match-string 1)))) |
0f4a15f8 SM |
371 | (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$" |
372 | nil t) | |
f58e0fd5 | 373 | (cl-incf low (string-to-number (match-string 1))))))) |
d0600240 RS |
374 | (setq full-capacity (if (> last-full-capacity 0) |
375 | last-full-capacity design-capacity)) | |
2b3cb60a | 376 | (and capacity rate |
062db3ec EZ |
377 | (setq minutes (if (zerop rate) 0 |
378 | (floor (* (/ (float (if (string= charging-state | |
379 | "charging") | |
d0600240 RS |
380 | (- full-capacity capacity) |
381 | capacity)) | |
382 | rate) | |
383 | 60))) | |
2b3cb60a EZ |
384 | hours (/ minutes 60))) |
385 | (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A")) | |
785428c7 RF |
386 | (cons ?L (or (battery-search-for-one-match-in-files |
387 | (mapcar (lambda (e) (concat e "/state")) | |
b8e3d88f RS |
388 | (ignore-errors |
389 | (directory-files "/proc/acpi/ac_adapter/" | |
390 | t "\\`[^.]"))) | |
785428c7 RF |
391 | "state: +\\(.*\\)$" 1) |
392 | ||
2b3cb60a | 393 | "N/A")) |
785428c7 RF |
394 | (cons ?d (or (battery-search-for-one-match-in-files |
395 | (mapcar (lambda (e) (concat e "/temperature")) | |
b8e3d88f RS |
396 | (ignore-errors |
397 | (directory-files "/proc/acpi/thermal_zone/" | |
398 | t "\\`[^.]"))) | |
785428c7 RF |
399 | "temperature: +\\([0-9]+\\) C$" 1) |
400 | ||
2b3cb60a | 401 | "N/A")) |
062db3ec EZ |
402 | (cons ?r (or (and rate (concat (number-to-string rate) " " |
403 | rate-type)) "N/A")) | |
2b3cb60a EZ |
404 | (cons ?B (or charging-state "N/A")) |
405 | (cons ?b (or (and (string= charging-state "charging") "+") | |
c93b1b4b JL |
406 | (and capacity (< capacity low) "!") |
407 | (and capacity (< capacity warn) "-") | |
2b3cb60a EZ |
408 | "")) |
409 | (cons ?h (or (and hours (number-to-string hours)) "N/A")) | |
410 | (cons ?m (or (and minutes (number-to-string minutes)) "N/A")) | |
411 | (cons ?t (or (and minutes | |
412 | (format "%d:%02d" hours (- minutes (* 60 hours)))) | |
413 | "N/A")) | |
d0600240 | 414 | (cons ?p (or (and full-capacity capacity |
5c79f850 | 415 | (> full-capacity 0) |
2b3cb60a EZ |
416 | (number-to-string |
417 | (floor (/ capacity | |
d0600240 | 418 | (/ (float full-capacity) 100))))) |
2b3cb60a EZ |
419 | "N/A"))))) |
420 | ||
421 | \f | |
3660b4f5 CY |
422 | ;;; `/sys/class/power_supply/BATN' interface for Linux. |
423 | ||
424 | (defun battery-linux-sysfs () | |
425 | "Get ACPI status information from Linux kernel. | |
426 | This function works only with the new `/sys/class/power_supply/' | |
427 | format introduced in Linux version 2.4.25. | |
428 | ||
429 | The following %-sequences are provided: | |
430 | %c Current capacity (mAh or mWh) | |
728a1f2b | 431 | %r Current rate |
3660b4f5 | 432 | %B Battery status (verbose) |
728a1f2b | 433 | %d Temperature (in degrees Celsius) |
3660b4f5 | 434 | %p Battery load percentage |
728a1f2b JC |
435 | %L AC line status (verbose) |
436 | %m Remaining time (to charge or discharge) in minutes | |
437 | %h Remaining time (to charge or discharge) in hours | |
438 | %t Remaining time (to charge or discharge) in the form `h:min'" | |
439 | (let (charging-state rate temperature hours | |
3660b4f5 CY |
440 | (charge-full 0.0) |
441 | (charge-now 0.0) | |
442 | (energy-full 0.0) | |
443 | (energy-now 0.0)) | |
444 | ;; SysFS provides information about each battery present in the | |
445 | ;; system in a separate subdirectory. We are going to merge the | |
446 | ;; available information together. | |
447 | (with-temp-buffer | |
448 | (dolist (dir (ignore-errors | |
449 | (directory-files | |
6decb6c2 SM |
450 | "/sys/class/power_supply/" t |
451 | battery--linux-sysfs-regexp))) | |
3660b4f5 CY |
452 | (erase-buffer) |
453 | (ignore-errors (insert-file-contents | |
454 | (expand-file-name "uevent" dir))) | |
455 | (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t) | |
456 | (goto-char (point-min)) | |
457 | (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t) | |
458 | (member charging-state '("Unknown" "Full" nil)) | |
459 | (setq charging-state (match-string 1))) | |
9e37f3b9 | 460 | (goto-char (point-min)) |
728a1f2b JC |
461 | (when (re-search-forward |
462 | "POWER_SUPPLY_\\(CURRENT\\|POWER\\)_NOW=\\([0-9]*\\)$" | |
463 | nil t) | |
464 | (setq rate (float (string-to-number (match-string 2))))) | |
9e37f3b9 | 465 | (goto-char (point-min)) |
728a1f2b JC |
466 | (when (re-search-forward "POWER_SUPPLY_TEMP=\\([0-9]*\\)$" nil t) |
467 | (setq temperature (match-string 1))) | |
9e37f3b9 | 468 | (goto-char (point-min)) |
3660b4f5 CY |
469 | (let (full-string now-string) |
470 | ;; Sysfs may list either charge (mAh) or energy (mWh). | |
471 | ;; Keep track of both, and choose which to report later. | |
472 | (cond ((and (re-search-forward | |
473 | "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t) | |
474 | (setq full-string (match-string 1)) | |
475 | (re-search-forward | |
476 | "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t) | |
477 | (setq now-string (match-string 1))) | |
478 | (setq charge-full (+ charge-full | |
479 | (string-to-number full-string)) | |
480 | charge-now (+ charge-now | |
481 | (string-to-number now-string)))) | |
9e37f3b9 RS |
482 | ((and (progn (goto-char (point-min)) t) |
483 | (re-search-forward | |
3660b4f5 CY |
484 | "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t) |
485 | (setq full-string (match-string 1)) | |
486 | (re-search-forward | |
487 | "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t) | |
488 | (setq now-string (match-string 1))) | |
489 | (setq energy-full (+ energy-full | |
490 | (string-to-number full-string)) | |
491 | energy-now (+ energy-now | |
728a1f2b JC |
492 | (string-to-number now-string)))))) |
493 | (goto-char (point-min)) | |
494 | (when (and energy-now rate (not (zerop rate)) | |
495 | (re-search-forward | |
496 | "POWER_SUPPLY_VOLTAGE_NOW=\\([0-9]*\\)$" nil t)) | |
497 | (let ((remaining (if (string= charging-state "Discharging") | |
498 | energy-now | |
499 | (- energy-full energy-now)))) | |
500 | (setq hours (/ (/ (* remaining (string-to-number | |
501 | (match-string 1))) | |
502 | rate) | |
503 | 10000000.0))))))) | |
3660b4f5 CY |
504 | (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0)) |
505 | (number-to-string charge-now)) | |
506 | ((or (> energy-full 0) (> energy-now 0)) | |
507 | (number-to-string energy-now)) | |
508 | (t "N/A"))) | |
728a1f2b JC |
509 | (cons ?r (if rate (format "%.1f" (/ rate 1000000.0)) "N/A")) |
510 | (cons ?m (if hours (format "%d" (* hours 60)) "N/A")) | |
511 | (cons ?h (if hours (format "%d" hours) "N/A")) | |
512 | (cons ?t (if hours | |
513 | (format "%d:%02d" hours (* (- hours (floor hours)) 60)) | |
514 | "N/A")) | |
515 | (cons ?d (or temperature "N/A")) | |
3660b4f5 | 516 | (cons ?B (or charging-state "N/A")) |
54071013 | 517 | (cons ?p (cond ((and (> charge-full 0) (> charge-now 0)) |
3660b4f5 CY |
518 | (format "%.1f" |
519 | (/ (* 100 charge-now) charge-full))) | |
520 | ((> energy-full 0) | |
521 | (format "%.1f" | |
522 | (/ (* 100 energy-now) energy-full))) | |
523 | (t "N/A"))) | |
524 | (cons ?L (if (file-readable-p "/sys/class/power_supply/AC/online") | |
525 | (if (battery-search-for-one-match-in-files | |
526 | (list "/sys/class/power_supply/AC/online" | |
527 | "/sys/class/power_supply/ACAD/online") | |
528 | "1" 0) | |
529 | "AC" | |
530 | "BAT") | |
531 | "N/A"))))) | |
19b748ad TM |
532 | |
533 | \f | |
534 | ;;; `apm' interface for BSD. | |
535 | (defun battery-bsd-apm () | |
536 | "Get APM status information from BSD apm binary. | |
537 | The following %-sequences are provided: | |
538 | %L AC line status (verbose) | |
539 | %B Battery status (verbose) | |
540 | %b Battery status, empty means high, `-' means low, | |
541 | `!' means critical, and `+' means charging | |
542 | %P Advanced power saving mode state (verbose) | |
543 | %p Battery charge percentage | |
544 | %s Remaining battery charge time in seconds | |
545 | %m Remaining battery charge time in minutes | |
546 | %h Remaining battery charge time in hours | |
547 | %t Remaining battery charge time in the form `h:min'" | |
548 | (let* ((os-name (car (split-string | |
549 | (shell-command-to-string "/usr/bin/uname")))) | |
550 | (apm-flag (if (equal os-name "OpenBSD") "P" "s")) | |
551 | (apm-cmd (concat "/usr/sbin/apm -ablm" apm-flag)) | |
552 | (apm-output (split-string (shell-command-to-string apm-cmd))) | |
553 | ;; Battery status | |
554 | (battery-status | |
555 | (let ((stat (string-to-number (nth 0 apm-output)))) | |
556 | (cond ((eq stat 0) '("high" . "")) | |
557 | ((eq stat 1) '("low" . "-")) | |
558 | ((eq stat 2) '("critical" . "!")) | |
559 | ((eq stat 3) '("charging" . "+")) | |
560 | ((eq stat 4) '("absent" . nil))))) | |
561 | ;; Battery percentage | |
562 | (battery-percentage (nth 1 apm-output)) | |
563 | ;; Battery life | |
564 | (battery-life (nth 2 apm-output)) | |
565 | ;; AC status | |
566 | (line-status | |
567 | (let ((ac (string-to-number (nth 3 apm-output)))) | |
568 | (cond ((eq ac 0) "disconnected") | |
569 | ((eq ac 1) "connected") | |
570 | ((eq ac 2) "backup power")))) | |
571 | ;; Advanced power savings mode | |
572 | (apm-mode | |
573 | (let ((apm (string-to-number (nth 4 apm-output)))) | |
574 | (if (string= os-name "OpenBSD") | |
575 | (cond ((eq apm 0) "manual") | |
576 | ((eq apm 1) "automatic") | |
577 | ((eq apm 2) "cool running")) | |
578 | (if (eq apm 1) "on" "off")))) | |
579 | seconds minutes hours remaining-time) | |
580 | (unless (member battery-life '("unknown" "-1")) | |
581 | (if (member os-name '("OpenBSD" "NetBSD")) | |
582 | (setq minutes (string-to-number battery-life) | |
583 | seconds (* 60 minutes)) | |
584 | (setq seconds (string-to-number battery-life) | |
585 | minutes (truncate (/ seconds 60)))) | |
586 | (setq hours (truncate (/ minutes 60)) | |
587 | remaining-time (format "%d:%02d" hours | |
588 | (- minutes (* 60 hours))))) | |
589 | (list (cons ?L (or line-status "N/A")) | |
590 | (cons ?B (or (car battery-status) "N/A")) | |
591 | (cons ?b (or (cdr battery-status) "N/A")) | |
592 | (cons ?p (if (string= battery-percentage "255") | |
593 | "N/A" | |
594 | battery-percentage)) | |
595 | (cons ?P (or apm-mode "N/A")) | |
596 | (cons ?s (or (and seconds (number-to-string seconds)) "N/A")) | |
597 | (cons ?m (or (and minutes (number-to-string minutes)) "N/A")) | |
598 | (cons ?h (or (and hours (number-to-string hours)) "N/A")) | |
599 | (cons ?t (or remaining-time "N/A"))))) | |
600 | ||
3660b4f5 | 601 | \f |
71d21198 LK |
602 | ;;; `pmset' interface for Darwin (OS X). |
603 | ||
604 | (defun battery-pmset () | |
605 | "Get battery status information using `pmset'. | |
606 | ||
607 | The following %-sequences are provided: | |
608 | %L Power source (verbose) | |
609 | %B Battery status (verbose) | |
610 | %b Battery status, empty means high, `-' means low, | |
611 | `!' means critical, and `+' means charging | |
612 | %p Battery load percentage | |
613 | %h Remaining time in hours | |
614 | %m Remaining time in minutes | |
615 | %t Remaining time in the form `h:min'" | |
616 | (let (power-source load-percentage battery-status battery-status-symbol | |
617 | remaining-time hours minutes) | |
618 | (with-temp-buffer | |
619 | (ignore-errors (call-process "pmset" nil t nil "-g" "ps")) | |
620 | (goto-char (point-min)) | |
d5f1282f | 621 | (when (re-search-forward "\\(?:Currentl?y\\|Now\\) drawing from '\\(AC\\|Battery\\) Power'" nil t) |
71d21198 LK |
622 | (setq power-source (match-string 1)) |
623 | (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t) | |
624 | (when (looking-at "\\([0-9]\\{1,3\\}\\)%") | |
625 | (setq load-percentage (match-string 1)) | |
626 | (goto-char (match-end 0)) | |
627 | (cond ((looking-at "; charging") | |
628 | (setq battery-status "charging" | |
629 | battery-status-symbol "+")) | |
630 | ((< (string-to-number load-percentage) battery-load-low) | |
631 | (setq battery-status "low" | |
632 | battery-status-symbol "-")) | |
633 | ((< (string-to-number load-percentage) battery-load-critical) | |
634 | (setq battery-status "critical" | |
635 | battery-status-symbol "!")) | |
636 | (t | |
637 | (setq battery-status "high" | |
638 | battery-status-symbol ""))) | |
639 | (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t) | |
640 | (setq remaining-time (match-string 1)) | |
641 | (let ((h (string-to-number (match-string 2))) | |
642 | (m (string-to-number (match-string 3)))) | |
643 | (setq hours (number-to-string (+ h (if (< m 30) 0 1))) | |
644 | minutes (number-to-string (+ (* h 60) m))))))))) | |
645 | (list (cons ?L (or power-source "N/A")) | |
646 | (cons ?p (or load-percentage "N/A")) | |
647 | (cons ?B (or battery-status "N/A")) | |
648 | (cons ?b (or battery-status-symbol "")) | |
649 | (cons ?h (or hours "N/A")) | |
650 | (cons ?m (or minutes "N/A")) | |
651 | (cons ?t (or remaining-time "N/A"))))) | |
652 | ||
653 | \f | |
6b279740 RS |
654 | ;;; Private functions. |
655 | ||
656 | (defun battery-format (format alist) | |
657 | "Substitute %-sequences in FORMAT." | |
0f4a15f8 SM |
658 | (replace-regexp-in-string |
659 | "%." | |
660 | (lambda (str) | |
661 | (let ((char (aref str 1))) | |
662 | (if (eq char ?%) "%" | |
663 | (or (cdr (assoc char alist)) "")))) | |
664 | format t t)) | |
6b279740 | 665 | |
785428c7 RF |
666 | (defun battery-search-for-one-match-in-files (files regexp match-num) |
667 | "Search REGEXP in the content of the files listed in FILES. | |
f14bbeeb | 668 | If a match occurred, return the parenthesized expression numbered by |
785428c7 RF |
669 | MATCH-NUM in the match. Otherwise, return nil." |
670 | (with-temp-buffer | |
671 | (catch 'found | |
672 | (dolist (file files) | |
673 | (and (ignore-errors (insert-file-contents file nil nil nil 'replace)) | |
674 | (re-search-forward regexp nil t) | |
675 | (throw 'found (match-string match-num))))))) | |
676 | ||
6b279740 RS |
677 | \f |
678 | (provide 'battery) | |
679 | ||
680 | ;;; battery.el ends here |