Updated the ChangeLog
[rox-lithium.git] / apm.py
blob41fd971159226bf847065e9460672e47de94f808
1 ##############################################################################
2 ##
3 ## $Id: apm.py,v 1.2 2003/04/10 22:13:55 riemer Exp $
4 ##
5 ## Copyright (C) 2002-2003 Tilo Riemer <riemer@lincvs.org>
6 ## All rights reserved.
7 ##
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following conditions
10 ## are met:
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 ###############################################################################
35 import sys
38 #enums
39 OFFLINE = 0
40 ONLINE = 1
41 CHARGING = 2 #implies ONLINE
42 NOBATTERY = 3
44 UNKNOWN = -1
46 class Apm:
47 """Interface class for APM"""
49 def __init__(self):
50 res = sys.platform
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:
58 self.apm = ApmLinux()
60 elif res.find("darwin") > -1:
61 self.apm = ApmGeneric() #defined in apm_lowlevel
63 else:
64 self.apm = None #throw exception (os unknown)
65 raise NotImplemented
68 def update(self):
69 """Updates the APM state"""
70 self.apm.update()
73 def percent(self):
74 """Returns percentage capacity of all batteries"""
75 return self.apm.percent()
78 def time(self):
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
93 class ApmBase:
94 def __init__(self):
95 self.ac_line_state = OFFLINE
97 self.life_percent = 0
98 self.life_time = 0 #0 seconds
100 #initial reading of apm info
101 self.update()
103 def percent(self):
104 #returns percentage capacity of all batteries
105 return self.life_percent
108 def time(self):
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):
125 def __init__(self):
126 try:
127 import apm_lowlevel
128 self.apm_lowlevel = apm_lowlevel
129 except ImportError:
130 raise
132 self.ac_line_state = OFFLINE
134 self.life_percent = 0
135 self.life_time = 0 #0 seconds
137 #initial reading of apm info
138 self.update()
141 def update(self):
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):
159 def update(self):
160 #read /proc/apm and extract needed infos
162 try:
163 apm_proc = open("/proc/apm")
164 except IOError:
165 raise EnvironmentError
168 line = apm_proc.readline()
169 token = line.split()
170 if token[3] == "0x00":
171 self.ac_line_state = OFFLINE
172 elif token[4] == "0x03":
173 self.ac_line_state = CHARGING
174 else:
175 self.ac_line_state = ONLINE
177 self.life_percent = int(token[6].split("%")[0])
179 self.life_time = int(token[7])
182 apm_proc.close()