annotate lisp/battery.el @ 54535:f5c18c1f02ce

(appt-check): Remove superfluous progn. When finished with diary buffer: if it was not being displayed before, kill it; otherwise restore its original state. Suggested by Matthew Mundell <matt@mundell.ukfsn.org>.
author Glenn Morris <rgm@gnu.org>
date Wed, 24 Mar 2004 18:36:20 +0000
parents b4d15685afef
children f3be7c4dc1e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38436
b174db545cfd Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 38292
diff changeset
1 ;;; battery.el --- display battery status information
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
52470
b4d15685afef 2003-09-10 Mario Lang <mlang@delysid.org>
Alex Schroeder <alex@gnu.org>
parents: 52450
diff changeset
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2003 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
52470
b4d15685afef 2003-09-10 Mario Lang <mlang@delysid.org>
Alex Schroeder <alex@gnu.org>
parents: 52450
diff changeset
27 ;; 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
28 ;; 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
29 ;; directory structure of Linux 2.4.20 and 2.6.
18388
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"))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
45 'battery-linux-proc-apm)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
46 ((and (eq system-type 'gnu/linux)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
47 (file-directory-p "/proc/acpi/battery"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
48 'battery-linux-proc-acpi))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 "*Function for getting battery status information.
36090
9ed7c3f74e35 (battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents: 32356
diff changeset
50 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
51 Its cons cells are of the form
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 (CONVERSION . REPLACEMENT-TEXT)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 CONVERSION is the character code of a \"conversion specification\"
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
56 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
57 :type '(choice (const nil) function)
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
58 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
60 (defcustom battery-echo-area-format
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 (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
62 "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
63 ((eq battery-status-function 'battery-linux-proc-acpi)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
64 "Power %L, battery %B at %r mA (%p%% load, remaining time %t)"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 "*Control string formatting the string to display in the echo area.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 Ordinary characters in the control string are printed as-is, while
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 conversion specifications introduced by a `%' character in the control
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 string are substituted as defined by the current value of the variable
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
69 `battery-status-function'."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
70 :type '(choice string (const nil))
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
71 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 (defvar battery-mode-line-string nil
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 "String to display in the mode line.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
76 (defcustom battery-mode-line-format
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 (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
78 " [%b%p%%]")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
79 ((eq battery-status-function 'battery-linux-proc-acpi)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
80 " [%b%p%%,%d°C]"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 "*Control string formatting the string to display in the mode line.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 Ordinary characters in the control string are printed as-is, while
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 conversion specifications introduced by a `%' character in the control
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 string are substituted as defined by the current value of the variable
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
85 `battery-status-function'."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
86 :type '(choice string (const nil))
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
87 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
89 (defcustom battery-update-interval 60
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
90 "*Seconds after which the battery status will be updated."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
91 :type 'integer
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
92 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 (defvar battery-update-timer nil
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 "Interval timer object.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96
21076
dc82196eac58 Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents: 20972
diff changeset
97 ;;;###autoload
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 (defun battery ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 "Display battery status information in the echo area.
26828
983f253505d0 (battery): Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents: 21317
diff changeset
100 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
101 `battery-echo-area-format' and `battery-status-function'."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 (interactive)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 (message "%s" (if (and battery-echo-area-format battery-status-function)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 (battery-format battery-echo-area-format
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 (funcall battery-status-function))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 "Battery status not available")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107
21076
dc82196eac58 Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents: 20972
diff changeset
108 ;;;###autoload
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 (defun display-battery ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 "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
111 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
112 `battery-mode-line-format' and `battery-status-function'.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 The mode line will be updated automatically every `battery-update-interval'
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 seconds."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 (interactive)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 (setq battery-mode-line-string "")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 (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
118 (add-to-list 'global-mode-string 'battery-mode-line-string t)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 (and battery-update-timer (cancel-timer battery-update-timer))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 (setq battery-update-timer (run-at-time nil battery-update-interval
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 'battery-update-handler))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 (battery-update))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 (defun battery-update-handler ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (battery-update)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 (sit-for 0))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 (defun battery-update ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 "Update battery status information in the mode line."
38292
1c462d61083a (battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents: 36090
diff changeset
130 (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
131 battery-status-function)
1c462d61083a (battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents: 36090
diff changeset
132 (battery-format
1c462d61083a (battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents: 36090
diff changeset
133 battery-mode-line-format
1c462d61083a (battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents: 36090
diff changeset
134 (funcall battery-status-function))
1c462d61083a (battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents: 36090
diff changeset
135 "")
1c462d61083a (battery-update): Add help-echo.
Gerd Moellmann <gerd@gnu.org>
parents: 36090
diff changeset
136 'help-echo "Battery status information"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 (force-mode-line-update))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 ;;; `/proc/apm' interface for Linux.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 (defconst battery-linux-proc-apm-regexp
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 (concat "^\\([^ ]+\\)" ; Driver version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 " \\([^ ]+\\)" ; APM BIOS version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 " 0x\\([0-9a-f]+\\)" ; AC line status.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 " 0x\\([0-9a-f]+\\)" ; Battery status.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 " 0x\\([0-9a-f]+\\)" ; Battery flags.
20972
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
149 " \\(-?[0-9]+\\)%" ; Load percentage.
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
150 " \\(-?[0-9]+\\)" ; Remaining time.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 " \\(.*\\)" ; Time unit.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 "$")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 "Regular expression matching contents of `/proc/apm'.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 (defun battery-linux-proc-apm ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 "Get APM status information from Linux kernel.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 This function works only with the new `/proc/apm' format introduced
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 in Linux version 1.3.58.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 The following %-sequences are provided:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 %v Linux driver version
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 %V APM BIOS version
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 %I APM BIOS status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 %L AC line status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 %B Battery status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 %b Battery status, empty means high, `-' means low,
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 `!' means critical, and `+' means charging
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 %p battery load percentage
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 %s Remaining time in seconds
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 %m Remaining time in minutes
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 %h Remaining time in hours
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 %t Remaining time in the form `h:min'"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 (let (driver-version bios-version bios-interface line-status
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 battery-status battery-status-symbol load-percentage
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 seconds minutes hours remaining-time buffer tem)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (unwind-protect
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 (save-excursion
21317
566100bc6eef (battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents: 21088
diff changeset
178 (setq buffer (get-buffer-create " *battery*"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (set-buffer buffer)
21317
566100bc6eef (battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents: 21088
diff changeset
180 (erase-buffer)
41919
842512c9651c (battery-insert-file-contents): Obsolete function removed.
Noah Friedman <friedman@splode.com>
parents: 38737
diff changeset
181 (insert-file-contents "/proc/apm")
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 (re-search-forward battery-linux-proc-apm-regexp)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 (setq driver-version (match-string 1))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 (setq bios-version (match-string 2))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 (setq tem (battery-hex-to-int-2 (match-string 3)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 (if (not (logand tem 2))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (setq bios-interface "not supported")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 (setq bios-interface "enabled")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (cond ((logand tem 16) (setq bios-interface "disabled"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 ((logand tem 32) (setq bios-interface "disengaged")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 (setq tem (battery-hex-to-int-2 (match-string 4)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 (cond ((= tem 0) (setq line-status "off-line"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 ((= tem 1) (setq line-status "on-line"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 ((= tem 2) (setq line-status "on backup")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 (setq tem (battery-hex-to-int-2 (match-string 6)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 (if (= tem 255)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 (setq battery-status "N/A")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 (setq tem (battery-hex-to-int-2 (match-string 5)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 (cond ((= tem 0) (setq battery-status "high"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 battery-status-symbol ""))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 ((= tem 1) (setq battery-status "low"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 battery-status-symbol "-"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 ((= tem 2) (setq battery-status "critical"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 battery-status-symbol "!"))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 ((= tem 3) (setq battery-status "charging"
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 battery-status-symbol "+")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (setq load-percentage (match-string 7))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 (setq seconds (string-to-number (match-string 8)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 (and (string-equal (match-string 9) "min")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 (setq seconds (* 60 seconds)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 (setq minutes (/ seconds 60)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 hours (/ seconds 3600))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 (setq remaining-time
21317
566100bc6eef (battery-linux-proc-apm): Re-use the temporary buffer.
Richard M. Stallman <rms@gnu.org>
parents: 21088
diff changeset
214 (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
215 (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
216 (cons ?V (or bios-version "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
217 (cons ?I (or bios-interface "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
218 (cons ?L (or line-status "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
219 (cons ?B (or battery-status "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
220 (cons ?b (or battery-status-symbol ""))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
221 (cons ?p (or load-percentage "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
222 (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
223 (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
224 (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
225 (cons ?t (or remaining-time "N/A")))))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
228 ;;; `/proc/acpi/' interface for Linux.
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
229
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
230 (defun battery-linux-proc-acpi ()
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
231 "Get ACPI status information from Linux kernel.
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
232 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
233 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
234
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
235 The following %-sequences are provided:
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
236 %c Current capacity (mAh)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
237 %B Battery status (verbose)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
238 %b Battery status, empty means high, `-' means low,
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
239 `!' means critical, and `+' means charging
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
240 %d Temperature (in degrees Celsius)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
241 %L AC line status (verbose)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
242 %p battery load percentage
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
243 %m Remaining time in minutes
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
244 %h Remaining time in hours
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
245 %t Remaining time in the form `h:min'"
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
246 (let (capacity design-capacity rate charging-state warn low minutes hours)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
247 (when (file-directory-p "/proc/acpi/battery/")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
248 ;; ACPI provides information about each battery present in the system in
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
249 ;; a separate subdirectory. We are going to merge the available
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
250 ;; information together since displaying for a variable amount of
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
251 ;; batteries seems overkill for format-strings.
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
252 (mapc
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
253 (lambda (dir)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
254 (with-temp-buffer
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
255 (insert-file-contents (expand-file-name "state" dir))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
256 (when (re-search-forward "present: +yes$" nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
257 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
258 (or (null charging-state) (string= charging-state
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
259 "unknown"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
260 ;; On most multi-battery systems, most of the time only one
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
261 ;; battery is "charging"/"discharging", the others are
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
262 ;; "unknown".
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
263 (setq charging-state (match-string 1)))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
264 (when (re-search-forward "present rate: +\\([0-9]+\\) mA$" nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
265 (setq rate (+ (or rate 0) (string-to-int (match-string 1)))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
266 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) mAh$"
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
267 nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
268 (setq capacity
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
269 (+ (or capacity 0) (string-to-int (match-string 1))))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
270 (goto-char (point-max))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
271 (insert-file-contents (expand-file-name "info" dir))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
272 (when (re-search-forward "present: +yes$" nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
273 (when (re-search-forward "design capacity: +\\([0-9]+\\) mAh$"
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
274 nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
275 (setq design-capacity (+ (or design-capacity 0)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
276 (string-to-int (match-string 1)))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
277 (when (re-search-forward "design capacity warning: +\\([0-9]+\\) mAh$"
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
278 nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
279 (setq warn (+ (or warn 0) (string-to-int (match-string 1)))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
280 (when (re-search-forward "design capacity low: +\\([0-9]+\\) mAh$"
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
281 nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
282 (setq low (+ (or low 0)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
283 (string-to-int (match-string 1))))))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
284 (directory-files "/proc/acpi/battery/" t "BAT")))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
285 (and capacity rate
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
286 (setq minutes (floor (* (/ (float (if (string= charging-state
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
287 "charging")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
288 (- design-capacity capacity)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
289 capacity)) rate) 60))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
290 hours (/ minutes 60)))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
291 (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
292 (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
293 (with-temp-buffer
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
294 (insert-file-contents
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
295 "/proc/acpi/ac_adapter/AC/state")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
296 (when (re-search-forward "state: +\\(.*\\)$" nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
297 (match-string 1))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
298 "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
299 (cons ?d (or (when (file-exists-p
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
300 "/proc/acpi/thermal_zone/THRM/temperature")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
301 (with-temp-buffer
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
302 (insert-file-contents
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
303 "/proc/acpi/thermal_zone/THRM/temperature")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
304 (when (re-search-forward
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
305 "temperature: +\\([0-9]+\\) C$" nil t)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
306 (match-string 1))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
307 "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
308 (cons ?r (or (and rate (number-to-string rate)) "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
309 (cons ?B (or charging-state "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
310 (cons ?b (or (and (string= charging-state "charging") "+")
52470
b4d15685afef 2003-09-10 Mario Lang <mlang@delysid.org>
Alex Schroeder <alex@gnu.org>
parents: 52450
diff changeset
311 (and low (< capacity low) "!")
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
312 (and warn (< capacity warn) "-")
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
313 ""))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
314 (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
315 (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
316 (cons ?t (or (and minutes
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
317 (format "%d:%02d" hours (- minutes (* 60 hours))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
318 "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
319 (cons ?p (or (and design-capacity capacity
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
320 (number-to-string
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
321 (floor (/ capacity
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
322 (/ (float design-capacity) 100)))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
323 "N/A")))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
324
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
325
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 ;;; Private functions.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 (defun battery-format (format alist)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 "Substitute %-sequences in FORMAT."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 (let ((index 0)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 (length (length format))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 (result "")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 char flag elem)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 (while (< index length)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 (setq char (aref format index))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (if (not flag)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 (if (char-equal char ?%)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (setq flag t)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 (setq result (concat result (char-to-string char))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 (cond ((char-equal char ?%)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 (setq result (concat result "%")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 ((setq elem (assoc char alist))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 (setq result (concat result (cdr elem)))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (setq flag nil))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 (setq index (1+ index)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 (or (null flag)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 (setq result (concat result "%")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 result))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 (defconst battery-hex-map '((?0 . 0) (?1 . 1) (?2 . 2) (?3 . 3)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 (?4 . 4) (?5 . 5) (?6 . 6) (?7 . 7)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 (?8 . 8) (?9 . 9) (?a . 10) (?b . 11)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 (?c . 12) (?d . 13) (?e . 14) (?f . 15)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 (defun battery-hex-to-int (string)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 "Convert a hexadecimal number (a string) into a number."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 (save-match-data
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 (and (string-match "^[ \t]+" string)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 (setq string (substring string (match-end 0))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 (and (string-match "^0[xX]" string)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (setq string (substring string (match-end 0)))))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 (battery-hex-to-int-2 string))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 (defun battery-hex-to-int-2 (string)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 (let ((index 0)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 (length (length string))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 (value 0)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 (elem nil))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 (while (and (< index length)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370 (setq elem (assoc (downcase (aref string index))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 battery-hex-map)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 (setq value (+ (* 16 value) (cdr elem))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 index (1+ index)))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 value))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 (provide 'battery)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 41919
diff changeset
379 ;;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 ;;; battery.el ends here