updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / awn-extras-applets / add-battery-upower-support.patch
blob0548df758daaac9b626b82d671cc67a3c769ea75
1 === modified file 'applets/maintained/battery/battery.py'
2 --- applets/maintained/battery/battery.py 2010-08-12 19:00:37 +0000
3 +++ applets/maintained/battery/battery.py 2010-09-12 22:24:21 +0000
4 @@ -1,6 +1,6 @@
5 #!/usr/bin/python
6 # Copyright (c) 2007 - 2008 Randal Barlow <im.tehk at gmail.com>
7 -# 2008 - 2009 onox <denkpadje@gmail.com>
8 +# 2008 - 2010 onox <denkpadje@gmail.com>
10 # This library is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public
12 @@ -223,11 +223,11 @@
13 return
15 charge_percentage = self.backend.get_capacity_percentage()
17 - charge_message = "Computer running on %s power" % ("AC", "battery")[self.backend.is_discharging()]
18 + power_type = "AC" if self.backend.is_on_ac_power() else "battery"
19 + charge_message = "Computer running on %s power" % power_type
21 if self.backend.is_charged():
22 - charge_message += "\nBattery charged"
23 + charge_message += "\n" + "Battery charged"
24 icon = os.path.join(themes_dir, self.settings["theme"], "battery-charged.svg")
25 else:
26 is_charging = self.backend.is_charging()
27 @@ -343,6 +343,148 @@
28 return self.get_remaining_capacity() <= self.get_warning_capacity()
31 +class UPowerBackend(AbstractBackend):
33 + """Backend that uses UPower via DBus.
35 + Reference: http://upower.freedesktop.org/docs/Device.html
37 + """
39 + device_iface_name = "org.freedesktop.UPower.Device"
41 + devices = None
43 + def __init__(self):
44 + self.__system_bus = dbus.SystemBus()
46 + self.udi = UPowerBackend.get_batteries().keys()[0]
47 + self.__set_dbus_interface(self.udi)
49 + @staticmethod
50 + def backend_useable():
51 + return dbus is not None and len(UPowerBackend.get_batteries()) > 0
53 + @staticmethod
54 + def get_batteries():
55 + udi_models = {}
57 + try:
58 + system_bus = dbus.SystemBus()
60 + proxy = system_bus.get_object("org.freedesktop.UPower", "/org/freedesktop/UPower")
61 + interface = dbus.Interface(proxy, "org.freedesktop.UPower")
63 + UPowerBackend.devices = list(interface.EnumerateDevices())
65 + for udi in UPowerBackend.devices:
66 + device_iface = UPowerBackend.__get_dbus_interface(system_bus, udi)
68 + # Type 2 is battery (1 is AC, 3 is UPS)
69 + device_type = int(UPowerBackend.__get_property(device_iface, "Type"))
70 + is_present = bool(UPowerBackend.__get_property(device_iface, "IsPresent"))
72 + if device_type == 2 and is_present:
73 + udi_models[str(udi)] = str(UPowerBackend.__get_property(device_iface, "Model"))
74 + except dbus.DBusException, e:
75 + print e.message
76 + finally:
77 + return udi_models
79 + def set_active_udi(self, udi):
80 + udi_models = UPowerBackend.get_batteries()
82 + assert udi in udi_models
84 + self.udi = udi
85 + self.__set_dbus_interface(udi)
87 + @staticmethod
88 + def __get_dbus_interface(system_bus, udi):
89 + proxy = system_bus.get_object("org.freedesktop.UPower", udi)
90 + return dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
92 + def __set_dbus_interface(self, udi):
93 + self.__battery = UPowerBackend.__get_dbus_interface(self.__system_bus, udi)
95 + @staticmethod
96 + def __get_property(interface, property_name):
97 + return interface.Get(UPowerBackend.device_iface_name, property_name)
99 + def get_active_udi(self):
100 + return self.udi
102 + def is_present(self):
103 + return bool(UPowerBackend.__get_property(self.__battery, "IsPresent"))
105 + def is_charging(self):
106 + # State 1 is charging
107 + return int(UPowerBackend.__get_property(self.__battery, "State")) == 1
109 + def is_discharging(self):
110 + # State 2 is discharging
111 + return int(UPowerBackend.__get_property(self.__battery, "State")) == 2
113 + def get_last_full_capacity(self):
114 + # Measured in Wh
115 + return int(UPowerBackend.__get_property(self.__battery, "EnergyFull"))
117 + def get_remaining_capacity(self):
118 + # Measured in Wh
119 + return int(UPowerBackend.__get_property(self.__battery, "Energy"))
121 + def get_charge_rate(self):
122 + # Measured in W. Original value positive if being discharged, negative if being charged
123 + return abs(int(UPowerBackend.__get_property(self.__battery, "EnergyRate")))
125 + def get_capacity_percentage(self):
126 + # Percentage in range 0 .. 100
127 + return int(UPowerBackend.__get_property(self.__battery, "Percentage"))
129 + def get_warning_capacity(self):
130 + # Measured in Wh
131 + return int(int(UPowerBackend.__get_property(self.__battery, "EnergyFullDesign")) * (warning_percentage / 100.))
133 + def get_charge_time(self):
134 + assert self.is_charging()
136 + # Charge time in seconds
137 + time = int(UPowerBackend.__get_property(self.__battery, "TimeToFull"))
139 + if time == 0:
140 + return super(UPowerBackend, self).get_charge_time()
142 + hours = time / 3600
143 + seconds = time % 3600
144 + return (hours, int(round(seconds / 60.0)))
146 + def get_remaining_time(self):
147 + assert self.is_discharging()
149 + # Discharge time in seconds
150 + time = int(UPowerBackend.__get_property(self.__battery, "TimeToEmpty"))
152 + if time == 0:
153 + return super(UPowerBackend, self).get_discharge_time()
155 + hours = time / 3600
156 + seconds = time % 3600
157 + return (hours, int(round(seconds / 60.0)))
159 + def is_charged(self):
160 + # State 4 is fully charged
161 + return int(UPowerBackend.__get_property(self.__battery, "State")) == 4
163 + def is_on_ac_power(self):
164 + for udi in UPowerBackend.devices:
165 + device_iface = UPowerBackend.__get_dbus_interface(self.__system_bus, udi)
167 + # Property "Online" only has a value for AC power device
168 + if bool(UPowerBackend.__get_property(device_iface, "Online")):
169 + return True
170 + return False
173 class HalBackend(AbstractBackend):
175 """Backend that uses HAL via DBus.
176 @@ -372,7 +514,7 @@
177 return udi_models
178 except dbus.DBusException, e:
179 print e.message
180 - return {}
181 + return udi_models
183 def set_active_udi(self, udi):
184 udi_models = HalBackend.get_batteries()
185 @@ -416,8 +558,12 @@
186 except dbus.DBusException:
187 return int(int(self.__hal_battery.GetProperty("battery.charge_level.design")) * (warning_percentage / 100.))
190 -backends = [HalBackend]
191 + def is_on_ac_power(self):
192 + # TODO Should check that _all_ batteries are not discharging
193 + return not self.is_discharging()
196 +backends = [UPowerBackend, HalBackend]
199 if __name__ == "__main__":