annotate lisp/battery.el @ 72550:666bd542be19

(get_window_cursor_type): Replace BOX cursor on images with a hollow box cursor if image is larger than 32x32 (or the default frame font if that is bigger). Replace any other cursor on images with hollow box cursor, as redisplay doesn't support bar and hbar cursors on images.
author Kim F. Storm <storm@cua.dk>
date Sun, 27 Aug 2006 22:23:07 +0000
parents b270f677c979
children 65d9fbabd719 146cd8369025
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38436
b174db545cfd Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 38292
diff changeset
1 ;;; battery.el --- display battery status information
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
64762
41bb365f41c4 Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 64094
diff changeset
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004,
68651
3bd95f4f2941 Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 68266
diff changeset
4 ;; 2005, 2006 Free Software Foundation, Inc.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5
30110
23cb074f9d88 Change author's mail address.
Gerd Moellmann <gerd@gnu.org>
parents: 26828
diff changeset
6 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
20972
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
7 ;; Keywords: hardware
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8
18685
4f3350c88e6c Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents: 18388
diff changeset
9 ;; This file is part of GNU Emacs.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
18685
4f3350c88e6c Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents: 18388
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; any later version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
18685
4f3350c88e6c Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents: 18388
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
18685
4f3350c88e6c Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents: 18388
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
64091
6fb026ad601f Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 63899
diff changeset
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
6fb026ad601f Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 63899
diff changeset
24 ;; Boston, MA 02110-1301, USA.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 ;;; Commentary:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
28 ;; There is at present support for GNU/Linux and OS X. This library
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
29 ;; supports both the `/proc/apm' file format of Linux version 1.3.58
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
30 ;; or newer and the `/proc/acpi/' directory structure of Linux 2.4.20
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
31 ;; and 2.6. Darwin (OS X) is supported by using the `pmset' program.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 ;;; Code:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 (require 'timer)
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
36 (eval-when-compile (require 'cl))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
39 (defgroup battery nil
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
40 "Display battery status information."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
41 :prefix "battery-"
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
42 :group 'hardware)
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
43
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
44 (defcustom battery-status-function
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 (cond ((and (eq system-type 'gnu/linux)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 (file-readable-p "/proc/apm"))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
47 'battery-linux-proc-apm)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
48 ((and (eq system-type 'gnu/linux)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
49 (file-directory-p "/proc/acpi/battery"))
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
50 'battery-linux-proc-acpi)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
51 ((and (eq system-type 'darwin)
64907
08b55dc9bcd1 (battery-status-function): Don't use ignore-errors.
Lute Kamstra <lute@gnu.org>
parents: 64762
diff changeset
52 (condition-case nil
08b55dc9bcd1 (battery-status-function): Don't use ignore-errors.
Lute Kamstra <lute@gnu.org>
parents: 64762
diff changeset
53 (with-temp-buffer
08b55dc9bcd1 (battery-status-function): Don't use ignore-errors.
Lute Kamstra <lute@gnu.org>
parents: 64762
diff changeset
54 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
08b55dc9bcd1 (battery-status-function): Don't use ignore-errors.
Lute Kamstra <lute@gnu.org>
parents: 64762
diff changeset
55 (> (buffer-size) 0)))
08b55dc9bcd1 (battery-status-function): Don't use ignore-errors.
Lute Kamstra <lute@gnu.org>
parents: 64762
diff changeset
56 (error nil)))
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
57 'battery-pmset))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 "*Function for getting battery status information.
36090
9ed7c3f74e35 (battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents: 32356
diff changeset
59 The function has to return an alist of conversion definitions.
9ed7c3f74e35 (battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents: 32356
diff changeset
60 Its cons cells are of the form
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 (CONVERSION . REPLACEMENT-TEXT)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 CONVERSION is the character code of a \"conversion specification\"
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
65 introduced by a `%' character in a control string."
36090
9ed7c3f74e35 (battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents: 32356
diff changeset
66 :type '(choice (const nil) function)
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
67 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
69 (defcustom battery-echo-area-format
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 (cond ((eq battery-status-function 'battery-linux-proc-apm)
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
71 "Power %L, battery %B (%p%% load, remaining time %t)")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
72 ((eq battery-status-function 'battery-linux-proc-acpi)
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
73 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
74 ((eq battery-status-function 'battery-pmset)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
75 "%L power, battery %B (%p%% load, remaining time %t)"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 "*Control string formatting the string to display in the echo area.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 Ordinary characters in the control string are printed as-is, while
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 conversion specifications introduced by a `%' character in the control
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 string are substituted as defined by the current value of the variable
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
80 `battery-status-function'. Here are the ones generally available:
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
81 %c Current capacity (mAh or mWh)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
82 %r Current rate of charge or discharge
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
83 %B Battery status (verbose)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
84 %b Battery status: empty means high, `-' means low,
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
85 `!' means critical, and `+' means charging
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
86 %d Temperature (in degrees Celsius)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
87 %L AC line status (verbose)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
88 %p Battery load percentage
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
89 %m Remaining time (to charge or discharge) in minutes
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
90 %h Remaining time (to charge or discharge) in hours
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
91 %t Remaining time (to charge or discharge) in the form `h:min'"
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
92 :type '(choice string (const nil))
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
93 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 (defvar battery-mode-line-string nil
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 "String to display in the mode line.")
59132
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
97 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
99 (defcustom battery-mode-line-format
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 (cond ((eq battery-status-function 'battery-linux-proc-apm)
59132
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
101 "[%b%p%%]")
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
102 ((eq battery-status-function 'battery-linux-proc-acpi)
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
103 "[%b%p%%,%d°C]")
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
104 ((eq battery-status-function 'battery-pmset)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
105 "[%b%p%%]"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 "*Control string formatting the string to display in the mode line.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 Ordinary characters in the control string are printed as-is, while
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 conversion specifications introduced by a `%' character in the control
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 string are substituted as defined by the current value of the variable
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
110 `battery-status-function'. Here are the ones generally available:
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
111 %c Current capacity (mAh or mWh)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
112 %r Current rate of charge or discharge
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
113 %B Battery status (verbose)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
114 %b Battery status: empty means high, `-' means low,
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
115 `!' means critical, and `+' means charging
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
116 %d Temperature (in degrees Celsius)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
117 %L AC line status (verbose)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
118 %p Battery load percentage
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
119 %m Remaining time (to charge or discharge) in minutes
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
120 %h Remaining time (to charge or discharge) in hours
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
121 %t Remaining time (to charge or discharge) in the form `h:min'"
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
122 :type '(choice string (const nil))
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
123 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
125 (defcustom battery-update-interval 60
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
126 "*Seconds after which the battery status will be updated."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
127 :type 'integer
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
128 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
130 (defcustom battery-load-low 25
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
131 "*Upper bound of low battery load percentage.
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
132 A battery load percentage below this number is considered low."
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
133 :type 'integer
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
134 :group 'battery)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
135
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
136 (defcustom battery-load-critical 10
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
137 "*Upper bound of critical battery load percentage.
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
138 A battery load percentage below this number is considered critical."
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
139 :type 'integer
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
140 :group 'battery)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
141
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 (defvar battery-update-timer nil
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 "Interval timer object.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144
21076
dc82196eac58 Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents: 20972
diff changeset
145 ;;;###autoload
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 (defun battery ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 "Display battery status information in the echo area.
26828
983f253505d0 (battery): Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents: 21317
diff changeset
148 The text being displayed in the echo area is controlled by the variables
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 `battery-echo-area-format' and `battery-status-function'."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 (interactive)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 (message "%s" (if (and battery-echo-area-format battery-status-function)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 (battery-format battery-echo-area-format
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (funcall battery-status-function))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 "Battery status not available")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155
21076
dc82196eac58 Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents: 20972
diff changeset
156 ;;;###autoload
59363
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
157 (define-minor-mode display-battery-mode
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 "Display battery status information in the mode line.
32346
fa3f5e8f6201 (display-battery): Doc spelling fix.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 30110
diff changeset
159 The text being displayed in the mode line is controlled by the variables
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 `battery-mode-line-format' and `battery-status-function'.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 The mode line will be updated automatically every `battery-update-interval'
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 seconds."
61268
abd0d265b4ba (display-battery-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents: 59363
diff changeset
163 :global t :group 'battery
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 (setq battery-mode-line-string "")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 (or global-mode-string (setq global-mode-string '("")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 (and battery-update-timer (cancel-timer battery-update-timer))
59363
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
167 (if (not display-battery-mode)
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
168 (setq global-mode-string
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
169 (delq 'battery-mode-line-string global-mode-string))
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
170 (add-to-list 'global-mode-string 'battery-mode-line-string t)
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
171 (setq battery-update-timer (run-at-time nil battery-update-interval
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
172 'battery-update-handler))
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
173 (battery-update)))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (defun battery-update-handler ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (battery-update)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 (sit-for 0))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (defun battery-update ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 "Update battery status information in the mode line."
59132
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
181 (setq battery-mode-line-string
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
182 (propertize (if (and battery-mode-line-format
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
183 battery-status-function)
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
184 (battery-format
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
185 battery-mode-line-format
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
186 (funcall battery-status-function))
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
187 "")
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
188 'help-echo "Battery status information"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (force-mode-line-update))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 ;;; `/proc/apm' interface for Linux.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (defconst battery-linux-proc-apm-regexp
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 (concat "^\\([^ ]+\\)" ; Driver version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 " \\([^ ]+\\)" ; APM BIOS version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 " 0x\\([0-9a-f]+\\)" ; AC line status.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 " 0x\\([0-9a-f]+\\)" ; Battery status.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 " 0x\\([0-9a-f]+\\)" ; Battery flags.
20972
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
201 " \\(-?[0-9]+\\)%" ; Load percentage.
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
202 " \\(-?[0-9]+\\)" ; Remaining time.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 " \\(.*\\)" ; Time unit.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 "$")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 "Regular expression matching contents of `/proc/apm'.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (defun battery-linux-proc-apm ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 "Get APM status information from Linux kernel.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 This function works only with the new `/proc/apm' format introduced
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 in Linux version 1.3.58.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 The following %-sequences are provided:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 %v Linux driver version
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 %V APM BIOS version
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 %I APM BIOS status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 %L AC line status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 %B Battery status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 %b Battery status, empty means high, `-' means low,
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 `!' means critical, and `+' means charging
63899
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
220 %p Battery load percentage
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
221 %s Remaining time (to charge or discharge) in seconds
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
222 %m Remaining time (to charge or discharge) in minutes
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
223 %h Remaining time (to charge or discharge) in hours
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
224 %t Remaining time (to charge or discharge) in the form `h:min'"
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 (let (driver-version bios-version bios-interface line-status
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 battery-status battery-status-symbol load-percentage
63899
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
227 seconds minutes hours remaining-time tem)
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
228 (with-temp-buffer
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
229 (ignore-errors (insert-file-contents "/proc/apm"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
230 (when (re-search-forward battery-linux-proc-apm-regexp)
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
231 (setq driver-version (match-string 1))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
232 (setq bios-version (match-string 2))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
233 (setq tem (string-to-number (match-string 3) 16))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
234 (if (not (logand tem 2))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
235 (setq bios-interface "not supported")
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
236 (setq bios-interface "enabled")
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
237 (cond ((logand tem 16) (setq bios-interface "disabled"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
238 ((logand tem 32) (setq bios-interface "disengaged")))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
239 (setq tem (string-to-number (match-string 4) 16))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
240 (cond ((= tem 0) (setq line-status "off-line"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
241 ((= tem 1) (setq line-status "on-line"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
242 ((= tem 2) (setq line-status "on backup")))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
243 (setq tem (string-to-number (match-string 6) 16))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
244 (if (= tem 255)
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
245 (setq battery-status "N/A")
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
246 (setq tem (string-to-number (match-string 5) 16))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
247 (cond ((= tem 0) (setq battery-status "high"
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
248 battery-status-symbol ""))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
249 ((= tem 1) (setq battery-status "low"
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
250 battery-status-symbol "-"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
251 ((= tem 2) (setq battery-status "critical"
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
252 battery-status-symbol "!"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
253 ((= tem 3) (setq battery-status "charging"
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
254 battery-status-symbol "+")))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
255 (setq load-percentage (match-string 7))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
256 (setq seconds (string-to-number (match-string 8)))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
257 (and (string-equal (match-string 9) "min")
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
258 (setq seconds (* 60 seconds)))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
259 (setq minutes (/ seconds 60)
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
260 hours (/ seconds 3600))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
261 (setq remaining-time
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
262 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
20972
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
263 (list (cons ?v (or driver-version "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
264 (cons ?V (or bios-version "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
265 (cons ?I (or bios-interface "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
266 (cons ?L (or line-status "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
267 (cons ?B (or battery-status "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
268 (cons ?b (or battery-status-symbol ""))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
269 (cons ?p (or load-percentage "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
270 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
271 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
272 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
273 (cons ?t (or remaining-time "N/A")))))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
276 ;;; `/proc/acpi/' interface for Linux.
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
277
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
278 (defun battery-linux-proc-acpi ()
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
279 "Get ACPI status information from Linux kernel.
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
280 This function works only with the new `/proc/acpi/' format introduced
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
281 in Linux version 2.4.20 and 2.6.0.
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
282
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
283 The following %-sequences are provided:
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
284 %c Current capacity (mAh)
63899
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
285 %r Current rate
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
286 %B Battery status (verbose)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
287 %b Battery status, empty means high, `-' means low,
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
288 `!' means critical, and `+' means charging
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
289 %d Temperature (in degrees Celsius)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
290 %L AC line status (verbose)
63899
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
291 %p Battery load percentage
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
292 %m Remaining time (to charge or discharge) in minutes
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
293 %h Remaining time (to charge or discharge) in hours
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
294 %t Remaining time (to charge or discharge) in the form `h:min'"
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
295 (let ((design-capacity 0)
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
296 (last-full-capacity 0)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
297 full-capacity
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
298 (warn 0)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
299 (low 0)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
300 capacity rate rate-type charging-state minutes hours)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
301 ;; ACPI provides information about each battery present in the system in
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
302 ;; a separate subdirectory. We are going to merge the available
57133
fd1a6df8927e Delete superfluous empty lines.
Eli Zaretskii <eliz@gnu.org>
parents: 56663
diff changeset
303 ;; information together since displaying for a variable amount of
fd1a6df8927e Delete superfluous empty lines.
Eli Zaretskii <eliz@gnu.org>
parents: 56663
diff changeset
304 ;; batteries seems overkill for format-strings.
fd1a6df8927e Delete superfluous empty lines.
Eli Zaretskii <eliz@gnu.org>
parents: 56663
diff changeset
305 (with-temp-buffer
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
306 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
307 t "\\`[^.]")))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
308 (erase-buffer)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
309 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
310 (when (re-search-forward "present: +yes$" nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
311 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
312 (member charging-state '("unknown" "charged" nil))
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
313 ;; On most multi-battery systems, most of the time only one
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
314 ;; battery is "charging"/"discharging", the others are
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
315 ;; "unknown".
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
316 (setq charging-state (match-string 1)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
317 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
318 nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
319 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
320 rate-type (or (and rate-type
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
321 (if (string= rate-type (match-string 2))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
322 rate-type
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
323 (error
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
324 "Inconsistent rate types (%s vs. %s)"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
325 rate-type (match-string 2))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
326 (match-string 2))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
327 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
328 nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
329 (setq capacity
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
330 (+ (or capacity 0) (string-to-number (match-string 1))))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
331 (goto-char (point-max))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
332 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
333 (when (re-search-forward "present: +yes$" nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
334 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
335 nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
336 (incf design-capacity (string-to-number (match-string 1))))
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
337 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
338 nil t)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
339 (incf last-full-capacity (string-to-number (match-string 1))))
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
340 (when (re-search-forward
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
341 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
342 (incf warn (string-to-number (match-string 1))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
343 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
344 nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
345 (incf low (string-to-number (match-string 1)))))))
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
346 (setq full-capacity (if (> last-full-capacity 0)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
347 last-full-capacity design-capacity))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
348 (and capacity rate
55904
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
349 (setq minutes (if (zerop rate) 0
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
350 (floor (* (/ (float (if (string= charging-state
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
351 "charging")
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
352 (- full-capacity capacity)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
353 capacity))
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
354 rate)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
355 60)))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
356 hours (/ minutes 60)))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
357 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
358 (cons ?L (or (when (file-exists-p "/proc/acpi/ac_adapter/AC/state")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
359 (with-temp-buffer
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
360 (insert-file-contents
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
361 "/proc/acpi/ac_adapter/AC/state")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
362 (when (re-search-forward "state: +\\(.*\\)$" nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
363 (match-string 1))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
364 "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
365 (cons ?d (or (when (file-exists-p
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
366 "/proc/acpi/thermal_zone/THRM/temperature")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
367 (with-temp-buffer
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
368 (insert-file-contents
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
369 "/proc/acpi/thermal_zone/THRM/temperature")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
370 (when (re-search-forward
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
371 "temperature: +\\([0-9]+\\) C$" nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
372 (match-string 1))))
55904
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
373 (when (file-exists-p
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
374 "/proc/acpi/thermal_zone/THM/temperature")
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
375 (with-temp-buffer
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
376 (insert-file-contents
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
377 "/proc/acpi/thermal_zone/THM/temperature")
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
378 (when (re-search-forward
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
379 "temperature: +\\([0-9]+\\) C$" nil t)
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
380 (match-string 1))))
67802
91c501f75eea (battery-linux-proc-acpi): Also try
Romain Francoise <romain@orebokech.com>
parents: 64907
diff changeset
381 (when (file-exists-p
91c501f75eea (battery-linux-proc-acpi): Also try
Romain Francoise <romain@orebokech.com>
parents: 64907
diff changeset
382 "/proc/acpi/thermal_zone/THM0/temperature")
91c501f75eea (battery-linux-proc-acpi): Also try
Romain Francoise <romain@orebokech.com>
parents: 64907
diff changeset
383 (with-temp-buffer
91c501f75eea (battery-linux-proc-acpi): Also try
Romain Francoise <romain@orebokech.com>
parents: 64907
diff changeset
384 (insert-file-contents
91c501f75eea (battery-linux-proc-acpi): Also try
Romain Francoise <romain@orebokech.com>
parents: 64907
diff changeset
385 "/proc/acpi/thermal_zone/THM0/temperature")
91c501f75eea (battery-linux-proc-acpi): Also try
Romain Francoise <romain@orebokech.com>
parents: 64907
diff changeset
386 (when (re-search-forward
91c501f75eea (battery-linux-proc-acpi): Also try
Romain Francoise <romain@orebokech.com>
parents: 64907
diff changeset
387 "temperature: +\\([0-9]+\\) C$" nil t)
91c501f75eea (battery-linux-proc-acpi): Also try
Romain Francoise <romain@orebokech.com>
parents: 64907
diff changeset
388 (match-string 1))))
70539
b270f677c979 (battery-linux-proc-acpi): Also try `/proc/acpi/thermal_zone/THR2/temperature'.
Juri Linkov <juri@jurta.org>
parents: 69414
diff changeset
389 (when (file-exists-p
b270f677c979 (battery-linux-proc-acpi): Also try `/proc/acpi/thermal_zone/THR2/temperature'.
Juri Linkov <juri@jurta.org>
parents: 69414
diff changeset
390 "/proc/acpi/thermal_zone/THR2/temperature")
b270f677c979 (battery-linux-proc-acpi): Also try `/proc/acpi/thermal_zone/THR2/temperature'.
Juri Linkov <juri@jurta.org>
parents: 69414
diff changeset
391 (with-temp-buffer
b270f677c979 (battery-linux-proc-acpi): Also try `/proc/acpi/thermal_zone/THR2/temperature'.
Juri Linkov <juri@jurta.org>
parents: 69414
diff changeset
392 (insert-file-contents
b270f677c979 (battery-linux-proc-acpi): Also try `/proc/acpi/thermal_zone/THR2/temperature'.
Juri Linkov <juri@jurta.org>
parents: 69414
diff changeset
393 "/proc/acpi/thermal_zone/THR2/temperature")
b270f677c979 (battery-linux-proc-acpi): Also try `/proc/acpi/thermal_zone/THR2/temperature'.
Juri Linkov <juri@jurta.org>
parents: 69414
diff changeset
394 (when (re-search-forward
b270f677c979 (battery-linux-proc-acpi): Also try `/proc/acpi/thermal_zone/THR2/temperature'.
Juri Linkov <juri@jurta.org>
parents: 69414
diff changeset
395 "temperature: +\\([0-9]+\\) C$" nil t)
b270f677c979 (battery-linux-proc-acpi): Also try `/proc/acpi/thermal_zone/THR2/temperature'.
Juri Linkov <juri@jurta.org>
parents: 69414
diff changeset
396 (match-string 1))))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
397 "N/A"))
55904
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
398 (cons ?r (or (and rate (concat (number-to-string rate) " "
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
399 rate-type)) "N/A"))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
400 (cons ?B (or charging-state "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
401 (cons ?b (or (and (string= charging-state "charging") "+")
69414
a159957988d5 (battery-linux-proc-acpi): Check `capacity' for non-nil
Juri Linkov <juri@jurta.org>
parents: 69381
diff changeset
402 (and capacity (< capacity low) "!")
a159957988d5 (battery-linux-proc-acpi): Check `capacity' for non-nil
Juri Linkov <juri@jurta.org>
parents: 69381
diff changeset
403 (and capacity (< capacity warn) "-")
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
404 ""))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
405 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
406 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
407 (cons ?t (or (and minutes
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
408 (format "%d:%02d" hours (- minutes (* 60 hours))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
409 "N/A"))
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
410 (cons ?p (or (and full-capacity capacity
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
411 (number-to-string
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
412 (floor (/ capacity
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
413 (/ (float full-capacity) 100)))))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
414 "N/A")))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
415
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
416
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
417 ;;; `pmset' interface for Darwin (OS X).
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
418
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
419 (defun battery-pmset ()
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
420 "Get battery status information using `pmset'.
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
421
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
422 The following %-sequences are provided:
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
423 %L Power source (verbose)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
424 %B Battery status (verbose)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
425 %b Battery status, empty means high, `-' means low,
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
426 `!' means critical, and `+' means charging
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
427 %p Battery load percentage
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
428 %h Remaining time in hours
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
429 %m Remaining time in minutes
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
430 %t Remaining time in the form `h:min'"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
431 (let (power-source load-percentage battery-status battery-status-symbol
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
432 remaining-time hours minutes)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
433 (with-temp-buffer
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
434 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
435 (goto-char (point-min))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
436 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
437 (setq power-source (match-string 1))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
438 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
439 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
440 (setq load-percentage (match-string 1))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
441 (goto-char (match-end 0))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
442 (cond ((looking-at "; charging")
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
443 (setq battery-status "charging"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
444 battery-status-symbol "+"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
445 ((< (string-to-number load-percentage) battery-load-low)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
446 (setq battery-status "low"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
447 battery-status-symbol "-"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
448 ((< (string-to-number load-percentage) battery-load-critical)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
449 (setq battery-status "critical"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
450 battery-status-symbol "!"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
451 (t
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
452 (setq battery-status "high"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
453 battery-status-symbol "")))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
454 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
455 (setq remaining-time (match-string 1))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
456 (let ((h (string-to-number (match-string 2)))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
457 (m (string-to-number (match-string 3))))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
458 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
459 minutes (number-to-string (+ (* h 60) m)))))))))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
460 (list (cons ?L (or power-source "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
461 (cons ?p (or load-percentage "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
462 (cons ?B (or battery-status "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
463 (cons ?b (or battery-status-symbol ""))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
464 (cons ?h (or hours "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
465 (cons ?m (or minutes "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
466 (cons ?t (or remaining-time "N/A")))))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
467
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
468
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469 ;;; Private functions.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 (defun battery-format (format alist)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472 "Substitute %-sequences in FORMAT."
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
473 (replace-regexp-in-string
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
474 "%."
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
475 (lambda (str)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
476 (let ((char (aref str 1)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
477 (if (eq char ?%) "%"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
478 (or (cdr (assoc char alist)) ""))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
479 format t t))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
481
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
482 (provide 'battery)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
483
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
484 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
485 ;;; battery.el ends here