Fix for incorrect charge level in acpi (Thomas Leonard)
[rox-lithium.git] / battery.py
blobcf0f424350b0559badf81f3d2469ac6b8e879952
1 """
2 battery.py - A Battery Status Monitor for ROX
3 (based largely on Baroque by Tilo Riemer)
4 """
6 ############################################################################
7 ##
8 ## $Id: baroque.py,v 1.13 2004/01/08 04:26:11 rds Exp $
9 ##
10 ## Copyright (C) 2002-2003 Rds <rds@rdsarts.com> and
11 ## Tilo Riemer <riemer@lincvs.org>
12 ## All rights reserved.
14 ## Baroque is a merge of BatMonitor and the old Baroque
16 ## Redistribution and use in source and binary forms, with or without
17 ## modification, are permitted provided that the following conditions
18 ## are met:
20 ## 1. Redistributions of source code must retain the above copyright
21 ## notice, this list of conditions and the following disclaimer.
22 ## 2. Redistributions in binary form must reproduce the above copyright
23 ## notice, this list of conditions and the following disclaimer in the
24 ## documentation and/or other materials provided with the distribution.
25 ## 3. The name of the author may not be used to endorse or promote products
26 ## derived from this software without specific prior written permission.
28 ## THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
29 ## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 ## OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 ## IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 ## INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ###############################################################################
41 # standard library modules
42 import sys, os, time, gtk, gobject, rox
43 from rox import applet, filer, tasks
44 from rox.options import Option
46 # globals
47 APP_NAME = 'Battery'
48 APP_DIR = rox.app_dir
49 APP_SIZE = [28, 28]
51 # Options.xml processing
52 from rox import Menu
53 rox.setup_app_options(APP_NAME, site='hayber.us')
54 Menu.set_save_name(APP_NAME, site='hayber.us')
56 #Options go here
57 WARN = Option('warn', True)
58 WARN_LEVEL = Option('warn_level', 10)
59 TIMER = Option('timeout', "1000")
61 #Enable notification of options changes
62 rox.app_options.notify()
64 BATT_TYPE = -1 # 0 = ACPI, 1 = APM
66 # Initalize the battery object
67 try:
68 import acpi
69 BATTERY = acpi.Acpi()
70 BATT_TYPE = 0
71 OFFLINE = acpi.OFFLINE
72 except (ImportError, NotImplementedError, EnvironmentError):
73 try:
74 import pmu #for PowerMac support
75 BATTERY = pmu.Pmu()
76 BATT_TYPE = 1
77 OFFLINE = pmu.OFFLINE
78 except (ImportError, NotImplementedError, EnvironmentError):
79 try:
80 import apm
81 BATTERY = apm.Apm()
82 BATT_TYPE = 1
83 OFFLINE = apm.OFFLINE
84 except (ImportError, NotImplementedError, EnvironmentError):
85 rox.croak(_("Sorry, but we could not load a Power Management module. Your system is not configured with power management support."))
88 class Battery(applet.Applet):
89 """A Battery Status Monitor Applet"""
91 warned = False
92 msg = 0
93 vertical = False
95 images = []
97 def __init__(self, id):
98 """Initialize applet."""
99 applet.Applet.__init__(self, id)
101 # load the applet icon
102 self.image = gtk.Image()
104 for name in [
105 'battery0', 'battery1', 'battery2', 'battery3', 'battery4',
106 'battery5', 'battery6', 'battery7', 'battery8', 'battery9',
107 'battery10'
109 self.images.append(gtk.gdk.pixbuf_new_from_file(os.path.join(rox.app_dir, 'images', name+'.svg')))
111 self.pixbuf = self.images[0]
112 self.image.set_from_pixbuf(self.pixbuf)
113 self.resize_image(8)
114 self.add(self.image)
116 self.vertical = self.get_panel_orientation() in ('Right', 'Left')
117 if self.vertical:
118 self.set_size_request(8, -1)
119 else:
120 self.set_size_request(-1, 8)
122 # set the tooltip
123 self.tooltips = gtk.Tooltips()
125 # menus
126 self.build_appmenu()
128 # event handling
129 self.add_events(gtk.gdk.BUTTON_PRESS_MASK)
130 self.connect('button-press-event', self.button_press)
131 self.connect('size-allocate', self.resize)
132 self.connect('delete_event', self.quit)
133 rox.app_options.add_notify(self.get_options)
135 gobject.timeout_add(int(TIMER.int_value) * 100, self.update_display, BATTERY)
136 gobject.timeout_add(5000, self.update_tooltip)
138 self.update_display(BATTERY)
139 self.update_tooltip()
140 self.show()
142 def update_display(self, BATTERY):
143 """Updates all the parts of our applet, and cleans it up if needed."""
144 BATTERY.update()
145 percent = BATTERY.percent()
147 if WARN.value == 'True':
148 if (BATTERY.charging_state() == OFFLINE and percent <= WARN_LEVEL.int_value):
149 if self.warned == False:
150 rox.info(_("Warning. Battery is currently at %d%%") % (BATTERY.percent(),))
151 self.warned = True
152 else:
153 self.warned = False
155 if BATTERY.charging_state():
156 pb = self.images[0]
157 else:
158 index = min(percent/10+1, 10)
159 pb = self.images[index]
160 self.pixbuf = pb
161 self.resize_image(self.size)
163 return 1 # to keep the timer going!
166 def update_tooltip(self):
167 self.tooltips.set_tip(self, self.status() + self.percent() + '%')
168 return 1 #to keep timer running
170 def status(self):
171 txt = _("Unknown")
172 BATTERY.update()
173 if BATTERY.charging_state() == 1:
174 txt = _("AC Online: ")
175 elif BATTERY.charging_state() == 2:
176 txt = _("Charging: ")
177 else:
178 # Discharing from the battery
179 if self.msg == 1:
180 self.msg = 0
181 txt = _("Battery: ")
182 else:
183 self.msg = 1
184 if BATT_TYPE == 1:
185 temp2 = BATTERY.time()
186 temp = int(temp2 / 60)
187 temp2 -= (temp * 60)
188 if temp < 0:
189 txt = _("Calculating... ")
190 else:
191 txt = "(%d:%02d) " % (temp, temp2)
192 else:
193 try:
194 temp = BATTERY.estimated_lifetime()
195 temp2 = int(60 * (temp - int(temp)))
196 txt = "(%d:%02d) " % (temp, temp2)
197 except ValueError:
198 txt = _("Charging")
199 return txt
201 def percent(self):
202 return str(BATTERY.percent())
205 def resize(self, widget, rectangle):
206 """Called when the panel sends a size."""
208 if self.vertical:
209 size = rectangle[2]
210 else:
211 size = rectangle[3]
212 if size != self.size:
213 self.resize_image(size)
215 def resize_image(self, size):
216 """Resize the application image."""
217 scaled_pixbuf = self.pixbuf.scale_simple(size, size, gtk.gdk.INTERP_BILINEAR)
218 self.image.set_from_pixbuf(scaled_pixbuf)
219 self.size = size
221 def button_press(self, window, event):
222 """Handle mouse clicks by popping up the matching menu."""
223 if event.button == 1:
224 self.run_it()
225 elif event.button == 2:
226 self.checkit()
227 elif event.button == 3:
228 self.appmenu.popup(self, event, self.position_menu)
230 def get_panel_orientation(self):
231 """ Return panel orientation and margin for displaying a popup menu.
232 Position in ('Top', 'Bottom', 'Left', 'Right').
234 pos = self.socket.property_get('_ROX_PANEL_MENU_POS', 'STRING', False)
235 if pos: pos = pos[2]
236 if pos:
237 side, margin = pos.split(',')
238 margin = int(margin)
239 else:
240 side, margin = None, 2
241 return side
243 def get_options(self, widget=None, rebuild=False, response=False):
244 """Used as the notify callback when options change."""
245 pass
247 def show_options(self, button=None):
248 """Open the options edit dialog."""
249 rox.edit_options()
251 def get_info(self):
252 """Display an InfoWin self."""
253 from rox import InfoWin
254 InfoWin.infowin(APP_NAME)
256 def build_appmenu(self):
257 """Build the right-click app menu."""
258 items = []
259 items.append(Menu.Action(_('Info...'), 'get_info', '', gtk.STOCK_DIALOG_INFO))
260 items.append(Menu.Action(_('Options...'), 'show_options', '', gtk.STOCK_PREFERENCES))
261 items.append(Menu.Separator())
262 items.append(Menu.Action(_('Close'), 'quit', '', gtk.STOCK_CLOSE))
263 self.appmenu = Menu.Menu('other', items)
264 self.appmenu.attach(self, self)
266 def quit(self, *args):
267 """Quit applet and close everything."""
268 self.destroy()