diff options
| author | Chong Yidong | 2008-08-08 17:46:49 +0000 |
|---|---|---|
| committer | Chong Yidong | 2008-08-08 17:46:49 +0000 |
| commit | 3660b4f5fe58ae932b7eadb4209adb402fc8a4d2 (patch) | |
| tree | df5037750ad57af064e2cd98b070d70322b8060d | |
| parent | 86d2b6128c9d319e3e0d8091c4d5a628b1f12ef9 (diff) | |
| download | emacs-3660b4f5fe58ae932b7eadb4209adb402fc8a4d2.tar.gz emacs-3660b4f5fe58ae932b7eadb4209adb402fc8a4d2.zip | |
(battery-echo-area-format, battery-status-function):
Handle new Linux sysfs format for battery reporting.
(battery-linux-sysfs): New function.
| -rw-r--r-- | lisp/battery.el | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/lisp/battery.el b/lisp/battery.el index 22b09e15e80..0b7407274f8 100644 --- a/lisp/battery.el +++ b/lisp/battery.el | |||
| @@ -47,6 +47,10 @@ | |||
| 47 | ((and (eq system-type 'gnu/linux) | 47 | ((and (eq system-type 'gnu/linux) |
| 48 | (file-directory-p "/proc/acpi/battery")) | 48 | (file-directory-p "/proc/acpi/battery")) |
| 49 | 'battery-linux-proc-acpi) | 49 | 'battery-linux-proc-acpi) |
| 50 | ((and (eq system-type 'gnu/linux) | ||
| 51 | (file-directory-p "/sys/class/power_supply/") | ||
| 52 | (directory-files "/sys/class/power_supply/" nil "BAT[0-9]$")) | ||
| 53 | 'battery-linux-sysfs) | ||
| 50 | ((and (eq system-type 'darwin) | 54 | ((and (eq system-type 'darwin) |
| 51 | (condition-case nil | 55 | (condition-case nil |
| 52 | (with-temp-buffer | 56 | (with-temp-buffer |
| @@ -70,6 +74,8 @@ introduced by a `%' character in a control string." | |||
| 70 | (defcustom battery-echo-area-format | 74 | (defcustom battery-echo-area-format |
| 71 | (cond ((eq battery-status-function 'battery-linux-proc-acpi) | 75 | (cond ((eq battery-status-function 'battery-linux-proc-acpi) |
| 72 | "Power %L, battery %B at %r (%p%% load, remaining time %t)") | 76 | "Power %L, battery %B at %r (%p%% load, remaining time %t)") |
| 77 | ((eq battery-status-function 'battery-linux-sysfs) | ||
| 78 | "Power %L, battery %B (%p%% load)") | ||
| 73 | ((eq battery-status-function 'battery-pmset) | 79 | ((eq battery-status-function 'battery-pmset) |
| 74 | "%L power, battery %B (%p%% load, remaining time %t)") | 80 | "%L power, battery %B (%p%% load, remaining time %t)") |
| 75 | (battery-status-function | 81 | (battery-status-function |
| @@ -276,7 +282,7 @@ The following %-sequences are provided: | |||
| 276 | 282 | ||
| 277 | (defun battery-linux-proc-acpi () | 283 | (defun battery-linux-proc-acpi () |
| 278 | "Get ACPI status information from Linux kernel. | 284 | "Get ACPI status information from Linux kernel. |
| 279 | This function works only with the new `/proc/acpi/' format introduced | 285 | This function works only with the `/proc/acpi/' format introduced |
| 280 | in Linux version 2.4.20 and 2.6.0. | 286 | in Linux version 2.4.20 and 2.6.0. |
| 281 | 287 | ||
| 282 | The following %-sequences are provided: | 288 | The following %-sequences are provided: |
| @@ -390,6 +396,85 @@ The following %-sequences are provided: | |||
| 390 | "N/A"))))) | 396 | "N/A"))))) |
| 391 | 397 | ||
| 392 | 398 | ||
| 399 | ;;; `/sys/class/power_supply/BATN' interface for Linux. | ||
| 400 | |||
| 401 | (defun battery-linux-sysfs () | ||
| 402 | "Get ACPI status information from Linux kernel. | ||
| 403 | This function works only with the new `/sys/class/power_supply/' | ||
| 404 | format introduced in Linux version 2.4.25. | ||
| 405 | |||
| 406 | The following %-sequences are provided: | ||
| 407 | %c Current capacity (mAh or mWh) | ||
| 408 | %B Battery status (verbose) | ||
| 409 | %p Battery load percentage | ||
| 410 | %L AC line status (verbose)" | ||
| 411 | (let (charging-state | ||
| 412 | (charge-full 0.0) | ||
| 413 | (charge-now 0.0) | ||
| 414 | (energy-full 0.0) | ||
| 415 | (energy-now 0.0)) | ||
| 416 | ;; SysFS provides information about each battery present in the | ||
| 417 | ;; system in a separate subdirectory. We are going to merge the | ||
| 418 | ;; available information together. | ||
| 419 | (with-temp-buffer | ||
| 420 | (dolist (dir (ignore-errors | ||
| 421 | (directory-files | ||
| 422 | "/sys/class/power_supply/" t "BAT[0-9]$"))) | ||
| 423 | (erase-buffer) | ||
| 424 | (ignore-errors (insert-file-contents | ||
| 425 | (expand-file-name "uevent" dir))) | ||
| 426 | (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t) | ||
| 427 | (goto-char (point-min)) | ||
| 428 | (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t) | ||
| 429 | (member charging-state '("Unknown" "Full" nil)) | ||
| 430 | (setq charging-state (match-string 1))) | ||
| 431 | (let (full-string now-string) | ||
| 432 | ;; Sysfs may list either charge (mAh) or energy (mWh). | ||
| 433 | ;; Keep track of both, and choose which to report later. | ||
| 434 | (cond ((and (re-search-forward | ||
| 435 | "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t) | ||
| 436 | (setq full-string (match-string 1)) | ||
| 437 | (re-search-forward | ||
| 438 | "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t) | ||
| 439 | (setq now-string (match-string 1))) | ||
| 440 | (setq charge-full (+ charge-full | ||
| 441 | (string-to-number full-string)) | ||
| 442 | charge-now (+ charge-now | ||
| 443 | (string-to-number now-string)))) | ||
| 444 | ((and (re-search-forward | ||
| 445 | "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t) | ||
| 446 | (setq full-string (match-string 1)) | ||
| 447 | (re-search-forward | ||
| 448 | "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t) | ||
| 449 | (setq now-string (match-string 1))) | ||
| 450 | (setq energy-full (+ energy-full | ||
| 451 | (string-to-number full-string)) | ||
| 452 | energy-now (+ energy-now | ||
| 453 | (string-to-number now-string))))))))) | ||
| 454 | (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0)) | ||
| 455 | (number-to-string charge-now)) | ||
| 456 | ((or (> energy-full 0) (> energy-now 0)) | ||
| 457 | (number-to-string energy-now)) | ||
| 458 | (t "N/A"))) | ||
| 459 | (cons ?B (or charging-state "N/A")) | ||
| 460 | (cons ?p (cond ((> charge-full 0) | ||
| 461 | (format "%.1f" | ||
| 462 | (/ (* 100 charge-now) charge-full))) | ||
| 463 | ((> energy-full 0) | ||
| 464 | (format "%.1f" | ||
| 465 | (/ (* 100 energy-now) energy-full))) | ||
| 466 | (t "N/A"))) | ||
| 467 | (cons ?L (if (file-readable-p "/sys/class/power_supply/AC/online") | ||
| 468 | (if (battery-search-for-one-match-in-files | ||
| 469 | (list "/sys/class/power_supply/AC/online" | ||
| 470 | "/sys/class/power_supply/ACAD/online") | ||
| 471 | "1" 0) | ||
| 472 | "AC" | ||
| 473 | "BAT") | ||
| 474 | "N/A"))))) | ||
| 475 | |||
| 476 | |||
| 477 | |||
| 393 | ;;; `pmset' interface for Darwin (OS X). | 478 | ;;; `pmset' interface for Darwin (OS X). |
| 394 | 479 | ||
| 395 | (defun battery-pmset () | 480 | (defun battery-pmset () |