Mercurial > emacs
annotate lisp/battery.el @ 42811:cf0c0ef57504
*** empty log message ***
| author | Jason Rumney <jasonr@gnu.org> |
|---|---|
| date | Thu, 17 Jan 2002 19:29:24 +0000 |
| parents | 842512c9651c |
| children | 695cf19ef79e d7ddb3e565de |
| 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 |
|
38292
1c462d61083a
(battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents:
36090
diff
changeset
|
3 ;; Copyright (C) 1997, 1998, 2000, 2001 Free Software Foundation, Inc. |
| 18388 | 4 |
|
30110
23cb074f9d88
Change author's mail address.
Gerd Moellmann <gerd@gnu.org>
parents:
26828
diff
changeset
|
5 ;; 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
|
6 ;; Keywords: hardware |
| 18388 | 7 |
|
18685
4f3350c88e6c
Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
| 18388 | 9 |
|
18685
4f3350c88e6c
Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
| 18388 | 11 ;; it under the terms of the GNU General Public License as published by |
| 12 ;; the Free Software Foundation; either version 2, or (at your option) | |
| 13 ;; any later version. | |
| 14 | |
|
18685
4f3350c88e6c
Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
| 18388 | 16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 ;; GNU General Public License for more details. | |
| 19 | |
| 20 ;; You should have received a copy of the GNU General Public License | |
|
18685
4f3350c88e6c
Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
21 ;; 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
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18388 | 23 ;; Boston, MA 02111-1307, USA. |
| 24 | |
| 25 ;;; Commentary: | |
| 26 | |
| 27 ;; There is at present only a function interpreting the new `/proc/apm' | |
| 28 ;; file format of Linux version 1.3.58 or newer. That is, what a lucky | |
| 38737 | 29 ;; coincidence, exactly the interface provided by the author's laptop. |
| 18388 | 30 |
| 31 ;;; Code: | |
| 32 | |
| 33 (require 'timer) | |
| 34 | |
| 35 | |
| 21088 | 36 |
| 37 (defgroup battery nil | |
| 38 "Display battery status information." | |
| 39 :prefix "battery-" | |
| 40 :group 'hardware) | |
| 41 | |
| 42 (defcustom battery-status-function | |
| 18388 | 43 (cond ((and (eq system-type 'gnu/linux) |
| 44 (file-readable-p "/proc/apm")) | |
|
32346
fa3f5e8f6201
(display-battery): Doc spelling fix.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
30110
diff
changeset
|
45 'battery-linux-proc-apm)) |
| 18388 | 46 "*Function for getting battery status information. |
|
36090
9ed7c3f74e35
(battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents:
32356
diff
changeset
|
47 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
|
48 Its cons cells are of the form |
| 18388 | 49 |
| 50 (CONVERSION . REPLACEMENT-TEXT) | |
| 51 | |
| 52 CONVERSION is the character code of a \"conversion specification\" | |
| 21088 | 53 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
|
54 :type '(choice (const nil) function) |
| 21088 | 55 :group 'battery) |
| 18388 | 56 |
| 21088 | 57 (defcustom battery-echo-area-format |
| 18388 | 58 (cond ((eq battery-status-function 'battery-linux-proc-apm) |
| 59 "Power %L, battery %B (%p%% load, remaining time %t)")) | |
| 60 "*Control string formatting the string to display in the echo area. | |
| 61 Ordinary characters in the control string are printed as-is, while | |
| 62 conversion specifications introduced by a `%' character in the control | |
| 63 string are substituted as defined by the current value of the variable | |
| 21088 | 64 `battery-status-function'." |
| 65 :type '(choice string (const nil)) | |
| 66 :group 'battery) | |
| 18388 | 67 |
| 68 (defvar battery-mode-line-string nil | |
| 69 "String to display in the mode line.") | |
| 70 | |
| 21088 | 71 (defcustom battery-mode-line-format |
| 18388 | 72 (cond ((eq battery-status-function 'battery-linux-proc-apm) |
| 73 " [%b%p%%]")) | |
| 74 "*Control string formatting the string to display in the mode line. | |
| 75 Ordinary characters in the control string are printed as-is, while | |
| 76 conversion specifications introduced by a `%' character in the control | |
| 77 string are substituted as defined by the current value of the variable | |
| 21088 | 78 `battery-status-function'." |
| 79 :type '(choice string (const nil)) | |
| 80 :group 'battery) | |
| 18388 | 81 |
| 21088 | 82 (defcustom battery-update-interval 60 |
| 83 "*Seconds after which the battery status will be updated." | |
| 84 :type 'integer | |
| 85 :group 'battery) | |
| 18388 | 86 |
| 87 (defvar battery-update-timer nil | |
| 88 "Interval timer object.") | |
| 89 | |
|
21076
dc82196eac58
Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents:
20972
diff
changeset
|
90 ;;;###autoload |
| 18388 | 91 (defun battery () |
| 92 "Display battery status information in the echo area. | |
| 26828 | 93 The text being displayed in the echo area is controlled by the variables |
| 18388 | 94 `battery-echo-area-format' and `battery-status-function'." |
| 95 (interactive) | |
| 96 (message "%s" (if (and battery-echo-area-format battery-status-function) | |
| 97 (battery-format battery-echo-area-format | |
| 98 (funcall battery-status-function)) | |
| 99 "Battery status not available"))) | |
| 100 | |
|
21076
dc82196eac58
Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents:
20972
diff
changeset
|
101 ;;;###autoload |
| 18388 | 102 (defun display-battery () |
| 103 "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
|
104 The text being displayed in the mode line is controlled by the variables |
| 18388 | 105 `battery-mode-line-format' and `battery-status-function'. |
| 106 The mode line will be updated automatically every `battery-update-interval' | |
| 107 seconds." | |
| 108 (interactive) | |
| 109 (setq battery-mode-line-string "") | |
| 110 (or global-mode-string (setq global-mode-string '(""))) | |
|
32356
fb23c215bdc8
(display-battery): Use `add-to-list'.
Miles Bader <miles@gnu.org>
parents:
32346
diff
changeset
|
111 (add-to-list 'global-mode-string 'battery-mode-line-string t) |
| 18388 | 112 (and battery-update-timer (cancel-timer battery-update-timer)) |
| 113 (setq battery-update-timer (run-at-time nil battery-update-interval | |
| 114 'battery-update-handler)) | |
| 115 (battery-update)) | |
| 116 | |
| 117 (defun battery-update-handler () | |
| 118 (battery-update) | |
| 119 (sit-for 0)) | |
| 120 | |
| 121 (defun battery-update () | |
| 122 "Update battery status information in the mode line." | |
|
38292
1c462d61083a
(battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents:
36090
diff
changeset
|
123 (setq battery-mode-line-string (propertize (if (and battery-mode-line-format |
|
1c462d61083a
(battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents:
36090
diff
changeset
|
124 battery-status-function) |
|
1c462d61083a
(battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents:
36090
diff
changeset
|
125 (battery-format |
|
1c462d61083a
(battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents:
36090
diff
changeset
|
126 battery-mode-line-format |
|
1c462d61083a
(battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents:
36090
diff
changeset
|
127 (funcall battery-status-function)) |
|
1c462d61083a
(battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents:
36090
diff
changeset
|
128 "") |
|
1c462d61083a
(battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents:
36090
diff
changeset
|
129 'help-echo "Battery status information")) |
| 18388 | 130 (force-mode-line-update)) |
| 131 | |
| 132 | |
| 133 ;;; `/proc/apm' interface for Linux. | |
| 134 | |
| 135 (defconst battery-linux-proc-apm-regexp | |
| 136 (concat "^\\([^ ]+\\)" ; Driver version. | |
| 137 " \\([^ ]+\\)" ; APM BIOS version. | |
| 138 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags. | |
| 139 " 0x\\([0-9a-f]+\\)" ; AC line status. | |
| 140 " 0x\\([0-9a-f]+\\)" ; Battery status. | |
| 141 " 0x\\([0-9a-f]+\\)" ; Battery flags. | |
|
20972
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
142 " \\(-?[0-9]+\\)%" ; Load percentage. |
|
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
143 " \\(-?[0-9]+\\)" ; Remaining time. |
| 18388 | 144 " \\(.*\\)" ; Time unit. |
| 145 "$") | |
| 146 "Regular expression matching contents of `/proc/apm'.") | |
| 147 | |
| 148 (defun battery-linux-proc-apm () | |
| 149 "Get APM status information from Linux kernel. | |
| 150 This function works only with the new `/proc/apm' format introduced | |
| 151 in Linux version 1.3.58. | |
| 152 | |
| 153 The following %-sequences are provided: | |
| 154 %v Linux driver version | |
| 155 %V APM BIOS version | |
| 156 %I APM BIOS status (verbose) | |
| 157 %L AC line status (verbose) | |
| 158 %B Battery status (verbose) | |
| 159 %b Battery status, empty means high, `-' means low, | |
| 160 `!' means critical, and `+' means charging | |
| 161 %p battery load percentage | |
| 162 %s Remaining time in seconds | |
| 163 %m Remaining time in minutes | |
| 164 %h Remaining time in hours | |
| 165 %t Remaining time in the form `h:min'" | |
| 166 (let (driver-version bios-version bios-interface line-status | |
| 167 battery-status battery-status-symbol load-percentage | |
| 168 seconds minutes hours remaining-time buffer tem) | |
| 169 (unwind-protect | |
| 170 (save-excursion | |
|
21317
566100bc6eef
(battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents:
21088
diff
changeset
|
171 (setq buffer (get-buffer-create " *battery*")) |
| 18388 | 172 (set-buffer buffer) |
|
21317
566100bc6eef
(battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents:
21088
diff
changeset
|
173 (erase-buffer) |
|
41919
842512c9651c
(battery-insert-file-contents): Obsolete function removed.
Noah Friedman <friedman@splode.com>
parents:
38737
diff
changeset
|
174 (insert-file-contents "/proc/apm") |
| 18388 | 175 (re-search-forward battery-linux-proc-apm-regexp) |
| 176 (setq driver-version (match-string 1)) | |
| 177 (setq bios-version (match-string 2)) | |
| 178 (setq tem (battery-hex-to-int-2 (match-string 3))) | |
| 179 (if (not (logand tem 2)) | |
| 180 (setq bios-interface "not supported") | |
| 181 (setq bios-interface "enabled") | |
| 182 (cond ((logand tem 16) (setq bios-interface "disabled")) | |
| 183 ((logand tem 32) (setq bios-interface "disengaged"))) | |
| 184 (setq tem (battery-hex-to-int-2 (match-string 4))) | |
| 185 (cond ((= tem 0) (setq line-status "off-line")) | |
| 186 ((= tem 1) (setq line-status "on-line")) | |
| 187 ((= tem 2) (setq line-status "on backup"))) | |
| 188 (setq tem (battery-hex-to-int-2 (match-string 6))) | |
| 189 (if (= tem 255) | |
| 190 (setq battery-status "N/A") | |
| 191 (setq tem (battery-hex-to-int-2 (match-string 5))) | |
| 192 (cond ((= tem 0) (setq battery-status "high" | |
| 193 battery-status-symbol "")) | |
| 194 ((= tem 1) (setq battery-status "low" | |
| 195 battery-status-symbol "-")) | |
| 196 ((= tem 2) (setq battery-status "critical" | |
| 197 battery-status-symbol "!")) | |
| 198 ((= tem 3) (setq battery-status "charging" | |
| 199 battery-status-symbol "+"))) | |
| 200 (setq load-percentage (match-string 7)) | |
| 201 (setq seconds (string-to-number (match-string 8))) | |
| 202 (and (string-equal (match-string 9) "min") | |
| 203 (setq seconds (* 60 seconds))) | |
| 204 (setq minutes (/ seconds 60) | |
| 205 hours (/ seconds 3600)) | |
| 206 (setq remaining-time | |
|
21317
566100bc6eef
(battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents:
21088
diff
changeset
|
207 (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
|
208 (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
|
209 (cons ?V (or bios-version "N/A")) |
|
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
210 (cons ?I (or bios-interface "N/A")) |
|
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
211 (cons ?L (or line-status "N/A")) |
|
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
212 (cons ?B (or battery-status "N/A")) |
|
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
213 (cons ?b (or battery-status-symbol "")) |
|
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
214 (cons ?p (or load-percentage "N/A")) |
|
9dff083ee7a9
(battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents:
18685
diff
changeset
|
215 (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
|
216 (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
|
217 (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
|
218 (cons ?t (or remaining-time "N/A"))))) |
| 18388 | 219 |
| 220 | |
| 221 ;;; Private functions. | |
| 222 | |
| 223 (defun battery-format (format alist) | |
| 224 "Substitute %-sequences in FORMAT." | |
| 225 (let ((index 0) | |
| 226 (length (length format)) | |
| 227 (result "") | |
| 228 char flag elem) | |
| 229 (while (< index length) | |
| 230 (setq char (aref format index)) | |
| 231 (if (not flag) | |
| 232 (if (char-equal char ?%) | |
| 233 (setq flag t) | |
| 234 (setq result (concat result (char-to-string char)))) | |
| 235 (cond ((char-equal char ?%) | |
| 236 (setq result (concat result "%"))) | |
| 237 ((setq elem (assoc char alist)) | |
| 238 (setq result (concat result (cdr elem))))) | |
| 239 (setq flag nil)) | |
| 240 (setq index (1+ index))) | |
| 241 (or (null flag) | |
| 242 (setq result (concat result "%"))) | |
| 243 result)) | |
| 244 | |
| 245 (defconst battery-hex-map '((?0 . 0) (?1 . 1) (?2 . 2) (?3 . 3) | |
| 246 (?4 . 4) (?5 . 5) (?6 . 6) (?7 . 7) | |
| 247 (?8 . 8) (?9 . 9) (?a . 10) (?b . 11) | |
| 248 (?c . 12) (?d . 13) (?e . 14) (?f . 15))) | |
| 249 | |
| 250 (defun battery-hex-to-int (string) | |
| 251 "Convert a hexadecimal number (a string) into a number." | |
| 252 (save-match-data | |
| 253 (and (string-match "^[ \t]+" string) | |
| 254 (setq string (substring string (match-end 0)))) | |
| 255 (and (string-match "^0[xX]" string) | |
| 256 (setq string (substring string (match-end 0))))) | |
| 257 (battery-hex-to-int-2 string)) | |
| 258 | |
| 259 (defun battery-hex-to-int-2 (string) | |
| 260 (let ((index 0) | |
| 261 (length (length string)) | |
| 262 (value 0) | |
| 263 (elem nil)) | |
| 264 (while (and (< index length) | |
| 265 (setq elem (assoc (downcase (aref string index)) | |
| 266 battery-hex-map))) | |
| 267 (setq value (+ (* 16 value) (cdr elem)) | |
| 268 index (1+ index))) | |
| 269 value)) | |
| 270 | |
| 271 | |
| 272 (provide 'battery) | |
| 273 | |
| 274 ;;; battery.el ends here |
