-
Notifications
You must be signed in to change notification settings - Fork 0
/
aerobat.py
executable file
·146 lines (114 loc) · 5.01 KB
/
aerobat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python2
'''
Aerobat - a simple battery application for the system tray
Copyright (C) 2014 Christian Zommerfelds
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import gtk
import math
import time
import gobject
import subprocess
# icon dimension
width = 12
height = 18
border = 1
# battery level thresholds for color changes
low_threshold = 0.3
critical_threshold = 0.1
# colors 0xRRGGBBAA
border_color = 0x000000ff # color of the border
background_color = 0x333333ff # color of the empty part of the battery
normal_color = 0x33ff00ff # color of the "energy" bar (normal state)
low_color = 0xffbf00ff # color of the "energy" bar (low state)
critical_color = 0xff0000ff # color of the "energy" bar (critical state)
disabled_color = 0xaaaaaaff # color of battery when no data is availlable
sleep = 5 # the number of seconds to sleep between every refresh
assert width > 2*border
assert height > 2*border
# Returns a tuple containing the battery level between 0.0 and 1.0 (or None) and a more verbose text
def get_battery_level():
p = subprocess.Popen(['acpi', '-b'], stdout=subprocess.PIPE)
text = "".join(p.stdout.readlines()).strip()
p.stdout.close()
i = text.find("%")
value = 0
if (i != -1):
try:
percent = int(text[i-3:i])
except ValueError:
value = None
else:
value = percent/100.0
else:
value = None
text = "No battery data availlable"
return value, text
def gen_pixbuf(color, w, h):
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True,
bits_per_sample=8, width=w, height=h)
pixbuf.fill(color)
return pixbuf
class Aerobat (object):
def __init__(self):
self.statusicon = gtk.StatusIcon()
while not self.statusicon.is_embedded():
gtk.main_iteration()
self.bkgr_pixbuf = gen_pixbuf(border_color, width, height)
tmp_pixbuf = gen_pixbuf(background_color, width-2*border, height-2*border)
tmp_pixbuf.copy_area(src_x=0, src_y=0, width=width-2*border, height=height-2*border,
dest_pixbuf=self.bkgr_pixbuf, dest_x=border, dest_y=border)
self.normal_pixbuf = gen_pixbuf(normal_color, width-2*border, height-2*border)
self.low_pixbuf = gen_pixbuf(low_color, width-2*border, height-2*border)
self.critical_pixbuf = gen_pixbuf(critical_color, width-2*border, height-2*border)
self.disabled_pixbuf = gen_pixbuf(border_color, width, height)
tmp_pixbuf = gen_pixbuf(disabled_color, width-2*border, height-2*border)
tmp_pixbuf.copy_area(src_x=0, src_y=0, width=width-2*border, height=height-2*border,
dest_pixbuf=self.disabled_pixbuf, dest_x=border, dest_y=border)
self.update()
gobject.timeout_add_seconds(sleep, self.update)
self.statusicon.connect("popup-menu", self.right_click_event)
def draw_battery(self, value):
if (value == None):
pixbuf = self.disabled_pixbuf.copy()
else:
pixbuf = self.bkgr_pixbuf.copy()
h = int(value*(height-2*border) + 0.5)
if h == 0:
h = 1 # the bar should always be visible
if value < low_threshold:
if value < critical_threshold:
color_pixbuf = self.critical_pixbuf
else:
color_pixbuf = self.low_pixbuf
else:
color_pixbuf = self.normal_pixbuf
color_pixbuf.copy_area(src_x=0, src_y=0, width=width-2*border, height=h,
dest_pixbuf=pixbuf, dest_x=border, dest_y=height-border-h)
self.statusicon.set_from_pixbuf(pixbuf)
def update(self):
value, text = get_battery_level()
self.statusicon.set_tooltip(text)
if value != None:
assert value >= 0.0 and value <= 1.0
self.draw_battery(value)
return True
def right_click_event(self, icon, button, time):
menu = gtk.Menu()
quit = gtk.MenuItem("Quit")
quit.connect("activate", gtk.main_quit)
menu.append(quit)
menu.show_all()
menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.statusicon)
if __name__ == "__main__":
tray = Aerobat()
gtk.main()