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