Fix vertical cursor motion under bidi on auto-composed characters.
[emacs.git] / lisp / battery.el
blobd7d3045fa581725f0494c977e50e64adfe1515ae
1 ;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1997-1998, 2000-2011 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))
37 (defgroup battery nil
38 "Display battery status information."
39 :prefix "battery-"
40 :group 'hardware)
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 'darwin)
54 (condition-case nil
55 (with-temp-buffer
56 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
57 (> (buffer-size) 0)))
58 (error nil)))
59 'battery-pmset)
60 ((eq system-type 'windows-nt)
61 'w32-battery-status))
62 "Function for getting battery status information.
63 The function has to return an alist of conversion definitions.
64 Its cons cells are of the form
66 (CONVERSION . REPLACEMENT-TEXT)
68 CONVERSION is the character code of a \"conversion specification\"
69 introduced by a `%' character in a control string."
70 :type '(choice (const nil) function)
71 :group 'battery)
73 (defcustom battery-echo-area-format
74 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
75 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
76 ((eq battery-status-function 'battery-linux-sysfs)
77 "Power %L, battery %B (%p%% load)")
78 ((eq battery-status-function 'battery-pmset)
79 "%L power, battery %B (%p%% load, remaining time %t)")
80 (battery-status-function
81 "Power %L, battery %B (%p%% load, remaining time %t)"))
82 "Control string formatting the string to display in the echo area.
83 Ordinary characters in the control string are printed as-is, while
84 conversion specifications introduced by a `%' character in the control
85 string are substituted as defined by the current value of the variable
86 `battery-status-function'. Here are the ones generally available:
87 %c Current capacity (mAh or mWh)
88 %r Current rate of charge or discharge
89 %B Battery status (verbose)
90 %b Battery status: empty means high, `-' means low,
91 `!' means critical, and `+' means charging
92 %d Temperature (in degrees Celsius)
93 %L AC line status (verbose)
94 %p Battery load percentage
95 %m Remaining time (to charge or discharge) in minutes
96 %h Remaining time (to charge or discharge) in hours
97 %t Remaining time (to charge or discharge) in the form `h:min'"
98 :type '(choice string (const nil))
99 :group 'battery)
101 (defvar battery-mode-line-string nil
102 "String to display in the mode line.")
103 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
105 (defcustom battery-mode-line-limit 100
106 "Percentage of full battery load below which display battery status"
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 "Display battery status information in the mode line.
168 The text being displayed in the mode line is controlled by the variables
169 `battery-mode-line-format' and `battery-status-function'.
170 The mode line will be updated automatically every `battery-update-interval'
171 seconds."
172 :global t :group 'battery
173 (setq battery-mode-line-string "")
174 (or global-mode-string (setq global-mode-string '("")))
175 (and battery-update-timer (cancel-timer battery-update-timer))
176 (if (not display-battery-mode)
177 (setq global-mode-string
178 (delq 'battery-mode-line-string global-mode-string))
179 (add-to-list 'global-mode-string 'battery-mode-line-string t)
180 (setq battery-update-timer (run-at-time nil battery-update-interval
181 'battery-update-handler))
182 (battery-update)))
184 (defun battery-update-handler ()
185 (battery-update)
186 (sit-for 0))
188 (defun battery-update ()
189 "Update battery status information in the mode line."
190 (let ((data (and battery-status-function (funcall battery-status-function))))
191 (setq battery-mode-line-string
192 (propertize (if (and battery-mode-line-format
193 (<= (car (read-from-string (cdr (assq ?p data))))
194 battery-mode-line-limit))
195 (battery-format
196 battery-mode-line-format
197 data)
199 'face
200 (and (<= (car (read-from-string (cdr (assq ?p data))))
201 battery-load-critical)
202 'font-lock-warning-face)
203 'help-echo "Battery status information")))
204 (force-mode-line-update))
206 ;;; `/proc/apm' interface for Linux.
208 (defconst battery-linux-proc-apm-regexp
209 (concat "^\\([^ ]+\\)" ; Driver version.
210 " \\([^ ]+\\)" ; APM BIOS version.
211 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
212 " 0x\\([0-9a-f]+\\)" ; AC line status.
213 " 0x\\([0-9a-f]+\\)" ; Battery status.
214 " 0x\\([0-9a-f]+\\)" ; Battery flags.
215 " \\(-?[0-9]+\\)%" ; Load percentage.
216 " \\(-?[0-9]+\\)" ; Remaining time.
217 " \\(.*\\)" ; Time unit.
218 "$")
219 "Regular expression matching contents of `/proc/apm'.")
221 (defun battery-linux-proc-apm ()
222 "Get APM status information from Linux kernel.
223 This function works only with the new `/proc/apm' format introduced
224 in Linux version 1.3.58.
226 The following %-sequences are provided:
227 %v Linux driver version
228 %V APM BIOS version
229 %I APM BIOS status (verbose)
230 %L AC line status (verbose)
231 %B Battery status (verbose)
232 %b Battery status, empty means high, `-' means low,
233 `!' means critical, and `+' means charging
234 %p Battery load percentage
235 %s Remaining time (to charge or discharge) in seconds
236 %m Remaining time (to charge or discharge) in minutes
237 %h Remaining time (to charge or discharge) in hours
238 %t Remaining time (to charge or discharge) in the form `h:min'"
239 (let (driver-version bios-version bios-interface line-status
240 battery-status battery-status-symbol load-percentage
241 seconds minutes hours remaining-time tem)
242 (with-temp-buffer
243 (ignore-errors (insert-file-contents "/proc/apm"))
244 (when (re-search-forward battery-linux-proc-apm-regexp)
245 (setq driver-version (match-string 1))
246 (setq bios-version (match-string 2))
247 (setq tem (string-to-number (match-string 3) 16))
248 (if (not (logand tem 2))
249 (setq bios-interface "not supported")
250 (setq bios-interface "enabled")
251 (cond ((logand tem 16) (setq bios-interface "disabled"))
252 ((logand tem 32) (setq bios-interface "disengaged")))
253 (setq tem (string-to-number (match-string 4) 16))
254 (cond ((= tem 0) (setq line-status "off-line"))
255 ((= tem 1) (setq line-status "on-line"))
256 ((= tem 2) (setq line-status "on backup")))
257 (setq tem (string-to-number (match-string 6) 16))
258 (if (= tem 255)
259 (setq battery-status "N/A")
260 (setq tem (string-to-number (match-string 5) 16))
261 (cond ((= tem 0) (setq battery-status "high"
262 battery-status-symbol ""))
263 ((= tem 1) (setq battery-status "low"
264 battery-status-symbol "-"))
265 ((= tem 2) (setq battery-status "critical"
266 battery-status-symbol "!"))
267 ((= tem 3) (setq battery-status "charging"
268 battery-status-symbol "+")))
269 (setq load-percentage (match-string 7))
270 (setq seconds (string-to-number (match-string 8)))
271 (and (string-equal (match-string 9) "min")
272 (setq seconds (* 60 seconds)))
273 (setq minutes (/ seconds 60)
274 hours (/ seconds 3600))
275 (setq remaining-time
276 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
277 (list (cons ?v (or driver-version "N/A"))
278 (cons ?V (or bios-version "N/A"))
279 (cons ?I (or bios-interface "N/A"))
280 (cons ?L (or line-status "N/A"))
281 (cons ?B (or battery-status "N/A"))
282 (cons ?b (or battery-status-symbol ""))
283 (cons ?p (or load-percentage "N/A"))
284 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
285 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
286 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
287 (cons ?t (or remaining-time "N/A")))))
290 ;;; `/proc/acpi/' interface for Linux.
292 (defun battery-linux-proc-acpi ()
293 "Get ACPI status information from Linux kernel.
294 This function works only with the `/proc/acpi/' format introduced
295 in Linux version 2.4.20 and 2.6.0.
297 The following %-sequences are provided:
298 %c Current capacity (mAh)
299 %r Current rate
300 %B Battery status (verbose)
301 %b Battery status, empty means high, `-' means low,
302 `!' means critical, and `+' means charging
303 %d Temperature (in degrees Celsius)
304 %L AC line status (verbose)
305 %p Battery load percentage
306 %m Remaining time (to charge or discharge) in minutes
307 %h Remaining time (to charge or discharge) in hours
308 %t Remaining time (to charge or discharge) in the form `h:min'"
309 (let ((design-capacity 0)
310 (last-full-capacity 0)
311 full-capacity
312 (warn 0)
313 (low 0)
314 capacity rate rate-type charging-state minutes hours)
315 ;; ACPI provides information about each battery present in the system in
316 ;; a separate subdirectory. We are going to merge the available
317 ;; information together since displaying for a variable amount of
318 ;; batteries seems overkill for format-strings.
319 (with-temp-buffer
320 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
321 t "\\`[^.]")))
322 (erase-buffer)
323 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
324 (when (re-search-forward "present: +yes$" nil t)
325 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
326 (member charging-state '("unknown" "charged" nil))
327 ;; On most multi-battery systems, most of the time only one
328 ;; battery is "charging"/"discharging", the others are
329 ;; "unknown".
330 (setq charging-state (match-string 1)))
331 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
332 nil t)
333 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
334 rate-type (or (and rate-type
335 (if (string= rate-type (match-string 2))
336 rate-type
337 (error
338 "Inconsistent rate types (%s vs. %s)"
339 rate-type (match-string 2))))
340 (match-string 2))))
341 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
342 nil t)
343 (setq capacity
344 (+ (or capacity 0) (string-to-number (match-string 1))))))
345 (goto-char (point-max))
346 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
347 (when (re-search-forward "present: +yes$" nil t)
348 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
349 nil t)
350 (incf design-capacity (string-to-number (match-string 1))))
351 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
352 nil t)
353 (incf last-full-capacity (string-to-number (match-string 1))))
354 (when (re-search-forward
355 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
356 (incf warn (string-to-number (match-string 1))))
357 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
358 nil t)
359 (incf low (string-to-number (match-string 1)))))))
360 (setq full-capacity (if (> last-full-capacity 0)
361 last-full-capacity design-capacity))
362 (and capacity rate
363 (setq minutes (if (zerop rate) 0
364 (floor (* (/ (float (if (string= charging-state
365 "charging")
366 (- full-capacity capacity)
367 capacity))
368 rate)
369 60)))
370 hours (/ minutes 60)))
371 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
372 (cons ?L (or (battery-search-for-one-match-in-files
373 (mapcar (lambda (e) (concat e "/state"))
374 (ignore-errors
375 (directory-files "/proc/acpi/ac_adapter/"
376 t "\\`[^.]")))
377 "state: +\\(.*\\)$" 1)
379 "N/A"))
380 (cons ?d (or (battery-search-for-one-match-in-files
381 (mapcar (lambda (e) (concat e "/temperature"))
382 (ignore-errors
383 (directory-files "/proc/acpi/thermal_zone/"
384 t "\\`[^.]")))
385 "temperature: +\\([0-9]+\\) C$" 1)
387 "N/A"))
388 (cons ?r (or (and rate (concat (number-to-string rate) " "
389 rate-type)) "N/A"))
390 (cons ?B (or charging-state "N/A"))
391 (cons ?b (or (and (string= charging-state "charging") "+")
392 (and capacity (< capacity low) "!")
393 (and capacity (< capacity warn) "-")
394 ""))
395 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
396 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
397 (cons ?t (or (and minutes
398 (format "%d:%02d" hours (- minutes (* 60 hours))))
399 "N/A"))
400 (cons ?p (or (and full-capacity capacity
401 (> full-capacity 0)
402 (number-to-string
403 (floor (/ capacity
404 (/ (float full-capacity) 100)))))
405 "N/A")))))
408 ;;; `/sys/class/power_supply/BATN' interface for Linux.
410 (defun battery-linux-sysfs ()
411 "Get ACPI status information from Linux kernel.
412 This function works only with the new `/sys/class/power_supply/'
413 format introduced in Linux version 2.4.25.
415 The following %-sequences are provided:
416 %c Current capacity (mAh or mWh)
417 %B Battery status (verbose)
418 %p Battery load percentage
419 %L AC line status (verbose)"
420 (let (charging-state
421 (charge-full 0.0)
422 (charge-now 0.0)
423 (energy-full 0.0)
424 (energy-now 0.0))
425 ;; SysFS provides information about each battery present in the
426 ;; system in a separate subdirectory. We are going to merge the
427 ;; available information together.
428 (with-temp-buffer
429 (dolist (dir (ignore-errors
430 (directory-files
431 "/sys/class/power_supply/" t "BAT[0-9]$")))
432 (erase-buffer)
433 (ignore-errors (insert-file-contents
434 (expand-file-name "uevent" dir)))
435 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t)
436 (goto-char (point-min))
437 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t)
438 (member charging-state '("Unknown" "Full" nil))
439 (setq charging-state (match-string 1)))
440 (let (full-string now-string)
441 ;; Sysfs may list either charge (mAh) or energy (mWh).
442 ;; Keep track of both, and choose which to report later.
443 (cond ((and (re-search-forward
444 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t)
445 (setq full-string (match-string 1))
446 (re-search-forward
447 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t)
448 (setq now-string (match-string 1)))
449 (setq charge-full (+ charge-full
450 (string-to-number full-string))
451 charge-now (+ charge-now
452 (string-to-number now-string))))
453 ((and (re-search-forward
454 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t)
455 (setq full-string (match-string 1))
456 (re-search-forward
457 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t)
458 (setq now-string (match-string 1)))
459 (setq energy-full (+ energy-full
460 (string-to-number full-string))
461 energy-now (+ energy-now
462 (string-to-number now-string)))))))))
463 (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0))
464 (number-to-string charge-now))
465 ((or (> energy-full 0) (> energy-now 0))
466 (number-to-string energy-now))
467 (t "N/A")))
468 (cons ?B (or charging-state "N/A"))
469 (cons ?p (cond ((> charge-full 0)
470 (format "%.1f"
471 (/ (* 100 charge-now) charge-full)))
472 ((> energy-full 0)
473 (format "%.1f"
474 (/ (* 100 energy-now) energy-full)))
475 (t "N/A")))
476 (cons ?L (if (file-readable-p "/sys/class/power_supply/AC/online")
477 (if (battery-search-for-one-match-in-files
478 (list "/sys/class/power_supply/AC/online"
479 "/sys/class/power_supply/ACAD/online")
480 "1" 0)
481 "AC"
482 "BAT")
483 "N/A")))))
487 ;;; `pmset' interface for Darwin (OS X).
489 (defun battery-pmset ()
490 "Get battery status information using `pmset'.
492 The following %-sequences are provided:
493 %L Power source (verbose)
494 %B Battery status (verbose)
495 %b Battery status, empty means high, `-' means low,
496 `!' means critical, and `+' means charging
497 %p Battery load percentage
498 %h Remaining time in hours
499 %m Remaining time in minutes
500 %t Remaining time in the form `h:min'"
501 (let (power-source load-percentage battery-status battery-status-symbol
502 remaining-time hours minutes)
503 (with-temp-buffer
504 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
505 (goto-char (point-min))
506 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
507 (setq power-source (match-string 1))
508 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
509 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
510 (setq load-percentage (match-string 1))
511 (goto-char (match-end 0))
512 (cond ((looking-at "; charging")
513 (setq battery-status "charging"
514 battery-status-symbol "+"))
515 ((< (string-to-number load-percentage) battery-load-low)
516 (setq battery-status "low"
517 battery-status-symbol "-"))
518 ((< (string-to-number load-percentage) battery-load-critical)
519 (setq battery-status "critical"
520 battery-status-symbol "!"))
522 (setq battery-status "high"
523 battery-status-symbol "")))
524 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
525 (setq remaining-time (match-string 1))
526 (let ((h (string-to-number (match-string 2)))
527 (m (string-to-number (match-string 3))))
528 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
529 minutes (number-to-string (+ (* h 60) m)))))))))
530 (list (cons ?L (or power-source "N/A"))
531 (cons ?p (or load-percentage "N/A"))
532 (cons ?B (or battery-status "N/A"))
533 (cons ?b (or battery-status-symbol ""))
534 (cons ?h (or hours "N/A"))
535 (cons ?m (or minutes "N/A"))
536 (cons ?t (or remaining-time "N/A")))))
539 ;;; Private functions.
541 (defun battery-format (format alist)
542 "Substitute %-sequences in FORMAT."
543 (replace-regexp-in-string
544 "%."
545 (lambda (str)
546 (let ((char (aref str 1)))
547 (if (eq char ?%) "%"
548 (or (cdr (assoc char alist)) ""))))
549 format t t))
551 (defun battery-search-for-one-match-in-files (files regexp match-num)
552 "Search REGEXP in the content of the files listed in FILES.
553 If a match occurred, return the parenthesized expression numbered by
554 MATCH-NUM in the match. Otherwise, return nil."
555 (with-temp-buffer
556 (catch 'found
557 (dolist (file files)
558 (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
559 (re-search-forward regexp nil t)
560 (throw 'found (match-string match-num)))))))
563 (provide 'battery)
565 ;;; battery.el ends here