]> code.delx.au - gnu-emacs/blob - lisp/battery.el
Add 2012 to FSF copyright years for Emacs files
[gnu-emacs] / lisp / battery.el
1 ;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
2
3 ;; Copyright (C) 1997-1998, 2000-2012 Free Software Foundation, Inc.
4
5 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
6 ;; Keywords: hardware
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
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
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
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.
30
31 ;;; Code:
32
33 (require 'timer)
34 (eval-when-compile (require 'cl))
35
36 \f
37 (defgroup battery nil
38 "Display battery status information."
39 :prefix "battery-"
40 :group 'hardware)
41
42 (defcustom battery-status-function
43 (cond ((and (eq system-type 'gnu/linux)
44 (file-readable-p "/proc/apm"))
45 'battery-linux-proc-apm)
46 ((and (eq system-type 'gnu/linux)
47 (file-directory-p "/proc/acpi/battery"))
48 'battery-linux-proc-acpi)
49 ((and (eq system-type 'gnu/linux)
50 (file-directory-p "/sys/class/power_supply/")
51 (directory-files "/sys/class/power_supply/" nil "BAT[0-9]$"))
52 'battery-linux-sysfs)
53 ((and (eq system-type 'gnu/linux)
54 (file-directory-p "/sys/class/power_supply/yeeloong-bat/")
55 (directory-files "/sys/class/power_supply/yeeloong-bat/" nil "charge_"))
56 'battery-yeeloong-sysfs)
57 ((and (eq system-type 'darwin)
58 (condition-case nil
59 (with-temp-buffer
60 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
61 (> (buffer-size) 0)))
62 (error nil)))
63 'battery-pmset)
64 ((eq system-type 'windows-nt)
65 'w32-battery-status))
66 "Function for getting battery status information.
67 The function has to return an alist of conversion definitions.
68 Its cons cells are of the form
69
70 (CONVERSION . REPLACEMENT-TEXT)
71
72 CONVERSION is the character code of a \"conversion specification\"
73 introduced by a `%' character in a control string."
74 :type '(choice (const nil) function)
75 :group 'battery)
76
77 (defcustom battery-echo-area-format
78 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
79 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
80 ((eq battery-status-function 'battery-linux-sysfs)
81 "Power %L, battery %B (%p%% load)")
82 ((eq battery-status-function 'battery-pmset)
83 "%L power, battery %B (%p%% load, remaining time %t)")
84 ((eq battery-status-function 'battery-yeeloong-sysfs)
85 "%L power, battery %B (%p%% load, remaining time %t)")
86 (battery-status-function
87 "Power %L, battery %B (%p%% load, remaining time %t)"))
88 "Control string formatting the string to display in the echo area.
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
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'"
104 :type '(choice string (const nil))
105 :group 'battery)
106
107 (defvar battery-mode-line-string nil
108 "String to display in the mode line.")
109 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
110
111 (defcustom battery-mode-line-limit 100
112 "Percentage of full battery load below which display battery status"
113 :type 'integer
114 :group 'battery)
115
116 (defcustom battery-mode-line-format
117 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
118 "[%b%p%%,%d°C]")
119 (battery-status-function
120 "[%b%p%%]"))
121 "Control string formatting the string to display in the mode line.
122 Ordinary characters in the control string are printed as-is, while
123 conversion specifications introduced by a `%' character in the control
124 string are substituted as defined by the current value of the variable
125 `battery-status-function'. Here are the ones generally available:
126 %c Current capacity (mAh or mWh)
127 %r Current rate of charge or discharge
128 %B Battery status (verbose)
129 %b Battery status: empty means high, `-' means low,
130 `!' means critical, and `+' means charging
131 %d Temperature (in degrees Celsius)
132 %L AC line status (verbose)
133 %p Battery load percentage
134 %m Remaining time (to charge or discharge) in minutes
135 %h Remaining time (to charge or discharge) in hours
136 %t Remaining time (to charge or discharge) in the form `h:min'"
137 :type '(choice string (const nil))
138 :group 'battery)
139
140 (defcustom battery-update-interval 60
141 "Seconds after which the battery status will be updated."
142 :type 'integer
143 :group 'battery)
144
145 (defcustom battery-load-low 25
146 "Upper bound of low battery load percentage.
147 A battery load percentage below this number is considered low."
148 :type 'integer
149 :group 'battery)
150
151 (defcustom battery-load-critical 10
152 "Upper bound of critical battery load percentage.
153 A battery load percentage below this number is considered critical."
154 :type 'integer
155 :group 'battery)
156
157 (defvar battery-update-timer nil
158 "Interval timer object.")
159
160 ;;;###autoload
161 (defun battery ()
162 "Display battery status information in the echo area.
163 The text being displayed in the echo area is controlled by the variables
164 `battery-echo-area-format' and `battery-status-function'."
165 (interactive)
166 (message "%s" (if (and battery-echo-area-format battery-status-function)
167 (battery-format battery-echo-area-format
168 (funcall battery-status-function))
169 "Battery status not available")))
170
171 ;;;###autoload
172 (define-minor-mode display-battery-mode
173 "Toggle battery status display in mode line (Display Battery mode).
174 With a prefix argument ARG, enable Display Battery mode if ARG is
175 positive, and disable it otherwise. If called from Lisp, enable
176 the mode if ARG is omitted or nil.
177
178 The text displayed in the mode line is controlled by
179 `battery-mode-line-format' and `battery-status-function'.
180 The mode line is be updated every `battery-update-interval'
181 seconds."
182 :global t :group 'battery
183 (setq battery-mode-line-string "")
184 (or global-mode-string (setq global-mode-string '("")))
185 (and battery-update-timer (cancel-timer battery-update-timer))
186 (if (and battery-status-function battery-mode-line-format)
187 (if (not display-battery-mode)
188 (setq global-mode-string
189 (delq 'battery-mode-line-string global-mode-string))
190 (add-to-list 'global-mode-string 'battery-mode-line-string t)
191 (setq battery-update-timer (run-at-time nil battery-update-interval
192 'battery-update-handler))
193 (battery-update))
194 (message "Battery status not available")
195 (setq display-battery-mode nil)))
196
197 (defun battery-update-handler ()
198 (battery-update)
199 (sit-for 0))
200
201 (defun battery-update ()
202 "Update battery status information in the mode line."
203 (let ((data (and battery-status-function (funcall battery-status-function))))
204 (setq battery-mode-line-string
205 (propertize (if (and battery-mode-line-format
206 (<= (car (read-from-string (cdr (assq ?p data))))
207 battery-mode-line-limit))
208 (battery-format
209 battery-mode-line-format
210 data)
211 "")
212 'face
213 (and (<= (car (read-from-string (cdr (assq ?p data))))
214 battery-load-critical)
215 'error)
216 'help-echo "Battery status information")))
217 (force-mode-line-update))
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.
228 " \\(-?[0-9]+\\)%" ; Load percentage.
229 " \\(-?[0-9]+\\)" ; Remaining time.
230 " \\(.*\\)" ; Time unit.
231 "$")
232 "Regular expression matching contents of `/proc/apm'.")
233
234 (defun battery-linux-proc-apm ()
235 "Get APM status information from Linux (the kernel).
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
247 %p Battery load percentage
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'"
252 (let (driver-version bios-version bios-interface line-status
253 battery-status battery-status-symbol load-percentage
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))))))))
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")))))
301
302 \f
303 ;;; `/proc/acpi/' interface for Linux.
304
305 (defun battery-linux-proc-acpi ()
306 "Get ACPI status information from Linux (the kernel).
307 This function works only with the `/proc/acpi/' format introduced
308 in Linux version 2.4.20 and 2.6.0.
309
310 The following %-sequences are provided:
311 %c Current capacity (mAh)
312 %r Current rate
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)
318 %p Battery load percentage
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'"
322 (let ((design-capacity 0)
323 (last-full-capacity 0)
324 full-capacity
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
330 ;; information together since displaying for a variable amount of
331 ;; batteries seems overkill for format-strings.
332 (with-temp-buffer
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)
339 (member charging-state '("unknown" "charged" nil))
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)
346 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
347 rate-type (or (and rate-type
348 (if (string= rate-type (match-string 2))
349 rate-type
350 (error
351 "Inconsistent rate types (%s vs. %s)"
352 rate-type (match-string 2))))
353 (match-string 2))))
354 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
355 nil t)
356 (setq capacity
357 (+ (or capacity 0) (string-to-number (match-string 1))))))
358 (goto-char (point-max))
359 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
360 (when (re-search-forward "present: +yes$" nil t)
361 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
362 nil t)
363 (incf design-capacity (string-to-number (match-string 1))))
364 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
365 nil t)
366 (incf last-full-capacity (string-to-number (match-string 1))))
367 (when (re-search-forward
368 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
369 (incf warn (string-to-number (match-string 1))))
370 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
371 nil t)
372 (incf low (string-to-number (match-string 1)))))))
373 (setq full-capacity (if (> last-full-capacity 0)
374 last-full-capacity design-capacity))
375 (and capacity rate
376 (setq minutes (if (zerop rate) 0
377 (floor (* (/ (float (if (string= charging-state
378 "charging")
379 (- full-capacity capacity)
380 capacity))
381 rate)
382 60)))
383 hours (/ minutes 60)))
384 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
385 (cons ?L (or (battery-search-for-one-match-in-files
386 (mapcar (lambda (e) (concat e "/state"))
387 (ignore-errors
388 (directory-files "/proc/acpi/ac_adapter/"
389 t "\\`[^.]")))
390 "state: +\\(.*\\)$" 1)
391
392 "N/A"))
393 (cons ?d (or (battery-search-for-one-match-in-files
394 (mapcar (lambda (e) (concat e "/temperature"))
395 (ignore-errors
396 (directory-files "/proc/acpi/thermal_zone/"
397 t "\\`[^.]")))
398 "temperature: +\\([0-9]+\\) C$" 1)
399
400 "N/A"))
401 (cons ?r (or (and rate (concat (number-to-string rate) " "
402 rate-type)) "N/A"))
403 (cons ?B (or charging-state "N/A"))
404 (cons ?b (or (and (string= charging-state "charging") "+")
405 (and capacity (< capacity low) "!")
406 (and capacity (< capacity warn) "-")
407 ""))
408 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
409 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
410 (cons ?t (or (and minutes
411 (format "%d:%02d" hours (- minutes (* 60 hours))))
412 "N/A"))
413 (cons ?p (or (and full-capacity capacity
414 (> full-capacity 0)
415 (number-to-string
416 (floor (/ capacity
417 (/ (float full-capacity) 100)))))
418 "N/A")))))
419
420 \f
421 ;;; `/sys/class/power_supply/BATN' interface for Linux.
422
423 (defun battery-linux-sysfs ()
424 "Get ACPI status information from Linux kernel.
425 This function works only with the new `/sys/class/power_supply/'
426 format introduced in Linux version 2.4.25.
427
428 The following %-sequences are provided:
429 %c Current capacity (mAh or mWh)
430 %r Current rate
431 %B Battery status (verbose)
432 %d Temperature (in degrees Celsius)
433 %p Battery load percentage
434 %L AC line status (verbose)
435 %m Remaining time (to charge or discharge) in minutes
436 %h Remaining time (to charge or discharge) in hours
437 %t Remaining time (to charge or discharge) in the form `h:min'"
438 (let (charging-state rate temperature hours
439 (charge-full 0.0)
440 (charge-now 0.0)
441 (energy-full 0.0)
442 (energy-now 0.0))
443 ;; SysFS provides information about each battery present in the
444 ;; system in a separate subdirectory. We are going to merge the
445 ;; available information together.
446 (with-temp-buffer
447 (dolist (dir (ignore-errors
448 (directory-files
449 "/sys/class/power_supply/" t "BAT[0-9]$")))
450 (erase-buffer)
451 (ignore-errors (insert-file-contents
452 (expand-file-name "uevent" dir)))
453 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t)
454 (goto-char (point-min))
455 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t)
456 (member charging-state '("Unknown" "Full" nil))
457 (setq charging-state (match-string 1)))
458 (when (re-search-forward
459 "POWER_SUPPLY_\\(CURRENT\\|POWER\\)_NOW=\\([0-9]*\\)$"
460 nil t)
461 (setq rate (float (string-to-number (match-string 2)))))
462 (when (re-search-forward "POWER_SUPPLY_TEMP=\\([0-9]*\\)$" nil t)
463 (setq temperature (match-string 1)))
464 (let (full-string now-string)
465 ;; Sysfs may list either charge (mAh) or energy (mWh).
466 ;; Keep track of both, and choose which to report later.
467 (cond ((and (re-search-forward
468 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t)
469 (setq full-string (match-string 1))
470 (re-search-forward
471 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t)
472 (setq now-string (match-string 1)))
473 (setq charge-full (+ charge-full
474 (string-to-number full-string))
475 charge-now (+ charge-now
476 (string-to-number now-string))))
477 ((and (re-search-forward
478 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t)
479 (setq full-string (match-string 1))
480 (re-search-forward
481 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t)
482 (setq now-string (match-string 1)))
483 (setq energy-full (+ energy-full
484 (string-to-number full-string))
485 energy-now (+ energy-now
486 (string-to-number now-string))))))
487 (goto-char (point-min))
488 (when (and energy-now rate (not (zerop rate))
489 (re-search-forward
490 "POWER_SUPPLY_VOLTAGE_NOW=\\([0-9]*\\)$" nil t))
491 (let ((remaining (if (string= charging-state "Discharging")
492 energy-now
493 (- energy-full energy-now))))
494 (setq hours (/ (/ (* remaining (string-to-number
495 (match-string 1)))
496 rate)
497 10000000.0)))))))
498 (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0))
499 (number-to-string charge-now))
500 ((or (> energy-full 0) (> energy-now 0))
501 (number-to-string energy-now))
502 (t "N/A")))
503 (cons ?r (if rate (format "%.1f" (/ rate 1000000.0)) "N/A"))
504 (cons ?m (if hours (format "%d" (* hours 60)) "N/A"))
505 (cons ?h (if hours (format "%d" hours) "N/A"))
506 (cons ?t (if hours
507 (format "%d:%02d" hours (* (- hours (floor hours)) 60))
508 "N/A"))
509 (cons ?d (or temperature "N/A"))
510 (cons ?B (or charging-state "N/A"))
511 (cons ?p (cond ((> charge-full 0)
512 (format "%.1f"
513 (/ (* 100 charge-now) charge-full)))
514 ((> energy-full 0)
515 (format "%.1f"
516 (/ (* 100 energy-now) energy-full)))
517 (t "N/A")))
518 (cons ?L (if (file-readable-p "/sys/class/power_supply/AC/online")
519 (if (battery-search-for-one-match-in-files
520 (list "/sys/class/power_supply/AC/online"
521 "/sys/class/power_supply/ACAD/online")
522 "1" 0)
523 "AC"
524 "BAT")
525 "N/A")))))
526
527 (defun battery-yeeloong-sysfs ()
528 "Get ACPI status information from Linux (the kernel).
529 This function works only on the Lemote Yeeloong.
530
531 The following %-sequences are provided:
532 %c Current capacity (mAh)
533 %r Current rate
534 %B Battery status (verbose)
535 %b Battery status, empty means high, `-' means low,
536 `!' means critical, and `+' means charging
537 %L AC line status (verbose)
538 %p Battery load percentage
539 %m Remaining time (to charge or discharge) in minutes
540 %h Remaining time (to charge or discharge) in hours
541 %t Remaining time (to charge or discharge) in the form `h:min'"
542
543 (let (capacity
544 capacity-level
545 status
546 ac-online
547 hours
548 current-now
549 charge-full
550 charge-now)
551
552 (with-temp-buffer
553 (ignore-errors
554 (insert-file-contents "/sys/class/power_supply/yeeloong-bat/uevent")
555 (goto-char 1)
556 (search-forward "POWER_SUPPLY_CHARGE_NOW=")
557 (setq charge-now (read (current-buffer)))
558 (goto-char 1)
559 (search-forward "POWER_SUPPLY_CHARGE_FULL=")
560 (setq charge-full (read (current-buffer)))
561 (goto-char 1)
562 (search-forward "POWER_SUPPLY_CURRENT_NOW=")
563 (setq current-now (read (current-buffer)))
564 (goto-char 1)
565 (search-forward "POWER_SUPPLY_CAPACITY_LEVEL=")
566 (setq capacity-level (buffer-substring (point) (line-end-position)))
567 (goto-char 1)
568 (search-forward "POWER_SUPPLY_STATUS=")
569 (setq status (buffer-substring (point) (line-end-position))))
570
571 (erase-buffer)
572 (ignore-errors
573 (insert-file-contents
574 "/sys/class/power_supply/yeeloong-ac/online")
575 (goto-char 1)
576 (setq ac-online (read (current-buffer)))
577 (erase-buffer)))
578
579
580 (setq capacity (round (/ (* charge-now 100.0) charge-full)))
581 (when (and current-now (not (= current-now 0)))
582 (if (< current-now 0)
583 ;; Charging
584 (setq hours (/ (- charge-now charge-full) (+ 0.0 current-now)))
585 ;; Discharging
586 (setq hours (/ charge-now (+ 0.0 current-now)))))
587
588 (list (cons ?c (if charge-now
589 (number-to-string charge-now)
590 "N/A"))
591 (cons ?r current-now)
592 (cons ?B (cond ((equal capacity-level "Full") "full")
593 ((equal status "Charging") "charging")
594 ((equal capacity-level "Low") "low")
595 ((equal capacity-level "Critical") "critical")
596 (t "high")))
597 (cons ?b (cond ((equal capacity-level "Full") " ")
598 ((equal status "Charging") "+")
599 ((equal capacity-level "Low") "-")
600 ((equal capacity-level "Critical") "!")
601 (t " ")))
602 (cons ?h (if hours (number-to-string hours) "N/A"))
603 (cons ?m (if hours (number-to-string (* 60 hours)) "N/A"))
604 (cons ?t (if hours
605 (format "%d:%d"
606 (/ (round (* 60 hours)) 60)
607 (% (round (* 60 hours)) 60))
608 "N/A"))
609 (cons ?p (if capacity (number-to-string capacity) "N/A"))
610 (cons ?L (if (eq ac-online 1) "AC" "BAT")))))
611 \f
612 ;;; `pmset' interface for Darwin (OS X).
613
614 (defun battery-pmset ()
615 "Get battery status information using `pmset'.
616
617 The following %-sequences are provided:
618 %L Power source (verbose)
619 %B Battery status (verbose)
620 %b Battery status, empty means high, `-' means low,
621 `!' means critical, and `+' means charging
622 %p Battery load percentage
623 %h Remaining time in hours
624 %m Remaining time in minutes
625 %t Remaining time in the form `h:min'"
626 (let (power-source load-percentage battery-status battery-status-symbol
627 remaining-time hours minutes)
628 (with-temp-buffer
629 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
630 (goto-char (point-min))
631 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
632 (setq power-source (match-string 1))
633 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
634 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
635 (setq load-percentage (match-string 1))
636 (goto-char (match-end 0))
637 (cond ((looking-at "; charging")
638 (setq battery-status "charging"
639 battery-status-symbol "+"))
640 ((< (string-to-number load-percentage) battery-load-low)
641 (setq battery-status "low"
642 battery-status-symbol "-"))
643 ((< (string-to-number load-percentage) battery-load-critical)
644 (setq battery-status "critical"
645 battery-status-symbol "!"))
646 (t
647 (setq battery-status "high"
648 battery-status-symbol "")))
649 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
650 (setq remaining-time (match-string 1))
651 (let ((h (string-to-number (match-string 2)))
652 (m (string-to-number (match-string 3))))
653 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
654 minutes (number-to-string (+ (* h 60) m)))))))))
655 (list (cons ?L (or power-source "N/A"))
656 (cons ?p (or load-percentage "N/A"))
657 (cons ?B (or battery-status "N/A"))
658 (cons ?b (or battery-status-symbol ""))
659 (cons ?h (or hours "N/A"))
660 (cons ?m (or minutes "N/A"))
661 (cons ?t (or remaining-time "N/A")))))
662
663 \f
664 ;;; Private functions.
665
666 (defun battery-format (format alist)
667 "Substitute %-sequences in FORMAT."
668 (replace-regexp-in-string
669 "%."
670 (lambda (str)
671 (let ((char (aref str 1)))
672 (if (eq char ?%) "%"
673 (or (cdr (assoc char alist)) ""))))
674 format t t))
675
676 (defun battery-search-for-one-match-in-files (files regexp match-num)
677 "Search REGEXP in the content of the files listed in FILES.
678 If a match occurred, return the parenthesized expression numbered by
679 MATCH-NUM in the match. Otherwise, return nil."
680 (with-temp-buffer
681 (catch 'found
682 (dolist (file files)
683 (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
684 (re-search-forward regexp nil t)
685 (throw 'found (match-string match-num)))))))
686
687 \f
688 (provide 'battery)
689
690 ;;; battery.el ends here