Make generic sysfs support apply for yeeloong as well.
[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)
0f4a15f8 34(eval-when-compile (require 'cl))
6b279740
RS
35
36\f
4bef9110
SE
37(defgroup battery nil
38 "Display battery status information."
39 :prefix "battery-"
40 :group 'hardware)
41
6decb6c2
SM
42;; Either BATn or yeeloong-bat, basically.
43(defconst battery--linux-sysfs-regexp "[bB][aA][tT][0-9]?$")
44
4bef9110 45(defcustom battery-status-function
6b279740
RS
46 (cond ((and (eq system-type 'gnu/linux)
47 (file-readable-p "/proc/apm"))
2b3cb60a
EZ
48 'battery-linux-proc-apm)
49 ((and (eq system-type 'gnu/linux)
50 (file-directory-p "/proc/acpi/battery"))
71d21198 51 'battery-linux-proc-acpi)
3660b4f5
CY
52 ((and (eq system-type 'gnu/linux)
53 (file-directory-p "/sys/class/power_supply/")
6decb6c2
SM
54 (directory-files "/sys/class/power_supply/" nil
55 battery--linux-sysfs-regexp))
3660b4f5 56 'battery-linux-sysfs)
71d21198 57 ((and (eq system-type 'darwin)
785428c7
RF
58 (condition-case nil
59 (with-temp-buffer
248634e9
LK
60 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
61 (> (buffer-size) 0)))
62 (error nil)))
20d4381e
JR
63 'battery-pmset)
64 ((eq system-type 'windows-nt)
65 'w32-battery-status))
9201cc28 66 "Function for getting battery status information.
681e6b4b
DL
67The function has to return an alist of conversion definitions.
68Its cons cells are of the form
6b279740
RS
69
70 (CONVERSION . REPLACEMENT-TEXT)
71
72CONVERSION is the character code of a \"conversion specification\"
4bef9110 73introduced by a `%' character in a control string."
681e6b4b 74 :type '(choice (const nil) function)
4bef9110 75 :group 'battery)
6b279740 76
4bef9110 77(defcustom battery-echo-area-format
20d4381e 78 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
71d21198 79 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
3660b4f5
CY
80 ((eq battery-status-function 'battery-linux-sysfs)
81 "Power %L, battery %B (%p%% load)")
71d21198 82 ((eq battery-status-function 'battery-pmset)
20d4381e
JR
83 "%L power, battery %B (%p%% load, remaining time %t)")
84 (battery-status-function
85 "Power %L, battery %B (%p%% load, remaining time %t)"))
9201cc28 86 "Control string formatting the string to display in the echo area.
6b279740
RS
87Ordinary characters in the control string are printed as-is, while
88conversion specifications introduced by a `%' character in the control
89string are substituted as defined by the current value of the variable
76815b2a
RS
90`battery-status-function'. Here are the ones generally available:
91%c Current capacity (mAh or mWh)
92%r Current rate of charge or discharge
93%B Battery status (verbose)
94%b Battery status: empty means high, `-' means low,
95 `!' means critical, and `+' means charging
96%d Temperature (in degrees Celsius)
97%L AC line status (verbose)
98%p Battery load percentage
99%m Remaining time (to charge or discharge) in minutes
100%h Remaining time (to charge or discharge) in hours
101%t Remaining time (to charge or discharge) in the form `h:min'"
4bef9110
SE
102 :type '(choice string (const nil))
103 :group 'battery)
6b279740
RS
104
105(defvar battery-mode-line-string nil
106 "String to display in the mode line.")
f4c4fc74 107;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
6b279740 108
43d5bf84
RS
109(defcustom battery-mode-line-limit 100
110 "Percentage of full battery load below which display battery status"
2bed3f04 111 :version "24.1"
43d5bf84
RS
112 :type 'integer
113 :group 'battery)
114
4bef9110 115(defcustom battery-mode-line-format
20d4381e 116 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
71d21198 117