(battery-status-function): Use w32-battery-status on Windows.
[bpt/emacs.git] / lisp / battery.el
CommitLineData
d4919479 1;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
6b279740 2
0d30b337 3;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6b279740 5
3b361901 6;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
d5323ba0 7;; Keywords: hardware
6b279740 8
f12b0b96 9;; This file is part of GNU Emacs.
6b279740 10
f12b0b96 11;; GNU Emacs is free software; you can redistribute it and/or modify
6b279740 12;; it under the terms of the GNU General Public License as published by
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
6b279740
RS
14;; any later version.
15
f12b0b96 16;; GNU Emacs is distributed in the hope that it will be useful,
6b279740
RS
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
f12b0b96 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
6b279740
RS
25
26;;; Commentary:
27
20d4381e
JR
28;; There is at present support for GNU/Linux, OS X and Windows. This
29;; library supports both the `/proc/apm' file format of Linux version
30;; 1.3.58 or newer and the `/proc/acpi/' directory structure of Linux
31;; 2.4.20 and 2.6. Darwin (OS X) is supported by using the `pmset'
32;; program. Windows is supported by the GetSystemPowerStatus API call.
6b279740
RS
33
34;;; Code:
35
36(require 'timer)
0f4a15f8 37(eval-when-compile (require 'cl))
6b279740
RS
38
39\f
4bef9110
SE
40(defgroup battery nil
41 "Display battery status information."
42 :prefix "battery-"
43 :group 'hardware)
44
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
LK
51 'battery-linux-proc-acpi)
52 ((and (eq system-type 'darwin)
785428c7
RF
53 (condition-case nil
54 (with-temp-buffer
248634e9
LK
55 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
56 (> (buffer-size) 0)))
57 (error nil)))
20d4381e
JR
58 'battery-pmset)
59 ((eq system-type 'windows-nt)
60 'w32-battery-status))
6b279740 61 "*Function for getting battery status information.
681e6b4b
DL
62The function has to return an alist of conversion definitions.
63Its cons cells are of the form
6b279740
RS
64
65 (CONVERSION . REPLACEMENT-TEXT)
66
67CONVERSION is the character code of a \"conversion specification\"
4bef9110 68introduced by a `%' character in a control string."
681e6b4b 69 :type '(choice (const nil) function)
4bef9110 70 :group 'battery)
6b279740 71
4bef9110 72(defcustom battery-echo-area-format
20d4381e 73 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
71d21198
LK
74 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
75 ((eq battery-status-function 'battery-pmset)
20d4381e
JR
76 "%L power, battery %B (%p%% load, remaining time %t)")
77 (battery-status-function
78 "Power %L, battery %B (%p%% load, remaining time %t)"))
6b279740
RS
79 "*Control string formatting the string to display in the echo area.
80Ordinary characters in the control string are printed as-is, while
81conversion specifications introduced by a `%' character in the control
82string are substituted as defined by the current value of the variable
76815b2a
RS
83`battery-status-function'. Here are the ones generally available:
84%c Current capacity (mAh or mWh)
85%r Current rate of charge or discharge
86%B Battery status (verbose)
87%b Battery status: empty means high, `-' means low,
88 `!' means critical, and `+' means charging
89%d Temperature (in degrees Celsius)
90%L AC line status (verbose)
91%p Battery load percentage
92%m Remaining time (to charge or discharge) in minutes
93%h Remaining time (to charge or discharge) in hours
94%t Remaining time (to charge or discharge) in the form `h:min'"
4bef9110
SE
95 :type '(choice string (const nil))
96 :group 'battery)
6b279740
RS
97
98(defvar battery-mode-line-string nil
99 "String to display in the mode line.")
f4c4fc74 100;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
6b279740 101
4bef9110 102(defcustom battery-mode-line-format
20d4381e 103 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
71d21198 104