1 ##############################################################################
3 ## $Id: apm.py,v 1.2 2003/04/10 22:13:55 riemer Exp $
5 ## Copyright (C) 2002-2003 Tilo Riemer <riemer@lincvs.org>
6 ## All rights reserved.
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following conditions
12 ## 1. Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## 2. Redistributions in binary form must reproduce the above copyright
15 ## notice, this list of conditions and the following disclaimer in the
16 ## documentation and/or other materials provided with the distribution.
17 ## 3. The name of the author may not be used to endorse or promote products
18 ## derived from this software without specific prior written permission.
20 ## THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ## OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ## IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ## INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ###############################################################################
41 CHARGING
= 2 #implies ONLINE
47 """Interface class for APM"""
51 if res
.find("freebsd4") > -1:
52 self
.apm
= ApmGeneric() #we use apm_lowlevel
54 elif res
.find("netbsd1") > -1:
55 self
.apm
= ApmGeneric() #we use apm_lowlevel
57 elif res
.find("linux2") > -1:
60 elif res
.find("darwin") > -1:
61 self
.apm
= ApmGeneric() #defined in apm_lowlevel
64 self
.apm
= None #throw exception (os unknown)
69 """Updates the APM state"""
74 """Returns percentage capacity of all batteries"""
75 return self
.apm
.percent()
79 """Returns time of all batteries (in minutes)"""
80 return self
.apm
.time()
83 def charging_state(self
):
84 """Returns ac state (off-/online/charging)"""
85 return self
.apm
.charging_state()
91 #base class for os dependent apm classes
95 self
.ac_line_state
= OFFLINE
98 self
.life_time
= 0 #0 seconds
100 #initial reading of apm info
104 #returns percentage capacity of all batteries
105 return self
.life_percent
109 #returns time of all batteries (in minutes)
110 return self
.life_time
113 def charging_state(self
):
114 return self
.ac_line_state
120 #implementation using apm_lowlevel.py
121 #there are implementations of apmLowlevel.py for NetBSD 1.6 and
122 #soon for FreeBSD 4 at the moment
124 class ApmGeneric(ApmBase
):
128 self
.apm_lowlevel
= apm_lowlevel
132 self
.ac_line_state
= OFFLINE
134 self
.life_percent
= 0
135 self
.life_time
= 0 #0 seconds
137 #initial reading of apm info
142 apm_info
= self
.apm_lowlevel
.state()
144 if (apm_info
[0] < 0):
145 raise EnvironmentError
147 self
.life_percent
= apm_info
[1]
148 self
.life_time
= apm_info
[2]
149 self
.ac_line_state
= apm_info
[3]
155 #implementation for Linux
156 #if we want support more than battery info we need a low level module implemented using C
158 class ApmLinux(ApmBase
):
160 #read /proc/apm and extract needed infos
163 apm_proc
= open("/proc/apm")
165 raise EnvironmentError
168 line
= apm_proc
.readline()
170 if token
[3] == "0x00":
171 self
.ac_line_state
= OFFLINE
172 elif token
[4] == "0x03":
173 self
.ac_line_state
= CHARGING
175 self
.ac_line_state
= ONLINE
177 self
.life_percent
= int(token
[6].split("%")[0])
179 self
.life_time
= int(token
[7])