Mercurial > emacs
annotate lisp/battery.el @ 62372:4560134d21fa
(open-network-stream-nowait): Remove.
(open-network-stream-server): Remove.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Sun, 15 May 2005 20:42:43 +0000 |
parents | abd0d265b4ba |
children | 1f503fd2046d 02f1dbc4a199 |
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 | 2 |
59363
9120644ff426
(display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59132
diff
changeset
|
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2003, 2004, 2005 |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
4 ;; Free Software Foundation, Inc. |
18388 | 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 | 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 | 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 | 12 ;; it under the terms of the GNU General Public License as published by |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
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 | 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 | |
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 |
4f3350c88e6c
Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18388 | 24 ;; Boston, MA 02111-1307, USA. |
25 | |
26 ;;; Commentary: | |
27 | |
52470
b4d15685afef
2003-09-10 Mario Lang <mlang@delysid.org>
Alex Schroeder <alex@gnu.org>
parents:
52450
diff
changeset
|
28 ;; There is at present support for interpreting the new `/proc/apm' |
b4d15685afef
2003-09-10 Mario Lang <mlang@delysid.org>
Alex Schroeder <alex@gnu.org>
parents:
52450
diff
changeset
|
29 ;; file format of Linux version 1.3.58 or newer and for the `/proc/acpi/' |
b4d15685afef
2003-09-10 Mario Lang <mlang@delysid.org>
Alex Schroeder <alex@gnu.org>
parents:
52450
diff
changeset
|
30 ;; directory structure of Linux 2.4.20 and 2.6. |
18388 | 31 |
32 ;;; Code: | |
33 | |
34 (require 'timer) | |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
35 (eval-when-compile (require 'cl)) |
18388 | 36 |
37 | |
21088 | 38 (defgroup battery nil |
39 "Display battery status information." | |
40 :prefix "battery-" | |
41 :group 'hardware) | |
42 | |
43 (defcustom battery-status-function | |
18388 | 44 (cond ((and (eq system-type 'gnu/linux) |
45 (file-readable-p "/proc/apm")) | |
52450
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
46 'battery-linux-proc-apm) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
47 ((and (eq system-type 'gnu/linux) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
48 (file-directory-p "/proc/acpi/battery")) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
49 'battery-linux-proc-acpi)) |
18388 | 50 "*Function for getting battery status information. |
36090
9ed7c3f74e35
(battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents:
32356
diff
changeset
|
51 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
|
52 Its cons cells are of the form |
18388 | 53 |
54 (CONVERSION . REPLACEMENT-TEXT) | |
55 | |
56 CONVERSION is the character code of a \"conversion specification\" | |
21088 | 57 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
|
58 :type '(choice (const nil) function) |
21088 | 59 :group 'battery) |
18388 | 60 |
21088 | 61 (defcustom battery-echo-area-format |
18388 | 62 (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
|
63 "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
|
64 ((eq battery-status-function 'battery-linux-proc-acpi) |
55904
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
65 "Power %L, battery %B at %r (%p%% load, remaining time %t)")) |
18388 | 66 "*Control string formatting the string to display in the echo area. |
67 Ordinary characters in the control string are printed as-is, while | |
68 conversion specifications introduced by a `%' character in the control | |
69 string are substituted as defined by the current value of the variable | |
21088 | 70 `battery-status-function'." |
71 :type '(choice string (const nil)) | |
72 :group 'battery) | |
18388 | 73 |
74 (defvar battery-mode-line-string nil | |
75 "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
|
76 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t) |
18388 | 77 |
21088 | 78 (defcustom battery-mode-line-format |
18388 | 79 (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
|
80 "[%b%p%%]") |
52450
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
81 ((eq battery-status-function 'battery-linux-proc-acpi) |
59132
38f1d5865861
(battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents:
57143
diff
changeset
|
82 "[%b%p%%,%d°C]")) |
18388 | 83 "*Control string formatting the string to display in the mode line. |
84 Ordinary characters in the control string are printed as-is, while | |
85 conversion specifications introduced by a `%' character in the control | |
86 string are substituted as defined by the current value of the variable | |
21088 | 87 `battery-status-function'." |
88 :type '(choice string (const nil)) | |
89 :group 'battery) | |
18388 | 90 |
21088 | 91 (defcustom battery-update-interval 60 |
92 "*Seconds after which the battery status will be updated." | |
93 :type 'integer | |
94 :group 'battery) | |
18388 | 95 |
96 (defvar battery-update-timer nil | |
97 "Interval timer object.") | |
98 | |
21076
dc82196eac58
Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents:
20972
diff
changeset
|
99 ;;;###autoload |
18388 | 100 (defun battery () |
101 "Display battery status information in the echo area. | |
26828 | 102 The text being displayed in the echo area is controlled by the variables |
18388 | 103 `battery-echo-area-format' and `battery-status-function'." |
104 (interactive) | |
105 (message "%s" (if (and battery-echo-area-format battery-status-function) | |
106 (battery-format battery-echo-area-format | |
107 (funcall battery-status-function)) | |
108 "Battery status not available"))) | |
109 | |
21076
dc82196eac58
Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents:
20972
diff
changeset
|
110 ;;;###autoload |
59363
9120644ff426
(display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59132
diff
changeset
|
111 (define-minor-mode display-battery-mode |
18388 | 112 "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
|
113 The text being displayed in the mode line is controlled by the variables |
18388 | 114 `battery-mode-line-format' and `battery-status-function'. |
115 The mode line will be updated automatically every `battery-update-interval' | |
116 seconds." | |
61268
abd0d265b4ba
(display-battery-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
59363
diff
changeset
|
117 :global t :group 'battery |
18388 | 118 (setq battery-mode-line-string "") |
119 (or global-mode-string (setq global-mode-string '(""))) | |
120 (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
|
121 (if (not display-battery-mode) |
9120644ff426
(display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59132
diff
changeset
|
122 (setq global-mode-string |
9120644ff426
(display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59132
diff
changeset
|
123 (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
|
124 (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
|
125 (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
|
126 'battery-update-handler)) |
9120644ff426
(display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59132
diff
changeset
|
127 (battery-update))) |
18388 | 128 |
129 (defun battery-update-handler () | |
130 (battery-update) | |
131 (sit-for 0)) | |
132 | |
133 (defun battery-update () | |
134 "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
|
135 (setq battery-mode-line-string |
38f1d5865861
(battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents:
57143
diff
changeset
|
136 (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
|
137 battery-status-function) |
38f1d5865861
(battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents:
57143
diff
changeset
|
138 (battery-format |
38f1d5865861
(battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents:
57143
diff
changeset
|
139 battery-mode-line-format |
38f1d5865861
(battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents:
57143
diff
changeset
|
140 (funcall battery-status-function)) |
38f1d5865861
(battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents:
57143
diff
changeset
|
141 "") |
38f1d5865861
(battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents:
57143
diff
changeset
|
142 'help-echo "Battery status information")) |
18388 | 143 (force-mode-line-update)) |
144 | |
145 | |
146 ;;; `/proc/apm' interface for Linux. | |
147 | |
148 (defconst battery-linux-proc-apm-regexp | |
149 (concat "^\\([^ ]+\\)" ; Driver version. | |
150 " \\([^ ]+\\)" ; APM BIOS version. | |
151 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags. | |
152 " 0x\\([0-9a-f]+\\)" ; AC line status. | |
153 " 0x\\([0-9a-f]+\\)" ; Battery status. | |
154 " 0x\\([0-9a-f]+\\)" ; Battery flags. | |
20972
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
155 " \\(-?[0-9]+\\)%" ; Load percentage. |
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
156 " \\(-?[0-9]+\\)" ; Remaining time. |
18388 | 157 " \\(.*\\)" ; Time unit. |
158 "$") | |
159 "Regular expression matching contents of `/proc/apm'.") | |
160 | |
161 (defun battery-linux-proc-apm () | |
162 "Get APM status information from Linux kernel. | |
163 This function works only with the new `/proc/apm' format introduced | |
164 in Linux version 1.3.58. | |
165 | |
166 The following %-sequences are provided: | |
167 %v Linux driver version | |
168 %V APM BIOS version | |
169 %I APM BIOS status (verbose) | |
170 %L AC line status (verbose) | |
171 %B Battery status (verbose) | |
172 %b Battery status, empty means high, `-' means low, | |
173 `!' means critical, and `+' means charging | |
174 %p battery load percentage | |
175 %s Remaining time in seconds | |
176 %m Remaining time in minutes | |
177 %h Remaining time in hours | |
178 %t Remaining time in the form `h:min'" | |
179 (let (driver-version bios-version bios-interface line-status | |
180 battery-status battery-status-symbol load-percentage | |
181 seconds minutes hours remaining-time buffer tem) | |
182 (unwind-protect | |
183 (save-excursion | |
21317
566100bc6eef
(battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents:
21088
diff
changeset
|
184 (setq buffer (get-buffer-create " *battery*")) |
18388 | 185 (set-buffer buffer) |
21317
566100bc6eef
(battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents:
21088
diff
changeset
|
186 (erase-buffer) |
41919
842512c9651c
(battery-insert-file-contents): Obsolete function removed.
Noah Friedman <friedman@splode.com>
parents:
38737
diff
changeset
|
187 (insert-file-contents "/proc/apm") |
18388 | 188 (re-search-forward battery-linux-proc-apm-regexp) |
189 (setq driver-version (match-string 1)) | |
190 (setq bios-version (match-string 2)) | |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
191 (setq tem (string-to-number (match-string 3) 16)) |
18388 | 192 (if (not (logand tem 2)) |
193 (setq bios-interface "not supported") | |
194 (setq bios-interface "enabled") | |
195 (cond ((logand tem 16) (setq bios-interface "disabled")) | |
196 ((logand tem 32) (setq bios-interface "disengaged"))) | |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
197 (setq tem (string-to-number (match-string 4) 16)) |
18388 | 198 (cond ((= tem 0) (setq line-status "off-line")) |
199 ((= tem 1) (setq line-status "on-line")) | |
200 ((= tem 2) (setq line-status "on backup"))) | |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
201 (setq tem (string-to-number (match-string 6) 16)) |
18388 | 202 (if (= tem 255) |
203 (setq battery-status "N/A") | |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
204 (setq tem (string-to-number (match-string 5) 16)) |
18388 | 205 (cond ((= tem 0) (setq battery-status "high" |
206 battery-status-symbol "")) | |
207 ((= tem 1) (setq battery-status "low" | |
208 battery-status-symbol "-")) | |
209 ((= tem 2) (setq battery-status "critical" | |
210 battery-status-symbol "!")) | |
211 ((= tem 3) (setq battery-status "charging" | |
212 battery-status-symbol "+"))) | |
213 (setq load-percentage (match-string 7)) | |
214 (setq seconds (string-to-number (match-string 8))) | |
215 (and (string-equal (match-string 9) "min") | |
216 (setq seconds (* 60 seconds))) | |
217 (setq minutes (/ seconds 60) | |
218 hours (/ seconds 3600)) | |
219 (setq remaining-time | |
21317
566100bc6eef
(battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents:
21088
diff
changeset
|
220 (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
|
221 (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
|
222 (cons ?V (or bios-version "N/A")) |
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
223 (cons ?I (or bios-interface "N/A")) |
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
224 (cons ?L (or line-status "N/A")) |
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
225 (cons ?B (or battery-status "N/A")) |
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
226 (cons ?b (or battery-status-symbol "")) |
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
227 (cons ?p (or load-percentage "N/A")) |
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
228 (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
|
229 (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
|
230 (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
|
231 (cons ?t (or remaining-time "N/A"))))) |
18388 | 232 |
233 | |
52450
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
234 ;;; `/proc/acpi/' interface for Linux. |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
235 |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
236 (defun battery-linux-proc-acpi () |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
237 "Get ACPI status information from Linux kernel. |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
238 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
|
239 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
|
240 |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
241 The following %-sequences are provided: |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
242 %c Current capacity (mAh) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
243 %B Battery status (verbose) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
244 %b Battery status, empty means high, `-' means low, |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
245 `!' means critical, and `+' means charging |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
246 %d Temperature (in degrees Celsius) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
247 %L AC line status (verbose) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
248 %p battery load percentage |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
249 %m Remaining time in minutes |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
250 %h Remaining time in hours |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
251 %t Remaining time 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
|
252 (let ((design-capacity 0) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
253 (warn 0) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
254 (low 0) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
255 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
|
256 ;; 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
|
257 ;; 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
|
258 ;; information together since displaying for a variable amount of |
fd1a6df8927e
Delete superfluous empty lines.
Eli Zaretskii <eliz@gnu.org>
parents:
56663
diff
changeset
|
259 ;; batteries seems overkill for format-strings. |
fd1a6df8927e
Delete superfluous empty lines.
Eli Zaretskii <eliz@gnu.org>
parents:
56663
diff
changeset
|
260 (with-temp-buffer |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
261 (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
|
262 t "\\`[^.]"))) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
263 (erase-buffer) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
264 (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
|
265 (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
|
266 (and (re-search-forward "charging state: +\\(.*\\)$" nil t) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
267 (member charging-state '("unknown" nil)) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
268 ;; 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
|
269 ;; 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
|
270 ;; "unknown". |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
271 (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
|
272 (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
|
273 nil t) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
274 (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
|
275 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
|
276 (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
|
277 rate-type |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
278 (error |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
279 "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
|
280 rate-type (match-string 2)))) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
281 (match-string 2)))) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
282 (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
|
283 nil t) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
284 (setq capacity |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
285 (+ (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
|
286 (goto-char (point-max)) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
287 (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
|
288 (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
|
289 (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
|
290 nil t) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
291 (incf design-capacity (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
|
292 (when (re-search-forward |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
293 "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
|
294 (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
|
295 (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
|
296 nil t) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
297 (incf low (string-to-number (match-string 1))))))) |
52450
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
298 (and capacity rate |
55904
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
299 (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
|
300 (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
|
301 "charging") |
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
302 (- design-capacity capacity) |
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
303 capacity)) rate) 60))) |
52450
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
304 hours (/ minutes 60))) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
305 (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
|
306 (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
|
307 (with-temp-buffer |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
308 (insert-file-contents |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
309 "/proc/acpi/ac_adapter/AC/state") |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
310 (when (re-search-forward "state: +\\(.*\\)$" nil t) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
311 (match-string 1)))) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
312 "N/A")) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
313 (cons ?d (or (when (file-exists-p |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
314 "/proc/acpi/thermal_zone/THRM/temperature") |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
315 (with-temp-buffer |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
316 (insert-file-contents |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
317 "/proc/acpi/thermal_zone/THRM/temperature") |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
318 (when (re-search-forward |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
319 "temperature: +\\([0-9]+\\) C$" nil t) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
320 (match-string 1)))) |
55904
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
321 (when (file-exists-p |
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
322 "/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
|
323 (with-temp-buffer |
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
324 (insert-file-contents |
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
325 "/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
|
326 (when (re-search-forward |
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
327 "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
|
328 (match-string 1)))) |
52450
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
329 "N/A")) |
55904
f3be7c4dc1e6
(battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents:
52470
diff
changeset
|
330 (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
|
331 rate-type)) "N/A")) |
52450
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
332 (cons ?B (or charging-state "N/A")) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
333 (cons ?b (or (and (string= charging-state "charging") "+") |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
334 (and (< capacity low) "!") |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
335 (and (< capacity warn) "-") |
52450
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
336 "")) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
337 (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
|
338 (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
|
339 (cons ?t (or (and minutes |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
340 (format "%d:%02d" hours (- minutes (* 60 hours)))) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
341 "N/A")) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
342 (cons ?p (or (and design-capacity capacity |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
343 (number-to-string |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
344 (floor (/ capacity |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
345 (/ (float design-capacity) 100))))) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
346 "N/A"))))) |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
347 |
d096f00804db
(battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
348 |
18388 | 349 ;;; Private functions. |
350 | |
351 (defun battery-format (format alist) | |
352 "Substitute %-sequences in FORMAT." | |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
353 (replace-regexp-in-string |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
354 "%." |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
355 (lambda (str) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
356 (let ((char (aref str 1))) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
357 (if (eq char ?%) "%" |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
358 (or (cdr (assoc char alist)) "")))) |
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
359 format t t)) |
18388 | 360 |
361 | |
362 (provide 'battery) | |
363 | |
57143
b628e08b0230
(battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57133
diff
changeset
|
364 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37 |
18388 | 365 ;;; battery.el ends here |