annotate lisp/battery.el @ 32407:ca7ec5a76115

fix last
author Dave Love <fx@gnu.org>
date Thu, 12 Oct 2000 09:19:23 +0000
parents fb23c215bdc8
children 9ed7c3f74e35
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; battery.el --- display battery status information.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
32356
fb23c215bdc8 (display-battery): Use `add-to-list'.
Miles Bader <miles@gnu.org>
parents: 32346
diff changeset
3 ;; Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; 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
12 ;; the Free Software Foundation; either version 2, or (at your option)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; any later version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 ;; Boston, MA 02111-1307, USA.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 ;;; Commentary:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;; There is at present only a function interpreting the new `/proc/apm'
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;; file format of Linux version 1.3.58 or newer. That is, what a lucky
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; coincidence, exactly the interface provided by the author's labtop.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;;; Code:
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 (require 'timer)
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
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
36
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
37 (defgroup battery nil
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
38 "Display battery status information."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
39 :prefix "battery-"
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
40 :group 'hardware)
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
41
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
42 (defcustom battery-status-function
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 (cond ((and (eq system-type 'gnu/linux)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 "*Function for getting battery status information.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 The function have to return an alist of conversion definitions.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 Cons cells are of the form
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 (CONVERSION . REPLACEMENT-TEXT)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 CONVERSION is the character code of a \"conversion specification\"
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
53 introduced by a `%' character in a control string."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
54 :type 'function
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
55 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
57 (defcustom battery-echo-area-format
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 (cond ((eq battery-status-function 'battery-linux-proc-apm)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 "Power %L, battery %B (%p%% load, remaining time %t)"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 "*Control string formatting the string to display in the echo area.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 Ordinary characters in the control string are printed as-is, while
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 conversion specifications introduced by a `%' character in the control
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 string are substituted as defined by the current value of the variable
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
64 `battery-status-function'."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
65 :type '(choice string (const nil))
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
66 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 (defvar battery-mode-line-string nil
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 "String to display in the mode line.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
71 (defcustom battery-mode-line-format
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 (cond ((eq battery-status-function 'battery-linux-proc-apm)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 " [%b%p%%]"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 "*Control string formatting the string to display in the mode line.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 Ordinary characters in the control string are printed as-is, while
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 conversion specifications introduced by a `%' character in the control
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 string are substituted as defined by the current value of the variable
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
78 `battery-status-function'."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
79 :type '(choice string (const nil))
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
80 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
82 (defcustom battery-update-interval 60
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
83 "*Seconds after which the battery status will be updated."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
84 :type 'integer
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
85 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 (defvar battery-update-timer nil
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 "Interval timer object.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89
21076
dc82196eac58 Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents: 20972
diff changeset
90 ;;;###autoload
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 (defun battery ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 "Display battery status information in the echo area.
26828
983f253505d0 (battery): Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents: 21317
diff changeset
93 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
94 `battery-echo-area-format' and `battery-status-function'."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 (interactive)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 (message "%s" (if (and battery-echo-area-format battery-status-function)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 (battery-format battery-echo-area-format
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 (funcall battery-status-function))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 "Battery status not available")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100
21076
dc82196eac58 Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents: 20972
diff changeset
101 ;;;###autoload
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 (defun display-battery ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 `battery-mode-line-format' and `battery-status-function'.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 The mode line will be updated automatically every `battery-update-interval'
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 seconds."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 (interactive)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 (setq battery-mode-line-string "")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 (and battery-update-timer (cancel-timer battery-update-timer))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 (setq battery-update-timer (run-at-time nil battery-update-interval
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 'battery-update-handler))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 (battery-update))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 (defun battery-update-handler ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 (battery-update)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 (sit-for 0))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 (defun battery-update ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 "Update battery status information in the mode line."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 (setq battery-mode-line-string (if (and battery-mode-line-format
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 battery-status-function)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (battery-format
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 battery-mode-line-format
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 (funcall battery-status-function))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 ""))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 (force-mode-line-update))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 ;;; `/proc/apm' interface for Linux.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 (defconst battery-linux-proc-apm-regexp
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 (concat "^\\([^ ]+\\)" ; Driver version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 " \\([^ ]+\\)" ; APM BIOS version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 " 0x\\([0-9a-f]+\\)" ; AC line status.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 " 0x\\([0-9a-f]+\\)" ; Battery status.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 " 0x\\([0-9a-f]+\\)" ; Battery flags.
20972
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
141 " \\(-?[0-9]+\\)%" ; Load percentage.
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
142 " \\(-?[0-9]+\\)" ; Remaining time.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 " \\(.*\\)" ; Time unit.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 "$")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 "Regular expression matching contents of `/proc/apm'.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (defun battery-linux-proc-apm ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 "Get APM status information from Linux kernel.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 This function works only with the new `/proc/apm' format introduced
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 in Linux version 1.3.58.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 The following %-sequences are provided:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 %v Linux driver version
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 %V APM BIOS version
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 %I APM BIOS status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 %L AC line status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 %B Battery status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 %b Battery status, empty means high, `-' means low,
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 `!' means critical, and `+' means charging
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 %p battery load percentage
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 %s Remaining time in seconds
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 %m Remaining time in minutes
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 %h Remaining time in hours
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 %t Remaining time in the form `h:min'"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 (let (driver-version bios-version bios-interface line-status
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 battery-status battery-status-symbol load-percentage
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 seconds minutes hours remaining-time buffer tem)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (unwind-protect
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 (save-excursion
21317
566100bc6eef (battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents: 21088
diff changeset
170 (setq buffer (get-buffer-create " *battery*"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 (set-buffer buffer)
21317
566100bc6eef (battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents: 21088
diff changeset
172 (erase-buffer)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 (battery-insert-file-contents "/proc/apm")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 (re-search-forward battery-linux-proc-apm-regexp)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (setq driver-version (match-string 1))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (setq bios-version (match-string 2))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 (setq tem (battery-hex-to-int-2 (match-string 3)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 (if (not (logand tem 2))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (setq bios-interface "not supported")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 (setq bios-interface "enabled")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 (cond ((logand tem 16) (setq bios-interface "disabled"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 ((logand tem 32) (setq bios-interface "disengaged")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 (setq tem (battery-hex-to-int-2 (match-string 4)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 (cond ((= tem 0) (setq line-status "off-line"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 ((= tem 1) (setq line-status "on-line"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 ((= tem 2) (setq line-status "on backup")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (setq tem (battery-hex-to-int-2 (match-string 6)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 (if (= tem 255)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (setq battery-status "N/A")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 (setq tem (battery-hex-to-int-2 (match-string 5)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 (cond ((= tem 0) (setq battery-status "high"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 battery-status-symbol ""))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 ((= tem 1) (setq battery-status "low"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 battery-status-symbol "-"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 ((= tem 2) (setq battery-status "critical"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 battery-status-symbol "!"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 ((= tem 3) (setq battery-status "charging"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 battery-status-symbol "+")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 (setq load-percentage (match-string 7))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 (setq seconds (string-to-number (match-string 8)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (and (string-equal (match-string 9) "min")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (setq seconds (* 60 seconds)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 (setq minutes (/ seconds 60)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 hours (/ seconds 3600))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 (setq remaining-time
21317
566100bc6eef (battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents: 21088
diff changeset
206 (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
207 (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
208 (cons ?V (or bios-version "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
209 (cons ?I (or bios-interface "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
210 (cons ?L (or line-status "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
211 (cons ?B (or battery-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-symbol ""))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
213 (cons ?p (or load-percentage "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
214 (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
215 (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
216 (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
217 (cons ?t (or remaining-time "N/A")))))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 ;;; Private functions.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 (defun battery-format (format alist)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 "Substitute %-sequences in FORMAT."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 (let ((index 0)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 (length (length format))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 (result "")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 char flag elem)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 (while (< index length)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 (setq char (aref format index))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (if (not flag)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (if (char-equal char ?%)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 (setq flag t)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 (setq result (concat result (char-to-string char))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 (cond ((char-equal char ?%)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 (setq result (concat result "%")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 ((setq elem (assoc char alist))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (setq result (concat result (cdr elem)))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 (setq flag nil))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (setq index (1+ index)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 (or (null flag)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (setq result (concat result "%")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 result))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 (defun battery-insert-file-contents (file-name)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 "Insert contents of file FILE-NAME after point.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 FILE-NAME can be a non-ordinary file, for example, a named pipe.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 Return t if file exists."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 (let ((load-read-function 'battery-read-function)
21317
566100bc6eef (battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents: 21088
diff changeset
249 (load-source-file-function nil)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 (load-path '("."))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (load-history nil))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 (save-excursion
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 (load file-name nil t t))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 (defun battery-read-function (&optional stream)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 "Function for reading expressions from STREAM.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 Value is always nil."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 (let (char)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 (while (not (< (setq char (get-file-char)) 0))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 (insert char))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 (defconst battery-hex-map '((?0 . 0) (?1 . 1) (?2 . 2) (?3 . 3)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 (?4 . 4) (?5 . 5) (?6 . 6) (?7 . 7)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 (?8 . 8) (?9 . 9) (?a . 10) (?b . 11)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 (?c . 12) (?d . 13) (?e . 14) (?f . 15)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 (defun battery-hex-to-int (string)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 "Convert a hexadecimal number (a string) into a number."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 (save-match-data
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (and (string-match "^[ \t]+" string)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 (setq string (substring string (match-end 0))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 (and (string-match "^0[xX]" string)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 (setq string (substring string (match-end 0)))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 (battery-hex-to-int-2 string))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 (defun battery-hex-to-int-2 (string)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 (let ((index 0)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 (length (length string))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 (value 0)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 (elem nil))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 (while (and (< index length)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 (setq elem (assoc (downcase (aref string index))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 battery-hex-map)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 (setq value (+ (* 16 value) (cdr elem))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 index (1+ index)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 value))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 (provide 'battery)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 ;;; battery.el ends here