* battery.el (battery-bsd-apm): New function.
[bpt/emacs.git] / lisp / battery.el
CommitLineData
d4919479 1;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
6b279740 2
acaf905b 3;; Copyright (C) 1997-1998, 2000-2012 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
69The function has to return an alist of conversion definitions.
70Its cons cells are of the form
6b279740
RS
71
72 (CONVERSION . REPLACEMENT-TEXT)
73
74CONVERSION is the character code of a \"conversion specification\"
4bef9110 75introduced 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
89Ordinary characters in the control string are printed as-is, while
90conversion specifications introduced by a `%' character in the control
91string 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)
71d21198 119